Skip to content

Commit

Permalink
new(tests): EIP-5656/7692 - EOFize remainder of MCOPY tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pdobacz committed Aug 19, 2024
1 parent 4e52b4c commit 1199abe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
2 changes: 2 additions & 0 deletions tests/cancun/eip5656_mcopy/test_mcopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def post(code_address: Address, code_storage: Storage) -> Mapping: # noqa: D103
"out_of_bounds_memory_extension",
],
)
@pytest.mark.with_all_evm_code_types
@pytest.mark.valid_from("Cancun")
def test_valid_mcopy_operations(
state_test: StateTestFiller,
Expand Down Expand Up @@ -202,6 +203,7 @@ def test_valid_mcopy_operations(
@pytest.mark.parametrize("src", [0x00, 0x20])
@pytest.mark.parametrize("length", [0x00, 0x01])
@pytest.mark.parametrize("initial_memory", [bytes()], ids=["empty_memory"])
@pytest.mark.with_all_evm_code_types
@pytest.mark.valid_from("Cancun")
def test_mcopy_on_empty_memory(
state_test: StateTestFiller,
Expand Down
44 changes: 15 additions & 29 deletions tests/cancun/eip5656_mcopy/test_mcopy_contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initial_memory_length() -> int: # noqa: D103
@pytest.fixture
def callee_bytecode(
initial_memory_length: int,
opcode: Op,
call_opcode: Op,
) -> Bytecode:
"""
Callee simply performs mcopy operations that should not have any effect on the
Expand All @@ -44,7 +44,7 @@ def callee_bytecode(
bytecode += Op.MCOPY(0x00, initial_memory_length * 2, 1)
bytecode += Op.MCOPY(initial_memory_length * 2, 0x00, 1)

if opcode != Op.STATICCALL:
if call_opcode != Op.STATICCALL and call_opcode != Op.EXTSTATICCALL:
# Simple sstore to make sure we actually ran the code
bytecode += Op.SSTORE(200_000, 1)

Expand All @@ -58,7 +58,7 @@ def callee_bytecode(
def initial_memory(
callee_bytecode: Bytecode,
initial_memory_length: int,
opcode: Op,
call_opcode: Op,
) -> bytes:
"""
Initial memory for the test.
Expand All @@ -67,7 +67,7 @@ def initial_memory(

ret = bytes(list(islice(cycle(range(0x01, 0x100)), initial_memory_length)))

if opcode in [Op.CREATE, Op.CREATE2]:
if call_opcode in [Op.CREATE, Op.CREATE2]:
# We also need to put the callee_bytecode as initcode in memory for create operations
ret = bytes(callee_bytecode) + ret[len(callee_bytecode) :]

Expand All @@ -85,7 +85,7 @@ def caller_bytecode(
initial_memory: bytes,
callee_address: Address,
callee_bytecode: Bytecode,
opcode: Op,
call_opcode: Op,
caller_storage: Storage,
) -> Bytecode:
"""
Expand All @@ -99,10 +99,10 @@ def caller_bytecode(
bytecode += Op.MSTORE(i, Op.PUSH32(initial_memory[i : i + 0x20]))

# Perform the call to the contract that is going to perform mcopy
if opcode in [Op.CALL, Op.CALLCODE, Op.DELEGATECALL, Op.STATICCALL]:
bytecode += opcode(address=callee_address)
elif opcode in [Op.CREATE, Op.CREATE2]:
bytecode += opcode(size=len(callee_bytecode))
if call_opcode in [Op.CREATE, Op.CREATE2]:
bytecode += call_opcode(size=len(callee_bytecode))
else:
bytecode += call_opcode(address=callee_address)

# First save msize
bytecode += Op.SSTORE(100_000, Op.MSIZE())
Expand Down Expand Up @@ -141,28 +141,20 @@ def post( # noqa: D103
caller_address: Address,
caller_storage: Storage,
callee_address: Address,
opcode: Op,
call_opcode: Op,
) -> Mapping:
callee_storage: Storage.StorageDictType = {}
if opcode in [Op.DELEGATECALL, Op.CALLCODE]:
if call_opcode in [Op.DELEGATECALL, Op.CALLCODE, Op.EXTDELEGATECALL]:
caller_storage[200_000] = 1
elif opcode in [Op.CALL]:
elif call_opcode in [Op.CALL, Op.EXTCALL]:
callee_storage[200_000] = 1
return {
caller_address: Account(storage=caller_storage),
callee_address: Account(storage=callee_storage),
}


@pytest.mark.parametrize(
"opcode",
[
Op.CALL,
Op.DELEGATECALL,
Op.STATICCALL,
Op.CALLCODE,
],
)
@pytest.mark.with_all_call_opcodes
@pytest.mark.valid_from("Cancun")
def test_no_memory_corruption_on_upper_call_stack_levels(
state_test: StateTestFiller,
Expand All @@ -172,13 +164,7 @@ def test_no_memory_corruption_on_upper_call_stack_levels(
):
"""
Perform a subcall with any of the following opcodes, which uses MCOPY during its execution,
and verify that the caller's memory is unaffected:
- `CALL`
- `CALLCODE`
- `DELEGATECALL`
- `STATICCALL`
TODO: [EOF] Add EOF EXT*CALL opcodes
and verify that the caller's memory is unaffected
"""
state_test(
env=Environment(),
Expand All @@ -189,7 +175,7 @@ def test_no_memory_corruption_on_upper_call_stack_levels(


@pytest.mark.parametrize(
"opcode",
"call_opcode",
[
Op.CREATE,
Op.CREATE2,
Expand Down

0 comments on commit 1199abe

Please sign in to comment.