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
6jptr varaddr1_old;
7jptr varaddr2_old;
8jptr lockaddr1_old;
9jptr lockaddr2_old;
10jptr varaddr1_new;
11jptr varaddr2_new;
12jptr lockaddr1_new;
13jptr lockaddr2_new;
14
15void *Thread(void *p) {
16 barrier_wait(barrier: &barrier);
17 __tsan_java_mutex_lock(addr: lockaddr1_new);
18 *(char*)varaddr1_new = 43;
19 __tsan_java_mutex_unlock(addr: lockaddr1_new);
20 __tsan_java_mutex_lock(addr: lockaddr2_new);
21 *(char*)varaddr2_new = 43;
22 __tsan_java_mutex_unlock(addr: lockaddr2_new);
23 return 0;
24}
25
26int main(int argc, char **argv) {
27 barrier_init(barrier: &barrier, count: 2);
28 int const kHeapSize = 1024 * 1024;
29 void *jheap = malloc(size: kHeapSize);
30 jheap = (char*)jheap + 8;
31 __tsan_java_init(heap_begin: (jptr)jheap, heap_size: 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(ptr: varaddr1_old, size: kBlockSize);
54
55 pthread_t th;
56 pthread_create(newthread: &th, attr: 0, start_routine: Thread, arg: 0);
57
58 __tsan_java_mutex_lock(addr: lockaddr1_old);
59 *(char*)varaddr1_old = 43;
60 __tsan_java_mutex_unlock(addr: lockaddr1_old);
61 __tsan_java_mutex_lock(addr: lockaddr2_old);
62 *(char*)varaddr2_old = 43;
63 __tsan_java_mutex_unlock(addr: lockaddr2_old);
64
65 __tsan_java_move(src: varaddr1_old, dst: varaddr1_new, size: kBlockSize);
66 barrier_wait(barrier: &barrier);
67 pthread_join(th: th, thread_return: 0);
68 __tsan_java_free(ptr: varaddr1_new, size: kBlockSize);
69 fprintf(stderr, format: "DONE\n");
70 return __tsan_java_fini();
71}
72
73// CHECK-NOT: WARNING: ThreadSanitizer: data race
74// CHECK: DONE
75

source code of compiler-rt/test/tsan/java_move_overlap.cpp