Fuzzing Harness Design

The harness is the glue that turns fuzzer bytes into calls on the target API or into a restored process/VM state; its scope, statefulness and input decoding decide what a fuzzer can reach and whether its crashes are real.

How it works

  • A libFuzzer-style harness implements LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); targets must be deterministic, fast and accept any input (libFuzzer). AFL++, LibAFL, Jazzer, Atheris, cargo-fuzz and Go’s testing.F all reuse that shape (see Coverage-Guided Fuzzing).
  • Process for a new target: pick the function from the library’s own examples/tutorials, map fuzz data to its parameters, free resources on every path (e.g. xmlFreeTextReader for libxml2’s reader API), validate return values, iterate on coverage (SRLabs: beginner's harness guide).
  • Scope: narrow harnesses find bugs in one function quickly; broad ones reach more but dilute mutations — set scope to “enough code to find issues”, no wider (SRLabs: effective harnesses).

Rules (SRLabs “DOs and DON’Ts”)

Don’t

  1. Reuse input bytes — e.g. data[0] as both a selector and a value couples two decisions; split the input.
  2. Reinterpret input — one harness dispatching to functionA/functionB on the first byte gives a corpus of incomparable “types” and noisy coverage; prefer one target per function.
  3. Keep state between iterations — leftover state makes crashes non-reproducible and shows as low stability in AFL++.
  4. Log or do I/O — strip fprintf/debug output.

Do

  1. Check minimum length first.
  2. Consume with FuzzedDataProvider (no reuse by construction).
  3. Check pointers in returned structures.
  4. Clean up state and memory before every return.
  5. Assert invariants — sorted output after a successful sort, balances summing in a transaction fuzzer, consistent fields after a parse. This is how harnesses catch logic bugs.
  6. Move heavy one-time init to LLVMFuzzerInitialize().

Outside the harness: gate checksums and CPU-heavy checks behind #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION; stub or link-time-replace non-target interactions (network, DB) (SRLabs).

Techniques

Input decoding

FuzzedDataProvider fdp(data, size);
size_t alloc = fdp.ConsumeIntegral<size_t>();
std::string s = fdp.ConsumeRandomLengthString();

Interleaved fuzzing uses one leading byte to drive switch (mode % 4) across several related APIs sharing state — the deliberate exception to “don’t reinterpret”, useful for API sequences; protobuf custom mutators are the step up to structure (Testing Handbook: Writing harnesses, Grammar-Based Fuzzing). Atheris exposes the same FuzzedDataProvider API in Python (Atheris).

Reaching valid state

The most productive zlib harness built by GPT-5.5-Cyber constructed valid gz* states before fuzzing instead of throwing random bytes at the gz* API like the OSS-Fuzz harness (ToB zlib). For containers, wrap the mutated unit in a minimal valid document: Remedio mutated one image XObject inside a minimal PDF (Remedio); Atredis mutated one atom inside an MP4 template and fixed ancestors’ sizes (Atredis).

Closed-source library harnesses (Android, Frida mode)

For Samsung’s libimagecodec.quram.so (no header, but exported API), the harness mirrored the platform decoder found in a P0 backtrace: QuramGetImageInfoFromBuffer for type and dimensions, reject non-JPEG and oversized frames, malloc(w*h*4) for ARGB8888, then QuramDecodeBuffer. Prototypes came from r2ghidra with PLT addresses mapped back to names. Link the target as DT_NEEDED (-limagecodec.quram) so dlopen of the harness maps it before the instrumentation ranges are computed; build a plain and an -fsanitize=address runner from the same harness for reproduction. Seeds were generated, not collected: a script built valid progressive JPEGs (scan scripts × chroma subsamplings, one shared DC and AC table, _trunc twins) after logcat [function:line] errors showed the vendor parser’s quirks (AC tables required even for DC-only scans; k-th scan component must have frame index ≥ k). A small dictionary of JPEG markers plus sampling bytes (0x11 0x12 0x21 0x22 0x41) helped (Mobile Hacking Lab).

Managed-language harnesses (Jazzer)

fuzzerInitialize / fuzzerTearDown for enterprise setup and cleanup; custom method hooks (--custom_hooks=...) must ship in a separate JAR, and because hooks only see system-level JARs at runtime, inspect app state via reflection; handle slow paths with fork mode and -ignore_timeouts (Practical Jazzer). Jazzer’s built-in SSRF/path-traversal/command-injection sanitizers are configurable via BugDetectorsAPI (Jazzer). A JSONPath library harness: only documented exceptions allowed, 5 s per input hard limit (hangs matter more than crashes for untrusted queries), 15 s per target per CI build, 10 min nightly (Treetrail hardening).

Snapshot harnesses

When startup is long, multithreaded or non-deterministic, snapshot just before input consumption and restore per iteration:

  • Nyx + HyperHook — detour into the target function, hypercall to the host, register exception handlers, place the input buffer, call the target; Windows and Linux userland, driven by LibAFL (HyperHook).
  • wtf — distributed snapshot fuzzer for Windows user and kernel mode on bochscpu, WHV or KVM backends; bugs in IDA Pro, games, the Windows kernel, NVIDIA drivers (wtf).
  • Unicorn-AFL — dump a Linux process with a small GDB plugin, load into Unicorn, harness the packet handler; 4 vulns in Siemens SCALANCE LPE9403’s PROFINET DCP daemon (Nozomi).
  • LibAFL-QEMU incremental snapshots for TCP state machines (libslirp CVE-2026-9539), alongside split and multipart inputs (Quarkslab).
  • WinPE as stateless harness — RAM-only WinPE in QEMU (pc machine type), bcdedit boot trimming, hypervisorlaunchtype off, testsigning yes, KDNET, and a custom agent in winpeshl.ini replacing cmd.exe give an idempotent driver-test VM (WinPE harness).

Harness bugs vs target bugs

A crash in harness-owned memory is noise. HarnessMinimizer reduces C/C++ harnesses (3.46–4.51x speedups, much smaller harnesses) and improves triage of harness-vs-library bugs (HarnessMinimizer). For generated harnesses, require deterministic replay, minimization and explicit ownership attribution before reporting (Evidence gate; see LLM-Assisted Fuzzing).

Checklist

Min-length check · FDP decoding, no byte reuse · one API per target (or deliberate interleaving) · global read-only init in LLVMFuzzerInitialize · free/reset on every path · invariant asserts · no I/O · checksum bypass under FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION · per-input timeout · seeds + dictionary · stability ≈100% in AFL++ · crash replays cleanly outside the fuzzer.

Sources