eBPF
eBPF¶
eBPF (extended Berkeley Packet Filter) is the underlying execution format for Solana smart contracts (Programs). Solana programs are typically written in Rust or C, and after compilation they generate eBPF bytecode, which is then executed by the Solana runtime.
What is eBPF¶
eBPF was originally a technology used in the Linux kernel for network packet filtering, which later evolved into a general-purpose virtual machine instruction set. Its characteristics include:
- Lightweight: Simple instruction set with high execution efficiency
- Verifiable: Programs can be statically analyzed before execution to ensure safety
- Deterministic: Same input always produces the same output
- Sandbox Isolation: Programs run in a restricted environment without access to system resources
Why Solana Chose eBPF¶
Solana chose eBPF as the execution format for smart contracts based on the following considerations:
1. Performance Advantage eBPF is a streamlined instruction set without a complex runtime environment. Solana can directly compile eBPF bytecode into native machine code for execution (via JIT or AOT), achieving performance close to native programs. Compared to Ethereum's EVM (a stack-based virtual machine), eBPF is register-based and offers higher execution efficiency.
2. Mature Toolchain eBPF is already very mature in the Linux ecosystem, with a complete compiler (LLVM), debugger, and performance analysis tools. Developers can write programs in familiar languages (Rust, C) and compile them to eBPF bytecode via LLVM.
3. Security Verification The eBPF verifier can perform static analysis before loading a program, checking for: - Infinite loops - Illegal memory access - Undefined instructions - Compliance with Solana's resource limits
This pre-verification mechanism prevents runtime errors and improves network stability.
eBPF Execution Flow in Solana¶
- Write the program: Developers write smart contracts using Rust/C
- Compile: Compile to eBPF bytecode via the LLVM toolchain
- Deploy: eBPF bytecode is deployed on-chain as program data
- Verify: The Solana verifier checks the bytecode's validity
- Execute: The runtime compiles eBPF to native code and executes it
During execution, Solana's Sealevel runtime will: - Allocate a virtual machine instance for the program - Load the account data required by the program - Execute eBPF instructions - Record Compute Unit (CU) consumption - Write execution results back to accounts
Compute Limits¶
To prevent programs from consuming excessive resources, Solana imposes strict limits on eBPF programs:
- Compute Unit cap: Default of 200,000 CU per transaction, with a maximum requestable limit of 1,400,000 CU
- Instruction count: eBPF programs cannot exceed a specific number of instructions
- Call depth: Cross-Program Invocation (CPI) depth is limited to 4 levels
- Stack size: 32KB of stack space
Comparison with EVM¶
| Feature | eBPF (Solana) | EVM (Ethereum) |
|---|---|---|
| Architecture | Register-based | Stack-based |
| Execution Method | JIT/AOT compilation | Interpretation or JIT |
| Language Support | Rust, C | Solidity, Vyper |
| Performance | Near-native | Relatively slower |
| Concurrency Model | Supports parallelism | Serial execution |
Related Concepts¶
- SVM: Solana Virtual Machine, the runtime environment responsible for executing eBPF programs
- CU (Compute Units): The unit measuring resource consumption of eBPF program execution
- Sealevel: Solana's parallel runtime, capable of executing multiple eBPF programs simultaneously
- BPF Loader: The system program responsible for loading and verifying eBPF programs