你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

如何排查内存泄漏问题?

[复制链接]
gaosmile 发布时间:2020-10-1 16:06
内存泄漏是指由于疏忽或错误造成程序未能释放已经不再使用的内存。内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制,从而造成了内存的浪费。
% 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等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有:
  • 内存泄漏
  • 堆栈和全局内存越界访问
  • free后继续使用
  • 局部内存被外层使用
  • Initialization order bugs(中文不知道怎么翻译才好,后面有代码举例,重要)

    $ _5 }# m, m5 O' h  b& ?4 o
使用方法直接看我下面的代码:
检测内存泄漏
内存泄漏代码:

  1. , D( g, V1 n% C- z
  2. #include <stdlib.h>
    6 I6 s0 ?6 ]) h
  3. " Z1 o- A- W4 A( _* ^" I
  4. void func1() { malloc(7); }
    6 p3 |5 c9 k) P+ R" C! U1 v
  5. $ |/ B0 M! B. A2 n' A$ J7 s! |0 r
  6. void func2() { malloc(5); }
      Q5 T8 x1 d8 K. x% x9 ?
  7. . ~/ _0 d/ i/ Q8 G
  8. int main() {4 k( ?( t! Q- e5 n
  9.    func1();
    , w# T8 P! G% m# ~- U
  10.    func2();# f( s# P# [4 x" J; b8 ]8 V
  11.    return 0;
    ; f  W6 U0 F% [% n* Q6 N
  12. }
复制代码

; o* S: ?5 p) s! P2 R
编译and输出:

  1. " P( s& i$ U4 X9 ], v
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out1 Y8 l  l; z0 `! |8 W

  3. ' _. z! w4 J( P3 C+ e6 ^! M+ _+ m
  4. =================================================================* k9 B) Z6 P0 y: v8 ^
  5. ==103==ERROR: LeakSanitizer: detected memory leaks
    4 V6 q; q/ o1 i! N  W$ q+ R
  6. + z, x4 `" D2 P- z4 E& W. Q! m. m. I
  7. Direct leak of 7 byte(s) in 1 object(s) allocated from:
    ) f' a; x0 f; C( Y5 L5 u
  8.    #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
  9.    #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:31 A6 Q5 H& X2 A' m2 {& C# @
  10.    #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8
    6 Y  `2 i1 G( w  G* A7 W4 F
  11.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    & h- Z# a5 B. u
  12. + R- ?+ x# ~- w
  13. Direct leak of 5 byte(s) in 1 object(s) allocated from:3 j: f9 x$ _3 _
  14.    #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
    ! r: ^# U0 D1 {! ~9 \
  15.    #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:5
    , B" ^6 |5 Z( a4 p9 g1 u' t
  16.    #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:9: |8 n; H" q* u  g) b, s
  17.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    8 ]4 _& t6 W6 o/ `7 d  G, W

  18. 7 h3 a3 D3 w( K7 z- x' W. E# [- B8 b; r
  19. SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码
& M2 ~: Y" R% Z
编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。
检测堆栈内存越界访问
示例:

  1. # V) D1 O% F5 }" ~7 T% Y# q/ ]
  2. #include <iostream>
    ) h: `) d2 x  K: R

  3. * C2 Q) P/ r% M+ [# E
  4. int main() {
    2 ?- V% B0 q  H" M* j' t4 L+ f
  5.    int *array = new int[100];
    % m! z& t* d# n. B- n, h1 D
  6.    array[0] = 0;
    5 t7 t' `; N6 I
  7.    int res = array[100];  // out of bounds/ B% @  e0 e* r, n7 }! s3 K
  8.    delete[] array;
    ( i" E- I7 Y  f7 g# ~( N9 l
  9.    return res;
    ; {6 J; M! T3 B8 N9 V0 {- M1 N
  10. }
复制代码

  v# x/ B" N6 @, F
编译and输出:
7 c1 ^  O* m" q4 c6 @; s% n

  1. , S. n9 G" M* \$ N
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out1 C# U/ n8 I# I8 C$ T" d# S( V
  3.    / K8 Z' S% p6 x! @* h' I0 t
  4. =================================================================
    ( H( J; V9 p( `
  5. ==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
  6. READ of size 4 at 0x6140000001d0 thread T0' x2 ~8 P. S0 K
  7.    #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:6
    , E& m' z4 P6 p& j
  8.    #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
  9.    #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)9 j6 j( p* G( \# [4 _7 o

  10. 5 T  T; O) A) V% b
  11. 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0)4 Z7 o. W+ G* Q: l8 h! ?
  12. allocated by thread T0 here:3 P9 ]: w6 q2 C/ }' ^
  13.    #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)
    + z2 [' R- s9 T5 V
  14.    #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:4
    - k  O* _0 j# F6 ~2 u; r
  15.    #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)5 ^/ V2 q" ]1 o8 m5 V: J

  16. 5 J( c8 V4 s. Z# Z! n! y! i* V
  17. SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main; h. g  t8 E4 p2 l  F; `" A) C
  18. Shadow bytes around the buggy address:
    # E! k% R& I+ p0 o5 T
  19. 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000 s9 i, K% n& T( I
  20. 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 006 X: g% n8 h: H
  21. 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00, D9 x! F1 Y4 y* [, C
  22. 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    2 C5 ^- s- l3 o6 N; [' T
  23. 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
  24. =>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
  25. 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    7 U& e9 l3 y1 R
  26. 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa& D6 E2 t& @: A/ f5 G3 [. n, {
  27. 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
  28. 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    0 N' S' z* w9 G/ K
  29. 0x0c287fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    7 T# q. k4 x+ N# g
  30. Shadow byte legend (one shadow byte represents 8 application bytes):1 J0 E4 u3 |" h1 \' \9 L
  31. Addressable:           00, _$ ~! R2 D$ t+ z4 f5 I
  32. Partially addressable: 01 02 03 04 05 06 07
    3 A, m+ _+ Y6 N. J+ N' n
  33. Heap left redzone:       fa4 K0 a* Y4 y" K% Y) W# V0 ~8 I
  34. Freed heap region:       fd
    , L* C2 A; f& Y4 s0 q5 K
  35. Stack left redzone:      f1# L2 H- ]. H( x7 D/ Y- k3 m0 m
  36. Stack mid redzone:       f2
    . G- q+ G) W1 c! w* t
  37. Stack right redzone:     f3# C) o: M$ B, \2 e# K0 ?' n5 M; d6 [
  38. Stack after return:      f5
    ! g. o$ c5 X) }) L* P4 l
  39. Stack use after scope:   f8
    . N" s8 `" f* x! e& g, v
  40. Global redzone:          f9% C+ _% {8 {9 v! S
  41. Global init order:       f6
    7 s( }4 g" f& }
  42. Poisoned by user:        f7
    ; w& {8 r  r, v# w0 J
  43. Container overflow:      fc! s6 w5 S; |- ~2 b. T  O( W8 M1 `
  44. Array cookie:            ac1 A1 b3 I6 Y+ K, Q
  45. Intra object redzone:    bb* Z! f7 X: d7 B. s
  46. ASan internal:           fe
    9 w; h& ?4 z1 m% A9 ^# B
  47. Left alloca redzone:     ca8 P. g4 B9 s3 b& ^* @& d' m  a" |
  48. Right alloca redzone:    cb* K; E! k% j( Q  k: w
  49. ==110==ABORTING
复制代码

$ Y. b% A( c2 [6 R% \# J3 _
可以方便定位到堆栈内存越界访问的错误。
全局内存越界访问:
示例:
  1. 1 _( `- [& t0 S( m
  2. #include <iostream>* `7 O& R$ M5 O& X  ]2 P4 E
  3. $ S5 k- \$ W$ ]$ a
  4. int global_array[100] = {0};( R' A& e3 }0 c, y

  5. 0 O2 c9 R! c4 ]) o) @" g% K6 A5 ^
  6. int main() {
    1 u9 q( V1 \, W3 {
  7.    int res = global_array[100];  // out of bounds
    ) ^9 G" V" ~% r
  8.    return 0;! P* ?; m, j& z6 \& D( k' Y
  9. }
复制代码

( a# ]4 d: u+ C5 J( c* P3 Y, }; |8 H
编译and输出:
+ @6 t! |4 D: ~) R8 A' H6 j2 B
  1. ) y5 c: r! o; }3 D; f& @0 n
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out/ a3 O( H; L$ ~( i/ Y! c
  3. =================================================================$ [4 v0 ?/ M8 p9 o
  4. ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda52770
    8 A% F2 P* D5 w6 F
  5. READ of size 4 at 0x7f42e6e02310 thread T0
    + @; j2 S5 q( E( q0 d' ]
  6.    #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:6
    9 Y+ x, V9 j/ M$ {. G( Z. M; ~
  7.    #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)  b1 N& b6 c8 O: S
  8.    #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)3 J& J: }$ ]; \5 ~- q& T
  9. : T! ~5 y# {2 t0 ^+ Y7 x
  10. 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
  11. SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
    # {6 {; p4 s, a
  12. Shadow bytes around the buggy address:6 r1 r# i& y- c
  13. 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00$ F6 x" J: }9 u* c( j
  14. 0x0fe8dcdb8420: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f9" O8 R8 ~" p. d; f' x) }
  15. 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# \
  16. 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ' g1 I' D- t# ], O$ Z8 B
  17. 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ' D% `1 E7 |/ Z) G2 O
  18. =>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
  19. 0x0fe8dcdb8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ; Y5 \: h2 n3 L" {5 h
  20. 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    . e  F2 b% o; p
  21. 0x0fe8dcdb8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ' p/ c. ?- [' X4 G$ |( R0 v7 Z
  22. 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
  23. 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 004 }: {/ |$ W" J7 Y: S8 p+ A
  24. Shadow byte legend (one shadow byte represents 8 application bytes):; K3 r2 {1 [7 p0 z
  25. Addressable:           00
      c- y2 c7 _. g8 g8 T7 o
  26. Partially addressable: 01 02 03 04 05 06 079 c; K, k# H" {- `
  27. Heap left redzone:       fa6 ^% U) u% q, }) }% ?+ b2 g, Q
  28. Freed heap region:       fd
    $ k4 i$ ~. h& x: S& k. r
  29. Stack left redzone:      f1$ k# t$ U* M9 r0 J% `7 {
  30. Stack mid redzone:       f2$ y1 B. t9 N# |" S) k9 r* t
  31. Stack right redzone:     f3' _; M1 d" r5 P, r0 U
  32. Stack after return:      f5
    3 x& G! ^) i, q0 a3 b' Y" r
  33. Stack use after scope:   f8( u% g* R0 U- M% e9 {& s- S4 W
  34. Global redzone:          f9
    2 T7 y/ m7 P5 _
  35. Global init order:       f6
    7 d1 }; z) D% n% x8 H1 V# y* Y
  36. Poisoned by user:        f7
    0 y& Q9 h& d" x# M1 K9 T1 d9 Z; J
  37. Container overflow:      fc
    4 k7 t% k, P8 ]% O- m6 S6 S
  38. Array cookie:            ac8 I& k  J, L7 |* M5 s5 g4 `
  39. Intra object redzone:    bb
    , V( m3 O: H' ]3 }0 `, c  J! g- R! ^
  40. ASan internal:           fe6 K6 F/ V6 [. ?( z
  41. Left alloca redzone:     ca
    / I9 h! b5 M1 K$ ]4 o" n% B
  42. Right alloca redzone:    cb* W, [2 q+ I# u5 n8 V3 d
  43. ==116==ABORTING
复制代码
) B" y5 A# o" h( [" n  k% F: s: l9 A
局部内存被外层使用
示例:
  1. 5 c! G- G4 Q% z- J$ C
  2. #include <iostream>/ a4 _+ t5 c/ I) Q
  3. ; o' b& z" i* z4 r3 I
  4. volatile int *p = 0;7 z0 H/ J! l8 i( H4 m2 G

  5. & b( L! o) r& g& _' z
  6. int main() {& a9 [+ ~9 O/ L5 U
  7. {
    - e% ^8 b; M. |& q' k
  8.    int x = 0;1 t+ T. W7 m- q4 j. Q6 l
  9.    p = &x;0 {( `6 B% J# ?# ^
  10. }
    ! h' u7 F3 q" c( m. F6 c  \8 {' [
  11. *p = 5;
    ) r6 _4 g- u3 r
  12. return 0;
    0 X' e+ G. c  }. s, s
  13. }
复制代码

, l5 K& E, B6 q4 G$ S1 z
编译and输出:. j1 \: K  A! D6 s

  1. % _: T9 l2 `7 Q* K6 y; t' Z4 {
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out" o  y4 A- \- s+ I  A7 w
  3. =================================================================# t( q8 s2 ?# R/ D
  4. ==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 @
  5. WRITE of size 4 at 0x7fffce12a4b0 thread T0+ L; ?5 k5 m' h( W
  6.   #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:10" r  B2 q: I, [  L; [- Z% |
  7.   #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)1 d# |9 P# I( N* N- t: I
  8.   #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)! _8 z# m6 T2 g; @$ A9 ?# g
  9. 5 Q0 e( ^. `: d1 C
  10. Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame1 x' j9 x0 @3 o4 M
  11.   #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5
    6 B% a1 }; x( V  U2 `$ [
  12. 6 ?9 x4 w" ~3 ?) F4 j9 A' |+ i
  13. This frame has 1 object(s):
    - H1 j( O* v1 }6 R: p
  14.   [32, 36) 'x' <== Memory access at offset 32 is inside this variable
    8 p7 @5 b. Q0 Y" W0 Y& A; j
  15. 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
  16.     (longjmp and C++ exceptions *are* supported)6 V; V/ p* Y* d! ^8 u! c
  17. SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main
    & e! t9 `$ P2 ]( K) Z$ E: `
  18. Shadow bytes around the buggy address:
    ! z7 C  s. J0 c$ B* B: y: t
  19. 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
  20. 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
  21. 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
  22. 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
  23. 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
      F! X5 S. J: k1 w
  24. =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 00
    5 D* v6 i+ E! B( y# {
  25. 0x100079c1d4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00. v' \* ?3 y5 H: _2 g! R
  26. 0x100079c1d4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, K6 b! V" ?( V1 \/ M2 s2 l
  27. 0x100079c1d4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    # ~# h$ T9 m3 R1 ^7 ~& E
  28. 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    # w) A: p# n. E
  29. 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    * n1 P, ]: u+ p3 F! Q4 e
  30. Shadow byte legend (one shadow byte represents 8 application bytes):
    ( O8 n+ k& L4 K# w$ F7 B
  31. Addressable:           008 c# z- f3 `' X! N. }! x
  32. Partially addressable: 01 02 03 04 05 06 07
      N( w7 a  }3 \' `0 h( W
  33. Heap left redzone:       fa; w! t; s. [4 e. Y1 ^8 n. Q
  34. Freed heap region:       fd
    , f/ j) V4 A  S3 G% P3 a& \
  35. Stack left redzone:     f1
    6 r/ c! {% o8 \; t; j5 ]8 D6 r
  36. Stack mid redzone:       f27 n& c6 n: L0 d4 Q
  37. Stack right redzone:     f3
    / ^2 p1 g' Z  Q% g9 k9 C; A8 p! r$ S$ j
  38. Stack after return:     f5
    ' {% z- x, B( `$ b  u1 E9 H
  39. Stack use after scope:   f8
    $ e5 m! H# O- B8 Z) o
  40. Global redzone:         f9
    3 T0 [% D" f) ^9 `2 z( p; h
  41. Global init order:       f6
    2 C9 r2 A* d$ G4 D+ B/ a' Z% F7 |
  42. Poisoned by user:       f74 h) D- F) B! N2 l/ [2 d6 r
  43. Container overflow:     fc
    ; L% o8 q7 j3 ], S
  44. Array cookie:           ac
    ' s# t) F' b% W2 T* y2 R
  45. Intra object redzone:   bb
    ) [) ^! ^4 E, K) w9 f( T: N
  46. ASan internal:           fe( Z" Z. P: n; ]) R2 t+ S  H4 \
  47. Left alloca redzone:     ca9 H' Z9 t, J9 r
  48. Right alloca redzone:   cb
    ! [9 I4 t+ Q3 k. y5 z- c: C2 {
  49. ==243==ABORTING
复制代码

$ X4 R7 A) E/ l( C( h  L
free后被使用
示例:

  1.   |1 X+ u/ f1 i3 D2 f  U1 V& E! G- C
  2. #include <iostream>  \5 ?) ~# J" v8 B5 c7 E
  3. . q5 W2 }# z1 O8 a, s5 n2 L
  4. int main() {
    6 U3 w; l" F1 |4 g: H
  5.     int *array = new int[100];5 \" [! {0 l1 n$ l9 Q* E% P
  6.     delete[] array;) d" @/ W6 N" W( W" A
  7.     int a = array[0];  // error- u2 Y! d+ i5 J6 T$ `( O
  8.     return 0;
      [, @7 t4 j* H8 Q5 M& ]; b$ O
  9. }
复制代码
- ^2 s/ m$ b$ j/ X
编译and输出:
& D6 K- x  X4 ^+ V0 u, ~
  1. * i+ n- D; J# S. @) J% q
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out8 v5 Z* I9 f* h7 T3 |# M! Y
  3. =================================================================
    0 y9 A0 q7 C$ R2 c" b
  4. ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a15010$ ~$ O/ q* X6 q# x" c3 m
  5. READ of size 4 at 0x614000000040 thread T0
    ' T: A: `5 q- K2 Y: h6 j: W
  6.     #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:69 D$ F& E- ^7 ]5 i+ n+ s
  7.     #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)0 J% {  h% k5 v% R0 c/ K4 y
  8.     #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
    : ^8 I# a; S3 \# w* t% @) L0 t3 K

  9. * @1 Z4 n6 g, R  l9 W: Z! F
  10. 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)
    % k: g! n0 ?' Q  I# T/ ^0 B8 _1 H0 D
  11. freed by thread T0 here:
    4 p( t1 _# H% ~5 S# ~" ~* L
  12.     #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)( |" u3 n$ W; N" H/ c4 `
  13.     #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:5
    " s$ r* A5 |: g5 v
  14.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)! N* W3 p, {. p; B; Y3 i( z
  15. . T" g; b. Z6 j" ~
  16. previously allocated by thread T0 here:& X. X# X2 P# [; R3 F  u! w5 v3 i
  17.     #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)1 r7 k% e$ e, }$ B
  18.     #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:4
    ( @3 ^  y, A, L) E
  19.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)4 q5 l# ^/ C0 o1 ^

  20. 4 [) z# W$ q9 o4 w, ^" k
  21. SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main
      ^2 f; ]% ?4 L6 `' I" L/ e4 D
  22. Shadow bytes around the buggy address:+ O# S4 ?- e$ z' a$ m/ J  ?. X1 v
  23.   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
  24.   0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
      r5 C' I3 L$ u
  25.   0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00& f  L! K, O3 i  j4 q
  26.   0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ' Z1 P- s3 b8 d: B9 B
  27.   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
  28. =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd. |" s' h6 g0 y/ X. Z4 }0 ?  @
  29.   0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd2 ~* a: u* H6 C
  30.   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
  31.   0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
    ( O1 A) D' ^1 `
  32.   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
  33.   0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    8 K  p5 ]; M1 X0 }% A
  34. Shadow byte legend (one shadow byte represents 8 application bytes):& j! |4 C7 r$ V1 t
  35.   Addressable:           00
    : X# |7 K5 ^& ?1 P$ d6 p2 Y
  36.   Partially addressable: 01 02 03 04 05 06 07: F0 y% q, c; Z8 Q3 X" b# P: m
  37.   Heap left redzone:       fa6 ^) r: E: t0 {9 W
  38.   Freed heap region:       fd
    - I( X7 p, B% p, M
  39.   Stack left redzone:      f1
    ! s, E. ?' ?" E
  40.   Stack mid redzone:       f2
    ' z! O$ _! X9 t; Q- `
  41.   Stack right redzone:     f3. i0 s! b8 g+ y  h" h
  42.   Stack after return:      f5
    2 ]# V6 i' u4 d) [  s! [
  43.   Stack use after scope:   f8+ b9 K. n. f7 }
  44.   Global redzone:          f90 A% y& Y5 G8 g0 V' }  z
  45.   Global init order:       f6
    9 W/ Q* i. g, m2 _" g. ]
  46.   Poisoned by user:        f7" i# p2 t. |$ t; N  |' V, t' E* ^
  47.   Container overflow:      fc
    ; y5 A3 y8 j5 M9 H5 X0 Y8 p
  48.   Array cookie:            ac
    0 q0 ?  P5 X" M6 ~$ E) e5 e
  49.   Intra object redzone:    bb
    9 `. J2 [5 ^5 e' R* R) ^5 L
  50.   ASan internal:           fe% C; J% M/ h/ d! `+ x
  51.   Left alloca redzone:     ca! G$ G. k4 C9 L: }$ U: H2 [) l. H! d+ A
  52.   Right alloca redzone:    cb
      s4 h% V' ~; j* k1 s% U
  53. ==282==ABORTING
复制代码

. B: _; ~# X6 ]6 y& A9 I5 |& t; Q" d
Initialization order bugs
示例,这里有两个文件:
  1. # b" }. P2 R, \, x& ~% d
  2. // test_memory1.cc
    4 j  p; N& X7 W7 E, F1 _8 x
  3. #include <stdio.h>
    ; l4 G2 C9 e  K  T  v

  4. # U4 y  q- g8 u
  5. extern int extern_global;$ I+ c+ ]  A8 h
  6. int read_extern_global() { return extern_global; }2 I% o% s8 C! U/ {
  7. ( {8 M/ @. B: H0 L* m0 _
  8. int x = read_extern_global() + 1;
      [5 c  }9 Y  p3 ]. v# S$ X* Y
  9. ) q* {& J- v: _' I# j5 @. D& Q$ D
  10. int main() {+ b6 C3 w: v3 Q/ g/ w& c
  11.     printf("%d\n", x);6 N; ]% r) {4 g0 @4 ~
  12.     return 0;
    ( w' ~) r6 N  F5 m
  13. }
复制代码
  1. " c% o0 q# ]2 B) }0 c; Y
  2. // test_memory2.cc
    4 i2 I- k2 f8 E2 O, ], k8 |- o

  3. 8 U( \3 F* A6 r. e
  4. int foo() { return 123; }
    # _: I8 }( I6 w$ K; Q7 m  |0 l5 X
  5. int extern_global = foo();
复制代码
. _# A- R1 n' a1 F
第一种编译方式输出如下:
; u+ G" [! k/ Z7 Q5 q5 Q* e+ `* V

  1. 8 q4 o$ Q) y# U
  2. g++ test_memory1.cc test_memory2.cc && ./a.out" j6 j: B& t- Y" t
  3. 1
复制代码

  H( f8 K* @% N2 q- y1 A
第二种编译方式输出如下:
  1. ( c  G8 }5 t1 y! h3 v7 C3 i
  2. g++ test_memory2.cc test_memory1.cc && ./a.out
    ) f6 k* b/ Q  ?5 o  b: C: Y
  3. 124
复制代码
1 S6 a* C6 r. j
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:
0 t5 y, `9 K9 L. y7 x
编译and输出:

  1. " y! R* a, c  F9 n
  2. g++ -fsanitize=address -g test_memory1.cc test_memory2.cc$ [" H* o3 z- Q! ~2 \

  3. ( D- X* _( G9 \/ K$ ?4 F. y  m
  4. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out8 K% e9 u5 o& H' O
  5. =================================================================6 k+ B2 V2 y" @0 B/ g; M
  6. ==419==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f46c20021a0 at pc 0x7f46c1e00c28 bp 0x7fffe423d920 sp 0x7fffe423d910& O1 p, v" R* K; c, y7 Y
  7. READ of size 4 at 0x7f46c20021a0 thread T0
    2 x" |3 C; o4 M) L6 R0 T
  8.     #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3
    # l5 P) d- y! ^. O
  9.     #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:41 }. j0 N  m2 [% ~0 v& _+ q
  10.     #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8
    ' e, a! m0 d7 k( _2 \
  11.     #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
    ) C, }3 D3 H. ?( P
  12.     #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)" j: m8 D" ]; P; H: d9 C
  13.     #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09)4 i. \, c: f# Q, P2 C% [2 q& |: I
  14. 3 ?2 V* V0 w% q
  15. 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
  16.   registered at:. y  h0 u% c  ~& v) d" k1 F
  17.     #0 0x7f46c08764a8  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8)
    . [! A. Y9 b5 P, Y7 o
  18.     #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
  19.     #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
  20. 0 v; q  d. x4 p
  21. 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
  22. Shadow bytes around the buggy address:: |$ d. _, K% l: `$ R
  23.   0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    , `, _: W, v+ @' R5 p
  24.   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
  25.   0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    & m) K% [+ k8 b  A; f7 e
  26.   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
  27.   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
  28. =>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
  29.   0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00& |5 ~* z0 v+ ^/ Q
  30.   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 _
  31.   0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00. j. _/ n' a+ `2 n2 t
  32.   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
  33.   0x0fe9583f8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ; v* H$ }! X$ F6 F% N
  34. Shadow byte legend (one shadow byte represents 8 application bytes):+ F6 D% G7 Z0 l, b  x
  35.   Addressable:           00
    9 j* t  E/ G: a1 v1 A: E
  36.   Partially addressable: 01 02 03 04 05 06 071 _2 m  n3 m0 S$ _0 I6 L, }
  37.   Heap left redzone:       fa, Z7 P3 e- ]# s% `6 A% a
  38.   Freed heap region:       fd
    ! z$ {4 p1 s8 b! o
  39.   Stack left redzone:      f1
    $ L; w% \" g+ a) D9 P
  40.   Stack mid redzone:       f2; D8 |/ @! p: s& y7 d; ?5 ^2 R
  41.   Stack right redzone:     f34 P. V2 y" o* m* N' i0 K
  42.   Stack after return:      f5% V& t$ _) Q. i# _2 [
  43.   Stack use after scope:   f8
    ' l  p/ B" z- a* f: I8 M. r3 c
  44.   Global redzone:          f9* w1 F1 G) j1 W! J0 Q) J
  45.   Global init order:       f6
    4 D7 I+ N' R: I  j' X; L
  46.   Poisoned by user:        f7( L- H) o3 U* a; `3 z7 J" H5 G
  47.   Container overflow:      fc! t( C1 x! x* r! Y  ]4 o
  48.   Array cookie:            ac
    / u. t1 M! p- k, c3 K
  49.   Intra object redzone:    bb
    5 f$ n2 L7 Y. m+ [9 k- v+ W8 f
  50.   ASan internal:           fe
    ! l7 i! _) L! e% v& M9 c
  51.   Left alloca redzone:     ca
    8 g4 M1 K$ r& `( a" L  e
  52.   Right alloca redzone:    cb) F0 _, q% x. E- ]: Q/ G  m
  53. ==419==ABORTING
复制代码
$ i, E/ ~! Q  T$ o/ t% ?) l
注意:这里在运行程序前需要添加环境变量:( J. g3 t. O" U; K9 W7 X
  1. ! j4 O; r6 G0 o  L5 c: r  Y$ v
  2. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码
; F7 {; S( N4 E1 P: E
小总结
ASan是个很好的检测内存问题的工具,不需要配置环境,使用还方便,编译时只需要-fsanitize=address -g就可以,运行程序时候可以选择添加对应的ASAN_OPTIONS环境变量就可以检测出很多内存问题。它的错误信息也很有用,明确指出当前是什么类型的内存错误,如:
  • detected memory leaks
  • heap-buffer-overflow
  • stack-buffer-overflow
  • global-buffer-overflow
  • heap-use-after-free
  • initialization-order-fiasco
    $ s# A: A$ ?: l4 _% d

( `" k0 C+ \* Y: \
赞 收藏 评论0 发布时间:2020-10-1 16:06

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版