Section 10.12

Coverage-guided fuzzing

Definition

Coverage-guided fuzzing is a form of fuzzing §10.9, the testing method that runs a program on very many automatically made inputs to find inputs that make it fail. In coverage-guided fuzzing the program under test reports which parts of its code each input executed. The fuzzer, the tool that makes and runs the inputs, keeps an input for further changes only when that input executed something no kept input had executed before. Blind fuzzing, its nearest neighbor, makes inputs without this feedback. The method is also called greybox fuzzing.

The idea

Programs check their input in stages: a parser may test the first byte, then the second, and only then reach the code that handles the rest. A fuzzer that makes every input from scratch must pass all the stages in one input, and the chance of that falls by a factor of 256 with every byte that must be right.

Coverage feedback turns one hard guess into a chain of easy ones. An input that happens to pass the first check executes new code, so the fuzzer keeps it. Changes to the kept input usually preserve the byte that passed, so the next task is only the second check. Each kept input is a stepping stone that records progress. The picture stops in one respect: a stone marks only that new code ran, not that a bug is nearer.

The name greybox says how much the fuzzer sees of the program: more than a black-box fuzzer, which sees only whether a run failed, and less than a white-box method such as symbolic execution §3.27, which solves for the input values that take each branch.

How it works

A coverage-guided fuzzer has three parts.

The loop:

  1. Run every seed once. The union of the features the seeds produce is the covered set.

  2. Pick an input from the corpus and mutate it: flip bits; change, insert or delete bytes; insert a token from an input dictionary §10.16, a list of byte strings the program compares against; or splice the input with another corpus input.

  3. Clear the counters, run the program on the new input, and read the features.

  4. If the run failed, save the input as a reproducer.

  5. If the run produced a feature outside the covered set, add the input to the corpus and its features to the covered set; otherwise discard the input.

  6. Go back to step 2.

Coverage decides only which inputs are kept. Whether a run failed is decided by a test oracle §10.5, the check that tells a correct run from a wrong one: a crash, a failed assertion, an exceeded time or memory limit, or an error report from a sanitizer §10.21, a compiler option that adds checks for one class of bug, such as invalid memory accesses.

Two refinements are common. Hit-count ranges: AFL (American fuzzy lop, a fuzzer named after a breed of rabbit) and its successor AFL++ §10.27 sort the number of times an edge ran into the ranges 1, 2, 3, 4–7, 8–15, 16–31, 32–127 and 128 or more, and a new range on an edge counts as a new feature. Comparison feedback: the instrumentation also records the two values of each comparison. libFuzzer’s value profile option counts each new number of differing bits at a comparison as a new feature, so an input that brings the two values closer than any earlier input did is kept before the comparison succeeds.

Worked example

A program reads 3 bytes d0d_0, d1d_1, d2d_2 and crashes only if d0d_0 is F, d1d_1 is U and d2d_2 is Z, tested by three nested if statements. A mutation picks one of the 3 positions and writes one of the 256 byte values there, both uniformly, so it sets a chosen position to a chosen value with probability 1/3×1/256=1/7681/3 \times 1/256 = 1/768. The seed is AAA.

Blind fuzzing. An input of 3 fresh random bytes crashes the program with probability (1/256)3=1/16,777,216(1/256)^3 = 1/16{,}777{,}216. The average number of tries until a first success is the inverse of the success probability: 16,777,216 runs.

Coverage-guided fuzzing. Writing F at position 0 of the seed, with probability 1/7681/768, gives FAA. It takes the true side of the first test and the false side of the second, edges no earlier input took, so it is kept. Writing U at position 1 of FAA, again with probability 1/7681/768, gives FUA, kept for the same reason, and writing Z at position 2 gives FUZ and the crash. Three stages of 768 runs on average make 3×768=2,3043 \times 768 = 2{,}304 runs, about 16,777,216/2,304≈7,28216{,}777{,}216 / 2{,}304 \approx 7{,}282 times fewer than blind fuzzing. A mutation that breaks a correct byte reaches no new edge and is discarded, so progress is never lost. The count assumes that the fuzzer always mutates the newest kept input; a real fuzzer spreads its effort over the corpus, which changes the numbers but not the picture.

Same edges, different behavior. A second function reads a position ii from the input and stores one byte at position ii of a 16-byte array §14.5, whose valid positions are 0 to 15, without checking ii. The inputs i=15i = 15 and i=16i = 16 execute the same edges, because no branch depends on ii, so edge feedback has no reason to prefer either. Yet i=16i = 16 is an out-of-bounds write §4.4. Only the oracle tells the two apart: under AddressSanitizer §10.22, a sanitizer that checks every memory access, the run with i=16i = 16 fails at once; without it, the write may silently change a neighboring variable.

Stated precisely

Let XX be the corpus, the set of kept inputs, and let F(x)F(x) be the set of features observed when the instrumented program runs on input xx. The covered set is U=⋃x∈XF(x),U = \bigcup_{x \in X} F(x), the union of the feature sets of all kept inputs. A new input yy is added to XX exactly when F(y)⊈UF(y) \not\subseteq U, that is, when yy produces at least one feature outside UU. Separately, yy is saved as a failure exactly when the test oracle flags its run.

Two guarantees follow: UU never shrinks during a campaign, and every saved input made the oracle fire at least once on that build. A saved input makes the oracle fire again on a rerun only under two assumptions: the program is deterministic, meaning the same input always produces the same execution; and the rerun uses the same build, options and starting state. Nothing is guaranteed about inputs the fuzzer never ran: there is no bound on the time to find a given bug, and no evidence that a bug is absent.

Limits and common mistakes

Coverage is a search heuristic, not an oracle. A run that executes faulty code without crashing, tripping a sanitizer or failing an assertion is not reported, which is why sanitizers are normally enabled.

More features do not mean more tested behavior: a new edge can be a branch that only writes a log line, and a loop that runs 8 times instead of 7 enters a new hit-count range without doing anything new. Equal features do not mean equal behavior either, as the 16-byte array shows. The Coverage versus correctness §10.40 entry treats this difference in full.

Code that is not instrumented gives no feedback: precompiled libraries, hand-written assembly, other processes and hardware.

Feedback can be lost. AFL keeps its edge counters in a table of 65,536 one-byte entries (64 KiB) and computes each edge’s entry from random numbers that the compiler gives to the edge’s two blocks. Two edges can share an entry, and then the second one never looks new.

A test of 4 bytes against a constant in one instruction is one branch, so getting 3 of the 4 bytes right earns nothing; comparison feedback and input dictionaries help. A checksum §4.32 over the input, such as a CRC (cyclic redundancy check), is harder, because almost every mutation breaks it. Fuzzing builds often skip such checks, and the libFuzzer documentation proposes the macro FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION to mark such fuzzing-only changes; the skipped check then needs tests of its own.

Randomness, timing and threads make features unstable: inputs are kept for features they showed by chance, and saved failures may not reproduce. Instrumentation also slows the program and changes its timing.

Questions answered

Does additional edge coverage §10.19 correspond to additional semantic behavior?

Additional edge coverage does not necessarily correspond to additional semantic behavior, and equal edge coverage does not imply equal behavior. Semantic behavior is the meaning of what the program does: the values it computes, the state it changes and the permissions it grants. Additional edge coverage means that some input executed a transfer of control between two blocks of code that no earlier input had executed. That new control path often brings new behavior, but the new edge may, for example, only write a log line.

Conversely, inputs with identical edges can differ in ways that matter, because behavior also depends on data values, object state and permissions, which edges do not record. Writing at position 15 and at position 16 of a 16-byte array takes the same edges, yet the second write is out of bounds. Treat edge coverage as a measure of the fuzzer’s search progress. Judge behavior with oracles, such as sanitizers, assertions and comparison with a reference implementation, and with an explicit list of the input classes the tests must contain.

Related entries: Coverage metric (§10.18), Edge coverage (§10.19), Test oracle (§10.5), Coverage versus correctness (§10.40), Fuzzing (§10.9), Seed corpus (§10.14), Input dictionary (§10.16), Sanitizer (§10.21), libFuzzer (§10.26), AFL++ (§10.27). Further reading: [24].