1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
4 #include <sanitizer/tsan_interface.h>
7 __attribute__((weak
)) // Required for dyld macOS 12.0+
9 __attribute__((disable_sanitizer_instrumentation
))
11 __tsan_symbolize_external_ex(jptr pc
,
12 void (*add_frame
)(void *, const char *,
13 const char *, int, int),
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
);
37 jptr
const kHeapSize
= 64 * 1024;
38 jptr java_heap
[kHeapSize
];
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);
50 pthread_create(&th
, 0, Thread
, (void*)jheap
);
51 __tsan_func_entry(2345 | kExternalPCBit
);
52 __tsan_write1_pc(jheap
+ 16, 1234 | kExternalPCBit
);
54 barrier_wait(&barrier
);
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