diff --git a/src/elements/module.rs b/src/elements/module.rs index 5cc3385bd..13e20ab85 100644 --- a/src/elements/module.rs +++ b/src/elements/module.rs @@ -253,7 +253,7 @@ impl Module { } } let insert_before = self.sections().iter().enumerate() - .filter_map(|(i, s)| if s.id() > 0x8 { Some(i) } else { None }) + .filter_map(|(i, s)| if s.order() > 0x8 { Some(i) } else { None }) .next() .unwrap_or(0); self.sections_mut().insert(insert_before, Section::Start(new_start)); @@ -482,20 +482,20 @@ impl Deserialize for Module { return Err(Error::UnsupportedVersion(version)); } - let mut last_section_id = 0; + let mut last_section_order = 0; loop { match Section::deserialize(reader) { Err(Error::UnexpectedEof) => { break; }, Err(e) => { return Err(e) }, Ok(section) => { - if section.id() != 0 { - if last_section_id > section.id() { + if section.order() != 0 { + if last_section_order > section.order() { return Err(Error::SectionsOutOfOrder); - } else if last_section_id == section.id() { - return Err(Error::DuplicatedSections(last_section_id)); + } else if last_section_order == section.order() { + return Err(Error::DuplicatedSections(last_section_order)); } - last_section_id = section.id(); + last_section_order = section.order(); } sections.push(section); } @@ -837,22 +837,22 @@ mod integration_tests { module.set_start_section(0); assert_eq!(module.start_section().expect("Did not find any start section"), 0); - let sections = module.sections().iter().map(|s| s.id()).collect::>(); - assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 10, 11]); + let sections = module.sections().iter().map(|s| s.order()).collect::>(); + assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 11, 12]); } #[test] fn add_start_custom() { let mut module = deserialize_file("./res/cases/v1/start_add_custom.wasm").expect("failed to deserialize"); - let sections = module.sections().iter().map(|s| s.id()).collect::>(); - assert_eq!(sections, vec![1, 2, 3, 6, 7, 9, 10, 11, 0]); + let sections = module.sections().iter().map(|s| s.order()).collect::>(); + assert_eq!(sections, vec![1, 2, 3, 6, 7, 9, 11, 12, 0]); assert!(module.start_section().is_none()); module.set_start_section(0); - assert_eq!(module.start_section().expect("Did not find any start section"), 0); + assert_eq!(module.start_section().expect("Dorder not find any start section"), 0); - let sections = module.sections().iter().map(|s| s.id()).collect::>(); - assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 10, 11, 0]); + let sections = module.sections().iter().map(|s| s.order()).collect::>(); + assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 11, 12, 0]); } } diff --git a/src/elements/section.rs b/src/elements/section.rs index 7776fb390..fe140520e 100644 --- a/src/elements/section.rs +++ b/src/elements/section.rs @@ -60,6 +60,8 @@ pub enum Section { Start(u32), /// Elements section. Element(ElementSection), + /// Number of passive data entries in the data section + DataCount(u32), /// Function bodies section. Code(CodeSection), /// Data definition section. @@ -127,6 +129,12 @@ impl Deserialize for Section { 11 => { Section::Data(DataSection::deserialize(reader)?) }, + 12 => { + let mut section_reader = SectionReader::new(reader)?; + let count = VarUint32::deserialize(&mut section_reader)?; + section_reader.close()?; + Section::DataCount(count.into()) + }, invalid_id => { return Err(Error::InvalidSectionId(invalid_id)) }, @@ -182,6 +190,12 @@ impl Serialize for Section { VarUint32::from(index).serialize(&mut counted_writer)?; counted_writer.done()?; }, + Section::DataCount(count) => { + VarUint7::from(0x0c).serialize(writer)?; + let mut counted_writer = CountedWriter::new(writer); + VarUint32::from(count).serialize(&mut counted_writer)?; + counted_writer.done()?; + }, Section::Element(element_section) => { VarUint7::from(0x09).serialize(writer)?; element_section.serialize(writer)?; @@ -212,7 +226,7 @@ impl Serialize for Section { } impl Section { - pub(crate) fn id(&self) -> u8 { + pub(crate) fn order(&self) -> u8 { match *self { Section::Custom(_) => 0x00, Section::Unparsed { .. } => 0x00, @@ -225,8 +239,9 @@ impl Section { Section::Export(_) => 0x7, Section::Start(_) => 0x8, Section::Element(_) => 0x9, - Section::Code(_) => 0x0a, - Section::Data(_) => 0x0b, + Section::DataCount(_) => 0x0a, + Section::Code(_) => 0x0b, + Section::Data(_) => 0x0c, Section::Name(_) => 0x00, Section::Reloc(_) => 0x00, }