Home | History | Annotate | Download | only in rtl
      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 
     27 void __tsan_init() {
     28   Initialize(cur_thread());
     29 }
     30 
     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 
     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 u16 __tsan_unaligned_read2(const uu16 *addr) {
     42   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, false, false);
     43   return *addr;
     44 }
     45 
     46 u32 __tsan_unaligned_read4(const uu32 *addr) {
     47   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, false, false);
     48   return *addr;
     49 }
     50 
     51 u64 __tsan_unaligned_read8(const uu64 *addr) {
     52   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, false, false);
     53   return *addr;
     54 }
     55 
     56 void __tsan_unaligned_write2(uu16 *addr, u16 v) {
     57   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, true, false);
     58   *addr = v;
     59 }
     60 
     61 void __tsan_unaligned_write4(uu32 *addr, u32 v) {
     62   UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, true, false);
     63   *addr = v;
     64 }
     65 
     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 uint16_t __sanitizer_unaligned_load16(void *addr)
     73     ALIAS("__tsan_unaligned_read2") SANITIZER_INTERFACE_ATTRIBUTE;
     74 uint32_t __sanitizer_unaligned_load32(void *addr)
     75     ALIAS("__tsan_unaligned_read4") SANITIZER_INTERFACE_ATTRIBUTE;
     76 uint64_t __sanitizer_unaligned_load64(void *addr)
     77     ALIAS("__tsan_unaligned_read8") SANITIZER_INTERFACE_ATTRIBUTE;
     78 void __sanitizer_unaligned_store16(void *addr, uint16_t v)
     79     ALIAS("__tsan_unaligned_write2") SANITIZER_INTERFACE_ATTRIBUTE;
     80 void __sanitizer_unaligned_store32(void *addr, uint32_t v)
     81     ALIAS("__tsan_unaligned_write4") SANITIZER_INTERFACE_ATTRIBUTE;
     82 void __sanitizer_unaligned_store64(void *addr, uint64_t v)
     83     ALIAS("__tsan_unaligned_write8") SANITIZER_INTERFACE_ATTRIBUTE;
     84 }
     85 
     86 void __tsan_acquire(void *addr) {
     87   Acquire(cur_thread(), CALLERPC, (uptr)addr);
     88 }
     89 
     90 void __tsan_release(void *addr) {
     91   Release(cur_thread(), CALLERPC, (uptr)addr);
     92 }
     93