Skip to main content

Module folded_slots

Module folded_slots 

Source
Expand description

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. A mapping entry that setUp wrote is such a constant slot in the backend, and a read through a symbolic key only selects it with a known preimage. Before a run, the mapping roots in the storage layout of each deployed contract are hashed with every candidate key: the source literals, the addresses of the backend’s accounts, and the contract’s PUSH immediates. The nested level of a nested mapping is a product over the literals and immediates, so it is searched only when the run accesses a large constant slot that the contract’s code pushes, and only the match is recorded. 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.

Re-exportsΒ§

use foundry_common::slot_identifier::ENCODING_INPLACE; πŸ”’
use foundry_common::slot_identifier::ENCODING_MAPPING; πŸ”’
use foundry_compilers::artifacts::Storage; πŸ”’
use foundry_compilers::artifacts::StorageLayout; πŸ”’
use foundry_compilers::artifacts::StorageType; πŸ”’
use super::*; πŸ”’

StructsΒ§

FoldedContract πŸ”’
FoldedSlots πŸ”’
Candidate keys and storage layouts of the deployed contracts, kept to resolve the nested level of a folded mapping slot on demand.

ConstantsΒ§

MAX_NESTED_MAPPING_HASHES πŸ”’

FunctionsΒ§

key_fits πŸ”’
Returns whether key is a value of the mapping key type ty as abi.encode pads it.
mapping_preimage πŸ”’
Returns keccak(key . slot) and its 64-byte preimage.
past_deadline πŸ”’
push_immediates πŸ”’
Returns the right-aligned values of the PUSH immediates in code.
record_mapping_preimage πŸ”’
Records keccak(key . slot) with its preimage and returns the hash.
record_preimage_bytes πŸ”’
walk_mapping_slots πŸ”’
Calls hash for every candidate key that fits the key type of the mapping at slot, descending into struct members and levels - 1 nested mapping levels through the returned hash. Returns false when hash stopped the walk.