Home | History | Annotate | Download | only in lit_tests
      1 // RUN: %clang_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s
      2 #include <pthread.h>
      3 #include <stdio.h>
      4 #include <unistd.h>
      5 
      6 int Global;
      7 
      8 void __attribute__((noinline)) foo1() {
      9   Global = 42;
     10 }
     11 
     12 void __attribute__((noinline)) bar1() {
     13   volatile int tmp = 42; (void)tmp;
     14   foo1();
     15 }
     16 
     17 void __attribute__((noinline)) foo2() {
     18   volatile int v = Global; (void)v;
     19 }
     20 
     21 void __attribute__((noinline)) bar2() {
     22   volatile int tmp = 42; (void)tmp;
     23   foo2();
     24 }
     25 
     26 void *Thread1(void *x) {
     27   sleep(1);
     28   bar1();
     29   return NULL;
     30 }
     31 
     32 void *Thread2(void *x) {
     33   bar2();
     34   return NULL;
     35 }
     36 
     37 void StartThread(pthread_t *t, void *(*f)(void*)) {
     38   pthread_create(t, NULL, f, NULL);
     39 }
     40 
     41 int main() {
     42   pthread_t t[2];
     43   StartThread(&t[0], Thread1);
     44   StartThread(&t[1], Thread2);
     45   pthread_join(t[0], NULL);
     46   pthread_join(t[1], NULL);
     47   return 0;
     48 }
     49 
     50 // CHECK:      WARNING: ThreadSanitizer: data race
     51 // CHECK-NEXT:   Write of size 4 at {{.*}} by thread T1:
     52 // CHECK-NEXT:     #0 foo1{{.*}} {{.*}}simple_stack.c:9{{(:3)?}} ({{.*}})
     53 // CHECK-NEXT:     #1 bar1{{.*}} {{.*}}simple_stack.c:14{{(:3)?}} ({{.*}})
     54 // CHECK-NEXT:     #2 Thread1{{.*}} {{.*}}simple_stack.c:28{{(:3)?}} ({{.*}})
     55 // CHECK:        Previous read of size 4 at {{.*}} by thread T2:
     56 // CHECK-NEXT:     #0 foo2{{.*}} {{.*}}simple_stack.c:18{{(:26)?}} ({{.*}})
     57 // CHECK-NEXT:     #1 bar2{{.*}} {{.*}}simple_stack.c:23{{(:3)?}} ({{.*}})
     58 // CHECK-NEXT:     #2 Thread2{{.*}} {{.*}}simple_stack.c:33{{(:3)?}} ({{.*}})
     59 // CHECK:        Thread T1 (tid={{.*}}, running) created by main thread at:
     60 // CHECK-NEXT:     #0 pthread_create {{.*}} ({{.*}})
     61 // CHECK-NEXT:     #1 StartThread{{.*}} {{.*}}simple_stack.c:38{{(:3)?}} ({{.*}})
     62 // CHECK-NEXT:     #2 main{{.*}} {{.*}}simple_stack.c:43{{(:3)?}} ({{.*}})
     63 // CHECK:        Thread T2 ({{.*}}) created by main thread at:
     64 // CHECK-NEXT:     #0 pthread_create {{.*}} ({{.*}})
     65 // CHECK-NEXT:     #1 StartThread{{.*}} {{.*}}simple_stack.c:38{{(:3)?}} ({{.*}})
     66 // CHECK-NEXT:     #2 main{{.*}} {{.*}}simple_stack.c:44{{(:3)?}} ({{.*}})
     67