1// RUN: %clangxx_tsan -O1 %s -o %t
2// RUN: %deflake %run %t 2>&1 | FileCheck %s
3// RUN: %deflake %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);
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, 2);
20 int const kHeapSize = 1024 * 1024;
21 void *jheap = malloc(kHeapSize);
22 jheap = (char*)jheap + 8;
23 __tsan_java_init((jptr)jheap, 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(varaddr1_old, kBlockSize);
38
39 pthread_t th;
40 pthread_create(&th, 0, Thread, 0);
41
42 *(int*)varaddr1_old = 43;
43 *(int*)varaddr2_old = 43;
44
45 __tsan_java_move(varaddr1_old, varaddr1_new, kBlockSize);
46 barrier_wait(&barrier);
47 pthread_join(th, 0);
48 __tsan_java_free(varaddr1_new, kBlockSize);
49 fprintf(stderr, "DONE\n");
50 return __tsan_java_fini();
51}
52
53// CHECK: WARNING: ThreadSanitizer: data race
54// CHECK: WARNING: ThreadSanitizer: data race
55// CHECK: DONE
56