EIP-2327 · Stagnant · Core · 2019-10-28
EIP-2327: BEGINDATA opcode
Proposed a BEGINDATA opcode (0xb6) that marks the rest of a contract's bytecode as data, so it can never be jumped into or mistaken for instructions.
What problem does it solve?
Contracts routinely carry raw data inside their bytecode: constructor arguments, constants, compiler metadata, and, during deployment, the runtime code itself. The EVM has no way to tell this data apart from real instructions. Clients still scan it during JUMPDEST analysis, a stray 0x5b byte inside data is a valid jump target, and disassemblers, explorers and debuggers render the data as a wall of nonsense opcodes.
EIP-2327 proposes a simple marker: everything after the first BEGINDATA is data, not code. That makes static analysis easier, makes it impossible to jump into data, and lets tools display bytecode honestly.
How it works
- A new opcode
BEGINDATAat byte0xb6(chosen to match the earlier EIP-615 proposal). - Jump analysis stops at the first
BEGINDATA. Any jump to a position at or after it fails with a bad-jump-destination error, even if the byte there is0x5b. - Executing
BEGINDATAbehaves likeSTOPand costs 0 gas. - Data after the marker is still readable:
CODECOPYandEXTCODECOPYwork as before, andCODESIZE/EXTCODESIZEstill report the full length.
The spec's two test cases capture the whole behaviour:
- Jumping to a
0x5bbyte located afterBEGINDATAmust fail. - Reaching
BEGINDATAduring execution stops the current call frame.
The motivation also mentions off-chain uses: systems that check whether code follows a certain pattern (the spec cites Optimism) would no longer need a full JUMPDEST analysis to prove that data is never executed.
Sample code
How a client's jump-destination analysis would change (pseudocode):
BEGINDATA = 0xB6
JUMPDEST = 0x5B
def valid_jumpdests(code: bytes) -> set[int]:
dests, pc = set(), 0
while pc < len(code):
op = code[pc]
if op == BEGINDATA:
break # everything after this is data
if op == JUMPDEST:
dests.add(pc)
if 0x60 <= op <= 0x7F: # PUSH1..PUSH32: skip immediate bytes
pc += op - 0x5F
pc += 1
return dests
A contract layout using the marker (illustrative):
<runtime code ...> STOP
BEGINDATA ; 0xb6
<constants / metadata> ; readable via CODECOPY, never executable
Things to know
- Status: Stagnant. The proposal was never scheduled for a network upgrade, and
0xb6is not aBEGINDATAopcode on Ethereum mainnet. - Backwards compatibility hinges on whether any deployed contract relies on executing bytes that would sit after a
0xb6. The spec notes Solidity never generated such code, but contracts produced by other means would have needed checking. - The rationale admits that treating
BEGINDATAasSTOPis somewhat arbitrary; aborting with an out-of-gas style error was the alternative. - The broader goal of separating code from data was later pursued by the EVM Object Format proposals (EIP-3540, EIP-3670), which use explicit code and data sections instead of an in-line marker.