EIPs Explained

EIP-3690 · Stagnant · Core · 2021-06-23

EIP-3690: EOF - JUMPDEST Table

Proposed storing an EOF contract's valid jump targets in a dedicated, creation-time-validated table so clients no longer scan code for JUMPDEST bytes on every execution.

What problem does it solve?

In the legacy EVM, a JUMP or JUMPI may only land on a JUMPDEST (0x5b) opcode. But a 0x5b byte can also appear inside PUSH data, where it is not a real instruction. So before running any contract, a client must walk the whole bytecode to work out which 0x5b bytes are genuine jump destinations. This "jumpdest analysis" is repeated every time code runs and grows linearly with code size.

EIP-3690 moves that work to deployment time for contracts using the EVM Object Format (EIP-3540). The list of valid jump targets is written into the container and checked once when the contract is created; at execution time the client just loads the list. The authors' benchmarks on popular mainnet contracts showed loading the table to be roughly 10x faster than the analysis it replaces, with the worst case about the same.

How it works

  • A new EOF section, jumpdests (section kind 3), holds a list of jump locations. It sits directly before the code section and must be present if and only if the code uses JUMP or JUMPI.
  • Locations are delta-encoded (each value is the distance from the previous one) and each delta is written as unsigned LEB128, the variable-length integer format used by WebAssembly. Because jump targets are usually close together, most entries take a single byte.
  • Encoding must be minimal (no padding zero bytes), and only the first delta may be 0, so there are no duplicates.
  • At creation, every location must point to a real opcode: not into PUSH data and not outside the code section.
  • In EOF code the JUMPDEST opcode itself becomes invalid, since the table replaces it. Code containing it fails validation (via the rules of EIP-3670).
  • At runtime, a JUMP or a taken JUMPI to a target not in the table aborts with "bad jump destination", as before.

Legacy (non-EOF) contracts are completely unaffected.

Sample code

Encoding and decoding a jumpdest table (Python, following the spec's rules):

def leb128u(n: int) -> bytes:
    out = bytearray()
    while True:
        b = n & 0x7F
        n >>= 7
        out.append(b | (0x80 if n else 0))
        if not n:
            return bytes(out)

def encode_table(absolute: list[int]) -> bytes:
    deltas, prev = [], 0
    for pos in sorted(absolute):
        deltas.append(pos - prev)
        prev = pos
    return b"".join(leb128u(d) for d in deltas)

# Jump targets at code offsets 0, 5 and 200
print(encode_table([0, 5, 200]).hex())   # "0005c301"

# Decoding: absolute offset of entry n is the partial sum of deltas
def jumpdest(n: int, deltas: list[int]) -> int:
    return sum(deltas[: n + 1])

Things to know

  • Status: Stagnant. This proposal was not adopted. Later EOF designs dropped dynamic jumps entirely in favour of static relative jumps (EIP-4200), which remove the need for a jumpdest table altogether. See the EOF meta proposal EIP-7692 for how the overall EOF bundle evolved.
  • It depended on EOF v1 (EIP-3540) and code validation (EIP-3670), both of which are also Stagnant.
  • The table size is bounded by the EOF maximum section size (0xffff), which also put an implicit bound on initcode analysis for EOF code; legacy code remained unaffected and unbounded by this change.
  • Delta encoding keeps total bytecode size roughly unchanged, because each removed 0x5b byte is replaced by about one table byte. Contracts with jump targets more than 127 bytes apart grow slightly.

#evm #eof #bytecode #jumps #core

Details

Authors
Alex Beregszaszi (@axic), Paweł Bylica (@chfast), Andrei Maiboroda (@gumb0)
Status
Stagnant
Type
Standards Track / Core
Created
2021-06-23
Requires

Machine-readable: Markdown · JSON