Definition
Differential semantics is the practice of checking what a language or instruction set means by running two implementations of it, called engines, on the same programs and initial states, and comparing their observable results under a stated comparison rule. Typical pairs are two interpreters §2.13, an interpreter and a just-in-time compiler §2.14, or an implementation and an executable specification. When the rule says that two results must match and they do not, at least one engine is wrong or the specification is unclear. Agreement proves nothing beyond the cases run.
The idea
Checking an engine against expected answers needs a test oracle §10.5, a mechanism that decides whether a result is correct, and writing expected answers by hand is slow. Two engines meant to have the same semantics act as each other’s oracle for any input, including millions of randomly generated programs. William McKeeman described this approach as differential testing §10.6 in 1998. A 2011 study using Csmith, a generator of random C programs, found hundreds of previously unknown bugs in C compilers by comparing their outputs. The name differential semantics is used here for differential testing aimed at the meaning of each instruction or language construct.
Two clocks that disagree prove that at least one of them is wrong, but not which one. To know which, compare with a reference trusted for an independent reason, such as a broadcast time signal, not with the clock that is older or more popular. Two clocks that agree may both be wrong in the same way, for example if both were set from the same wrong source. The picture stops where a specification allows several answers: two engines may then give different results and both be right.
How it works
A differential campaign runs in six steps.
Align the inputs. Give both engines the same program bytes, the same initial registers and memory, and the same environment: external functions replaced by identical stand-ins with fixed answers, a fixed clock, fixed random seeds, and one thread or a fixed schedule.
Choose what to observe: the returned value, the final registers, the contents of chosen memory regions, whether execution faulted and with which fault, the sequence of external calls with their arguments, and whether the program stopped within a step limit §2.32.
Choose the comparison rule. Use equality where the specification fixes one result. Use “both results are allowed” where it permits several, which is unspecified behavior §4.14. Where the specification gives the program no meaning at all, which is undefined behavior §4.12, either leave such programs out or record that any result is acceptable.
Generate programs, by random generation, by mutating existing programs, or by coverage-guided fuzzing §10.12, which keeps the inputs that reach new code.
Analyze each mismatch. Shrink the program to a smallest one that still shows the difference, for example with delta debugging §10.39. Run both engines one instruction at a time and find the first instruction after which their states differ. Read what the specification says for that instruction with those operand values, and classify the case: engine A wrong, engine B wrong, both wrong, or the specification silent or ambiguous.
Keep the shrunk program as a regression test §10.4 once the fault is fixed.
Worked example
The specification decides. An instruction set has 8-bit registers, and its specification says that addition keeps the remainder after division by 256. Register r0 holds 255, and the program adds 1 to it. Engine A reports 0. Engine B computes with the host’s 64-bit integers, never reduces the result, and reports 256. The specification gives , so A is right and B is wrong. Without the specification, the mismatch shows only that at least one of them is wrong.
Hardware disagrees too. A 32-bit register holds 1, and the program shifts it left by the amount held in another register, 33. Engine A is a just-in-time compiler for x86-64 processors and uses their native shift, which takes the amount modulo 32: , so the result is 2. Engine B is a just-in-time compiler for the 32-bit Arm instruction set and uses its native shift, which takes the low 8 bits of the amount, here 33, and gives 0 for any amount of 32 or more; the result is 0. A third engine, an interpreter written in C, computes x << 33 on a 32-bit type, which is undefined behavior in C, so its result depends on the C compiler that built it. If the specification says that the amount is taken modulo 32, A is right, B is wrong, and the interpreter has a defect even when its output happens to be 2. If the specification leaves shifts by 32 or more undefined, no engine is wrong, and such programs must be left out of the comparison. If the specification says nothing, the specification has a defect, and it must be fixed before any engine can be judged.
The observation set matters. Both engines return 1. Engine A leaves the memory byte at offset 5 holding 9, as the specification requires; engine B sets it to 0. A comparison of return values passes. A comparison that includes memory finds the mismatch.
Agreement is not correctness. Engines A and B share one instruction decoder, which reads a 16-bit constant field of an instruction as an unsigned number. The specification says the field is signed, so the bit pattern 0xffff, sixteen ones, means . An instruction that adds this constant to a register holding 10 should give . Both engines give : they agree, and both are wrong. Only a reference written independently from the specification exposes the shared mistake.
Stated precisely
Let be a program, an initial state, and the record of observations that engine produces when it runs from , with the fields chosen for the campaign. Let be the set of records that the specification permits; it has one element when the specification fixes the result. Engine is correct on when .
A comparison rule is a relation between records. Call it faithful to the specification when any two records in are related by ; for a specification that fixes one result, equality is faithful. With a faithful rule:
if , the two records are not both in , so at least one engine is wrong on ;
if , nothing follows about correctness, because both records may lie outside in the same way.
Every conclusion covers only the pairs actually run, and only the recorded fields. Equivalence of two engines on all programs needs a proof, such as a refinement §3.32 proof or translation validation §3.33, not a campaign.
The trusted oracle is therefore the specification, not either engine. It is most useful in executable form: a reference interpreter or formal model written directly from the specification text, sharing no code with the engines under test, and checked against the text independently of them.
Limits and common mistakes
Agreement can have a common cause: shared code such as a decoder or a library, a shared misreading of the specification, or the same authors. A majority of three engines built on the same misreading is still wrong; the common-mode errors §19.2.13 that this produces are invisible to any comparison among them.
A campaign covers only what its generator produces. Random programs rarely contain particular constants, deep nesting or rare instruction sequences, and a loader that checks programs before running them may reject most random ones.
Uncontrolled nondeterminism, such as real clocks, unseeded randomness or thread scheduling, produces mismatches that are not bugs. Control it, or compare against the set of allowed outcomes.
Programs with undefined behavior have no correct result. Comparing engines on them produces false alarms §1.22, or hides real bugs when one engine’s accidental result is taken as the reference. Csmith avoids generating such programs for this reason.
Effects that are not recorded are not checked: memory outside the compared regions, timing, and other side channels §1.29.
A mismatch says that something is wrong, not what. The first divergent instruction and the specification text decide.
Questions answered
Which engine supplies the trusted oracle when they disagree?
Neither engine supplies the trusted oracle by default: the specification of the language or instruction set does, and an engine is right exactly when its result is one the specification allows for that program and initial state. Being older, simpler, more widely used or in the majority is evidence, not proof, because engines can share code or share a misreading of the specification. To settle a disagreement, shrink the program to a smallest one that still disagrees, run both engines one instruction at a time to find the first instruction where their states differ, and read what the specification says for that instruction with those operand values. An executable reference, meaning an interpreter or formal model written directly from the specification, checked independently and sharing no code with either engine, may serve as the oracle in automated comparison. If the specification is silent or ambiguous at that point, the disagreement exposes a defect in the specification, which must be fixed before either engine can be called wrong.
Related entries: Interpreter semantic fidelity (§2.38), Translation validation (§3.33), Undefined versus unsupported semantics (§3.35), Test oracle (§10.5), Differential testing (§10.6), Unspecified behavior (§4.14). Further reading: [231].