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