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
21namespace gwp_asan {
22enum class Error {
23  UNKNOWN,
24  USE_AFTER_FREE,
25  DOUBLE_FREE,
26  INVALID_FREE,
27  BUFFER_OVERFLOW,
28  BUFFER_UNDERFLOW
29};
30
31const char *ErrorToString(const Error &E);
32
33static constexpr uint64_t kInvalidThreadID = UINT64_MAX;
34// Get the current thread ID, or kInvalidThreadID if failure. Note: This
35// implementation is platform-specific.
36uint64_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.
40struct 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 Size);
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 Size = 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.
85struct 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;
91  }
92
93  // Returns the address of the N-th guarded slot.
94  uintptr_t slotToAddr(size_t N) const;
95
96  // Returns the largest allocation that is supported by this pool.
97  size_t maximumAllocationSize() const;
98
99  // Gets the nearest slot to the provided address.
100  size_t getNearestSlot(uintptr_t Ptr) const;
101
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;
105
106  // The number of guarded slots that this pool holds.
107  size_t MaxSimultaneousAllocations = 0;
108
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;
113
114  // Cached page size for this system in bytes.
115  size_t PageSize = 0;
116
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;
122};
123
124} // namespace gwp_asan
125#endif // GWP_ASAN_COMMON_H_
126