Linux command
asan 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Compile a C program with AddressSanitizer
gcc -fsanitize=address -g [program.c] -o [program]
Compile a C++ program with ASan
g++ -fsanitize=address -g [program.cpp] -o [program]
Compile with ASan using Clang
clang -fsanitize=address -g [program.c] -o [program]
Run with custom ASan options
ASAN_OPTIONS=detect_leaks=1:verbosity=1 ./[program]
Compile with ASan and UBSan combined
gcc -fsanitize=address,undefined -g [program.c] -o [program]
Compile for debugging with frame pointers
gcc -fsanitize=address -g -fno-omit-frame-pointer [program.c] -o [program]
说明
AddressSanitizer (ASan) is a fast memory error detector built into GCC and Clang. It detects buffer overflows, use-after-free, use-after-return, memory leaks, and other memory corruption bugs at runtime with relatively low overhead. When enabled via -fsanitize=address, the compiler instruments memory accesses and allocations. The instrumented program maintains shadow memory that tracks the state of each byte. Memory errors are detected immediately when they occur, with detailed reports including stack traces for both the error location and the allocation/deallocation points. ASan typically adds 2x slowdown and 3x memory overhead, making it suitable for testing and development but not production use. Compile with -g for source locations in reports.
参数
- -fsanitize=address
- Enable AddressSanitizer instrumentation.
- -g
- Include debug information for meaningful stack traces.
- -fno-omit-frame-pointer
- Preserve frame pointers for better stack traces.
- -fsanitize-recover=address
- Continue execution after detecting errors (not recommended).
- -static-libasan
- Link ASan runtime statically.
FAQ
What is the asan command used for?
AddressSanitizer (ASan) is a fast memory error detector built into GCC and Clang. It detects buffer overflows, use-after-free, use-after-return, memory leaks, and other memory corruption bugs at runtime with relatively low overhead. When enabled via -fsanitize=address, the compiler instruments memory accesses and allocations. The instrumented program maintains shadow memory that tracks the state of each byte. Memory errors are detected immediately when they occur, with detailed reports including stack traces for both the error location and the allocation/deallocation points. ASan typically adds 2x slowdown and 3x memory overhead, making it suitable for testing and development but not production use. Compile with -g for source locations in reports.
How do I run a basic asan example?
Run `gcc -fsanitize=address -g [program.c] -o [program]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -fsanitize=address do in asan?
Enable AddressSanitizer instrumentation.