xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/gwp_asan/common.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
15ffd83dbSDimitry Andric //===-- common.h ------------------------------------------------*- C++ -*-===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric 
95ffd83dbSDimitry Andric // This file contains code that is common between the crash handler and the
105ffd83dbSDimitry Andric // GuardedPoolAllocator.
115ffd83dbSDimitry Andric 
125ffd83dbSDimitry Andric #ifndef GWP_ASAN_COMMON_H_
135ffd83dbSDimitry Andric #define GWP_ASAN_COMMON_H_
145ffd83dbSDimitry Andric 
155ffd83dbSDimitry Andric #include "gwp_asan/definitions.h"
165ffd83dbSDimitry Andric #include "gwp_asan/options.h"
175ffd83dbSDimitry Andric 
185ffd83dbSDimitry Andric #include <stddef.h>
195ffd83dbSDimitry Andric #include <stdint.h>
205ffd83dbSDimitry Andric 
215ffd83dbSDimitry Andric namespace gwp_asan {
22349cc55cSDimitry Andric 
23349cc55cSDimitry Andric // Magic header that resides in the AllocatorState so that GWP-ASan bugreports
24349cc55cSDimitry Andric // can be understood by tools at different versions. Out-of-process crash
25349cc55cSDimitry Andric // handlers, like crashpad on Fuchsia, take the raw contents of the
26349cc55cSDimitry Andric // AllocationMetatada array and the AllocatorState, and shove them into the
27349cc55cSDimitry Andric // minidump. Online unpacking of these structs needs to know from which version
28349cc55cSDimitry Andric // of GWP-ASan it's extracting the information, as the structures are not
29349cc55cSDimitry Andric // stable.
30349cc55cSDimitry Andric struct AllocatorVersionMagic {
31349cc55cSDimitry Andric   // The values are copied into the structure at runtime, during
32349cc55cSDimitry Andric   // `GuardedPoolAllocator::init()` so that GWP-ASan remains completely in the
33349cc55cSDimitry Andric   // `.bss` segment.
34349cc55cSDimitry Andric   static constexpr uint8_t kAllocatorVersionMagic[4] = {'A', 'S', 'A', 'N'};
35349cc55cSDimitry Andric   uint8_t Magic[4] = {};
36349cc55cSDimitry Andric   // Update the version number when the AllocatorState or AllocationMetadata
37349cc55cSDimitry Andric   // change.
38*bdd1243dSDimitry Andric   static constexpr uint16_t kAllocatorVersion = 2;
39349cc55cSDimitry Andric   uint16_t Version = 0;
40349cc55cSDimitry Andric   uint16_t Reserved = 0;
41349cc55cSDimitry Andric };
42349cc55cSDimitry Andric 
43349cc55cSDimitry Andric enum class Error : uint8_t {
445ffd83dbSDimitry Andric   UNKNOWN,
455ffd83dbSDimitry Andric   USE_AFTER_FREE,
465ffd83dbSDimitry Andric   DOUBLE_FREE,
475ffd83dbSDimitry Andric   INVALID_FREE,
485ffd83dbSDimitry Andric   BUFFER_OVERFLOW,
495ffd83dbSDimitry Andric   BUFFER_UNDERFLOW
505ffd83dbSDimitry Andric };
515ffd83dbSDimitry Andric 
525ffd83dbSDimitry Andric const char *ErrorToString(const Error &E);
535ffd83dbSDimitry Andric 
545ffd83dbSDimitry Andric static constexpr uint64_t kInvalidThreadID = UINT64_MAX;
555ffd83dbSDimitry Andric // Get the current thread ID, or kInvalidThreadID if failure. Note: This
565ffd83dbSDimitry Andric // implementation is platform-specific.
575ffd83dbSDimitry Andric uint64_t getThreadID();
585ffd83dbSDimitry Andric 
595ffd83dbSDimitry Andric // This struct contains all the metadata recorded about a single allocation made
605ffd83dbSDimitry Andric // by GWP-ASan. If `AllocationMetadata.Addr` is zero, the metadata is non-valid.
615ffd83dbSDimitry Andric struct AllocationMetadata {
625ffd83dbSDimitry Andric   // The number of bytes used to store a compressed stack frame. On 64-bit
635ffd83dbSDimitry Andric   // platforms, assuming a compression ratio of 50%, this should allow us to
645ffd83dbSDimitry Andric   // store ~64 frames per trace.
655ffd83dbSDimitry Andric   static constexpr size_t kStackFrameStorageBytes = 256;
665ffd83dbSDimitry Andric 
675ffd83dbSDimitry Andric   // Maximum number of stack frames to collect on allocation/deallocation. The
685ffd83dbSDimitry Andric   // actual number of collected frames may be less than this as the stack
695ffd83dbSDimitry Andric   // frames are compressed into a fixed memory range.
705ffd83dbSDimitry Andric   static constexpr size_t kMaxTraceLengthToCollect = 128;
715ffd83dbSDimitry Andric 
725ffd83dbSDimitry Andric   // Records the given allocation metadata into this struct.
73fe6060f1SDimitry Andric   void RecordAllocation(uintptr_t Addr, size_t RequestedSize);
745ffd83dbSDimitry Andric   // Record that this allocation is now deallocated.
755ffd83dbSDimitry Andric   void RecordDeallocation();
765ffd83dbSDimitry Andric 
775ffd83dbSDimitry Andric   struct CallSiteInfo {
785ffd83dbSDimitry Andric     // Record the current backtrace to this callsite.
795ffd83dbSDimitry Andric     void RecordBacktrace(options::Backtrace_t Backtrace);
805ffd83dbSDimitry Andric 
815ffd83dbSDimitry Andric     // The compressed backtrace to the allocation/deallocation.
825ffd83dbSDimitry Andric     uint8_t CompressedTrace[kStackFrameStorageBytes];
835ffd83dbSDimitry Andric     // The thread ID for this trace, or kInvalidThreadID if not available.
845ffd83dbSDimitry Andric     uint64_t ThreadID = kInvalidThreadID;
855ffd83dbSDimitry Andric     // The size of the compressed trace (in bytes). Zero indicates that no
865ffd83dbSDimitry Andric     // trace was collected.
875ffd83dbSDimitry Andric     size_t TraceSize = 0;
885ffd83dbSDimitry Andric   };
895ffd83dbSDimitry Andric 
905ffd83dbSDimitry Andric   // The address of this allocation. If zero, the rest of this struct isn't
915ffd83dbSDimitry Andric   // valid, as the allocation has never occurred.
925ffd83dbSDimitry Andric   uintptr_t Addr = 0;
935ffd83dbSDimitry Andric   // Represents the actual size of the allocation.
94fe6060f1SDimitry Andric   size_t RequestedSize = 0;
955ffd83dbSDimitry Andric 
965ffd83dbSDimitry Andric   CallSiteInfo AllocationTrace;
975ffd83dbSDimitry Andric   CallSiteInfo DeallocationTrace;
985ffd83dbSDimitry Andric 
995ffd83dbSDimitry Andric   // Whether this allocation has been deallocated yet.
1005ffd83dbSDimitry Andric   bool IsDeallocated = false;
101*bdd1243dSDimitry Andric 
102*bdd1243dSDimitry Andric   // In recoverable mode, whether this allocation has had a crash associated
103*bdd1243dSDimitry Andric   // with it. This has certain side effects, like meaning this allocation will
104*bdd1243dSDimitry Andric   // permanently occupy a slot, and won't ever have another crash reported from
105*bdd1243dSDimitry Andric   // it.
106*bdd1243dSDimitry Andric   bool HasCrashed = false;
1075ffd83dbSDimitry Andric };
1085ffd83dbSDimitry Andric 
1095ffd83dbSDimitry Andric // This holds the state that's shared between the GWP-ASan allocator and the
1105ffd83dbSDimitry Andric // crash handler. This, in conjunction with the Metadata array, forms the entire
1115ffd83dbSDimitry Andric // set of information required for understanding a GWP-ASan crash.
1125ffd83dbSDimitry Andric struct AllocatorState {
AllocatorStateAllocatorState113fe6060f1SDimitry Andric   constexpr AllocatorState() {}
114349cc55cSDimitry Andric   AllocatorVersionMagic VersionMagic{};
115fe6060f1SDimitry Andric 
1165ffd83dbSDimitry Andric   // Returns whether the provided pointer is a current sampled allocation that
1175ffd83dbSDimitry Andric   // is owned by this pool.
pointerIsMineAllocatorState1185ffd83dbSDimitry Andric   GWP_ASAN_ALWAYS_INLINE bool pointerIsMine(const void *Ptr) const {
1195ffd83dbSDimitry Andric     uintptr_t P = reinterpret_cast<uintptr_t>(Ptr);
1205ffd83dbSDimitry Andric     return P < GuardedPagePoolEnd && GuardedPagePool <= P;
1215ffd83dbSDimitry Andric   }
1225ffd83dbSDimitry Andric 
1235ffd83dbSDimitry Andric   // Returns the address of the N-th guarded slot.
1245ffd83dbSDimitry Andric   uintptr_t slotToAddr(size_t N) const;
1255ffd83dbSDimitry Andric 
1265ffd83dbSDimitry Andric   // Returns the largest allocation that is supported by this pool.
1275ffd83dbSDimitry Andric   size_t maximumAllocationSize() const;
1285ffd83dbSDimitry Andric 
1295ffd83dbSDimitry Andric   // Gets the nearest slot to the provided address.
1305ffd83dbSDimitry Andric   size_t getNearestSlot(uintptr_t Ptr) const;
1315ffd83dbSDimitry Andric 
1325ffd83dbSDimitry Andric   // Returns whether the provided pointer is a guard page or not. The pointer
1335ffd83dbSDimitry Andric   // must be within memory owned by this pool, else the result is undefined.
1345ffd83dbSDimitry Andric   bool isGuardPage(uintptr_t Ptr) const;
1355ffd83dbSDimitry Andric 
136*bdd1243dSDimitry Andric   // Returns the address that's used by __gwp_asan_get_internal_crash_address()
137*bdd1243dSDimitry Andric   // and GPA::raiseInternallyDetectedError() to communicate that the SEGV in
138*bdd1243dSDimitry Andric   // question comes from an internally-detected error.
139*bdd1243dSDimitry Andric   uintptr_t internallyDetectedErrorFaultAddress() const;
140*bdd1243dSDimitry Andric 
1415ffd83dbSDimitry Andric   // The number of guarded slots that this pool holds.
1425ffd83dbSDimitry Andric   size_t MaxSimultaneousAllocations = 0;
1435ffd83dbSDimitry Andric 
1445ffd83dbSDimitry Andric   // Pointer to the pool of guarded slots. Note that this points to the start of
1455ffd83dbSDimitry Andric   // the pool (which is a guard page), not a pointer to the first guarded page.
1465ffd83dbSDimitry Andric   uintptr_t GuardedPagePool = 0;
1475ffd83dbSDimitry Andric   uintptr_t GuardedPagePoolEnd = 0;
1485ffd83dbSDimitry Andric 
1495ffd83dbSDimitry Andric   // Cached page size for this system in bytes.
1505ffd83dbSDimitry Andric   size_t PageSize = 0;
1515ffd83dbSDimitry Andric 
1525ffd83dbSDimitry Andric   // The type and address of an internally-detected failure. For INVALID_FREE
1535ffd83dbSDimitry Andric   // and DOUBLE_FREE, these errors are detected in GWP-ASan, which will set
1545ffd83dbSDimitry Andric   // these values and terminate the process.
1555ffd83dbSDimitry Andric   Error FailureType = Error::UNKNOWN;
1565ffd83dbSDimitry Andric   uintptr_t FailureAddress = 0;
1575ffd83dbSDimitry Andric };
1585ffd83dbSDimitry Andric 
159349cc55cSDimitry Andric // Below are various compile-time checks that the layout of the internal
160349cc55cSDimitry Andric // GWP-ASan structures are undisturbed. If they are disturbed, the version magic
161349cc55cSDimitry Andric // number needs to be increased by one, and the asserts need to be updated.
162349cc55cSDimitry Andric // Out-of-process crash handlers, like breakpad/crashpad, may copy the internal
163349cc55cSDimitry Andric // GWP-ASan structures into a minidump for offline reconstruction of the crash.
164349cc55cSDimitry Andric // In order to accomplish this, the offline reconstructor needs to know the
165349cc55cSDimitry Andric // version of GWP-ASan internal structures that it's unpacking (along with the
166349cc55cSDimitry Andric // architecture-specific layout info, which is left as an exercise to the crash
167349cc55cSDimitry Andric // handler).
168349cc55cSDimitry Andric static_assert(offsetof(AllocatorState, VersionMagic) == 0, "");
169349cc55cSDimitry Andric static_assert(sizeof(AllocatorVersionMagic) == 8, "");
170349cc55cSDimitry Andric #if defined(__x86_64__)
171349cc55cSDimitry Andric static_assert(sizeof(AllocatorState) == 56, "");
172349cc55cSDimitry Andric static_assert(offsetof(AllocatorState, FailureAddress) == 48, "");
173349cc55cSDimitry Andric static_assert(sizeof(AllocationMetadata) == 568, "");
174349cc55cSDimitry Andric static_assert(offsetof(AllocationMetadata, IsDeallocated) == 560, "");
175349cc55cSDimitry Andric #elif defined(__aarch64__)
176349cc55cSDimitry Andric static_assert(sizeof(AllocatorState) == 56, "");
177349cc55cSDimitry Andric static_assert(offsetof(AllocatorState, FailureAddress) == 48, "");
178349cc55cSDimitry Andric static_assert(sizeof(AllocationMetadata) == 568, "");
179349cc55cSDimitry Andric static_assert(offsetof(AllocationMetadata, IsDeallocated) == 560, "");
180349cc55cSDimitry Andric #elif defined(__i386__)
181349cc55cSDimitry Andric static_assert(sizeof(AllocatorState) == 32, "");
182349cc55cSDimitry Andric static_assert(offsetof(AllocatorState, FailureAddress) == 28, "");
183349cc55cSDimitry Andric static_assert(sizeof(AllocationMetadata) == 548, "");
184349cc55cSDimitry Andric static_assert(offsetof(AllocationMetadata, IsDeallocated) == 544, "");
185349cc55cSDimitry Andric #elif defined(__arm__)
186349cc55cSDimitry Andric static_assert(sizeof(AllocatorState) == 32, "");
187349cc55cSDimitry Andric static_assert(offsetof(AllocatorState, FailureAddress) == 28, "");
188349cc55cSDimitry Andric static_assert(sizeof(AllocationMetadata) == 560, "");
189349cc55cSDimitry Andric static_assert(offsetof(AllocationMetadata, IsDeallocated) == 552, "");
190349cc55cSDimitry Andric #endif // defined($ARCHITECTURE)
191349cc55cSDimitry Andric 
1925ffd83dbSDimitry Andric } // namespace gwp_asan
1935ffd83dbSDimitry Andric #endif // GWP_ASAN_COMMON_H_
194