xref: /llvm-project/compiler-rt/lib/gwp_asan/common.cpp (revision 35b5499d7259ac3e5c648a711678290695703a87)
1a6258684SMitch Phillips //===-- common.cpp ----------------------------------------------*- C++ -*-===//
2a6258684SMitch Phillips //
3a6258684SMitch Phillips // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a6258684SMitch Phillips // See https://llvm.org/LICENSE.txt for license information.
5a6258684SMitch Phillips // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a6258684SMitch Phillips //
7a6258684SMitch Phillips //===----------------------------------------------------------------------===//
8a6258684SMitch Phillips 
9a6258684SMitch Phillips #include "gwp_asan/common.h"
10a6258684SMitch Phillips #include "gwp_asan/stack_trace_compressor.h"
11a6258684SMitch Phillips 
12a6258684SMitch Phillips #include <assert.h>
13a6258684SMitch Phillips 
14a6258684SMitch Phillips using AllocationMetadata = gwp_asan::AllocationMetadata;
15a6258684SMitch Phillips using Error = gwp_asan::Error;
16a6258684SMitch Phillips 
17a6258684SMitch Phillips namespace gwp_asan {
18a6258684SMitch Phillips 
ErrorToString(const Error & E)19a6258684SMitch Phillips const char *ErrorToString(const Error &E) {
20a6258684SMitch Phillips   switch (E) {
21a6258684SMitch Phillips   case Error::UNKNOWN:
22a6258684SMitch Phillips     return "Unknown";
23a6258684SMitch Phillips   case Error::USE_AFTER_FREE:
24a6258684SMitch Phillips     return "Use After Free";
25a6258684SMitch Phillips   case Error::DOUBLE_FREE:
26a6258684SMitch Phillips     return "Double Free";
27a6258684SMitch Phillips   case Error::INVALID_FREE:
28a6258684SMitch Phillips     return "Invalid (Wild) Free";
29a6258684SMitch Phillips   case Error::BUFFER_OVERFLOW:
30a6258684SMitch Phillips     return "Buffer Overflow";
31a6258684SMitch Phillips   case Error::BUFFER_UNDERFLOW:
32a6258684SMitch Phillips     return "Buffer Underflow";
33a6258684SMitch Phillips   }
34a6258684SMitch Phillips   __builtin_trap();
35a6258684SMitch Phillips }
36a6258684SMitch Phillips 
37d19af2f2SMitch Phillips constexpr size_t AllocationMetadata::kStackFrameStorageBytes;
38d19af2f2SMitch Phillips constexpr size_t AllocationMetadata::kMaxTraceLengthToCollect;
39d19af2f2SMitch Phillips 
RecordAllocation(uintptr_t AllocAddr,size_t AllocSize)40a6258684SMitch Phillips void AllocationMetadata::RecordAllocation(uintptr_t AllocAddr,
41a6258684SMitch Phillips                                           size_t AllocSize) {
42a6258684SMitch Phillips   Addr = AllocAddr;
433d8823b8SMitch Phillips   RequestedSize = AllocSize;
44a6258684SMitch Phillips   IsDeallocated = false;
45a6258684SMitch Phillips 
46a6258684SMitch Phillips   AllocationTrace.ThreadID = getThreadID();
47a6258684SMitch Phillips   DeallocationTrace.TraceSize = 0;
48a6258684SMitch Phillips   DeallocationTrace.ThreadID = kInvalidThreadID;
49a6258684SMitch Phillips }
50a6258684SMitch Phillips 
RecordDeallocation()51a6258684SMitch Phillips void AllocationMetadata::RecordDeallocation() {
52a6258684SMitch Phillips   IsDeallocated = true;
53a6258684SMitch Phillips   DeallocationTrace.ThreadID = getThreadID();
54a6258684SMitch Phillips }
55a6258684SMitch Phillips 
RecordBacktrace(options::Backtrace_t Backtrace)56a6258684SMitch Phillips void AllocationMetadata::CallSiteInfo::RecordBacktrace(
57a6258684SMitch Phillips     options::Backtrace_t Backtrace) {
58a6258684SMitch Phillips   TraceSize = 0;
59a6258684SMitch Phillips   if (!Backtrace)
60a6258684SMitch Phillips     return;
61a6258684SMitch Phillips 
62a6258684SMitch Phillips   uintptr_t UncompressedBuffer[kMaxTraceLengthToCollect];
63a6258684SMitch Phillips   size_t BacktraceLength =
64a6258684SMitch Phillips       Backtrace(UncompressedBuffer, kMaxTraceLengthToCollect);
65a4e8d897SMitch Phillips   // Backtrace() returns the number of available frames, which may be greater
66a4e8d897SMitch Phillips   // than the number of frames in the buffer. In this case, we need to only pack
67a4e8d897SMitch Phillips   // the number of frames that are in the buffer.
68a4e8d897SMitch Phillips   if (BacktraceLength > kMaxTraceLengthToCollect)
69a4e8d897SMitch Phillips     BacktraceLength = kMaxTraceLengthToCollect;
70a6258684SMitch Phillips   TraceSize =
71a6258684SMitch Phillips       compression::pack(UncompressedBuffer, BacktraceLength, CompressedTrace,
72a6258684SMitch Phillips                         AllocationMetadata::kStackFrameStorageBytes);
73a6258684SMitch Phillips }
74a6258684SMitch Phillips 
maximumAllocationSize() const75a6258684SMitch Phillips size_t AllocatorState::maximumAllocationSize() const { return PageSize; }
76a6258684SMitch Phillips 
slotToAddr(size_t N) const77a6258684SMitch Phillips uintptr_t AllocatorState::slotToAddr(size_t N) const {
78a6258684SMitch Phillips   return GuardedPagePool + (PageSize * (1 + N)) + (maximumAllocationSize() * N);
79a6258684SMitch Phillips }
80a6258684SMitch Phillips 
isGuardPage(uintptr_t Ptr) const81a6258684SMitch Phillips bool AllocatorState::isGuardPage(uintptr_t Ptr) const {
82a6258684SMitch Phillips   assert(pointerIsMine(reinterpret_cast<void *>(Ptr)));
83a6258684SMitch Phillips   size_t PageOffsetFromPoolStart = (Ptr - GuardedPagePool) / PageSize;
84a6258684SMitch Phillips   size_t PagesPerSlot = maximumAllocationSize() / PageSize;
85a6258684SMitch Phillips   return (PageOffsetFromPoolStart % (PagesPerSlot + 1)) == 0;
86a6258684SMitch Phillips }
87a6258684SMitch Phillips 
addrToSlot(const AllocatorState * State,uintptr_t Ptr)88a6258684SMitch Phillips static size_t addrToSlot(const AllocatorState *State, uintptr_t Ptr) {
89a6258684SMitch Phillips   size_t ByteOffsetFromPoolStart = Ptr - State->GuardedPagePool;
90a6258684SMitch Phillips   return ByteOffsetFromPoolStart /
91a6258684SMitch Phillips          (State->maximumAllocationSize() + State->PageSize);
92a6258684SMitch Phillips }
93a6258684SMitch Phillips 
getNearestSlot(uintptr_t Ptr) const94a6258684SMitch Phillips size_t AllocatorState::getNearestSlot(uintptr_t Ptr) const {
95a6258684SMitch Phillips   if (Ptr <= GuardedPagePool + PageSize)
96a6258684SMitch Phillips     return 0;
97a6258684SMitch Phillips   if (Ptr > GuardedPagePoolEnd - PageSize)
98a6258684SMitch Phillips     return MaxSimultaneousAllocations - 1;
99a6258684SMitch Phillips 
100a6258684SMitch Phillips   if (!isGuardPage(Ptr))
101a6258684SMitch Phillips     return addrToSlot(this, Ptr);
102a6258684SMitch Phillips 
103a6258684SMitch Phillips   if (Ptr % PageSize <= PageSize / 2)
104a6258684SMitch Phillips     return addrToSlot(this, Ptr - PageSize); // Round down.
105a6258684SMitch Phillips   return addrToSlot(this, Ptr + PageSize);   // Round up.
106a6258684SMitch Phillips }
107a6258684SMitch Phillips 
internallyDetectedErrorFaultAddress() const108*35b5499dSMitch Phillips uintptr_t AllocatorState::internallyDetectedErrorFaultAddress() const {
109*35b5499dSMitch Phillips   return GuardedPagePoolEnd - 0x10;
110*35b5499dSMitch Phillips }
111*35b5499dSMitch Phillips 
112a6258684SMitch Phillips } // namespace gwp_asan
113