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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 | @pytest.mark.parametrize(
"target_address",
(
pytest.param(b"", id="zero"),
pytest.param(b"\xc0\xde", id="short"),
pytest.param(b"\x78" * 20, id="mid_20"),
pytest.param(b"\xff" * 20, id="max_20"),
pytest.param(b"\x01" + (b"\x00" * 20), id="min_ase"),
pytest.param(b"\x5a" * 28, id="mid_ase"),
pytest.param(b"\x5a" * 32, id="full_ase"),
pytest.param(b"\xff" * 32, id="max_ase"),
),
)
@pytest.mark.parametrize(
"target_account_type",
(
"empty",
"EOA",
"LegacyContract", # Hard-codes an address in pre-alloc
"EOFContract", # Hard-codes an address in pre-alloc
),
ids=lambda x: x,
)
@pytest.mark.parametrize(
"target_opcode",
(
Op.CALL,
Op.CALLCODE,
Op.STATICCALL,
Op.DELEGATECALL,
Op.EXTCALL,
Op.EXTDELEGATECALL,
Op.EXTSTATICCALL,
),
)
def test_address_space_extension(
state_test: StateTestFiller,
pre: Alloc,
target_address: bytes,
target_opcode: Op,
target_account_type: str,
):
"""
Test contacts with possibly extended address and fail if address is too large
"""
env = Environment()
ase_address = len(target_address) > 20
stripped_address = target_address[-20:] if ase_address else target_address
if ase_address and target_address[0] == b"00":
raise ValueError("Test instrumentation requires target addresses trim leading zeros")
match target_opcode:
case Op.CALL | Op.CALLCODE:
call_suffix = [0, 0, 0, 0, 0]
ase_ready_opcode = False
case Op.DELEGATECALL | Op.STATICCALL:
call_suffix = [0, 0, 0, 0]
ase_ready_opcode = False
case Op.EXTCALL:
call_suffix = [0, 0, 0]
ase_ready_opcode = True
case Op.EXTDELEGATECALL | Op.EXTSTATICCALL:
call_suffix = [0, 0]
ase_ready_opcode = True
case _:
raise ValueError("Unexpected opcode ", target_opcode)
sender = pre.fund_eoa()
address_caller = pre.deploy_contract(
Container(
sections=[
Section.Code(
code=Op.SSTORE(
slot_target_call_status,
target_opcode(Op.CALLDATALOAD(0), *call_suffix), # type: ignore
)
+ Op.RETURNDATACOPY(0, 0, Op.RETURNDATASIZE)
+ Op.SSTORE(slot_target_returndata, Op.MLOAD(0))
+ Op.STOP,
code_inputs=0,
)
],
)
if ase_ready_opcode
else Op.SSTORE(
slot_target_call_status,
target_opcode(Op.GAS, Op.CALLDATALOAD(0), *call_suffix), # type: ignore
)
+ Op.RETURNDATACOPY(0, 0, Op.RETURNDATASIZE)
+ Op.SSTORE(slot_target_returndata, Op.MLOAD(0))
+ Op.STOP,
storage={
slot_target_call_status: value_exceptional_abort_canary,
slot_target_returndata: value_exceptional_abort_canary,
},
)
address_entry_point = pre.deploy_contract(
Op.MSTORE(0, Op.PUSH32(target_address))
+ Op.SSTORE(
slot_top_level_call_status,
Op.CALL(50000, address_caller, 0, 0, 32, 0, 0),
)
+ Op.STOP(),
storage={
slot_top_level_call_status: value_exceptional_abort_canary,
},
)
match target_account_type:
case "empty":
# add no account
pass
case "EOA":
pre.fund_address(Address(stripped_address), 10**18)
# TODO: we could use pre.fund_eoa here with nonce!=0.
case "LegacyContract":
pre[Address(stripped_address)] = Account(
code=Op.MSTORE(0, Op.ADDRESS) + Op.RETURN(0, 32),
balance=0,
nonce=0,
)
case "EOFContract":
pre[Address(stripped_address)] = Account(
code=Container(
sections=[
Section.Code(
code=Op.MSTORE(0, Op.ADDRESS) + Op.RETURN(0, 32),
)
],
),
balance=0,
nonce=0,
)
caller_storage: dict[int, int | bytes | Address] = {}
match target_account_type:
case "empty" | "EOA":
if ase_address and ase_ready_opcode:
caller_storage[slot_target_call_status] = value_exceptional_abort_canary
caller_storage[slot_target_returndata] = value_exceptional_abort_canary
elif target_opcode == Op.EXTDELEGATECALL:
caller_storage[slot_target_call_status] = EXTCALL_REVERT
caller_storage[slot_target_returndata] = 0
else:
caller_storage[slot_target_call_status] = (
EXTCALL_SUCCESS if ase_ready_opcode else CALL_SUCCESS
)
case "LegacyContract" | "EOFContract":
match target_opcode:
case Op.CALL | Op.STATICCALL:
caller_storage[slot_target_call_status] = CALL_SUCCESS
# CALL and STATICCALL call will call the stripped address
caller_storage[slot_target_returndata] = stripped_address
case Op.CALLCODE | Op.DELEGATECALL:
caller_storage[slot_target_call_status] = CALL_SUCCESS
# CALLCODE and DELEGATECALL call will call the stripped address
# but will change the sender to self
caller_storage[slot_target_returndata] = address_caller
case Op.EXTCALL | Op.EXTSTATICCALL:
# EXTCALL and EXTSTATICCALL will fault if calling an ASE address
if ase_address:
caller_storage[slot_target_call_status] = value_exceptional_abort_canary
caller_storage[slot_target_returndata] = value_exceptional_abort_canary
else:
caller_storage[slot_target_call_status] = EXTCALL_SUCCESS
caller_storage[slot_target_returndata] = stripped_address
case Op.EXTDELEGATECALL:
if ase_address:
caller_storage[slot_target_call_status] = value_exceptional_abort_canary
caller_storage[slot_target_returndata] = value_exceptional_abort_canary
elif target_account_type == "LegacyContract":
caller_storage[slot_target_call_status] = EXTCALL_REVERT
caller_storage[slot_target_returndata] = 0
else:
caller_storage[slot_target_call_status] = EXTCALL_SUCCESS
# EXTDELEGATECALL call will call the stripped address
# but will change the sender to self
caller_storage[slot_target_returndata] = address_caller
post = {
address_entry_point: Account(
storage={
slot_top_level_call_status: EXTCALL_SUCCESS
if ase_ready_opcode and ase_address
else CALL_SUCCESS
}
),
address_caller: Account(storage=caller_storage),
}
tx = Transaction(
sender=sender,
to=address_entry_point,
gas_limit=50_000_000,
gas_price=10,
protected=False,
data="",
)
state_test(
env=env,
pre=pre,
post=post,
tx=tx,
)
|