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

Add allowCalls cheatcode #926

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
6305559
Draft for the `allowCalls` implementation
palinatolmach Jan 8, 2025
6f33d53
Make `AllowedCallCellMap` empty in init cterm
palinatolmach Jan 10, 2025
f4a75d7
`allowedCallsList` refactoring, add `allowedAllCalls`
palinatolmach Jan 10, 2025
2d0f9ef
Merge branch 'master' into allow-calls-cheatcode
palinatolmach Jan 13, 2025
5c47b68
Add new rule, K cell pattern
palinatolmach Jan 13, 2025
248e7c0
Merge branch 'master' into allow-calls-cheatcode
palinatolmach Jan 14, 2025
fcc15d9
Merge branch 'master' into allow-calls-cheatcode
palinatolmach Jan 16, 2025
759caeb
Add list simplifications
palinatolmach Jan 16, 2025
50bf832
Adjust and add tests for `allowCalls`
palinatolmach Jan 16, 2025
7e5e33b
Add tests for `allowCalls` to end-to-end tests
palinatolmach Jan 16, 2025
bff0810
Update expected output in `end-to-end` tests
palinatolmach Jan 16, 2025
001cf55
Remove fixed `kontrol-cheatcodes` version
palinatolmach Jan 16, 2025
e5ab34b
Removed a TODO comment
palinatolmach Jan 16, 2025
a4ad642
`cheatcodes.md` cleanup
palinatolmach Jan 16, 2025
eb4b118
Remove `ADDRESSLIST_CELL`
palinatolmach Jan 16, 2025
5a32c70
`AllowChangesTest` cleanup
palinatolmach Jan 16, 2025
7918983
Another output update
palinatolmach Jan 16, 2025
f5a8b12
Update expected output
palinatolmach Jan 16, 2025
3181d14
Update CSE expected output
palinatolmach Jan 16, 2025
2ffc75d
Update `trace` tests output
palinatolmach Jan 16, 2025
0c4380b
Use `auxiliary_lemmas` in end-to-end tests
palinatolmach Jan 16, 2025
bef331a
Output update for `RandomVarTest`
palinatolmach Jan 16, 2025
232ec94
Enable `auxiliary_lemmas` in `build` instead of `prove`
palinatolmach Jan 16, 2025
c9cce21
Merge branch 'master' into allow-calls-cheatcode
palinatolmach Jan 16, 2025
c7d2bab
Change `testFailAllowCalls_ifNotWhitelisted` signature
palinatolmach Jan 16, 2025
376146c
Remove `expectRevert` from failing test
palinatolmach Jan 16, 2025
b758ee0
Apply review suggestion
palinatolmach Jan 16, 2025
a9fae41
Another output update for `RandomVarTest`
palinatolmach Jan 16, 2025
7a09749
Reduce `end-to-end` parallel processes to 6
palinatolmach Jan 17, 2025
33d6eb5
update expected output
anvacaru Jan 17, 2025
c412328
Apply `CallToAddress` review suggestion
palinatolmach Jan 17, 2025
ab78122
Use `.Bytes` to represent all calls being allowed
palinatolmach Jan 17, 2025
0eeb826
Another output update for `RandomVarTest.test_custom_names`
palinatolmach Jan 17, 2025
a533c8b
Experiment: add `--force-sequential` to end-to-end tests
palinatolmach Jan 19, 2025
44cc1dc
Reduce num processes in the update-output job to 4
palinatolmach Jan 19, 2025
bc8c5f9
Update `RandomVarTest.test_custom_names`
palinatolmach Jan 19, 2025
e8b1db5
update expected output
anvacaru Jan 20, 2025
63ee68e
Merge branch 'master' into allow-calls-cheatcode
anvacaru Jan 20, 2025
2c03441
see full diff in ci
anvacaru Jan 21, 2025
5fbe1cc
show diff in ci
anvacaru Jan 21, 2025
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
76 changes: 54 additions & 22 deletions src/kontrol/kdist/cheatcodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ module FOUNDRY-CHEAT-CODES
</expectEmit>
<whitelist>
<isCallWhitelistActive> false </isCallWhitelistActive>
<allowedCallsList> .List </allowedCallsList>
<isStorageWhitelistActive> false </isStorageWhitelistActive>
<addressList> .List </addressList>
<storageSlotList> .List </storageSlotList>
</whitelist>
<mockCalls>
Expand Down Expand Up @@ -946,6 +946,7 @@ A `StorageSlot` pair is formed from an address and a storage index.

```k
syntax StorageSlot ::= "{" Int "|" Int "}"
syntax CallToAddress ::= "{" Int "|" Bytes "}"
// ------------------------------------------
palinatolmach marked this conversation as resolved.
Show resolved Hide resolved
```

Expand All @@ -965,15 +966,14 @@ If the address is not in the whitelist `WLIST` then `KEVM` goes into an error st

```k
rule [foundry.catchNonWhitelistedCalls]:
<k> (#call _ ACCTTO _ _ _ _ false
~> #popCallStack
~> #popWorldState) => #end KONTROL_WHITELISTCALL ... </k>
<k> (#call _ ACCTTO _ _ _ CALLDATA false
~> #return _ _) => #end KONTROL_WHITELISTCALL ... </k>
<whitelist>
<isCallWhitelistActive> true </isCallWhitelistActive>
<addressList> WLIST </addressList>
<allowedCallsList> WLIST </allowedCallsList>
...
</whitelist>
requires notBool ACCTTO in WLIST
requires notBool ({ACCTTO|CALLDATA} in WLIST orBool {ACCTTO|b"*"} in WLIST)
[priority(40)]
```

Expand Down Expand Up @@ -1003,9 +1003,32 @@ function allowCallsToAddress(address) external;
Adds an account address to the whitelist. The execution of the modified KEVM will stop when a call has been made to an address which is not in the whitelist.

```k
rule [foundry.allowCallsToAddress]:
<k> #cheatcode_call SELECTOR ARGS => #loadAccount #asWord(ARGS) ~> #addAddressToWhitelist #asWord(ARGS) ... </k>
requires SELECTOR ==Int selector ( "allowCallsToAddress(address)" )
rule [foundry.allowAllCallsToAddress]:
<k> #cheatcode_call SELECTOR ARGS
=> #loadAccount #asWord(ARGS)
~> #setAllowedAllCalls #asWord(ARGS) ... </k>
palinatolmach marked this conversation as resolved.
Show resolved Hide resolved
requires SELECTOR ==Int selector("allowCallsToAddress(address)")
```

#### `allowCalls` - Add an account address and calldata prefix to a whitelist.

```
function allowCalls(address, bytes calldata data) external;
```

Adds an account address and calldata prefix to the whitelist. The execution of the modified KEVM will stop when a call has been made to an address and/or with calldata which are not in the whitelist.

```k
rule [foundry.allowCalls]:
<k> #cheatcode_call SELECTOR ARGS
=> #loadAccount #asWord(#range(ARGS, 0, 32))
~> #etchAccountIfEmpty #asWord(#range(ARGS, 0, 32))
~> #setAllowedCall
#asWord(#range(ARGS, 0, 32))
#range(ARGS, #asWord(#range(ARGS, 32, 32)) +Int 32, #asWord(#range(ARGS, #asWord(#range(ARGS, 32, 32)), 32)))
...
</k>
requires SELECTOR ==Int selector ( "allowCalls(address,bytes)" )
```

#### `allowChangesToStorage` - Add an account address and a storage slot to a whitelist.
Expand Down Expand Up @@ -1588,19 +1611,6 @@ If the flag is false, it skips comparison, assuming success; otherwise, it compa
rule #checkTopics(ListItem(CHECK) CHECKS, ListItem(V1) L1, ListItem(V2) L2) => #checkTopic(CHECK, V1, V2) andBool #checkTopics(CHECKS, L1, L2) requires size(L1) ==Int size(L2)
```

- `#addAddressToWhitelist` enables the whitelist restriction for calls and adds an address to the whitelist.

```k
syntax KItem ::= "#addAddressToWhitelist" Int [symbol(foundry_addAddressToWhitelist)]
// -------------------------------------------------------------------------------------
rule <k> #addAddressToWhitelist ACCT => .K ... </k>
<whitelist>
<isCallWhitelistActive> _ => true </isCallWhitelistActive>
<addressList> WLIST => WLIST ListItem(ACCT) </addressList>
...
</whitelist>
```

- `#addStorageSlotToWhitelist` enables the whitelist restriction for storage chagnes and adds a `StorageSlot` item to the whitelist.

```k
Expand Down Expand Up @@ -1632,6 +1642,27 @@ If the flag is false, it skips comparison, assuming success; otherwise, it compa
rule <k> #etchAccountIfEmpty _ => .K ... </k> [owise]
```

- `#setAllowedCall ALLOWEDACCOUNT ALLOWEDCALLDATA` and `setAllowedAllCalls ALLOWEDACCOUNT` will update the `<allowedCallsList>` list with the given account and calldata.

```k
syntax KItem ::= "#setAllowedCall" Account Bytes [symbol(foundry_setAllowedCall)]
syntax KItem ::= "#setAllowedAllCalls" Account [symbol(foundry_setAllowedAllCalls)]
// ---------------------------------------------------------------------------------
palinatolmach marked this conversation as resolved.
Show resolved Hide resolved
rule <k> #setAllowedCall ALLOWEDACCOUNT ALLOWEDCALLDATA => .K ... </k>
<whitelist>
<isCallWhitelistActive> _ => true </isCallWhitelistActive>
<allowedCallsList> ALLOWEDCALLS => ALLOWEDCALLS ListItem({ALLOWEDACCOUNT|ALLOWEDCALLDATA}) </allowedCallsList>
...
</whitelist>

rule <k> #setAllowedAllCalls ALLOWEDACCOUNT => .K ... </k>
<whitelist>
<isCallWhitelistActive> _ => true </isCallWhitelistActive>
<allowedCallsList> ALLOWEDCALLS => ALLOWEDCALLS ListItem({ALLOWEDACCOUNT|b"*"}) </allowedCallsList>
...
</whitelist>
```

- `#setMockCall MOCKADDRESS MOCKCALLDATA MOCKRETURN` will update the `<mockcalls>` mapping for the given account.

```k
Expand Down Expand Up @@ -1745,6 +1776,7 @@ Selectors for **implemented** cheat code functions.
rule ( selector ( "prank(address)" ) => 3395723175 )
rule ( selector ( "prank(address,address)" ) => 1206193358 )
rule ( selector ( "allowCallsToAddress(address)" ) => 1850795572 )
rule ( selector ( "allowCalls(address,bytes)" ) => 1808051021 )
rule ( selector ( "allowChangesToStorage(address,uint256)" ) => 4207417100 )
rule ( selector ( "infiniteGas()" ) => 3986649939 )
rule ( selector ( "setGas(uint256)" ) => 3713137314 )
Expand Down
13 changes: 13 additions & 0 deletions src/kontrol/kdist/kontrol_lemmas.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,18 @@ module KONTROL-AUX-LEMMAS
requires 0 <Int X andBool 0 <=Int Z andBool ( Z +Int 1) modInt X ==Int 0
[simplification, concrete(X, Z), preserves-definedness]

//
// List simplifications for `allowCalls`
//

// List membership check simplification for lists with a single element
rule KI:KItem in ListItem(KI:KItem) => true [simplification]
rule KI:KItem in ListItem(KJ:KItem) => KI ==K KJ [simplification]

// Recursive list membership check for lists with multiple elements
rule KI:KItem in (ListItem(KI) Rest) => true [simplification]
rule KI:KItem in (ListItem(KJ) Rest) => KI in Rest [simplification]
rule KI:KItem in .List => false [simplification]

endmodule
```
4 changes: 2 additions & 2 deletions src/kontrol/prove.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,8 @@ def _init_cterm(
'ISEVENTEXPECTED_CELL': FALSE,
'ISCALLWHITELISTACTIVE_CELL': FALSE,
'ISSTORAGEWHITELISTACTIVE_CELL': FALSE,
'ADDRESSLIST_CELL': list_empty(),
'STORAGESLOTLIST_CELL': list_empty(),
'ALLOWEDCALLSLIST_CELL': list_empty(),
'MOCKCALLS_CELL': KApply('.MockCallCellMap'),
'MOCKFUNCTIONS_CELL': KApply('.MockFunctionCellMap'),
'ACTIVETRACING_CELL': TRUE if trace_options.active_tracing else FALSE,
Expand Down Expand Up @@ -1425,7 +1425,7 @@ def _final_term(
'ISEVENTEXPECTED_CELL': KVariable('ISEVENTEXPECTED_FINAL'),
'ISCALLWHITELISTACTIVE_CELL': KVariable('ISCALLWHITELISTACTIVE_FINAL'),
'ISSTORAGEWHITELISTACTIVE_CELL': KVariable('ISSTORAGEWHITELISTACTIVE_FINAL'),
'ADDRESSLIST_CELL': KVariable('ADDRESSLIST_FINAL'),
'ALLOWEDCALLSLIST_CELL': KVariable('ALLOWEDCALLSLIST_FINAL'),
'STORAGESLOTLIST_CELL': KVariable('STORAGESLOTLIST_FINAL'),
}

Expand Down
2 changes: 2 additions & 0 deletions src/tests/integration/test-data/end-to-end-prove-all
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
AllowChangesTest.testAllowCalls(uint256)
AllowChangesTest.testAllowCalls_failIfNotWhitelisted(uint256)
CounterTest.test_Increment()
RandomVarTest.test_custom_names()
RandomVarTest.test_randomBool()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ contract AllowChangesTest is Test, KontrolCheats {
}
function testFailAllowCallsToAddress() public {
kevm.allowCallsToAddress(address(canChange));
kevm.allowChangesToStorage(address(canChange), 0);

cannotChange.changeSlot0(10245);
}

function testFailAllowChangesToStorage() public {
kevm.allowCallsToAddress(address(canChange));
kevm.allowChangesToStorage(address(canChange), 0);

canChange.changeSlot1(23452);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ module SUMMARY-TEST%ACCOUNTPARAMSTEST.TESTDEALCONCRETE():0
<isCallWhitelistActive>
false
</isCallWhitelistActive>
<allowedCallsList>
.List
</allowedCallsList>
<isStorageWhitelistActive>
false
</isStorageWhitelistActive>
<addressList>
.List
</addressList>
<storageSlotList>
.List
</storageSlotList>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
<isCallWhitelistActive>
false
</isCallWhitelistActive>
<allowedCallsList>
.List
</allowedCallsList>
<isStorageWhitelistActive>
false
</isStorageWhitelistActive>
<addressList>
.List
</addressList>
<storageSlotList>
.List
</storageSlotList>
Expand Down Expand Up @@ -368,12 +368,12 @@ module SUMMARY-SRC%CSE%ADDCONST.APPLYOP(UINT256):0
<isCallWhitelistActive>
false
</isCallWhitelistActive>
<allowedCallsList>
.List
</allowedCallsList>
<isStorageWhitelistActive>
false
</isStorageWhitelistActive>
<addressList>
.List
</addressList>
<storageSlotList>
.List
</storageSlotList>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ module SUMMARY-TEST%ADDRTEST.TEST-ADDR-TRUE():0
<isCallWhitelistActive>
false
</isCallWhitelistActive>
<allowedCallsList>
.List
</allowedCallsList>
<isStorageWhitelistActive>
false
</isStorageWhitelistActive>
<addressList>
.List
</addressList>
<storageSlotList>
.List
</storageSlotList>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,12 @@ module SUMMARY-TEST%ARITHMETICCALLTEST.TEST-DOUBLE-ADD(UINT256,UINT256):0
<isCallWhitelistActive>
false
</isCallWhitelistActive>
<allowedCallsList>
.List
</allowedCallsList>
<isStorageWhitelistActive>
false
</isStorageWhitelistActive>
<addressList>
.List
</addressList>
<storageSlotList>
.List
</storageSlotList>
Expand Down Expand Up @@ -859,12 +859,12 @@ module SUMMARY-TEST%ARITHMETICCALLTEST.TEST-DOUBLE-ADD(UINT256,UINT256):0
<isCallWhitelistActive>
false
</isCallWhitelistActive>
<allowedCallsList>
.List
</allowedCallsList>
<isStorageWhitelistActive>
false
</isStorageWhitelistActive>
<addressList>
.List
</addressList>
<storageSlotList>
.List
</storageSlotList>
Expand Down Expand Up @@ -1250,12 +1250,12 @@ module SUMMARY-TEST%ARITHMETICCALLTEST.TEST-DOUBLE-ADD(UINT256,UINT256):0
<isCallWhitelistActive>
false
</isCallWhitelistActive>
<allowedCallsList>
.List
</allowedCallsList>
<isStorageWhitelistActive>
false
</isStorageWhitelistActive>
<addressList>
.List
</addressList>
<storageSlotList>
.List
</storageSlotList>
Expand Down Expand Up @@ -1643,12 +1643,12 @@ module SUMMARY-TEST%ARITHMETICCALLTEST.TEST-DOUBLE-ADD(UINT256,UINT256):0
<isCallWhitelistActive>
false
</isCallWhitelistActive>
<allowedCallsList>
.List
</allowedCallsList>
<isStorageWhitelistActive>
false
</isStorageWhitelistActive>
<addressList>
.List
</addressList>
<storageSlotList>
.List
</storageSlotList>
Expand Down Expand Up @@ -2037,12 +2037,12 @@ module SUMMARY-TEST%ARITHMETICCALLTEST.TEST-DOUBLE-ADD(UINT256,UINT256):0
<isCallWhitelistActive>
false
</isCallWhitelistActive>
<allowedCallsList>
.List
</allowedCallsList>
<isStorageWhitelistActive>
false
</isStorageWhitelistActive>
<addressList>
.List
</addressList>
<storageSlotList>
.List
</storageSlotList>
Expand Down
Loading
Loading