Skip to content

Test Tload Reentrancy

Documentation for tests/cancun/eip1153_tstore/test_tload_reentrancy.py.

Generate fixtures for these test cases with:

fill -v tests/cancun/eip1153_tstore/test_tload_reentrancy.py

Ethereum Transient Storage EIP Tests https://eips.ethereum.org/EIPS/eip-1153

test_tload_reentrancy(state_test, pre, call_type, call_return, call_dest_type)

Ported .json vectors:

(05_tloadReentrancyFiller.yml) Reentrant calls access the same transient storage

Source code in tests/cancun/eip1153_tstore/test_tload_reentrancy.py
 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
@pytest.mark.valid_from("Cancun")
@pytest.mark.parametrize("call_type", [Op.CALL, Op.CALLCODE, Op.DELEGATECALL, Op.STATICCALL])
@pytest.mark.parametrize("call_return", [Op.RETURN, Op.REVERT, Om.OOG])
@pytest.mark.parametrize("call_dest_type", [CallDestType.REENTRANCY, CallDestType.EXTERNAL_CALL])
def test_tload_reentrancy(
    state_test: StateTestFiller,
    pre: Alloc,
    call_type: Op,
    call_return: Op,
    call_dest_type: CallDestType,
):
    """
    Ported .json vectors:

    (05_tloadReentrancyFiller.yml)
    Reentrant calls access the same transient storage
    """
    tload_value = 44
    empty_value = 0

    # Storage variables
    slot_tload_in_subcall_result = 1
    slot_tload_after_subcall_result = 2
    slot_subcall_worked = 3
    slot_code_worked = 4

    # Function names
    do_load = 1
    do_reenter = 2
    call_dest_address: Bytecode | Address
    call_dest_address = Op.ADDRESS()

    def make_call(call_type: Op) -> Bytecode:
        if call_type == Op.DELEGATECALL or call_type == Op.STATICCALL:
            return call_type(Op.GAS(), call_dest_address, 0, 32, 32, 32)
        else:
            return call_type(Op.GAS(), call_dest_address, 0, 0, 32, 32, 32)

    subcall_code = Op.MSTORE(0, Op.TLOAD(0)) + call_return(0, 32)

    address_code = pre.deploy_contract(
        balance=0,
        code=subcall_code,
        storage={},
    )
    if call_dest_type == CallDestType.EXTERNAL_CALL:
        call_dest_address = address_code

    address_to = pre.deploy_contract(
        balance=1_000_000_000_000_000_000,
        code=Switch(
            cases=[
                Case(
                    condition=Op.EQ(Op.CALLDATALOAD(0), do_load),
                    action=subcall_code,
                ),
                Case(
                    condition=Op.EQ(Op.CALLDATALOAD(0), do_reenter),
                    action=Op.TSTORE(0, tload_value)
                    + Op.MSTORE(0, do_load)
                    + Op.MSTORE(32, 0xFF)
                    + Op.SSTORE(slot_subcall_worked, make_call(call_type))
                    + Op.SSTORE(slot_tload_in_subcall_result, Op.MLOAD(32))
                    + Op.SSTORE(slot_tload_after_subcall_result, Op.TLOAD(0))
                    + Op.SSTORE(slot_code_worked, 1),
                ),
            ],
            default_action=None,
        ),
        storage={
            slot_tload_in_subcall_result: 0xFF,
            slot_tload_after_subcall_result: 0xFF,
            slot_subcall_worked: 0xFF,
            slot_code_worked: 0xFF,
        },
    )

    if call_dest_type == CallDestType.REENTRANCY:
        post = {
            address_to: Account(
                storage={
                    slot_code_worked: 1,
                    # if call OOG, we fail to obtain the result
                    slot_tload_in_subcall_result: 0xFF if call_return == Om.OOG else tload_value,
                    slot_tload_after_subcall_result: tload_value,
                    slot_subcall_worked: (
                        0 if call_return == Op.REVERT or call_return == Om.OOG else 1
                    ),
                }
            )
        }
    else:
        post = {
            address_to: Account(
                storage={
                    slot_code_worked: 1,
                    slot_tload_in_subcall_result: (
                        0xFF  # if call OOG, we fail to obtain the result
                        if call_return == Om.OOG
                        # else delegate and callcode are working in the same context so tload works
                        else (
                            tload_value
                            if call_type == Op.DELEGATECALL or call_type == Op.CALLCODE
                            else empty_value
                        )
                    ),
                    # no subcall errors can change the tload result
                    slot_tload_after_subcall_result: 44,
                    slot_subcall_worked: (
                        0 if call_return == Op.REVERT or call_return == Om.OOG else 1
                    ),
                }
            )
        }

    tx = Transaction(
        sender=pre.fund_eoa(7_000_000_000_000_000_000),
        to=address_to,
        gas_price=10,
        data=Hash(do_reenter),
        gas_limit=5000000,
        value=0,
    )

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