|
内存泄漏是指由于疏忽或错误造成程序未能释放已经不再使用的内存。内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制,从而造成了内存的浪费。$ u+ k5 f" @) u/ f2 r' V& K8 f
我们平时开发过程中不可避免的会遇到内存泄漏问题,你是如何排查的呢?估计你是使用下面这几个工具吧? valgrind mtrace dmalloc ccmalloc memwatch debug_new
# P6 U, ~5 M1 @1 ?, t E7 ^; @: F
这里程序喵向大家推荐新的一个排查内存泄漏的工具:AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本都可以使用,支持Linux、OS、Android等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有: 使用方法直接看我下面的代码: 检测内存泄漏 内存泄漏代码:
8 c# ^5 p: {2 _. Z( \ ] L- #include <stdlib.h>
e2 m0 d4 h7 h+ } k- G7 V - ( ?* u9 s4 W F" C
- void func1() { malloc(7); }1 T; E; u; `" E1 O; ]3 u
5 q5 p* `7 t$ c5 O# [, i+ t- void func2() { malloc(5); }
! K- G' I, w. t - 0 i' u1 f- r6 p0 f! D
- int main() {
" }* {6 ]( M8 R* q* e. y H' L - func1();: w: r- y. t G1 f/ \& \
- func2(); O, j! l1 G8 V) F- |4 B( l
- return 0;
# h6 F9 I7 N6 V0 R) G P, z - }
复制代码: W3 V; k- N, Y4 E3 `
编译and输出:
) S- N# i* ^- o7 W5 {+ @5 y9 p- g++ -fsanitize=address -g test_leak.cc && ./a.out! n* l$ S. w/ _* W2 M& d5 p
- * \. h" v% U( z6 n0 v# Q1 e/ n2 E
- =================================================================) Q+ F, U8 q U" C8 P
- ==103==ERROR: LeakSanitizer: detected memory leaks9 }6 u; t+ n* P6 x8 S* ]$ U# U
. W7 u0 N1 J( n# q' w' L! q* Y* v# C' H- Direct leak of 7 byte(s) in 1 object(s) allocated from:) k( q. l4 z2 F- Z0 [& n
- #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
. Y9 @, E/ ^% z p - #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:3
! w0 W7 @5 i6 A' J2 i( g - #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8
9 g0 o* Y1 T: q/ d - #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
R, U0 X5 t4 U1 Z% g4 B# q
; `* v+ B( F- m+ Z- Direct leak of 5 byte(s) in 1 object(s) allocated from:! G0 P5 L. Q0 M1 |0 X3 A& X6 r; h1 f
- #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
1 ~4 p! n R6 e7 k& z4 v1 f. c - #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:58 Q$ F1 l- [: |
- #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:9
! b2 Z# A! M: i ` - #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
& p" ^( i5 m3 A0 p3 x3 J% \9 y, V - ! D2 M0 Y* a A: n9 C5 b+ A
- SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码
. G) N' B* V) g8 H: V: Z* Q4 \9 [
编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。 检测堆栈内存越界访问 示例: - 7 C# U) e# { ^' Q
- #include <iostream>2 ~- {$ G% y: P8 M+ ^
7 b- D9 n) H; \4 {( h0 I, |- int main() {
/ D: R8 ^) s" p% y - int *array = new int[100];
& U3 U- {2 i* F. H/ C1 M - array[0] = 0;1 Z1 K2 T' J" Q3 r/ Z( q3 h5 ^
- int res = array[100]; // out of bounds3 v$ e- p: l. a! p* N; j r
- delete[] array; N- F& r: p, Y k
- return res;
; N3 D7 o0 K+ |2 H, I. x - }
复制代码
; f9 @$ @+ T: J7 v' I; n
编译and输出:8 ^3 ]) L' p2 |6 c1 d' i
- a& P2 H! \3 w- g++ -fsanitize=address -g test_leak.cc && ./a.out4 U" e; x: p2 V$ ?1 `' C$ n2 [. G
-
# `4 V7 h. M/ J) a, `% e - =================================================================# S& m1 x6 U& x% b+ T! k" @3 b
- ==110==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x7f0e06400d2e bp 0x7ffff5963f10 sp 0x7ffff5963f00
% f, `& I& W' m+ m# ` - READ of size 4 at 0x6140000001d0 thread T0
( {" s0 o' v- H& H2 f - #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:6; @7 W% U' \0 v; i- x( @. ~
- #1 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
5 X+ ^7 {" g6 G$ u8 j9 d - #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)
- c. i4 k6 j# A6 L% H - - {) Z1 w2 D b9 w c) a+ Y
- 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0)
. U ?% c5 A9 P - allocated by thread T0 here:
6 h' E& R' l5 P* b% l: M/ M - #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608); Z& |, {( U3 h9 @# _6 Q
- #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:42 M9 i8 g6 L1 @" U$ B& K
- #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)9 r3 s7 N+ y. g7 t( @& r
6 F' m: a* a( l, `# b+ o; L- SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main o; O5 b0 C- G) B$ U
- Shadow bytes around the buggy address:
" J% v0 [ C n% ~: L+ { - 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
& B( t" P S& d: ]0 ^9 ^) V, e - 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00! z; M# Z$ H* j2 |
- 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 002 M, r: z' z( e! r, P& E
- 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, J- X$ w. ~" Z% |, m8 H, t- H
- 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
, ?3 o$ f7 d4 c, A - =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa
' d9 U5 U: `: t - 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
8 [) @# Q' F- Z6 ]* S6 T. z - 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
* E; Y- T& k1 f/ b - 0x0c287fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa( S! _" Y& j7 q0 M% m" P
- 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
+ v+ O4 o7 q3 p5 S - 0x0c287fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa' E, v4 A' w9 g5 K4 N+ n2 o' n' _
- Shadow byte legend (one shadow byte represents 8 application bytes):6 x( k, I& r5 b( ^) l; k
- Addressable: 002 x* p) u$ Z+ ?
- Partially addressable: 01 02 03 04 05 06 07
4 o) Q% g9 N' I' D) ~5 \ ` - Heap left redzone: fa- V) \' [% ?8 M
- Freed heap region: fd
2 a5 j9 G d5 d- R - Stack left redzone: f1; h& ?: l6 m' X3 |# n |0 N
- Stack mid redzone: f2- P0 z1 Q1 Z+ `0 p& @
- Stack right redzone: f34 u4 g& W' I4 [2 @, B
- Stack after return: f5/ J4 ^- }% X' P
- Stack use after scope: f8" Q! E! b5 E9 _: o% U. p& W
- Global redzone: f9- U% J8 p; e# o" B9 r; e6 R1 i
- Global init order: f6
+ K* O W/ o1 w) }" I - Poisoned by user: f74 N7 ?$ S: S! U" ^. z, ?% r
- Container overflow: fc% H* u/ W3 C7 m
- Array cookie: ac/ c: P) g3 s, |6 g( d# E3 ~ V
- Intra object redzone: bb# {4 x6 R# c9 `5 l6 o& N: A
- ASan internal: fe
$ F2 L5 c/ k. L7 I, l1 d9 P - Left alloca redzone: ca
: _" Z( [0 M0 U9 p0 y# l - Right alloca redzone: cb
' P6 K) y. D- h# J - ==110==ABORTING
复制代码3 @! S2 ]6 H1 R7 |/ T
可以方便定位到堆栈内存越界访问的错误。 全局内存越界访问: 示例:
; G/ b( W* a {+ i5 |- #include <iostream>2 _% o! J' J5 `! v
- 6 p: ~4 o+ L6 c# R) C
- int global_array[100] = {0};/ s& Q! O* k% s# K8 K# p) ?
6 Q& j0 z3 f. p+ v( J9 [- int main() {! p4 d) m- o# [3 v* e8 }
- int res = global_array[100]; // out of bounds" z. H& D5 A$ R
- return 0; O' I0 t. m5 r/ I |
- }
复制代码
3 ^: J1 ?3 c+ {; ~1 @
编译and输出:
. K; O2 [$ \+ Y5 W: l
9 M% ~% ]5 H9 Q- O: l- g++ -fsanitize=address -g test_leak.cc && ./a.out
# W; c& P* j8 V# i) Y& e5 z/ P - =================================================================
0 q1 N' R& r: C' w' C& g - ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda52770
$ T- S; ^. `, i6 C% G5 X - READ of size 4 at 0x7f42e6e02310 thread T0 T: e3 b7 `' D: [8 ]* g
- #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:6& O+ \! O0 _! U5 v4 C
- #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
, _: x* y( N0 r0 B) z2 ^2 B - #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)6 N+ j$ L0 S) Z
- 7 ~7 O6 q& T0 |7 z! V
- 0x7f42e6e02310 is located 0 bytes to the right of global variable 'global_array' defined in 'test_leak.cc:3:5' (0x7f42e6e02180) of size 400
' e7 u; f& a( e# {* n' j+ V - SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
2 c7 d$ u( s8 `1 O) {3 a) w - Shadow bytes around the buggy address:
& f0 ~, Z3 S' `/ {, Z( H - 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 001 B+ O$ t9 Q% L4 ?/ C N
- 0x0fe8dcdb8420: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f9
: ?+ L2 k# G6 j - 0x0fe8dcdb8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; F G) M% w! N' R3 Z; ~$ _
- 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
3 V& |4 @0 Z% g$ ^3 l - 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
& w2 k2 C% c+ ? - =>0x0fe8dcdb8460: 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
+ E# Z) Z4 L" \5 b, U1 F - 0x0fe8dcdb8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00$ `2 A3 O& ~% n; G
- 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
- ]$ `+ h# S9 Q; {. {# Z6 X - 0x0fe8dcdb8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
; s% C8 Q0 z& ^) r; d9 K - 0x0fe8dcdb84a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
( w1 Z; j. V' Z% ]/ A w& { - 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ |7 ~5 A% p# B% b
- Shadow byte legend (one shadow byte represents 8 application bytes):
) k8 H: S5 {. f- d6 d - Addressable: 00( I O! q* l6 M6 k" R' i3 y
- Partially addressable: 01 02 03 04 05 06 07
& z3 F a1 z+ X6 r* e - Heap left redzone: fa
5 z; |/ W1 d1 ~, Q6 d7 M, H - Freed heap region: fd+ G2 M7 e, S( Q2 H8 f9 P! U
- Stack left redzone: f1% L# q# W& `" [! H- G2 B. W
- Stack mid redzone: f2
; L) Y; v, W4 V3 i1 s N5 U/ x - Stack right redzone: f3
1 X+ P: ?. E' y% {1 U; } - Stack after return: f5
8 j; \/ H4 s8 l- G# X7 ^ - Stack use after scope: f8
' L' d2 G e8 g( b( n, K( L# D - Global redzone: f9/ P( M: N" v- i! K$ A8 [4 g
- Global init order: f6% o) o5 K' G$ m9 j. l) v2 p# V- P9 O: ~
- Poisoned by user: f7
' \/ x( G) }% P0 n - Container overflow: fc9 d8 `& ]0 X. X& ]% v* N( O+ ~
- Array cookie: ac* W5 R" ?/ E" x1 K( ]
- Intra object redzone: bb
! m; N8 f& s! o( _ - ASan internal: fe0 C5 k9 L; h9 } C, g! q- j7 z' l, ^8 K* l
- Left alloca redzone: ca
0 s6 W, ^% |- ?' j - Right alloca redzone: cb
+ p' E6 c# k0 f! d* ` - ==116==ABORTING
复制代码
9 c4 b2 d! E1 {; e
局部内存被外层使用 示例:
# R. } Q. M) \; G, F8 [4 e$ F- #include <iostream>
/ X* H9 E) B$ a# O% w
% _$ B8 V- z( i/ U- volatile int *p = 0;
+ `+ R- e; ?8 `4 e6 O$ Q. ?- e
8 W. M! s8 p; f- int main() {$ P- Q6 U! ~6 i9 k! A S; Q
- {; u# ?3 }; Q# R7 q
- int x = 0;5 W7 `) e# I7 V
- p = &x;
/ x- x' s9 H; {; Z: ?+ b - }0 R0 A1 i) l% W! f K; m, F
- *p = 5;+ ?0 c, a. _3 {
- return 0;! O$ m8 `) O7 {8 N% v/ v
- }
复制代码
9 @; q4 g9 h- U/ E' j l
编译and输出:
/ J& {8 B, _( I! e1 r - - {6 x7 n, B8 P K; c! k3 }7 @
- g++ -fsanitize=address -g test_leak.cc && ./a.out
% f7 P& a: N0 Y M - =================================================================8 e2 t8 y5 J2 v* ^+ [: B& S
- ==243==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fffce12a4b0 at pc 0x7f3993e00e7e bp 0x7fffce12a480 sp 0x7fffce12a470 B! I3 S* c; ~
- WRITE of size 4 at 0x7fffce12a4b0 thread T0
4 X5 F' j; I7 G5 H - #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:10
+ |. Z" V i( }5 N - #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96): v X2 p& R d) I
- #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)
3 ?3 Y9 [! x: k$ J
& K) f8 d9 @9 t- Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame
/ T' n7 {) a; d5 r - #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5! ~8 e; K& S7 y
' x3 H: @3 d4 F5 j$ _- This frame has 1 object(s):6 f' @9 Y! ?) A# Y4 p
- [32, 36) 'x' <== Memory access at offset 32 is inside this variable
! O+ T. c) `5 d3 q6 K& D3 G- x - HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
9 H- q& w3 h/ O+ p9 @ - (longjmp and C++ exceptions *are* supported)+ N- h8 g, b5 d! d
- SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main
: a( w$ L. a% l) v {9 v' E - Shadow bytes around the buggy address:
- s% K. N, W' P5 r - 0x100079c1d440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
- e! x: {8 F& B L- T - 0x100079c1d450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 006 u6 s5 x' w2 W" V8 J! D- i
- 0x100079c1d460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00! U0 G' ?* M& \' v' A0 K
- 0x100079c1d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" d( ?9 L/ x9 S" ^
- 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 003 N9 Z& w1 m6 f1 z) k! q
- =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 00
! X5 g( T$ l5 d& x) S# [- t - 0x100079c1d4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
: I1 M0 [* q+ ] - 0x100079c1d4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
% I2 m0 R) Z8 J/ y) L - 0x100079c1d4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00# u1 f6 U; v+ c* }9 f: B( @# D5 M0 Y
- 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ _) |) Q) q8 o6 |8 F
- 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 K6 R9 Z! A- v0 z) S
- Shadow byte legend (one shadow byte represents 8 application bytes):
* @- f o4 N. Q* R - Addressable: 00( V2 K: o9 p( u N1 b5 e3 E; `8 {
- Partially addressable: 01 02 03 04 05 06 07% X# p. S( K% ?$ G
- Heap left redzone: fa: u; L h9 Y4 X6 d) e1 y4 q
- Freed heap region: fd
1 W2 L2 Q. `) `( }& ~8 e+ N - Stack left redzone: f1
8 q" p x6 Y$ I - Stack mid redzone: f2
, r1 M+ {. l- H7 Q0 `# O {" S4 | - Stack right redzone: f3' [& ]# S# U1 q r: Y
- Stack after return: f57 ~ B2 X3 B5 i u
- Stack use after scope: f8
: M+ \& o y- I+ W4 d9 L" G - Global redzone: f9
8 W. r4 ~" W, T/ _ - Global init order: f6* T3 `; B( {$ T8 E2 Y' g P% @
- Poisoned by user: f78 }4 ?% p9 H. w6 Z1 E: U
- Container overflow: fc
9 D; z9 @0 r1 M0 z& f& X - Array cookie: ac9 M7 y; i4 A0 i& F) b3 C6 \
- Intra object redzone: bb s( F1 L, M7 d# y/ O
- ASan internal: fe
& u; T1 @0 Y4 H, A6 Q - Left alloca redzone: ca
9 P' k7 n+ g7 P1 _ - Right alloca redzone: cb0 ?- p' r; M7 C; f' q
- ==243==ABORTING
复制代码6 i) C1 I z Z, J5 _, K
free后被使用 示例:
& ]! {% W( n( l- A2 L8 C7 }- #include <iostream>0 G. F9 |, D) a
9 {8 m8 l. v8 a( _8 b v+ p- int main() {
5 T# {5 s% p7 F8 Q4 M6 p- Q6 l - int *array = new int[100];
- F7 V- f9 M! ^4 f+ u - delete[] array;
7 Y; k, @2 N* m+ d8 @ - int a = array[0]; // error; O6 h8 t' B' Z+ Z/ v0 I1 t6 R& K
- return 0;
, B( u4 L" `7 u0 R5 k+ v - }
复制代码
X6 ]/ ?) X( I+ n' \# r" ^4 ?0 S
编译and输出:1 j! ^7 Z, t' A
w. w* ? {; T- E/ t- g++ -fsanitize=address -g test_leak.cc && ./a.out
3 k* |( M2 E- R) C, e0 ?% i4 k- Z - =================================================================, D* n# `0 c3 m- |; z3 D
- ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a15010
$ b8 X5 p6 @ ]9 M, T - READ of size 4 at 0x614000000040 thread T0
' X, }# Q1 c& ?: S% a - #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:66 M2 [2 m: k1 w
- #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
, R' a/ d5 a$ ?/ i/ u, D% u( a - #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69). }; E# \. ^$ f# P8 o4 G+ u% W
& p7 s, A$ d# }" U$ J0 h- 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)
. E5 c: S! i1 E) w {8 N; @ - freed by thread T0 here:! k/ T" }. a$ L/ s( O: @+ {7 E
- #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)
: Q% L: I. D c/ }6 l8 Q - #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:5
2 s! A- _& `/ f0 h: m8 Q% s* G - #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
# ?8 j' M1 K8 T0 V' e - , @& k @3 ~3 y/ o$ [
- previously allocated by thread T0 here:( r- ]: u! I" [! K% C- q
- #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)6 A' J" m5 j/ y3 I: n: n
- #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:40 [2 j$ V- h( y% Y+ c
- #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
8 k) R5 O9 p& r$ X! v$ E
7 \4 Y* w) c# G/ ^& i- SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main
7 ]# o4 m5 K+ K" z; R7 j( z9 a' L - Shadow bytes around the buggy address:
~0 ]: O% {$ \& G - 0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c& ]9 d8 m1 N' v |6 H( n. R
- 0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 006 M. ?# ]/ T. ~6 i
- 0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
9 V+ G9 l2 q5 ~& d7 l1 g - 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0 H7 K- N2 \4 x! D, I - 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00% M9 `0 q- d. M2 s+ T% B7 I8 @+ t
- =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd9 H4 k2 H' U* D+ H6 E- ]
- 0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd6 \5 U9 B6 l/ L! n* v, @3 I0 {
- 0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd/ f. G4 k2 f, i+ m- L; f9 a% V
- 0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
+ c/ p! D9 h% @1 ~! I' ]: x - 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa8 N5 v/ p$ ?% l1 i' ]
- 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
4 u) \# l2 T3 K4 C4 s - Shadow byte legend (one shadow byte represents 8 application bytes):
+ P( h1 M' }) s) I$ x5 r# k - Addressable: 00
. C! @4 R) H% _7 g$ P# ? - Partially addressable: 01 02 03 04 05 06 07; J5 K$ U0 o5 W+ h( r
- Heap left redzone: fa
7 u, N! @8 j- B - Freed heap region: fd
# |! K4 Z/ o! Y; W$ X - Stack left redzone: f1/ B( z* S8 O7 w! J. Z/ C
- Stack mid redzone: f2+ U D% G# j# j
- Stack right redzone: f3
1 v. V8 U6 h( l1 B4 E! D; d% l - Stack after return: f55 u% J+ T0 B2 I: D
- Stack use after scope: f8/ ^! R: F9 a, U+ o, d
- Global redzone: f9
% Y/ Z7 w3 t7 r0 p - Global init order: f6 Y e9 J9 u, Q- B1 X. ?- Z) l
- Poisoned by user: f72 Q4 ?( ^# D2 ^( E9 `
- Container overflow: fc: }3 \0 U# z/ K$ P* c" h! t5 @
- Array cookie: ac4 o, ~; ?/ F# K3 M3 z! B: d
- Intra object redzone: bb
5 s8 h( E$ v; p9 g - ASan internal: fe
. g- e6 I7 `3 R( H* \ - Left alloca redzone: ca
9 b9 d1 i# v$ f: Z2 m - Right alloca redzone: cb
, |, P* ^ C D3 ~3 d# `( j - ==282==ABORTING
复制代码" k2 {) s! B8 _8 O# ]5 B. v
Initialization order bugs 示例,这里有两个文件: - 9 P8 M! @ D6 k
- // test_memory1.cc0 [3 y9 w1 V8 {2 Z8 C( @
- #include <stdio.h>& U* H, ^; }1 O: e
0 W# k. ?3 v: b+ \3 n8 ?- extern int extern_global;2 f& J- m( T2 {: Z( Y+ Z
- int read_extern_global() { return extern_global; }
) [' Q1 |: o3 P. p" |9 ~ - 0 m. T. i! _6 r% t$ W( w
- int x = read_extern_global() + 1;. o! N# I# g$ _- O1 A
6 } u) \9 w; S/ q+ H1 s4 O8 E- int main() {
& O/ t5 t: D8 [* b9 ` - printf("%d\n", x);
0 [3 Y( C2 P4 O* a9 L2 o8 w - return 0;) W. ~& k3 r0 L
- }
复制代码
5 Z3 j% I& e$ R/ z! d' x- // test_memory2.cc: _, y2 w- g# a' }* U# ?
- + o, k9 S2 j x: C" }
- int foo() { return 123; }
& R! {/ o# C; q7 L7 {1 W$ M$ W- H - int extern_global = foo();
复制代码
4 N; }% N# U4 N3 ?
第一种编译方式输出如下:+ a9 e9 P+ @; c2 c2 V
- 5 \. J& U: ?1 T* K- ^+ E; h% _: M
- g++ test_memory1.cc test_memory2.cc && ./a.out
& v2 c6 l, _! P$ L5 j( X - 1
复制代码. \9 c2 I1 n- Q# p4 N
第二种编译方式输出如下:
5 Q" L$ b) O0 i5 o( W$ M& Z- g++ test_memory2.cc test_memory1.cc && ./a.out* E- m k# Z; a
- 124
复制代码
5 K2 _7 i' v) X- Q1 l
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:
- ?' O+ u/ g" q0 E7 U! z 编译and输出: - . e3 s9 W# o) H% k
- g++ -fsanitize=address -g test_memory1.cc test_memory2.cc Y5 S2 n. S3 F' b
- ) u( l+ o! w) X* r% F
- ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out
; u1 z: H+ }& N! d) ] - =================================================================
6 I6 @6 K0 v. [4 C9 G9 O - ==419==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f46c20021a0 at pc 0x7f46c1e00c28 bp 0x7fffe423d920 sp 0x7fffe423d9109 @/ [$ L" x: H: O' e6 }
- READ of size 4 at 0x7f46c20021a0 thread T0
( F3 _# |* Y- z* r1 h - #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3
8 ]1 J5 H) J0 U) S+ I. a - #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:4: V) s! @0 ~: C, L6 j
- #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8! V1 J0 I5 o* b( z
- #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
2 M \! C; ~, M7 G" G7 n - #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)
) P U2 O3 C: z% v- U: @- I, L - #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09)
6 i. f2 u5 ?5 X3 Q( d% L
0 M7 ~4 b9 ]5 F" _4 O3 O- 0x7f46c20021a0 is located 0 bytes inside of global variable 'extern_global' defined in 'test_memory2.cc:2:5' (0x7f46c20021a0) of size 4
$ P* X) j4 M$ K$ E) i; q, R - registered at:- m* O7 Z8 C& B- g. H
- #0 0x7f46c08764a8 (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8)8 A% t/ `4 y0 e7 D X, N% |
- #1 0x7f46c1e00e0b in _GLOBAL__sub_I_00099_1__Z3foov (/mnt/d/wzq/wzq/util/test/a.out+0xe0b): V; [! }: N& ?$ N) |, G- } q1 Y" E
- #2 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
2 J" x$ M9 L) [0 S8 T! k - / i: o$ `% [& O. {9 R
- SUMMARY: AddressSanitizer: initialization-order-fiasco /home/wangzhiqiang/test/test_memory1.cc:3 in read_extern_global()4 j; V6 x0 G4 z' z# d; O/ r
- Shadow bytes around the buggy address:
# d6 z! H- f* p - 0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. m6 z: z4 f/ L- H" c6 d; _ - 0x0fe9583f83f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000 @: j: D3 K: G
- 0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
) A: o5 f3 y. |$ u - 0x0fe9583f8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 008 F. l4 N, [5 s$ ?$ C7 J- i
- 0x0fe9583f8420: 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9 f9
" ?* C2 b$ Y: M! d- l# j) m7 S0 o: I - =>0x0fe9583f8430: 00 00 00 00[f6]f6 f6 f6 f6 f6 f6 f6 00 00 00 003 q' q4 t/ n" h' {' Y$ }
- 0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00$ i, P# y1 e5 j2 n" c- ?
- 0x0fe9583f8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
( T) W& T' n1 t8 A1 x' N. p - 0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# r4 d( H( W" F E - 0x0fe9583f8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
W4 G' t0 T2 M$ @ - 0x0fe9583f8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
& t! @& Z$ L s. y6 l1 T0 H7 {4 Z - Shadow byte legend (one shadow byte represents 8 application bytes):
/ V( j5 i3 ]5 D" Z- F% R - Addressable: 00
2 W3 ~7 {/ w2 |, I% v4 R# p - Partially addressable: 01 02 03 04 05 06 07% Y0 R3 h6 y1 C
- Heap left redzone: fa# {" Z1 T8 B: _0 \. B7 Z
- Freed heap region: fd
. a1 h: x; u. `9 S2 c1 `1 M9 [ - Stack left redzone: f1
7 a" X- H) _9 C4 ^5 [0 A - Stack mid redzone: f2
+ ~) n) w" F! O - Stack right redzone: f3& D8 ?: U4 |0 G( N5 a
- Stack after return: f5
4 u! ^6 A" b6 L# q% S - Stack use after scope: f8& t7 Z0 A) A% H0 X
- Global redzone: f9
, B0 Z7 z7 v# t; x. @; {, } - Global init order: f6
G6 s; p; g1 E7 \7 x - Poisoned by user: f7
( C" b3 ]. J+ p5 m" j8 W - Container overflow: fc
, B. B: S5 o+ U! r7 ?0 U! Z3 i# s - Array cookie: ac4 T% R# |! ]2 J
- Intra object redzone: bb5 W% H' B& `7 w/ g; Q* d2 p
- ASan internal: fe
% ?; L0 A& B: l5 r - Left alloca redzone: ca! h/ i5 @4 X1 _8 L
- Right alloca redzone: cb
1 ~1 e" E4 r! E: v3 [7 Z$ x - ==419==ABORTING
复制代码
* g/ W, r: ^. }9 V! p
注意:这里在运行程序前需要添加环境变量:
# `- Z. u5 f/ ^6 m - - V5 h/ p1 C" a+ a
- ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码
5 Z' s# A: {5 Z% M" T* o; V: f5 q/ [
小总结 ASan是个很好的检测内存问题的工具,不需要配置环境,使用还方便,编译时只需要-fsanitize=address -g就可以,运行程序时候可以选择添加对应的ASAN_OPTIONS环境变量就可以检测出很多内存问题。它的错误信息也很有用,明确指出当前是什么类型的内存错误,如: " q$ ?8 _! M: ~ n8 \6 J
|