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
41 // __tsan_unaligned_read/write calls are emitted by compiler.
42
__tsan_unaligned_read2(const void * addr)43 void __tsan_unaligned_read2(const void *addr) {
44 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, false, false);
45 }
46
__tsan_unaligned_read4(const void * addr)47 void __tsan_unaligned_read4(const void *addr) {
48 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, false, false);
49 }
50
__tsan_unaligned_read8(const void * addr)51 void __tsan_unaligned_read8(const void *addr) {
52 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, false, false);
53 }
54
__tsan_unaligned_read16(const void * addr)55 void __tsan_unaligned_read16(const void *addr) {
56 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 16, false, false);
57 }
58
__tsan_unaligned_write2(void * addr)59 void __tsan_unaligned_write2(void *addr) {
60 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, true, false);
61 }
62
__tsan_unaligned_write4(void * addr)63 void __tsan_unaligned_write4(void *addr) {
64 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, true, false);
65 }
66
__tsan_unaligned_write8(void * addr)67 void __tsan_unaligned_write8(void *addr) {
68 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, true, false);
69 }
70
__tsan_unaligned_write16(void * addr)71 void __tsan_unaligned_write16(void *addr) {
72 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 16, true, false);
73 }
74
75 // __sanitizer_unaligned_load/store are for user instrumentation.
76
77 extern "C" {
78 SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_unaligned_load16(const uu16 * addr)79 u16 __sanitizer_unaligned_load16(const uu16 *addr) {
80 __tsan_unaligned_read2(addr);
81 return *addr;
82 }
83
84 SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_unaligned_load32(const uu32 * addr)85 u32 __sanitizer_unaligned_load32(const uu32 *addr) {
86 __tsan_unaligned_read4(addr);
87 return *addr;
88 }
89
90 SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_unaligned_load64(const uu64 * addr)91 u64 __sanitizer_unaligned_load64(const uu64 *addr) {
92 __tsan_unaligned_read8(addr);
93 return *addr;
94 }
95
96 SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_unaligned_store16(uu16 * addr,u16 v)97 void __sanitizer_unaligned_store16(uu16 *addr, u16 v) {
98 __tsan_unaligned_write2(addr);
99 *addr = v;
100 }
101
102 SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_unaligned_store32(uu32 * addr,u32 v)103 void __sanitizer_unaligned_store32(uu32 *addr, u32 v) {
104 __tsan_unaligned_write4(addr);
105 *addr = v;
106 }
107
108 SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_unaligned_store64(uu64 * addr,u64 v)109 void __sanitizer_unaligned_store64(uu64 *addr, u64 v) {
110 __tsan_unaligned_write8(addr);
111 *addr = v;
112 }
113 } // extern "C"
114
__tsan_acquire(void * addr)115 void __tsan_acquire(void *addr) {
116 Acquire(cur_thread(), CALLERPC, (uptr)addr);
117 }
118
__tsan_release(void * addr)119 void __tsan_release(void *addr) {
120 Release(cur_thread(), CALLERPC, (uptr)addr);
121 }
122