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

如何排查Linux内存泄漏问题?

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

    # B7 Y, B# ?1 t- L) J9 [% ]
这里程序喵向大家推荐新的一个排查内存泄漏的工具:AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本都可以使用,支持Linux、OS、Android等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有:
  • 内存泄漏
  • 堆栈和全局内存越界访问
  • free后继续使用
  • 局部内存被外层使用
  • Initialization order bugs(中文不知道怎么翻译才好,后面有代码举例,重要)
    * h$ s; h. V5 w8 `/ g
使用方法直接看我下面的代码:
检测内存泄漏
内存泄漏代码:

  1. % H3 h" z  @8 V' U+ a4 `
  2. #include <stdlib.h>
    6 X" K$ I: B- m* M% i

  3. ! f0 {! K! B6 c4 E9 L3 W
  4. void func1() { malloc(7); }
    ; T0 {' p' m/ r& p$ Q5 l" ~
  5. 9 d) S4 X2 F: |; z2 T) J' K2 d6 g% F. W
  6. void func2() { malloc(5); }
    * l- B3 P6 k, ^% E# v7 D7 j# t
  7. , T$ y2 z1 _/ M  d, T* q
  8. int main() {7 M$ v: g  O% H2 i* k- V
  9.    func1();1 f. O/ [% E# r( Q& Q
  10.    func2();
    ( d. J7 C; x8 R! ^' B: _& U
  11.    return 0;) V  c4 d& a8 Y% ?' S  }$ b$ ?6 n9 B
  12. }
复制代码
! ^# x3 L$ n- `) ?5 C5 S' c! y) n$ Q
1 l9 J* ]; |7 u- J) p' g: o
编译and输出:

  1. ; R7 g6 X7 f. c  a9 {5 {
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    ; s6 F4 ^; a$ \% G6 u: _% A

  3. - j0 Z) x5 g+ i0 u. P
  4. =================================================================
    1 d% H1 P- M( ~+ Y1 Y# |
  5. ==103==ERROR: LeakSanitizer: detected memory leaks
    " G6 H( @6 w6 i: S$ Y. @
  6. + |! S. I/ _* `$ a' @# F
  7. Direct leak of 7 byte(s) in 1 object(s) allocated from:4 q; D0 a. m+ Q7 z
  8.    #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)% C( y+ s$ ?% [" C' U1 ]
  9.    #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:3. c, I6 g: V' {0 K7 t0 K
  10.    #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8
    * n, m; t$ `: }# ^
  11.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)8 D1 s+ a( g5 {1 E: C4 c8 F: E
  12. 2 z: R! E7 P: c+ q$ L
  13. Direct leak of 5 byte(s) in 1 object(s) allocated from:
    0 B/ R* S, B7 b  U* c
  14.    #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)# i2 C$ e" s& K
  15.    #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:5
    3 C) f( S$ d1 v7 }
  16.    #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:9$ C, i4 n$ m6 Q$ m
  17.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    / B* {$ E, `3 M% K- ~% Y- n: P4 K
  18. % L5 ~' u9 L; m' a
  19. SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码
* F3 V- e0 k' b
) M0 f8 J8 j8 A3 D* l$ U6 r& k) \6 G
编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。
检测堆栈内存越界访问
示例:

  1.   x# c. S8 a, `8 t
  2. #include <iostream>
    ) {; N1 U! w, Z3 \, M9 n

  3. 1 Y3 N! f6 E! ^) x5 w& L7 L0 u4 ?+ d
  4. int main() {; \9 p; h" y- `2 }& V; c
  5.    int *array = new int[100];
    $ x5 i4 U8 f! f1 S2 S! V2 N
  6.    array[0] = 0;  }: h. m  m- J. Y
  7.    int res = array[100];  // out of bounds. d+ e, ]% f8 o$ {% S; q- {- I
  8.    delete[] array;
    : o4 ^5 v  i+ e. u4 z! F* N6 @+ `
  9.    return res;7 T+ ?6 I" n7 r- D5 N
  10. }
复制代码

" [5 b6 N; A6 I" `0 E
编译and输出:

  1. % }* s& [. E5 W7 |
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    # i0 B" D4 G% R$ w
  3.    
    $ w2 g/ R6 S' \; B  i
  4. =================================================================/ z8 q4 A1 [6 T- S( z; K' u
  5. ==110==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x7f0e06400d2e bp 0x7ffff5963f10 sp 0x7ffff5963f00  d! U$ u1 B& ^) a% m6 x( P& T- q
  6. READ of size 4 at 0x6140000001d0 thread T0
    % o0 G9 A$ _3 T  W6 l4 D- @
  7.    #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:68 K: s4 M% D* h5 f( L! C' ]
  8.    #1 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    / R5 _: d+ o& b: G% x0 P
  9.    #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)! r/ k9 \  G3 D! h, e

  10. ( ~4 G8 X0 w! i; J0 ^9 Y
  11. 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0)2 }) j- _+ ^+ Z$ v$ y$ {
  12. allocated by thread T0 here:
    ) f0 z, P; |1 Z" h! K' u2 |; e0 z
  13.    #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)
    # g3 ?/ |5 E  U' ~
  14.    #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:4, Q: P8 ~" ?5 |# C7 R$ \
  15.    #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    0 V! k' E% e* Z( N$ X( i- ?
  16. . E: o! d; d$ p
  17. SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main: B$ ]0 F" w0 a% C9 b
  18. Shadow bytes around the buggy address:
    5 S4 \$ t: P- C/ P8 M4 x
  19. 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    . Y8 x" `3 x+ Y! g( f# s4 o2 d
  20. 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    4 ~* O) V& E1 q( [- J
  21. 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00, w( [( \* D' J
  22. 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    . m5 `3 R: E1 D6 z9 o
  23. 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00( J5 e& n/ X1 |. C: O
  24. =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa/ n) ^# r& O: x
  25. 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa  ^- v1 N- N) C8 t$ j6 I
  26. 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    4 F, @5 M6 V8 E% n. h  p1 B# N
  27. 0x0c287fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa5 V. L, O2 r/ g
  28. 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa- x3 u0 \& a/ l/ G; V
  29. 0x0c287fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa: X( a: c, {5 `! k' i- E
  30. Shadow byte legend (one shadow byte represents 8 application bytes):9 @& q6 D' j$ |) E
  31. Addressable:           00
    ' b( H2 D9 W) T& r& ]/ M
  32. Partially addressable: 01 02 03 04 05 06 07
    ; L& Q) Y4 S4 j8 {5 x
  33. Heap left redzone:       fa4 a+ a( o+ d7 _5 O
  34. Freed heap region:       fd& ]; z* K( ~$ t( ~- U9 s
  35. Stack left redzone:      f1
    ) x  d4 W) e+ @) R
  36. Stack mid redzone:       f2
    : U/ z* D- j8 P+ Q) e
  37. Stack right redzone:     f3# J5 F0 R2 L( [
  38. Stack after return:      f5
    ) n% u( F6 q5 P# ~+ d4 ~
  39. Stack use after scope:   f8: L" M* H1 ~9 Y  x+ ~" ?
  40. Global redzone:          f99 L2 |5 t" `- V( j: z8 \
  41. Global init order:       f64 R3 |: E5 `  g  \5 E1 Y4 Q/ F( y
  42. Poisoned by user:        f76 y; B+ L5 S5 K0 @
  43. Container overflow:      fc( |: q& v( c0 Z  T! x9 t% v
  44. Array cookie:            ac
    ' t0 Y6 n' g5 h- _* b
  45. Intra object redzone:    bb: v+ x; `1 ~# C# F: a  n0 O2 ]) _
  46. ASan internal:           fe
    3 _! C2 }' z% N0 M; `
  47. Left alloca redzone:     ca6 o, o. l6 c- ^+ D9 R4 ?( B2 l
  48. Right alloca redzone:    cb) Y) f. `2 g  x. ~: L. A
  49. ==110==ABORTING
复制代码

) {6 \9 m6 L# P: n& i( [
可以方便定位到堆栈内存越界访问的错误。
全局内存越界访问
示例:
  1. " X; D" e" k9 _4 u
  2. #include <iostream>
    ' k; ?0 N3 F' u% h2 F# j3 w, d# l
  3. $ }+ t! _. }* W; x# K, U) z
  4. int global_array[100] = {0};5 C- v& N: _" S; f

  5. % B- z7 e, @+ q  _# o0 M3 f
  6. int main() {- i% C+ ?5 o" A+ e6 h) p
  7.    int res = global_array[100];  // out of bounds7 c% ~: E: }! s' ?: w1 \9 x  ^% S0 _
  8.    return 0;
    2 t7 ^5 P# b9 o, K5 X' m: L& o0 N& z+ ^
  9. }
复制代码
+ A) C+ h- W% M% p
编译and输出:

  1. 5 k" y3 D* _& i, y
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out6 o2 j0 m3 W) S' w' [
  3. =================================================================
    ( W( I. q2 h- `: D1 x
  4. ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda52770
    ( |) C" X0 d7 ^9 b
  5. READ of size 4 at 0x7f42e6e02310 thread T0
    $ z" K: t. e" E: g$ J( I, _6 h
  6.    #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:6
    : {9 R" _6 N- R3 z( [
  7.    #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    . ^2 `4 e' R6 ]. z/ [0 ^# q
  8.    #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
    , L5 o5 G. ^2 D

  9. # ~3 y' o& E" L8 o& h. K5 k) _% k
  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
    7 G7 q1 E$ a; V% j9 K
  11. SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main3 h! k& a# c% u" R% J
  12. Shadow bytes around the buggy address:
    $ y! p: Y# L% r) b$ u
  13. 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    1 a+ i! l4 G, M+ y0 k2 T
  14. 0x0fe8dcdb8420: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f94 m, w) @- [* R/ {! ?
  15. 0x0fe8dcdb8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    / r$ \, f& |/ @" E; K
  16. 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    * `+ |7 T( d) ~0 P" T
  17. 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    . `/ s( J! @, V3 B
  18. =>0x0fe8dcdb8460: 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
    : i7 ]  N7 `; G, ?7 H
  19. 0x0fe8dcdb8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00: A! }8 h' }; }, U3 K5 I& E
  20. 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 003 C! m8 k& }! {
  21. 0x0fe8dcdb8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    & o$ p% {# `% l# G+ L9 V
  22. 0x0fe8dcdb84a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00/ Y) B: A8 r% i# b# j9 p
  23. 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 000 n1 o8 U6 V& w1 p! P2 R5 ]+ ^0 q
  24. Shadow byte legend (one shadow byte represents 8 application bytes):* Y$ H( y* w( H0 p& l& a& x% F
  25. Addressable:           00
    2 `& c0 A; V/ _# c
  26. Partially addressable: 01 02 03 04 05 06 07
    ( V* F! f1 R. w8 D
  27. Heap left redzone:       fa
    / a5 V4 S& u" E
  28. Freed heap region:       fd( [& z2 d# I# U3 S
  29. Stack left redzone:      f16 F5 N) W$ w$ V; ~# B
  30. Stack mid redzone:       f2
    + T  u0 W6 g9 K, @* c5 Q2 n
  31. Stack right redzone:     f32 _& v  Y1 \8 ]  j2 ^; C6 V1 G' B
  32. Stack after return:      f5- q( F) f: r# ]* P+ @
  33. Stack use after scope:   f84 Q4 B  U$ A& B9 i
  34. Global redzone:          f9
    ; M; \8 y' D0 F# ?9 m( q
  35. Global init order:       f6' m( _: H% H& D2 T4 Z
  36. Poisoned by user:        f7
    7 O1 b* k( f# u( V* q3 U2 b
  37. Container overflow:      fc/ O# e) _1 T- M) _2 M
  38. Array cookie:            ac7 n, K2 G  e3 g. u
  39. Intra object redzone:    bb
    5 M6 |- p5 B! n1 c6 V; [
  40. ASan internal:           fe4 z$ L8 O. \8 [2 O" |9 q) C
  41. Left alloca redzone:     ca8 n, {* M* f2 q" ?
  42. Right alloca redzone:    cb
    5 W1 N$ A. _$ j" D; G% n- B' }' U
  43. ==116==ABORTING
复制代码

# q9 D. ?( g# v4 ]; k5 S
局部内存被外层使用
示例:

  1. 5 m7 z" c' l0 ]" Y- W: h& z, r4 }
  2. #include <iostream>9 u! Y; ~9 X, w3 s5 J) ~/ z' n5 F! X
  3. 4 i4 X7 X: ~1 y9 t) I$ n: b
  4. volatile int *p = 0;
    / q- p2 T5 K' r5 f

  5. ) I# W2 _$ o7 z% A$ T
  6. int main() {2 R3 |$ b$ l# i: u5 J, d
  7. {. U( }& {+ h* Q! K2 E5 q& v3 g
  8.    int x = 0;
    : I5 f7 u" v. C! |; R, ?2 {( ?
  9.    p = &x;+ F) z( h9 C. e4 G
  10. }% T5 S2 X9 j/ `8 A  e2 J
  11. *p = 5;
    " q* Z4 b' t# S  C2 G5 _8 o
  12. return 0;9 O% ?, U: j* J
  13. }
复制代码
) t* C- w8 k1 g

- V- G. s1 g' e+ |
编译and输出:
  1. $ x; g4 ~" ^9 Z7 E) ]( i% \5 ^2 u! T: U
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    7 ~. i. {! t, M  \. y
  3. =================================================================
    $ r- n8 D0 `3 i7 v7 K6 l
  4. ==243==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fffce12a4b0 at pc 0x7f3993e00e7e bp 0x7fffce12a480 sp 0x7fffce12a470
    * o7 \! v! i- ?0 F/ D) ]
  5. WRITE of size 4 at 0x7fffce12a4b0 thread T0- m1 A' }" C, d$ p3 ^, e5 x
  6.   #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:10: r! p# f! u  D9 w/ A" \
  7.   #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    1 i! e: ^( t$ l, v
  8.   #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)/ w! z! m( k9 R0 k
  9. 7 @( D: P" I8 D6 \  X6 w3 Z
  10. Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame
      V7 O& S$ g9 Z
  11.   #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5; u' L2 i4 e. ~* \9 T& h
  12. 5 X& M) e& ^! S
  13. This frame has 1 object(s):
    6 {, A) D# O% T% H" M& e
  14.   [32, 36) 'x' <== Memory access at offset 32 is inside this variable
    ) w% S0 p  ^& c
  15. HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext% d6 E$ _" v. N
  16.     (longjmp and C++ exceptions *are* supported)
    : ^: j* w3 h2 u5 S4 i4 [; N4 E
  17. SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main
    : e; [& d4 o; t. A( o! [( V
  18. Shadow bytes around the buggy address:4 X: Q0 ]9 _! v' d1 g
  19. 0x100079c1d440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    , x- z. m. d$ ~4 `$ I& p
  20. 0x100079c1d450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    & b4 L3 u0 R: V. n
  21. 0x100079c1d460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    % R" n+ `8 l% q& V
  22. 0x100079c1d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    4 _) R1 a3 F' `5 n6 l/ u
  23. 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    6 k4 t4 ?* I: z; T7 b; G$ t
  24. =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 00# v; B1 ?" T+ e2 k8 g; \' d( l
  25. 0x100079c1d4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ R& @3 o8 F6 r+ S- r( T  w
  26. 0x100079c1d4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, a9 M! f3 ?$ `/ h$ u& _- A+ U# r
  27. 0x100079c1d4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00) ]( Z: M+ {9 d2 N8 |7 e; F
  28. 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" u# d# X) J9 D4 g4 x7 ^' \
  29. 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    6 x5 B+ ]; ~0 K! S- ]) u$ |& C6 p
  30. Shadow byte legend (one shadow byte represents 8 application bytes):
    ) L( O- W0 v9 c- {2 s6 m
  31. Addressable:           006 U. c; c2 D" t9 n( b
  32. Partially addressable: 01 02 03 04 05 06 07  p( l- S$ M3 I% }
  33. Heap left redzone:       fa% h( S9 W$ T$ c6 l
  34. Freed heap region:       fd# V5 y$ P3 H. Y7 Z- V
  35. Stack left redzone:     f12 r" M# `0 ^2 ~" O3 \& m+ i
  36. Stack mid redzone:       f2, N5 ]3 D7 ^) l4 S( O4 j* Z
  37. Stack right redzone:     f3
    ! Z9 A8 n; ^2 p1 x& I2 _
  38. Stack after return:     f5
    0 H9 h9 W' F! W5 n- Y; K
  39. Stack use after scope:   f8
    # }- B3 ^2 V9 c7 m5 q. P6 \" y
  40. Global redzone:         f9
    / x* E4 h# k! w7 ]6 o; x; a
  41. Global init order:       f6
    ; @0 V1 G3 O' s3 k" A' q5 o
  42. Poisoned by user:       f7
    4 y. k6 T8 Z9 H( E  j" [5 h
  43. Container overflow:     fc
    ' O5 h  m6 @+ \
  44. Array cookie:           ac
    2 s$ {" j, e  ~8 `
  45. Intra object redzone:   bb
    ! `9 ~6 ~8 T5 N2 i9 X
  46. ASan internal:           fe, G% @: Z6 W$ e; i  L" c% J8 h
  47. Left alloca redzone:     ca
    : i) T1 m: H. p+ i
  48. Right alloca redzone:   cb1 x2 M9 G/ Q3 V1 [0 s6 w) Z2 I
  49. ==243==ABORTING
复制代码

3 h. O( r$ d7 Y: }/ H
free后被使用
示例:

  1. / v9 H& Q/ w8 M8 u3 j/ p
  2. #include <iostream>7 [* }" A3 z$ |( V& X0 j- Q

  3. ! W; B. b4 K. r8 K7 F5 _, p
  4. int main() {6 u) h5 L/ J+ ?% `
  5.     int *array = new int[100];
    8 f; M) T% r" g2 y( `' I$ [
  6.     delete[] array;
    5 j  }) O, E4 a% V8 t) w$ Y
  7.     int a = array[0];  // error
    1 k0 a, S& }+ C/ M
  8.     return 0;( l% y1 N% d3 d- Q
  9. }
复制代码

3 d2 h* l+ z0 l) Q# R+ h
编译and输出:

  1. : K: T8 E  R% n( _" t( D- r
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    : C4 @* V& O# c5 d0 |8 h) L! P
  3. =================================================================. k& i) A9 _- Q7 M: y  q
  4. ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a15010
    ! v! _9 o# j* M! Z9 F& ]( r
  5. READ of size 4 at 0x614000000040 thread T06 m( }2 y2 c4 u, J
  6.     #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:69 d; d8 \* h0 Y% N/ c. I
  7.     #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)% E* }& x3 i9 m6 r% v) O" f! S! T
  8.     #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)8 r* X, ~$ K* v# y2 O# @1 p

  9. : G0 i4 i9 C; m8 _$ L7 Z* T
  10. 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)
    ; g& b* ?8 P0 w0 ~& S) U6 }% _
  11. freed by thread T0 here:
    8 c! Y  W, I( ~0 b
  12.     #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)* o4 [( s% P+ i6 J/ D) ^
  13.     #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:5
    ' |$ |/ L) i7 l
  14.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)4 W, o! S! l- q' F
  15. ! m5 E5 B2 s; V0 ]0 O
  16. previously allocated by thread T0 here:5 x3 m9 U8 @- R
  17.     #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)* `* F" W: }  D( H8 t. L- n. z: k
  18.     #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:4/ n( r  e, g# \& ?, }/ _
  19.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    " ]8 l* R3 q- W  m, O$ K  _$ f- n

  20. 4 Z: _$ i% A; A! |5 a5 A* l
  21. SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main
    + J" b8 O, B4 G
  22. Shadow bytes around the buggy address:1 \2 j7 u! I) P$ d/ W7 d
  23.   0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 Z! V. L3 f0 h7 y4 @; H# B8 c
  24.   0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 x$ K6 l! k. a8 h# F
  25.   0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 009 ~* ]: m9 G* i) x
  26.   0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, J' d9 w  z. e8 S
  27.   0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; q5 }% K7 ]0 [3 m$ x
  28. =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd
    3 p! b) R" S& m
  29.   0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd2 j$ c' [% o3 S1 B5 b; K
  30.   0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
    ' K! j- a( k" o  F* }3 t* c
  31.   0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa% |6 i" J! i9 K1 `; Y0 }$ z
  32.   0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    - J9 A5 g! K* ?6 h( B' ]
  33.   0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    2 W) _+ S, X( j; ~+ C: b+ @* e
  34. Shadow byte legend (one shadow byte represents 8 application bytes):
    ; e7 C4 k/ U" P  W8 E
  35.   Addressable:           00
    / F) r( E3 Q. D* i
  36.   Partially addressable: 01 02 03 04 05 06 07% ]$ ?# E, U" n$ F" @
  37.   Heap left redzone:       fa, Y/ V- l% `* F$ _. Y1 p
  38.   Freed heap region:       fd
    1 w# ]9 M- }9 ?3 X7 l4 R
  39.   Stack left redzone:      f1
    ; x8 ]! Z# B- k( l
  40.   Stack mid redzone:       f2& |/ L& C$ H8 o5 e9 \
  41.   Stack right redzone:     f3
    7 p  G' W" w8 d. ~0 [2 H
  42.   Stack after return:      f59 F& R* o4 M, t/ z
  43.   Stack use after scope:   f83 n' M' ^' |$ H7 m
  44.   Global redzone:          f9
    + z- G- S8 O9 r9 ^7 R8 `4 @. A
  45.   Global init order:       f65 n! e& l7 N) j5 B% q
  46.   Poisoned by user:        f7
    1 A: o+ t) o0 a6 i; }. X
  47.   Container overflow:      fc
    ! q8 u# w- \5 L4 d0 G- w- s4 N' L
  48.   Array cookie:            ac
    . O7 a9 v# |. Z/ U$ @: d  ?
  49.   Intra object redzone:    bb% v/ G' F$ ]! i
  50.   ASan internal:           fe
    . p# D$ {* k( T, D. g
  51.   Left alloca redzone:     ca
    4 M! T/ ~* n1 f0 h: x
  52.   Right alloca redzone:    cb' R( \; a5 D" Y: K  d
  53. ==282==ABORTING
复制代码
" W# x1 Z8 q! L$ K
Initialization order bugs
示例,这里有两个文件:

  1. 6 w. w5 R0 y. x
  2. // test_memory1.cc: }% E0 \7 Y7 E7 T3 E
  3. #include <stdio.h>
    . _" R( r) t! v9 d

  4. * ]  H9 E* P2 H0 Y# _7 O  }
  5. extern int extern_global;- ~; M# }% [' _  X3 f2 F6 n) L
  6. int read_extern_global() { return extern_global; }1 E7 h2 {; a6 z6 B
  7. ( |& J" c: w! m5 w
  8. int x = read_extern_global() + 1;
    6 e' p: B% _, [# J

  9. ( M/ x' b8 F+ i
  10. int main() {
    8 x4 m2 M) @/ ]
  11.     printf("%d\n", x);, U+ V- g3 u6 `( L# k, h
  12.     return 0;
    $ ?: ]' P5 c) `6 n/ j
  13. }
复制代码
  1. <font color="#001000"><font style="background-color:rgb(255, 255, 255)"><font face="Tahoma">
    2 u9 X/ o( t9 e( x0 \! a
  2. // test_memory2.cc7 {$ B2 q! B- N
  3. 3 b  F) d0 V+ |& ~0 D; j# B
  4. int foo() { return 123; }* W# T4 r1 d2 S( u) `2 n6 v$ v
  5. </font></font></font>
复制代码
第一种编译方式输出如下:

  1. & H% H- ]( c$ M8 K2 ~4 t& N
  2. g++ test_memory1.cc test_memory2.cc && ./a.out* r! b7 E0 Q( D% ^3 P; l" B3 h1 Q
  3. 1
复制代码

: T. h9 i' e( m" t# ~. W
第二种编译方式输出如下:
  1.   N" X  w. C9 |2 i$ B" V- l  N
  2. g++ test_memory2.cc test_memory1.cc && ./a.out
      t' p( r. G' x. R! s% c
  3. 124
复制代码
5 `  W1 {& |1 S3 t
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:
+ d% H! R* w/ j# d" M% v8 K! U
编译and输出:
  1. ! J. i) l* K9 K& @4 Y& |5 D
  2. g++ -fsanitize=address -g test_memory1.cc test_memory2.cc
    8 ^3 I3 n- B/ @1 u1 V. u

  3. * e" k! J  Z' C6 `& s% q
  4. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out5 }/ M0 c/ k1 ]' P$ B
  5. =================================================================
    ; _6 d# z# a4 }7 m5 r! Q8 W; L
  6. ==419==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f46c20021a0 at pc 0x7f46c1e00c28 bp 0x7fffe423d920 sp 0x7fffe423d910: \5 t& X- [+ S
  7. READ of size 4 at 0x7f46c20021a0 thread T0
    . W# E1 I' H6 q1 b8 N) W5 g- ?5 }
  8.     #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3' C9 H5 F, R. a
  9.     #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:4
    8 q6 K9 t* ?$ @2 h2 \: Q. k0 K
  10.     #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8+ O  c; l7 O. V$ b. B4 ]- ]' p1 a
  11.     #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
    # g1 d) F& W# {8 u  x" L: D3 D
  12.     #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)
    6 T% [$ J6 W& C# i( S
  13.     #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09)0 M: i2 a5 D8 X6 i" U: P, ?6 _# u, ?- J
  14. 3 x% _% B1 _! r# B
  15. 0x7f46c20021a0 is located 0 bytes inside of global variable 'extern_global' defined in 'test_memory2.cc:2:5' (0x7f46c20021a0) of size 4. i  b0 I' x3 r" e) c% m
  16.   registered at:: B2 p" c" J' O, A7 P5 D, Y5 P
  17.     #0 0x7f46c08764a8  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8). D% w; S( Z  Q$ J) g2 ?& N3 T
  18.     #1 0x7f46c1e00e0b in _GLOBAL__sub_I_00099_1__Z3foov (/mnt/d/wzq/wzq/util/test/a.out+0xe0b)4 I. G" m3 g5 d9 C) q- j1 Q! y
  19.     #2 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)
    , |* i5 W, u% ]6 B0 Z! \

  20. ( ^  p0 q7 L" M9 _
  21. SUMMARY: AddressSanitizer: initialization-order-fiasco /home/wangzhiqiang/test/test_memory1.cc:3 in read_extern_global()8 V4 f/ k5 B' F
  22. Shadow bytes around the buggy address:
    ) j( V' `/ m, S$ w
  23.   0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 O: N7 X/ e8 G2 t. O" c4 v
  24.   0x0fe9583f83f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00+ d/ H7 p5 C6 r: X( I. `
  25.   0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 008 k; `8 P$ h# B: D
  26.   0x0fe9583f8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00( h/ f* d4 R% Y' K! `
  27.   0x0fe9583f8420: 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9 f9
    - ^1 ?& H7 C1 o, u
  28. =>0x0fe9583f8430: 00 00 00 00[f6]f6 f6 f6 f6 f6 f6 f6 00 00 00 00
    + U9 w/ ^9 l  b
  29.   0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" p( Q5 A! r7 v. F9 G
  30.   0x0fe9583f8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    2 S" b* `- U6 Q; {2 f
  31.   0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ! ]3 Y7 p. o  D/ K
  32.   0x0fe9583f8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    * q) Q9 V# W0 @4 T7 Z7 z# Q% l- r3 o
  33.   0x0fe9583f8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00: h1 v( Q& x, I! z8 d: `
  34. Shadow byte legend (one shadow byte represents 8 application bytes):" X- f" n8 N5 b% e! H, ]4 p
  35.   Addressable:           00
    + U! D4 c" h- [. D" x  P- T$ ~! Z
  36.   Partially addressable: 01 02 03 04 05 06 07; W  ?- i/ Y% x: i) ?+ b; c; e
  37.   Heap left redzone:       fa
    % k: Q6 G, Z# Z
  38.   Freed heap region:       fd. ?! ?) `6 c; E/ p9 a6 `
  39.   Stack left redzone:      f1
    # N8 ~/ b% H2 s+ h( ]' H
  40.   Stack mid redzone:       f2
    4 U" f2 x5 F( g5 S: I7 @9 t, I
  41.   Stack right redzone:     f32 R: w/ f" h1 j; v7 d2 `0 F2 f
  42.   Stack after return:      f5, R. T9 m. y# w: o! l
  43.   Stack use after scope:   f85 z/ S% N1 d4 w5 y% K: l
  44.   Global redzone:          f9$ d. V" u5 y( C6 S/ B3 E. [
  45.   Global init order:       f6
    ( Q' @& Z% B4 a; e
  46.   Poisoned by user:        f74 h( m: R7 x. p5 U: |' e
  47.   Container overflow:      fc
    + D$ j# `) K" z. w( Q8 E! u
  48.   Array cookie:            ac
    + v! `; q! x/ J5 O5 J
  49.   Intra object redzone:    bb8 {; b* g- F$ u. G
  50.   ASan internal:           fe
    , D4 G2 {8 a9 D3 h* c9 e" l% {# T6 x
  51.   Left alloca redzone:     ca+ e) c7 ?5 ]& v/ u
  52.   Right alloca redzone:    cb3 v# X- ?) G/ \. j, _
  53. ==419==ABORTING
复制代码
7 k9 W# ]" ]# t: n) y/ `
注意:这里在运行程序前需要添加环境变量:

  1. , T8 w: R! k' ^! G
  2. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码
* M5 H  k/ p! h0 o1 Q9 w& i
小总结
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
    / `  T" I: i. c1 j4 B1 y
收藏 1 评论1 发布时间:2020-5-26 22:52

举报

1个回答
慎微 回答时间:2020-5-27 10:47:20
以前,内存泄漏是个大难题,现在 gcc 加个编译开关就行!? 有 malloc 没配对 free 可直接查到?

所属标签

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