Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc - how to use address sanitizer

I use gcc 4.8.5 on linux. I want to use address sanitizer but it doesn't return any information about the program. Flags:

SET(CMAKE_CXX_FLAGS "-Wall -Wno-error -g -std=c++11 -fno-omit-frame-pointer -fsanitize=address")
SET(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address")

Linked libraries:

target_link_libraries(testcpp asan)

The test program with a memory leak:

int main()
{
    int *prt = new int;
    return 0;
}

What is wrong ?

like image 306
Irbis Avatar asked Nov 09 '17 11:11

Irbis


1 Answers

With GCC7 on a recent Debian/Sid/x86-64 I compiled this

// file irbis.cc
int main()
{
  int *prt = new int;
  return 0;
}

using

g++ -fsanitize=address -g3 -std=c++11 irbis.cc -o irbis

and at execution of ./irbis a leak is rightfully detected :

=================================================================
==22742==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x7f77ea911340 in operator new(unsigned long) 
            (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdb340)
    #1 0x55ea91cca81b in main /home/basile/tmp/irbis.cc:4
    #2 0x7f77e9c1f2e0 in __libc_start_main 
            (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)

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

So upgrade your GCC compiler (to at least GCC6). I do know that GCC4.8 had incomplete support for address sanitizer & C++11 (BTW, GCC4.8 is obsolete, and so is GCC5, in november 2017).

like image 72
Basile Starynkevitch Avatar answered Sep 27 '22 21:09

Basile Starynkevitch