432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546 | @pytest.mark.parametrize(
"via_relay",
[pytest.param(False, id="direct"), pytest.param(True, id="via_relay")],
)
@pytest.mark.parametrize(
"initial_balance",
[pytest.param(0, id="zero_balance"), pytest.param(3, id="funded")],
)
@EIPChecklist.Opcode.Test.ExecutionContext.Staticcall(eip=[8246])
@EIPChecklist.Opcode.Test.ExecutionContext.Staticcall.BanCheck(eip=[8246])
@EIPChecklist.Opcode.Test.ExecutionContext.Staticcall.BanNoModification(
eip=[8246]
)
@EIPChecklist.Opcode.Test.ExecutionContext.Staticcall.SubCalls(eip=[8246])
@EIPChecklist.Opcode.Test.ExceptionalAbort(eip=[8246])
def test_selfdestruct_static_context_same_tx(
state_test: StateTestFiller,
pre: Alloc,
fork: Fork,
via_relay: bool,
initial_balance: int,
) -> None:
"""
SELFDESTRUCT to self inside a static call aborts the frame, so the
contract is never registered for deletion and keeps its code.
"""
sender = pre.fund_eoa()
victim_code = Op.SELFDESTRUCT(Op.ADDRESS)
initcode = Initcode(deploy_code=victim_code)
if via_relay:
# The relay cannot SSTORE inside the static context, so it returns
# the inner call result instead. Without that witness a broken
# relay call would leave the same post-state as a correct abort.
relay = pre.deploy_contract(
code=Op.MSTORE(0, Op.CALL(gas=Op.GAS, address=Op.CALLDATALOAD(0)))
+ Op.RETURN(0, 32)
)
static_target: Address | Bytecode = relay
static_result = 1
else:
static_target = Op.SLOAD(0)
static_result = 0
entry = pre.deploy_contract(
code=Om.MSTORE(initcode, 0)
+ Op.SSTORE(
0, Op.CREATE(value=initial_balance, offset=0, size=len(initcode))
)
+ Op.MSTORE(0, Op.SLOAD(0))
+ Op.SSTORE(
1,
Op.STATICCALL(
gas=Op.GAS,
address=static_target,
args_offset=0,
args_size=32,
ret_offset=64,
ret_size=32,
),
)
# Store the inner result offset by one so that a failing inner call
# is still a written slot.
+ Op.SSTORE(2, Op.ADD(Op.MLOAD(64), 1))
+ Op.STOP
)
victim = compute_create_address(address=entry, nonce=1)
tx = Transaction(sender=sender, to=entry, value=initial_balance)
logs: list[TransactionLog] = []
if initial_balance > 0:
logs.append(transfer_log(sender, entry, initial_balance))
logs.append(transfer_log(entry, victim, initial_balance))
if fork.is_eip_enabled(7708):
tx.expected_receipt = TransactionReceipt(logs=logs)
expected_bal = None
if fork.is_eip_enabled(7928):
balance_changes = []
if initial_balance > 0:
balance_changes = [
BalBalanceChange(
block_access_index=1, post_balance=initial_balance
)
]
expected_bal = BlockAccessListExpectation(
account_expectations={
victim: BalAccountExpectation(
nonce_changes=[
BalNonceChange(block_access_index=1, post_nonce=1)
],
code_changes=[
BalCodeChange(
block_access_index=1, new_code=bytes(victim_code)
)
],
balance_changes=balance_changes,
storage_changes=[],
storage_reads=[],
)
}
)
state_test(
pre=pre,
post={
entry: Account(storage={0: victim, 1: static_result, 2: 1}),
victim: Account(
balance=initial_balance, nonce=1, code=victim_code
),
},
tx=tx,
expected_block_access_list=expected_bal,
)
|