For obj-c stage-final re-use the checksum from the previous stage
[official-gcc.git] / libsanitizer / hwasan / hwasan.h
blob8515df559f30acb1c16933aae126b950021f45ab
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.
11 // Private Hwasan header.
12 //===----------------------------------------------------------------------===//
14 #ifndef HWASAN_H
15 #define HWASAN_H
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
27 #endif
29 #ifndef HWASAN_WITH_INTERCEPTORS
30 #define HWASAN_WITH_INTERCEPTORS 0
31 #endif
33 #ifndef HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
34 #define HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE HWASAN_WITH_INTERCEPTORS
35 #endif
37 typedef u8 tag_t;
39 #if defined(__x86_64__)
40 // Tags are done in middle bits using userspace aliasing.
41 constexpr unsigned kAddressTagShift = 39;
42 constexpr unsigned kTagBits = 3;
44 // The alias region is placed next to the shadow so the upper bits of all
45 // taggable addresses matches the upper bits of the shadow base. This shift
46 // value determines which upper bits must match. It has a floor of 44 since the
47 // shadow is always 8TB.
48 // TODO(morehouse): In alias mode we can shrink the shadow and use a
49 // simpler/faster shadow calculation.
50 constexpr unsigned kTaggableRegionCheckShift =
51 __sanitizer::Max(kAddressTagShift + kTagBits + 1U, 44U);
52 #else
53 // TBI (Top Byte Ignore) feature of AArch64: bits [63:56] are ignored in address
54 // translation and can be used to store a tag.
55 constexpr unsigned kAddressTagShift = 56;
56 constexpr unsigned kTagBits = 8;
57 #endif // defined(__x86_64__)
59 // Mask for extracting tag bits from the lower 8 bits.
60 constexpr uptr kTagMask = (1UL << kTagBits) - 1;
62 // Mask for extracting tag bits from full pointers.
63 constexpr uptr kAddressTagMask = kTagMask << kAddressTagShift;
65 // Minimal alignment of the shadow base address. Determines the space available
66 // for threads and stack histories. This is an ABI constant.
67 const unsigned kShadowBaseAlignment = 32;
69 const unsigned kRecordAddrBaseTagShift = 3;
70 const unsigned kRecordFPShift = 48;
71 const unsigned kRecordFPLShift = 4;
72 const unsigned kRecordFPModulus = 1 << (64 - kRecordFPShift + kRecordFPLShift);
74 static inline tag_t GetTagFromPointer(uptr p) {
75 return (p >> kAddressTagShift) & kTagMask;
78 static inline uptr UntagAddr(uptr tagged_addr) {
79 return tagged_addr & ~kAddressTagMask;
82 static inline void *UntagPtr(const void *tagged_ptr) {
83 return reinterpret_cast<void *>(
84 UntagAddr(reinterpret_cast<uptr>(tagged_ptr)));
87 static inline uptr AddTagToPointer(uptr p, tag_t tag) {
88 return (p & ~kAddressTagMask) | ((uptr)tag << kAddressTagShift);
91 namespace __hwasan {
93 extern int hwasan_inited;
94 extern bool hwasan_init_is_running;
95 extern int hwasan_report_count;
97 bool InitShadow();
98 void InitPrctl();
99 void InitThreads();
100 void InitializeInterceptors();
102 void HwasanAllocatorInit();
104 void *hwasan_malloc(uptr size, StackTrace *stack);
105 void *hwasan_calloc(uptr nmemb, uptr size, StackTrace *stack);
106 void *hwasan_realloc(void *ptr, uptr size, StackTrace *stack);
107 void *hwasan_reallocarray(void *ptr, uptr nmemb, uptr size, StackTrace *stack);
108 void *hwasan_valloc(uptr size, StackTrace *stack);
109 void *hwasan_pvalloc(uptr size, StackTrace *stack);
110 void *hwasan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack);
111 void *hwasan_memalign(uptr alignment, uptr size, StackTrace *stack);
112 int hwasan_posix_memalign(void **memptr, uptr alignment, uptr size,
113 StackTrace *stack);
114 void hwasan_free(void *ptr, StackTrace *stack);
116 void InstallAtExitHandler();
118 #define GET_MALLOC_STACK_TRACE \
119 BufferedStackTrace stack; \
120 if (hwasan_inited) \
121 stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), \
122 nullptr, common_flags()->fast_unwind_on_malloc, \
123 common_flags()->malloc_context_size)
125 #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \
126 BufferedStackTrace stack; \
127 if (hwasan_inited) \
128 stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal)
130 void HwasanTSDInit();
131 void HwasanTSDThreadInit();
133 void HwasanOnDeadlySignal(int signo, void *info, void *context);
135 void UpdateMemoryUsage();
137 void AppendToErrorMessageBuffer(const char *buffer);
139 void AndroidTestTlsSlot();
141 } // namespace __hwasan
143 #define HWASAN_MALLOC_HOOK(ptr, size) \
144 do { \
145 if (&__sanitizer_malloc_hook) { \
146 __sanitizer_malloc_hook(ptr, size); \
148 RunMallocHooks(ptr, size); \
149 } while (false)
150 #define HWASAN_FREE_HOOK(ptr) \
151 do { \
152 if (&__sanitizer_free_hook) { \
153 __sanitizer_free_hook(ptr); \
155 RunFreeHooks(ptr); \
156 } while (false)
158 #if HWASAN_WITH_INTERCEPTORS && defined(__aarch64__)
159 // For both bionic and glibc __sigset_t is an unsigned long.
160 typedef unsigned long __hw_sigset_t;
161 // Setjmp and longjmp implementations are platform specific, and hence the
162 // interception code is platform specific too. As yet we've only implemented
163 // the interception for AArch64.
164 typedef unsigned long long __hw_register_buf[22];
165 struct __hw_jmp_buf_struct {
166 // NOTE: The machine-dependent definition of `__sigsetjmp'
167 // assume that a `__hw_jmp_buf' begins with a `__hw_register_buf' and that
168 // `__mask_was_saved' follows it. Do not move these members or add others
169 // before it.
170 __hw_register_buf __jmpbuf; // Calling environment.
171 int __mask_was_saved; // Saved the signal mask?
172 __hw_sigset_t __saved_mask; // Saved signal mask.
174 typedef struct __hw_jmp_buf_struct __hw_jmp_buf[1];
175 typedef struct __hw_jmp_buf_struct __hw_sigjmp_buf[1];
176 #endif // HWASAN_WITH_INTERCEPTORS && __aarch64__
178 #endif // HWASAN_H