EVM (Ethereum Virtual Machine)
The runtime environment for executing smart contract bytecode on Ethereum and compatible blockchains.
The Ethereum Virtual Machine (EVM) is the computation engine that executes smart contract code on Ethereum and EVM-compatible chains like Polygon, Arbitrum, and BSC. Every node in the network runs an identical copy of the EVM, ensuring that all participants reach consensus on the state of smart contracts. Understanding EVM mechanics is essential for writing secure, gas-efficient smart contracts and for auditing potential vulnerabilities.
How the EVM Works
The EVM is a stack-based virtual machine that processes bytecode instructions called opcodes:
1Source Code (.sol)2 │3 ▼ Compiler (solc)4Bytecode (hex)5 │6 ▼ Deployment7EVM Execution8 │9 ▼10State Changes (storage, balances)
When you deploy a contract, the compiled bytecode is stored on-chain. When someone calls a function, the EVM:
- Loads the contract's bytecode
- Executes opcodes sequentially
- Reads/writes to storage as instructed
- Deducts gas for each operation
- Reverts all changes if execution fails
EVM Architecture
Stack
The EVM uses a 1024-element stack for computations. Each element is 256 bits (32 bytes). Operations push and pop values from the stack.
Memory
Temporary, byte-addressable storage that exists only during execution. Expands dynamically but costs gas.
Storage
Persistent key-value store (256-bit keys to 256-bit values). Extremely expensive to write (~20,000 gas for new slots).
Calldata
Read-only input data passed to a function. Cheaper than memory for external calls.
Common Opcodes
| Opcode | Gas | Description |
|---|---|---|
| ADD | 3 | Addition |
| MUL | 5 | Multiplication |
| SLOAD | 2100 | Read storage |
| SSTORE | 5000-20000 | Write storage |
| CALL | 2600+ | External call |
| DELEGATECALL | 2600+ | Execute code in caller's context |
| CREATE | 32000 | Deploy contract |
EVM Security Considerations
Reentrancy
The EVM allows external calls to execute arbitrary code before the calling function completes:
1// Vulnerable pattern2function withdraw() external {3 uint256 amount = balances[msg.sender];4 (bool success,) = msg.sender.call{value: amount}(""); // External call5 balances[msg.sender] = 0; // State update AFTER call6}
Integer Overflow (Pre-0.8.0)
Before Solidity 0.8.0, arithmetic didn't check for overflow:
1// In Solidity < 0.8.02uint8 x = 255;3x += 1; // x becomes 0, not 256!
Gas Limitations
Block gas limits constrain computation. Unbounded loops can make functions uncallable.
Storage Collisions
In proxy patterns, mismatched storage layouts between proxy and implementation cause corruption.
EVM Compatibility
"EVM-compatible" chains run the same bytecode:
| Chain | EVM Compatible | Notes |
|---|---|---|
| Polygon | Yes | Lower gas costs |
| Arbitrum | Yes | Optimistic rollup |
| BSC | Yes | Faster blocks |
| Avalanche C-Chain | Yes | Subnet architecture |
| Solana | No | Different VM (SVM) |
Code deployed on Ethereum can typically run on EVM-compatible chains with minimal changes.
EVM Execution Context
Every transaction has access to:
1msg.sender // Address that called this function2msg.value // ETH sent with the call3msg.data // Raw calldata4tx.origin // Original transaction sender (use carefully!)5block.number // Current block number6block.timestamp // Current block timestamp7gasleft() // Remaining gas
EVM Limitations
Determinism requirement: No randomness, no external API calls—everything must be reproducible by all nodes.
Gas costs: Complex computation is expensive, limiting what's practical on-chain.
Storage model: 256-bit slots make small values inefficient; packing is essential.
No floating point: Only integer math; decimals must be handled manually.
Audit Relevance
Understanding EVM mechanics helps auditors identify:
- Gas optimization opportunities
- Reentrancy vectors via external calls
- Storage layout issues in upgradeable contracts
- Arithmetic edge cases
- Opcode-level vulnerabilities
The EVM is the foundation of Ethereum's smart contract ecosystem. Deep understanding of its mechanics is essential for both developing secure contracts and identifying vulnerabilities during audits.
Related Terms
Solidity
The primary programming language for writing smart contracts on Ethereum and EVM-compatible blockchains.
Gas
A unit measuring computational effort required to execute operations on Ethereum, paid by users to incentivize validators.
Bytecode
Compiled low-level instructions executed by the Ethereum Virtual Machine, produced from Solidity or other high-level languages.
Need expert guidance on EVM (Ethereum Virtual Machine)?
Our team at Zealynx has deep expertise in blockchain security and DeFi protocols. Whether you need an audit or consultation, we're here to help.
Get a Quote
