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

如何排查内存泄漏问题?

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

    ; X! N. ]$ M0 A" e0 n0 l& y
这里程序喵向大家推荐新的一个排查内存泄漏的工具:AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本都可以使用,支持Linux、OS、Android等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有:
  • 内存泄漏
  • 堆栈和全局内存越界访问
  • free后继续使用
  • 局部内存被外层使用
  • Initialization order bugs(中文不知道怎么翻译才好,后面有代码举例,重要)
    + k, k5 _( d# _
使用方法直接看我下面的代码:
检测内存泄漏
内存泄漏代码:
  1. * [: V% n# _3 ]& T* _
  2. #include <stdlib.h>0 Z1 X$ f. _1 V8 R9 n0 X4 s

  3. 1 v8 ?' e- P# `! E5 k. ~$ u
  4. void func1() { malloc(7); }6 C+ @" w7 T3 E
  5.   {  ?# n' J' y; s$ q! u
  6. void func2() { malloc(5); }
    + S8 D+ q' ]; l4 F4 W- D

  7. 4 X6 F( B) \: [' f" W/ Y
  8. int main() {0 E6 s8 r7 F' t$ `, P9 ]2 M4 N
  9.    func1();1 i* f  e) Z+ I3 l2 y! y
  10.    func2();
    ; F& [; `! C3 t
  11.    return 0;
    0 B$ v8 E' n. e9 J. }4 G
  12. }
复制代码

) r+ q% ^. q+ k! r  [- K! i" `% \
编译and输出:
  1. 1 c' z6 E0 {( ?2 B$ G/ y
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out! ~1 M4 G. E- ^" ^, @& u0 ^* K

  3. 8 E. S# b8 w& Y
  4. =================================================================+ R% h# x$ _5 T2 E4 m2 V# p, Z
  5. ==103==ERROR: LeakSanitizer: detected memory leaks
    9 r! Q' b2 `* V$ _" k: F! k

  6. + a1 U  H% P! b' ^1 A
  7. Direct leak of 7 byte(s) in 1 object(s) allocated from:2 w% {, D  g1 j4 a1 v
  8.    #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40); b2 O3 R* w7 \1 c
  9.    #1 0x7f95b36007f7 in func1() /home/wangzhiqiang/test/test_leak.cc:3
    ) x1 V  h. [( s. s
  10.    #2 0x7f95b3600814 in main /home/wangzhiqiang/test/test_leak.cc:8
    ; L7 p# f: S8 c6 j2 ~  P; ^& i
  11.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    1 Y; k. u. Q; U0 `
  12. ( b/ V, a! F( `
  13. Direct leak of 5 byte(s) in 1 object(s) allocated from:+ m2 S! C- X5 Y, O
  14.    #0 0x7f95b231eb40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
    ( \7 j+ A% u7 z1 J: v# z; s
  15.    #1 0x7f95b3600808 in func2() /home/wangzhiqiang/test/test_leak.cc:5
    1 y# A+ E, @0 e4 \( O4 r
  16.    #2 0x7f95b3600819 in main /home/wangzhiqiang/test/test_leak.cc:9
    % ~' @8 {3 p) _" l" D) C
  17.    #3 0x7f95b1e61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    $ i) P* Q0 O8 I2 K# g4 B$ V
  18. 1 i. Z) n/ V. x) X6 G
  19. SUMMARY: AddressSanitizer: 12 byte(s) leaked in 2 allocation(s).
复制代码

) y& j& \) p$ V9 M) E- Y4 I
编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。
检测堆栈内存越界访问
示例:
  1. + n+ M$ l7 C3 P9 V
  2. #include <iostream>
    4 p$ g0 }* b4 a: l% m
  3. $ @8 A. i/ ^3 c+ Z* w/ x) k
  4. int main() {
    5 o" p! W. X) j+ C2 M3 g
  5.    int *array = new int[100];
    9 w! E( ^1 L  d8 I" g
  6.    array[0] = 0;
    * Y3 a3 W$ k; L4 s" B
  7.    int res = array[100];  // out of bounds
    3 h% m! d4 z- _% ^! H' ~: Z1 J8 l% p
  8.    delete[] array;" }. T$ R* N9 ]1 U/ K# ?
  9.    return res;
    " R, w1 _3 D& [4 R, Y! g) Z
  10. }
复制代码
6 M7 n  h0 ^& i) X  u: a
编译and输出:
' G0 S& G! r3 U. @% [& p$ j
  1. 0 M' s) X6 t, @9 w
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out; M" u) u1 q, ?2 {+ M" m1 F  U% |
  3.    
    4 F2 T* g1 c9 ~
  4. =================================================================6 w9 B+ d' X2 H# ]
  5. ==110==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x7f0e06400d2e bp 0x7ffff5963f10 sp 0x7ffff5963f006 T2 J+ I9 Z* l8 q& x' {: A
  6. READ of size 4 at 0x6140000001d0 thread T0
    - b' m3 @3 o: i. R; u
  7.    #0 0x7f0e06400d2d in main /home/wangzhiqiang/test/test_leak.cc:6
    : _7 ^) r0 F, l$ P& B  M7 L" q
  8.    #1 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    - ?, h; `6 M/ j- Z. u, t
  9.    #2 0x7f0e06400bb9 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xbb9)
    & B" t* K" q- S" Z
  10. / W7 n4 |, }% F; j. \
  11. 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0)
    & ^  n- w/ K* x/ \: G: W  Z1 S1 V
  12. allocated by thread T0 here:
    : t6 J2 y: V! A, }0 w
  13.    #0 0x7f0e05120608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)
    - b! i3 q: g- Q
  14.    #1 0x7f0e06400cab in main /home/wangzhiqiang/test/test_leak.cc:4+ g4 D6 ?2 l( F1 q- _; a
  15.    #2 0x7f0e048d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
      ?# N$ U2 A. K
  16. # z: Y+ z# l8 I7 R/ L
  17. SUMMARY: AddressSanitizer: heap-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
    , ~! q* r& k* N  l4 x8 t) Q
  18. Shadow bytes around the buggy address:
    " D4 M& @! |, X) E5 r% [7 W" }
  19. 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00# ?( r- g5 ~1 \6 j( G( \+ Y
  20. 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    & g& S9 J/ `* q% Y' D* e8 E4 g
  21. 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 005 z5 h& x; I/ m2 a+ N
  22. 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    / S) j- U# U! g4 q; j6 ?4 P0 M' U
  23. 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00, `% F5 b2 a8 ~  Y8 k7 Z. S
  24. =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa
    : E! T" f# b0 A4 k& x3 v% N2 y3 M
  25. 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    ) }4 [) e' h( g. t4 p, v) u. L
  26. 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa! V0 f) y0 M9 i' {
  27. 0x0c287fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    7 e8 e! `0 o* Z" {# M
  28. 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa  K) A' q4 T0 C& Y7 K2 O
  29. 0x0c287fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    ' d0 q* {9 l# ?! t9 `, L7 D9 b
  30. Shadow byte legend (one shadow byte represents 8 application bytes):
      }/ k, W2 \3 g2 g+ ~
  31. Addressable:           00) P& v. @7 E$ W2 i3 V
  32. Partially addressable: 01 02 03 04 05 06 071 Y" G: z( l1 {3 a: Z
  33. Heap left redzone:       fa
    ) [6 u# Q0 i4 p
  34. Freed heap region:       fd, C; w4 H' V/ ]4 b8 m7 g& _
  35. Stack left redzone:      f1# Q1 l9 V+ {5 t! O( y( b1 f: X
  36. Stack mid redzone:       f2# r* f- C: \4 Q3 `2 Z1 p! x, m
  37. Stack right redzone:     f3
    4 ]! w, z( x$ v9 V+ @( D- d; E
  38. Stack after return:      f5
    6 ~* n7 v! Z' `7 R0 _6 I4 e
  39. Stack use after scope:   f8
    ; ]3 h2 j3 |: _: o! V3 V
  40. Global redzone:          f97 {) a/ z- @6 l7 r& F9 V
  41. Global init order:       f6
    3 K3 d1 b, R' i4 H* x& U6 _* P; z" R
  42. Poisoned by user:        f7
    ( ]6 V) j$ v/ w8 T- `. A
  43. Container overflow:      fc. }/ {. M/ I8 I& {6 z) h
  44. Array cookie:            ac
    : Q& R: c. w. d6 h
  45. Intra object redzone:    bb
    % x# V+ t1 z! Q7 @* L
  46. ASan internal:           fe
    ; T( n# M. X7 W3 N" j" [) u+ E
  47. Left alloca redzone:     ca
    7 u8 W6 K+ m: H- f
  48. Right alloca redzone:    cb
    ! {4 ?7 O; d) b; v" @2 b6 I
  49. ==110==ABORTING
复制代码
1 H$ D. J1 K! i4 n; z: c
可以方便定位到堆栈内存越界访问的错误。
全局内存越界访问
示例:
  1. 2 l+ T2 t. P0 k3 T  O
  2. #include <iostream>
    . F4 G( y2 ?' j, B  n
  3. & E1 \( A# ~5 I4 s3 O7 |! ?
  4. int global_array[100] = {0};
    . s& y, A9 w2 H+ f1 k
  5. . Q! K& o9 W* R8 Y' O6 @0 T
  6. int main() {
    7 K5 t" s8 v$ ?" x( g! T
  7.    int res = global_array[100];  // out of bounds; i. S& \4 \! P
  8.    return 0;
    . ]- {3 z8 t# }" h( f+ x' ~
  9. }
复制代码
* u* v5 Y# @- Z0 O  v0 a
编译and输出:
0 m  h- {0 g- l
  1. : S9 H, K  X: w4 F
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    / T4 \" U6 [- t) ?8 _0 ]" N
  3. =================================================================7 g) d- A: M( e# F) _* r: f) M
  4. ==116==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f42e6e02310 at pc 0x7f42e6c00c84 bp 0x7fffdda52780 sp 0x7fffdda527703 l) w9 u1 z$ ?5 \
  5. READ of size 4 at 0x7f42e6e02310 thread T0
    $ N. `* w% ^$ G, g+ Y# `
  6.    #0 0x7f42e6c00c83 in main /home/wangzhiqiang/test/test_leak.cc:6
    " e8 i5 k1 P* w7 a; R( T
  7.    #1 0x7f42e50d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)/ V5 P( s+ \* l, ^+ H# L/ Y2 l9 T- W( {
  8.    #2 0x7f42e6c00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69)
    * k$ O. G) f! H  P$ A
  9. 8 D- j" _. D0 q2 n. Y2 s
  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
    3 p. d- d9 D  x& F$ l$ L
  11. SUMMARY: AddressSanitizer: global-buffer-overflow /home/wangzhiqiang/test/test_leak.cc:6 in main
    * A8 b2 \3 x) [4 \
  12. Shadow bytes around the buggy address:
    ( |& ?# U! S) P# {
  13. 0x0fe8dcdb8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    / G; R) w  C+ o3 D
  14. 0x0fe8dcdb8420: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f9
    $ p0 Q# A9 }3 `/ o- S/ {; ^2 @
  15. 0x0fe8dcdb8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    * p1 p. w& y6 E+ @1 J  ~
  16. 0x0fe8dcdb8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ; }, h3 Q/ S& ~1 F2 G! q9 E
  17. 0x0fe8dcdb8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 002 z+ x3 [% N9 P0 R
  18. =>0x0fe8dcdb8460: 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
    3 j9 i4 @, R3 ?1 ^' {
  19. 0x0fe8dcdb8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ' }/ e" |0 L) o* Z0 {) J
  20. 0x0fe8dcdb8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0 q" x& h. k6 n! O* x0 N1 X  C
  21. 0x0fe8dcdb8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    " h' @( [6 j- |5 L" B/ P
  22. 0x0fe8dcdb84a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00: W  `/ K! v% P! B2 _7 Q8 S- q  V8 g
  23. 0x0fe8dcdb84b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    " p$ @; g; d" ?
  24. Shadow byte legend (one shadow byte represents 8 application bytes):5 ]& `$ h  ?5 B8 w( [
  25. Addressable:           00
    # d7 D9 I2 d3 N5 P2 N
  26. Partially addressable: 01 02 03 04 05 06 07
    % \9 n! H  j1 y! L& \1 J
  27. Heap left redzone:       fa
    # r0 V, a. L3 d* m
  28. Freed heap region:       fd2 z2 n! u/ B3 c7 a6 C! R1 q
  29. Stack left redzone:      f1
      K! d1 F* c* J3 Y
  30. Stack mid redzone:       f2' W# J6 W+ m. h: [
  31. Stack right redzone:     f3
    7 m6 e3 y. t4 }( W# S5 }/ d
  32. Stack after return:      f5& ^) ~! I7 i0 x4 A" I3 ^& K+ r$ D7 l; u
  33. Stack use after scope:   f8* k; r/ q- m: e; E# a
  34. Global redzone:          f93 q" o' j' j/ M% t: A  V! ?  I% f/ W
  35. Global init order:       f6
    ; z& {* F6 T+ g6 w
  36. Poisoned by user:        f7# V" e: `+ y- l0 _8 K; d
  37. Container overflow:      fc7 P( V" H$ E) T4 b# C6 T
  38. Array cookie:            ac$ z# _  a6 {% y" a: v+ H
  39. Intra object redzone:    bb
    2 J+ i, ^  p) |7 ~# U
  40. ASan internal:           fe
    ! |* z, b- @. b' i. |4 W
  41. Left alloca redzone:     ca
    2 `9 i- A/ J  f: T/ Q7 \
  42. Right alloca redzone:    cb+ y/ ]9 Q4 `" d! Z$ g
  43. ==116==ABORTING
复制代码

; i4 n# C8 J7 p
局部内存被外层使用
示例:

  1. : l5 g% e; K% f1 w4 s; h* i
  2. #include <iostream>
    2 `( _  N7 V1 Y
  3.   s. i: w" G# t+ U: f
  4. volatile int *p = 0;8 S' f9 ^2 b$ j7 p

  5. ! v/ [6 ~; o4 c, P, L% d
  6. int main() {$ u' f3 U4 w2 x: @* w6 o
  7. {
    9 @* r* @, o2 l; ]1 [
  8.    int x = 0;* \( _3 B! Z. F/ S
  9.    p = &x;. V* m+ {$ [+ o1 K8 U
  10. }( |: q- j+ G& l+ N
  11. *p = 5;- {) g$ S) a4 \( w1 n
  12. return 0;. u  q) [7 |6 [
  13. }
复制代码
+ }: T* x/ b+ _) j1 r
编译and输出:
3 ?2 \1 l9 b3 T

  1. 0 j' H; y2 i7 e. [8 Y7 h$ Z" W
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out. f+ e+ |/ p6 z2 W
  3. =================================================================
    ( P/ }- n* A' H% G5 M" [
  4. ==243==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fffce12a4b0 at pc 0x7f3993e00e7e bp 0x7fffce12a480 sp 0x7fffce12a4701 s. a1 i& d. P  U; ^4 B
  5. WRITE of size 4 at 0x7fffce12a4b0 thread T0
    6 \! i" h: S! ^
  6.   #0 0x7f3993e00e7d in main /home/wangzhiqiang/test/test_leak.cc:10
    ' ?" F9 y  n+ ?0 @, h7 ?3 T
  7.   #1 0x7f39922d1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)" Q, Y& g' f# _$ ^: c3 R. e2 W
  8.   #2 0x7f3993e00c89 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xc89)! V" m8 \" ]4 o1 X8 Q

  9. % S4 P, Y3 _, u4 `! X2 D6 a# B* Y
  10. Address 0x7fffce12a4b0 is located in stack of thread T0 at offset 32 in frame1 F( C& y3 i) e0 U' {0 E
  11.   #0 0x7f3993e00d79 in main /home/wangzhiqiang/test/test_leak.cc:5
    9 c6 z+ o% K+ t2 L9 y

  12. 7 V5 w& |% s/ q, |, `2 S0 R. K
  13. This frame has 1 object(s):5 E7 L+ ?1 D; Q9 @
  14.   [32, 36) 'x' <== Memory access at offset 32 is inside this variable4 t; [( V8 \6 ^3 a# A. [9 R- w% ?3 V
  15. HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
    ) j- X! Z4 |- G' c/ \
  16.     (longjmp and C++ exceptions *are* supported)
    6 @3 m1 \" i6 F; |9 n
  17. SUMMARY: AddressSanitizer: stack-use-after-scope /home/wangzhiqiang/test/test_leak.cc:10 in main
    5 r( H6 _' ~: B1 t
  18. Shadow bytes around the buggy address:
    / p3 w" \3 }) f9 W6 D+ u. y' {
  19. 0x100079c1d440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; ?. o6 m$ X+ w' z6 S7 R
  20. 0x100079c1d450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 005 \* X; W  U$ Z: F
  21. 0x100079c1d460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    * {# k. m2 i: v+ G1 i4 q
  22. 0x100079c1d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00- O- x2 {' e6 X
  23. 0x100079c1d480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    4 u7 \: [8 j& d/ F- n4 N, t% s$ X% h
  24. =>0x100079c1d490: 00 00 f1 f1 f1 f1[f8]f2 f2 f2 00 00 00 00 00 008 M. `4 i. w, ?! b" ?$ n( d
  25. 0x100079c1d4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00# p4 R& T! |' L% d
  26. 0x100079c1d4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    * n% q/ Z2 z: G# a4 X
  27. 0x100079c1d4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    7 @. q- g3 O  u) }& u' R
  28. 0x100079c1d4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 004 a& p& h7 E9 ^: w7 n" B% G
  29. 0x100079c1d4e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    4 h2 V3 v' a+ `
  30. Shadow byte legend (one shadow byte represents 8 application bytes):
    0 @: {: G- i8 S6 [( x
  31. Addressable:           00
    4 E& u/ ]1 I' `
  32. Partially addressable: 01 02 03 04 05 06 071 _- ]6 _& _4 c4 p
  33. Heap left redzone:       fa( f# J/ k4 y8 z7 A- R' `, d, M; q
  34. Freed heap region:       fd7 z) v! r. d9 ]8 y5 K
  35. Stack left redzone:     f1
    2 }+ T/ b! @& v9 ^
  36. Stack mid redzone:       f2' o/ Q; m$ B2 H' u
  37. Stack right redzone:     f3
    2 F& W0 h7 Q6 I' C$ @0 J: _
  38. Stack after return:     f5
    " @' d/ Z6 F4 b; c% w: A8 m' P2 Z
  39. Stack use after scope:   f8
    2 C* g3 p- Q; ?; g2 @* D( c; r! W
  40. Global redzone:         f9
    ) h& z; Y5 V8 Z4 ]1 M: p2 s3 f
  41. Global init order:       f68 c- U9 `/ G' L- P1 V
  42. Poisoned by user:       f7
    ( @  N" M9 ~/ j+ c8 X
  43. Container overflow:     fc
    , \, M! u3 ^8 {7 }4 ]. n; F
  44. Array cookie:           ac
    ! E- X( a$ y: b% O2 K$ n4 `& }* K" j
  45. Intra object redzone:   bb& U5 U% \; @# n
  46. ASan internal:           fe3 U8 o2 r! G! f3 g+ T) B* _
  47. Left alloca redzone:     ca
    7 r1 U) @& q2 R. N" p
  48. Right alloca redzone:   cb
    % ^$ @, h7 Z5 m/ w/ P6 @; X# ]3 y% E
  49. ==243==ABORTING
复制代码
7 u8 [; i' g# d
free后被使用
示例:
  1. ! t" v) w; P5 q6 O, p: M' Z
  2. #include <iostream>  u5 j* X4 {6 }7 R: ~2 j

  3. ( h* x" f$ o( `/ I" z
  4. int main() {% ?3 u% V# n8 g5 W. q& Z
  5.     int *array = new int[100];
    9 I. P! [4 r* i$ }
  6.     delete[] array;
    4 c6 F2 m8 l0 \. F9 y8 J( o
  7.     int a = array[0];  // error- E7 H9 m; D2 w/ w" I, ~; H9 d! q
  8.     return 0;5 f: h& x+ ~7 U7 Z3 p6 A
  9. }
复制代码
, }: _/ F8 ~/ B7 ?+ b7 K& I
编译and输出:
- @; g7 \* ?- l. H: r6 t; n. j
  1. 1 M" Q: {; j+ B
  2. g++ -fsanitize=address -g test_leak.cc && ./a.out
    . F: |; K; q* U  e! e
  3. =================================================================
    # Q1 a' U) x; N7 L! I
  4. ==282==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x7f209fa00caa bp 0x7ffff2a15020 sp 0x7ffff2a15010; y$ h0 ^; ~# c$ f
  5. READ of size 4 at 0x614000000040 thread T0
    7 {1 p9 D: t. T7 L
  6.     #0 0x7f209fa00ca9 in main /home/wangzhiqiang/test/test_leak.cc:60 r5 J- @4 c8 S! l1 y; F2 C
  7.     #1 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)$ C  L& [- V; s3 @2 r5 Q
  8.     #2 0x7f209fa00b69 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb69). L% i1 f. Y5 `8 U& I9 i* w" D
  9. 7 X5 e/ x8 k  \' y
  10. 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)! b$ H4 {2 D, h9 o1 E) Q% m( }: W
  11. freed by thread T0 here:
    6 O2 F# Q' t: S) O" X$ j/ r0 Z% S
  12.     #0 0x7f209e721480 in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe1480)
    6 L, |$ ~& j7 j2 \
  13.     #1 0x7f209fa00c72 in main /home/wangzhiqiang/test/test_leak.cc:52 x8 g* W. E0 o( @( a" A
  14.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    5 b$ ?7 g5 O+ q

  15. 6 t/ p# X9 v" `; `$ _
  16. previously allocated by thread T0 here:0 X7 B$ {$ e; W& Y7 ]
  17.     #0 0x7f209e720608 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0608)* l: @+ m* K5 V. K
  18.     #1 0x7f209fa00c5b in main /home/wangzhiqiang/test/test_leak.cc:4! B; ^1 o  z5 T  R
  19.     #2 0x7f209ded1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    / Q  K9 c; h  \, S. u! Y( ^/ K/ z
  20. ; y8 @6 J4 h0 w. T+ w, ]
  21. SUMMARY: AddressSanitizer: heap-use-after-free /home/wangzhiqiang/test/test_leak.cc:6 in main
    : y/ K9 D, u7 P$ H5 c3 J
  22. Shadow bytes around the buggy address:
    5 ?, d+ E0 h3 d
  23.   0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    1 W- [$ }: l) Y8 r  W# @& V
  24.   0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ! q; ~9 f! r; j1 Y; Q" c
  25.   0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 004 F) H* z4 z1 W9 b1 D
  26.   0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; E& E9 r  c2 C7 b5 v
  27.   0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" M# a  @3 Z3 a5 |9 H# J1 M
  28. =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd
    & o" ~- |) U* Y$ ]2 ]
  29.   0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd% q+ G" I. A, h$ [* ~" e5 D
  30.   0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
    / O- }2 @- b/ L# z7 |+ k
  31.   0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa% i3 h6 R; t3 s- _4 X" ]1 R
  32.   0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    : o  N6 E$ B* ~. [' Z! @
  33.   0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    & H! r- ~/ {& M* P; k- ~
  34. Shadow byte legend (one shadow byte represents 8 application bytes):* Z; I" H9 _- Q& i/ A. a
  35.   Addressable:           00
    * v+ ?8 H$ P2 B$ R8 g1 g! H
  36.   Partially addressable: 01 02 03 04 05 06 07, H3 n4 _! {8 }7 F% U; T6 U0 m
  37.   Heap left redzone:       fa/ K* Y& X8 E" L" H  a! r) a; Z
  38.   Freed heap region:       fd$ w; n( o2 D$ _1 t
  39.   Stack left redzone:      f1
    4 @+ y( A) E! W* W( C  j+ O, ?
  40.   Stack mid redzone:       f2( }+ z. B, i8 ]8 Q  \
  41.   Stack right redzone:     f3, g7 |* ]# W9 s. C, Z3 d" M
  42.   Stack after return:      f5
    , S1 f/ [5 b2 [/ C( x; P0 E( e2 ~# ^
  43.   Stack use after scope:   f8
    3 H" Y# M; E9 g1 t
  44.   Global redzone:          f9
    7 c7 t- l0 r* ?  Z, I2 X! {
  45.   Global init order:       f6
    1 ^) c" ?% m; o' x  D- u" ~" T
  46.   Poisoned by user:        f7" |1 x4 D1 o4 ]* P. z" w
  47.   Container overflow:      fc
    ) R' t- T, ~; ]( G- x
  48.   Array cookie:            ac5 U& W/ K6 p! P9 j* O
  49.   Intra object redzone:    bb' g& r0 v% P7 R% z) l; t
  50.   ASan internal:           fe/ L: N1 F: b5 C
  51.   Left alloca redzone:     ca. |9 |, V$ h  V* [8 N# i
  52.   Right alloca redzone:    cb9 G2 M0 ^- w; t- a: ]* s; C1 f
  53. ==282==ABORTING
复制代码
- R) c% F6 K; Y- v& v
Initialization order bugs
示例,这里有两个文件:

  1. ; l9 c2 L3 i6 B
  2. // test_memory1.cc+ i5 T, P+ X" ^( B
  3. #include <stdio.h>; }) t: A+ A2 ^
  4. ( `7 O6 i# H* N4 \4 v
  5. extern int extern_global;% }( V+ b. e4 r8 D8 ?$ r( T
  6. int read_extern_global() { return extern_global; }/ J6 n, H" N, Z  e! x
  7. 6 w  {0 l8 a4 Y! p; |& B7 F+ g6 a
  8. int x = read_extern_global() + 1;
    . T; k  l# ], b: ]( V$ d( }) \
  9. ' z) t9 {% z! k. S: t) {- s
  10. int main() {
    + a5 {" k, O  f  a/ @+ M, q
  11.     printf("%d\n", x);3 X  C1 S4 F: p" Y6 e; h0 C$ h9 G/ _
  12.     return 0;
    . n" J3 f+ ^- {3 S6 R: [
  13. }
复制代码
  1. % O* ?, S0 L) s# a2 Z' O
  2. // test_memory2.cc
    6 G1 Z2 x4 ^4 ~) M6 z

  3. 6 F! y" G6 I2 H
  4. int foo() { return 123; }  I4 s' W5 `4 x) q) c
  5. int extern_global = foo();
复制代码
, h* ]+ k4 T$ I' z7 w5 y  E$ ~
第一种编译方式输出如下:2 q4 I) D; U/ V/ [. {
  1. 4 w, {6 k6 T4 o/ T. [% d
  2. g++ test_memory1.cc test_memory2.cc && ./a.out) t  {' y5 k) {, i! e6 w! Q. v6 y- T# B
  3. 1
复制代码
$ I2 H( x; W4 ^3 K& N' E5 u' `- O
第二种编译方式输出如下:

  1. ' z! w8 D8 z  k* c
  2. g++ test_memory2.cc test_memory1.cc && ./a.out6 s1 `/ K3 F" [/ ]% z
  3. 124
复制代码
, l$ w& ]( I9 i& ~6 Q. {
这种问题我们平时编程过程中可以都不会太注意,然而通过ASan可以检测出这种潜在的bug:
" J, B3 e5 a7 ?; l5 J/ p
编译and输出:
  1. 3 P1 p' i& g. b  N
  2. g++ -fsanitize=address -g test_memory1.cc test_memory2.cc3 [5 }- L' a% L" m# R7 d% T
  3. $ s, ~  u3 K) [/ f4 T
  4. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true ./a.out
    ' N9 a- I& d  S' {
  5. =================================================================
    8 G7 U# \2 q% I( _: v# m& u4 I2 d1 W
  6. ==419==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x7f46c20021a0 at pc 0x7f46c1e00c28 bp 0x7fffe423d920 sp 0x7fffe423d9103 X% X: D) g: V: L
  7. READ of size 4 at 0x7f46c20021a0 thread T0- ~1 \+ H( l$ P1 e' H) D
  8.     #0 0x7f46c1e00c27 in read_extern_global() /home/wangzhiqiang/test/test_memory1.cc:3
      t5 F. ?% b9 ~5 H! |% c: g. I) x/ L
  9.     #1 0x7f46c1e00cb3 in __static_initialization_and_destruction_0 /home/wangzhiqiang/test/test_memory1.cc:4
    : {$ @5 s5 f4 h: r
  10.     #2 0x7f46c1e00d0a in _GLOBAL__sub_I__Z18read_extern_globalv /home/wangzhiqiang/test/test_memory1.cc:8
    $ [0 e- D: B. k# P5 m, e
  11.     #3 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c)' q. T/ D& z+ Z! ~6 `
  12.     #4 0x7f46c0461b27 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b27)! Q2 p3 o1 F; r, P/ N6 H! I
  13.     #5 0x7f46c1e00b09 in _start (/mnt/d/wzq/wzq/util/test/a.out+0xb09)
    + A, t$ R8 g  N- v5 Z

  14. 2 X" b& A$ H# f* H
  15. 0x7f46c20021a0 is located 0 bytes inside of global variable 'extern_global' defined in 'test_memory2.cc:2:5' (0x7f46c20021a0) of size 4
    . d3 n# z) ~6 C. P" z  O
  16.   registered at:
    0 d( y! t, l/ }. f) A( R6 E  g
  17.     #0 0x7f46c08764a8  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x364a8); R$ R+ \1 @( x1 A5 w! {% e- h
  18.     #1 0x7f46c1e00e0b in _GLOBAL__sub_I_00099_1__Z3foov (/mnt/d/wzq/wzq/util/test/a.out+0xe0b); P6 A; G. V8 h
  19.     #2 0x7f46c1e00e5c in __libc_csu_init (/mnt/d/wzq/wzq/util/test/a.out+0xe5c); h7 C! l% c  d' M+ A, Y1 a
  20. , Y" E- F* w8 A6 z% P
  21. SUMMARY: AddressSanitizer: initialization-order-fiasco /home/wangzhiqiang/test/test_memory1.cc:3 in read_extern_global()
    " A1 ~8 Y+ D; z; V( n8 M, i! c
  22. Shadow bytes around the buggy address:
    ; x0 X2 w3 S' z
  23.   0x0fe9583f83e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    % _! [& r2 m9 m
  24.   0x0fe9583f83f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    8 t, l, F3 r0 E, i8 O( F8 C# E
  25.   0x0fe9583f8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00) F, y3 i& t. S" S
  26.   0x0fe9583f8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00. r6 G! L& N( t
  27.   0x0fe9583f8420: 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9 f99 S& {9 ?: }# T+ U
  28. =>0x0fe9583f8430: 00 00 00 00[f6]f6 f6 f6 f6 f6 f6 f6 00 00 00 00) c+ J' \; S$ u/ q/ o
  29.   0x0fe9583f8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00/ O. B9 Q1 y6 q% M" M
  30.   0x0fe9583f8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0 U* V# D- C4 M( e
  31.   0x0fe9583f8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00. }- B  ~& M1 V/ J* U6 g
  32.   0x0fe9583f8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    - c5 C& k6 ^* o/ ]
  33.   0x0fe9583f8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    ; a2 W. h' O9 `, s! v2 f- X! ?+ ^% }
  34. Shadow byte legend (one shadow byte represents 8 application bytes):
    ' I+ |8 o# f6 C1 `0 r
  35.   Addressable:           00
    ! v4 v. L& E6 d1 |4 x0 o; M6 u
  36.   Partially addressable: 01 02 03 04 05 06 073 O6 n' y/ r% l
  37.   Heap left redzone:       fa2 G( [; I9 ?' u/ \
  38.   Freed heap region:       fd
    # m1 ^8 m5 H$ b5 k5 m" V
  39.   Stack left redzone:      f1
    8 Z' A9 c) i& T1 c( ]: @
  40.   Stack mid redzone:       f2
    ' _4 a$ x/ e- J9 R
  41.   Stack right redzone:     f3- f5 t: L, k& L: k+ R1 R( Y1 p
  42.   Stack after return:      f5
    0 j" b8 F5 P0 f& V, T" i' P6 c7 w5 r
  43.   Stack use after scope:   f8* \" q4 Q& }  X# Q% s7 T6 {9 s
  44.   Global redzone:          f9, e' p- \/ B9 y5 h- Y
  45.   Global init order:       f6
    5 j3 A5 x, e* q0 c( D0 F1 }
  46.   Poisoned by user:        f7
    * n. _& ^' c) E) q6 m# C- b
  47.   Container overflow:      fc& i: x6 w! |9 K$ f8 u4 m3 f
  48.   Array cookie:            ac2 l; W* T! p7 S- a
  49.   Intra object redzone:    bb
    & J* d( ]7 A& o6 `3 A/ V
  50.   ASan internal:           fe- {6 S# q3 X9 j" F# |8 x+ L  E
  51.   Left alloca redzone:     ca
    . q' L$ B' V$ ], H- n
  52.   Right alloca redzone:    cb; f. G2 N3 L0 m0 }
  53. ==419==ABORTING
复制代码
0 [2 c* l5 y1 f" N, \5 q
注意:这里在运行程序前需要添加环境变量:) ]7 }6 y, n: o' N  J# H: ?. ?  O
  1. 2 H' v4 W% J) \
  2. ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true
复制代码

9 j' }3 B$ I% i3 Z  v, `2 `4 n
小总结
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

    ; ~4 B0 _# E# H" n2 }- V9 b+ u

5 M8 f/ {2 R5 ~" ^5 [
收藏 评论0 发布时间:2020-10-1 16:06

举报

0个回答

所属标签

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