[daily] 内存越界的分析与定位

 

valgrind 自不必说

1.  Address Sanitize

很好有,只需要在gcc编译的时候,加上选项 -fsanitize=address

它的工程:https://github.com/google/sanitizers/wiki/AddressSanitizer

我的测试例子: https://github.com/tony-caotong/knickknack/tree/master/test/sanitize-address

运行的时候,遇见内存问题会直接退出,包括前边越界和后边越界,如下:

 

https://wizardforcel.gitbooks.io/100-gcc-tips/content/address-sanitizer.html

需要主要的是,在CentOS7里面,需要单独安装依赖库

[root@dpdk sanitize-address]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[root@dpdk sanitize-address]# yum search asan
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * epel: mirrors.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * rpmfusion-free-updates: mirrors.ustc.edu.cn
 * rpmfusion-nonfree-updates: mirrors.ustc.edu.cn
 * updates: mirrors.shuosc.org
===================================================================================== N/S matched: asan =====================================================================================
libasan.i686 : The Address Sanitizer runtime library
libasan.x86_64 : The Address Sanitizer runtime library
libasan-static.i686 : The Address Sanitizer static library
libasan-static.x86_64 : The Address Sanitizer static library
oflb-asana-math-fonts.noarch : An OpenType font with a MATH table

  Name and summary matches only, use "search all" for everything.
[root@dpdk sanitize-address]# yum install libasan

 

使用gdb在报错的地方,打断点:

https://github.com/google/sanitizers/wiki/AddressSanitizerAndDebugger

也就是

(gdb)break __sanitizer::Die

 

但是, 在CentOS7里竟然不好使,因为这个库竟然没有symbol,我相信,有symbol的库定能成功break

[root@dpdk anthropoid]# nm /lib64/libasan.so.0.0.0 
nm: /lib64/libasan.so.0.0.0: no symbols
[root@dpdk anthropoid]# 

   comment @2018-01-26 当时的我还真是太傻太天真呢,还你相信。。。  正确的解释,请看:

    [daily] 在CentOS7中使用 sanitizer-address 发现内存问题 / CentOS7使用SCLo软件源安装devtoolset软件

 

2. mprotect

http://man7.org/linux/man-pages/man2/mprotect.2.html

linux系统API,可以在内存的前后扇区开始处,打上标签。当该标签被读写时,系统会给出提示。

 

 

3. 自己在内存前后打标记 --!!!

 

 

参考:http://www.cnblogs.com/djinmusic/archive/2013/02/04/2891753.html

 

后续:[daily] 在CentOS7中使用 sanitizer-address 发现内存问题 / CentOS7使用SCLo软件源安装devtoolset软件

posted on 2018-01-10 18:07  toong  阅读(3762)  评论(0编辑  收藏  举报