EIP-3779 EVM control flow safety rules (Safer Control Flow for the EVM)

No call decision · EVM & execution · EIP status Withdrawn · created 2021-08-30

Defines rules that validate jump destinations and stack usage at deployment time so that an EVM contract does not halt on an exception during execution. All errors other than out-of-gas and recursive stack overflow are filtered out before deployment.

Why it is needed

Currently, EVM bytecode allows JUMP and JUMPI destinations to be determined by a stack value at runtime. Because of this dynamic jumping, whether code will encounter errors such as stack underflow, invalid jumps, or invalid instructions can only be known by running it. To verify safety by tracing code ahead of time, jump destinations must be determinable in advance just by looking at the code, but if this verification itself is slow, it becomes a vector for denial-of-service attacks. This EIP places constraints on jumps and stack usage so that a contract's safety can be verified, before it is placed on chain, in time proportional to the size of the code.

What changes

Today

After EIP-3779

Who is affected

In depth

Five exceptional halting states

The Yellow Paper defines five cases in which the EVM halts abnormally: out of gas, more than 1024 stack items (overflow), insufficient stack items (underflow), invalid jump destination, and invalid instruction. Of these, out of gas and stack overflow caused by recursion cannot be known just by looking at the code, so they are checked at runtime. The remaining three, stack underflow, invalid jump, and invalid instruction, can be filtered out in advance through deployment-time verification. In other words, valid contracts cannot halt for any reason other than out of gas or recursive stack overflow.

Constraints on valid code

Every instruction must be valid and every jump must be valid. JUMP and JUMPI must be static, and RJUMP, RJUMPI, and RJUMPSUB must not point into an immediate-value region. The number of items on the data stack and return stack must always be between 1 and 1024 inclusive. In addition, the data stack must be consistently aligned: no matter how many times the same bytecode is executed, the number of items between the stack pointer at entry to the most recent basic block and the current stack pointer must always be the same. Following these rules allows the code's control flow graph to be verified by visiting each edge exactly once, in time and space proportional to the size of the code.

Definition of a static jump

A JUMP or JUMPI is considered static if its jumpsrc argument was first pushed onto the stack by PUSH and has not changed value since. Copying that value with DUP or SWAP is allowed. RJUMP, RJUMPI, and RJUMPSUB, on the other hand, carry their destination directly as an immediate argument, so they are always static by definition. The original text explains that this is the minimal definition needed to implement subroutines, and that using deeper analysis could recognize more jump patterns as valid, but at the cost of increased verification expense.

Two options for handling dynamic jumps

The original text presents two ways to handle dynamic jumps (JUMP and JUMPI whose destination is only determined at runtime). One is to define JUMP and JUMPI as INVALID for the purposes of EOF code validation, blocking their use; the other is to constrain them through static analysis. This EIP chooses the second approach, showing that a subroutine call/return pattern, where a destination constant is pushed with PUSH and then that value moves unchanged across the top of the stack, can be traced statically. This demonstrates that unconstrained dynamic jumps are not actually necessary to implement subroutines.

Verification algorithm and its limitations

The reference implementation is a recursive symbolic execution algorithm written in pseudo-Go. It treats the code as a control flow graph and visits each basic block (vertex) and branch (edge) once, running in O(vertices + edges), i.e., time proportional to code size. The original text explicitly notes about this verification function that "this is a work in progress, and the version below is known to be wrong," disclosing that the algorithm included in the specification itself is not yet complete.

Backward compatibility

Because this change restricts JUMP, JUMPI, and how the stack is used, some code that previously executed normally would be judged invalid under these rules. The original text explicitly states that this is a change affecting the semantics of EVM code.

Status: Withdrawn, and the related proposal

This EIP's status is Withdrawn, and the withdrawal-reason is "content moved to EIP-2315." The body describes a static control flow instruction set built on top of EIP-4200's relative jumps (RJUMP, RJUMPI) and EIP-2315's simple subroutines (RJUMPSUB, RETURNSUB), showing that this proposal was conceptually bundled with those two EIPs.

Original EIP · ← EIPs