Section 3.40

Compiler correctness

Definition

Compiler correctness is the property that the code a compiler produces behaves as the source program’s semantics allows: every observable behavior of the compiled code, such as its input and output, is one that the source language permits for that program. The property is relative to three things that must be named: the semantics of the source language, the semantics of the target machine, and the programs, compiler options §12.3 and outside conventions covered. A verified compiler, such as CompCert for C, proves the property as a mathematical theorem.

The idea

Programmers test and verify source code, but the machine runs compiled code. Every guarantee established on the source reaches the machine only through the compiler, so a compiler that mistranslates a program silently voids the guarantee. Compiler bugs that produce wrong code are real. A study published in 2011 by Yang, Chen, Eide and Regehr [231] generated random C programs with a tool called Csmith and compared how different compilers translated them. It reported that, by early 2011, the development version of CompCert, a C compiler whose main passes are proved correct, was the only compiler tested in which Csmith could find no wrong-code errors.

Correctness here is not a promise that the compiled code follows the source line by line; correct compilers delete, merge and reorder code (the Compiler optimization §3.39 entry). It is a relation between two semantics: whatever the compiled code can be seen to do, the source program was allowed to do. A contract translated into another language is a fair picture. A faithful translation binds the parties to exactly the obligations the original allows. Where the original leaves a choice open, the translation may settle it either way; where a clause of the original is void, the translation owes nothing for that clause. The picture stops at scale: compiler correctness is one statement about every program the compiler accepts, not a judgment made translation by translation.

How it works

A compiler correctness claim has four parts.

  1. Source semantics. A precise definition of what each source program may do, including which executions have undefined behavior §4.12. CompCert defines a semantics for its own version of C, called CompCert C, based on the ISO C99 standard [270].

  2. Target semantics. A precise definition of what machine code does. CompCert defines one for the assembly language of each processor family it supports.

  3. Observations. What counts as behavior. In CompCert, an observable behavior is the trace of calls to input and output functions and of accesses to volatile global variables, together with whether the program terminates, runs forever or stops on undefined behavior. Execution time §9.32 and memory consumption are not observed.

  4. The theorem. It relates the observations of the compiled code to those of the source. CompCert’s manual states it as follows: if the compiler, applied to a source program SS, produces code CC without reporting a compile-time error, then the observable behavior of CC improves on one of the allowed observable behaviors of SS. To improve means to be equal, or to replace a run-time error of the source, such as a division by zero whose result is never used, with a defined behavior.

CompCert’s proof is checked by the Rocq proof assistant (formerly named Coq), a program that checks every step of a mathematical proof. The proof is built pass by pass. Each pass of the compiler, whether it translates one intermediate language §3.37 into the next or optimizes code within one, has its own preservation theorem, and composing these theorems gives the theorem for the whole compiler. The proof does not cover every step from source file to running program: CompCert’s manual lists preprocessing, parts of the front end such as elaboration and some early simplifications, assembling and linking as unverified, and the external assembler and linker §12.4 are not part of CompCert.

Instead of proving a compiler correct once, one can check each compilation after the fact. Translation validation §3.33 compares the source and the produced code of one run and either confirms that they correspond or reports a failure.

Undefined behavior, the ABI §12.47 (application binary interface) and compiler flags fix the scope of the claim.

Worked example

Undefined behavior. Consider int bigger(int x) { return x + 1 > x; } with a 32-bit int, whose largest value is 2,147,483,647. For x = 5, the source computes 5+1=65 + 1 = 6 and 6>56 > 5, so it returns 1. For x = 2,147,483,647, the addition overflows, which is undefined behavior. Compiler A emits an addition and a comparison; on an Arm or x86 processor the addition wraps to −2,147,483,648-2{,}147{,}483{,}648, the comparison fails, and the function returns 0. Compiler B notes that x+1>xx + 1 > x holds for every x at which the addition is defined, and compiles the function to return 1. Both compilers are correct: the source semantics constrains neither of them at x = 2,147,483,647.

A flag changes the source semantics. With -fwrapv, signed addition is defined to wrap, so the source itself now computes 2,147,483,647+1=−2,147,483,6482{,}147{,}483{,}647 + 1 = -2{,}147{,}483{,}648 and returns 0 at that input. Compiler B’s simplification is then incorrect, and a compiler honoring the flag must return 0. The same source text has two different semantics, and a correctness claim must say which one it is about.

The ABI. Under AAPCS, a caller of a function f that computes x+1x + 1 passes x = 5 in r0 and reads the result from r0. Suppose f is replaced by hand-written assembly that computes 5+1=65 + 1 = 6 correctly but leaves the result in r1. The caller reads r0, which still holds the argument, and receives 5. The compiler translated the caller correctly and the assembly computed the right number, yet the program is wrong, because the two pieces do not follow the same ABI. No correctness theorem about the compiler can detect this mismatch.

Stated precisely

Let SS be a source program and CC the code the compiler produces from it. Let Beh(S)\mathrm{Beh}(S) be the set of observable behaviors that the source semantics allows for SS, and Beh(C)\mathrm{Beh}(C) the set that the target semantics allows for CC. An observable behavior is a trace tt of input and output events and volatile accesses, together with how the run ends: termination with a result, running forever, or, for the source, reaching undefined behavior, which the CompCert manual calls going wrong. A target behavior bb improves a source behavior b′b' when b=b′b = b', or when b′b' goes wrong after trace tt and bb begins with that same trace tt.

Correctness, in the form CompCert proves, states: if the compiler produces CC from SS without reporting an error, then every b∈Beh(C)b \in \mathrm{Beh}(C) improves some b′∈Beh(S)b' \in \mathrm{Beh}(S). If no behavior of SS goes wrong, this reduces to Beh(C)⊆Beh(S),\mathrm{Beh}(C) \subseteq \mathrm{Beh}(S), so the compiled code does only what the source allows. A consequence follows for any property of observable behavior: if no behavior of SS goes wrong and every behavior in Beh(S)\mathrm{Beh}(S) has the property, then every behavior of CC has it too. This one-way inclusion is a form of refinement §3.32.

The guarantee rests on named assumptions.

  1. The source semantics matches the language the programmer relied on.

  2. The target semantics matches the real processor.

  3. Every function outside the compiled code, including library functions and hand-written assembly, follows the ABI and behaves as the semantics assumes.

  4. The unverified steps work: preprocessing, the unverified parts of the front end, assembling, linking and loading.

  5. The compiler is used with options inside the theorem’s scope.

  6. The proof assistant that checked the proof is itself correct.

Limits and common mistakes

A verified compiler is not a verified system. The source can implement the wrong rule, a library can break its contract, and the hardware can differ from its manual.

Time and memory are outside the theorem. CompCert’s observable behavior excludes execution time and memory consumption, so deadlines, timing side channels §1.29 and stack overflow §4.10 at run time are not covered.

Correct compilation does not repair source bugs. A source program with undefined behavior on some input has no required behavior on that input, and different correct compilers may produce code that behaves differently there.

Testing a compiler, for example with random programs, finds bugs but proves nothing about the programs not tested. A proof covers every program in its scope, and the scope is the part to read.

A theorem about one compiler covers that compiler’s own output. Linking objects from two compilers, or from one compiler run with two different sets of compiler flags, relies on an ABI agreement that neither compiler’s theorem checks.

Code outside the source semantics is outside the theorem. Inline assembly and hand-written assembly have no meaning in the semantics of C, so a theorem about C compilation says nothing about them.

Compiler correctness says nothing about details that are not observable. A correct compiler may remove an unused read, merge two stores or reorder ordinary memory accesses.

Questions answered

What role do undefined behavior, ABI, and compiler flags play?

Undefined behavior, the ABI (application binary interface) and compiler flags together fix the scope of a compiler correctness claim: which source executions it constrains, which outside code the compiled code may be combined with, and which language and target machine it is about.

Related entries: Refinement (§3.32), Translation validation (§3.33), Guest-host ABI (§2.37), Undefined behavior (UB) (§4.12), Native application binary interface (ABI) (§12.47), Compiler flag (§12.3), Compiler optimization (§3.39). Further reading: [72, 78, 82, 231, 270, 271].