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