1 //===-- common.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 contains code that is common between the crash handler and the 10 // GuardedPoolAllocator. 11 12 #ifndef GWP_ASAN_COMMON_H_ 13 #define GWP_ASAN_COMMON_H_ 14 15 #include "gwp_asan/definitions.h" 16 #include "gwp_asan/options.h" 17 18 #include <stddef.h> 19 #include <stdint.h> 20 21 namespace gwp_asan { 22 enum class Error { 23 UNKNOWN, 24 USE_AFTER_FREE, 25 DOUBLE_FREE, 26 INVALID_FREE, 27 BUFFER_OVERFLOW, 28 BUFFER_UNDERFLOW 29 }; 30 31 const char *ErrorToString(const Error &E); 32 33 static constexpr uint64_t kInvalidThreadID = UINT64_MAX; 34 // Get the current thread ID, or kInvalidThreadID if failure. Note: This 35 // implementation is platform-specific. 36 uint64_t getThreadID(); 37 38 // This struct contains all the metadata recorded about a single allocation made 39 // by GWP-ASan. If `AllocationMetadata.Addr` is zero, the metadata is non-valid. 40 struct AllocationMetadata { 41 // The number of bytes used to store a compressed stack frame. On 64-bit 42 // platforms, assuming a compression ratio of 50%, this should allow us to 43 // store ~64 frames per trace. 44 static constexpr size_t kStackFrameStorageBytes = 256; 45 46 // Maximum number of stack frames to collect on allocation/deallocation. The 47 // actual number of collected frames may be less than this as the stack 48 // frames are compressed into a fixed memory range. 49 static constexpr size_t kMaxTraceLengthToCollect = 128; 50 51 // Records the given allocation metadata into this struct. 52 void RecordAllocation(uintptr_t Addr, size_t RequestedSize); 53 // Record that this allocation is now deallocated. 54 void RecordDeallocation(); 55 56 struct CallSiteInfo { 57 // Record the current backtrace to this callsite. 58 void RecordBacktrace(options::Backtrace_t Backtrace); 59 60 // The compressed backtrace to the allocation/deallocation. 61 uint8_t CompressedTrace[kStackFrameStorageBytes]; 62 // The thread ID for this trace, or kInvalidThreadID if not available. 63 uint64_t ThreadID = kInvalidThreadID; 64 // The size of the compressed trace (in bytes). Zero indicates that no 65 // trace was collected. 66 size_t TraceSize = 0; 67 }; 68 69 // The address of this allocation. If zero, the rest of this struct isn't 70 // valid, as the allocation has never occurred. 71 uintptr_t Addr = 0; 72 // Represents the actual size of the allocation. 73 size_t RequestedSize = 0; 74 75 CallSiteInfo AllocationTrace; 76 CallSiteInfo DeallocationTrace; 77 78 // Whether this allocation has been deallocated yet. 79 bool IsDeallocated = false; 80 }; 81 82 // 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 84 // set of information required for understanding a GWP-ASan crash. 85 struct AllocatorState { 86 constexpr AllocatorState() {} 87 88 // Returns whether the provided pointer is a current sampled allocation that 89 // is owned by this pool. 90 GWP_ASAN_ALWAYS_INLINE bool pointerIsMine(const void *Ptr) const { 91 uintptr_t P = reinterpret_cast<uintptr_t>(Ptr); 92 return P < GuardedPagePoolEnd && GuardedPagePool <= P; 93 } 94 95 // Returns the address of the N-th guarded slot. 96 uintptr_t slotToAddr(size_t N) const; 97 98 // Returns the largest allocation that is supported by this pool. 99 size_t maximumAllocationSize() const; 100 101 // Gets the nearest slot to the provided address. 102 size_t getNearestSlot(uintptr_t Ptr) const; 103 104 // Returns whether the provided pointer is a guard page or not. The pointer 105 // must be within memory owned by this pool, else the result is undefined. 106 bool isGuardPage(uintptr_t Ptr) const; 107 108 // The number of guarded slots that this pool holds. 109 size_t MaxSimultaneousAllocations = 0; 110 111 // Pointer to the pool of guarded slots. Note that this points to the start of 112 // the pool (which is a guard page), not a pointer to the first guarded page. 113 uintptr_t GuardedPagePool = 0; 114 uintptr_t GuardedPagePoolEnd = 0; 115 116 // Cached page size for this system in bytes. 117 size_t PageSize = 0; 118 119 // The type and address of an internally-detected failure. For INVALID_FREE 120 // and DOUBLE_FREE, these errors are detected in GWP-ASan, which will set 121 // these values and terminate the process. 122 Error FailureType = Error::UNKNOWN; 123 uintptr_t FailureAddress = 0; 124 }; 125 126 } // namespace gwp_asan 127 #endif // GWP_ASAN_COMMON_H_ 128