Check if context is static for state-changing opcodes (#973)

* Check if context is static for state-changing opcodes

* PR feedback
This commit is contained in:
wborgeaud 2023-04-12 08:24:33 +02:00 committed by GitHub
parent 142be4e114
commit c7e60073f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 7 deletions

View File

@ -2,7 +2,15 @@
// Creates a new sub context and executes the code of the given account.
global sys_call:
// Check that the value is zero if the context is static.
// stack: kexit_info, gas, address, value, args_offset, args_size, ret_offset, ret_size
DUP4 ISZERO %not_bit
// stack: value0, kexit_info, gas, address, value, args_offset, args_size, ret_offset, ret_size
%mload_context_metadata(@CTX_METADATA_STATIC)
// stack: is_static, value0, kexit_info, gas, address, value, args_offset, args_size, ret_offset, ret_size
MUL // Cheaper than AND
%jumpi(fault_exception)
SWAP2
// stack: address, gas, kexit_info, value, args_offset, args_size, ret_offset, ret_size
%u256_to_addr // Truncate to 160 bits
@ -311,7 +319,7 @@ global after_call_instruction:
// Compute C_xfer
// stack: Caaccess, address, gas, kexit_info, value
DUP5 ISZERO PUSH 1 SUB
DUP5 ISZERO %not_bit
// stack: value0, Caaccess, address, gas, kexit_info, value
DUP1
%mul_const(@GAS_CALLVALUE)
@ -336,7 +344,7 @@ global after_call_instruction:
// stack: leftover_gas<Cextra, leftover_gas, Cextra, address, gas, kexit_info, value
DUP5 DUP2 MUL
// stack: (leftover_gas<Cextra)*gas, leftover_gas<Cextra, leftover_gas, Cextra, address, gas, kexit_info, value
SWAP1 PUSH 1 SUB
SWAP1 %not_bit
// stack: leftover_gas>=Cextra, (leftover_gas<Cextra)*gas, leftover_gas, Cextra, address, gas, kexit_info, value
DUP4 DUP4 SUB
// stack: leftover_gas - Cextra, leftover_gas>=Cextra, (leftover_gas<Cextra)*gas, leftover_gas, Cextra, address, gas, kexit_info, value
@ -355,7 +363,7 @@ global after_call_instruction:
// Compute C_callgas
%stack (kexit_info, Cgascap, address, gas, value) ->
(Cgascap, address, gas, kexit_info, value)
DUP5 ISZERO PUSH 1 SUB
DUP5 ISZERO %not_bit
// stack: value!=0, Cgascap, address, gas, kexit_info, value
%mul_const(@GAS_CALLSTIPEND) ADD
%stack (C_callgas, address, gas, kexit_info, value) ->

View File

@ -4,6 +4,7 @@
// Pre stack: kexit_info, value, code_offset, code_len
// Post stack: address
global sys_create:
%check_static
// stack: kexit_info, value, code_offset, code_len
// TODO: Charge gas.
%stack (kexit_info, value, code_offset, code_len)
@ -25,6 +26,7 @@ sys_create_got_address:
// Pre stack: kexit_info, value, code_offset, code_len, salt
// Post stack: address
global sys_create2:
%check_static
// stack: kexit_info, value, code_offset, code_len, salt
// TODO: Charge gas.
SWAP4

View File

@ -16,12 +16,17 @@ global sys_chainid:
SWAP1
EXIT_KERNEL
global sys_log0:
%check_static
PANIC
global sys_log1:
%check_static
PANIC
global sys_log2:
%check_static
PANIC
global sys_log3:
%check_static
PANIC
global sys_log4:
%check_static
PANIC

View File

@ -45,6 +45,7 @@ sys_return_finish:
%jump(terminate_common)
global sys_selfdestruct:
%check_static
// stack: kexit_info, recipient
SWAP1 %u256_to_addr
%address DUP1 %balance

View File

@ -283,3 +283,10 @@ global sys_basefee:
%jumpi(fault_exception)
// stack: (empty)
%endmacro
// Convenience macro for checking if the current context is static.
// Called before state-changing opcodes.
%macro check_static
%mload_context_metadata(@CTX_METADATA_STATIC)
%jumpi(fault_exception)
%endmacro

View File

@ -4,6 +4,7 @@
// Post stack: (empty)
global sys_sstore:
%check_static
%stack (kexit_info, slot, value) -> (slot, kexit_info, slot, value)
%address %insert_accessed_storage_keys POP // TODO: Use return value in gas calculation.
// TODO: Assuming a cold zero -> nonzero write for now.

View File

@ -360,8 +360,6 @@
%macro not_bit
// stack: b
PUSH 1
// stack: 1, b
SUB
// stack: 1 - b
ISZERO
// stack: not b
%endmacro