hwasan_checks.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "sanitizer_common/sanitizer_common.h"
  17. namespace __hwasan {
  18. template <unsigned X>
  19. __attribute__((always_inline)) static void SigTrap(uptr p) {
  20. #if defined(__aarch64__)
  21. (void)p;
  22. // 0x900 is added to do not interfere with the kernel use of lower values of
  23. // brk immediate.
  24. register uptr x0 asm("x0") = p;
  25. asm("brk %1\n\t" ::"r"(x0), "n"(0x900 + X));
  26. #elif defined(__x86_64__)
  27. // INT3 + NOP DWORD ptr [EAX + X] to pass X to our signal handler, 5 bytes
  28. // total. The pointer is passed via rdi.
  29. // 0x40 is added as a safeguard, to help distinguish our trap from others and
  30. // to avoid 0 offsets in the command (otherwise it'll be reduced to a
  31. // different nop command, the three bytes one).
  32. asm volatile(
  33. "int3\n"
  34. "nopl %c0(%%rax)\n" ::"n"(0x40 + X),
  35. "D"(p));
  36. #else
  37. // FIXME: not always sigill.
  38. __builtin_trap();
  39. #endif
  40. // __builtin_unreachable();
  41. }
  42. // Version with access size which is not power of 2
  43. template <unsigned X>
  44. __attribute__((always_inline)) static void SigTrap(uptr p, uptr size) {
  45. #if defined(__aarch64__)
  46. register uptr x0 asm("x0") = p;
  47. register uptr x1 asm("x1") = size;
  48. asm("brk %2\n\t" ::"r"(x0), "r"(x1), "n"(0x900 + X));
  49. #elif defined(__x86_64__)
  50. // Size is stored in rsi.
  51. asm volatile(
  52. "int3\n"
  53. "nopl %c0(%%rax)\n" ::"n"(0x40 + X),
  54. "D"(p), "S"(size));
  55. #else
  56. __builtin_trap();
  57. #endif
  58. // __builtin_unreachable();
  59. }
  60. __attribute__((always_inline, nodebug)) static bool PossiblyShortTagMatches(
  61. tag_t mem_tag, uptr ptr, uptr sz) {
  62. tag_t ptr_tag = GetTagFromPointer(ptr);
  63. if (ptr_tag == mem_tag)
  64. return true;
  65. if (mem_tag >= kShadowAlignment)
  66. return false;
  67. if ((ptr & (kShadowAlignment - 1)) + sz > mem_tag)
  68. return false;
  69. #ifndef __aarch64__
  70. ptr = UntagAddr(ptr);
  71. #endif
  72. return *(u8 *)(ptr | (kShadowAlignment - 1)) == ptr_tag;
  73. }
  74. enum class ErrorAction { Abort, Recover };
  75. enum class AccessType { Load, Store };
  76. template <ErrorAction EA, AccessType AT, unsigned LogSize>
  77. __attribute__((always_inline, nodebug)) static void CheckAddress(uptr p) {
  78. if (!InTaggableRegion(p))
  79. return;
  80. uptr ptr_raw = p & ~kAddressTagMask;
  81. tag_t mem_tag = *(tag_t *)MemToShadow(ptr_raw);
  82. if (UNLIKELY(!PossiblyShortTagMatches(mem_tag, p, 1 << LogSize))) {
  83. SigTrap<0x20 * (EA == ErrorAction::Recover) +
  84. 0x10 * (AT == AccessType::Store) + LogSize>(p);
  85. if (EA == ErrorAction::Abort)
  86. __builtin_unreachable();
  87. }
  88. }
  89. template <ErrorAction EA, AccessType AT>
  90. __attribute__((always_inline, nodebug)) static void CheckAddressSized(uptr p,
  91. uptr sz) {
  92. if (sz == 0 || !InTaggableRegion(p))
  93. return;
  94. tag_t ptr_tag = GetTagFromPointer(p);
  95. uptr ptr_raw = p & ~kAddressTagMask;
  96. tag_t *shadow_first = (tag_t *)MemToShadow(ptr_raw);
  97. tag_t *shadow_last = (tag_t *)MemToShadow(ptr_raw + sz);
  98. for (tag_t *t = shadow_first; t < shadow_last; ++t)
  99. if (UNLIKELY(ptr_tag != *t)) {
  100. SigTrap<0x20 * (EA == ErrorAction::Recover) +
  101. 0x10 * (AT == AccessType::Store) + 0xf>(p, sz);
  102. if (EA == ErrorAction::Abort)
  103. __builtin_unreachable();
  104. }
  105. uptr end = p + sz;
  106. uptr tail_sz = end & 0xf;
  107. if (UNLIKELY(tail_sz != 0 &&
  108. !PossiblyShortTagMatches(
  109. *shadow_last, end & ~(kShadowAlignment - 1), tail_sz))) {
  110. SigTrap<0x20 * (EA == ErrorAction::Recover) +
  111. 0x10 * (AT == AccessType::Store) + 0xf>(p, sz);
  112. if (EA == ErrorAction::Abort)
  113. __builtin_unreachable();
  114. }
  115. }
  116. } // end namespace __hwasan
  117. #endif // HWASAN_CHECKS_H