Section 10.39

Delta debugging

Definition

Delta debugging is an algorithm §14.1 that automatically reduces a failure-inducing input to a smaller input that still causes the failure. It repeatedly removes parts of the input, runs the test on what remains, and keeps any smaller version on which the test still fails. Andreas Zeller introduced it. Its minimizing form, ddmin, published with Ralf Hildebrandt in 2002 [498], returns a 1-minimal input: removing any single remaining part makes the failure disappear. Applied to code changes instead of an input, the same idea finds which changes cause a failure.

The idea

A fuzzer, a tool that runs a program on very many generated inputs, reports that a 4,000-byte input crashes a parser. Most of those bytes are probably irrelevant, but nobody knows which. A developer who deletes pieces by hand, reruns the program, and keeps each deletion that still crashes is doing delta debugging manually. The algorithm automates this. It needs only the input, split into parts such as bytes, lines or tokens, and a test that answers fail, pass or unresolved. Unresolved means the run says nothing about the failure, for example because the shortened input no longer has a valid format.

Halving the input, as in a binary search, is the natural first try. It breaks down as soon as the failure needs two parts that lie in different halves: each half alone passes, and binary search has nowhere to go. Delta debugging’s answer is to change granularity. When no half fails on its own, it splits the input into quarters and tries removing each quarter in turn, then eighths, and so on. Removing one quarter keeps the other three, so two distant parts can survive together. The algorithm preserves exactly what its test checks and nothing more: a test that asks only “does it crash?” accepts any crash.

How it works

The ddmin algorithm works on the current input cc, a list of parts in their original order, and a number nn of chunks. Write |c||c| for the number of parts in cc.

  1. Start with the whole failing input and n=2n = 2.

  2. If cc has one part, return it: removing that part leaves the empty input, which is assumed below not to fail. Otherwise split cc into nn nonempty chunks of nearly equal size, Δ1,…,Δn\Delta_1, \dots, \Delta_n.

  3. Reduce to a chunk: if the test fails on some chunk Δi\Delta_i alone, make Δi\Delta_i the current input, set n=2n = 2, and go to step 2.

  4. Reduce to a complement: otherwise, if the test fails on some complement c\Δic \setminus \Delta_i, the current input without chunk ii, make that complement the current input, set n=max(n−1,2)n = \max(n - 1, 2), and go to step 2.

  5. Increase granularity: otherwise, if n<|c|n < |c|, set n=min(2n,|c|)n = \min(2n, |c|) and go to step 2.

  6. Otherwise stop and return cc.

Unresolved results count as not failing: the algorithm keeps only inputs on which the test fails. Implementations remember the result for every sublist already tested, because the same sublist comes up again. When one single part causes the failure, the number of tests grows with the logarithm of the input size. In the worst case it grows with the square of the input size.

Related procedures solve neighboring problems. A second algorithm by Zeller and Hildebrandt, called dd, starts from a passing and a failing input and narrows the difference between them, isolating the parts of the difference that matter. Applied to the changes between a working and a failing version of a program, the parts are individual changes. The command git bisect performs a binary search over the history of versions recorded in the version control system Git: it assumes that every version before some point passes and every version after it fails, while delta debugging over a set of changes also handles a failure that needs several changes together. Tools for property-based testing §10.7 perform the same reduction under the name shrinking, using simplifications that know the type of the input, such as replacing a number with a smaller one or a list with a shorter one.

Worked example

A full run of ddmin. The input has 8 parts, numbered 1 to 8. The test fails exactly when parts 3 and 6 are both present; this stands for a crash that needs two separate fields. Results already known are not run again.

  1. n=2n = 2. Chunks {1,2,3,4}\{1,2,3,4\} and {5,6,7,8}\{5,6,7,8\} both pass. With two chunks, each complement equals the other chunk, so nothing new is tested. Since 2<82 < 8, set n=4n = 4.

  2. n=4n = 4. Chunks {1,2}\{1,2\}, {3,4}\{3,4\}, {5,6}\{5,6\}, {7,8}\{7,8\} all pass. The first complement tried, the input without {1,2}\{1,2\}, which is {3,4,5,6,7,8}\{3,4,5,6,7,8\}, fails. It becomes the input, and n=max(4−1,2)=3n = \max(4 - 1, 2) = 3.

  3. n=3n = 3. Chunks {3,4}\{3,4\}, {5,6}\{5,6\}, {7,8}\{7,8\} are known to pass. Complements: {5,6,7,8}\{5,6,7,8\} is known to pass, {3,4,7,8}\{3,4,7,8\} passes, {3,4,5,6}\{3,4,5,6\} fails. It becomes the input, and n=max(3−1,2)=2n = \max(3 - 1, 2) = 2.

  4. n=2n = 2. Chunks {3,4}\{3,4\} and {5,6}\{5,6\} are known to pass, and the complements are the same two sets. Since 2<42 < 4, set n=min(4,4)=4n = \min(4, 4) = 4.

  5. n=4n = 4. Chunks {3}\{3\}, {4}\{4\}, {5}\{5\}, {6}\{6\} all pass. Complements: {4,5,6}\{4,5,6\} passes, {3,5,6}\{3,5,6\} fails. It becomes the input, and n=max(4−1,2)=3n = \max(4 - 1, 2) = 3.

  6. n=3n = 3. Chunks {3}\{3\}, {5}\{5\}, {6}\{6\} are known to pass. Complements: {5,6}\{5,6\} is known to pass, {3,6}\{3,6\} fails. It becomes the input, and n=max(3−1,2)=2n = \max(3 - 1, 2) = 2.

  7. n=2n = 2. Chunks {3}\{3\} and {6}\{6\} are known to pass, and the complements are the same. Since n=|c|=2n = |c| = 2, stop.

The result is {3,6}\{3,6\}. The new test runs per round were 2,5,2,0,6,1,02, 5, 2, 0, 6, 1, 0, a total of 2+5+2+0+6+1+0=162 + 5 + 2 + 0 + 6 + 1 + 0 = 16, compared with the 28=2562^8 = 256 sublists an exhaustive search could try.

A different cause. A 100-byte input crashes a program. Bytes 10 to 13 hold a length field that is too large, so the header parser reads past the end of its buffer and crashes; call this defect X. Further on, bytes 70 to 72 would trigger a null-pointer §4.26 dereference in a later stage, if the program ever got that far; call this defect Y. With the original input, X crashes the program first, and Y is never reached. The test is “the program crashes”. A reduction step that deletes bytes 10 to 13 leaves a header the parser accepts, the program reaches the later stage, and Y crashes it. The test still reports a failure on that smaller input, so delta debugging keeps it, and the final result is a small input that shows Y and not X. The reduced input is real and useful, but it belongs to a different defect. Research on test-case reduction calls this effect slippage. The cure is a narrower test, such as “the program crashes with an out-of-bounds read §4.5 in the header parser”, checked against the crash location or the report of a sanitizer §10.21, a compiler option §12.3 that adds run-time checks for one class of error.

Stated precisely

Let CC be the set of parts of the original failing input. For any subset c⊆Cc \subseteq C, with its parts kept in their original order, let test(c)\mathrm{test}(c) be fail, pass or unresolved. A failing subset cc is 1-minimal when removing any one part leaves a subset on which the test does not fail: test(c\{δ})≠𝑓𝑎𝑖𝑙for every part δ∈c.\mathrm{test}(c \setminus \{\delta\}) \ne \textit{fail} \quad \text{for every part } \delta \in c. A failing subset is globally minimal when no failing subset of CC has fewer parts.

Assume test\mathrm{test} is deterministic, meaning it returns the same result every time for the same subset, and does not fail on the empty input. Then ddmin terminates and returns a 1-minimal failing subset. It terminates because every reduction makes the input strictly smaller, and between two reductions nn only grows, up to |c||c|. For a one-part result the empty-input assumption establishes 1-minimality. For a larger result the final granularity tests every complement that leaves out a single part, and none fails. ddmin does not promise global minimality. A smaller failing subset can exist that its sequence of removals never reaches, and finding the smallest one can, in the worst case, require testing all 2|C|2^{|C|} subsets. Whatever the result, ddmin preserves exactly the property that test\mathrm{test} checks.

Limits and common mistakes

A broad test lets the reduced input fail for another reason, as in the slippage example. Make the test check the identity of the failure: the crash location, the type of error a sanitizer reports, the assertion message, or the property violated.

1-minimal is not smallest. Each step of the reduction records a candidate that was found, such as 100, then 50, then 10 bytes; it does not prove that no 9-byte input fails.

A nondeterministic test §10.38 misleads the reduction. If the failure occurs only in some runs, a correct removal can be rejected because the failure did not show in that run, and an unrelated intermittent failure can make a wrong removal look correct. Make the run deterministic first, or rerun each candidate several times and record which results were uncertain.

A test defined by a timeout §9.34, such as “fails if the run takes longer than 10 s”, can stop failing only because a smaller input runs faster, and it can keep an input that is slow for an unrelated reason.

When the parts are steps of a sequence, such as the messages of a protocol, removing an early step can make later steps invalid or move the program into a different state. Remove whole steps, and have the test answer unresolved for a sequence that is no longer valid. Leftover state between runs, such as a file or an open connection, can also change the result; reset it before every run.

Corpus minimization §10.15 is a different operation: it removes whole inputs from a fuzzing §10.9 corpus while keeping the coverage those inputs reach. Delta debugging shrinks one failing input.

A reduced input is ready to serve as a regression test §10.4 only after a rerun confirms that it shows the intended defect. Keep the original input alongside it.

Questions answered

Can shrinking change the failure’s underlying cause?

Yes. Shrinking, the reduction of a failing input that property-based testing tools perform and that delta debugging performs for general inputs, keeps a smaller input whenever the test still reports a failure, and it preserves exactly what the test checks and nothing more. If the test checks only that the program crashes, a smaller input that crashes through a different defect still counts as failing and is kept. For example, an input crashes because of an out-of-bounds read in a header parser; deleting the bad header bytes lets the program reach a later null-pointer defect, and the reduced input now shows that later defect instead. Research on test-case reduction calls this slippage.

To prevent it, make the test identify the failure, for example by the crash location and the error type a sanitizer reports, the assertion that failed, or the property violated. Reset leftover state between runs, and rerun the final input to confirm it shows the original defect. Report the result for what it is: a 1-minimal input, from which no single part can be removed without losing the failure, not necessarily the smallest possible one.

Related entries: Regression test (§10.4), Fuzz harness (§10.13), Deterministic replay (§10.37), Corpus minimization (§10.15), Property-based testing (§10.7), Fuzzing (§10.9), Sanitizer (§10.21). Further reading: [498].