简体   繁体   中英

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?

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

The cause of the problem might be that the main doesn't use ptr so it probably was optimized out entierly. Consider this instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM