| 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 |
fn can_mutate(&self, config: &WasmMutate) -> bool { |
| 13 |
!config.preserve_semantics && config.info().start.is_some() |
| 14 |
} |
| 15 |
|
| 16 |
fn mutate<'a>( |
| 17 |
&self, |
| 18 |
config: &'a mut WasmMutate, |
| 19 |
) -> Result<Box<dyn Iterator<Item = crate::Result<Module>> + 'a>> { |
| 20 |
let mut module = Module::new(); |
| 21 |
let start_section_index = config.info().start.unwrap(); |
| 22 |
|
| 23 |
for (i, section) in config.info().raw_sections.iter().enumerate() { |
| 24 |
if i == start_section_index { |
| 25 |
continue; |
| 26 |
} |
| 27 |
module.section(section); |
| 28 |
} |
| 29 |
|
| 30 |
Ok(Box::new(std::iter::once(Ok(module)))) |
| 31 |
} |
| 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 |
} |