hwasan_allocator.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //===-- hwasan_allocator.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 is a part of HWAddressSanitizer.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef HWASAN_ALLOCATOR_H
  13. #define HWASAN_ALLOCATOR_H
  14. #include "hwasan.h"
  15. #include "hwasan_interface_internal.h"
  16. #include "hwasan_mapping.h"
  17. #include "hwasan_poisoning.h"
  18. #include "lsan/lsan_common.h"
  19. #include "sanitizer_common/sanitizer_allocator.h"
  20. #include "sanitizer_common/sanitizer_allocator_checks.h"
  21. #include "sanitizer_common/sanitizer_allocator_interface.h"
  22. #include "sanitizer_common/sanitizer_allocator_report.h"
  23. #include "sanitizer_common/sanitizer_common.h"
  24. #include "sanitizer_common/sanitizer_ring_buffer.h"
  25. #if !defined(__aarch64__) && !defined(__x86_64__) && !(SANITIZER_RISCV64)
  26. # error Unsupported platform
  27. #endif
  28. namespace __hwasan {
  29. struct Metadata {
  30. private:
  31. atomic_uint64_t alloc_context_id;
  32. u32 requested_size_low;
  33. u16 requested_size_high;
  34. atomic_uint8_t chunk_state;
  35. u8 lsan_tag;
  36. public:
  37. inline void SetAllocated(u32 stack, u64 size);
  38. inline void SetUnallocated();
  39. inline bool IsAllocated() const;
  40. inline u64 GetRequestedSize() const;
  41. inline u32 GetAllocStackId() const;
  42. inline u32 GetAllocThreadId() const;
  43. inline void SetLsanTag(__lsan::ChunkTag tag);
  44. inline __lsan::ChunkTag GetLsanTag() const;
  45. };
  46. static_assert(sizeof(Metadata) == 16);
  47. struct HwasanMapUnmapCallback {
  48. void OnMap(uptr p, uptr size) const { UpdateMemoryUsage(); }
  49. void OnMapSecondary(uptr p, uptr size, uptr user_begin,
  50. uptr user_size) const {
  51. UpdateMemoryUsage();
  52. }
  53. void OnUnmap(uptr p, uptr size) const {
  54. // We are about to unmap a chunk of user memory.
  55. // It can return as user-requested mmap() or another thread stack.
  56. // Make it accessible with zero-tagged pointer.
  57. TagMemory(p, size, 0);
  58. }
  59. };
  60. static const uptr kMaxAllowedMallocSize = 1UL << 40; // 1T
  61. struct AP64 {
  62. static const uptr kSpaceBeg = ~0ULL;
  63. #if defined(HWASAN_ALIASING_MODE)
  64. static const uptr kSpaceSize = 1ULL << kAddressTagShift;
  65. typedef __sanitizer::DefaultSizeClassMap SizeClassMap;
  66. #elif SANITIZER_LINUX && !SANITIZER_ANDROID
  67. static const uptr kSpaceSize = 0x40000000000ULL; // 4T.
  68. typedef __sanitizer::DefaultSizeClassMap SizeClassMap;
  69. #else
  70. static const uptr kSpaceSize = 0x2000000000ULL; // 128G.
  71. typedef __sanitizer::VeryDenseSizeClassMap SizeClassMap;
  72. #endif
  73. static const uptr kMetadataSize = sizeof(Metadata);
  74. using AddressSpaceView = LocalAddressSpaceView;
  75. typedef HwasanMapUnmapCallback MapUnmapCallback;
  76. static const uptr kFlags = 0;
  77. };
  78. typedef SizeClassAllocator64<AP64> PrimaryAllocator;
  79. typedef CombinedAllocator<PrimaryAllocator> Allocator;
  80. typedef Allocator::AllocatorCache AllocatorCache;
  81. void AllocatorThreadStart(AllocatorCache *cache);
  82. void AllocatorThreadFinish(AllocatorCache *cache);
  83. class HwasanChunkView {
  84. public:
  85. HwasanChunkView() : block_(0), metadata_(nullptr) {}
  86. HwasanChunkView(uptr block, Metadata *metadata)
  87. : block_(block), metadata_(metadata) {}
  88. bool IsAllocated() const; // Checks if the memory is currently allocated
  89. uptr Beg() const; // First byte of user memory
  90. uptr End() const; // Last byte of user memory
  91. uptr UsedSize() const; // Size requested by the user
  92. uptr ActualSize() const; // Size allocated by the allocator.
  93. u32 GetAllocStackId() const;
  94. u32 GetAllocThreadId() const;
  95. bool FromSmallHeap() const;
  96. bool AddrIsInside(uptr addr) const;
  97. private:
  98. friend class __lsan::LsanMetadata;
  99. uptr block_;
  100. Metadata *const metadata_;
  101. };
  102. HwasanChunkView FindHeapChunkByAddress(uptr address);
  103. // Information about one (de)allocation that happened in the past.
  104. // These are recorded in a thread-local ring buffer.
  105. struct HeapAllocationRecord {
  106. uptr tagged_addr;
  107. u32 alloc_thread_id;
  108. u32 alloc_context_id;
  109. u32 free_context_id;
  110. u32 requested_size;
  111. };
  112. typedef RingBuffer<HeapAllocationRecord> HeapAllocationsRingBuffer;
  113. void GetAllocatorStats(AllocatorStatCounters s);
  114. } // namespace __hwasan
  115. #endif // HWASAN_ALLOCATOR_H