Files
sisi/verifiers/many-inputs/structure-aware-fuzzing.md
T
swaitsandClaude Fable 5.2 2fc1fe9c2d
ci / tests (macos-latest, python 3.11) (push) Canceled after 0s
ci / tests (macos-latest, python 3.13) (push) Canceled after 0s
ci / tests (macos-latest, python 3.9) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.11) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.13) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.9) (push) Canceled after 0s
sisi 0.1.0: a shared rulebook and verification tool for AI coding agents
Contracts with a ladder of checks, an end-of-turn gate, retrospectives and
shared lessons, time-to-delivery measurement, a library of 140 verification
techniques and 20 recipes, personal data kept outside the clone, and
releases with rollback. Standard library only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-22 22:29:02 -07:00

3.5 KiB

id, title, answers, rung, section, languages, kinds, cost, detect, ran_here, verified
id title answers rung section languages kinds cost detect ran_here verified
structure-aware-fuzzing Structure-aware fuzzing with `arbitrary` and grammar-based inputs Can sequences of valid-looking operations, not just random bytes, break the code or its invariants? 2 many-inputs rust state-machine, parsing, money medium cargo fuzz --version yes ran

Use when

  • Raw bytes almost never form valid input (JSON, command lists, balanced entries), so a plain fuzzer wastes its time on rejections.
  • You want the fuzzer to drive a sequence of operations on a state machine and check invariants after each.
  • The fuzz target should check more than "no panic": a model, a round trip, "a refused command changes nothing".

Do not use when

  • The input really is arbitrary bytes (a file format parser): use cargo-fuzz with &[u8].
  • You want shrinking and readable counterexamples: proptest is better at that.

Set up

cargo add arbitrary --features derive      # in the fuzz crate (cargo fuzz init already adds it)

Derive Arbitrary on the input types. For grammar-style input (valid SQL, JSON), write a small generator that turns the fuzzer's bytes into a valid document with Unstructured, or use a grammar-based fuzzer such as Grammarinator.

Minimal example (fuzz/fuzz_targets/fuzz_target_1.rs)

#![no_main]
use libfuzzer_sys::fuzz_target;

// in the library crate:
// #[derive(Debug, arbitrary::Arbitrary)]
// pub enum Op { Deposit(u32), Withdraw(u32) }
// Account::apply(&mut self, &Op) -> Result<(), &'static str>

fuzz_target!(|ops: Vec<mycrate::Op>| {
    let mut acct = mycrate::Account::default();
    let mut model: u64 = 0;
    for op in &ops {
        let before = acct.cents;
        if acct.apply(op).is_ok() {
            match op {
                mycrate::Op::Deposit(n) => model += *n as u64,
                mycrate::Op::Withdraw(n) => model -= *n as u64,
            }
        } else {
            assert_eq!(acct.cents, before, "refused op changed state");
        }
        assert_eq!(acct.cents as u64, model, "balance diverged from model");
    }
});

Run

RUSTUP_TOOLCHAIN=nightly cargo fuzz run fuzz_target_1 fuzz/corpus/fuzz_target_1 -- -max_total_time=60

Passing looks like: Done N runs in 60 second(s), exit 0. Failure prints the panic or assertion and the input as the Debug of the Rust value ([Withdraw(...)]), then exits 1 with a crash file in fuzz/artifacts/. Print an old crash as Rust values with cargo fuzz fmt fuzz_target_1 <crash-file>.

As a contract check

{"id": "fuzz-account-ops", "claim": "no fuzzed operation sequence breaks the account model or changes state on refusal", "kind": "state-machine", "rung": 2, "cmd": "cd fuzz-host && RUSTUP_TOOLCHAIN=nightly cargo fuzz run fuzz_target_1 fuzz/corpus/fuzz_target_1 -- -max_total_time=60", "expect": {"exit": 0, "stdout_contains": "Done"}, "tier": "deep", "timeout_s": 600}

What a pass does not prove

  • Only what the run reached in its time budget; a clean run is not a proof.
  • A derived Arbitrary makes uniform-looking values. If the interesting values (zero, maximum, the same account twice) are rare, bias the generator by hand.
  • The invariants are only as good as the asserts. One reference project's operation-sequence target checks that the trial balance balances after any sequence; without that assert the same run would only look for panics.
  • Check the check: seed a bug (the example subtracts one extra cent on withdrawal) and see the fuzzer find it within seconds.