# 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 ([[raw/llvm.org/libfuzzer-a-library-for-coverage-guided-fuzz-testing-llvm-2925|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 ([[raw/srlabs.de/beginner-s-guide-to-writing-a-fuzzing-harness-3923|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 ([[raw/srlabs.de/secrets-of-effective-fuzzing-harnesses-3922|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) ([[raw/srlabs.de/secrets-of-effective-fuzzing-harnesses-3922|SRLabs]]).
## Techniques
### Input decoding
```cpp
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 ([[raw/appsec.guide/writing-harnesses-testing-handbook-3921|Testing Handbook: Writing harnesses]], [[Grammar-Based Fuzzing]]). Atheris exposes the same `FuzzedDataProvider` API in Python ([[raw/seeinglogic.com/getting-started-with-python-fuzzing-using-atheris-3903|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 ([[raw/blog.trailofbits.com/gpt-5-5-cyber-built-a-zlib-fuzzing-lab-in-a-day-10085|ToB zlib]]). For containers, wrap the mutated unit in a minimal valid document: Remedio mutated one image XObject inside a minimal PDF ([[raw/remedio.io/we-let-ai-fuzz-adobe-reader-for-a-week-it-found-a-critical-zero-day-cv-12256|Remedio]]); Atredis mutated one atom inside an MP4 template and fixed ancestors' sizes ([[raw/atredis.com/atredis-crafting-targeted-mp4-fuzzers-14956|Atredis]]).
### 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` ([[raw/securitylab.servicenow.com/practical-jazzer-for-the-snazzy-fuzzer-3909|Practical Jazzer]]). Jazzer's built-in SSRF/path-traversal/command-injection sanitizers are configurable via `BugDetectorsAPI` ([[raw/github.com/jazzer-coverage-guided-in-process-fuzzing-for-the-jvm-4876|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 ([[raw/christoph-sens.com/what-the-test-suite-does-not-find-hardening-a-jsonpath-library-with-fu-13951|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 ([[raw/neodyme.io/hyperhook-a-harnessing-framework-for-nyx-3908|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 ([[raw/offsecblog.com/wtf-snapshot-based-fuzzing-of-windows-user-and-kernel-mode-targets-14698|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 ([[raw/nozominetworks.com/fuzzing-siemens-profinet-dcp-with-unicorn-afl-14270|Nozomi]]).
- **LibAFL-QEMU incremental snapshots** for TCP state machines (libslirp CVE-2026-9539), alongside split and multipart inputs ([[raw/blog.quarkslab.com/snapshotting-our-way-to-cve-2026-9539-quarkslab-14269|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 ([[raw/bednars.me/winpe-as-a-stateless-harness-for-windows-driver-testing-and-fuzzing-9935|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 ([[raw/arxiv.org/efficient-program-reduction-for-library-fuzzing-harnesses-12685|HarnessMinimizer]]). For generated harnesses, require deterministic replay, minimization and explicit ownership attribution before reporting ([[raw/paulo.seg.br/llm-fuzzing-build-an-evidence-gate-for-generated-harnesses-12526|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.
## Related
- [[Fuzzing]]
- [[Coverage-Guided Fuzzing]]
- [[Grammar-Based Fuzzing]]
- [[LLM-Assisted Fuzzing]]
- [[Kernel Fuzzing]]
## Sources
- [[raw/llvm.org/libfuzzer-a-library-for-coverage-guided-fuzz-testing-llvm-2925|libFuzzer - A Library for Coverage-Guided Fuzz Testing | LLVM]]
- [[raw/srlabs.de/beginner-s-guide-to-writing-a-fuzzing-harness-3923|Beginner's Guide to Writing a Fuzzing Harness]]
- [[raw/srlabs.de/secrets-of-effective-fuzzing-harnesses-3922|Secrets of Effective Fuzzing Harnesses]]
- [[raw/appsec.guide/writing-harnesses-testing-handbook-3921|Writing Harnesses - Testing Handbook]]
- [[raw/seeinglogic.com/getting-started-with-python-fuzzing-using-atheris-3903|Getting Started with Python Fuzzing Using Atheris]]
- [[raw/blog.trailofbits.com/gpt-5-5-cyber-built-a-zlib-fuzzing-lab-in-a-day-10085|GPT-5.5-Cyber built a zlib fuzzing lab in a day]]
- [[raw/remedio.io/we-let-ai-fuzz-adobe-reader-for-a-week-it-found-a-critical-zero-day-cv-12256|We Let AI Fuzz Adobe Reader for a Week. It Found a Critical Zero-Day CVE-2026-81983]]
- [[raw/atredis.com/atredis-crafting-targeted-mp4-fuzzers-14956|Atredis: Crafting targeted MP4 fuzzers]]
- [[raw/securitylab.servicenow.com/practical-jazzer-for-the-snazzy-fuzzer-3909|Practical Jazzer for the Snazzy Fuzzer]]
- [[raw/github.com/jazzer-coverage-guided-in-process-fuzzing-for-the-jvm-4876|Jazzer: Coverage-guided, in-process fuzzing for the JVM]]
- [[raw/christoph-sens.com/what-the-test-suite-does-not-find-hardening-a-jsonpath-library-with-fu-13951|What the test suite does not find: hardening a JSONPath library with fuzzing]]
- [[raw/neodyme.io/hyperhook-a-harnessing-framework-for-nyx-3908|HyperHook: A Harnessing Framework for Nyx]]
- [[raw/offsecblog.com/wtf-snapshot-based-fuzzing-of-windows-user-and-kernel-mode-targets-14698|wtf: snapshot-based fuzzing of Windows user- and kernel-mode targets]]
- [[raw/nozominetworks.com/fuzzing-siemens-profinet-dcp-with-unicorn-afl-14270|Fuzzing Siemens PROFINET DCP with Unicorn-AFL]]
- [[raw/blog.quarkslab.com/snapshotting-our-way-to-cve-2026-9539-quarkslab-14269|Snapshotting our way to CVE-2026-9539 (Quarkslab)]]
- [[raw/bednars.me/winpe-as-a-stateless-harness-for-windows-driver-testing-and-fuzzing-9935|WinPE as a stateless harness for Windows driver testing and fuzzing]]
- [[raw/arxiv.org/efficient-program-reduction-for-library-fuzzing-harnesses-12685|Efficient Program Reduction for Library Fuzzing Harnesses]]
- [[raw/paulo.seg.br/llm-fuzzing-build-an-evidence-gate-for-generated-harnesses-12526|LLM Fuzzing: Build an Evidence Gate for Generated Harnesses]]