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 "sanitizer_common/sanitizer_common.h"
20 #include "sanitizer_common/sanitizer_flags.h"
21 #include "sanitizer_common/sanitizer_internal_defs.h"
22 #include "sanitizer_common/sanitizer_stacktrace.h"
23 #include "ubsan/ubsan_platform.h"
25 #ifndef HWASAN_CONTAINS_UBSAN
26 # define HWASAN_CONTAINS_UBSAN CAN_SANITIZE_UB
29 #ifndef HWASAN_WITH_INTERCEPTORS
30 #define HWASAN_WITH_INTERCEPTORS 0
33 #ifndef HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
34 #define HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE HWASAN_WITH_INTERCEPTORS
39 #if defined(HWASAN_ALIASING_MODE)
40 # if !defined(__x86_64__)
41 # error Aliasing mode is only supported on x86_64
43 // Tags are done in middle bits using userspace aliasing.
44 constexpr unsigned kAddressTagShift
= 39;
45 constexpr unsigned kTagBits
= 3;
47 // The alias region is placed next to the shadow so the upper bits of all
48 // taggable addresses matches the upper bits of the shadow base. This shift
49 // value determines which upper bits must match. It has a floor of 44 since the
50 // shadow is always 8TB.
51 // TODO(morehouse): In alias mode we can shrink the shadow and use a
52 // simpler/faster shadow calculation.
53 constexpr unsigned kTaggableRegionCheckShift
=
54 __sanitizer::Max(kAddressTagShift
+ kTagBits
+ 1U, 44U);
55 #elif defined(__x86_64__)
56 // Tags are done in upper bits using Intel LAM.
57 constexpr unsigned kAddressTagShift
= 57;
58 constexpr unsigned kTagBits
= 6;
60 // TBI (Top Byte Ignore) feature of AArch64: bits [63:56] are ignored in address
61 // translation and can be used to store a tag.
62 constexpr unsigned kAddressTagShift
= 56;
63 constexpr unsigned kTagBits
= 8;
64 #endif // defined(HWASAN_ALIASING_MODE)
66 // Mask for extracting tag bits from the lower 8 bits.
67 constexpr uptr kTagMask
= (1UL << kTagBits
) - 1;
69 // Mask for extracting tag bits from full pointers.
70 constexpr uptr kAddressTagMask
= kTagMask
<< kAddressTagShift
;
72 // Minimal alignment of the shadow base address. Determines the space available
73 // for threads and stack histories. This is an ABI constant.
74 const unsigned kShadowBaseAlignment
= 32;
76 const unsigned kRecordAddrBaseTagShift
= 3;
77 const unsigned kRecordFPShift
= 48;
78 const unsigned kRecordFPLShift
= 4;
79 const unsigned kRecordFPModulus
= 1 << (64 - kRecordFPShift
+ kRecordFPLShift
);
81 static inline tag_t
GetTagFromPointer(uptr p
) {
82 return (p
>> kAddressTagShift
) & kTagMask
;
85 static inline uptr
UntagAddr(uptr tagged_addr
) {
86 return tagged_addr
& ~kAddressTagMask
;
89 static inline void *UntagPtr(const void *tagged_ptr
) {
90 return reinterpret_cast<void *>(
91 UntagAddr(reinterpret_cast<uptr
>(tagged_ptr
)));
94 static inline uptr
AddTagToPointer(uptr p
, tag_t tag
) {
95 return (p
& ~kAddressTagMask
) | ((uptr
)tag
<< kAddressTagShift
);
100 extern int hwasan_inited
;
101 extern bool hwasan_init_is_running
;
102 extern int hwasan_report_count
;
105 void InitializeOsSupport();
107 void InitializeInterceptors();
109 void HwasanAllocatorInit();
110 void HwasanAllocatorLock();
111 void HwasanAllocatorUnlock();
113 void *hwasan_malloc(uptr size
, StackTrace
*stack
);
114 void *hwasan_calloc(uptr nmemb
, uptr size
, StackTrace
*stack
);
115 void *hwasan_realloc(void *ptr
, uptr size
, StackTrace
*stack
);
116 void *hwasan_reallocarray(void *ptr
, uptr nmemb
, uptr size
, StackTrace
*stack
);
117 void *hwasan_valloc(uptr size
, StackTrace
*stack
);
118 void *hwasan_pvalloc(uptr size
, StackTrace
*stack
);
119 void *hwasan_aligned_alloc(uptr alignment
, uptr size
, StackTrace
*stack
);
120 void *hwasan_memalign(uptr alignment
, uptr size
, StackTrace
*stack
);
121 int hwasan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
123 void hwasan_free(void *ptr
, StackTrace
*stack
);
125 void InstallAtExitHandler();
127 #define GET_MALLOC_STACK_TRACE \
128 BufferedStackTrace stack; \
130 stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), \
131 nullptr, common_flags()->fast_unwind_on_malloc, \
132 common_flags()->malloc_context_size)
134 #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \
135 BufferedStackTrace stack; \
137 stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal)
139 void HwasanTSDInit();
140 void HwasanTSDThreadInit();
143 void HwasanOnDeadlySignal(int signo
, void *info
, void *context
);
145 void HwasanInstallAtForkHandler();
147 void UpdateMemoryUsage();
149 void AppendToErrorMessageBuffer(const char *buffer
);
151 void AndroidTestTlsSlot();
153 // This is a compiler-generated struct that can be shared between hwasan
163 // Given access info and frame information, unwind the stack and report the tag
165 void HandleTagMismatch(AccessInfo ai
, uptr pc
, uptr frame
, void *uc
,
166 uptr
*registers_frame
= nullptr);
168 // This dispatches to HandleTagMismatch but sets up the AccessInfo, program
169 // counter, and frame pointer.
170 void HwasanTagMismatch(uptr addr
, uptr access_info
, uptr
*registers_frame
,
173 } // namespace __hwasan
175 #define HWASAN_MALLOC_HOOK(ptr, size) \
177 if (&__sanitizer_malloc_hook) { \
178 __sanitizer_malloc_hook(ptr, size); \
180 RunMallocHooks(ptr, size); \
182 #define HWASAN_FREE_HOOK(ptr) \
184 if (&__sanitizer_free_hook) { \
185 __sanitizer_free_hook(ptr); \
190 #if HWASAN_WITH_INTERCEPTORS
191 // For both bionic and glibc __sigset_t is an unsigned long.
192 typedef unsigned long __hw_sigset_t
;
193 // Setjmp and longjmp implementations are platform specific, and hence the
194 // interception code is platform specific too.
195 # if defined(__aarch64__)
196 constexpr size_t kHwRegisterBufSize
= 22;
197 # elif defined(__x86_64__)
198 constexpr size_t kHwRegisterBufSize
= 8;
200 typedef unsigned long long __hw_register_buf
[kHwRegisterBufSize
];
201 struct __hw_jmp_buf_struct
{
202 // NOTE: The machine-dependent definition of `__sigsetjmp'
203 // assume that a `__hw_jmp_buf' begins with a `__hw_register_buf' and that
204 // `__mask_was_saved' follows it. Do not move these members or add others
207 // We add a __magic field to our struct to catch cases where libc's setjmp
208 // populated the jmp_buf instead of our interceptor.
209 __hw_register_buf __jmpbuf
; // Calling environment.
210 unsigned __mask_was_saved
: 1; // Saved the signal mask?
211 unsigned __magic
: 31; // Used to distinguish __hw_jmp_buf from jmp_buf.
212 __hw_sigset_t __saved_mask
; // Saved signal mask.
214 typedef struct __hw_jmp_buf_struct __hw_jmp_buf
[1];
215 typedef struct __hw_jmp_buf_struct __hw_sigjmp_buf
[1];
216 constexpr unsigned kHwJmpBufMagic
= 0x248ACE77;
217 #endif // HWASAN_WITH_INTERCEPTORS
219 #define ENSURE_HWASAN_INITED() \
221 CHECK(!hwasan_init_is_running); \
222 if (!hwasan_inited) { \