日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

AddressSanitizer+cmake

發(fā)布時間:2023/11/27 生活经验 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AddressSanitizer+cmake 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. AddressSanitizer+cmake(Linux)

編譯指令:
CXXFLAGS通常需要加上

-fsanitize=address
-fno-omit-frame-pointer     #打印函數(shù)調(diào)用路徑
-fsanitize-recover=address  #AddressSanitizer遇到錯誤時能夠繼續(xù)
-fsanitize=address-fno-omit-frame-pointer-fsanitize-recover=address### 2. 運行時動態(tài)鏈接
設(shè)置環(huán)境變量,如設(shè)置AddressSanitizer遇到錯誤時繼續(xù)運行export ASAN_OPTIONS="halt_on_error=0:abort_on_error=0"

鏈接ASAN庫

LD_PRELOAD=xxxxx/libasan.so ./out
sun@sun:~/nolo/asan_test$ g++ asan_test.cpp -o main -fsanitize=leak
sun@sun:~/nolo/asan_test$ ls
asan_test.cpp  main
sun@sun:~/nolo/asan_test$ ./main =================================================================
==184528==ERROR: LeakSanitizer: detected memory leaksDirect leak of 40 byte(s) in 1 object(s) allocated from:#0 0x7fa1c10cc501 in operator new[](unsigned long) ../../../../src/libsanitizer/lsan/lsan_interceptors.cpp:231#1 0x5595b681319e in main (/home/sun/nolo/asan_test/main+0x119e)#2 0x7fa1c0ba50b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)SUMMARY: LeakSanitizer: 40 byte(s) leaked in 1 allocation(s).
sun@sun:~/nolo/asan_test$ cat asan_test.cpp 
#include <iostream>int main()
{
int* p = new int[10];
p = NULL;
return 0;
}

3. 安裝

AddressSanitizer(地址殺菌劑,簡稱 ASan) 是谷歌出品的內(nèi)存檢查工具,比 Valgrind 更高效。其由兩部組成:編譯器 instrumentation 模塊
提供malloc()/free()替代項的運行時庫
gcc 4.8 開始,AddressSanitizer 成為 gcc 的一部分,但不支持符號信息,無法顯示出問題的函數(shù)和行數(shù)。從 4.9 開始,gcc 支持 AddressSanitizer 的所有功能。Ubuntu 一般不用安裝,CentOS 一般需要安裝。如果使用 AddressSanitizer 時報錯:/usr/bin/ld: cannot find /usr/lib64/libasan.so.0.0.0
則需要先安裝。Ubuntu 安裝命令:sudo apt-get install libasan0
CentOS 安裝命令:sudo yum install libasan
使用
在用 gcc 編譯程序時,指定 -fsanitize=address 選項即可自動調(diào)用 AddressSanitizer。運行程序時,就可以看到相關(guān)信息。通過 -g 選項,可以看到報錯的函數(shù)和行號。

3. 參考

博客園:https://www.cnblogs.com/kika/p/10851494.html
Github 地址:https://github.com/google/sanitizers
Wiki 地址:https://github.com/google/sanitizers/wiki/AddressSanitizer
基本使用:https://blog.csdn.net/c_lazy/article/details/80009627
輸出信息的詳細(xì)解釋:https://www.jianshu.com/p/3a2df9b7c353

總結(jié)

以上是生活随笔為你收集整理的AddressSanitizer+cmake的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。