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> |
5 | |
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); |
18 | } |
19 | if (pc == (2345 | kExternalPCBit)) { |
20 | add_frame(ctx, "Caller1" , "CallerFile.java" , 111, 22); |
21 | add_frame(ctx, "Caller2" , "CallerFile.java" , 333, 44); |
22 | } |
23 | if (pc == (3456 | kExternalPCBit)) { |
24 | add_frame(ctx, "Allocer1" , "Alloc.java" , 11, 222); |
25 | add_frame(ctx, "Allocer2" , "Alloc.java" , 33, 444); |
26 | } |
27 | } |
28 | |
29 | void *Thread(void *p) { |
30 | barrier_wait(barrier: &barrier); |
31 | __tsan_func_entry(pc: 2345 | kExternalPCBit); |
32 | __tsan_write1_pc(addr: (jptr)p + 16, pc: 1234 | kExternalPCBit); |
33 | __tsan_func_exit(); |
34 | return 0; |
35 | } |
36 | |
37 | jptr const kHeapSize = 64 * 1024; |
38 | jptr java_heap[kHeapSize]; |
39 | |
40 | int main() { |
41 | barrier_init(barrier: &barrier, count: 2); |
42 | jptr jheap = (jptr)java_heap; |
43 | __tsan_java_init(heap_begin: jheap, heap_size: kHeapSize); |
44 | const int kBlockSize = 32; |
45 | __tsan_func_entry(pc: 3456 | kExternalPCBit); |
46 | __tsan_java_alloc(ptr: jheap, size: kBlockSize); |
47 | print_address("addr:" , 2, jheap, jheap + 16); |
48 | __tsan_func_exit(); |
49 | pthread_t th; |
50 | pthread_create(newthread: &th, attr: 0, start_routine: Thread, arg: (void*)jheap); |
51 | __tsan_func_entry(pc: 2345 | kExternalPCBit); |
52 | __tsan_write1_pc(addr: jheap + 16, pc: 1234 | kExternalPCBit); |
53 | __tsan_func_exit(); |
54 | barrier_wait(barrier: &barrier); |
55 | pthread_join(th: th, thread_return: 0); |
56 | __tsan_java_free(ptr: jheap, size: kBlockSize); |
57 | fprintf(stderr, format: "DONE\n" ); |
58 | return __tsan_java_fini(); |
59 | } |
60 | |
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 |
77 | |