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

Draft
wants to merge 3 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
78 changes: 63 additions & 15 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 "}"
// ------------------------------------------
```

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

```k
rule [foundry.catchNonWhitelistedCalls]:
<k> (#call _ ACCTTO _ _ _ _ false
<k> #call _ ACCTTO _ _ _ CALLDATA false
/*
~> #popCallStack
~> #popWorldState) => #end KONTROL_WHITELISTCALL ... </k>
~> #popWorldState)
*/ => #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 +1006,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>
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 @@ -1591,14 +1617,14 @@ If the flag is false, it skips comparison, assuming success; otherwise, it compa
- `#addAddressToWhitelist` enables the whitelist restriction for calls and adds an address to the whitelist.

```k
syntax KItem ::= "#addAddressToWhitelist" Int [symbol(foundry_addAddressToWhitelist)]
// syntax KItem ::= "#addAddressToWhitelist" Int [symbol(foundry_addAddressToWhitelist)]
// -------------------------------------------------------------------------------------
rule <k> #addAddressToWhitelist ACCT => .K ... </k>
<whitelist>
<isCallWhitelistActive> _ => true </isCallWhitelistActive>
<addressList> WLIST => WLIST ListItem(ACCT) </addressList>
...
</whitelist>
// 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.
Expand Down Expand Up @@ -1632,6 +1658,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)]
// ---------------------------------------------------------------------------------
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 +1792,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
1 change: 1 addition & 0 deletions src/kontrol/prove.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ def _init_cterm(
'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
Loading