diff --git a/src/ethereum_test_forks/forks/forks.py b/src/ethereum_test_forks/forks/forks.py index 5e8c90185f..9d16946e31 100644 --- a/src/ethereum_test_forks/forks/forks.py +++ b/src/ethereum_test_forks/forks/forks.py @@ -619,7 +619,11 @@ def valid_opcodes( cls, ) -> List[Opcodes]: """Return list of Opcodes that are valid to work on this fork.""" - return [Opcodes.RETURNDATASIZE, Opcodes.STATICCALL] + super(Byzantium, cls).valid_opcodes() + return [ + Opcodes.RETURNDATASIZE, + Opcodes.RETURNDATACOPY, + Opcodes.STATICCALL, + ] + super(Byzantium, cls).valid_opcodes() class Constantinople(Byzantium): diff --git a/tests/frontier/opcodes/test_all_opcodes.py b/tests/frontier/opcodes/test_all_opcodes.py index 854e66d939..467c1b666b 100644 --- a/tests/frontier/opcodes/test_all_opcodes.py +++ b/tests/frontier/opcodes/test_all_opcodes.py @@ -34,6 +34,8 @@ def prepare_stack(opcode: Opcode) -> Bytecode: return Op.PUSH1(1) + Op.PUSH1(5) if opcode == Op.JUMP: return Op.PUSH1(3) + if opcode == Op.RETURNDATACOPY: + return Op.PUSH1(0x00) * 32 return Op.PUSH1(0x01) * 32 @@ -65,8 +67,8 @@ def test_all_opcodes(state_test: StateTestFiller, pre: Alloc, fork: Fork): contract_address = pre.deploy_contract( code=sum( Op.SSTORE( - Op.PUSH1(opcode.int()), - Op.CALL(1_000_000, opcode_address, 0, 0, 0, 0, 0), + opcode.int(), + Op.CALL(gas=100_000, address=opcode_address), ) for opcode, opcode_address in code_contract.items() ) @@ -82,7 +84,7 @@ def test_all_opcodes(state_test: StateTestFiller, pre: Alloc, fork: Fork): tx = Transaction( sender=pre.fund_eoa(), - gas_limit=500_000_000, + gas_limit=50_000_000, to=contract_address, data=b"", value=0, diff --git a/tests/homestead/identity_precompile/__init__.py b/tests/homestead/identity_precompile/__init__.py new file mode 100644 index 0000000000..786785884f --- /dev/null +++ b/tests/homestead/identity_precompile/__init__.py @@ -0,0 +1 @@ +"""abstract: EIP-2: Homestead Precompile Identity Test Cases.""" diff --git a/tests/homestead/identity_precompile/test_identity.py b/tests/homestead/identity_precompile/test_identity.py new file mode 100644 index 0000000000..14e27fce05 --- /dev/null +++ b/tests/homestead/identity_precompile/test_identity.py @@ -0,0 +1,103 @@ +"""abstract: EIP-2: Homestead Identity Precompile Test Cases.""" + +import pytest + +from ethereum_test_tools import ( + Account, + Alloc, + Environment, + StateTestFiller, + Transaction, + keccak256, +) +from ethereum_test_tools import Opcodes as Op + + +@pytest.mark.with_all_call_opcodes( + selector=lambda call_opcode: call_opcode + not in [Op.EXTCALL, Op.EXTDELEGATECALL, Op.EXTSTATICCALL] + # EXT*CALL opcodes do not contain accept the return data size/offset as an argument +) +@pytest.mark.valid_from("Byzantium") +def test_identity_return_overwrite( + state_test: StateTestFiller, + pre: Alloc, + call_opcode: Op, +): + """Test the return data of the identity precompile overwriting its input.""" + env = Environment() + code = ( + sum(Op.MSTORE8(offset=i, value=(i + 1)) for i in range(4)) # memory = [1, 2, 3, 4] + + call_opcode( + address=4, + args_offset=0, + args_size=4, # args = [1, 2, 3, 4] + ret_offset=1, + ret_size=4, + ) # memory = [1, 1, 2, 3, 4] + + Op.RETURNDATACOPY( + dest_offset=0, offset=0, size=Op.RETURNDATASIZE() + ) # memory correct = [1, 2, 3, 4, 4], corrupt = [1, 1, 2, 3, 4] + + Op.SSTORE(1, Op.SHA3(offset=0, size=Op.MSIZE)) + ) + contract_address = pre.deploy_contract( + code=code, + ) + tx = Transaction( + sender=pre.fund_eoa(), + to=contract_address, + gas_limit=100_000, + ) + + post = { + contract_address: Account( + storage={ + 1: keccak256(bytes([1, 2, 3, 4, 4]).ljust(32, b"\0")), + }, + ), + } + + state_test(env=env, pre=pre, post=post, tx=tx) + + +@pytest.mark.with_all_call_opcodes() +@pytest.mark.valid_from("Byzantium") +def test_identity_return_buffer_modify( + state_test: StateTestFiller, + pre: Alloc, + call_opcode: Op, +): + """Test the modification of the input range to attempt to modify the return buffer.""" + env = Environment() + code = ( + sum(Op.MSTORE8(offset=i, value=(i + 1)) for i in range(4)) # memory = [1, 2, 3, 4] + + call_opcode( + address=4, + args_offset=0, + args_size=4, # args = [1, 2, 3, 4] + ) # memory = [1, 2, 3, 4] + + Op.MSTORE8(offset=0, value=5) # memory = [5, 2, 3, 4] + + Op.MSTORE8(offset=4, value=5) # memory = [5, 2, 3, 4, 5] + + Op.RETURNDATACOPY( + dest_offset=0, offset=0, size=Op.RETURNDATASIZE() + ) # memory correct = [1, 2, 3, 4, 5], corrupt = [5, 2, 3, 4, 5] + + Op.SSTORE(1, Op.SHA3(offset=0, size=Op.MSIZE)) + ) + contract_address = pre.deploy_contract( + code=code, + ) + tx = Transaction( + sender=pre.fund_eoa(), + to=contract_address, + gas_limit=100_000, + ) + + post = { + contract_address: Account( + storage={ + 1: keccak256(bytes([1, 2, 3, 4, 5]).ljust(32, b"\0")), + }, + ), + } + + state_test(env=env, pre=pre, post=post, tx=tx)