EIP-3690: EOF - JUMPDEST Table
A special EOF section for storing the list of JUMPDESTs, which simplifies execution time analysis.
Abstract
Introduce a section in the EOF format (EIP-3540) for storing the list of JUMPDEST
s, validate the correctness of this list at the time of contract creation, and remove the need for JUMPDEST
-analysis at execution time. In EOF contracts, the JUMPDEST
instruction is not needed anymore and becomes invalid. Legacy contracts are entirely unaffected by this change.
Motivation
Currently existing contracts require no validation of correctness, but every time they are executed, a list must be built containing all the valid jump-destinations. This is an overhead which can be avoided, albeit the effect of the overhead depends on the client implementation.
With the structure provided by EIP-3540 it is easy to store and transmit a table of valid jump-destinations instead of using designated JUMPDEST
(0x5b) opcodes in the code.
The goal of this change is that we trade less complexity (and processing time) at execution time for more complexity at contract creation time. Through benchmarks we have identified that the mandatory execution preparation time is the same as before for extreme cases (i.e. deliberate edge cases), while it is ~10x faster for the average case.
Finally, this change puts an implicit bound on "initcode analysis" which is now limited to jumpdests section loading of max size of 0xffff. The legacy code remains vulnerable.
Specification
This feature is introduced on the very same block EIP-3540 is enabled, therefore every EOF1-compatible bytecode MUST have a JUMPDEST-table if it uses jumps.
Remark: We rely on the notation of initcode, code and creation as defined by EIP-3540, and extend validation rules of EIP-3670.
EOF container changes
-
A new EOF section called
jumpdests
(section_kind = 3
) is introduced. It contains a sequence of n unsigned integers jumploci. -
The jumploci values are encoded with unsigned LEB128.
description encoding jumploc0 unsigned LEB128 jumploc1 unsigned LEB128 ... jumplocn unsigned LEB128 -
The jump destinations represent the set of valid code positions as arguments to jump instructions. They are delta-encoded therefore partial sum must be performed to retrieve the absolute offsets.
Validation rules
This section extends contract creation validation rules (as defined in EIP-3540).
- The
jumpdests
section MUST be present if and only if thecode
section containsJUMP
orJUMPI
opcodes. - If the
jumpdests
section is present it MUST directly precede thecode
section. In this case a valid EOF bytecode will have the form offormat, magic, version, [jumpdests_section_header], code_section_header, [data_section_header], 0, [jumpdests_section_contents], code_section_contents, [data_section_contents]
. - The LEB128 encoding of a
jumploc
must be valid: the encoding must be complete and not read out ofjumpdests
section. As an additional constraint, the shortest possible encoding must be used. - With an exception of the first entry, the value of
jumploc
MUST NOT be 0. - Every
jumploc
MUST point to a valid opcode. They MUST NOT point into PUSH-data or outside of the code section. - The
JUMPDEST
(0x5b) instruction becomes undefined (Note: According to rules of EIP-3670, deploying the code will fail if it containsJUMPDEST
)
Execution
- When executing
JUMP
orJUMPI
instructions, the jump destination MUST be in thejumpdests
table. Otherwise, the execution aborts with bad jump destination. In case ofJUMPI
, the check is done only when the jump is to be taken (no change to the previous behaviour).
Rationale
Jumpdests section is bounded
The length of the jumpdests
section is bounded by the EOF maximum section size value 0xffff. Moreover, for deployed code this additionally limited by the max bytecode size 0x6000. Then any valid jumpdests
section may not be more larger than 0x3000.
Delta encoding
Delta-encoding is very efficient for this job. From a quick analysis of a small set of contracts JUMPDEST
opcodes are relatively close to each other. In the delta-encoding the values almost never exceed 128. Combined with any form of variable-length quantity (VLQ) where values < 128 occupy one byte, encoding of single jumpdest takes ~1 byte. We also remove JUMPDEST
opcodes from the code section therefore the total bytecode length remains the same if extreme examples are ignored.
By extreme examples we mean contracts having a distance between two subsequent JUMPDESTs larger than 128. Then the LEB128 encoding of such distance requires more than one byte and the total bytecode size will increase by the additional number of bytes used.
LEB128 for offsets
The LEB128 encoding is the most popular VLQ used in DWARF and WebAssembly.
LEB128 allows encoding a fixed value with arbitrary number of bytes by having zero payloads for most significant bits of the value. To ensure there exists only single encoding of a given value, we additionally require the shortest possible LEB128 encoding to be used. This constraint is also required by WebAssembly.
Size-prefix for offsets
This is another option for encoding inspired by UTF-8. The benefit is that the number of following bytes is encoded in the first byte (the top two bits), so the expected length is known upfront.
A simple decoder is the following:
Empty table
In case code does not use jumps, an empty JUMPDEST table is represented by omitting jumpdests
section as opposed to a section that is always present, but allowed to be empty. This is consistent with the requirement of EIP-3540 for section size to be non-zero. Additionally, omitting the section saves 3 bytes of code storage.
Why jumpdests before code?
The contents of jumpdests
section are always needed to start EVM execution. For chunked and/or merkleized bytecode it is more efficient to have jumpdests
just after the EOF header so they can share the same first chunk(s).
Code chunking / merkleization
In code chunking the contract code is split into (fixed size) chunks. Due to the requirement of jumpdest-analysis, it must be known where the first instruction starts in a given chunk, in case the split happened within a PUSH-data. This is commonly accomplished with reserving the first byte of the chunk as the "first instruction offset" (FIO) field.
With this EIP, code chunking does not need to have such a field. However, the jumpdest table must be provided instead (for all the chunks up until the last jump location used during execution).
Benchmarks / performance analysis
We compared the performance of jumpdests
section loading to JUMPDEST analysis in evmone/Baseline interpreter. In both cases a bitset of valid jumpdest positions is built.
We used the worst case for jumpdests
section as the benchmark baseline. This is the case where every position in the code section is valid jumpdest. I.e. the bytecode has as many jumpdests as possible making the jumpdests section as large as possible. The encoded representation is 0x00, 0x01, 0x01, 0x01, ...
.
This also happen to be the worst case for the JUMPDEST analysis.
Further, we picked 5 popular contracts from the Ethereum mainnet.
case | size | num JUMPDESTs | JUMPDEST analysis (cycles/byte) | jumpdests load (cycles/byte) | performance change |
---|---|---|---|---|---|
worst | 65535 | 65535 | 9.11 | 9.36 | 2.75% |
RoninBridge | 1760 | 71 | 3.57 | -89.41% | |
UniswapV2ERC20 | 2319 | 61 | 2.10 | -88.28% | |
DepositContract | 6358 | 123 | 1.86 | -90.24% | |
TetherToken | 11075 | 236 | 1.91 | -89.58% | |
UniswapV2Router02 | 21943 | 468 | 2.26 | -91.17% |
For the worst case the performance difference between JUMPDEST analysis and jumpdests section loading is very small. The performance very slow compared to memory copy (0.15 cycles/byte).
However, the maximum length for the worst cases is different. For JUMPDEST analysis this is 24576 (0x6000) for deployed contracts and only limited by EVM memory cost in case of initcode (can be over 1MB). For jumpdests sections, the limit is 12288 for deployed contracts (the deployed bytecode length limit must be split equally between jumpdests and code sections). For initcode case, the limit is 65535 because this is the maximum section size allowed by EOF.
For "popular" contracts the gained efficiency is ~10x because the jumpdests section is relatively small compared to the code section and therefore there is much less bytes to loop over than in JUMPDEST analysis.
Reference Implementation
We extend the validate_code()
function of EIP-3670:
Test Cases
Valid bytecodes
- No jumpdests
- Every byte is a jumpdest
- Distant jumpdests (0x7f and 0x3f01 bytes apart)
- Max number of jumpdests
- 1-byte offset encoding: initcode of max size (64K) with jumpdest at each byte - table contains 65536 1-byte offsets, first one is 0x00, all others equal 0x01
- 2-byte offset encoding: inicode of max size with jumpdests 0x80 (128) bytes apart - table contains 512 offsets, first one is 0x7f (127), all others equal 0x8001
- 3-byte offset encoding: inicode of max size with jumpdests 0x4000 (16384) bytes apart - table contains 4 offsets: 0xFF7F (16383), 0x808001, 0x808001, 0x808001
Invalid bytecodes
- Empty jumpdest section
- Multiple jumpdest sections
- jumpdest section after the code section
- jumpdest section after the data section
- Final jumploc in the table is truncated (not a valid LEB128)
- LEB128 encoding with extra 0s (non-minimal encoding)
- Jumpdest location pointing to PUSH data
- Jumpdest location out of code section bounds
- pointing into data section
- pointing into jumpdest section
- pointing outside container bounds
- Duplicate jumpdest locations (0 deltas in table other than 1st offset)
- Code containing
JUMP
but no jumpdest table - Code containing
JUMPI
but no jumpdest table - Code containing jumpdest table but not
JUMP
/JUMPI
- Code containing
JUMPDEST
Backwards Compatibility
This change poses no risk to backwards compatibility, as it is introduced at the same time EIP-3540 is. The requirement of a JUMPDEST table does not cover legacy bytecode.
Security Considerations
The authors are not aware of any security or DoS risks posed by this change.
Copyright
Copyright and related rights waived via CC0.