当前位置: 首页 > news >正文

C/C++:AddressSanitizer内存检测工具

AddressSanitizer是gcc自带的内存检测工具,无需额外安装

常见问题

#include <stdlib.h>// 越界访问
void stack_buffer_overflow()
{char buffer[1];int i = 10;buffer[i] = 'A'; // 访问越界
}// 野指针
void use_after_free()
{char *text = (char *)malloc(sizeof(char) * 5);free(text);text[0] = '1'; // 访问已释放内存
}// 内存泄漏
void leak_memory()
{char *text = (char *)malloc(sizeof(char) * 5);
}

AddressSanitizer使用示例

// main.c
#include <stdlib.h>// 野指针
void use_after_free()
{char *text = (char *)malloc(sizeof(char) * 5);free(text);text[0] = '1'; // 访问已释放内存
}int main(int argc, char const *argv[])
{use_after_free();return 0;
}

运行就能输出对应的错误信息

$ gcc main.c -o main -fsanitize=address  -g && ./main=================================================================
==25561==ERROR: AddressSanitizer: heap-use-after-free on address 0x6020000000d0 at pc 0x000109372ee1 bp 0x7ff7b6b8cef0 sp 0x7ff7b6b8cee8
WRITE of size 1 at 0x6020000000d0 thread T0#0 0x109372ee0 in use_after_free main.c:17#1 0x109372f2a in main main.c:28#2 0x7ff80bb45365 in start+0x795 (dyld:x86_64+0xfffffffffff5c365)0x6020000000d0 is located 0 bytes inside of 5-byte region [0x6020000000d0,0x6020000000d5)
freed by thread T0 here:#0 0x109e18b69 in wrap_free+0xa9 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0xdcb69)#1 0x109372e9e in use_after_free main.c:15#2 0x109372f2a in main main.c:28#3 0x7ff80bb45365 in start+0x795 (dyld:x86_64+0xfffffffffff5c365)previously allocated by thread T0 here:#0 0x109e18a20 in wrap_malloc+0xa0 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0xdca20)#1 0x109372e91 in use_after_free main.c:14#2 0x109372f2a in main main.c:28#3 0x7ff80bb45365 in start+0x795 (dyld:x86_64+0xfffffffffff5c365)SUMMARY: AddressSanitizer: heap-use-after-free main.c:17 in use_after_free
Shadow bytes around the buggy address:0x601ffffffe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000x601ffffffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000x601fffffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000x601fffffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000x602000000000: fa fa fd fd fa fa 00 00 fa fa 00 04 fa fa 00 00
=>0x602000000080: fa fa 00 04 fa fa 00 00 fa fa[fd]fa fa fa fa fa0x602000000100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa0x602000000180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa0x602000000200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa0x602000000280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa0x602000000300: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):Addressable:           00Partially addressable: 01 02 03 04 05 06 07 Heap left redzone:       faFreed heap region:       fdStack left redzone:      f1Stack mid redzone:       f2Stack right redzone:     f3Stack after return:      f5Stack use after scope:   f8Global redzone:          f9Global init order:       f6Poisoned by user:        f7Container overflow:      fcArray cookie:            acIntra object redzone:    bbASan internal:           feLeft alloca redzone:     caRight alloca redzone:    cb
==25561==ABORTING
zsh: abort      ./main
http://www.xdnf.cn/news/1404775.html

相关文章:

  • 基于单片机甲醛浓度检测报警系统Proteus仿真(含全部资料)
  • 存储的未来之战:RustFS如何用ZK框架重构分布式协调?
  • L10 Function Calling与智能Agent开发
  • IC验证 AXI 项目(二)——断言
  • LeetCode - 21. 合并两个有序链表
  • 【前端教程】JavaScript 基础实战案例(5-10)
  • UE5多人MOBA+GAS 56、WSL + Docker 编排 Linux 服务器与 Windows 客户端
  • Linux基础1
  • AI融合高等教育:从通识到专业 - 学科+AI人才培养白皮书(上)
  • Linux查看Java进程PID、端口号和内存占用脚本
  • 【多项式】快速莫比乌斯变换(FMT)
  • ⭐CVPR2025 自动驾驶半监督 LiDAR 分割新范式:HiLoTs 框架深度解析
  • Python 数据分析:计算,分组统计2,df.groupby()和grouped.agg()。听故事学知识点怎么这么容易?
  • 告别图片处理焦虑:用imgix实现智能、实时且高效的视觉媒体交付(含案例、截图)
  • 一键掌控三线资源:极简 Shell 脚本实现 CPU·磁盘·内存可视化巡检
  • SRE命令行兵器谱之二:lsof - 解密“端口被占用”与“文件句柄泄漏”的终极侦探
  • MySQL-事务(下)-MySQL事务隔离级别与MVCC
  • 2021-11-10 C++不变初心数
  • ans1语法的一个例子nt5inf.cat
  • 详解Vue2、Vue3与React的Diff算法
  • TuringComplete游戏攻略(2.2存储器)
  • spark.sparkContext.broadcast() 与 org.apache.spark.sql.functions.broadcast 的区别
  • Docker实战避坑指南:从入门到精通
  • 神经网络激活函数:从ReLU到前沿SwiGLU
  • 分分合合,门模块方案又兴起了
  • 用更少的数据识别更多情绪:低资源语言中的语音情绪识别新方法
  • Vue生命周期、工程化开发和脚手架、组件化开发
  • hubert模型代码分析
  • 聚中原·贸全国·达世界,2026郑州台球展8月15至17举办
  • 深入解析Nginx常见模块1