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
6static void verify_find(jptr from, jptr to, jptr expected_addr,
7 jptr expected_size) {
8 jptr addr = from;
9 jptr size = __tsan_java_find(&addr, to);
10 if (expected_size) {
11 if (!size) {
12 fprintf(stderr, "FAILED: range: [%p..%p): found nothing\n", (void *)from,
13 (void *)to);
14 return;
15 } else if (expected_size != size) {
16 fprintf(stderr, "FAILED: range: [%p..%p): wrong size, %lu instead of %lu\n",
17 (void *)from, (void *)to, size, expected_size);
18 return;
19 }
20 } else if (size) {
21 fprintf(stderr,
22 "FAILED: range [%p..%p): did not expect to find anything here\n",
23 (void *)from, (void *)to);
24 return;
25 } else {
26 return;
27 }
28 if (expected_addr != addr) {
29 fprintf(
30 stderr,
31 "FAILED: range [%p..%p): expected to find object at %p, found at %p\n",
32 (void *)from, (void *)to, (void *)expected_addr, (void *)addr);
33 }
34}
35
36int main() {
37 const jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
38 const jptr jheap_end = jheap + kHeapSize;
39 __tsan_java_init(jheap, kHeapSize);
40 const jptr addr1 = jheap;
41 const int size1 = 16;
42 __tsan_java_alloc(jheap, size1);
43
44 const jptr addr2 = addr1 + size1;
45 const int size2 = 32;
46 __tsan_java_alloc(jheap + size1, size2);
47
48 const jptr addr3 = addr2 + size2;
49 const int size3 = 1024;
50 __tsan_java_alloc(jheap + size1 + size2, size3);
51
52 const jptr addr4 = addr3 + size3;
53
54 verify_find(jheap, jheap_end, addr1, size1);
55 verify_find(jheap + 8, jheap_end, addr2, size2);
56 verify_find(addr2 + 8, jheap_end, addr3, size3);
57 verify_find(addr3 + 8, jheap_end, 0, 0);
58
59 __tsan_java_move(addr2, addr4, size2);
60 verify_find(jheap + 8, jheap_end, addr3, size3);
61 verify_find(addr3 + 8, jheap_end, addr4, size2);
62 verify_find(addr4 + 8, jheap_end, 0, 0);
63
64 fprintf(stderr, "DONE\n");
65 return 0;
66}
67
68// CHECK-NOT: FAILED
69// CHECK: DONE
70