c-AddressSanitizer在更改printf后没有发现明显的泄漏



我绞尽脑汁想弄清楚ASAN为什么没有发现一个简单的内存泄漏。valgrind觉得很好。帮助

ASAN查找的示例

#include <stdlib.h>
#include <stdio.h>
void blah(void)
{
int *some_int = malloc(sizeof(int));
*some_int = 1;
printf("hello %pn", some_int);
// some_int is lost here
}
int main()
{
blah();
return 0;
}
mbryan@remotedev-mbryan:~/git/mbryan/onefs$ clang -fsanitize=address -O0 q.c
mbryan@remotedev-mbryan:~/git/mbryan/onefs$ ./a.out
hello 0x602000000010
=================================================================
==10751==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 4 byte(s) in 1 object(s) allocated from:
#0 0x4d9bd0 in malloc (/ifs/home/mbryan/git/mbryan/onefs/a.out+0x4d9bd0)
#1 0x5120f3 in blah (/ifs/home/mbryan/git/mbryan/onefs/a.out+0x5120f3)
#2 0x512183 in main (/ifs/home/mbryan/git/mbryan/onefs/a.out+0x512183)
#3 0x7f3515000b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310
SUMMARY: AddressSanitizer: 4 byte(s) leaked in 1 allocation(s).

到目前为止还不错。现在打印值而不是指针:

#include <stdlib.h>
#include <stdio.h>
void blah(void)
{
int *some_int = malloc(sizeof(int));
*some_int = 1;
printf("hello %dn", *some_int);  // <---------------
}
int main()
{
blah();
return 0;
}
mbryan@remotedev-mbryan:~/git/mbryan/onefs$ clang -fsanitize=address -O0 q.c
mbryan@remotedev-mbryan:~/git/mbryan/onefs$ ./a.out
hello 1

现在泄漏没有出现。

在后者上,如果我在没有消毒程序的情况下重新编译并运行valgrind,valgrind确实显示了泄漏:===10782===肯定丢失:1块中有4个字节

看看程序集:我发现优化器并没有让我的malloc'd变量成为局部变量或其他一些技巧。那么:为什么AddressSanitizer没有收到这个?我是不是错过了一些显而易见的东西?

这是在Ubuntu18.04上使用clang 6.0.0-1ubuntu2。

ASAN人员通知我,这是一个已知的错误:https://github.com/google/sanitizers/issues/937

LeakSanitizer:当函数堆栈帧覆盖#937 时为假阴性

最新更新