Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
handle unknown sections
Browse files Browse the repository at this point in the history
  • Loading branch information
mangas committed May 21, 2022
1 parent 1080066 commit 9cc9ca2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
Binary file added res/cases/v1/global_section.wasm
Binary file not shown.
8 changes: 1 addition & 7 deletions src/builder/table.rs
Expand Up @@ -3,7 +3,7 @@ use crate::elements;
use alloc::vec::Vec;

/// Table definition
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Default)]
pub struct TableDefinition {
/// Minimum length
pub min: u32,
Expand Down Expand Up @@ -79,9 +79,3 @@ where
self.callback.invoke(self.table)
}
}

impl Default for TableDefinition {
fn default() -> Self {
TableDefinition { min: 0, max: None, elements: Vec::new() }
}
}
28 changes: 28 additions & 0 deletions src/elements/module.rs
Expand Up @@ -904,6 +904,34 @@ mod integration_tests {
assert!(found_section, "Name section should be present in dedicated example");
}

#[test]
fn names_with_global_section() {
let module = deserialize_file("./res/cases/v1/global_section.wasm")
.expect("Should be deserialized")
.parse_names()
.expect("Names to be parsed");

let mut found_section = false;
for section in module.sections() {
if let Section::Name(ref name_section) = *section {
let function_name_subsection =
name_section.functions().expect("function_name_subsection should be present");
assert_eq!(
function_name_subsection.names().get(0).expect("Should be entry #0"),
"~lib/builtins/abort"
);
assert_eq!(
function_name_subsection.names().get(11).expect("Should be entry #0"),
"~lib/typedarray/Uint8Array#__set"
);

found_section = true;
}
}

assert!(found_section, "Name section should be present in dedicated example");
}

// This test fixture has FLAG_SHARED so it depends on atomics feature.
#[test]
fn shared_memory_flag() {
Expand Down
15 changes: 10 additions & 5 deletions src/elements/name_section.rs
Expand Up @@ -73,31 +73,36 @@ impl NameSection {
while let Ok(raw_subsection_type) = VarUint7::deserialize(rdr) {
let subsection_type = raw_subsection_type.into();
// deserialize the section size
VarUint32::deserialize(rdr)?;
let size: usize = VarUint32::deserialize(rdr)?.into();

match subsection_type {
NAME_TYPE_MODULE => {
if module_name.is_some() {
return Err(Error::DuplicatedNameSubsections(NAME_TYPE_FUNCTION))
return Err(Error::DuplicatedNameSubsections(NAME_TYPE_FUNCTION));
}
module_name = Some(ModuleNameSubsection::deserialize(rdr)?);
},

NAME_TYPE_FUNCTION => {
if function_names.is_some() {
return Err(Error::DuplicatedNameSubsections(NAME_TYPE_FUNCTION))
return Err(Error::DuplicatedNameSubsections(NAME_TYPE_FUNCTION));
}
function_names = Some(FunctionNameSubsection::deserialize(module, rdr)?);
},

NAME_TYPE_LOCAL => {
if local_names.is_some() {
return Err(Error::DuplicatedNameSubsections(NAME_TYPE_LOCAL))
return Err(Error::DuplicatedNameSubsections(NAME_TYPE_LOCAL));
}
local_names = Some(LocalNameSubsection::deserialize(module, rdr)?);
},

_ => return Err(Error::UnknownNameSubsectionType(subsection_type)),
_ => {
// Consume the entire subsection size and drop it. This allows other sections to still be
// consumed if there are any.
let mut buf = vec![0; size];
rdr.read(&mut buf)?;
},
};
}

Expand Down

0 comments on commit 9cc9ca2

Please sign in to comment.