# 形式化验证

形式化验证是计算机科学中一类数学技术的总称，用于证明或检查系统的设计或实现是否满足以形式化语言规定的性质。存在两种成熟的方法：模型检测，即针对某个时序逻辑规格穷尽式地探索系统的有限模型；以及定理证明，即构造一个逻辑证明来表明系统满足其规格。<sup>[1](https://www.cs.cmu.edu/~emc/papers/Papers%20In%20Refereed%20Journals/Formal%20Methods_State%20of%20the%20Art%20and%20Future%20Directions.pdf)</sup> 当模型检测的性质不成立时，工具会给出一个反例执行，指出问题的来源。<sup>[2](https://www-verimag.imag.fr/PEOPLE/Joseph.Sifakis/TuringAwardPaper-Apr14.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 两种主要方法 | 模型检测与定理证明<sup>[1](https://www.cs.cmu.edu/~emc/papers/Papers%20In%20Refereed%20Journals/Formal%20Methods_State%20of%20the%20Art%20and%20Future%20Directions.pdf)</sup> |
| 失败时的输出 | 一个反例：一个初始状态以及一组通向错误状态的迁移<sup>[3](https://www.cs.cmu.edu/~emc/papers/Conference%20Papers/Model%20Checking%20and%20the%20State%20Explosion%20Problem.pdf)</sup> |
| CTL 模型检测代价 | \( O(\lvert M \rvert \cdot \lvert f \rvert) \)，关于状态图规模与公式规模均为线性<sup>[4](https://www.cs.unc.edu/~stotts/COMP590-059-f21/slides/clarke-turing.pdf)</sup> |
| 已分析的最大状态空间 | 约 \( 10^{120} \) 个状态，远超可观测宇宙中大约 \( 10^{80} \) 个原子<sup>[5](https://amturing.acm.org/award_winners/clarke_1167964.cfm)</sup> |
| seL4 证明规模 | 20 万行 Isabelle 脚本，约 20 人年的工作量<sup>[6](https://dl.acm.org/doi/10.1145/1629575.1629596)</sup> |
| 最常用的模型检测技术 | 借助快速 SAT 求解器对硬件进行有界模型检测<sup>[2](https://www-verimag.imag.fr/PEOPLE/Joseph.Sifakis/TuringAwardPaper-Apr14.pdf)</sup> |
| 自动化极限 | 没有任何工具能够对任意程序和任意规格，在有限时间内自动给出既不漏报也不误报的答案<sup>[7](https://link.springer.com/chapter/10.1007/978-3-031-71177-0_24)</sup> |

## 工作原理

模型检测依赖于为系统建立一个有限模型，并对照规格对其进行检查。<sup>[1](https://www.cs.cmu.edu/~emc/papers/Papers%20In%20Refereed%20Journals/Formal%20Methods_State%20of%20the%20Art%20and%20Future%20Directions.pdf)</sup> 规格用时序逻辑书写，这种逻辑允许对时间进行显式推理；该方法背后的关键洞见在于，这类公式可以由机器直接检查，设计者只需规定系统，软件则负责保证正确性。<sup>[5](https://amturing.acm.org/award_winners/clarke_1167964.cfm)</sup>

验证义务因逻辑不同而异。若性质 \( \varphi \) 以 LTL（线性时序逻辑）表达，则模型 \( M \) 的每一条执行轨迹都必须满足 \( \varphi \)；若以 CTL（计算树逻辑）表达，则 \( M \) 的每一个初始状态都必须满足 \( \varphi \)。<sup>[8](https://ptolemy.berkeley.edu/projects/embedded/eecsx44/lectures/07-model-checking.pdf)</sup> CTL 模型检测基于时序模态词的不动点刻画：例如，\( AF p \) 是某个涉及 \( p \) 与 \( AX Z \) 的公式的最小不动点，通过 Tarski–Knaster 定理迭代计算。<sup>[4](https://www.cs.unc.edu/~stotts/COMP590-059-f21/slides/clarke-turing.pdf)</sup> 最初的算法运行时间为 \( O(\lvert f \rvert \cdot \lvert M \rvert^{2}) \)，后来改进为 \( O(\lvert M \rvert \cdot \lvert f \rvert) \)；LTL 模型检测则需要 \( O(\lvert M \rvert \cdot \exp(\lvert f \rvert)) \)，之所以可以接受，是因为模型通常非常大而公式很小。<sup>[4](https://www.cs.unc.edu/~stotts/COMP590-059-f21/slides/clarke-turing.pdf)</sup> [有界模型检测](https://www.edgepedia.org/zh/bounded-model-checking)则把问题归约为布尔可满足性，将公式交给 SAT 求解器，后者接受[合取范式](https://www.edgepedia.org/zh/conjunctive-normal-form)形式的输入，并能处理大量布尔变量。<sup>[9](https://www.jucs.org/jucs_13_5/model_checking_software_and/jucs_13_5_0639_0649_clarke.pdf)</sup>

## 具体做法

CTL 模型检测分两个宏观步骤进行：首先构造指称 \( [\varphi] := \{ s \in S: M, s \models \varphi \} \)，即公式成立的状态集合，然后通过检查 \( I \subseteq [\varphi] \) 将其与初始状态集合进行比较。<sup>[10](http://disi.unitn.it/rseba/DIDATTICA/fm2020/04_CTL_MODEL_CHECKING_HANDOUT.pdf)</sup>

软件模型检测通常使用反例引导的抽象精化（CEGAR）循环，该循环由四步组成：对程序进行抽象，用模型检测器验证抽象，检查所报告的反例是否为虚假反例，以及对抽象进行精化。<sup>[11](https://doi.org/10.1184/r1/6604547)</sup> SLAM、BLAST 和 MAGIC 等工具通过调用定理证明器并传播最弱前置条件或最强后置条件，来判断反例是否为真实行为。<sup>[12](http://www.kroening.com/papers/icfem2004.pdf)</sup>

当性质相对于系统而言较为复杂时，人们会选择交互式定理证明：模型检测器可以验证简单系统的一些复杂性质，或复杂系统的一些简单性质，而 seL4 的正确性证明是复杂系统的一个复杂性质，交互式定理证明恰好适合这种情况。<sup>[13](https://www.trustworthy.systems/publications/nicta_full_text/5396.pdf)</sup> 在 Rocq 证明助手（原名 Coq）这类工具中，实践者以交互方式构造证明，随后工具会自动重新检查其有效性。<sup>[14](https://compcert.org/man/manual.pdf)</sup>

## 起源

模型检测的奠基性论文后来获得了 ACM 图灵奖，该方法也是 Clarke、Grumberg、Kroening、Peled 和 Veith 所著标准专著的主题。该领域所依托的早期工作包括 Hoare 逻辑——一种面向语法的程序正确性推理方法，它之所以影响深远，是因为其风格使之几乎可以扩展到任何类型的程序<sup>[15](https://ar5iv.labs.arxiv.org/html/1904.03917)</sup>；以及作为验证计算机系统之逻辑的时序逻辑。<sup>[5](https://amturing.acm.org/award_winners/clarke_1167964.cfm)</sup>

若干具名技术都有文献记载的提出过程。有界模型检测由 Armin Biere 及其同事在 1999 年的 TACAS 论文《Symbolic Model Checking without BDDs》中提出；所引的 2003 年《Advances in Computers》章节是该方法的后续阐述。<sup>[16](https://doi.org/10.1016/s0065-2458%2803%2958003-2)</sup> [反例引导的抽象精化](https://www.edgepedia.org/zh/counterexample-guided-abstraction-refinement)由 [Edmund M. Clarke](https://www.edgepedia.org/zh/edmund-m-clarke) 及其同事于 2000 年在 KiltHub Repository 中提出。<sup>[11](https://doi.org/10.1184/r1/6604547)</sup> seL4 内核的功能正确性验证由 Gerwin Klein 及其同事于 2009 年报告。<sup>[6](https://dl.acm.org/doi/10.1145/1629575.1629596)</sup>

## 变体

**有界模型检测（BMC）** 将给定长度 \( k \) 的反例之存在性编码为可满足性问题，其公式形如 \( I(s_{0}) \land R(s_{0}, s_{1}) \land \cdots \land R(s_{k-1}, s_{k}) \land F(s_{k}) \)，当且仅当存在长度为 \( k \) 的反例时该公式可满足。<sup>[9](https://www.jucs.org/jucs_13_5/model_checking_software_and/jucs_13_5_0639_0649_clarke.pdf)</sup> 该 SAT 实例描述了一条参数化的反例轨迹，其迁移为布尔变量，SAT 检查器验证其与实际迁移的一致性。<sup>[17](https://largo.lip6.fr/~cecile/558_10931370_Chapter_9.pdf)</sup>

**基于 BDD 的符号模型检测** 用 OBDD 紧凑地表示状态集合，是对状态爆炸的第一次重大回应，尽管对所有变量序而言，OBDD 的规模都可能关于变量数目呈指数增长，例如两个 \( n \) 位数的乘法器的中间输出位。<sup>[4](https://www.cs.unc.edu/~stotts/COMP590-059-f21/slides/clarke-turing.pdf)</sup>

**基于 CEGAR 的软件模型检测**（SLAM、BLAST、MAGIC）通过迭代抽象与精化来验证程序。<sup>[12](http://www.kroening.com/papers/icfem2004.pdf)</sup> **符号执行** 用符号值驱动执行；其主要局限是路径爆炸，因为路径数目通常随程序规模呈指数增长，在存在无界循环时甚至可能无穷；具体符号混合测试将符号执行与具体执行相结合，以最大化代码覆盖率。<sup>[7](https://link.springer.com/chapter/10.1007/978-3-031-71177-0_24)</sup> 使用 Rocq 证明助手（原名 Coq）等工具的**演绎验证**产生机器检查的证明。<sup>[14](https://compcert.org/man/manual.pdf)</sup>

## 应用

模型检测在实践中用于超大规模集成电路、通信协议、软件设备驱动、实时嵌入式系统和安全算法。<sup>[2](https://www-verimag.imag.fr/PEOPLE/Joseph.Sifakis/TuringAwardPaper-Apr14.pdf)</sup> 其采用集中在一些虽属细分却至关重要的领域，在这些领域中，缺陷的代价足以证明验证的代价是值得的：硬件设计、通信交换机、嵌入式系统和操作系统设备驱动。<sup>[18](https://patricegodefroid.github.io/public_psfiles/mchandbook2018.pdf)</sup>

seL4 微内核已在假定编译器、汇编代码和硬件正确的前提下，从抽象规格一直到其 C 实现，完成了形式化的机器检查验证<sup>[6](https://dl.acm.org/doi/10.1145/1629575.1629596)</sup>；作者指出，这是对一个完整的通用操作系统内核之功能正确性的首个形式化证明。<sup>[6](https://dl.acm.org/doi/10.1145/1629575.1629596)</sup> CompCert 是一个经过验证的编译器，从 C 的一个大子集 Clight 编译到包括 PowerPC、ARM、x86 和 RISC-V 在内的多种架构，它用 Coq 编写并证明，附带机器检查的语义保持证明；这保证了在源代码上证明的安全性质对编译后的可执行文件同样成立，其目标应用是航空电子等关键嵌入式软件。<sup>[19](https://cacm.acm.org/research/formal-verification-of-a-realistic-compiler/)</sup>

## 局限与替代方案

模型检测的核心障碍是状态爆炸问题：CTL 算法的实际可用性关键取决于状态空间的大小，若状态数目过大，验证可能无法使用。<sup>[3](https://www.cs.cmu.edu/~emc/papers/Conference%20Papers/Model%20Checking%20and%20the%20State%20Explosion%20Problem.pdf)</sup> 模型检测器不提供正确性证明，且只能直接应用于有限状态系统；将无限状态系统抽象为有限模型会损失精度。<sup>[3](https://www.cs.cmu.edu/~emc/papers/Conference%20Papers/Model%20Checking%20and%20the%20State%20Explosion%20Problem.pdf)</sup> 抽象还可能产生虚假反例，即误报——性质在[抽象](https://www.edgepedia.org/zh/abstraction)模型中不成立，而在具体情形中却成立。<sup>[17](https://largo.lip6.fr/~cecile/558_10931370_Chapter_9.pdf)</sup>

BMC 避免了状态爆炸，因为 SAT 过程不会生成大型附加数据结构，但它是不完备的：每次运行只考虑长度至多为 \( k \) 的反例，而计算出的完备性阈值往往相当大。<sup>[17](https://largo.lip6.fr/~cecile/558_10931370_Chapter_9.pdf)</sup><sup> • </sup><sup>[9](https://www.jucs.org/jucs_13_5/model_checking_software_and/jucs_13_5_0639_0649_clarke.pdf)</sup> 更根本地说，没有任何验证工具能够对任意程序和任意规格，在有限时间内自动给出保证不漏报、不误报的答案。<sup>[7](https://link.springer.com/chapter/10.1007/978-3-031-71177-0_24)</sup>

与测试相比，模型检测提供更好的覆盖率，但计算代价更高；与交互式定理证明相比，它给出的保证较为有限，但由于自动化程度更高而代价更低。<sup>[18](https://patricegodefroid.github.io/public_psfiles/mchandbook2018.pdf)</sup> 由于对非平凡程序的检查通常是近似的，实践中的模型检测最好被视为一种“超级测试”，而非严格的数学验证。<sup>[18](https://patricegodefroid.github.io/public_psfiles/mchandbook2018.pdf)</sup>

## References

1. [Formal Methods: State of the Art and Future Directions (Clarke, Emerson et al.)](https://www.cs.cmu.edu/~emc/papers/Papers%20In%20Refereed%20Journals/Formal%20Methods_State%20of%20the%20Art%20and%20Future%20Directions.pdf)
2. [Model Checking: Algorithmic Verification and Debugging (Clarke Turing Award retrospective, Sifakis copy)](https://www-verimag.imag.fr/PEOPLE/Joseph.Sifakis/TuringAwardPaper-Apr14.pdf)
3. [Model Checking and the State Explosion Problem (Clarke et al., CMU)](https://www.cs.cmu.edu/~emc/papers/Conference%20Papers/Model%20Checking%20and%20the%20State%20Explosion%20Problem.pdf)
4. [Clarke Turing Award lecture slides (course-hosted copy)](https://www.cs.unc.edu/~stotts/COMP590-059-f21/slides/clarke-turing.pdf)
5. [Edmund Clarke - A.M. Turing Award Laureate](https://amturing.acm.org/award_winners/clarke_1167964.cfm)
6. [seL4: formal verification of an OS kernel (SOSP 2009, Klein et al.)](https://dl.acm.org/doi/10.1145/1629575.1629596)
7. [A Pyramid Of (Formal) Software Verification (Springer)](https://link.springer.com/chapter/10.1007/978-3-031-71177-0_24)
8. [EE 244: Model Checking lecture (UC Berkeley)](https://ptolemy.berkeley.edu/projects/embedded/eecsx44/lectures/07-model-checking.pdf)
9. [Model Checking: Software and Beyond (Clarke et al., Journal of Universal Computer Science)](https://www.jucs.org/jucs_13_5/model_checking_software_and/jucs_13_5_0639_0649_clarke.pdf)
10. [Introduction to Formal Methods, Chapter 04: CTL Model Checking (University of Trento)](http://disi.unitn.it/rseba/DIDATTICA/fm2020/04_CTL_MODEL_CHECKING_HANDOUT.pdf)
11. [Clarke, Edmund M and colleagues (2000). Counterexample-guided Abstraction Refinement. KiltHub Repository.](https://doi.org/10.1184/r1/6604547)
12. [Counterexample Guided Abstraction Refinement via Program Execution (ICFEM 2004)](http://www.kroening.com/papers/icfem2004.pdf)
13. [Large-Scale Formal Verification in Practice: A Process Perspective (NICTA)](https://www.trustworthy.systems/publications/nicta_full_text/5396.pdf)
14. [The CompCert C verified compiler, manual](https://compcert.org/man/manual.pdf)
15. [Fifty Years of Hoare's Logic](https://ar5iv.labs.arxiv.org/html/1904.03917)
16. [Bounded Model Checking (Advances in computers, 2003)](https://doi.org/10.1016/s0065-2458%2803%2958003-2)
17. [Counterexamples Revisited: Principles, Algorithms, Applications (LNCS 2772)](https://largo.lip6.fr/~cecile/558_10931370_Chapter_9.pdf)
18. [Combining Model Checking and Testing (Handbook of Model Checking chapter)](https://patricegodefroid.github.io/public_psfiles/mchandbook2018.pdf)
19. [Formal Verification of a Realistic Compiler (Communications of the ACM)](https://cacm.acm.org/research/formal-verification-of-a-realistic-compiler/)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software testing and quality

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/formal-verification

English original: https://www.edgepedia.org/formal-verification
