common.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===-- common.cpp ----------------------------------------------*- 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. #include "gwp_asan/common.h"
  9. #include "gwp_asan/stack_trace_compressor.h"
  10. #include <assert.h>
  11. using AllocationMetadata = gwp_asan::AllocationMetadata;
  12. using Error = gwp_asan::Error;
  13. namespace gwp_asan {
  14. const char *ErrorToString(const Error &E) {
  15. switch (E) {
  16. case Error::UNKNOWN:
  17. return "Unknown";
  18. case Error::USE_AFTER_FREE:
  19. return "Use After Free";
  20. case Error::DOUBLE_FREE:
  21. return "Double Free";
  22. case Error::INVALID_FREE:
  23. return "Invalid (Wild) Free";
  24. case Error::BUFFER_OVERFLOW:
  25. return "Buffer Overflow";
  26. case Error::BUFFER_UNDERFLOW:
  27. return "Buffer Underflow";
  28. }
  29. __builtin_trap();
  30. }
  31. constexpr size_t AllocationMetadata::kStackFrameStorageBytes;
  32. constexpr size_t AllocationMetadata::kMaxTraceLengthToCollect;
  33. void AllocationMetadata::RecordAllocation(uintptr_t AllocAddr,
  34. size_t AllocSize) {
  35. Addr = AllocAddr;
  36. RequestedSize = AllocSize;
  37. IsDeallocated = false;
  38. AllocationTrace.ThreadID = getThreadID();
  39. DeallocationTrace.TraceSize = 0;
  40. DeallocationTrace.ThreadID = kInvalidThreadID;
  41. }
  42. void AllocationMetadata::RecordDeallocation() {
  43. IsDeallocated = true;
  44. DeallocationTrace.ThreadID = getThreadID();
  45. }
  46. void AllocationMetadata::CallSiteInfo::RecordBacktrace(
  47. options::Backtrace_t Backtrace) {
  48. TraceSize = 0;
  49. if (!Backtrace)
  50. return;
  51. uintptr_t UncompressedBuffer[kMaxTraceLengthToCollect];
  52. size_t BacktraceLength =
  53. Backtrace(UncompressedBuffer, kMaxTraceLengthToCollect);
  54. // Backtrace() returns the number of available frames, which may be greater
  55. // than the number of frames in the buffer. In this case, we need to only pack
  56. // the number of frames that are in the buffer.
  57. if (BacktraceLength > kMaxTraceLengthToCollect)
  58. BacktraceLength = kMaxTraceLengthToCollect;
  59. TraceSize =
  60. compression::pack(UncompressedBuffer, BacktraceLength, CompressedTrace,
  61. AllocationMetadata::kStackFrameStorageBytes);
  62. }
  63. size_t AllocatorState::maximumAllocationSize() const { return PageSize; }
  64. uintptr_t AllocatorState::slotToAddr(size_t N) const {
  65. return GuardedPagePool + (PageSize * (1 + N)) + (maximumAllocationSize() * N);
  66. }
  67. bool AllocatorState::isGuardPage(uintptr_t Ptr) const {
  68. assert(pointerIsMine(reinterpret_cast<void *>(Ptr)));
  69. size_t PageOffsetFromPoolStart = (Ptr - GuardedPagePool) / PageSize;
  70. size_t PagesPerSlot = maximumAllocationSize() / PageSize;
  71. return (PageOffsetFromPoolStart % (PagesPerSlot + 1)) == 0;
  72. }
  73. static size_t addrToSlot(const AllocatorState *State, uintptr_t Ptr) {
  74. size_t ByteOffsetFromPoolStart = Ptr - State->GuardedPagePool;
  75. return ByteOffsetFromPoolStart /
  76. (State->maximumAllocationSize() + State->PageSize);
  77. }
  78. size_t AllocatorState::getNearestSlot(uintptr_t Ptr) const {
  79. if (Ptr <= GuardedPagePool + PageSize)
  80. return 0;
  81. if (Ptr > GuardedPagePoolEnd - PageSize)
  82. return MaxSimultaneousAllocations - 1;
  83. if (!isGuardPage(Ptr))
  84. return addrToSlot(this, Ptr);
  85. if (Ptr % PageSize <= PageSize / 2)
  86. return addrToSlot(this, Ptr - PageSize); // Round down.
  87. return addrToSlot(this, Ptr + PageSize); // Round up.
  88. }
  89. } // namespace gwp_asan