-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: add timeouts to fuzz testing #9394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
47bf075
b2a5f8d
a9003b3
b704851
73c1b77
af9324a
851275e
364c33b
4111e13
da87095
b6c137e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ use eyre::Result; | |
| use foundry_common::evm::Breakpoints; | ||
| use foundry_config::FuzzConfig; | ||
| use foundry_evm_core::{ | ||
| constants::MAGIC_ASSUME, | ||
| constants::{MAGIC_ASSUME, TEST_TIMEOUT}, | ||
| decode::{RevertDecoder, SkipReason}, | ||
| }; | ||
| use foundry_evm_coverage::HitMaps; | ||
|
|
@@ -17,7 +17,11 @@ use foundry_evm_fuzz::{ | |
| use foundry_evm_traces::SparsedTraceArena; | ||
| use indicatif::ProgressBar; | ||
| use proptest::test_runner::{TestCaseError, TestError, TestRunner}; | ||
| use std::{cell::RefCell, collections::BTreeMap}; | ||
| use std::{ | ||
| cell::RefCell, | ||
| collections::BTreeMap, | ||
| time::{Duration, Instant}, | ||
| }; | ||
|
|
||
| mod types; | ||
| pub use types::{CaseOutcome, CounterExampleOutcome, FuzzOutcome}; | ||
|
|
@@ -98,7 +102,17 @@ impl FuzzedExecutor { | |
| let max_traces_to_collect = std::cmp::max(1, self.config.gas_report_samples) as usize; | ||
| let show_logs = self.config.show_logs; | ||
|
|
||
| // Start a timer if timeout is set. | ||
| let start_time = start_timer(self.config.timeout); | ||
|
|
||
| let run_result = self.runner.clone().run(&strategy, |calldata| { | ||
| // Check if the timeout has been reached. | ||
| if let Some((start_time, timeout)) = start_time { | ||
| if start_time.elapsed() > timeout { | ||
DaniPopes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return Err(TestCaseError::fail(TEST_TIMEOUT)); | ||
| } | ||
| } | ||
|
|
||
| let fuzz_res = self.single_fuzz(address, should_fail, calldata)?; | ||
|
|
||
| // If running with progress then increment current run. | ||
|
|
@@ -193,17 +207,21 @@ impl FuzzedExecutor { | |
| } | ||
| Err(TestError::Fail(reason, _)) => { | ||
| let reason = reason.to_string(); | ||
| result.reason = (!reason.is_empty()).then_some(reason); | ||
|
|
||
| let args = if let Some(data) = calldata.get(4..) { | ||
| func.abi_decode_input(data, false).unwrap_or_default() | ||
| if reason == TEST_TIMEOUT { | ||
| // If the reason is a timeout, we consider the fuzz test successful. | ||
| result.success = true; | ||
| } else { | ||
| vec![] | ||
| }; | ||
| result.reason = (!reason.is_empty()).then_some(reason); | ||
| let args = if let Some(data) = calldata.get(4..) { | ||
| func.abi_decode_input(data, false).unwrap_or_default() | ||
| } else { | ||
| vec![] | ||
| }; | ||
smartcontracts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| result.counterexample = Some(CounterExample::Single( | ||
| BaseCounterExample::from_fuzz_call(calldata, args, call.traces), | ||
| )); | ||
| result.counterexample = Some(CounterExample::Single( | ||
| BaseCounterExample::from_fuzz_call(calldata, args, call.traces), | ||
| )); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -270,3 +288,8 @@ impl FuzzedExecutor { | |
| } | ||
| } | ||
| } | ||
|
|
||
| /// Starts timer for fuzz test, if any timeout configured. | ||
| pub(crate) fn start_timer(timeout: Option<u32>) -> Option<(Instant, Duration)> { | ||
| timeout.map(|timeout| (Instant::now(), Duration::from_secs(timeout.into()))) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.