• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_tsan -O1 %s -o %t
2 // RUN: %deflake %run %t | FileCheck %s
3 // RUN: %deflake %run %t arg | FileCheck %s
4 #include "java.h"
5 
6 jptr varaddr1_old;
7 jptr varaddr2_old;
8 jptr varaddr1_new;
9 jptr varaddr2_new;
10 
Thread(void * p)11 void *Thread(void *p) {
12   sleep(1);
13   *(int*)varaddr1_new = 43;
14   *(int*)varaddr2_new = 43;
15   return 0;
16 }
17 
main(int argc,char ** argv)18 int main(int argc, char **argv) {
19   int const kHeapSize = 1024 * 1024;
20   void *jheap = malloc(kHeapSize);
21   jheap = (char*)jheap + 8;
22   __tsan_java_init((jptr)jheap, kHeapSize);
23   const int kBlockSize = 64;
24   int const kMove = 32;
25   varaddr1_old = (jptr)jheap;
26   varaddr2_old = (jptr)jheap + kBlockSize - 1;
27   varaddr1_new = varaddr1_old + kMove;
28   varaddr2_new = varaddr2_old + kMove;
29   if (argc > 1) {
30     // Move memory backwards.
31     varaddr1_old += kMove;
32     varaddr2_old += kMove;
33     varaddr1_new -= kMove;
34     varaddr2_new -= kMove;
35   }
36   __tsan_java_alloc(varaddr1_old, kBlockSize);
37 
38   pthread_t th;
39   pthread_create(&th, 0, Thread, 0);
40 
41   *(int*)varaddr1_old = 43;
42   *(int*)varaddr2_old = 43;
43 
44   __tsan_java_move(varaddr1_old, varaddr1_new, kBlockSize);
45   pthread_join(th, 0);
46   __tsan_java_free(varaddr1_new, kBlockSize);
47   printf("DONE\n");
48   return __tsan_java_fini();
49 }
50 
51 // CHECK: WARNING: ThreadSanitizer: data race
52 // CHECK: WARNING: ThreadSanitizer: data race
53 // CHECK: DONE
54