hwasan_allocator.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 void SetLsanTag(__lsan::ChunkTag tag);
  43. inline __lsan::ChunkTag GetLsanTag() const;
  44. };
  45. static_assert(sizeof(Metadata) == 16);
  46. struct HwasanMapUnmapCallback {
  47. void OnMap(uptr p, uptr size) const { UpdateMemoryUsage(); }
  48. void OnUnmap(uptr p, uptr size) const {
  49. // We are about to unmap a chunk of user memory.
  50. // It can return as user-requested mmap() or another thread stack.
  51. // Make it accessible with zero-tagged pointer.
  52. TagMemory(p, size, 0);
  53. }
  54. };
  55. static const uptr kMaxAllowedMallocSize = 1UL << 40; // 1T
  56. struct AP64 {
  57. static const uptr kSpaceBeg = ~0ULL;
  58. #if defined(HWASAN_ALIASING_MODE)
  59. static const uptr kSpaceSize = 1ULL << kAddressTagShift;
  60. #else
  61. static const uptr kSpaceSize = 0x2000000000ULL;
  62. #endif
  63. static const uptr kMetadataSize = sizeof(Metadata);
  64. typedef __sanitizer::VeryDenseSizeClassMap SizeClassMap;
  65. using AddressSpaceView = LocalAddressSpaceView;
  66. typedef HwasanMapUnmapCallback MapUnmapCallback;
  67. static const uptr kFlags = 0;
  68. };
  69. typedef SizeClassAllocator64<AP64> PrimaryAllocator;
  70. typedef CombinedAllocator<PrimaryAllocator> Allocator;
  71. typedef Allocator::AllocatorCache AllocatorCache;
  72. void AllocatorSwallowThreadLocalCache(AllocatorCache *cache);
  73. class HwasanChunkView {
  74. public:
  75. HwasanChunkView() : block_(0), metadata_(nullptr) {}
  76. HwasanChunkView(uptr block, Metadata *metadata)
  77. : block_(block), metadata_(metadata) {}
  78. bool IsAllocated() const; // Checks if the memory is currently allocated
  79. uptr Beg() const; // First byte of user memory
  80. uptr End() const; // Last byte of user memory
  81. uptr UsedSize() const; // Size requested by the user
  82. uptr ActualSize() const; // Size allocated by the allocator.
  83. u32 GetAllocStackId() const;
  84. bool FromSmallHeap() const;
  85. bool AddrIsInside(uptr addr) const;
  86. private:
  87. friend class __lsan::LsanMetadata;
  88. uptr block_;
  89. Metadata *const metadata_;
  90. };
  91. HwasanChunkView FindHeapChunkByAddress(uptr address);
  92. // Information about one (de)allocation that happened in the past.
  93. // These are recorded in a thread-local ring buffer.
  94. // TODO: this is currently 24 bytes (20 bytes + alignment).
  95. // Compress it to 16 bytes or extend it to be more useful.
  96. struct HeapAllocationRecord {
  97. uptr tagged_addr;
  98. u32 alloc_context_id;
  99. u32 free_context_id;
  100. u32 requested_size;
  101. };
  102. typedef RingBuffer<HeapAllocationRecord> HeapAllocationsRingBuffer;
  103. void GetAllocatorStats(AllocatorStatCounters s);
  104. inline bool InTaggableRegion(uptr addr) {
  105. #if defined(HWASAN_ALIASING_MODE)
  106. // Aliases are mapped next to shadow so that the upper bits match the shadow
  107. // base.
  108. return (addr >> kTaggableRegionCheckShift) ==
  109. (GetShadowOffset() >> kTaggableRegionCheckShift);
  110. #endif
  111. return true;
  112. }
  113. } // namespace __hwasan
  114. #endif // HWASAN_ALLOCATOR_H