1 //===-- tsan_interface.cc -------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "tsan_interface.h"
15 #include "tsan_interface_ann.h"
16 #include "tsan_rtl.h"
17 #include "sanitizer_common/sanitizer_internal_defs.h"
18
19 #define CALLERPC ((uptr)__builtin_return_address(0))
20
21 using namespace __tsan; // NOLINT
22
23 typedef u16 uint16_t;
24 typedef u32 uint32_t;
25 typedef u64 uint64_t;
26
__tsan_init()27 void __tsan_init() {
28 Initialize(cur_thread());
29 }
30
__tsan_read16(void * addr)31 void __tsan_read16(void *addr) {
32 MemoryRead(cur_thread(), CALLERPC, (uptr)addr, kSizeLog8);
33 MemoryRead(cur_thread(), CALLERPC, (uptr)addr + 8, kSizeLog8);
34 }
35
__tsan_write16(void * addr)36 void __tsan_write16(void *addr) {
37 MemoryWrite(cur_thread(), CALLERPC, (uptr)addr, kSizeLog8);
38 MemoryWrite(cur_thread(), CALLERPC, (uptr)addr + 8, kSizeLog8);
39 }
40
__tsan_unaligned_read2(const uu16 * addr)41 u16 __tsan_unaligned_read2(const uu16 *addr) {
42 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, false, false);
43 return *addr;
44 }
45
__tsan_unaligned_read4(const uu32 * addr)46 u32 __tsan_unaligned_read4(const uu32 *addr) {
47 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, false, false);
48 return *addr;
49 }
50
__tsan_unaligned_read8(const uu64 * addr)51 u64 __tsan_unaligned_read8(const uu64 *addr) {
52 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, false, false);
53 return *addr;
54 }
55
__tsan_unaligned_write2(uu16 * addr,u16 v)56 void __tsan_unaligned_write2(uu16 *addr, u16 v) {
57 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, true, false);
58 *addr = v;
59 }
60
__tsan_unaligned_write4(uu32 * addr,u32 v)61 void __tsan_unaligned_write4(uu32 *addr, u32 v) {
62 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, true, false);
63 *addr = v;
64 }
65
__tsan_unaligned_write8(uu64 * addr,u64 v)66 void __tsan_unaligned_write8(uu64 *addr, u64 v) {
67 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, true, false);
68 *addr = v;
69 }
70
71 extern "C" {
72 SANITIZER_INTERFACE_ATTRIBUTE
73 uint16_t __sanitizer_unaligned_load16(void *addr)
74 ALIAS("__tsan_unaligned_read2");
75 SANITIZER_INTERFACE_ATTRIBUTE
76 uint32_t __sanitizer_unaligned_load32(void *addr)
77 ALIAS("__tsan_unaligned_read4");
78 SANITIZER_INTERFACE_ATTRIBUTE
79 uint64_t __sanitizer_unaligned_load64(void *addr)
80 ALIAS("__tsan_unaligned_read8");
81 SANITIZER_INTERFACE_ATTRIBUTE
82 void __sanitizer_unaligned_store16(void *addr, uint16_t v)
83 ALIAS("__tsan_unaligned_write2");
84 SANITIZER_INTERFACE_ATTRIBUTE
85 void __sanitizer_unaligned_store32(void *addr, uint32_t v)
86 ALIAS("__tsan_unaligned_write4");
87 SANITIZER_INTERFACE_ATTRIBUTE
88 void __sanitizer_unaligned_store64(void *addr, uint64_t v)
89 ALIAS("__tsan_unaligned_write8");
90 }
91
__tsan_acquire(void * addr)92 void __tsan_acquire(void *addr) {
93 Acquire(cur_thread(), CALLERPC, (uptr)addr);
94 }
95
__tsan_release(void * addr)96 void __tsan_release(void *addr) {
97 Release(cur_thread(), CALLERPC, (uptr)addr);
98 }
99