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 | @pytest.mark.parametrize(
"code_size",
[32, 256, 1024],
ids=["32B", "256B", "1KB"],
)
@pytest.mark.parametrize(
"access_opcode",
[Op.EXTCODEHASH, Op.BALANCE, Op.EXTCODECOPY],
)
def test_create2_immediate_access(
benchmark_test: BenchmarkTestFiller,
pre: Alloc,
fork: Fork,
gas_benchmark_value: int,
code_size: int,
access_opcode: Op,
) -> None:
"""
Benchmark CREATE2 followed by immediate opcode access.
Deploy a contract via CREATE2, then immediately query it with the
specified access opcode. Each iteration creates a new trie entry
and reads from it, stressing the deploy-then-access path.
"""
# Build init code that deploys `code_size` bytes of zeros
deploy_code = bytes(code_size)
initcode = Initcode(deploy_code=deploy_code)
init_code_bytes = bytes(initcode)
init_code_size = len(init_code_bytes)
# Deploy holder contract whose runtime code IS the init code
init_holder = pre.deploy_contract(
code=Bytecode(
init_code_bytes,
popped_stack_items=0,
pushed_stack_items=0,
),
)
# Memory layout:
# MEM[0 .. init_code_size-1] = init code (for CREATE2)
# MEM[init_code_size .. +31] = counter (salt)
counter_offset = init_code_size
# Setup: load init code + starting counter
setup = (
Op.EXTCODECOPY(
address=init_holder,
dest_offset=0,
offset=0,
size=init_code_size,
address_warm=False,
data_size=init_code_size,
old_memory_size=0,
new_memory_size=init_code_size,
)
+ Op.MSTORE(
counter_offset,
Op.CALLDATALOAD(32),
old_memory_size=init_code_size,
new_memory_size=counter_offset + 32,
)
+ Op.CALLDATALOAD(0) # [num_iters]
)
# CREATE2 — deploys new contract each iteration
create2_op = Op.CREATE2(
value=0,
offset=0,
size=init_code_size,
salt=Op.MLOAD(counter_offset),
init_code_size=init_code_size,
old_memory_size=counter_offset + 32,
new_memory_size=counter_offset + 32,
)
# Access the just-deployed contract
if access_opcode == Op.EXTCODEHASH:
access_op = Op.POP(Op.EXTCODEHASH(create2_op, address_warm=True))
elif access_opcode == Op.BALANCE:
access_op = Op.POP(Op.BALANCE(create2_op, address_warm=True))
elif access_opcode == Op.EXTCODECOPY:
# Copy 1 byte from end of deployed code
access_op = Op.EXTCODECOPY(
address=create2_op,
dest_offset=counter_offset + 32,
offset=max(code_size - 1, 0),
size=1,
address_warm=True,
data_size=1,
old_memory_size=counter_offset + 32,
new_memory_size=counter_offset + 33,
)
else:
raise ValueError(f"Unsupported opcode: {access_opcode}")
# Increment counter
increment = Op.MSTORE(
counter_offset,
Op.ADD(Op.MLOAD(counter_offset), 1),
)
loop = While(
body=access_op + increment,
condition=DECREMENT_COUNTER_CONDITION,
)
subcall_cost = initcode.execution_gas(fork) + initcode.deployment_gas(fork)
code = IteratingBytecode(
setup=setup,
iterating=loop,
iterating_subcall=subcall_cost,
)
attack_contract_address = pre.deploy_contract(code=code)
def calldata_builder(iteration_count: int, start_iteration: int) -> bytes:
return bytes(Hash(iteration_count) + Hash(start_iteration))
txs = list(
code.transactions_by_gas_limit(
fork=fork,
gas_limit=gas_benchmark_value,
sender=pre.fund_eoa(),
to=attack_contract_address,
calldata=calldata_builder,
)
)
benchmark_test(
pre=pre,
blocks=[Block(txs=txs)],
skip_gas_used_validation=True,
)
|