# 变异方法（形式化方法）

形式化方法中的变异方法一次性验证或分析可配置系统的所有变体，而不是分别检查每个配置。动机在于规模：[Linux 内核](https://www.edgepedia.org/zh/linux-kernel)可通过约 10,000 个编译期配置选项进行配置，由此可能产生数十亿个变体<sup>[1](http://www.cs.cmu.edu/~ckaestne/pdf/fse13.pdf)</sup>；而对一个含 n 个特征的产品线进行暴力验证，最多需要调用模型检验算法 2^n 次，每个有效配置一次。<sup>[2](https://pure.unamur.be/ws/portalfiles/portal/1090322/73891.pdf)</sup> 变异分析接收一个带标注的制品，返回一个覆盖所有有效变体的结果结构：通常是一个证明、一个反例连同违反性质的变体集合，或者一个模型。

| 关键事实 | 细节 |
|---|---|
| 输出 | 一次覆盖所有变体的证明、反例或模型；FTS 模型检验能精确指出哪些产品违反或满足某条 LTL 性质<sup>[3](http://kishi-lab.sakura.ne.jp/mip_award/SPLC/Nominations/Classen10.pdf)</sup> |
| 核心表示 | 选择、存在条件，以及迁移或 AST 节点上的特征守卫<sup>[4](https://eric.walkingshaw.net/files/pubs/2011/tosem11-choice-calculus.pdf)</sup><sup> • </sup><sup>[1](http://www.cs.cmu.edu/~ckaestne/pdf/fse13.pdf)</sup> |
| 奠基性表示 | 选择演算（马丁·埃尔维希（Martin Erwig）与埃里克·沃金肖（Eric Walkingshaw），TOSEM 2011）<sup>[4](https://eric.walkingshaw.net/files/pubs/2011/tosem11-choice-calculus.pdf)</sup>；特征迁移系统（克拉森（Classen）、科迪（Cordy）、肖本斯（Schobbens）、海曼斯（Heymans）、勒盖（Legay）与拉斯金（Raskin），IEEE TSE 2013）<sup>[5](https://doi.org/10.1109/tse.2012.86)</sup> |
| 典型收益 | 可变性感知解释（VarexJ）最高可达 2,843 倍<sup>[6](https://wwwiti.cs.uni-magdeburg.de/iti_db/publikationen/ps/auto/M14.pdf)</sup>；FTS 模型检验平均 3.5 倍，最高 7 倍<sup>[3](http://kishi-lab.sakura.ne.jp/mip_award/SPLC/Nominations/Classen10.pdf)</sup> |
| 输入 | 一个特征模型，加上带标注的代码或模型（#ifdef、TVL、fPromela、MTS 编码）<sup>[2](https://pure.unamur.be/ws/portalfiles/portal/1090322/73891.pdf)</sup> |
| 主要工具 | SNIP、VMC/FTS4VMC、TypeChef、SPLverifier、Varex/VarexJ、ProVeLines、fNuSMV |
| 主要局限 | 最坏情况下代价随特征数量指数增长；抽象可能产生虚假反例<sup>[7](https://pure.itu.dk/ws/files/82367741/STTT16_main.pdf)</sup> |

## 工作原理

**可变性存在于制品内部。** 选择演算用代码备选片段之间的选择来表示变异，而不是把代码可选地包含进来；它被提出作为软件变异的一种基础表示，意在在变异研究中扮演类似 λ 演算在编程语言中所扮演的角色。<sup>[4](https://eric.walkingshaw.net/files/pubs/2011/tosem11-choice-calculus.pdf)</sup> 在可变性感知抽象语法树中，Choice 节点表示两个或多个备选子树之间的选择；共享并让可变性保持局部（晚分裂、早合并）是效率的关键。<sup>[1](http://www.cs.cmu.edu/~ckaestne/pdf/fse13.pdf)</sup>

**存在条件与受守卫行为。** 软件产品线（SPL）通常形式化为元组 (F, Φ, D, φ)：特征集 F、关于有效配置的特征模型公式 Φ、领域模型 D（即 150% 表示），以及把每个程序元素映射到特征表达式的存在条件 φ。<sup>[8](https://ar5iv.labs.arxiv.org/html/2010.00697)</sup> 特征迁移系统（FTS）通过给每条迁移标注一个特征表达式来扩展迁移系统；该表达式是能够执行该迁移的变体集合的符号编码；FTS 配有参数化语义，可给出每个产品的行为。<sup>[3](http://kishi-lab.sakura.ne.jp/mip_award/SPLC/Nominations/Classen10.pdf)</sup><sup> • </sup><sup>[5](https://doi.org/10.1109/tse.2012.86)</sup> 模态迁移系统（MTS）则区分可选（may）迁移与必需（must）迁移，并用 ALT、EXC、REQ 和 IFF 约束定义有效产品。<sup>[9](http://fmt.isti.cnr.it/~mtbeek/FM12.pdf)</sup>

**正确性契约。** 保持变异的计算要求选择与求值可交换：运行一个变异程序，等价于分别运行所有单个变体。<sup>[10](https://eric.walkingshaw.net/files/pubs/2016/ecoop16-variational-programming-calculus.pdf)</sup> 对于精确的提升分析，在一个程序族上运行提升分析必须得到一个结果族，使得用决策 δ 选择任一变体时，所得结果与传统分析在该变体上得到的结果相同；对于使用可变性抽象的提升分析，结果只是传统结果的近似。<sup>[11](https://web.engr.oregonstate.edu/~erwig/papers/VHMX_ICFP14.pdf)</sup>

## 实施方式

实践者通常遵循以下顺序。首先，对系统进行标注：一个特征模型，加上源代码中的可变性标注（#ifdef 指令）、诸如 TVL 的文本特征语言，或诸如 fPromela 的建模语言；fPromela 用特征变量和由特征守卫的语句扩展 [Promela](https://www.edgepedia.org/zh/promela)。<sup>[2](https://pure.unamur.be/ws/portalfiles/portal/1090322/73891.pdf)</sup> 其次，构建可变性编码或模型：fPromela 模型可编译为 FTS。<sup>[7](https://pure.itu.dk/ws/files/82367741/STTT16_main.pdf)</sup> 第三，可选地检查模型中的歧义、死迁移、虚假可选迁移和隐藏死锁状态；该算法把检测归约为 SAT 问题，并在 FTS4VMC 中用 Z3 SMT 求解器实现。<sup>[12](http://fmt.isti.cnr.it/~mtbeek/SPLC21b.pdf)</sup><sup> • </sup><sup>[13](https://link.springer.com/article/10.1007/s10664-020-09930-8)</sup> 第四，运行提升的、基于族的分析：模型检验、类型检查或抽象解释。在无法实现完全可变性感知时，应用可变性抽象；可变性抽象定义为伽罗瓦连接，有意以精度换速度<sup>[14](https://www.sciencedirect.com/science/article/pii/S0167642318300583)</sup>；三种基本抽象分别把多个配置合并为一个抽象配置（α_join）、把配置空间投影到满足某约束的子集（α_projφ），以及忽略被认为无关的特征（α_fignore）。<sup>[15](https://aleksdimovski.github.io/files/FAOC19-main.pdf)</sup> 最后，把结果投影回各个变体。Reconfigurator 源到源转换会对变异 fPromela 模型进行划分和抽象，直到其中不再含有可变性，从而可由标准的单系统检验器 SPIN 进行验证。<sup>[7](https://pure.itu.dk/ws/files/82367741/STTT16_main.pdf)</sup>

## 起源

选择演算由马丁·埃尔维希和埃里克·沃金肖在 2011 年《ACM Transactions on Software Engineering and Methodology》的论文中提出，同时给出了可靠变换、策略范式以及变异结构的设计理论<sup>[4](https://eric.walkingshaw.net/files/pubs/2011/tosem11-choice-calculus.pdf)</sup>；同一篇论文把软件产品线追溯到帕纳斯（Parnas）1976 年的工作，把特征图追溯到康（Kang）等人 1990 年的工作。<sup>[4](https://eric.walkingshaw.net/files/pubs/2011/tosem11-choice-calculus.pdf)</sup> 更早时，李（Li）、克里希纳穆尔蒂（Krishnamurthi）和菲斯勒（Fisler）用三值模型检验对开放特征进行模块化验证（《Automated Software Engineering》，2005）<sup>[16](https://doi.org/10.1007/s10515-005-2643-9)</sup>；格鲁勒（Gruler）、洛伊克（Leucker）和沙伊德曼（Scheidemann）在 2008 年对软件产品线进行建模和模型检验。特征迁移系统配有专门的 LTL 模型检验算法<sup>[3](http://kishi-lab.sakura.ne.jp/mip_award/SPLC/Nominations/Classen10.pdf)</sup>，而用于验证可变性密集型系统的 FTS 基础发表于 2013 年的《IEEE Transactions on Software Engineering》。<sup>[5](https://doi.org/10.1109/tse.2012.86)</sup> 在实现方面，卡斯特纳（Kästner）及其同事构建了可变性感知的 C 解析器 TypeChef（2011）<sup>[17](https://doi.org/10.1145/2076021.2048128)</sup>；卡斯特纳、阿佩尔（Apel）、蒂姆（Thüm）和萨克（Saake）对基于标注的产品线进行了类型检查（TOSEM 2012）<sup>[18](https://doi.org/10.1145/2211616.2211617)</sup>；蒂姆及其同事在 2014 年《ACM Computing Surveys》的文章中对分析策略进行了分类。<sup>[19](https://doi.org/10.1145/2580950)</sup>

## 变体

**模型检验工具。** SNIP 使用 FTS 一步检查一个 SPL 的所有产品，用 TVL 表示可变性，用 fPromela 表示行为。<sup>[2](https://pure.unamur.be/ws/portalfiles/portal/1090322/73891.pdf)</sup> VMC 接受以带可变性约束的 MTS 指定的产品族，并在可变性感知逻辑 v-ACTL 中执行显式状态即时模型检验<sup>[9](http://fmt.isti.cnr.it/~mtbeek/FM12.pdf)</sup>；其 FTS4VMC 前端增加了歧义检查、消歧以及 FTS 到 MTS 的转换。<sup>[12](http://fmt.isti.cnr.it/~mtbeek/SPLC21b.pdf)</sup> 专用的 SPL 模型检验器还包括 ProVeLines、fNuSMV、ProFeat（概率）和 QFLan（统计）。<sup>[13](https://link.springer.com/article/10.1007/s10664-020-09930-8)</sup>

**程序分析工具。** SPLverifier 使用可变性编码：把每个条件预处理指令替换为相应的 if 语句，从而检查一个覆盖 n 个特征的产品模拟器，而不是最多 2^n 个产品，并使用现成的模型检验器 CBMC 和 CPAchecker。<sup>[20](https://ar5iv.labs.arxiv.org/html/1110.0021)</sup> TypeChef 把带 #ifdef 的 C 代码解析为可变 AST。<sup>[17](https://doi.org/10.1145/2076021.2048128)</sup> Varex 是一个 PHP 解释器，可扩展到 \( 2^{50} \) 个配置；VarexJ 提升 JavaPathfinder v7.0 解释器，并在延斯·迈尼克（Jens Meinicke）2014 年的硕士论文中有所记录。<sup>[6](https://wwwiti.cs.uni-magdeburg.de/iti_db/publikationen/ps/auto/M14.pdf)</sup><sup> • </sup><sup>[21](http://meinicke.github.io/VarexJ/)</sup> 提升框架推广了这一思想：浅提升把分析包装成黑盒，探索所有输入组合；深提升把程序重写为可变性感知程序<sup>[8](https://ar5iv.labs.arxiv.org/html/2010.00697)</sup>；VHM(X) 把提升系统化为 HM(X) 的基于类型的扩展。<sup>[11](https://web.engr.oregonstate.edu/~erwig/papers/VHMX_ICFP14.pdf)</sup>

**近期扩展。** 变异可满足性求解能高效求解许多相关的 SAT 问题，由杰弗里·M·扬（Jeffrey M. Young）、保罗·马克西米利安·比特纳（Paul Maximilian Bittner）、埃里克·沃金肖和托马斯·蒂姆于 2022 年在《Empirical Software Engineering》上提出。<sup>[22](https://doi.org/10.1007/s10664-022-10217-3)</sup> SPLFaultLoc 一趟就在整个程序族上执行可变性故障定位，并推断提升的错误不变式<sup>[23](https://aleksdimovski.github.io/files/dimovski-sle25.pdf)</sup>；MONOPOLY 是一个通用的黑盒提升框架，带有正确性定理和证明。<sup>[24](https://conf.researchr.org/details/variability-2026/variability-2026-papers/18/MONOPOLY-Product-Line-Analysis-via-Monotonicity)</sup>

## 应用

可变性感知类型检查和活跃性分析被应用于 Busybox 工具套件和 Linux 内核<sup>[25](https://www.se.cs.uni-saarland.de/publications/docs/TR_LRKADL12.pdf)</sup>；一项更广泛的实证研究用七种控制流和数据流分析覆盖了 Busybox、OpenSSL、SQLite、x86 Linux 内核和 uClibc。<sup>[26](https://dl.acm.org/doi/10.1145/3280986)</sup> 基于族的类型检查在 12 个 Java 产品线上用 FUJI 编译器进行了评估，发现了 556 个特征局部错误和跨特征错误。<sup>[27](https://www.se.cs.uni-saarland.de/publications/docs/GPCE2013fuji.pdf)</sup> 约束提升用宝马集团（BMW Group）和[美诺](https://www.edgepedia.org/zh/miele)（Miele）的生产计划数据验证了模型产品线，其中包括一个拥有超过 10,000 个特征的产品线。<sup>[28](https://arxiv.org/pdf/2008.11427v2.pdf)</sup>

收益取决于技术和负载。在含 64 个产品的矿井泵 SPL 上，FTS 模型检验的速度平均为分别验证所有产品的 3.5 倍，最高为 7 倍。<sup>[3](http://kishi-lab.sakura.ne.jp/mip_award/SPLC/Nominations/Classen10.pdf)</sup> 对 Linux 内核进行可变性感知类型检查的耗时约等于检查 27 个抽样产品（Busybox 中为 4 个）；而在两项案例研究中，可变性感知活跃性分析的耗时都仅相当于分别分析两个产品的耗时。<sup>[25](https://www.se.cs.uni-saarland.de/publications/docs/TR_LRKADL12.pdf)</sup> 在 10 个可配置程序上，VarexJ 相对暴力执行最高加速 2,843 倍。<sup>[6](https://wwwiti.cs.uni-magdeburg.de/iti_db/publikationen/ps/auto/M14.pdf)</sup> 对于 OpenSSL，以变异方式检查所有变体，比不利用相似性、仅检查两个变体还要快。<sup>[26](https://dl.acm.org/doi/10.1145/3280986)</sup>

## 局限与替代方案

**指数爆炸。** 提升分析的代价在最坏情况下随静态可配置特征的数量呈指数增长。<sup>[14](https://www.sciencedirect.com/science/article/pii/S0167642318300583)</sup> 在一项评估中，当有 11 个特征（2,048 个变体）时，SNIP 因内存不足而崩溃，而暴力 SPIN 分析耗时近一分钟；当有 25 个特征时，暴力方法耗时升至近一年。<sup>[7](https://pure.itu.dk/ws/files/82367741/STTT16_main.pdf)</sup>

**不精确与不完备。** 抽象后的结果不那么精确：一些报告的反例可能是虚假的，是在抽象过程中引入的。<sup>[7](https://pure.itu.dk/ws/files/82367741/STTT16_main.pdf)</sup> 基于特征的类型检查速度快但不完备，会漏掉由特征交互引起的类型错误；基于产品的检查在最坏情况下需要指数级工作量，因为要按产品重复冗余工作。<sup>[27](https://www.se.cs.uni-saarland.de/publications/docs/GPCE2013fuji.pdf)</sup> 如果某条性质落在所保留的线性复杂度片段之外，就必须以指数代价用经典的基于族或基于产品的模型检验来检查。<sup>[13](https://link.springer.com/article/10.1007/s10664-020-09930-8)</sup>

**采样作为替代方案。** 基于采样的验证分析选定的产品，而不是整个产品族。t 路交互采样确保覆盖 t 个特征之间的所有交互，但在大型产品线上，当今的算法会耗尽内存、不终止，或产生大到无法测试的样本。<sup>[29](https://dl.acm.org/doi/10.1145/3336294.3336322)</sup> 在直接对比中，可变性感知分析在效率和发现的缺陷两方面都优于大多数基于采样的静态分析技术。<sup>[26](https://dl.acm.org/doi/10.1145/3280986)</sup> 已发表的 Linux 内核选项数量数据并不一致：一项研究称约有 10,000 个编译期配置选项<sup>[1](http://www.cs.cmu.edu/~ckaestne/pdf/fse13.pdf)</sup>，另一项研究则称约有 15,000 个不同特征，且不限于布尔值。<sup>[30](https://sfortz.github.io/publication/2023-lifts-ds/2023-lifts-ds.pdf)</sup>

## References

1. [Scalable Analysis of Variable Software (FSE 2013, Kästner et al.)](http://www.cs.cmu.edu/~ckaestne/pdf/fse13.pdf)
2. [Model checking software product lines with SNIP (STTT)](https://pure.unamur.be/ws/portalfiles/portal/1090322/73891.pdf)
3. [Model Checking Lots of Systems: Efficient Verification of Temporal Properties in Software Product Lines (Classen et al., 2010)](http://kishi-lab.sakura.ne.jp/mip_award/SPLC/Nominations/Classen10.pdf)
4. [The Choice Calculus: A Representation for Software Variation (TOSEM 2011)](https://eric.walkingshaw.net/files/pubs/2011/tosem11-choice-calculus.pdf)
5. [Andreas Classen and colleagues (2013). Featured Transition Systems: Foundations for Verifying Variability-Intensive Systems and Their Application to LTL Model Checking. IEEE Transactions on Software Engineering.](https://doi.org/10.1109/tse.2012.86)
6. [VarexJ: A Variability-Aware Interpreter for Java Applications (Meinicke master's thesis, 2014)](https://wwwiti.cs.uni-magdeburg.de/iti_db/publikationen/ps/auto/M14.pdf)
7. [Efficient family-based model checking via variability abstractions (STTT)](https://pure.itu.dk/ws/files/82367741/STTT16_main.pdf)
8. [Automatic and Efficient Variability-Aware Lifting of Functional Programs (Shahin & Chechik, arXiv 2010.00697)](https://ar5iv.labs.arxiv.org/html/2010.00697)
9. [VMC: A Tool for Product Variability Analysis (FM 2012, LNCS 7436)](http://fmt.isti.cnr.it/~mtbeek/FM12.pdf)
10. [A Calculus for Variational Programming (ECOOP 2016, Chen, Erwig, Walkingshaw)](https://eric.walkingshaw.net/files/pubs/2016/ecoop16-variational-programming-calculus.pdf)
11. [Type-Based Parametric Analysis of Program Families (VHM(X), ICFP 2014)](https://web.engr.oregonstate.edu/~erwig/papers/VHMX_ICFP14.pdf)
12. [Static Analysis and Family-based Model Checking of Featured Transition Systems with VMC (SPLC 2021)](http://fmt.isti.cnr.it/~mtbeek/SPLC21b.pdf)
13. [Efficient static analysis and verification of featured transition systems (Empirical Software Engineering)](https://link.springer.com/article/10.1007/s10664-020-09930-8)
14. [Variability abstractions for lifted analyses (Science of Computer Programming)](https://www.sciencedirect.com/science/article/pii/S0167642318300583)
15. [Finding Suitable Variability Abstractions for Lifted Analysis (Formal Aspects of Computing)](https://aleksdimovski.github.io/files/FAOC19-main.pdf)
16. [Harry C. Li, Shriram Krishnamurthi, Kathi Fisler (2005). Modular Verification of Open Features Using Three-Valued Model Checking. Automated Software Engineering.](https://doi.org/10.1007/s10515-005-2643-9)
17. [Christian Kästner and colleagues (2011). Variability-aware parsing in the presence of lexical macros and conditional compilation. ACM SIGPLAN Notices.](https://doi.org/10.1145/2076021.2048128)
18. [Christian Kästner and colleagues (2012). Type checking annotation-based product lines. ACM Transactions on Software Engineering and Methodology.](https://doi.org/10.1145/2211616.2211617)
19. [Thomas Thüm and colleagues (2014). A Classification and Survey of Analysis Strategies for Software Product Lines. ACM Computing Surveys.](https://doi.org/10.1145/2580950)
20. [Feature-Aware Verification (SPLverifier, ASE 2011)](https://ar5iv.labs.arxiv.org/html/1110.0021)
21. [VarexJ, Variability-Aware Execution for Java (project page)](http://meinicke.github.io/VarexJ/)
22. [Jeffrey M. Young and colleagues (2022). Variational satisfiability solving: efficiently solving lots of related SAT problems. Empirical Software Engineering.](https://doi.org/10.1007/s10664-022-10217-3)
23. [Variability Fault Localization by Abstract Interpretation and its Application to SPL Repair (SLE 2025)](https://aleksdimovski.github.io/files/dimovski-sle25.pdf)
24. [MONOPOLY: Product Line Analysis via Monotonicity (VARIABILITY 2026)](https://conf.researchr.org/details/variability-2026/variability-2026-papers/18/MONOPOLY-Product-Line-Analysis-via-Monotonicity)
25. [Large-Scale Variability-Aware Type Checking and Dataflow Analysis (Liebig et al., ESEC/FSE 2013 technical report, TypeChef)](https://www.se.cs.uni-saarland.de/publications/docs/TR_LRKADL12.pdf)
26. [Variability-Aware Static Analysis at Scale: An Empirical Study (ACM TOSEM 27(4))](https://dl.acm.org/doi/10.1145/3280986)
27. [A Comparison of Product-based, Feature-based, and Family-based Type Checking (GPCE 2013)](https://www.se.cs.uni-saarland.de/publications/docs/GPCE2013fuji.pdf)
28. [Generic Analysis of Model Product-Lines via Constraint Lifting](https://arxiv.org/pdf/2008.11427v2.pdf)
29. [Product Sampling for Product Lines: The Scalability Challenge (SPLC 2019)](https://dl.acm.org/doi/10.1145/3336294.3336322)
30. [Variability-aware Behavioural Learning (LiFTS, 2023)](https://sfortz.github.io/publication/2023-lifts-ds/2023-lifts-ds.pdf)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026 · Last review: Sep 30, 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/variational-methods-formal-methods

English original: https://www.edgepedia.org/variational-methods-formal-methods
