| 1 |
//! This mutator selects a random `if` construction in a function and swap its branches. |
| 2 |
//! Since this mutator preserves the original semantic of the input Wasm, |
| 3 |
//! before the mutated if structure is encoded, a "negation" of the previous operand |
| 4 |
//! in the stack is written. The "negation" is encoded with a `i32.eqz` operator. |
| 5 |
use rand::prelude::*; |
| 6 |
use wasm_encoder::{Function, ValType}; |
| 7 |
|
| 8 |
use crate::{ |
| 9 |
WasmMutate, |
| 10 |
module::map_block_type, |
| 11 |
mutators::{ |
| 12 |
OperatorAndByteOffset, |
| 13 |
codemotion::{ |
| 14 |
AstMutator, |
| 15 |
ir::{AstWriter, parse_context::Ast}, |
| 16 |
}, |
| 17 |
}, |
| 18 |
}; |
| 19 |
|
| 20 |
/// This mutator selects a random `if` construction in a function and swap its branches. |
| 21 |
/// Since this mutator preserves the original semantic of the input Wasm, |
| 22 |
/// before the mutated if structure is encoded, a "negation" of the previous operand |
| 23 |
/// in the stack is written. The "negation" is encoded with a `i32.eqz` operator. |
| 24 |
pub struct IfComplementMutator; |
| 25 |
|
| 26 |
#[derive(Default)] |
| 27 |
struct IfComplementWriter { |
| 28 |
if_to_mutate: usize, |
| 29 |
} |
| 30 |
|
| 31 |
impl IfComplementWriter { |
| 32 |
/// Swap the then and alternative branches and negates the expected value for the "if" condition |
| 33 |
fn write_complement<'a>( |
| 34 |
&self, |
| 35 |
ast: &Ast, |
| 36 |
_: usize, |
| 37 |
then: &[usize], |
| 38 |
alternative: &Option<Vec<usize>>, |
| 39 |
newfunc: &mut wasm_encoder::Function, |
| 40 |
operators: &Vec<crate::mutators::OperatorAndByteOffset>, |
| 41 |
input_wasm: &'a [u8], |
| 42 |
ty: &wasmparser::BlockType, |
| 43 |
) -> crate::Result<()> { |
| 44 |
// negate the value on the stack |
| 45 |
newfunc.instructions().i32_eqz(); |
| 46 |
newfunc.instructions().if_(map_block_type(*ty)?); |
| 47 |
|
| 48 |
// Swap, write alternative first |
| 49 |
if let Some(alternative) = alternative { |
| 50 |
for ch in alternative { |
| 51 |
self.write(ast, *ch, newfunc, operators, input_wasm)?; |
| 52 |
} |
| 53 |
} else { |
| 54 |
// Write an unreachable instruction |
| 55 |
newfunc.instructions().nop(); |
| 56 |
} |
| 57 |
newfunc.instructions().else_(); |
| 58 |
for ch in then { |
| 59 |
self.write(ast, *ch, newfunc, operators, input_wasm)?; |
| 60 |
} |
| 61 |
newfunc.instructions().end(); |
| 62 |
|
| 63 |
Ok(()) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl AstWriter for IfComplementWriter { |
| 68 |
/// Replaces the default implementation of the if_else_writing |
| 69 |
fn write_if_else<'a>( |
| 70 |
&self, |
| 71 |
ast: &Ast, |
| 72 |
nodeidx: usize, |
| 73 |
then: &[usize], |
| 74 |
alternative: &Option<Vec<usize>>, |
| 75 |
newfunc: &mut wasm_encoder::Function, |
| 76 |
operators: &Vec<crate::mutators::OperatorAndByteOffset>, |
| 77 |
input_wasm: &'a [u8], |
| 78 |
ty: &wasmparser::BlockType, |
| 79 |
) -> crate::Result<()> { |
| 80 |
if self.if_to_mutate == nodeidx { |
| 81 |
self.write_complement( |
| 82 |
ast, |
| 83 |
nodeidx, |
| 84 |
then, |
| 85 |
alternative, |
| 86 |
newfunc, |
| 87 |
operators, |
| 88 |
input_wasm, |
| 89 |
ty, |
| 90 |
)?; |
| 91 |
} else { |
| 92 |
self.write_if_else_default( |
| 93 |
ast, |
| 94 |
nodeidx, |
| 95 |
then, |
| 96 |
alternative, |
| 97 |
newfunc, |
| 98 |
operators, |
| 99 |
input_wasm, |
| 100 |
ty, |
| 101 |
)?; |
| 102 |
} |
| 103 |
Ok(()) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl AstMutator for IfComplementMutator { |
| 108 |
fn can_mutate(&self, _: &crate::WasmMutate, ast: &Ast) -> bool { |
| 109 |
ast.has_if() |
| 110 |
} |
| 111 |
|
| 112 |
fn mutate<'a>( |
| 113 |
&self, |
| 114 |
config: &'a mut WasmMutate, |
| 115 |
ast: &Ast, |
| 116 |
locals: &[(u32, ValType)], |
| 117 |
operators: &Vec<OperatorAndByteOffset>, |
| 118 |
input_wasm: &'a [u8], |
| 119 |
) -> crate::Result<Function> { |
| 120 |
// Select the if index |
| 121 |
let mut newfunc = Function::new(locals.to_vec()); |
| 122 |
let if_index = ast |
| 123 |
.get_ifs() |
| 124 |
.choose(config.rng()) |
| 125 |
.expect("This mutator should check first if the AST contains at least one if"); |
| 126 |
let writer = IfComplementWriter { |
| 127 |
if_to_mutate: *if_index, |
| 128 |
}; |
| 129 |
writer.write(ast, ast.get_root(), &mut newfunc, operators, input_wasm)?; |
| 130 |
Ok(newfunc) |
| 131 |
} |
| 132 |
} |