EIP-7480 EOF Data Section Access Opcodes (EOF - Data section access instructions)
No call decision · EVM & execution · EIP status Stagnant · created 2023-08-11
Introduces four new opcodes, DATALOAD, DATALOADN, DATASIZE, and DATACOPY, for reading the data section of an EOF1 container.
Why it is needed
EOF1 clearly separates executed code from other data within a container. The data section can hold various kinds of information, such as metadata left by a compiler, but until now there was no way to read it from within a smart contract. The legacy EVM opcodes CODECOPY and CODESIZE were meant for copying the code itself and are outright banned in EOF code, so they cannot be used to read the data section. To fill this gap, new read opcodes dedicated to the data section were needed, following the same pattern as the existing opcodes for reading calldata or returndata.
What changes
Today
- EOF1 containers separate code and data sections, but there is no dedicated opcode for reading the data section
- The legacy CODECOPY and CODESIZE are banned in EOF code and cannot be used to access the data section
- Calling EXTCODECOPY against an EOF container does not copy the code
After EIP-7480
- DATALOAD and DATALOADN can push a 32-byte value from the data section onto the stack
- DATASIZE can query the data section size directly
- DATACOPY can copy part of the data section into memory
- Out-of-range accesses return zero padding instead of an exceptional halt
Who is affected
- EOF smart contract developers and compilers: Metadata and similar data can be placed in the data section and read at runtime
- Client implementation teams: The gas costs of the new opcodes and the code validation rules related to DATALOADN must be reflected in the EOF validation logic
- Users who previously operated data-only contracts via EXTCODECOPY: EOF has no EXTDATACOPY for the same purpose, so this is not supported for now
In depth
Overview of the four opcodes
The four opcodes DATALOAD (0xd0), DATALOADN (0xd1), DATASIZE (0xd2), and DATACOPY (0xd3) are introduced at the same block number at which EIP-3540 is activated. If the code is legacy bytecode, all four opcodes cause an exceptional halt, which is no different from existing behavior. Each opcode actually behaves according to the rules below only when the code is valid EOF1.
DATALOAD
Pops one offset value from the stack, reads the [offset:offset+32] range of the data section, and pushes it onto the stack as a 32-byte value. If offset+32 exceeds the data section size, the excess is padded with zeros. Consumes 4 gas.
DATALOADN
Takes offset as a 16-bit big-endian immediate argument and pops nothing from the stack. Reads the [offset:offset+32] range of the data section and pushes it onto the stack. Because code validation guarantees in advance that this offset+32 range falls within the data section, no runtime check is needed, so it consumes a cheaper 3 gas compared to DATALOAD.
DATASIZE
Pops nothing from the stack and pushes the data section size of the currently executing container onto the stack. Consumes 2 gas.
DATACOPY
Pops three values, mem_offset, offset, and size, from the stack. Expands memory up to mem_offset+size, consuming the corresponding memory expansion gas, then consumes an additional 3 + 3 * ((size + 31) // 32) gas for the copy itself. Reads the [offset:offset+size] range of the data section and writes it starting at the mem_offset position in memory; if offset+size exceeds the data section size, the excess is padded with zeros.
Extended code validation rules
Two rules are added to the code section validation rules of EIP-3670. First, if DATALOADN's immediate offset argument plus 32 exceeds the data section size recorded in the container header before deployment, the code section is invalid. Second, if a relative jump offset of RJUMP, RJUMPI, or RJUMPV points to the 2 bytes immediately following a DATALOADN opcode (i.e., inside its immediate argument), the code section is invalid.
Design choices
The choice to pad out-of-range accesses with zeros instead of causing an exceptional halt follows the convention of existing calldata-style opcodes other than returndata copying, and it allows compilers to safely apply optimizations that remove unused copy code afterward. Introducing an EXTDATACOPY counterpart to EXTCODECOPY was also considered, but it was excluded from this EIP to limit the scope of changes. It is explicitly noted that EXTDATACOPY could be introduced and revived in a future upgrade if needed.
Compatibility and current status
These four opcodes are introduced only for EOF1 contracts, and since EOF1 prohibits deploying undefined opcodes, no existing contract could already be using them, so there is no backwards compatibility issue. These opcodes are not introduced into legacy bytecode. However, this EIP's status is Stagnant, so its inclusion in a fork is not yet confirmed, and its content may change or it may not be adopted.