1 // RUN: %clangxx_asan -m64 -O0 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s 2 // RUN: %clangxx_asan -m64 -O1 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s 3 // RUN: %clangxx_asan -m64 -O2 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s 4 // RUN: %clangxx_asan -m64 -O3 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s 5 // RUN: %clangxx_asan -m32 -O0 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s 6 // RUN: %clangxx_asan -m32 -O1 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s 7 // RUN: %clangxx_asan -m32 -O2 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s 8 // RUN: %clangxx_asan -m32 -O3 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s 9 10 #include <pthread.h> 11 12 int *x; 13 14 void *AllocThread(void *arg) { 15 x = new int; 16 *x = 42; 17 return NULL; 18 } 19 20 void *FreeThread(void *arg) { 21 delete x; 22 return NULL; 23 } 24 25 void *AccessThread(void *arg) { 26 *x = 43; // BOOM 27 return NULL; 28 } 29 30 typedef void* (*callback_type)(void* arg); 31 32 void *RunnerThread(void *function) { 33 pthread_t thread; 34 pthread_create(&thread, NULL, (callback_type)function, NULL); 35 pthread_join(thread, NULL); 36 return NULL; 37 } 38 39 void RunThread(callback_type function) { 40 pthread_t runner; 41 pthread_create(&runner, NULL, RunnerThread, (void*)function); 42 pthread_join(runner, NULL); 43 } 44 45 int main(int argc, char *argv[]) { 46 RunThread(AllocThread); 47 RunThread(FreeThread); 48 RunThread(AccessThread); 49 return (x != 0); 50 } 51 52 // CHECK: AddressSanitizer: heap-use-after-free 53 // CHECK: WRITE of size 4 at 0x{{.*}} thread T[[ACCESS_THREAD:[0-9]+]] 54 // CHECK: freed by thread T[[FREE_THREAD:[0-9]+]] here: 55 // CHECK: previously allocated by thread T[[ALLOC_THREAD:[0-9]+]] here: 56 // CHECK: Thread T[[ACCESS_THREAD]] created by T[[ACCESS_RUNNER:[0-9]+]] here: 57 // CHECK: Thread T[[ACCESS_RUNNER]] created by T0 here: 58 // CHECK: Thread T[[FREE_THREAD]] created by T[[FREE_RUNNER:[0-9]+]] here: 59 // CHECK: Thread T[[FREE_RUNNER]] created by T0 here: 60 // CHECK: Thread T[[ALLOC_THREAD]] created by T[[ALLOC_RUNNER:[0-9]+]] here: 61 // CHECK: Thread T[[ALLOC_RUNNER]] created by T0 here: 62