EIPs Explained

EIP-3779 · Withdrawn · Core · 2021-08-30

EIP-3779: Safer Control Flow for the EVM

A withdrawn proposal to reject, at deployment time, EVM code that could hit invalid jumps, invalid opcodes or stack underflow, using a linear-time static check.

What problem does it solve?

Today the EVM discovers many bugs only while running: a jump to a bad destination, an undefined opcode, or popping from an empty stack all cause an exceptional halt at runtime. The EVM also has to keep checking for these conditions on every instruction.

EIP-3779 proposed defining a safe contract as one that can never reach such a state, and validating that property once, when the code is deployed. Unsafe code would simply never make it onchain, and clients could skip some runtime checks.

How it works

The EIP adds no new opcodes. It restricts how existing and proposed control-flow instructions may be used, so that a validator can walk the control-flow graph once, in time and space linear in the code size (important to avoid denial-of-service via expensive validation).

Of the five exceptional halting states the Yellow Paper defines, three would be ruled out at validation time:

Halting state Where it is caught
Invalid instruction Validation
Invalid jump destination Validation
Stack underflow Validation
Out of gas Runtime
Stack overflow (recursive) Runtime

Constraints on valid code include:

  • Every instruction is defined.
  • Every JUMP/JUMPI is static: its destination was pushed with a PUSH and only moved by DUP/SWAP since. Relative jumps (RJUMP, RJUMPI from EIP-4200) and subroutine calls (RJUMPSUB/RETURNSUB from EIP-2315) are static by construction.
  • No jump may land inside immediate data.
  • Data and return stack heights stay within bounds, and the data stack height on entering a basic block is the same on every path ("consistently aligned"). This is what prevents underflow and catches subroutine calls with the wrong number of arguments.

Sample code

The classic "subroutine via JUMP" pattern that stays valid, because both addresses are constants pushed onto the stack:

TEST_SQUARE:
    JUMPDEST
    PUSH RTN_SQUARE   ; return address, a constant
    PUSH 0x02         ; argument
    PUSH SQUARE       ; destination, a constant
    JUMP              ; static: target came from PUSH
RTN_SQUARE:
    JUMPDEST
    SWAP1
    JUMP              ; static: target is the pushed RTN_SQUARE
SQUARE:
    JUMPDEST
    DUP1
    MUL
    SWAP1
    JUMP

Something like CALLDATALOAD followed by JUMP (destination taken from user input) would be rejected.

Things to know

  • Status: Withdrawn. Its material was folded back into EIP-2315, which was itself later withdrawn.
  • The idea of deploy-time validation lives on in the EVM Object Format work: EIP-3540 (container), EIP-3670 (code validation) and EIP-5450 (stack validation), which took the route of banning dynamic jumps inside EOF code instead of constraining them.
  • Validation is conservative: code with an invalid path that can never actually execute is still rejected.

#evm #validation #control-flow #core #withdrawn

Details

Authors
Greg Colvin (@gcolvin), Greg Colvin <[email protected]>, Brooklyn Zelenka (@expede)
Status
Withdrawn
Type
Standards Track / Core
Created
2021-08-30
Required by

Machine-readable: Markdown · JSON