[英]gcc - how to use address sanitizer
我在 linux 上使用 gcc 4.8.5。我想使用地址消毒器,但它不返回有關該程序的任何信息。 標志:
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")
鏈接庫:
target_link_libraries(testcpp asan)
泄漏 memory 的測試程序:
int main()
{
int *prt = new int;
return 0;
}
怎么了?
在最近的Debian / Sid / x86-64上使用GCC7,我編譯了這個
// file irbis.cc
int main()
{
int *prt = new int;
return 0;
}
運用
g++ -fsanitize=address -g3 -std=c++11 irbis.cc -o irbis
並且在執行./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).
所以升級你的GCC編譯器 (至少到GCC6)。 我知道GCC4.8對地址消毒劑和C ++ 11的支持不完全(BTW,GCC4.8已經過時,2017年11月GCC5也是如此)。
問題的原因可能是 main 沒有使用ptr
所以它可能被完全優化了。 考慮一下:
// file irbis.cc
int main()
{
int *prt = new int;
return *ptr;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.