1 //===-- hwasan_checks.h -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of HWAddressSanitizer.
11 //===----------------------------------------------------------------------===//
13 #ifndef HWASAN_CHECKS_H
14 #define HWASAN_CHECKS_H
16 #include "hwasan_allocator.h"
17 #include "hwasan_mapping.h"
18 #include "sanitizer_common/sanitizer_common.h"
22 __attribute__((always_inline
)) static void SigTrap(uptr p
) {
23 #if defined(__aarch64__)
25 // 0x900 is added to do not interfere with the kernel use of lower values of
27 register uptr x0
asm("x0") = p
;
28 asm("brk %1\n\t" ::"r"(x0
), "n"(0x900 + X
));
29 #elif defined(__x86_64__)
30 // INT3 + NOP DWORD ptr [EAX + X] to pass X to our signal handler, 5 bytes
31 // total. The pointer is passed via rdi.
32 // 0x40 is added as a safeguard, to help distinguish our trap from others and
33 // to avoid 0 offsets in the command (otherwise it'll be reduced to a
34 // different nop command, the three bytes one).
37 "nopl %c0(%%rax)\n" ::"n"(0x40 + X
),
40 // FIXME: not always sigill.
43 // __builtin_unreachable();
46 // Version with access size which is not power of 2
48 __attribute__((always_inline
)) static void SigTrap(uptr p
, uptr size
) {
49 #if defined(__aarch64__)
50 register uptr x0
asm("x0") = p
;
51 register uptr x1
asm("x1") = size
;
52 asm("brk %2\n\t" ::"r"(x0
), "r"(x1
), "n"(0x900 + X
));
53 #elif defined(__x86_64__)
54 // Size is stored in rsi.
57 "nopl %c0(%%rax)\n" ::"n"(0x40 + X
),
62 // __builtin_unreachable();
65 __attribute__((always_inline
, nodebug
)) static bool PossiblyShortTagMatches(
66 tag_t mem_tag
, uptr ptr
, uptr sz
) {
67 tag_t ptr_tag
= GetTagFromPointer(ptr
);
68 if (ptr_tag
== mem_tag
)
70 if (mem_tag
>= kShadowAlignment
)
72 if ((ptr
& (kShadowAlignment
- 1)) + sz
> mem_tag
)
77 return *(u8
*)(ptr
| (kShadowAlignment
- 1)) == ptr_tag
;
80 enum class ErrorAction
{ Abort
, Recover
};
81 enum class AccessType
{ Load
, Store
};
83 template <ErrorAction EA
, AccessType AT
, unsigned LogSize
>
84 __attribute__((always_inline
, nodebug
)) static void CheckAddress(uptr p
) {
85 if (!InTaggableRegion(p
))
87 uptr ptr_raw
= p
& ~kAddressTagMask
;
88 tag_t mem_tag
= *(tag_t
*)MemToShadow(ptr_raw
);
89 if (UNLIKELY(!PossiblyShortTagMatches(mem_tag
, p
, 1 << LogSize
))) {
90 SigTrap
<0x20 * (EA
== ErrorAction::Recover
) +
91 0x10 * (AT
== AccessType::Store
) + LogSize
>(p
);
92 if (EA
== ErrorAction::Abort
)
93 __builtin_unreachable();
97 template <ErrorAction EA
, AccessType AT
>
98 __attribute__((always_inline
, nodebug
)) static void CheckAddressSized(uptr p
,
100 if (sz
== 0 || !InTaggableRegion(p
))
102 tag_t ptr_tag
= GetTagFromPointer(p
);
103 uptr ptr_raw
= p
& ~kAddressTagMask
;
104 tag_t
*shadow_first
= (tag_t
*)MemToShadow(ptr_raw
);
105 tag_t
*shadow_last
= (tag_t
*)MemToShadow(ptr_raw
+ sz
);
106 for (tag_t
*t
= shadow_first
; t
< shadow_last
; ++t
)
107 if (UNLIKELY(ptr_tag
!= *t
)) {
108 SigTrap
<0x20 * (EA
== ErrorAction::Recover
) +
109 0x10 * (AT
== AccessType::Store
) + 0xf>(p
, sz
);
110 if (EA
== ErrorAction::Abort
)
111 __builtin_unreachable();
114 uptr tail_sz
= end
& 0xf;
115 if (UNLIKELY(tail_sz
!= 0 &&
116 !PossiblyShortTagMatches(
117 *shadow_last
, end
& ~(kShadowAlignment
- 1), tail_sz
))) {
118 SigTrap
<0x20 * (EA
== ErrorAction::Recover
) +
119 0x10 * (AT
== AccessType::Store
) + 0xf>(p
, sz
);
120 if (EA
== ErrorAction::Abort
)
121 __builtin_unreachable();
125 } // end namespace __hwasan
127 #endif // HWASAN_CHECKS_H