1 //===-- common.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 contains code that is common between the crash handler and the
10 // GuardedPoolAllocator.
12 #ifndef GWP_ASAN_COMMON_H_
13 #define GWP_ASAN_COMMON_H_
15 #include "gwp_asan/definitions.h"
16 #include "gwp_asan/options.h"
31 const char *ErrorToString(const Error &E);
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();
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;
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;
51 // Records the given allocation metadata into this struct.
52 void RecordAllocation(uintptr_t Addr, size_t Size);
53 // Record that this allocation is now deallocated.
54 void RecordDeallocation();
57 // Record the current backtrace to this callsite.
58 void RecordBacktrace(options::Backtrace_t Backtrace);
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.
69 // The address of this allocation. If zero, the rest of this struct isn't
70 // valid, as the allocation has never occurred.
72 // Represents the actual size of the allocation.
75 CallSiteInfo AllocationTrace;
76 CallSiteInfo DeallocationTrace;
78 // Whether this allocation has been deallocated yet.
79 bool IsDeallocated = false;
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 // Returns whether the provided pointer is a current sampled allocation that
87 // is owned by this pool.
88 GWP_ASAN_ALWAYS_INLINE bool pointerIsMine(const void *Ptr) const {
89 uintptr_t P = reinterpret_cast<uintptr_t>(Ptr);
90 return P < GuardedPagePoolEnd && GuardedPagePool <= P;
93 // Returns the address of the N-th guarded slot.
94 uintptr_t slotToAddr(size_t N) const;
96 // Returns the largest allocation that is supported by this pool.
97 size_t maximumAllocationSize() const;
99 // Gets the nearest slot to the provided address.
100 size_t getNearestSlot(uintptr_t Ptr) const;
102 // Returns whether the provided pointer is a guard page or not. The pointer
103 // must be within memory owned by this pool, else the result is undefined.
104 bool isGuardPage(uintptr_t Ptr) const;
106 // The number of guarded slots that this pool holds.
107 size_t MaxSimultaneousAllocations = 0;
109 // Pointer to the pool of guarded slots. Note that this points to the start of
110 // the pool (which is a guard page), not a pointer to the first guarded page.
111 uintptr_t GuardedPagePool = 0;
112 uintptr_t GuardedPagePoolEnd = 0;
114 // Cached page size for this system in bytes.
117 // The type and address of an internally-detected failure. For INVALID_FREE
118 // and DOUBLE_FREE, these errors are detected in GWP-ASan, which will set
119 // these values and terminate the process.
120 Error FailureType = Error::UNKNOWN;
121 uintptr_t FailureAddress = 0;
124 } // namespace gwp_asan
125 #endif // GWP_ASAN_COMMON_H_