fuzzing
它能做什么
这个技能向你的智能体展示如何使用 FuzzTest 框架编写并注册模糊测试。适合希望测试读取不可信输入的代码的开发者。
适用场景
- ✓当你让 AI 在 C++ 单元测试文件中添加模糊测试时。
- ✓当你想用随机输入检查解析器或数据处理程序是否会崩溃时。
- ✓当你需要设置一个启用模糊测试的构建输出文件夹时。
技能链接
https://github.com/chromium/chromium/blob/main/agents/skills/fuzzing/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“能为我的 C++ URL 解析器编写一个模糊测试,用随机输入检查它是否会崩溃吗?”
每周 5 个新技能,1 分钟读完。
工作原理
技能指导 AI 编写一个属性函数,检查代码的某项规则,然后用 FUZZ_TEST 宏注册它。之后,模糊测试工具会尝试大量输入,寻找失败的情况。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Fuzzing (Chromium)
1. Setup
Ensure the output directory is configured:
gn gen out/fuzz --args='enable_fuzztest_fuzz=true is_debug=false is_asan=true \
is_component_build=false use_remoteexec=true'
2. Implement the FUZZ_TEST
Add to *_unittest.cc alongside existing tests:
#include "third_party/fuzztest/src/fuzztest/fuzztest.h"
#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
// 1. Define the property function. Use a descriptive name that reflects
// the property being tested (e.g., "ParseFooDoesNotCrash" or "RoundTripIsLossless").
void MyPropertyFunctionDoesNotCrash(int i, const std::string& s) {
// Call code under test. Focus on functions parsing untrusted input, complex
// state machines, or data processing.
bool result = MyComponent::DoSomething(i, s);
// Add test assertions about invariants (e.g. "roundtrip equality", "valid
// output structure"). Sanitizers like ASAN catch crashes.
EXPECT_TRUE(result);
}
// 2. Register with FUZZ_TEST macro
FUZZ_TEST(MyComponentFuzzTest, MyPropertyFunctionDoesNotCrash)
.WithDomains(
fuzztest::InRange(0, 100),
fuzztest::Arbitrary<std::string>()
);
For complex types:
- Construct from primitives: If the object has a parsing constructor (e.g.
GURL(string)), accept the primitive and construct it inside your test function. - Define a local domain: Use
fuzztest::Constructororfuzztest::Mapto build valid objects.auto ArbitraryFoo() { return fuzztest::Constructor<Foo>(fuzztest::InRange(0, 10)); }
3. Register in BUILD.gn
You MUST register the test in the fuzztests list (in alphabetical order)
of the executable test target.
Case A: File is in a test() target
test("my_component_unittests") {
sources = [ "my_component_unittest.cc" ]
# Format: SuiteName.TestName
fuzztests = [
"MyComponentFuzzTest.MyPropertyFunctionDoesNotCrash",
]
# No dependency changes are needed here. The build system
# automatically adds FuzzTest dependencies for targets
# with a `fuzztests` list.
}
Case B: File is in a source_set(): Add //third_party/fuzztest:fuzztest
to deps.
source_set("tests") {
sources = [ "my_component_unittest.cc" ]
deps = [
"//third_party/fuzztest:fuzztest",
# ...
]
}
Find the executable test() target that depends on this source_set():
gn refs out/fuzz //path/to:source_set --testonly=true --type=executable --all
Then, ensure it lists the fuzz test in its fuzztests variable (in alphabetical
order).
4. Mandatory verification workflow
The task is incomplete until you successfully execute this sequence:
- Find the target and Build
Find the executable test() target that contains your test file:
gn refs out/fuzz //path/to/my_component_unittest.cc --type=executable --all
Build the identified target (e.g. unit_tests or browser_tests):
autoninja --quiet -C out/fuzz <target_name>
Note: If the build fails with an include not allowed by DEPS error, see the
Common Issues & Fixes section below.
- Verify unit tests pass
./out/fuzz/<target_name> \
--gtest_filter="MyComponentFuzzTest.MyPropertyFunctionDoesNotCrash"
- Verify fuzzing mode doesn't crash
./out/fuzz/<target_name> \
--fuzz="MyComponentFuzzTest.MyPropertyFunctionDoesNotCrash" --fuzz_for=10s
Resources
- Chromium Guide:
testing/libfuzzer/getting_started.md - Macro Usage:
third_party/fuzztest/src/doc/fuzz-test-macro.md - Domains:
third_party/fuzztest/src/doc/domains-reference.md - Fixtures:
third_party/fuzztest/src/doc/fixtures.md
Common Issues & Fixes
Build failure: include not allowed by DEPS
Symptom: Build fails with an error like:
ERROR: include not allowed by DEPS: third_party/fuzztest/...
Fix: You must allow the fuzztest include in the nearest DEPS file
(usually in the same directory as your test or a parent directory). Add
+third_party/fuzztest to specific_include_rules for your test files:
specific_include_rules = {
".*_unittest\\.cc": [
"+third_party/fuzztest",
],
}
常见问题
什么是模糊测试?
模糊测试向代码发送大量随机或异常的输入,以找出崩溃或被破坏的规则。它擅长发现普通测试遗漏的错误。
我需要是 C++ 专家才能使用吗?
需要一定的 C++ 知识才能用好它。技能会帮你的 AI 编写测试代码和构建设置。
这个技能能用于 Chromium 项目之外吗?
示例来自 Chromium,但 FuzzTest 模式是一个通用框架,其步骤可以应用于其他 C++ 项目。
详情
作者的原始描述
Implements, registers, and verifies fuzz tests in Chromium. Use when the user asks to add or write fuzzers in C++, or mentions fuzz testing or FUZZ_TEST.
repo: chromium/chromium
热度
- GitHub 星标
- 25k
- 排名
- #10057
- 许可证
- BSD-3-Clause
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
gn-deps-debugging
25k帮你的智能体修复 GN 构建文件中因依赖缺失或错误而导致的构建错误,且不添加范围过大、不必要的依赖。
java-memory-leaks
25k帮你的智能体通过读取 LeakCanary 的泄漏报告,查找并修复 Android 应用中的 Java 内存泄漏。
jni-type-conversion
25k帮你的智能体用更简单的 @JniType 注解,替换 Android 代码中手动的 Java 到 C++ 转换代码。
loadline
25k帮你的智能体运行 LoadLine 基准测试,衡量网页在 Android 和桌面版 Chrome 上的加载速度。