Debian Codegraph @

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

raw ◀ ▶
1 Debian patch touch this file
1 //! Mutator that adds new, empty functions.
2
3 use super::Mutator;
4 use crate::module::{PrimitiveTypeInfo, TypeInfo};
5 use crate::{Result, WasmMutate};
•6 use rand::RngExt;
7 use wasm_encoder::{AbstractHeapType, HeapType, Module};
8
9 /// Mutator that adds new, empty functions to a Wasm module.
10 #[derive(Clone, Copy)]
11 pub struct AddFunctionMutator;
12
13 impl Mutator for AddFunctionMutator {
14 fn mutate<'a>(
15 &self,
16 config: &'a mut WasmMutate,
17 ) -> Result<Box<dyn Iterator<Item = Result<Module>> + 'a>> {
18 let max_ty_idx = config.info().num_types() - 1;
19 let ty_idx = config.rng().random_range(0..=max_ty_idx);
20
21 // (Re)encode the function section and add this new entry.
22 let mut func_sec_enc = wasm_encoder::FunctionSection::new();
23 if let Some(func_sec_idx) = config.info().functions {
24 let reader = config.info().get_binary_reader(func_sec_idx);
25 let reader = wasmparser::FunctionSectionReader::new(reader)?;
26 for x in reader {
27 func_sec_enc.function(x?);
28 }
29 }
30 func_sec_enc.function(ty_idx);
31
32 // Copy the existing code bodies over and then add a new dummy body for
33 // this function.
34 let mut code_sec_enc = wasm_encoder::CodeSection::new();
35 if let Some(code_sec_idx) = config.info().code {
36 let reader = config.info().get_binary_reader(code_sec_idx);
37 let reader = wasmparser::CodeSectionReader::new(reader)?;
38 for body in reader {
39 let body = body?;
40 code_sec_enc.raw(body.as_bytes());
41 }
42 }
43 let func_ty = match &config.info().types_map[usize::try_from(ty_idx).unwrap()] {
44 TypeInfo::Func(func_ty) => func_ty,
45 };
46 let mut func = wasm_encoder::Function::new(vec![]);
47 for ty in &func_ty.returns {
48 match ty {
49 PrimitiveTypeInfo::I32 => {
50 func.instructions().i32_const(0);
51 }
52 PrimitiveTypeInfo::I64 => {
53 func.instructions().i64_const(0);
54 }
55 PrimitiveTypeInfo::F32 => {
56 func.instructions().f32_const(0.0.into());
57 }
58 PrimitiveTypeInfo::F64 => {
59 func.instructions().f64_const(0.0.into());
60 }
61 PrimitiveTypeInfo::V128 => {
62 func.instructions().v128_const(0);
63 }
64 PrimitiveTypeInfo::FuncRef => {
65 func.instructions().ref_null(HeapType::Abstract {
66 shared: false,
67 ty: AbstractHeapType::Func,
68 });
69 }
70 PrimitiveTypeInfo::ExternRef => {
71 func.instructions().ref_null(HeapType::Abstract {
72 shared: false,
73 ty: AbstractHeapType::Extern,
74 });
75 }
76 PrimitiveTypeInfo::Empty => unreachable!(),
77 }
78 }
79 func.instructions().end();
80 code_sec_enc.function(&func);
81
82 let module = if config.info().functions.is_some() {
83 // Replace the old sections with the new ones.
84 config
85 .info()
86 .replace_multiple_sections(|_, sec_id, module| match sec_id {
87 x if x == wasm_encoder::SectionId::Function as u8 => {
88 module.section(&func_sec_enc);
89 true
90 }
91 x if x == wasm_encoder::SectionId::Code as u8 => {
92 module.section(&code_sec_enc);
93 true
94 }
95 _ => false,
96 })
97 } else {
98 // Insert the new sections in their respective places.
99 let mut added_func = false;
100 let mut added_code = false;
101 let mut module = config
102 .info()
103 .replace_multiple_sections(|_, sec_id, module| {
104 if !added_func && sec_id >= wasm_encoder::SectionId::Function as u8 {
105 module.section(&func_sec_enc);
106 added_func = true;
107 }
108
109 if !added_code
110 && sec_id >= wasm_encoder::SectionId::Code as u8
111 && sec_id != wasm_encoder::SectionId::DataCount as u8
112 {
113 module.section(&code_sec_enc);
114 added_code = true;
115 }
116
117 sec_id == wasm_encoder::SectionId::Function as u8
118 || sec_id == wasm_encoder::SectionId::Code as u8
119 });
120 if !added_func {
121 module.section(&func_sec_enc);
122 }
123 if !added_code {
124 module.section(&code_sec_enc);
125 }
126 module
127 };
128
129 Ok(Box::new(std::iter::once(Ok(module))))
130 }
131
132 fn can_mutate<'a>(&self, config: &'a WasmMutate) -> bool {
133 // Note: adding a new, never-called function preserves semantics so we
134 // don't need to gate on whether `config.preserve_semantics` is set or
135 // not.
136 !config.reduce && config.info().num_types() > 0
137 }
138 }
139
140 #[cfg(test)]
141 mod tests {
142 use super::*;
143
144 #[test]
145 fn test_add_first_function() {
146 crate::mutators::match_mutation(
147 r#"
148 (module
149 (type (;0;) (func (param i32 f32) (result i64)))
150 )
151 "#,
152 AddFunctionMutator,
153 r#"
154 (module
155 (type (;0;) (func (param i32 f32) (result i64)))
156 (func (;0;) (type 0) (param i32 f32) (result i64)
157 i64.const 0)
158 )
159 "#,
160 );
161 }
162
163 #[test]
164 fn test_add_another_function() {
165 crate::mutators::match_mutation(
166 r#"
167 (module
168 (type (;0;) (func (param i32 f32) (result i64 f64)))
169 (func (;0;) (type 0) (param i32 f32) (result i64 f64)
170 i64.const 0
171 f64.const 0.0
172 )
173 )
174 "#,
175 AddFunctionMutator,
176 r#"
177 (module
178 (type (;0;) (func (param i32 f32) (result i64 f64)))
179 (func (;0;) (type 0) (param i32 f32) (result i64 f64)
180 i64.const 0
181 f64.const 0.0
182 )
183 (func (;0;) (type 0) (param i32 f32) (result i64 f64)
184 i64.const 0
185 f64.const 0.0
186 )
187 )
188 "#,
189 );
190 }
191 }