tsan_interface.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //===-- tsan_interface.inc --------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of ThreadSanitizer (TSan), a race detector.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_common/sanitizer_ptrauth.h"
  13. #include "tsan_interface.h"
  14. #include "tsan_rtl.h"
  15. #define CALLERPC ((uptr)__builtin_return_address(0))
  16. using namespace __tsan;
  17. void __tsan_read1(void *addr) {
  18. MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 1, kAccessRead);
  19. }
  20. void __tsan_read2(void *addr) {
  21. MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessRead);
  22. }
  23. void __tsan_read4(void *addr) {
  24. MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessRead);
  25. }
  26. void __tsan_read8(void *addr) {
  27. MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessRead);
  28. }
  29. void __tsan_read16(void *addr) {
  30. MemoryAccess16(cur_thread(), CALLERPC, (uptr)addr, kAccessRead);
  31. }
  32. void __tsan_write1(void *addr) {
  33. MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 1, kAccessWrite);
  34. }
  35. void __tsan_write2(void *addr) {
  36. MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessWrite);
  37. }
  38. void __tsan_write4(void *addr) {
  39. MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessWrite);
  40. }
  41. void __tsan_write8(void *addr) {
  42. MemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessWrite);
  43. }
  44. void __tsan_write16(void *addr) {
  45. MemoryAccess16(cur_thread(), CALLERPC, (uptr)addr, kAccessWrite);
  46. }
  47. void __tsan_read1_pc(void *addr, void *pc) {
  48. MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 1, kAccessRead | kAccessExternalPC);
  49. }
  50. void __tsan_read2_pc(void *addr, void *pc) {
  51. MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 2, kAccessRead | kAccessExternalPC);
  52. }
  53. void __tsan_read4_pc(void *addr, void *pc) {
  54. MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 4, kAccessRead | kAccessExternalPC);
  55. }
  56. void __tsan_read8_pc(void *addr, void *pc) {
  57. MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 8, kAccessRead | kAccessExternalPC);
  58. }
  59. void __tsan_write1_pc(void *addr, void *pc) {
  60. MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 1, kAccessWrite | kAccessExternalPC);
  61. }
  62. void __tsan_write2_pc(void *addr, void *pc) {
  63. MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 2, kAccessWrite | kAccessExternalPC);
  64. }
  65. void __tsan_write4_pc(void *addr, void *pc) {
  66. MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 4, kAccessWrite | kAccessExternalPC);
  67. }
  68. void __tsan_write8_pc(void *addr, void *pc) {
  69. MemoryAccess(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, 8, kAccessWrite | kAccessExternalPC);
  70. }
  71. ALWAYS_INLINE USED void __tsan_unaligned_read2(const void *addr) {
  72. UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessRead);
  73. }
  74. ALWAYS_INLINE USED void __tsan_unaligned_read4(const void *addr) {
  75. UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessRead);
  76. }
  77. ALWAYS_INLINE USED void __tsan_unaligned_read8(const void *addr) {
  78. UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessRead);
  79. }
  80. ALWAYS_INLINE USED void __tsan_unaligned_write2(void *addr) {
  81. UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, kAccessWrite);
  82. }
  83. ALWAYS_INLINE USED void __tsan_unaligned_write4(void *addr) {
  84. UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, kAccessWrite);
  85. }
  86. ALWAYS_INLINE USED void __tsan_unaligned_write8(void *addr) {
  87. UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, kAccessWrite);
  88. }
  89. extern "C" {
  90. // __sanitizer_unaligned_load/store are for user instrumentation.
  91. SANITIZER_INTERFACE_ATTRIBUTE
  92. u16 __sanitizer_unaligned_load16(const uu16 *addr) {
  93. __tsan_unaligned_read2(addr);
  94. return *addr;
  95. }
  96. SANITIZER_INTERFACE_ATTRIBUTE
  97. u32 __sanitizer_unaligned_load32(const uu32 *addr) {
  98. __tsan_unaligned_read4(addr);
  99. return *addr;
  100. }
  101. SANITIZER_INTERFACE_ATTRIBUTE
  102. u64 __sanitizer_unaligned_load64(const uu64 *addr) {
  103. __tsan_unaligned_read8(addr);
  104. return *addr;
  105. }
  106. SANITIZER_INTERFACE_ATTRIBUTE
  107. void __sanitizer_unaligned_store16(uu16 *addr, u16 v) {
  108. *addr = v;
  109. __tsan_unaligned_write2(addr);
  110. }
  111. SANITIZER_INTERFACE_ATTRIBUTE
  112. void __sanitizer_unaligned_store32(uu32 *addr, u32 v) {
  113. *addr = v;
  114. __tsan_unaligned_write4(addr);
  115. }
  116. SANITIZER_INTERFACE_ATTRIBUTE
  117. void __sanitizer_unaligned_store64(uu64 *addr, u64 v) {
  118. *addr = v;
  119. __tsan_unaligned_write8(addr);
  120. }
  121. }
  122. void __tsan_vptr_update(void **vptr_p, void *new_val) {
  123. if (*vptr_p == new_val)
  124. return;
  125. MemoryAccess(cur_thread(), CALLERPC, (uptr)vptr_p, sizeof(*vptr_p),
  126. kAccessWrite | kAccessVptr);
  127. }
  128. void __tsan_vptr_read(void **vptr_p) {
  129. MemoryAccess(cur_thread(), CALLERPC, (uptr)vptr_p, sizeof(*vptr_p),
  130. kAccessRead | kAccessVptr);
  131. }
  132. void __tsan_func_entry(void *pc) { FuncEntry(cur_thread(), STRIP_PAC_PC(pc)); }
  133. void __tsan_func_exit() { FuncExit(cur_thread()); }
  134. void __tsan_ignore_thread_begin() { ThreadIgnoreBegin(cur_thread(), CALLERPC); }
  135. void __tsan_ignore_thread_end() { ThreadIgnoreEnd(cur_thread()); }
  136. void __tsan_read_range(void *addr, uptr size) {
  137. MemoryAccessRange(cur_thread(), CALLERPC, (uptr)addr, size, false);
  138. }
  139. void __tsan_write_range(void *addr, uptr size) {
  140. MemoryAccessRange(cur_thread(), CALLERPC, (uptr)addr, size, true);
  141. }
  142. void __tsan_read_range_pc(void *addr, uptr size, void *pc) {
  143. MemoryAccessRange(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, size, false);
  144. }
  145. void __tsan_write_range_pc(void *addr, uptr size, void *pc) {
  146. MemoryAccessRange(cur_thread(), STRIP_PAC_PC(pc), (uptr)addr, size, true);
  147. }