19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 | @pytest.mark.valid_from("Istanbul")
@pytest.mark.parametrize("call_return_size", [35, 32, 0])
@pytest.mark.parametrize("create_type", [Op.CREATE, Op.CREATE2])
@pytest.mark.parametrize("return_type", [Op.RETURN, Op.REVERT])
@pytest.mark.parametrize("return_type_in_create", [Op.RETURN, Op.REVERT])
def test_create2_return_data(
call_return_size: int,
create_type: Op,
return_type: Op,
return_type_in_create: Op,
pre: Alloc,
state_test: StateTestFiller,
):
"""
Validate that create2 return data does not interfere with previously existing memory
"""
# Storage vars
slot_returndatasize_before_create = 0
slot_returndatasize_after_create = 1
slot_return_data_hash_before_create = 2
slot_return_data_hash_after_create = 3
slot_code_worked = 4
slot_returndatacopy_before_create = 5
slot_returndatacopy_before_create_2 = 6
slot_returndatacopy_after_create = 7
slot_begin_memory_after_create = 8
# CREATE2 Initcode
create2_salt = 1
return_data_in_create = 0xFFFAFB
initcode = Op.MSTORE(0, return_data_in_create) + return_type_in_create(0, 32)
call_return_data_value = 0x1122334455667788991011121314151617181920212223242526272829303132
expected_call_return_data = int.to_bytes(call_return_data_value, 32, byteorder="big").ljust(
call_return_size, b"\0"
)[0:call_return_size]
expected_returndatacopy = expected_call_return_data[0:32]
empty_data = b""
address_call = pre.deploy_contract(
code=Op.MSTORE(0, call_return_data_value)
+ Op.MSTORE(32, 0xFFFFFFFF)
+ return_type(0, call_return_size),
storage={},
)
address_to = pre.deploy_contract(
balance=100_000_000,
code=Op.JUMPDEST()
+ Op.MSTORE(0x100, Op.CALLDATALOAD(0))
+ Op.CALL(0x0900000000, address_call, 0, 0, 0, 0, call_return_size)
#
#
+ Op.SSTORE(slot_returndatasize_before_create, Op.RETURNDATASIZE())
+ Op.RETURNDATACOPY(0x200, 0, call_return_size)
+ Op.SSTORE(slot_returndatacopy_before_create, Op.MLOAD(0x200))
+ Op.SSTORE(slot_returndatacopy_before_create_2, Op.MLOAD(0x220))
+ Op.SSTORE(slot_return_data_hash_before_create, Op.SHA3(0, call_return_size))
#
#
+ create_type(offset=0x100, size=Op.CALLDATASIZE(), salt=create2_salt)
+ Op.SSTORE(slot_returndatasize_after_create, Op.RETURNDATASIZE())
+ Op.RETURNDATACOPY(0x300, 0, Op.RETURNDATASIZE())
+ Op.SSTORE(slot_returndatacopy_after_create, Op.MLOAD(0x300))
+ Op.SSTORE(slot_return_data_hash_after_create, Op.SHA3(0x300, Op.RETURNDATASIZE()))
+ Op.SSTORE(slot_begin_memory_after_create, Op.MLOAD(0))
+ Op.SSTORE(slot_code_worked, 1)
+ Op.STOP(),
storage={
slot_returndatasize_before_create: 0xFF,
slot_returndatasize_after_create: 0xFF,
slot_return_data_hash_before_create: 0xFF,
slot_return_data_hash_after_create: 0xFF,
slot_returndatacopy_before_create: 0xFF,
slot_returndatacopy_before_create_2: 0xFF,
slot_begin_memory_after_create: 0xFF,
slot_begin_memory_after_create: 0xFF,
},
)
post = {
address_to: Account(
storage={
slot_code_worked: 1,
slot_returndatacopy_before_create: expected_returndatacopy,
slot_returndatacopy_before_create_2: 0,
#
# the actual bytes returned by returndatacopy opcode after create
slot_returndatacopy_after_create: return_data_in_create
if return_type_in_create == Op.REVERT
else 0,
slot_returndatasize_before_create: call_return_size,
#
# return datasize value after create
slot_returndatasize_after_create: 0x20
if return_type_in_create == Op.REVERT
else 0,
#
slot_return_data_hash_before_create: keccak256(expected_call_return_data),
slot_return_data_hash_after_create: keccak256(empty_data)
if return_type_in_create == Op.RETURN
else keccak256(int.to_bytes(return_data_in_create, 32, byteorder="big")),
#
# check that create 2 didn't mess up with initial memory space declared for return
slot_begin_memory_after_create: expected_returndatacopy,
} # type: ignore
)
}
tx = Transaction(
sender=pre.fund_eoa(),
to=address_to,
protected=False,
data=initcode,
gas_limit=0x0A00000000,
value=0,
) # type: ignore
state_test(env=Environment(), pre=pre, post=post, tx=tx)
|