Fork of capstone-rust/capstone-rs with added Solana sBPF disassembly support.
This fork extends the upstream capstone-rs bindings with support for Solana sBPF (Solana Berkeley Packet Filter), the custom bytecode format used by Solana on-chain programs.
Solana programs are compiled to sBPF, a custom variant of eBPF with Solana-specific instruction semantics (different calling conventions, register layout, and syscall ABI). This fork bundles a capstone C library patched with sBPF architecture support and exposes a full Rust API.
| Feature | Description |
|---|---|
| Registers | r0–r10 (11 registers) |
| ALU instructions | add32/64, sub32/64, mul32/64, div32/64, or, and, lsh, rsh, arsh, neg, mod, xor, mov |
| Load/Store | lddw, ldxb/h/w/dw, stb/h/w/dw, stxb/h/w/dw |
| Byte-swap | le16/32/64, be16/32/64 |
| Branches | ja, jeq, jgt, jge, jset, jne, jsgt, jsge, jlt, jle, jslt, jsle |
| Control flow | call, exit |
| Operand access info | signed/unsigned flag, read/write access type |
Add to Cargo.toml:
[dependencies]
capstone = { git = "https://github.com/cpkt9762/capstone-rs", features = ["arch_sbpf"] }use capstone::prelude::*;
use capstone::arch::sbpf;
// sBPF bytecode: mov64 r1, 1; ldxw r0, [r10-12]; exit
const SBPF_CODE: &[u8] = &[
0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // mov64 r1, 1
0x61, 0xa0, 0xf4, 0xff, 0x00, 0x00, 0x00, 0x00, // ldxw r0, [r10-12]
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
fn main() {
let cs = Capstone::new()
.sbpf()
.mode(sbpf::ArchMode::SbpfV0)
.detail(true)
.build()
.expect("Failed to create Capstone object");
let insns = cs.disasm_all(SBPF_CODE, 0x100)
.expect("Failed to disassemble");
println!("Found {} instructions", insns.len());
for insn in insns.as_ref() {
println!("0x{:04x}: {} {}", insn.address(), insn.mnemonic().unwrap_or(""), insn.op_str().unwrap_or(""));
}
}Output:
Found 3 instructions
0x0100: mov64 r1, 1
0x0108: ldxw r0, [r10-0xc]
0x0110: exit
| Flag | Description |
|---|---|
arch_sbpf |
Enable Solana sBPF disassembly support |
support_all_archs |
Enable all architectures including sBPF (default) |
Bindings to the capstone library disassembly framework.
The Capstone struct is the main interface to the library.
capstone-rs uses the capstone-sys crate to provide the low-level bindings to the Capstone C library.
See the capstone-sys page for the requirements and supported platforms.
- Minimum Rust Version:
1.81.0
extern crate capstone;
use capstone::prelude::*;
const X86_CODE: &'static [u8] = b"\x55\x48\x8b\x05\xb8\x13\x00\x00\xe9\x14\x9e\x08\x00\x45\x31\xe4";
fn main() {
let cs = Capstone::new()
.x86()
.mode(arch::x86::ArchMode::Mode64)
.syntax(arch::x86::ArchSyntax::Att)
.detail(true)
.build()
.expect("Failed to create Capstone object");
let insns = cs.disasm_all(X86_CODE, 0x1000)
.expect("Failed to disassemble");
println!("Found {} instructions", insns.len());
for i in insns.as_ref() {
println!();
println!("{}", i);
let detail: InsnDetail = cs.insn_detail(&i).expect("Failed to get insn detail");
let arch_detail: ArchDetail = detail.arch_detail();
let ops = arch_detail.operands();
println!(" operands: {}", ops.len());
}
}full†: do not compile Capstone C library in diet modestd†: enablestd-only features, such as theErrortraituse_bindgen: runbindgento generate Rust bindings to Capstone C library instead of using pre-generated bindings (not recommended)arch_$ARCH†: enable arch$ARCHsupport in capstone, e.g.arch_arm64enables arch arm64 supportarch_sbpf†: enable Solana sBPF support (added in this fork)support_all_archs†: enable all archs available in capstone, imply allarch_$ARCHfeatures (includesarch_sbpf)check_only: do not compile and link capstone C library, you can enable it to speed upcargo checkby 5x
†: enabled by default
This fork is based on capstone-rust/capstone-rs. Issues unrelated to Solana sBPF should be reported upstream.