Skip to content

Commit

Permalink
chore: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Evalir committed Oct 17, 2023
1 parent 8bc67d2 commit 691e3d5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 36 deletions.
31 changes: 17 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/evm/src/fuzz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<'a> FuzzedExecutor<'a> {
counterexample: None,
decoded_logs: decode_console_logs(&call.logs),
logs: call.logs,
labeled_addresses: call.labels.into_iter().map(|l| (l.0.to_ethers(), l.1)).collect(),
labeled_addresses: call.labels,
traces: if run_result.is_ok() { traces.into_inner() } else { call.traces.clone() },
coverage: coverage.into_inner(),
};
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/fuzz/strategies/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl IntStrategy {
let rng = runner.rng();

let offset = I256::from_raw(U256::from(rng.gen_range(0..4)));
let umax: U256 = (U256::from(1u8).rotate_left(self.bits - 1)).sub(U256::from(1u8));
let umax: U256 = (U256::from(1u8).shl(self.bits - 1)).sub(U256::from(1u8));
// Choose if we want values around min, -0, +0, or max
let kind = rng.gen_range(0..4);
let start = match kind {
Expand Down
31 changes: 15 additions & 16 deletions crates/evm/src/fuzz/strategies/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,30 @@ pub fn fuzz_param_from_state(
.collect::<Vec<_>>()
.prop_map(DynSolValue::Tuple)
.boxed(),
_ => panic!("Unimplemented"),
_ => unimplemented!(),
}
}

#[cfg(test)]
mod tests {
use crate::fuzz::strategies::{build_initial_state, fuzz_calldata, fuzz_calldata_from_state};
use alloy_json_abi::Function;
use ethers::abi::HumanReadableParser;
use foundry_config::FuzzDictionaryConfig;
use revm::db::{CacheDB, EmptyDB};

// TODO: Need a human readable function parser to re-enable the test.
// #[test]
// fn can_fuzz_array() {
// let f = "function testArray(uint64[2] calldata values)";
// let func = HumanReadableParser::parse_function(f).unwrap();
// let db = CacheDB::new(EmptyDB::default());
// let state = build_initial_state(&db, &FuzzDictionaryConfig::default());
// let strat = proptest::strategy::Union::new_weighted(vec![
// (60, fuzz_calldata(func.clone())),
// (40, fuzz_calldata_from_state(func, state)),
// ]);
// let cfg = proptest::test_runner::Config { failure_persistence: None, ..Default::default()
// }; let mut runner = proptest::test_runner::TestRunner::new(cfg);
// let _ = runner.run(&strat, |_| Ok(()));
// }
#[test]
fn can_fuzz_array() {
let f = "function testArray(uint64[2] calldata values)";
let func = Function::parse(f).unwrap();
let db = CacheDB::new(EmptyDB::default());
let state = build_initial_state(&db, &FuzzDictionaryConfig::default());
let strat = proptest::strategy::Union::new_weighted(vec![
(60, fuzz_calldata(func.clone())),
(40, fuzz_calldata_from_state(func, state)),
]);
let cfg = proptest::test_runner::Config { failure_persistence: None, ..Default::default()
}; let mut runner = proptest::test_runner::TestRunner::new(cfg);
let _ = runner.run(&strat, |_| Ok(()));
}
}
6 changes: 3 additions & 3 deletions crates/evm/src/fuzz/strategies/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn build_initial_state<DB: DatabaseRef>(
for (address, account) in db.accounts.iter() {
let address: Address = *address;
// Insert basic account information
state.values_mut().insert(B256::from_slice(address.as_slice()).into());
state.values_mut().insert(B256::from_slice(address.into_word()).into());

// Insert push bytes
if config.include_push_bytes {
Expand Down Expand Up @@ -136,7 +136,7 @@ pub fn build_initial_state<DB: DatabaseRef>(
// fuzzing
if state.values().is_empty() {
// prefill with a random addresses
state.values_mut().insert(B256::from_slice(Address::random().as_slice()).into());
state.values_mut().insert(B256::from_slice(Address::random().into_word()).into());
}

Arc::new(RwLock::new(state))
Expand All @@ -153,7 +153,7 @@ pub fn collect_state_from_call(

for (address, account) in state_changeset {
// Insert basic account information
state.values_mut().insert(B256::from_slice(address.as_slice()).into());
state.values_mut().insert(B256::from_slice(address.into_word()).into());

if config.include_push_bytes && state.addresses.len() < config.max_fuzz_dictionary_addresses
{
Expand Down
4 changes: 3 additions & 1 deletion crates/evm/src/fuzz/strategies/uint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Shl;

use ethers::core::rand::Rng;
use proptest::{
strategy::{NewTree, Strategy, ValueTree},
Expand Down Expand Up @@ -99,7 +101,7 @@ impl UintStrategy {
let is_min = rng.gen_bool(0.5);
let offset = U256::from(rng.gen_range(0..4));
let max = if self.bits < 256 {
(U256::from(1u8).rotate_left(self.bits)) - U256::from(1)
(U256::from(1u8).shl(self.bits)) - U256::from(1)
} else {
U256::MAX
};
Expand Down

0 comments on commit 691e3d5

Please sign in to comment.