|
| 1 | +"""test `CALLDATACOPY` opcode.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ethereum_test_forks import Byzantium, Fork |
| 6 | +from ethereum_test_tools import Account, Alloc, Bytecode, StateTestFiller, Transaction |
| 7 | +from ethereum_test_tools.vm.opcode import Opcodes as Op |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize( |
| 11 | + "code,tx_data,code_address_storage,to_address_storage", |
| 12 | + [ |
| 13 | + ( |
| 14 | + ( |
| 15 | + Op.CALLDATACOPY(dest_offset=0, offset=1, size=2) |
| 16 | + + Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0)) |
| 17 | + + Op.RETURN(offset=0, size=Op.MSIZE) |
| 18 | + ), |
| 19 | + b"\x00", |
| 20 | + Account( |
| 21 | + storage={0x00: 0x3456000000000000000000000000000000000000000000000000000000000000} |
| 22 | + ), |
| 23 | + Account( |
| 24 | + storage={0x00: 0x3456000000000000000000000000000000000000000000000000000000000000} |
| 25 | + ), |
| 26 | + ), |
| 27 | + ( |
| 28 | + ( |
| 29 | + Op.CALLDATACOPY(dest_offset=0, offset=1, size=1) |
| 30 | + + Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0)) |
| 31 | + + Op.RETURN(offset=0, size=Op.MSIZE) |
| 32 | + ), |
| 33 | + b"\x01", |
| 34 | + Account( |
| 35 | + storage={0x00: 0x3400000000000000000000000000000000000000000000000000000000000000}, |
| 36 | + ), |
| 37 | + Account( |
| 38 | + storage={0x00: 0x3400000000000000000000000000000000000000000000000000000000000000}, |
| 39 | + ), |
| 40 | + ), |
| 41 | + ( |
| 42 | + ( |
| 43 | + Op.CALLDATACOPY(dest_offset=0, offset=1, size=0) |
| 44 | + + Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0)) |
| 45 | + + Op.RETURN(offset=0, size=Op.MSIZE) |
| 46 | + ), |
| 47 | + b"\x02", |
| 48 | + Account( |
| 49 | + storage={0x00: 0x00}, |
| 50 | + ), |
| 51 | + Account( |
| 52 | + storage={0x00: 0x00}, |
| 53 | + ), |
| 54 | + ), |
| 55 | + ( |
| 56 | + ( |
| 57 | + Op.CALLDATACOPY(dest_offset=0, offset=0, size=0) |
| 58 | + + Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0)) |
| 59 | + + Op.RETURN(offset=0, size=Op.MSIZE) |
| 60 | + ), |
| 61 | + b"\x03", |
| 62 | + Account( |
| 63 | + storage={0x00: 0x00}, |
| 64 | + ), |
| 65 | + Account( |
| 66 | + storage={0x00: 0x00}, |
| 67 | + ), |
| 68 | + ), |
| 69 | + ( |
| 70 | + ( |
| 71 | + Op.CALLDATACOPY( |
| 72 | + dest_offset=0, |
| 73 | + offset=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA, |
| 74 | + size=0xFF, |
| 75 | + ) |
| 76 | + + Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0)) |
| 77 | + + Op.RETURN(offset=0, size=Op.MSIZE) |
| 78 | + ), |
| 79 | + b"\x04", |
| 80 | + Account(storage={0x00: 0x00}), |
| 81 | + Account(storage={0x00: 0x00}), |
| 82 | + ), |
| 83 | + ( |
| 84 | + ( |
| 85 | + Op.CALLDATACOPY( |
| 86 | + dest_offset=0, |
| 87 | + offset=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA, |
| 88 | + size=0x9, |
| 89 | + ) |
| 90 | + + Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0)) |
| 91 | + + Op.RETURN(offset=0, size=Op.MSIZE) |
| 92 | + ), |
| 93 | + b"\x05", |
| 94 | + Account(storage={0x00: 0x00}), |
| 95 | + Account(storage={0x00: 0x00}), |
| 96 | + ), |
| 97 | + ( |
| 98 | + (Op.SSTORE(key=0x1, value=0x1) + Op.PUSH1[0x1] + Op.PUSH1[0x2] + Op.CALLDATACOPY), |
| 99 | + b"\x10", |
| 100 | + Account(storage={0x01: 0x00}), |
| 101 | + None, |
| 102 | + ), |
| 103 | + ( |
| 104 | + ( |
| 105 | + Op.JUMP(pc=0x5) |
| 106 | + + Op.JUMPDEST |
| 107 | + + Op.STOP |
| 108 | + + Op.JUMPDEST |
| 109 | + + Op.MSTORE8(offset=0x1F, value=0x42) |
| 110 | + + Op.CALLDATACOPY(dest_offset=0x1F, offset=0x0, size=0x103) |
| 111 | + + Op.MLOAD(offset=0x0) |
| 112 | + + Op.DUP1 |
| 113 | + + Op.PUSH1[0x60] |
| 114 | + + Op.JUMPI(pc=0x3, condition=Op.EQ) |
| 115 | + + Op.SSTORE(key=0xFF, value=0xBADC0FFEE) |
| 116 | + ), |
| 117 | + b"\x11", |
| 118 | + Account(storage={0xFF: 0xBADC0FFEE}), |
| 119 | + None, |
| 120 | + ), |
| 121 | + ], |
| 122 | + ids=[ |
| 123 | + "cdc 0 1 2", |
| 124 | + "cdc 0 1 1", |
| 125 | + "cdc 0 1 0", |
| 126 | + "cdc 0 0 0", |
| 127 | + "cdc 0 neg6 ff", |
| 128 | + "cdc 0 neg6 9", |
| 129 | + "underflow", |
| 130 | + "sec", |
| 131 | + ], |
| 132 | +) |
| 133 | +def test_calldatacopy( |
| 134 | + state_test: StateTestFiller, |
| 135 | + code: Bytecode, |
| 136 | + fork: Fork, |
| 137 | + tx_data: bytes, |
| 138 | + pre: Alloc, |
| 139 | + code_address_storage: Account, |
| 140 | + to_address_storage: Account | None, |
| 141 | +): |
| 142 | + """ |
| 143 | + Test `CALLDATACOPY` opcode. |
| 144 | +
|
| 145 | + Based on https://github.com/ethereum/tests/blob/ae4791077e8fcf716136e70fe8392f1a1f1495fb/src/GeneralStateTestsFiller/VMTests/vmTests/calldatacopyFiller.ym |
| 146 | + """ |
| 147 | + code_address = pre.deploy_contract(code) |
| 148 | + to = pre.deploy_contract( |
| 149 | + code=( |
| 150 | + Op.MSTORE(offset=0x0, value=0x1234567890ABCDEF01234567890ABCDEF0) |
| 151 | + + Op.CALL( |
| 152 | + gas=Op.SUB(Op.GAS(), 0x100), |
| 153 | + address=code_address, |
| 154 | + value=0x0, |
| 155 | + args_offset=0xF, |
| 156 | + args_size=0x10, |
| 157 | + ret_offset=0x20, |
| 158 | + ret_size=0x40, |
| 159 | + ) |
| 160 | + + Op.POP |
| 161 | + + Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0x20)) |
| 162 | + + Op.SSTORE(key=0x1, value=Op.MLOAD(offset=0x40)) |
| 163 | + + Op.STOP |
| 164 | + ), |
| 165 | + ) |
| 166 | + |
| 167 | + tx = Transaction( |
| 168 | + data=tx_data, |
| 169 | + gas_limit=100_000, |
| 170 | + gas_price=0x0A, |
| 171 | + protected=fork >= Byzantium, |
| 172 | + sender=pre.fund_eoa(), |
| 173 | + to=to, |
| 174 | + value=0x01, |
| 175 | + ) |
| 176 | + if to_address_storage: |
| 177 | + post = {code_address: code_address_storage, to: to_address_storage} |
| 178 | + else: |
| 179 | + post = {code_address: code_address_storage} |
| 180 | + state_test(pre=pre, post=post, tx=tx) |
0 commit comments