为什么valgrind抱怨完美初始化缓冲区?

时间:2019-01-13 20:14:58

标签: valgrind glibc

这是测试代码“ valgrind.c”。它初始化堆栈缓冲区,然后对其进行简单的字符串比较。

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


int  main( void)
{
   char   buf[ 6];

   memset( buf, 'X', sizeof( buf));
   if( strncmp( buf, "XXXX", 4))
      abort();

   return( 0);
}

我用cc -O0 -g valgrind.c -o valgrind进行编译。 单独运行,效果很好。 当我通过valgrind --track-origins=yes ./valgrind运行它时,尽管这给了我:

==28182== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==28182== Conditional jump or move depends on uninitialised value(s)
==28182==    at 0x4E058CC: ??? (in /lib/x86_64-linux-gnu/libc-2.28.so)
==28182==    by 0x4CAA09A: ??? (in /lib/x86_64-linux-gnu/libc-2.28.so)
==28182==  Uninitialised value was created by a stack allocation
==28182==    at 0x4CA9FBD: ??? (in /lib/x86_64-linux-gnu/libc-2.28.so)

这对我来说真的没有任何意义。我正在Ubuntu 18.10上运行它。

1 个答案:

答案 0 :(得分:0)

答案是 valgrind 库存在错误。完全dist升级后,现在一切正常。 valgrind和可执行文件的版本号保持不变(我当前的dpkg编号现在为1:3.13.0-2ubuntu6,抱歉,我忘记记下旧版本了。)

这些是strace打开的带有其阴影的库。实际上,这与打开的库有所不同,您可以看到在两种情况下libc以及实际的test和valgrind可执行文件均未更改:

损坏:

41bd206c714bcd2be561b477d756a4104dddd2d3578040cca30ff06d19730d61  /etc/ld.so.cache
b0d9f1bc02b4500cff157d16b2761b9b2420151cc129de37ccdecf6d3005a1e0  /lib64/ld-linux-x86-64.so.2
b0d9f1bc02b4500cff157d16b2761b9b2420151cc129de37ccdecf6d3005a1e0  /lib/x86_64-linux-gnu/ld-2.28.so
701e316140eda639d651efad20b187a0811ea4deac0a52f8bcd322dffbb29d94  /lib/x86_64-linux-gnu/libc-2.28.so
701e316140eda639d651efad20b187a0811ea4deac0a52f8bcd322dffbb29d94  /lib/x86_64-linux-gnu/libc.so.6
38705bdbed45a77c2de28bedf5560d6ca016d57861bf60caa42255ceab8f076a  /tmp/valgrind
4652774bd116cb49951ef74115ad4237cad5021b2bd4d80002f09d986ec438b9  /usr/bin/valgrind
0369719ef5fe66d467a385299396bab0937002694ffc78027ede22c09d39abf3  /usr/lib/valgrind/default.supp
16b5f1e6ae25663620edb8f8d4a7f1a392e059d6cf9eb20a270129295548ffb2  /usr/lib/valgrind/memcheck-amd64-linux
6335747b07b2e8a6150fbfa777ade9bd80d56626bba9772d61c7d33328e68bda  /usr/lib/valgrind/vgpreload_core-amd64-linux.so
827b4c18aefad7788b6e654b1519d3caa1ab223cf7a6ba58d22d7ad7d383b032  /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
38705bdbed45a77c2de28bedf5560d6ca016d57861bf60caa42255ceab8f076a  ./valgrind

健康:

b0d9f1bc02b4500cff157d16b2761b9b2420151cc129de37ccdecf6d3005a1e0  /lib64/ld-linux-x86-64.so.2
b0d9f1bc02b4500cff157d16b2761b9b2420151cc129de37ccdecf6d3005a1e0  /lib/x86_64-linux-gnu/ld-2.28.so
701e316140eda639d651efad20b187a0811ea4deac0a52f8bcd322dffbb29d94  /lib/x86_64-linux-gnu/libc-2.28.so
701e316140eda639d651efad20b187a0811ea4deac0a52f8bcd322dffbb29d94  /lib/x86_64-linux-gnu/libc.so.6
38705bdbed45a77c2de28bedf5560d6ca016d57861bf60caa42255ceab8f076a  /tmp/valgrind
4652774bd116cb49951ef74115ad4237cad5021b2bd4d80002f09d986ec438b9  /usr/bin/valgrind
391826262f9dc33565a8ac0b762ba860951267e73b0b4db7d02d1fd62782f8c8  /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.28.so
3ab1f160af6c3198de45f286dd569fad7ae976a89ff1655e955ef0544b8b5d6c  /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.28.so
ae4ea44f87787b9b80d19a69ad287195dc7840eea08c08732d36d2ef1e6ecff3  /usr/lib/valgrind/default.supp
ba18f39979d22efc89340b839257f953a505ef5ca774b5bf06edd78ecb6ed86e  /usr/lib/valgrind/memcheck-amd64-linux
1649637bba73e84b962222f3756cc810c5413239ed180e0029cd98f069612613  /usr/lib/valgrind/vgpreload_core-amd64-linux.so
ab1501fa569e0185dea7248648255276ca965bbe270803dcbb930a22ea7a59b7  /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
38705bdbed45a77c2de28bedf5560d6ca016d57861bf60caa42255ceab8f076a  ./valgrind

感谢有用的评论,尤其是Florian的评论,这使我走上了正轨。