1 //===-- hwasan_allocator.h --------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of HWAddressSanitizer.
11 //===----------------------------------------------------------------------===//
13 #ifndef HWASAN_ALLOCATOR_H
14 #define HWASAN_ALLOCATOR_H
16 #include "interception/interception.h"
17 #include "sanitizer_common/sanitizer_allocator.h"
18 #include "sanitizer_common/sanitizer_allocator_checks.h"
19 #include "sanitizer_common/sanitizer_allocator_interface.h"
20 #include "sanitizer_common/sanitizer_allocator_report.h"
21 #include "sanitizer_common/sanitizer_common.h"
22 #include "sanitizer_common/sanitizer_ring_buffer.h"
23 #include "hwasan_poisoning.h"
25 #if !defined(__aarch64__) && !defined(__x86_64__)
26 #error Unsupported platform
29 #if HWASAN_WITH_INTERCEPTORS
30 DECLARE_REAL(void *, realloc, void *ptr, uptr size)
31 DECLARE_REAL(void, free, void *ptr)
37 u32 requested_size : 31; // sizes are < 2G.
38 u32 right_aligned : 1;
42 struct HwasanMapUnmapCallback {
43 void OnMap(uptr p, uptr size) const { UpdateMemoryUsage(); }
44 void OnUnmap(uptr p, uptr size) const {
45 // We are about to unmap a chunk of user memory.
46 // It can return as user-requested mmap() or another thread stack.
47 // Make it accessible with zero-tagged pointer.
48 TagMemory(p, size, 0);
52 static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G
55 static const uptr kSpaceBeg = ~0ULL;
56 static const uptr kSpaceSize = 0x2000000000ULL;
57 static const uptr kMetadataSize = sizeof(Metadata);
58 typedef __sanitizer::VeryDenseSizeClassMap SizeClassMap;
59 using AddressSpaceView = LocalAddressSpaceView;
60 typedef HwasanMapUnmapCallback MapUnmapCallback;
61 static const uptr kFlags = 0;
63 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
64 typedef CombinedAllocator<PrimaryAllocator> Allocator;
65 typedef Allocator::AllocatorCache AllocatorCache;
67 void AllocatorSwallowThreadLocalCache(AllocatorCache *cache);
69 class HwasanChunkView {
71 HwasanChunkView() : block_(0), metadata_(nullptr) {}
72 HwasanChunkView(uptr block, Metadata *metadata)
73 : block_(block), metadata_(metadata) {}
74 bool IsAllocated() const; // Checks if the memory is currently allocated
75 uptr Beg() const; // First byte of user memory
76 uptr End() const; // Last byte of user memory
77 uptr UsedSize() const; // Size requested by the user
78 uptr ActualSize() const; // Size allocated by the allocator.
79 u32 GetAllocStackId() const;
80 bool FromSmallHeap() const;
83 Metadata *const metadata_;
86 HwasanChunkView FindHeapChunkByAddress(uptr address);
88 // Information about one (de)allocation that happened in the past.
89 // These are recorded in a thread-local ring buffer.
90 // TODO: this is currently 24 bytes (20 bytes + alignment).
91 // Compress it to 16 bytes or extend it to be more useful.
92 struct HeapAllocationRecord {
99 typedef RingBuffer<HeapAllocationRecord> HeapAllocationsRingBuffer;
101 void GetAllocatorStats(AllocatorStatCounters s);
103 } // namespace __hwasan
105 #endif // HWASAN_ALLOCATOR_H