• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_tsan -O1 %s -o %t
2 // RUN: %run %t 2>&1 | FileCheck %s
3 // RUN: %run %t arg 2>&1 | FileCheck %s
4 #include "java.h"
5 
6 jptr varaddr1_old;
7 jptr varaddr2_old;
8 jptr lockaddr1_old;
9 jptr lockaddr2_old;
10 jptr varaddr1_new;
11 jptr varaddr2_new;
12 jptr lockaddr1_new;
13 jptr lockaddr2_new;
14 
Thread(void * p)15 void *Thread(void *p) {
16   barrier_wait(&barrier);
17   __tsan_java_mutex_lock(lockaddr1_new);
18   *(char*)varaddr1_new = 43;
19   __tsan_java_mutex_unlock(lockaddr1_new);
20   __tsan_java_mutex_lock(lockaddr2_new);
21   *(char*)varaddr2_new = 43;
22   __tsan_java_mutex_unlock(lockaddr2_new);
23   return 0;
24 }
25 
main(int argc,char ** argv)26 int main(int argc, char **argv) {
27   barrier_init(&barrier, 2);
28   int const kHeapSize = 1024 * 1024;
29   void *jheap = malloc(kHeapSize);
30   jheap = (char*)jheap + 8;
31   __tsan_java_init((jptr)jheap, kHeapSize);
32   const int kBlockSize = 64;
33   int const kMove = 32;
34   varaddr1_old = (jptr)jheap;
35   lockaddr1_old = (jptr)jheap + 1;
36   varaddr2_old = (jptr)jheap + kBlockSize - 1;
37   lockaddr2_old = (jptr)jheap + kBlockSize - 16;
38   varaddr1_new = varaddr1_old + kMove;
39   lockaddr1_new = lockaddr1_old + kMove;
40   varaddr2_new = varaddr2_old + kMove;
41   lockaddr2_new = lockaddr2_old + kMove;
42   if (argc > 1) {
43     // Move memory backwards.
44     varaddr1_old += kMove;
45     lockaddr1_old += kMove;
46     varaddr2_old += kMove;
47     lockaddr2_old += kMove;
48     varaddr1_new -= kMove;
49     lockaddr1_new -= kMove;
50     varaddr2_new -= kMove;
51     lockaddr2_new -= kMove;
52   }
53   __tsan_java_alloc(varaddr1_old, kBlockSize);
54 
55   pthread_t th;
56   pthread_create(&th, 0, Thread, 0);
57 
58   __tsan_java_mutex_lock(lockaddr1_old);
59   *(char*)varaddr1_old = 43;
60   __tsan_java_mutex_unlock(lockaddr1_old);
61   __tsan_java_mutex_lock(lockaddr2_old);
62   *(char*)varaddr2_old = 43;
63   __tsan_java_mutex_unlock(lockaddr2_old);
64 
65   __tsan_java_move(varaddr1_old, varaddr1_new, kBlockSize);
66   barrier_wait(&barrier);
67   pthread_join(th, 0);
68   __tsan_java_free(varaddr1_new, kBlockSize);
69   fprintf(stderr, "DONE\n");
70   return __tsan_java_fini();
71 }
72 
73 // CHECK-NOT: WARNING: ThreadSanitizer: data race
74 // CHECK: DONE
75