ASan(address sanitizer)初体验

  |   0 评论   |   0 浏览

背景

在一个C++代码中,出现了内存问题。专家建议我用工具address sanitizer(ASan)检查一下。

产品

ASan可以发现下面的问题。

初体验

环境

在LLVM 3.1和GCC 4.8中,已经包含了ASan。

示例代码

% cat tests/use-after-free.c
#include <stdlib.h>
int main() {
  char *x = (char*)malloc(10 * sizeof(char*));
  free(x);
  return x[5];
}

编译

g++ -fsanitize=address -O1 -fno-omit-frame-pointer -g use-after-free.c

执行

=================================================================
==2388220==ERROR: AddressSanitizer: heap-use-after-free on address 0x607000000105 at pc 0x55d3273db21b bp 0x7ffdf5dce230 sp 0x7ffdf5dce220
READ of size 1 at 0x607000000105 thread T0
    #0 0x55d3273db21a in main /home/abeffect/cppProjects/asan/use-after-free.c:5
    #1 0x7fa73d9da082 in __libc_start_main ../csu/libc-start.c:308
    #2 0x55d3273db10d in _start (/home/xuqian/cppProjects/asan/a.out+0x110d)

0x607000000105 is located 5 bytes inside of 80-byte region [0x607000000100,0x607000000150)
freed by thread T0 here:
    #0 0x7fa73e00140f in __interceptor_free ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:122
    #1 0x55d3273db1ea in main /home/abeffect/cppProjects/asan/use-after-free.c:4
    #2 0x7fa73d9da082 in __libc_start_main ../csu/libc-start.c:308

previously allocated by thread T0 here:
    #0 0x7fa73e001808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x55d3273db1df in main /home/abeffect/cppProjects/asan/use-after-free.c:3
    #2 0x7fa73d9da082 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: heap-use-after-free /home/abeffect/cppProjects/asan/use-after-free.c:5 in main
Shadow bytes around the buggy address:
  0x0c0e7fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c0e7fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c0e7fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c0e7fff8000: fa fa fa fa fd fd fd fd fd fd fd fd fd fa fa fa
  0x0c0e7fff8010: fa fa 00 00 00 00 00 00 00 00 05 fa fa fa fa fa
=>0x0c0e7fff8020:[fd]fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
  0x0c0e7fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==2388220==ABORTING

原理

[2]

程序申请的每 8bytes ​内存映射到 1byte 的 shadown 内存上。

因为 malloc 返回的地址都是基于8字节对齐的,所以每8个字节实际可能有以下几个状态:

​case 1:​8 个字节全部可以访问,例如char* p = new char[8]; 将0写入到这8个字节对应的1个字节的shadow内存。

​case 2:​前 1<=n<8 个字节可以访问, 例如char* p = new char[n], 将数值n写入到相应的1字节的shadow内存,尽管这个对象实际只占用5bytes,malloc的实现里[p+5, p+7]这尾部的3个字节的内存也不会再用于分配其他对象,所以通过指针p来越界访问最后3个字节的内存也是被允许的。

asan还会在程序申请的内存的前后,各增加一个redzone区域(n * 8bytes),用来解决overflow/underflow类问题。

free对象时,​asan不会立即把这个对象的内存释放掉,而是写入1个负数到该对象的shadown内存中,即将该对象成不可读写的状态, 并将它记录放到一个隔离区(book keeping)中​, 这样当有野指针或use-after-free的情况时,就能跟进shadow内存的状态,发现程序的异常;一段时间后如果程序没有异常,就会再释放隔离区中的对象。

编译器​​在对每个变量的load/store操作指令前都插入检查代码​,确认是否有overflow、underflow、use-after-free等问题。

实现

参考

  1. AddressSanitizer@github
  2. Android Address Sanitizer (ASan) 原理简介