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_internal.h"
19 #include "asan_interceptors.h"
20 #include "sanitizer_common/sanitizer_allocator.h"
21 #include "sanitizer_common/sanitizer_list.h"
26 FROM_MALLOC
= 1, // Memory block came from malloc, calloc, realloc, etc.
27 FROM_NEW
= 2, // Memory block came from operator new.
28 FROM_NEW_BR
= 3 // Memory block came from operator new [ ]
33 struct AllocatorOptions
{
34 u32 quarantine_size_mb
;
35 u32 thread_local_quarantine_size_kb
;
39 u8 alloc_dealloc_mismatch
;
40 s32 release_to_os_interval_ms
;
42 void SetFrom(const Flags
*f
, const CommonFlags
*cf
);
43 void CopyTo(Flags
*f
, CommonFlags
*cf
);
46 void InitializeAllocator(const AllocatorOptions
&options
);
47 void ReInitializeAllocator(const AllocatorOptions
&options
);
48 void GetAllocatorOptions(AllocatorOptions
*options
);
52 explicit AsanChunkView(AsanChunk
*chunk
) : chunk_(chunk
) {}
53 bool IsValid() const; // Checks if AsanChunkView points to a valid
54 // allocated or quarantined chunk.
55 bool IsAllocated() const; // Checks if the memory is currently allocated.
56 bool IsQuarantined() const; // Checks if the memory is currently quarantined.
57 uptr
Beg() const; // First byte of user memory.
58 uptr
End() const; // Last byte of user memory.
59 uptr
UsedSize() const; // Size requested by the user.
60 u32
UserRequestedAlignment() const; // Originally requested alignment.
61 uptr
AllocTid() const;
63 bool Eq(const AsanChunkView
&c
) const { return chunk_
== c
.chunk_
; }
64 u32
GetAllocStackId() const;
65 u32
GetFreeStackId() const;
66 StackTrace
GetAllocStack() const;
67 StackTrace
GetFreeStack() const;
68 AllocType
GetAllocType() const;
69 bool AddrIsInside(uptr addr
, uptr access_size
, sptr
*offset
) const {
70 if (addr
>= Beg() && (addr
+ access_size
) <= End()) {
71 *offset
= addr
- Beg();
76 bool AddrIsAtLeft(uptr addr
, uptr access_size
, sptr
*offset
) const {
79 *offset
= Beg() - addr
;
84 bool AddrIsAtRight(uptr addr
, uptr access_size
, sptr
*offset
) const {
85 if (addr
+ access_size
> End()) {
86 *offset
= addr
- End();
93 AsanChunk
*const chunk_
;
96 AsanChunkView
FindHeapChunkByAddress(uptr address
);
97 AsanChunkView
FindHeapChunkByAllocBeg(uptr address
);
99 // List of AsanChunks with total size.
100 class AsanChunkFifoList
: public IntrusiveList
<AsanChunk
> {
102 explicit AsanChunkFifoList(LinkerInitialized
) { }
103 AsanChunkFifoList() { clear(); }
104 void Push(AsanChunk
*n
);
105 void PushList(AsanChunkFifoList
*q
);
107 uptr
size() { return size_
; }
109 IntrusiveList
<AsanChunk
>::clear();
116 struct AsanMapUnmapCallback
{
117 void OnMap(uptr p
, uptr size
) const;
118 void OnUnmap(uptr p
, uptr size
) const;
121 #if SANITIZER_CAN_USE_ALLOCATOR64
122 # if SANITIZER_FUCHSIA
123 const uptr kAllocatorSpace
= ~(uptr
)0;
124 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
125 typedef DefaultSizeClassMap SizeClassMap
;
126 # elif defined(__powerpc64__)
127 const uptr kAllocatorSpace
= ~(uptr
)0;
128 const uptr kAllocatorSize
= 0x20000000000ULL
; // 2T.
129 typedef DefaultSizeClassMap SizeClassMap
;
130 # elif defined(__aarch64__) && SANITIZER_ANDROID
131 // Android needs to support 39, 42 and 48 bit VMA.
132 const uptr kAllocatorSpace
= ~(uptr
)0;
133 const uptr kAllocatorSize
= 0x2000000000ULL
; // 128G.
134 typedef VeryCompactSizeClassMap SizeClassMap
;
135 # elif defined(__aarch64__)
136 // AArch64/SANITIZER_CAN_USE_ALLOCATOR64 is only for 42-bit VMA
137 // so no need to different values for different VMA.
138 const uptr kAllocatorSpace
= 0x10000000000ULL
;
139 const uptr kAllocatorSize
= 0x10000000000ULL
; // 3T.
140 typedef DefaultSizeClassMap SizeClassMap
;
141 #elif defined(__sparc__)
142 const uptr kAllocatorSpace
= ~(uptr
)0;
143 const uptr kAllocatorSize
= 0x20000000000ULL
; // 2T.
144 typedef DefaultSizeClassMap SizeClassMap
;
145 # elif SANITIZER_WINDOWS
146 const uptr kAllocatorSpace
= ~(uptr
)0;
147 const uptr kAllocatorSize
= 0x8000000000ULL
; // 500G
148 typedef DefaultSizeClassMap SizeClassMap
;
150 const uptr kAllocatorSpace
= 0x600000000000ULL
;
151 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
152 typedef DefaultSizeClassMap SizeClassMap
;
154 template <typename AddressSpaceViewTy
>
155 struct AP64
{ // Allocator64 parameters. Deliberately using a short name.
156 static const uptr kSpaceBeg
= kAllocatorSpace
;
157 static const uptr kSpaceSize
= kAllocatorSize
;
158 static const uptr kMetadataSize
= 0;
159 typedef __asan::SizeClassMap SizeClassMap
;
160 typedef AsanMapUnmapCallback MapUnmapCallback
;
161 static const uptr kFlags
= 0;
162 using AddressSpaceView
= AddressSpaceViewTy
;
165 template <typename AddressSpaceView
>
166 using PrimaryAllocatorASVT
= SizeClassAllocator64
<AP64
<AddressSpaceView
>>;
167 using PrimaryAllocator
= PrimaryAllocatorASVT
<LocalAddressSpaceView
>;
168 #else // Fallback to SizeClassAllocator32.
169 typedef CompactSizeClassMap SizeClassMap
;
170 template <typename AddressSpaceViewTy
>
172 static const uptr kSpaceBeg
= 0;
173 static const u64 kSpaceSize
= SANITIZER_MMAP_RANGE_SIZE
;
174 static const uptr kMetadataSize
= 16;
175 typedef __asan::SizeClassMap SizeClassMap
;
176 static const uptr kRegionSizeLog
= 20;
177 using AddressSpaceView
= AddressSpaceViewTy
;
178 typedef AsanMapUnmapCallback MapUnmapCallback
;
179 static const uptr kFlags
= 0;
181 template <typename AddressSpaceView
>
182 using PrimaryAllocatorASVT
= SizeClassAllocator32
<AP32
<AddressSpaceView
> >;
183 using PrimaryAllocator
= PrimaryAllocatorASVT
<LocalAddressSpaceView
>;
184 #endif // SANITIZER_CAN_USE_ALLOCATOR64
186 static const uptr kNumberOfSizeClasses
= SizeClassMap::kNumClasses
;
188 template <typename AddressSpaceView
>
189 using AsanAllocatorASVT
=
190 CombinedAllocator
<PrimaryAllocatorASVT
<AddressSpaceView
>>;
191 using AsanAllocator
= AsanAllocatorASVT
<LocalAddressSpaceView
>;
192 using AllocatorCache
= AsanAllocator::AllocatorCache
;
194 struct AsanThreadLocalMallocStorage
{
195 uptr quarantine_cache
[16];
196 AllocatorCache allocator_cache
;
199 // These objects are allocated via mmap() and are zero-initialized.
200 AsanThreadLocalMallocStorage() {}
203 void *asan_memalign(uptr alignment
, uptr size
, BufferedStackTrace
*stack
,
204 AllocType alloc_type
);
205 void asan_free(void *ptr
, BufferedStackTrace
*stack
, AllocType alloc_type
);
206 void asan_delete(void *ptr
, uptr size
, uptr alignment
,
207 BufferedStackTrace
*stack
, AllocType alloc_type
);
209 void *asan_malloc(uptr size
, BufferedStackTrace
*stack
);
210 void *asan_calloc(uptr nmemb
, uptr size
, BufferedStackTrace
*stack
);
211 void *asan_realloc(void *p
, uptr size
, BufferedStackTrace
*stack
);
212 void *asan_reallocarray(void *p
, uptr nmemb
, uptr size
,
213 BufferedStackTrace
*stack
);
214 void *asan_valloc(uptr size
, BufferedStackTrace
*stack
);
215 void *asan_pvalloc(uptr size
, BufferedStackTrace
*stack
);
217 void *asan_aligned_alloc(uptr alignment
, uptr size
, BufferedStackTrace
*stack
);
218 int asan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
219 BufferedStackTrace
*stack
);
220 uptr
asan_malloc_usable_size(const void *ptr
, uptr pc
, uptr bp
);
222 uptr
asan_mz_size(const void *ptr
);
223 void asan_mz_force_lock();
224 void asan_mz_force_unlock();
226 void PrintInternalAllocatorStats();
227 void AsanSoftRssLimitExceededCallback(bool exceeded
);
229 } // namespace __asan
230 #endif // ASAN_ALLOCATOR_H