使用AddressSanitarizer的clang与gcc行为差异

使用AddressSanitarizer的clang与gcc行为差异,gcc,memory-leaks,clang,address-sanitizer,Gcc,Memory Leaks,Clang,Address Sanitizer,我有一个内存泄漏的示例代码。虽然clang正确地显示了泄漏,但我无法使用gcc实现同样的效果。我使用的gcc版本是4.8.5-39 代码: 通用条款: 我需要使用gcc。有人能帮我理解为什么gcc表现得不像叮当声,我应该怎么做才能让它正常工作。中的此项与以下内容相关: Q: Why didn't ASan report an obviously invalid memory access in my code? A1: If your errors is too obvious, com

我有一个内存泄漏的示例代码。虽然clang正确地显示了泄漏,但我无法使用gcc实现同样的效果。我使用的gcc版本是4.8.5-39

代码:

通用条款:

我需要使用gcc。有人能帮我理解为什么gcc表现得不像叮当声,我应该怎么做才能让它正常工作。

中的此项与以下内容相关:

Q: Why didn't ASan report an obviously invalid
   memory access in my code?

A1: If your errors is too obvious, compiler might have
    already optimized it out by the time Asan runs.
有人能帮我理解为什么gcc表现得不像叮当声吗

Gcc-4.8

该修补程序支持
-fsanize=leak
,并且可能不会在4.8版本中进行后端口

GCC-8.3在检测该泄漏时没有问题:

$ gcc -g -fsanitize=address memory-leak.c && ./a.out

=================================================================
==166614==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7 byte(s) in 1 object(s) allocated from:
    #0 0x7fcaf3dc5330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x55d297afc162 in main /tmp/memory-leak.c:4
    #2 0x7fcaf394052a in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).

但与ASan一起使用时,同样的ASan会检测内存泄漏clang@VinodiniNatrajanClang的Asan的实现与GCC完全不同,所以不要期望它们之间100%匹配。一般来说,他们会检测到或多或少相同的错误,但这并不能保证,特别是在像这样对其他优化过程非常敏感的角落情况下。请使用不太古老的gcc版本再试一次,它适合我。
gcc -fsanitize=address -g memory-leak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out

Q: Why didn't ASan report an obviously invalid
   memory access in my code?

A1: If your errors is too obvious, compiler might have
    already optimized it out by the time Asan runs.
$ gcc -g -fsanitize=address memory-leak.c && ./a.out

=================================================================
==166614==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7 byte(s) in 1 object(s) allocated from:
    #0 0x7fcaf3dc5330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x55d297afc162 in main /tmp/memory-leak.c:4
    #2 0x7fcaf394052a in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).