xref: /freebsd/contrib/llvm-project/compiler-rt/lib/tsan/rtl/tsan_interface.cpp (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 //===-- tsan_interface.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "tsan_interface.h"
14 #include "tsan_interface_ann.h"
15 #include "tsan_rtl.h"
16 #include "sanitizer_common/sanitizer_internal_defs.h"
17 
18 #define CALLERPC ((uptr)__builtin_return_address(0))
19 
20 using namespace __tsan;
21 
22 typedef u16 uint16_t;
23 typedef u32 uint32_t;
24 typedef u64 uint64_t;
25 
26 void __tsan_init() {
27   cur_thread_init();
28   Initialize(cur_thread());
29 }
30 
31 void __tsan_flush_memory() {
32   FlushShadowMemory();
33 }
34 
35 void __tsan_read16(void *addr) {
36   MemoryRead(cur_thread(), CALLERPC, (uptr)addr, kSizeLog8);
37   MemoryRead(cur_thread(), CALLERPC, (uptr)addr + 8, kSizeLog8);
38 }
39 
40 void __tsan_write16(void *addr) {
41   MemoryWrite(cur_thread(), CALLERPC, (uptr)addr, kSizeLog8);
42   MemoryWrite(cur_thread(), CALLERPC, (uptr)addr + 8, kSizeLog8);
43 }
44 
45 void __tsan_read16_pc(void *addr, void *pc) {
46   MemoryRead(cur_thread(), (uptr)pc, (uptr)addr, kSizeLog8);
47   MemoryRead(cur_thread(), (uptr)pc, (uptr)addr + 8, kSizeLog8);
48 }
49 
50 void __tsan_write16_pc(void *addr, void *pc) {
51   MemoryWrite(cur_thread(), (uptr)pc, (uptr)addr, kSizeLog8);
52   MemoryWrite(cur_thread(), (uptr)pc, (uptr)addr + 8, kSizeLog8);
53 }
54 
55 // __tsan_unaligned_read/write calls are emitted by compiler.
56 
57 void __tsan_unaligned_read2(const void *addr) {
58   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, false, false);
59 }
60 
61 void __tsan_unaligned_read4(const void *addr) {
62   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, false, false);
63 }
64 
65 void __tsan_unaligned_read8(const void *addr) {
66   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, false, false);
67 }
68 
69 void __tsan_unaligned_read16(const void *addr) {
70   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 16, false, false);
71 }
72 
73 void __tsan_unaligned_write2(void *addr) {
74   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, true, false);
75 }
76 
77 void __tsan_unaligned_write4(void *addr) {
78   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, true, false);
79 }
80 
81 void __tsan_unaligned_write8(void *addr) {
82   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, true, false);
83 }
84 
85 void __tsan_unaligned_write16(void *addr) {
86   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 16, true, false);
87 }
88 
89 // __sanitizer_unaligned_load/store are for user instrumentation.
90 
91 extern "C" {
92 SANITIZER_INTERFACE_ATTRIBUTE
93 u16 __sanitizer_unaligned_load16(const uu16 *addr) {
94   __tsan_unaligned_read2(addr);
95   return *addr;
96 }
97 
98 SANITIZER_INTERFACE_ATTRIBUTE
99 u32 __sanitizer_unaligned_load32(const uu32 *addr) {
100   __tsan_unaligned_read4(addr);
101   return *addr;
102 }
103 
104 SANITIZER_INTERFACE_ATTRIBUTE
105 u64 __sanitizer_unaligned_load64(const uu64 *addr) {
106   __tsan_unaligned_read8(addr);
107   return *addr;
108 }
109 
110 SANITIZER_INTERFACE_ATTRIBUTE
111 void __sanitizer_unaligned_store16(uu16 *addr, u16 v) {
112   __tsan_unaligned_write2(addr);
113   *addr = v;
114 }
115 
116 SANITIZER_INTERFACE_ATTRIBUTE
117 void __sanitizer_unaligned_store32(uu32 *addr, u32 v) {
118   __tsan_unaligned_write4(addr);
119   *addr = v;
120 }
121 
122 SANITIZER_INTERFACE_ATTRIBUTE
123 void __sanitizer_unaligned_store64(uu64 *addr, u64 v) {
124   __tsan_unaligned_write8(addr);
125   *addr = v;
126 }
127 
128 SANITIZER_INTERFACE_ATTRIBUTE
129 void *__tsan_get_current_fiber() {
130   return cur_thread();
131 }
132 
133 SANITIZER_INTERFACE_ATTRIBUTE
134 void *__tsan_create_fiber(unsigned flags) {
135   return FiberCreate(cur_thread(), CALLERPC, flags);
136 }
137 
138 SANITIZER_INTERFACE_ATTRIBUTE
139 void __tsan_destroy_fiber(void *fiber) {
140   FiberDestroy(cur_thread(), CALLERPC, static_cast<ThreadState *>(fiber));
141 }
142 
143 SANITIZER_INTERFACE_ATTRIBUTE
144 void __tsan_switch_to_fiber(void *fiber, unsigned flags) {
145   FiberSwitch(cur_thread(), CALLERPC, static_cast<ThreadState *>(fiber), flags);
146 }
147 
148 SANITIZER_INTERFACE_ATTRIBUTE
149 void __tsan_set_fiber_name(void *fiber, const char *name) {
150   ThreadSetName(static_cast<ThreadState *>(fiber), name);
151 }
152 }  // extern "C"
153 
154 void __tsan_acquire(void *addr) {
155   Acquire(cur_thread(), CALLERPC, (uptr)addr);
156 }
157 
158 void __tsan_release(void *addr) {
159   Release(cur_thread(), CALLERPC, (uptr)addr);
160 }
161