Linux内存越界检测方法——valgrind



一.Valgrind

1.下载安装

下载地址:http://valgrind.org/downloads/current.html#current

#configure

#make

#make install

2.使用

2.1内在越界

写一段有内存访问越界的代码,如下:

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

int main()

{

char *p = NULL;

char *temp = NULL;

p = (char*)malloc(sizeof(char));

if (NULL == p)

{

perror("Cannot allocate memory.");

return -1;

}

temp[0] = p[6];

free(p);

p = NULL;

return 0;

}

保存文件test.c

在上面的代码中,我们给p分配了一个字节的空间,后面却要访问p[6],看看会出现什么情况。

2.1.1带DEBUG编译

#gcc t-g test.c -o test

2.1.2运行分析

首先直接运行

#./test

段错误 (core dumped)

直接报错退出。

使用valgrind加载运行test

#valgrind --tool=memcheck --leak-check=full ./test

==17686== Memcheck, a memory error detector

==17686== Copyright (C) 2002-2013, and GNU GPL‘d, by Julian Seward et al.

==17686== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info

==17686== Command: ./test

==17686==

==17686== Invalid read of size 1

==17686==    at 0x40059A: main (test.c:19)

==17686==  Address 0x4c22046 is 5 bytes after a block of size 1 alloc‘d

==17686==    at 0x4A0720A: malloc (vg_replace_malloc.c:296)

==17686==    by 0x400575: main (test.c:10)

==17686==

==17686== Invalid write of size 1

==17686==    at 0x4005A1: main (test.c:19)

==17686==  Address 0x0 is not stack‘d, malloc‘d or (recently) free‘d

==17686==

==17686==

==17686== Process terminating with default action of signal 11 (SIGSEGV)

==17686==  Access not within mapped region at address 0x0

==17686==    at 0x4005A1: main (test.c:19)

==17686==  If you believe this happened as a result of a stack

==17686==  overflow in your program‘s main thread (unlikely but

==17686==  possible), you can try to increase the size of the

==17686==  main thread stack using the --main-stacksize= flag.

==17686==  The main thread stack size used in this run was 10485760.

==17686==

==17686== HEAP SUMMARY:

==17686==     in use at exit: 1 bytes in 1 blocks

==17686==   total heap usage: 1 allocs, 0 frees, 1 bytes allocated

==17686==

==17686== LEAK SUMMARY:

==17686==    definitely lost: 0 bytes in 0 blocks

==17686==    indirectly lost: 0 bytes in 0 blocks

==17686==      possibly lost: 0 bytes in 0 blocks

==17686==    still reachable: 1 bytes in 1 blocks

==17686==         suppressed: 0 bytes in 0 blocks

==17686== Reachable blocks (those to which a pointer was found) are not shown.

==17686== To see them, rerun with: --leak-check=full --show-leak-kinds=all

==17686==

==17686== For counts of detected and suppressed errors, rerun with: -v

==17686== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)

段错误 (core dumped)

我们看到,valgrind共报了两个错误,见上面红色部分,我们分别分析一下:

==17686== Invalid read of size 1

==17686==    at 0x40059A: main (test.c:19)

==17686==  Address 0x4c22046 is 5 bytes after a block of size 1 alloc‘d

==17686==    at 0x4A0720A: malloc (vg_replace_malloc.c:296)

==17686==    by 0x400575: main (test.c:10)

这个是读错误,指出在test.c的第19行,在第10行(p = (char*)malloc(sizeof(char));)正常分配给地址(0x4A0720A)1个字节,读的地址(0x4c22046)却是之后的5个字节处(我们访问的p[6])。

再分析第二个错误:

==17686== Invalid write of size 1

==17686==    at 0x4005A1: main (test.c:19)

==17686==  Address 0x0 is not stack‘d, malloc‘d or (recently) free‘d

Valgrind报怨我们没有分配就开始使用,说的应该是temp了。

2.2内存泄露

2.2.1代码修改

对于内存泄露,valgrind也可以很好地检查出来,在上例中,做如下修改:

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

int main()

{

char *p = NULL;

char *temp = NULL;

p = (char*)malloc(sizeof(char));

if (NULL == p)

{

perror("Cannot allocate memory.");

return -1;

}

//temp[0] = p[6];

//free(p);

p = NULL;

return 0;

}

注释内存访问越界和释放内存的代码。

2.2.2编译并运行

编译(见上)。

先直接运行:

#./test

没有任何提示。

再次使用Valgrind加载。

valgrind --tool=memcheck --leak-check=full ./test

==17793== Memcheck, a memory error detector

==17793== Copyright (C) 2002-2013, and GNU GPL‘d, by Julian Seward et al.

==17793== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info

==17793== Command: ./test

==17793==

==17793==

==17793== HEAP SUMMARY:

==17793==     in use at exit: 1 bytes in 1 blocks

==17793==   total heap usage: 1 allocs, 0 frees, 1 bytes allocated

==17793==

==17793== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1

==17793==    at 0x4A0720A: malloc (vg_replace_malloc.c:296)

==17793==    by 0x400525: main (test.c:10)

==17793==

==17793== LEAK SUMMARY:

==17793==    definitely lost: 1 bytes in 1 blocks

==17793==    indirectly lost: 0 bytes in 0 blocks

==17793==      possibly lost: 0 bytes in 0 blocks

==17793==    still reachable: 0 bytes in 0 blocks

==17793==         suppressed: 0 bytes in 0 blocks

==17793==

==17793== For counts of detected and suppressed errors, rerun with: -v

==17793== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

2.2.3分析

在上面标红的描述中,告诉我们1个字节初分配,但未释放,这1个字节是在test.c的第10行(p = (char*)malloc(sizeof(char));)分配的。

3.总结

由于HEAP SUMMARY是在程序正常退出时统计的内存泄露,所以如果程序异常退出,只能对已经运行的内存进行越界检查,无法进行内存泄露的统计。

时间: 2024-08-27 06:38:16

Linux内存越界检测方法——valgrind的相关文章

Unix下C程序内存泄漏检测工具Valgrind安装与使用

Unix下C程序内存泄漏检测工具Valgrind安装与使用 Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具. Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届Google-O'Reilly开源代码奖. Valgrind遵守GNU通用公共许可证条款,是一款自由软件. 官网 http://www.valgrind.org 下载与安装 #wget http://www.valgrind.org/downlo

VS2005内存泄漏检测方法[转载]

一.非MFC程序可以用以下方法检测内存泄露: 1. 程序开始包含如下定义: #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__) #else #define DEBUG_CLIENTBLOCK #endif   // _DEBUG #define _CRTDBG_MAP_ALLOC #include #include #ifdef _DEBUG #define new   DEBUG_CLI

linux c 内存泄露检测工具valgrind

Linux c/c++上常用内存泄露检测工具有valgrind, Rational  purify.Valgrind免费.Valgrind 可以在 32 位或 64 位 PowerPC/Linux 内核上工作.Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind,  Callgrind,Massif.下面分别介绍个工具的作用:Memcheck  工具主要检查下面的程序错误:? 使用未初始化的内存 (Use of uninitialised  memory

内存泄露检测工具Valgrind

内存泄露简介 什么是内存泄漏 内存泄漏(Memory Leak)是指程序中已动态分配的堆内存由于某种原因,程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果. 内存泄漏缺陷具有隐蔽性.积累性的特征,比其他内存非法访问错误更难检测.因为内存泄漏的产生原因是内存块未被释放,属于遗漏型缺陷而不是过错型缺陷.此外,内存泄漏通常不会直接产生可观察的错误症状,而是逐渐积累,降低系统整体性能,极端的情况下可能使系统崩溃. 内存泄露产生的方式 以产生的方式来分类,内存泄漏可以分

Linux内存泄漏检测

如题,就工具而言主要包括valgrind.mtrace.dmalloc和memwatch等,具体使用请参照以下连接 Linux C内存泄露检测工具 http://blog.sina.com.cn/s/blog_4b9216f50100e6o7.html Linux C/C++ 内存泄漏检测工具:Valgrind http://zyan.cc/post/419/ 就内存泄漏检测的理论和实现请参照以下连接: 一个跨平台的 C++ 内存泄漏检测器 http://www.ibm.com/develope

Linux 内存泄漏检查工具 valgrind

抄自<从零开始的JSON库教程>,先mark一下,以后再慢慢研究. ======== 引用分割线 ======== 在 Linux.OS X 下,我们可以使用 valgrind 工具(用 apt-get install valgrind. brew install valgrind).我们完全不用修改代码,只要在命令行执行: $ valgrind --leak-check=full ./leptjson_test $ valgrind --leak-check=full ./leptjson_

_CrtSetBreakAlloc简单内存泄漏检测方法,解决Detected memory leaks!问题

我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} normal block at 0x05785AD0, 152 bytes long. Data: << N N x 7 > 3C AC 4E 10 00 00 00 00 BC A4 4E 10 78 B6 37 00 Object dump complete.   而且每次退出都是一样的.

简单内存泄漏检测方法 解决 Detected memory leaks! 问题

最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} normal block at 0x05785AD0, 152 bytes long. Data: << N N x 7 > 3C AC 4E 10 00 00 00 00 BC A4 4E 10 78 B6 37 00 Object dump complete.   而且每次退出都是一样的.泄漏的内存块都是98500. 解决方法: 1.

Android Native内存泄漏检测方法

Android 检测 C/C++内存泄漏的方法越来越简便了,下面列举一下不同场景下检测C/C++内存泄漏的方法. Android O(针对root设备,调试APP) 1. 准备一个userdebug或eng版本手机,下载native_heapdump_viewer.py脚本备用 2. 执行以下命令 adb shell setprop wrap.<APP_PACKAGE_NAME> '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace"' 3. 执行重现