EIP-5450 · Stagnant · Core · 2022-08-12
EIP-5450: EOF - Stack Validation
Part of EOF: a deploy-time, single-pass check that proves an EOF function can never underflow the stack and can only overflow at CALLF/JUMPF, so the EVM can skip most per-instruction stack checks.
What problem does it solve?
Today's EVM checks for stack underflow and overflow on every instruction it executes. That costs time, and it means bad bytecode is only caught when it runs.
The EVM Object Format (EOF, EIP-3540) validates code when it is deployed instead. EIP-5450 adds stack validation to that process. Every EOF code section is analyzed once, before deployment, and rejected unless the analysis proves stack errors are impossible. After that:
- run-time underflow checks can be removed entirely,
- overflow is checked only at
CALLF/JUMPF, once for the whole called function, - every path ends in a terminating instruction, and unreachable code (sometimes used to hide data in code) is rejected,
- AOT/JIT compilation of EVM code becomes easier.
How it works
The rules build on EIP-3670 (opcode validation), EIP-4200 (static relative jumps RJUMP/RJUMPI/RJUMPV) and EIP-4750 (functions with a type section declaring inputs, outputs and max_stack_increase).
Each code section is scanned once, in order. For each instruction the validator keeps a range [stack_height_min, stack_height_max] of possible stack heights:
- The first instruction starts at
inputs. An instruction that has no recorded range was never reached by fall-through or a forward jump, so it is unreachable and the code is rejected. Compilers must order basic blocks so that no block is reached only by a backward jump. - The lower bound must cover what the instruction pops.
RETFneeds exactlyoutputsitems, andCALLFneeds at least the callee'sinputs. - For
CALLF/JUMPF, the upper bound plus the callee'smax_stack_increasemust not exceed 1024. - Successors reached by forward jumps widen the target's range. A backward jump must reach its target at exactly the recorded height, so loops cannot grow or shrink the stack without limit.
- At the end, the highest height seen minus
inputsmust equal the declaredmax_stack_increase.
The pass is linear in code size. Other terminating instructions (STOP, RETURN, REVERT, …) may leave extra items on the stack. That allows the common compiler pattern of several asserts jumping into one shared revert block from different stack heights.
Sample code
A simplified version of the validator loop:
def validate_stack(code, types, sec):
lo = {0: types[sec].inputs}; hi = {0: types[sec].inputs}
for pc, ins in instructions(code):
if pc not in lo: raise Invalid("unreachable")
need, out = stack_effect(ins, types) # pops, pushes (CALLF uses callee type)
if lo[pc] < need: raise Invalid("underflow")
if ins.op in ("CALLF", "JUMPF") and hi[pc] > 1024 - types[ins.target].max_stack_increase:
raise Invalid("overflow")
nlo, nhi = lo[pc] - need + out, hi[pc] - need + out
for succ in successors(pc, ins):
if succ < pc: # backward jump
if (lo[succ], hi[succ]) != (nlo, nhi): raise Invalid("loop height mismatch")
else:
lo[succ] = min(lo.get(succ, nlo), nlo); hi[succ] = max(hi.get(succ, nhi), nhi)
if max(hi.values()) - types[sec].inputs != types[sec].max_stack_increase:
raise Invalid("max_stack_increase mismatch")
Things to know
- Status: Stagnant, together with the rest of EOF (EIP-7692 is the EOFv1 meta EIP). EOF was considered for several network upgrades but has not shipped on mainnet.
- The rules apply only to EOF-formatted contracts. Legacy bytecode is unaffected.
- An earlier, stricter design required one exact stack height per instruction. Compiler teams objected, so the current version uses min/max ranges for forward jumps.
JUMPFandRETURNCODEare defined in separate EOF EIPs.