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

fix failed() cheatcode error #359

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 18 additions & 0 deletions src/halmos/cheatcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,18 @@ class hevm_cheat_code:
)
)

# abi.encodePacked(
# bytes4(keccak256("load(address,bytes32)")),
# abi.encode(HEVM_ADDRESS, bytes32("failed"))
# )
failed_payload = ByteVec(
bytes.fromhex(
"667f9d70"
+ "0000000000000000000000007109709ecfa91a80626ff3989d68f67f5b1dd12d"
+ "6661696c65640000000000000000000000000000000000000000000000000000"
)
)

# bytes4(keccak256("assume(bool)"))
assume_sig: int = 0x4C63E562

Expand Down Expand Up @@ -533,6 +545,12 @@ def handle(sevm, ex, arg: ByteVec, stack, step_id) -> ByteVec | None:

# vm.load(address,bytes32)
elif funsig == hevm_cheat_code.load_sig:
# vm.load(HEVM_ADDRESS, "failed") is handled separately
if arg == hevm_cheat_code.failed_payload:
# since fail(), which triggers vm.store(HEVM_ADDRESS, "failed", 1), halts immediately, (see vm.store() above)
# the "failed" slot is never assigned, thus vm.load(HEVM_ADDRESS, "failed") always return zero at this point
return ByteVec(con(0))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be bad to return 0 if load_account_alias is None?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally we wouldn't handle hevm as an edge case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea! how should we do for vm.store() with nonexistent account?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done for vm.load().

for vm.store(), currently an exception is raised if the given account doesn't exist.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for vm.store(), currently an exception is raised if the given account doesn't exist

ok, so the idea is that we need to first deploy code at an address and then we can vm.store to it? is that because deploying resets the storage?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so far, we have the invariant that for any address x, x in ex.code iff x in ex.storage. now, if we allow vm.store() for nonexistent addresses, the invariant would no longer hold, which leads to some logic changes in the codebase.

another invariant we have is that all addresses in ex.code/ex.storage are concrete. if the given address is symbolic, this invariant would be broken. note that calls to symbolic addresses don't break this, because of branching over concrete aliases, and calls to nonexistent address are essentially no-op.

while these invariants are convenient for various reasons, they aren't absolutely necessary. so we can break them and adjust our logic, if needed, although this would increase code complexity.

i'd suggest we wait until we see concrete use cases where vm.store() is used with nonexistent or symbolic addresses, before deciding how to redesign this logic.


load_account = uint160(arg.get_word(4))
load_slot = uint256(arg.get_word(36))
load_account_alias = sevm.resolve_address_alias(
Expand Down
9 changes: 9 additions & 0 deletions tests/expected/all.json
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,15 @@
"num_paths": null,
"time": null,
"num_bounded_loops": null
},
{
"name": "check_failed_cheatcode()",
"exitcode": 0,
"num_models": 0,
"models": null,
"num_paths": null,
"time": null,
"num_bounded_loops": null
}
],
"test/Foundry.t.sol:FoundryTest": [
Expand Down
5 changes: 5 additions & 0 deletions tests/regression/test/Foundry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ contract EarlyFailTest is Test {
// - counterexample caused by assert(x > 0): x == 0
assert(x > 0);
}

function check_failed_cheatcode() public {
// since fail() halts immediately, failed() always return true here
assertFalse(failed());
}
}

contract FoundryTest is Test {
Expand Down
Loading