我没有在 clang LeakSanitizer 输出中获取源代码引用。我错过了什么?
我的问题有一个次要目标。如果我无法让 LeakSanitizer 显示源代码行,如何将
(/home/x/a.out+0xd4f39)
等输出交叉引用到源代码?
clang 文档有以下用法示例...
https://releases.llvm.org/14.0.0/tools/clang/docs/LeakSanitizer.html
$ cat memory-leak.c
include <stdlib.h>
void *p;
int main() {
p = malloc(7);
p = 0; // The memory is leaked here.
return 0;
}
clang -fsanitize=address -g memory-leak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out
==23646==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 7 byte(s) in 1 object(s) allocated from:
0 0x4af01b in __interceptor_malloc /projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52:3
1 0x4da26a in main memory-leak.c:4:7
2 0x7f076fd9cec4 in __libc_start_main libc-start.c:287
SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).
但是当我在我的系统上尝试他们的示例时,我得到了
$ clang -fsanitize=address -g memory-leak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out
=================================================================
==67103==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 7 byte(s) in 1 object(s) allocated from:
#0 0x5558ea37af39 (/home/x/a.out+0xd4f39) (BuildId: 23165eb1ea7f2560b5067bb2c4436e6e17eb5ebb)
#1 0x5558ea3bedd8 (/home/x/swrender/a.out+0x118dd8) (BuildId: 23165eb1ea7f2560b5067bb2c4436e6e17eb5ebb)
#2 0x7fd945c9328f (/usr/lib/libc.so.6+0x2328f) (BuildId: 1e94beb079e278ac4f2c8bce1f53091548ea1584)
SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).
源代码输出丢失。我使用的是 clang 版本 14.0.6,上面的示例取自 14.0.0 文档,这是我能找到的最接近的匹配。
$ clang -v
clang version 14.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/12.2.0
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
跑步前设置
ASAN_SYMBOLIZER_PATH
例如:
ASAN_SYMBOLIZER_PATH=/usr/local/bin/llvm-symbolizer
这在 https://clang.llvm.org/docs/AddressSanitizer.html#symbolizing-the-reports
上进行了描述此答案中有更多详细信息,这是关于 MemorySanitizer 的,但似乎适用相同的规则: