转载:asan 使用
十二月 10, 2025 [C/C++] #obsidian #调试 #编译链接 #安全转载:asan 使用
原文:https://www.cnblogs.com/fellow1988/p/17263427.html
参考:
- https://blog.csdn.net/u013171226/article/details/126876335
- https://www.osc.edu/resources/getting_started/howto/howto_use_address_sanitizer
1. make cflags
# 静态链接 libasan
static: gcc test.c -o test -fsanitize=address -static-libasan -g
# 动态链接 libasan
dynamic: gcc test.c -o test -fsanitize=address -lasan -g
2. asan env
export ASAN_OPTIONS=halt_on_error=false:print_scariness=true:fast_unwind_on_malloc=true:detect_leak=1
ASAN_OPTIONS 是 Address-Sanitizer 的运行选项环境变量。常用选项:
halt_on_error=0:检测内存错误后继续运行detect_leaks=1:使能内存泄露检测malloc_context_size=15:内存错误发生时,显示的调用栈层数为 15log_path=/home/xos/asan.log:内存检查问题日志存放文件路径suppressions=$SUPP_FILE:屏蔽打印某些内存错误
除了上述常用选项,以下还有一些选项可根据实际需要添加:
detect_stack_use_after_return=1:检查访问指向已被释放的栈空间handle_segv=1:处理段错误;也可以添加handle_sigill=1处理 SIGILL 信号quarantine_size=4194304:内存 cache 可缓存 free 内存大小 4M
3. asan runtime does not come first in initial library list
export LD_PRELOAD=/usr/lib/basan.so