Test TSTORE is rolled back after failed CREATE/CREATE2 initcode.
Regression test for
#917
Initcode does TLOAD(1) to compute a return size, then does
TSTORE(1, max_code_size), then returns data of the computed size.
When TLOAD(1) is 0, the return size exceeds the max code size, so
creation fails.
The caller invokes CREATE/CREATE2 twice with the same initcode.
If TSTORE from the first (failed) creation is properly rolled
back, the second CREATE2 also sees TLOAD(1)==0 and fails the same
way, so nothing is deployed. If it were not rolled back, the
second CREATE2 would see TLOAD(1)==max_code_size, return a small
valid contract and succeed; the post-state therefore asserts the
target address is non-existent.
The create results are not recorded with SSTORE: a failed create
burns its full 63/64 gas forward, and under the EIP-8037
transaction gas-limit cap two of them in sequence leave too little
regular gas to write the result. Asserting account non-existence
checks the same rollback property without that write.
Source code in tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 | @pytest.mark.ported_from(
[
"https://github.com/holiman/goevmlab/blob/master/examples/tstore_bug-2/main.go",
],
)
@pytest.mark.parametrize("create_opcode", [Op.CREATE, Op.CREATE2])
def test_tstore_rollback_on_failed_create(
state_test: StateTestFiller,
pre: Alloc,
create_opcode: Op,
fork: Fork,
) -> None:
"""
Test TSTORE is rolled back after failed CREATE/CREATE2 initcode.
Regression test for
https://github.com/ethereum/execution-specs/issues/917
Initcode does TLOAD(1) to compute a return size, then does
TSTORE(1, max_code_size), then returns data of the computed size.
When TLOAD(1) is 0, the return size exceeds the max code size, so
creation fails.
The caller invokes CREATE/CREATE2 twice with the same initcode.
If TSTORE from the first (failed) creation is properly rolled
back, the second CREATE2 also sees TLOAD(1)==0 and fails the same
way, so nothing is deployed. If it were not rolled back, the
second CREATE2 would see TLOAD(1)==max_code_size, return a small
valid contract and succeed; the post-state therefore asserts the
target address is non-existent.
The create results are not recorded with SSTORE: a failed create
burns its full 63/64 gas forward, and under the EIP-8037
transaction gas-limit cap two of them in sequence leave too little
regular gas to write the result. Asserting account non-existence
checks the same rollback property without that write.
"""
# Initcode:
# return_size = (max_code_size + 0x0A) - TLOAD(1)
# TSTORE(1, max_code_size)
# RETURN(offset=0, size=return_size)
#
# TLOAD(1)==0: return_size > max code size -> fail
# TLOAD(1)==max_code_size: return_size = 0x0A <= max -> succeed
max_code_size = fork.max_code_size()
salt = 0
initcode = (
Op.TLOAD(1)
+ Op.PUSH4(max_code_size + 0x0A)
+ Op.SUB
+ Op.TSTORE(1, max_code_size)
+ Op.PUSH1(0)
+ Op.RETURN
)
initcode_bytes = bytes(initcode)
initcode_len = len(initcode_bytes)
create_call = (
create_opcode(0, 0, initcode_len, salt)
if create_opcode == Op.CREATE2
else create_opcode(0, 0, initcode_len)
)
caller_code = (
Om.MSTORE(initcode_bytes, 0)
+ create_call
+ Op.POP
+ create_call
+ Op.POP
)
caller_address = pre.deploy_contract(caller_code)
# CREATE2 targets one deterministic address (salt + initcode) for
# both attempts; CREATE targets nonce-derived addresses (the
# deployed caller starts at nonce 1).
if create_opcode == Op.CREATE2:
created_addresses = [
compute_create_address(
address=caller_address,
salt=salt,
initcode=initcode,
opcode=Op.CREATE2,
)
]
else:
created_addresses = [
compute_create_address(
address=caller_address, nonce=nonce, opcode=Op.CREATE
)
for nonce in (1, 2)
]
sender = pre.fund_eoa()
tx = Transaction(sender=sender, to=caller_address)
# Both creations fail because TSTORE is rolled back, so nothing is
# deployed; the caller nonce still advances once per attempt.
post = {caller_address: Account(nonce=3)}
for created_address in created_addresses:
post[created_address] = Account.NONEXISTENT # type: ignore
state_test(pre=pre, post=post, tx=tx)
|