xref: /freebsd/contrib/llvm-project/compiler-rt/lib/tsan/rtl/tsan_interface.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
168d75effSDimitry Andric //===-- tsan_interface.cpp ------------------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is a part of ThreadSanitizer (TSan), a race detector.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric //===----------------------------------------------------------------------===//
1268d75effSDimitry Andric 
1368d75effSDimitry Andric #include "tsan_interface.h"
1468d75effSDimitry Andric #include "tsan_interface_ann.h"
1568d75effSDimitry Andric #include "tsan_rtl.h"
1668d75effSDimitry Andric #include "sanitizer_common/sanitizer_internal_defs.h"
17e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_ptrauth.h"
1868d75effSDimitry Andric 
1968d75effSDimitry Andric #define CALLERPC ((uptr)__builtin_return_address(0))
2068d75effSDimitry Andric 
2168d75effSDimitry Andric using namespace __tsan;
2268d75effSDimitry Andric 
__tsan_init()23*349cc55cSDimitry Andric void __tsan_init() { Initialize(cur_thread_init()); }
2468d75effSDimitry Andric 
__tsan_flush_memory()2568d75effSDimitry Andric void __tsan_flush_memory() {
2668d75effSDimitry Andric   FlushShadowMemory();
2768d75effSDimitry Andric }
2868d75effSDimitry Andric 
__tsan_read16_pc(void * addr,void * pc)2968d75effSDimitry Andric void __tsan_read16_pc(void *addr, void *pc) {
30*349cc55cSDimitry Andric   uptr pc_no_pac = STRIP_PAC_PC(pc);
31*349cc55cSDimitry Andric   ThreadState *thr = cur_thread();
32*349cc55cSDimitry Andric   MemoryAccess(thr, pc_no_pac, (uptr)addr, 8, kAccessRead);
33*349cc55cSDimitry Andric   MemoryAccess(thr, pc_no_pac, (uptr)addr + 8, 8, kAccessRead);
3468d75effSDimitry Andric }
3568d75effSDimitry Andric 
__tsan_write16_pc(void * addr,void * pc)3668d75effSDimitry Andric void __tsan_write16_pc(void *addr, void *pc) {
37*349cc55cSDimitry Andric   uptr pc_no_pac = STRIP_PAC_PC(pc);
38*349cc55cSDimitry Andric   ThreadState *thr = cur_thread();
39*349cc55cSDimitry Andric   MemoryAccess(thr, pc_no_pac, (uptr)addr, 8, kAccessWrite);
40*349cc55cSDimitry Andric   MemoryAccess(thr, pc_no_pac, (uptr)addr + 8, 8, kAccessWrite);
4168d75effSDimitry Andric }
4268d75effSDimitry Andric 
4368d75effSDimitry Andric // __tsan_unaligned_read/write calls are emitted by compiler.
4468d75effSDimitry Andric 
__tsan_unaligned_read16(const void * addr)4568d75effSDimitry Andric void __tsan_unaligned_read16(const void *addr) {
46*349cc55cSDimitry Andric   uptr pc = CALLERPC;
47*349cc55cSDimitry Andric   ThreadState *thr = cur_thread();
48*349cc55cSDimitry Andric   UnalignedMemoryAccess(thr, pc, (uptr)addr, 8, kAccessRead);
49*349cc55cSDimitry Andric   UnalignedMemoryAccess(thr, pc, (uptr)addr + 8, 8, kAccessRead);
5068d75effSDimitry Andric }
5168d75effSDimitry Andric 
__tsan_unaligned_write16(void * addr)5268d75effSDimitry Andric void __tsan_unaligned_write16(void *addr) {
53*349cc55cSDimitry Andric   uptr pc = CALLERPC;
54*349cc55cSDimitry Andric   ThreadState *thr = cur_thread();
55*349cc55cSDimitry Andric   UnalignedMemoryAccess(thr, pc, (uptr)addr, 8, kAccessWrite);
56*349cc55cSDimitry Andric   UnalignedMemoryAccess(thr, pc, (uptr)addr + 8, 8, kAccessWrite);
5768d75effSDimitry Andric }
5868d75effSDimitry Andric 
5968d75effSDimitry Andric extern "C" {
6068d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
__tsan_get_current_fiber()6168d75effSDimitry Andric void *__tsan_get_current_fiber() {
6268d75effSDimitry Andric   return cur_thread();
6368d75effSDimitry Andric }
6468d75effSDimitry Andric 
6568d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
__tsan_create_fiber(unsigned flags)6668d75effSDimitry Andric void *__tsan_create_fiber(unsigned flags) {
6768d75effSDimitry Andric   return FiberCreate(cur_thread(), CALLERPC, flags);
6868d75effSDimitry Andric }
6968d75effSDimitry Andric 
7068d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
__tsan_destroy_fiber(void * fiber)7168d75effSDimitry Andric void __tsan_destroy_fiber(void *fiber) {
7268d75effSDimitry Andric   FiberDestroy(cur_thread(), CALLERPC, static_cast<ThreadState *>(fiber));
7368d75effSDimitry Andric }
7468d75effSDimitry Andric 
7568d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
__tsan_switch_to_fiber(void * fiber,unsigned flags)7668d75effSDimitry Andric void __tsan_switch_to_fiber(void *fiber, unsigned flags) {
7768d75effSDimitry Andric   FiberSwitch(cur_thread(), CALLERPC, static_cast<ThreadState *>(fiber), flags);
7868d75effSDimitry Andric }
7968d75effSDimitry Andric 
8068d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
__tsan_set_fiber_name(void * fiber,const char * name)8168d75effSDimitry Andric void __tsan_set_fiber_name(void *fiber, const char *name) {
8268d75effSDimitry Andric   ThreadSetName(static_cast<ThreadState *>(fiber), name);
8368d75effSDimitry Andric }
8468d75effSDimitry Andric }  // extern "C"
8568d75effSDimitry Andric 
__tsan_acquire(void * addr)8668d75effSDimitry Andric void __tsan_acquire(void *addr) {
8768d75effSDimitry Andric   Acquire(cur_thread(), CALLERPC, (uptr)addr);
8868d75effSDimitry Andric }
8968d75effSDimitry Andric 
__tsan_release(void * addr)9068d75effSDimitry Andric void __tsan_release(void *addr) {
9168d75effSDimitry Andric   Release(cur_thread(), CALLERPC, (uptr)addr);
9268d75effSDimitry Andric }
93