common.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // This file contains code that is common between the crash handler and the
  9. // GuardedPoolAllocator.
  10. #ifndef GWP_ASAN_COMMON_H_
  11. #define GWP_ASAN_COMMON_H_
  12. #include "gwp_asan/definitions.h"
  13. #include "gwp_asan/options.h"
  14. #include <stddef.h>
  15. #include <stdint.h>
  16. namespace gwp_asan {
  17. // Magic header that resides in the AllocatorState so that GWP-ASan bugreports
  18. // can be understood by tools at different versions. Out-of-process crash
  19. // handlers, like crashpad on Fuchsia, take the raw contents of the
  20. // AllocationMetatada array and the AllocatorState, and shove them into the
  21. // minidump. Online unpacking of these structs needs to know from which version
  22. // of GWP-ASan it's extracting the information, as the structures are not
  23. // stable.
  24. struct AllocatorVersionMagic {
  25. // The values are copied into the structure at runtime, during
  26. // `GuardedPoolAllocator::init()` so that GWP-ASan remains completely in the
  27. // `.bss` segment.
  28. static constexpr uint8_t kAllocatorVersionMagic[4] = {'A', 'S', 'A', 'N'};
  29. uint8_t Magic[4] = {};
  30. // Update the version number when the AllocatorState or AllocationMetadata
  31. // change.
  32. static constexpr uint16_t kAllocatorVersion = 2;
  33. uint16_t Version = 0;
  34. uint16_t Reserved = 0;
  35. };
  36. enum class Error : uint8_t {
  37. UNKNOWN,
  38. USE_AFTER_FREE,
  39. DOUBLE_FREE,
  40. INVALID_FREE,
  41. BUFFER_OVERFLOW,
  42. BUFFER_UNDERFLOW
  43. };
  44. const char *ErrorToString(const Error &E);
  45. static constexpr uint64_t kInvalidThreadID = UINT64_MAX;
  46. // Get the current thread ID, or kInvalidThreadID if failure. Note: This
  47. // implementation is platform-specific.
  48. uint64_t getThreadID();
  49. // This struct contains all the metadata recorded about a single allocation made
  50. // by GWP-ASan. If `AllocationMetadata.Addr` is zero, the metadata is non-valid.
  51. struct AllocationMetadata {
  52. // The number of bytes used to store a compressed stack frame. On 64-bit
  53. // platforms, assuming a compression ratio of 50%, this should allow us to
  54. // store ~64 frames per trace.
  55. static constexpr size_t kStackFrameStorageBytes = 256;
  56. // Maximum number of stack frames to collect on allocation/deallocation. The
  57. // actual number of collected frames may be less than this as the stack
  58. // frames are compressed into a fixed memory range.
  59. static constexpr size_t kMaxTraceLengthToCollect = 128;
  60. // Records the given allocation metadata into this struct.
  61. void RecordAllocation(uintptr_t Addr, size_t RequestedSize);
  62. // Record that this allocation is now deallocated.
  63. void RecordDeallocation();
  64. struct CallSiteInfo {
  65. // Record the current backtrace to this callsite.
  66. void RecordBacktrace(options::Backtrace_t Backtrace);
  67. // The compressed backtrace to the allocation/deallocation.
  68. uint8_t CompressedTrace[kStackFrameStorageBytes];
  69. // The thread ID for this trace, or kInvalidThreadID if not available.
  70. uint64_t ThreadID = kInvalidThreadID;
  71. // The size of the compressed trace (in bytes). Zero indicates that no
  72. // trace was collected.
  73. size_t TraceSize = 0;
  74. };
  75. // The address of this allocation. If zero, the rest of this struct isn't
  76. // valid, as the allocation has never occurred.
  77. uintptr_t Addr = 0;
  78. // Represents the actual size of the allocation.
  79. size_t RequestedSize = 0;
  80. CallSiteInfo AllocationTrace;
  81. CallSiteInfo DeallocationTrace;
  82. // Whether this allocation has been deallocated yet.
  83. bool IsDeallocated = false;
  84. // In recoverable mode, whether this allocation has had a crash associated
  85. // with it. This has certain side effects, like meaning this allocation will
  86. // permanently occupy a slot, and won't ever have another crash reported from
  87. // it.
  88. bool HasCrashed = false;
  89. };
  90. // This holds the state that's shared between the GWP-ASan allocator and the
  91. // crash handler. This, in conjunction with the Metadata array, forms the entire
  92. // set of information required for understanding a GWP-ASan crash.
  93. struct AllocatorState {
  94. constexpr AllocatorState() {}
  95. AllocatorVersionMagic VersionMagic{};
  96. // Returns whether the provided pointer is a current sampled allocation that
  97. // is owned by this pool.
  98. GWP_ASAN_ALWAYS_INLINE bool pointerIsMine(const void *Ptr) const {
  99. uintptr_t P = reinterpret_cast<uintptr_t>(Ptr);
  100. return P < GuardedPagePoolEnd && GuardedPagePool <= P;
  101. }
  102. // Returns the address of the N-th guarded slot.
  103. uintptr_t slotToAddr(size_t N) const;
  104. // Returns the largest allocation that is supported by this pool.
  105. size_t maximumAllocationSize() const;
  106. // Gets the nearest slot to the provided address.
  107. size_t getNearestSlot(uintptr_t Ptr) const;
  108. // Returns whether the provided pointer is a guard page or not. The pointer
  109. // must be within memory owned by this pool, else the result is undefined.
  110. bool isGuardPage(uintptr_t Ptr) const;
  111. // Returns the address that's used by __gwp_asan_get_internal_crash_address()
  112. // and GPA::raiseInternallyDetectedError() to communicate that the SEGV in
  113. // question comes from an internally-detected error.
  114. uintptr_t internallyDetectedErrorFaultAddress() const;
  115. // The number of guarded slots that this pool holds.
  116. size_t MaxSimultaneousAllocations = 0;
  117. // Pointer to the pool of guarded slots. Note that this points to the start of
  118. // the pool (which is a guard page), not a pointer to the first guarded page.
  119. uintptr_t GuardedPagePool = 0;
  120. uintptr_t GuardedPagePoolEnd = 0;
  121. // Cached page size for this system in bytes.
  122. size_t PageSize = 0;
  123. // The type and address of an internally-detected failure. For INVALID_FREE
  124. // and DOUBLE_FREE, these errors are detected in GWP-ASan, which will set
  125. // these values and terminate the process.
  126. Error FailureType = Error::UNKNOWN;
  127. uintptr_t FailureAddress = 0;
  128. };
  129. // Below are various compile-time checks that the layout of the internal
  130. // GWP-ASan structures are undisturbed. If they are disturbed, the version magic
  131. // number needs to be increased by one, and the asserts need to be updated.
  132. // Out-of-process crash handlers, like breakpad/crashpad, may copy the internal
  133. // GWP-ASan structures into a minidump for offline reconstruction of the crash.
  134. // In order to accomplish this, the offline reconstructor needs to know the
  135. // version of GWP-ASan internal structures that it's unpacking (along with the
  136. // architecture-specific layout info, which is left as an exercise to the crash
  137. // handler).
  138. static_assert(offsetof(AllocatorState, VersionMagic) == 0, "");
  139. static_assert(sizeof(AllocatorVersionMagic) == 8, "");
  140. #if defined(__x86_64__)
  141. static_assert(sizeof(AllocatorState) == 56, "");
  142. static_assert(offsetof(AllocatorState, FailureAddress) == 48, "");
  143. static_assert(sizeof(AllocationMetadata) == 568, "");
  144. static_assert(offsetof(AllocationMetadata, IsDeallocated) == 560, "");
  145. #elif defined(__aarch64__)
  146. static_assert(sizeof(AllocatorState) == 56, "");
  147. static_assert(offsetof(AllocatorState, FailureAddress) == 48, "");
  148. static_assert(sizeof(AllocationMetadata) == 568, "");
  149. static_assert(offsetof(AllocationMetadata, IsDeallocated) == 560, "");
  150. #elif defined(__i386__)
  151. static_assert(sizeof(AllocatorState) == 32, "");
  152. static_assert(offsetof(AllocatorState, FailureAddress) == 28, "");
  153. static_assert(sizeof(AllocationMetadata) == 548, "");
  154. static_assert(offsetof(AllocationMetadata, IsDeallocated) == 544, "");
  155. #elif defined(__arm__)
  156. static_assert(sizeof(AllocatorState) == 32, "");
  157. static_assert(offsetof(AllocatorState, FailureAddress) == 28, "");
  158. static_assert(sizeof(AllocationMetadata) == 560, "");
  159. static_assert(offsetof(AllocationMetadata, IsDeallocated) == 552, "");
  160. #endif // defined($ARCHITECTURE)
  161. } // namespace gwp_asan
  162. #endif // GWP_ASAN_COMMON_H_