# libFuzzer

**libFuzzer** 是一个进程内、覆盖率引导、进化式的模糊测试（一种向目标输入随机变异数据以发现缺陷的自动化测试方法）引擎，属于 LLVM 项目的一部分。它与被测库链接在一起，通过一个特定的模糊测试入口点——称为目标函数——向库中送入经过模糊处理的输入；模糊器跟踪代码中哪些区域被到达，并对输入数据语料库生成变异，以最大化代码覆盖率。覆盖率信息由 LLVM 的 SanitizerCoverage 插桩提供。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 类型 | 进程内、覆盖率引导、进化式模糊测试引擎，属于 LLVM<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup> |
| 入口点 | 模糊测试目标实现 `LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)`，接收一个字节缓冲区并调用被测 API<sup>[2](https://github.com/Microsoft/llvm/blob/master/docs/LibFuzzer.rst)</sup> |
| 构建 | 使用 `-fsanitize=fuzzer` 编译并链接；Clang 6.0 及更高版本自带 libFuzzer，无需额外安装<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup> |
| 消毒器 | 通常与 AddressSanitizer（ASAN）或 UndefinedBehaviorSanitizer（UBSAN）结合使用，或两者兼用；MemorySanitizer（MSAN）支持为实验性<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup> |
| 资源限制 | 默认内存限制为 2048 MB（`-rss_limit_mb`），每个输入的默认超时为 1200 秒<sup>[2](https://github.com/Microsoft/llvm/blob/master/docs/LibFuzzer.rst)</sup> |
| 状态 | 原始作者已停止积极开发，转向 Centipede 模糊测试引擎；重要缺陷仍会修复，但预计不会再有重大新功能<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup> |
| 生产使用 | libFuzzer 是 OSS-Fuzz 唯一必需的模糊测试引擎，后者持续对 1000 多个开源项目进行模糊测试<sup>[3](https://github.com/google/oss-fuzz/blob/master/docs/getting-started/new_project_guide.md)</sup><sup> • </sup><sup>[4](https://github.com/google/oss-fuzz/blob/master/docs/research/target_generation.md)</sup> |

## 工作原理

libFuzzer 是一个进程内模糊器：模糊测试目标、被测库以及模糊器自身的 main 例程——负责语料库维护、输入选择和变异——全部链接到同一个可执行文件中。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup><sup> • </sup><sup>[5](https://www.doc.ic.ac.uk/~afd/homepages/papers/pdfs/2023/ICST.pdf)</sup> 引擎在一个循环中反复用变异后的输入调用目标函数，从而避免了进程外模糊器在每个测试用例上都要付出的进程重启开销。<sup>[6](https://fileserver-az.core.ac.uk/download/pdf/161427746.pdf)</sup>

覆盖率反馈来自编译时插入的 SanitizerCoverage 插桩。libFuzzer 组合了多种信号，它称之为特征：边覆盖、边计数器、值剖面以及间接调用者/被调用者对。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup> 通过额外的编译器标志 `-fsanitize-coverage=trace-cmp`（作为 `-fsanitize=fuzzer` 的一部分默认开启），libFuzzer 还会拦截 CMP 指令，并根据比较值引导变异。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup>

**模糊测试目标。** 对某个库使用 libFuzzer 首先要编写一个模糊测试目标，即一个接受字节数组并通过被测 API 对这些字节做有意义操作的函数。文档记载的签名为 `extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)`。<sup>[2](https://github.com/Microsoft/llvm/blob/master/docs/LibFuzzer.rst)</sup> 目标本身不依赖于 libFuzzer，因此同一个目标可以与其他模糊测试引擎（如 AFL 或 Radamsa）配合使用。<sup>[2](https://github.com/Microsoft/llvm/blob/master/docs/LibFuzzer.rst)</sup> 文档对一个好的目标提出的要求包括：容忍任何类型的输入、绝不调用 `exit()`、尽可能保持确定性、运行快速，并且最好不修改全局状态；一个能解析多种数据格式的目标应拆分为每种格式一个目标。<sup>[2](https://github.com/Microsoft/llvm/blob/master/docs/LibFuzzer.rst)</sup>

## 语料库与运行

像 libFuzzer 这样的覆盖率引导模糊器依赖于被测代码的样本输入语料库。理想情况下，语料库应以多样化的有效和无效输入集合作为种子；对于一个图形库，初始语料库可能包含各种小的 PNG、JPG 和 GIF 文件。模糊器基于当前语料库中的样本输入生成随机变异，任何触发此前未覆盖路径的变异都会被保存到语料库中供后续变异使用。libFuzzer 在没有初始种子的情况下也能工作，但当库接受复杂的结构化输入时效率较低。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup>

默认情况下，模糊测试过程会无限持续下去，至少直到发现缺陷为止。崩溃和消毒器失败会终止进程，触发缺陷的输入会被写入磁盘，通常命名为 `crash-<sha1>`、`leak-<sha1>` 或 `timeout-<sha1>`。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup> 如果在命令行上传递的是文件列表而非目录，二进制程序会将这些文件作为测试输入重新运行而不进行模糊测试，这使其可用于持续集成系统上的回归测试。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup>

每个 libFuzzer 进程都是单线程的，除非被测库启动了自己的线程，但多个进程可以针对共享的语料库目录并行运行；一个进程发现的新输入会通过定期重新加载对其他进程可用，除非用 `-reload=0` 禁用。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup> 大型语料库可以在保持完整覆盖率的同时用 `-merge=1` 标志进行最小化，该标志只将触发新覆盖率的输入复制到目标目录中。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup>

**资源限制。** 由于进程内模糊测试在模糊器自身的进程内运行不受信任的输入，失控的目标必须被约束。libFuzzer 默认将常驻内存限制为 2048 MB，由一个独立线程每秒检查一次，并将运行时间超过默认 1200 秒超时的输入视为失败；两个限制均可通过 `-rss_limit_mb` 和 `-timeout` 调整。<sup>[2](https://github.com/Microsoft/llvm/blob/master/docs/LibFuzzer.rst)</sup><sup> • </sup><sup>[7](https://github.com/google/fuzzing/blob/master/tutorial/libFuzzerTutorial.md)</sup> 当与 [AddressSanitizer](https://www.edgepedia.org/zh/addresssanitizer) 或 LeakSanitizer 结合使用时，libFuzzer 会在每次执行输入后尝试检测内存泄漏，并在发现泄漏时将复现用例保存到磁盘。<sup>[7](https://github.com/google/fuzzing/blob/master/tutorial/libFuzzerTutorial.md)</sup>

## 高级特性

libFuzzer 支持用户提供的字典，其中包含输入语言的关键词或其他有趣的字节序列（如多字节魔术值），通过 `-dict=DICTIONARY_FILE` 传入；对于某些输入语言，字典可以显著提高搜索速度。字典语法类似于 AFL 的 `-x` 选项。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup> libFuzzer 还可以与 AFL 在同一测试语料库上一起使用，因为两个模糊器都期望语料库位于一个目录中，每个输入一个文件。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup>

## 历史与状态

libFuzzer 被加入 LLVM 代码树，并由 Google 软件工程师、从事消毒器和模糊测试工具工作的 Kostya Serebryany（科斯佳·谢列布里亚内）在 2015 年 4 月 9 日的一篇 LLVM 项目博客文章中宣布。<sup>[8](https://blog.llvm.org/2015/04/fuzz-all-clangs.html)</sup> 该文章将进程内模糊测试的优势描述为每秒测试更多输入，相应的弱点是每个输入的执行时间无法有效限制；该项目自己的模糊测试机器人曾在发现 clang-format 中的指数级解析时间后几乎瘫痪。<sup>[8](https://blog.llvm.org/2015/04/fuzz-all-clangs.html)</sup> 作为早期演示，libFuzzer 应用于 OpenSSL 后在不到一分钟内发现了 [Heartbleed](https://www.edgepedia.org/zh/heartbleed) 缺陷，该工具还在 PCRE2、Glibc 和 MUSL libc 中发现了新缺陷。<sup>[8](https://blog.llvm.org/2015/04/fuzz-all-clangs.html)</sup>

LLVM 文档现在指出，libFuzzer 的原始作者已停止对其的积极开发，转向另一个模糊测试引擎 [Centipede](https://www.edgepedia.org/zh/centipede)——一个在 Google 开发的分布式引擎，面向非常大且非常慢的目标，libFuzzer 和 AFL 在这些目标上不一定能很好地扩展。libFuzzer 在重要缺陷会被修复的意义上仍完全受支持，但预计不会有重大新功能。<sup>[1](https://llvm.org/docs/LibFuzzer.html)</sup><sup> • </sup><sup>[9](https://github.com/google/fuzztest/blob/main/centipede/README.md)</sup> Centipede 开箱即支持基于 libFuzzer 的模糊测试目标：现有的 `LLVMFuzzerTestOneInput()` 目标只需用 Centipede 的编译器和链接器选项构建即可。<sup>[9](https://github.com/google/fuzztest/blob/main/centipede/README.md)</sup> Google 将合并了 Centipede 的 FuzzTest 描述为此前使用的模糊测试工具（如 libFuzzer）的继任者。<sup>[10](https://github.com/google/fuzztest)</sup>

## 在 OSS-Fuzz 和 ClusterFuzz 中的使用

libFuzzer 是 OSS-Fuzz 所要求的引擎，后者是 Google 面向开源软件的持续模糊测试服务，默认还会为 AFL++、Honggfuzz 和 Centipede 构建目标。<sup>[3](https://github.com/google/oss-fuzz/blob/master/docs/getting-started/new_project_guide.md)</sup> OSS-Fuzz 项目通过 `$LIB_FUZZING_ENGINE` 编译器参数将模糊测试目标链接到预构建的引擎库，而不是直接假定使用 libFuzzer。<sup>[3](https://github.com/google/oss-fuzz/blob/master/docs/getting-started/new_project_guide.md)</sup> ClusterFuzz 是 Google 用于自家产品并作为 OSS-Fuzz 后端的模糊测试基础设施，支持 libFuzzer、AFL++ 和 Honggfuzz 作为覆盖率引导引擎并进行集成模糊测试；截至 2022 年 5 月，它已在 Google 产品中发现超过 25,000 个缺陷，在 550 多个 OSS-Fuzz 项目中发现超过 36,000 个缺陷。<sup>[11](https://google.github.io/clusterfuzz/)</sup> 在 Windows 上，libFuzzer 是 ClusterFuzz 支持的唯一模糊测试引擎。<sup>[12](https://google.github.io/clusterfuzz/production-setup/setting-up-bots/)</sup>

## 与其他模糊器的比较

与 AFL 及其衍生工具的主要架构差异在于进程模型：AFL++ 在默认模式下是一个独立进程，启动目标并从插桩后的二进制文件中读回覆盖率，而 libFuzzer 目标被编译为库，引擎在同一进程中运行。<sup>[13](https://fuzze.rs/blog/libfuzzer-vs-afl-plus-plus)</sup><sup> • </sup><sup>[14](https://hysenlabs.com/projects/aflplusplus-aflplusplus)</sup> 插桩方式也不同：AFL++ 跟踪边覆盖，即基本块之间的转移，而 libFuzzer 的 SanitizerCoverage 插桩主要报告分支覆盖，即单个控制流决策。<sup>[15](https://www.ndss-symposium.org/wp-content/uploads/2026-s820-paper.pdf)</sup> AFL++ 还通过 QEMU、Frida 和 Unicorn 模式支持无法重新编译的目标，目标支持范围比 libFuzzer 更广。<sup>[16](https://faun.dev/toolbox/aflplusplus-vs-libfuzzer/)</sup>

基准测试结果并未给 libFuzzer 最高排名。2022 年 ICST 的一项研究在 FuzzBench 和一套精选的 12 个真实系统上比较了九种知名模糊器，发现 honggfuzz 和 AFL++ 是通用模糊测试效果的最佳选择，libFuzzer 属于落后组。<sup>[17](https://ieeexplore.ieee.org/document/9787836/)</sup> USENIX Security 2023 的一项变异分析研究测得 libFuzzer 杀死了 7.5% 的变异体，落后于 AFL++ 的 8.8%、Honggfuzz 的 8.7% 和 AFL 的 8.4%。<sup>[18](https://www.usenix.org/system/files/usenixsecurity23-gorz.pdf)</sup> 后来的基准测试工作发现，libFuzzer 的相对性能随着程序规模和平均执行时间的增加而下降，而在种子输入执行更快的较小程序上差距缩小，libFuzzer 的扩展 Entropic 表现最佳。<sup>[19](https://doi.org/10.1145/3732936)</sup>

**libFuzzer 的适用场景。** 项目自身的文档指出，最佳适用场景是输入相对较小、每个输入运行时间低于 10 毫秒、且代码预计不会在无效输入上崩溃的库，并以解析器、正则表达式匹配器、压缩库、网络库和加密库为例。当目标的输入验证器遇到无效输入会触发断言失败或崩溃时，当库在多次运行之间保留大量全局状态时，或当持久线程存活时间超过单个测试时，进程内模糊测试不适用，因为结果会变得不可靠。<sup>[2](https://github.com/Microsoft/llvm/blob/master/docs/LibFuzzer.rst)</sup>

## References

1. [libFuzzer – a library for coverage-guided fuzz testing, LLVM documentation](https://llvm.org/docs/LibFuzzer.html)
2. [docs/LibFuzzer.rst, llvm repository (Microsoft mirror)](https://github.com/Microsoft/llvm/blob/master/docs/LibFuzzer.rst)
3. [Setting up a new project, OSS-Fuzz documentation](https://github.com/google/oss-fuzz/blob/master/docs/getting-started/new_project_guide.md)
4. [Fuzz target generation using LLMs, OSS-Fuzz documentation](https://github.com/google/oss-fuzz/blob/master/docs/research/target_generation.md)
5. [To use libFuzzer, one must write a fuzz target (ICST 2023 paper, Imperial College London)](https://www.doc.ic.ac.uk/~afd/homepages/papers/pdfs/2023/ICST.pdf)
6. [Appendix describing LibFuzzer as an in-process library fuzzer inspired by AFL (CORE PDF)](https://fileserver-az.core.ac.uk/download/pdf/161427746.pdf)
7. [libFuzzerTutorial, google/fuzzing repository](https://github.com/google/fuzzing/blob/master/tutorial/libFuzzerTutorial.md)
8. [Simple guided fuzzing for libraries using LLVM's new libFuzzer, The LLVM Project Blog, April 9, 2015](https://blog.llvm.org/2015/04/fuzz-all-clangs.html)
9. [Centipede README, google/fuzztest repository](https://github.com/google/fuzztest/blob/main/centipede/README.md)
10. [google/fuzztest repository](https://github.com/google/fuzztest)
11. [ClusterFuzz documentation](https://google.github.io/clusterfuzz/)
12. [Setting up bots, ClusterFuzz documentation](https://google.github.io/clusterfuzz/production-setup/setting-up-bots/)
13. [libFuzzer vs AFL++: Choosing the Right Fuzzer for Your Project, Fuzze.rs](https://fuzze.rs/blog/libfuzzer-vs-afl-plus-plus)
14. [AFL++ install and fuzzing tutorial, Hysen Labs](https://hysenlabs.com/projects/aflplusplus-aflplusplus)
15. [NDSS Symposium paper on coverage metrics for AFL++ and LibFuzzer](https://www.ndss-symposium.org/wp-content/uploads/2026-s820-paper.pdf)
16. [AFL++ vs libFuzzer: Coverage-Guided Fuzzers Comparison, Faun.dev](https://faun.dev/toolbox/aflplusplus-vs-libfuzzer/)
17. [Comparing Fuzzers on a Level Playing Field with FuzzBench, ICST 2022, IEEE](https://ieeexplore.ieee.org/document/9787836/)
18. [Systematic Assessment of Fuzzers using Mutation Analysis, USENIX Security 2023](https://www.usenix.org/system/files/usenixsecurity23-gorz.pdf)
19. [Fuzzing: On Benchmarking Outcome as a Function of Benchmark Properties, ACM TOSEM](https://doi.org/10.1145/3732936)

---
*Initially written Oct 15, 2026*

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

Copyright 2026 Edgepedia

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

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

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