Halt execution, revert state changes, and return error data
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode:0xfdIntroduced: Byzantium (EIP-140)REVERT halts execution, reverts all state changes in the current execution context, and returns error data to the caller. Unlike pre-Byzantium failures, REVERT refunds remaining gas and provides error information.This enables graceful failure handling with gas efficiency and informative error messages.
Specification
Stack Input:
offset (top) - Memory offset of error datalength - Length of error data in bytes
1. Pop offset and length from stack2. Charge gas for memory expansion3. Copy memory[offset:offset+length] to output4. Revert all state changes in current context5. Set reverted = true6. Return to caller with failure status7. Refund remaining gas
Behavior
REVERT terminates execution with error:
Pops offset from stack (top)
Pops length from stack (second)
Validates offset and length fit in u32
Charges gas for memory expansion to offset+length
Copies length bytes from memory[offset] to output buffer
Reverts all state changes (storage, logs, balance transfers)
Sets execution state to reverted
Returns control to caller with failure status
Refunds remaining gas to transaction
State Effects:
All state changes reverted in current call context
Error data available to caller
Remaining gas refunded (not consumed like INVALID)
// State changes are revertedconst frame = createFrame({ stack: [0n, 0n], storage: new Map([[0n, 42n]]), evm: { hardfork: Hardfork.BYZANTIUM }});// Make state changeframe.storage.set(1n, 123n);handler_0xfd_REVERT(frame);// State changes reverted by EVM executor// storage[1] not persisted
Common Usage
Input Validation
function withdraw(uint256 amount) external { require(amount > 0, "Amount must be positive"); require(balances[msg.sender] >= amount, "Insufficient balance"); balances[msg.sender] -= amount; payable(msg.sender).transfer(amount);}
Use for: validation, business logic, access control
INVALID (0xfe):
Consumes all gas
No error data
Hard failure
Use for: should-never-happen, invariant violations
Example:
// GOOD: Use REVERT for expected failuresfunction withdraw(uint256 amount) external { require(amount <= balance, "Insufficient balance"); // REVERT // ...}// GOOD: Use INVALID for invariant violationsfunction criticalOperation() internal { assert(invariant); // INVALID if false (should never happen) // ...}
Gas Refund Implications
REVERT refunds gas - important for:Nested calls:
contract Parent { function callChild(address child) external { // Provide gas stipend (bool success, ) = child.call{gas: 10000}(""); if (!success) { // Child may have reverted - gas refunded // Remaining gas available for error handling } }}
Gas griefing prevention:
// SAFE: REVERT refunds gasfunction safeOperation() external { require(condition, "Failed"); // REVERT // Caller gets unused gas back}// DANGEROUS: INVALID consumes all gasfunction dangerousOperation() external { assert(condition); // INVALID - consumes all gas // Caller loses all provided gas}
Error Data Validation
Caller must validate error data:
// VULNERABLE: Trusts error data sizefunction unsafeCall(address target) external { (bool success, bytes memory data) = target.call(""); if (!success) { // data could be arbitrarily large string memory error = abi.decode(data, (string)); emit Error(error); // Could run out of gas }}
Safe pattern:
function safeCall(address target) external { (bool success, bytes memory data) = target.call(""); if (!success) { // Validate size before decoding require(data.length <= 1024, "Error too large"); if (data.length >= 4) { bytes4 errorSig = bytes4(data); // Handle specific errors } }}
State Reversion Scope
REVERT only reverts current call context:
contract Parent { uint256 public value; function callChild(address child) external { value = 1; // State change in Parent try child.doSomething() { // Success } catch { // Child reverted, but Parent's state change persists } // value = 1 (Parent state not reverted) }}contract Child { function doSomething() external { revert("Failed"); // Only reverts Child's state }}
Reentrancy
REVERT doesn’t prevent reentrancy:
// VULNERABLE: Reentrancy still possiblefunction withdraw() external { uint256 balance = balances[msg.sender]; // External call before state update (bool success, ) = msg.sender.call{value: balance}(""); // REVERT doesn't prevent reentrancy if call succeeds require(success, "Transfer failed"); balances[msg.sender] = 0; // Too late}
Safe: Update state before external calls (Checks-Effects-Interactions).