Skip to content

trailofbits/anchor-coverage

Repository files navigation

anchor-coverage

A wrapper around anchor test for computing test coverage

anchor-coverage requires a patched Solana test validator (see below). The patch is known to work with Agave commit cd29142.

Steps to use

  1. Clone the Agave repository and checkout the commit named above.

  2. Add the following to the [patch.crates-io] section near the end of Agave's Cargo.toml, and install Agave from source:

    solana-sbpf = { git = "https://github.com/trail-of-forks/sbpf-coverage" }

    For Linux users, the following commands should suffice:

    sed -i '/^\[patch\.crates-io\]$/a solana-sbpf = { git = "https://github.com/trail-of-forks/sbpf-coverage" }' Cargo.toml
    ./scripts/cargo-install-all.sh .
    export PATH=$PWD/bin:$PATH
  3. Add the following to the [profile.release] section of your Anchor project's root Cargo.toml:

    debug = true

    This tells Cargo to build with debug information.

  4. Run anchor-coverage as follows:

    anchor-coverage [ANCHOR_TEST_ARGS]...

    This will create an sbf_trace_dir directory with an LCOV file for each executable run.

  5. Run the following command to generate and open an HTML coverage report:

    genhtml --output-directory coverage sbf_trace_dir/*.lcov && open coverage/index.html

Known problems

anchor-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 :     pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
            1 :         let signer = &ctx.accounts.signer;
            4 :         let pubkey = signer.signer_key().unwrap();
           11 :         msg!("Signer's pubkey: {}", pubkey);
            1 :         Ok(())
            1 :     }

Troubleshooting

  • If you see:
    Line hits: 0
    
    Check that you added debug = true to the [profile.release] section of your Anchor project's root Cargo.toml.

Links