hwasan_checks.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //===-- hwasan_checks.h -----------------------------------------*- 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 HWAddressSanitizer.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef HWASAN_CHECKS_H
  13. #define HWASAN_CHECKS_H
  14. #include "hwasan_allocator.h"
  15. #include "hwasan_mapping.h"
  16. #include "hwasan_registers.h"
  17. #include "sanitizer_common/sanitizer_common.h"
  18. namespace __hwasan {
  19. enum class ErrorAction { Abort, Recover };
  20. enum class AccessType { Load, Store };
  21. // Used when the access size is known.
  22. constexpr unsigned SigTrapEncoding(ErrorAction EA, AccessType AT,
  23. unsigned LogSize) {
  24. return 0x20 * (EA == ErrorAction::Recover) +
  25. 0x10 * (AT == AccessType::Store) + LogSize;
  26. }
  27. // Used when the access size varies at runtime.
  28. constexpr unsigned SigTrapEncoding(ErrorAction EA, AccessType AT) {
  29. return SigTrapEncoding(EA, AT, 0xf);
  30. }
  31. template <ErrorAction EA, AccessType AT, size_t LogSize>
  32. __attribute__((always_inline)) static void SigTrap(uptr p) {
  33. // Other platforms like linux can use signals for intercepting an exception
  34. // and dispatching to HandleTagMismatch. The fuchsias implementation doesn't
  35. // use signals so we can call it here directly instead.
  36. #if CAN_GET_REGISTERS && SANITIZER_FUCHSIA
  37. auto regs = GetRegisters();
  38. size_t size = 2 << LogSize;
  39. AccessInfo access_info = {
  40. .addr = p,
  41. .size = size,
  42. .is_store = AT == AccessType::Store,
  43. .is_load = AT == AccessType::Load,
  44. .recover = EA == ErrorAction::Recover,
  45. };
  46. HandleTagMismatch(access_info, (uptr)__builtin_return_address(0),
  47. (uptr)__builtin_frame_address(0), /*uc=*/nullptr, regs.x);
  48. #elif defined(__aarch64__)
  49. (void)p;
  50. // 0x900 is added to do not interfere with the kernel use of lower values of
  51. // brk immediate.
  52. register uptr x0 asm("x0") = p;
  53. asm("brk %1\n\t" ::"r"(x0), "n"(0x900 + SigTrapEncoding(EA, AT, LogSize)));
  54. #elif defined(__x86_64__)
  55. // INT3 + NOP DWORD ptr [EAX + X] to pass X to our signal handler, 5 bytes
  56. // total. The pointer is passed via rdi.
  57. // 0x40 is added as a safeguard, to help distinguish our trap from others and
  58. // to avoid 0 offsets in the command (otherwise it'll be reduced to a
  59. // different nop command, the three bytes one).
  60. asm volatile(
  61. "int3\n"
  62. "nopl %c0(%%rax)\n" ::"n"(0x40 + SigTrapEncoding(EA, AT, LogSize)),
  63. "D"(p));
  64. #elif SANITIZER_RISCV64
  65. // Put pointer into x10
  66. // addiw contains immediate of 0x40 + X, where 0x40 is magic number and X
  67. // encodes access size
  68. register uptr x10 asm("x10") = p;
  69. asm volatile(
  70. "ebreak\n"
  71. "addiw x0, x0, %1\n" ::"r"(x10),
  72. "I"(0x40 + SigTrapEncoding(EA, AT, LogSize)));
  73. #else
  74. // FIXME: not always sigill.
  75. __builtin_trap();
  76. #endif
  77. // __builtin_unreachable();
  78. }
  79. // Version with access size which is not power of 2
  80. template <ErrorAction EA, AccessType AT>
  81. __attribute__((always_inline)) static void SigTrap(uptr p, uptr size) {
  82. // Other platforms like linux can use signals for intercepting an exception
  83. // and dispatching to HandleTagMismatch. The fuchsias implementation doesn't
  84. // use signals so we can call it here directly instead.
  85. #if CAN_GET_REGISTERS && SANITIZER_FUCHSIA
  86. auto regs = GetRegisters();
  87. AccessInfo access_info = {
  88. .addr = p,
  89. .size = size,
  90. .is_store = AT == AccessType::Store,
  91. .is_load = AT == AccessType::Load,
  92. .recover = EA == ErrorAction::Recover,
  93. };
  94. HandleTagMismatch(access_info, (uptr)__builtin_return_address(0),
  95. (uptr)__builtin_frame_address(0), /*uc=*/nullptr, regs.x);
  96. #elif defined(__aarch64__)
  97. register uptr x0 asm("x0") = p;
  98. register uptr x1 asm("x1") = size;
  99. asm("brk %2\n\t" ::"r"(x0), "r"(x1), "n"(0x900 + SigTrapEncoding(EA, AT)));
  100. #elif defined(__x86_64__)
  101. // Size is stored in rsi.
  102. asm volatile(
  103. "int3\n"
  104. "nopl %c0(%%rax)\n" ::"n"(0x40 + SigTrapEncoding(EA, AT)),
  105. "D"(p), "S"(size));
  106. #elif SANITIZER_RISCV64
  107. // Put access size into x11
  108. register uptr x10 asm("x10") = p;
  109. register uptr x11 asm("x11") = size;
  110. asm volatile(
  111. "ebreak\n"
  112. "addiw x0, x0, %2\n" ::"r"(x10),
  113. "r"(x11), "I"(0x40 + SigTrapEncoding(EA, AT)));
  114. #else
  115. __builtin_trap();
  116. #endif
  117. // __builtin_unreachable();
  118. }
  119. __attribute__((always_inline, nodebug)) static inline uptr ShortTagSize(
  120. tag_t mem_tag, uptr ptr) {
  121. DCHECK(IsAligned(ptr, kShadowAlignment));
  122. tag_t ptr_tag = GetTagFromPointer(ptr);
  123. if (ptr_tag == mem_tag)
  124. return kShadowAlignment;
  125. if (!mem_tag || mem_tag >= kShadowAlignment)
  126. return 0;
  127. if (*(u8 *)(ptr | (kShadowAlignment - 1)) != ptr_tag)
  128. return 0;
  129. return mem_tag;
  130. }
  131. __attribute__((always_inline, nodebug)) static inline bool
  132. PossiblyShortTagMatches(tag_t mem_tag, uptr ptr, uptr sz) {
  133. DCHECK(IsAligned(ptr, kShadowAlignment));
  134. tag_t ptr_tag = GetTagFromPointer(ptr);
  135. if (ptr_tag == mem_tag)
  136. return true;
  137. if (mem_tag >= kShadowAlignment)
  138. return false;
  139. if ((ptr & (kShadowAlignment - 1)) + sz > mem_tag)
  140. return false;
  141. return *(u8 *)(ptr | (kShadowAlignment - 1)) == ptr_tag;
  142. }
  143. template <ErrorAction EA, AccessType AT, unsigned LogSize>
  144. __attribute__((always_inline, nodebug)) static void CheckAddress(uptr p) {
  145. if (!InTaggableRegion(p))
  146. return;
  147. uptr ptr_raw = p & ~kAddressTagMask;
  148. tag_t mem_tag = *(tag_t *)MemToShadow(ptr_raw);
  149. if (UNLIKELY(!PossiblyShortTagMatches(mem_tag, p, 1 << LogSize))) {
  150. SigTrap<EA, AT, LogSize>(p);
  151. if (EA == ErrorAction::Abort)
  152. __builtin_unreachable();
  153. }
  154. }
  155. template <ErrorAction EA, AccessType AT>
  156. __attribute__((always_inline, nodebug)) static void CheckAddressSized(uptr p,
  157. uptr sz) {
  158. if (sz == 0 || !InTaggableRegion(p))
  159. return;
  160. tag_t ptr_tag = GetTagFromPointer(p);
  161. uptr ptr_raw = p & ~kAddressTagMask;
  162. tag_t *shadow_first = (tag_t *)MemToShadow(ptr_raw);
  163. tag_t *shadow_last = (tag_t *)MemToShadow(ptr_raw + sz);
  164. for (tag_t *t = shadow_first; t < shadow_last; ++t)
  165. if (UNLIKELY(ptr_tag != *t)) {
  166. SigTrap<EA, AT>(p, sz);
  167. if (EA == ErrorAction::Abort)
  168. __builtin_unreachable();
  169. }
  170. uptr end = p + sz;
  171. uptr tail_sz = end & (kShadowAlignment - 1);
  172. if (UNLIKELY(tail_sz != 0 &&
  173. !PossiblyShortTagMatches(
  174. *shadow_last, end & ~(kShadowAlignment - 1), tail_sz))) {
  175. SigTrap<EA, AT>(p, sz);
  176. if (EA == ErrorAction::Abort)
  177. __builtin_unreachable();
  178. }
  179. }
  180. } // end namespace __hwasan
  181. #endif // HWASAN_CHECKS_H