1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "java.h"
3
4void *Thread(void *p) {
5 barrier_wait(barrier: &barrier);
6 *(int*)p = 42;
7 return 0;
8}
9
10int main() {
11 barrier_init(barrier: &barrier, count: 2);
12 int const kHeapSize = 1024 * 1024;
13 jptr jheap = (jptr)malloc(size: kHeapSize + 8) + 8;
14 __tsan_java_init(heap_begin: jheap, heap_size: kHeapSize);
15 const int kBlockSize = 16;
16 __tsan_java_alloc(ptr: jheap, size: kBlockSize);
17 pthread_t th;
18 pthread_create(newthread: &th, attr: 0, start_routine: Thread, arg: (void*)jheap);
19 *(int*)jheap = 43;
20 barrier_wait(barrier: &barrier);
21 pthread_join(th: th, thread_return: 0);
22 __tsan_java_free(ptr: jheap, size: kBlockSize);
23 fprintf(stderr, format: "DONE\n");
24 return __tsan_java_fini();
25}
26
27// CHECK: WARNING: ThreadSanitizer: data race
28// CHECK: DONE
29

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