1 //===-- tsan_interface.cpp ------------------------------------------------===//
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 "tsan_interface.h"
14 #include "tsan_interface_ann.h"
16 #include "sanitizer_common/sanitizer_internal_defs.h"
18 #define CALLERPC ((uptr)__builtin_return_address(0))
20 using namespace __tsan
;
28 Initialize(cur_thread());
31 void __tsan_flush_memory() {
35 void __tsan_read16(void *addr
) {
36 MemoryRead(cur_thread(), CALLERPC
, (uptr
)addr
, kSizeLog8
);
37 MemoryRead(cur_thread(), CALLERPC
, (uptr
)addr
+ 8, kSizeLog8
);
40 void __tsan_write16(void *addr
) {
41 MemoryWrite(cur_thread(), CALLERPC
, (uptr
)addr
, kSizeLog8
);
42 MemoryWrite(cur_thread(), CALLERPC
, (uptr
)addr
+ 8, kSizeLog8
);
45 void __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
);
50 void __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
);
55 // __tsan_unaligned_read/write calls are emitted by compiler.
57 void __tsan_unaligned_read2(const void *addr
) {
58 UnalignedMemoryAccess(cur_thread(), CALLERPC
, (uptr
)addr
, 2, false, false);
61 void __tsan_unaligned_read4(const void *addr
) {
62 UnalignedMemoryAccess(cur_thread(), CALLERPC
, (uptr
)addr
, 4, false, false);
65 void __tsan_unaligned_read8(const void *addr
) {
66 UnalignedMemoryAccess(cur_thread(), CALLERPC
, (uptr
)addr
, 8, false, false);
69 void __tsan_unaligned_read16(const void *addr
) {
70 UnalignedMemoryAccess(cur_thread(), CALLERPC
, (uptr
)addr
, 16, false, false);
73 void __tsan_unaligned_write2(void *addr
) {
74 UnalignedMemoryAccess(cur_thread(), CALLERPC
, (uptr
)addr
, 2, true, false);
77 void __tsan_unaligned_write4(void *addr
) {
78 UnalignedMemoryAccess(cur_thread(), CALLERPC
, (uptr
)addr
, 4, true, false);
81 void __tsan_unaligned_write8(void *addr
) {
82 UnalignedMemoryAccess(cur_thread(), CALLERPC
, (uptr
)addr
, 8, true, false);
85 void __tsan_unaligned_write16(void *addr
) {
86 UnalignedMemoryAccess(cur_thread(), CALLERPC
, (uptr
)addr
, 16, true, false);
89 // __sanitizer_unaligned_load/store are for user instrumentation.
92 SANITIZER_INTERFACE_ATTRIBUTE
93 u16
__sanitizer_unaligned_load16(const uu16
*addr
) {
94 __tsan_unaligned_read2(addr
);
98 SANITIZER_INTERFACE_ATTRIBUTE
99 u32
__sanitizer_unaligned_load32(const uu32
*addr
) {
100 __tsan_unaligned_read4(addr
);
104 SANITIZER_INTERFACE_ATTRIBUTE
105 u64
__sanitizer_unaligned_load64(const uu64
*addr
) {
106 __tsan_unaligned_read8(addr
);
110 SANITIZER_INTERFACE_ATTRIBUTE
111 void __sanitizer_unaligned_store16(uu16
*addr
, u16 v
) {
112 __tsan_unaligned_write2(addr
);
116 SANITIZER_INTERFACE_ATTRIBUTE
117 void __sanitizer_unaligned_store32(uu32
*addr
, u32 v
) {
118 __tsan_unaligned_write4(addr
);
122 SANITIZER_INTERFACE_ATTRIBUTE
123 void __sanitizer_unaligned_store64(uu64
*addr
, u64 v
) {
124 __tsan_unaligned_write8(addr
);
128 SANITIZER_INTERFACE_ATTRIBUTE
129 void *__tsan_get_current_fiber() {
133 SANITIZER_INTERFACE_ATTRIBUTE
134 void *__tsan_create_fiber(unsigned flags
) {
135 return FiberCreate(cur_thread(), CALLERPC
, flags
);
138 SANITIZER_INTERFACE_ATTRIBUTE
139 void __tsan_destroy_fiber(void *fiber
) {
140 FiberDestroy(cur_thread(), CALLERPC
, static_cast<ThreadState
*>(fiber
));
143 SANITIZER_INTERFACE_ATTRIBUTE
144 void __tsan_switch_to_fiber(void *fiber
, unsigned flags
) {
145 FiberSwitch(cur_thread(), CALLERPC
, static_cast<ThreadState
*>(fiber
), flags
);
148 SANITIZER_INTERFACE_ATTRIBUTE
149 void __tsan_set_fiber_name(void *fiber
, const char *name
) {
150 ThreadSetName(static_cast<ThreadState
*>(fiber
), name
);
154 void __tsan_acquire(void *addr
) {
155 Acquire(cur_thread(), CALLERPC
, (uptr
)addr
);
158 void __tsan_release(void *addr
) {
159 Release(cur_thread(), CALLERPC
, (uptr
)addr
);