You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: AVM witgen track gas for nested calls and external halts (#10731)
Resolves#10033Resolves#10374
This PR does the following:
- Witgen handles out-of-gas errors for all opcodes
- all halts (return/revert/exceptional) work as follows:
- charge gas for the problematic instruction as always, adding a row to
the gas trace
- pop the parent/caller's latest gas from the stack
- call a helper function on the gas trace to mutate that most recent gas
row, returning to the parent's latest gas minus any consumed gas (all
gas consumed on exceptional halt)
- `GasTraceEntry` includes a field `is_halt_or_first_row_in_nested_call`
which lets us break gas rules on a halt or when starting a nested call
because in both cases gas will jump.
- `constrain_gas` returns a bool `out_of_gas` so that opcode
implementations can handle out of gas
- `write_to_memory` now has an option to skip the "jump back to correct
pc" which was problematic when halting because the `jump` wouldn't
result in a next row with the right pc
Explanation on how gas works for calls:
- Parent snapshots its gas right before a nested call in
`ctx.*_gas_left`
- Nested call is given a `ctx.start_*_gas_left` and the gas trace is
forced to that same value
- throughout the nested call, the gas trace operates normally, charging
per instruction
- when any halt is encountered, the instruction that halted must have
its gas charged normally, but then we call a helper function on the gas
trace to mutate the most recent row, flagging it to eventually become a
sort of "fake" row that skips some constraints
- the mutation of the halting row resets the gas to the parents last gas
before the call (minus however much gas was consumed by the nested
call... if exceptional halt, that is _all_ allocated gas)
Follow-up work
- properly constrain gas for nested calls, returns, reverts and
exceptional halts
- if `jump` exceptionally halts (i.e. out of gas), it should be okay
that the next row doesn't have the target pc
- Handle the edge case when an error is encountered on
return/revert/call, but after the stack has already been modified
0 commit comments