Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Apr 9, 2024
1 parent 9568184 commit d52674f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 77 deletions.
1 change: 0 additions & 1 deletion tests/cancun/eip1153_tstore/test_basic_tload.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ def test_basic_tload_gasprice(
(16_tloadGasFiller.yml)
tload costs 100 gas same as a warm sload
"""

slot_tload_nonzero_gas_price_result = 1
slot_tload_zero_gas_price_result = 2
slot_code_worked = 3
Expand Down
38 changes: 19 additions & 19 deletions tests/cancun/eip1153_tstore/test_tload_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@pytest.mark.parametrize("call_type", [Op.CALL, Op.CALLCODE, Op.DELEGATECALL])
def test_tload_calls(state_test: StateTestFiller, call_type: Op):
"""
Covered .json vectors:
Ported .json vectors:
(04_tloadAfterCallFiller.yml)
Loading a slot after a call to another contract is 0.
Expand All @@ -37,10 +37,10 @@ def test_tload_calls(state_test: StateTestFiller, call_type: Op):
address_call = Address("B00000000000000000000000000000000000000B")

# Storage variables
str_a_tload_after_subcall_result = 0
str_a_subcall_result = 1
str_b_subcall_tload_result = 2
str_b_subcall_updated_tload_result = 3
slot_a_tload_after_subcall_result = 0
slot_a_subcall_result = 1
slot_b_subcall_tload_result = 2
slot_b_subcall_updated_tload_result = 3

def make_call(call_type: Op) -> bytes:
if call_type == Op.DELEGATECALL or call_type == Op.STATICCALL:
Expand All @@ -54,23 +54,23 @@ def make_call(call_type: Op) -> bytes:
nonce=0,
code=Op.JUMPDEST()
+ Op.TSTORE(0, 10)
+ Op.SSTORE(str_a_subcall_result, make_call(call_type))
+ Op.SSTORE(str_a_tload_after_subcall_result, Op.TLOAD(0)),
+ Op.SSTORE(slot_a_subcall_result, make_call(call_type))
+ Op.SSTORE(slot_a_tload_after_subcall_result, Op.TLOAD(0)),
storage={
str_a_subcall_result: 0xFF,
str_a_tload_after_subcall_result: 0xFF,
slot_a_subcall_result: 0xFF,
slot_a_tload_after_subcall_result: 0xFF,
},
),
address_call: Account(
balance=7000000000000000000,
nonce=0,
code=Op.JUMPDEST()
+ Op.SSTORE(str_b_subcall_tload_result, Op.TLOAD(0))
+ Op.SSTORE(slot_b_subcall_tload_result, Op.TLOAD(0))
+ Op.TSTORE(0, 20)
+ Op.SSTORE(str_b_subcall_updated_tload_result, Op.TLOAD(0)),
+ Op.SSTORE(slot_b_subcall_updated_tload_result, Op.TLOAD(0)),
storage={
str_b_subcall_tload_result: 0xFF,
str_b_subcall_updated_tload_result: 0xFF,
slot_b_subcall_tload_result: 0xFF,
slot_b_subcall_updated_tload_result: 0xFF,
},
),
TestAddress: Account(
Expand All @@ -86,18 +86,18 @@ def make_call(call_type: Op) -> bytes:
post[address_to] = Account(
storage={
# other calls don't change context, there for tload updated in this account
str_a_tload_after_subcall_result: 10 if call_type == Op.CALL else 20,
str_a_subcall_result: 1,
slot_a_tload_after_subcall_result: 10 if call_type == Op.CALL else 20,
slot_a_subcall_result: 1,
# since context unchanged the subcall works as if continued execution
str_b_subcall_tload_result: 0 if call_type == Op.CALL else 10,
str_b_subcall_updated_tload_result: 0 if call_type == Op.CALL else 20,
slot_b_subcall_tload_result: 0 if call_type == Op.CALL else 10,
slot_b_subcall_updated_tload_result: 0 if call_type == Op.CALL else 20,
}
)

post[address_call] = Account(
storage={
str_b_subcall_tload_result: 0 if call_type == Op.CALL else 0xFF,
str_b_subcall_updated_tload_result: 20 if call_type == Op.CALL else 0xFF,
slot_b_subcall_tload_result: 0 if call_type == Op.CALL else 0xFF,
slot_b_subcall_updated_tload_result: 20 if call_type == Op.CALL else 0xFF,
}
)

Expand Down
42 changes: 21 additions & 21 deletions tests/cancun/eip1153_tstore/test_tload_reentrancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_tload_reentrancy(
state_test: StateTestFiller, call_type: Op, call_return: Op, call_dest: bytes
):
"""
Covered .json vectors:
Ported .json vectors:
(05_tloadReentrancyFiller.yml)
Reentrant calls access the same transient storage
Expand All @@ -44,10 +44,10 @@ def test_tload_reentrancy(
empty_value = 0

# Storage variables
str_tload_in_subcall_result = 1
str_tload_after_subcall_result = 2
str_subcall_worked = 3
str_code_worked = 4
slot_tload_in_subcall_result = 1
slot_tload_after_subcall_result = 2
slot_subcall_worked = 3
slot_code_worked = 4

# Function names
do_load = 1
Expand Down Expand Up @@ -76,19 +76,19 @@ def make_call(call_type: Op) -> bytes:
action=Op.TSTORE(0, tload_value)
+ Op.MSTORE(0, do_load)
+ Op.MSTORE(32, 0xFF)
+ Op.SSTORE(str_subcall_worked, make_call(call_type))
+ Op.SSTORE(str_tload_in_subcall_result, Op.MLOAD(32))
+ Op.SSTORE(str_tload_after_subcall_result, Op.TLOAD(0))
+ Op.SSTORE(str_code_worked, 1),
+ Op.SSTORE(slot_subcall_worked, make_call(call_type))
+ Op.SSTORE(slot_tload_in_subcall_result, Op.MLOAD(32))
+ Op.SSTORE(slot_tload_after_subcall_result, Op.TLOAD(0))
+ Op.SSTORE(slot_code_worked, 1),
),
],
default_action=b"",
),
storage={
str_tload_in_subcall_result: 0xFF,
str_tload_after_subcall_result: 0xFF,
str_subcall_worked: 0xFF,
str_code_worked: 0xFF,
slot_tload_in_subcall_result: 0xFF,
slot_tload_after_subcall_result: 0xFF,
slot_subcall_worked: 0xFF,
slot_code_worked: 0xFF,
},
),
address_code: Account(
Expand All @@ -111,11 +111,11 @@ def make_call(call_type: Op) -> bytes:
# if reentrancy
post[address_to] = Account(
storage={
str_code_worked: 1,
slot_code_worked: 1,
# if call OOG, we fail to obtain the result
str_tload_in_subcall_result: 0xFF if call_return == Om.OOG else tload_value,
str_tload_after_subcall_result: tload_value,
str_subcall_worked: (
slot_tload_in_subcall_result: 0xFF if call_return == Om.OOG else tload_value,
slot_tload_after_subcall_result: tload_value,
slot_subcall_worked: (
0 if call_return == Op.REVERT or call_return == Om.OOG else 1
),
}
Expand All @@ -124,8 +124,8 @@ def make_call(call_type: Op) -> bytes:
# if external call
post[address_to] = Account(
storage={
str_code_worked: 1,
str_tload_in_subcall_result: (
slot_code_worked: 1,
slot_tload_in_subcall_result: (
0xFF # if call OOG, we fail to obtain the result
if call_return == Om.OOG
# else delegate and callcode are working in the same context so tload works
Expand All @@ -136,8 +136,8 @@ def make_call(call_type: Op) -> bytes:
)
),
# no subcall errors can change the tload result
str_tload_after_subcall_result: 44,
str_subcall_worked: (
slot_tload_after_subcall_result: 44,
slot_subcall_worked: (
0 if call_return == Op.REVERT or call_return == Om.OOG else 1
),
}
Expand Down
72 changes: 36 additions & 36 deletions tests/cancun/eip1153_tstore/test_tstore_reentrancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_tstore_reentrancy(
state_test: StateTestFiller, call_type: Op, call_return: Op, call_dest: bytes
):
"""
Covered .json vectors:
Ported .json vectors:
(06_tstoreInReentrancyCallFiller.yml)
Reentrant calls access the same transient storage
Expand Down Expand Up @@ -62,13 +62,13 @@ def test_tstore_reentrancy(
tload_value_set_in_call = 90

# Storage cells
str_tload_before_call = 0
str_tload_in_subcall_result = 1
str_tload_after_call = 2
str_subcall_worked = 3
str_tload_1_after_call = 4
str_tstore_overwrite = 5
str_code_worked = 6
slot_tload_before_call = 0
slot_tload_in_subcall_result = 1
slot_tload_after_call = 2
slot_subcall_worked = 3
slot_tload_1_after_call = 4
slot_tstore_overwrite = 5
slot_code_worked = 6

# Function names
do_tstore = 1
Expand Down Expand Up @@ -102,28 +102,28 @@ def make_call(call_type: Op) -> bytes:
Case(
condition=Op.EQ(Op.CALLDATALOAD(0), do_reenter),
action=Op.TSTORE(0, tload_value_set_before_call)
+ Op.SSTORE(str_tload_before_call, Op.TLOAD(0))
+ Op.SSTORE(slot_tload_before_call, Op.TLOAD(0))
+ Op.MSTORE(0, do_tstore)
+ Op.MSTORE(32, 0xFF)
+ Op.SSTORE(str_subcall_worked, make_call(call_type))
+ Op.SSTORE(str_tload_in_subcall_result, Op.MLOAD(32))
+ Op.SSTORE(str_tload_after_call, Op.TLOAD(0))
+ Op.SSTORE(str_tload_1_after_call, Op.TLOAD(1))
+ Op.SSTORE(slot_subcall_worked, make_call(call_type))
+ Op.SSTORE(slot_tload_in_subcall_result, Op.MLOAD(32))
+ Op.SSTORE(slot_tload_after_call, Op.TLOAD(0))
+ Op.SSTORE(slot_tload_1_after_call, Op.TLOAD(1))
+ Op.TSTORE(0, 50)
+ Op.SSTORE(str_tstore_overwrite, Op.TLOAD(0))
+ Op.SSTORE(str_code_worked, 1),
+ Op.SSTORE(slot_tstore_overwrite, Op.TLOAD(0))
+ Op.SSTORE(slot_code_worked, 1),
),
],
default_action=b"",
),
storage={
str_tload_before_call: 0xFF,
str_tload_in_subcall_result: 0xFF,
str_tload_after_call: 0xFF,
str_subcall_worked: 0xFF,
str_tload_1_after_call: 0xFF,
str_tstore_overwrite: 0xFF,
str_code_worked: 0xFF,
slot_tload_before_call: 0xFF,
slot_tload_in_subcall_result: 0xFF,
slot_tload_after_call: 0xFF,
slot_subcall_worked: 0xFF,
slot_tload_1_after_call: 0xFF,
slot_tstore_overwrite: 0xFF,
slot_code_worked: 0xFF,
},
),
address_code: Account(
Expand Down Expand Up @@ -156,46 +156,46 @@ def successful_delegate_or_callcode() -> bool:
# if reentrancy
post[address_to] = Account(
storage={
str_code_worked: 1,
str_tload_before_call: tload_value_set_before_call,
str_tload_in_subcall_result: (
slot_code_worked: 1,
slot_tload_before_call: tload_value_set_before_call,
slot_tload_in_subcall_result: (
# we fail to obtain in call result if it fails
0xFF
if call_type == Op.STATICCALL or call_return == Om.OOG
else tload_value_set_in_call
),
# reentrant tstore overrides value in upper level
str_tload_after_call: (
slot_tload_after_call: (
tload_value_set_before_call if failing_calls() else tload_value_set_in_call
),
str_tload_1_after_call: 0 if failing_calls() else 12,
str_tstore_overwrite: 50,
slot_tload_1_after_call: 0 if failing_calls() else 12,
slot_tstore_overwrite: 50,
# tstore in static call not allowed
str_subcall_worked: 0 if failing_calls() else 1,
slot_subcall_worked: 0 if failing_calls() else 1,
}
)
else:
post[address_to] = Account(
# if external call
storage={
str_code_worked: 1,
str_tload_before_call: tload_value_set_before_call,
str_tload_in_subcall_result: (
slot_code_worked: 1,
slot_tload_before_call: tload_value_set_before_call,
slot_tload_in_subcall_result: (
# we fail to obtain in call result if it fails
0xFF
if call_type == Op.STATICCALL or call_return == Om.OOG
else tload_value_set_in_call
),
# external tstore overrides value in upper level only in delegate and callcode
str_tload_after_call: (
slot_tload_after_call: (
tload_value_set_in_call
if successful_delegate_or_callcode()
else tload_value_set_before_call
),
str_tload_1_after_call: 12 if successful_delegate_or_callcode() else 0,
str_tstore_overwrite: 50,
slot_tload_1_after_call: 12 if successful_delegate_or_callcode() else 0,
slot_tstore_overwrite: 50,
# tstore in static call not allowed, reentrancy means external call here
str_subcall_worked: 0 if failing_calls() else 1,
slot_subcall_worked: 0 if failing_calls() else 1,
}
)

Expand Down

0 comments on commit d52674f

Please sign in to comment.