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
17 #include "hwasan_interface_internal.h"
18 #include "hwasan_mapping.h"
19 #include "hwasan_poisoning.h"
20 #include "lsan/lsan_common.h"
21 #include "sanitizer_common/sanitizer_allocator.h"
22 #include "sanitizer_common/sanitizer_allocator_checks.h"
23 #include "sanitizer_common/sanitizer_allocator_interface.h"
24 #include "sanitizer_common/sanitizer_allocator_report.h"
25 #include "sanitizer_common/sanitizer_common.h"
26 #include "sanitizer_common/sanitizer_ring_buffer.h"
28 #if !defined(__aarch64__) && !defined(__x86_64__) && !(SANITIZER_RISCV64)
29 # error Unsupported platform
36 atomic_uint64_t alloc_context_id
;
37 u32 requested_size_low
;
38 u16 requested_size_high
;
39 atomic_uint8_t chunk_state
;
43 inline void SetAllocated(u32 stack
, u64 size
);
44 inline void SetUnallocated();
46 inline bool IsAllocated() const;
47 inline u64
GetRequestedSize() const;
48 inline u32
GetAllocStackId() const;
49 inline u32
GetAllocThreadId() const;
50 inline void SetLsanTag(__lsan::ChunkTag tag
);
51 inline __lsan::ChunkTag
GetLsanTag() const;
53 static_assert(sizeof(Metadata
) == 16);
55 struct HwasanMapUnmapCallback
{
56 void OnMap(uptr p
, uptr size
) const { UpdateMemoryUsage(); }
57 void OnUnmap(uptr p
, uptr size
) const {
58 // We are about to unmap a chunk of user memory.
59 // It can return as user-requested mmap() or another thread stack.
60 // Make it accessible with zero-tagged pointer.
61 TagMemory(p
, size
, 0);
65 static const uptr kMaxAllowedMallocSize
= 1UL << 40; // 1T
68 static const uptr kSpaceBeg
= ~0ULL;
70 #if defined(HWASAN_ALIASING_MODE)
71 static const uptr kSpaceSize
= 1ULL << kAddressTagShift
;
72 typedef __sanitizer::DefaultSizeClassMap SizeClassMap
;
73 #elif SANITIZER_LINUX && !SANITIZER_ANDROID
74 static const uptr kSpaceSize
= 0x40000000000ULL
; // 4T.
75 typedef __sanitizer::DefaultSizeClassMap SizeClassMap
;
77 static const uptr kSpaceSize
= 0x2000000000ULL
; // 128G.
78 typedef __sanitizer::VeryDenseSizeClassMap SizeClassMap
;
81 static const uptr kMetadataSize
= sizeof(Metadata
);
82 using AddressSpaceView
= LocalAddressSpaceView
;
83 typedef HwasanMapUnmapCallback MapUnmapCallback
;
84 static const uptr kFlags
= 0;
87 typedef SizeClassAllocator64
<AP64
> PrimaryAllocator
;
88 typedef CombinedAllocator
<PrimaryAllocator
> Allocator
;
89 typedef Allocator::AllocatorCache AllocatorCache
;
91 void AllocatorSwallowThreadLocalCache(AllocatorCache
*cache
);
93 class HwasanChunkView
{
95 HwasanChunkView() : block_(0), metadata_(nullptr) {}
96 HwasanChunkView(uptr block
, Metadata
*metadata
)
97 : block_(block
), metadata_(metadata
) {}
98 bool IsAllocated() const; // Checks if the memory is currently allocated
99 uptr
Beg() const; // First byte of user memory
100 uptr
End() const; // Last byte of user memory
101 uptr
UsedSize() const; // Size requested by the user
102 uptr
ActualSize() const; // Size allocated by the allocator.
103 u32
GetAllocStackId() const;
104 u32
GetAllocThreadId() const;
105 bool FromSmallHeap() const;
106 bool AddrIsInside(uptr addr
) const;
109 friend class __lsan::LsanMetadata
;
111 Metadata
*const metadata_
;
114 HwasanChunkView
FindHeapChunkByAddress(uptr address
);
116 // Information about one (de)allocation that happened in the past.
117 // These are recorded in a thread-local ring buffer.
118 struct HeapAllocationRecord
{
121 u32 alloc_context_id
;
126 typedef RingBuffer
<HeapAllocationRecord
> HeapAllocationsRingBuffer
;
128 void GetAllocatorStats(AllocatorStatCounters s
);
130 } // namespace __hwasan
132 #endif // HWASAN_ALLOCATOR_H