1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include "java.h"
3
4int const kHeapSize = 1024 * 1024;
5
6void stress(jptr addr) {
7 for (jptr sz = 8; sz <= 32; sz <<= 1) {
8 for (jptr i = 0; i < kHeapSize / 4 / sz; i++) {
9 __tsan_java_alloc(ptr: addr + i * sz, size: sz);
10 }
11 __tsan_java_move(src: addr, dst: addr + kHeapSize / 2, size: kHeapSize / 4);
12 __tsan_java_free(ptr: addr + kHeapSize / 2, size: kHeapSize / 4);
13 }
14}
15
16void *Thread(void *p) {
17 stress(addr: (jptr)p);
18 return 0;
19}
20
21int main() {
22 jptr jheap = (jptr)malloc(size: kHeapSize + 8) + 8;
23 __tsan_java_init(heap_begin: jheap, heap_size: kHeapSize);
24 pthread_t th;
25 pthread_create(newthread: &th, attr: 0, start_routine: Thread, arg: (void*)(jheap + kHeapSize / 4));
26 stress(addr: jheap);
27 pthread_join(th: th, thread_return: 0);
28 if (__tsan_java_fini() != 0) {
29 fprintf(stderr, format: "FAILED\n");
30 return 1;
31 }
32 fprintf(stderr, format: "DONE\n");
33 return 0;
34}
35
36// CHECK-NOT: WARNING: ThreadSanitizer: data race
37// CHECK-NOT: FAILED
38// CHECK: DONE
39

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