EIP-3670: EOF - Code Validation
Validate EOF bytecode for correctness at the time of deployment.
Abstract
Introduce code validation at contract creation time for EOF formatted (EIP-3540)
contracts. Reject contracts which contain truncated PUSH
-data or undefined instructions.
Legacy bytecode (code which is not EOF formatted) is unaffected by this change.
Motivation
Currently existing contracts require no validation of correctness and EVM implementations can decide how they handle truncated bytecode or undefined instructions. This change aims to bring code validity into consensus, so that it becomes easier to reason about bytecode. Moreover, EVM implementations may require fewer paths to decide which instruction is valid in the current execution context.
If there's a desire to introduce new instructions without bumping the EOF version, having undefined instructions already deployed could potentially break such contracts, as some instructions might change their behavior. Rejecting to deploy undefined instructions allows introducing new instructions with or without bumping the EOF version.
EOF1 forward compatibility
The EOF1 format provides following forward compatibility properties:
- New instructions can be defined for previously unassigned opcodes. These instructions may have immediate values.
- Mandatory EOF sections may be made optional.
- New optional EOF sections may be introduced. They can be placed in any order in relation to previously defined sections.
Specification
This feature is introduced on the same block EIP-3540 is enabled, therefore every EOF1-compatible bytecode MUST be validated according to these rules.
- Previously deprecated instructions
CALLCODE
(0xf2) andSELFDESTRUCT
(0xff), as well as instructions deprecated in EIP-3540, are invalid and their opcodes are undefined. (NOTE there are more instructions deprecated and rejected in EOF, as specced out by separate EIPs) - At contract creation time code validation is performed on each code section of the EOF container. The code is invalid if any of the checks below fails. For each instruction:
- Check if the opcode is defined. The
INVALID
(0xfe) is considered defined. - Check if all instructions' immediate bytes are present in the code (code does not end in the middle of instruction).
- Check if the opcode is defined. The
Rationale
Immediate data
Allowing implicit zero immediate data for PUSH
instructions introduces inefficiencies to EVM implementations without any practical use-case (the value of a PUSH
instruction at the code end cannot be observed by EVM). This EIP requires all immediate bytes to be explicitly present in the code.
Rejection of deprecated instructions
The deprecated instructions CALLCODE
(0xf2) and SELFDESTRUCT
(0xff) are removed from the valid_opcodes
list to prevent their use in the future.
Backwards Compatibility
This change poses no risk to backwards compatibility, as it is introduced at the same time EIP-3540 is. The validation does not cover legacy bytecode (code which is not EOF formatted).
Test Cases
Contract creation
Each case should be tested by submitting an EOF container to EOF contract creation (as specced out in a separate EIP). Each case should be tested with code placed in code sections at different indices.
Valid codes
- EOF code containing
INVALID
- EOF code with data section containing bytes that are undefined instructions
Invalid codes
- EOF code containing an undefined instruction
- EOF code ending with incomplete
PUSH
instruction
Reference Implementation
Security Considerations
See Security Considerations of EIP-3540.
Copyright
Copyright and related rights waived via CC0.