Skip to content

Test Returndataload

Documentation for tests/prague/eip7692_eof_v1/eip7069_extcall/test_returndataload.py.

Generate fixtures for these test cases for Prague with:

Prague only:

fill -v tests/prague/eip7692_eof_v1/eip7069_extcall/test_returndataload.py --fork=Prague --evm-bin=/path/to/evm-tool-dev-version
For all forks up to and including Prague:
fill -v tests/prague/eip7692_eof_v1/eip7069_extcall/test_returndataload.py --until=Prague --evm-bin=/path/to/evm-tool-dev-version

Tests EIP-7069: Revamped CALL instructions

Tests for the RETURNDATALOAD instriction

test_returndatacopy_handling(state_test, pre, call_prefix, opcode, call_suffix, return_data, offset, size)

Tests ReturnDataLoad including multiple offset conditions and differeing legacy vs. eof boundary conditions.

entrypoint creates a "0xff" test area of memory, delegate calls to caller. Caller is either EOF or legacy, as per parameter. Calls returner and copies the return data based on offset and size params. Cases are expected to trigger boundary violations.

Entrypoint copies the test area to storage slots, and the expected result is asserted.

Source code in tests/prague/eip7692_eof_v1/eip7069_extcall/test_returndataload.py
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
@pytest.mark.parametrize(
    ["call_prefix", "opcode", "call_suffix"],
    [
        pytest.param([500_000], Op.CALL, [0, 0, 0, 0, 0], id="CALL"),
        pytest.param([500_000], Op.CALLCODE, [0, 0, 0, 0, 0], id="CALLCODE"),
        pytest.param([500_000], Op.DELEGATECALL, [0, 0, 0, 0], id="DELEGATECALL"),
        pytest.param([500_000], Op.STATICCALL, [0, 0, 0, 0], id="STATICCALL"),
        pytest.param([], Op.EXTCALL, [0, 0, 0], id="EXTCALL"),
        pytest.param([], Op.EXTDELEGATECALL, [0, 0], id="EXTDELEGATECALL"),
        pytest.param([], Op.EXTSTATICCALL, [0, 0], id="EXTSTATICCALL"),
    ],
    ids=lambda x: x,
)
@pytest.mark.parametrize(
    "return_data",
    [
        b"",
        b"\x10" * 0x10,
        b"\x20" * 0x20,
        b"\x30" * 0x30,
    ],
    ids=lambda x: "len_%x" % len(x),
)
@pytest.mark.parametrize(
    "offset",
    [
        0,
        0x10,
        0x20,
        0x30,
    ],
    ids=lambda x: "offset_%x" % x,
)
@pytest.mark.parametrize(
    "size",
    [
        0,
        0x10,
        0x20,
        0x30,
    ],
    ids=lambda x: "size_%x" % x,
)
def test_returndatacopy_handling(
    state_test: StateTestFiller,
    pre: Alloc,
    call_prefix: List[int],
    opcode: Op,
    call_suffix: List[int],
    return_data: bytes,
    offset: int,
    size: int,
):
    """
    Tests ReturnDataLoad including multiple offset conditions and differeing legacy vs. eof
    boundary conditions.

    entrypoint creates a "0xff" test area of memory, delegate calls to caller.
    Caller is either EOF or legacy, as per parameter.  Calls returner and copies the return data
    based on offset and size params.  Cases are expected to trigger boundary violations.

    Entrypoint copies the test area to storage slots, and the expected result is asserted.
    """
    env = Environment()

    slot_result_start = 0x1000

    sender = pre.fund_eoa(10**18)

    address_returner = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.DATACOPY(0, 0, Op.DATASIZE) + Op.RETURN(0, Op.DATASIZE),
                ),
                Section.Data(data=return_data),
            ]
        )
    )

    result = [0xFF] * 0x40
    result[0:size] = [0] * size
    extent = size - max(0, size + offset - len(return_data))
    if extent > 0 and len(return_data) > 0:
        result[0:extent] = [return_data[0]] * extent

    code_under_test = (
        opcode(*call_prefix, address_returner, *call_suffix)
        + Op.RETURNDATACOPY(0, offset, size)
        + Op.SSTORE(slot_code_worked, value_code_worked)
        + Op.RETURN(0, size)
    )
    match opcode:
        case Op.EXTCALL | Op.EXTDELEGATECALL | Op.EXTSTATICCALL:
            address_caller = pre.deploy_contract(
                Container(
                    sections=[
                        Section.Code(
                            code=code_under_test,
                        )
                    ]
                )
            )
        case Op.CALL | Op.CALLCODE | Op.DELEGATECALL | Op.STATICCALL:
            address_caller = pre.deploy_contract(code_under_test)

    address_entry_point = pre.deploy_contract(
        Op.NOOP
        # First, create a "dirty" area, so we can check zero overwrite
        + Op.MSTORE(0x00, -1)
        + Op.MSTORE(0x20, -1)
        # call the contract under test
        + Op.DELEGATECALL(1_000_000, address_caller, 0, 0, 0, 0)
        + Op.RETURNDATACOPY(0, 0, Op.RETURNDATASIZE)
        # store the return data
        + Op.SSTORE(slot_result_start, Op.MLOAD(0x0))
        + Op.SSTORE(slot_result_start + 1, Op.MLOAD(0x20))
        + Op.SSTORE(slot_code_worked, value_code_worked)
        + Op.STOP,
    )

    post = {
        address_entry_point: Account(
            storage={
                slot_code_worked: value_code_worked,
                slot_result_start: bytes(result[:0x20]),
                (slot_result_start + 0x1): bytes(result[0x20:]),
            }
        )
    }
    if opcode in [Op.CALL, Op.CALLCODE, Op.DELEGATECALL, Op.STATICCALL] and (
        (offset + size) > len(return_data)
    ):
        post[address_entry_point] = Account(
            storage={
                slot_code_worked: value_code_worked,
                slot_result_start: b"\xff" * 32,
                slot_result_start + 1: b"\xff" * 32,
            }
        )

    tx = Transaction(to=address_entry_point, gas_limit=2_000_000, sender=sender)

    state_test(
        env=env,
        pre=pre,
        tx=tx,
        post=post,
    )

test_returndataload_handling(state_test, pre, opcode, call_suffix, return_data, offset)

Much simpler than returndatacopy, no memory or boosted call. Returner is called and results are stored in storage slot, which is asserted for expected values. The parameters offset and return data are configured to test boundary conditions.

Source code in tests/prague/eip7692_eof_v1/eip7069_extcall/test_returndataload.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
@pytest.mark.parametrize(
    ["opcode", "call_suffix"],
    [
        pytest.param(Op.EXTCALL, [0, 0, 0], id="EXTCALL"),
        pytest.param(Op.EXTDELEGATECALL, [0, 0], id="EXTDELEGATECALL"),
        pytest.param(Op.EXTSTATICCALL, [0, 0], id="EXTSTATICCALL"),
    ],
    ids=lambda x: x,
)
@pytest.mark.parametrize(
    "return_data",
    [
        b"",
        b"\x10" * 0x10,
        b"\x20" * 0x20,
        b"\x30" * 0x30,
    ],
    ids=lambda x: "len_%x" % len(x),
)
@pytest.mark.parametrize(
    "offset",
    [
        0,
        0x10,
        0x20,
        0x30,
    ],
    ids=lambda x: "offset_%x" % x,
)
def test_returndataload_handling(
    state_test: StateTestFiller,
    pre: Alloc,
    opcode: Op,
    call_suffix: List[int],
    return_data: bytes,
    offset: int,
):
    """
    Much simpler than returndatacopy, no memory or boosted call.  Returner is called
    and results are stored in storage slot, which is asserted for expected values.
    The parameters offset and return data are configured to test boundary conditions.
    """
    env = Environment()

    slot_result_start = 0x1000

    sender = pre.fund_eoa(10**18)
    address_returner = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.DATACOPY(0, 0, Op.DATASIZE) + Op.RETURN(0, Op.DATASIZE),
                ),
                Section.Data(data=return_data),
            ]
        )
    )
    address_entry_point = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=opcode(address_returner, *call_suffix)
                    + Op.SSTORE(slot_result_start, Op.RETURNDATALOAD(offset))
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP,
                )
            ]
        )
    )

    result = [0] * 0x20
    extent = 0x20 - max(0, 0x20 + offset - len(return_data))
    if extent > 0 and len(return_data) > 0:
        result[0:extent] = [return_data[0]] * extent
    post = {
        address_entry_point: Account(
            storage={
                slot_code_worked: value_code_worked,
                slot_result_start: bytes(result),
            }
        )
    }

    tx = Transaction(to=address_entry_point, gas_limit=2_000_000, sender=sender)

    state_test(
        env=env,
        pre=pre,
        tx=tx,
        post=post,
    )

test_returndatacopy_oob(state_test, pre, call_prefix, opcode, call_suffix)

Extends the RETURNDATACOPY test for correct out-of-bounds behavior, by checking if the caller frame's context being EOF or legacy doesn't impact the execution logic of the RETURNDATACOPY instance under test.

Source code in tests/prague/eip7692_eof_v1/eip7069_extcall/test_returndataload.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
@pytest.mark.parametrize(
    ["call_prefix", "opcode", "call_suffix"],
    [
        pytest.param([500_000], Op.CALL, [0, 0, 0, 0, 0], id="CallerIsLegacy"),
        pytest.param([], Op.EXTCALL, [0, 0, 0], id="CallerIsEOF"),
    ],
    ids=lambda x: x,
)
def test_returndatacopy_oob(
    state_test: StateTestFiller,
    pre: Alloc,
    call_prefix: List[int],
    opcode: Op,
    call_suffix: List[int],
):
    """
    Extends the RETURNDATACOPY test for correct out-of-bounds behavior, by checking if the
    caller frame's context being EOF or legacy doesn't impact the execution logic of the
    RETURNDATACOPY instance under test.
    """
    env = Environment()

    sender = pre.fund_eoa(10**18)

    # Both callee codes below make an OOB (out-of-bounds) RETURNDATACOPY of one byte,
    # which they then attempt to return (Legacy should exceptionally halt on RETURNDATACOPY).
    address_callee_eof = pre.deploy_contract(
        code=Container(
            sections=[
                Section.Code(
                    code=Op.RETURNDATACOPY(0, 0, 1) + Op.RETURN(0, 1),
                    max_stack_height=3,
                )
            ]
        )
    )
    address_callee_legacy = pre.deploy_contract(Op.RETURNDATACOPY(0, 0, 1) + Op.RETURN(0, 1))

    # Caller code is selected to either be Legacy or EOF using params.
    code_entry_point = (
        Op.SSTORE(
            slot_eof_target_call_status, opcode(*call_prefix, address_callee_eof, *call_suffix)
        )
        + Op.SSTORE(slot_eof_target_returndatasize, Op.RETURNDATASIZE)
        + Op.SSTORE(slot_eof_target_returndata, Op.RETURNDATACOPY(0, 0, 1) + Op.MLOAD(0))
        + Op.SSTORE(
            slot_legacy_target_call_status,
            opcode(*call_prefix, address_callee_legacy, *call_suffix),
        )
        + Op.SSTORE(slot_legacy_target_returndatasize, Op.RETURNDATASIZE)
        + Op.STOP
    )

    storage_entry_point = Storage(
        {
            slot_eof_target_call_status: value_exceptional_abort_canary,
            slot_eof_target_returndata: value_exceptional_abort_canary,
            slot_eof_target_returndatasize: value_exceptional_abort_canary,
            slot_legacy_target_call_status: value_exceptional_abort_canary,
            slot_legacy_target_returndatasize: value_exceptional_abort_canary,
        }
    )

    address_entry_point = (
        pre.deploy_contract(code=code_entry_point, storage=storage_entry_point)
        if opcode == Op.CALL
        else pre.deploy_contract(
            Container(
                sections=[
                    Section.Code(
                        code=code_entry_point,
                        max_stack_height=4,
                        storage=storage_entry_point,
                    )
                ]
            )
        )
    )

    tx = Transaction(to=address_entry_point, gas_limit=2_000_000, sender=sender)

    post = {
        address_entry_point: Account(
            storage={
                slot_eof_target_call_status: CALL_SUCCESS,
                slot_eof_target_returndata: "0x00",
                slot_eof_target_returndatasize: "0x01",
                slot_legacy_target_call_status: CALL_FAILURE,
                slot_legacy_target_returndatasize: "0x00",
            }
            if opcode == Op.CALL
            else {
                slot_eof_target_call_status: EXTCALL_SUCCESS,
                slot_eof_target_returndata: "0x00",
                slot_eof_target_returndatasize: "0x01",
                slot_legacy_target_call_status: EXTCALL_FAILED,
                slot_legacy_target_returndatasize: "0x00",
            }
        )
    }

    state_test(
        env=env,
        pre=pre,
        post=post,
        tx=tx,
    )