1//===-- tsan_interface.cc -------------------------------------------------===//
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
20using namespace __tsan;  // NOLINT
21
22typedef u16 uint16_t;
23typedef u32 uint32_t;
24typedef u64 uint64_t;
25
26void __tsan_init() {
27  cur_thread_init();
28  Initialize(cur_thread());
29}
30
31void __tsan_flush_memory() {
32  FlushShadowMemory();
33}
34
35void __tsan_read16(void *addr) {
36  MemoryRead(cur_thread(), CALLERPC, (uptr)addr, kSizeLog8);
37  MemoryRead(cur_thread(), CALLERPC, (uptr)addr + 8, kSizeLog8);
38}
39
40void __tsan_write16(void *addr) {
41  MemoryWrite(cur_thread(), CALLERPC, (uptr)addr, kSizeLog8);
42  MemoryWrite(cur_thread(), CALLERPC, (uptr)addr + 8, kSizeLog8);
43}
44
45void __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
50void __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
57void __tsan_unaligned_read2(const void *addr) {
58  UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, false, false);
59}
60
61void __tsan_unaligned_read4(const void *addr) {
62  UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, false, false);
63}
64
65void __tsan_unaligned_read8(const void *addr) {
66  UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, false, false);
67}
68
69void __tsan_unaligned_read16(const void *addr) {
70  UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 16, false, false);
71}
72
73void __tsan_unaligned_write2(void *addr) {
74  UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, true, false);
75}
76
77void __tsan_unaligned_write4(void *addr) {
78  UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, true, false);
79}
80
81void __tsan_unaligned_write8(void *addr) {
82  UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, true, false);
83}
84
85void __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
91extern "C" {
92SANITIZER_INTERFACE_ATTRIBUTE
93u16 __sanitizer_unaligned_load16(const uu16 *addr) {
94  __tsan_unaligned_read2(addr);
95  return *addr;
96}
97
98SANITIZER_INTERFACE_ATTRIBUTE
99u32 __sanitizer_unaligned_load32(const uu32 *addr) {
100  __tsan_unaligned_read4(addr);
101  return *addr;
102}
103
104SANITIZER_INTERFACE_ATTRIBUTE
105u64 __sanitizer_unaligned_load64(const uu64 *addr) {
106  __tsan_unaligned_read8(addr);
107  return *addr;
108}
109
110SANITIZER_INTERFACE_ATTRIBUTE
111void __sanitizer_unaligned_store16(uu16 *addr, u16 v) {
112  __tsan_unaligned_write2(addr);
113  *addr = v;
114}
115
116SANITIZER_INTERFACE_ATTRIBUTE
117void __sanitizer_unaligned_store32(uu32 *addr, u32 v) {
118  __tsan_unaligned_write4(addr);
119  *addr = v;
120}
121
122SANITIZER_INTERFACE_ATTRIBUTE
123void __sanitizer_unaligned_store64(uu64 *addr, u64 v) {
124  __tsan_unaligned_write8(addr);
125  *addr = v;
126}
127
128SANITIZER_INTERFACE_ATTRIBUTE
129void *__tsan_get_current_fiber() {
130  return cur_thread();
131}
132
133SANITIZER_INTERFACE_ATTRIBUTE
134void *__tsan_create_fiber(unsigned flags) {
135  return FiberCreate(cur_thread(), CALLERPC, flags);
136}
137
138SANITIZER_INTERFACE_ATTRIBUTE
139void __tsan_destroy_fiber(void *fiber) {
140  FiberDestroy(cur_thread(), CALLERPC, static_cast<ThreadState *>(fiber));
141}
142
143SANITIZER_INTERFACE_ATTRIBUTE
144void __tsan_switch_to_fiber(void *fiber, unsigned flags) {
145  FiberSwitch(cur_thread(), CALLERPC, static_cast<ThreadState *>(fiber), flags);
146}
147
148SANITIZER_INTERFACE_ATTRIBUTE
149void __tsan_set_fiber_name(void *fiber, const char *name) {
150  ThreadSetName(static_cast<ThreadState *>(fiber), name);
151}
152}  // extern "C"
153
154void __tsan_acquire(void *addr) {
155  Acquire(cur_thread(), CALLERPC, (uptr)addr);
156}
157
158void __tsan_release(void *addr) {
159  Release(cur_thread(), CALLERPC, (uptr)addr);
160}
161