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#include "sanitizer_common/sanitizer_ptrauth.h"
18
19#define CALLERPC ((uptr)__builtin_return_address(0))
20
21using namespace __tsan;
22
23void __tsan_init() { Initialize(thr: cur_thread_init()); }
24
25void __tsan_flush_memory() {
26 FlushShadowMemory();
27}
28
29void __tsan_read16_pc(void *addr, void *pc) {
30 uptr pc_no_pac = STRIP_PAC_PC(pc);
31 ThreadState *thr = cur_thread();
32 MemoryAccess(thr, pc: pc_no_pac, addr: (uptr)addr, size: 8, typ: kAccessRead);
33 MemoryAccess(thr, pc: pc_no_pac, addr: (uptr)addr + 8, size: 8, typ: kAccessRead);
34}
35
36void __tsan_write16_pc(void *addr, void *pc) {
37 uptr pc_no_pac = STRIP_PAC_PC(pc);
38 ThreadState *thr = cur_thread();
39 MemoryAccess(thr, pc: pc_no_pac, addr: (uptr)addr, size: 8, typ: kAccessWrite);
40 MemoryAccess(thr, pc: pc_no_pac, addr: (uptr)addr + 8, size: 8, typ: kAccessWrite);
41}
42
43// __tsan_unaligned_read/write calls are emitted by compiler.
44
45void __tsan_unaligned_read16(const void *addr) {
46 uptr pc = CALLERPC;
47 ThreadState *thr = cur_thread();
48 UnalignedMemoryAccess(thr, pc, addr: (uptr)addr, size: 8, typ: kAccessRead);
49 UnalignedMemoryAccess(thr, pc, addr: (uptr)addr + 8, size: 8, typ: kAccessRead);
50}
51
52void __tsan_unaligned_write16(void *addr) {
53 uptr pc = CALLERPC;
54 ThreadState *thr = cur_thread();
55 UnalignedMemoryAccess(thr, pc, addr: (uptr)addr, size: 8, typ: kAccessWrite);
56 UnalignedMemoryAccess(thr, pc, addr: (uptr)addr + 8, size: 8, typ: kAccessWrite);
57}
58
59extern "C" {
60SANITIZER_INTERFACE_ATTRIBUTE
61void *__tsan_get_current_fiber() {
62 return cur_thread();
63}
64
65SANITIZER_INTERFACE_ATTRIBUTE
66void *__tsan_create_fiber(unsigned flags) {
67 return FiberCreate(thr: cur_thread(), CALLERPC, flags);
68}
69
70SANITIZER_INTERFACE_ATTRIBUTE
71void __tsan_destroy_fiber(void *fiber) {
72 FiberDestroy(thr: cur_thread(), CALLERPC, fiber: static_cast<ThreadState *>(fiber));
73}
74
75SANITIZER_INTERFACE_ATTRIBUTE
76void __tsan_switch_to_fiber(void *fiber, unsigned flags) {
77 FiberSwitch(thr: cur_thread(), CALLERPC, fiber: static_cast<ThreadState *>(fiber), flags);
78}
79
80SANITIZER_INTERFACE_ATTRIBUTE
81void __tsan_set_fiber_name(void *fiber, const char *name) {
82 ThreadSetName(thr: static_cast<ThreadState *>(fiber), name);
83}
84} // extern "C"
85
86void __tsan_acquire(void *addr) {
87 Acquire(thr: cur_thread(), CALLERPC, addr: (uptr)addr);
88}
89
90void __tsan_release(void *addr) {
91 Release(thr: cur_thread(), CALLERPC, addr: (uptr)addr);
92}
93

source code of compiler-rt/lib/tsan/rtl/tsan_interface.cpp