1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include "java.h"
3#include <errno.h>
4#include <sys/mman.h>
5
6int main() {
7 // Test that munmap interceptor resets meta shadow for the memory range.
8 // Previously __tsan_java_move failed because it encountered non-zero meta
9 // shadow for the destination.
10 int const kHeapSize = 1024 * 1024;
11 jptr jheap = (jptr)mmap(addr: 0, len: kHeapSize, PROT_READ | PROT_WRITE,
12 MAP_ANON | MAP_PRIVATE, fd: -1, offset: 0);
13 if (jheap == (jptr)MAP_FAILED)
14 return printf(format: "mmap failed with %d\n", errno);
15 __atomic_store_n((int*)jheap, 1, __ATOMIC_RELEASE);
16 munmap(addr: (void*)jheap, len: kHeapSize);
17 jheap = (jptr)mmap(addr: (void*)jheap, len: kHeapSize, PROT_READ | PROT_WRITE,
18 MAP_ANON | MAP_PRIVATE, fd: -1, offset: 0);
19 if (jheap == (jptr)MAP_FAILED)
20 return printf(format: "second mmap failed with %d\n", errno);
21 __tsan_java_init(heap_begin: jheap, heap_size: kHeapSize);
22 __tsan_java_move(src: jheap + 16, dst: jheap, size: 16);
23 fprintf(stderr, format: "DONE\n");
24 return __tsan_java_fini();
25}
26
27// CHECK-NOT: WARNING: ThreadSanitizer: data race
28// CHECK: DONE
29

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