Skip to content

Add support for _storeData and _loadData #14

Open
@palinatolmach

Description

@palinatolmach

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:
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions