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