# CompCert

**CompCert** 是一个针对 C 语言较大子集的形式化验证优化编译器，自 2005 年起由泽维尔·勒鲁瓦（法国国家信息与自动化研究所与[法兰西公学院](https://www.edgepedia.org/zh/college-de-france)）及其合作者开发。其显著特性是：用 Rocq/Coq 证明助手构造的机器检验证明，保证它生成的汇编代码行为符合源 C 程序形式语义的规定，从而排除了误编译。<sup>[1](https://compcert.org/man/manual001.html)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/2201.10280)</sup> 它面向生命攸关和任务关键的嵌入式软件，并获得了 2021 年 ACM 软件系统奖。<sup>[1](https://compcert.org/man/manual001.html)</sup><sup> • </sup><sup>[3](https://www.cs.cornell.edu/courses/cs6120/2022sp/blog/compcert/)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一个经过验证的 C 编译器，支持几乎全部 ISO C 2011（以及 ISO C 1999 和 ANSI C），为 PowerPC、ARM、x86 和 RISC-V 处理器生成代码<sup>[1](https://compcert.org/man/manual001.html)</sup><sup> • </sup><sup>[4](https://compcert.org/?trk=public_profile__posts-text)</sup> |
| 保证 | 在 Rocq/Coq 中证明的语义保持定理：若编译成功，生成代码的可观察行为改进源程序某一允许行为<sup>[1](https://compcert.org/man/manual001.html)</sup> |
| 验证覆盖范围 | 编译器约 90% 的算法，包括全部优化和代码生成，均已证明正确；细化处理、预化简、汇编和链接则未验证<sup>[1](https://compcert.org/man/manual001.html)</sup> |
| 流水线 | 经过验证的核心从 CompCert C 抽象语法到 Asm 抽象语法，途经 8 种中间语言和 15 个编译遍<sup>[1](https://compcert.org/man/manual001.html)</sup> |
| 性能 | 运行速度通常约为 gcc -O0 的两倍，比 gcc -O1 慢约 10%；在 ARM 64 位上约为 GCC 11 在 -O1 时的 90%<sup>[1](https://compcert.org/man/manual001.html)</sup><sup> • </sup><sup>[5](https://www.cs.utexas.edu/~bornholt/courses/cs345h-22au/papers/compcert.pdf)</sup> |
| 测试记录 | 在 Csmith 随机测试研究中，CompCert 是唯一一个 Csmith 未能发现生成错误代码缺陷的被测编译器<sup>[6](https://www.cs.princeton.edu/techreports/2020/006.pdf)</sup> |
| 当前版本 | 3.18 版，2026 年 8 月发布<sup>[7](https://compcert.org/download.html)</sup> |

## 误编译问题

有缺陷的编译器可能悄无声息地把缺陷植入正确的程序，这种现象称为误编译。CompCert 文档中引用的实证研究报告了普遍存在的缺陷：Nullstone 一致性测试套件在 1995 年发现，二十个商业编译器中有十二个存在整数除法缺陷；艾德和雷格尔发现十三个生产级 C 编译器每一个都对 volatile 内存访问生成了错误代码；杨及其同事使用 Csmith 随机程序生成器，报告了超过 325 个此前未知的编译器缺陷，并发现每个被测编译器都可能崩溃或在有效输入上静默生成错误代码。<sup>[1](https://compcert.org/man/manual001.html)</sup>

对于日常软件，这只是小麻烦，但对于安全攸关或任务关键的系统，它迫使人们对生成的汇编进行昂贵的额外测试和审查。误编译还会削弱源码级形式化验证：静态分析器或程序证明器确立了 C 源代码的性质，而有缺陷的编译器可能使这些保证在可执行文件中失效。<sup>[1](https://compcert.org/man/manual001.html)</sup>

## 语义保持

CompCert 的正确性陈述是一个语义保持定理：对于所有源程序 S 和生成的代码 C，如果编译器作用于 S 产生 C 且未报告编译期错误，那么 C 的可观察行为改进 S 的某一允许可观察行为。<sup>[1](https://compcert.org/man/manual001.html)</sup> 可观察行为遵循 ISO C 标准，包括终止或发散（以及终止是正常终止还是通过运行时错误终止）、对标准库函数的所有输入/输出调用，以及对 volatile 全局变量的所有读写；执行时间和内存消耗不包括在内。<sup>[1](https://compcert.org/man/manual001.html)</sup>

该定理内建了三个限定条件。编译器可能在编译期失败，例如出现类型错误或内部容量超限时（CompCert 拒绝局部变量超过 4 Gb 的函数）。它可能选择非确定性源程序的某一可能行为，例如表达式求值顺序。它还可能改进行为，当错误计算被优化掉时，将运行时错误转换为更明确的行为；如果已知源程序没有运行时错误，这种情况永远不会发生，行为被精确保持。<sup>[1](https://compcert.org/man/manual001.html)</sup>

一个推论对经过验证的软件很重要：如果一个可靠的源码级验证工具为程序建立了规约 Σ，而 CompCert 编译了该程序，那么编译后的代码也满足 Σ。因此，形式化验证编译使得源码级保证能够贯穿到可执行文件。<sup>[1](https://compcert.org/man/manual001.html)</sup>

证明被拆分为 15 个独立的语义保持证明，每个编译遍一个，组合成最终定理。证明在 Rocq 证明器（原名 Coq）中进行，该证明器以交互方式构造证明，然后自动重新检查，赋予该项目所描述的对其有效性的近乎绝对的信心。<sup>[1](https://compcert.org/man/manual001.html)</sup>

## 编译器的结构

编译分四个阶段进行。预处理由外部 [C 预处理器](https://www.edgepedia.org/zh/c-preprocessor)执行。解析、类型检查和细化处理产生带类型的 CompCert C 抽象语法树，沿途应用一些可选的源到源化简。经过验证的编译本体随后经过 8 种中间语言和 15 个编译遍，最终到达 Asm——一种汇编抽象语法，存在五个版本，每个支持的架构一个。最后，外部汇编器和链接器生成可执行文件。<sup>[1](https://compcert.org/man/manual001.html)</sup>

只有阶段 3 和阶段 2 的解析器在 Rocq/Coq 中形式化并证明；预处理缺乏数学规约，汇编和链接是外部工具。编译器约 90% 的算法，包括所有优化和所有代码生成，经过验证；其余 10%，包括细化处理和预化简，则未验证。对于未验证的汇编和链接步骤，AbsInt 的 Valex 工具提供可执行文件的事后验证。<sup>[1](https://compcert.org/man/manual001.html)</sup>

并非每个经过验证的组件都直接证明。寄存器分配的图着色步骤用未验证的 OCaml 实现，并由形式化验证的验证器守护：仅当着色满足正确性条件（冲突的临时变量获得正确寄存器类别的不同位置）时才被接受，否则编译失败。<sup>[8](https://www.cs.cornell.edu/courses/cs6120/2019fa/blog/comp-cert/)</sup>

**优化。** 经过验证的编译遍包括图着色寄存器分配和迭代寄存器合并、利用 PowerPC“旋转与掩码”和 x86 寻址模式等组合指令的指令选择、常量传播、公共子表达式消除、死代码消除、函数内联、尾调用消除，以及用条件移动替代分支的 if 转换。不执行循环优化。<sup>[1](https://compcert.org/man/manual001.html)</sup>

## 性能

在最初针对 2 GHz PowerPC 970 上 GCC 4.0.1 的基准测试中，CompCert 生成的代码运行速度是未优化的 GCC 所生成代码的两倍多，平均比 gcc -O1 慢 7%，比 gcc -O2 慢 12%；编译时间在 gcc -O1 的两倍以内。<sup>[5](https://www.cs.utexas.edu/~bornholt/courses/cs345h-22au/papers/compcert.pdf)</sup> 后来的记述给出在 PowerPC 和 ARM 上比 GCC 4 在 -O1 时慢约 10%，-O2 时慢 15%，-O3 时慢 20%。<sup>[9](https://www.researchgate.net/publication/293814383_CompCert_-_A_Formally_Verified_Optimizing_Compiler)</sup> 当前手册报告，在 ARM 64 位上，CompCert 达到 GCC 11 在优化级别 1 时约 90% 的性能，并将矩阵密集代码上较弱的结果归因于缺少激进的[循环优化](https://www.edgepedia.org/zh/loop-optimization)。<sup>[1](https://compcert.org/man/manual001.html)</sup>

这些数字被认为对于关键嵌入式代码是足够的，因为正确性保证才是重点；项目自己的评估是 CompCert 并不面向高性能计算。<sup>[5](https://www.cs.utexas.edu/~bornholt/courses/cs345h-22au/papers/compcert.pdf)</sup> 后来的工作缩小了某些目标上的差距：一个修改过的 CompCert RISC-V 分支比主线版本平均性能提高约 30%，尽管不同基准测试的结果差异很大。<sup>[10](https://doi.org/10.1007/978-3-031-38828-6_3)</sup>

## 测试记录

使用 Csmith 的随机[差分测试](https://www.edgepedia.org/zh/differential-testing)为证明提供了独立检查。Csmith 作者报告，在所有其他编译器中发现的中端缺陷在 CompCert 中不存在，并且截至 2011 年初，开发中的版本是他们测试过的唯一一个 Csmith 在花费大约六个 CPU 年后仍无法发现生成错误代码缺陷的编译器。<sup>[1](https://compcert.org/man/manual001.html)</sup> 该研究确实在未验证的前端发现了几个缺陷，这些缺陷已修复，前端经过验证的部分后来扩展到包括经过验证的解析器；还发现了一个可追溯到对 PowerPC 语义误解的独立缺陷。<sup>[6](https://www.cs.princeton.edu/techreports/2020/006.pdf)</sup> 后来的测试工具如 Orion 报告了额外问题，主要是前端 OCaml 代码中的崩溃失败，而非来自已验证部分的静默误编译。<sup>[8](https://www.cs.cornell.edu/courses/cs6120/2019fa/blog/comp-cert/)</sup>

## 支持的语言和目标

CompCert 接受几乎全部 ISO C 2011、ISO C 1999 和 [ANSI C](https://www.edgepedia.org/zh/ansi-c)。它支持全部 ISO C 99，但有以下例外：switch 语句默认必须按照 MISRA-C 的结构编写（非结构化 switch 可通过选项使用），不支持变长数组，longjmp 和 setjmp 不保证工作。因此它覆盖整个 MISRA-C 2004 子集，外加 MISRA-C 排除的特性，如递归和动态分配。还支持选定的 C 2011 特性（_Alignof、_Alignas、匿名结构和联合、_Static_assert、_Generic、Unicode 字面量）以及 GNU 和 Diab 扩展，包括带 GNU 风格参数的内联汇编。<sup>[1](https://compcert.org/man/manual001.html)</sup>

代码生成器存在于 PowerPC（32 位指针模式下 32 和 64 位）、ARM v6 到 v8 的 32 位模式带 VFP、AArch64、x86 的 32 位模式（需要 [SSE2](https://www.edgepedia.org/zh/sse2)）和 64 位模式，以及 RISC-V 的 32 位和 64 位模式。<sup>[1](https://compcert.org/man/manual001.html)</sup> CompCert 力求 ABI 一致性，使其输出可与其他编译器的代码链接，并记录了剩余的不兼容性，特别是在多个架构上 long double 映射为 64 位浮点而 ABI 要求 80 或 128 位值，以及某些目标上对 struct 和 union 参数的不同处理。<sup>[1](https://compcert.org/man/manual001.html)</sup>

## 历史、商业化与衍生

泽维尔·勒鲁瓦在法国国家信息与自动化研究所实现了 CompCert 并在 Coq 中证明其正确，从正确性证明中提取编译器；早期统计将实现和证明合计为 42,000 行代码和三人年的努力。<sup>[11](https://pmc.ncbi.nlm.nih.gov/articles/PMC5597724/)</sup> 后来的描述将证明工作定为约 100,000 行 Coq 和 6 人年，是当时用证明助手完成的最大证明之一。<sup>[9](https://www.researchgate.net/publication/293814383_CompCert_-_A_Formally_Verified_Optimizing_Compiler)</sup> 2014 年 6 月，法国国家信息与自动化研究所与德国萨尔布吕肯的 AbsInt Angewandte Informatik GmbH 签署许可协议，以营销和支持该编译器。<sup>[12](https://radar.inria.fr/rapportsactivite/intranet/PDF-2014/resultats-tri-Rocquencourt.pdf)</sup> 2010 年代中期，根据 DO-178 标准进行航空使用资格认证的工作正在进行，CompCert 被评估用于 A 级飞行控制软件，其证明可以取代生成与源结构相似的非优化汇编的做法。<sup>[11](https://pmc.ncbi.nlm.nih.gov/articles/PMC5597724/)</sup><sup> • </sup><sup>[13](https://drops.dagstuhl.de/entities/document/10.4230/OASIcs.PPES.2011.59)</sup> 2021 年它获得了 ACM 软件系统奖。<sup>[3](https://www.cs.cornell.edu/courses/cs6120/2022sp/blog/compcert/)</sup>

可信计算基包括源语言和目标语言的形式语义、用于构建 CompCert 本身的 Coq 提取机制和 OCaml 编译器，以及 Coq 证明助手的内核。<sup>[6](https://www.cs.princeton.edu/techreports/2020/006.pdf)</sup> 一系列研究衍生版本扩展了核心结果：用于并发计算的 CompCertTSO，组合正确性变体如 SepCompCert、CompCompCert、CompCertX 和 CompCertM，栈空间边界的保持，以及密码学常数时间行为的保持。<sup>[6](https://www.cs.princeton.edu/techreports/2020/006.pdf)</sup> 最近的版本继续扩大验证范围，增加了形式化验证的类型检查器、x86-64、AArch64 和 RISC-V 上的位置无关代码和可执行文件，以及改进的 DWARF 调试信息。<sup>[4](https://compcert.org/?trk=public_profile__posts-text)</sup> CompCert 还证明了这种规模的验证是可行的，引发了人们对形式化验证软件的更广泛兴趣；美国 DARPA HACMS 计划将其列为可用于构建高保障系统的验证工件之一。<sup>[3](https://www.cs.cornell.edu/courses/cs6120/2022sp/blog/compcert/)</sup><sup> • </sup><sup>[11](https://pmc.ncbi.nlm.nih.gov/articles/PMC5597724/)</sup>

## References

1. [CompCert C: a trustworthy compiler, CompCert user's manual, version 3.18 (Xavier Leroy)](https://compcert.org/man/manual001.html)
2. [The Trusted Computing Base of the CompCert Verified Compiler, arXiv:2201.10280](https://arxiv.org/abs/2201.10280)
3. [Formal Verification of a Realistic Compiler, CS 6120 course blog, Cornell University](https://www.cs.cornell.edu/courses/cs6120/2022sp/blog/compcert/)
4. [CompCert main page](https://compcert.org/?trk=public_profile__posts-text)
5. [Leroy, Formal verification of a realistic compiler, Communications of the ACM 52(7), 2009](https://www.cs.utexas.edu/~bornholt/courses/cs345h-22au/papers/compcert.pdf)
6. [Verified Optimizations for Functional Languages, Princeton University technical report TR-2020-006](https://www.cs.princeton.edu/techreports/2020/006.pdf)
7. [CompCert downloads](https://compcert.org/download.html)
8. [CompCert: Formally Verified C Compiler, CS 6120 course blog, Cornell University (2019)](https://www.cs.cornell.edu/courses/cs6120/2019fa/blog/comp-cert/)
9. [CompCert: A Formally Verified Optimizing Compiler](https://www.researchgate.net/publication/293814383_CompCert_-_A_Formally_Verified_Optimizing_Compiler)
10. [Testing a Formally Verified Compiler](https://doi.org/10.1007/978-3-031-38828-6_3)
11. [The HACMS program: using formal methods to eliminate exploitable bugs](https://pmc.ncbi.nlm.nih.gov/articles/PMC5597724/)
12. [Inria Project-Team GALLIUM, Activity report 2014](https://radar.inria.fr/rapportsactivite/intranet/PDF-2014/resultats-tri-Rocquencourt.pdf)
13. [França et al., Towards Formally Verified Optimizing Compilation in Flight Control Software, OASIcs PPES 2011](https://drops.dagstuhl.de/entities/document/10.4230/OASIcs.PPES.2011.59)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains › Compiler implementations

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/compcert

English original: https://www.edgepedia.org/compcert
