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