Add --prefill-corpus option to extract transaction sequences from Foundry tests#1523
Draft
gustavo-grieco wants to merge 14 commits intomasterfrom
Draft
Add --prefill-corpus option to extract transaction sequences from Foundry tests#1523gustavo-grieco wants to merge 14 commits intomasterfrom
--prefill-corpus option to extract transaction sequences from Foundry tests#1523gustavo-grieco wants to merge 14 commits intomasterfrom
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR adds a new feature that extracts transaction sequences from Foundry test functions and uses them to seed Echidna's corpus. This gives the fuzzer a head start by leveraging existing test cases as starting points for mutation-based fuzzing.
Motivation
When fuzzing a contract that already has Foundry tests, those tests contain valuable information about meaningful transaction sequences. Rather than starting from scratch, Echidna can use these sequences as seeds, potentially finding bugs faster by mutating known-good call patterns.
Usage
CLI flag
Config file (
echidna.yaml)How it works
test/directory relative to the source filetest*,testFuzz*, orinvariant_*patternsExample output
Note:
getCount(view) andadd(pure) calls are automatically filtered out.Implementation details
New files
lib/Echidna/SourceAnalysis/FoundryTests.hs- Haskell module for extraction orchestrationlib/Echidna/SourceAnalysis/assets/extract_foundry_tests.py- Python script using Slither (embedded at compile time via Template Haskell)tests/solidity/foundry-prefill/- Test fixtures for the featuresrc/test/Tests/PrefillCorpus.hs- Unit testsModified files
lib/Echidna/Types/Solidity.hs- AddedprefillCorpus :: Boolfield toSolConflib/Echidna/Config.hs- Added YAML parser forprefillCorpussrc/Main.hs- Added--prefill-corpusCLI option and override logiclib/Echidna.hs- Integrated prefilled corpus intoloadInitialCorpuspackage.yaml- Addedtemplate-haskelldependencyKey design decisions
Embedded Python script - The extraction script is embedded at compile time using Template Haskell, similar to how mustache templates are embedded. This ensures the script is always available without requiring external file installation.
Slither-based extraction - Uses Slither's IR (Intermediate Representation) to accurately identify external contract calls, rather than parsing Solidity source directly.
Target contract filtering - When
--contractis specified, only calls to that contract type are extracted, avoiding noise from helper contracts.View/pure filtering - Read-only functions are automatically excluded since they don't contribute to state exploration.
Requirements
pip install slither-analyzer)If Python or Slither is not available, the feature gracefully degrades with a warning and continues without prefilling.
Testing
Tests verify:
Limitations
Future improvements
forge test --listfor test discovery