更改 printf 后 AddressSanitizer 未发现明显泄漏

AddressSanitizer not finding an obvious leak after changing printf

我绞尽脑汁想弄清楚为什么 ASAN 没有发现简单的内存泄漏。 valgrind 发现它很好。帮忙?

ASAN 找到的示例。

#include <stdlib.h>
#include <stdio.h>

void blah(void)
{
        int *some_int = malloc(sizeof(int));
        *some_int = 1;
        printf("hello %p\n", 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 %d\n", *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 变量成为本地变量或其他一些诡计。那么:为什么 AddressSanitizer 没有选择这个呢?我是否漏掉了一些明显的东西?

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

我从 ASAN 人员那里得知这是一个已知错误: https://github.com/google/sanitizers/issues/937

LeakSanitizer:当函数堆栈帧重叠时出现假阴性 #937