|
内存泄漏是指由于疏忽或错误造成程序未能释放已经不再使用的内存。内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制,从而造成了内存的浪费。 我们平时开发过程中不可避免的会遇到内存泄漏问题,你是如何排查的呢?估计你是使用下面这几个工具吧? valgrind mtrace dmalloc ccmalloc memwatch debug_new
+ h: l" N6 M3 |* i) K9 I* Y
这里程序喵向大家推荐新的一个排查内存泄漏的工具:AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本都可以使用,支持Linux、OS、Android等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有: 使用方法直接看我下面的代码: 检测内存泄漏 内存泄漏代码:
S4 U" S& `$ E. x- #include <stdlib.h>
* A# u+ `* y( I5 Y5 g6 J - 2 M( ?$ {2 G, V% z. j# ]; s
- void func1() { malloc(7); }
* W7 D5 [3 y- Z( A# F' \( y, @- n - " h9 ?$ o' _0 {9 ]* S% g4 e
- void func2() { malloc(5); }8 ~( |+ ~3 b% T' J6 D3 J
/ |# N" s8 q6 P# H: f- int main() {4 w) r6 _& z' Y3 W6 \
- func1();
+ Q$ ?0 l- L. Y8 ^ u& \ - func2();
0 R" s: ?$ P* I, K' ^9 R - return 0;* }% g3 W& @5 l" D+ D }
- }
复制代码 4 V5 B S+ e' y/ O
% p7 l( p' I/ c1 r) ?. g0 O n
编译and输出:
( I7 m9 V8 H* W1 H7 T$ @9 N5 V- g++ -fsanitize=address -g test_leak.cc && ./a.out
2 a, g/ B( B: P
! D/ |9 f4 j5 z9 g; y- =================================================================% I8 a' W+ P6 D0 F* W
- ==103==ERROR: LeakSanitizer: detected memory leaks
% x \# ^6 e$ L! z- K+ D- @, N- O - ! R Z# c* b# v& o5 Z
- Direct leak of 7 byte(s) in 1 object(s) allocated from:% a# v4 v9 a# H# K
- #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)3 j# o, ^' O- u
- #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:3
) h# m- K2 E6 [1 \8 h - #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8& l! c: z8 s& D" q
- #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
^; Q: r- T2 g2 [5 S! n
+ c# Z/ [; l- h- Direct leak of 5 byte(s) in 1 object(s) allocated from:' \0 j' e3 i& F& I
- #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)" @' ?! t7 f0 B2 h( b8 [, _
- #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:5' N* [1 A; @' Q$ p! _* j. a
- #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:9, N2 N ~ I# U! }! @5 w
- #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
$ F3 t4 f7 n3 [! C m3 U - ) {" b Q s; o: c) e* |/ Q
- SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码 & V3 v) C7 A! }( O1 v* h5 N/ d
: c. z! m% P% \. _编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。 检测堆栈内存越界访问 示例: - ; Y' H4 ?3 k0 f! Z9 Y
- #include <iostream>
8 x. l$ y* W O g# p5 J
- A0 ~/ |' a s3 C& c: X- int main() {
4 u4 l( ~7 F; q& o. p - int *array = new int[100];
% E/ Z# p( P) k1 s - array[0] = 0;) n' f: T4 J- t( U
- int res = array[100]; // out of bounds
9 ]* [: r4 Q2 V2 S9 `6 `+ z3 p6 p - delete[] array;2 o c' Z3 t* T0 {% z4 Z+ @
- return res;
/ w, A* `. J9 O7 i% r - }
复制代码 * ], g' J+ U4 L% o7 H! c
编译and输出: - ' o+ U0 ^+ p8 J3 l, k6 K
- g++ -fsanitize=address -g test_leak.cc && ./a.out
9 D" |' @9 ^4 x/ ~$ h; b' t -
- @8 T* b: ]* } - =================================================================, D* [( G. M( g4 i" Z
- ==110==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x7f0e06400d2e bp 0x7ffff5963f10 sp 0x7ffff5963f00: W& Y2 n8 p6 K: Z
- READ of size 4 at 0x6140000001d0 thread T0, N1 N% z/ l5 b4 l
- #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:60 [2 s; x$ H' H( G# p
- #1 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)" u; E3 m2 B& K- X6 `% u3 @3 N
- #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)
9 J5 x) ~; h3 f - 1 E( }. x# y8 A+ \8 h8 z
- 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0)
9 ?1 W) f: T) o6 ]1 ? - allocated by thread T0 here:# `7 b: D j+ T! Y0 S
- #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)! Z) | ` p# Y- `/ y
- #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:4; @/ R( e* z, n3 H/ x2 ^
- #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96). a+ T" Q4 C3 h6 e- m) h
, }0 p( u+ A0 m% Q. z- SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main T- S! ^' n; m' F
- Shadow bytes around the buggy address:
+ H$ L" g9 `: V J# w6 e1 b - 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 007 @& U0 n8 f3 W S' `$ Z$ S
- 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
9 j3 h; W, d2 l$ B0 f5 a8 [ - 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00, d4 t; K+ k9 ^8 ^4 }
- 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 001 s" ?6 S, D4 R* j0 b1 q
- 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
! T8 h. F4 [, g2 k7 `5 O - =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa
; G v! E# ?$ Q1 \$ J1 V5 A - 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa' \# P/ M; ?+ |3 T# ]4 Z0 r
- 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa8 f( ]8 y0 `( ^4 j
- 0x0c287fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa% C8 B4 }2 d$ Z$ B: ~" b( t. e
- 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa# L9 f; [" \- Z7 N! k* Y
- 0x0c287fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
a5 t R) Z1 }2 g8 ~# q - Shadow byte legend (one shadow byte represents 8 application bytes):7 A! @( M( f8 |% m" j
- Addressable: 00
% c7 n/ k1 ~; M# z - Partially addressable: 01 02 03 04 05 06 07! k5 _" r- ?2 Z8 [9 p8 H
- Heap left redzone: fa
% e! O2 r j" x7 y/ B - Freed heap region: fd
6 q4 d6 e$ S5 X! Z; v" C - Stack left redzone: f1
+ c' t" k1 y' L$ f* V D& o - Stack mid redzone: f2
( a& _. P; [$ u7 B - Stack right redzone: f3
3 @' }' {7 U5 o# B1 n) M% K - Stack after return: f5
" x: H, y7 t' o - Stack use after scope: f8
" h( l% M+ E% e- q3 T - Global redzone: f9
4 [5 v" s' u0 L2 N- w - Global init order: f6
7 I2 C6 I$ ]9 Q1 y - Poisoned by user: f7
I6 \+ l2 y# q: ^( ] - Container overflow: fc
% r1 ~# |( q' Z3 t% Y - Array cookie: ac) d3 v4 o/ N$ D: l
- Intra object redzone: bb
* O8 T1 k( S E" p' X u/ a - ASan internal: fe
h9 W, B' _, ]) o- f - Left alloca redzone: ca! P. A% o. n& Q4 g3 j
- Right alloca redzone: cb
7 k% V$ D8 [4 F- E! X# Q - ==110==ABORTING
复制代码 : Y& I) S" U! |/ A/ {* M0 P
可以方便定位到堆栈内存越界访问的错误。 全局内存越界访问: 示例:
+ x" n5 k( f, Y8 P- #include <iostream>' \: ]) A2 U8 S4 W3 r( `* e; s
- ]0 D/ \4 j, Q, x S, f' T
- int global_array[100] = {0};
9 \; B. v( F3 A, K3 X( a
3 W. o L o5 B1 N Z$ O- int main() {
/ T, u1 x7 P6 p - int res = global_array[100]; // out of bounds2 v7 G6 I" \" m( B
- return 0;
* a3 {7 E* ?- f9 i6 v - }
复制代码 9 d7 V( O" T9 I
编译and输出:
K. F7 T' \9 H! _6 y S- g++ -fsanitize=address -g test_leak.cc && ./a.out: `- y9 K- }, I3 S: T" {
- =================================================================& }# g% q) ~9 ?' l& b+ B
- ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda527701 { L2 d. \$ s, K$ u
- READ of size 4 at 0x7f42e6e02310 thread T07 W3 _* v6 D3 F
- #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:6) ?% I7 D# }( l5 B; ~, i# e
- #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
& }0 R3 n) G. k f0 ?5 m - #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
3 W# z: Z* B4 P1 ?5 e) O - 8 V) I9 Q3 |& r8 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" ^. m5 U! ^2 ^/ R! _+ s7 }
- SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
) g" @! W6 |* y9 g' o - Shadow bytes around the buggy address:* K8 U% ^9 n2 r8 b/ u
- 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00( x. X7 D4 k+ Q; ~
- 0x0fe8dcdb8420: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f99 @, P; v' _3 d8 f& K" m" J
- 0x0fe8dcdb8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00$ q$ X5 q% F! X: N2 k
- 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
! n$ K0 k5 w! f - 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
* n* B+ j" x. Y) I( b$ K2 t - =>0x0fe8dcdb8460: 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
g$ N" `5 e2 j - 0x0fe8dcdb8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
" {% U) w* t2 g! E - 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
" b8 T4 E' F6 [ - 0x0fe8dcdb8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
) y- V# R+ q# u% w! [# D - 0x0fe8dcdb84a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00# X8 s# Z0 r" { a' J
- 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 v* d3 @8 K/ N4 G! k, v3 ^6 [
- Shadow byte legend (one shadow byte represents 8 application bytes):
! ]# Z X9 V# P! q - Addressable: 00# {+ b( m/ C2 W% H1 S
- Partially addressable: 01 02 03 04 05 06 07
9 |5 t# @1 h0 h' A, z' W - Heap left redzone: fa9 X3 h' }$ w% \4 A2 S
- Freed heap region: fd5 H- N }; `7 X; _& ]; H/ y% d
- Stack left redzone: f1
+ E9 v) u/ G8 X/ A' x3 T; O5 m - Stack mid redzone: f2
: z, p' F5 D2 e, ] - Stack right redzone: f3
% ?+ \6 G1 q+ t6 ^4 h7 A - Stack after return: f5$ K. S7 _$ G3 E& z( r* m
- Stack use after scope: f8
( }' @2 _7 g. t, U4 n3 v/ E: C - Global redzone: f9
0 u1 r! F- I3 G) A5 X, c1 k! C% O4 a - Global init order: f6
6 |; L ~* o* y9 n: e6 K. f - Poisoned by user: f7
5 A1 } Y! P9 s( ]! O; D3 _ - Container overflow: fc! A& i8 _+ ?. J7 f
- Array cookie: ac
+ Y$ L4 b2 i' M - Intra object redzone: bb
; U' A& F4 d, f5 z - ASan internal: fe( y: w' H& G% M8 f! Y
- Left alloca redzone: ca0 z7 p6 }: p' P, F/ o3 g/ ?
- Right alloca redzone: cb
W7 R. ~; K8 ~: Q# Q3 J - ==116==ABORTING
复制代码 $ N5 E0 G" c: N* K6 [% ^
局部内存被外层使用 示例: - 0 U8 c4 M6 W" w1 {& M
- #include <iostream>
6 f; w& B8 W1 Y
0 }/ K! @; o* G: Z+ j- volatile int *p = 0;
, d$ I! Q0 c0 i8 E# @* s - , J/ O; q' Q5 k
- int main() {7 e% k4 c: p# q3 E: J% @. y
- {4 i" g: d& S0 r, e% u" q& ^+ O
- int x = 0;4 q1 V; B: C: M+ U; B
- p = &x;
l0 x6 m+ r& ^! i! P, W2 Y9 C# Q - }# h0 `( j }6 Q5 K( j
- *p = 5;; e+ Z J' e/ j1 s; W. N# {6 J
- return 0;
4 s9 \" {) Z2 Q6 Y$ n* N8 P) K0 B1 i K - }
复制代码
4 W6 {( f% d9 |) K* C! ?! j2 o
5 o' k/ P7 t: r. U) d& x. L编译and输出:
& r) x. d( t5 C" M: i/ S0 l- g++ -fsanitize=address -g test_leak.cc && ./a.out! v% N& v& F8 O* _
- =================================================================
# S. c* [$ q6 e/ f J) @ - ==243==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fffce12a4b0 at pc 0x7f3993e00e7e bp 0x7fffce12a480 sp 0x7fffce12a470
{" o. E6 L0 ?7 y: U( s - WRITE of size 4 at 0x7fffce12a4b0 thread T0 q/ d4 Z" a. N
- #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:10
+ J* S8 N! ?! j' ~& l$ d; T; J - #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)! R- x8 @% c! `6 |7 U! M
- #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)/ r: R. a# y; v
- ' C# t+ Y+ w( g
- Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame, s0 S9 u0 L" p: d% ]( s) K. j
- #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5
! \5 i# \# N$ e ~& _- d2 o8 B - 8 E2 n; c! K! T2 L0 O) w
- This frame has 1 object(s):/ t6 c+ ]: b3 t& X2 E/ J* w
- [32, 36) 'x' <== Memory access at offset 32 is inside this variable/ V% W" H/ w. q# @" _1 f
- HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
7 Q. K3 f9 M' F8 N4 h& C; a - (longjmp and C++ exceptions *are* supported)6 v# L) O8 u0 _3 C9 z( F
- SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main& e# ?6 p0 \1 `6 t* p
- Shadow bytes around the buggy address:
: j0 U% A5 _! K" q, d+ c- t - 0x100079c1d440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 002 B2 v, O6 c' B. q
- 0x100079c1d450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
3 ~/ W1 l9 f7 w0 ^ - 0x100079c1d460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
+ z s: ~1 ~/ o& {- F) A - 0x100079c1d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00$ x4 O0 N5 z9 J9 D$ @" G
- 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. V" S9 b" c: r" z - =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 00" |0 d P. B; Q
- 0x100079c1d4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
- i6 ?: l8 \6 ?" W1 C5 w$ d" f: ] - 0x100079c1d4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00! T! s3 ?8 p5 \! ~
- 0x100079c1d4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00! w+ M- ?0 z/ K
- 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 _6 j; e, ?1 B5 s7 t0 R, C$ U
- 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 002 K% |/ a/ J# h. M& o& c; E
- Shadow byte legend (one shadow byte represents 8 application bytes):
; c$ S8 R- s% A3 ?! s, S - Addressable: 00
$ d) K# a1 a8 Y z3 H/ {, F - Partially addressable: 01 02 03 04 05 06 07
! P# q1 r- @, L5 ~ - Heap left redzone: fa5 w: ?/ M6 \& q9 D
- Freed heap region: fd
# s1 [3 N" s: i. a - Stack left redzone: f1
* M! g) W+ Q) a9 a! n: Y5 O% n! Q - Stack mid redzone: f2! T: {- u) v( z7 M' T( ~
- Stack right redzone: f3) n ]0 x& x2 Y# x
- Stack after return: f5( N7 B7 G; r* E& k4 B
- Stack use after scope: f8
K+ r; Z$ S4 ?8 B A' P# C - Global redzone: f96 P3 V9 T2 A; d' s9 B; h
- Global init order: f6# j2 I" r, q% {4 ?9 h: f L
- Poisoned by user: f7. m/ @, T2 v8 u9 R! c: V. O
- Container overflow: fc/ o7 O$ F6 `1 P, N
- Array cookie: ac# K5 D+ V8 C2 }6 N& [% |0 a
- Intra object redzone: bb# U' N: M9 N" Z
- ASan internal: fe0 V8 F9 v4 ~! x4 ?/ _" J
- Left alloca redzone: ca% T3 a5 _- w1 J
- Right alloca redzone: cb4 l r2 x& |, U; y9 P& G: G% C2 i
- ==243==ABORTING
复制代码 1 z0 G. C" ^ `2 c1 l! W
free后被使用 示例:
5 w/ f0 X# U; N3 b+ W% g6 o- #include <iostream>
- i; E7 j" O% F/ [" a4 Z/ q% q
3 ~8 l' ]: F* O+ x; u- int main() {1 M" G2 K7 ?( N x" P
- int *array = new int[100];
5 x! ~ k1 h8 Q0 S! K' x. z - delete[] array;$ t/ I1 v$ ?' H0 [! H
- int a = array[0]; // error1 |* N8 n0 N! o( M @6 s$ w) _
- return 0;
" l* j% Q. ], v4 @7 a - }
复制代码 8 F3 \) y1 |/ E* Y9 s
编译and输出: - - v4 p$ B9 ]6 Y" c# B3 {0 T! C
- g++ -fsanitize=address -g test_leak.cc && ./a.out9 D; C; |1 K6 e% K3 s
- =================================================================
/ l# i) A, v$ ? - ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a15010
X& E* I# K- W% R8 n - READ of size 4 at 0x614000000040 thread T0 ~1 N" q/ f& P9 Q1 P
- #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:6
8 d: `- s( S3 |0 ~8 M I - #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
/ }5 v: ]/ p' d) p - #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
- C$ v: Q& Q; I& U( D/ z
% a# a. c% d$ G: W; i' o- Q- 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)
! d0 y/ F: B& N2 W; ~ - freed by thread T0 here:1 }) C) G% h" [0 c. R
- #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)
, S- ^. Y C: @6 M$ _ - #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:5, m K6 @5 ? q# @+ h4 _/ C, I; e
- #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)5 N3 K' T/ G) f: t3 f2 @
' j7 A b0 J6 @: P* L( ]- previously allocated by thread T0 here:
/ L# |) b5 q% Q5 v: B* { - #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)7 }# S! o! W* R4 M3 V
- #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:4
2 z) A6 g: X" M o - #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
: |& q' a) k8 W- ]' O
9 k/ l3 o* Q" O- SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main F6 R j0 q3 b: E
- Shadow bytes around the buggy address:
: [' X6 m# Q; u6 f* _ - 0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
+ D9 s$ B4 E$ m" f2 U; `' |' F% q - 0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
; J) x$ T* b' x B5 u - 0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
: \6 B! q! [6 o - 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" a+ P6 R k/ F X6 ?
- 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00& F) D1 a; v9 z: G) O) z3 L! F
- =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd
2 a- d: u' _& j% k - 0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd3 g+ c6 z; g \+ a" F
- 0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
. I# _" r/ C1 T - 0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa: j/ e% Y( C1 b6 l
- 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa+ G! }6 z3 t% P. Y% g+ M1 r
- 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
" U0 S) y. _' q* v% e" ] - Shadow byte legend (one shadow byte represents 8 application bytes):
o) t2 a! p/ W# m# b1 U; a - Addressable: 00
/ t+ I. x# R' x - Partially addressable: 01 02 03 04 05 06 07, L9 G" \& b) ~* a6 V6 ^5 `
- Heap left redzone: fa
/ ~& H6 h. ~0 H- ]* @. z1 q - Freed heap region: fd
0 s j6 G* O- i( O) _6 s - Stack left redzone: f16 y& u$ w j5 e! d1 B0 R5 b1 H
- Stack mid redzone: f22 _# x5 f5 U7 K9 _
- Stack right redzone: f3
* |, E! [: g8 O, C0 P - Stack after return: f5$ ^* t8 B0 S; G. P/ I7 J
- Stack use after scope: f8
$ V8 S: @9 L+ \5 U* F, _ - Global redzone: f9* U5 Q ~3 d7 @9 d6 E0 h& f& b
- Global init order: f6# ~4 N1 k% B- G s2 U( s) C" G+ m
- Poisoned by user: f7
1 X) V* D( _9 Y q - Container overflow: fc& E$ ]0 W, e# }) v( f8 K
- Array cookie: ac5 @" `! U0 K% A2 Z! r7 w
- Intra object redzone: bb d9 S4 q8 _' d2 Q0 k6 n0 A' E; r( ?: z
- ASan internal: fe" q' M4 v8 n& q
- Left alloca redzone: ca
: m& l i( V0 M0 e; k( l - Right alloca redzone: cb
2 S, F" m" H1 L4 x7 ?( [ - ==282==ABORTING
复制代码
) d4 T1 U$ x4 p U# P d" TInitialization order bugs 示例,这里有两个文件: - * b4 e* t6 E) K$ e
- // test_memory1.cc; @9 J/ G4 `# d+ o; w2 R- x: b
- #include <stdio.h>6 ]+ W/ p! N' E' c0 W% A! b
- _! }4 A( ?) r; {2 D1 g- extern int extern_global;
$ d1 G5 q* M5 Y - int read_extern_global() { return extern_global; }! z- L. \4 a [: a
4 s; G4 x* a9 L0 M/ e. ~' c- int x = read_extern_global() + 1;
- @) _; T, T/ R+ s; Z5 C( s' Z - 6 U$ y7 d. K3 V( e* {
- int main() {
+ X0 y8 _( n: s% M - printf("%d\n", x);3 y1 \6 }! o3 N- J. G) G5 M
- return 0;
9 u9 V4 g1 Y; c; V9 }, p; h - }
复制代码- <font color="#001000"><font style="background-color:rgb(255, 255, 255)"><font face="Tahoma">
- O. ^2 H, P9 I - // test_memory2.cc Y; |' l/ t8 g
0 s+ t t, N: ?0 i2 M6 @: I- int foo() { return 123; }! d, g, K2 N6 C
- </font></font></font>
复制代码第一种编译方式输出如下:
/ n8 P) \0 l6 o- g++ test_memory1.cc test_memory2.cc && ./a.out2 j' ~+ j3 `5 ^8 K
- 1
复制代码 / S& X: l* u* @- Y
第二种编译方式输出如下:
7 S; E% M; l: _2 `: W- g++ test_memory2.cc test_memory1.cc && ./a.out
* A# [) a# p- c2 x6 N9 d - 124
复制代码 ! e0 ?: \ ~- q" j# B
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:5 r2 T+ t; p v3 j
编译and输出: - 6 e% s/ n- ?# t6 E7 h
- g++ -fsanitize=address -g test_memory1.cc test_memory2.cc
( H c- q- f% I, w* ~+ ^3 e - / m u# P6 R/ M& l) r% v% y% n
- ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out
( z9 F, _% u* a! O" O - =================================================================
- }0 s+ ]" t( @' y: |7 p: G* G6 W - ==419==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f46c20021a0 at pc 0x7f46c1e00c28 bp 0x7fffe423d920 sp 0x7fffe423d910
* R# T' ^" V2 p& b$ N - READ of size 4 at 0x7f46c20021a0 thread T0
; n6 \4 i* L" q3 u2 t - #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3: v) z8 _! y+ |# W- F) b
- #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:4
+ i: B" O+ e. g! b8 E2 N - #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8- y; m9 U9 {1 F+ i2 I( X
- #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
. l% R& x! J( H% ~" L# ?. P - #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)/ p2 K1 `5 q8 T) {! g4 v3 i
- #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09)( u% Q) x2 r$ s: c. U8 h6 m' ]# U
- # C7 e( U5 b+ @1 G2 Q, N9 O6 q
- 0x7f46c20021a0 is located 0 bytes inside of global variable 'extern_global' defined in 'test_memory2.cc:2:5' (0x7f46c20021a0) of size 4+ d. R% Z/ X: H# r; F
- registered at:
% P; N! S6 n" H4 F0 Q( ?: _ - #0 0x7f46c08764a8 (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8)/ _2 D6 Z: n; a* `
- #1 0x7f46c1e00e0b in _GLOBAL__sub_I_00099_1__Z3foov (/mnt/d/wzq/wzq/util/test/a.out+0xe0b)! F, x- g! [; E' X' ^( q
- #2 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)/ @) a" u, }- J
- ! ~/ B' c- H9 i- t) G4 q
- SUMMARY: AddressSanitizer: initialization-order-fiasco /home/wangzhiqiang/test/test_memory1.cc:3 in read_extern_global()
* e: D- S s" n9 k2 I1 M: O - Shadow bytes around the buggy address:% f* ~* G9 n# V+ t8 }( A, Y b$ `
- 0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
( D; ?5 @' O& {8 }% c- [ - 0x0fe9583f83f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00/ z0 I4 b. D6 c9 h* p
- 0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 J& j' S' e+ w& B! D
- 0x0fe9583f8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4 Z; R1 S$ }0 G b% c+ q - 0x0fe9583f8420: 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9 f9
/ F. a* m% b& p8 _1 r" `4 |1 I4 k - =>0x0fe9583f8430: 00 00 00 00[f6]f6 f6 f6 f6 f6 f6 f6 00 00 00 00# s1 b0 c* E/ n4 e8 ~0 {8 z
- 0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
! T5 G- D) z$ n. U" |2 t - 0x0fe9583f8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
% l5 H z. V4 h3 X1 | - 0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000 {' P- d1 G; f1 Y/ R
- 0x0fe9583f8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
5 X, ~1 b/ U1 E. V - 0x0fe9583f8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
: J ^) f( {; O - Shadow byte legend (one shadow byte represents 8 application bytes):; g/ }9 V9 x( O9 l: D$ P6 c+ N, C
- Addressable: 00
2 Z9 S+ _% o. R+ f - Partially addressable: 01 02 03 04 05 06 07
( b) P5 q8 k; n C- b - Heap left redzone: fa
4 F5 [8 I& G. M, U! _6 k - Freed heap region: fd. d5 t C( J E+ D
- Stack left redzone: f1$ j% s" z0 ~) Q# T0 j$ ^, M
- Stack mid redzone: f2" U4 y" C( s3 K) v
- Stack right redzone: f3
g2 O4 S V; o2 M" } - Stack after return: f5
% w: [4 S& h3 S1 ^& m4 [ - Stack use after scope: f8" n1 S6 s( C* h4 W$ g; ?, I6 t
- Global redzone: f9
6 V' |4 {% Z5 G+ L - Global init order: f6/ B1 p8 u' s, i0 z: Z7 s
- Poisoned by user: f77 r- `, H4 K' l1 L& X
- Container overflow: fc/ }8 P6 C y9 R* r9 c/ ~' x
- Array cookie: ac6 U) |9 R5 w- v5 Z6 x' k( W
- Intra object redzone: bb, S: \- Z( Z5 ^: }
- ASan internal: fe& C7 g0 r& l* ~% p% d) D3 f# b1 ]
- Left alloca redzone: ca
+ F1 E: y+ O7 a1 ]6 n. @& z/ u - Right alloca redzone: cb7 ^! D- K7 V$ `" J1 Q. X& B
- ==419==ABORTING
复制代码 * K$ a- S& ~: `
注意:这里在运行程序前需要添加环境变量: - & h j" y9 B" v- w# V# X" C" K
- ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码
$ D/ @" M- V, l1 @; T小总结 ASan是个很好的检测内存问题的工具,不需要配置环境,使用还方便,编译时只需要-fsanitize=address -g就可以,运行程序时候可以选择添加对应的ASAN_OPTIONS环境变量就可以检测出很多内存问题。它的错误信息也很有用,明确指出当前是什么类型的内存错误,如: |