EIP-3540 - EOF - EVM Object Format v1

Created 2021-03-16
Status Review
Category Core
Type Standards Track
Authors
Requires

Abstract

We introduce an extensible and versioned container format for the EVM with a once-off validation at deploy time. The version described here brings the tangible benefit of code and data separation, and allows for easy introduction of a variety of changes in the future. This change relies on the reserved byte introduced by EIP-3541.

To summarise, EOF bytecode has the following layout:

magic, version, (section_kind, section_size_or_sizes)+, 0, <section contents>

Motivation

On-chain deployed EVM bytecode contains no pre-defined structure today. Code is typically validated in clients to the extent of JUMPDEST analysis at runtime, every single time prior to execution. This poses not only an overhead, but also a challenge for introducing new or deprecating existing features.

Validating code during the contract creation process allows code versioning without an additional version field in the account. Versioning is a useful tool for introducing or deprecating features, especially for larger changes (such as significant changes to control flow, or features like account abstraction).

The format described in this EIP introduces a simple and extensible container with a minimal set of changes required to both clients and languages, and introduces validation.

The first tangible feature it provides is separation of code and data. This separation is especially beneficial for on-chain code validators (like those utilised by layer-2 scaling tools, such as Optimism), because they can distinguish code and data (this includes deployment code and constructor arguments too). Currently, they a) require changes prior to contract deployment; b) implement a fragile method; or c) implement an expensive and restrictive jump analysis. Code and data separation can result in ease of use and significant gas savings for such use cases. Additionally, various (static) analysis tools can also benefit, though off-chain tools can already deal with existing code, so the impact is smaller.

A non-exhaustive list of proposed changes which could benefit from this format:

Specification

The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “NOT RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119 and RFC 8174.

In order to guarantee that every EOF-formatted contract in the state is valid, we need to prevent already deployed (and not validated) contracts from being recognized as such format. This is achieved by choosing a byte sequence for the magic that doesn't exist in any of the already deployed contracts.

Remarks

If code starts with the MAGIC, it is considered to be EOF formatted, otherwise it is considered to be legacy code. For clarity, the MAGIC together with a version number n is denoted as the EOFn prefix, e.g. EOF1 prefix.

EOF-formatted contracts are created using new instructions which are introduced in a separate EIP.

The opcode 0xEF is currently an undefined instruction, therefore: It pops no stack items and pushes no stack items, and it causes an exceptional abort when executed. This means legacy initcode or already deployed legacy code starting with this instruction will continue to abort execution.

Unless otherwised specified, all integers are encoded in big-endian byte order.

Code validation

We introduce code validation for new contract creation. To achieve this, we define a format called EVM Object Format (EOF), containing a version indicator, and a ruleset of validity tied to a given version.

Legacy code is not affected by EOF code validation.

Code validation is performed during contract creation, and is elaborated on in separate EIPs. The EOF format itself and its formal validation are described in the following sections.

Container specification

EOF container is a binary format with the capability of providing the EOF version number and a list of EOF sections.

The container starts with the EOF prefix:

description length value
magic 2-bytes 0xEF00
version 1-byte 0x01–0xFF EOF version number

The EOF prefix is followed by at least one section header. Each section header contains two fields, section_kind and either section_size or section_size_list, depending on the kind. section_size_list is a list of size values when multiple sections of this kind are allowed, encoded as a count of items followed by the items.

description length value
section_kind 1-byte 0x01–0xFF uint8
section_size 2-bytes 0x0000–0xFFFF uint16
section_size_list dynamic n/a uint16, uint16+

The list of section headers is terminated with the section headers terminator byte 0x00. The body content follows immediately after.

Container validation rules

  1. version MUST NOT be 0.
  2. section_kind MUST NOT be 0. The value 0 is reserved for section headers terminator byte.
  3. There MUST be at least one section (and therefore section header).
  4. Stray bytes outside of sections MUST NOT be present. This includes trailing bytes after the last section.

EOF version 1

EOF version 1 is made up of several EIPs, including this one. Some values in this specification are only discussed briefly. To understand the full scope of EOF, it is necessary to review each EIP in-depth.

Container

The EOF version 1 container consists of a header and body.

container := header, body
header := 
    magic, version, 
    kind_type, type_size, 
    kind_code, num_code_sections, code_size+,
    [kind_container, num_container_sections, container_size+,]
    kind_data, data_size,
    terminator
body := types_section, code_section+, container_section*, data_section
types_section := (inputs, outputs, max_stack_height)+

note: , is a concatenation operator, + should be interpreted as "one or more" of the preceding item, * should be interpreted as "zero or more" of the preceding item, and [item] should be interpreted as an optional item.

Header

name length value description
magic 2 bytes 0xEF00
version 1 byte 0x01 EOF version
kind_type 1 byte 0x01 kind marker for type section
type_size 2 bytes 0x0004-0x1000 16-bit unsigned big-endian integer denoting the length of the type section content, 4 bytes per code section
kind_code 1 byte 0x02 kind marker for code size section
num_code_sections 2 bytes 0x0001-0x0400 16-bit unsigned big-endian integer denoting the number of the code sections
code_size 2 bytes 0x0001-0xFFFF 16-bit unsigned big-endian integer denoting the length of the code section content
kind_container 1 byte 0x03 kind marker for container size section
num_container_sections 2 bytes 0x0001-0x0100 16-bit unsigned big-endian integer denoting the number of the container sections
container_size 2 bytes 0x0001-0xFFFF 16-bit unsigned big-endian integer denoting the length of the container section content
kind_data 1 byte 0x04 kind marker for data size section
data_size 2 bytes 0x0000-0xFFFF 16-bit unsigned big-endian integer denoting the length of the data section content (*)
terminator 1 byte 0x00 marks the end of the header

(*) For not yet deployed containers this can be greater than the actual content length.

Body

name length value description
types_section variable n/a stores code section metadata
inputs 1 byte 0x00-0x7F number of stack elements the code section consumes
outputs 1 byte 0x00-0x7F number of stack elements the code section returns
max_stack_height 2 bytes 0x0000-0x03FF maximum number of elements ever placed onto the operand stack by the code section
code_section variable n/a arbitrary bytecode
container_section variable n/a arbitrary EOF-formatted container
data_section variable n/a arbitrary sequence of bytes

See EIP-4750 for more information on the type section content.

NOTE: A special value of outputs being 0x80 is designated to denote non-returning functions as defined in a separate EIP.

EOF version 1 validation rules

The following validity constraints are placed on the container format:

Changes to execution semantics

For an EOF contract:

For a legacy contract:

NOTE Like for legacy targets, the aforementioned behavior of EXTCODECOPY, EXTCODEHASH and EXTCODESIZE does not apply to EOF contract targets mid-creation, i.e. those report same as accounts without code.

Rationale

EVM and/or account versioning has been discussed numerous times over the past years. This proposal aims to learn from them. See "Ethereum account versioning" on the Fellowship of Ethereum Magicians Forum for a good starting point.

Execution vs. creation time validation

This specification introduces creation time validation, which means:

The alternative is to have execution time validation for EOF. This is performed every single time a contract is executed, however clients may be able to cache validation results. This alternative approach has the following properties:

The MAGIC

  1. The first byte 0xEF was chosen because it is reserved for this purpose by EIP-3541.

  2. The second byte 0x00 was chosen to avoid clashes with three contracts which were deployed on Mainnet:

  3. 0xca7bf67ab492b49806e24b6e2e4ec105183caa01: EFF09f918bf09f9fa9
  4. 0x897da0f23ccc5e939ec7a53032c5e80fd1a947ec: EF
  5. 0x6e51d4d9be52b623a3d3a2fa8d3c5e3e01175cd0: EF

  6. No contracts starting with 0xEF bytes exist on public testnets: Goerli, Ropsten, Rinkeby, Kovan and Sepolia at their London fork block.

NOTE: This EIP MUST NOT be enabled on chains which contain bytecodes starting with MAGIC and not being valid EOF.

EOF version range start with 1

The version number 0 will never be used in EOF, so we can call legacy code EOF0. Also, implementations may use APIs where 0 version number denotes legacy code.

Section structure

We have considered different questions for the sections:

Data-only contracts

See section Lack of EXTDATACOPY in EIP-7480.

EOF1 contracts can only DELEGATECALL EOF1 contracts

Currently contracts can selfdestruct in three different ways (directly through SELFDESTRUCT, indirectly through CALLCODE and indirectly through DELEGATECALL). EIP-3670 disables the first two possibilities, however the third possibility remains. Allowing EOF1 contracts to only DELEGATECALL other EOF1 contracts allows the following strong statement: EOF1 contract can never be destructed. Attacks based on SELFDESTRUCT completely disappear for EOF1 contracts. These include destructed library contracts (e.g. Parity Multisig).

EOF1 containers have a size limit

Imposing an EOF-validation time limit for the size of EOF containers provides a reference limit of how large the containers should EVM implementations be able to handle when validating and processing containers. MAX_INITCODE_SIZE was chosen for EOF1, as it is what contract creation currently allows for.

Given one of the main reasons for the limit is to avoid attack vectors on JUMPDEST-analysis, and EOF removes the need for JUMPDEST-analysis and introduces a cost structure for deploy-time analysis, in the future this limit could be increased or even lifted for EOF.

Backwards Compatibility

This is a breaking change given that any code starting with 0xEF was not deployable before (and resulted in exceptional abort if executed), but now some subset of such codes can be deployed and executed successfully.

The choice of MAGIC guarantees that none of the contracts existing on the chain are affected by the new rules.

Security Considerations

With the anticipated EOF extensions, the validation is expected to have linear computational and space complexity. We think that the validation cost is sufficiently covered by:

Copyright

Copyright and related rights waived via CC0.