# Infer（静态分析器）

**Infer** 是一款开源静态分析工具，以 OCaml 编写，接收 Java、C、C++ 或 [Objective-C](https://www.edgepedia.org/zh/objective-c) 源代码及一条构建命令，在不运行程序的情况下产出一份潜在缺陷列表。<sup>[1](https://github.com/facebook/infer/blob/main/infer/man/man1/infer.txt)</sup><sup> • </sup><sup>[2](https://github.com/facebook/infer)</sup> 它由 Facebook（现 Meta）开发，于 2015 年 6 月 11 日开源，此前该公司于 2013 年收购了 Monoidics——一家由该工具创造者于 2009 年创立的验证领域初创公司。<sup>[3](https://engineering.fb.com/2015/06/11/developer-tools/open-sourcing-facebook-infer-identify-bugs-before-you-ship/)</sup>

| 关键事实 | 详情 |
|---|---|
| 功能 | 给定 Java 或 C 族源代码及一条构建命令，产出潜在问题列表<sup>[1](https://github.com/facebook/infer/blob/main/infer/man/man1/infer.txt)</sup> |
| 实现 | 以 OCaml 编写；MIT 许可（Java 支持可能需要 GPL 许可的组件）<sup>[2](https://github.com/facebook/infer)</sup> |
| 开源日期 | 2015 年 6 月 11 日<sup>[3](https://engineering.fb.com/2015/06/11/developer-tools/open-sourcing-facebook-infer-identify-bugs-before-you-ship/)</sup> |
| 核心技术 | 分离逻辑与双溯因；后来以抽象解释加以扩展<sup>[3](https://engineering.fb.com/2015/06/11/developer-tools/open-sourcing-facebook-infer-identify-bugs-before-you-ship/)</sup><sup> • </sup><sup>[4](https://discovery.ucl.ac.uk/id/eprint/10048925/1/Pym2018_Article_WhySeparationLogicWorks.pdf)</sup> |
| 在 Meta 的部署 | 在持续集成中对每一次移动端代码变更运行；所报告问题的修复率约为 80%（2015 年）<sup>[3](https://engineering.fb.com/2015/06/11/developer-tools/open-sourcing-facebook-infer-identify-bugs-before-you-ship/)</sup> |
| 速度 | 完整分析 Facebook Android 应用约需 130 分钟；平均代码变更的分析时间不足 12 分钟<sup>[5](https://engineering.fb.com/2017/10/19/android/open-sourcing-racerd-fast-static-race-detection-at-scale/)</sup> |
| 当前引擎 | 自 v1.2.0 起，Pulse 一直是默认的内存与值分析引擎，取代了 Biabduction<sup>[6](https://github.com/facebook/infer/releases)</sup> |

## 用途与缺陷类型

静态分析器通过在不运行程序的情况下扫描源代码来发现缺陷，与动态测试互为补充：测试检查的是单次运行，而静态分析可以一次性检查多条乃至全部执行路径。<sup>[3](https://engineering.fb.com/2015/06/11/developer-tools/open-sourcing-facebook-infer-identify-bugs-before-you-ship/)</sup> 开源之时，Infer 报告的问题包括空指针访问以及资源和内存泄漏，Facebook 称这类问题在应用崩溃中占很大比例。<sup>[3](https://engineering.fb.com/2015/06/11/developer-tools/open-sourcing-facebook-infer-identify-bugs-before-you-ship/)</sup> 项目网站列出了当前的覆盖范围：对于 Android 和 Java，包括空指针异常、资源泄漏、注解可达性、缺失的锁保护以及并发竞态条件；对于 C、C++ 和 iOS/Objective-C，包括空指针解引用、内存泄漏、编码规范以及不可用的 API。<sup>[7](https://fbinfer.com/)</sup>

一项与众不同的能力是过程间分析，能够发现涉及多个过程之间交互的缺陷。Facebook 的工程师将其与 FindBugs、Error-prone 和 Clang Static Analyzer 等工具作了对比，后者只进行过程内或文件内分析，无法发现此类缺陷。在 Infer 发现的若干类缺陷中，过程间缺陷占大多数，Facebook 的开发者已修复了数千个此类问题。<sup>[8](https://engineering.fb.com/2017/09/06/android/finding-inter-procedural-bugs-at-scale-with-infer-static-analyzer/)</sup>

## 分析基础

Infer 的分析脱胎于[分离逻辑](https://www.edgepedia.org/zh/separation-logic)——一种描述计算机内存变更的数学逻辑——以及双溯因——一种推断技术，用于发现应用代码中相互独立部分的行为性质。分离逻辑使分析能够针对应用存储中细小的独立部分进行推理，而不必在每一步都考虑整个内存；双溯因则让 Infer 能够在多次运行之间保存摘要，从而只需重新分析软件中发生变更的部分。<sup>[3](https://engineering.fb.com/2015/06/11/developer-tools/open-sourcing-facebook-infer-identify-bugs-before-you-ship/)</sup> 这种方法具有组合性：过程的摘要独立于调用上下文生成，这使得该工具能够扩展到数百万行的代码库，并针对频繁的修改增量式地工作。<sup>[8](https://engineering.fb.com/2017/09/06/android/finding-inter-procedural-bugs-at-scale-with-infer-static-analyzer/)</sup>

此后，该工具已超出其逻辑学源头。其创造者之一、[伦敦大学学院](https://www.edgepedia.org/zh/university-college-london)教授 [Peter O'Hearn](https://www.edgepedia.org/zh/peter-ohearn) 的一篇综述指出，Infer 已演进到涵盖使用抽象解释的其他推理方法，即 Cousot 与 Cousot 于 1977 年提出的技术。<sup>[4](https://discovery.ucl.ac.uk/id/eprint/10048925/1/Pym2018_Article_WhySeparationLogicWorks.pdf)</sup> 例如，竞态检测器 RacerD 就构建在 Infer 基于抽象解释的组合式分析基础设施之上：检查器作者提供一个抽象域和逐语句变换，框架便产出一个可扩展的分析。<sup>[5](https://engineering.fb.com/2017/10/19/android/open-sourcing-racerd-fast-static-race-detection-at-scale/)</sup>

## 历史

其谱系可追溯至一系列研究原型——[Smallfoot](https://www.edgepedia.org/zh/smallfoot)、Space Invader 和 Abductor——它们为应用和自动化分离逻辑而构建。基于这项工作，创造者们于 2009 年成立了初创公司 Monoidics；Monoidics 于 2013 年加入 Facebook，此后该分析器通过开发团队与 Facebook 移动软件工程师之间的迭代反馈循环不断改进。<sup>[3](https://engineering.fb.com/2015/06/11/developer-tools/open-sourcing-facebook-infer-identify-bugs-before-you-ship/)</sup> [伦敦玛丽女王大学](https://www.edgepedia.org/zh/queen-mary-university-of-london)将该工具描述为最初由该校衍生公司 Monoidics 开发，自 2013 年收购以来则在 Facebook 开发。<sup>[9](https://www.seresearch.qmul.ac.uk/cfcs/impact/inferstatic/)</sup>

在 Facebook，Infer 对提交评审的代码修改增量运行，并在发现潜在问题时自动在源代码上写入评论。2015 年该公司报告称，该工具每月识别出的数百个潜在缺陷在进入代码库之前即由开发者修复，修复率约为 80%。<sup>[3](https://engineering.fb.com/2015/06/11/developer-tools/open-sourcing-facebook-infer-identify-bugs-before-you-ship/)</sup>

## 并发检查与 RacerD

2017 年，Facebook 开源了 RacerD，这是一款构建在 Infer 之上的静态竞态检测器，其开发与 News Feed 迁移至 Litho 同步进行——Litho 是一个支持多线程渲染的声明式 Android UI 框架。在 Litho 发布之时，该线程安全分析器已识别出数百个问题，并由工程师在投入生产环境之前加以解决。<sup>[10](https://engineering.fb.com/2017/09/26/android/multithreaded-rendering-on-android-with-litho-and-infer/)</sup>

RacerD 以两种模式部署：批处理模式，开发者借此在将代码放入并发上下文之前检查各个类；以及持续集成模式，对每一次 Android 代码变更运行，仅报告由该次变更引入的缺陷。在批处理与持续集成合计之下，已有超过 1,000 个分析器问题得到处理，分为记录线程假设的注解变更与避免竞态的功能性变更两类，每类均达数百个。<sup>[5](https://engineering.fb.com/2017/10/19/android/open-sourcing-racerd-fast-static-race-detection-at-scale/)</sup>

## 性能与独立评测

速度是持续集成的一项设计要求。在比持续集成机器更强大的机器上，对整个 Facebook Android 应用运行 Infer 约需 130 分钟，但平均代码变更的分析时间不足 12 分钟，快了十倍以上，这得益于组合式分析和 Facebook Buck 构建系统的分布式缓存。<sup>[5](https://engineering.fb.com/2017/10/19/android/open-sourcing-racerd-fast-static-race-detection-at-scale/)</sup>

在 SV-COMP 2022——一项面向软件验证器的学术竞赛——中，一个名为 Infer-sv 的封装被提交，以便进行客观比较。在 8,000 个带有验证结论的任务中，Infer 对 6,000 个任务在不到一秒的 CPU 时间内完成分析，其余 2,000 个任务各在 100 秒内完成，远低于 900 秒的限制。竞赛报告还记录了一些任务上的不一致结果，包括一个程序，Infer 对其错误地报告为安全。<sup>[11](https://www.sosy-lab.org/research/pub/2022-SVCOMP.The_Static_Analyzer_Infer_in_SV-COMP.pdf)</sup> SV-COMP 的作者指出，尽管 Infer 颇受欢迎，但在本次提交之前，从未有关于其精确度和效率的量化报告。<sup>[12](https://epub.ub.uni-muenchen.de/110209/)</sup>

## 生态系统与相关工具

Infer 归属于 Meta 分析工具家族。Getafix 从过往修复中学习，为 Infer 报告的空解引用缺陷自动建议修复方案，既针对新引入的缺陷，也针对已进入代码库的积压问题。<sup>[13](https://ai.meta.com/blog/getafix-how-facebook-tools-learn-to-fix-bugs-automatically/)</sup> [Mariana Trench](https://www.edgepedia.org/zh/mariana-trench) 于 2021 年开源，是一款独立工具，用于发现 Android 和 Java 应用中的安全与隐私缺陷，它使用抽象解释计算其模型。<sup>[14](https://engineering.fb.com/2021/09/29/security/mariana-trench/)</sup> Infer 还曾附带 AL——一种用于在抽象语法树上编写检查器的声明式语言；AL 代码检查框架后来被移除，转而采用基于 clang 的替代方案，如 clang-tidy。<sup>[15](https://engineering.fb.com/2017/05/24/developer-tools/al-a-new-declarative-language-for-detecting-bugs-with-infer/)</sup><sup> • </sup><sup>[6](https://github.com/facebook/infer/releases)</sup>

该工具持续增加前端。1.2.0 版新增了 Erlang 和 Hack 前端以及一种“Textual”中间输入格式；1.3.0 版新增了用于检测可等待对象未被等待这一错误的 Python 前端、面向循环引用检测的 Swift 前端，以及一个由社区贡献的实验性 Rust 前端，同时将 C/C++/Objective-C 前端升级至 LLVM 21。<sup>[6](https://github.com/facebook/infer/releases)</sup> GitHub 仓库显示约 1.57 万星标和 2,100 个复刻，项目网站列出的用户包括京东、Sky 和 wolfSSL。<sup>[2](https://github.com/facebook/infer)</sup><sup> • </sup><sup>[7](https://fbinfer.com/)</sup>

## References

1. [infer(1) manual page, facebook/infer GitHub repository](https://github.com/facebook/infer/blob/main/infer/man/man1/infer.txt)
2. [facebook/infer repository README, GitHub](https://github.com/facebook/infer)
3. [Open-sourcing Facebook Infer: Identify bugs before you ship, Engineering at Meta (2015)](https://engineering.fb.com/2015/06/11/developer-tools/open-sourcing-facebook-infer-identify-bugs-before-you-ship/)
4. [Why Separation Logic Works, Peter O'Hearn and David Pym (2018)](https://discovery.ucl.ac.uk/id/eprint/10048925/1/Pym2018_Article_WhySeparationLogicWorks.pdf)
5. [Open-sourcing RacerD: Fast static race detection at scale, Engineering at Meta (2017)](https://engineering.fb.com/2017/10/19/android/open-sourcing-racerd-fast-static-race-detection-at-scale/)
6. [Releases, facebook/infer GitHub repository](https://github.com/facebook/infer/releases)
7. [Infer Static Analyzer, fbinfer.com](https://fbinfer.com/)
8. [Finding inter-procedural bugs at scale with Infer static analyzer, Engineering at Meta (2017)](https://engineering.fb.com/2017/09/06/android/finding-inter-procedural-bugs-at-scale-with-infer-static-analyzer/)
9. [Infer Static Analyser Supporting Social Media, Queen Mary University of London](https://www.seresearch.qmul.ac.uk/cfcs/impact/inferstatic/)
10. [Multithreaded rendering on Android with Litho and Infer, Engineering at Meta (2017)](https://engineering.fb.com/2017/09/26/android/multithreaded-rendering-on-android-with-litho-and-infer/)
11. [The Static Analyzer Infer in SV-COMP (Competition Contribution), Kettl and Lemberger, TACAS 2022](https://www.sosy-lab.org/research/pub/2022-SVCOMP.The_Static_Analyzer_Infer_in_SV-COMP.pdf)
12. [The Static Analyzer Infer in SV-COMP, LMU Munich publication record](https://epub.ub.uni-muenchen.de/110209/)
13. [Getafix: How Facebook tools learn to fix bugs automatically, Meta AI (2018)](https://ai.meta.com/blog/getafix-how-facebook-tools-learn-to-fix-bugs-automatically/)
14. [Open-sourcing Mariana Trench: Analyzing Android and Java app security in depth, Engineering at Meta (2021)](https://engineering.fb.com/2021/09/29/security/mariana-trench/)
15. [AL: A new declarative language for detecting bugs with Infer, Engineering at Meta (2017)](https://engineering.fb.com/2017/05/24/developer-tools/al-a-new-declarative-language-for-detecting-bugs-with-infer/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › 具名软件产品与平台 › 静态分析与代码质量工具

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/infer-static-analyzer

English original: https://www.edgepedia.org/infer-static-analyzer
