EIP-141 · Final · Core · 2017-02-09
EIP-141: Designated invalid EVM instruction
Reserves opcode 0xfe as the permanent INVALID instruction, giving compilers a deliberate way to abort execution that no future upgrade will ever reuse.
What problem does it solve?
Any byte that is not a defined opcode makes the EVM stop with an exceptional halt. Compilers took advantage of this: to crash on purpose, they emitted an unused byte. The catch is that "unused" could change. A later hard fork might give that byte a meaning, and old contracts that relied on it to abort would suddenly behave differently.
EIP-141 removes that risk. It picks one byte and promises that it will always stay invalid, so "abort here" has a stable, explicit encoding.
How it works
The specification is one line: opcode 0xfe is the INVALID instruction.
- Executing
0xfecauses an exceptional halt, just like any other undefined opcode. - As with every exceptional halt, the current call frame's state changes are rolled back and all remaining gas in that frame is consumed.
- The caller sees the call as failed, with no return data.
- No hard fork will ever assign
0xfea different meaning.
The spec notes that nobody had used this byte before, so the designation changed nothing for existing contracts. It simply turned an accident of the opcode table into a guarantee.
Sample code
Raw bytecode that immediately aborts:
0xfe // INVALID: exceptional halt, all gas consumed
Emitting it from inline assembly in Solidity:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Abort {
function boom() external pure {
assembly {
invalid() // compiles to 0xfe
}
}
}
Compare this with revert(), which also undoes state changes but refunds unused gas and can return an error message:
function softFail() external pure {
revert("nope"); // REVERT (0xfd), unused gas is returned
}
Things to know
- Before Solidity 0.8.0, a failed
assertcompiled to0xfe, so it burned all remaining gas. Since 0.8.0,assertusesREVERTwith aPanic(uint256)error instead. - EIP-140
REVERTis almost always the better choice for intentional failures. It refunds unused gas and can carry an error reason. - The spec notes that
INVALIDcan serve as anABORTinstruction. That is its whole purpose: it gives a halt a distinct, intentional cause. INVALIDis treated like any other undefined byte. The difference is that0xfeis guaranteed to stay undefined.