Communitygithub.com

SamuelChien/mega-skills-collection

AddressSanitizer 可在模糊測試期間檢測記憶體錯誤。在對 C/C++ 程式碼進行模糊測試時使用,以發現緩衝區溢位和釋放後使用等錯誤。

mega-skills-collection 是什麼?

AddressSanitizer (ASan) 是一種廣泛使用的記憶體錯誤檢測工具,特別適用於軟體測試,尤其是模糊測試。它能在編譯時對程式碼進行插樁,追蹤記憶體分配和存取,並在運行時偵測非法操作,從而發現緩衝區溢位、釋放後使用、雙重釋放和記憶體洩漏等記憶體損壞錯誤。ASan 透過陰影記憶體(Shadow Memory)映射 20TB 虛擬記憶體來追蹤分配狀態,但會導致約 2-4 倍的效能開銷。此工具主要支援 Linux 平台,對 macOS 和 Windows 的支援有限,不適合用於生產環境。在使用 ASan 時,需透過編譯選項 `-fsanitize=address` 啟用,並可透過 `ASAN_OPTIONS` 環境變數進行配置,例如設定 `verbosity=1` 確認啟用狀態或 `abort_on_error=1` 強制終止程式。針對模糊測試工具如 libFuzzer 和 AFL++,ASan 能提供無縫整合:libFuzzer 需同時啟用 `-fsanitize=fuzzer,address` 並設定 `-rss_limit_mb=0` 關閉記憶體限制;AFL++ 則可透過 `AFL_USE_ASAN=1` 環境變數自動添加編譯標記,並以 `-m none` 取消記憶體上限。ASan 還能與其他清理工具(如 UndefinedBehaviorSanitizer)結合,實現更全面的錯誤檢測。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/SamuelChien/mega-skills-collection/tree/HEAD/address-sanitizer

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

AddressSanitizer (ASan)

AddressSanitizer (ASan) 是一種廣泛採用的記憶體錯誤偵測工具,在軟體測試(特別是模糊測試)中大量使用。它有助於偵測可能被忽略的記憶體損毀錯誤,例如緩衝區溢位、釋放後使用錯誤及其他記憶體安全違規。

概述

ASan 是模糊測試中的標準做法,因其在識別記憶體漏洞方面的有效性。它在編譯時對程式碼進行插樁,以追蹤記憶體分配和存取,並在執行時偵測非法操作。

關鍵概念

概念描述
插樁ASan 在編譯期間為記憶體操作添加執行時檢查
影子記憶體映射 20TB 的虛擬記憶體以追蹤分配狀態
效能成本與未插樁程式碼相比,速度降低約 2-4 倍
偵測範圍發現緩衝區溢位、釋放後使用、雙重釋放及記憶體洩漏

何時應用

在以下情況應用此技術:

  • 對 C/C++ 程式碼進行模糊測試以尋找記憶體安全漏洞
  • 測試帶有不安全區塊的 Rust 程式碼
  • 除錯與記憶體損毀相關的崩潰
  • 執行單元測試時懷疑有記憶體錯誤

在以下情況跳過此技術:

  • 執行生產環境程式碼(ASan 可能降低安全性)
  • 平台為 Windows 或 macOS(ASan 支援有限)
  • 效能開銷對您的使用案例不可接受
  • 對沒有 FFI 的純安全語言進行模糊測試(例如純 Go、純 Java)

快速參考

任務命令/模式
啟用 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

逐步指南

步驟 1:使用 ASan 編譯

使用 -fsanitize=address 標誌編譯和連結您的程式碼:

clang -fsanitize=address -g -o my_program my_program.c

建議使用 -g 標誌,以便在 ASan 偵測到錯誤時獲得更好的堆疊追蹤。

步驟 2:設定 ASan 選項

設定 ASAN_OPTIONS 環境變數以配置 ASan 行為:

export ASAN_OPTIONS=verbosity=1:abort_on_error=1:detect_leaks=0

步驟 3:執行程式

執行經過 ASan 插樁的二進位檔案。當偵測到記憶體錯誤時,ASan 會列印詳細報告:

./my_program

步驟 4:調整模糊測試器記憶體限制

ASan 需要約 20TB 的虛擬記憶體。停用模糊測試器的記憶體限制:

  • libFuzzer:-rss_limit_mb=0
  • AFL++:-m none

常見模式

模式:基本 ASan 整合

使用案例: 使用 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 搭配單元測試

使用案例: 對單元測試套件啟用 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 偵測到記憶體錯誤時,會列印詳細報告,包括:

  • 錯誤類型:緩衝區溢位、釋放後使用等
  • 堆疊追蹤:錯誤發生的位置
  • 分配/釋放追蹤:記憶體被分配/釋放的位置
  • 記憶體映射:錯誤周圍的影子記憶體狀態

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
忽略洩漏報告記憶體洩漏表示資源管理問題在模糊測試活動結束時審查洩漏報告

工具特定指南

libFuzzer

同時使用模糊測試器和位址清理器編譯:

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 以獲得更好的崩潰處理

請參閱:libFuzzer: AddressSanitizer

AFL++

使用 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

請參閱:AFL++: AddressSanitizer

cargo-fuzz (Rust)

使用 --sanitizer=address 標誌:

cargo fuzz run fuzz_target --sanitizer=address

或在 fuzz/Cargo.toml 中配置:

[profile.release]
opt-level = 3
debug = true

整合提示:

  • ASan 對於模糊測試不安全的 Rust 程式碼或 FFI 邊界很有用
  • 安全的 Rust 程式碼可能受益較少(編譯器已防止許多錯誤)
  • 專注於不安全區塊、原始指標和 C 函式庫綁定

請參閱:cargo-fuzz: AddressSanitizer

honggfuzz

使用 ASan 編譯並連結 honggfuzz:

honggfuzz -i input_dir -o output_dir -- ./fuzz_target_asan

編譯目標:

hfuzz-clang -fsanitize=address -g target.c -o fuzz_target_asan

整合提示:

  • honggfuzz 開箱即用與 ASan 搭配良好
  • 使用反饋驅動模式以獲得更好的覆蓋率與清理器搭配
  • 監控記憶體使用,因為 ASan 增加了記憶體佔用

故障排除

問題原因解決方案
模糊測試器立即終止程序記憶體限制過低,無法滿足 ASan 的 20TB 虛擬記憶體需求使用 -rss_limit_mb=0(libFuzzer)或 -m none(AFL++)
"ASan runtime not initialized"連結順序錯誤或缺少執行時期庫確保在編譯和連結時都使用了 -fsanitize=address
洩漏報告使輸出混亂LeakSanitizer 預設為啟用設定 ASAN_OPTIONS=detect_leaks=0
效能差(超過 4 倍減速)除錯模式或未最佳化的建置搭配 -O2 或 -O3 以及 -fsanitize=address 編譯
ASan 未偵測到明顯錯誤二進位檔案未經插樁使用 ASAN_OPTIONS=verbosity=1 檢查 ASan 是否輸出了啟動資訊
誤報攔截器衝突檢查 ASan FAQ 以獲取特定函式庫的已知問題

相關技能

使用此技術的工具

技能如何應用
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 可以偵測記憶體錯誤的程式碼路徑

資源

主要外部資源

AddressSanitizer on Google Sanitizers Wiki

官方 ASan 文件涵蓋:

  • 演算法和實作細節
  • 完整偵測到的錯誤類型列表
  • 效能特性和開銷
  • 平台特定行為
  • 已知限製和不相容性

SanitizerCommonFlags

所有清理器共用的配置標誌:

  • verbosity:控制診斷輸出層級
  • log_path:將清理器輸出重新導向到檔案
  • symbolize:啟用/停用報告中的符號解析
  • external_symbolizer_path:使用自訂符號化工具

AddressSanitizerFlags

ASan 特定配置選項:

  • detect_leaks:控制記憶體洩漏偵測
  • abort_on_error:在錯誤時呼叫 abort() 與 _exit()
  • detect_stack_use_after_return:偵測堆疊使用後返回錯誤
  • check_initialization_order:尋找初始化順序錯誤

AddressSanitizer FAQ

常見陷阱和解決方案:

  • 連結順序問題
  • 與其他工具的衝突
  • 平台特定問題
  • 效能調優技巧

Clang AddressSanitizer Documentation

Clang 特定指南:

  • 編譯標誌和選項
  • 與其他 Clang 功能的互動
  • 支援的平台和架構

GCC Instrumentation Options

GCC 特定 ASan 文件:

  • GCC 特定標誌和行為
  • 與 Clang 實作的差異
  • GCC 中的平台支援

AddressSanitizer: A Fast Address Sanity Checker (USENIX Paper)

包含技術細節的原始研究論文:

  • 影子記憶體演算法
  • 虛擬記憶體需求(歷史上為 16TB,現在約為 20TB)
  • 效能基準測試
  • 設計決策和權衡

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

SamuelChien/mega-skills-collection

Security audit, hardening, threat modeling (STRIDE/PASTA), Red/Blue Team, OWASP checks, code review, incident response, and infrastructure security for any project.

SamuelChien/mega-skills-collection

Arquitecto de Soluciones Principal y Consultor Tecnológico de Andru.ia. Diagnostica y traza la hoja de ruta óptima para proyectos de IA en español.

SamuelChien/mega-skills-collection

Ingeniero de Sistemas de Andru.ia. Diseña, redacta y despliega nuevas habilidades (skills) dentro del repositorio siguiendo el Estándar de Diamante.

SamuelChien/mega-skills-collection

Estratega de Inteligencia de Dominio de Andru.ia. Analiza el nicho específico de un proyecto para inyectar conocimientos, regulaciones y estándares únicos del sector. Actívalo tras definir el nicho.

SamuelChien/mega-skills-collection

2D game development principles. Sprites, tilemaps, physics, camera.

SamuelChien/mega-skills-collection

3D game development principles. Rendering, shaders, physics, cameras.

SamuelChien/mega-skills-collection

Expert in building 3D experiences for the web - Three.js, React Three Fiber, Spline, WebGL, and interactive 3D scenes. Covers product configurators, 3D portfolios, immersive websites, and bringing depth to web experiences.

SamuelChien/mega-skills-collection

Accessibility audit skill for scanning, fixing, and verifying WCAG 2.2 Level A and AA compliance across React, Next.js, Vue, Angular, Svelte, and plain HTML codebases. Use when auditing accessibility, fixing a11y violations, checking color contrast, generating compliance reports, or integrating accessibility checks into CI/CD pipelines.

SamuelChien/mega-skills-collection

Ab Test Analyzer - Auto-activating skill for Data Analytics. Triggers on: ab test analyzer, ab test analyzer Part of the Data Analytics skill category.

SamuelChien/mega-skills-collection

A B Test Config Creator - Auto-activating skill for ML Deployment. Triggers on: a b test config creator, a b test config creator Part of the ML Deployment skill category.

SamuelChien/mega-skills-collection

When the user wants to plan, design, or implement an A/B test or experiment. Also use when the user mentions "A/B test," "split test," "experiment," "test this change," "variant copy," "multivariate test," "hypothesis," "conversion experiment," "statistical significance," or "test this." For tracking implementation, see analytics-tracking.

SamuelChien/mega-skills-collection

Acceptance Criteria Creator - Auto-activating skill for Enterprise Workflows. Triggers on: acceptance criteria creator, acceptance criteria creator Part of the Enterprise Workflows skill category.

SamuelChien/mega-skills-collection

Use when a coding task should be driven end-to-end from issue intake through implementation, review, deployment, and acceptance verification with minimal human re-intervention.

SamuelChien/mega-skills-collection

Accessibility Audit Runner - Auto-activating skill for Frontend Development. Triggers on: accessibility audit runner, accessibility audit runner Part of the Frontend Development skill category.

SamuelChien/mega-skills-collection

You are an accessibility expert specializing in WCAG compliance, inclusive design, and assistive technology compatibility. Conduct audits, identify barriers, and provide remediation guidance.

SamuelChien/mega-skills-collection

Web accessibility patterns for WCAG 2.2 compliance including ARIA, keyboard navigation, screen readers, and testing

SamuelChien/mega-skills-collection

Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed, or change policy for the Discord channel.

SamuelChien/mega-skills-collection

Create unified specification packages across Business, Development, and Design teams. Staged elaboration (L0 Vision → L1 Requirements → L2 Team Detail → L3 Acceptance Criteria) to build shared understanding. Does not write code.

SamuelChien/mega-skills-collection

Research a company or person and get actionable sales intel. Works standalone with web search, supercharged when you connect enrichment tools or your CRM. Trigger with "research [company]", "look up [person]", "intel on [prospect]", "who is [name] at [company]", or "tell me about [company]".

SamuelChien/mega-skills-collection

Validate messaging consistency across website, GitHub repos, and local documentation generating read-only discrepancy reports. Use when checking content alignment or finding mixed messaging. Trigger with phrases like "check consistency", "validate documentation", or "audit messaging".

相關技能