| 1 |
//! Mutate custom sections. |
| 2 |
|
| 3 |
use std::borrow::Cow; |
| 4 |
|
| 5 |
use super::Mutator; |
| •6 |
use rand::prelude::*; |
| 7 |
|
| 8 |
#[derive(Clone, Copy)] |
| 9 |
pub struct CustomSectionMutator; |
| 10 |
|
| 11 |
impl Mutator for CustomSectionMutator { |
| 12 |
fn can_mutate(&self, config: &crate::WasmMutate) -> bool { |
| 13 |
config.info().has_custom_section() |
| 14 |
} |
| 15 |
|
| 16 |
fn mutate<'a>( |
| 17 |
&self, |
| 18 |
config: &'a mut crate::WasmMutate, |
| 19 |
) -> crate::Result<Box<dyn Iterator<Item = crate::Result<wasm_encoder::Module>> + 'a>> { |
| 20 |
let custom_section_indices: Vec<_> = config |
| 21 |
.info() |
| 22 |
.raw_sections |
| 23 |
.iter() |
| 24 |
.enumerate() |
| 25 |
.filter(|(_i, s)| s.id == wasm_encoder::SectionId::Custom as u8) |
| 26 |
.map(|(i, _s)| i) |
| 27 |
.collect(); |
| 28 |
assert!(!custom_section_indices.is_empty()); |
| 29 |
|
| 30 |
let custom_section_index = *custom_section_indices.choose(config.rng()).unwrap(); |
| 31 |
let reader = config.info().get_binary_reader(custom_section_index); |
| 32 |
let old_custom_section = wasmparser::CustomSectionReader::new(reader).unwrap(); |
| 33 |
|
| 34 |
let name_string; |
| 35 |
let data_vec; |
| 36 |
let mut name = old_custom_section.name(); |
| 37 |
let mut data = old_custom_section.data(); |
| 38 |
|
| 39 |
if config.rng().random_ratio(1, 20) { |
| 40 |
// Mutate the custom section's name. |
| 41 |
let mut new_name = name.to_string().into_bytes(); |
| 42 |
config.raw_mutate( |
| 43 |
&mut new_name, |
| 44 |
if config.reduce { |
| 45 |
name.len().saturating_sub(1) |
| 46 |
} else { |
| 47 |
std::cmp::max(name.len() * 2, 32) |
| 48 |
}, |
| 49 |
)?; |
| 50 |
name_string = String::from_utf8_lossy(&new_name).to_string(); |
| 51 |
name = &name_string; |
| 52 |
} else { |
| 53 |
// Mutate the custom section's data. |
| 54 |
let mut new_data = data.to_vec(); |
| 55 |
config.raw_mutate( |
| 56 |
&mut new_data, |
| 57 |
if config.reduce { |
| 58 |
data.len().saturating_sub(1) |
| 59 |
} else { |
| 60 |
std::cmp::max(data.len() * 2, 32) |
| 61 |
}, |
| 62 |
)?; |
| 63 |
data_vec = new_data; |
| 64 |
data = &data_vec; |
| 65 |
}; |
| 66 |
|
| 67 |
Ok(Box::new(std::iter::once(Ok(config |
| 68 |
.info() |
| 69 |
.replace_section( |
| 70 |
custom_section_index, |
| 71 |
&wasm_encoder::CustomSection { |
| 72 |
name: name.into(), |
| 73 |
data: Cow::Borrowed(data), |
| 74 |
}, |
| 75 |
))))) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
#[derive(Clone, Copy)] |
| 80 |
pub struct AddCustomSectionMutator; |
| 81 |
|
| 82 |
const MAX_NEW_DATA_LEN: usize = 100; |
| 83 |
const MAX_NEW_NAME_LEN: usize = 20; |
| 84 |
|
| 85 |
impl Mutator for AddCustomSectionMutator { |
| 86 |
fn can_mutate(&self, config: &crate::WasmMutate) -> bool { |
| 87 |
!config.reduce |
| 88 |
} |
| 89 |
|
| 90 |
fn mutate<'a>( |
| 91 |
&self, |
| 92 |
config: &'a mut crate::WasmMutate, |
| 93 |
) -> crate::Result<Box<dyn Iterator<Item = crate::Result<wasm_encoder::Module>> + 'a>> { |
| 94 |
let num_sections = config.info().raw_sections.len(); |
| 95 |
let new_custom_section_idx = config.rng().random_range(0..=num_sections); |
| 96 |
let mut name = vec![]; |
| 97 |
config.raw_mutate(&mut name, MAX_NEW_NAME_LEN)?; |
| 98 |
let name = String::from_utf8_lossy(&name); |
| 99 |
let mut data = vec![]; |
| 100 |
config.raw_mutate(&mut data, MAX_NEW_DATA_LEN)?; |
| 101 |
|
| 102 |
Ok(Box::new(std::iter::once(Ok(config.info().insert_section( |
| 103 |
new_custom_section_idx, |
| 104 |
&wasm_encoder::CustomSection { |
| 105 |
name: name.into(), |
| 106 |
data: Cow::Borrowed(&data), |
| 107 |
}, |
| 108 |
))))) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
#[derive(Copy, Clone)] |
| 113 |
pub struct ReorderCustomSectionMutator; |
| 114 |
|
| 115 |
impl Mutator for ReorderCustomSectionMutator { |
| 116 |
fn can_mutate(&self, config: &crate::WasmMutate) -> bool { |
| 117 |
config.info().has_custom_section() && config.info().raw_sections.len() > 1 |
| 118 |
} |
| 119 |
|
| 120 |
fn mutate<'a>( |
| 121 |
&self, |
| 122 |
config: &'a mut crate::WasmMutate, |
| 123 |
) -> crate::Result<Box<dyn Iterator<Item = crate::Result<wasm_encoder::Module>> + 'a>> { |
| 124 |
let custom_section_indices: Vec<_> = config |
| 125 |
.info() |
| 126 |
.raw_sections |
| 127 |
.iter() |
| 128 |
.enumerate() |
| 129 |
.filter(|(_i, s)| s.id == wasm_encoder::SectionId::Custom as u8) |
| 130 |
.map(|(i, _s)| i) |
| 131 |
.collect(); |
| 132 |
assert!(!custom_section_indices.is_empty()); |
| 133 |
|
| 134 |
let src_idx = *custom_section_indices.choose(config.rng()).unwrap(); |
| 135 |
let num_sections = config.info().raw_sections.len(); |
| 136 |
let mut dest_idx; |
| 137 |
loop { |
| 138 |
dest_idx = config.rng().random_range(0..num_sections); |
| 139 |
if dest_idx != src_idx { |
| 140 |
break; |
| 141 |
} |
| 142 |
config.consume_fuel(1)?; |
| 143 |
} |
| 144 |
|
| 145 |
Ok(Box::new(std::iter::once(Ok(config |
| 146 |
.info() |
| 147 |
.move_section(src_idx, dest_idx))))) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
#[cfg(test)] |
| 152 |
mod tests { |
| 153 |
use super::*; |
| 154 |
|
| 155 |
#[test] |
| 156 |
fn test_add_custom_section() { |
| 157 |
crate::mutators::match_mutation( |
| 158 |
r#" |
| 159 |
(module) |
| 160 |
"#, |
| 161 |
AddCustomSectionMutator, |
| 162 |
r#" |
| 163 |
(module |
| 164 |
(@custom "" "") |
| 165 |
) |
| 166 |
"#, |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn test_grow_custom_section() { |
| 172 |
crate::mutators::match_mutation( |
| 173 |
r#" |
| 174 |
(module |
| 175 |
(@custom "name" "data") |
| 176 |
) |
| 177 |
"#, |
| 178 |
CustomSectionMutator, |
| 179 |
r#" |
| 180 |
(module |
| 181 |
(@custom "name" "datadata") |
| 182 |
) |
| 183 |
"#, |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn test_shrink_custom_section() { |
| 189 |
crate::mutators::match_mutation( |
| 190 |
r#" |
| 191 |
(module |
| 192 |
(@custom "name" "data") |
| 193 |
) |
| 194 |
"#, |
| 195 |
CustomSectionMutator, |
| 196 |
r#" |
| 197 |
(module |
| 198 |
(@custom "name" "d") |
| 199 |
) |
| 200 |
"#, |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn test_mutate_custom_section() { |
| 206 |
crate::mutators::match_mutation( |
| 207 |
r#" |
| 208 |
(module |
| 209 |
(@custom "name" "data") |
| 210 |
) |
| 211 |
"#, |
| 212 |
CustomSectionMutator, |
| 213 |
r#" |
| 214 |
(module |
| 215 |
(@custom "name" "aaaa") |
| 216 |
) |
| 217 |
"#, |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
#[test] |
| 222 |
fn test_mutate_custom_section_name() { |
| 223 |
crate::mutators::match_mutation( |
| 224 |
r#" |
| 225 |
(module |
| 226 |
(@custom "custom-name" "data") |
| 227 |
) |
| 228 |
"#, |
| 229 |
CustomSectionMutator, |
| 230 |
r#" |
| 231 |
(module |
| 232 |
(@custom "custom" "data") |
| 233 |
) |
| 234 |
"#, |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn test_reorder_custom_section() { |
| 240 |
crate::mutators::match_mutation( |
| 241 |
r#" |
| 242 |
(module |
| 243 |
(@custom "name2" "data") |
| 244 |
(@custom "name3" "data") |
| 245 |
) |
| 246 |
"#, |
| 247 |
ReorderCustomSectionMutator, |
| 248 |
r#" |
| 249 |
(module |
| 250 |
(@custom "name3" "data") |
| 251 |
(@custom "name2" "data") |
| 252 |
) |
| 253 |
"#, |
| 254 |
) |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
fn test_reorder_custom_section2() { |
| 259 |
crate::mutators::match_mutation( |
| 260 |
r#" |
| 261 |
(module |
| 262 |
(func (export "x")) |
| 263 |
(@custom "name2" "data") |
| 264 |
(@custom "name3" "data") |
| 265 |
) |
| 266 |
"#, |
| 267 |
ReorderCustomSectionMutator, |
| 268 |
r#" |
| 269 |
(module |
| 270 |
(@custom "name3" "data") |
| 271 |
(func (export "x")) |
| 272 |
(@custom "name2" "data") |
| 273 |
) |
| 274 |
"#, |
| 275 |
) |
| 276 |
} |
| 277 |
} |