ubuntu 13.04 編譯 安裝 升級 gcc 4.9.0 address sanitizer

來源:互聯網
上載者:User

標籤:blog   io   art   代碼   div   ar   linux   c++   

@ 前記:

    最近查一個線上項目的crash,review代碼無果,crash幾率低,不可在本地環境重現。之後線上上好幾個伺服器跑valgrind就不crash了。個人猜測可能是跑valgrind後效能下降的比較嚴重,導致crash那個點觸發不到了。

    gcc從4.8開始支援address sanitizer,也比較好用,文章最後會簡單分析下用法。

    另我的環境是ubuntu13.04+gcc4.7.3,目標是升級到4.9.0。

@ 編譯gcc4.9.0

$cd /tmp$wget ftp://mirrors.kernel.org/gnu/gcc/gcc-4.9.0/gcc-4.9.0.tar.gz$tar zxvf gcc-4.9.0.tar.gz$cd gcc-4.9.0$./contrib/download_prerequisites$cd ..$mkdir gcc-build-4.9.0$cd gcc-build-4.9.0$../gcc-4.9.0/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib$make -j4$make install$gcc -v

補充:我這邊編譯出來的lib都在路徑/usr/local/lib64

@ 用法

#include <stdio.h>                                                                                int main()                                                                      {                                                                                   int *p = new int;                                                               delete p;                                                                       *p = 10;                                                                        return 0;                                                                   }

這是個訪問野指標的程式,但是用gcc編譯(不管加不加-g)後運行卻不會coredump。有了address sanitize,我們可以這樣

$g++ test.cc -fsanitize=address -g$export LD_LIBRARY_PATH=/usr/local/lib64/$./a.out

我摘一部分運行資訊貼出來

==25380==ERROR: AddressSanitizer: heap-use-after-free on address 0x60200000eff0 at pc 0x4007d1 bp 0x7ffffbe70890 sp 0x7ffffbe70888WRITE of size 4 at 0x60200000eff0 thread T0    #0 0x4007d0 in main /home/parallels/sss/cc/test/test.cc:7    #1 0x7f6454635ea4 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21ea4)    #2 0x4006a8 (/home/parallels/sss/cc/test/a.out+0x4006a8)0x60200000eff0 is located 0 bytes inside of 4-byte region [0x60200000eff0,0x60200000eff4)freed by thread T0 here:    #0 0x7f645525a3e7 in operator delete(void*) ../../../../gcc-4.9.0/libsanitizer/asan/asan_new_delete.cc:81    #1 0x400799 in main /home/parallels/sss/cc/test/test.cc:6    #2 0x7f6454635ea4 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21ea4)previously allocated by thread T0 here:    #0 0x7f6455259f6f in operator new(unsigned long) ../../../../gcc-4.9.0/libsanitizer/asan/asan_new_delete.cc:50    #1 0x400789 in main /home/parallels/sss/cc/test/test.cc:5    #2 0x7f6454635ea4 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21ea4)

這資訊夠清楚了吧。

@ 後記

    我暫時還沒有測它對效能的影響,希望上線後能協助定位到bug。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.