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

After EIP-615

Who is affected

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.

Original EIP · ← EIPs