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 varaddr1_new;
9jptr varaddr2_new;
10
11void *Thread(void *p) {
12 barrier_wait(barrier: &barrier);
13 *(int*)varaddr1_new = 43;
14 *(int*)varaddr2_new = 43;
15 return 0;
16}
17
18int main(int argc, char **argv) {
19 barrier_init(barrier: &barrier, count: 2);
20 int const kHeapSize = 1024 * 1024;
21 void *jheap = malloc(size: kHeapSize);
22 jheap = (char*)jheap + 8;
23 __tsan_java_init(heap_begin: (jptr)jheap, heap_size: kHeapSize);
24 const int kBlockSize = 64;
25 int const kMove = 32;
26 varaddr1_old = (jptr)jheap;
27 varaddr2_old = (jptr)jheap + kBlockSize - 1;
28 varaddr1_new = varaddr1_old + kMove;
29 varaddr2_new = varaddr2_old + kMove;
30 if (argc > 1) {
31 // Move memory backwards.
32 varaddr1_old += kMove;
33 varaddr2_old += kMove;
34 varaddr1_new -= kMove;
35 varaddr2_new -= kMove;
36 }
37 __tsan_java_alloc(ptr: varaddr1_old, size: kBlockSize);
38
39 pthread_t th;
40 pthread_create(newthread: &th, attr: 0, start_routine: Thread, arg: 0);
41
42 *(int*)varaddr1_old = 43;
43 *(int*)varaddr2_old = 43;
44
45 __tsan_java_move(src: varaddr1_old, dst: varaddr1_new, size: kBlockSize);
46 barrier_wait(barrier: &barrier);
47 pthread_join(th: th, thread_return: 0);
48 __tsan_java_free(ptr: varaddr1_new, size: kBlockSize);
49 fprintf(stderr, format: "DONE\n");
50 return __tsan_java_fini();
51}
52
53// Note: there is a race on the moved object (which we used to detect),
54// but now __tsan_java_move resets the object shadow, so we don't detect it anymore.
55// CHECK-NOT: WARNING: ThreadSanitizer: data race
56// CHECK: DONE
57

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