【问题标题】:Valgrind Not Catching SegfaultsValgrind 没有捕获 Segfaults
【发布时间】:2012-10-31 20:55:05
【问题描述】:

我知道 Valgrind 以允许捕获段错误的方式跟踪内存。但是,为什么没有捕获以下段错误?

int main() {
    char *x = calloc(16, 1);
    char *y = calloc(16, 1);

    x[80] = 'c';
    y[-80] = 'c';

    printf("%c %c\n", *x, *y);
    return 0;
}

它不应该捕获堆中的越界访问吗?根据 Valgrind 的文档:

But it should detect many errors that could crash your program (eg. cause a segmentation fault).

【问题讨论】:

  • 程序真的有段错误吗?
  • 你用什么参数运行valgrind?
  • 嗯,它确实说“应该”和“许多”,而不是“将”和“全部”:-P

标签: c segmentation-fault valgrind


【解决方案1】:

我认为你赋予了 valgrind 权力,而不是可能的。

它会尝试检测各种类型的错误并将其报告给您,但它不可能检测到所有错误,即使在它尝试检测的某些错误类别中也是如此。

在这种情况下,您正在处理的是对数组的越界写入,如果 valgrind 设法捕获它,则会报告为“无效写入”错误。这些是通过跟踪哪些地址是“有效的”来检测的,因为它们是已知堆块的一部分。

问题是,如果你索引太远超出数组的开始或结束,你实际上可能会得到一个在相邻块中是有效地址的地址,因此对于 valgrind 来说看起来绝对没问题。为了减少发生这种情况的可能性,valgrind 在块的每一侧添加了一个填充区域(称为“红色区域”),但默认情况下只有 16 个字节。

如果您使用--redzone-size=128 选项增加红色区域大小,那么您会发现 valgrind 确实检测到该程序中的错误。

【讨论】:

  • 确实如此。我忘记了 Valgrind 没有看到 C 语义。它只看到机器代码,所以x[80] 在 Valgrind 看到它时已经是一个地址。它无法知道你实际上在做x + 80。
  • 我在 Valgrind 中看不到 --redzone-size 标志。此外,访问 N 处的数组位置(因此数组后面的 1 个元素,因为它从 0 开始索引),也不会被 Valgrind 捕获。
【解决方案2】:

为我工作:

==24344== Memcheck, a memory error detector.
==24344== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==24344== Using LibVEX rev 1854, a library for dynamic binary translation.
==24344== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==24344== Using valgrind-3.3.1-Debian, a dynamic binary instrumentation framework.
==24344== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==24344== For more details, rerun with: -v
==24344== 
==24344== Invalid write of size 1
==24344==    at 0x8048419: main (testValgrind.c:5)
==24344==  Address 0x418f078 is 0 bytes after a block of size 16 alloc'd
==24344==    at 0x4021E22: calloc (vg_replace_malloc.c:397)
==24344==    by 0x804840F: main (testValgrind.c:3)
==24344== 
==24344== Invalid write of size 1
==24344==    at 0x8048422: main (testValgrind.c:6)
==24344==  Address 0x418f018 is 16 bytes before a block of size 16 alloc'd
==24344==    at 0x4021E22: calloc (vg_replace_malloc.c:397)
==24344==    by 0x80483F8: main (testValgrind.c:2)

==24344== 
==24344== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 12 from 1)
==24344== malloc/free: in use at exit: 32 bytes in 2 blocks.
==24344== malloc/free: 2 allocs, 0 frees, 32 bytes allocated.
==24344== For counts of detected errors, rerun with: -v
==24344== searching for pointers to 2 not-freed blocks.
==24344== checked 58,940 bytes.
==24344== 
==24344== LEAK SUMMARY:
==24344==    definitely lost: 32 bytes in 2 blocks.
==24344==      possibly lost: 0 bytes in 0 blocks.
==24344==    still reachable: 0 bytes in 0 blocks.
==24344==         suppressed: 0 bytes in 0 blocks.
==24344== Rerun with --leak-check=full to see details of leaked memory.

【讨论】:

  • 这里没有。奇怪的是,只有索引 80 干净地通过了。对于 79 或 81,它会检测到错误。对于 80 来说,它没有。 -80 与 -79 和 -81 相同。 Valgrind 版本 3.8.1。
  • 这取决于堆的布局,这取决于它是 32 位还是 64 位可执行文件以及 C 运行时分配的其他块。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 2012-05-31
  • 2018-01-27
  • 2017-12-07
相关资源
最近更新 更多