Definition
Differential testing is a testing method that runs two or more implementations of the same specification on the same input and compares their results. A specification is the document that defines the correct behavior. If the results differ in a way the specification does not allow, at least one implementation, or the specification, is wrong. Each implementation thus serves as the others’ test oracle §10.5, the check that decides whether a result is correct. With inputs produced automatically by a fuzzer, the method is called differential fuzzing.
The idea
Working out the correct result is often the hardest part of a test. A fuzzer, a tool that produces test inputs automatically, can write a million random C programs, but nobody can say by hand what each of them must print. Several independent C compilers exist, however, and if all of them are correct, their builds of the same well-defined program print the same output. A program on which two compilers disagree points at a bug in one of them, found without knowing the right answer. The same works for two decoders of one file format, or for an interpreter §2.13, which executes a program one instruction at a time, and a just-in-time (JIT §2.14) compiler, which translates the program into the processor’s machine code just before running it. Some testing glossaries call the method back-to-back testing.
Picture two clerks who compute the same tax return independently. If their totals differ, at least one of them has made a mistake, and the rule book says which. The picture fails in two places. Two clerks taught the same wrong rule agree and are both wrong, so agreement is weaker evidence than disagreement. And where the rules leave a choice open, such as how to round, two different totals can both be correct. Differential testing therefore starts by stating which results the specification fixes and which it leaves open.
How it works
A differential test has six parts.
Implementations. Choose two or more implementations of one specification, or two modes of one implementation, such as the interpreter and the JIT compiler of one runtime. Note what they share: copied code or a common library makes them fail alike.
Common domain. Use only inputs for which the specification defines the result and every implementation claims to support every feature the input uses. Other inputs are either never generated or set aside in a class of their own, as the answer below explains.
Observation. Compare everything the run may affect: the returned value, the output bytes, changed memory or storage, and whether the run ended normally or with an error. Comparing only the exit status §13.24, the number a program returns when it ends, misses wrong outputs.
Normalization. Remove what the specification allows to differ, such as timestamps, memory addresses and the order of items the specification leaves unordered, and convert every result to one byte order §4.21 and one integer width.
Comparison rule. State when two observations count as the same: equal bytes, equality within a stated tolerance for floating-point numbers §4.49, or the same class of error.
Triage. Run each input on every implementation from the same initial state. Classify each difference: a bug in one implementation; a bug in the harness, the test code that runs the implementations and collects their results; an input outside the common domain; or an unclear specification. Shrink the input, for example by delta debugging §10.39, which removes parts of an input while the difference persists, and keep it as a regression test §10.4, a test that catches the bug if it returns.
Each difference is resolved against the specification. A majority vote among three or more implementations is only a first guess, since the two that agree can share a bug. Csmith, a generator of random C programs described by Yang, Chen, Eide and Regehr in 2011, shows the common-domain rule at work: it avoids every construct whose behavior the C standard leaves undefined or unspecified [78], each generated program prints a checksum §4.32 (a short value computed from data) of its variables when it ends, and compilers whose builds print different checksums are examined further.
Worked example
Two runtimes, A and B, execute programs for eBPF §2.1 (extended Berkeley Packet Filter), an instruction set for small programs that run inside a virtual machine §2.3, a computer defined in software. The harness compares the 64-bit value in register r0, which holds the result of an eBPF program, and whether the run ended normally.
Program 1 puts 4,294,967,295, the largest unsigned 32-bit value, in register r0, adds 1 with a 32-bit addition, and stops. The eBPF instruction set computes 32-bit arithmetic modulo and sets the upper 32 bits of the register to zero. The expected result is therefore , and , where is the remainder of divided by . Runtime A returns 0; runtime B returns 4,294,967,296. The specification shows that B is wrong: B added in 64 bits and never cut the result back to 32 bits.
Program 2 divides 7 by 0 as unsigned 64-bit numbers. RFC 9669, the standard that defines the eBPF instruction set, says that a division by zero sets the destination register to 0. Runtime A returns 0; runtime B stops with the error “division by zero”. If the documentation of B is silent on this, B violates the standard. If it states that B rejects division by zero on purpose, the case is intentionally unsupported by B: it moves to a separate class whose expected outcome is a clean error from B on every run.
Program 3 is a C program built by two C compilers. It sets a variable x of type int, 32 bits wide here, to its largest value, 2,147,483,647, and prints the value of the comparison x + 1 > x. The build from compiler A prints 0: its addition wrapped around to , which is less than x. The build from compiler B prints 1: compiler B replaced the comparison by “true”, because signed integer overflow §4.15 is undefined behavior §4.12 in C, so a compiler may assume that it never happens. The C standard imposes no requirement on this program. Both outputs conform, the difference is not a bug, and the program lies outside the common domain.
Stated precisely
Let be the implementations under test, with . For an input , let be the normalized observation of implementation when it runs from the agreed initial state. Let be the set of outcomes that the specification allows for . Write when observations and pass the comparison rule, that is, count as the same. The common domain is the set of inputs for which the specification defines and every implementation claims to support everything uses.
Call the comparison rule faithful when any two allowed outcomes pass it: whenever and both belong to , . Then for every in and every pair : The reason is short: if both outcomes were allowed, faithfulness would make them pass the comparison. The converse fails: is possible with both outcomes outside .
The implication rests on four assumptions: the input lies in ; the comparison rule is faithful, which needs the normalization to remove every difference the specification allows; each observation is recorded correctly, so that a harness error such as reading a result in the wrong byte order creates no false difference; and every implementation starts from the same initial state and runs in the same environment. If the second or third assumption fails, a reported difference can be a false alarm §1.22. Inputs that were not run get no result at all.
Differential fuzzing of eBPF runtimes: BpfChecker
Several independent eBPF runtimes exist, and many contain both an interpreter and a JIT compiler, which makes eBPF a natural target for differential fuzzing. BpfChecker, presented by Peng, Jiang, Wu and Zhou at the 2024 Association for Computing Machinery Conference on Computer and Communications Security, is a differential fuzzing framework for eBPF runtimes [488]. It generates eBPF programs, runs each one on several runtimes, and compares what the paper calls the critical states of the runs. To keep its programs valid enough to be accepted and executed, it builds them in a simplified program form of its own and mutates them under constraints, guided by the error messages that the runtimes report. The authors evaluated it on three runtimes. Two are versions of rbpf, an eBPF virtual machine written in the Rust programming language: the original, and the modified copy that the Solana blockchain uses to run the programs stored on it. Despite the similar name, rbpf is unrelated to rBPF §2.17, the eBPF interpreter of the RIOT operating system for microcontrollers §6.1. The third is the virtual machine at the core of eBPF for Windows. The authors report 28 new implementation flaws. The published code also compares the interpreter mode with the JIT mode of one runtime.
In such a campaign the test oracle is the set of other runtimes: when two runtimes end a program in different states, at least one of them has a flaw, provided the program lies in the common domain. Agreement shows much less. A flaw that all runtimes share, such as the same misreading of the specification, produces identical wrong states. Memory errors stay unseen unless the harness also watches for them, for example with a sanitizer §10.21, a set of checks that the compiler adds to a program to report invalid memory accesses. And programs that the generator never produced are not tested at all.
Limits and common mistakes
Agreement is not correctness. Implementations that share code, a library or a misreading of the specification fail alike and agree; this is a common-mode error §19.2.13. Two parsers that omit the same bounds check, the test that a position lies inside a buffer, agree on every input that overflows both of them.
With two implementations, a difference does not say which one is wrong. A majority among three or more is a guess, not a verdict.
Only differences are found. A requirement that no implementation meets, or a feature that none has, produces no difference.
Many differences are harness errors: results read in different byte orders, a C long that is 64 bits wide on the test computer and 32 bits wide on the target device, or uninitialized memory with different leftover values in each run. Check the harness before blaming an implementation.
A narrow observation hides bugs. Two runtimes that return the same value but write different bytes to storage agree when only return values are compared.
Speed is not compared unless the specification states a bound; a runtime ten times slower than another shows no difference in this sense.
Questions answered
How are undefined or intentionally unsupported cases treated?
Undefined and intentionally unsupported cases are kept out of the bug count of a differential test: the input generator either never produces them, or each such input is recorded in a separate class with its own expected outcome. Differential testing runs several implementations of one specification on the same input and treats a difference between their results as a sign of a bug. That inference holds only where the specification defines the result and every implementation claims support, a region called the common domain.
On an undefined case, such as signed integer overflow in C, the specification imposes no requirement. Any outcome conforms, so a difference proves nothing. Csmith, a random generator of C programs for testing compilers, therefore produces only programs free of undefined and unspecified behavior §4.14. Some checks still apply to an undefined case: a compiler must not crash while compiling the program, and a runtime that promises to protect its host must not let any program crash the host.
An intentionally unsupported case uses a feature that one implementation documents as absent, for example an instruction that one runtime refuses to execute. Its expected outcome is a clean, documented refusal by that implementation; a crash, a hang or a silently wrong result is still a bug. Differences inside the common domain are resolved against the specification and independent evidence, such as a result computed by hand, never by declaring one implementation correct by default.
Related entries: Test oracle (§10.5), Undefined behavior (UB) (§4.12), Unsigned modular arithmetic (§4.16), Integer promotion and conversion (§4.18), Differential semantics (§3.34), Undefined versus unsupported semantics (§3.35), Semantic oracle independence and common-mode errors (§19.2.13), Just-in-time compilation (JIT) (§2.14). Further reading: [78, 160, 231, 488].