1 //===-- hwasan_allocator.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of HWAddressSanitizer.
12 //===----------------------------------------------------------------------===//
14 #ifndef HWASAN_ALLOCATOR_H
15 #define HWASAN_ALLOCATOR_H
17 #include "interception/interception.h"
18 #include "sanitizer_common/sanitizer_allocator.h"
19 #include "sanitizer_common/sanitizer_allocator_checks.h"
20 #include "sanitizer_common/sanitizer_allocator_interface.h"
21 #include "sanitizer_common/sanitizer_allocator_report.h"
22 #include "sanitizer_common/sanitizer_common.h"
23 #include "sanitizer_common/sanitizer_ring_buffer.h"
24 #include "hwasan_poisoning.h"
26 #if !defined(__aarch64__) && !defined(__x86_64__)
27 #error Unsupported platform
30 #if HWASAN_WITH_INTERCEPTORS
31 DECLARE_REAL(void *, realloc, void *ptr, uptr size)
32 DECLARE_REAL(void, free, void *ptr)
38 u32 requested_size : 31; // sizes are < 2G.
39 u32 right_aligned : 1;
43 struct HwasanMapUnmapCallback {
44 void OnMap(uptr p, uptr size) const { UpdateMemoryUsage(); }
45 void OnUnmap(uptr p, uptr size) const {
46 // We are about to unmap a chunk of user memory.
47 // It can return as user-requested mmap() or another thread stack.
48 // Make it accessible with zero-tagged pointer.
49 TagMemory(p, size, 0);
53 static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G
56 static const uptr kSpaceBeg = ~0ULL;
57 static const uptr kSpaceSize = 0x2000000000ULL;
58 static const uptr kMetadataSize = sizeof(Metadata);
59 typedef __sanitizer::VeryDenseSizeClassMap SizeClassMap;
60 using AddressSpaceView = LocalAddressSpaceView;
61 typedef HwasanMapUnmapCallback MapUnmapCallback;
62 static const uptr kFlags = 0;
64 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
65 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
66 typedef LargeMmapAllocator<HwasanMapUnmapCallback> SecondaryAllocator;
67 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
68 SecondaryAllocator> Allocator;
70 void AllocatorSwallowThreadLocalCache(AllocatorCache *cache);
72 class HwasanChunkView {
74 HwasanChunkView() : block_(0), metadata_(nullptr) {}
75 HwasanChunkView(uptr block, Metadata *metadata)
76 : block_(block), metadata_(metadata) {}
77 bool IsAllocated() const; // Checks if the memory is currently allocated
78 uptr Beg() const; // First byte of user memory
79 uptr End() const; // Last byte of user memory
80 uptr UsedSize() const; // Size requested by the user
81 uptr ActualSize() const; // Size allocated by the allocator.
82 u32 GetAllocStackId() const;
83 bool FromSmallHeap() const;
86 Metadata *const metadata_;
89 HwasanChunkView FindHeapChunkByAddress(uptr address);
91 // Information about one (de)allocation that happened in the past.
92 // These are recorded in a thread-local ring buffer.
93 // TODO: this is currently 24 bytes (20 bytes + alignment).
94 // Compress it to 16 bytes or extend it to be more useful.
95 struct HeapAllocationRecord {
102 typedef RingBuffer<HeapAllocationRecord> HeapAllocationsRingBuffer;
104 void GetAllocatorStats(AllocatorStatCounters s);
106 } // namespace __hwasan
108 #endif // HWASAN_ALLOCATOR_H