Skip to content
systemcoding.dev
Security

Build and Minimize a Fuzzing Corpus

Collect seeds, merge and shrink a corpus with libFuzzer -merge=1 and afl-cmin, replay it in CI with -runs=0, and keep it clean of crashes and private data.

8 min read
Summarize in AI
Flat illustration of a shield with a lock, a key and a password field, with the OWASP, Linux and Rust logos.

A fuzzer is only as good as the inputs it starts from and the inputs it remembers. That collection is the corpus, and it is the one fuzzing asset that is worth keeping under version control: it records everything the fuzzer learned about your format, it lets a new fuzzing run start where the last one ended, and it doubles as a fast regression suite. A corpus left alone grows into thousands of near-duplicate files that slow every run and bloat the repository. This post shows how to gather seeds, merge and minimize with libFuzzer in LLVM 19 and with AFL++ 4.x, replay the corpus in CI, and keep it free of crashes and data you should not publish.

It assumes you have a working fuzz target; the guide to fuzzing C and C++ code builds one. The examples use a libFuzzer target named fuzz_tlv.

What goes into a corpus

Three kinds of file belong in it, in order of value.

Real samples. Files produced by real software for the format you parse: a handful of small valid images for an image decoder, captured protocol messages for a network parser, configuration files from real projects. They are valid, they carry the structures the format exists to hold, and they put the fuzzer at the top of the format immediately.

Specification and test-suite examples. Many formats ship conformance files, and your own unit tests contain inputs chosen to hit edge cases. Convert test fixtures to files and copy them in.

Fuzzer discoveries. Everything the fuzzer kept because it reached new code. These are what you merge back after each run.

Start small. A few dozen seeds, each a few hundred bytes at most, are better than a thousand large files, because the cost of every mutation scales with input size and large files crowd out the exploration that finds new coverage.

Where to get seeds

Sources, from most to least reliable:

  • Your own repository's test fixtures and sample data.

  • The upstream project's test suite, if it is open source and its licence allows redistribution.

  • Output of a generator, such as a tool that writes the format. Run it with different options and save each result.

  • Public corpora published for common formats by fuzzing projects.

  • Handwritten minimal files, built from the specification, for each feature of the format.

Check the licence before committing third-party files, and never copy files containing personal or confidential data. A seed is public the moment the repository is, and fuzzing infrastructure may copy it widely. Strip metadata such as author names, GPS tags and embedded paths from sample images and documents before adding them.

Merge new findings with libFuzzer

After a run, the working corpus directory contains the inputs the fuzzer kept, many of which become redundant as later inputs cover the same ground. libFuzzer's merge mode picks the smallest set of files that reproduces the full coverage. Give it an empty destination directory first, then the sources:

Shell
mkdir -p corpus_min
./fuzz_tlv -merge=1 corpus_min corpus new_findings
Text
MERGE-OUTER: 412 files, 120 in the initial corpus, 0 processed earlier
MERGE-OUTER: attempt 1
INFO: Seed: 3317420891
INFO: Loaded 1 modules   (214 inline 8-bit counters): 214 [0x55d0c1a40ee8, 0x55d0c1a40fbe), 
INFO: Loaded 1 PC tables (214 PCs): 214 [0x55d0c1a40fc0,0x55d0c1a41d20), 
MERGE-INNER: using the control file '/tmp/libFuzzerTemp.FuzzWithFork4822.merge'
MERGE-INNER: 532 total files; 0 processed earlier; will process 532 files now
#1	pulse  cov: 12 ft: 12 exec/s: 0 rss: 40Mb
#532	DONE   cov: 187 ft: 903 exec/s: 0 rss: 41Mb
MERGE-INNER: 532 new files with 903 new features added; 187 new coverage edges
MERGE-OUTER: successful in 1 attempt(s)
MERGE-OUTER: the control file has 0 bytes
MERGE-OUTER: consumed 0Mb (41Mb rss) to parse the control file
MERGE-OUTER: 61 new files with 903 new features added; 187 new coverage edges

Read the last line: of 532 candidate files, 61 were enough to reproduce 187 coverage edges and 903 features. Counts, seeds and temporary paths will differ on your machine. The merged corpus_min directory is the one to commit. Merging also deduplicates identical content, because files are named by their hash.

Two details matter. The first directory is the destination, and files from later directories are added only when they contribute coverage that the earlier ones lack, so list the directory you trust most first. And -merge=1 replays every input, which takes real time on a large corpus; run it as a batch job, not on each commit.

Minimize with AFL++

AFL++ separates the two jobs. afl-cmin reduces the number of files while keeping the same set of coverage tuples, and afl-tmin shrinks the content of one file.

Shell
afl-cmin -i out/default/queue -o corpus_min -m none -- ./target @@
Text
corpus minimization tool for afl-fuzz by Michal Zalewski

[*] Testing the target binary...
[+] OK, 1124 tuples recorded.
[*] Obtaining traces for input files in 'out/default/queue'...
    Processing file 212/212...
[*] Sorting trace sets (this may take a while)...
[+] Found 3140 unique tuples across 212 files.
[*] Finding best candidates for each tuple...
[*] Sorting candidate list (be patient)...
[*] Processing candidates and writing output files...
[+] Narrowed down to 38 files, saved in 'corpus_min'.

The shipped afl-cmin is a shell script, and AFL++ also installs afl-cmin.bash, a pure-shell version. Both pick files that cover each tuple, preferring smaller files. Follow it with afl-tmin on each file if the corpus will be used for repeated runs, since a 40-byte input mutates and runs faster than the 4 KiB one it was reduced from:

Shell
mkdir -p corpus_tmin
for f in corpus_min/*; do
    afl-tmin -i "$f" -o "corpus_tmin/$(basename "$f")" -m none -- ./target @@ >/dev/null 2>&1
done

The loop discards the tool's chatter. Minimizing a few hundred files can take a while, so run it once per major corpus update, not every day.

Keep the corpus small by policy

A written policy prevents corpus drift. A workable one: seeds are checked in under corpus/<target>/, one directory per fuzz target, and only the minimized output of a merge is ever committed. A file enters the corpus only through a merge, so each file adds coverage. Files are named by content hash, which is what both libFuzzer and AFL++ do by default, so names carry no meaning and conflicts do not occur.

Set a size limit per file and enforce it. -max_len on the fuzzer and a check in CI that no corpus file exceeds it stops a stray large file from slowing every run:

Shell
find corpus -type f -size +4k -print

An empty result means no file is larger than about 4 KiB. Choose the threshold from your format; for a protocol with small messages it can be a few hundred bytes.

Replay the corpus in CI

The cheapest, most valuable CI use is replay: run the fuzz target over the committed corpus once, with no mutation, and fail if anything crashes. With libFuzzer, passing directories or files and -runs=0 does exactly that:

Shell
./fuzz_tlv -runs=0 corpus/fuzz_tlv regression/
Text
INFO: Running with entropic power schedule (0xFF, 100).
INFO: Seed: 1203340975
INFO: Loaded 1 modules   (214 inline 8-bit counters): 214 [0x5581a0d46ee8, 0x5581a0d46fbe), 
INFO: Loaded 1 PC tables (214 PCs): 214 [0x5581a0d46fc0,0x5581a0d47d20), 
INFO:       61 files found in corpus/fuzz_tlv
INFO:        3 files found in regression/
INFO: -max_len is not provided; libFuzzer will not generate inputs larger than 4096 bytes
INFO: seed corpus: files: 64 min: 1b max: 612b total: 2211b rss: 40Mb
#65	INITED cov: 187 ft: 903 corp: 61/2211b exec/s: 0 rss: 40Mb
#65	DONE   cov: 187 ft: 903 corp: 61/2211b exec/s: 0 rss: 40Mb
Done 65 runs in 0 second(s)

-runs=0 replays every file and exits. The run takes about as long as running the inputs once, usually a second or two, and it catches the case where a change reintroduces an old bug, the one situation where the regression directory pays for itself. Keep regression/ separate from the corpus: it holds crash inputs and must never be minimized away, because those files are there for the bug they trigger, not for their coverage.

The AFL++ equivalent is to run the instrumented binary over each file with a loop, which the sanitizer will abort on if a file crashes it:

Shell
for f in corpus/arc/* regression/arc/*; do
    ./arc_asan "$f" >/dev/null || { echo "FAIL: $f"; exit 1; }
done

Measure what the corpus covers

A corpus can be large and still miss important code. Replay it against a coverage build to see:

Shell
clang-19 -g -O1 -fprofile-instr-generate -fcoverage-mapping -fsanitize=fuzzer fuzz_tlv.c tlv.c -o fuzz_tlv_cov
LLVM_PROFILE_FILE=corpus.profraw ./fuzz_tlv_cov -runs=0 corpus_min
llvm-profdata-19 merge -sparse corpus.profraw -o corpus.profdata
llvm-cov-19 report ./fuzz_tlv_cov -instr-profile=corpus.profdata
Text
Filename    Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover
-------------------------------------------------------------------------------------------------------------------------------
tlv.c             9                 2    77.78%           1                 0   100.00%          16                 3    81.25%
-------------------------------------------------------------------------------------------------------------------------------
TOTAL             9                 2    77.78%           1                 0   100.00%          16                 3    81.25%

The numbers are an example. Use the line-level view, llvm-cov show, to see which branches never ran, then decide whether the gap is a gate the fuzzer cannot cross, which calls for a seed, dictionary or mutator, or code that is genuinely unreachable from the entry point. Track the total over time. A change that drops it points at a harness or build regression.

Keep crashes and secrets out

Two rules prevent embarrassing mistakes.

Crashes do not belong in the corpus. A corpus is replayed with the expectation that every file runs cleanly. If a finding is committed there, the replay step fails forever, or it hides behind a suppression. Fix the bug, then move the minimized input to regression/ as a test that must keep passing. The fuzzer's own artifact directory, such as fuzz/artifacts/ in cargo-fuzz or out/default/crashes/ in AFL++, is a working area, not a destination.

Seeds must not contain private data. Real files carry metadata, and a sample from a customer system can contain names, addresses or tokens. Scan new seeds the way you would scan any file you publish, and prefer synthetic samples whose content you wrote.

When corpus management is overkill

A tiny format that a fuzzer saturates in a minute needs no corpus policy; keep the three or four seeds in the repository and replay them. A target that is rewritten often changes its coverage so much that an old corpus helps less, and regenerating it from seeds on each large change is cheaper than maintaining it. For a project fuzzed continuously by OSS-Fuzz or ClusterFuzzLite, the service stores and merges the corpus itself, and your repository needs only the seed corpus and the regression files.

Also inDebugging
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.