/src/wasm-tools/crates/wasm-mutate/src/mutators/start.rs
Line | Count | Source |
1 | | //! Mutators related to the start section. |
2 | | |
3 | | use super::Mutator; |
4 | | use crate::{Result, WasmMutate}; |
5 | | use wasm_encoder::Module; |
6 | | |
7 | | /// A mutator to remove the start section. |
8 | | #[derive(Clone, Copy)] |
9 | | pub struct RemoveStartSection; |
10 | | |
11 | | impl Mutator for RemoveStartSection { |
12 | 529 | fn can_mutate(&self, config: &WasmMutate) -> bool { |
13 | 529 | !config.preserve_semantics && config.info().start.is_some() |
14 | 529 | } |
15 | | |
16 | 4 | fn mutate<'a>( |
17 | 4 | &self, |
18 | 4 | config: &'a mut WasmMutate, |
19 | 4 | ) -> Result<Box<dyn Iterator<Item = crate::Result<Module>> + 'a>> { |
20 | 4 | let mut module = Module::new(); |
21 | 4 | let start_section_index = config.info().start.unwrap(); |
22 | | |
23 | 29 | for (i, section) in config.info().raw_sections.iter().enumerate() { |
24 | 29 | if i == start_section_index { |
25 | 4 | continue; |
26 | 25 | } |
27 | 25 | module.section(section); |
28 | | } |
29 | | |
30 | 4 | Ok(Box::new(std::iter::once(Ok(module)))) |
31 | 4 | } |
32 | | } |
33 | | |
34 | | #[cfg(test)] |
35 | | mod tests { |
36 | | use super::*; |
37 | | |
38 | | #[test] |
39 | | fn test_remove_start_segment() { |
40 | | crate::mutators::match_mutation( |
41 | | r#" |
42 | | (module |
43 | | (func $f (param i32) (result i32) |
44 | | local.get 0 |
45 | | ) |
46 | | (start $f) |
47 | | ) |
48 | | "#, |
49 | | RemoveStartSection, |
50 | | r#" |
51 | | (module |
52 | | (func $f (param i32) (result i32) |
53 | | local.get 0 |
54 | | ) |
55 | | ) |
56 | | "#, |
57 | | ); |
58 | | } |
59 | | } |