1 //===-- hwasan.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 // Private Hwasan header.
12 //===----------------------------------------------------------------------===//
17 #include "hwasan_flags.h"
18 #include "hwasan_interface_internal.h"
19 #include "hwasan_mapping.h"
20 #include "sanitizer_common/sanitizer_common.h"
21 #include "sanitizer_common/sanitizer_flags.h"
22 #include "sanitizer_common/sanitizer_internal_defs.h"
23 #include "sanitizer_common/sanitizer_stacktrace.h"
24 #include "ubsan/ubsan_platform.h"
26 #ifndef HWASAN_CONTAINS_UBSAN
27 # define HWASAN_CONTAINS_UBSAN CAN_SANITIZE_UB
30 #ifndef HWASAN_WITH_INTERCEPTORS
31 #define HWASAN_WITH_INTERCEPTORS 0
34 #ifndef HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
35 #define HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE HWASAN_WITH_INTERCEPTORS
40 #if defined(HWASAN_ALIASING_MODE)
41 # if !defined(__x86_64__)
42 # error Aliasing mode is only supported on x86_64
44 // Tags are done in middle bits using userspace aliasing.
45 constexpr unsigned kAddressTagShift
= 39;
46 constexpr unsigned kTagBits
= 3;
48 // The alias region is placed next to the shadow so the upper bits of all
49 // taggable addresses matches the upper bits of the shadow base. This shift
50 // value determines which upper bits must match. It has a floor of 44 since the
51 // shadow is always 8TB.
52 // TODO(morehouse): In alias mode we can shrink the shadow and use a
53 // simpler/faster shadow calculation.
54 constexpr unsigned kTaggableRegionCheckShift
=
55 __sanitizer::Max(kAddressTagShift
+ kTagBits
+ 1U, 44U);
56 #elif defined(__x86_64__)
57 // Tags are done in upper bits using Intel LAM.
58 constexpr unsigned kAddressTagShift
= 57;
59 constexpr unsigned kTagBits
= 6;
61 // TBI (Top Byte Ignore) feature of AArch64: bits [63:56] are ignored in address
62 // translation and can be used to store a tag.
63 constexpr unsigned kAddressTagShift
= 56;
64 constexpr unsigned kTagBits
= 8;
65 #endif // defined(HWASAN_ALIASING_MODE)
67 // Mask for extracting tag bits from the lower 8 bits.
68 constexpr uptr kTagMask
= (1UL << kTagBits
) - 1;
70 // Mask for extracting tag bits from full pointers.
71 constexpr uptr kAddressTagMask
= kTagMask
<< kAddressTagShift
;
73 // Minimal alignment of the shadow base address. Determines the space available
74 // for threads and stack histories. This is an ABI constant.
75 const unsigned kShadowBaseAlignment
= 32;
77 const unsigned kRecordAddrBaseTagShift
= 3;
78 const unsigned kRecordFPShift
= 48;
79 const unsigned kRecordFPLShift
= 4;
80 const unsigned kRecordFPModulus
= 1 << (64 - kRecordFPShift
+ kRecordFPLShift
);
82 static inline bool InTaggableRegion(uptr addr
) {
83 #if defined(HWASAN_ALIASING_MODE)
84 // Aliases are mapped next to shadow so that the upper bits match the shadow
86 return (addr
>> kTaggableRegionCheckShift
) ==
87 (__hwasan::GetShadowOffset() >> kTaggableRegionCheckShift
);
92 static inline tag_t
GetTagFromPointer(uptr p
) {
93 return InTaggableRegion(p
) ? ((p
>> kAddressTagShift
) & kTagMask
) : 0;
96 static inline uptr
UntagAddr(uptr tagged_addr
) {
97 return InTaggableRegion(tagged_addr
) ? (tagged_addr
& ~kAddressTagMask
)
101 static inline void *UntagPtr(const void *tagged_ptr
) {
102 return reinterpret_cast<void *>(
103 UntagAddr(reinterpret_cast<uptr
>(tagged_ptr
)));
106 static inline uptr
AddTagToPointer(uptr p
, tag_t tag
) {
107 return InTaggableRegion(p
) ? ((p
& ~kAddressTagMask
) |
108 ((uptr
)(tag
& kTagMask
) << kAddressTagShift
))
114 extern int hwasan_inited
;
115 extern bool hwasan_init_is_running
;
116 extern int hwasan_report_count
;
119 void InitializeOsSupport();
121 void InitializeInterceptors();
123 void HwasanAllocatorInit();
124 void HwasanAllocatorLock();
125 void HwasanAllocatorUnlock();
127 void *hwasan_malloc(uptr size
, StackTrace
*stack
);
128 void *hwasan_calloc(uptr nmemb
, uptr size
, StackTrace
*stack
);
129 void *hwasan_realloc(void *ptr
, uptr size
, StackTrace
*stack
);
130 void *hwasan_reallocarray(void *ptr
, uptr nmemb
, uptr size
, StackTrace
*stack
);
131 void *hwasan_valloc(uptr size
, StackTrace
*stack
);
132 void *hwasan_pvalloc(uptr size
, StackTrace
*stack
);
133 void *hwasan_aligned_alloc(uptr alignment
, uptr size
, StackTrace
*stack
);
134 void *hwasan_memalign(uptr alignment
, uptr size
, StackTrace
*stack
);
135 int hwasan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
137 void hwasan_free(void *ptr
, StackTrace
*stack
);
139 void InstallAtExitHandler();
141 #define GET_MALLOC_STACK_TRACE \
142 UNINITIALIZED BufferedStackTrace stack; \
144 stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), \
145 nullptr, common_flags()->fast_unwind_on_malloc, \
146 common_flags()->malloc_context_size)
148 #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \
149 UNINITIALIZED BufferedStackTrace stack; \
151 stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal)
153 void HwasanTSDInit();
154 void HwasanTSDThreadInit();
157 void HwasanOnDeadlySignal(int signo
, void *info
, void *context
);
159 void HwasanInstallAtForkHandler();
161 void InstallAtExitCheckLeaks();
163 void UpdateMemoryUsage();
165 void AppendToErrorMessageBuffer(const char *buffer
);
167 void AndroidTestTlsSlot();
169 // This is a compiler-generated struct that can be shared between hwasan
179 // Given access info and frame information, unwind the stack and report the tag
181 void HandleTagMismatch(AccessInfo ai
, uptr pc
, uptr frame
, void *uc
,
182 uptr
*registers_frame
= nullptr);
184 // This dispatches to HandleTagMismatch but sets up the AccessInfo, program
185 // counter, and frame pointer.
186 void HwasanTagMismatch(uptr addr
, uptr pc
, uptr frame
, uptr access_info
,
187 uptr
*registers_frame
, size_t outsize
);
189 } // namespace __hwasan
191 #if HWASAN_WITH_INTERCEPTORS
192 // For both bionic and glibc __sigset_t is an unsigned long.
193 typedef unsigned long __hw_sigset_t
;
194 // Setjmp and longjmp implementations are platform specific, and hence the
195 // interception code is platform specific too.
196 # if defined(__aarch64__)
197 constexpr size_t kHwRegisterBufSize
= 22;
198 # elif defined(__x86_64__)
199 constexpr size_t kHwRegisterBufSize
= 8;
200 # elif SANITIZER_RISCV64
201 // saving PC, 12 int regs, sp, 12 fp regs
202 # ifndef __riscv_float_abi_soft
203 constexpr size_t kHwRegisterBufSize
= 1 + 12 + 1 + 12;
205 constexpr size_t kHwRegisterBufSize
= 1 + 12 + 1;
208 typedef unsigned long long __hw_register_buf
[kHwRegisterBufSize
];
209 struct __hw_jmp_buf_struct
{
210 // NOTE: The machine-dependent definition of `__sigsetjmp'
211 // assume that a `__hw_jmp_buf' begins with a `__hw_register_buf' and that
212 // `__mask_was_saved' follows it. Do not move these members or add others
215 // We add a __magic field to our struct to catch cases where libc's setjmp
216 // populated the jmp_buf instead of our interceptor.
217 __hw_register_buf __jmpbuf
; // Calling environment.
218 unsigned __mask_was_saved
: 1; // Saved the signal mask?
219 unsigned __magic
: 31; // Used to distinguish __hw_jmp_buf from jmp_buf.
220 __hw_sigset_t __saved_mask
; // Saved signal mask.
222 typedef struct __hw_jmp_buf_struct __hw_jmp_buf
[1];
223 typedef struct __hw_jmp_buf_struct __hw_sigjmp_buf
[1];
224 constexpr unsigned kHwJmpBufMagic
= 0x248ACE77;
225 #endif // HWASAN_WITH_INTERCEPTORS
227 #define ENSURE_HWASAN_INITED() \
229 CHECK(!hwasan_init_is_running); \
230 if (!hwasan_inited) { \