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" bool |
11 | __tsan_symbolize_external(jptr pc, char *func_buf, jptr func_siz, |
12 | char *file_buf, jptr file_siz, int *line, int *col) { |
13 | if (pc == (1234 | kExternalPCBit)) { |
14 | memcpy(dest: func_buf, src: "MyFunc" , n: sizeof("MyFunc" )); |
15 | memcpy(dest: file_buf, src: "MyFile.java" , n: sizeof("MyFile.java" )); |
16 | *line = 1234; |
17 | *col = 56; |
18 | return true; |
19 | } |
20 | return false; |
21 | } |
22 | |
23 | void *Thread(void *p) { |
24 | barrier_wait(barrier: &barrier); |
25 | __tsan_write1_pc(addr: (jptr)p, pc: 1234 | kExternalPCBit); |
26 | return 0; |
27 | } |
28 | |
29 | int main() { |
30 | barrier_init(barrier: &barrier, count: 2); |
31 | int const kHeapSize = 1024 * 1024; |
32 | jptr jheap = (jptr)malloc(size: kHeapSize + 8) + 8; |
33 | __tsan_java_init(heap_begin: jheap, heap_size: kHeapSize); |
34 | const int kBlockSize = 16; |
35 | __tsan_java_alloc(ptr: jheap, size: kBlockSize); |
36 | pthread_t th; |
37 | pthread_create(newthread: &th, attr: 0, start_routine: Thread, arg: (void*)jheap); |
38 | __tsan_write1_pc(addr: (jptr)jheap, pc: 1234 | kExternalPCBit); |
39 | barrier_wait(barrier: &barrier); |
40 | pthread_join(th: th, thread_return: 0); |
41 | __tsan_java_free(ptr: jheap, size: kBlockSize); |
42 | fprintf(stderr, format: "DONE\n" ); |
43 | return __tsan_java_fini(); |
44 | } |
45 | |
46 | // CHECK: WARNING: ThreadSanitizer: data race |
47 | // CHECK: #0 MyFunc MyFile.java:1234:56 |
48 | // CHECK: DONE |
49 | |