sbpf-coverage
A tool for computing test code coverage of Solana programs and providing trace disassembly mapping between SBPF instructions and source code.
Prerequisites
Install lcov to generate HTML coverage reports:
# macOS
# Ubuntu/Debian
# Fedora
Steps to use for code coverage
-
Add the following to
[profile.release]section of your Solana program's Cargo.toml:= true = "off" = 0This tells Cargo to build with debug information, without optimizations. Be sure to also use SBF Version 1 allowing for dynamic stack frames. This is necessary in the case of working without optimizations. Also be sure to use the latest platform-tools version v1.51 or higher.
Note: from
solana-cargo-build-sbf4.0.0 onwards, building with--debugoutputs the artifacts totarget/deploy/debuginstead oftarget/deploy. -
Execution:
This tool is agnostic from the framework used (Anchor, StarFrame, Typhoon) for collecting the tracing data. In other words it's up to the user to generate the register tracing data which can later be ingested with this tool.
For example in the case of having a few Rust/TS tests for your program using facilities that support register tracing -
LiteSVM(since0.9.0) ormollusk(since0.8.1) you would typically do:SBF_TRACE_DIR=/sbf_trace_dirAfter the tests are finished the register tracing data will be dumped into
sbf_trace_dirand this is the data this tool can ingest and generate code coverage statistics on top of.Finally after having executed your tests:
RUST_BACKTRACE=1This would work for a program called myapp.
-
Run the following command to generate and open an HTML coverage report:
&&
Trace disassembly usage
In addition to generating coverage data, sbpf-coverage can also provide a mapping between
program counters, SBPF disassembly, and your native source code. This is useful for understanding
exactly which instructions correspond to which lines of your program.
The executable must include debug symbols (best in a separate file .so.debug). For best results, build with optimizations enabled
and ensure debug information is preserved. Use -Copt-level=2 or -Copt-level=3 depending on
the optimization level you want to inspect:
RUSTFLAGS="-C strip=none -C debuginfo=2 -Copt-level=2"
Note: Currently
--debugtellscargo-build-sbfto build without--release, which means no optimizations. TheRUSTFLAGSabove override this by forcing the desired opt-level. For a fully production-like build, you would also want LTO (lto = "thin"orlto = "fat") andcodegen-units = 1, but these are only applied in--releasemode. Ideally,cargo-build-sbfwould support a mode that builds with--release(optimizations enabled) while preserving the debug sections — this would produce the most accurate disassembly mapping against production-like code. This is the case with Blueshift's sbpf-linker which can preserve debug sections in optimized release builds.
To use this feature, first collect the register tracing data with SBF_TRACE_DISASSEMBLE set:
SBF_TRACE_DISASSEMBLE=1 SBF_TRACE_DIR=/sbf_trace_dir
This will generate .trace files alongside the register dumps in sbf_trace_dir. Then run:
The output shows each executed instruction alongside its disassembly, source file location, and
the corresponding line of source code. To disable colored output (e.g. when piping to a file),
pass --no-color.
Known problems
sbpf-coverage uses Dwarf debug information, not LLVM instrumentation-based coverage, to map instructions to source code locations. This can have confusing implications. For example:
- one line can appear directly before another
- the latter line can have a greater number of hits
The reason is that multiple instructions can map to the same source line. If multiple instructions map to the latter source line, it can have a greater number of hits than the former.
The following is an example. The line with the assignment to signer is hit only once. But the immediately following line is hit multiple times, because the instructions that map to it are interspersed with instructions that map elsewhere.
5 :
Troubleshooting related to code coverage (unrelated to trace disassembly)
- If you see:
Check that you addedLine hits: 0debug = trueto the[profile.release]section of your project's root Cargo.toml. Again, it's desirable to turn off optimizations which implies dynamic stack frames at the moment of this writing.