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
),
39 #elif SANITIZER_RISCV64
40 // Put pointer into x10
41 // addiw contains immediate of 0x40 + X, where 0x40 is magic number and X
42 // encodes access size
43 register uptr x10
asm("x10") = p
;
46 "addiw x0, x0, %1\n" ::"r"(x10
),
49 // FIXME: not always sigill.
52 // __builtin_unreachable();
55 // Version with access size which is not power of 2
57 __attribute__((always_inline
)) static void SigTrap(uptr p
, uptr size
) {
58 #if defined(__aarch64__)
59 register uptr x0
asm("x0") = p
;
60 register uptr x1
asm("x1") = size
;
61 asm("brk %2\n\t" ::"r"(x0
), "r"(x1
), "n"(0x900 + X
));
62 #elif defined(__x86_64__)
63 // Size is stored in rsi.
66 "nopl %c0(%%rax)\n" ::"n"(0x40 + X
),
68 #elif SANITIZER_RISCV64
69 // Put access size into x11
70 register uptr x10
asm("x10") = p
;
71 register uptr x11
asm("x11") = size
;
74 "addiw x0, x0, %2\n" ::"r"(x10
),
75 "r"(x11
), "I"(0x40 + X
));
79 // __builtin_unreachable();
82 __attribute__((always_inline
, nodebug
)) static bool PossiblyShortTagMatches(
83 tag_t mem_tag
, uptr ptr
, uptr sz
) {
84 tag_t ptr_tag
= GetTagFromPointer(ptr
);
85 if (ptr_tag
== mem_tag
)
87 if (mem_tag
>= kShadowAlignment
)
89 if ((ptr
& (kShadowAlignment
- 1)) + sz
> mem_tag
)
91 #if !defined(__aarch64__) && !(SANITIZER_RISCV64)
94 return *(u8
*)(ptr
| (kShadowAlignment
- 1)) == ptr_tag
;
97 enum class ErrorAction
{ Abort
, Recover
};
98 enum class AccessType
{ Load
, Store
};
100 template <ErrorAction EA
, AccessType AT
, unsigned LogSize
>
101 __attribute__((always_inline
, nodebug
)) static void CheckAddress(uptr p
) {
102 if (!InTaggableRegion(p
))
104 uptr ptr_raw
= p
& ~kAddressTagMask
;
105 tag_t mem_tag
= *(tag_t
*)MemToShadow(ptr_raw
);
106 if (UNLIKELY(!PossiblyShortTagMatches(mem_tag
, p
, 1 << LogSize
))) {
107 SigTrap
<0x20 * (EA
== ErrorAction::Recover
) +
108 0x10 * (AT
== AccessType::Store
) + LogSize
>(p
);
109 if (EA
== ErrorAction::Abort
)
110 __builtin_unreachable();
114 template <ErrorAction EA
, AccessType AT
>
115 __attribute__((always_inline
, nodebug
)) static void CheckAddressSized(uptr p
,
117 if (sz
== 0 || !InTaggableRegion(p
))
119 tag_t ptr_tag
= GetTagFromPointer(p
);
120 uptr ptr_raw
= p
& ~kAddressTagMask
;
121 tag_t
*shadow_first
= (tag_t
*)MemToShadow(ptr_raw
);
122 tag_t
*shadow_last
= (tag_t
*)MemToShadow(ptr_raw
+ sz
);
123 for (tag_t
*t
= shadow_first
; t
< shadow_last
; ++t
)
124 if (UNLIKELY(ptr_tag
!= *t
)) {
125 SigTrap
<0x20 * (EA
== ErrorAction::Recover
) +
126 0x10 * (AT
== AccessType::Store
) + 0xf>(p
, sz
);
127 if (EA
== ErrorAction::Abort
)
128 __builtin_unreachable();
131 uptr tail_sz
= end
& 0xf;
132 if (UNLIKELY(tail_sz
!= 0 &&
133 !PossiblyShortTagMatches(
134 *shadow_last
, end
& ~(kShadowAlignment
- 1), tail_sz
))) {
135 SigTrap
<0x20 * (EA
== ErrorAction::Recover
) +
136 0x10 * (AT
== AccessType::Store
) + 0xf>(p
, sz
);
137 if (EA
== ErrorAction::Abort
)
138 __builtin_unreachable();
142 } // end namespace __hwasan
144 #endif // HWASAN_CHECKS_H