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

如何排查内存泄漏问题?

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

    # P6 U, ~5 M1 @1 ?, t  E7 ^; @: F
这里程序喵向大家推荐新的一个排查内存泄漏的工具:AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本都可以使用,支持Linux、OS、Android等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有:
  • 内存泄漏
  • 堆栈和全局内存越界访问
  • free后继续使用
  • 局部内存被外层使用
  • Initialization order bugs(中文不知道怎么翻译才好,后面有代码举例,重要)

    0 b, O; u7 d* T; ^1 [9 T( B0 z
使用方法直接看我下面的代码:
检测内存泄漏
内存泄漏代码:

  1. 8 c# ^5 p: {2 _. Z( \  ]  L
  2. #include <stdlib.h>
      e2 m0 d4 h7 h+ }  k- G7 V
  3. ( ?* u9 s4 W  F" C
  4. void func1() { malloc(7); }1 T; E; u; `" E1 O; ]3 u

  5. 5 q5 p* `7 t$ c5 O# [, i+ t
  6. void func2() { malloc(5); }
    ! K- G' I, w. t
  7. 0 i' u1 f- r6 p0 f! D
  8. int main() {
    " }* {6 ]( M8 R* q* e. y  H' L
  9.    func1();: w: r- y. t  G1 f/ \& \
  10.    func2();  O, j! l1 G8 V) F- |4 B( l
  11.    return 0;
    # h6 F9 I7 N6 V0 R) G  P, z
  12. }
复制代码
: W3 V; k- N, Y4 E3 `
编译and输出:

  1. ) S- N# i* ^- o7 W5 {+ @5 y9 p
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out! n* l$ S. w/ _* W2 M& d5 p
  3. * \. h" v% U( z6 n0 v# Q1 e/ n2 E
  4. =================================================================) Q+ F, U8 q  U" C8 P
  5. ==103==ERROR: LeakSanitizer: detected memory leaks9 }6 u; t+ n* P6 x8 S* ]$ U# U

  6. . W7 u0 N1 J( n# q' w' L! q* Y* v# C' H
  7. Direct leak of 7 byte(s) in 1 object(s) allocated from:) k( q. l4 z2 F- Z0 [& n
  8.    #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
    . Y9 @, E/ ^% z  p
  9.    #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:3
    ! w0 W7 @5 i6 A' J2 i( g
  10.    #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8
    9 g0 o* Y1 T: q/ d
  11.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
      R, U0 X5 t4 U1 Z% g4 B# q

  12. ; `* v+ B( F- m+ Z
  13. Direct leak of 5 byte(s) in 1 object(s) allocated from:! G0 P5 L. Q0 M1 |0 X3 A& X6 r; h1 f
  14.    #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
    1 ~4 p! n  R6 e7 k& z4 v1 f. c
  15.    #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:58 Q$ F1 l- [: |
  16.    #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:9
    ! b2 Z# A! M: i  `
  17.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    & p" ^( i5 m3 A0 p3 x3 J% \9 y, V
  18. ! D2 M0 Y* a  A: n9 C5 b+ A
  19. SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码

. G) N' B* V) g8 H: V: Z* Q4 \9 [
编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。
检测堆栈内存越界访问
示例:
  1. 7 C# U) e# {  ^' Q
  2. #include <iostream>2 ~- {$ G% y: P8 M+ ^

  3. 7 b- D9 n) H; \4 {( h0 I, |
  4. int main() {
    / D: R8 ^) s" p% y
  5.    int *array = new int[100];
    & U3 U- {2 i* F. H/ C1 M
  6.    array[0] = 0;1 Z1 K2 T' J" Q3 r/ Z( q3 h5 ^
  7.    int res = array[100];  // out of bounds3 v$ e- p: l. a! p* N; j  r
  8.    delete[] array;  N- F& r: p, Y  k
  9.    return res;
    ; N3 D7 o0 K+ |2 H, I. x
  10. }
复制代码

; f9 @$ @+ T: J7 v' I; n
编译and输出:8 ^3 ]) L' p2 |6 c1 d' i

  1. - a& P2 H! \3 w
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out4 U" e; x: p2 V$ ?1 `' C$ n2 [. G
  3.    
    # `4 V7 h. M/ J) a, `% e
  4. =================================================================# S& m1 x6 U& x% b+ T! k" @3 b
  5. ==110==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x7f0e06400d2e bp 0x7ffff5963f10 sp 0x7ffff5963f00
    % f, `& I& W' m+ m# `
  6. READ of size 4 at 0x6140000001d0 thread T0
    ( {" s0 o' v- H& H2 f
  7.    #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:6; @7 W% U' \0 v; i- x( @. ~
  8.    #1 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    5 X+ ^7 {" g6 G$ u8 j9 d
  9.    #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)
    - c. i4 k6 j# A6 L% H
  10. - {) Z1 w2 D  b9 w  c) a+ Y
  11. 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0)
    . U  ?% c5 A9 P
  12. allocated by thread T0 here:
    6 h' E& R' l5 P* b% l: M/ M
  13.    #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608); Z& |, {( U3 h9 @# _6 Q
  14.    #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:42 M9 i8 g6 L1 @" U$ B& K
  15.    #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)9 r3 s7 N+ y. g7 t( @& r

  16. 6 F' m: a* a( l, `# b+ o; L
  17. SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main  o; O5 b0 C- G) B$ U
  18. Shadow bytes around the buggy address:
    " J% v0 [  C  n% ~: L+ {
  19. 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    & B( t" P  S& d: ]0 ^9 ^) V, e
  20. 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00! z; M# Z$ H* j2 |
  21. 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 002 M, r: z' z( e! r, P& E
  22. 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, J- X$ w. ~" Z% |, m8 H, t- H
  23. 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    , ?3 o$ f7 d4 c, A
  24. =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa
    ' d9 U5 U: `: t
  25. 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    8 [) @# Q' F- Z6 ]* S6 T. z
  26. 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    * E; Y- T& k1 f/ b
  27. 0x0c287fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa( S! _" Y& j7 q0 M% m" P
  28. 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    + v+ O4 o7 q3 p5 S
  29. 0x0c287fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa' E, v4 A' w9 g5 K4 N+ n2 o' n' _
  30. Shadow byte legend (one shadow byte represents 8 application bytes):6 x( k, I& r5 b( ^) l; k
  31. Addressable:           002 x* p) u$ Z+ ?
  32. Partially addressable: 01 02 03 04 05 06 07
    4 o) Q% g9 N' I' D) ~5 \  `
  33. Heap left redzone:       fa- V) \' [% ?8 M
  34. Freed heap region:       fd
    2 a5 j9 G  d5 d- R
  35. Stack left redzone:      f1; h& ?: l6 m' X3 |# n  |0 N
  36. Stack mid redzone:       f2- P0 z1 Q1 Z+ `0 p& @
  37. Stack right redzone:     f34 u4 g& W' I4 [2 @, B
  38. Stack after return:      f5/ J4 ^- }% X' P
  39. Stack use after scope:   f8" Q! E! b5 E9 _: o% U. p& W
  40. Global redzone:          f9- U% J8 p; e# o" B9 r; e6 R1 i
  41. Global init order:       f6
    + K* O  W/ o1 w) }" I
  42. Poisoned by user:        f74 N7 ?$ S: S! U" ^. z, ?% r
  43. Container overflow:      fc% H* u/ W3 C7 m
  44. Array cookie:            ac/ c: P) g3 s, |6 g( d# E3 ~  V
  45. Intra object redzone:    bb# {4 x6 R# c9 `5 l6 o& N: A
  46. ASan internal:           fe
    $ F2 L5 c/ k. L7 I, l1 d9 P
  47. Left alloca redzone:     ca
    : _" Z( [0 M0 U9 p0 y# l
  48. Right alloca redzone:    cb
    ' P6 K) y. D- h# J
  49. ==110==ABORTING
复制代码
3 @! S2 ]6 H1 R7 |/ T
可以方便定位到堆栈内存越界访问的错误。
全局内存越界访问:
示例:

  1. ; G/ b( W* a  {+ i5 |
  2. #include <iostream>2 _% o! J' J5 `! v
  3. 6 p: ~4 o+ L6 c# R) C
  4. int global_array[100] = {0};/ s& Q! O* k% s# K8 K# p) ?

  5. 6 Q& j0 z3 f. p+ v( J9 [
  6. int main() {! p4 d) m- o# [3 v* e8 }
  7.    int res = global_array[100];  // out of bounds" z. H& D5 A$ R
  8.    return 0;  O' I0 t. m5 r/ I  |
  9. }
复制代码

3 ^: J1 ?3 c+ {; ~1 @
编译and输出:
. K; O2 [$ \+ Y5 W: l

  1. 9 M% ~% ]5 H9 Q- O: l
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    # W; c& P* j8 V# i) Y& e5 z/ P
  3. =================================================================
    0 q1 N' R& r: C' w' C& g
  4. ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda52770
    $ T- S; ^. `, i6 C% G5 X
  5. READ of size 4 at 0x7f42e6e02310 thread T0  T: e3 b7 `' D: [8 ]* g
  6.    #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:6& O+ \! O0 _! U5 v4 C
  7.    #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    , _: x* y( N0 r0 B) z2 ^2 B
  8.    #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)6 N+ j$ L0 S) Z
  9. 7 ~7 O6 q& T0 |7 z! V
  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
    ' e7 u; f& a( e# {* n' j+ V
  11. SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
    2 c7 d$ u( s8 `1 O) {3 a) w
  12. Shadow bytes around the buggy address:
    & f0 ~, Z3 S' `/ {, Z( H
  13. 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 001 B+ O$ t9 Q% L4 ?/ C  N
  14. 0x0fe8dcdb8420: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f9
    : ?+ L2 k# G6 j
  15. 0x0fe8dcdb8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; F  G) M% w! N' R3 Z; ~$ _
  16. 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    3 V& |4 @0 Z% g$ ^3 l
  17. 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    & w2 k2 C% c+ ?
  18. =>0x0fe8dcdb8460: 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
    + E# Z) Z4 L" \5 b, U1 F
  19. 0x0fe8dcdb8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00$ `2 A3 O& ~% n; G
  20. 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    - ]$ `+ h# S9 Q; {. {# Z6 X
  21. 0x0fe8dcdb8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ; s% C8 Q0 z& ^) r; d9 K
  22. 0x0fe8dcdb84a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ( w1 Z; j. V' Z% ]/ A  w& {
  23. 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ |7 ~5 A% p# B% b
  24. Shadow byte legend (one shadow byte represents 8 application bytes):
    ) k8 H: S5 {. f- d6 d
  25. Addressable:           00( I  O! q* l6 M6 k" R' i3 y
  26. Partially addressable: 01 02 03 04 05 06 07
    & z3 F  a1 z+ X6 r* e
  27. Heap left redzone:       fa
    5 z; |/ W1 d1 ~, Q6 d7 M, H
  28. Freed heap region:       fd+ G2 M7 e, S( Q2 H8 f9 P! U
  29. Stack left redzone:      f1% L# q# W& `" [! H- G2 B. W
  30. Stack mid redzone:       f2
    ; L) Y; v, W4 V3 i1 s  N5 U/ x
  31. Stack right redzone:     f3
    1 X+ P: ?. E' y% {1 U; }
  32. Stack after return:      f5
    8 j; \/ H4 s8 l- G# X7 ^
  33. Stack use after scope:   f8
    ' L' d2 G  e8 g( b( n, K( L# D
  34. Global redzone:          f9/ P( M: N" v- i! K$ A8 [4 g
  35. Global init order:       f6% o) o5 K' G$ m9 j. l) v2 p# V- P9 O: ~
  36. Poisoned by user:        f7
    ' \/ x( G) }% P0 n
  37. Container overflow:      fc9 d8 `& ]0 X. X& ]% v* N( O+ ~
  38. Array cookie:            ac* W5 R" ?/ E" x1 K( ]
  39. Intra object redzone:    bb
    ! m; N8 f& s! o( _
  40. ASan internal:           fe0 C5 k9 L; h9 }  C, g! q- j7 z' l, ^8 K* l
  41. Left alloca redzone:     ca
    0 s6 W, ^% |- ?' j
  42. Right alloca redzone:    cb
    + p' E6 c# k0 f! d* `
  43. ==116==ABORTING
复制代码

9 c4 b2 d! E1 {; e
局部内存被外层使用
示例:

  1. # R. }  Q. M) \; G, F8 [4 e$ F
  2. #include <iostream>
    / X* H9 E) B$ a# O% w

  3. % _$ B8 V- z( i/ U
  4. volatile int *p = 0;
    + `+ R- e; ?8 `4 e6 O$ Q. ?- e

  5. 8 W. M! s8 p; f
  6. int main() {$ P- Q6 U! ~6 i9 k! A  S; Q
  7. {; u# ?3 }; Q# R7 q
  8.    int x = 0;5 W7 `) e# I7 V
  9.    p = &x;
    / x- x' s9 H; {; Z: ?+ b
  10. }0 R0 A1 i) l% W! f  K; m, F
  11. *p = 5;+ ?0 c, a. _3 {
  12. return 0;! O$ m8 `) O7 {8 N% v/ v
  13. }
复制代码

9 @; q4 g9 h- U/ E' j  l
编译and输出:
/ J& {8 B, _( I! e1 r
  1. - {6 x7 n, B8 P  K; c! k3 }7 @
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    % f7 P& a: N0 Y  M
  3. =================================================================8 e2 t8 y5 J2 v* ^+ [: B& S
  4. ==243==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fffce12a4b0 at pc 0x7f3993e00e7e bp 0x7fffce12a480 sp 0x7fffce12a470  B! I3 S* c; ~
  5. WRITE of size 4 at 0x7fffce12a4b0 thread T0
    4 X5 F' j; I7 G5 H
  6.   #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:10
    + |. Z" V  i( }5 N
  7.   #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96): v  X2 p& R  d) I
  8.   #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)
    3 ?3 Y9 [! x: k$ J

  9. & K) f8 d9 @9 t
  10. Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame
    / T' n7 {) a; d5 r
  11.   #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5! ~8 e; K& S7 y

  12. ' x3 H: @3 d4 F5 j$ _
  13. This frame has 1 object(s):6 f' @9 Y! ?) A# Y4 p
  14.   [32, 36) 'x' <== Memory access at offset 32 is inside this variable
    ! O+ T. c) `5 d3 q6 K& D3 G- x
  15. HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
    9 H- q& w3 h/ O+ p9 @
  16.     (longjmp and C++ exceptions *are* supported)+ N- h8 g, b5 d! d
  17. SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main
    : a( w$ L. a% l) v  {9 v' E
  18. Shadow bytes around the buggy address:
    - s% K. N, W' P5 r
  19. 0x100079c1d440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    - e! x: {8 F& B  L- T
  20. 0x100079c1d450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 006 u6 s5 x' w2 W" V8 J! D- i
  21. 0x100079c1d460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00! U0 G' ?* M& \' v' A0 K
  22. 0x100079c1d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" d( ?9 L/ x9 S" ^
  23. 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 003 N9 Z& w1 m6 f1 z) k! q
  24. =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 00
    ! X5 g( T$ l5 d& x) S# [- t
  25. 0x100079c1d4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    : I1 M0 [* q+ ]
  26. 0x100079c1d4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    % I2 m0 R) Z8 J/ y) L
  27. 0x100079c1d4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00# u1 f6 U; v+ c* }9 f: B( @# D5 M0 Y
  28. 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ _) |) Q) q8 o6 |8 F
  29. 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 K6 R9 Z! A- v0 z) S
  30. Shadow byte legend (one shadow byte represents 8 application bytes):
    * @- f  o4 N. Q* R
  31. Addressable:           00( V2 K: o9 p( u  N1 b5 e3 E; `8 {
  32. Partially addressable: 01 02 03 04 05 06 07% X# p. S( K% ?$ G
  33. Heap left redzone:       fa: u; L  h9 Y4 X6 d) e1 y4 q
  34. Freed heap region:       fd
    1 W2 L2 Q. `) `( }& ~8 e+ N
  35. Stack left redzone:     f1
    8 q" p  x6 Y$ I
  36. Stack mid redzone:       f2
    , r1 M+ {. l- H7 Q0 `# O  {" S4 |
  37. Stack right redzone:     f3' [& ]# S# U1 q  r: Y
  38. Stack after return:     f57 ~  B2 X3 B5 i  u
  39. Stack use after scope:   f8
    : M+ \& o  y- I+ W4 d9 L" G
  40. Global redzone:         f9
    8 W. r4 ~" W, T/ _
  41. Global init order:       f6* T3 `; B( {$ T8 E2 Y' g  P% @
  42. Poisoned by user:       f78 }4 ?% p9 H. w6 Z1 E: U
  43. Container overflow:     fc
    9 D; z9 @0 r1 M0 z& f& X
  44. Array cookie:           ac9 M7 y; i4 A0 i& F) b3 C6 \
  45. Intra object redzone:   bb  s( F1 L, M7 d# y/ O
  46. ASan internal:           fe
    & u; T1 @0 Y4 H, A6 Q
  47. Left alloca redzone:     ca
    9 P' k7 n+ g7 P1 _
  48. Right alloca redzone:   cb0 ?- p' r; M7 C; f' q
  49. ==243==ABORTING
复制代码
6 i) C1 I  z  Z, J5 _, K
free后被使用
示例:

  1. & ]! {% W( n( l- A2 L8 C7 }
  2. #include <iostream>0 G. F9 |, D) a

  3. 9 {8 m8 l. v8 a( _8 b  v+ p
  4. int main() {
    5 T# {5 s% p7 F8 Q4 M6 p- Q6 l
  5.     int *array = new int[100];
    - F7 V- f9 M! ^4 f+ u
  6.     delete[] array;
    7 Y; k, @2 N* m+ d8 @
  7.     int a = array[0];  // error; O6 h8 t' B' Z+ Z/ v0 I1 t6 R& K
  8.     return 0;
    , B( u4 L" `7 u0 R5 k+ v
  9. }
复制代码

  X6 ]/ ?) X( I+ n' \# r" ^4 ?0 S
编译and输出:1 j! ^7 Z, t' A

  1.   w. w* ?  {; T- E/ t
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    3 k* |( M2 E- R) C, e0 ?% i4 k- Z
  3. =================================================================, D* n# `0 c3 m- |; z3 D
  4. ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a15010
    $ b8 X5 p6 @  ]9 M, T
  5. READ of size 4 at 0x614000000040 thread T0
    ' X, }# Q1 c& ?: S% a
  6.     #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:66 M2 [2 m: k1 w
  7.     #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    , R' a/ d5 a$ ?/ i/ u, D% u( a
  8.     #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69). }; E# \. ^$ f# P8 o4 G+ u% W

  9. & p7 s, A$ d# }" U$ J0 h
  10. 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)
    . E5 c: S! i1 E) w  {8 N; @
  11. freed by thread T0 here:! k/ T" }. a$ L/ s( O: @+ {7 E
  12.     #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)
    : Q% L: I. D  c/ }6 l8 Q
  13.     #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:5
    2 s! A- _& `/ f0 h: m8 Q% s* G
  14.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    # ?8 j' M1 K8 T0 V' e
  15. , @& k  @3 ~3 y/ o$ [
  16. previously allocated by thread T0 here:( r- ]: u! I" [! K% C- q
  17.     #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)6 A' J" m5 j/ y3 I: n: n
  18.     #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:40 [2 j$ V- h( y% Y+ c
  19.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    8 k) R5 O9 p& r$ X! v$ E

  20. 7 \4 Y* w) c# G/ ^& i
  21. SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main
    7 ]# o4 m5 K+ K" z; R7 j( z9 a' L
  22. Shadow bytes around the buggy address:
      ~0 ]: O% {$ \& G
  23.   0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  c& ]9 d8 m1 N' v  |6 H( n. R
  24.   0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 006 M. ?# ]/ T. ~6 i
  25.   0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    9 V+ G9 l2 q5 ~& d7 l1 g
  26.   0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0 H7 K- N2 \4 x! D, I
  27.   0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00% M9 `0 q- d. M2 s+ T% B7 I8 @+ t
  28. =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd9 H4 k2 H' U* D+ H6 E- ]
  29.   0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd6 \5 U9 B6 l/ L! n* v, @3 I0 {
  30.   0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd/ f. G4 k2 f, i+ m- L; f9 a% V
  31.   0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
    + c/ p! D9 h% @1 ~! I' ]: x
  32.   0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa8 N5 v/ p$ ?% l1 i' ]
  33.   0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    4 u) \# l2 T3 K4 C4 s
  34. Shadow byte legend (one shadow byte represents 8 application bytes):
    + P( h1 M' }) s) I$ x5 r# k
  35.   Addressable:           00
    . C! @4 R) H% _7 g$ P# ?
  36.   Partially addressable: 01 02 03 04 05 06 07; J5 K$ U0 o5 W+ h( r
  37.   Heap left redzone:       fa
    7 u, N! @8 j- B
  38.   Freed heap region:       fd
    # |! K4 Z/ o! Y; W$ X
  39.   Stack left redzone:      f1/ B( z* S8 O7 w! J. Z/ C
  40.   Stack mid redzone:       f2+ U  D% G# j# j
  41.   Stack right redzone:     f3
    1 v. V8 U6 h( l1 B4 E! D; d% l
  42.   Stack after return:      f55 u% J+ T0 B2 I: D
  43.   Stack use after scope:   f8/ ^! R: F9 a, U+ o, d
  44.   Global redzone:          f9
    % Y/ Z7 w3 t7 r0 p
  45.   Global init order:       f6  Y  e9 J9 u, Q- B1 X. ?- Z) l
  46.   Poisoned by user:        f72 Q4 ?( ^# D2 ^( E9 `
  47.   Container overflow:      fc: }3 \0 U# z/ K$ P* c" h! t5 @
  48.   Array cookie:            ac4 o, ~; ?/ F# K3 M3 z! B: d
  49.   Intra object redzone:    bb
    5 s8 h( E$ v; p9 g
  50.   ASan internal:           fe
    . g- e6 I7 `3 R( H* \
  51.   Left alloca redzone:     ca
    9 b9 d1 i# v$ f: Z2 m
  52.   Right alloca redzone:    cb
    , |, P* ^  C  D3 ~3 d# `( j
  53. ==282==ABORTING
复制代码
" k2 {) s! B8 _8 O# ]5 B. v
Initialization order bugs
示例,这里有两个文件:
  1. 9 P8 M! @  D6 k
  2. // test_memory1.cc0 [3 y9 w1 V8 {2 Z8 C( @
  3. #include <stdio.h>& U* H, ^; }1 O: e

  4. 0 W# k. ?3 v: b+ \3 n8 ?
  5. extern int extern_global;2 f& J- m( T2 {: Z( Y+ Z
  6. int read_extern_global() { return extern_global; }
    ) [' Q1 |: o3 P. p" |9 ~
  7. 0 m. T. i! _6 r% t$ W( w
  8. int x = read_extern_global() + 1;. o! N# I# g$ _- O1 A

  9. 6 }  u) \9 w; S/ q+ H1 s4 O8 E
  10. int main() {
    & O/ t5 t: D8 [* b9 `
  11.     printf("%d\n", x);
    0 [3 Y( C2 P4 O* a9 L2 o8 w
  12.     return 0;) W. ~& k3 r0 L
  13. }
复制代码

  1. 5 Z3 j% I& e$ R/ z! d' x
  2. // test_memory2.cc: _, y2 w- g# a' }* U# ?
  3. + o, k9 S2 j  x: C" }
  4. int foo() { return 123; }
    & R! {/ o# C; q7 L7 {1 W$ M$ W- H
  5. int extern_global = foo();
复制代码

4 N; }% N# U4 N3 ?
第一种编译方式输出如下:+ a9 e9 P+ @; c2 c2 V
  1. 5 \. J& U: ?1 T* K- ^+ E; h% _: M
  2. g++ test_memory1.cc test_memory2.cc && ./a.out
    & v2 c6 l, _! P$ L5 j( X
  3. 1
复制代码
. \9 c2 I1 n- Q# p4 N
第二种编译方式输出如下:

  1. 5 Q" L$ b) O0 i5 o( W$ M& Z
  2. g++ test_memory2.cc test_memory1.cc && ./a.out* E- m  k# Z; a
  3. 124
复制代码

5 K2 _7 i' v) X- Q1 l
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:
- ?' O+ u/ g" q0 E7 U! z
编译and输出:
  1. . e3 s9 W# o) H% k
  2. g++ -fsanitize=address -g test_memory1.cc test_memory2.cc  Y5 S2 n. S3 F' b
  3. ) u( l+ o! w) X* r% F
  4. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out
    ; u1 z: H+ }& N! d) ]
  5. =================================================================
    6 I6 @6 K0 v. [4 C9 G9 O
  6. ==419==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f46c20021a0 at pc 0x7f46c1e00c28 bp 0x7fffe423d920 sp 0x7fffe423d9109 @/ [$ L" x: H: O' e6 }
  7. READ of size 4 at 0x7f46c20021a0 thread T0
    ( F3 _# |* Y- z* r1 h
  8.     #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3
    8 ]1 J5 H) J0 U) S+ I. a
  9.     #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:4: V) s! @0 ~: C, L6 j
  10.     #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8! V1 J0 I5 o* b( z
  11.     #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
    2 M  \! C; ~, M7 G" G7 n
  12.     #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)
    ) P  U2 O3 C: z% v- U: @- I, L
  13.     #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09)
    6 i. f2 u5 ?5 X3 Q( d% L

  14. 0 M7 ~4 b9 ]5 F" _4 O3 O
  15. 0x7f46c20021a0 is located 0 bytes inside of global variable 'extern_global' defined in 'test_memory2.cc:2:5' (0x7f46c20021a0) of size 4
    $ P* X) j4 M$ K$ E) i; q, R
  16.   registered at:- m* O7 Z8 C& B- g. H
  17.     #0 0x7f46c08764a8  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8)8 A% t/ `4 y0 e7 D  X, N% |
  18.     #1 0x7f46c1e00e0b in _GLOBAL__sub_I_00099_1__Z3foov (/mnt/d/wzq/wzq/util/test/a.out+0xe0b): V; [! }: N& ?$ N) |, G- }  q1 Y" E
  19.     #2 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
    2 J" x$ M9 L) [0 S8 T! k
  20. / i: o$ `% [& O. {9 R
  21. SUMMARY: AddressSanitizer: initialization-order-fiasco /home/wangzhiqiang/test/test_memory1.cc:3 in read_extern_global()4 j; V6 x0 G4 z' z# d; O/ r
  22. Shadow bytes around the buggy address:
    # d6 z! H- f* p
  23.   0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    . m6 z: z4 f/ L- H" c6 d; _
  24.   0x0fe9583f83f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000 @: j: D3 K: G
  25.   0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ) A: o5 f3 y. |$ u
  26.   0x0fe9583f8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 008 F. l4 N, [5 s$ ?$ C7 J- i
  27.   0x0fe9583f8420: 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9 f9
    " ?* C2 b$ Y: M! d- l# j) m7 S0 o: I
  28. =>0x0fe9583f8430: 00 00 00 00[f6]f6 f6 f6 f6 f6 f6 f6 00 00 00 003 q' q4 t/ n" h' {' Y$ }
  29.   0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00$ i, P# y1 e5 j2 n" c- ?
  30.   0x0fe9583f8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ( T) W& T' n1 t8 A1 x' N. p
  31.   0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    # r4 d( H( W" F  E
  32.   0x0fe9583f8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
      W4 G' t0 T2 M$ @
  33.   0x0fe9583f8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    & t! @& Z$ L  s. y6 l1 T0 H7 {4 Z
  34. Shadow byte legend (one shadow byte represents 8 application bytes):
    / V( j5 i3 ]5 D" Z- F% R
  35.   Addressable:           00
    2 W3 ~7 {/ w2 |, I% v4 R# p
  36.   Partially addressable: 01 02 03 04 05 06 07% Y0 R3 h6 y1 C
  37.   Heap left redzone:       fa# {" Z1 T8 B: _0 \. B7 Z
  38.   Freed heap region:       fd
    . a1 h: x; u. `9 S2 c1 `1 M9 [
  39.   Stack left redzone:      f1
    7 a" X- H) _9 C4 ^5 [0 A
  40.   Stack mid redzone:       f2
    + ~) n) w" F! O
  41.   Stack right redzone:     f3& D8 ?: U4 |0 G( N5 a
  42.   Stack after return:      f5
    4 u! ^6 A" b6 L# q% S
  43.   Stack use after scope:   f8& t7 Z0 A) A% H0 X
  44.   Global redzone:          f9
    , B0 Z7 z7 v# t; x. @; {, }
  45.   Global init order:       f6
      G6 s; p; g1 E7 \7 x
  46.   Poisoned by user:        f7
    ( C" b3 ]. J+ p5 m" j8 W
  47.   Container overflow:      fc
    , B. B: S5 o+ U! r7 ?0 U! Z3 i# s
  48.   Array cookie:            ac4 T% R# |! ]2 J
  49.   Intra object redzone:    bb5 W% H' B& `7 w/ g; Q* d2 p
  50.   ASan internal:           fe
    % ?; L0 A& B: l5 r
  51.   Left alloca redzone:     ca! h/ i5 @4 X1 _8 L
  52.   Right alloca redzone:    cb
    1 ~1 e" E4 r! E: v3 [7 Z$ x
  53. ==419==ABORTING
复制代码

* g/ W, r: ^. }9 V! p
注意:这里在运行程序前需要添加环境变量:
# `- Z. u5 f/ ^6 m
  1. - V5 h/ p1 C" a+ a
  2. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码

5 Z' s# A: {5 Z% M" T* o; V: f5 q/ [
小总结
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 ~: K! N9 @6 b4 t- y0 z# k
" q$ ?8 _! M: ~  n8 \6 J
赞 收藏 评论0 发布时间:2020-10-1 16:06

举报

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