annotate compiler-rt/lib/gwp_asan/common.h @ 209:dd44ba33042e

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