free_race.c revision 6a211c5814e25d6745a5058cc0e499e5235d3821
1// RUN: %clang_tsan -O1 %s -o %t 2// RUN: %deflake %run %t | FileCheck %s --check-prefix=CHECK-NOZUPP 3// RUN: TSAN_OPTIONS="suppressions=%s.supp print_suppressions=1" %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SUPP 4 5#include <pthread.h> 6#include <stdlib.h> 7#include <stdio.h> 8#include <stddef.h> 9#include <unistd.h> 10 11int *mem; 12pthread_mutex_t mtx; 13 14void *Thread1(void *x) { 15 pthread_mutex_lock(&mtx); 16 free(mem); 17 pthread_mutex_unlock(&mtx); 18 return NULL; 19} 20 21void *Thread2(void *x) { 22 sleep(1); 23 pthread_mutex_lock(&mtx); 24 mem[0] = 42; 25 pthread_mutex_unlock(&mtx); 26 return NULL; 27} 28 29int main() { 30 mem = (int*)malloc(100); 31 pthread_mutex_init(&mtx, 0); 32 pthread_t t; 33 pthread_create(&t, NULL, Thread1, NULL); 34 Thread2(0); 35 pthread_join(t, NULL); 36 pthread_mutex_destroy(&mtx); 37 return 0; 38} 39 40// CHECK-NOZUPP: WARNING: ThreadSanitizer: heap-use-after-free 41// CHECK-NOZUPP: Write of size 4 at {{.*}} by main thread{{.*}}: 42// CHECK-NOZUPP: #0 Thread2 43// CHECK-NOZUPP: #1 main 44// CHECK-NOZUPP: Previous write of size 8 at {{.*}} by thread T1{{.*}}: 45// CHECK-NOZUPP: #0 free 46// CHECK-NOZUPP: #{{(1|2)}} Thread1 47// CHECK-NOZUPP: SUMMARY: ThreadSanitizer: heap-use-after-free{{.*}}Thread2 48// CHECK-SUPP: ThreadSanitizer: Matched 1 suppressions 49// CHECK-SUPP: 1 race:^Thread2$ 50