fix: sort Foundry sources by ID for correct sourceList ordering#653
Open
Mike4751 wants to merge 4 commits intocrytic:masterfrom
Open
fix: sort Foundry sources by ID for correct sourceList ordering#653Mike4751 wants to merge 4 commits intocrytic:masterfrom
Mike4751 wants to merge 4 commits intocrytic:masterfrom
Conversation
When processing Foundry/Hardhat build-info, the sourceList was generated in JSON iteration order rather than by source ID. This caused bytecode source maps to reference wrong files when Echidna/Slither processed coverage data. Changes: - Add source_id_to_filename mapping to CompilationUnit - Sort sources by ID when parsing build-info - Store source ID mapping during parsing - Use filenames_for_export in solc export to maintain correct indices Fixes source map index mismatch where e.g. sourceList[185] would point to the wrong file because sources were not indexed by their compiler- assigned IDs.
e13a126 to
4a9ab22
Compare
smonicas
reviewed
Mar 10, 2026
crytic_compile/compilation_unit.py
Outdated
|
|
||
| # Build list indexed by source ID | ||
| max_id = max(self._source_id_to_filename.keys()) | ||
| result: list[Filename | None] = [None] * (max_id + 1) |
Collaborator
There was a problem hiding this comment.
It's not clear to me why 1 is added, also shouldn't be max(max_id, len(self._filenames)) to handle the case of self._filenames > max_id?
Author
There was a problem hiding this comment.
Sorry for the delay! The Solidity compiler assigns source IDs starting at 0. Also, you're right I included this change.
crytic_compile/compilation_unit.py
Outdated
| pass | ||
|
|
||
| # Filter out None entries and return | ||
| return [f for f in result if f is not None] |
Collaborator
There was a problem hiding this comment.
Filtering out None elements can move indices, i think is better if there would be a warning or error if a None is present
3dcba28 to
57e6f5c
Compare
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.
Fixes #652
Summary
When building Foundry projects with subdirectories that use relative paths in their
foundry.toml, thesourceListindices in exportedcombined_solc.jsonbecome misaligned with source IDs in bytecode source maps.Files Changed
compilation_unit.py_source_id_to_filenamedict to track ID→filename mappingfilenames_for_exportproperty returning filenames ordered by source IDset_source_id()method to store the mappinghardhat.pyset_source_id()to store the mapping for each sourcesolc.pyfilenames_for_exportinstead offilenamesfor correct orderingTesting
Minimal reproduction: https://github.com/Mike4751/crytic-compile-sourcelist-bug
Backwards Compatibility
✅ Simple/standard projects continue to work correctly
✅ Complex projects with relative paths are now fixed