Clang 的 ASan 不检测悬挂指针的使用

Clang's ASan does not detect dangling pointer use

在工具比较的上下文中,如果 ASan 可以检测到以下程序中的问题,我不想对 ASan 不公平:

$ cat t.c
#include <stdio.h>

int *G;

int f(void) {
  int l = 1;
  int res = *G;
  G = &l;
  return res + *G;
}

int main(void) {
  int x = 2;
  G = &x;
  f();
  printf("%d\n", f());
}
$ clang -v
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
...
$ clang -O2 -fsanitize=address t.c
$ ./a.out 
1
$ clang -fsanitize=address t.c
$ ./a.out 
2

第一次出现 G 第二次调用 f 调用未定义的行为,因为 G 在那个时候是不确定的。此外,G 会立即取消引用,从而使这种内存错误成为 ASan 可能会检测到的错误。这是 ASan 规范的一部分,它有时无法检测到它应该发现的问题,但我想知道我是否可以在这里使用它来发现这个特定问题。

我找到了选项 -fsanitize-address-use-after-scope here,但是这个选项在我使用的 Clang 版本中不起作用:

$ clang -fsanitize=address t.c -fsanitize-address-use-after-scope
clang: error: unknown argument: '-fsanitize-address-use-after-scope'

是否有 ASan 版本在执行上述程序时标记错误,有或没有特殊的命令行选项?

您的版本:clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)

页眉:Clang 5 documentation

您必须更新您的 clang

您在这里谈论 return 后使用错误。这些应该由 ASan 支持,但由于显着更高的内存开销而默认禁用(参见 here 了解详细信息)。要启用,运行 和 ASAN_OPTIONS=detect_stack_use_after_return=1.

不幸的是,我无法检查它是否适用于您的特定情况,但如果不适用,您应该在 ASan's tracker.

提交错误

yugr 为我指出了在我的测试程序中激活错误检测的正确方法。此功能已存在于 Clang 3.8 中。

为完整起见,Clang 3.8 的结果如下。有趣的是,该问题是在默认优化级别检测到的,但未在 -O2.

检测到
$ clang -fsanitize=address t.c -Wall
$ ASAN_OPTIONS=detect_stack_use_after_return=1 ./a.out 
=================================================================
==21949==ERROR: AddressSanitizer: stack-use-after-return on address 0x7f5eeb100060 ...
READ of size 4 at 0x7f5eeb100060 thread T0
...
$ clang -O2 -fsanitize=address t.c -Wall
$ ASAN_OPTIONS=detect_stack_use_after_return=1 ./a.out
1