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: 0x57
Introduced: Frontier (EVM genesis)
JUMPI performs a conditional jump to a destination if a condition is non-zero. Like JUMP, the destination MUST be a valid JUMPDEST opcode. If the condition is zero, execution continues sequentially.
This enables if-statements, loops, and conditional control flow in EVM bytecode.
Specification
Stack Input:
Stack Output: None
Gas Cost: 10 (GasSlowStep)
Operation:
Behavior
JUMPI conditionally alters program flow:
- Consumes 10 gas (GasSlowStep)
- Pops destination address from stack (top)
- Pops condition value from stack (second)
- If condition is non-zero (any value except 0):
- Validates destination is valid JUMPDEST
- Updates PC to destination
- If condition is zero:
- Increments PC by 1 (continue sequentially)
Validation (when jumping):
- Destination must be JUMPDEST opcode (0x5b)
- Destination must not be inside PUSH data
- Destination must be within bytecode bounds
- Failure causes InvalidJump error
Note: Validation only occurs if jump is taken. If condition is zero, destination is not validated.
Examples
Basic Conditional Jump
Conditional Not Taken
Loop Pattern
Bytecode pattern:
If-Else Pattern
Compiled to:
Non-Zero Condition
Gas Cost
Cost: 10 gas (GasSlowStep)
Comparison:
- JUMP: 8 gas (unconditional)
- JUMPI: 10 gas (conditional)
- PC: 2 gas
- JUMPDEST: 1 gas
Total Conditional Jump:
Cost is same whether jump is taken or not:
- Jump taken: 10 gas + JUMPDEST (1 gas) = 11 gas
- Jump not taken: 10 gas
Edge Cases
Invalid Destination When Jumped
Invalid Destination Not Validated
Zero is False
Stack Underflow
Out of Bounds
Common Usage
Require Statements
Solidity require compiles to JUMPI:
Compiled (simplified):
For Loops
Compiled to:
While Loops
Compiled to:
Switch Statements
Compiled to:
Implementation
Testing
Test Coverage
Security
Conditional Validation Bypass
Pattern to avoid:
Attack:
- Set condition = 0
- Invalid destination not validated
- If destination is later used, could cause issues
Not exploitable in practice:
- Invalid destinations only checked when jumped
- No jump means no execution at destination
- Still poor practice - validate explicitly
Infinite Loops
JUMPI enables infinite loops that exhaust gas:
Gas protection:
- Out-of-gas halts execution
- Only affects transaction sender
- Transaction reverts, state unchanged
Condition Manipulation
VULNERABLE:
Attack:
- User sets userCondition = 1
- Jumps to privileged code
- Bypasses access control
SAFE:
Short-Circuit Evaluation
Solidity’s && and || compile to JUMPI for short-circuiting:
Compiled to:
This prevents evaluating condition2 if condition1 is false, saving gas and avoiding side effects.
Compiler Behavior
Require Statements
Compiles to:
If Statements
Compiles to:
References