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.
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.
The EOF1 format provides following forward compatibility properties:
This feature is introduced on the same block EIP-3540 is enabled, therefore every EOF1-compatible bytecode MUST be validated according to these rules.
CALLCODE
(0xf2) and SELFDESTRUCT
(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)INVALID
(0xfe) is considered defined.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.
The deprecated instructions CALLCODE
(0xf2) and SELFDESTRUCT
(0xff) are removed from the valid_opcodes
list to prevent their use in the future.
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).
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.
INVALID
PUSH
instruction# The ranges below are as specified by Execution Specs for Shanghai.
# Note: range(s, e) excludes e, hence the +1
shanghai_opcodes = [
*range(0x00, 0x0b + 1),
*range(0x10, 0x1d + 1),
0x20,
*range(0x30, 0x3f + 1),
*range(0x40, 0x48 + 1),
*range(0x50, 0x5b + 1),
0x5f,
*range(0x60, 0x6f + 1),
*range(0x70, 0x7f + 1),
*range(0x80, 0x8f + 1),
*range(0x90, 0x9f + 1),
*range(0xa0, 0xa4 + 1),
# Note: 0xfe is considered assigned.
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xfa, 0xfd, 0xfe, 0xff
]
# Drop the opcodes deprecated and rejected in here and in EIP-3540
rejected_in_eof = [
0x38, 0x39, 0x3b, 0x3c, 0x3f, 0x5a, 0xf1, 0xf2, 0xf4, 0xfa, 0xff
]
valid_opcodes = [op for op in shanghai_opcodes not in rejected_in_eof]
immediate_sizes = 256 * [0]
immediate_sizes[0x60:0x7f + 1] = range(1, 32 + 1) # PUSH1..PUSH32
# Raises ValidationException on invalid code
def validate_instructions(code: bytes):
# Note that EOF1 already asserts this with the code section requirements
assert len(code) > 0
pos = 0
while pos < len(code):
# Ensure the opcode is valid
opcode = code[pos]
if opcode not in valid_opcodes:
raise ValidationException("undefined opcode")
# Skip immediate data
pos += 1 + immediate_sizes[opcode]
# Ensure last instruction's immediate doesn't go over code end
if pos != len(code):
raise ValidationException("truncated immediate")
See Security Considerations of EIP-3540.
Copyright and related rights waived via CC0.