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

如何排查内存泄漏问题?

[复制链接]
gaosmile 发布时间:2020-10-1 16:06
内存泄漏是指由于疏忽或错误造成程序未能释放已经不再使用的内存。内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制,从而造成了内存的浪费。
) p0 _4 t$ g  c) ]
我们平时开发过程中不可避免的会遇到内存泄漏问题,你是如何排查的呢?估计你是使用下面这几个工具吧?
  • valgrind
  • mtrace
  • dmalloc
  • ccmalloc
  • memwatch
  • debug_new

    $ ^8 o9 N3 e7 Y# C% M
这里程序喵向大家推荐新的一个排查内存泄漏的工具:AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本都可以使用,支持Linux、OS、Android等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有:
  • 内存泄漏
  • 堆栈和全局内存越界访问
  • free后继续使用
  • 局部内存被外层使用
  • Initialization order bugs(中文不知道怎么翻译才好,后面有代码举例,重要)
    2 b/ c/ q5 D/ @' {2 n) w0 U
使用方法直接看我下面的代码:
检测内存泄漏
内存泄漏代码:
  1. 5 \/ y/ k2 a+ E4 F: p6 U
  2. #include <stdlib.h>* p1 I" n" _& }: k  x

  3. " n* U1 F  z/ ]8 D
  4. void func1() { malloc(7); }. j1 d+ M5 @# H; O4 g

  5. : r; G1 E  B& W  G: @/ S) m! |
  6. void func2() { malloc(5); }
    $ x* Y! s( `* e5 _: X! i$ O+ m# u

  7. 0 l% F  Q2 h, o7 C- S/ p
  8. int main() {
    , b4 v* J, F: f
  9.    func1();% Z6 C& j- y5 r0 z
  10.    func2();
    9 j8 O0 S& x; X/ L7 }8 [. @
  11.    return 0;
    * Q4 u  w$ {5 L% u2 g
  12. }
复制代码

8 @3 D' h: P1 L" A9 ~0 {" Z" U$ E
编译and输出:
  1. # B' X, T9 N* x9 s
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out* t( w4 v& g3 N0 `' u" K* q- J! D

  3. 7 Q* |2 G/ ]- @! c3 j8 ?
  4. =================================================================
    , f2 z9 G( j' Z  b$ O
  5. ==103==ERROR: LeakSanitizer: detected memory leaks6 u+ k- g; j& S
  6. * ?. i2 ?) i3 h$ c, Q
  7. Direct leak of 7 byte(s) in 1 object(s) allocated from:
    & h: _5 F; S+ }- l* b" Z
  8.    #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)& _9 C  ]" S3 B2 y3 }( ~4 q
  9.    #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:3
    : K) m9 h' B$ l  }: v
  10.    #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8. f) [& @4 J- e3 |$ d
  11.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)) |' [  q' z$ o
  12. ; r) M* W( M5 C: v
  13. Direct leak of 5 byte(s) in 1 object(s) allocated from:. Q) e/ O  f) W9 ?1 X; V- _  x; R
  14.    #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
    5 X: ^( S$ {( \
  15.    #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:5
      a5 v, V* S+ s3 b
  16.    #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:9
    * _# |; w9 X0 L# Z) b
  17.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    * h" m9 g! A. Q8 w6 o/ [# p+ W
  18. ( ~: I' ^  c7 C3 u* i
  19. SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码

# Q+ w; K, y5 Y6 M/ V
编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。
检测堆栈内存越界访问
示例:
  1. ' Y6 N* ^9 y+ I; Z
  2. #include <iostream>/ k9 O( I1 W$ k1 m
  3. ( B) y" H4 Y2 F* T" l* B
  4. int main() {
      s( G# e; g* D' b2 v
  5.    int *array = new int[100];; |) J" I; g0 F( s# \+ k
  6.    array[0] = 0;+ {$ H' d; v. S# L/ G" D  s1 K
  7.    int res = array[100];  // out of bounds0 S& i; |& `$ f" r+ n& c) `
  8.    delete[] array;/ p+ O7 Q9 v4 c. _
  9.    return res;) ]& S4 r" f- o- S
  10. }
复制代码
  r; o* S7 d5 x2 D
编译and输出:0 T; c1 M( H* J: x
  1. / q. W/ M% A4 w- W, H7 m# a) P( V
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    ) n$ g+ ^+ k# |' G
  3.    
    : t8 }( _& z* M" S7 q
  4. =================================================================& w+ R) E$ H6 w! m. X
  5. ==110==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x7f0e06400d2e bp 0x7ffff5963f10 sp 0x7ffff5963f00
    ; b$ ]6 r7 f  G+ j5 Z
  6. READ of size 4 at 0x6140000001d0 thread T0( v5 D* t7 h4 Y
  7.    #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:6
    & j& u) q( n5 a0 T6 d) O6 i
  8.    #1 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)" m- h3 B7 |1 T! l' c  _6 n+ ]
  9.    #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)9 M( N4 M! {& W& W" h" G
  10. 9 J+ [0 [% m) F1 l) F
  11. 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0); O. n% T4 p% u  d( z
  12. allocated by thread T0 here:3 N% \3 k! `- w. g) ?
  13.    #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608). J# X, K8 q3 [  _
  14.    #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:4! U% L1 D4 P- E4 R" k+ Q- Y
  15.    #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    , [( w( X, u2 r
  16. / K6 {/ K6 I$ P, z$ y$ {3 k
  17. SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
    ! y7 j! |+ ^% J( _6 m$ r8 M
  18. Shadow bytes around the buggy address:2 P7 Z4 n5 B0 _0 _
  19. 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00% t2 ?; u- d* q7 i* Y0 K% `) y
  20. 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    3 `! o) @6 h' T3 _3 p; p$ p
  21. 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
    $ W  T2 x% ?! P1 {. Q
  22. 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 003 S2 c3 u/ F  h; [: v3 W, M. j6 r$ b, R
  23. 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ m. x: M6 d( K
  24. =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa1 \/ `. x5 y+ C9 ~/ n9 ^
  25. 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa( y! K& k5 P$ B* h6 t
  26. 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa/ p& Y& X# G. n8 p; L) X8 ~/ V
  27. 0x0c287fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa: B, C; w- p& o
  28. 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
      q! E8 P1 N1 h: N4 D( e# ^+ r
  29. 0x0c287fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    9 ?' `1 `8 g( _8 g
  30. Shadow byte legend (one shadow byte represents 8 application bytes):% K1 r5 @  ?3 N; g* V* ^! Z4 T2 I
  31. Addressable:           00
    % D& d( d; W/ s9 B) t4 N3 w0 o
  32. Partially addressable: 01 02 03 04 05 06 074 w! y6 ]' R5 ~
  33. Heap left redzone:       fa
    - o3 \' W9 C6 u2 h' V( H- Z2 }7 R
  34. Freed heap region:       fd
    * Q$ y3 c0 L; \& O
  35. Stack left redzone:      f1
    5 v+ H: A, J1 t2 A, d& Z) _1 A5 ~
  36. Stack mid redzone:       f2
    ; w# ?6 x  j8 Z; Z
  37. Stack right redzone:     f3. Z8 `+ A( |4 e/ p. w; y4 z( c7 C" V  F
  38. Stack after return:      f5
    : D: _% n5 `+ f6 j
  39. Stack use after scope:   f8
    : j, \/ [: F$ P8 U& V# p% A
  40. Global redzone:          f9( I- L* ]- C0 L8 g2 D, @
  41. Global init order:       f6
    5 ~. W* h! ?. U& \: F1 r- [, s  m
  42. Poisoned by user:        f7" Q+ b6 o$ |; W5 y  i) x
  43. Container overflow:      fc
    8 h9 H8 I% G0 F! C" ?3 _
  44. Array cookie:            ac
    $ N7 [7 B2 @/ X' O" C: d
  45. Intra object redzone:    bb
    # r7 s9 j( p0 [5 b
  46. ASan internal:           fe
    8 v! V( v/ \( h; Z% ?2 m7 ^5 p+ P* J
  47. Left alloca redzone:     ca
    1 H# p* I1 x& r" g+ A; w# ]0 A  |
  48. Right alloca redzone:    cb
    , u! t  o4 n3 @/ Z
  49. ==110==ABORTING
复制代码

) R$ u# e8 d: G# E, O( N* I
可以方便定位到堆栈内存越界访问的错误。
全局内存越界访问
示例:
  1. 3 Q  Y% Z$ ~: {" `0 W3 R. R
  2. #include <iostream>
    6 t, Q) V$ |$ D  M

  3. # W; {! K' m# }: M3 y" o8 k
  4. int global_array[100] = {0};
    " q! _8 A9 A7 ]$ C1 C
  5. 1 {& q! W7 \2 r$ i% D
  6. int main() {
    2 F1 t! h* C6 E$ D! b
  7.    int res = global_array[100];  // out of bounds! t6 i, k- Z" X8 e' ^% Y; L
  8.    return 0;  x1 w* T. x/ u& `& u
  9. }
复制代码
& k* |, E' m: Z' a0 V
编译and输出:
( t1 d5 n1 `9 G' q" C
  1. : q' v1 c1 V' C6 K4 X
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    1 h" c' O9 a% z0 q. s3 B% F  _. N
  3. =================================================================
    " ~" a8 d1 @2 V
  4. ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda52770
      i& H0 t, `. \5 `
  5. READ of size 4 at 0x7f42e6e02310 thread T0
    ! d# u1 _* _# z, ~1 J1 w4 L# O! s9 n
  6.    #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:6
    ; p" j. H( |2 Z5 e
  7.    #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    % J$ _" m6 o& o! [  n  X
  8.    #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
    ! C6 A$ D% [) ~& V7 a3 Z6 Q1 t1 |

  9. 8 M! D) K6 A6 a
  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. C; g, m5 ~3 ?. e7 l
  11. SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main( A' I1 `. ?9 p3 Y
  12. Shadow bytes around the buggy address:4 k$ r+ |- y: B6 E, b/ {
  13. 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 003 u8 K( R- _1 y9 Q
  14. 0x0fe8dcdb8420: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f9
    3 B) L6 f) s' r3 ?1 [- ?$ g7 L3 y
  15. 0x0fe8dcdb8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ; G! Z3 j$ F, J( N$ M/ f
  16. 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00. K/ `. m* y: f; c7 M. v$ r) `
  17. 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    4 x& G7 i. F+ y& |
  18. =>0x0fe8dcdb8460: 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
    8 ]9 `: Y0 A' T6 g
  19. 0x0fe8dcdb8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    1 A! |; _2 D$ ]/ x9 R# Q
  20. 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, S0 }( b: t1 w  m% L
  21. 0x0fe8dcdb8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00( q/ T$ }( p' b
  22. 0x0fe8dcdb84a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    9 M7 A  U& S( H$ E( Z5 E- }# X
  23. 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0 l, b( S8 Q- K! d7 a6 a  U
  24. Shadow byte legend (one shadow byte represents 8 application bytes):
    0 j! _7 b+ l; a) s
  25. Addressable:           00+ l3 q  n; c. Z( e/ k9 g  w: v2 k( c
  26. Partially addressable: 01 02 03 04 05 06 07- L$ R; |, y) D# F/ u7 h6 |& N
  27. Heap left redzone:       fa
    4 F1 x! l2 Q- g
  28. Freed heap region:       fd1 T: Q( e( W! B' \
  29. Stack left redzone:      f1
    : M% H5 P2 t9 @5 u
  30. Stack mid redzone:       f2
    # _$ e4 T" h4 d3 s  e- W3 x$ l
  31. Stack right redzone:     f3  k, m1 j9 d1 r% }; M& k. {+ l
  32. Stack after return:      f5
    ( z/ f' U1 \7 |( M' M8 q* g
  33. Stack use after scope:   f8
    + x% {; N& v3 T- |5 G! O3 O* e3 |
  34. Global redzone:          f9! s  K; l' [8 ]  D8 y+ k% P2 o1 U
  35. Global init order:       f6
    : w+ |: q1 \# N  V) v6 J. C! Y
  36. Poisoned by user:        f7, ~. }  e2 o; J8 a, ~8 d
  37. Container overflow:      fc
    $ g4 ?! R- h8 w( ~  `/ U' X. W* U
  38. Array cookie:            ac
    + j  h9 o; `' ^% Z  M
  39. Intra object redzone:    bb
    " S7 q9 g# \$ `. e
  40. ASan internal:           fe$ t4 E- H/ _7 {/ M0 z2 B: \
  41. Left alloca redzone:     ca
    7 l: u3 T* q/ h6 x) g$ U
  42. Right alloca redzone:    cb
    0 B- z: c6 Q; E7 L- H: j
  43. ==116==ABORTING
复制代码

& @* B7 q) i" d3 r6 f0 a
局部内存被外层使用
示例:

  1. - e0 ~/ |4 R/ [+ w; P5 E7 b) W
  2. #include <iostream>* M" a3 J! f; M* L* R  L

  3. 4 n+ G/ B% @8 q# `) W' m0 J
  4. volatile int *p = 0;
    6 D# _4 p; r, J: L: z# j
  5. ! A: F  b, h5 ]8 N6 a/ C2 Q6 Q6 D
  6. int main() {
    , W: s; K- I1 }" D8 w3 X, B
  7. {( K" ~5 n; Z3 F, r8 q
  8.    int x = 0;
    - D: w' f# v. C
  9.    p = &x;
    . e+ p( J: O! Z. e, G! ~
  10. }
    # R' n+ t0 E8 r
  11. *p = 5;6 K6 Q. K! k; K1 f. \, h9 S7 q3 j! ?$ q
  12. return 0;( ^* _8 c$ H; F1 P$ g
  13. }
复制代码
/ f) o% r& p& b9 m* Y
编译and输出:) }- `$ V3 C6 f3 k! j7 h1 ?
  1. 4 J3 ^5 v: W  I; N; {. u
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out: X' r& y7 A" r& K
  3. =================================================================
    - S$ x. e7 X1 f/ C
  4. ==243==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fffce12a4b0 at pc 0x7f3993e00e7e bp 0x7fffce12a480 sp 0x7fffce12a470
    " Q' f: l* E$ E7 k  a
  5. WRITE of size 4 at 0x7fffce12a4b0 thread T08 ?  }; a9 `& u4 j
  6.   #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:10  j1 c; j' f0 H: G% P
  7.   #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)4 U8 @. s# A" d
  8.   #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)
    ! ]% y- A1 P. J8 G

  9. : @, F( j! H$ s9 Q
  10. Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame
      B% o. Q* }1 }" t2 u. w! S
  11.   #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5! O$ G6 X+ D% \9 _0 B/ H7 }5 [
  12. ! S) Q6 y6 q9 n3 L2 ?" H
  13. This frame has 1 object(s):
    % O; p, U% q; ]  J0 `3 T" V: ^
  14.   [32, 36) 'x' <== Memory access at offset 32 is inside this variable
    1 D- N  H* H4 d$ V% s) j, Z. A
  15. HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
    5 I. P( g+ e* f% w' j% e5 U
  16.     (longjmp and C++ exceptions *are* supported)
    / @$ Q- G, v1 \: t1 G) y
  17. SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main. k5 j, G5 x$ G0 ~  o' v, s
  18. Shadow bytes around the buggy address:% W- ~8 @' k& P9 h) Y/ a% [, ~
  19. 0x100079c1d440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00& S% o/ b3 g& e2 J6 x
  20. 0x100079c1d450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ! c1 j/ J5 H1 _
  21. 0x100079c1d460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ; B; ^! ~  n5 Y- _- `
  22. 0x100079c1d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    + n! U3 Z2 ]( c" b0 c2 S* `
  23. 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 009 s, O, c$ T' w" T. u0 @
  24. =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 00
    / ^( l- a( b; L- J4 k
  25. 0x100079c1d4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00& ^$ A7 ^  b9 g  n/ p
  26. 0x100079c1d4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    $ o3 q5 p9 j1 r1 T
  27. 0x100079c1d4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    9 G( S: c0 L, k- i9 o8 f
  28. 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    " ~# {% C/ I' w9 Q$ g- x
  29. 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 003 n6 ?. [, _2 P' p. G9 j
  30. Shadow byte legend (one shadow byte represents 8 application bytes):. G, K. M/ A/ a' l5 C+ Z& s7 \
  31. Addressable:           00
    1 p2 [% z- Q1 O- b3 _3 D
  32. Partially addressable: 01 02 03 04 05 06 07" r4 H, `" k# f; }2 m! w
  33. Heap left redzone:       fa# c1 R) b3 V' Z/ i1 `; O, a
  34. Freed heap region:       fd
    7 z" G' _' \1 N, @: O% A
  35. Stack left redzone:     f1
    ! c6 A. Y! o* b6 @
  36. Stack mid redzone:       f2
    7 V, i4 y6 m; U. n& q( h, R
  37. Stack right redzone:     f3
    / J1 C$ k+ ?  X7 e3 l. \
  38. Stack after return:     f5
    $ ~1 s* H% g  F! w& n" a
  39. Stack use after scope:   f8
    . u! z! E% a$ H: R7 m" o. x
  40. Global redzone:         f9
    0 [0 A* T- m1 N2 U' K
  41. Global init order:       f6
    3 G& ]9 {/ t+ Y( r! Q* C
  42. Poisoned by user:       f7; q8 F- O, O3 ?* v: L
  43. Container overflow:     fc
    6 J2 @3 _3 f7 N4 t; x
  44. Array cookie:           ac
    9 X; l) i7 D+ T4 Y
  45. Intra object redzone:   bb1 w* D( S% ?$ J3 n2 F, \+ |  `
  46. ASan internal:           fe; |$ G2 @3 a2 b. c
  47. Left alloca redzone:     ca. T! U& H( h* x5 d
  48. Right alloca redzone:   cb
    " ?, y" t1 M; n0 J2 d
  49. ==243==ABORTING
复制代码
9 A+ m* j' p4 d2 P2 ~; B+ o
free后被使用
示例:
  1. : `, k- }1 l+ I! W. V) H9 A
  2. #include <iostream>* w4 o6 W1 ]/ T. c( ?

  3. 6 X; @5 Y# m! p9 I
  4. int main() {
    $ s. x9 a) Q2 }8 e5 P* M1 E
  5.     int *array = new int[100];6 N: l' r; f; Z3 q* k5 [# H9 N* W* V
  6.     delete[] array;
    - z5 U3 f" d) k2 S. d+ l
  7.     int a = array[0];  // error/ e/ B' v$ O+ ]: I; v
  8.     return 0;- L3 a$ w# U7 V+ R( A
  9. }
复制代码

8 E( w, m- ]$ W/ o( u. e# [
编译and输出:
5 c& z5 j4 y2 I: y: \" |
  1. / N: O, n: y6 W7 f" G
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    , g0 G9 V4 V) p( x% E) I* c! Q2 x
  3. =================================================================
    ' x4 H. d, [& p! v8 O, S
  4. ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a150108 X( b& _$ }0 [: _, p
  5. READ of size 4 at 0x614000000040 thread T0
    # B2 }5 H) {! b! z3 G
  6.     #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:6
    5 b6 {6 W# {' h, L
  7.     #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    ( [  B: d2 S: A+ W
  8.     #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
    8 [1 f2 P/ S( W% w+ Y

  9. ; |1 `- Z" n- J: G# X8 I+ A; G
  10. 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)# I* M7 X/ e3 _0 T( g3 ]% V- h9 K) f& b
  11. freed by thread T0 here:1 {: W- e  T/ Z
  12.     #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)
      @# r0 R. `- h3 b
  13.     #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:5/ l7 H. q  z- }! C& e! H. Q: Q' Z
  14.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    ; c2 n7 n! _4 v* E
  15. 8 Q) X' W  o: n
  16. previously allocated by thread T0 here:
    ) [7 B; V9 l2 }! `6 c
  17.     #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)
    . R  `) a# ?" w, K* X# |. z
  18.     #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:4
    4 T% U8 H/ n- l7 S: j
  19.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    / {7 S" I# v2 ?4 `4 z/ N" n
  20. 5 E: {# h/ D/ W* @
  21. SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main) v9 M1 T2 ~" T# b2 Z5 G+ U
  22. Shadow bytes around the buggy address:
    2 H, c9 Z* Y6 i) n' ?7 L+ l1 ^
  23.   0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 004 X' H# Y# e! x& W/ S6 n+ z. _
  24.   0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 008 s, a, ?8 H$ ]' q; l2 }& X) E
  25.   0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    8 _2 R( S$ r$ E: G
  26.   0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00* c& B' u. |/ K2 L5 ?& H( X
  27.   0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00! r/ B" O& ~7 m3 e" T
  28. =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd& R1 U- F8 O2 `6 ?" b: n3 b, U+ @
  29.   0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd$ F* A0 q# `; C8 s. t
  30.   0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd( A0 i8 N5 s: r% X1 X* Q! E' e
  31.   0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
    / l6 v3 c& O* T& h' P9 D
  32.   0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa0 @- \' }0 Y1 V" _3 [1 X
  33.   0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    * q. C8 H7 R. y* g- h/ }
  34. Shadow byte legend (one shadow byte represents 8 application bytes):8 c4 f# E/ N$ e4 B2 v; L) W
  35.   Addressable:           00
    ' B5 F8 Z# X; x
  36.   Partially addressable: 01 02 03 04 05 06 07! O, p2 ]# l' r& X7 ?- m, c
  37.   Heap left redzone:       fa
    . j( L! `( K% N( x, r$ ^
  38.   Freed heap region:       fd
    ) B0 K$ l* v$ t' I( \9 t
  39.   Stack left redzone:      f1
    3 p/ P" z; p' {. C
  40.   Stack mid redzone:       f2
    " E6 b9 M( ^) Q4 L' j4 y6 h
  41.   Stack right redzone:     f32 j& J. R# A0 K* C7 |
  42.   Stack after return:      f5& h& h5 d! F! H# \6 V$ ^
  43.   Stack use after scope:   f8
    ( S0 w) E! q/ G2 N) @, p  m$ J
  44.   Global redzone:          f9) N4 z( n0 }4 q
  45.   Global init order:       f68 j) k, L7 n5 [5 X0 p
  46.   Poisoned by user:        f7. g7 c: I& h0 `
  47.   Container overflow:      fc: m( Q# }2 D# l: o& G
  48.   Array cookie:            ac
    : j# Q' F! \( d. ~- M
  49.   Intra object redzone:    bb+ @! x3 F9 O( t* A
  50.   ASan internal:           fe
    - L( q# G7 p! d# z2 R" k, T% Q
  51.   Left alloca redzone:     ca3 s" l. g2 O0 J( T0 G7 Q- I
  52.   Right alloca redzone:    cb6 }8 Z! Q- ^. B( U# x5 U
  53. ==282==ABORTING
复制代码

* C; f' s) f% @9 l2 Q
Initialization order bugs
示例,这里有两个文件:
  1. 4 P- {6 ^( J4 |9 J3 U3 h
  2. // test_memory1.cc
    0 ]: d' e" w/ ?( `
  3. #include <stdio.h>
    3 a: q/ b* b7 _( Y+ D/ M+ S
  4. & s. J0 G9 R* ]
  5. extern int extern_global;
    # M+ U9 q- O% |" A. p+ }7 p
  6. int read_extern_global() { return extern_global; }
    . G, A. y4 Z* s2 |6 y& [) W
  7. ! v6 c  B- V: v: g( x
  8. int x = read_extern_global() + 1;9 _5 \9 M7 ^$ k8 ~! O- i$ A

  9. / N+ K) T0 G6 U3 c* F  b+ [
  10. int main() {
    2 r; J/ l! Z, U, E. L1 ]
  11.     printf("%d\n", x);8 V5 S9 P: z* {( F
  12.     return 0;" W  |* ^, c# a; u. r+ D
  13. }
复制代码
  1. 0 e* x2 c  ?& U- a
  2. // test_memory2.cc
    . \" ]8 Y9 q1 f
  3. ; p* O5 w: M. a6 O% z- V( F
  4. int foo() { return 123; }1 J) G5 ]. {, Q$ ]. a! T' x. V
  5. int extern_global = foo();
复制代码

& Q' b7 F7 d3 E. d$ R' P
第一种编译方式输出如下:" l. _! O; ]; P  G* R5 n7 I

  1. ' b) D5 i4 I7 H: w) a
  2. g++ test_memory1.cc test_memory2.cc && ./a.out6 ?0 S/ ^" A! D8 Z2 U
  3. 1
复制代码

& Z& E* F' h/ i4 C. M
第二种编译方式输出如下:

  1. - F& ?$ r* Y' T- j6 U8 m& k
  2. g++ test_memory2.cc test_memory1.cc && ./a.out$ |  ]) R# [+ S' L- [' |
  3. 124
复制代码

& y- L* \: }  B
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:
3 t4 E9 p1 G' K1 ]0 R9 Q* V. X
编译and输出:

  1. ( C0 B3 Y' E& I3 I
  2. g++ -fsanitize=address -g test_memory1.cc test_memory2.cc
    ( k1 n- n$ F$ p1 L: m5 D8 v" \
  3.   N- @$ ~: \- J7 A
  4. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out$ m& B! g% [$ C- O
  5. =================================================================- a3 h( v2 w% X$ A4 A
  6. ==419==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f46c20021a0 at pc 0x7f46c1e00c28 bp 0x7fffe423d920 sp 0x7fffe423d9104 D) W# l& w1 t& W: B2 H
  7. READ of size 4 at 0x7f46c20021a0 thread T0
    + Y. X1 b) s: T- \- R: n
  8.     #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3
    4 g3 f/ x; l+ N8 p# u
  9.     #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:40 n& M* C5 O$ D7 F. d5 t
  10.     #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8
    # _' k/ b" j; U) |0 `( Y1 U0 a! y. J
  11.     #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)4 H, a2 y, ?% B$ A9 g
  12.     #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)* D& E' p# u' Q1 l0 Y1 ^# }2 n0 D; N: W. c
  13.     #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09): B8 M0 v% x3 E# m
  14. 6 v) g) R5 C7 W1 j! }' B+ ^
  15. 0x7f46c20021a0 is located 0 bytes inside of global variable 'extern_global' defined in 'test_memory2.cc:2:5' (0x7f46c20021a0) of size 4" ^" q8 B* ~* q
  16.   registered at:
    4 |6 j1 E3 X8 G8 f' k/ C  t9 d% X
  17.     #0 0x7f46c08764a8  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8)4 z* V& d! w& s3 M) }
  18.     #1 0x7f46c1e00e0b in _GLOBAL__sub_I_00099_1__Z3foov (/mnt/d/wzq/wzq/util/test/a.out+0xe0b)1 P0 n  e- B+ r2 I6 G. Y: U, ]" u
  19.     #2 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
    ! z5 N/ [- a1 y
  20. 9 Y+ f+ w3 r, S8 ]$ M
  21. SUMMARY: AddressSanitizer: initialization-order-fiasco /home/wangzhiqiang/test/test_memory1.cc:3 in read_extern_global()
    2 ^7 a$ k* Y3 P$ K( n
  22. Shadow bytes around the buggy address:! j4 s% n% z4 l  Q9 c, `( k7 ^
  23.   0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    * @3 X2 E) T; X
  24.   0x0fe9583f83f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    : ]0 T+ K, T6 G
  25.   0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
      K6 x* k3 V1 W
  26.   0x0fe9583f8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0 C" Z# m5 v3 Y
  27.   0x0fe9583f8420: 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9 f9
    ( p+ o, d( V9 k+ f
  28. =>0x0fe9583f8430: 00 00 00 00[f6]f6 f6 f6 f6 f6 f6 f6 00 00 00 00
    # C3 J; l: `7 m# y
  29.   0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, j; c7 t: K9 K1 g1 w& a
  30.   0x0fe9583f8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 U4 n1 Z  g, D% k" T: ~) X
  31.   0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    + r( o  A7 C* a- p9 r
  32.   0x0fe9583f8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 004 P! V& {2 k2 P, R% y) y
  33.   0x0fe9583f8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    6 [8 L: L9 Z0 q$ e1 ^: S2 @
  34. Shadow byte legend (one shadow byte represents 8 application bytes):, ~: G/ M; H9 {# f
  35.   Addressable:           00
    0 ]7 i$ `9 U" X9 ~9 k9 @- n- O+ B
  36.   Partially addressable: 01 02 03 04 05 06 07
    3 H1 h, {6 T) A3 y8 _' A
  37.   Heap left redzone:       fa
    ) p4 f( t0 E% z, c/ k: I- T2 i
  38.   Freed heap region:       fd
    4 k, f' a7 R% v# X& J- j
  39.   Stack left redzone:      f1
    - z) k$ m6 {4 y) v5 J( U# D9 V
  40.   Stack mid redzone:       f2- d  l6 l- H" ^; ~
  41.   Stack right redzone:     f3
    ' G! p/ j/ K8 c2 ?
  42.   Stack after return:      f56 @9 [3 F: P+ R: g$ c0 z1 e) y
  43.   Stack use after scope:   f8$ x! o) t3 |8 D6 U, ^3 B
  44.   Global redzone:          f99 g/ N3 }& z/ w/ E/ O( n
  45.   Global init order:       f6
    ' U( u. M7 N& @5 _6 O( `' F( u6 F: S
  46.   Poisoned by user:        f7
    5 v" u7 m  m* f; V
  47.   Container overflow:      fc7 n2 ]: ^+ I4 i- g
  48.   Array cookie:            ac1 A: |' Y, l; b4 ^0 j" [0 T3 X& Q
  49.   Intra object redzone:    bb* ?3 d$ ^+ Y+ M7 w
  50.   ASan internal:           fe& l+ {( n- z% P* B+ v% z; M
  51.   Left alloca redzone:     ca
    / d3 o* S- t, N7 S$ h
  52.   Right alloca redzone:    cb
      c7 i8 m7 }7 R1 @$ ^* x& i% O, H
  53. ==419==ABORTING
复制代码

8 d% v1 v! ^3 @' ^/ i- j
注意:这里在运行程序前需要添加环境变量:; }( W- R+ t+ C9 X, f  L- N

  1. 6 s3 _- D6 B" }! L4 `
  2. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码

: k3 \; J# u% W3 W
小总结
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
    1 Q1 A% E- C- `2 w& ]

0 _0 d0 S1 I* J$ h. r2 {8 d8 n/ s* c
收藏 评论0 发布时间:2020-10-1 16:06

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版