内存泄漏是指由于疏忽或错误造成程序未能释放已经不再使用的内存。内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制,从而造成了内存的浪费。 我们平时开发过程中不可避免的会遇到内存泄漏问题,你是如何排查的呢?估计你是使用下面这几个工具吧? valgrind mtrace dmalloc ccmalloc memwatch debug_new
# B7 Y, B# ?1 t- L) J9 [% ]
这里程序喵向大家推荐新的一个排查内存泄漏的工具:AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本都可以使用,支持Linux、OS、Android等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有: 使用方法直接看我下面的代码: 检测内存泄漏 内存泄漏代码:
% H3 h" z @8 V' U+ a4 `- #include <stdlib.h>
6 X" K$ I: B- m* M% i
! f0 {! K! B6 c4 E9 L3 W- void func1() { malloc(7); }
; T0 {' p' m/ r& p$ Q5 l" ~ - 9 d) S4 X2 F: |; z2 T) J' K2 d6 g% F. W
- void func2() { malloc(5); }
* l- B3 P6 k, ^% E# v7 D7 j# t - , T$ y2 z1 _/ M d, T* q
- int main() {7 M$ v: g O% H2 i* k- V
- func1();1 f. O/ [% E# r( Q& Q
- func2();
( d. J7 C; x8 R! ^' B: _& U - return 0;) V c4 d& a8 Y% ?' S }$ b$ ?6 n9 B
- }
复制代码 ! ^# x3 L$ n- `) ?5 C5 S' c! y) n$ Q
1 l9 J* ]; |7 u- J) p' g: o
编译and输出:
; R7 g6 X7 f. c a9 {5 {- g++ -fsanitize=address -g test_leak.cc && ./a.out
; s6 F4 ^; a$ \% G6 u: _% A
- j0 Z) x5 g+ i0 u. P- =================================================================
1 d% H1 P- M( ~+ Y1 Y# | - ==103==ERROR: LeakSanitizer: detected memory leaks
" G6 H( @6 w6 i: S$ Y. @ - + |! S. I/ _* `$ a' @# F
- Direct leak of 7 byte(s) in 1 object(s) allocated from:4 q; D0 a. m+ Q7 z
- #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)% C( y+ s$ ?% [" C' U1 ]
- #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:3. c, I6 g: V' {0 K7 t0 K
- #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8
* n, m; t$ `: }# ^ - #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)8 D1 s+ a( g5 {1 E: C4 c8 F: E
- 2 z: R! E7 P: c+ q$ L
- Direct leak of 5 byte(s) in 1 object(s) allocated from:
0 B/ R* S, B7 b U* c - #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)# i2 C$ e" s& K
- #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:5
3 C) f( S$ d1 v7 } - #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:9$ C, i4 n$ m6 Q$ m
- #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
/ B* {$ E, `3 M% K- ~% Y- n: P4 K - % L5 ~' u9 L; m' a
- SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码 * F3 V- e0 k' b
) M0 f8 J8 j8 A3 D* l$ U6 r& k) \6 G
编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。 检测堆栈内存越界访问 示例:
x# c. S8 a, `8 t- #include <iostream>
) {; N1 U! w, Z3 \, M9 n
1 Y3 N! f6 E! ^) x5 w& L7 L0 u4 ?+ d- int main() {; \9 p; h" y- `2 }& V; c
- int *array = new int[100];
$ x5 i4 U8 f! f1 S2 S! V2 N - array[0] = 0; }: h. m m- J. Y
- int res = array[100]; // out of bounds. d+ e, ]% f8 o$ {% S; q- {- I
- delete[] array;
: o4 ^5 v i+ e. u4 z! F* N6 @+ ` - return res;7 T+ ?6 I" n7 r- D5 N
- }
复制代码
" [5 b6 N; A6 I" `0 E编译and输出:
% }* s& [. E5 W7 |- g++ -fsanitize=address -g test_leak.cc && ./a.out
# i0 B" D4 G% R$ w -
$ w2 g/ R6 S' \; B i - =================================================================/ z8 q4 A1 [6 T- S( z; K' u
- ==110==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x7f0e06400d2e bp 0x7ffff5963f10 sp 0x7ffff5963f00 d! U$ u1 B& ^) a% m6 x( P& T- q
- READ of size 4 at 0x6140000001d0 thread T0
% o0 G9 A$ _3 T W6 l4 D- @ - #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:68 K: s4 M% D* h5 f( L! C' ]
- #1 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
/ R5 _: d+ o& b: G% x0 P - #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)! r/ k9 \ G3 D! h, e
( ~4 G8 X0 w! i; J0 ^9 Y- 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0)2 }) j- _+ ^+ Z$ v$ y$ {
- allocated by thread T0 here:
) f0 z, P; |1 Z" h! K' u2 |; e0 z - #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)
# g3 ?/ |5 E U' ~ - #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:4, Q: P8 ~" ?5 |# C7 R$ \
- #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
0 V! k' E% e* Z( N$ X( i- ? - . E: o! d; d$ p
- SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main: B$ ]0 F" w0 a% C9 b
- Shadow bytes around the buggy address:
5 S4 \$ t: P- C/ P8 M4 x - 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. Y8 x" `3 x+ Y! g( f# s4 o2 d - 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4 ~* O) V& E1 q( [- J - 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00, w( [( \* D' J
- 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. m5 `3 R: E1 D6 z9 o - 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00( J5 e& n/ X1 |. C: O
- =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa/ n) ^# r& O: x
- 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa ^- v1 N- N) C8 t$ j6 I
- 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
4 F, @5 M6 V8 E% n. h p1 B# N - 0x0c287fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa5 V. L, O2 r/ g
- 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa- x3 u0 \& a/ l/ G; V
- 0x0c287fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa: X( a: c, {5 `! k' i- E
- Shadow byte legend (one shadow byte represents 8 application bytes):9 @& q6 D' j$ |) E
- Addressable: 00
' b( H2 D9 W) T& r& ]/ M - Partially addressable: 01 02 03 04 05 06 07
; L& Q) Y4 S4 j8 {5 x - Heap left redzone: fa4 a+ a( o+ d7 _5 O
- Freed heap region: fd& ]; z* K( ~$ t( ~- U9 s
- Stack left redzone: f1
) x d4 W) e+ @) R - Stack mid redzone: f2
: U/ z* D- j8 P+ Q) e - Stack right redzone: f3# J5 F0 R2 L( [
- Stack after return: f5
) n% u( F6 q5 P# ~+ d4 ~ - Stack use after scope: f8: L" M* H1 ~9 Y x+ ~" ?
- Global redzone: f99 L2 |5 t" `- V( j: z8 \
- Global init order: f64 R3 |: E5 ` g \5 E1 Y4 Q/ F( y
- Poisoned by user: f76 y; B+ L5 S5 K0 @
- Container overflow: fc( |: q& v( c0 Z T! x9 t% v
- Array cookie: ac
' t0 Y6 n' g5 h- _* b - Intra object redzone: bb: v+ x; `1 ~# C# F: a n0 O2 ]) _
- ASan internal: fe
3 _! C2 }' z% N0 M; ` - Left alloca redzone: ca6 o, o. l6 c- ^+ D9 R4 ?( B2 l
- Right alloca redzone: cb) Y) f. `2 g x. ~: L. A
- ==110==ABORTING
复制代码
) {6 \9 m6 L# P: n& i( [可以方便定位到堆栈内存越界访问的错误。 全局内存越界访问: 示例: - " X; D" e" k9 _4 u
- #include <iostream>
' k; ?0 N3 F' u% h2 F# j3 w, d# l - $ }+ t! _. }* W; x# K, U) z
- int global_array[100] = {0};5 C- v& N: _" S; f
% B- z7 e, @+ q _# o0 M3 f- int main() {- i% C+ ?5 o" A+ e6 h) p
- int res = global_array[100]; // out of bounds7 c% ~: E: }! s' ?: w1 \9 x ^% S0 _
- return 0;
2 t7 ^5 P# b9 o, K5 X' m: L& o0 N& z+ ^ - }
复制代码 + A) C+ h- W% M% p
编译and输出:
5 k" y3 D* _& i, y- g++ -fsanitize=address -g test_leak.cc && ./a.out6 o2 j0 m3 W) S' w' [
- =================================================================
( W( I. q2 h- `: D1 x - ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda52770
( |) C" X0 d7 ^9 b - READ of size 4 at 0x7f42e6e02310 thread T0
$ z" K: t. e" E: g$ J( I, _6 h - #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:6
: {9 R" _6 N- R3 z( [ - #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
. ^2 `4 e' R6 ]. z/ [0 ^# q - #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
, L5 o5 G. ^2 D
# ~3 y' o& E" L8 o& h. K5 k) _% k- 0x7f42e6e02310 is located 0 bytes to the right of global variable 'global_array' defined in 'test_leak.cc:3:5' (0x7f42e6e02180) of size 400
7 G7 q1 E$ a; V% j9 K - SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main3 h! k& a# c% u" R% J
- Shadow bytes around the buggy address:
$ y! p: Y# L% r) b$ u - 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1 a+ i! l4 G, M+ y0 k2 T - 0x0fe8dcdb8420: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f94 m, w) @- [* R/ {! ?
- 0x0fe8dcdb8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
/ r$ \, f& |/ @" E; K - 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
* `+ |7 T( d) ~0 P" T - 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. `/ s( J! @, V3 B - =>0x0fe8dcdb8460: 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
: i7 ] N7 `; G, ?7 H - 0x0fe8dcdb8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00: A! }8 h' }; }, U3 K5 I& E
- 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 003 C! m8 k& }! {
- 0x0fe8dcdb8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
& o$ p% {# `% l# G+ L9 V - 0x0fe8dcdb84a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00/ Y) B: A8 r% i# b# j9 p
- 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000 n1 o8 U6 V& w1 p! P2 R5 ]+ ^0 q
- Shadow byte legend (one shadow byte represents 8 application bytes):* Y$ H( y* w( H0 p& l& a& x% F
- Addressable: 00
2 `& c0 A; V/ _# c - Partially addressable: 01 02 03 04 05 06 07
( V* F! f1 R. w8 D - Heap left redzone: fa
/ a5 V4 S& u" E - Freed heap region: fd( [& z2 d# I# U3 S
- Stack left redzone: f16 F5 N) W$ w$ V; ~# B
- Stack mid redzone: f2
+ T u0 W6 g9 K, @* c5 Q2 n - Stack right redzone: f32 _& v Y1 \8 ] j2 ^; C6 V1 G' B
- Stack after return: f5- q( F) f: r# ]* P+ @
- Stack use after scope: f84 Q4 B U$ A& B9 i
- Global redzone: f9
; M; \8 y' D0 F# ?9 m( q - Global init order: f6' m( _: H% H& D2 T4 Z
- Poisoned by user: f7
7 O1 b* k( f# u( V* q3 U2 b - Container overflow: fc/ O# e) _1 T- M) _2 M
- Array cookie: ac7 n, K2 G e3 g. u
- Intra object redzone: bb
5 M6 |- p5 B! n1 c6 V; [ - ASan internal: fe4 z$ L8 O. \8 [2 O" |9 q) C
- Left alloca redzone: ca8 n, {* M* f2 q" ?
- Right alloca redzone: cb
5 W1 N$ A. _$ j" D; G% n- B' }' U - ==116==ABORTING
复制代码
# q9 D. ?( g# v4 ]; k5 S局部内存被外层使用 示例:
5 m7 z" c' l0 ]" Y- W: h& z, r4 }- #include <iostream>9 u! Y; ~9 X, w3 s5 J) ~/ z' n5 F! X
- 4 i4 X7 X: ~1 y9 t) I$ n: b
- volatile int *p = 0;
/ q- p2 T5 K' r5 f
) I# W2 _$ o7 z% A$ T- int main() {2 R3 |$ b$ l# i: u5 J, d
- {. U( }& {+ h* Q! K2 E5 q& v3 g
- int x = 0;
: I5 f7 u" v. C! |; R, ?2 {( ? - p = &x;+ F) z( h9 C. e4 G
- }% T5 S2 X9 j/ `8 A e2 J
- *p = 5;
" q* Z4 b' t# S C2 G5 _8 o - return 0;9 O% ?, U: j* J
- }
复制代码 ) t* C- w8 k1 g
- V- G. s1 g' e+ |编译and输出: - $ x; g4 ~" ^9 Z7 E) ]( i% \5 ^2 u! T: U
- g++ -fsanitize=address -g test_leak.cc && ./a.out
7 ~. i. {! t, M \. y - =================================================================
$ r- n8 D0 `3 i7 v7 K6 l - ==243==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fffce12a4b0 at pc 0x7f3993e00e7e bp 0x7fffce12a480 sp 0x7fffce12a470
* o7 \! v! i- ?0 F/ D) ] - WRITE of size 4 at 0x7fffce12a4b0 thread T0- m1 A' }" C, d$ p3 ^, e5 x
- #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:10: r! p# f! u D9 w/ A" \
- #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
1 i! e: ^( t$ l, v - #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)/ w! z! m( k9 R0 k
- 7 @( D: P" I8 D6 \ X6 w3 Z
- Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame
V7 O& S$ g9 Z - #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5; u' L2 i4 e. ~* \9 T& h
- 5 X& M) e& ^! S
- This frame has 1 object(s):
6 {, A) D# O% T% H" M& e - [32, 36) 'x' <== Memory access at offset 32 is inside this variable
) w% S0 p ^& c - HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext% d6 E$ _" v. N
- (longjmp and C++ exceptions *are* supported)
: ^: j* w3 h2 u5 S4 i4 [; N4 E - SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main
: e; [& d4 o; t. A( o! [( V - Shadow bytes around the buggy address:4 X: Q0 ]9 _! v' d1 g
- 0x100079c1d440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
, x- z. m. d$ ~4 `$ I& p - 0x100079c1d450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
& b4 L3 u0 R: V. n - 0x100079c1d460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
% R" n+ `8 l% q& V - 0x100079c1d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4 _) R1 a3 F' `5 n6 l/ u - 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
6 k4 t4 ?* I: z; T7 b; G$ t - =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 00# v; B1 ?" T+ e2 k8 g; \' d( l
- 0x100079c1d4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ R& @3 o8 F6 r+ S- r( T w
- 0x100079c1d4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, a9 M! f3 ?$ `/ h$ u& _- A+ U# r
- 0x100079c1d4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00) ]( Z: M+ {9 d2 N8 |7 e; F
- 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" u# d# X) J9 D4 g4 x7 ^' \
- 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
6 x5 B+ ]; ~0 K! S- ]) u$ |& C6 p - Shadow byte legend (one shadow byte represents 8 application bytes):
) L( O- W0 v9 c- {2 s6 m - Addressable: 006 U. c; c2 D" t9 n( b
- Partially addressable: 01 02 03 04 05 06 07 p( l- S$ M3 I% }
- Heap left redzone: fa% h( S9 W$ T$ c6 l
- Freed heap region: fd# V5 y$ P3 H. Y7 Z- V
- Stack left redzone: f12 r" M# `0 ^2 ~" O3 \& m+ i
- Stack mid redzone: f2, N5 ]3 D7 ^) l4 S( O4 j* Z
- Stack right redzone: f3
! Z9 A8 n; ^2 p1 x& I2 _ - Stack after return: f5
0 H9 h9 W' F! W5 n- Y; K - Stack use after scope: f8
# }- B3 ^2 V9 c7 m5 q. P6 \" y - Global redzone: f9
/ x* E4 h# k! w7 ]6 o; x; a - Global init order: f6
; @0 V1 G3 O' s3 k" A' q5 o - Poisoned by user: f7
4 y. k6 T8 Z9 H( E j" [5 h - Container overflow: fc
' O5 h m6 @+ \ - Array cookie: ac
2 s$ {" j, e ~8 ` - Intra object redzone: bb
! `9 ~6 ~8 T5 N2 i9 X - ASan internal: fe, G% @: Z6 W$ e; i L" c% J8 h
- Left alloca redzone: ca
: i) T1 m: H. p+ i - Right alloca redzone: cb1 x2 M9 G/ Q3 V1 [0 s6 w) Z2 I
- ==243==ABORTING
复制代码
3 h. O( r$ d7 Y: }/ Hfree后被使用 示例:
/ v9 H& Q/ w8 M8 u3 j/ p- #include <iostream>7 [* }" A3 z$ |( V& X0 j- Q
! W; B. b4 K. r8 K7 F5 _, p- int main() {6 u) h5 L/ J+ ?% `
- int *array = new int[100];
8 f; M) T% r" g2 y( `' I$ [ - delete[] array;
5 j }) O, E4 a% V8 t) w$ Y - int a = array[0]; // error
1 k0 a, S& }+ C/ M - return 0;( l% y1 N% d3 d- Q
- }
复制代码
3 d2 h* l+ z0 l) Q# R+ h编译and输出:
: K: T8 E R% n( _" t( D- r- g++ -fsanitize=address -g test_leak.cc && ./a.out
: C4 @* V& O# c5 d0 |8 h) L! P - =================================================================. k& i) A9 _- Q7 M: y q
- ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a15010
! v! _9 o# j* M! Z9 F& ]( r - READ of size 4 at 0x614000000040 thread T06 m( }2 y2 c4 u, J
- #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:69 d; d8 \* h0 Y% N/ c. I
- #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)% E* }& x3 i9 m6 r% v) O" f! S! T
- #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)8 r* X, ~$ K* v# y2 O# @1 p
: G0 i4 i9 C; m8 _$ L7 Z* T- 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)
; g& b* ?8 P0 w0 ~& S) U6 }% _ - freed by thread T0 here:
8 c! Y W, I( ~0 b - #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)* o4 [( s% P+ i6 J/ D) ^
- #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:5
' |$ |/ L) i7 l - #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)4 W, o! S! l- q' F
- ! m5 E5 B2 s; V0 ]0 O
- previously allocated by thread T0 here:5 x3 m9 U8 @- R
- #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)* `* F" W: } D( H8 t. L- n. z: k
- #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:4/ n( r e, g# \& ?, }/ _
- #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
" ]8 l* R3 q- W m, O$ K _$ f- n
4 Z: _$ i% A; A! |5 a5 A* l- SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main
+ J" b8 O, B4 G - Shadow bytes around the buggy address:1 \2 j7 u! I) P$ d/ W7 d
- 0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 Z! V. L3 f0 h7 y4 @; H# B8 c
- 0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 x$ K6 l! k. a8 h# F
- 0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 009 ~* ]: m9 G* i) x
- 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, J' d9 w z. e8 S
- 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; q5 }% K7 ]0 [3 m$ x
- =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd
3 p! b) R" S& m - 0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd2 j$ c' [% o3 S1 B5 b; K
- 0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
' K! j- a( k" o F* }3 t* c - 0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa% |6 i" J! i9 K1 `; Y0 }$ z
- 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
- J9 A5 g! K* ?6 h( B' ] - 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
2 W) _+ S, X( j; ~+ C: b+ @* e - Shadow byte legend (one shadow byte represents 8 application bytes):
; e7 C4 k/ U" P W8 E - Addressable: 00
/ F) r( E3 Q. D* i - Partially addressable: 01 02 03 04 05 06 07% ]$ ?# E, U" n$ F" @
- Heap left redzone: fa, Y/ V- l% `* F$ _. Y1 p
- Freed heap region: fd
1 w# ]9 M- }9 ?3 X7 l4 R - Stack left redzone: f1
; x8 ]! Z# B- k( l - Stack mid redzone: f2& |/ L& C$ H8 o5 e9 \
- Stack right redzone: f3
7 p G' W" w8 d. ~0 [2 H - Stack after return: f59 F& R* o4 M, t/ z
- Stack use after scope: f83 n' M' ^' |$ H7 m
- Global redzone: f9
+ z- G- S8 O9 r9 ^7 R8 `4 @. A - Global init order: f65 n! e& l7 N) j5 B% q
- Poisoned by user: f7
1 A: o+ t) o0 a6 i; }. X - Container overflow: fc
! q8 u# w- \5 L4 d0 G- w- s4 N' L - Array cookie: ac
. O7 a9 v# |. Z/ U$ @: d ? - Intra object redzone: bb% v/ G' F$ ]! i
- ASan internal: fe
. p# D$ {* k( T, D. g - Left alloca redzone: ca
4 M! T/ ~* n1 f0 h: x - Right alloca redzone: cb' R( \; a5 D" Y: K d
- ==282==ABORTING
复制代码 " W# x1 Z8 q! L$ K
Initialization order bugs 示例,这里有两个文件:
6 w. w5 R0 y. x- // test_memory1.cc: }% E0 \7 Y7 E7 T3 E
- #include <stdio.h>
. _" R( r) t! v9 d
* ] H9 E* P2 H0 Y# _7 O }- extern int extern_global;- ~; M# }% [' _ X3 f2 F6 n) L
- int read_extern_global() { return extern_global; }1 E7 h2 {; a6 z6 B
- ( |& J" c: w! m5 w
- int x = read_extern_global() + 1;
6 e' p: B% _, [# J
( M/ x' b8 F+ i- int main() {
8 x4 m2 M) @/ ] - printf("%d\n", x);, U+ V- g3 u6 `( L# k, h
- return 0;
$ ?: ]' P5 c) `6 n/ j - }
复制代码- <font color="#001000"><font style="background-color:rgb(255, 255, 255)"><font face="Tahoma">
2 u9 X/ o( t9 e( x0 \! a - // test_memory2.cc7 {$ B2 q! B- N
- 3 b F) d0 V+ |& ~0 D; j# B
- int foo() { return 123; }* W# T4 r1 d2 S( u) `2 n6 v$ v
- </font></font></font>
复制代码第一种编译方式输出如下:
& H% H- ]( c$ M8 K2 ~4 t& N- g++ test_memory1.cc test_memory2.cc && ./a.out* r! b7 E0 Q( D% ^3 P; l" B3 h1 Q
- 1
复制代码
: T. h9 i' e( m" t# ~. W第二种编译方式输出如下: - N" X w. C9 |2 i$ B" V- l N
- g++ test_memory2.cc test_memory1.cc && ./a.out
t' p( r. G' x. R! s% c - 124
复制代码 5 ` W1 {& |1 S3 t
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:
+ d% H! R* w/ j# d" M% v8 K! U 编译and输出: - ! J. i) l* K9 K& @4 Y& |5 D
- g++ -fsanitize=address -g test_memory1.cc test_memory2.cc
8 ^3 I3 n- B/ @1 u1 V. u
* e" k! J Z' C6 `& s% q- ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out5 }/ M0 c/ k1 ]' P$ B
- =================================================================
; _6 d# z# a4 }7 m5 r! Q8 W; L - ==419==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f46c20021a0 at pc 0x7f46c1e00c28 bp 0x7fffe423d920 sp 0x7fffe423d910: \5 t& X- [+ S
- READ of size 4 at 0x7f46c20021a0 thread T0
. W# E1 I' H6 q1 b8 N) W5 g- ?5 } - #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3' C9 H5 F, R. a
- #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:4
8 q6 K9 t* ?$ @2 h2 \: Q. k0 K - #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8+ O c; l7 O. V$ b. B4 ]- ]' p1 a
- #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
# g1 d) F& W# {8 u x" L: D3 D - #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)
6 T% [$ J6 W& C# i( S - #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09)0 M: i2 a5 D8 X6 i" U: P, ?6 _# u, ?- J
- 3 x% _% B1 _! r# B
- 0x7f46c20021a0 is located 0 bytes inside of global variable 'extern_global' defined in 'test_memory2.cc:2:5' (0x7f46c20021a0) of size 4. i b0 I' x3 r" e) c% m
- registered at:: B2 p" c" J' O, A7 P5 D, Y5 P
- #0 0x7f46c08764a8 (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8). D% w; S( Z Q$ J) g2 ?& N3 T
- #1 0x7f46c1e00e0b in _GLOBAL__sub_I_00099_1__Z3foov (/mnt/d/wzq/wzq/util/test/a.out+0xe0b)4 I. G" m3 g5 d9 C) q- j1 Q! y
- #2 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
, |* i5 W, u% ]6 B0 Z! \
( ^ p0 q7 L" M9 _- SUMMARY: AddressSanitizer: initialization-order-fiasco /home/wangzhiqiang/test/test_memory1.cc:3 in read_extern_global()8 V4 f/ k5 B' F
- Shadow bytes around the buggy address:
) j( V' `/ m, S$ w - 0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 O: N7 X/ e8 G2 t. O" c4 v
- 0x0fe9583f83f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ d/ H7 p5 C6 r: X( I. `
- 0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 008 k; `8 P$ h# B: D
- 0x0fe9583f8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00( h/ f* d4 R% Y' K! `
- 0x0fe9583f8420: 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9 f9
- ^1 ?& H7 C1 o, u - =>0x0fe9583f8430: 00 00 00 00[f6]f6 f6 f6 f6 f6 f6 f6 00 00 00 00
+ U9 w/ ^9 l b - 0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" p( Q5 A! r7 v. F9 G
- 0x0fe9583f8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2 S" b* `- U6 Q; {2 f - 0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
! ]3 Y7 p. o D/ K - 0x0fe9583f8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
* q) Q9 V# W0 @4 T7 Z7 z# Q% l- r3 o - 0x0fe9583f8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00: h1 v( Q& x, I! z8 d: `
- Shadow byte legend (one shadow byte represents 8 application bytes):" X- f" n8 N5 b% e! H, ]4 p
- Addressable: 00
+ U! D4 c" h- [. D" x P- T$ ~! Z - Partially addressable: 01 02 03 04 05 06 07; W ?- i/ Y% x: i) ?+ b; c; e
- Heap left redzone: fa
% k: Q6 G, Z# Z - Freed heap region: fd. ?! ?) `6 c; E/ p9 a6 `
- Stack left redzone: f1
# N8 ~/ b% H2 s+ h( ]' H - Stack mid redzone: f2
4 U" f2 x5 F( g5 S: I7 @9 t, I - Stack right redzone: f32 R: w/ f" h1 j; v7 d2 `0 F2 f
- Stack after return: f5, R. T9 m. y# w: o! l
- Stack use after scope: f85 z/ S% N1 d4 w5 y% K: l
- Global redzone: f9$ d. V" u5 y( C6 S/ B3 E. [
- Global init order: f6
( Q' @& Z% B4 a; e - Poisoned by user: f74 h( m: R7 x. p5 U: |' e
- Container overflow: fc
+ D$ j# `) K" z. w( Q8 E! u - Array cookie: ac
+ v! `; q! x/ J5 O5 J - Intra object redzone: bb8 {; b* g- F$ u. G
- ASan internal: fe
, D4 G2 {8 a9 D3 h* c9 e" l% {# T6 x - Left alloca redzone: ca+ e) c7 ?5 ]& v/ u
- Right alloca redzone: cb3 v# X- ?) G/ \. j, _
- ==419==ABORTING
复制代码 7 k9 W# ]" ]# t: n) y/ `
注意:这里在运行程序前需要添加环境变量:
, T8 w: R! k' ^! G- ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码 * M5 H k/ p! h0 o1 Q9 w& i
小总结 ASan是个很好的检测内存问题的工具,不需要配置环境,使用还方便,编译时只需要-fsanitize=address -g就可以,运行程序时候可以选择添加对应的ASAN_OPTIONS环境变量就可以检测出很多内存问题。它的错误信息也很有用,明确指出当前是什么类型的内存错误,如: |