Debian Codegraph @

crates/wasm-mutate/src/mutators/remove_section.rs

raw
1 //! Mutators related to custom sections.
2
3 use super::Mutator;
4 use crate::{Result, WasmMutate};
5 use rand::prelude::*;
6 use wasm_encoder::{Module, SectionId};
7
8 /// A mutator that removes a section.
9 #[derive(Clone, Copy)]
10 pub enum RemoveSection {
11 /// Remove a random custom section, even if it contains data.
12 Custom,
13 /// Remove a random empty section.
14 Empty,
15 }
16
17 fn is_empty_section(section: &wasm_encoder::RawSection) -> bool {
18 use wasmparser::*;
19 let reader = BinaryReader::new(section.data, 0);
20 crate::module::match_section_id! {
21 match section.id;
22 Custom => Ok(section.data.is_empty()),
23 Type => TypeSectionReader::new(reader).map(|r| r.count() == 0),
24 Import => ImportSectionReader::new(reader).map(|r| r.count() == 0),
25 Function => FunctionSectionReader::new(reader).map(|r| r.count() == 0),
26 Table => FunctionSectionReader::new(reader).map(|r| r.count() == 0),
27 Memory => MemorySectionReader::new(reader).map(|r| r.count() == 0),
28 Global => GlobalSectionReader::new(reader).map(|r| r.count() == 0),
29 Export => ExportSectionReader::new(reader).map(|r| r.count() == 0),
30 Start => Ok(section.data.is_empty()),
31 Element => ElementSectionReader::new(reader).map(|r| r.count() == 0),
32 Code => CodeSectionReader::new(reader).map(|r| r.count() == 0),
33 Data => DataSectionReader::new(reader).map(|r| r.count() == 0),
34 DataCount => Ok(section.data.is_empty()),
35 Tag => TagSectionReader::new(reader).map(|r| r.count() == 0),
36 _ => Ok(section.data.is_empty()),
37 }
38 .unwrap_or(false)
39 }
40
41 impl Mutator for RemoveSection {
42 fn can_mutate(&self, config: &WasmMutate) -> bool {
43 match self {
44 &Self::Custom => config.info().has_custom_section(),
45 &Self::Empty => config
46 .info()
47 .raw_sections
48 .iter()
49 .any(|s| is_empty_section(s)),
50 }
51 }
52
53 fn mutate<'a>(
54 &self,
55 config: &'a mut WasmMutate,
56 ) -> Result<Box<dyn Iterator<Item = Result<Module>> + 'a>> {
57 let removal_candidates = config
58 .info()
59 .raw_sections
60 .iter()
61 .enumerate()
62 .filter_map(|(i, s)| match self {
63 Self::Empty if is_empty_section(s) => Some(i),
64 Self::Custom if s.id == wasm_encoder::SectionId::Custom as u8 => Some(i),
65 _ => None,
66 })
67 .collect::<Vec<_>>();
68 let section_index_to_remove = *removal_candidates.choose(config.rng()).unwrap();
69
70 let mut module = Module::new();
71 for (i, section) in config.info().raw_sections.iter().enumerate() {
72 if i == section_index_to_remove {
73 continue;
74 }
75 module.section(section);
76 }
77 Ok(Box::new(std::iter::once(Ok(module))))
78 }
79 }
80
81 #[cfg(test)]
82 mod tests {
83 use super::*;
84
85 #[test]
86 fn test_remove_custom_section() {
87 // This will remove the name section so `$f` becomes `(;0;)`.
88 crate::mutators::match_mutation(
89 r#"
90 (module
91 (func $f (param i32) (result i32)
92 local.get 0
93 )
94 )
95 "#,
96 RemoveSection::Custom,
97 r#"
98 (module
99 (func (;0;) (param i32) (result i32)
100 local.get 0
101 )
102 )
103 "#,
104 );
105 }
106
107 #[test]
108 fn test_remove_empty_section() {
109 // This will remove the import section as it is empty.
110 crate::mutators::match_mutation(
111 r#"(module binary
112 "\00asm\01\00\00\00"
113 "\01\05"
114 "\01\60\00\01\7f"
115 "\02\01\00"
116 )"#,
117 RemoveSection::Empty,
118 r#"(module binary
119 "\00asm\01\00\00\00"
120 "\01\05"
121 "\01\60\00\01\7f"
122 )"#,
123 );
124 }
125 }