Skip to content

test_worst_returndatacopy()

Documentation for tests/benchmark/test_worst_memory.py::test_worst_returndatacopy@0f7c73a7.

Generate fixtures for these test cases for Prague with:

fill -v tests/benchmark/test_worst_memory.py::test_worst_returndatacopy --fork Prague

Test running a block filled with RETURNDATACOPY executions.

Source code in tests/benchmark/test_worst_memory.py
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
@pytest.mark.valid_from("Cancun")
@pytest.mark.parametrize(
    "size",
    [
        pytest.param(0, id="0 bytes"),
        pytest.param(100, id="100 bytes"),
        pytest.param(10 * 1024, id="10KiB"),
        pytest.param(1024 * 1024, id="1MiB"),
    ],
)
@pytest.mark.parametrize(
    "fixed_dst",
    [
        True,
        False,
    ],
)
def test_worst_returndatacopy(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    size: int,
    fixed_dst: bool,
):
    """Test running a block filled with RETURNDATACOPY executions."""
    env = Environment()
    max_code_size = fork.max_code_size()

    # Create the contract that will RETURN the data that will be used for RETURNDATACOPY.
    # Random-ish data is injected at different points in memory to avoid making the content
    # predictable. If `size` is 0, this helper contract won't be used.
    code = (
        Op.MSTORE8(0, Op.GAS)
        + Op.MSTORE8(size // 2, Op.GAS)
        + Op.MSTORE8(size - 1, Op.GAS)
        + Op.RETURN(0, size)
    )
    helper_contract = pre.deploy_contract(code=code)

    # We create the contract that will be doing the RETURNDATACOPY multiple times.
    returndata_gen = Op.STATICCALL(address=helper_contract) if size > 0 else Bytecode()
    dst = 0 if fixed_dst else Op.MOD(Op.GAS, 7)
    attack_iter = Op.RETURNDATACOPY(dst, Op.PUSH0, Op.RETURNDATASIZE)

    jumpdest = Op.JUMPDEST
    jump_back = Op.JUMP(len(returndata_gen))
    # The attack loop is constructed as:
    # ```
    # JUMPDEST(#)
    # RETURNDATACOPY(...)
    # RETURNDATACOPY(...)
    # ...
    # STATICCALL(address=helper_contract)
    # JUMP(#)
    # ```
    # The goal is that once per (big) loop iteration, the helper contract is called to
    # generate fresh returndata to continue calling RETURNDATACOPY.
    max_iters_loop = (
        max_code_size - 2 * len(returndata_gen) - len(jumpdest) - len(jump_back)
    ) // len(attack_iter)
    code = (
        returndata_gen
        + jumpdest
        + sum([attack_iter] * max_iters_loop)
        + returndata_gen
        + jump_back
    )
    assert len(code) <= max_code_size, (
        f"Code size {len(code)} is not equal to max code size {max_code_size}."
    )

    tx = Transaction(
        to=pre.deploy_contract(code=code),
        gas_limit=env.gas_limit,
        sender=pre.fund_eoa(),
    )

    state_test(
        genesis_environment=env,
        pre=pre,
        post={},
        tx=tx,
    )

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) fixed_dst size
...fork_Cancun-state_test-fixed_dst_True-0 bytes True 0
...fork_Cancun-state_test-fixed_dst_True-100 bytes True 100
...fork_Cancun-state_test-fixed_dst_True-10KiB True 10240
...fork_Cancun-state_test-fixed_dst_True-1MiB True 1048576
...fork_Cancun-state_test-fixed_dst_False-0 bytes False 0
...fork_Cancun-state_test-fixed_dst_False-100 bytes False 100
...fork_Cancun-state_test-fixed_dst_False-10KiB False 10240
...fork_Cancun-state_test-fixed_dst_False-1MiB False 1048576
...fork_Cancun-blockchain_test_from_state_test-fixed_dst_True-0 bytes True 0
...fork_Cancun-blockchain_test_from_state_test-fixed_dst_True-100 bytes True 100
...fork_Cancun-blockchain_test_from_state_test-fixed_dst_True-10KiB True 10240
...fork_Cancun-blockchain_test_from_state_test-fixed_dst_True-1MiB True 1048576
...fork_Cancun-blockchain_test_from_state_test-fixed_dst_False-0 bytes False 0
...fork_Cancun-blockchain_test_from_state_test-fixed_dst_False-100 bytes False 100
...fork_Cancun-blockchain_test_from_state_test-fixed_dst_False-10KiB False 10240
...fork_Cancun-blockchain_test_from_state_test-fixed_dst_False-1MiB False 1048576
...fork_Prague-state_test-fixed_dst_True-0 bytes True 0
...fork_Prague-state_test-fixed_dst_True-100 bytes True 100
...fork_Prague-state_test-fixed_dst_True-10KiB True 10240
...fork_Prague-state_test-fixed_dst_True-1MiB True 1048576
...fork_Prague-state_test-fixed_dst_False-0 bytes False 0
...fork_Prague-state_test-fixed_dst_False-100 bytes False 100
...fork_Prague-state_test-fixed_dst_False-10KiB False 10240
...fork_Prague-state_test-fixed_dst_False-1MiB False 1048576
...fork_Prague-blockchain_test_from_state_test-fixed_dst_True-0 bytes True 0
...fork_Prague-blockchain_test_from_state_test-fixed_dst_True-100 bytes True 100
...fork_Prague-blockchain_test_from_state_test-fixed_dst_True-10KiB True 10240
...fork_Prague-blockchain_test_from_state_test-fixed_dst_True-1MiB True 1048576
...fork_Prague-blockchain_test_from_state_test-fixed_dst_False-0 bytes False 0
...fork_Prague-blockchain_test_from_state_test-fixed_dst_False-100 bytes False 100
...fork_Prague-blockchain_test_from_state_test-fixed_dst_False-10KiB False 10240
...fork_Prague-blockchain_test_from_state_test-fixed_dst_False-1MiB False 1048576
...fork_Osaka-state_test-fixed_dst_True-0 bytes True 0
...fork_Osaka-state_test-fixed_dst_True-100 bytes True 100
...fork_Osaka-state_test-fixed_dst_True-10KiB True 10240
...fork_Osaka-state_test-fixed_dst_True-1MiB True 1048576
...fork_Osaka-state_test-fixed_dst_False-0 bytes False 0
...fork_Osaka-state_test-fixed_dst_False-100 bytes False 100
...fork_Osaka-state_test-fixed_dst_False-10KiB False 10240
...fork_Osaka-state_test-fixed_dst_False-1MiB False 1048576
...fork_Osaka-blockchain_test_from_state_test-fixed_dst_True-0 bytes True 0
...fork_Osaka-blockchain_test_from_state_test-fixed_dst_True-100 bytes True 100
...fork_Osaka-blockchain_test_from_state_test-fixed_dst_True-10KiB True 10240
...fork_Osaka-blockchain_test_from_state_test-fixed_dst_True-1MiB True 1048576
...fork_Osaka-blockchain_test_from_state_test-fixed_dst_False-0 bytes False 0
...fork_Osaka-blockchain_test_from_state_test-fixed_dst_False-100 bytes False 100
...fork_Osaka-blockchain_test_from_state_test-fixed_dst_False-10KiB False 10240
...fork_Osaka-blockchain_test_from_state_test-fixed_dst_False-1MiB False 1048576