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
