【问题标题】:Valgrind not showing error regarding memory region overlapValgrind 未显示有关内存区域重叠的错误
【发布时间】:2013-12-02 12:16:32
【问题描述】:

在使用Valgrind(或者具体来说,Memcheck)测试各种动态内存相关错误的过程中,我遇到了一种情况,我故意创建内存重叠,但是有Valgrind/memcheck 没有错误报告。以下是使用的代码。请分享我所缺少的。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char * pOne;
    char * pTwo;

    pOne = (char *)malloc (24);
    pTwo = pOne + 4;
    strcpy (pOne, "Sourav Ghosh");
    printf("pOne = %s\npTwo = %s\n", pOne, pTwo);
    memcpy (pTwo, pOne, 16); //Overlapping issue should be here
    printf("pOne = %s\npTwo = %s\n", pOne, pTwo);

    free (pOne);
    return 0;
}

编译

[sourav@titan temp]$ gcc -g srvtest.c -o memory

示例运行和输出

[sourav@titan temp]$ valgrind --leak-check=full ./memory
==6982== Memcheck, a memory error detector
==6982== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==6982== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==6982== Command: ./memory
==6982==
pOne = Sourav Ghosh
pTwo = av Ghosh
==6982== Conditional jump or move depends on uninitialised value(s)
==6982==    at 0x4006817: strlen (mc_replace_strmem.c:275)
==6982==    by 0xAD0C0D: vfprintf (in /lib/libc-2.5.so)
==6982==    by 0xAD6E82: printf (in /lib/libc-2.5.so)
==6982==    by 0x8048477: main (srvtest.c:15)
==6982==
pOne = SourSourSourSourSour
pTwo = SourSourSourSour
==6982==
==6982== HEAP SUMMARY:
==6982==     in use at exit: 0 bytes in 0 blocks
==6982==   total heap usage: 1 allocs, 1 frees, 24 bytes allocated
==6982==
==6982== All heap blocks were freed -- no leaks are possible
==6982==
==6982== For counts of detected and suppressed errors, rerun with: -v
==6982== Use --track-origins=yes to see where uninitialised values come from
==6982== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 12 from 8)
[sourav@titan temp]$

实际上没有关于内存区域重叠的信息。根据 Valgrind 手册,它应该显示如下:

==27492== Source and destination overlap in memcpy(0xbffff294, 0xbffff280, 21)

我的案例中缺少什么?

系统信息:

[sourav@titan temp]$ uname -r
2.6.18-194.el5PAE
[sourav@titan temp]$ gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)
[sourav@titan temp]$ ldd --version
ldd (GNU libc) 2.5
[sourav@titan temp]$ valgrind --version
valgrind-3.5.0

【问题讨论】:

  • 为我工作(使用 gcc 4.7.3、valgrind 3.8.1 编译)。编译器可以内联memcpy()(带有内置函数)吗?您可以尝试使用-fno-builtin 进行编译,看看是否会发生变化。
  • 使用 Valgrind (valgrind-3.6.0.SVN-Debian) 和 gcc ((Debian 4.4.5-8) 4.4.5) 我也收到了预期的 Valgrind 消息。
  • @unwind 仅供参考,此代码是来自更大代码库的 sn-p,其中所有可能的动态内存相关问题场景都在那里,包括 C/C++ 分配器 - 释放器之间的不匹配。为了遵守g++,那里有演员表。不管怎样,谢谢你的i/p。 :-)
  • @Hasturkun .. TY 获取信息。已添加答案。

标签: c valgrind memcpy dynamic-memory-allocation memcheck


【解决方案1】:

感谢@Hasturkun,问题在于编译器将memcpy() 与内置函数内联。因此,valgrind 无法挂钩 memcpy() 并捕获错误。

修改编译

 [sourav@titan temp]$ gcc -g -fno-builtin srvtest.c -o memory

修改后的输出

[sourav@titan temp]$ valgrind --leak-check=full ./memory 
==19785== Memcheck, a memory error detector
==19785== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==19785== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==19785== Command: ./memory
==19785== 
pOne = Sourav Ghosh
pTwo = av Ghosh
==19785== Source and destination overlap in memcpy(0x402602c, 0x4026028, 16)
==19785==    at 0x4007AE2: memcpy (mc_replace_strmem.c:482)
==19785==    by 0x80484B3: main (srvtest.c:14)
==19785== 
pOne = SourSourav Ghosh
pTwo = Sourav Ghosh
==19785== 
==19785== HEAP SUMMARY:
==19785==     in use at exit: 0 bytes in 0 blocks
==19785==   total heap usage: 1 allocs, 1 frees, 24 bytes allocated
==19785== 
==19785== All heap blocks were freed -- no leaks are possible
==19785== 
==19785== For counts of detected and suppressed errors, rerun with: -v
==19785== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 8)
[sourav@titan temp]$

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多