Corpus, Dictionaries and Minimisation
A fuzzer's results depend less on the engine than on what you feed it. Two campaigns with the same engine and the same harness can differ by orders of magnitude because one started from good samples and knew the format's keywords, and the other started from nothing. This lesson covers the three inputs you control: the seed corpus, the dictionary, and the minimisation of both the corpus and the crashes. A session measures the effect of a dictionary on a target whose keywords the fuzzer cannot discover by itself.
The seed corpus
The seed corpus is the set of inputs the first mutations start from. Good seeds are:
- valid: real files or messages that pass the first checks (magic, version, checksums), taken from the project's tests, from your own sample generator, or from the specification's examples;
- small: a few hundred bytes, not megabytes, because every mutation and every run costs time proportional to the size;
- diverse: one sample per feature (every record type, every compression mode, every option), rather than many similar samples.
During a campaign the corpus grows with every input that adds coverage. Kept between runs, it is the most valuable artefact of fuzzing: the next campaign starts where the last one stopped, and CI can replay it in minutes to catch regressions.
Dictionaries
A dictionary lists tokens the format uses: magic numbers, keywords, tag names, delimiters. The fuzzer inserts or overwrites them as whole units, so it doesn't have to guess begin one byte at a time. The format is the same for libFuzzer and AFL++:
# cfg.dict
kw1="begin "
kw2=" level"
kw3=" end"
magic="\x89PNG"Modern engines also find tokens themselves. libFuzzer traces comparisons and memcmp/strcmp arguments (-use_cmp, on by default) and keeps the operands it sees; AFL++'s CmpLog and LTO "autodictionary" do the same. That works when the target compares against the token directly. It fails when the token is hidden: compared through a hash, a lookup table, a decompression step or a checksum. Then a hand-written dictionary is the difference between finding the bug and not.
A target that hides its keywords
The toy config reader below recognises the words begin, level and end by their djb2 hash in a switch, as generated keyword tables often do. The comparison the fuzzer can observe is between two 32-bit hashes, which gives it no clue about the letters. The seeded bug: each level after begin stores into a 4-entry stack array, and the depth is never checked, so the fifth level writes past it.
The session runs three seeds without and with a three-word dictionary (at most 2,000,000 runs each), then merges all corpus inputs into a minimal set and minimises the first crash. The build uses -O0 so that the toy's out-of-bounds store stays exactly as written: optimisers may reason away code whose behaviour is undefined, which is one reason fuzzing builds avoid high optimisation levels. It runs in your sandbox:
cat > cfg.c <<'SRC'
#include <stddef.h>
#include <stdint.h>
#include <string.h>
/* Toy config reader: words separated by spaces. Keywords are recognised by a hash,
as generated keyword tables often do, so the fuzzer cannot see the strings it compares. */
static uint32_t djb2(const uint8_t *s, size_t n) {
uint32_t h = 5381;
for (size_t i = 0; i < n; i++) h = h * 33 + s[i];
return h;
}
enum { KW_BEGIN = 1, KW_LEVEL, KW_END };
static int keyword(const uint8_t *s, size_t n) {
switch (djb2(s, n)) {
case 0x0f2587eau: return KW_BEGIN; /* "begin" */
case 0x0fdabc3du: return KW_LEVEL; /* "level" */
case 0x0b886f1cu: return KW_END; /* "end" */
}
return 0;
}
int cfg_parse(const uint8_t *d, size_t n) {
int levels[4] = {0}, state = 0, depth = 0;
size_t i = 0;
while (i < n) {
size_t j = i;
while (j < n && d[j] != ' ') j++;
int kw = keyword(d + i, j - i);
if (kw == KW_BEGIN) state = 1;
else if (state == 1 && kw == KW_LEVEL) { levels[depth] = (int)(j - i); depth++; } /* BUG: depth never checked against 4 */
else if (kw == KW_END) state = 0;
i = j + 1;
}
int sum = 0;
for (int k = 0; k < depth; k++) sum += levels[k];
return sum;
}
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { cfg_parse(data, size); return 0; }
SRC
clang -g -O0 -fsanitize=fuzzer,address cfg.c -o cfg_fuzz
printf 'kw1="begin "\nkw2=" level"\nkw3=" end"\n' > cfg.dict
echo "== 2,000,000 runs at most, three seeds, without and with the dictionary"
for s in 1 2 3; do
for mode in plain dict; do
rm -rf corpus-$mode && mkdir corpus-$mode && printf 'begin end' > corpus-$mode/seed
extra=""; [ $mode = dict ] && extra="-dict=cfg.dict"
./cfg_fuzz -seed=$s -runs=2000000 -max_len=64 $extra corpus-$mode > $mode.log 2>&1
found=$(grep -m1 -oE 'AddressSanitizer: [a-z-]+' $mode.log || echo "no crash")
printf ' seed %s %-5s %-38s after %s runs, corpus %s files\n' $s $mode "$found" \
"$(grep -oE '^#[0-9]+' $mode.log | tail -1 | tr -d '#')" "$(ls corpus-$mode | wc -l)"
done
done
echo "== merge: keep only inputs that add coverage"
mkdir small && ./cfg_fuzz -merge=1 small corpus-plain corpus-dict > merge.log 2>&1
echo " $(cat corpus-plain/* corpus-dict/* | wc -c) bytes in $(find corpus-plain corpus-dict -type f | wc -l) files -> $(cat small/* | wc -c) bytes in $(ls small | wc -l) files"
echo "== minimise the crash"
crash=$(ls crash-* | head -1)
./cfg_fuzz -minimize_crash=1 -runs=20000 -seed=1 "$crash" > min.log 2>&1
small_crash=$(ls -S minimized-from-* | tail -1)
echo " $(wc -c < "$crash") bytes -> $(wc -c < "$small_crash") bytes: $(cat "$small_crash")"== 2,000,000 runs at most, three seeds, without and with the dictionary
seed 1 plain no crash after 2000000 runs, corpus 31 files
seed 1 dict AddressSanitizer: stack-buffer-overflow after 32097 runs, corpus 36 files
seed 2 plain no crash after 2000000 runs, corpus 32 files
seed 2 dict AddressSanitizer: stack-buffer-overflow after 20538 runs, corpus 37 files
seed 3 plain no crash after 2000000 runs, corpus 32 files
seed 3 dict AddressSanitizer: stack-buffer-overflow after 13939 runs, corpus 31 files
== merge: keep only inputs that add coverage
907 bytes in 63 files -> 551 bytes in 33 files
== minimise the crash
46 bytes -> 35 bytes: begin level level level level level
- Without the dictionary, two million runs per seed produce about 30 corpus files (different spacings, lengths and word counts around the seed's own
beginandend) and no crash: buildinglevelletter by letter gives no feedback until the whole word matches its hash. - With the dictionary, each seed crashes after 14,000 to 32,000 runs, while the plain campaigns found nothing in 2,000,000. The
#Ncounter is the last status line before the crash, so the numbers are approximate. - Merge (
-merge=1 out in1 in2) replays every input and keeps only those that add coverage: 63 files become 33. Merging after each campaign keeps the corpus fast; AFL++'safl-cmindoes the same job. - Minimise (
-minimize_crash=1) keeps mutating the crashing input, keeping any shorter version that still crashes. The 46-byte crash shrank to 35 bytes,begin level level level level level, which explains itself: onebegin, five levels, four slots.
Minimisation for humans
A minimised crash is the best part of a bug report. It shows the root cause by having nothing else in it, it is easy to turn into a regression test, and it is easy to step through in a debugger. Engines have their own minimisers (-minimize_crash, afl-tmin); for inputs from other sources, the general algorithm is delta debugging (next section). Always confirm that the minimised input crashes the same way (same sanitizer kind, same top frames): shrinking can drift into a different bug.
How bugs like this one are fixed
The root cause is the missing bound on depth: check if (depth >= 4) return -1; before the store, or size the array from the input and allocate it. Then the 35-byte minimised file goes into the regression corpus. The dictionary goes into the repository next to the harness, so every future campaign uses it.
Corpus management in practice
| Practice | Why |
|---|---|
| keep the corpus in version control or a storage bucket, per harness | campaigns resume; regressions replay |
| merge after each long campaign | keeps runs fast and the corpus small |
| add every fixed crash's minimised input to the regression set | the bug can never silently return |
| seed new harnesses from the test suite's sample files | valid inputs from day one |
| ship a dictionary for every text or keyword-based format | tokens the engine cannot infer |
set -max_len deliberately |
too small hides bugs, too large wastes time |
Pitfalls
- Huge seeds. A 5 MB sample makes every run slow and most mutations land in irrelevant data. Cut samples down.
- Duplicated seeds. A hundred similar files waste the engine's scheduling. Merge first.
- Minimising into another bug. Check the minimised crash's report against the original.
- Dictionary tokens without context. Here the tokens include the space that separates words; a dictionary that ignores the format's delimiters helps much less.
Practice
1. Why could libFuzzer's comparison tracing not discover level here?
The program compares the 32-bit hash of the word with a constant, so the traced operands are numbers like 0x0fdabc3d. Writing that number into the input does not produce a word with that hash.
2. A colleague's PNG decoder campaign has 40,000 corpus files, each about 1 MB. What do you do?
Replace the seeds with small, diverse PNGs (one per colour type, bit depth, interlace mode, chunk type), merge the existing corpus to the inputs that add coverage, and add a dictionary of chunk names (IHDR, IDAT, PLTE, tEXt...) and the PNG signature. Check whether the CRC of each chunk should be ignored in the fuzzing build.
3. The minimised crash is begin level level level level level. Write the regression test for C.
assert(cfg_parse((const uint8_t *)"begin level level level level level", 35) == -1); after the fix, run under ASan in the test suite (or keep the file in regress/ and replay it with the fuzz binary).
Test yourself
What makes a good seed corpus?
- Many large random files
- Small, valid, diverse samples, one per feature
- Only crashing inputs
- One huge file
When is a hand-written dictionary essential?
- Never, engines find all tokens
- When tokens are hidden from comparison tracing, e.g. compared through a hash or a lookup table
- Only for binary formats
- Only with AFL++
What does libFuzzer's -merge=1 do?
- Combines crash files
- Keeps the subset of inputs that together give the same coverage
- Merges two fuzzers
- Deletes the corpus
Why check a minimised crash's sanitizer report against the original?
- Reports are random
- Shrinking may drift into a different bug
- ASan forgets reports
- For performance