C/C++使用AddressSanitizer检测内存错误

AddressSanitizer 是一种内存错误检测工具,编译时添加 -fsanitize=address 选项可以在运行时检测出非法内存访问,当发生段错误时,AddressSanitizer 会输出详细的错误报告,包括出错位置的代码行号和调用栈,有助于快速定位问题。

demo.c段错误程序

cpp 复制代码
#include <stdio.h>
#include <assert.h>

void buggy_function(int *ptr) {
    // 确保指针不是 NULL
    //assert(ptr != NULL);
    *ptr = 10;
}

int main() {
    int *ptr = NULL;
    *ptr = 10;
    return 0;
}

gcc编译

bash 复制代码
gcc -g -fsanitize=address demo.c

可以在运行程序时通过设置 ASAN_OPTIONS 环境变量,将 AddressSanitizer 的输出重定向到文件。具体命令如下:

bash 复制代码
export ASAN_OPTIONS="log_path=asan_log.txt"
./a.out

生成日志如下:

bash 复制代码
bwton@DESKTOP-UJNM808:~/project/tmp$ cat asan_log.txt.302
=================================================================
==302==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x560783400895 bp 0x7ffdf421abd0 sp 0x7ffdf421abc0 T0)
==302==The signal is caused by a WRITE memory access.
==302==Hint: address points to the zero page.
    #0 0x560783400894 in main /home/bwton/project/tmp/demo.c:12
    #1 0x7ff6d0eb4c86 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21c86)
    #2 0x560783400719 in _start (/home/bwton/project/tmp/a.out+0x719)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/bwton/project/tmp/demo.c:12 in main
==302==ABORTING
bwton@DESKTOP-UJNM808:~/project/tmp$

Eclipse编译

如果是嵌入式设备使用Eclipse编译,可如下设置:

1. 添加编译选项

  1. 在编译器的设置中,找到 Miscellaneous(杂项)部分。

  2. Other flags(其他标志)框中添加 -fsanitize=address

  3. 如果你使用的是 C++ 编译器,也可以在 GCC C++ Compiler 下重复相同的步骤。

2. 修改链接器设置

为了确保 AddressSanitizer 正常工作,还需要在链接器中添加相应的选项:

  1. 在同样的 Tool Settings 下,选择 GCC C Linker

  2. Miscellaneous 部分的 Other flags 中添加 -fsanitize=address

  3. 如果运行时报错"error while loading shared libraries: libasan.so.1:"可以在编译时添加静态链接-static-libasan

相关推荐
SuperByteMaster8 小时前
keil 工程 .gitignore配置文件
c语言
汉克老师9 小时前
GESP2025年3月认证C++五级( 第三部分编程题(1、平均分配))
c++·算法·贪心算法·排序·gesp5级·gesp五级
老花眼猫12 小时前
编制椭圆旋转绘图函数
c语言·经验分享·青少年编程·课程设计
智者知已应修善业12 小时前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
云泽80814 小时前
C++11 核心特性全解:列表初始化、右值引用与移动语义实战
开发语言·c++
AI进化营-智能译站14 小时前
ROS2 C++开发系列12-用多态与虚函数构建可扩展的ROS2机器人行为模块
开发语言·c++·ai·机器人
iCxhust15 小时前
微机原理实践教程(C语言篇)---A002流水灯
c语言·开发语言·单片机·嵌入式硬件·51单片机·课程设计·微机原理
Morwit15 小时前
QML组件之间的通信方案(暴露子组件)
c++·qt·职场和发展
qeen8715 小时前
【数据结构】建堆的时间复杂度讨论与TOP-K问题
c语言·数据结构·c++·学习·
图码15 小时前
如何用多种方法判断字符串是否为回文?
开发语言·数据结构·c++·算法·阿里云·线性回归·数字雕刻