使 valgrind 因堆损坏检查错误而中止?
我想尝试使用 valgrind 进行一些堆损坏检测。通过以下损坏的“单元测试”:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
char * c = (char *) malloc(10) ;
memset( c, 0xAB, 20 ) ;
printf("not aborted\n") ;
return 0 ;
}
我惊讶地发现 valgrind 不会在错误时中止,而只是产生一条消息:
valgrind -q --leak-check=no a.out ==11097== Invalid write of size 4 ==11097== at 0x40061F: main (in /home/hotellnx94/peeterj/tmp/a.out) ==11097== Address 0x51c6048 is 8 bytes inside a block of size 10 alloc'd ==11097== at 0x4A2058F: malloc (vg_replace_malloc.c:236) ==11097== by 0x400609: main (in /home/hotellnx94/peeterj/tmp/a.out) ... not aborted
我没有看到 valgrind 选项在错误时中止(就像 gnu-libc 的 mcheck 那样,但是我不能使用 mcheck 因为它不是线程安全的)。有谁知道这是否可能(我们的代码 dup2 的标准输出到 /dev/null 因为它作为守护进程运行,所以报告没有用,我宁愿抓住罪魁祸首或更接近罪魁祸首)。
I'd like to try using valgrind to do some heap corruption detection. With the following corruption "unit test":
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
char * c = (char *) malloc(10) ;
memset( c, 0xAB, 20 ) ;
printf("not aborted\n") ;
return 0 ;
}
I was suprised to find that valgrind doesn't abort on error, but just produces a message:
valgrind -q --leak-check=no a.out ==11097== Invalid write of size 4 ==11097== at 0x40061F: main (in /home/hotellnx94/peeterj/tmp/a.out) ==11097== Address 0x51c6048 is 8 bytes inside a block of size 10 alloc'd ==11097== at 0x4A2058F: malloc (vg_replace_malloc.c:236) ==11097== by 0x400609: main (in /home/hotellnx94/peeterj/tmp/a.out) ... not aborted
I don't see a valgrind option to abort on error (like gnu-libc's mcheck does, but I can't use mcheck because it isn't thread safe). Does anybody know if that is possible (our code dup2's stdout to /dev/null since it runs as a daemon, so a report isn't useful and I'd rather catch the culprit in the act or closer to it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
valgrind 中没有这样的选项。
考虑将非守护程序模式(调试模式)添加到您的守护程序中。
http://valgrind.org/docs/manual/mc-manual .html#mc-manual.clientreqs 4.6 解释了从调试程序到 valgrind+memcheck 的一些请求,因此您可以在守护程序中使用其中一些在固定代码位置进行一些检查。
There is no such option in valgrind.
Consider adding a non-daemon mode (debug mode) into your daemon.
http://valgrind.org/docs/manual/mc-manual.html#mc-manual.clientreqs 4.6 explains some requests from debugged program to valgrind+memcheck, so you can use some of this in your daemon to do some checks at fixed code positions.