• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 #include "java.h"
3 
4 jptr varaddr;
5 jptr lockaddr;
6 jptr varaddr2;
7 jptr lockaddr2;
8 
Thread(void * p)9 void *Thread(void *p) {
10   barrier_wait(&barrier);
11   __tsan_java_mutex_lock(lockaddr2);
12   *(int*)varaddr2 = 42;
13   __tsan_java_mutex_unlock(lockaddr2);
14   return 0;
15 }
16 
main()17 int main() {
18   barrier_init(&barrier, 2);
19   int const kHeapSize = 1024 * 1024;
20   jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
21   __tsan_java_init(jheap, kHeapSize);
22   const int kBlockSize = 64;
23   int const kMove = 1024;
24   __tsan_java_alloc(jheap, kBlockSize);
25   varaddr = jheap;
26   lockaddr = jheap + 46;
27   varaddr2 = varaddr + kMove;
28   lockaddr2 = lockaddr + kMove;
29   pthread_t th;
30   pthread_create(&th, 0, Thread, 0);
31   __tsan_java_mutex_lock(lockaddr);
32   *(int*)varaddr = 43;
33   __tsan_java_mutex_unlock(lockaddr);
34   __tsan_java_move(varaddr, varaddr2, kBlockSize);
35   barrier_wait(&barrier);
36   pthread_join(th, 0);
37   __tsan_java_free(varaddr2, kBlockSize);
38   fprintf(stderr, "DONE\n");
39   return __tsan_java_fini();
40 }
41 
42 // CHECK-NOT: WARNING: ThreadSanitizer: data race
43 // CHECK: DONE
44