1 //===-- tsan_interface.inc --------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_ptrauth.h"
14 #include "tsan_interface.h"
17 #define CALLERPC ((uptr)__builtin_return_address(0))
19 using namespace __tsan;
21 void __tsan_read1(void *addr) {
22 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 1, kAccessRead);
25 void __tsan_read2(void *addr) {
26 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessRead);
29 void __tsan_read4(void *addr) {
30 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessRead);
33 void __tsan_read8(void *addr) {
34 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessRead);
37 void __tsan_read16(void *addr) {
38 MemoryAccess16(cur_thread(), CALLERPC, (uptr)addr, kAccessRead);
41 void __tsan_write1(void *addr) {
42 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 1, kAccessWrite);
45 void __tsan_write2(void *addr) {
46 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessWrite);
49 void __tsan_write4(void *addr) {
50 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessWrite);
53 void __tsan_write8(void *addr) {
54 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessWrite);
57 void __tsan_write16(void *addr) {
58 MemoryAccess16(cur_thread(), CALLERPC, (uptr)addr, kAccessWrite);
61 void __tsan_read1_pc(void *addr, void *pc) {
62 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 1, kAccessRead | kAccessExternalPC);
65 void __tsan_read2_pc(void *addr, void *pc) {
66 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 2, kAccessRead | kAccessExternalPC);
69 void __tsan_read4_pc(void *addr, void *pc) {
70 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 4, kAccessRead | kAccessExternalPC);
73 void __tsan_read8_pc(void *addr, void *pc) {
74 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 8, kAccessRead | kAccessExternalPC);
77 void __tsan_write1_pc(void *addr, void *pc) {
78 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 1, kAccessWrite | kAccessExternalPC);
81 void __tsan_write2_pc(void *addr, void *pc) {
82 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 2, kAccessWrite | kAccessExternalPC);
85 void __tsan_write4_pc(void *addr, void *pc) {
86 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 4, kAccessWrite | kAccessExternalPC);
89 void __tsan_write8_pc(void *addr, void *pc) {
90 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 8, kAccessWrite | kAccessExternalPC);
93 ALWAYS_INLINE USED void __tsan_unaligned_read2(const void *addr) {
94 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessRead);
97 ALWAYS_INLINE USED void __tsan_unaligned_read4(const void *addr) {
98 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessRead);
101 ALWAYS_INLINE USED void __tsan_unaligned_read8(const void *addr) {
102 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessRead);
105 ALWAYS_INLINE USED void __tsan_unaligned_write2(void *addr) {
106 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessWrite);
109 ALWAYS_INLINE USED void __tsan_unaligned_write4(void *addr) {
110 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessWrite);
113 ALWAYS_INLINE USED void __tsan_unaligned_write8(void *addr) {
114 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessWrite);
118 // __sanitizer_unaligned_load/store are for user instrumentation.
119 SANITIZER_INTERFACE_ATTRIBUTE
120 u16 __sanitizer_unaligned_load16(const uu16 *addr) {
121 __tsan_unaligned_read2(addr);
125 SANITIZER_INTERFACE_ATTRIBUTE
126 u32 __sanitizer_unaligned_load32(const uu32 *addr) {
127 __tsan_unaligned_read4(addr);
131 SANITIZER_INTERFACE_ATTRIBUTE
132 u64 __sanitizer_unaligned_load64(const uu64 *addr) {
133 __tsan_unaligned_read8(addr);
137 SANITIZER_INTERFACE_ATTRIBUTE
138 void __sanitizer_unaligned_store16(uu16 *addr, u16 v) {
140 __tsan_unaligned_write2(addr);
143 SANITIZER_INTERFACE_ATTRIBUTE
144 void __sanitizer_unaligned_store32(uu32 *addr, u32 v) {
146 __tsan_unaligned_write4(addr);
149 SANITIZER_INTERFACE_ATTRIBUTE
150 void __sanitizer_unaligned_store64(uu64 *addr, u64 v) {
152 __tsan_unaligned_write8(addr);
156 void __tsan_vptr_update(void **vptr_p, void *new_val) {
157 if (*vptr_p == new_val)
159 MemoryAccess(cur_thread(), CALLERPC, (uptr)vptr_p, sizeof(*vptr_p),
160 kAccessWrite | kAccessVptr);
163 void __tsan_vptr_read(void **vptr_p) {
164 MemoryAccess(cur_thread(), CALLERPC, (uptr)vptr_p, sizeof(*vptr_p),
165 kAccessRead | kAccessVptr);
168 void __tsan_func_entry(void *pc) { FuncEntry(cur_thread(), STRIP_PAC_PC(pc)); }
170 void __tsan_func_exit() { FuncExit(cur_thread()); }
172 void __tsan_ignore_thread_begin() { ThreadIgnoreBegin(cur_thread(), CALLERPC); }
174 void __tsan_ignore_thread_end() { ThreadIgnoreEnd(cur_thread()); }
176 void __tsan_read_range(void *addr, uptr size) {
177 MemoryAccessRange(cur_thread(), CALLERPC, (uptr)addr, size, false);
180 void __tsan_write_range(void *addr, uptr size) {
181 MemoryAccessRange(cur_thread(), CALLERPC, (uptr)addr, size, true);
184 void __tsan_read_range_pc(void *addr, uptr size, void *pc) {
185 MemoryAccessRange(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, size, false);
188 void __tsan_write_range_pc(void *addr, uptr size, void *pc) {
189 MemoryAccessRange(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, size, true);