Repository navigation
fix(symbolic): register folded mapping slots as keccak preimages #18340
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
Open
figtracer
wants to merge
4
commits into
mablr/evm2-migration
Choose a base branch
from
fig/symbolic-folded-slot-preimages
base: mablr/evm2-migration
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.
+346
−16
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a67cb22
fix(symbolic): register folded mapping slots as keccak preimages
figtracer 6344e38
chore(symbolic): fix clippy lints in folded slot registration
figtracer 5a560a7
fix(symbolic): resolve nested folded mapping slots on demand
figtracer 370fc36
fix(symbolic): scope folded-slot searches by contract
figtracer 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
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| //! Registration of constant-folded mapping slots as concrete Keccak preimages. | ||
| //! | ||
| //! With the optimizer on, solc folds the slot of a mapping entry at a constant key, | ||
| //! `keccak256(abi.encode(key, slot))`, into one `PUSH32`. The engine treats a constant slot as a | ||
| //! hash only when it knows a preimage, so the folded slot would otherwise count as a fixed slot | ||
| //! that a write through a symbolic key never reaches. Before a run, the mapping roots in the | ||
| //! storage layout of each deployed contract are hashed with every candidate key: the source | ||
| //! literals and the contract's `PUSH` immediates. Nested mappings get one more level. A registered | ||
| //! preimage is a real one, so this adds no unsoundness; constants that no candidate produces, such | ||
| //! as ERC-1967 and ERC-7201 roots, stay fixed slots. | ||
|
|
||
| use super::*; | ||
| use foundry_common::slot_identifier::{ENCODING_INPLACE, ENCODING_MAPPING}; | ||
| use foundry_compilers::artifacts::{Storage, StorageLayout, StorageType}; | ||
|
|
||
| /// Mapping levels hashed below a storage layout root: the mapping itself and one nested mapping. | ||
| const FOLDED_MAPPING_LEVELS: usize = 2; | ||
|
|
||
| /// Records the folded mapping slots of every deployed contract with a known storage layout. | ||
| pub(crate) fn register_folded_mapping_slots(cx: &mut SymCx, executor: &ExecutorView<'_>) { | ||
| let Some(known_contracts) = executor.known_contracts else { return }; | ||
| let literals = executor | ||
| .literals | ||
| .map(|literals| { | ||
| literals | ||
| .get() | ||
| .words | ||
| .values() | ||
| .flatten() | ||
| .map(|word| (*word).into()) | ||
| .collect::<HashSet<U256>>() | ||
| }) | ||
| .unwrap_or_default(); | ||
| for &address in &executor.account_addresses { | ||
| if let Ok(Some(account)) = executor.backend.basic(address) | ||
| && let Some(code) = &account.code | ||
| && !code.is_empty() | ||
| && let Some((_, contract)) = | ||
| known_contracts.find_by_deployed_code_exact(code.original_byte_slice()) | ||
| && let Some(layout) = &contract.storage_layout | ||
| { | ||
| let mut keys = literals.clone(); | ||
| keys.extend(push_immediates(code.original_byte_slice())); | ||
| let keys = keys.into_iter().collect::<Vec<_>>(); | ||
| for entry in &layout.storage { | ||
| if let Ok(slot) = entry.slot.parse::<U256>() { | ||
| register_mapping_slots( | ||
| cx, | ||
| layout, | ||
| slot, | ||
| &entry.storage_type, | ||
| &keys, | ||
| FOLDED_MAPPING_LEVELS, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Returns the right-aligned values of the `PUSH` immediates in `code`. | ||
| fn push_immediates(code: &[u8]) -> impl Iterator<Item = U256> { | ||
| let mut pc = 0; | ||
| std::iter::from_fn(move || { | ||
| while let Some(&op) = code.get(pc) { | ||
| pc += 1; | ||
| if let opcode::PUSH1..=opcode::PUSH32 = op { | ||
| let len = usize::from(op - opcode::PUSH1 + 1); | ||
| let data = &code[pc..code.len().min(pc + len)]; | ||
| pc += len; | ||
| return Some(U256::from_be_slice(data)); | ||
| } | ||
| } | ||
| None | ||
| }) | ||
| } | ||
|
|
||
| /// Hashes every candidate key that fits the key type of the mapping at `slot`, descending into | ||
| /// struct members and `levels - 1` nested mapping levels. | ||
| fn register_mapping_slots( | ||
| cx: &mut SymCx, | ||
| layout: &StorageLayout, | ||
| slot: U256, | ||
| type_id: &str, | ||
| keys: &[U256], | ||
| levels: usize, | ||
| ) { | ||
| let Some(ty) = layout.types.get(type_id) else { return }; | ||
| if levels == 0 { | ||
| return; | ||
| } | ||
| if ty.encoding == ENCODING_MAPPING { | ||
| let Some((key_ty, value_id)) = | ||
| ty.key.as_deref().and_then(|key| layout.types.get(key)).zip(ty.value.as_deref()) | ||
| else { | ||
| return; | ||
| }; | ||
| for &key in keys.iter().filter(|&&key| key_fits(key_ty, key)) { | ||
| let hash = record_mapping_preimage(cx, key, slot); | ||
| register_mapping_slots(cx, layout, hash, value_id, keys, levels - 1); | ||
| } | ||
| } else if ty.encoding == ENCODING_INPLACE | ||
| && let Some(members) = ty | ||
| .other | ||
| .get("members") | ||
| .and_then(|members| serde_json::from_value::<Vec<Storage>>(members.clone()).ok()) | ||
| { | ||
| for member in &members { | ||
| if let Ok(offset) = member.slot.parse::<U256>() { | ||
| let slot = slot.wrapping_add(offset); | ||
| register_mapping_slots(cx, layout, slot, &member.storage_type, keys, levels); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Returns whether `key` is a value of the mapping key type `ty` as `abi.encode` pads it. | ||
| fn key_fits(ty: &StorageType, key: U256) -> bool { | ||
| let Ok(bytes) = ty.number_of_bytes.parse::<usize>() else { return false }; | ||
| let bits = bytes * 8; | ||
| if bits == 0 || bits > 256 || ty.label == "string" || ty.label == "bytes" { | ||
| // Dynamic keys are hashed from their bytes, not from a padded word. | ||
| false | ||
| } else if ty.label.starts_with("int") { | ||
| let magnitude = U256::ONE << (bits - 1); | ||
| key < magnitude || key >= U256::ZERO.wrapping_sub(magnitude) | ||
| } else if ty.label.starts_with("bytes") { | ||
| key.trailing_zeros() >= 256 - bits | ||
| } else { | ||
| bits == 256 || key < U256::ONE << bits | ||
| } | ||
| } | ||
|
|
||
| /// Records `keccak(key . slot)` with its 64-byte preimage and returns the hash. | ||
| fn record_mapping_preimage(cx: &mut SymCx, key: U256, slot: U256) -> U256 { | ||
| let mut bytes = [0u8; 64]; | ||
| bytes[..32].copy_from_slice(B256::from(key).as_slice()); | ||
| bytes[32..].copy_from_slice(B256::from(slot).as_slice()); | ||
| let hash = keccak256(bytes).into(); | ||
| let preimage = bytes.iter().map(|&byte| SymExpr::constant(cx, U256::from(byte))).collect(); | ||
| cx.record_concrete_keccak_preimage(hash, preimage); | ||
| hash | ||
| } | ||
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.
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.
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.
Please avoid eagerly materializing every candidate-key pair for nested mappings. For
mapping(uint256 => mapping(uint256 => uint256)), this iterates allkeysagain for each outer key and retains N² preimages on every symbolic run, even if the property never accesses the mapping. In a local dev build of this head, a pureassert(x == x)test with that unused mapping and 2,000 unused integer constants took 7.74s and 3.06 GB peak RSS; with 400 constants it took 0.28s and 213 MB. Discovery also has no deadline/cancellation check, so larger projects can exhaust memory or overrun frontier-search budgets. Please avoid retaining the full Cartesian product and honor the deadline while scanning.