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 sleep(1);
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 int const kHeapSize = 1024 * 1024;
28 void *jheap = malloc(kHeapSize);
29 jheap = (char*)jheap + 8;
30 __tsan_java_init((jptr)jheap, kHeapSize);
31 const int kBlockSize = 64;
32 int const kMove = 32;
33 varaddr1_old = (jptr)jheap;
34 lockaddr1_old = (jptr)jheap + 1;
35 varaddr2_old = (jptr)jheap + kBlockSize - 1;
36 lockaddr2_old = (jptr)jheap + kBlockSize - 16;
37 varaddr1_new = varaddr1_old + kMove;
38 lockaddr1_new = lockaddr1_old + kMove;
39 varaddr2_new = varaddr2_old + kMove;
40 lockaddr2_new = lockaddr2_old + kMove;
41 if (argc > 1) {
42 // Move memory backwards.
43 varaddr1_old += kMove;
44 lockaddr1_old += kMove;
45 varaddr2_old += kMove;
46 lockaddr2_old += kMove;
47 varaddr1_new -= kMove;
48 lockaddr1_new -= kMove;
49 varaddr2_new -= kMove;
50 lockaddr2_new -= kMove;
51 }
52 __tsan_java_alloc(varaddr1_old, kBlockSize);
53
54 pthread_t th;
55 pthread_create(&th, 0, Thread, 0);
56
57 __tsan_java_mutex_lock(lockaddr1_old);
58 *(char*)varaddr1_old = 43;
59 __tsan_java_mutex_unlock(lockaddr1_old);
60 __tsan_java_mutex_lock(lockaddr2_old);
61 *(char*)varaddr2_old = 43;
62 __tsan_java_mutex_unlock(lockaddr2_old);
63
64 __tsan_java_move(varaddr1_old, varaddr1_new, kBlockSize);
65 pthread_join(th, 0);
66 __tsan_java_free(varaddr1_new, kBlockSize);
67 printf("DONE\n");
68 return __tsan_java_fini();
69 }
70
71 // CHECK-NOT: WARNING: ThreadSanitizer: data race
72 // CHECK: DONE
73