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

如何排查内存泄漏问题?

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

    % W  _& Q7 y5 w6 p0 H* B! ]8 R
使用方法直接看我下面的代码:
检测内存泄漏
内存泄漏代码:

  1. 6 S. j/ Y- d8 q
  2. #include <stdlib.h>$ U. [6 P7 u& [7 O0 Y9 L( @

  3. 8 Y( c( t8 N+ D' X7 h8 o' I
  4. void func1() { malloc(7); }5 _0 {- w  W/ F3 [$ r- b
  5. & o& m0 A) M  k1 H. x
  6. void func2() { malloc(5); }! |: F, p& D, _3 S  p& V! j4 n4 Y0 n

  7. / u8 G0 K* m9 d3 B7 p# U
  8. int main() {
    6 ]1 ?, c# y* s2 }+ G! f
  9.    func1();$ N. e5 n( Q; v4 b2 Q: U
  10.    func2();
    4 ~! E1 l  u7 M' x* y0 `
  11.    return 0;
    - o2 Q1 [; H5 f: e) C& S
  12. }
复制代码
3 o( `+ p) w2 S! I
编译and输出:

  1. 5 r9 I- f& k3 r( M. ?: c
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    : M' i+ c% R5 U3 P3 H

  3. + a. H! c! H0 K* M! `
  4. =================================================================: T9 ^- K4 |( V4 V
  5. ==103==ERROR: LeakSanitizer: detected memory leaks8 @9 g( U5 d. G8 U% Q9 r

  6. , p, M& d- Y- c& T5 Q3 g$ n
  7. Direct leak of 7 byte(s) in 1 object(s) allocated from:
    9 f" j/ G8 h% B& u) X
  8.    #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40); R" l# R4 `! m" p( d# G7 ~3 R
  9.    #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:3' x$ w; `1 s( P; \
  10.    #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8
      |8 L5 U+ r% W1 E
  11.    #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
  12. 3 l2 r  `! J5 R8 u9 g! ^, F# P
  13. Direct leak of 5 byte(s) in 1 object(s) allocated from:
    5 Y+ n9 ?, X, z: [) |
  14.    #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
  15.    #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:5
    5 h0 ~/ ~' Y' C0 ^  {* g
  16.    #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:90 x" ]  ], z5 ]  K
  17.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    " D% ^7 d$ I2 o, W6 ]$ a: W. J

  18. 1 @% F9 Q; y0 F
  19. SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码
' n  H) b; i7 ~, R: Z
编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。
检测堆栈内存越界访问
示例:

  1. ( v& a3 Q( a5 S3 o( G9 v
  2. #include <iostream>5 }$ H, T! u" b8 ]& j9 V

  3. ) O' `4 N  R" N7 [' k
  4. int main() {6 I  K  s0 L" z* m1 a- D/ }! y; K
  5.    int *array = new int[100];
    2 M$ [/ J1 c% V6 u2 v) F( h- l) p
  6.    array[0] = 0;
    " ], H4 O; Q/ R4 Y2 x, t
  7.    int res = array[100];  // out of bounds
    - F; G% D5 l6 E" e4 y$ t
  8.    delete[] array;7 R5 l* t, Q6 j- D/ i4 e' N
  9.    return res;
    $ Z' D/ c# b: X$ h# j
  10. }
复制代码

9 y) p+ ~8 S3 K4 X
编译and输出:
  T0 q6 `, z; |& F
  1. : n7 @% @/ O. ~  _$ Y. a7 H
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    ; ?; @7 ]  ]$ k' `
  3.    
    1 P; s; o4 u2 t/ V- v. I& I
  4. =================================================================
      v4 o: K; B* _) g+ P' ~9 @4 |; h
  5. ==110==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x7f0e06400d2e bp 0x7ffff5963f10 sp 0x7ffff5963f00
    ; Z0 C7 M4 ]4 G5 B5 @
  6. READ of size 4 at 0x6140000001d0 thread T0
    " }* b+ M  _* d" ]3 n$ \
  7.    #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:6
    " K% g! Y' l6 Y7 i
  8.    #1 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    * X; _2 H" E* F9 W1 F
  9.    #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)
    # w+ f) L. M( d" ?2 b* r# d

  10. * c. ~4 F& `8 P' \" J& X
  11. 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0)" m8 Y4 U6 S7 p% v6 b( h: c
  12. allocated by thread T0 here:
    & N* @6 |# G& V/ ^! U$ g; B
  13.    #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)
    / Q  j/ q6 s% o- f! r  _
  14.    #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:48 c- p0 b! s$ R
  15.    #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    3 x7 a/ Q5 d8 ^' I1 R3 x+ g

  16. ' S: l! O# I" G4 {
  17. SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
      L1 z7 `% T6 c0 B
  18. Shadow bytes around the buggy address:
    % f; \! s% Y0 ~8 A
  19. 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
  20. 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 002 s2 ]: d. ~1 R/ r3 R3 ~
  21. 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00( i1 X4 l& h+ L8 |0 `
  22. 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 004 Y9 t/ E; s7 X: W) ]  q
  23. 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00' y' \$ g* M1 E" c" Z
  24. =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa6 G7 i7 ]+ v& N7 i
  25. 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
  26. 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa" x* |4 F# y8 A6 E: a
  27. 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; ~
  28. 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    + W$ B/ W" ~, [; p
  29. 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
  30. Shadow byte legend (one shadow byte represents 8 application bytes):
    - A5 _/ P. B& E
  31. Addressable:           00
    2 y3 n8 h2 w) u( V& r0 i
  32. Partially addressable: 01 02 03 04 05 06 07* u& a2 E( s9 c( n! b
  33. Heap left redzone:       fa
    + X$ `: G) g4 _  h" ^; m" F
  34. Freed heap region:       fd. m) X8 J8 @1 U
  35. Stack left redzone:      f19 t: D% I) j& z2 |: r& V9 C
  36. Stack mid redzone:       f2
    3 M6 h$ s# @$ K
  37. Stack right redzone:     f34 e  Q/ q+ b5 h/ _5 Y$ f- j
  38. Stack after return:      f5
    6 k; [8 Y, y" s5 ?
  39. Stack use after scope:   f8
    & z1 M" c+ y6 d; B1 k
  40. Global redzone:          f9
    % O  `5 j- I2 o$ L
  41. Global init order:       f6& ?- U; r1 Z; u
  42. Poisoned by user:        f7- ^% R8 O: g. b' ~) m, `
  43. Container overflow:      fc; q4 L5 F+ G7 u! `3 c
  44. Array cookie:            ac
    6 ~. W8 A1 |: @, j1 V
  45. Intra object redzone:    bb/ F$ p8 C$ J5 t/ e# A( c% t
  46. ASan internal:           fe; K' d+ L7 ~; O5 M5 y$ C5 ]7 ~# U) N
  47. Left alloca redzone:     ca
    9 O5 O+ x" v6 M4 B2 j
  48. Right alloca redzone:    cb
    1 X! ~2 j9 q: ]# ], I
  49. ==110==ABORTING
复制代码
7 [& P: {8 Y8 X! r: ]
可以方便定位到堆栈内存越界访问的错误。
全局内存越界访问:
示例:

  1. ( `1 o* a7 {/ }, d' d* ]
  2. #include <iostream>' L3 l* m8 k3 {* v7 @$ `! ^, o* F

  3. 0 D# T2 o1 J, ]4 M" h' d" x* V
  4. int global_array[100] = {0};
    2 L" K" \, I+ u

  5. : [) ]7 u- c* f( V, B  q
  6. int main() {
    ! z3 a- ^# U& W% ~
  7.    int res = global_array[100];  // out of bounds
    0 R9 w, R3 W3 W% ~9 W( v* T
  8.    return 0;
    ; p( q) F, N) f& F1 Q5 d; C0 M; e
  9. }
复制代码

3 M0 Z& ]/ Y; j# L( y
编译and输出:+ P8 k% i( l2 l- y' ]5 ~

  1. / |, R6 m0 g* Q7 e
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    6 G8 s$ e, ~; I5 B$ B) o6 _7 ~
  3. =================================================================5 A9 u" U% v  F  X$ S' h) x/ O
  4. ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda527702 I* ]2 A+ a8 H" i% R
  5. READ of size 4 at 0x7f42e6e02310 thread T0
    * B$ P- ~5 O! d* l
  6.    #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:67 s2 H9 D( v2 |& Q
  7.    #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)6 y6 y1 q: g$ Z( ^
  8.    #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69): ?7 Q3 W- ]8 ]6 U  N
  9. 5 P, s' z  I2 S8 @& b
  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. S7 W* }  a0 r2 H. v) a0 {& N9 W
  11. SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main' i: u( @* \# t0 R% D! Q+ r
  12. Shadow bytes around the buggy address:
      T+ ^6 u# L) J( ]& s
  13. 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; G4 X3 |' I# e6 C. g
  14. 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
  15. 0x0fe8dcdb8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00! H8 `: \! }9 e1 U
  16. 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    " k: Y) C1 \9 w. O) Y# }
  17. 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 002 E, A+ N' ^. `; o. M
  18. =>0x0fe8dcdb8460: 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00) Z* Y3 @) X  R. s
  19. 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
  20. 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ! B9 \# t. |2 ]
  21. 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 \
  22. 0x0fe8dcdb84a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 009 N6 s; A2 ?' `" f, o
  23. 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    & P& m# r: _' ]( i5 x' ?" y
  24. Shadow byte legend (one shadow byte represents 8 application bytes):
    * c4 x) W( x  W6 t+ y" _$ y
  25. Addressable:           00, X8 _% G$ R* T3 A+ [
  26. Partially addressable: 01 02 03 04 05 06 07' {3 d5 e$ _. m9 R/ E
  27. Heap left redzone:       fa
    & r$ }6 |( c' a; g% x/ S
  28. Freed heap region:       fd
    1 y/ g, y4 G4 t0 u/ S
  29. Stack left redzone:      f1
    & h) T4 s' q5 p3 [, W$ Q' S+ _
  30. Stack mid redzone:       f2: x  @& t5 b% w" w
  31. Stack right redzone:     f3
    0 x6 Y" D0 Y# s; }
  32. Stack after return:      f5
    ( R0 R% A! p3 _4 B* B0 V
  33. Stack use after scope:   f8/ c& ~9 ?" E& k1 C
  34. Global redzone:          f97 E' w4 x) M9 G1 @( x* F* |% B4 `4 L
  35. Global init order:       f6
    $ u7 M5 h$ u, F' S3 c
  36. Poisoned by user:        f7
    ) L; K  K' {# q; t5 E
  37. Container overflow:      fc/ l: E" F6 h& o
  38. Array cookie:            ac( u$ U, M( x$ T/ `. _
  39. Intra object redzone:    bb
    5 U! j' m5 e1 z! w; b/ O7 M
  40. ASan internal:           fe
    4 P% t/ s$ t  a$ Q
  41. Left alloca redzone:     ca; t: V: Z7 I( r& ^! e0 j5 h6 F  w( t
  42. Right alloca redzone:    cb- t! j3 f3 J, C' N; ]. n2 P
  43. ==116==ABORTING
复制代码

1 H( I4 }4 {& s1 A3 z$ c
局部内存被外层使用
示例:
  1. ' P! ]  K5 I6 Y  N. Q
  2. #include <iostream>
    9 _, ?2 J& F3 z

  3. : x1 w8 u2 p4 _3 E6 L% ^  g
  4. volatile int *p = 0;8 ?& T4 ~4 i( d- d5 O& K, l9 l+ J
  5.   ~3 i2 X& s/ X2 Y
  6. int main() {
    . J: c) n9 I% F  R5 c
  7. {
      E* |2 o4 m' F. L% E1 ^
  8.    int x = 0;& Q8 p% e" C* B6 Y6 A
  9.    p = &x;/ H: x- C  L, E& x4 H# I$ i
  10. }
      H' C  L- [+ j$ K5 I& ^
  11. *p = 5;
    6 M8 k8 Y; r0 m- M7 g0 B
  12. return 0;
    1 U3 F. j7 t4 H
  13. }
复制代码

3 u3 v+ J- _1 H- M  z
编译and输出:
6 T2 I( u# |) z7 j( E- T
  1. 2 U, l' u( o( W
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out6 r2 @3 \" N. g# c" _9 A# M7 R! R
  3. =================================================================
    . d# A, I4 W: O- r- q5 G# S
  4. ==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
  5. WRITE of size 4 at 0x7fffce12a4b0 thread T0
    9 v" s8 R7 {; U* d# L# C( x5 h9 b
  6.   #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:109 y+ h3 @  j& l0 o. |) _
  7.   #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96); m$ A1 g- I" a+ R5 S! M8 ?
  8.   #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)+ e* M5 b4 }7 H  K4 P2 X

  9. % ]$ M% U& K3 R' n  T
  10. Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame5 Z. }# B- ^$ r$ t4 V# @
  11.   #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5
    ) O5 x+ m2 J/ [9 O7 P# c" `
  12.   K  h  y! t; w- e- T# c1 L+ M, Q
  13. This frame has 1 object(s):
    , @% ?6 A5 S, L( J) R
  14.   [32, 36) 'x' <== Memory access at offset 32 is inside this variable  b/ x! n& w: u+ F7 W+ Y, U) O$ z
  15. HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext& S! [( b2 F+ G7 @' _& m
  16.     (longjmp and C++ exceptions *are* supported)
    ( D8 p2 _  Q* W& p! o1 q
  17. SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main9 T, ~5 [) G, Y7 h) T! B
  18. Shadow bytes around the buggy address:
    7 T0 r6 {# t. l! t
  19. 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* ?
  20. 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
  21. 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
  22. 0x100079c1d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00/ H$ n: D2 k3 u
  23. 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 002 z: a% f% }$ n. |
  24. =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 005 ]6 ^5 S/ L6 ~! @* j% t
  25. 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
  26. 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
  27. 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
  28. 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 007 \( s; w+ \" s; a# G) H) L
  29. 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, l7 u4 F8 i9 Q
  30. Shadow byte legend (one shadow byte represents 8 application bytes):. s% t4 J0 g( C1 I' _, c. o
  31. Addressable:           00
    7 v: Z, [# m- o6 o
  32. Partially addressable: 01 02 03 04 05 06 07; i: N4 }! s" [) e
  33. Heap left redzone:       fa" Q# Y- G4 I+ P
  34. Freed heap region:       fd" g  r( ~7 s) p% e9 J# J
  35. Stack left redzone:     f1
    2 x) k5 H( G: [& \3 u, D
  36. Stack mid redzone:       f2
    ( v) A2 b9 T' l; j0 Q& i
  37. Stack right redzone:     f3
    ) Z2 e  H0 J7 ]: c" N4 F5 O9 g
  38. Stack after return:     f5
    4 o  ]7 ~2 m. ?$ w- m- X
  39. Stack use after scope:   f8
    3 _" ]* f! n$ Y1 N2 D" {
  40. Global redzone:         f9
    / r. {2 S! a: P$ {8 y
  41. Global init order:       f6- b/ s& M- ^8 ]" w/ ?
  42. Poisoned by user:       f78 \5 W# C9 K5 x& w$ d0 X; i
  43. Container overflow:     fc
    % Y4 d7 r4 S0 t* ^: a/ i
  44. Array cookie:           ac) X4 O% u; \6 B& p5 q; y
  45. Intra object redzone:   bb' S+ ?; G: `& ?( s
  46. ASan internal:           fe
    ; J* q4 x; z2 z: t' D
  47. Left alloca redzone:     ca
    ) l" S% g( |/ N0 x& k1 b# s
  48. Right alloca redzone:   cb; D5 t" N3 D9 R" m
  49. ==243==ABORTING
复制代码
4 Y: A+ P- _  i/ N" N
free后被使用
示例:

  1. ( Y9 E+ b7 ~6 `; G/ w& F
  2. #include <iostream>
    9 R5 j; m0 u" @2 S* p8 `2 I

  3. , F3 o, p& t& F7 S# E
  4. int main() {
    7 t0 T0 u! u  }) N" J
  5.     int *array = new int[100];
    : V/ q* X, Z! @# g# E6 W, Y
  6.     delete[] array;
    ; ?! A& W# }6 C& Z
  7.     int a = array[0];  // error! p- q5 C" w& O" v- k
  8.     return 0;
    - J% l. e" O. e2 `1 Z& A
  9. }
复制代码

( k  {. K+ m+ Q
编译and输出:0 V. e8 b) W' Z* F" V
  1. 7 c5 z" X5 t: L, _6 M0 y' i! _
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    2 x$ H0 x' A) l
  3. =================================================================
    ) Q  |: M0 J* y) W: u1 P4 q
  4. ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a15010
    $ F1 W8 f" J% n; H8 W
  5. READ of size 4 at 0x614000000040 thread T0, {0 H5 x% {1 D8 C% Q8 }
  6.     #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:6
    4 M1 G, d) F) g$ t0 E( Q) |
  7.     #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    7 m% `  Y0 g$ e( ~" D
  8.     #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
    % o1 v7 E, \( r1 T2 d
  9. % `( `' q( _. ~" b
  10. 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)  i. L! t1 @  Q( @
  11. freed by thread T0 here:5 L7 `8 \( `0 r9 M: h  T% d
  12.     #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)
    & ]) b! W' {- S4 U. m
  13.     #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:5
    ; w. A4 b3 x' i+ i  q
  14.     #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

  15. 4 k4 i4 \$ g0 |2 N
  16. previously allocated by thread T0 here:+ d. S: D* W: C8 R5 J4 K
  17.     #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)
    8 X% J! ?" H+ }2 ~  v
  18.     #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:4
    " k' w# P, x4 b4 N/ m* {& `4 \
  19.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)+ G1 A+ e& ?1 J- v9 c% m" ]

  20. # |1 c% I0 K2 L  |5 m: X! R
  21. SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main
    6 C4 \; ?& q6 @; ~1 @: R4 {3 C* |4 L
  22. Shadow bytes around the buggy address:2 N/ `& |5 e  \! G. Z& D
  23.   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
  24.   0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 ^' Y5 y' g+ P+ W
  25.   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
  26.   0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    2 [/ x* b& ]8 ^; S% a' N
  27.   0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    . F# f  l5 k8 {' |' y
  28. =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd
    % N" g/ b) |# U$ G9 R
  29.   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 ]
  30.   0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
    + N9 E4 p1 G7 X
  31.   0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
      B1 S/ V7 v' A5 I
  32.   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 |
  33.   0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa, I9 t/ _& ^( _5 m5 M
  34. Shadow byte legend (one shadow byte represents 8 application bytes):
    * _' w2 h' p1 N6 G! }" k2 k* H
  35.   Addressable:           00
    ) G% R! Y& ^, g& _; g
  36.   Partially addressable: 01 02 03 04 05 06 07; N9 c6 }) `5 p8 J3 _/ G& }. U9 y
  37.   Heap left redzone:       fa
    " {2 s- T. _  c& y
  38.   Freed heap region:       fd
    . x$ t3 ~/ T- e2 O0 G& Y9 A
  39.   Stack left redzone:      f18 ?' @3 `/ ^2 r! v5 M( x
  40.   Stack mid redzone:       f2/ v4 n3 K0 X0 o
  41.   Stack right redzone:     f3$ _7 ~& x6 \; U) `3 j8 N
  42.   Stack after return:      f5; D% e/ C# A1 u( y( x% o
  43.   Stack use after scope:   f87 d4 E: R: T" K( ~* s
  44.   Global redzone:          f9& q" Q, p6 E: I3 ]7 x) c1 i1 ^
  45.   Global init order:       f6
    * R6 \. L/ T$ ~: e
  46.   Poisoned by user:        f79 P& K! O! H" p( a+ Q
  47.   Container overflow:      fc
    & Y+ G; V$ K4 n5 |" t7 v
  48.   Array cookie:            ac, [" a& Y% g/ A/ K& p/ k& p* f- Y8 e& j
  49.   Intra object redzone:    bb7 v" L& ^2 }1 f' k5 y
  50.   ASan internal:           fe
    1 t9 x* J& s, a
  51.   Left alloca redzone:     ca% g7 z0 p. D- Q, P. T4 `# x
  52.   Right alloca redzone:    cb
    ! C  r! _  G- z- w: O4 k: u
  53. ==282==ABORTING
复制代码

7 J' v. w$ C& G
Initialization order bugs
示例,这里有两个文件:

  1. 7 i2 b; U3 q6 m# ^% E
  2. // test_memory1.cc) o( ]! j) j9 U, q% F/ }! L
  3. #include <stdio.h>  k" y3 g; E  b) ]

  4. " Y) J* P; h  g& |# m/ u
  5. extern int extern_global;
      W4 P+ t# \" y
  6. int read_extern_global() { return extern_global; }
    . c+ K9 P5 x% Z' B8 v: H0 _

  7. " M) W$ J  i: r
  8. int x = read_extern_global() + 1;
    / D# A& _/ a% U7 z2 Z
  9. + I9 [  Q7 f+ c+ O$ a0 g
  10. int main() {& `5 t0 [, T: K; h  O! x0 U  x9 h
  11.     printf("%d\n", x);
    9 o; m" k0 k; y8 d- g3 F( j5 K! ?
  12.     return 0;" n* z3 Y. R- u: x+ a5 h( \+ y8 y
  13. }
复制代码
  1. 2 n9 L7 K* {2 [! j! m6 [1 b$ q
  2. // test_memory2.cc
    ( v# z% n+ U* a" w
  3. : L: T& E8 Z. {% D% @4 U
  4. int foo() { return 123; }
    9 S+ h" R4 U) {1 e' h
  5. int extern_global = foo();
复制代码

# k0 i8 r2 p4 g7 s2 z) P
第一种编译方式输出如下:% R8 R4 n8 K9 s0 {& A  Q7 \
  1.   G' l# x- {8 v; D. t/ E1 d
  2. g++ test_memory1.cc test_memory2.cc && ./a.out0 E3 H( U. T: S% p
  3. 1
复制代码

1 z' U8 ^4 w8 d) ?/ Z
第二种编译方式输出如下:

  1. 8 |: K0 `" `( N' x" z( u
  2. g++ test_memory2.cc test_memory1.cc && ./a.out$ ?  r' N$ i+ a  U: U, s
  3. 124
复制代码

) W( `$ t# v8 W% d' l- u  c0 K
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:
( }3 K1 l  n& [% m  {$ G9 D
编译and输出:
  1. " [5 I2 Y  N  o- Z4 @: i
  2. g++ -fsanitize=address -g test_memory1.cc test_memory2.cc( \) c) _2 o+ P% d
  3. * i2 D% t. K; x; T( a4 R3 ^
  4. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out2 A7 ]* w, r- j. [
  5. =================================================================
    & w$ }! L% h  ^$ |& \: Q9 y( ^& p
  6. ==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  `
  7. READ of size 4 at 0x7f46c20021a0 thread T0
    * K" ?* X' g) g3 Q& T6 V, x
  8.     #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3
    ( q9 w- _* L- ^, E6 r/ R
  9.     #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:4& U: m1 f3 n4 ]" i; L8 [
  10.     #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8' d3 o; z& M3 N; M* {; B
  11.     #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
    0 N' M, G0 Y3 x9 z" G* T9 ]
  12.     #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)
    / S6 M) @  I9 ?: ?! w, b* l
  13.     #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09)
    # L1 o; D  z9 `, f

  14. 9 i% Y- j& q9 y8 c
  15. 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
  16.   registered at:
    3 a8 {7 k7 V# p# I( i3 ?3 F6 W9 t
  17.     #0 0x7f46c08764a8  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8)
    " Q' K1 {9 h7 o1 ]( x0 t: j8 O
  18.     #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
  19.     #2 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
    ' m7 N1 e2 s8 Z/ b
  20. / |8 }" P; N; \
  21. SUMMARY: AddressSanitizer: initialization-order-fiasco /home/wangzhiqiang/test/test_memory1.cc:3 in read_extern_global()
    : @% }5 c8 w7 Z& {/ {( m- i
  22. Shadow bytes around the buggy address:
    7 u% R; Y$ t( d7 Z4 ]; V9 l6 I
  23.   0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00% N/ |! `" V0 M6 a. D
  24.   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
  25.   0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; K, s# a1 Y" m1 l9 _
  26.   0x0fe9583f8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ ]' Y! h4 M# K, F
  27.   0x0fe9583f8420: 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9 f9# |2 W2 m& v" [1 @
  28. =>0x0fe9583f8430: 00 00 00 00[f6]f6 f6 f6 f6 f6 f6 f6 00 00 00 00  _9 M' |( g9 {" h) U
  29.   0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    9 k7 e+ _' i9 w- R0 ?
  30.   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
  31.   0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    8 e) e% U/ `/ ]3 ^
  32.   0x0fe9583f8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    8 l3 O% `& r1 }; f7 A# t
  33.   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
  34. Shadow byte legend (one shadow byte represents 8 application bytes):
    * h9 b' H* z. D& Q$ `  g! r
  35.   Addressable:           00
    8 A' P3 K$ e% \9 h3 j1 w
  36.   Partially addressable: 01 02 03 04 05 06 07  `+ \" `9 }( W3 l: ?
  37.   Heap left redzone:       fa% }4 G2 }1 i6 y5 [6 L
  38.   Freed heap region:       fd5 E% P4 i' n# m6 Y! H1 c- b
  39.   Stack left redzone:      f1
    / w) }1 f: ~/ f, s' g
  40.   Stack mid redzone:       f2
    & Z4 D* q( u0 M& a
  41.   Stack right redzone:     f3
    5 {* q6 h8 J4 Z$ Z) ]* M* f6 |, t: J
  42.   Stack after return:      f5
    ) F6 t. \! S, B. ~$ V9 }$ ?# \8 ]
  43.   Stack use after scope:   f8
    . _4 ?" f" ~* x" W+ S. K1 ~
  44.   Global redzone:          f95 Y$ W/ s" _% W( C8 m1 h9 |8 l
  45.   Global init order:       f6  M4 g7 i+ S2 U2 C; @5 r; G4 F. v8 i6 U6 V
  46.   Poisoned by user:        f7
    , k1 r# s5 i+ y/ i3 C2 C5 K/ C
  47.   Container overflow:      fc" s9 c& ^( i9 ]4 j2 I, `, s
  48.   Array cookie:            ac8 I) L" ^5 T: B2 j2 [' M
  49.   Intra object redzone:    bb
    ! w2 G# j8 @* c1 x# _
  50.   ASan internal:           fe
    ' {; e: |" ~  `% A6 j
  51.   Left alloca redzone:     ca
    ' b, b& L4 d5 d; ~9 I5 W" ^
  52.   Right alloca redzone:    cb
    ' R7 E9 r0 |& I7 s1 h
  53. ==419==ABORTING
复制代码
% j! B+ x! Y, M3 d! m
注意:这里在运行程序前需要添加环境变量:; g5 T8 {3 _# ?% G' ?% P, U

  1. 5 f# z- C0 ^2 ?
  2. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码

8 G! t$ p& Q0 y, ?* O
小总结
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
    # Z* P4 H5 F! A

% r( q: c; l2 x" |& H5 W! m+ I& l
赞 收藏 评论0 发布时间:2020-10-1 16:06

举报

0个回答

所属标签

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