Skip to content

Slot of contract with arbitrary storage set to 0 in setUp, but goes back to being arbitrary in the test #667

@lucasmt

Description

@lucasmt

I'm trying to write a setUp function that first uses the setArbitraryStorage cheatcode to make the storage of a contract arbitrary, then uses store to set a single storage slot to 0. This seems to work inside the setUp, but when the test itself is executed, the value of the storage slot goes back to being a random number. This seems to only happen when the value is 0.

Forge version:

forge Version: 1.0.0-stable
Commit SHA: e144b82070619b6e10485c38734b4d4d45aebe04
Build Timestamp: 2025-02-13T20:03:31.026474817Z (1739477011)
Build Profile: maxperf

Minimal Example

pragma solidity ^0.8.25;

import "../lib/forge-std/src/Test.sol";

contract StorageContract {
    uint256 public value;
}

contract ArbitraryStorageTest is Test {
    uint256 constant DEFAULT_VALUE = 0;

    StorageContract target;

    function setUp() public {
        target = new StorageContract();
        vm.setArbitraryStorage(address(target));

        bytes32 valueSlot = bytes32(uint256(0));
        vm.store(address(target), valueSlot, bytes32(DEFAULT_VALUE));
        emit log_named_uint("value in setUp", target.value());
    }

    function test_stored_value() public {
        emit log_named_uint("value in test", target.value());

        assertEq(target.value(), DEFAULT_VALUE);
    }
}

The test fails and this is what the logs show:

[FAIL: assertion failed: 36440280036344177981963967840477496355477405577495123809133350823800994840471 != 0] test_stored_value() (gas: 10093)
Logs:
  value in setUp: 0
  value in test: 36440280036344177981963967840477496355477405577495123809133350823800994840471

However, if DEFAULT_VALUE is changed to 1, the result is correct:

[PASS] test_stored_value() (gas: 10090)
Logs:
  value in setUp: 1
  value in test: 1

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