|
| 1 | +"""abstract: EIP-2: Homestead Identity Precompile Test Cases.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ethereum_test_tools import ( |
| 6 | + Account, |
| 7 | + Alloc, |
| 8 | + Environment, |
| 9 | + StateTestFiller, |
| 10 | + Transaction, |
| 11 | + keccak256, |
| 12 | +) |
| 13 | +from ethereum_test_tools import Opcodes as Op |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.with_all_call_opcodes() |
| 17 | +@pytest.mark.valid_from("Byzantium") |
| 18 | +def test_identity_return_overwrite( |
| 19 | + state_test: StateTestFiller, |
| 20 | + pre: Alloc, |
| 21 | + call_opcode: Op, |
| 22 | +): |
| 23 | + """Test the return data of the identity precompile overwriting its input.""" |
| 24 | + code = ( |
| 25 | + sum(Op.MSTORE8(offset=i, value=(i + 1)) for i in range(4)) # memory = [1, 2, 3, 4] |
| 26 | + + call_opcode( |
| 27 | + address=4, |
| 28 | + args_offset=0, |
| 29 | + args_size=4, # args = [1, 2, 3, 4] |
| 30 | + ret_offset=1, |
| 31 | + ret_size=4, |
| 32 | + ) # memory = [1, 1, 2, 3, 4] |
| 33 | + + Op.RETURNDATACOPY( |
| 34 | + dest_offset=0, offset=0, size=Op.RETURNDATASIZE() |
| 35 | + ) # memory correct = [1, 2, 3, 4, 4], corrupt = [1, 1, 2, 3, 4] |
| 36 | + + Op.SSTORE(1, Op.SHA3(offset=0, size=Op.MSIZE)) |
| 37 | + ) |
| 38 | + contract_address = pre.deploy_contract( |
| 39 | + code=code, |
| 40 | + ) |
| 41 | + tx = Transaction( |
| 42 | + sender=pre.fund_eoa(), |
| 43 | + to=contract_address, |
| 44 | + gas_limit=100_000, |
| 45 | + ) |
| 46 | + |
| 47 | + post = { |
| 48 | + contract_address: Account( |
| 49 | + storage={ |
| 50 | + 1: keccak256(bytes([1, 2, 3, 4, 4]).ljust(32, b"\0")), |
| 51 | + }, |
| 52 | + ), |
| 53 | + } |
| 54 | + |
| 55 | + state_test(pre=pre, post=post, tx=tx) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.with_all_call_opcodes() |
| 59 | +@pytest.mark.valid_from("Byzantium") |
| 60 | +def test_identity_return_buffer_modify( |
| 61 | + state_test: StateTestFiller, |
| 62 | + pre: Alloc, |
| 63 | + call_opcode: Op, |
| 64 | +): |
| 65 | + """Test the modification of the input range to attempt to modify the return buffer.""" |
| 66 | + env = Environment() |
| 67 | + code = ( |
| 68 | + sum(Op.MSTORE8(offset=i, value=(i + 1)) for i in range(4)) # memory = [1, 2, 3, 4] |
| 69 | + + call_opcode( |
| 70 | + address=4, |
| 71 | + args_offset=0, |
| 72 | + args_size=4, # args = [1, 2, 3, 4] |
| 73 | + ) # memory = [1, 2, 3, 4] |
| 74 | + + Op.MSTORE8(offset=0, value=5) # memory = [5, 2, 3, 4] |
| 75 | + + Op.MSTORE8(offset=4, value=5) # memory = [5, 2, 3, 4, 5] |
| 76 | + + Op.RETURNDATACOPY( |
| 77 | + dest_offset=0, offset=0, size=Op.RETURNDATASIZE() |
| 78 | + ) # memory correct = [1, 2, 3, 4, 5], corrupt = [5, 2, 3, 4, 5] |
| 79 | + + Op.SSTORE(1, Op.SHA3(offset=0, size=Op.MSIZE)) |
| 80 | + ) |
| 81 | + contract_address = pre.deploy_contract( |
| 82 | + code=code, |
| 83 | + ) |
| 84 | + tx = Transaction( |
| 85 | + sender=pre.fund_eoa(), |
| 86 | + to=contract_address, |
| 87 | + gas_limit=100_000, |
| 88 | + ) |
| 89 | + |
| 90 | + post = { |
| 91 | + contract_address: Account( |
| 92 | + storage={ |
| 93 | + 1: keccak256(bytes([1, 2, 3, 4, 5]).ljust(32, b"\0")), |
| 94 | + }, |
| 95 | + ), |
| 96 | + } |
| 97 | + |
| 98 | + state_test(env=env, pre=pre, post=post, tx=tx) |
0 commit comments