内存泄漏是指由于疏忽或错误造成程序未能释放已经不再使用的内存。内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制,从而造成了内存的浪费。1 ]& A- Y$ s% {3 V, w" j
我们平时开发过程中不可避免的会遇到内存泄漏问题,你是如何排查的呢?估计你是使用下面这几个工具吧? valgrind mtrace dmalloc ccmalloc memwatch debug_new
; X! N. ]$ M0 A" e0 n0 l& y
这里程序喵向大家推荐新的一个排查内存泄漏的工具:AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本都可以使用,支持Linux、OS、Android等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有: 使用方法直接看我下面的代码: 检测内存泄漏 内存泄漏代码: - * [: V% n# _3 ]& T* _
- #include <stdlib.h>0 Z1 X$ f. _1 V8 R9 n0 X4 s
1 v8 ?' e- P# `! E5 k. ~$ u- void func1() { malloc(7); }6 C+ @" w7 T3 E
- { ?# n' J' y; s$ q! u
- void func2() { malloc(5); }
+ S8 D+ q' ]; l4 F4 W- D
4 X6 F( B) \: [' f" W/ Y- int main() {0 E6 s8 r7 F' t$ `, P9 ]2 M4 N
- func1();1 i* f e) Z+ I3 l2 y! y
- func2();
; F& [; `! C3 t - return 0;
0 B$ v8 E' n. e9 J. }4 G - }
复制代码
) r+ q% ^. q+ k! r [- K! i" `% \
编译and输出: - 1 c' z6 E0 {( ?2 B$ G/ y
- g++ -fsanitize=address -g test_leak.cc && ./a.out! ~1 M4 G. E- ^" ^, @& u0 ^* K
8 E. S# b8 w& Y- =================================================================+ R% h# x$ _5 T2 E4 m2 V# p, Z
- ==103==ERROR: LeakSanitizer: detected memory leaks
9 r! Q' b2 `* V$ _" k: F! k
+ a1 U H% P! b' ^1 A- Direct leak of 7 byte(s) in 1 object(s) allocated from:2 w% {, D g1 j4 a1 v
- #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40); b2 O3 R* w7 \1 c
- #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:3
) x1 V h. [( s. s - #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8
; L7 p# f: S8 c6 j2 ~ P; ^& i - #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
1 Y; k. u. Q; U0 ` - ( b/ V, a! F( `
- Direct leak of 5 byte(s) in 1 object(s) allocated from:+ m2 S! C- X5 Y, O
- #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
( \7 j+ A% u7 z1 J: v# z; s - #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:5
1 y# A+ E, @0 e4 \( O4 r - #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:9
% ~' @8 {3 p) _" l" D) C - #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
$ i) P* Q0 O8 I2 K# g4 B$ V - 1 i. Z) n/ V. x) X6 G
- SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码
) y& j& \) p$ V9 M) E- Y4 I
编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。 检测堆栈内存越界访问 示例: - + n+ M$ l7 C3 P9 V
- #include <iostream>
4 p$ g0 }* b4 a: l% m - $ @8 A. i/ ^3 c+ Z* w/ x) k
- int main() {
5 o" p! W. X) j+ C2 M3 g - int *array = new int[100];
9 w! E( ^1 L d8 I" g - array[0] = 0;
* Y3 a3 W$ k; L4 s" B - int res = array[100]; // out of bounds
3 h% m! d4 z- _% ^! H' ~: Z1 J8 l% p - delete[] array;" }. T$ R* N9 ]1 U/ K# ?
- return res;
" R, w1 _3 D& [4 R, Y! g) Z - }
复制代码6 M7 n h0 ^& i) X u: a
编译and输出:
' G0 S& G! r3 U. @% [& p$ j - 0 M' s) X6 t, @9 w
- g++ -fsanitize=address -g test_leak.cc && ./a.out; M" u) u1 q, ?2 {+ M" m1 F U% |
-
4 F2 T* g1 c9 ~ - =================================================================6 w9 B+ d' X2 H# ]
- ==110==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x7f0e06400d2e bp 0x7ffff5963f10 sp 0x7ffff5963f006 T2 J+ I9 Z* l8 q& x' {: A
- READ of size 4 at 0x6140000001d0 thread T0
- b' m3 @3 o: i. R; u - #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:6
: _7 ^) r0 F, l$ P& B M7 L" q - #1 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
- ?, h; `6 M/ j- Z. u, t - #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)
& B" t* K" q- S" Z - / W7 n4 |, }% F; j. \
- 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0)
& ^ n- w/ K* x/ \: G: W Z1 S1 V - allocated by thread T0 here:
: t6 J2 y: V! A, }0 w - #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)
- b! i3 q: g- Q - #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:4+ g4 D6 ?2 l( F1 q- _; a
- #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
?# N$ U2 A. K - # z: Y+ z# l8 I7 R/ L
- SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
, ~! q* r& k* N l4 x8 t) Q - Shadow bytes around the buggy address:
" D4 M& @! |, X) E5 r% [7 W" } - 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00# ?( r- g5 ~1 \6 j( G( \+ Y
- 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
& g& S9 J/ `* q% Y' D* e8 E4 g - 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 005 z5 h& x; I/ m2 a+ N
- 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
/ S) j- U# U! g4 q; j6 ?4 P0 M' U - 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, `% F5 b2 a8 ~ Y8 k7 Z. S
- =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa
: E! T" f# b0 A4 k& x3 v% N2 y3 M - 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
) }4 [) e' h( g. t4 p, v) u. L - 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa! V0 f) y0 M9 i' {
- 0x0c287fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
7 e8 e! `0 o* Z" {# M - 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa K) A' q4 T0 C& Y7 K2 O
- 0x0c287fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
' d0 q* {9 l# ?! t9 `, L7 D9 b - Shadow byte legend (one shadow byte represents 8 application bytes):
}/ k, W2 \3 g2 g+ ~ - Addressable: 00) P& v. @7 E$ W2 i3 V
- Partially addressable: 01 02 03 04 05 06 071 Y" G: z( l1 {3 a: Z
- Heap left redzone: fa
) [6 u# Q0 i4 p - Freed heap region: fd, C; w4 H' V/ ]4 b8 m7 g& _
- Stack left redzone: f1# Q1 l9 V+ {5 t! O( y( b1 f: X
- Stack mid redzone: f2# r* f- C: \4 Q3 `2 Z1 p! x, m
- Stack right redzone: f3
4 ]! w, z( x$ v9 V+ @( D- d; E - Stack after return: f5
6 ~* n7 v! Z' `7 R0 _6 I4 e - Stack use after scope: f8
; ]3 h2 j3 |: _: o! V3 V - Global redzone: f97 {) a/ z- @6 l7 r& F9 V
- Global init order: f6
3 K3 d1 b, R' i4 H* x& U6 _* P; z" R - Poisoned by user: f7
( ]6 V) j$ v/ w8 T- `. A - Container overflow: fc. }/ {. M/ I8 I& {6 z) h
- Array cookie: ac
: Q& R: c. w. d6 h - Intra object redzone: bb
% x# V+ t1 z! Q7 @* L - ASan internal: fe
; T( n# M. X7 W3 N" j" [) u+ E - Left alloca redzone: ca
7 u8 W6 K+ m: H- f - Right alloca redzone: cb
! {4 ?7 O; d) b; v" @2 b6 I - ==110==ABORTING
复制代码1 H$ D. J1 K! i4 n; z: c
可以方便定位到堆栈内存越界访问的错误。 全局内存越界访问: 示例: - 2 l+ T2 t. P0 k3 T O
- #include <iostream>
. F4 G( y2 ?' j, B n - & E1 \( A# ~5 I4 s3 O7 |! ?
- int global_array[100] = {0};
. s& y, A9 w2 H+ f1 k - . Q! K& o9 W* R8 Y' O6 @0 T
- int main() {
7 K5 t" s8 v$ ?" x( g! T - int res = global_array[100]; // out of bounds; i. S& \4 \! P
- return 0;
. ]- {3 z8 t# }" h( f+ x' ~ - }
复制代码* u* v5 Y# @- Z0 O v0 a
编译and输出:
0 m h- {0 g- l - : S9 H, K X: w4 F
- g++ -fsanitize=address -g test_leak.cc && ./a.out
/ T4 \" U6 [- t) ?8 _0 ]" N - =================================================================7 g) d- A: M( e# F) _* r: f) M
- ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda527703 l) w9 u1 z$ ?5 \
- READ of size 4 at 0x7f42e6e02310 thread T0
$ N. `* w% ^$ G, g+ Y# ` - #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:6
" e8 i5 k1 P* w7 a; R( T - #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)/ V5 P( s+ \* l, ^+ H# L/ Y2 l9 T- W( {
- #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
* k$ O. G) f! H P$ A - 8 D- j" _. D0 q2 n. Y2 s
- 0x7f42e6e02310 is located 0 bytes to the right of global variable 'global_array' defined in 'test_leak.cc:3:5' (0x7f42e6e02180) of size 400
3 p. d- d9 D x& F$ l$ L - SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
* A8 b2 \3 x) [4 \ - Shadow bytes around the buggy address:
( |& ?# U! S) P# { - 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
/ G; R) w C+ o3 D - 0x0fe8dcdb8420: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f9
$ p0 Q# A9 }3 `/ o- S/ {; ^2 @ - 0x0fe8dcdb8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
* p1 p. w& y6 E+ @1 J ~ - 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
; }, h3 Q/ S& ~1 F2 G! q9 E - 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 002 z+ x3 [% N9 P0 R
- =>0x0fe8dcdb8460: 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
3 j9 i4 @, R3 ?1 ^' { - 0x0fe8dcdb8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
' }/ e" |0 L) o* Z0 {) J - 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0 q" x& h. k6 n! O* x0 N1 X C - 0x0fe8dcdb8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
" h' @( [6 j- |5 L" B/ P - 0x0fe8dcdb84a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00: W `/ K! v% P! B2 _7 Q8 S- q V8 g
- 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
" p$ @; g; d" ? - Shadow byte legend (one shadow byte represents 8 application bytes):5 ]& `$ h ?5 B8 w( [
- Addressable: 00
# d7 D9 I2 d3 N5 P2 N - Partially addressable: 01 02 03 04 05 06 07
% \9 n! H j1 y! L& \1 J - Heap left redzone: fa
# r0 V, a. L3 d* m - Freed heap region: fd2 z2 n! u/ B3 c7 a6 C! R1 q
- Stack left redzone: f1
K! d1 F* c* J3 Y - Stack mid redzone: f2' W# J6 W+ m. h: [
- Stack right redzone: f3
7 m6 e3 y. t4 }( W# S5 }/ d - Stack after return: f5& ^) ~! I7 i0 x4 A" I3 ^& K+ r$ D7 l; u
- Stack use after scope: f8* k; r/ q- m: e; E# a
- Global redzone: f93 q" o' j' j/ M% t: A V! ? I% f/ W
- Global init order: f6
; z& {* F6 T+ g6 w - Poisoned by user: f7# V" e: `+ y- l0 _8 K; d
- Container overflow: fc7 P( V" H$ E) T4 b# C6 T
- Array cookie: ac$ z# _ a6 {% y" a: v+ H
- Intra object redzone: bb
2 J+ i, ^ p) |7 ~# U - ASan internal: fe
! |* z, b- @. b' i. |4 W - Left alloca redzone: ca
2 `9 i- A/ J f: T/ Q7 \ - Right alloca redzone: cb+ y/ ]9 Q4 `" d! Z$ g
- ==116==ABORTING
复制代码
; i4 n# C8 J7 p
局部内存被外层使用 示例:
: l5 g% e; K% f1 w4 s; h* i- #include <iostream>
2 `( _ N7 V1 Y - s. i: w" G# t+ U: f
- volatile int *p = 0;8 S' f9 ^2 b$ j7 p
! v/ [6 ~; o4 c, P, L% d- int main() {$ u' f3 U4 w2 x: @* w6 o
- {
9 @* r* @, o2 l; ]1 [ - int x = 0;* \( _3 B! Z. F/ S
- p = &x;. V* m+ {$ [+ o1 K8 U
- }( |: q- j+ G& l+ N
- *p = 5;- {) g$ S) a4 \( w1 n
- return 0;. u q) [7 |6 [
- }
复制代码+ }: T* x/ b+ _) j1 r
编译and输出:
3 ?2 \1 l9 b3 T
0 j' H; y2 i7 e. [8 Y7 h$ Z" W- g++ -fsanitize=address -g test_leak.cc && ./a.out. f+ e+ |/ p6 z2 W
- =================================================================
( P/ }- n* A' H% G5 M" [ - ==243==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fffce12a4b0 at pc 0x7f3993e00e7e bp 0x7fffce12a480 sp 0x7fffce12a4701 s. a1 i& d. P U; ^4 B
- WRITE of size 4 at 0x7fffce12a4b0 thread T0
6 \! i" h: S! ^ - #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:10
' ?" F9 y n+ ?0 @, h7 ?3 T - #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)" Q, Y& g' f# _$ ^: c3 R. e2 W
- #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)! V" m8 \" ]4 o1 X8 Q
% S4 P, Y3 _, u4 `! X2 D6 a# B* Y- Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame1 F( C& y3 i) e0 U' {0 E
- #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5
9 c6 z+ o% K+ t2 L9 y
7 V5 w& |% s/ q, |, `2 S0 R. K- This frame has 1 object(s):5 E7 L+ ?1 D; Q9 @
- [32, 36) 'x' <== Memory access at offset 32 is inside this variable4 t; [( V8 \6 ^3 a# A. [9 R- w% ?3 V
- HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
) j- X! Z4 |- G' c/ \ - (longjmp and C++ exceptions *are* supported)
6 @3 m1 \" i6 F; |9 n - SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main
5 r( H6 _' ~: B1 t - Shadow bytes around the buggy address:
/ p3 w" \3 }) f9 W6 D+ u. y' { - 0x100079c1d440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; ?. o6 m$ X+ w' z6 S7 R
- 0x100079c1d450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 \* X; W U$ Z: F
- 0x100079c1d460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
* {# k. m2 i: v+ G1 i4 q - 0x100079c1d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00- O- x2 {' e6 X
- 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4 u7 \: [8 j& d/ F- n4 N, t% s$ X% h - =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 008 M. `4 i. w, ?! b" ?$ n( d
- 0x100079c1d4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00# p4 R& T! |' L% d
- 0x100079c1d4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
* n% q/ Z2 z: G# a4 X - 0x100079c1d4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
7 @. q- g3 O u) }& u' R - 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 004 a& p& h7 E9 ^: w7 n" B% G
- 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4 h2 V3 v' a+ ` - Shadow byte legend (one shadow byte represents 8 application bytes):
0 @: {: G- i8 S6 [( x - Addressable: 00
4 E& u/ ]1 I' ` - Partially addressable: 01 02 03 04 05 06 071 _- ]6 _& _4 c4 p
- Heap left redzone: fa( f# J/ k4 y8 z7 A- R' `, d, M; q
- Freed heap region: fd7 z) v! r. d9 ]8 y5 K
- Stack left redzone: f1
2 }+ T/ b! @& v9 ^ - Stack mid redzone: f2' o/ Q; m$ B2 H' u
- Stack right redzone: f3
2 F& W0 h7 Q6 I' C$ @0 J: _ - Stack after return: f5
" @' d/ Z6 F4 b; c% w: A8 m' P2 Z - Stack use after scope: f8
2 C* g3 p- Q; ?; g2 @* D( c; r! W - Global redzone: f9
) h& z; Y5 V8 Z4 ]1 M: p2 s3 f - Global init order: f68 c- U9 `/ G' L- P1 V
- Poisoned by user: f7
( @ N" M9 ~/ j+ c8 X - Container overflow: fc
, \, M! u3 ^8 {7 }4 ]. n; F - Array cookie: ac
! E- X( a$ y: b% O2 K$ n4 `& }* K" j - Intra object redzone: bb& U5 U% \; @# n
- ASan internal: fe3 U8 o2 r! G! f3 g+ T) B* _
- Left alloca redzone: ca
7 r1 U) @& q2 R. N" p - Right alloca redzone: cb
% ^$ @, h7 Z5 m/ w/ P6 @; X# ]3 y% E - ==243==ABORTING
复制代码7 u8 [; i' g# d
free后被使用 示例: - ! t" v) w; P5 q6 O, p: M' Z
- #include <iostream> u5 j* X4 {6 }7 R: ~2 j
( h* x" f$ o( `/ I" z- int main() {% ?3 u% V# n8 g5 W. q& Z
- int *array = new int[100];
9 I. P! [4 r* i$ } - delete[] array;
4 c6 F2 m8 l0 \. F9 y8 J( o - int a = array[0]; // error- E7 H9 m; D2 w/ w" I, ~; H9 d! q
- return 0;5 f: h& x+ ~7 U7 Z3 p6 A
- }
复制代码, }: _/ F8 ~/ B7 ?+ b7 K& I
编译and输出:
- @; g7 \* ?- l. H: r6 t; n. j - 1 M" Q: {; j+ B
- g++ -fsanitize=address -g test_leak.cc && ./a.out
. F: |; K; q* U e! e - =================================================================
# Q1 a' U) x; N7 L! I - ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a15010; y$ h0 ^; ~# c$ f
- READ of size 4 at 0x614000000040 thread T0
7 {1 p9 D: t. T7 L - #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:60 r5 J- @4 c8 S! l1 y; F2 C
- #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)$ C L& [- V; s3 @2 r5 Q
- #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69). L% i1 f. Y5 `8 U& I9 i* w" D
- 7 X5 e/ x8 k \' y
- 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)! b$ H4 {2 D, h9 o1 E) Q% m( }: W
- freed by thread T0 here:
6 O2 F# Q' t: S) O" X$ j/ r0 Z% S - #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)
6 L, |$ ~& j7 j2 \ - #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:52 x8 g* W. E0 o( @( a" A
- #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
5 b$ ?7 g5 O+ q
6 t/ p# X9 v" `; `$ _- previously allocated by thread T0 here:0 X7 B$ {$ e; W& Y7 ]
- #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)* l: @+ m* K5 V. K
- #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:4! B; ^1 o z5 T R
- #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
/ Q K9 c; h \, S. u! Y( ^/ K/ z - ; y8 @6 J4 h0 w. T+ w, ]
- SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main
: y/ K9 D, u7 P$ H5 c3 J - Shadow bytes around the buggy address:
5 ?, d+ E0 h3 d - 0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1 W- [$ }: l) Y8 r W# @& V - 0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
! q; ~9 f! r; j1 Y; Q" c - 0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 004 F) H* z4 z1 W9 b1 D
- 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; E& E9 r c2 C7 b5 v
- 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" M# a @3 Z3 a5 |9 H# J1 M
- =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd
& o" ~- |) U* Y$ ]2 ] - 0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd% q+ G" I. A, h$ [* ~" e5 D
- 0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
/ O- }2 @- b/ L# z7 |+ k - 0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa% i3 h6 R; t3 s- _4 X" ]1 R
- 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
: o N6 E$ B* ~. [' Z! @ - 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
& H! r- ~/ {& M* P; k- ~ - Shadow byte legend (one shadow byte represents 8 application bytes):* Z; I" H9 _- Q& i/ A. a
- Addressable: 00
* v+ ?8 H$ P2 B$ R8 g1 g! H - Partially addressable: 01 02 03 04 05 06 07, H3 n4 _! {8 }7 F% U; T6 U0 m
- Heap left redzone: fa/ K* Y& X8 E" L" H a! r) a; Z
- Freed heap region: fd$ w; n( o2 D$ _1 t
- Stack left redzone: f1
4 @+ y( A) E! W* W( C j+ O, ? - Stack mid redzone: f2( }+ z. B, i8 ]8 Q \
- Stack right redzone: f3, g7 |* ]# W9 s. C, Z3 d" M
- Stack after return: f5
, S1 f/ [5 b2 [/ C( x; P0 E( e2 ~# ^ - Stack use after scope: f8
3 H" Y# M; E9 g1 t - Global redzone: f9
7 c7 t- l0 r* ? Z, I2 X! { - Global init order: f6
1 ^) c" ?% m; o' x D- u" ~" T - Poisoned by user: f7" |1 x4 D1 o4 ]* P. z" w
- Container overflow: fc
) R' t- T, ~; ]( G- x - Array cookie: ac5 U& W/ K6 p! P9 j* O
- Intra object redzone: bb' g& r0 v% P7 R% z) l; t
- ASan internal: fe/ L: N1 F: b5 C
- Left alloca redzone: ca. |9 |, V$ h V* [8 N# i
- Right alloca redzone: cb9 G2 M0 ^- w; t- a: ]* s; C1 f
- ==282==ABORTING
复制代码- R) c% F6 K; Y- v& v
Initialization order bugs 示例,这里有两个文件:
; l9 c2 L3 i6 B- // test_memory1.cc+ i5 T, P+ X" ^( B
- #include <stdio.h>; }) t: A+ A2 ^
- ( `7 O6 i# H* N4 \4 v
- extern int extern_global;% }( V+ b. e4 r8 D8 ?$ r( T
- int read_extern_global() { return extern_global; }/ J6 n, H" N, Z e! x
- 6 w {0 l8 a4 Y! p; |& B7 F+ g6 a
- int x = read_extern_global() + 1;
. T; k l# ], b: ]( V$ d( }) \ - ' z) t9 {% z! k. S: t) {- s
- int main() {
+ a5 {" k, O f a/ @+ M, q - printf("%d\n", x);3 X C1 S4 F: p" Y6 e; h0 C$ h9 G/ _
- return 0;
. n" J3 f+ ^- {3 S6 R: [ - }
复制代码- % O* ?, S0 L) s# a2 Z' O
- // test_memory2.cc
6 G1 Z2 x4 ^4 ~) M6 z
6 F! y" G6 I2 H- int foo() { return 123; } I4 s' W5 `4 x) q) c
- int extern_global = foo();
复制代码, h* ]+ k4 T$ I' z7 w5 y E$ ~
第一种编译方式输出如下:2 q4 I) D; U/ V/ [. {
- 4 w, {6 k6 T4 o/ T. [% d
- g++ test_memory1.cc test_memory2.cc && ./a.out) t {' y5 k) {, i! e6 w! Q. v6 y- T# B
- 1
复制代码$ I2 H( x; W4 ^3 K& N' E5 u' `- O
第二种编译方式输出如下:
' z! w8 D8 z k* c- g++ test_memory2.cc test_memory1.cc && ./a.out6 s1 `/ K3 F" [/ ]% z
- 124
复制代码, l$ w& ]( I9 i& ~6 Q. {
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:
" J, B3 e5 a7 ?; l5 J/ p 编译and输出: - 3 P1 p' i& g. b N
- g++ -fsanitize=address -g test_memory1.cc test_memory2.cc3 [5 }- L' a% L" m# R7 d% T
- $ s, ~ u3 K) [/ f4 T
- ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out
' N9 a- I& d S' { - =================================================================
8 G7 U# \2 q% I( _: v# m& u4 I2 d1 W - ==419==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f46c20021a0 at pc 0x7f46c1e00c28 bp 0x7fffe423d920 sp 0x7fffe423d9103 X% X: D) g: V: L
- READ of size 4 at 0x7f46c20021a0 thread T0- ~1 \+ H( l$ P1 e' H) D
- #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3
t5 F. ?% b9 ~5 H! |% c: g. I) x/ L - #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:4
: {$ @5 s5 f4 h: r - #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8
$ [0 e- D: B. k# P5 m, e - #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)' q. T/ D& z+ Z! ~6 `
- #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)! Q2 p3 o1 F; r, P/ N6 H! I
- #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09)
+ A, t$ R8 g N- v5 Z
2 X" b& A$ H# f* H- 0x7f46c20021a0 is located 0 bytes inside of global variable 'extern_global' defined in 'test_memory2.cc:2:5' (0x7f46c20021a0) of size 4
. d3 n# z) ~6 C. P" z O - registered at:
0 d( y! t, l/ }. f) A( R6 E g - #0 0x7f46c08764a8 (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8); R$ R+ \1 @( x1 A5 w! {% e- h
- #1 0x7f46c1e00e0b in _GLOBAL__sub_I_00099_1__Z3foov (/mnt/d/wzq/wzq/util/test/a.out+0xe0b); P6 A; G. V8 h
- #2 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c); h7 C! l% c d' M+ A, Y1 a
- , Y" E- F* w8 A6 z% P
- SUMMARY: AddressSanitizer: initialization-order-fiasco /home/wangzhiqiang/test/test_memory1.cc:3 in read_extern_global()
" A1 ~8 Y+ D; z; V( n8 M, i! c - Shadow bytes around the buggy address:
; x0 X2 w3 S' z - 0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
% _! [& r2 m9 m - 0x0fe9583f83f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
8 t, l, F3 r0 E, i8 O( F8 C# E - 0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00) F, y3 i& t. S" S
- 0x0fe9583f8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00. r6 G! L& N( t
- 0x0fe9583f8420: 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9 f99 S& {9 ?: }# T+ U
- =>0x0fe9583f8430: 00 00 00 00[f6]f6 f6 f6 f6 f6 f6 f6 00 00 00 00) c+ J' \; S$ u/ q/ o
- 0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00/ O. B9 Q1 y6 q% M" M
- 0x0fe9583f8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0 U* V# D- C4 M( e - 0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00. }- B ~& M1 V/ J* U6 g
- 0x0fe9583f8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
- c5 C& k6 ^* o/ ] - 0x0fe9583f8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
; a2 W. h' O9 `, s! v2 f- X! ?+ ^% } - Shadow byte legend (one shadow byte represents 8 application bytes):
' I+ |8 o# f6 C1 `0 r - Addressable: 00
! v4 v. L& E6 d1 |4 x0 o; M6 u - Partially addressable: 01 02 03 04 05 06 073 O6 n' y/ r% l
- Heap left redzone: fa2 G( [; I9 ?' u/ \
- Freed heap region: fd
# m1 ^8 m5 H$ b5 k5 m" V - Stack left redzone: f1
8 Z' A9 c) i& T1 c( ]: @ - Stack mid redzone: f2
' _4 a$ x/ e- J9 R - Stack right redzone: f3- f5 t: L, k& L: k+ R1 R( Y1 p
- Stack after return: f5
0 j" b8 F5 P0 f& V, T" i' P6 c7 w5 r - Stack use after scope: f8* \" q4 Q& } X# Q% s7 T6 {9 s
- Global redzone: f9, e' p- \/ B9 y5 h- Y
- Global init order: f6
5 j3 A5 x, e* q0 c( D0 F1 } - Poisoned by user: f7
* n. _& ^' c) E) q6 m# C- b - Container overflow: fc& i: x6 w! |9 K$ f8 u4 m3 f
- Array cookie: ac2 l; W* T! p7 S- a
- Intra object redzone: bb
& J* d( ]7 A& o6 `3 A/ V - ASan internal: fe- {6 S# q3 X9 j" F# |8 x+ L E
- Left alloca redzone: ca
. q' L$ B' V$ ], H- n - Right alloca redzone: cb; f. G2 N3 L0 m0 }
- ==419==ABORTING
复制代码0 [2 c* l5 y1 f" N, \5 q
注意:这里在运行程序前需要添加环境变量:) ]7 }6 y, n: o' N J# H: ?. ? O
- 2 H' v4 W% J) \
- ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码
9 j' }3 B$ I% i3 Z v, `2 `4 n
小总结 ASan是个很好的检测内存问题的工具,不需要配置环境,使用还方便,编译时只需要-fsanitize=address -g就可以,运行程序时候可以选择添加对应的ASAN_OPTIONS环境变量就可以检测出很多内存问题。它的错误信息也很有用,明确指出当前是什么类型的内存错误,如:
5 M8 f/ {2 R5 ~" ^5 [ |