From 8770cafbb6ef8800e51dcd71b5737c3f83c6781c Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Fri, 28 Dec 2018 17:31:09 -0800 Subject: [PATCH 1/4] Fix section ordering when adding a Start section Fixes #259 --- res/cases/v1/start_add.wasm | Bin 0 -> 180 bytes src/elements/module.rs | 24 ++++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 res/cases/v1/start_add.wasm diff --git a/res/cases/v1/start_add.wasm b/res/cases/v1/start_add.wasm new file mode 100644 index 0000000000000000000000000000000000000000..8e14a068f2111df688e8e74242d1ce016b723516 GIT binary patch literal 180 zcmYj|K?=e!6h!AIrX>x9f(yZw;?_&ly=Q161{9K}G*xjGkFIGpF8=U%GaouQ0zezl zQ>54<9Wp1u&VrlaDZ2472dolT&Nx54PdXSLk&K-6#hO1+>(); + let sorted_sections = { + let mut s = sections.clone(); + s.sort(); + s + }; + assert_eq!(sections, sorted_sections, "Sections must be correctly sorted by id"); + } } From b91a0aa14014cdc9f13ba5c53489f28ff9e9489a Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Fri, 28 Dec 2018 17:36:15 -0800 Subject: [PATCH 2/4] Assert the exact order of sections, to be clear about what's actually being tested --- src/elements/module.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/elements/module.rs b/src/elements/module.rs index a41e65d..beb2e7a 100644 --- a/src/elements/module.rs +++ b/src/elements/module.rs @@ -768,11 +768,6 @@ mod integration_tests { assert_eq!(module.start_section().expect("Did not find any start section"), 0); let sections = module.sections().iter().map(|s| s.id()).collect::>(); - let sorted_sections = { - let mut s = sections.clone(); - s.sort(); - s - }; - assert_eq!(sections, sorted_sections, "Sections must be correctly sorted by id"); + assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 10, 11]); } } From a39ab02b5cda994a229f518c83fc3d2e992602e4 Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Fri, 28 Dec 2018 18:17:28 -0800 Subject: [PATCH 3/4] Add test involving custom section, fix the ensuing bug --- src/elements/module.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/elements/module.rs b/src/elements/module.rs index beb2e7a..3256d1b 100644 --- a/src/elements/module.rs +++ b/src/elements/module.rs @@ -244,18 +244,18 @@ impl Module { } /// Changes the module's start section. - pub fn set_start_section(&mut self, new_start : u32) { - let mut insert_after = 0; - for (i, section) in self.sections_mut().iter_mut().enumerate() { + pub fn set_start_section(&mut self, new_start: u32) { + for section in self.sections_mut().iter_mut() { if let &mut Section::Start(_sect) = section { *section = Section::Start(new_start); return } - if section.id() < 0x8 { - insert_after = i; - } } - self.sections_mut().insert(insert_after + 1, Section::Start(new_start)); + let insert_before = self.sections().iter().enumerate() + .filter_map(|(i, s)| if s.id() > 0x8 { Some(i) } else { None }) + .next() + .unwrap_or(0); + self.sections_mut().insert(insert_before, Section::Start(new_start)); } /// Removes the module's start section. @@ -770,4 +770,19 @@ mod integration_tests { let sections = module.sections().iter().map(|s| s.id()).collect::>(); assert_eq!(sections, vec![1, 2, 3, 6, 7, 8, 9, 10, 11]); } + + #[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]); + + assert!(module.start_section().is_none()); + 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, 0]); + } } From baeb1950c52745512e10e686cb9a454bba11cee7 Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Fri, 28 Dec 2018 18:59:00 -0800 Subject: [PATCH 4/4] Add start_add_custom.wasm test file, with a custom section --- res/cases/v1/start_add_custom.wasm | Bin 0 -> 202 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 res/cases/v1/start_add_custom.wasm diff --git a/res/cases/v1/start_add_custom.wasm b/res/cases/v1/start_add_custom.wasm new file mode 100644 index 0000000000000000000000000000000000000000..90a719d323e57794129327ebce3ed279b1ca612d GIT binary patch literal 202 zcmYj|%?g4*6ot>7@dp)2CpZ_Y6AOK*Ee8&RDpb9=+5}i+lL^&bbg)69C$W zHF@$i=EKUfV7HLV?iE)3=q!SnQ!TX_rmL`$d(0+r;;!VMh}H?ihb`YrPd&gHv8dpl zN@Q!`M{z=7rA6C;5<;3#j57)xvTREz^DKW#rOf5{HiLScu-oXV8Lczg%Eq;(BgTU- D1jR1^ literal 0 HcmV?d00001