当我编译这个简单的测试程序时,我从地址清理程序中得到了明显的泄漏报告,但是当我编译相同的程序但具有无限循环时,并打破它发出的信号SIGINT
我没有得到任何输出。
检查 asm 输出,malloc
没有被优化掉(如果这可能的话)
这是地址清理程序的预期行为吗?我在其他开发中没有遇到这个问题。
工作示例:
#include <stdlib.h>
int main(void)
{
char *a = malloc(1024);
return 1;
}
不工作(用 SIGINT 杀死):
#include <stdlib.h>
int main(void)
{
char *a = malloc(1024);
for(;;);
return 1;
}
编译:gcc test.c -o test -fsanitize=address
我在完整的程序中遇到这个问题,但我将其简化为这个最小的示例。
我尝试了很多方法,与exit()
and abort()
调用,这有效:
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <setjmp.h>
jmp_buf jmpbuf;
void handler (int signum) {
printf("handler %d \n", signum);
// we jump from here to main()
// and then call return
longjmp(jmpbuf, 1);
}
int main(int argc, char *argv[])
{
if (setjmp(jmpbuf)) {
// we are in signal context here
return 2;
}
signal(SIGINT, handler);
signal(SIGTERM, handler);
char *a = malloc(1024);
while (argc - 1);
return 1;
}
结果是:
> gcc file.c -fsanitize=address && timeout 1 ./a.out arg
handler 15
=================================================================
==12970==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 1024 byte(s) in 1 object(s) allocated from:
#0 0x7f4798c9bd99 in __interceptor_malloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cc:86
#1 0x5569e64e0acd in main (/tmp/a.out+0xacd)
#2 0x7f479881206a in __libc_start_main (/usr/lib/libc.so.6+0x2306a)
SUMMARY: AddressSanitizer: 1024 byte(s) leaked in 1 allocation(s).
我猜想地址清理函数是在 main 返回后执行的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)