EIP-3670 EOF code validation (EOF - Code Validation)
No call decision · EVM & execution · EIP status Stagnant · created 2021-06-23
Checks contract code deployed in EOF (EIP-3540) format at creation time, and rejects the deployment itself if it contains undefined instructions or truncated PUSH data. Legacy code is unaffected.
Why it is needed
Currently the EVM performs no validation at all on the correctness of deployed code. Code containing bytes not defined as opcodes, or PUSH instructions truncated at the end with insufficient data, can still be deployed as is, leaving room for different client implementations to handle such code differently. Also, if a contract using a currently undefined opcode is already deployed, that contract's behavior could break later when a new instruction is assigned to that opcode. EIP-3670 mandates creation-time validation for code deployed in the EOF1 format (EIP-3540), blocking the deployment of code with undefined opcodes and truncated PUSH data. This makes code validity part of the consensus rules, opening a path to safely assign new instructions to opcodes without bumping the EOF version.
What changes
Today
- Undefined opcodes can also be deployed, so handling may differ across clients
- Even if a PUSH instruction is truncated at the end of the code, it is deployed with implicit zero-padding
- Deprecated instructions such as CALLCODE and SELFDESTRUCT remain usable in legacy code
After EIP-3670
- EOF1 code undergoes validation at creation time, and deployment is rejected if it contains an undefined opcode
- All immediate bytes of a PUSH instruction must be explicitly present in the code; if truncated, deployment is rejected
- CALLCODE, SELFDESTRUCT, and others are treated as having undefined opcodes themselves and cannot be used in EOF code
- Legacy code (non-EOF code) is not subject to this validation and is entirely unaffected
Who is affected
- Smart contract developers: To deploy as EOF, the code must contain only opcodes that are defined and PUSH data that is complete to the end.
- Compiler developers: Compilers targeting EOF must ensure they do not produce invalid bytecode.
- Client and EVM implementation teams: Implementation is simplified because fewer cases need to be handled for undefined opcodes or truncated code during execution.
In depth
Activation timing and scope
This validation is introduced together in the same block in which EIP-3540 activates, so creation-time validation applies to all EOF1-compatible bytecode. The validation target is each code section within an EOF container; legacy bytecode is not a target.
Two validation rules
For each instruction in a code section, two things are checked. First, whether the opcode is defined. INVALID (0xfe) is treated as a defined opcode. Second, whether all of an instruction's immediate bytes are fully contained in the code, i.e., that the code does not end in the middle of an instruction. If either condition is violated, the code is treated as invalid via a ValidationException.
Rejection of deprecated instructions
CALLCODE (0xf2) and SELFDESTRUCT (0xff) are previously deprecated instructions, and together with other instructions deprecated in this EIP and EIP-3540, they are excluded from the valid_opcodes list, treating the opcodes themselves as undefined. This is a measure to prevent these instructions from being usable in EOF code going forward.
Why immediate data is not implicitly padded
If a PUSH instruction truncated at the end of the code were allowed to execute by implicitly zero-filling the missing bytes, it would add inefficiency to EVM implementations without any real benefit (since a PUSH value at the end of the code can never be observed anyway). So this EIP requires that all immediate bytes of every PUSH instruction be explicitly present in the code.
BLOCKHASH remains valid as an exception
The BLOCKHASH instruction can be replaced by the system contract introduced by EIP-2935, but it has not been officially deprecated. So to avoid creating a difference from legacy code, this opcode remains a valid instruction in EOF as well.
Three forward-compatibility guarantees of EOF1
The EOF1 format has three forms of forward compatibility. New instructions, possibly with immediate values, can be defined for previously unassigned opcodes; EOF sections that are currently mandatory can later be made optional; and new optional sections can be added without ordering constraints relative to existing sections. This EIP's mandatory code validation is the precondition that makes these extensions safe.
Backward compatibility and current status
This change does not target legacy bytecode (non-EOF-format code) for validation and is introduced at the same time as EIP-3540, so there is no backwards compatibility risk. However, this EIP's status is marked Stagnant, indicating it is not an actively progressing proposal.