Skip to content

Feat/coverage guided fuzzing #10190

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
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

0xalpharush
Copy link
Contributor

@0xalpharush 0xalpharush commented Mar 28, 2025

Motivation

Right now, all it does is store "interesting" call sequences for invariant mode in a corpus but they aren't mutated or replayed.

Solution

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

error!(%err, "Failed to create invariant corpus dir");
} else if let Err(err) = foundry_common::fs::write_json_file(
corpus_dir.join(timestamp).as_path(),
&current_run.inputs.clone(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we'd have the ABI sig here and not have to call from_invariant_call all of the time to recover it

@@ -315,6 +320,7 @@ impl<'a> InvariantExecutor<'a> {
setup_contracts,
project_contracts,
artifact_filters: ArtifactFilters::default(),
history_map: RefCell::new(vec![0u8; COVERAGE_MAP_SIZE]),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The corpus should probably be ran on startup and populate this from run_invariant_test

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can move this under InvariantTestData

pub struct InvariantTestData {

which is part of InvariantTest execution data

// Data collected during invariant runs.
pub execution_data: RefCell<InvariantTestData>,

that is created / populated on prepare_test

/// Prepares certain structures to execute the invariant tests:
/// * Invariant Fuzz Test.
/// * Invariant Strategy
fn prepare_test(
&mut self,
invariant_contract: &InvariantContract<'_>,
fuzz_fixtures: &FuzzFixtures,
deployed_libs: &[Address],
) -> Result<(InvariantTest, impl Strategy<Value = BasicTxDetails>)> {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ig if the corpus can't be shared globally, this may make sense.

// TODO expose config and add tests
// splice
0 => {
let start1 = rng.gen_range(0..one.len());
Copy link
Collaborator

@grandizzy grandizzy Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's an edge case here when generated start1 is the same as end1 and start2 as end2. This will result in an empty new_seq which will then panic when performing runs in initial_seq[0].clone(),

    let mut current_run = InvariantTestRun::new(
        initial_seq[0].clone(),
        // Before each run, we must reset the backend state.
        self.executor.clone(),
        self.config.depth as usize,
);

should we maybe just test if for empty and if it is empty then fallback into proptest strategy new tree?

if !new_seq.is_empty() {
    return new_seq;
}

let item = &tx[item_idx];
for i in start..end {
new_seq[i] = item.clone();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to return the new_seq here too or not needed?

.current(),
);
// dictionary if initial seq isn't `depth` long.
if current_run.depth as usize > initial_seq.len().saturating_sub(1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should apply this logic for discarded inputs as well, otherwise the corpus will get stuck at the same discarded sequence and won't advance (invariant::test_invariant_assume_does_not_revert test failure)

if discarded || (current_run.depth as usize > initial_seq.len().saturating_sub(1)) {

@grandizzy grandizzy force-pushed the feat/coverage-guided-fuzzing branch from abb95de to 3f148eb Compare March 31, 2025 09:28
@grandizzy grandizzy force-pushed the feat/coverage-guided-fuzzing branch from 3f148eb to e7a878f Compare March 31, 2025 10:23
@jenpaff jenpaff moved this to Ready For Review in Foundry Apr 15, 2025
@0xalpharush
Copy link
Contributor Author

b7f09d8 operated under the assumption that the libafl function updated the history map, but it doesn't. So we either need to revert back to the other way or iterate over the history map and set every index to the "classified" hitcount when there's new coverage

@grandizzy
Copy link
Collaborator

b7f09d8 operated under the assumption that the libafl function updated the history map, but it doesn't. So we either need to revert back to the other way or iterate over the history map and set every index to the "classified" hitcount when there's new coverage

I think reverting and using the other way should work here, do you see any big disadvantages using one or another?

@0xalpharush
Copy link
Contributor Author

Mainly perf considerations... But it can wait and we avoid taking a dep on libafl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Ready For Review
Development

Successfully merging this pull request may close these issues.

2 participants