|
内存泄漏是指由于疏忽或错误造成程序未能释放已经不再使用的内存。内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制,从而造成了内存的浪费。6 L! a* X, f0 [! V! _3 S6 P# w( M
我们平时开发过程中不可避免的会遇到内存泄漏问题,你是如何排查的呢?估计你是使用下面这几个工具吧? valgrind mtrace dmalloc ccmalloc memwatch debug_new & y9 k5 _# c1 a3 Y) z
这里程序喵向大家推荐新的一个排查内存泄漏的工具:AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本都可以使用,支持Linux、OS、Android等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有: 使用方法直接看我下面的代码: 检测内存泄漏 内存泄漏代码:
6 S. j/ Y- d8 q- #include <stdlib.h>$ U. [6 P7 u& [7 O0 Y9 L( @
8 Y( c( t8 N+ D' X7 h8 o' I- void func1() { malloc(7); }5 _0 {- w W/ F3 [$ r- b
- & o& m0 A) M k1 H. x
- void func2() { malloc(5); }! |: F, p& D, _3 S p& V! j4 n4 Y0 n
/ u8 G0 K* m9 d3 B7 p# U- int main() {
6 ]1 ?, c# y* s2 }+ G! f - func1();$ N. e5 n( Q; v4 b2 Q: U
- func2();
4 ~! E1 l u7 M' x* y0 ` - return 0;
- o2 Q1 [; H5 f: e) C& S - }
复制代码3 o( `+ p) w2 S! I
编译and输出:
5 r9 I- f& k3 r( M. ?: c- g++ -fsanitize=address -g test_leak.cc && ./a.out
: M' i+ c% R5 U3 P3 H
+ a. H! c! H0 K* M! `- =================================================================: T9 ^- K4 |( V4 V
- ==103==ERROR: LeakSanitizer: detected memory leaks8 @9 g( U5 d. G8 U% Q9 r
, p, M& d- Y- c& T5 Q3 g$ n- Direct leak of 7 byte(s) in 1 object(s) allocated from:
9 f" j/ G8 h% B& u) X - #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40); R" l# R4 `! m" p( d# G7 ~3 R
- #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:3' x$ w; `1 s( P; \
- #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8
|8 L5 U+ r% W1 E - #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)3 r1 z1 n4 B$ F, Y. S0 l" Q
- 3 l2 r `! J5 R8 u9 g! ^, F# P
- Direct leak of 5 byte(s) in 1 object(s) allocated from:
5 Y+ n9 ?, X, z: [) | - #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)5 w, U6 {1 J) y6 x/ a* g, u% t
- #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:5
5 h0 ~/ ~' Y' C0 ^ {* g - #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:90 x" ] ], z5 ] K
- #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
" D% ^7 d$ I2 o, W6 ]$ a: W. J
1 @% F9 Q; y0 F- SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码' n H) b; i7 ~, R: Z
编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。 检测堆栈内存越界访问 示例:
( v& a3 Q( a5 S3 o( G9 v- #include <iostream>5 }$ H, T! u" b8 ]& j9 V
) O' `4 N R" N7 [' k- int main() {6 I K s0 L" z* m1 a- D/ }! y; K
- int *array = new int[100];
2 M$ [/ J1 c% V6 u2 v) F( h- l) p - array[0] = 0;
" ], H4 O; Q/ R4 Y2 x, t - int res = array[100]; // out of bounds
- F; G% D5 l6 E" e4 y$ t - delete[] array;7 R5 l* t, Q6 j- D/ i4 e' N
- return res;
$ Z' D/ c# b: X$ h# j - }
复制代码
9 y) p+ ~8 S3 K4 X
编译and输出:
T0 q6 `, z; |& F - : n7 @% @/ O. ~ _$ Y. a7 H
- g++ -fsanitize=address -g test_leak.cc && ./a.out
; ?; @7 ] ]$ k' ` -
1 P; s; o4 u2 t/ V- v. I& I - =================================================================
v4 o: K; B* _) g+ P' ~9 @4 |; h - ==110==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x7f0e06400d2e bp 0x7ffff5963f10 sp 0x7ffff5963f00
; Z0 C7 M4 ]4 G5 B5 @ - READ of size 4 at 0x6140000001d0 thread T0
" }* b+ M _* d" ]3 n$ \ - #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:6
" K% g! Y' l6 Y7 i - #1 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
* X; _2 H" E* F9 W1 F - #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)
# w+ f) L. M( d" ?2 b* r# d
* c. ~4 F& `8 P' \" J& X- 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0)" m8 Y4 U6 S7 p% v6 b( h: c
- allocated by thread T0 here:
& N* @6 |# G& V/ ^! U$ g; B - #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)
/ Q j/ q6 s% o- f! r _ - #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:48 c- p0 b! s$ R
- #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
3 x7 a/ Q5 d8 ^' I1 R3 x+ g
' S: l! O# I" G4 {- SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
L1 z7 `% T6 c0 B - Shadow bytes around the buggy address:
% f; \! s% Y0 ~8 A - 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
/ s" u8 A& Y: W) v+ Z$ F; j - 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 002 s2 ]: d. ~1 R/ r3 R3 ~
- 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00( i1 X4 l& h+ L8 |0 `
- 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 004 Y9 t/ E; s7 X: W) ] q
- 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00' y' \$ g* M1 E" c" Z
- =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa6 G7 i7 ]+ v& N7 i
- 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
; j3 Q) _0 t- E/ y# t+ j - 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa" x* |4 F# y8 A6 E: a
- 0x0c287fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa9 Z/ V4 q4 @" R: D1 L4 Q" e; ~
- 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
+ W$ B/ W" ~, [; p - 0x0c287fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
/ H( {# d1 s3 u! O: I# z! o - Shadow byte legend (one shadow byte represents 8 application bytes):
- A5 _/ P. B& E - Addressable: 00
2 y3 n8 h2 w) u( V& r0 i - Partially addressable: 01 02 03 04 05 06 07* u& a2 E( s9 c( n! b
- Heap left redzone: fa
+ X$ `: G) g4 _ h" ^; m" F - Freed heap region: fd. m) X8 J8 @1 U
- Stack left redzone: f19 t: D% I) j& z2 |: r& V9 C
- Stack mid redzone: f2
3 M6 h$ s# @$ K - Stack right redzone: f34 e Q/ q+ b5 h/ _5 Y$ f- j
- Stack after return: f5
6 k; [8 Y, y" s5 ? - Stack use after scope: f8
& z1 M" c+ y6 d; B1 k - Global redzone: f9
% O `5 j- I2 o$ L - Global init order: f6& ?- U; r1 Z; u
- Poisoned by user: f7- ^% R8 O: g. b' ~) m, `
- Container overflow: fc; q4 L5 F+ G7 u! `3 c
- Array cookie: ac
6 ~. W8 A1 |: @, j1 V - Intra object redzone: bb/ F$ p8 C$ J5 t/ e# A( c% t
- ASan internal: fe; K' d+ L7 ~; O5 M5 y$ C5 ]7 ~# U) N
- Left alloca redzone: ca
9 O5 O+ x" v6 M4 B2 j - Right alloca redzone: cb
1 X! ~2 j9 q: ]# ], I - ==110==ABORTING
复制代码7 [& P: {8 Y8 X! r: ]
可以方便定位到堆栈内存越界访问的错误。 全局内存越界访问: 示例:
( `1 o* a7 {/ }, d' d* ]- #include <iostream>' L3 l* m8 k3 {* v7 @$ `! ^, o* F
0 D# T2 o1 J, ]4 M" h' d" x* V- int global_array[100] = {0};
2 L" K" \, I+ u
: [) ]7 u- c* f( V, B q- int main() {
! z3 a- ^# U& W% ~ - int res = global_array[100]; // out of bounds
0 R9 w, R3 W3 W% ~9 W( v* T - return 0;
; p( q) F, N) f& F1 Q5 d; C0 M; e - }
复制代码
3 M0 Z& ]/ Y; j# L( y
编译and输出:+ P8 k% i( l2 l- y' ]5 ~
/ |, R6 m0 g* Q7 e- g++ -fsanitize=address -g test_leak.cc && ./a.out
6 G8 s$ e, ~; I5 B$ B) o6 _7 ~ - =================================================================5 A9 u" U% v F X$ S' h) x/ O
- ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda527702 I* ]2 A+ a8 H" i% R
- READ of size 4 at 0x7f42e6e02310 thread T0
* B$ P- ~5 O! d* l - #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:67 s2 H9 D( v2 |& Q
- #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)6 y6 y1 q: g$ Z( ^
- #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69): ?7 Q3 W- ]8 ]6 U N
- 5 P, s' z I2 S8 @& b
- 0x7f42e6e02310 is located 0 bytes to the right of global variable 'global_array' defined in 'test_leak.cc:3:5' (0x7f42e6e02180) of size 400. S7 W* } a0 r2 H. v) a0 {& N9 W
- SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main' i: u( @* \# t0 R% D! Q+ r
- Shadow bytes around the buggy address:
T+ ^6 u# L) J( ]& s - 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; G4 X3 |' I# e6 C. g
- 0x0fe8dcdb8420: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f96 G8 _# Z- A" O* X& _6 X& Q
- 0x0fe8dcdb8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00! H8 `: \! }9 e1 U
- 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
" k: Y) C1 \9 w. O) Y# } - 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 002 E, A+ N' ^. `; o. M
- =>0x0fe8dcdb8460: 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00) Z* Y3 @) X R. s
- 0x0fe8dcdb8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, ~1 C: c- ^8 _7 {5 G6 x& L
- 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
! B9 \# t. |2 ] - 0x0fe8dcdb8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
' m V7 v) y( o/ j! H1 q- c: t7 \ - 0x0fe8dcdb84a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 009 N6 s; A2 ?' `" f, o
- 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
& P& m# r: _' ]( i5 x' ?" y - Shadow byte legend (one shadow byte represents 8 application bytes):
* c4 x) W( x W6 t+ y" _$ y - Addressable: 00, X8 _% G$ R* T3 A+ [
- Partially addressable: 01 02 03 04 05 06 07' {3 d5 e$ _. m9 R/ E
- Heap left redzone: fa
& r$ }6 |( c' a; g% x/ S - Freed heap region: fd
1 y/ g, y4 G4 t0 u/ S - Stack left redzone: f1
& h) T4 s' q5 p3 [, W$ Q' S+ _ - Stack mid redzone: f2: x @& t5 b% w" w
- Stack right redzone: f3
0 x6 Y" D0 Y# s; } - Stack after return: f5
( R0 R% A! p3 _4 B* B0 V - Stack use after scope: f8/ c& ~9 ?" E& k1 C
- Global redzone: f97 E' w4 x) M9 G1 @( x* F* |% B4 `4 L
- Global init order: f6
$ u7 M5 h$ u, F' S3 c - Poisoned by user: f7
) L; K K' {# q; t5 E - Container overflow: fc/ l: E" F6 h& o
- Array cookie: ac( u$ U, M( x$ T/ `. _
- Intra object redzone: bb
5 U! j' m5 e1 z! w; b/ O7 M - ASan internal: fe
4 P% t/ s$ t a$ Q - Left alloca redzone: ca; t: V: Z7 I( r& ^! e0 j5 h6 F w( t
- Right alloca redzone: cb- t! j3 f3 J, C' N; ]. n2 P
- ==116==ABORTING
复制代码
1 H( I4 }4 {& s1 A3 z$ c
局部内存被外层使用 示例: - ' P! ] K5 I6 Y N. Q
- #include <iostream>
9 _, ?2 J& F3 z
: x1 w8 u2 p4 _3 E6 L% ^ g- volatile int *p = 0;8 ?& T4 ~4 i( d- d5 O& K, l9 l+ J
- ~3 i2 X& s/ X2 Y
- int main() {
. J: c) n9 I% F R5 c - {
E* |2 o4 m' F. L% E1 ^ - int x = 0;& Q8 p% e" C* B6 Y6 A
- p = &x;/ H: x- C L, E& x4 H# I$ i
- }
H' C L- [+ j$ K5 I& ^ - *p = 5;
6 M8 k8 Y; r0 m- M7 g0 B - return 0;
1 U3 F. j7 t4 H - }
复制代码
3 u3 v+ J- _1 H- M z
编译and输出:
6 T2 I( u# |) z7 j( E- T - 2 U, l' u( o( W
- g++ -fsanitize=address -g test_leak.cc && ./a.out6 r2 @3 \" N. g# c" _9 A# M7 R! R
- =================================================================
. d# A, I4 W: O- r- q5 G# S - ==243==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fffce12a4b0 at pc 0x7f3993e00e7e bp 0x7fffce12a480 sp 0x7fffce12a470
2 f. \+ ~6 O" |2 F% Z0 y0 e' i - WRITE of size 4 at 0x7fffce12a4b0 thread T0
9 v" s8 R7 {; U* d# L# C( x5 h9 b - #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:109 y+ h3 @ j& l0 o. |) _
- #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96); m$ A1 g- I" a+ R5 S! M8 ?
- #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)+ e* M5 b4 }7 H K4 P2 X
% ]$ M% U& K3 R' n T- Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame5 Z. }# B- ^$ r$ t4 V# @
- #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5
) O5 x+ m2 J/ [9 O7 P# c" ` - K h y! t; w- e- T# c1 L+ M, Q
- This frame has 1 object(s):
, @% ?6 A5 S, L( J) R - [32, 36) 'x' <== Memory access at offset 32 is inside this variable b/ x! n& w: u+ F7 W+ Y, U) O$ z
- HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext& S! [( b2 F+ G7 @' _& m
- (longjmp and C++ exceptions *are* supported)
( D8 p2 _ Q* W& p! o1 q - SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main9 T, ~5 [) G, Y7 h) T! B
- Shadow bytes around the buggy address:
7 T0 r6 {# t. l! t - 0x100079c1d440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
! e2 b( w2 p8 g! S3 A# O* ? - 0x100079c1d450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 001 D9 k. p6 K3 m/ c3 m2 C
- 0x100079c1d460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00* o9 ]( D. F. g- Q! E% y# N
- 0x100079c1d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00/ H$ n: D2 k3 u
- 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 002 z: a% f% }$ n. |
- =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 005 ]6 ^5 S/ L6 ~! @* j% t
- 0x100079c1d4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4 A; J. f% h! |5 |; G. {" d - 0x100079c1d4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
7 C0 M8 @0 i" X. D( y - 0x100079c1d4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
8 q/ p# c! n$ p! E3 Y+ {8 w1 U - 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 007 \( s; w+ \" s; a# G) H) L
- 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, l7 u4 F8 i9 Q
- Shadow byte legend (one shadow byte represents 8 application bytes):. s% t4 J0 g( C1 I' _, c. o
- Addressable: 00
7 v: Z, [# m- o6 o - Partially addressable: 01 02 03 04 05 06 07; i: N4 }! s" [) e
- Heap left redzone: fa" Q# Y- G4 I+ P
- Freed heap region: fd" g r( ~7 s) p% e9 J# J
- Stack left redzone: f1
2 x) k5 H( G: [& \3 u, D - Stack mid redzone: f2
( v) A2 b9 T' l; j0 Q& i - Stack right redzone: f3
) Z2 e H0 J7 ]: c" N4 F5 O9 g - Stack after return: f5
4 o ]7 ~2 m. ?$ w- m- X - Stack use after scope: f8
3 _" ]* f! n$ Y1 N2 D" { - Global redzone: f9
/ r. {2 S! a: P$ {8 y - Global init order: f6- b/ s& M- ^8 ]" w/ ?
- Poisoned by user: f78 \5 W# C9 K5 x& w$ d0 X; i
- Container overflow: fc
% Y4 d7 r4 S0 t* ^: a/ i - Array cookie: ac) X4 O% u; \6 B& p5 q; y
- Intra object redzone: bb' S+ ?; G: `& ?( s
- ASan internal: fe
; J* q4 x; z2 z: t' D - Left alloca redzone: ca
) l" S% g( |/ N0 x& k1 b# s - Right alloca redzone: cb; D5 t" N3 D9 R" m
- ==243==ABORTING
复制代码4 Y: A+ P- _ i/ N" N
free后被使用 示例:
( Y9 E+ b7 ~6 `; G/ w& F- #include <iostream>
9 R5 j; m0 u" @2 S* p8 `2 I
, F3 o, p& t& F7 S# E- int main() {
7 t0 T0 u! u }) N" J - int *array = new int[100];
: V/ q* X, Z! @# g# E6 W, Y - delete[] array;
; ?! A& W# }6 C& Z - int a = array[0]; // error! p- q5 C" w& O" v- k
- return 0;
- J% l. e" O. e2 `1 Z& A - }
复制代码
( k {. K+ m+ Q
编译and输出:0 V. e8 b) W' Z* F" V
- 7 c5 z" X5 t: L, _6 M0 y' i! _
- g++ -fsanitize=address -g test_leak.cc && ./a.out
2 x$ H0 x' A) l - =================================================================
) Q |: M0 J* y) W: u1 P4 q - ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a15010
$ F1 W8 f" J% n; H8 W - READ of size 4 at 0x614000000040 thread T0, {0 H5 x% {1 D8 C% Q8 }
- #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:6
4 M1 G, d) F) g$ t0 E( Q) | - #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
7 m% ` Y0 g$ e( ~" D - #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
% o1 v7 E, \( r1 T2 d - % `( `' q( _. ~" b
- 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0) i. L! t1 @ Q( @
- freed by thread T0 here:5 L7 `8 \( `0 r9 M: h T% d
- #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)
& ]) b! W' {- S4 U. m - #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:5
; w. A4 b3 x' i+ i q - #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
/ V& |0 V6 y) P: B, r9 |* e9 K W
4 k4 i4 \$ g0 |2 N- previously allocated by thread T0 here:+ d. S: D* W: C8 R5 J4 K
- #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)
8 X% J! ?" H+ }2 ~ v - #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:4
" k' w# P, x4 b4 N/ m* {& `4 \ - #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)+ G1 A+ e& ?1 J- v9 c% m" ]
# |1 c% I0 K2 L |5 m: X! R- SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main
6 C4 \; ?& q6 @; ~1 @: R4 {3 C* |4 L - Shadow bytes around the buggy address:2 N/ `& |5 e \! G. Z& D
- 0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
; [7 n/ A' ] }: P. z% G8 O, k - 0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 ^' Y5 y' g+ P+ W
- 0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
+ B1 |5 M: d# R/ K& H I( ?7 J$ `: T - 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2 [/ x* b& ]8 ^; S% a' N - 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. F# f l5 k8 {' |' y - =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd
% N" g/ b) |# U$ G9 R - 0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
3 M. d' J$ C+ B, r8 ]) X' Q! f* R9 ] - 0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
+ N9 E4 p1 G7 X - 0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
B1 S/ V7 v' A5 I - 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa: \# E* w6 M' x! |% Z8 m8 w4 H1 |
- 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa, I9 t/ _& ^( _5 m5 M
- Shadow byte legend (one shadow byte represents 8 application bytes):
* _' w2 h' p1 N6 G! }" k2 k* H - Addressable: 00
) G% R! Y& ^, g& _; g - Partially addressable: 01 02 03 04 05 06 07; N9 c6 }) `5 p8 J3 _/ G& }. U9 y
- Heap left redzone: fa
" {2 s- T. _ c& y - Freed heap region: fd
. x$ t3 ~/ T- e2 O0 G& Y9 A - Stack left redzone: f18 ?' @3 `/ ^2 r! v5 M( x
- Stack mid redzone: f2/ v4 n3 K0 X0 o
- Stack right redzone: f3$ _7 ~& x6 \; U) `3 j8 N
- Stack after return: f5; D% e/ C# A1 u( y( x% o
- Stack use after scope: f87 d4 E: R: T" K( ~* s
- Global redzone: f9& q" Q, p6 E: I3 ]7 x) c1 i1 ^
- Global init order: f6
* R6 \. L/ T$ ~: e - Poisoned by user: f79 P& K! O! H" p( a+ Q
- Container overflow: fc
& Y+ G; V$ K4 n5 |" t7 v - Array cookie: ac, [" a& Y% g/ A/ K& p/ k& p* f- Y8 e& j
- Intra object redzone: bb7 v" L& ^2 }1 f' k5 y
- ASan internal: fe
1 t9 x* J& s, a - Left alloca redzone: ca% g7 z0 p. D- Q, P. T4 `# x
- Right alloca redzone: cb
! C r! _ G- z- w: O4 k: u - ==282==ABORTING
复制代码
7 J' v. w$ C& G
Initialization order bugs 示例,这里有两个文件:
7 i2 b; U3 q6 m# ^% E- // test_memory1.cc) o( ]! j) j9 U, q% F/ }! L
- #include <stdio.h> k" y3 g; E b) ]
" Y) J* P; h g& |# m/ u- extern int extern_global;
W4 P+ t# \" y - int read_extern_global() { return extern_global; }
. c+ K9 P5 x% Z' B8 v: H0 _
" M) W$ J i: r- int x = read_extern_global() + 1;
/ D# A& _/ a% U7 z2 Z - + I9 [ Q7 f+ c+ O$ a0 g
- int main() {& `5 t0 [, T: K; h O! x0 U x9 h
- printf("%d\n", x);
9 o; m" k0 k; y8 d- g3 F( j5 K! ? - return 0;" n* z3 Y. R- u: x+ a5 h( \+ y8 y
- }
复制代码- 2 n9 L7 K* {2 [! j! m6 [1 b$ q
- // test_memory2.cc
( v# z% n+ U* a" w - : L: T& E8 Z. {% D% @4 U
- int foo() { return 123; }
9 S+ h" R4 U) {1 e' h - int extern_global = foo();
复制代码
# k0 i8 r2 p4 g7 s2 z) P
第一种编译方式输出如下:% R8 R4 n8 K9 s0 {& A Q7 \
- G' l# x- {8 v; D. t/ E1 d
- g++ test_memory1.cc test_memory2.cc && ./a.out0 E3 H( U. T: S% p
- 1
复制代码
1 z' U8 ^4 w8 d) ?/ Z
第二种编译方式输出如下:
8 |: K0 `" `( N' x" z( u- g++ test_memory2.cc test_memory1.cc && ./a.out$ ? r' N$ i+ a U: U, s
- 124
复制代码
) W( `$ t# v8 W% d' l- u c0 K
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:
( }3 K1 l n& [% m {$ G9 D 编译and输出: - " [5 I2 Y N o- Z4 @: i
- g++ -fsanitize=address -g test_memory1.cc test_memory2.cc( \) c) _2 o+ P% d
- * i2 D% t. K; x; T( a4 R3 ^
- ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out2 A7 ]* w, r- j. [
- =================================================================
& w$ }! L% h ^$ |& \: Q9 y( ^& p - ==419==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f46c20021a0 at pc 0x7f46c1e00c28 bp 0x7fffe423d920 sp 0x7fffe423d910
+ J: y1 W8 m4 x5 W# x# B: X ` - READ of size 4 at 0x7f46c20021a0 thread T0
* K" ?* X' g) g3 Q& T6 V, x - #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3
( q9 w- _* L- ^, E6 r/ R - #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:4& U: m1 f3 n4 ]" i; L8 [
- #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8' d3 o; z& M3 N; M* {; B
- #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
0 N' M, G0 Y3 x9 z" G* T9 ] - #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)
/ S6 M) @ I9 ?: ?! w, b* l - #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09)
# L1 o; D z9 `, f
9 i% Y- j& q9 y8 c- 0x7f46c20021a0 is located 0 bytes inside of global variable 'extern_global' defined in 'test_memory2.cc:2:5' (0x7f46c20021a0) of size 4
4 d% L0 V. w2 k( k7 G- W9 h+ l3 i; m4 T - registered at:
3 a8 {7 k7 V# p# I( i3 ?3 F6 W9 t - #0 0x7f46c08764a8 (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8)
" Q' K1 {9 h7 o1 ]( x0 t: j8 O - #1 0x7f46c1e00e0b in _GLOBAL__sub_I_00099_1__Z3foov (/mnt/d/wzq/wzq/util/test/a.out+0xe0b)
& D- J. @6 ~ U l. Z( K - #2 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
' m7 N1 e2 s8 Z/ b - / |8 }" P; N; \
- SUMMARY: AddressSanitizer: initialization-order-fiasco /home/wangzhiqiang/test/test_memory1.cc:3 in read_extern_global()
: @% }5 c8 w7 Z& {/ {( m- i - Shadow bytes around the buggy address:
7 u% R; Y$ t( d7 Z4 ]; V9 l6 I - 0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00% N/ |! `" V0 M6 a. D
- 0x0fe9583f83f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; W+ z5 q/ f: H( u$ f7 y3 x; c# j; p
- 0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; K, s# a1 Y" m1 l9 _
- 0x0fe9583f8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ ]' Y! h4 M# K, F
- 0x0fe9583f8420: 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9 f9# |2 W2 m& v" [1 @
- =>0x0fe9583f8430: 00 00 00 00[f6]f6 f6 f6 f6 f6 f6 f6 00 00 00 00 _9 M' |( g9 {" h) U
- 0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
9 k7 e+ _' i9 w- R0 ? - 0x0fe9583f8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00) b- e8 A, Y. n. ~/ ?7 F G I
- 0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
8 e) e% U/ `/ ]3 ^ - 0x0fe9583f8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
8 l3 O% `& r1 }; f7 A# t - 0x0fe9583f8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000 q6 t2 |8 H: V$ x9 r" h1 h
- Shadow byte legend (one shadow byte represents 8 application bytes):
* h9 b' H* z. D& Q$ ` g! r - Addressable: 00
8 A' P3 K$ e% \9 h3 j1 w - Partially addressable: 01 02 03 04 05 06 07 `+ \" `9 }( W3 l: ?
- Heap left redzone: fa% }4 G2 }1 i6 y5 [6 L
- Freed heap region: fd5 E% P4 i' n# m6 Y! H1 c- b
- Stack left redzone: f1
/ w) }1 f: ~/ f, s' g - Stack mid redzone: f2
& Z4 D* q( u0 M& a - Stack right redzone: f3
5 {* q6 h8 J4 Z$ Z) ]* M* f6 |, t: J - Stack after return: f5
) F6 t. \! S, B. ~$ V9 }$ ?# \8 ] - Stack use after scope: f8
. _4 ?" f" ~* x" W+ S. K1 ~ - Global redzone: f95 Y$ W/ s" _% W( C8 m1 h9 |8 l
- Global init order: f6 M4 g7 i+ S2 U2 C; @5 r; G4 F. v8 i6 U6 V
- Poisoned by user: f7
, k1 r# s5 i+ y/ i3 C2 C5 K/ C - Container overflow: fc" s9 c& ^( i9 ]4 j2 I, `, s
- Array cookie: ac8 I) L" ^5 T: B2 j2 [' M
- Intra object redzone: bb
! w2 G# j8 @* c1 x# _ - ASan internal: fe
' {; e: |" ~ `% A6 j - Left alloca redzone: ca
' b, b& L4 d5 d; ~9 I5 W" ^ - Right alloca redzone: cb
' R7 E9 r0 |& I7 s1 h - ==419==ABORTING
复制代码% j! B+ x! Y, M3 d! m
注意:这里在运行程序前需要添加环境变量:; g5 T8 {3 _# ?% G' ?% P, U
5 f# z- C0 ^2 ?- ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码
8 G! t$ p& Q0 y, ?* O
小总结 ASan是个很好的检测内存问题的工具,不需要配置环境,使用还方便,编译时只需要-fsanitize=address -g就可以,运行程序时候可以选择添加对应的ASAN_OPTIONS环境变量就可以检测出很多内存问题。它的错误信息也很有用,明确指出当前是什么类型的内存错误,如:
% r( q: c; l2 x" |& H5 W! m+ I& l |