2016-08-31 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libsanitizer / tsan / tsan_interface.cc
blobee9a627076ba394ca715ebb420c97ad2e1561803
1 //===-- tsan_interface.cc -------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
12 #include "tsan_interface.h"
13 #include "tsan_interface_ann.h"
14 #include "tsan_rtl.h"
15 #include "sanitizer_common/sanitizer_internal_defs.h"
17 #define CALLERPC ((uptr)__builtin_return_address(0))
19 using namespace __tsan; // NOLINT
21 typedef u16 uint16_t;
22 typedef u32 uint32_t;
23 typedef u64 uint64_t;
25 void __tsan_init() {
26 Initialize(cur_thread());
29 void __tsan_read16(void *addr) {
30 MemoryRead(cur_thread(), CALLERPC, (uptr)addr, kSizeLog8);
31 MemoryRead(cur_thread(), CALLERPC, (uptr)addr + 8, kSizeLog8);
34 void __tsan_write16(void *addr) {
35 MemoryWrite(cur_thread(), CALLERPC, (uptr)addr, kSizeLog8);
36 MemoryWrite(cur_thread(), CALLERPC, (uptr)addr + 8, kSizeLog8);
39 void __tsan_read16_pc(void *addr, void *pc) {
40 MemoryRead(cur_thread(), (uptr)pc, (uptr)addr, kSizeLog8);
41 MemoryRead(cur_thread(), (uptr)pc, (uptr)addr + 8, kSizeLog8);
44 void __tsan_write16_pc(void *addr, void *pc) {
45 MemoryWrite(cur_thread(), (uptr)pc, (uptr)addr, kSizeLog8);
46 MemoryWrite(cur_thread(), (uptr)pc, (uptr)addr + 8, kSizeLog8);
49 // __tsan_unaligned_read/write calls are emitted by compiler.
51 void __tsan_unaligned_read2(const void *addr) {
52 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, false, false);
55 void __tsan_unaligned_read4(const void *addr) {
56 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, false, false);
59 void __tsan_unaligned_read8(const void *addr) {
60 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, false, false);
63 void __tsan_unaligned_read16(const void *addr) {
64 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 16, false, false);
67 void __tsan_unaligned_write2(void *addr) {
68 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, true, false);
71 void __tsan_unaligned_write4(void *addr) {
72 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, true, false);
75 void __tsan_unaligned_write8(void *addr) {
76 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, true, false);
79 void __tsan_unaligned_write16(void *addr) {
80 UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 16, true, false);
83 // __sanitizer_unaligned_load/store are for user instrumentation.
85 extern "C" {
86 SANITIZER_INTERFACE_ATTRIBUTE
87 u16 __sanitizer_unaligned_load16(const uu16 *addr) {
88 __tsan_unaligned_read2(addr);
89 return *addr;
92 SANITIZER_INTERFACE_ATTRIBUTE
93 u32 __sanitizer_unaligned_load32(const uu32 *addr) {
94 __tsan_unaligned_read4(addr);
95 return *addr;
98 SANITIZER_INTERFACE_ATTRIBUTE
99 u64 __sanitizer_unaligned_load64(const uu64 *addr) {
100 __tsan_unaligned_read8(addr);
101 return *addr;
104 SANITIZER_INTERFACE_ATTRIBUTE
105 void __sanitizer_unaligned_store16(uu16 *addr, u16 v) {
106 __tsan_unaligned_write2(addr);
107 *addr = v;
110 SANITIZER_INTERFACE_ATTRIBUTE
111 void __sanitizer_unaligned_store32(uu32 *addr, u32 v) {
112 __tsan_unaligned_write4(addr);
113 *addr = v;
116 SANITIZER_INTERFACE_ATTRIBUTE
117 void __sanitizer_unaligned_store64(uu64 *addr, u64 v) {
118 __tsan_unaligned_write8(addr);
119 *addr = v;
121 } // extern "C"
123 void __tsan_acquire(void *addr) {
124 Acquire(cur_thread(), CALLERPC, (uptr)addr);
127 void __tsan_release(void *addr) {
128 Release(cur_thread(), CALLERPC, (uptr)addr);