Definition
Translation validation is a method for trusting a compiler’s output without proving the compiler correct. After each run of the compiler, a separate program, the validator, checks that this particular output is a correct translation of this particular input, and the output is used only if the check succeeds. “Correct translation” is a stated semantic relation, usually that every observable behavior of the output is one that the input program allows. Amir Pnueli, Michael Siegel and Eli Singerman introduced the term in 1998.
The idea
An optimizing compiler is large and changes often. A proof that it translates every possible program correctly, as was done for the CompCert C compiler, costs years of work and must be extended after each change. Yet a user needs only the programs actually compiled to be right. Translation validation checks exactly those: it treats the compiler as an untrusted black box and checks each result. Checking an answer is often easier than checking the method that produced it. A teacher does not prove that a student’s method of long division works for all numbers; for each exercise, the teacher multiplies the quotient by the divisor, adds the remainder and compares the result with the dividend. The picture stops at failure. The multiplication always settles a division exercise, while a validator can fail to confirm a correct translation and answer “unknown”.
The validator, much smaller than the compiler, takes the compiler’s place among the components that must be trusted. If the validator is itself proved correct, a validated compiler step is as trustworthy as a proved one. CompCert, a C compiler with a machine-checked correctness proof, uses this combination for some of its steps: an unproved algorithm §14.1, such as its register allocator, computes the result, and a proved validator checks it.
How it works
Validation of one compiler run proceeds in five steps.
Fix the pair. The input is the source program or its intermediate representation §3.37 (IR), the compiler’s internal form of the program. The output is the final code, exactly as it will run.
Fix the meaning. State the semantics of both languages, including integer widths and overflow rules; the inputs covered; how those inputs are passed to under the platform’s application binary interface (ABI §12.47), its rules for argument registers and memory layout; the contracts assumed for external functions; and the observable events to compare.
Match the programs. Pair up points of and , such as function entries, loop heads, calls and returns, and state a relation between their states, for example “variable
iof is held in registerr3of ”. Some compilers emit this correspondence as a hint; the validator checks the hint and need not trust it.Generate and prove obligations. Each obligation says, for one pair of matched segments: if the states are related at the start, they are related at the end, and the same observable events occurred. The validator writes each obligation as a formula, usually over fixed-width bit vectors, and asks an SMT solver §3.28 (satisfiability modulo theories solver, a program that decides such formulas) whether the negation of the formula can be satisfied; if it cannot, the obligation holds for every input.
Give a verdict. Valid: every obligation was proved, and is used. Invalid: the solver found an input on which and differ; is rejected and a compiler bug is reported for that input. Unknown: the validator does not support some feature, the solver timed out, or the matching was too weak; is rejected, or the build falls back to a simpler compilation that does validate.
Worked example
An optimization that validates. On 8-bit unsigned integers, computes t := x * 2; return t, and the compiler emits as return x << 1, a shift of left by one bit. The obligation is: for every 8-bit , , the remainder of after division by 256, equals shifted left by one bit with only the low 8 bits kept. The solver proves it by finding no that breaks it. One instance by hand: for , and . In binary, 200 is 11001000; shifted left it is 110010000, and the low 8 bits 10010000 are .
The same pair, two verdicts. is return x + 1 > x on 8-bit signed integers, which hold to 127, and the compiler emits as return true. For every from to 126, fits and is true, so everything turns on .
If the source language makes signed overflow §4.15 undefined behavior §4.12, as C does for its type
int, then has no defined result at , every behavior of is allowed there, and is a valid translation.If the source language defines wrap-around, then , and is false, so returns false while returns true. The validator reports invalid, with as the counterexample.
The two programs are the same in both cases; only the source semantics differs. This is why a validation claim must name the semantics, and why the relation is refinement §3.32 rather than equality: in the first case returns true where allows anything.
Code changed after the check. A later step, such as the linker §12.4 filling in an address (a relocation §12.50) or a patch that replaces one instruction, changes the bytes of . The verdict was about the bytes before the change. The changed code needs its own check, or a separate argument that the change preserves the relation.
Stated precisely
Let be the set of inputs the claim covers, each an initial state with its arguments and memory contents. For an input in , let be the set of complete observable behaviors of started on under the source semantics. A complete behavior is a sequence of observable events together with how the run ends: a return with a value, a fault, or running forever. If has undefined behavior on , is the set of all behaviors. Define in the same way under the target semantics, with passed to according to the ABI. The validator checks refinement: Equality of the two sets is more than a compiler needs to provide. Refinement lets the compiler resolve undefined behavior and choose among outcomes that the source leaves open.
A verdict of valid proves this relation for this one pair under four assumptions. (1) The validator’s semantics of matches the source language as the compiler reads it. (2) Its semantics of matches the processor. (3) The validator and the solver have no bug that accepts a wrong pair. (4) External functions behave according to the contracts assumed. A verdict of invalid comes with an input on which the two models differ; running and on that input confirms the bug.
Limits and common mistakes
Validation is not compiler correctness §3.40. A valid verdict says nothing about the next program, the next compiler version or other compiler options §12.3.
Rejection is not proof of a bug. Validators are incomplete: a correct translation that uses an unsupported feature, or that is beyond the solver’s power, gets “unknown”. Only an invalid verdict with a confirmed counterexample shows a compiler bug.
Some validators are bounded. Alive2 validates optimizations in the widely used compiler framework LLVM §12.18 (the name began as Low Level Virtual Machine; the project now treats it as a plain name). It unrolls loops a fixed number of times, so its verdict covers only executions within that bound.
The validator is trusted. If the validator and the compiler share a misreading of the language standard, for example because they reuse one semantics library, the check accepts wrong code.
A validator that compares only results ignores timing. A compiler that turns a constant-time selection into a conditional branch preserves every result and can still leak secrets through timing. Only a relation that includes timing, or the branch decisions, among its observable events catches that.
Validate the final artifact. Linking, relocation and patching after the check change the code that runs.
An invalid verdict names an input, not the compiler step that caused the error; finding that step is ordinary debugging.
Questions answered
What semantic equivalence is checked for this specific build?
For a single build, the relation usually checked is refinement rather than strict equivalence. For every input in the covered set, every observable behavior of the produced code must be a behavior that the source program allows under the source language’s rules, so where the source has undefined behavior the output may do anything. A complete answer for one build names:
the two exact artifacts compared, the source or intermediate representation and the final machine code, identified for example by a cryptographic hash §5.1;
the source and target semantics, including integer widths, overflow rules and the memory model;
the inputs covered, and how they are passed to the machine code under the application binary interface, the platform’s rules for arguments and memory layout;
the observable events compared, such as return values, memory writes, calls to external functions with their arguments, faults and termination;
the contracts assumed for external functions;
what is excluded, such as timing, code changed after the check, and executions beyond a loop bound if the validator is bounded.
A valid verdict proves that relation for that build only.
Related entries: Refinement (§3.32), Differential semantics (§3.34), Compiler correctness (§3.40), Intermediate representation (IR) (§3.37), Compiler optimization (§3.39), Undefined behavior (UB) (§4.12). Further reading: [230].