| 1 |
//! Mutator that replaces a function's body with an `unreachable` instruction. |
| 2 |
|
| 3 |
use crate::{Result, WasmMutate}; |
| 4 |
|
| •5 |
use rand::RngExt; |
| 6 |
use wasm_encoder::{CodeSection, Function, Module}; |
| 7 |
use wasmparser::CodeSectionReader; |
| 8 |
|
| 9 |
use super::Mutator; |
| 10 |
|
| 11 |
/// Sets the body of a function to unreachable |
| 12 |
#[derive(Clone, Copy)] |
| 13 |
pub struct FunctionBodyUnreachable; |
| 14 |
|
| 15 |
impl Mutator for FunctionBodyUnreachable { |
| 16 |
fn mutate<'a>( |
| 17 |
&self, |
| 18 |
config: &mut WasmMutate<'a>, |
| 19 |
) -> Result<Box<dyn Iterator<Item = Result<Module>> + 'a>> { |
| 20 |
let mut codes = CodeSection::new(); |
| 21 |
|
| 22 |
let code_section = config.info().code.unwrap(); |
| 23 |
let reader = config.info().get_binary_reader(code_section); |
| 24 |
let reader = CodeSectionReader::new(reader)?; |
| 25 |
|
| 26 |
let count = reader.count(); |
| 27 |
let function_to_mutate = config.rng().random_range(0..count); |
| 28 |
|
| 29 |
for (i, f) in reader.into_iter().enumerate() { |
| 30 |
config.consume_fuel(1)?; |
| 31 |
|
| 32 |
let f = f?; |
| 33 |
if i as u32 == function_to_mutate { |
| 34 |
log::trace!("Mutating function {i}"); |
| 35 |
let locals = vec![]; |
| 36 |
let mut f = Function::new(locals); |
| 37 |
f.instructions().unreachable(); |
| 38 |
f.instructions().end(); |
| 39 |
|
| 40 |
codes.function(&f); |
| 41 |
} else { |
| 42 |
codes.raw(f.as_bytes()); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
Ok(Box::new(std::iter::once(Ok(config |
| 47 |
.info() |
| 48 |
.replace_section(config.info().code.unwrap(), &codes))))) |
| 49 |
} |
| 50 |
|
| 51 |
fn can_mutate<'a>(&self, config: &'a WasmMutate) -> bool { |
| 52 |
!config.preserve_semantics && config.info().has_nonempty_code() |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[cfg(test)] |
| 57 |
mod tests { |
| 58 |
use super::FunctionBodyUnreachable; |
| 59 |
use crate::mutators::Mutator; |
| 60 |
|
| 61 |
#[test] |
| 62 |
fn test_code_unreachable_mutator() { |
| 63 |
crate::mutators::match_mutation( |
| 64 |
r#" |
| 65 |
(module |
| 66 |
(func (result i64) |
| 67 |
i64.const 42 |
| 68 |
) |
| 69 |
(func (result i64) |
| 70 |
i64.const 42 |
| 71 |
) |
| 72 |
(func (export "exported_func") (result i32) |
| 73 |
i32.const 42 |
| 74 |
) |
| 75 |
) |
| 76 |
"#, |
| 77 |
FunctionBodyUnreachable, |
| 78 |
r#"(module |
| 79 |
(type (;0;) (func (result i64))) |
| 80 |
(type (;1;) (func (result i32))) |
| 81 |
(func (;0;) (type 0) (result i64) |
| 82 |
i64.const 42) (func (;1;) (type 0) (result i64) |
| 83 |
i64.const 42) (func (;2;) (type 1) (result i32) unreachable) |
| 84 |
(export "exported_func" (func 2)))"#, |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn test_fn_body_unreachable_empty_code_section() { |
| 90 |
let wasm = b"\x00\x61\x73\x6d\x01\x00\x00\x00\x0a\x02\x00\x0b"; |
| 91 |
let mut config = crate::WasmMutate::default(); |
| 92 |
config.setup(wasm).unwrap(); |
| 93 |
assert_eq!(FunctionBodyUnreachable.can_mutate(&config), false); |
| 94 |
} |
| 95 |
} |