Skip to content

Experimental: update cheatcode preconditions in CSE #1029

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
52 changes: 49 additions & 3 deletions src/kontrol/prove.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ def _init_cterm(
'CALLER_CELL': KVariable('CALLER_ID', sort=KSort('Int')),
'LOCALMEM_CELL': bytesToken(b''),
'ACTIVE_CELL': FALSE,
'DEPTH_CELL': intToken(0),
'MEMORYUSED_CELL': intToken(0),
'WORDSTACK_CELL': KApply('.WordStack_EVM-TYPES_WordStack'),
'PC_CELL': intToken(0),
Expand All @@ -935,6 +936,7 @@ def _init_cterm(
'ISREVERTEXPECTED_CELL': FALSE,
'ISOPCODEEXPECTED_CELL': FALSE,
'RECORDEVENT_CELL': FALSE,
'EXPECTEDDEPTH_CELL': intToken(0),
'ISEVENTEXPECTED_CELL': FALSE,
'ISCALLWHITELISTACTIVE_CELL': FALSE,
'ISSTORAGEWHITELISTACTIVE_CELL': FALSE,
Expand All @@ -950,7 +952,7 @@ def _init_cterm(
'TRACEDATA_CELL': KApply('.List'),
}

storage_constraints: list[KApply] = []
cse_constraints: list[KApply] = []

if config_type == ConfigType.TEST_CONFIG or active_simbolik:
init_account_list = (
Expand Down Expand Up @@ -991,7 +993,7 @@ def _init_cterm(
accounts.append(Foundry.symbolic_account(contract_account_name, contract_code))
else:
# Symbolic accounts of all relevant contracts
accounts, storage_constraints = _create_cse_accounts(
accounts, cse_constraints = _create_cse_accounts(
foundry, storage_fields, contract_account_name, contract_code
)

Expand All @@ -1006,6 +1008,50 @@ def _init_cterm(
if not isinstance(method, Contract.Constructor) and not (method.view or method.pure):
init_subst['STATIC_CELL'] = FALSE

# TODO:
# andBool notBool (ACTIVE_CELL orBool PRAKNDEPTH_CELL >=Int CALLDEPTH_CELL)
# andBool notBool (EXPECTED_REVERT_CELL orBool REVERTDEPTH_CELL >=Int CALLDEPTH_CELL)

# Assume we're not in an active prank context
inactive_prank_constraint = mlEqualsTrue(
notBool(
KApply(
'_orBool_',
[
KVariable('ACTIVE_CELL', sort=KSort('Bool')),
KApply(
'_>=Int_',
[
KVariable('DEPTH_CELL', sort=KSort('Int')),
KVariable('CALLDEPTH_CELL', sort=KSort('Int')),
],
),
],
)
)
)
inactive_expect_revert_constraint = mlEqualsTrue(
notBool(
KApply(
'_orBool_',
[
KVariable('ISREVERTEXPECTED_CELL', sort=KSort('Bool')),
KApply(
'_>=Int_',
[
KVariable('EXPECTEDDEPTH_CELL', sort=KSort('Int')),
KVariable('CALLDEPTH_CELL', sort=KSort('Int')),
],
),
],
)
)
)
cse_constraints += [
inactive_prank_constraint,
inactive_expect_revert_constraint,
]

if calldata is not None:
init_subst['CALLDATA_CELL'] = calldata

Expand All @@ -1030,7 +1076,7 @@ def _init_cterm(
if preconditions is not None:
for precondition in preconditions:
init_cterm = init_cterm.add_constraint(mlEqualsTrue(precondition))
for constraint in storage_constraints:
for constraint in cse_constraints:
init_cterm = init_cterm.add_constraint(constraint)

non_cheatcode_contract_ids = []
Expand Down
2 changes: 2 additions & 0 deletions src/tests/integration/test-data/foundry-dependency-all
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ AddConst.applyOp(uint256)
ArithmeticCallTest.test_double_add(uint256,uint256)
ArithmeticCallTest.test_double_add_double_sub(uint256,uint256)
ArithmeticCallTest.test_double_add_sub_external(uint256,uint256,uint256)
ArithmeticCallTest.test_double_add_sub_external_revert(uint256,uint256,uint256)
ArithmeticCallTest.test_double_add_sub_external_prank(uint256,uint256,uint256)
ArithmeticContract.add(uint256,uint256)
ArithmeticContract.add_sub_external(uint256,uint256,uint256)
CallableStorageContract.str()
Expand Down
18 changes: 18 additions & 0 deletions src/tests/integration/test-data/foundry/test/ArithmeticCall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,22 @@ contract ArithmeticCallTest is Test {
a = arith.add_sub_external(a, y, z);
assert(a > x);
}

function test_double_add_sub_external_revert(uint x, uint y, uint z) external {
vm.assume(x == type(uint256).max);
vm.assume(y > 0);

// the call should revert due to overflow in `add`
vm.expectRevert();
uint a = arith.add_sub_external(x, y, z);
assert(a > x);
}

function test_double_add_sub_external_prank(uint x, uint y, uint z) external {
address prankCaller = address(0xBEEF);

vm.prank(prankCaller);
uint a = arith.add_sub_external(x, y, z);
assert(a > x);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ module SUMMARY-TEST%ACCOUNTPARAMSTEST.TESTDEALCONCRETE():0
<active>
false
</active>
<depth>
0
</depth>
<singleCall>
false
</singleCall>
Expand All @@ -237,6 +240,9 @@ module SUMMARY-TEST%ACCOUNTPARAMSTEST.TESTDEALCONCRETE():0
<isRevertExpected>
false
</isRevertExpected>
<expectedDepth>
0
</expectedDepth>
...
</expectedRevert>
<expectedOpcode>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
<callGas>
0
</callGas>
<callDepth>
CALLDEPTH_CELL:Int
</callDepth>
...
</callState>
<substate>
Expand Down Expand Up @@ -179,6 +182,24 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
true
</stackChecks>
<cheatcodes>
<prank>
<active>
false
</active>
<depth>
0
</depth>
...
</prank>
<expectedRevert>
<isRevertExpected>
false
</isRevertExpected>
<expectedDepth>
0
</expectedDepth>
...
</expectedRevert>
<expectedOpcode>
<isOpcodeExpected>
false
Expand Down Expand Up @@ -228,7 +249,9 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
</traceData>
</KEVMTracing>
</foundry>
requires ( 0 <=Int KV0_x:Int
requires ( ( notBool _ACTIVE_CELL:Bool )
andBool ( ( notBool _ISREVERTEXPECTED_CELL:Bool )
andBool ( 0 <=Int KV0_x:Int
andBool ( 0 <=Int CALLER_ID:Int
andBool ( 0 <=Int ORIGIN_ID:Int
andBool ( 0 <=Int C_ADDCONST_ID:Int
Expand All @@ -238,6 +261,10 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
andBool ( NUMBER_CELL:Int <Int pow32
andBool ( 1073741824 <Int TIMESTAMP_CELL:Int
andBool ( TIMESTAMP_CELL:Int <Int 34359738368
andBool ( DEPTH_CELL:Int <Int CALLDEPTH_CELL:Int
andBool ( ( notBool CALLDEPTH_CELL:Int <=Int DEPTH_CELL:Int )
andBool ( EXPECTEDDEPTH_CELL:Int <Int CALLDEPTH_CELL:Int
andBool ( ( notBool CALLDEPTH_CELL:Int <=Int EXPECTEDDEPTH_CELL:Int )
andBool ( C_ADDCONST_NONCE:Int <Int maxUInt64
andBool ( CALLER_ID:Int =/=Int 645326474426547203313410069153905908525362434349
andBool ( ORIGIN_ID:Int =/=Int 645326474426547203313410069153905908525362434349
Expand All @@ -254,7 +281,7 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
andBool ( ( notBool #range ( 0 < ORIGIN_ID:Int <= 10 ) )
andBool ( ( notBool #range ( 0 < C_ADDCONST_ID:Int <= 10 ) )
andBool ( KV0_x:Int <=Int ( maxUInt256 -Int #lookup ( C_ADDCONST_STORAGE:Map , 0 ) )
))))))))))))))))))))))))
))))))))))))))))))))))))))))))
[priority(20), label(BASIC-BLOCK-8-TO-6)]

rule [BASIC-BLOCK-9-TO-7]: <foundry>
Expand Down Expand Up @@ -305,6 +332,9 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
<callGas>
0
</callGas>
<callDepth>
CALLDEPTH_CELL:Int
</callDepth>
...
</callState>
<substate>
Expand Down Expand Up @@ -358,6 +388,24 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
true
</stackChecks>
<cheatcodes>
<prank>
<active>
false
</active>
<depth>
0
</depth>
...
</prank>
<expectedRevert>
<isRevertExpected>
false
</isRevertExpected>
<expectedDepth>
0
</expectedDepth>
...
</expectedRevert>
<expectedOpcode>
<isOpcodeExpected>
false
Expand Down Expand Up @@ -407,7 +455,9 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
</traceData>
</KEVMTracing>
</foundry>
requires ( 0 <=Int KV0_x:Int
requires ( ( notBool _ACTIVE_CELL:Bool )
andBool ( ( notBool _ISREVERTEXPECTED_CELL:Bool )
andBool ( 0 <=Int KV0_x:Int
andBool ( 0 <=Int CALLER_ID:Int
andBool ( 0 <=Int ORIGIN_ID:Int
andBool ( 0 <=Int C_ADDCONST_ID:Int
Expand All @@ -417,6 +467,10 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
andBool ( NUMBER_CELL:Int <Int pow32
andBool ( 1073741824 <Int TIMESTAMP_CELL:Int
andBool ( TIMESTAMP_CELL:Int <Int 34359738368
andBool ( DEPTH_CELL:Int <Int CALLDEPTH_CELL:Int
andBool ( ( notBool CALLDEPTH_CELL:Int <=Int DEPTH_CELL:Int )
andBool ( EXPECTEDDEPTH_CELL:Int <Int CALLDEPTH_CELL:Int
andBool ( ( notBool CALLDEPTH_CELL:Int <=Int EXPECTEDDEPTH_CELL:Int )
andBool ( C_ADDCONST_NONCE:Int <Int maxUInt64
andBool ( CALLER_ID:Int =/=Int 645326474426547203313410069153905908525362434349
andBool ( ORIGIN_ID:Int =/=Int 645326474426547203313410069153905908525362434349
Expand All @@ -433,7 +487,7 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
andBool ( ( notBool #range ( 0 < ORIGIN_ID:Int <= 10 ) )
andBool ( ( notBool #range ( 0 < C_ADDCONST_ID:Int <= 10 ) )
andBool ( ( maxUInt256 -Int #lookup ( C_ADDCONST_STORAGE:Map , 0 ) ) <Int KV0_x:Int
))))))))))))))))))))))))
))))))))))))))))))))))))))))))
[priority(20), label(BASIC-BLOCK-9-TO-7)]

endmodule
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ module SUMMARY-TEST%ADDRTEST.TEST-ADDR-TRUE():0
<active>
false
</active>
<depth>
0
</depth>
<singleCall>
false
</singleCall>
Expand All @@ -197,6 +200,9 @@ module SUMMARY-TEST%ADDRTEST.TEST-ADDR-TRUE():0
<isRevertExpected>
false
</isRevertExpected>
<expectedDepth>
0
</expectedDepth>
...
</expectedRevert>
<expectedOpcode>
Expand Down
Loading
Loading