1//===-- tsan_interface.inc --------------------------------------*- C++ -*-===// 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 "sanitizer_common/sanitizer_ptrauth.h" 14#include "tsan_interface.h" 15#include "tsan_rtl.h" 16 17#define CALLERPC ((uptr)__builtin_return_address(0)) 18 19using namespace __tsan; 20 21void __tsan_read1(void *addr) { 22 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 1, kAccessRead); 23} 24 25void __tsan_read2(void *addr) { 26 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessRead); 27} 28 29void __tsan_read4(void *addr) { 30 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessRead); 31} 32 33void __tsan_read8(void *addr) { 34 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessRead); 35} 36 37void __tsan_read16(void *addr) { 38 MemoryAccess16(cur_thread(), CALLERPC, (uptr)addr, kAccessRead); 39} 40 41void __tsan_write1(void *addr) { 42 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 1, kAccessWrite); 43} 44 45void __tsan_write2(void *addr) { 46 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessWrite); 47} 48 49void __tsan_write4(void *addr) { 50 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessWrite); 51} 52 53void __tsan_write8(void *addr) { 54 MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessWrite); 55} 56 57void __tsan_write16(void *addr) { 58 MemoryAccess16(cur_thread(), CALLERPC, (uptr)addr, kAccessWrite); 59} 60 61void __tsan_read1_pc(void *addr, void *pc) { 62 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 1, kAccessRead | kAccessExternalPC); 63} 64 65void __tsan_read2_pc(void *addr, void *pc) { 66 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 2, kAccessRead | kAccessExternalPC); 67} 68 69void __tsan_read4_pc(void *addr, void *pc) { 70 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 4, kAccessRead | kAccessExternalPC); 71} 72 73void __tsan_read8_pc(void *addr, void *pc) { 74 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 8, kAccessRead | kAccessExternalPC); 75} 76 77void __tsan_write1_pc(void *addr, void *pc) { 78 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 1, kAccessWrite | kAccessExternalPC); 79} 80 81void __tsan_write2_pc(void *addr, void *pc) { 82 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 2, kAccessWrite | kAccessExternalPC); 83} 84 85void __tsan_write4_pc(void *addr, void *pc) { 86 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 4, kAccessWrite | kAccessExternalPC); 87} 88 89void __tsan_write8_pc(void *addr, void *pc) { 90 MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 8, kAccessWrite | kAccessExternalPC); 91} 92 93ALWAYS_INLINE USED void __tsan_unaligned_read2(const void *addr) { 94 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessRead); 95} 96 97ALWAYS_INLINE USED void __tsan_unaligned_read4(const void *addr) { 98 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessRead); 99} 100 101ALWAYS_INLINE USED void __tsan_unaligned_read8(const void *addr) { 102 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessRead); 103} 104 105ALWAYS_INLINE USED void __tsan_unaligned_write2(void *addr) { 106 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessWrite); 107} 108 109ALWAYS_INLINE USED void __tsan_unaligned_write4(void *addr) { 110 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessWrite); 111} 112 113ALWAYS_INLINE USED void __tsan_unaligned_write8(void *addr) { 114 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessWrite); 115} 116 117extern "C" { 118// __sanitizer_unaligned_load/store are for user instrumentation. 119SANITIZER_INTERFACE_ATTRIBUTE 120u16 __sanitizer_unaligned_load16(const uu16 *addr) { 121 __tsan_unaligned_read2(addr); 122 return *addr; 123} 124 125SANITIZER_INTERFACE_ATTRIBUTE 126u32 __sanitizer_unaligned_load32(const uu32 *addr) { 127 __tsan_unaligned_read4(addr); 128 return *addr; 129} 130 131SANITIZER_INTERFACE_ATTRIBUTE 132u64 __sanitizer_unaligned_load64(const uu64 *addr) { 133 __tsan_unaligned_read8(addr); 134 return *addr; 135} 136 137SANITIZER_INTERFACE_ATTRIBUTE 138void __sanitizer_unaligned_store16(uu16 *addr, u16 v) { 139 *addr = v; 140 __tsan_unaligned_write2(addr); 141} 142 143SANITIZER_INTERFACE_ATTRIBUTE 144void __sanitizer_unaligned_store32(uu32 *addr, u32 v) { 145 *addr = v; 146 __tsan_unaligned_write4(addr); 147} 148 149SANITIZER_INTERFACE_ATTRIBUTE 150void __sanitizer_unaligned_store64(uu64 *addr, u64 v) { 151 *addr = v; 152 __tsan_unaligned_write8(addr); 153} 154} 155 156void __tsan_vptr_update(void **vptr_p, void *new_val) { 157 if (*vptr_p == new_val) 158 return; 159 MemoryAccess(cur_thread(), CALLERPC, (uptr)vptr_p, sizeof(*vptr_p), 160 kAccessWrite | kAccessVptr); 161} 162 163void __tsan_vptr_read(void **vptr_p) { 164 MemoryAccess(cur_thread(), CALLERPC, (uptr)vptr_p, sizeof(*vptr_p), 165 kAccessRead | kAccessVptr); 166} 167 168void __tsan_func_entry(void *pc) { FuncEntry(cur_thread(), STRIP_PAC_PC(pc)); } 169 170void __tsan_func_exit() { FuncExit(cur_thread()); } 171 172void __tsan_ignore_thread_begin() { ThreadIgnoreBegin(cur_thread(), CALLERPC); } 173 174void __tsan_ignore_thread_end() { ThreadIgnoreEnd(cur_thread()); } 175 176void __tsan_read_range(void *addr, uptr size) { 177 MemoryAccessRange(cur_thread(), CALLERPC, (uptr)addr, size, false); 178} 179 180void __tsan_write_range(void *addr, uptr size) { 181 MemoryAccessRange(cur_thread(), CALLERPC, (uptr)addr, size, true); 182} 183 184void __tsan_read_range_pc(void *addr, uptr size, void *pc) { 185 MemoryAccessRange(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, size, false); 186} 187 188void __tsan_write_range_pc(void *addr, uptr size, void *pc) { 189 MemoryAccessRange(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, size, true); 190} 191