Skip to content

Commit 9c9885f

Browse files
authored
Update rand and env_logger dependencies (#461)
* Update rand and env_logger dependencies Just noticed that they're a bit behind, no other reason to update than for update's sake here though. * Make wasm-mutate tests more robust Tweak some assertions to keep pumping the mutator with a different rng to eventually find the right mutation, otherwise tweak some test expectations. * Fix wasm-mutate integration test
1 parent 7c87904 commit 9c9885f

File tree

18 files changed

+144
-147
lines changed

18 files changed

+144
-147
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ members = ['fuzz', 'crates/wasm-encoder', 'crates/fuzz-stats', 'crates/wasm-muta
1717

1818
[dependencies]
1919
anyhow = "1.0"
20-
env_logger = "0.8"
20+
env_logger = "0.9"
2121
log = "0.4"
2222
clap = { version = "3.0", features = ['derive'] }
2323
tempfile = "3.2.0"

crates/wasm-mutate-stats/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ publish = false
88
anyhow = "1.0"
99
arbitrary = "1.0"
1010
num_cpus = "1.13"
11-
rand = { version = "0.7.3", features = ["small_rng"] }
11+
rand = { version = "0.8.0", features = ["small_rng"] }
1212
wasm-mutate = { path = '../wasm-mutate' }
1313
wasmprinter = { path = '../wasmprinter' }
1414
wasmparser = { path = "../wasmparser" }
1515
wasmtime = "0.32.0"
16-
env_logger = "0.8"
16+
env_logger = "0.9"
1717
itertools = "0.10.0"
1818
clap = "3.0"
1919
log = "0.4"

crates/wasm-mutate/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ clap = { optional = true, version = "3.0", features = ['derive'] }
1111
thiserror = "1.0.28"
1212
wasmparser = { version = "0.82.0", path = "../wasmparser" }
1313
wasm-encoder = { version = "0.9.0", path = "../wasm-encoder"}
14-
rand = { version = "0.7.3", features = ["small_rng"] }
14+
rand = { version = "0.8.0", features = ["small_rng"] }
1515
log = "0.4.14"
1616
egg = "0.6.0"
1717

1818
[dev-dependencies]
1919
anyhow = "1"
2020
wat = { path = "../wat" }
2121
wasmprinter = { path = "../wasmprinter" }
22-
env_logger = "0.8"
22+
env_logger = "0.9"

crates/wasm-mutate/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ macro_rules! define_mutators {
4242
let m = $first;
4343

4444
if m.can_mutate($self) {
45-
match m.mutate($self) {
45+
match m.clone().mutate($self) {
4646
Ok(iter) => {
4747
return Ok(Box::new(iter.into_iter().map(|r| r.map(|m| m.finish()))))
4848
}
@@ -57,7 +57,7 @@ macro_rules! define_mutators {
5757
let m = $rest;
5858

5959
if m.can_mutate($self) {
60-
match m.mutate($self) {
60+
match m.clone().mutate($self) {
6161
Ok(iter) => {
6262
return Ok(Box::new(iter.into_iter().map(|r| r.map(|m| m.finish()))))
6363
}
@@ -73,7 +73,7 @@ macro_rules! define_mutators {
7373
let m = $head;
7474

7575
if m.can_mutate($self) {
76-
match m.mutate($self) {
76+
match m.clone().mutate($self) {
7777
Ok(iter) => {
7878
return Ok(Box::new(iter.into_iter().map(|r| r.map(|m| m.finish()))))
7979
}
@@ -91,7 +91,7 @@ macro_rules! define_mutators {
9191
( $self: ident , ($first: expr , $( $tail: expr ,)* ) ) => {
9292
{
9393
let count = define_mutators!(@count $first , $($tail ,)*);
94-
let discriminator:u32 = $self.rng().gen_range(0, count);
94+
let discriminator:u32 = $self.rng().gen_range(0..count);
9595
define_mutators!(@expand $self, discriminator , 0 , $first , $($tail ,)* . , );
9696
}
9797
};
@@ -316,8 +316,8 @@ impl<'wasm> WasmMutate<'wasm> {
316316
// subslice of data with a random slice of other data.
317317
//
318318
// First up start/end indices are picked.
319-
let a = self.rng().gen_range(0, data.len() + 1);
320-
let b = self.rng().gen_range(0, data.len() + 1);
319+
let a = self.rng().gen_range(0..=data.len());
320+
let b = self.rng().gen_range(0..=data.len());
321321
let start = a.min(b);
322322
let end = a.max(b);
323323

@@ -331,7 +331,7 @@ impl<'wasm> WasmMutate<'wasm> {
331331
};
332332
let len = self
333333
.rng()
334-
.gen_range(0, end - start + max_size.saturating_sub(data.len()) + 1);
334+
.gen_range(0..=end - start + max_size.saturating_sub(data.len()));
335335

336336
// With parameters chosen the `Vec::splice` method is used to replace
337337
// the data in the input.

crates/wasm-mutate/src/mutators.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ pub trait Mutator {
8989
fn mutate<'a>(
9090
self,
9191
config: &'a mut WasmMutate,
92-
) -> Result<Box<dyn Iterator<Item = Result<Module>> + 'a>>
93-
where
94-
Self: Copy;
92+
) -> Result<Box<dyn Iterator<Item = Result<Module>> + 'a>>;
9593

9694
/// What is this mutator's name?
9795
///
@@ -107,7 +105,7 @@ pub type OperatorAndByteOffset<'a> = (Operator<'a>, usize);
107105
#[cfg(test)]
108106
fn match_mutation<T>(original: &str, mutator: T, expected: &str)
109107
where
110-
T: Mutator + Copy,
108+
T: Mutator + Clone,
111109
{
112110
WasmMutate::default().match_mutation(original, mutator, expected)
113111
}
@@ -116,11 +114,13 @@ impl WasmMutate<'_> {
116114
#[cfg(test)]
117115
fn match_mutation<T>(&mut self, original: &str, mutator: T, expected: &str)
118116
where
119-
T: Mutator + Copy,
117+
T: Mutator + Clone,
120118
{
121119
use crate::ErrorKind;
122120
use wasmparser::WasmFeatures;
123121

122+
drop(env_logger::try_init());
123+
124124
let original = &wat::parse_str(original).unwrap();
125125

126126
let expected = &wat::parse_str(expected).unwrap();
@@ -134,13 +134,16 @@ impl WasmMutate<'_> {
134134
assert!(can_mutate);
135135

136136
let attempts = 100;
137+
let mut last_mutation = None;
137138

138139
for _ in 0..attempts {
139140
let mutation = match mutator
141+
.clone()
140142
.mutate(&mut config)
141-
.and_then(|mut mutation| mutation.next().unwrap())
143+
.map(|mut mutation| mutation.next())
142144
{
143-
Ok(mutation) => mutation,
145+
Ok(Some(mutation)) => mutation.unwrap(),
146+
Ok(None) => continue,
144147
Err(e) if matches!(e.kind(), ErrorKind::NoMutationsApplicable) => continue,
145148
Err(e) => panic!("mutation error: {}", e),
146149
};
@@ -157,13 +160,27 @@ impl WasmMutate<'_> {
157160
// If it fails, it is probably an invalid
158161
// reformatting expected
159162
let text = wasmprinter::print_bytes(mutation_bytes).unwrap();
160-
assert_eq!(text.trim(), expected_text.trim());
161-
return;
163+
if text.trim() == expected_text.trim() {
164+
return;
165+
}
166+
log::debug!("skipping mutation {}", text);
167+
last_mutation = Some(text);
162168
}
163169

164-
panic!(
165-
"never found any applicable mutations after {} attempts",
166-
attempts
167-
);
170+
match last_mutation {
171+
Some(mutation) => {
172+
panic!(
173+
"after {} attempts the last mutation:\n{:?}\n\n\
174+
did not match the expected mutation\n{:?}",
175+
attempts, mutation, expected_text
176+
);
177+
}
178+
None => {
179+
panic!(
180+
"never found any applicable mutations after {} attempts",
181+
attempts
182+
);
183+
}
184+
}
168185
}
169186
}

crates/wasm-mutate/src/mutators/codemotion.rs

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl CodemotionMutator {
6666

6767
let mut sectionreader = CodeSectionReader::new(original_code_section.data, 0)?;
6868
let function_count = sectionreader.get_count();
69-
let function_to_mutate = config.rng().gen_range(0, function_count);
69+
let function_to_mutate = config.rng().gen_range(0..function_count);
7070

7171
// This split strategy will avoid very often mutating the first function
7272
// and very rarely mutating the last function
@@ -169,44 +169,12 @@ impl Mutator for CodemotionMutator {
169169

170170
#[cfg(test)]
171171
mod tests {
172-
use crate::{
173-
info::ModuleInfo,
174-
mutators::{codemotion::CodemotionMutator, Mutator},
175-
WasmMutate,
176-
};
177-
use rand::{rngs::SmallRng, SeedableRng};
172+
use crate::{mutators::codemotion::CodemotionMutator, WasmMutate};
178173

179174
fn test_motion_mutator(original: &str, expected: &str, seed: u64) {
180-
let _ = env_logger::try_init();
181-
182-
let mut wasmmutate = WasmMutate::default();
183-
let original = &wat::parse_str(original).unwrap();
184-
185-
let mutator = CodemotionMutator; // the string is empty
186-
187-
let rnd = SmallRng::seed_from_u64(seed);
188-
let info = ModuleInfo::new(original).unwrap();
189-
wasmmutate.info = Some(info);
190-
wasmmutate.rng = Some(rnd);
191-
192-
let can_mutate = mutator.can_mutate(&wasmmutate);
193-
194-
assert_eq!(can_mutate, true);
195-
196-
let mutated = mutator
197-
.mutate(&mut wasmmutate)
198-
.unwrap()
199-
.next()
200-
.unwrap()
201-
.unwrap();
202-
203-
let mut validator = wasmparser::Validator::new();
204-
let mutated_bytes = &mutated.finish();
205-
let text = wasmprinter::print_bytes(mutated_bytes).unwrap();
206-
crate::validate(&mut validator, mutated_bytes);
207-
let expected_bytes = &wat::parse_str(expected).unwrap();
208-
let expectedtext = wasmprinter::print_bytes(expected_bytes).unwrap();
209-
assert_eq!(expectedtext, text);
175+
let mut config = WasmMutate::default();
176+
config.seed(seed);
177+
config.match_mutation(original, CodemotionMutator, expected);
210178
}
211179

212180
#[test]
@@ -319,6 +287,8 @@ mod tests {
319287
end
320288
if (result i32)
321289
i32.const 50
290+
else
291+
unreachable
322292
end
323293
)
324294
)

crates/wasm-mutate/src/mutators/function_body_unreachable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Mutator for FunctionBodyUnreachable {
2323
let mut reader = CodeSectionReader::new(code_section.data, 0)?;
2424

2525
let count = reader.get_count();
26-
let function_to_mutate = config.rng().gen_range(0, count);
26+
let function_to_mutate = config.rng().gen_range(0..count);
2727

2828
(0..count)
2929
.map(|i| {

crates/wasm-mutate/src/mutators/modify_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Mutator for ModifyDataMutator {
2121
let mut reader = DataSectionReader::new(config.info().get_data_section().data, 0)?;
2222

2323
// Select an arbitrary data segment to modify.
24-
let data_to_modify = config.rng().gen_range(0, reader.get_count());
24+
let data_to_modify = config.rng().gen_range(0..reader.get_count());
2525

2626
// Iterate over all data segments in the old data section and re-add
2727
// them to the `new_section` one-by-one.

0 commit comments

Comments
 (0)