comparison compiler-rt/lib/gwp_asan/common.h @ 236:c4bab56944e8 llvm-original

LLVM 16
author kono
date Wed, 09 Nov 2022 17:45:10 +0900
parents 79ff65ed7e25
children 1f2b6ac9f198
comparison
equal deleted inserted replaced
232:70dce7da266c 236:c4bab56944e8
17 17
18 #include <stddef.h> 18 #include <stddef.h>
19 #include <stdint.h> 19 #include <stdint.h>
20 20
21 namespace gwp_asan { 21 namespace gwp_asan {
22 enum class Error { 22
23 // Magic header that resides in the AllocatorState so that GWP-ASan bugreports
24 // can be understood by tools at different versions. Out-of-process crash
25 // handlers, like crashpad on Fuchsia, take the raw contents of the
26 // AllocationMetatada array and the AllocatorState, and shove them into the
27 // minidump. Online unpacking of these structs needs to know from which version
28 // of GWP-ASan it's extracting the information, as the structures are not
29 // stable.
30 struct AllocatorVersionMagic {
31 // The values are copied into the structure at runtime, during
32 // `GuardedPoolAllocator::init()` so that GWP-ASan remains completely in the
33 // `.bss` segment.
34 static constexpr uint8_t kAllocatorVersionMagic[4] = {'A', 'S', 'A', 'N'};
35 uint8_t Magic[4] = {};
36 // Update the version number when the AllocatorState or AllocationMetadata
37 // change.
38 static constexpr uint16_t kAllocatorVersion = 1;
39 uint16_t Version = 0;
40 uint16_t Reserved = 0;
41 };
42
43 enum class Error : uint8_t {
23 UNKNOWN, 44 UNKNOWN,
24 USE_AFTER_FREE, 45 USE_AFTER_FREE,
25 DOUBLE_FREE, 46 DOUBLE_FREE,
26 INVALID_FREE, 47 INVALID_FREE,
27 BUFFER_OVERFLOW, 48 BUFFER_OVERFLOW,
82 // This holds the state that's shared between the GWP-ASan allocator and the 103 // This holds the state that's shared between the GWP-ASan allocator and the
83 // crash handler. This, in conjunction with the Metadata array, forms the entire 104 // crash handler. This, in conjunction with the Metadata array, forms the entire
84 // set of information required for understanding a GWP-ASan crash. 105 // set of information required for understanding a GWP-ASan crash.
85 struct AllocatorState { 106 struct AllocatorState {
86 constexpr AllocatorState() {} 107 constexpr AllocatorState() {}
108 AllocatorVersionMagic VersionMagic{};
87 109
88 // Returns whether the provided pointer is a current sampled allocation that 110 // Returns whether the provided pointer is a current sampled allocation that
89 // is owned by this pool. 111 // is owned by this pool.
90 GWP_ASAN_ALWAYS_INLINE bool pointerIsMine(const void *Ptr) const { 112 GWP_ASAN_ALWAYS_INLINE bool pointerIsMine(const void *Ptr) const {
91 uintptr_t P = reinterpret_cast<uintptr_t>(Ptr); 113 uintptr_t P = reinterpret_cast<uintptr_t>(Ptr);
121 // these values and terminate the process. 143 // these values and terminate the process.
122 Error FailureType = Error::UNKNOWN; 144 Error FailureType = Error::UNKNOWN;
123 uintptr_t FailureAddress = 0; 145 uintptr_t FailureAddress = 0;
124 }; 146 };
125 147
148 // Below are various compile-time checks that the layout of the internal
149 // GWP-ASan structures are undisturbed. If they are disturbed, the version magic
150 // number needs to be increased by one, and the asserts need to be updated.
151 // Out-of-process crash handlers, like breakpad/crashpad, may copy the internal
152 // GWP-ASan structures into a minidump for offline reconstruction of the crash.
153 // In order to accomplish this, the offline reconstructor needs to know the
154 // version of GWP-ASan internal structures that it's unpacking (along with the
155 // architecture-specific layout info, which is left as an exercise to the crash
156 // handler).
157 static_assert(offsetof(AllocatorState, VersionMagic) == 0, "");
158 static_assert(sizeof(AllocatorVersionMagic) == 8, "");
159 #if defined(__x86_64__)
160 static_assert(sizeof(AllocatorState) == 56, "");
161 static_assert(offsetof(AllocatorState, FailureAddress) == 48, "");
162 static_assert(sizeof(AllocationMetadata) == 568, "");
163 static_assert(offsetof(AllocationMetadata, IsDeallocated) == 560, "");
164 #elif defined(__aarch64__)
165 static_assert(sizeof(AllocatorState) == 56, "");
166 static_assert(offsetof(AllocatorState, FailureAddress) == 48, "");
167 static_assert(sizeof(AllocationMetadata) == 568, "");
168 static_assert(offsetof(AllocationMetadata, IsDeallocated) == 560, "");
169 #elif defined(__i386__)
170 static_assert(sizeof(AllocatorState) == 32, "");
171 static_assert(offsetof(AllocatorState, FailureAddress) == 28, "");
172 static_assert(sizeof(AllocationMetadata) == 548, "");
173 static_assert(offsetof(AllocationMetadata, IsDeallocated) == 544, "");
174 #elif defined(__arm__)
175 static_assert(sizeof(AllocatorState) == 32, "");
176 static_assert(offsetof(AllocatorState, FailureAddress) == 28, "");
177 static_assert(sizeof(AllocationMetadata) == 560, "");
178 static_assert(offsetof(AllocationMetadata, IsDeallocated) == 552, "");
179 #endif // defined($ARCHITECTURE)
180
126 } // namespace gwp_asan 181 } // namespace gwp_asan
127 #endif // GWP_ASAN_COMMON_H_ 182 #endif // GWP_ASAN_COMMON_H_