Skip to content
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
5 changes: 5 additions & 0 deletions .changelog/symbolic-mockcall-remock-replace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
forge: patch
---

Fixed symbolic `vm.mockCall`/`vm.mockCalls` to replace mocks with the same callee, value, and calldata.
11 changes: 10 additions & 1 deletion crates/evm/symbolic/src/executor/cheatcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,16 @@ impl SymbolicExecutor {
returns: Vec<SymReturnData>,
reverts: bool,
) -> CheatcodeOutcome {
state.call_mocks.push(CallMock::new(callee, value, data, returns, reverts));
// Replace identical definitions in place to preserve mock precedence.
if let Some(existing) = state.call_mocks.iter_mut().find(|mock| {
mock.callee == callee
&& mock.value() == value
&& mock.data.same_bytes(&mut self.cx, &data)
}) {
*existing = CallMock::new(callee, value, data, returns, reverts);
} else {
state.call_mocks.push(CallMock::new(callee, value, data, returns, reverts));
}
CheatcodeOutcome::Continue(Vec::new())
}

Expand Down
4 changes: 2 additions & 2 deletions crates/evm/symbolic/src/runtime/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,9 +1139,9 @@ impl ExpectedCall {

#[derive(Clone, Debug)]
pub(crate) struct CallMock {
callee: SymExpr,
pub(crate) callee: SymExpr,
value: Option<U256>,
data: SymBytes,
pub(crate) data: SymBytes,
returns: Vec<SymReturnData>,
reverts: bool,
calls: usize,
Expand Down
35 changes: 35 additions & 0 deletions crates/forge/tests/cli/test_cmd/symbolic_cheatcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2936,6 +2936,25 @@ contract SymbolicMockCall is Test {
assertEq(IMockedTarget(target).value(input), input + 2);
assertEq(IMockedTarget(target).value(input), input + 2);
}

function checkMockCallRemockReplacesStaleValue(uint256) public {
address target = address(0x1234);

vm.mockCall(
target,
abi.encodeWithSelector(IMockedTarget.value.selector, uint256(1)),
abi.encode(uint256(10))
);
assertEq(IMockedTarget(target).value(1), 10);

// Re-registering the same mock replaces its return value.
vm.mockCall(
target,
abi.encodeWithSelector(IMockedTarget.value.selector, uint256(1)),
abi.encode(uint256(20))
);
assertEq(IMockedTarget(target).value(1), 20);
}
}
"#,
);
Expand Down Expand Up @@ -3012,6 +3031,22 @@ checkSymbolicCalleeMockMismatch(address)
"#]],
);
assert!(!stdout.contains("symbolic vm.mockCall"), "{stdout}");

let stdout = prj
.forge_command()
.args(["test", "--symbolic", "--match-test", "checkMockCallRemockReplacesStaleValue"])
.assert_success()
.get_output()
.stdout_lossy();

assert_relevant_lines(
&stdout,
foundry_test_utils::str![[r#"
[PASS] checkMockCallRemockReplacesStaleValue(uint256)
"#]],
);
assert!(!stdout.contains("symbolic Foundry cheatcode"), "{stdout}");
assert!(!stdout.contains("symbolic vm.mockCall"), "{stdout}");
});

forgetest_init!(symbolic_vm_call_expectations_allow_symbolic_value_when_unpinned, |prj, cmd| {
Expand Down
Loading