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