Github 地址:https://github.com/google/sanitizers

Wiki 地址:https://github.com/google/sanitizers/wiki/AddressSanitizer

参考:

基本使用:https://blog.csdn.net/c_lazy/article/details/80009627

输出信息的详细解释:https://www.jianshu.com/p/3a2df9b7c353

AddressSanitizer(地址杀菌剂,简称 ASan) 是谷歌出品的内存检查工具,比 Valgrind 更高效。其由两部组成:

  • 编译器 instrumentation 模块
  • 提供malloc()/free()替代项的运行时库

gcc 4.8 开始,AddressSanitizer 成为 gcc 的一部分,但不支持符号信息,无法显示出问题的函数和行数。从 4.9 开始,gcc 支持 AddressSanitizer 的所有功能。

安装

Ubuntu 一般不用安装,CentOS 一般需要安装。

如果使用 AddressSanitizer 时报错:

/usr/bin/ld: cannot find /usr/lib64/libasan.so.0.0.0

则需要先安装。Ubuntu 安装命令:

sudo apt-get install libasan0

CentOS 安装命令:

sudo yum install libasan

使用

在用 gcc 编译程序时,指定 -fsanitize=address 选项即可自动调用 AddressSanitizer。运行程序时,就可以看到相关信息。

通过 -g 选项,可以看到报错的函数和行号。

编译

gcc -fsanitize=address -g twoSum.c

运行

运行上面编译的结果,如果报错,会打印详细信息:

$ ./a.out
=================================================================
==5343==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffee7f9dde8 at pc 0x55d6a041bd9b bp 0x7ffee7f9dce0 sp 0x7ffee7f9dcd0
READ of size 8 at 0x7ffee7f9dde8 thread T0
#0 0x55d6a041bd9a in insertHashTable /home/ubuntu/test/leetcode/twoSum.c:23
#1 0x55d6a041c284 in twoSum /home/ubuntu/test/leetcode/twoSum.c:73
#2 0x55d6a041c6cc in main /home/ubuntu/test/leetcode/twoSum.c:94
#3 0x7f96a8c2db96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#4 0x55d6a041bb79 in _start (/home/ubuntu/test/leetcode/a.out+0xb79) Address 0x7ffee7f9dde8 is located in stack of thread T0 at offset 120 in frame
#0 0x55d6a041c11d in twoSum /home/ubuntu/test/leetcode/twoSum.c:67 This frame has 1 object(s):
[32, 112) 'ht' <== Memory access at offset 120 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow /home/ubuntu/test/leetcode/twoSum.c:23 in insertHashTable
Shadow bytes around the buggy address:
0x10005cfebb60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005cfebb70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005cfebb80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005cfebb90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005cfebba0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1
=>0x10005cfebbb0: f1 f1 00 00 00 00 00 00 00 00 00 00 f2[f2]00 00
0x10005cfebbc0: 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1 04 f2
0x10005cfebbd0: f2 f2 f2 f2 f2 f2 00 00 04 f2 00 00 00 00 00 00
0x10005cfebbe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005cfebbf0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005cfebc00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==5343==ABORTING

详细信息可以参考上面的参考目录。

【C语言工具】AddressSanitizer - 内存检测工具的更多相关文章

  1. valgrind内存检测工具

    valgrind 那点事 ---------------------------------------内存检测工具 valgrind要使用此工具,可以使用--tool=memcheck 在Valgr ...

  2. Android 内存泄露总结(附内存检测工具)

    https://segmentfault.com/a/1190000006852540 主要是分三块: 静态储存区:编译时就分配好,在程序整个运行期间都存在.它主要存放静态数据和常量. 栈区:当方法执 ...

  3. 【调试】Linux下超强内存检测工具Valgrind

    [调试]Linux下超强内存检测工具Valgrind 内容简介 Valgrind是什么? Valgrind的使用 Valgrind详细教程 1. Valgrind是什么? Valgrind是一套Lin ...

  4. linux下内存检测工具的使用和对比

    linux背后隐藏着各种丰富的工具,学会这些工具,让这些工具更好地服务于我们的项目开发,不仅可以提高工作的效率,而且可以增强个人技术力. 参考:http://blog.chinaunix.net/ui ...

  5. Visual Leak Detector 2.2.3 Visual C++内存检测工具

      Visual Leak Detector是一款免费的.健全的.开源的Visual C++内存泄露检测系统.相比Visual C++自带的内存检测机制,Visual Leak Detector可以显 ...

  6. c++Valgrind内存检测工具---19

    原创博文,转载请标明出处--周学伟  http://www.cnblogs.com/zxouxuewei/ 一.Valgrind 概述 Valgrind是一套Linux下,开放源代码(GPL V2)的 ...

  7. iOS一个很好的内存检测工具

    虽然Xcode提供了instrument来检测内存,但是使用起来怎么看都很麻烦.然后有一个很不错的内存泄露的检测工具MLeaksFinder,使用的话不需要注入任何代码,直接导入库就行了.出现泄露的时 ...

  8. Android内存优化9 内存检测工具3 MAT比Menmery Monitor更强大

    在Android性能优化第(一)篇---基本概念中讲了JAVA的四大引用,讲了一下GCRoot,第二篇Memory Monitor检测内存泄露仅仅说了Menmery Monitor的使用,这篇博客谈一 ...

  9. Android内存优化7 内存检测工具1 Memory Monitor检测内存泄露

    上篇说了一些性能优化的理论部分,主要是回顾一下,有了理论,小平同志又讲了,实践是检验真理的唯一标准,对于内存泄露的问题,现在通过Android Studio自带工具Memory Monitor 检测出 ...

随机推荐

  1. Github删除仓库文件夹问题集合

    记得上次使用GitHub,看时间提示,最近的一次,是三年前,而且都是长传文件,这次是删除文件,才发现删除库可以,但是删除库里的某个目录,就不行了,除非是下载下来,在GitHub把仓库删了重新添加.使用 ...

  2. Linux安装redis,启动配置不生效(指定启动加载配置文件)

    一.今天有个同学问我,为什么明明安装了redis,修改了配置,启动的时候,配置还是不生效.如下图是安装后的redis文件图. 二.想加载上图中的redis.conf,进入到src中寻找到启动文件red ...

  3. 在win32中使用SetWindowSubclass阻止Enter键

    使用阻止Enter键的编辑控件的简单子类来完成此操作: LRESULT CALLBACK EditSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, L ...

  4. safari兼容时间格式 NAN

    问题: safari中不支持2018-08-01这种格式转为时间戳会显示NaN 方案: new Date('2018/08/01').getTime() 将-转化为/ // 正则替换 new Date ...

  5. 【优化】SPA项目的基础优化

    开启gzip压缩功能 引入CDN 路由懒加载 某些第三方组件按需加载而不是全部加载 较小的图片资源用base64嵌入src中,减少http请求

  6. Ubuntu中linux虚拟机全屏

    登录客户机操作系统.在虚拟机中装载CD驱动器启动终端,使用tar解压缩安装程序,然后执行vmware-insall.pl安装VMware Tools. 1.进入文件界面,找到左侧“设备”右击“安装VM ...

  7. Gym-100923I-Por Costel and the Pairs(数学,思维)

    链接: https://vjudge.net/problem/Gym-100923I 题意: We don't know how Por Costel the pig arrived at FMI's ...

  8. 通过源码安装PostgresSQL

    通过源码安装PostgresSQL 1.1 下载源码包环境: Centos6.8 64位 yum -y install bison flex readline-devel zlib-devel yum ...

  9. Linux shell 下简单的进度条实现

    Linux shell 下简单的进度条实现 [root@db145 ~]# cat print_process.sh function Proceess(){ spa='' i= ] do print ...

  10. 【shell】sed指定追加模式空间的次数

    最近遇到一个文本复制的场景,需要把文本的每一行都重复输出三次. 这个用awk或者sed实现都还是很简单的. sed代码: [root]$ seq | sed '{h;G;G}' 现在的问题是,如果每行 ...