EIP-3670 · Stagnant · Core · 2021-06-23
EIP-3670: EOF - Code Validation
Proposed deploy-time validation for EOF contracts that rejects undefined opcodes and truncated PUSH data, so every EOF contract on chain is well-formed bytecode.
What problem does it solve?
Today's (legacy) EVM accepts any byte sequence as contract code. A contract can contain bytes that are not assigned to any instruction, or end in the middle of a PUSH whose data runs past the end of the code. Each EVM implementation has to handle these cases at run time, and nobody can reason about "the instructions in this contract" without caveats.
There is also a forward-compatibility trap: if a contract already contains an unassigned opcode, giving that opcode a meaning later could change how the deployed contract behaves. That makes adding new instructions risky.
EIP-3670 brings code validity into consensus for the EVM Object Format (EIP-3540). Because EOF contracts are validated once at creation, the EVM can trust them afterwards.
How it works
The rule activates together with EIP-3540 and applies only to EOF-formatted code. Legacy bytecode is untouched.
At contract creation, every code section of the EOF container is walked instruction by instruction. The code is rejected if:
- An opcode is undefined.
INVALID(0xfe) counts as defined. Deprecated instructions such asCALLCODE(0xf2) andSELFDESTRUCT(0xff), plus others removed by EIP-3540 and later EOF EIPs, are treated as undefined. - Immediate data is truncated. If a
PUSH1…PUSH32near the end of the section is missing some of its data bytes, the code is invalid. Legacy EVM would implicitly pad with zeros; EOF does not.
Bytes in the data section are not checked; only code sections are.
BLOCKHASH stays valid even though EIP-2935 offers a replacement, to avoid diverging from legacy code without need.
Sample code
Simplified validator, adapted from the spec's reference implementation (opcode table abbreviated):
# valid_opcodes: the fork's assigned opcodes minus those rejected in EOF
immediate_sizes = [0] * 256
for op in range(0x60, 0x80): # PUSH1..PUSH32
immediate_sizes[op] = op - 0x5f
def validate_instructions(code: bytes, valid_opcodes: set[int]) -> None:
assert len(code) > 0
pos = 0
while pos < len(code):
op = code[pos]
if op not in valid_opcodes:
raise ValueError(f"undefined opcode 0x{op:02x} at {pos}")
pos += 1 + immediate_sizes[op] # skip PUSH data
if pos != len(code):
raise ValueError("truncated PUSH data")
validate_instructions(bytes([0x60, 0x01, 0x00]), VALID) # PUSH1 1, STOP -> ok
validate_instructions(bytes([0x61, 0x01]), VALID) # PUSH2 with 1 byte -> error
Things to know
- Status: Stagnant. It is one piece of the EOF package; the bundle is tracked in the meta EIP-7692. Later EOF EIPs such as EIP-5450 (stack validation) and EIP-4750 (functions) add further deploy-time checks.
- Validation only happens at creation, so a deployed EOF contract never needs the run-time checks legacy code does.
- Legacy contracts are never re-validated. The
0xEFprefix reserved by EIP-3541 is what lets the EVM tell EOF code apart. - Developers normally do not run this check themselves; compilers that target EOF emit valid code, and clients reject anything else at deploy time.