• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_tsan -O1 %s -o %t
2 // RUN: not %t 2>&1 | FileCheck %s --check-prefix=NOZUPP
3 // RUN: TSAN_OPTIONS="suppressions=%s.supp print_suppressions=1" %t 2>&1 | FileCheck %s --check-prefix=SUPP
4 
5 #include <pthread.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <stddef.h>
9 #include <unistd.h>
10 
11 int *mem;
12 pthread_mutex_t mtx;
13 
Thread1(void * x)14 void *Thread1(void *x) {
15   pthread_mutex_lock(&mtx);
16   free(mem);
17   pthread_mutex_unlock(&mtx);
18   return NULL;
19 }
20 
Thread2(void * x)21 void *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 
main()29 int 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