1//===-- hwasan.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// Private Hwasan header.
12//===----------------------------------------------------------------------===//
13
14#ifndef HWASAN_H
15#define HWASAN_H
16
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"
25
26#ifndef HWASAN_CONTAINS_UBSAN
27# define HWASAN_CONTAINS_UBSAN CAN_SANITIZE_UB
28#endif
29
30#ifndef HWASAN_WITH_INTERCEPTORS
31#define HWASAN_WITH_INTERCEPTORS 0
32#endif
33
34#ifndef HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
35#define HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE HWASAN_WITH_INTERCEPTORS
36#endif
37
38typedef u8 tag_t;
39
40#if defined(HWASAN_ALIASING_MODE)
41# if !defined(__x86_64__)
42# error Aliasing mode is only supported on x86_64
43# endif
44// Tags are done in middle bits using userspace aliasing.
45constexpr unsigned kAddressTagShift = 39;
46constexpr unsigned kTagBits = 3;
47
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.
54constexpr unsigned kTaggableRegionCheckShift =
55 __sanitizer::Max(kAddressTagShift + kTagBits + 1U, 44U);
56#elif defined(__x86_64__)
57// Tags are done in upper bits using Intel LAM.
58constexpr unsigned kAddressTagShift = 57;
59constexpr unsigned kTagBits = 6;
60#elif defined(__aarch64__)
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.
63constexpr unsigned kAddressTagShift = 56;
64constexpr unsigned kTagBits = 8;
65#elif SANITIZER_RISCV64
66// Pointer Masking extension for RISC-V: Top PMLEN (16 or 7) bits are ignored in
67// address translation and can be used to store a tag.
68constexpr unsigned kAddressTagShift = 56;
69constexpr unsigned kTagBits = 8;
70#else
71# error Architecture not supported
72#endif // defined(HWASAN_ALIASING_MODE)
73
74// Mask for extracting tag bits from the lower 8 bits.
75constexpr uptr kTagMask = (1UL << kTagBits) - 1;
76
77// Mask for extracting tag bits from full pointers.
78constexpr uptr kAddressTagMask = kTagMask << kAddressTagShift;
79
80// Minimal alignment of the shadow base address. Determines the space available
81// for threads and stack histories. This is an ABI constant.
82const unsigned kShadowBaseAlignment = 32;
83
84const unsigned kRecordAddrBaseTagShift = 3;
85const unsigned kRecordFPShift = 48;
86const unsigned kRecordFPLShift = 4;
87const unsigned kRecordFPModulus = 1 << (64 - kRecordFPShift + kRecordFPLShift);
88
89static inline bool InTaggableRegion(uptr addr) {
90#if defined(HWASAN_ALIASING_MODE)
91 // Aliases are mapped next to shadow so that the upper bits match the shadow
92 // base.
93 return (addr >> kTaggableRegionCheckShift) ==
94 (__hwasan::GetShadowOffset() >> kTaggableRegionCheckShift);
95#endif
96 return true;
97}
98
99static inline tag_t GetTagFromPointer(uptr p) {
100 return InTaggableRegion(addr: p) ? ((p >> kAddressTagShift) & kTagMask) : 0;
101}
102
103static inline uptr UntagAddr(uptr tagged_addr) {
104 return InTaggableRegion(addr: tagged_addr) ? (tagged_addr & ~kAddressTagMask)
105 : tagged_addr;
106}
107
108static inline void *UntagPtr(const void *tagged_ptr) {
109 return reinterpret_cast<void *>(
110 UntagAddr(tagged_addr: reinterpret_cast<uptr>(tagged_ptr)));
111}
112
113static inline uptr AddTagToPointer(uptr p, tag_t tag) {
114 return InTaggableRegion(addr: p) ? ((p & ~kAddressTagMask) |
115 ((uptr)(tag & kTagMask) << kAddressTagShift))
116 : p;
117}
118
119namespace __hwasan {
120
121extern int hwasan_inited;
122extern bool hwasan_init_is_running;
123extern int hwasan_report_count;
124
125bool InitShadow();
126void InitializeOsSupport();
127void InitThreads();
128void InitializeInterceptors();
129
130void HwasanAllocatorInit();
131void HwasanAllocatorLock();
132void HwasanAllocatorUnlock();
133
134void *hwasan_malloc(uptr size, StackTrace *stack);
135void *hwasan_calloc(uptr nmemb, uptr size, StackTrace *stack);
136void *hwasan_realloc(void *ptr, uptr size, StackTrace *stack);
137void *hwasan_reallocarray(void *ptr, uptr nmemb, uptr size, StackTrace *stack);
138void *hwasan_valloc(uptr size, StackTrace *stack);
139void *hwasan_pvalloc(uptr size, StackTrace *stack);
140void *hwasan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack);
141void *hwasan_memalign(uptr alignment, uptr size, StackTrace *stack);
142int hwasan_posix_memalign(void **memptr, uptr alignment, uptr size,
143 StackTrace *stack);
144void hwasan_free(void *ptr, StackTrace *stack);
145
146void InstallAtExitHandler();
147
148#define GET_MALLOC_STACK_TRACE \
149 UNINITIALIZED BufferedStackTrace stack; \
150 if (hwasan_inited) \
151 stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), \
152 nullptr, common_flags()->fast_unwind_on_malloc, \
153 common_flags()->malloc_context_size)
154
155#define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \
156 UNINITIALIZED BufferedStackTrace stack; \
157 if (hwasan_inited) \
158 stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal)
159
160void HwasanTSDInit();
161void HwasanTSDThreadInit();
162void HwasanAtExit();
163
164void HwasanOnDeadlySignal(int signo, void *info, void *context);
165
166void HwasanInstallAtForkHandler();
167
168void InstallAtExitCheckLeaks();
169
170void UpdateMemoryUsage();
171
172void AppendToErrorMessageBuffer(const char *buffer);
173
174void AndroidTestTlsSlot();
175
176// This is a compiler-generated struct that can be shared between hwasan
177// implementations.
178struct AccessInfo {
179 uptr addr;
180 uptr size;
181 bool is_store;
182 bool is_load;
183 bool recover;
184};
185
186// Given access info and frame information, unwind the stack and report the tag
187// mismatch.
188void HandleTagMismatch(AccessInfo ai, uptr pc, uptr frame, void *uc,
189 uptr *registers_frame = nullptr);
190
191// This dispatches to HandleTagMismatch but sets up the AccessInfo, program
192// counter, and frame pointer.
193void HwasanTagMismatch(uptr addr, uptr pc, uptr frame, uptr access_info,
194 uptr *registers_frame, size_t outsize);
195
196} // namespace __hwasan
197
198#if HWASAN_WITH_INTERCEPTORS
199// For both bionic and glibc __sigset_t is an unsigned long.
200typedef unsigned long __hw_sigset_t;
201// Setjmp and longjmp implementations are platform specific, and hence the
202// interception code is platform specific too.
203# if defined(__aarch64__)
204constexpr size_t kHwRegisterBufSize = 22;
205# elif defined(__x86_64__)
206constexpr size_t kHwRegisterBufSize = 8;
207# elif SANITIZER_RISCV64
208// saving PC, 12 int regs, sp, 12 fp regs
209# ifndef __riscv_float_abi_soft
210constexpr size_t kHwRegisterBufSize = 1 + 12 + 1 + 12;
211# else
212constexpr size_t kHwRegisterBufSize = 1 + 12 + 1;
213# endif
214# endif
215typedef unsigned long long __hw_register_buf[kHwRegisterBufSize];
216struct __hw_jmp_buf_struct {
217 // NOTE: The machine-dependent definition of `__sigsetjmp'
218 // assume that a `__hw_jmp_buf' begins with a `__hw_register_buf' and that
219 // `__mask_was_saved' follows it. Do not move these members or add others
220 // before it.
221 //
222 // We add a __magic field to our struct to catch cases where libc's setjmp
223 // populated the jmp_buf instead of our interceptor.
224 __hw_register_buf __jmpbuf; // Calling environment.
225 unsigned __mask_was_saved : 1; // Saved the signal mask?
226 unsigned __magic : 31; // Used to distinguish __hw_jmp_buf from jmp_buf.
227 __hw_sigset_t __saved_mask; // Saved signal mask.
228};
229typedef struct __hw_jmp_buf_struct __hw_jmp_buf[1];
230typedef struct __hw_jmp_buf_struct __hw_sigjmp_buf[1];
231constexpr unsigned kHwJmpBufMagic = 0x248ACE77;
232#endif // HWASAN_WITH_INTERCEPTORS
233
234#define ENSURE_HWASAN_INITED() \
235 do { \
236 CHECK(!hwasan_init_is_running); \
237 if (!hwasan_inited) { \
238 __hwasan_init(); \
239 } \
240 } while (0)
241
242#endif // HWASAN_H
243