EIP-615 EVM Subroutines and Static Jumps (Subroutines and Static Jumps for the EVM)
No call decision · EVM & execution · EIP status Stagnant · created 2016-12-10
A proposal to replace the EVM's dynamic jumps (JUMP/JUMPI) with static jumps and dedicated subroutine instructions, so that code safety can be verified in linear time before it is deployed on chain.
Why it is needed
Today the EVM only supports dynamic jumps, where the destination is determined by a value on the stack. Because the destination can only be known by executing the code, static analysis tools that try to verify safety by inspecting the code alone must trace every possible path. As the number of jumps and destinations grows, the number of paths to trace grows multiplicatively, causing analysis time to explode, and in some cases making verification at deployment time flatly impossible. On a chain handling billions of dollars, it should be possible to confirm before deployment that no runtime exception will occur, but dynamic jumps block this. Dynamic jumps also make fast compilation to machine code harder, pushing excessive reliance on precompiles to compensate for a slow EVM.
What changes
Today
- Only dynamic jumps (JUMP/JUMPI) exist, where the jump destination is determined by a stack value
- Subroutines can only be emulated using static jumps plus a return address pushed onto the stack
- Because code cannot statically determine where it will jump, analyzers must inspect every possible path
- Return addresses and local variables are mixed into the general data stack, creating a risk of tampering
After EIP-615
- Provide static jumps, JUMPTO/JUMPIF, with destinations fixed in the code
- Provide subroutines with a single entry point via BEGINSUB/JUMPSUB/RETURNSUB, with return addresses and the frame pointer managed on separate stacks
- Allow jump tables (switches, callbacks, virtual functions) only within predetermined ranges via JUMPV/JUMPSUBV
- Access subroutine local variables at fixed offsets via GETLOCAL/PUTLOCAL
Who is affected
- Smart contract compiler and language teams: Bytecode generated by Solidity, Vyper, and similar tools must use the new instruction set, and existing dynamic-jump code becomes a target for gradual deprecation.
- Client and EVM implementation teams: New opcodes (0xb0-0xb9), a return stack and frame stack, and deployment-time validation logic must all be newly added to the interpreter.
- Security analysis and formal verification tool builders: Code that uses only static jumps becomes analyzable in linear time, making analysis tools like Mythril and Securify, as well as formal verification work, easier.
- Users of existing contracts: Dynamic-jump code already deployed before the hard fork continues to execute as-is, so there is no immediate impact.
In depth
Deprecating dynamic jumps and introducing static jumps
JUMP and JUMPI are marked for deprecation, replaced by JUMPTO and JUMPIF, whose destination is a fixed value in the bytecode (jump_target, a 2-byte MSB-first integer) rather than a stack value. They behave like JUMP and JUMPI, but since the destination cannot change at runtime, the branch structure can be determined statically just by inspecting the code.
The three subroutine instructions
BEGINSUB n_args, n_results marks the single entry point of a subroutine: it pops n_args values from the stack on entry and pushes n_results values on return. A subroutine ends at the next BEGINSUB, BEGINDATA, or the end of the code. JUMPSUB jump_target enters that subroutine, and RETURNSUB returns to the instruction after that JUMPSUB.
Restricted dynamic jumps: JUMPV and JUMPSUBV
For cases needing O(1) branching, such as switch statements, callbacks, or virtual functions, JUMPDEST or BEGINSUB offsets are stored in advance as an inline vector, and one is selected using an index from the stack. If the index is out of range, the last (default) entry of the vector is used. Since the space required to store the vector equals the space required to craft the worst-case attack code, verification complexity cannot explode.
Separating the return stack and frame stack from the program
A return stack holding JUMPSUB/JUMPSUBV return addresses, and a frame stack holding the frame pointer FP, are kept separate from the data stack. On entering a subroutine, FP is set to SP+n_args, and the stack items between FP and the current SP form that subroutine's frame. Since the program cannot directly access either stack, it is impossible to tamper with control flow by overwriting a return address or frame pointer.
Accessing local variables via GETLOCAL/PUTLOCAL
PUTLOCAL n places a stack value into local variable n, and GETLOCAL n pushes local variable n onto the stack. Local variable n is the stack item n slots below the frame pointer (FP[-n]). Without this instruction, only 16 variables would be directly accessible via DUP/SWAP.
The 8 validity conditions and linear-time verification
This expands the Ethereum Yellow Paper's 5 exceptional halting conditions (out of gas, stack above 1024, stack underflow, invalid jump destination, invalid instruction) to 8. It requires: the data stack and return stack each stay at or below 1024 (2a, 2b); SP is always at or below FP (3); JUMPTO, JUMPIF, and JUMPV point only to a JUMPDEST (4a), JUMPSUB and JUMPSUBV point only to a BEGINSUB (4b), and jump instructions never target outside their own subroutine (4c); the frame size at JUMPSUB is at least n_args (6); the frame size at RETURNSUB equals n_results (7); and the frame size is constant at every instruction point (8). Only out-of-gas and stack-underflow need to be checked at runtime; the rest are verified at deployment time in roughly n log n, i.e. linear, time.
Separating data from the validation target with BEGINDATA
BEGINDATA marks everything after it to the end of the bytecode as non-executed data, excluding it from validation. This separation is needed because the jump vectors used by JUMPV/JUMPSUBV are not valid code, and runtime code produced by init code may also not be valid code.
Rollout stages and current status
Rollout is proposed in two stages. In stage 1, invalid code is marked for deprecation: tools stop generating it and clients warn about it. In the stage 2 hard fork, only validated code is allowed to be newly deployed on chain. However, even after the hard fork, previously deployed legacy EVM code continues to execute, and new legacy contracts can still be created. For old and new code to coexist on the same chain, a versioning scheme such as EIP-1702 is needed. This EIP's status is Stagnant, meaning discussion has stalled and it was never actually adopted.