Address Sanitizer(ASan)是一個快速的內(nèi)存錯誤檢測工具曹货。這里說明它的用法祥楣。
參考資料
AddressSanitizer
https://github.com/google/sanitizers/wiki/AddressSanitizer
1. 簡介
Address Sanitizer(ASan)是一個快速的內(nèi)存錯誤檢測工具哆键。它非衬也罚快耍铜,只拖慢程序兩倍左右(比起Valgrind快多了)塞赂。它包括一個編譯器instrumentation模塊和一個提供malloc()/free()替代項的運行時庫泪勒。
從gcc 4.8開始,AddressSanitizer成為gcc的一部分减途。當然酣藻,要獲得更好的體驗,最好使用4.9及以上版本鳍置,因為gcc 4.8的AddressSanitizer還不完善辽剧,最大的缺點是沒有符號信息。
2. 使用步驟
- 用-fsanitize=address選項編譯和鏈接你的程序税产。
- 用-fno-omit-frame-pointer編譯怕轿,以得到更容易理解stack trace。
- 可選擇-O1或者更高的優(yōu)化級別編譯
gcc -fsanitize=address -fno-omit-frame-pointer -O1 -g use-after-free.c -o use-after-free
運行use-after-fee辟拷。如果發(fā)現(xiàn)了錯誤撞羽,就會打印出類似下面的信息:
==9901==ERROR: AddressSanitizer: heap-use-after-free on address 0x60700000dfb5
at pc 0x45917b bp 0x7fff4490c700 sp 0x7fff4490c6f8
READ of size 1 at 0x60700000dfb5 thread T0
#0 0x45917a in main use-after-free.c:5
#1 0x7fce9f25e76c in __libc_start_main /build/buildd/eglibc-2.15/csu/libc-start.c:226
#2 0x459074 in _start (a.out+0x459074)
0x60700000dfb5 is located 5 bytes inside of 80-byte region [0x60700000dfb0,0x60700000e000)
freed by thread T0 here:
#0 0x4441ee in __interceptor_free projects/compiler-rt/lib/asan/asan_malloc_linux.cc:64
#1 0x45914a in main use-after-free.c:4
#2 0x7fce9f25e76c in __libc_start_main /build/buildd/eglibc-2.15/csu/libc-start.c:226
previously allocated by thread T0 here:
#0 0x44436e in __interceptor_malloc projects/compiler-rt/lib/asan/asan_malloc_linux.cc:74
#1 0x45913f in main use-after-free.c:3
#2 0x7fce9f25e76c in __libc_start_main /build/buildd/eglibc-2.15/csu/libc-start.c:226
SUMMARY: AddressSanitizer: heap-use-after-free use-after-free.c:5 main
- 第一部分(ERROR)指出錯誤類型是heap-use-after-free;
- 第二部分(READ), 指出線程名thread T0衫冻,操作為READ诀紊,發(fā)生的位置是use-after-free.c:5。
- 該heapk塊之前已經(jīng)在use-after-free.c:4被釋放了隅俘;
- 該heap塊是在use-fater-free.c:3分配
- 第三部分 (SUMMARY) 前面輸出的概要說明邻奠。
3. 錯誤類型
3.1 (heap) use after free 釋放后使用
下面的代碼中,分配array數(shù)組并釋放为居,然后返回它的一個元素碌宴。
5 int main (int argc, char** argv)
6 {
7 int* array = new int[100];
8 delete []array;
9 return array[1];
10 }
下面的錯誤信息與前面的“使用步驟”一節(jié)中的類似。
==3189==ERROR: AddressSanitizer: heap-use-after-free on address 0x61400000fe44
at pc 0x0000004008f1 bp 0x7ffc9b6e2630 sp 0x7ffc9b6e2620
READ of size 4 at 0x61400000fe44 thread T0
#0 0x4008f0 in main /home/ron/dev/as/use_after_free.cpp:9
#1 0x7f3763aa882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#2 0x4007b8 in _start (/home/ron/dev/as/build/use_after_free+0x4007b8)
0x61400000fe44 is located 4 bytes inside of 400-byte region [0x61400000fe40,0x61400000ffd0)
freed by thread T0 here:
#0 0x7f3763ef1caa in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99caa)
#1 0x4008b5 in main /home/ron/dev/as/use_after_free.cpp:8
#2 0x7f3763aa882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
previously allocated by thread T0 here:
#0 0x7f3763ef16b2 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x996b2)
#1 0x40089e in main /home/ron/dev/as/use_after_free.cpp:7
#2 0x7f3763aa882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
SUMMARY: AddressSanitizer: heap-use-after-free /home/ron/dev/as/use_after_free.cpp:9 main
3.2 heap buffer overflow 堆緩存訪問溢出
如下代碼中蒙畴,訪問的位置超出堆上數(shù)組array的邊界贰镣。
2 int main (int argc, char** argv)
3 {
4 int* array = new int[100];
5 int res = array[100];
6 delete [] array;
7 return res;
8 }
下面的錯誤信息指出:
- 錯誤類型是heap-buffer-overflow
- 不合法操作READ發(fā)生在線程T0, heap_buf_overflow.cpp:5
- heap塊分配發(fā)生在heap_buf_overflow.cpp
==3322==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61400000ffd0
at pc 0x0000004008e0 bp 0x7ffeddce53a0 sp 0x7ffeddce5390
READ of size 4 at 0x61400000ffd0 thread T0
#0 0x4008df in main /home/ron/dev/as/heap_buf_overflow.cpp:5
#1 0x7f3b83d0882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#2 0x4007b8 in _start (/home/ron/dev/as/build/heap_buf_overflow+0x4007b8)
0x61400000ffd0 is located 0 bytes to the right of 400-byte region [0x61400000fe40,0x61400000ffd0)
allocated by thread T0 here:
#0 0x7f3b841516b2 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x996b2)
#1 0x40089e in main /home/ron/dev/as/heap_buf_overflow.cpp:4
#2 0x7f3b83d0882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/ron/dev/as/heap_buf_overflow.cpp:5 main
3.2 stack buffer overflow 棧緩存訪問溢出
如下代碼中,訪問的位置超出棧上數(shù)組array的邊界膳凝。
2 int main (int argc, char** argv)
3 {
4 int array[100];
5 return array[100];
6 }
下面的錯誤信息指出:
- 錯誤類型是stack-buffer-overflow
- 不合法操作READ發(fā)生在線程T0, stack_buf_overflow.cpp:5
- 棧塊在線程T0的棧上432偏移位置上碑隆。
==3389==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffd061fa4a0
at pc 0x0000004009ff bp 0x7ffd061fa2d0 sp 0x7ffd061fa2c0
READ of size 4 at 0x7ffd061fa4a0 thread T0
#0 0x4009fe in main /home/ron/dev/as/stack_buf_overflow.cpp:5
#1 0x7fbade4e882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#2 0x400858 in _start (/home/ron/dev/as/build/stack_buf_overflow+0x400858)
Address 0x7ffd061fa4a0 is located in stack of thread T0 at offset 432 in frame
#0 0x400935 in main /home/ron/dev/as/stack_buf_overflow.cpp:3
This frame has 1 object(s):
[32, 432) 'array' <== Memory access at offset 432 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow /home/ron/dev/as/stack_buf_overflow.cpp:5 main
3.3 global buffer overflow 全局緩沖訪問溢出
如下代碼中,訪問的位置超出全局數(shù)組array的邊界蹬音。
2 int array[100];
3
4 int main (int argc, char** argv)
5 {
6 return array[100];
7 }
下面的錯誤信息指出:
- 錯誤類型是global-buffer-overflow
- 不合法操作READ發(fā)生在線程T0, global_buf_overflow.cpp:6
- 緩存塊在global_buf_overflow.cpp:2 定義上煤。
==3499==ERROR: AddressSanitizer: global-buffer-overflow on address 0x000000601270
at pc 0x000000400915 bp 0x7ffd8e80c020 sp 0x7ffd8e80c010
READ of size 4 at 0x000000601270 thread T0
#0 0x400914 in main /home/ron/dev/as/global_buf_overflow.cpp:6
#1 0x7f613c1c882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#2 0x400808 in _start (/home/ron/dev/as/build/global_buf_overflow+0x400808)
0x000000601270 is located 0 bytes to the right of global variable 'array' defined in
'/home/ron/dev/as/global_buf_overflow.cpp:2:5' (0x6010e0) of size 400
SUMMARY: AddressSanitizer: global-buffer-overflow /home/ron/dev/as/global_buf_overflow.cpp:6 main
3.4 use after return
3.5 use after scope
3.6 initializations order bugs
3.7 memory leaks 內(nèi)存泄露
檢測內(nèi)存的LeakSanitizer是集成在AddressSanitizer中的一個相對獨立的工具,它工作在檢查過程的最后階段祟绊。
下面代碼中楼入,p指向的內(nèi)存沒有釋放。
4 void* p;
5
6 int main ()
7 {
8 p = malloc (7);
9 p = 0;
10 return 0;
11 }
下面的錯誤信息指出 detected memory leaks
- 內(nèi)存在mem_leak.cpp:8分配
- 緩存塊在global_buf_overflow.cpp:2 定義牧抽。
==4088==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 7 byte(s) in 1 object(s) allocated from:
#0 0x7ff9ae510602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
#1 0x4008d3 in main /home/ron/dev/as/mem_leak.cpp:8
#2 0x7ff9ae0c882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).
目前嘉熊,并不是所有的平臺都默認檢測內(nèi)存泄露,可以指定ASAN_OPTIONS開啟如下:
ASAN_OPTIONS=detect_leaks=1 yourapp
而且不是所有的平臺支持檢測內(nèi)存泄露扬舒,比如ARM阐肤,就會得到這樣的提示:
==1901==AddressSanitizer: detect_leaks is not supported on this platform.
4. 工作原理
4.1 概要說明
AddressSanitizer的運行時庫替換malloc()/free()。分配緩存前后的空間標記為poisoned讲坎,已經(jīng)被釋放的緩存也被標記為poisoned孕惜。內(nèi)存訪問的代碼都被編譯器替換如下:
替換之前:
*address = ...;
替換之后:
if (IsPoisoned(address))
{
ReportError(address, kAccessSize, kIsWrite);
}
*address = ...;
訪問之前檢查訪問地址是否poisoned,如果是晨炕,報告錯誤衫画。
4.2 memory mapping 和 instrumentation
進程的虛擬地址空間劃分為兩個不相連的部分:
- main application memory (Mem)。這是程序自身代碼使用的內(nèi)存瓮栗;
- Shadow memory (Shadow)削罩。這里放的是shadow value(meta data)。從Mem到Shadow之間有映射關(guān)系费奸。將Mem的一個字節(jié)標記為poisoned弥激,其實就是在對應(yīng)的Shadow內(nèi)存中寫入指定值。
偽代碼如下愿阐。它先從Mem中地址計算對應(yīng)的Shadow地址微服。
shadow_address = MemToShadow (address);
if (ShadowIsPoisoned(shadow_address))
{
ReportError (address, kAccessSize, kIsWrite);
}
4.3 mapping
Mem中的8字節(jié)映射到Shadow memory中是1字節(jié)。
這個字節(jié)可能有9種不同的值:
- 所有8字節(jié)都是unpoisoned的缨历,則值為0以蕴;
- 所有8字節(jié)都是poisoned的,則值為負戈二;
- 前k字節(jié)為unpoisoned舒裤,后面8-k字節(jié)為poisoned, 則值為k觉吭。
- malloc()分配的內(nèi)存總是8字節(jié)的倍數(shù)腾供,如果要分配的緩存不是8字節(jié)的倍數(shù),則尾部的8個字節(jié)poisoned狀態(tài)不同鲜滩。比如分配13字節(jié)伴鳖,會得到兩個8字節(jié)。前1個全是unpoisoned徙硅,后一個只有前5個字節(jié)是unpoisoned榜聂,后3個字節(jié)是poisoned。
4.4 棧的處理
為了捕捉棧的訪問溢出嗓蘑,AddressSanitizer在緩存前后加上保護區(qū)须肆。這里可以看到設(shè)置對應(yīng)Shadow memory的代碼匿乃。
改編之前為:
void foo()
{
char a[8];
...
return;
}
改編之后為:
void foo()
{
char redzone1[32]; // 32-byte aligned
char a[8]; // 32-byte aligned
char redzone2[24];
char redzone3[32]; // 32-byte aligned
int *shadow_base = MemToShadow(redzone1);
shadow_base[0] = 0xffffffff; // poison redzone1
shadow_base[1] = 0xffffff00; // poison redzone2, unpoison 'a'
shadow_base[2] = 0xffffffff; // poison redzone3
...
shadow_base[0] = shadow_base[1] = shadow_base[2] = 0; // unpoison all
return;
}
4.5 malloc()/free()的處理
運行時庫用自己的函數(shù)替換malloc() / free()。
- malloc()在緩存前后分配保護區(qū)豌汇。緩存本身標記為unpoisoned幢炸,保護區(qū)標記為poisoned。
- free() 將整個區(qū)域拒贱,包括緩存和保護區(qū)宛徊,都標記為poisoned,并將該區(qū)域放入一個特別的隊列中逻澳,以保證malloc()在相當長的時間內(nèi)不會再次使用它)闸天。
相關(guān)鏈接
GDB 常用法
GDB 調(diào)試Coredump問題
嵌入式開發(fā)中GDB調(diào)試Coredump問題
嵌入式開發(fā)中GDB串口遠程調(diào)試
用backtrace()調(diào)試coredump問題
Valgrind memcheck 用法
Address Sanitizer 用法