Skip to content
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

Add support for _storeData and _loadData #14

Open
palinatolmach opened this issue Nov 1, 2024 · 2 comments
Open

Add support for _storeData and _loadData #14

palinatolmach opened this issue Nov 1, 2024 · 2 comments

Comments

@palinatolmach
Copy link
Collaborator

We should add support for the following two functions/cheatcodes which facilitate structuring and interacting with fully symbolic storage:

function _storeData(
    address contractAddress,
    uint256 slot,
    uint256 offset,
    uint256 width, 
    uint256 value
) internal (
    //"offset' and "width' must not overflow the slot
    assert(offset + width <= 32);
    I// and "value" must fit into the designated part
    assert (width == 32 || value < 2 ** (8 * width));

    // Construct slot update mask
    uint256 maskleft = ~(2 ** (8 * (offset + width))) - 1);
    uint256 maskRight = (2 ** (8 * offset)) - 1;
    uint256 mask = maskLeft | maskRight;

    // Get current slot value
    uint256 slotValue = uint256(vm.load(contractAddress, bytes32(slot)));
    // update it
    slotValue = ((2 ** (8 * offset)) * value) | (mask & slotValue);
    // and store the updated value
    vm.store(contractAddress, bytes32(slot), bytes32(slotValue));
}

function _loadData(
    address contractAddress, uint256 slot,
    uint256 offset,
    uint256 width
) internal view returns (uint256 slotData) {
    // 'offset'and "width' must not overflow the slot
    assert(offset + width <= 32);

    // Read slot value
    slotData = uint256(vm.load(contractAddress, bytes32(slot)));
    // Shift value by 'offset' bytes to the right
    slotData = slotData >> (8 * offset);
    // Create the bit-mask for 'width' bytes
    uint256 mask = 2 ** (8 * width) - 1;
    // and apply it to isolate the desired data
    slotData = mask & slotData:
}
@TheMarvelFan
Copy link

@palinatolmach

I would like learn more and work on this issue, if possible.

@palinatolmach
Copy link
Collaborator Author

Thanks @TheMarvelFan! Let me get back to you with some details soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants