Skip to content

Commit 06f713c

Browse files
committed
cleanup fuzz corpus on forge clean, display errors
1 parent 3f0a9cb commit 06f713c

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

crates/config/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,7 @@ impl Config {
10701070
}
10711071
};
10721072
remove_test_dir(&self.fuzz.failure_persist_dir);
1073+
remove_test_dir(&self.fuzz.corpus.corpus_dir);
10731074
remove_test_dir(&self.invariant.corpus.corpus_dir);
10741075
remove_test_dir(&self.invariant.failure_persist_dir);
10751076

crates/evm/evm/src/executors/corpus.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,15 @@ impl CorpusManager {
196196

197197
let can_reply_tx = |tx: &BasicTxDetails| -> bool {
198198
fuzzed_contracts.is_some_and(|contracts| contracts.targets.lock().can_replay(tx))
199-
|| fuzzed_function
200-
.is_some_and(|function| function.selector() == tx.call_details.calldata[..4])
199+
|| fuzzed_function.is_some_and(|function| {
200+
tx.call_details
201+
.calldata
202+
.get(..4)
203+
.is_some_and(|selector| function.selector() == selector)
204+
})
201205
};
202206

203-
for entry in std::fs::read_dir(corpus_dir)? {
207+
'corpus_replay: for entry in std::fs::read_dir(corpus_dir)? {
204208
let path = entry?.path();
205209
if path.is_file()
206210
&& let Some(name) = path.file_name().and_then(|s| s.to_str())
@@ -246,6 +250,12 @@ impl CorpusManager {
246250
}
247251
} else {
248252
failed_replays += 1;
253+
254+
// If the only input for fuzzed function cannot be replied, then move to
255+
// next one without adding it in memory.
256+
if fuzzed_function.is_some() {
257+
continue 'corpus_replay;
258+
}
249259
}
250260
}
251261

@@ -368,7 +378,7 @@ impl CorpusManager {
368378
let mutation_type = self
369379
.mutation_generator
370380
.new_tree(test_runner)
371-
.expect("Could not generate mutation type")
381+
.map_err(|err| eyre!("Could not generate mutation type {err}"))?
372382
.current();
373383
let rng = test_runner.rng();
374384
let corpus_len = self.in_memory_corpus.len();
@@ -491,7 +501,6 @@ impl CorpusManager {
491501
let corpus = &self.in_memory_corpus
492502
[test_runner.rng().random_range(0..self.in_memory_corpus.len())];
493503
self.current_mutated = Some(corpus.uuid);
494-
495504
let new_seq = corpus.tx_seq.clone();
496505
let mut tx = new_seq.first().unwrap().clone();
497506
self.abi_mutate(&mut tx, function, test_runner, fuzz_state)?;
@@ -573,10 +582,7 @@ impl CorpusManager {
573582
debug!(target: "corpus", "evict corpus {uuid}");
574583

575584
// Flush to disk the seed metadata at the time of eviction.
576-
let eviction_time = SystemTime::now()
577-
.duration_since(UNIX_EPOCH)
578-
.expect("Time went backwards")
579-
.as_secs();
585+
let eviction_time = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
580586
foundry_common::fs::write_json_file(
581587
self.config
582588
.corpus_dir
@@ -611,7 +617,7 @@ impl CorpusManager {
611617
};
612618
let mut prev_inputs = function
613619
.abi_decode_input(&tx.call_details.calldata[4..])
614-
.expect("function cannot abi decode input");
620+
.map_err(|err| eyre!("failed to load previous inputs: {err}"))?;
615621

616622
// For now, only new inputs are generated, no existing inputs are
617623
// mutated.

crates/evm/evm/src/executors/fuzz/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,15 @@ impl FuzzedExecutor {
160160

161161
test_data.runs += 1;
162162

163-
let Ok(input) = corpus_manager.new_input(&mut self.runner, &state, func) else {
164-
test_data.failure =
165-
Some(TestCaseError::fail("no input generated to call fuzzed target"));
166-
break 'stop;
167-
};
168-
169-
input
163+
match corpus_manager.new_input(&mut self.runner, &state, func) {
164+
Ok(input) => input,
165+
Err(err) => {
166+
test_data.failure = Some(TestCaseError::fail(format!(
167+
"failed to generate fuzzed input: {err}"
168+
)));
169+
break 'stop;
170+
}
171+
}
170172
};
171173

172174
match self.single_fuzz(address, input, &mut corpus_manager) {

0 commit comments

Comments
 (0)