clang vs gcc behavioral difference with AddressSanitizer

五迷三道 提交于 2019-12-11 17:58:17

问题


I have a sample code with memory leaks. Though clang displays the leak correctly, I am unable to achieve the same with gcc. gcc version I am using is 4.8.5-39

Code:

#include <stdlib.h>
void *p;
int main() {
  p = malloc(7);
  p = 0; // The memory is leaked here.
  return 0;
}

CLANG:

clang -fsanitize=address -g memory-leak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out

=================================================================
==15543==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7 byte(s) in 1 object(s) allocated from:
    #0 0x465289 in __interceptor_malloc (/u/optest/a.out+0x465289)
    #1 0x47b549 in main /u/optest/memory-leak.c:4
    #2 0x7f773fe14544 in __libc_start_main /usr/src/debug/glibc-2.17-c758a686/csu/../csu/libc-start.c:266

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

GCC:

gcc -fsanitize=address -g memory-leak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out

I need to use gcc. Can someone help me understand why gcc isn't behaving like clang and what should I do to make it work.


回答1:


This item in Asan's FAQ is relevant:

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.



回答2:


Can someone help me understand why gcc isn't behaving like clang

Gcc-4.8 was released on March 22, 2013.

The patch to support -fsanitize=leak was sent on Nov. 15, 2013, and is likely not backported into the 4.8 release.

GCC-8.3 has no trouble detecting this leak:

$ 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).


来源:https://stackoverflow.com/questions/57971882/clang-vs-gcc-behavioral-difference-with-addresssanitizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!