| 1 |
use super::Mutator; |
| 2 |
use crate::{Result, WasmMutate}; |
| •3 |
use rand::RngExt; |
| 4 |
use wasm_encoder::reencode::{Reencode, RoundtripReencoder}; |
| 5 |
use wasm_encoder::{DataSection, Module}; |
| 6 |
use wasmparser::{DataKind, DataSectionReader}; |
| 7 |
|
| 8 |
/// Mutator that modifies a data segment, either adding or removing bytes. |
| 9 |
#[derive(Clone, Copy)] |
| 10 |
pub struct ModifyDataMutator { |
| 11 |
pub max_data_size: usize, |
| 12 |
} |
| 13 |
|
| 14 |
impl Mutator for ModifyDataMutator { |
| 15 |
fn mutate<'a>( |
| 16 |
&self, |
| 17 |
config: &'a mut WasmMutate, |
| 18 |
) -> Result<Box<dyn Iterator<Item = Result<Module>> + 'a>> { |
| 19 |
let mut new_section = DataSection::new(); |
| 20 |
let section_idx = config.info().data.unwrap(); |
| 21 |
let reader = config.info().get_binary_reader(section_idx); |
| 22 |
let reader = DataSectionReader::new(reader)?; |
| 23 |
|
| 24 |
// Select an arbitrary data segment to modify. |
| 25 |
let data_to_modify = config.rng().random_range(0..reader.count()); |
| 26 |
|
| 27 |
// Iterate over all data segments in the old data section and re-add |
| 28 |
// them to the `new_section` one-by-one. |
| 29 |
for (i, data) in reader.into_iter().enumerate() { |
| 30 |
let data = data?; |
| 31 |
// If this is the correct data segment apply the mutation, |
| 32 |
// otherwise preserve the data. |
| 33 |
let mut contents = data.data.to_vec(); |
| 34 |
if i as u32 == data_to_modify { |
| 35 |
config.raw_mutate(&mut contents, self.max_data_size)?; |
| 36 |
} |
| 37 |
|
| 38 |
// Add the data segment to the section that we're building |
| 39 |
match data.kind { |
| 40 |
DataKind::Active { |
| 41 |
memory_index, |
| 42 |
offset_expr, |
| 43 |
} => { |
| 44 |
new_section.active( |
| 45 |
memory_index, |
| 46 |
&RoundtripReencoder.const_expr(offset_expr)?, |
| 47 |
contents, |
| 48 |
); |
| 49 |
} |
| 50 |
DataKind::Passive => { |
| 51 |
new_section.passive(contents); |
| 52 |
} |
| 53 |
}; |
| 54 |
} |
| 55 |
|
| 56 |
Ok(Box::new(std::iter::once(Ok(config |
| 57 |
.info() |
| 58 |
.replace_section( |
| 59 |
config.info().data.unwrap(), |
| 60 |
&new_section, |
| 61 |
))))) |
| 62 |
} |
| 63 |
|
| 64 |
fn can_mutate<'a>(&self, config: &'a WasmMutate) -> bool { |
| 65 |
// Modifying a data segment doesn't preserve the semantics of the |
| 66 |
// original module and also only works if there's actually some data. |
| 67 |
!config.preserve_semantics && config.info().num_data() > 0 |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
#[cfg(test)] |
| 72 |
mod tests { |
| 73 |
use super::ModifyDataMutator; |
| 74 |
use crate::WasmMutate; |
| 75 |
use std::sync::Arc; |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn test_remove_export_mutator() { |
| 79 |
let mut config = WasmMutate::default(); |
| 80 |
config.raw_mutate_func(Some(Arc::new(|data, _| { |
| 81 |
assert_eq!(data, b"x"); |
| 82 |
*data = "y".to_string().into_bytes(); |
| 83 |
Ok(()) |
| 84 |
}))); |
| 85 |
config.match_mutation( |
| 86 |
r#"(module (data "x"))"#, |
| 87 |
ModifyDataMutator { max_data_size: 100 }, |
| 88 |
r#"(module (data "y"))"#, |
| 89 |
); |
| 90 |
} |
| 91 |
} |