OS X Yosemite上的Valgrind,出现虚假错误?

我正在跟随学习C艰难的方式 ,我正在练习4:介绍Valgrind 。 我在Mac OS X Yosemite上,在撰写本文时,没有稳定的Valgrind for Yosemite版本。 我找到了Yosemite和Valgrind,并使用了最高投票回答brew install --HEAD valgrind 。 这个安装了Valgrind和我能够跟随Zed的练习。 但是,当我“修复”应用程序时,我仍然遇到错误。

为了仔细检查,我回到练习3 ,这应该没有错误,但我仍然在Valgrind中出错。 这是代码,然后是输出:

ex3.c

 #include  int main() { int age = 10; int height = 72; printf("I am %d years old.\n", age); printf("I am %d inches tall.\n", height); return 0; } 

在iTerm中:

 ransom:learn-c-the-hard-way ben$ rm -f ex3 ransom:learn-c-the-hard-way ben$ make ex3 cc -Wall -g ex3.c -o ex3 ransom:learn-c-the-hard-way ben$ valgrind ./ex3 ==8795== Memcheck, a memory error detector ==8795== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==8795== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info ==8795== Command: ./ex3 ==8795== ==8795== Conditional jump or move depends on uninitialised value(s) ==8795== at 0x1003FBC3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib) ==8795== by 0x1001EFB96: __sfvwrite (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x1001F9FE5: __vfprintf (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x10021F9AE: __v2printf (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x10021FC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x1001F5B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x1001F39D7: printf (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x100000F2D: main (ex3.c:8) ==8795== ==8795== Conditional jump or move depends on uninitialised value(s) ==8795== at 0x1003FBC47: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib) ==8795== by 0x1001EFB96: __sfvwrite (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x1001F9FE5: __vfprintf (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x10021F9AE: __v2printf (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x10021FC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x1001F5B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x1001F39D7: printf (in /usr/lib/system/libsystem_c.dylib) ==8795== by 0x100000F2D: main (ex3.c:8) ==8795== I am 10 years old. I am 72 inches tall. ==8795== ==8795== HEAP SUMMARY: ==8795== in use at exit: 38,888 bytes in 426 blocks ==8795== total heap usage: 506 allocs, 80 frees, 45,016 bytes allocated ==8795== ==8795== LEAK SUMMARY: ==8795== definitely lost: 0 bytes in 0 blocks ==8795== indirectly lost: 0 bytes in 0 blocks ==8795== possibly lost: 0 bytes in 0 blocks ==8795== still reachable: 4,096 bytes in 1 blocks ==8795== suppressed: 34,792 bytes in 425 blocks ==8795== Rerun with --leak-check=full to see details of leaked memory ==8795== ==8795== For counts of detected and suppressed errors, rerun with: -v ==8795== Use --track-origins=yes to see where uninitialised values come from ==8795== ERROR SUMMARY: 4 errors from 2 contexts (suppressed: 0 from 0) 

它说我得到Conditional jump or move depends on uninitialized value(s) ex3.c:8 Conditional jump or move depends on uninitialized value(s) ,但height变量在第6行初始化。

我的猜测是这是Valgrind在优胜美地的一个问题而且错误是假的,但我对C很新,虽然我很确定代码是正确的,但我也不知道是否有某些东西我我失踪了。

这是Valgrind或我的代码的问题吗?

这份具体的报告对Valgrind来说是误报。 Yosemite上的Valgrind尚未完全涵盖所有极端情况的系统库以及这些库使用的优化。

这里的提示是函数名称_platform_memchr $ VARIANT $ Haswell,即此错误的存在将取决于您的系统硬件,在这种情况下,您是否拥有基于Intel Haswell的CPU。

如果您可以根据Valgrind的http://valgrind.org/support/bug_reports.html报告此错误,那么可以在下一个稳定的Valgrind版本之前修复它。

完全披露:我是Valgrind开发人员之一,他们提供了补丁以支持OS X 10.10

你可以使用参数运行valgrind来忽略库代码。

然后所有这些(你应该忽略)库错误消息将消失。

来自valgrind.org网页: http : //valgrind.org/docs/manual/manual-core.html#manual-core.suppress

错误检查工具可以检测系统库中的许多问题,例如C库,它是随操作系统预安装的。 您无法轻松修复这些错误,但您不希望看到这些错误(是的,有很多错误!)因此,Valgrind会在启动时读取要抑制的错误列表。 构建系统时,。/ configure脚本会创建默认抑制文件。

您可以随意修改并添加到抑制文件,或者更好地编写自己的文件。 允许多个抑制文件。 如果您的项目的一部分包含您不能或不想修复的错误,这很有用,但您不希望不断地提醒它们。

注意:到目前为止,添加抑制的最简单方法是使用Core命令行选项中描述的--gen-suppressions=yes 选项 。 这会自动生成抑制。 但是,为了获得最佳结果,您可能希望--gen-suppressions=yes编辑--gen-suppressions=yes的输出,在这种情况下,建议您--gen-suppressions=yes阅读本节。

每个要被抑制的错误都会被非常具体地描述,以最大限度地减少抑制指令无意中抑制了您想要看到的一堆类似错误的可能性。 抑制机制旨在允许精确但灵活的错误指定来抑制。

好吧,我可以添加另一个“我在Yosemite上运行的自制valgrind不会发生这种情况”。 该二进制文件的日期为2014-11-25,版本为“Valgrind-3.11.0.SVN”。 运行测试代码,我得到输出:

 $ valgrind --suppressions=suppressions ./ex3 ==40416== Memcheck, a memory error detector ==40416== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==40416== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info ==40416== Command: ./ex3 ==40416== --40416-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option --40416-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times) --40416-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times) I am 10 years old. I am 72 inches tall. ==40416== ==40416== HEAP SUMMARY: ==40416== in use at exit: 39,086 bytes in 428 blocks ==40416== total heap usage: 511 allocs, 83 frees, 45,358 bytes allocated ==40416== ==40416== LEAK SUMMARY: ==40416== definitely lost: 0 bytes in 0 blocks ==40416== indirectly lost: 0 bytes in 0 blocks ==40416== possibly lost: 0 bytes in 0 blocks ==40416== still reachable: 25,940 bytes in 308 blocks ==40416== suppressed: 13,146 bytes in 120 blocks ==40416== Rerun with --leak-check=full to see details of leaked memory ==40416== ==40416== For counts of detected and suppressed errors, rerun with: -v ==40416== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) $ 

我的抑制文件列出了来自Yosemite运行时的18个泄漏(并且没有访问错误)(如果有帮助,我很乐意分享它;它随着时间的推移用--gen-suppressions=yes选项创建)。 我正在运行一款带有英特尔酷睿i7的老式(2011年初,17英寸)MacBook Pro。

我不喜欢’Unknown mach_msg’消息,但它们总是出现在我身上,并且它们不会自然地停止valgrind工作(它发现了我的真正问题,并且没有报告应该工作的代码上的错误)。

我认为您遇到的问题是在系统库中,并且抑制这两个消息是合理的,但是不得不这样做(非常不希望必须抑制这么多的o / s泄漏)。

Valgrind绝对是优胜美地的实验。 但是,我正在以不同的方式(更正确?)运行您的示例。 我也使用svn版本,可能比你更新,因为我在测试之前更新了它。 另一个区别是我自己建造它,没有使用brew。

 ==14456== Memcheck, a memory error detector ==14456== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==14456== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info ==14456== Command: ./t1 ==14456== I am 10 years old. I am 72 inches tall. ==14456== ==14456== HEAP SUMMARY: ==14456== in use at exit: 38,396 bytes in 418 blocks ==14456== total heap usage: 503 allocs, 85 frees, 44,692 bytes allocated ==14456== ==14456== LEAK SUMMARY: ==14456== definitely lost: 0 bytes in 0 blocks ==14456== indirectly lost: 0 bytes in 0 blocks ==14456== possibly lost: 0 bytes in 0 blocks ==14456== still reachable: 4,096 bytes in 1 blocks ==14456== suppressed: 34,300 bytes in 417 blocks ==14456== Rerun with --leak-check=full to see details of leaked memory ==14456== ==14456== For counts of detected and suppressed errors, rerun with: -v ==14456== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 

更新:

这就是我构建valgrind的方法(默认选项):

 ./autogen.sh ./configure make sudo make install 

这是我的示例二进制文件链接的libc(libSystem metalibrary)版本:

 $ otool -L t1 t1: /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0) 

这是我的铿锵版:

 $ clang -v Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.1.0 Thread model: posix