`
luotuoass
  • 浏览: 652369 次
文章分类
社区版块
存档分类
最新评论

valgrind memcheck 错误分析

 
阅读更多

1.默认使用工具memcheck

2.输出到XML文件:valgrind --leak-check=full --xml=yes --log-file="log.xml" myprog arg1 arg2

3.错误解释

3.1Illegal read / Illegal write errors

例如:

Invalid read of size 4
   at 0x40F6BBCC: (within /usr/lib/libpng.so.2.1.0.9)
   by 0x40F6B804: (within /usr/lib/libpng.so.2.1.0.9)
   by 0x40B07FF4: read_png_image(QImageIO *) (kernel/qpngio.cpp:326)
   by 0x40AC751B: QImageIO::read() (kernel/qimage.cpp:3621)
 Address 0xBFFFF0E0 is not stack'd, malloc'd or free'd
这个错误的发生是因为对一些memcheck猜想不应该访问的内存进行了读写。
3.2 Use of uninitialised values

例如:

Conditional jump or move depends on uninitialised value(s)
   at 0x402DFA94: _IO_vfprintf (_itoa.h:49)
   by 0x402E8476: _IO_printf (printf.c:36)
   by 0x8048472: main (tests/manuel1.c:8)
这个错误的发生是因为使用了未初始化的数据。一般情况下有两种情形容易出现这个错误:
程序中的局部变量未初始化;
C语言malloc的内存未初始化;C++中new的对象其成员未被初始化。
 
3.3 Illegal frees
例如:
Invalid free()
   at 0x4004FFDF: free (vg_clientmalloc.c:577)
   by 0x80484C7: main (tests/doublefree.c:10)
 Address 0x3807F7B4 is 0 bytes inside a block of size 177 free'd
   at 0x4004FFDF: free (vg_clientmalloc.c:577)
   by 0x80484C7: main (tests/doublefree.c:10)
 
3.4 When a block is freed with an inappropriate deallocation function
例如:
Mismatched free() / delete / delete []
   at 0x40043249: free (vg_clientfuncs.c:171)
   by 0x4102BB4E: QGArray::~QGArray(void) (tools/qgarray.cpp:149)
   by 0x4C261C41: PptDoc::~PptDoc(void) (include/qmemarray.h:60)
   by 0x4C261F0E: PptXml::~PptXml(void) (pptxml.cc:44)
 Address 0x4BB292A8 is 0 bytes inside a block of size 64 alloc'd
   at 0x4004318C: operator new[](unsigned int) (vg_clientfuncs.c:152)
   by 0x4C21BC15: KLaola::readSBStream(int) const (klaola.cc:314)
   by 0x4C21C155: KLaola::stream(KLaola::OLENode const *) (klaola.cc:416)
   by 0x4C21788F: OLEFilter::convert(QCString const &) (olefilter.cc:272)
  • If allocated with malloc, calloc, realloc, valloc or memalign, you must deallocate with free.

  • If allocated with new[], you must deallocate with delete[].

  • If allocated with new, you must deallocate with delete.

    linux系统对上述错误可能不在意,但是移值到其他平台时却会有问题。

    3.5 Passing system call parameters with inadequate read/write permissions

  • 例如:
    Syscall param write(buf) points to uninitialised byte(s)
         at 0x25A48723: __write_nocancel (in /lib/tls/libc-2.3.3.so)
         by 0x259AFAD3: __libc_start_main (in /lib/tls/libc-2.3.3.so)
         by 0x8048348: (within /auto/homes/njn25/grind/head4/a.out)
       Address 0x25AB8028 is 0 bytes inside a block of size 10 alloc'd
         at 0x259852B0: malloc (vg_replace_malloc.c:130)
         by 0x80483F1: main (a.c:5)
    
      Syscall param exit(error_code) contains uninitialised byte(s)
         at 0x25A21B44: __GI__exit (in /lib/tls/libc-2.3.3.so)
         by 0x8048426: main (a.c:8)
    Memcheck检查所有的被系统调用的参数。
    
    
  • It checks all the direct parameters themselves.

  • Also, if a system call needs to read from a buffer provided by your program, Memcheck checks that the entire buffer is addressable and has valid data, ie, it is readable.

  • Also, if the system call needs to write to a user-supplied buffer, Memcheck checks that the buffer is addressable.

    例如:

    #include <stdlib.h> #include <unistd.h> int main( void ) { char* arr = malloc(10); int* arr2 = malloc(sizeof(int)); write( 1 /* stdout */, arr, 10 ); exit(arr2[0]); }

    错误信息:

    Syscall param write(buf) points to uninitialised byte(s) at 0x25A48723: __write_nocancel (in /lib/tls/libc-2.3.3.so) by 0x259AFAD3: __libc_start_main (in /lib/tls/libc-2.3.3.so) by 0x8048348: (within /auto/homes/njn25/grind/head4/a.out) Address 0x25AB8028 is 0 bytes inside a block of size 10 alloc'd at 0x259852B0: malloc (vg_replace_malloc.c:130) by 0x80483F1: main (a.c:5) Syscall param exit(error_code) contains uninitialised byte(s) at 0x25A21B44: __GI__exit (in /lib/tls/libc-2.3.3.so) by 0x8048426: main (a.c:8)

    传递了无效参数到系统函数中。

    3.6 Overlapping source and destination blocks

    C的以下库函数拷贝数据从一块内存到另一块内存时: memcpy(), strcpy(), strncpy(), strcat(), strncat(). 源和目的都不允许溢出。

    例如:

    ==27492== Source and destination overlap in memcpy(0xbffff294, 0xbffff280, 21) ==27492== at 0x40026CDC: memcpy (mc_replace_strmem.c:71) ==27492== by 0x804865A: main (overlap.c:40)

    3.7 Memory leak detection

    错误信息:

    Still reachable: A pointer to the start of the block is found. This usually indicates programming sloppiness. Since the block is still pointed at, the programmer could, at least in principle, free it before program exit. Because these are very common and arguably not a problem, Memcheck won't report such blocks unless --show-reachable=yes is specified.

    Possibly lost, or "dubious": A pointer to the interior of the block is found. The pointer might originally have pointed to the start and have been moved along, or it might be entirely unrelated. Memcheck deems such a block as "dubious", because it's unclear whether or not a pointer to it still exists.

    Definitely lost, or "leaked": The worst outcome is that no pointer to the block can be found. The block is classified as "leaked", because the programmer could not possibly have freed it at program exit, since no pointer to it exists. This is likely a symptom of having lost the pointer at some earlier point in the program.

  • 分享到:
    评论

    相关推荐

      valgrind内存泄露分析工具

      Valgrind是一款强大的开源内存调试、内存泄漏检测和性能分析工具,主要针对C和C++程序。它通过创建一个虚拟的运行时环境,对程序进行逐指令模拟,从而能够检查程序在运行过程中出现的各种内存问题。Valgrind的内存...

      valgrind_manual

      Valgrind 最常用的工具之一是 Memcheck,它能够检测 C 和 C++ 程序中的多种内存错误,如内存泄漏、未初始化内存使用等。 #### 二、Valgrind 快速入门指南 ##### 1. 引言 Valgrind 工具包提供了一系列调试和性能分析...

      valgrind教程

      - **介绍**:介绍了Valgrind工具集提供的多种调试和性能分析工具,其中最受欢迎的是Memcheck工具,它可以检测C和C++程序中常见的内存错误。 - **准备程序**: - 使用`-g`选项编译程序以包含调试信息,这样可以帮助...

      Valgrind_manual Valgrind手册文档

      Memcheck是Valgrind工具集中最受欢迎的工具,它可以检测C和C++程序中常见的许多内存相关错误,这些错误可能会导致程序崩溃或者出现不可预测的行为。Memcheck通过在程序执行时检查对内存的所有读写操作来实现这一功能...

      valgrind详细手册

      Valgrind是由英国剑桥大学开发的一款动态分析工具,它的核心是虚拟机,可以在不修改程序的情况下运行,从而提供一系列实用工具,如Memcheck(内存错误检测)、LeakCheck(内存泄漏检测)、Cachegrind(缓存行为分析...

      valgrind内存检查工具

      Valgrind是一款强大的开源内存分析工具,主要用于检测C和C++程序中的内存错误,包括内存泄漏、无效内存访问以及未初始化的内存使用等问题。这款工具是程序员和软件开发者的重要助手,能够帮助他们发现并修复程序在...

      valgrind_manual.pdf

      Valgrind的核心功能之一是Memcheck,一个能够检测并报告内存错误的强大工具,它能识别出常见的内存管理问题,如未初始化的变量、越界的数组访问、内存泄漏等,这些问题往往是导致程序崩溃或行为异常的根源。...

      valgrind manual

      Valgrind是一款用于调试与性能分析的强大工具集,其主要目标是帮助开发者找出程序中的内存错误,并提高程序的性能。Valgrind的核心功能之一是Memcheck工具,它能够检测出C/C++程序中常见的内存相关错误,如内存泄漏...

      valgrind 的安装 使用

      1. **Memcheck**:这是 Valgrind 最常用的工具之一,主要用于检测内存错误。它可以帮助开发者找出诸如使用未初始化的内存、读/写已经被释放的内存、内存越界等常见的内存问题。此外,它还能检测到内存泄漏和不当的...

      valgrind安装与使用

      Valgrind 是一款功能强大的内存调试、内存泄漏检测和性能分析软件开发工具。下面将详细介绍 Valgrind 的安装和使用。 Valgrind 简介 Valgrind 是一个开源的软件开发工具,用于检测和调试 C 和 C++ 程序中的内存...

      valgrind3.12.0

      Valgrind 3.12.0 是一个强大的开源工具集,主要用于动态分析程序的行为,尤其是内存管理和错误检测。这个版本的Valgrind是开发者在寻找内存泄漏、未初始化的内存访问、无效指针引用等问题时的重要帮手。在本文中,...

      Valgrind简介和使用方法

      Valgrind包含了多个子工具,其中最常用的是Memcheck,它能够检测诸如未初始化的内存、使用已释放内存、内存越界等问题。其他工具如Callgrind用于分析函数调用,Cachegrind关注缓存效率,Helgrind检查多线程竞争条件...

      Valgrind for ARMv6

      Valgrind是一款强大的开源内存调试、性能分析和系统调用检查工具,主要用于帮助开发者发现C和C++程序中的错误,优化代码性能。在ARMv6架构上使用Valgrind,可以为开发针对嵌入式设备的应用提供强有力的支持。ARMv6是...

      valgrind调试教程

      Valgrind是一款用于检测程序中内存问题的工具集,它由多个调试和分析工具组成,最常用的是Memcheck工具。Memcheck能够帮助开发者发现C和C++程序中常见的与内存相关的错误,例如内存泄漏、内存越界、错误的内存释放等...

      Linux内存调试工具Valgrind -PDF

      运行Valgrind的Memcheck工具,可以检测出这种“非法写”操作,并提供函数调用栈信息以便开发者准确地定位错误位置。 ##### 3. 使用未初始化变量示例 Valgrind还可以检测出当一个变量被赋值为未初始化变量的情况。...

      应用-Valgrind-发现-Linux-程序的内存问题.doc

      它还提供了几个子工具,如Memcheck用于基本的内存错误检查,Helgrind用于检测线程间的竞态条件,和Dr.Malloc用于更详细的内存分配分析。 使用Valgrind进行调试通常包括以下步骤: 1. 编译程序时添加`-g`选项以包含...

      Valgrind使用手册

      Valgrind提供了多种子工具,每个都有特定的用途,如Memcheck用于检查内存错误,Leak Checker用于查找内存泄漏,以及Helgrind和DRD用于多线程程序中的竞态条件和死锁检测。 **Memcheck子工具** Memcheck是Valgrind中...

      arm-linux-gnueabihf-valgrind-3.15.0.tar.gz

      "arm-linux-gnueabihf-valgrind-3.15.0.tar.gz"便是这样一款专为ARM平台设计的工具,旨在帮助开发者进行高效且准确的内存错误检测、性能分析和调试工作。本文将详细介绍该工具的核心功能、使用方法以及它在arm-linux...

    Global site tag (gtag.js) - Google Analytics