| 1 |
//! Mutator that replaces the body of a function with an empty body |
| 2 |
|
| 3 |
use super::Mutator; |
| 4 |
use crate::module::{PrimitiveTypeInfo, TypeInfo}; |
| 5 |
use crate::{Result, WasmMutate}; |
| •6 |
use rand::RngExt; |
| 7 |
use wasm_encoder::{CodeSection, Function, HeapType, Module}; |
| 8 |
use wasmparser::CodeSectionReader; |
| 9 |
|
| 10 |
/// Mutator that replaces the body of a function with an empty body |
| 11 |
#[derive(Clone, Copy)] |
| 12 |
pub struct SnipMutator; |
| 13 |
|
| 14 |
impl Mutator for SnipMutator { |
| 15 |
fn mutate<'a>( |
| 16 |
&self, |
| 17 |
config: &'a mut WasmMutate, |
| 18 |
) -> Result<Box<dyn Iterator<Item = Result<Module>> + 'a>> { |
| 19 |
let mut codes = CodeSection::new(); |
| 20 |
let code_section = config.info().code.unwrap(); |
| 21 |
let reader = config.info().get_binary_reader(code_section); |
| 22 |
let reader = CodeSectionReader::new(reader)?; |
| 23 |
let count = reader.count(); |
| 24 |
let function_to_mutate = config.rng().random_range(0..count); |
| 25 |
let ftype = config |
| 26 |
.info() |
| 27 |
.get_functype_idx(function_to_mutate + config.info().num_imported_functions()) |
| 28 |
.clone(); |
| 29 |
|
| 30 |
for (i, func) in reader.into_iter().enumerate() { |
| 31 |
config.consume_fuel(1)?; |
| 32 |
let f = func?; |
| 33 |
|
| 34 |
if i as u32 != function_to_mutate { |
| 35 |
codes.raw(f.as_bytes()); |
| 36 |
continue; |
| 37 |
} |
| 38 |
|
| 39 |
log::trace!("Snipping function {function_to_mutate}"); |
| 40 |
|
| 41 |
let locals = vec![]; |
| 42 |
let mut f = Function::new(locals); |
| 43 |
|
| 44 |
match &ftype { |
| 45 |
TypeInfo::Func(t) => { |
| 46 |
for primitive in t.returns.iter() { |
| 47 |
match primitive { |
| 48 |
PrimitiveTypeInfo::I32 => { |
| 49 |
f.instructions().i32_const(0); |
| 50 |
} |
| 51 |
PrimitiveTypeInfo::I64 => { |
| 52 |
f.instructions().i64_const(0); |
| 53 |
} |
| 54 |
PrimitiveTypeInfo::F32 => { |
| 55 |
f.instructions().f32_const(0.0.into()); |
| 56 |
} |
| 57 |
PrimitiveTypeInfo::F64 => { |
| 58 |
f.instructions().f64_const(0.0.into()); |
| 59 |
} |
| 60 |
PrimitiveTypeInfo::V128 => { |
| 61 |
f.instructions().v128_const(0); |
| 62 |
} |
| 63 |
PrimitiveTypeInfo::FuncRef => { |
| 64 |
f.instructions().ref_null(HeapType::FUNC); |
| 65 |
} |
| 66 |
PrimitiveTypeInfo::ExternRef => { |
| 67 |
f.instructions().ref_null(HeapType::EXTERN); |
| 68 |
} |
| 69 |
PrimitiveTypeInfo::Empty => { |
| 70 |
unreachable!() |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
f.instructions().end(); |
| 78 |
codes.function(&f); |
| 79 |
} |
| 80 |
|
| 81 |
Ok(Box::new(std::iter::once(Ok(config |
| 82 |
.info() |
| 83 |
.replace_section(config.info().code.unwrap(), &codes))))) |
| 84 |
} |
| 85 |
|
| 86 |
fn can_mutate<'a>(&self, config: &'a WasmMutate) -> bool { |
| 87 |
!config.preserve_semantics && config.info().has_nonempty_code() |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
#[cfg(test)] |
| 92 |
mod tests { |
| 93 |
use super::SnipMutator; |
| 94 |
use crate::mutators::Mutator; |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn test_code_snip_mutator() { |
| 98 |
crate::mutators::match_mutation( |
| 99 |
r#" |
| 100 |
(module |
| 101 |
(func (result i64) |
| 102 |
i64.const 42 |
| 103 |
) |
| 104 |
) |
| 105 |
"#, |
| 106 |
SnipMutator, |
| 107 |
r#" |
| 108 |
(module |
| 109 |
(type (;0;) (func (result i64))) |
| 110 |
(func (;0;) (type 0) (result i64) |
| 111 |
i64.const 0) |
| 112 |
) |
| 113 |
"#, |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
#[test] |
| 118 |
fn test_snip_function_empty_code_section() { |
| 119 |
let wasm = b"\x00\x61\x73\x6d\x01\x00\x00\x00\x0a\x02\x00\x0b"; |
| 120 |
let mut config = crate::WasmMutate::default(); |
| 121 |
config.setup(wasm).unwrap(); |
| 122 |
assert_eq!(SnipMutator.can_mutate(&config), false); |
| 123 |
} |
| 124 |
} |