【问题标题】:AddressSanitizer not finding an obvious leak after changing printfAddressSanitizer 在更改 printf 后没有发现明显的泄漏
【发布时间】:2020-03-02 10:37:40
【问题描述】:

我正在挠头,试图弄清楚为什么 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

...现在泄漏没有出现。

对于后者,如果我在不使用 sanitizer 的情况下重新编译并运行 valgrind,valgrind 确实会显示泄漏: ==10782== 肯定丢失:1 个块中的 4 个字节

查看程序集:我看到优化器没有使我的 malloc 变量成为本地变量或其他一些诡计。那么:为什么 AddressSanitizer 不选择这个呢?我错过了什么明显的东西吗?

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

【问题讨论】:

  • FWIW,当我在装有 clang 3.4.2 的 CentOS 7.7 系统上尝试这个时,它报告任何一个都没有内存问题。
  • @Steve 你设置了 ASAN_OPTIONS=detect_leaks=1 吗?显然 lsan 默认是关闭的。 github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer
  • 这是我第一次使用 clang,但按照您的建议再次尝试会发现相同的行为。

标签: c memory-leaks address-sanitizer


【解决方案1】:

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

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

【讨论】:

  • 是的; SO让我至少要等5个小时才能做到,呵呵。我不知道为什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 2012-11-16
  • 1970-01-01
  • 2012-01-23
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多