nginx下使用asan和valgrind兩個靜態檢查工具


1、valgrind

wegit:http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2

tar -jxvf valgrind-3.12.0.tar.bz2

 cd valgrind-3.12.0

 ./configure

 make

sudo make install

輸入valgrind–h顯示valgrind的參數及提示,說明安裝成功

 sudo valgrind --tool=memcheck --leak-check=full /usr/local/nginx-1.4.2/sbin/nginx
==41400== Memcheck, a memory error detector
==41400== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==41400== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==41400== Command: /usr/local/nginx-1.4.2/sbin/nginx
==41400==
==41400== Warning: set address range perms: large range [0x957a040, 0x2e45f7e2) (undefined)
==41400== Warning: set address range perms: large range [0x957b040, 0x2e45f040) (defined)
nginx: [error] failed to initialize Lua VM in /usr/local/nginx-1.4.2/conf/nginx.conf:125
==41400==
==41400== HEAP SUMMARY:
==41400==     in use at exit: 619,703,912 bytes in 3,285 blocks
==41400==   total heap usage: 20,476 allocs, 17,191 frees, 626,783,730 bytes allocated
==41400==
==41400== 128 bytes in 1 blocks are possibly lost in loss record 583 of 650
==41400==    at 0x4C2ABA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==41400==    by 0x431D0A: ngx_alloc (ngx_alloc.c:22)
==41400==    by 0x4222CE: ngx_crc32_table_init (ngx_crc32.c:117)
==41400==    by 0x41A20B: main (nginx.c:320)
==41400==
==41400== 1,024 bytes in 1 blocks are definitely lost in loss record 626 of 650
==41400==    at 0x4C2D136: memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==41400==    by 0x4C2D251: posix_memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==41400==    by 0x431DBB: ngx_memalign (ngx_alloc.c:57)
==41400==    by 0x41B5C2: ngx_create_pool (ngx_palloc.c:21)
==41400==    by 0x419F52: main (nginx.c:299)
==41400==
==41400== LEAK SUMMARY:
==41400==    definitely lost: 1,024 bytes in 1 blocks
==41400==    indirectly lost: 0 bytes in 0 blocks
==41400==      possibly lost: 128 bytes in 1 blocks
==41400==    still reachable: 619,702,760 bytes in 3,283 blocks
==41400==         suppressed: 0 bytes in 0 blocks
==41400== Reachable blocks (those to which a pointer was found) are not shown.
==41400== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==41400==
==41400== For counts of detected and suppressed errors, rerun with: -v
==41400== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

解決的辦法是使用 openresty 的 luajit 倉庫的 v2.1-agentzh 分支重新:  https://github.com/openresty/luajit2/tree/v2.1-agentzh ,然后,重新編譯一個特殊版本的 LuaJIT,強制它使用系統的分配器,即在編譯 LuaJIT 時使用類似下面的命令: make CCDEBUG=-g Q= XCFLAGS='-DLUAJIT_USE_VALGRIND -DLUAJIT_USE_SYSMALLOC' 

 
編譯腳本做下面的修改:
 
nginx處理請求都是在worker進程中,如果不特殊配置valgrind只檢測master進程的信息,可以配置下面的樣子,設置worker進程啟動:

worker_processes 1;
daemon off;
master_process off;

或者使用在valgrind中使用–trace-children=yes 選項跟蹤子進程信息

啟動方式
或以日志形式記錄
sudo valgrind --log-file=./valgrind_report.log --leak-check=full --show-leak-kinds=all --show-reachable=no --track-origins=yes --trace-children= yes ./sbin/nginx -p . -c conf/main.conf
 
2、asan
(1)適配代碼:
編譯腳本中添加:

--fsanitize=address -fno-omit-frame-pointer -static-libasan
--with-ld-opt="-fsanitize=address -static-libasan"

makefile中添加 :-fsanitize=address -fno-omit-frame-pointer -static-libasan
nginx本身需要適配一些文件,不然,會誤認為是內存泄漏。
修改下面的文件:
 modified:   libs/libwtf/GNUmakefile
 modified:   libs/protobuf/Makefile
 modified:   src/core/nginx.h
 modified:   src/core/ngx_array.c
 modified:   src/core/ngx_array.h
 modified:   src/core/ngx_palloc.c
 modified:   src/core/ngx_palloc.h
(2)安裝clang
(3)編譯新版本運行起來,然后查看日志
默認情況下,asan遇到內存問題,就會終止當前進程的執行,並在spanner的日志文件error.log中打印相關的問題,以及問題的調用棧信息。
線上生成的調用棧只有函數地址,沒有符號信息,可以將生成的調用棧弄下來,


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM