-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(fuzz): enhance corpus mutation with all-call strategy and msg.value support #13177
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
Draft
grandizzy
wants to merge
8
commits into
foundry-rs:master
Choose a base branch
from
grandizzy:feat/constraint-guided-abi-mutation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1465ae0
feat(fuzz): enhance corpus mutation with all-call strategy and msg.va…
grandizzy 23d39bc
Merge branch 'master' into feat/constraint-guided-abi-mutation
grandizzy 9ebe3d8
Merge remote-tracking branch 'origin/master' into feat/constraint-gui…
grandizzy 679fe6d
Merge upstream/master
grandizzy d452197
feat(fuzz): respect targeted/excluded senders in corpus abi_mutate
grandizzy 40fd045
feat(fuzz): rename mutation types to GenPrefix/GenSuffix/GenMutate
grandizzy 6ce3a1e
feat(fuzz): add max_deal config to fund senders for payable functions
grandizzy cda8d11
Merge branch 'master' into feat/constraint-guided-abi-mutation
grandizzy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,7 @@ use foundry_config::FuzzCorpusConfig; | |
| use foundry_evm_fuzz::{ | ||
| BasicTxDetails, | ||
| invariant::FuzzRunIdentifiedContracts, | ||
| strategies::{EvmFuzzState, mutate_param_value}, | ||
| strategies::{EvmFuzzState, generate_msg_value, mutate_param_value}, | ||
| }; | ||
| use proptest::{ | ||
| prelude::{Just, Rng, Strategy}, | ||
|
|
@@ -560,12 +560,23 @@ impl WorkerCorpus { | |
|
|
||
| new_seq = corpus.tx_seq.clone(); | ||
|
|
||
| let idx = rng.random_range(0..new_seq.len()); | ||
| let tx = new_seq.get_mut(idx).unwrap(); | ||
| if let (_, Some(function)) = targets.fuzzed_artifacts(tx) { | ||
| // TODO: add call_value to call details and mutate it as well as sender some | ||
| // of the time. | ||
| if !function.inputs.is_empty() { | ||
| // 30% chance to mutate ALL calls in the sequence. | ||
| // This helps break multi-constraint bugs where any call could hit the target. | ||
| if rng.random_range(0..10) < 3 { | ||
| for tx in &mut new_seq { | ||
| if let (_, Some(function)) = targets.fuzzed_artifacts(tx) | ||
| && !function.inputs.is_empty() | ||
| { | ||
| self.abi_mutate(tx, function, test_runner, fuzz_state)?; | ||
| } | ||
| } | ||
| } else { | ||
| // Standard: mutate a single random call. | ||
| let idx = rng.random_range(0..new_seq.len()); | ||
| let tx = new_seq.get_mut(idx).unwrap(); | ||
| if let (_, Some(function)) = targets.fuzzed_artifacts(tx) | ||
| && !function.inputs.is_empty() | ||
| { | ||
| self.abi_mutate(tx, function, test_runner, fuzz_state)?; | ||
| } | ||
| } | ||
|
|
@@ -687,7 +698,26 @@ impl WorkerCorpus { | |
| test_runner: &mut TestRunner, | ||
| fuzz_state: &EvmFuzzState, | ||
| ) -> Result<()> { | ||
| // let rng = test_runner.rng(); | ||
| // Mutate sender with 15% probability using addresses from dictionary. | ||
| if test_runner.rng().random_ratio(15, 100) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this also check targeted/excluded senders as was done here #13090?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added in d452197 |
||
| let dict = fuzz_state.dictionary_read(); | ||
| let addresses = dict.addresses(); | ||
| if !addresses.is_empty() { | ||
| let idx = test_runner.rng().random_range(0..addresses.len()); | ||
| if let Some(&addr) = addresses.get_index(idx) { | ||
| tx.sender = addr; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Mutate value with 15% probability for payable functions. | ||
| if function.state_mutability == alloy_json_abi::StateMutability::Payable | ||
| && test_runner.rng().random_ratio(15, 100) | ||
| { | ||
| tx.call_details.value = Some(generate_msg_value(test_runner)); | ||
| } | ||
|
|
||
| // Mutate calldata. | ||
| let mut arg_mutation_rounds = | ||
| test_runner.rng().random_range(0..=function.inputs.len()).max(1); | ||
| let round_arg_idx: Vec<usize> = if function.inputs.len() <= 1 { | ||
|
|
@@ -1104,6 +1134,7 @@ mod tests { | |
| call_details: foundry_evm_fuzz::CallDetails { | ||
| target: Address::ZERO, | ||
| calldata: Bytes::new(), | ||
| value: None, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Prefixmutation is essentially this but a subset and generation instead of mutation. Maybe we should haveGenPrefixandGenMutateand mutate up to every element, but not always every elementThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, will merge with prefix mutations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushed 40fd045
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the name
GenMutatewas a mistake on my part and the old nameAbiwas better. The implementation is fine though.What I was suggesting was to add two new mutators,
MutatePrefixandMutateSuffix, where gen means callnew_txand mutate means use a tx in the seq and mutate it withabi_mutate(optionally you can have even an identity one which clones existing txs) . Here is a patch to reduce confusion. It also fixes the repeat mutation to insert instead of splice (which overwrites) as well as adds a swap and delete mutation.