Skip to content

Commit

Permalink
feat(cheatcodes): stopExpectSafeMemory (#7028)
Browse files Browse the repository at this point in the history
* add cheat

* fix spec
  • Loading branch information
klkvr authored Feb 7, 2024
1 parent 8885e97 commit 5ef0d6f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
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.

0 comments on commit 5ef0d6f

Please sign in to comment.