How anza-xyz/sbpf works
Purpose and entry points
solana-sbpf is a Rust library implementing a user-space Solana BPF (SBPF/eBPF) virtual machine: program loading, validation, memory isolation, interpreted execution, and native JIT execution. The public crate root is src/lib.rs; the standalone command-line entry point is cli/src/main.rs. Programs can be constructed from bytecode or loaded from ELF artifacts.
Program ingestion and representation
The executable-program boundary is centered on src/program.rs, which represents loaded SBPF programs and their callable functions. ELF loading and relocation are handled by src/elf.rs plus the parser subsystem in src/elf_parser/mod.rs, with format constants/types separated into src/elf_parser/consts.rs and src/elf_parser/types.rs. Assembly input can alternatively pass through src/asm_parser.rs and src/assembler.rs; src/disassembler.rs exposes the inverse inspection path.
ISA, versioning, and verification
src/ebpf.rs defines the SBPF/eBPF instruction model and version-dependent ISA behavior. src/verifier.rs is the safety gate before execution, with src/static_analysis.rs supplying control-flow or related analysis. This boundary ensures bytecode or ELF-derived instructions are structurally valid before they reach the interpreter or JIT, including differences across SBPF versions.
Execution engines
src/vm.rs assembles the runtime-facing virtual-machine abstraction from an executable, memory mapping, execution context, and selected engine. src/interpreter.rs executes instructions directly. src/jit.rs provides native-code compilation/execution, with architecture-specific backends in src/x86.rs and src/aarch64.rs. JIT availability is platform-sensitive; the README identifies x86_64 JIT support and excludes Windows JIT operation.
Memory and host-call boundary
Sandboxed guest addressing is represented through memory regions and mappings in src/memory_region.rs and src/memory_management.rs; alignment-aware backing storage is in src/aligned_memory.rs. This is the principal isolation boundary between SBPF program memory and host process memory. Host-visible built-ins/syscalls and invocation behavior are part of the VM/program execution model, while doc/syscalls.md documents the syscall surface.
Runtime flow and ecosystem
Typical control flow is: configure loader/built-ins → create an executable from bytecode or ELF → verify/analyze → optionally JIT compile → construct mapped memory regions and an instruction-meter context → create vm → execute via interpreter or JIT. The primary runtime is Rust (minimum rustc 1.83); distribution is through crates.io. CI includes normal build/test automation and fuzzing (.github/workflows/main.yml, .github/workflows/fuzz.yml), with fuzz targets exercising ELF parsing, verification, and JIT/interpreter differential behavior under fuzz/fuzz_targets/.
Main entry points: program.rs Executable program · vm.rs Virtual machine · jit.rs Native JIT · verifier.rs Verifier
Components 19
Program ingestion
- Executable program · Loaded program and callable-function representation.
src/program.rs - ELF loader and relocation
src/elf.rs - ELF parser
src/elf_parser/mod.rs - Assembly pipeline · Parses assembly and emits bytecode.
src/assembler.rs - Disassembler
src/disassembler.rs
ISA and validation
- SBPF ISA and versions
src/ebpf.rs - Verifier · Rejects structurally invalid programs before execution.
src/verifier.rs - Static analysis
src/static_analysis.rs
VM execution
- Virtual machine · Combines executable, memory mapping, context, and engine.
src/vm.rs - Interpreter
src/interpreter.rs - Native JIT · Platform-sensitive native compilation and execution.
src/jit.rs - x86 backend
src/x86.rs - AArch64 backend
src/aarch64.rs
Memory isolation
- Memory regions and mapping · Isolates guest addresses from host process memory.
src/memory_region.rs - Memory management
src/memory_management.rs - Aligned backing memory
src/aligned_memory.rs
Other components
- Public library · Public SBPF VM API.
src/lib.rs - CLI
cli/src/main.rs - Syscall surface
doc/syscalls.md
Connections 20
- Public library → Executable program: constructs
- CLI → Public library: uses
- ELF loader and relocation → ELF parser: parses through
- ELF loader and relocation → Executable program: loads and relocates
- Assembly pipeline → Executable program: emits
- Executable program → Verifier: submitted for validation
- SBPF ISA and versions → Verifier: defines rules
- Static analysis → Verifier: supports
- Verifier → Virtual machine: validated executable
- Executable program → Disassembler: inspected by
- Virtual machine → Interpreter: selects
- Virtual machine → Native JIT: optionally selects
- Native JIT → x86 backend: targets
- Native JIT → AArch64 backend: targets
- Memory regions and mapping → Virtual machine: mapped guest memory
- Memory management → Memory regions and mapping: manages
- Aligned backing memory → Memory regions and mapping: backs regions
- Syscall surface → Virtual machine: documents host calls
- SBPF ISA and versions → Interpreter: instruction semantics
- SBPF ISA and versions → Native JIT: compiles instructions