1 //===-- asan_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 AddressSanitizer, an address sanity checker.
11 // ASan-private header for asan_allocator.cpp.
12 //===----------------------------------------------------------------------===//
14 #ifndef ASAN_ALLOCATOR_H
15 #define ASAN_ALLOCATOR_H
17 #include "asan_flags.h"
18 #include "asan_interceptors.h"
19 #include "asan_internal.h"
20 #include "sanitizer_common/sanitizer_allocator.h"
21 #include "sanitizer_common/sanitizer_list.h"
22 #include "sanitizer_common/sanitizer_platform.h"
27 FROM_MALLOC
= 1, // Memory block came from malloc, calloc, realloc, etc.
28 FROM_NEW
= 2, // Memory block came from operator new.
29 FROM_NEW_BR
= 3 // Memory block came from operator new [ ]
34 struct AllocatorOptions
{
35 u32 quarantine_size_mb
;
36 u32 thread_local_quarantine_size_kb
;
40 u8 alloc_dealloc_mismatch
;
41 s32 release_to_os_interval_ms
;
43 void SetFrom(const Flags
*f
, const CommonFlags
*cf
);
44 void CopyTo(Flags
*f
, CommonFlags
*cf
);
47 void InitializeAllocator(const AllocatorOptions
&options
);
48 void ReInitializeAllocator(const AllocatorOptions
&options
);
49 void GetAllocatorOptions(AllocatorOptions
*options
);
53 explicit AsanChunkView(AsanChunk
*chunk
) : chunk_(chunk
) {}
54 bool IsValid() const; // Checks if AsanChunkView points to a valid
55 // allocated or quarantined chunk.
56 bool IsAllocated() const; // Checks if the memory is currently allocated.
57 bool IsQuarantined() const; // Checks if the memory is currently quarantined.
58 uptr
Beg() const; // First byte of user memory.
59 uptr
End() const; // Last byte of user memory.
60 uptr
UsedSize() const; // Size requested by the user.
61 u32
UserRequestedAlignment() const; // Originally requested alignment.
62 uptr
AllocTid() const;
64 bool Eq(const AsanChunkView
&c
) const { return chunk_
== c
.chunk_
; }
65 u32
GetAllocStackId() const;
66 u32
GetFreeStackId() const;
67 AllocType
GetAllocType() const;
68 bool AddrIsInside(uptr addr
, uptr access_size
, sptr
*offset
) const {
69 if (addr
>= Beg() && (addr
+ access_size
) <= End()) {
70 *offset
= addr
- Beg();
75 bool AddrIsAtLeft(uptr addr
, uptr access_size
, sptr
*offset
) const {
78 *offset
= Beg() - addr
;
83 bool AddrIsAtRight(uptr addr
, uptr access_size
, sptr
*offset
) const {
84 if (addr
+ access_size
> End()) {
85 *offset
= addr
- End();
92 AsanChunk
*const chunk_
;
95 AsanChunkView
FindHeapChunkByAddress(uptr address
);
96 AsanChunkView
FindHeapChunkByAllocBeg(uptr address
);
98 // List of AsanChunks with total size.
99 class AsanChunkFifoList
: public IntrusiveList
<AsanChunk
> {
101 explicit AsanChunkFifoList(LinkerInitialized
) { }
102 AsanChunkFifoList() { clear(); }
103 void Push(AsanChunk
*n
);
104 void PushList(AsanChunkFifoList
*q
);
106 uptr
size() { return size_
; }
108 IntrusiveList
<AsanChunk
>::clear();
115 struct AsanMapUnmapCallback
{
116 void OnMap(uptr p
, uptr size
) const;
117 void OnUnmap(uptr p
, uptr size
) const;
120 #if SANITIZER_CAN_USE_ALLOCATOR64
121 # if SANITIZER_FUCHSIA
122 const uptr kAllocatorSpace
= ~(uptr
)0;
123 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
124 typedef DefaultSizeClassMap SizeClassMap
;
125 # elif defined(__powerpc64__)
126 const uptr kAllocatorSpace
= ~(uptr
)0;
127 const uptr kAllocatorSize
= 0x20000000000ULL
; // 2T.
128 typedef DefaultSizeClassMap SizeClassMap
;
129 # elif defined(__aarch64__) && SANITIZER_ANDROID
130 // Android needs to support 39, 42 and 48 bit VMA.
131 const uptr kAllocatorSpace
= ~(uptr
)0;
132 const uptr kAllocatorSize
= 0x2000000000ULL
; // 128G.
133 typedef VeryCompactSizeClassMap SizeClassMap
;
134 #elif SANITIZER_RISCV64
135 const uptr kAllocatorSpace
= ~(uptr
)0;
136 const uptr kAllocatorSize
= 0x2000000000ULL
; // 128G.
137 typedef VeryDenseSizeClassMap SizeClassMap
;
138 #elif defined(__sparc__)
139 const uptr kAllocatorSpace
= ~(uptr
)0;
140 const uptr kAllocatorSize
= 0x20000000000ULL
; // 2T.
141 typedef DefaultSizeClassMap SizeClassMap
;
142 # elif SANITIZER_WINDOWS
143 const uptr kAllocatorSpace
= ~(uptr
)0;
144 const uptr kAllocatorSize
= 0x8000000000ULL
; // 500G
145 typedef DefaultSizeClassMap SizeClassMap
;
146 # elif SANITIZER_APPLE
147 const uptr kAllocatorSpace
= 0x600000000000ULL
;
148 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
149 typedef DefaultSizeClassMap SizeClassMap
;
151 const uptr kAllocatorSpace
= 0x500000000000ULL
;
152 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
153 typedef DefaultSizeClassMap SizeClassMap
;
155 template <typename AddressSpaceViewTy
>
156 struct AP64
{ // Allocator64 parameters. Deliberately using a short name.
157 static const uptr kSpaceBeg
= kAllocatorSpace
;
158 static const uptr kSpaceSize
= kAllocatorSize
;
159 static const uptr kMetadataSize
= 0;
160 typedef __asan::SizeClassMap SizeClassMap
;
161 typedef AsanMapUnmapCallback MapUnmapCallback
;
162 static const uptr kFlags
= 0;
163 using AddressSpaceView
= AddressSpaceViewTy
;
166 template <typename AddressSpaceView
>
167 using PrimaryAllocatorASVT
= SizeClassAllocator64
<AP64
<AddressSpaceView
>>;
168 using PrimaryAllocator
= PrimaryAllocatorASVT
<LocalAddressSpaceView
>;
169 #else // Fallback to SizeClassAllocator32.
170 typedef CompactSizeClassMap SizeClassMap
;
171 template <typename AddressSpaceViewTy
>
173 static const uptr kSpaceBeg
= 0;
174 static const u64 kSpaceSize
= SANITIZER_MMAP_RANGE_SIZE
;
175 static const uptr kMetadataSize
= 0;
176 typedef __asan::SizeClassMap SizeClassMap
;
177 static const uptr kRegionSizeLog
= 20;
178 using AddressSpaceView
= AddressSpaceViewTy
;
179 typedef AsanMapUnmapCallback MapUnmapCallback
;
180 static const uptr kFlags
= 0;
182 template <typename AddressSpaceView
>
183 using PrimaryAllocatorASVT
= SizeClassAllocator32
<AP32
<AddressSpaceView
> >;
184 using PrimaryAllocator
= PrimaryAllocatorASVT
<LocalAddressSpaceView
>;
185 #endif // SANITIZER_CAN_USE_ALLOCATOR64
187 static const uptr kNumberOfSizeClasses
= SizeClassMap::kNumClasses
;
189 template <typename AddressSpaceView
>
190 using AsanAllocatorASVT
=
191 CombinedAllocator
<PrimaryAllocatorASVT
<AddressSpaceView
>>;
192 using AsanAllocator
= AsanAllocatorASVT
<LocalAddressSpaceView
>;
193 using AllocatorCache
= AsanAllocator::AllocatorCache
;
195 struct AsanThreadLocalMallocStorage
{
196 uptr quarantine_cache
[16];
197 AllocatorCache allocator_cache
;
200 // These objects are allocated via mmap() and are zero-initialized.
201 AsanThreadLocalMallocStorage() {}
204 void *asan_memalign(uptr alignment
, uptr size
, BufferedStackTrace
*stack
,
205 AllocType alloc_type
);
206 void asan_free(void *ptr
, BufferedStackTrace
*stack
, AllocType alloc_type
);
207 void asan_delete(void *ptr
, uptr size
, uptr alignment
,
208 BufferedStackTrace
*stack
, AllocType alloc_type
);
210 void *asan_malloc(uptr size
, BufferedStackTrace
*stack
);
211 void *asan_calloc(uptr nmemb
, uptr size
, BufferedStackTrace
*stack
);
212 void *asan_realloc(void *p
, uptr size
, BufferedStackTrace
*stack
);
213 void *asan_reallocarray(void *p
, uptr nmemb
, uptr size
,
214 BufferedStackTrace
*stack
);
215 void *asan_valloc(uptr size
, BufferedStackTrace
*stack
);
216 void *asan_pvalloc(uptr size
, BufferedStackTrace
*stack
);
218 void *asan_aligned_alloc(uptr alignment
, uptr size
, BufferedStackTrace
*stack
);
219 int asan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
220 BufferedStackTrace
*stack
);
221 uptr
asan_malloc_usable_size(const void *ptr
, uptr pc
, uptr bp
);
223 uptr
asan_mz_size(const void *ptr
);
224 void asan_mz_force_lock();
225 void asan_mz_force_unlock();
227 void PrintInternalAllocatorStats();
228 void AsanSoftRssLimitExceededCallback(bool exceeded
);
230 } // namespace __asan
231 #endif // ASAN_ALLOCATOR_H