简体   繁体   English

gcc - 如何使用地址清理器

[英]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.我在 linux 上使用 gcc 4.8.5。我想使用地址消毒器,但它不返回有关该程序的任何信息。 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:泄漏 memory 的测试程序:

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

What is wrong?怎么了?

With GCC7 on a recent Debian/Sid/x86-64 I compiled this 在最近的Debian / Sid / x86-64上使用GCC7,我编译了这个

// 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 : 并且在执行./irbis正确检测到泄漏:

=================================================================
==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). 所以升级你的GCC编译器 (至少到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). 我知道GCC4.8对地址消毒剂和C ++ 11的支持不完全(BTW,GCC4.8已经过时,2017年11月GCC5也是如此)。

The cause of the problem might be that the main doesn't use ptr so it probably was optimized out entierly.问题的原因可能是 main 没有使用ptr所以它可能被完全优化了。 Consider this instead:考虑一下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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