foundry icon indicating copy to clipboard operation
foundry copied to clipboard

feat(cheatcodes): expose byte offset and mapping base slot in getStorageSlots

Open mablr opened this issue 1 month ago • 3 comments

Component

Forge

Describe the feature you would like

Follow-up to #4022. vm.getStorageSlots(address target, string variableName) (#11537) covers the main request there, looking up a named variable's slot from the storage layout so tests don't hard-code it. Since #17869 it also resolves ERC-7201 namespaced fields such as "openzeppelin.storage.ERC20._totalSupply". Two parts of the original request still aren't covered.

Byte offset of packed variables. The cheatcode returns only slots, so variables that share a slot can't be told apart. With

contract C {
    uint256 bar;
    uint64 a;
    uint64 b;
}

getStorageSlots(c, "a") and getStorageSlots(c, "b") both return [1]. A test that wants to vm.store only b has to hard-code its offset (8) and mask the word itself, which is the brittleness #4022 wanted to remove. The storage layout already has the offset (forge inspect C storageLayout shows it). The same applies to packed fields in ERC-7201 namespaces, such as openzeppelin.storage.Initializable._initialized (offset 0) and ._initializing (offset 8).

Base slot of mappings and dynamic arrays. For these types the cheatcode reverts with

vm.getStorageSlots: cannot get storage slots for variables with mapping or dynamic array types

But the base slot is exactly what a test needs to compute keccak256(abi.encode(key, baseSlot)) for a mapping entry, or keccak256(baseSlot) for array data. For example, that's how you'd write openzeppelin.storage.ERC20._balances[user] without hard-coding the namespace root.

One possible shape, without changing the existing cheatcode's return type:

/// Returns the base slot, byte offset and size in bytes of the named variable.
function getStorageSlot(address target, string calldata variableName)
    external view returns (uint256 slot, uint256 offset, uint256 numberOfBytes);

This one would work for every encoding, including mappings and dynamic arrays, since it reports only the variable's base location. Alternatively getStorageSlots could stop reverting on mappings and dynamic arrays and return [baseSlot], with offset exposed separately.

Additional context

Checked on master 59182ef47 with --extra-output storageLayout.

mablr avatar Nov 02 '26 15:11 mablr

Would it be useful to clarify whether the preferred API should be a new single-location getStorageSlot returning slot, offset, and size, or an extension of getStorageSlots?

DefiAudit0x avatar Nov 09 '26 19:11 DefiAudit0x

@mablr I'd go with a separate getStorageSlot returning (slot, offset, numberOfBytes) that also works for mappings and dynamic arrays, and leave getStorageSlots as is. OK if I open a PR for that?

SibghatUllah1997 avatar Nov 10 '26 11:11 SibghatUllah1997

Yes, please open a PR for a separate getStorageSlot returning (slot, offset, numberOfBytes), while leaving getStorageSlots unchanged. Please include tests for packed variables, mappings, and dynamic arrays. We can review the implementation once it’s ready.

DefiAudit0x avatar Nov 10 '26 12:11 DefiAudit0x