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

feat(cheatcodes): stopExpectSafeMemory #7028

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,10 @@ interface Vm {
#[cheatcode(group = Testing, safety = Unsafe)]
function expectSafeMemory(uint64 min, uint64 max) external;

/// Stops all safe memory expectation in the current subcontext.
#[cheatcode(group = Testing, safety = Unsafe)]
function stopExpectSafeMemory() external;

/// Only allows memory writes to offsets [0x00, 0x60) ∪ [min, max) in the next created subcontext.
/// If any other memory is written to, the test will fail. Can be called multiple times to add more ranges
/// to the set.
Expand Down
8 changes: 8 additions & 0 deletions crates/cheatcodes/src/test/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ impl Cheatcode for expectSafeMemoryCall {
}
}

impl Cheatcode for stopExpectSafeMemoryCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self {} = self;
ccx.state.allowed_mem_writes.remove(&ccx.data.journaled_state.depth());
Ok(Default::default())
}
}

impl Cheatcode for expectSafeMemoryCallCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self { min, max } = *self;
Expand Down
41 changes: 41 additions & 0 deletions testdata/cheats/MemSafety.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,47 @@ contract MemSafetyTest is DSTest {
}
}

////////////////////////////////////////////////////////////////
// `stopExpectSafeMemory` cheatcode //
////////////////////////////////////////////////////////////////

/// @dev Tests that the `stopExpectSafeMemory` cheatcode works as expected.
function testStopExpectSafeMemory() public {
uint64 initPtr;
assembly {
initPtr := mload(0x40)
}

vm.expectSafeMemory(initPtr, initPtr + 0x20);
assembly {
// write to allowed range
mstore(initPtr, 0x01)
}

vm.stopExpectSafeMemory();

assembly {
// write ouside allowed range, this should be fine
mstore(add(initPtr, 0x20), 0x01)
}
}

/// @dev Tests that the `stopExpectSafeMemory` cheatcode does not cause violations not being noticed.
function testFailStopExpectSafeMemory() public {
uint64 initPtr;
assembly {
initPtr := mload(0x40)
}

vm.expectSafeMemory(initPtr, initPtr + 0x20);
assembly {
// write outside of allowed range, this should revert
mstore(add(initPtr, 0x20), 0x01)
}

vm.stopExpectSafeMemory();
}

////////////////////////////////////////////////////////////////
// HELPERS //
////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading