address-sanitizer by trailofbits/skills
npx skills add https://github.com/trailofbits/skills --skill address-sanitizerAddressSanitizer (ASan) 是一款广泛采用的内存错误检测工具,在软件测试(尤其是模糊测试)中被大量使用。它有助于检测那些可能被忽略的内存损坏错误,例如缓冲区溢出、释放后使用错误以及其他内存安全违规行为。
ASan 因其在识别内存漏洞方面的有效性,已成为模糊测试中的标准实践。它在编译时对代码进行插桩,以跟踪内存分配和访问,并在运行时检测非法操作。
| 概念 | 描述 |
|---|---|
| 插桩 | ASan 在编译期间为内存操作添加运行时检查 |
| 影子内存 | 映射 20TB 的虚拟内存来跟踪分配状态 |
| 性能开销 | 与非插桩代码相比,速度大约降低 2-4 倍 |
| 检测范围 | 发现缓冲区溢出、释放后使用、双重释放和内存泄漏 |
在以下情况应用此技术:
在以下情况跳过此技术:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 任务 | 命令/模式 |
|---|
| 启用 ASan (Clang/GCC) | -fsanitize=address |
| 启用详细输出 | ASAN_OPTIONS=verbosity=1 |
| 禁用泄漏检测 | ASAN_OPTIONS=detect_leaks=0 |
| 出错时强制中止 | ASAN_OPTIONS=abort_on_error=1 |
| 多个选项 | ASAN_OPTIONS=verbosity=1:abort_on_error=1 |
使用 -fsanitize=address 标志编译和链接您的代码:
clang -fsanitize=address -g -o my_program my_program.c
建议使用 -g 标志,以便在 ASan 检测到错误时获得更好的堆栈跟踪。
设置 ASAN_OPTIONS 环境变量以配置 ASan 行为:
export ASAN_OPTIONS=verbosity=1:abort_on_error=1:detect_leaks=0
执行经过 ASan 插桩的二进制文件。当检测到内存错误时,ASan 将打印详细的报告:
./my_program
ASan 需要大约 20TB 的虚拟内存。请禁用模糊测试器的内存限制:
-rss_limit_mb=0-m none用例: 使用 ASan 的标准模糊测试设置
之前:
clang -o fuzz_target fuzz_target.c
./fuzz_target
之后:
clang -fsanitize=address -g -o fuzz_target fuzz_target.c
ASAN_OPTIONS=verbosity=1:abort_on_error=1 ./fuzz_target
用例: 为单元测试套件启用 ASan
之前:
gcc -o test_suite test_suite.c -lcheck
./test_suite
之后:
gcc -fsanitize=address -g -o test_suite test_suite.c -lcheck
ASAN_OPTIONS=detect_leaks=1 ./test_suite
| 技巧 | 原因 |
|---|---|
使用 -g 标志 | 为调试提供详细的堆栈跟踪 |
设置 verbosity=1 | 在程序启动前确认 ASan 已启用 |
| 模糊测试期间禁用泄漏检测 | 泄漏检测不会导致立即崩溃,但会使输出混乱 |
启用 abort_on_error=1 | 某些模糊测试器需要 abort() 而不是 _exit() |
当 ASan 检测到内存错误时,它会打印一份详细的报告,包括:
ASan 报告示例:
==12345==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60300000eff4 at pc 0x00000048e6a3
READ of size 4 at 0x60300000eff4 thread T0
#0 0x48e6a2 in main /path/to/file.c:42
ASan 可以与其他检测器结合使用,以实现全面的检测:
clang -fsanitize=address,undefined -g -o fuzz_target fuzz_target.c
Linux:完整的 ASan 支持,性能最佳 macOS:支持有限,某些功能可能无法工作 Windows:实验性支持,不建议用于生产环境模糊测试
| 反面模式 | 问题 | 正确方法 |
|---|---|---|
| 在生产环境中使用 ASan | 可能降低应用程序的安全性 | 仅在测试时使用 ASan |
| 不禁用内存限制 | 模糊测试器可能因 20TB 虚拟内存而终止进程 | 设置 -rss_limit_mb=0 或 -m none |
| 忽略泄漏报告 | 内存泄漏表明存在资源管理问题 | 在模糊测试活动结束时审查泄漏报告 |
同时使用模糊测试器和地址检测器进行编译:
clang++ -fsanitize=fuzzer,address -g harness.cc -o fuzz
以无限制 RSS 运行:
./fuzz -rss_limit_mb=0
集成技巧:
-fsanitize=fuzzer 与 -fsanitize=address 结合使用-g 在崩溃报告中获得详细的堆栈跟踪ASAN_OPTIONS=abort_on_error=1 以获得更好的崩溃处理使用 AFL_USE_ASAN 环境变量:
AFL_USE_ASAN=1 afl-clang-fast++ -g harness.cc -o fuzz
以无限制内存运行:
afl-fuzz -m none -i input_dir -o output_dir ./fuzz
集成技巧:
AFL_USE_ASAN=1 会自动添加正确的编译标志-m none 来禁用 AFL++ 的内存限制AFL_MAP_SIZE使用 --sanitizer=address 标志:
cargo fuzz run fuzz_target --sanitizer=address
或者在 fuzz/Cargo.toml 中配置:
[profile.release]
opt-level = 3
debug = true
集成技巧:
使用 ASan 编译并与 honggfuzz 链接:
honggfuzz -i input_dir -o output_dir -- ./fuzz_target_asan
编译目标:
hfuzz-clang -fsanitize=address -g target.c -o fuzz_target_asan
集成技巧:
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 模糊测试器立即终止进程 | 内存限制对于 ASan 的 20TB 虚拟内存来说太低 | 使用 -rss_limit_mb=0 (libFuzzer) 或 -m none (AFL++) |
| "ASan 运行时未初始化" | 链接顺序错误或缺少运行时 | 确保在编译和链接中都使用了 -fsanitize=address |
| 泄漏报告使输出混乱 | 默认启用了 LeakSanitizer | 设置 ASAN_OPTIONS=detect_leaks=0 |
| 性能差(>4 倍减速) | 调试模式或未优化的构建 | 在 -fsanitize=address 的同时使用 -O2 或 -O3 进行编译 |
| ASan 未检测到明显的错误 | 二进制文件未插桩 | 使用 ASAN_OPTIONS=verbosity=1 检查 ASan 是否打印启动信息 |
| 误报 | 拦截器冲突 | 查看 ASan 常见问题解答,了解特定库的已知问题 |
| 技能 | 如何应用 |
|---|---|
| libfuzzer | 使用 -fsanitize=fuzzer,address 编译,实现集成模糊测试与内存错误检测 |
| aflpp | 在编译期间使用 AFL_USE_ASAN=1 环境变量 |
| cargo-fuzz | 使用 --sanitizer=address 标志为 Rust 模糊测试目标启用 ASan |
| honggfuzz | 使用 -fsanitize=address 编译目标,进行 ASan 插桩的模糊测试 |
| 技能 | 关系 |
|---|---|
| undefined-behavior-sanitizer | 常与 ASan 结合使用,以实现全面的错误检测(未定义行为 + 内存错误) |
| fuzz-harness-writing | 必须设计测试框架以处理 ASan 检测到的崩溃并避免误报 |
| coverage-analysis | 覆盖率引导的模糊测试有助于触发 ASan 可以检测到内存错误的代码路径 |
官方 ASan 文档涵盖:
所有检测器共享的通用配置标志:
verbosity:控制诊断输出级别log_path:将检测器输出重定向到文件symbolize:启用/禁用报告中的符号解析external_symbolizer_path:使用自定义符号解析器ASan 特定配置选项:
detect_leaks:控制内存泄漏检测abort_on_error:出错时调用 abort() 还是 _exit()detect_stack_use_after_return:检测栈返回后使用错误check_initialization_order:查找初始化顺序错误常见陷阱和解决方案:
Clang 特定指南:
GCC 特定 ASan 文档:
包含技术细节的原始研究论文:
每周安装量
1.2K
代码库
GitHub 星标数
4.0K
首次出现
Jan 19, 2026
安全审计
安装于
claude-code1.0K
opencode994
gemini-cli975
codex970
cursor946
github-copilot918
AddressSanitizer (ASan) is a widely adopted memory error detection tool used extensively during software testing, particularly fuzzing. It helps detect memory corruption bugs that might otherwise go unnoticed, such as buffer overflows, use-after-free errors, and other memory safety violations.
ASan is a standard practice in fuzzing due to its effectiveness in identifying memory vulnerabilities. It instruments code at compile time to track memory allocations and accesses, detecting illegal operations at runtime.
| Concept | Description |
|---|---|
| Instrumentation | ASan adds runtime checks to memory operations during compilation |
| Shadow Memory | Maps 20TB of virtual memory to track allocation state |
| Performance Cost | Approximately 2-4x slowdown compared to non-instrumented code |
| Detection Scope | Finds buffer overflows, use-after-free, double-free, and memory leaks |
Apply this technique when:
Skip this technique when:
| Task | Command/Pattern |
|---|---|
| Enable ASan (Clang/GCC) | -fsanitize=address |
| Enable verbosity | ASAN_OPTIONS=verbosity=1 |
| Disable leak detection | ASAN_OPTIONS=detect_leaks=0 |
| Force abort on error | ASAN_OPTIONS=abort_on_error=1 |
| Multiple options | ASAN_OPTIONS=verbosity=1:abort_on_error=1 |
Compile and link your code with the -fsanitize=address flag:
clang -fsanitize=address -g -o my_program my_program.c
The -g flag is recommended to get better stack traces when ASan detects errors.
Set the ASAN_OPTIONS environment variable to configure ASan behavior:
export ASAN_OPTIONS=verbosity=1:abort_on_error=1:detect_leaks=0
Execute the ASan-instrumented binary. When memory errors are detected, ASan will print detailed reports:
./my_program
ASan requires approximately 20TB of virtual memory. Disable fuzzer memory restrictions:
-rss_limit_mb=0-m noneUse Case: Standard fuzzing setup with ASan
Before:
clang -o fuzz_target fuzz_target.c
./fuzz_target
After:
clang -fsanitize=address -g -o fuzz_target fuzz_target.c
ASAN_OPTIONS=verbosity=1:abort_on_error=1 ./fuzz_target
Use Case: Enable ASan for unit test suite
Before:
gcc -o test_suite test_suite.c -lcheck
./test_suite
After:
gcc -fsanitize=address -g -o test_suite test_suite.c -lcheck
ASAN_OPTIONS=detect_leaks=1 ./test_suite
| Tip | Why It Helps |
|---|---|
Use -g flag | Provides detailed stack traces for debugging |
Set verbosity=1 | Confirms ASan is enabled before program starts |
| Disable leaks during fuzzing | Leak detection doesn't cause immediate crashes, clutters output |
Enable abort_on_error=1 | Some fuzzers require abort() instead of _exit() |
When ASan detects a memory error, it prints a detailed report including:
Example ASan report:
==12345==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60300000eff4 at pc 0x00000048e6a3
READ of size 4 at 0x60300000eff4 thread T0
#0 0x48e6a2 in main /path/to/file.c:42
ASan can be combined with other sanitizers for comprehensive detection:
clang -fsanitize=address,undefined -g -o fuzz_target fuzz_target.c
Linux : Full ASan support with best performance macOS : Limited support, some features may not work Windows : Experimental support, not recommended for production fuzzing
| Anti-Pattern | Problem | Correct Approach |
|---|---|---|
| Using ASan in production | Can make applications less secure | Use ASan only for testing |
| Not disabling memory limits | Fuzzer may kill process due to 20TB virtual memory | Set -rss_limit_mb=0 or -m none |
| Ignoring leak reports | Memory leaks indicate resource management issues | Review leak reports at end of fuzzing campaign |
Compile with both fuzzer and address sanitizer:
clang++ -fsanitize=fuzzer,address -g harness.cc -o fuzz
Run with unlimited RSS:
./fuzz -rss_limit_mb=0
Integration tips:
-fsanitize=fuzzer with -fsanitize=address-g for detailed stack traces in crash reportsASAN_OPTIONS=abort_on_error=1 for better crash handlingSee: libFuzzer: AddressSanitizer
Use the AFL_USE_ASAN environment variable:
AFL_USE_ASAN=1 afl-clang-fast++ -g harness.cc -o fuzz
Run with unlimited memory:
afl-fuzz -m none -i input_dir -o output_dir ./fuzz
Integration tips:
AFL_USE_ASAN=1 automatically adds proper compilation flags-m none to disable AFL++'s memory limitAFL_MAP_SIZE for programs with large coverage mapsUse the --sanitizer=address flag:
cargo fuzz run fuzz_target --sanitizer=address
Or configure in fuzz/Cargo.toml:
[profile.release]
opt-level = 3
debug = true
Integration tips:
See: cargo-fuzz: AddressSanitizer
Compile with ASan and link with honggfuzz:
honggfuzz -i input_dir -o output_dir -- ./fuzz_target_asan
Compile the target:
hfuzz-clang -fsanitize=address -g target.c -o fuzz_target_asan
Integration tips:
| Issue | Cause | Solution |
|---|---|---|
| Fuzzer kills process immediately | Memory limit too low for ASan's 20TB virtual memory | Use -rss_limit_mb=0 (libFuzzer) or -m none (AFL++) |
| "ASan runtime not initialized" | Wrong linking order or missing runtime | Ensure -fsanitize=address used in both compile and link |
| Leak reports clutter output | LeakSanitizer enabled by default | Set ASAN_OPTIONS=detect_leaks=0 |
| Poor performance (>4x slowdown) | Debug mode or unoptimized build | Compile with -O2 or alongside |
| Skill | How It Applies |
|---|---|
| libfuzzer | Compile with -fsanitize=fuzzer,address for integrated fuzzing with memory error detection |
| aflpp | Use AFL_USE_ASAN=1 environment variable during compilation |
| cargo-fuzz | Use --sanitizer=address flag to enable ASan for Rust fuzz targets |
| honggfuzz | Compile target with -fsanitize=address for ASan-instrumented fuzzing |
| Skill | Relationship |
|---|---|
| undefined-behavior-sanitizer | Often used together with ASan for comprehensive bug detection (undefined behavior + memory errors) |
| fuzz-harness-writing | Harnesses must be designed to handle ASan-detected crashes and avoid false positives |
| coverage-analysis | Coverage-guided fuzzing helps trigger code paths where ASan can detect memory errors |
AddressSanitizer on Google Sanitizers Wiki
The official ASan documentation covers:
Common configuration flags shared across all sanitizers:
verbosity: Control diagnostic output levellog_path: Redirect sanitizer output to filessymbolize: Enable/disable symbol resolution in reportsexternal_symbolizer_path: Use custom symbolizerASan-specific configuration options:
detect_leaks: Control memory leak detectionabort_on_error: Call abort() vs _exit() on errordetect_stack_use_after_return: Detect stack use-after-return bugscheck_initialization_order: Find initialization order bugsCommon pitfalls and solutions:
Clang AddressSanitizer Documentation
Clang-specific guidance:
GCC-specific ASan documentation:
AddressSanitizer: A Fast Address Sanity Checker (USENIX Paper)
Original research paper with technical details:
Weekly Installs
1.2K
Repository
GitHub Stars
4.0K
First Seen
Jan 19, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code1.0K
opencode994
gemini-cli975
codex970
cursor946
github-copilot918
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
-O3-fsanitize=address| ASan not detecting obvious bugs | Binary not instrumented | Check with ASAN_OPTIONS=verbosity=1 that ASan prints startup info |
| False positives | Interceptor conflicts | Check ASan FAQ for known issues with specific libraries |