Section 3.33

Translation validation

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.

  1. Fix the pair. The input SS is the source program or its intermediate representation §3.37 (IR), the compiler’s internal form of the program. The output TT is the final code, exactly as it will run.

  2. Fix the meaning. State the semantics of both languages, including integer widths and overflow rules; the inputs covered; how those inputs are passed to TT 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.

  3. Match the programs. Pair up points of SS and TT, such as function entries, loop heads, calls and returns, and state a relation between their states, for example “variable i of SS is held in register r3 of TT”. Some compilers emit this correspondence as a hint; the validator checks the hint and need not trust it.

  4. 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.

  5. Give a verdict. Valid: every obligation was proved, and TT is used. Invalid: the solver found an input on which SS and TT differ; TT 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; TT 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, SS computes t := x * 2; return t, and the compiler emits TT as return x << 1, a shift of xx left by one bit. The obligation is: for every 8-bit xx, (2x)mod256(2x) \bmod 256, the remainder of 2x2x after division by 256, equals xx shifted left by one bit with only the low 8 bits kept. The solver proves it by finding no xx that breaks it. One instance by hand: for x=200x = 200, 2×200=4002 \times 200 = 400 and 400−256=144400 - 256 = 144. In binary, 200 is 11001000; shifted left it is 110010000, and the low 8 bits 10010000 are 128+16=144128 + 16 = 144.

The same pair, two verdicts. SS is return x + 1 > x on 8-bit signed integers, which hold −128-128 to 127, and the compiler emits TT as return true. For every xx from −128-128 to 126, x+1x + 1 fits and x+1>xx + 1 > x is true, so everything turns on x=127x = 127.

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 TT returns true where SS 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 TT. 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 DD be the set of inputs the claim covers, each an initial state with its arguments and memory contents. For an input xx in DD, let BehS(x)\mathrm{Beh}_S(x) be the set of complete observable behaviors of SS started on xx 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 SS has undefined behavior on xx, BehS(x)\mathrm{Beh}_S(x) is the set of all behaviors. Define BehT(x)\mathrm{Beh}_T(x) in the same way under the target semantics, with xx passed to TT according to the ABI. The validator checks refinement: BehT(x)⊆BehS(x)for every x∈D.\mathrm{Beh}_T(x) \subseteq \mathrm{Beh}_S(x) \quad \text{for every } x \in D. 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 (S,T)(S, T) under four assumptions. (1) The validator’s semantics of SS matches the source language as the compiler reads it. (2) Its semantics of TT 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 SS and TT 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:

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].