Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC and -fsanitize=leak

I'm working and on a large C++ project and making it compile with clang would be painful, so I'm stuck with GCC.

I want to use the nice -fsanitize=leak flag that I already used with clang on a previous job, but it does not seem to work.

I made a very simple example to test it:

#include <stdlib.h>
void FooBar() {
  malloc(7);
}
int main() {
  FooBar();
  return 0;
}

With clang it works as expected:

>> clang -fsanitize=leak main.cpp
>> LSAN_OPTIONS=detect_leaks=1 ./a.out
=================================================================
==18052==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 7 byte(s) in 1 object(s) allocated from:
#0 0x41dcbc  (~/dev/addresssanitizertest/a.out+0x41dcbc)
#1 0x431ac3  (~/dev/addresssanitizertest/a.out+0x431ac3)
#2 0x431ae3  (~/dev/addresssanitizertest/a.out+0x431ae3)
#3 0x7f8077e71a3f  (/lib/x86_64-linux-gnu/libc.so.6+0x20a3f)
SUMMARY: LeakSanitizer: 7 byte(s) leaked in 1 allocation(s).
>>

But with gcc it does not seem to detect anything:

>> gcc -fsanitize=leak main.cpp
>> LSAN_OPTIONS=detect_leaks=1 ./a.out
>>

Did I miss something like a nice environment variable? Did someone ever made it work with gcc?

EDIT: This works for instance:

g++ -fsanitize=address main.cpp
ASAN_OPTIONS=detect_leaks=1 ./a.out

But I can't do that: the perf drawback is too much. I only want leak detection.

like image 562
Jeremad Avatar asked Jul 03 '15 15:07

Jeremad


People also ask

What is GCC and CPP?

GCC stands for GNU Compiler Collections which is used to compile mainly C and C++ language. It can also be used to compile Objective C and Objective C++.

Is GCC and G ++ the same?

We use the GCC command for compiling the C program. The G++ command is capable of compiling the . cpp or . c files, but they will be treated and released in the form of C++ files only.

Which is better GCC or G ++?

gcc vs g++ All stock C programs can be made to compile under g++ and without using any C++ libraries or templates. Our experience is that g++ is a better compiler than gcc.

What is GNU vs GCC?

The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages, hardware architectures and operating systems. The Free Software Foundation (FSF) distributes GCC as free software under the GNU General Public License (GNU GPL).


2 Answers

You must read this and use the patch :

https://gcc.gnu.org/ml/gcc-patches/2013-11/msg01874.html

like image 181
eroween Avatar answered Sep 29 '22 06:09

eroween


I resolved it with gcc 5.1 (I was using 4.9).

EDIT: it looks like 5.2 does not work either EDIT2: it does not work with the gcc provided with ubuntu 15.10 (5.2.1), however I recompiled a 5.2.0 from sources and it worked fine. I really have no clue.

like image 45
Jeremad Avatar answered Sep 29 '22 08:09

Jeremad