[docs] Fix build-docs.sh
[llvm-project.git] / compiler-rt / test / tsan / java_symbolization.cpp
blobe6a3ee8ac1536f016a2ea7e6fbb403201b69faa8
1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2 #include "java.h"
3 #include <memory.h>
5 #if (__APPLE__)
6 __attribute__((weak)) // Required for dyld macOS 12.0+
7 #endif
8 __attribute__((disable_sanitizer_instrumentation))
9 extern "C" void
10 __tsan_symbolize_external_ex(jptr pc,
11 void (*add_frame)(void *, const char *,
12 const char *, int, int),
13 void *ctx) {
14 if (pc == (1234 | kExternalPCBit)) {
15 add_frame(ctx, "MyInnerFunc", "MyInnerFile.java", 1234, 56);
16 add_frame(ctx, "MyOuterFunc", "MyOuterFile.java", 4321, 65);
18 if (pc == (2345 | kExternalPCBit)) {
19 add_frame(ctx, "Caller1", "CallerFile.java", 111, 22);
20 add_frame(ctx, "Caller2", "CallerFile.java", 333, 44);
22 if (pc == (3456 | kExternalPCBit)) {
23 add_frame(ctx, "Allocer1", "Alloc.java", 11, 222);
24 add_frame(ctx, "Allocer2", "Alloc.java", 33, 444);
28 void *Thread(void *p) {
29 barrier_wait(&barrier);
30 __tsan_func_entry(2345 | kExternalPCBit);
31 __tsan_write1_pc((jptr)p + 16, 1234 | kExternalPCBit);
32 __tsan_func_exit();
33 return 0;
36 jptr const kHeapSize = 64 * 1024;
37 jptr java_heap[kHeapSize];
39 int main() {
40 barrier_init(&barrier, 2);
41 jptr jheap = (jptr)java_heap;
42 __tsan_java_init(jheap, kHeapSize);
43 const int kBlockSize = 32;
44 __tsan_func_entry(3456 | kExternalPCBit);
45 __tsan_java_alloc(jheap, kBlockSize);
46 print_address("addr:", 2, jheap, jheap + 16);
47 __tsan_func_exit();
48 pthread_t th;
49 pthread_create(&th, 0, Thread, (void*)jheap);
50 __tsan_func_entry(2345 | kExternalPCBit);
51 __tsan_write1_pc(jheap + 16, 1234 | kExternalPCBit);
52 __tsan_func_exit();
53 barrier_wait(&barrier);
54 pthread_join(th, 0);
55 __tsan_java_free(jheap, kBlockSize);
56 fprintf(stderr, "DONE\n");
57 return __tsan_java_fini();
60 // CHECK: addr:[[BLOCK:0x[0-9,a-f]+]] [[ADDR:0x[0-9,a-f]+]]
61 // CHECK: WARNING: ThreadSanitizer: data race
62 // CHECK: Write of size 1 at [[ADDR]] by thread T1:
63 // CHECK: #0 MyInnerFunc MyInnerFile.java:1234:56
64 // CHECK: #1 MyOuterFunc MyOuterFile.java:4321:65
65 // CHECK: #2 Caller1 CallerFile.java:111:22
66 // CHECK: #3 Caller2 CallerFile.java:333:44
67 // CHECK: Previous write of size 1 at [[ADDR]] by main thread:
68 // CHECK: #0 MyInnerFunc MyInnerFile.java:1234:56
69 // CHECK: #1 MyOuterFunc MyOuterFile.java:4321:65
70 // CHECK: #2 Caller1 CallerFile.java:111:22
71 // CHECK: #3 Caller2 CallerFile.java:333:44
72 // CHECK: Location is heap block of size 32 at [[BLOCK]] allocated by main thread:
73 // CHECK: #0 Allocer1 Alloc.java:11:222
74 // CHECK: #1 Allocer2 Alloc.java:33:444
75 // CHECK: DONE