1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2
3#include "java.h"
4#include <errno.h>
5#include <sys/mman.h>
6
7int main() {
8 // Test a non-regular kHeapSize
9 // Previously __tsan_java_init failed because it encountered non-zero meta
10 // shadow for the destination.
11 size_t const kPageSize = sysconf(_SC_PAGESIZE);
12 int const kSize = kPageSize - 1;
13 jptr jheap2 = (jptr)mmap(addr: 0, len: kSize, PROT_READ | PROT_WRITE,
14 MAP_ANON | MAP_PRIVATE, fd: -1, offset: 0);
15 if (jheap2 == (jptr)MAP_FAILED)
16 return printf(format: "mmap failed with %d\n", errno);
17 __atomic_store_n((int *)(jheap2 + kSize - 3), 1, __ATOMIC_RELEASE);
18 // Due to the previous incorrect meta-end calculation, the following munmap
19 // did not clear the tail meta shadow.
20 munmap(addr: (void *)jheap2, len: kSize);
21 int const kHeapSize2 = kSize + 1;
22 jheap2 = (jptr)mmap(addr: (void *)jheap2, len: kHeapSize2, PROT_READ | PROT_WRITE,
23 MAP_ANON | MAP_PRIVATE, fd: -1, offset: 0);
24 if (jheap2 == (jptr)MAP_FAILED)
25 return printf(format: "second mmap failed with %d\n", errno);
26 __tsan_java_init(heap_begin: jheap2, heap_size: kHeapSize2);
27 __tsan_java_move(src: jheap2, dst: jheap2 + kHeapSize2 - 8, size: 8);
28 fprintf(stderr, format: "DONE\n");
29 return __tsan_java_fini();
30}
31
32// CHECK-NOT: WARNING: ThreadSanitizer: data race
33// CHECK: DONE
34

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