Section 10.15

Corpus minimization

Definition

Corpus minimization is the selection of a smaller subset of a fuzzing §10.9 corpus, the set of inputs a fuzzer has kept, that still produces every coverage feature the whole corpus produces. A coverage feature is one fact that the program, compiled with extra counting code, reports about a run, such as “this branch was taken”. The input is a corpus and one such build; the output is a subset with the same union of features. Test-case minimization is different: it shrinks one input while that input still triggers one failure.

The idea

A coverage-guided fuzzer keeps every input that shows a new feature, so after days of fuzzing its corpus can hold tens of thousands of inputs. Many of them are now redundant: other inputs show all their features too. Redundant inputs cost time, because the fuzzer runs every corpus input at each start and spreads its changes over all of them, and a regression suite built from the corpus runs all of them after every code change.

Choosing a small team so that every skill a job needs is held by at least one member is a fair picture: a member whose skills others already hold can leave. The picture stops in one respect. The skill list holds only what the counting code measures, so two inputs with the same features are interchangeable to the minimization even when they differ in something that matters for security, such as a length exactly at the limit and a length just below it.

How it works

  1. Fix the measuring conditions: one instrumented build, one fuzz harness §10.13 (the code that feeds each input to the program), and one definition of a feature, for example edges alone, or edges together with hit-count ranges. An edge is a jump from one block of code to another, such as the true side of an if statement.

  2. Run every input once and record its feature set.

  3. Form the union of all the feature sets.

  4. Choose a subset whose union equals the full union. A simple greedy method repeatedly takes the input that adds the most features not yet covered, breaking ties in favor of the smaller input, until no feature is left uncovered.

  5. Confirm: run the chosen subset again on the same build and check that its union still equals the full union, since a program that does not behave deterministically can show different features on a second run.

  6. Add back, from a separate labeled set that minimization never touches, every input that must stay for reasons the features do not capture: inputs that once made the program fail, kept as regression tests §10.4, and the seed inputs §10.14 chosen to represent input classes.

Tools do steps 2 to 4 in one command. In libFuzzer §10.26,

./my_fuzzer -merge=1 NEW_CORPUS_DIR FULL_CORPUS_DIR

writes into the empty directory NEW_CORPUS_DIR a subset of FULL_CORPUS_DIR that preserves its full coverage. AFL (American fuzzy lop, a fuzzer named after a breed of rabbit) and its successor AFL++ §10.27 provide afl-cmin for the same job. Each tool uses its own fast selection rule. Test-case minimization has separate tools: afl-tmin, and libFuzzer’s option -minimize_crash=1.

Worked example

A corpus holds five inputs. Their features are numbered 1 to 6:

The union is {1,2,3,4,5,6}\{1, 2, 3, 4, 5, 6\}, which is 6 features. The corpus has 5 inputs and 900+40+60+50+20=1,070900 + 40 + 60 + 50 + 20 = 1{,}070 bytes.

Greedy selection. Round 1: A adds 3 new features and every other input adds 2, so take A. Still uncovered: {4,5,6}\{4, 5, 6\}. Round 2: B, C and D each add 1 and E adds none; the tie goes to the smallest, B, at 40 bytes. Still uncovered: {5,6}\{5, 6\}. Round 3: C and D each add 1; take D, at 50 bytes. Still uncovered: {5}\{5\}. Round 4: take C. The result is A, B, C and D: 4 inputs and 900+40+50+60=1,050900 + 40 + 50 + 60 = 1{,}050 bytes.

Smallest cover. Feature 4 appears only in B, feature 5 only in C and feature 6 only in D, so every cover must contain B, C and D. Those three already cover {1,4}∪{2,5}∪{3,6}={1,2,3,4,5,6}\{1, 4\} \cup \{2, 5\} \cup \{3, 6\} = \{1, 2, 3, 4, 5, 6\}, so the smallest cover is B, C and D: 3 inputs and 40+60+50=15040 + 60 + 50 = 150 bytes. The greedy method kept one more input and 1,050−150=9001{,}050 - 150 = 900 more bytes, because its first choice, A, looked best and later proved unnecessary.

What was lost. Both covers keep 6 of the 6 features. Both drop E, because other inputs already show its features 1 and 2. The minimized corpus no longer contains a message at the exact length limit, although no feature was lost.

Stated precisely

Let XX be the corpus, a finite set of inputs, and let F(x)F(x) be the set of features observed when input xx runs on one fixed instrumented build. A minimized corpus is a subset S⊆XS \subseteq X with ⋃x∈SF(x)=⋃x∈XF(x).\bigcup_{x \in S} F(x) = \bigcup_{x \in X} F(x). That equation is the whole contract. It says nothing about any property of the inputs other than their features on that build.

Finding a smallest such SS is the minimum set cover problem: the features are the elements to be covered, and each input is the set of features it covers. The problem is NP-hard (NP stands for nondeterministic polynomial time). In practice this means that no known algorithm §14.1 finds a smallest SS for every corpus in time that grows only polynomially, like n2n^2 or n3n^3, with the number nn of inputs. The greedy method returns a cover with at most H(d)H(d) times as many inputs as a smallest cover, where dd is the largest number of features of any single input and H(d)=1+12+…+1d≤1+lnd.H(d) = 1 + \frac{1}{2} + \dots + \frac{1}{d} \le 1 + \ln d. In the example d=3d = 3, so H(3)=1+0.5+0.333≈1.83H(3) = 1 + 0.5 + 0.333 \approx 1.83, and the bound allows 1.83×3≈5.51.83 \times 3 \approx 5.5 inputs; the greedy method used 4.

The equation keeps holding on later runs only under two assumptions: the program is deterministic, so that F(x)F(x) is the same on every run; and later runs use the same build and the same feature definition. A changed program produces different features, so a corpus minimized for an old build must be checked again on the new one.

Limits and common mistakes

Minimization preserves features, not meanings. Valid versus revoked credentials, a length at the limit versus just below it, one protocol version versus another: each distinction survives only if it changes the measured features.

The result depends on the feature definition. With edges alone, inputs that differ only in how many times a loop runs collapse into one; with hit-count ranges some of them stay; with comparison feedback, which also records compared values, many more stay.

A smallest cover need not be unique, and tools do not promise a smallest cover, so two runs or two tools can keep different inputs from the same corpus.

A minimized corpus is tied to its build. After the code changes, inputs that the old build showed to be redundant may be the only ones that reach the new code. Keep the full corpus until the minimized one has been checked on the new build.

Corpus minimization is neither test-case minimization nor delta debugging §10.39. Those methods shrink one failing input, or one set of changes, while the failure persists. Corpus minimization removes whole inputs and leaves every input it keeps unchanged.

Fewer inputs do not guarantee less time: a small corpus can still contain one input that takes a second to run.

Questions answered

Does minimizing coverage also preserve security-relevant semantic distinctions?

No. Corpus minimization keeps a subset of inputs that together produce every coverage feature the full corpus produces, and that is the only property it preserves. A coverage feature is a fact recorded by counting code compiled into the program, such as “this branch was taken”. A security-relevant distinction survives only if it changes the measured features. Examples are valid versus revoked credentials, a length exactly at the limit versus just below it, one protocol version versus another, and a message in the right versus the wrong protocol state. Two inputs that take the same branches are interchangeable to the minimization, so one of them is dropped.

For example, if the only message with a length exactly at the maximum takes the same branches as other valid messages, the minimization drops it, and the minimized corpus no longer tests that boundary. Keep the inputs that the security argument depends on, the labeled seed classes and the inputs that once made the program fail, in a separate set that minimization never touches. Run that set with every campaign, and state which classes the minimized corpus no longer contains.

Related entries: Coverage-guided fuzzing (§10.12), Seed corpus (§10.14), Delta debugging (§10.39), Coverage metric (§10.18), Regression test (§10.4), libFuzzer (§10.26), AFL++ (§10.27).