liveBook · Manning

Cargo-fuzz

As explained in Code Like a Pro in Rust

cargo-fuzz is a Cargo subcommand and tool that integrates Rust projects with LLVM's libFuzzer for fuzz testing.

cargo-fuzz connects a Rust project to LLVM’s libFuzzer. It generates fuzz-test boilerplate and provides a Rust API, avoiding direct FFI. Use it to probe code for unexpected failures caused by inputs you might not think to test manually. section 7.5

Start a fuzz target

Install the tool with `cargo install cargo-fuzz`, initialize fuzzing with `cargo fuzz init`, then write a target that calls the function under test. Run the target with `cargo fuzz run fuzz_target_1`. section 3.8.3 section 7.5

Listing 7.11 Fuzz test
#![no_main]
use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
 
#[derive(Arbitrary, Debug)]         #1
struct Input {
    s: String,                      #2
}
 
fuzz_target!(|input: Input| {       #3
    use fuzzme::parse_integer;
 
    parse_integer(&input.s);        #4
});

A typed target derives `Arbitrary` for its input, then passes generated strings to the function under test through `fuzz_target!`. section 7.5

The string-parsing example shows why plausible validation deserves fuzzing: its regex accepts some digit strings that cannot be parsed as `i32`, while the subsequent parse uses `expect`. libFuzzer repeatedly calls the target with generated inputs and records a test case when one triggers an error. Inspect the reported failing-input artifact rather than assuming the validation covers every edge case. section 7.5

How Code Like a Pro in Rust covers it

How deep it goes
Walks through installation, target setup, a typed harness, execution, and a failing artifact; explains libFuzzer only at a high level.
Examples
Demonstrates a regex-validated integer parser, an `Arbitrary` string input, a `fuzz_target!` harness, and a reported crash artifact.
What it assumes
Assumes Rust project familiarity while introducing libFuzzer’s repeated-input testing at a high level.
What you can do after
Diagnose/debug with it by running a fuzz target and inspecting the failing input artifact.
How it connects
Connects fuzz testing with integration testing, input parsing, regex validation, and security vulnerabilities.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.