1 //===-- asan_allocator.h ----------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // ASan-private header for asan_allocator.cc.
11 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_ALLOCATOR_H
14 #define ASAN_ALLOCATOR_H
16 #include "asan_flags.h"
17 #include "asan_internal.h"
18 #include "asan_interceptors.h"
19 #include "sanitizer_common/sanitizer_allocator.h"
20 #include "sanitizer_common/sanitizer_list.h"
25 FROM_MALLOC
= 1, // Memory block came from malloc, calloc, realloc, etc.
26 FROM_NEW
= 2, // Memory block came from operator new.
27 FROM_NEW_BR
= 3 // Memory block came from operator new [ ]
32 struct AllocatorOptions
{
33 u32 quarantine_size_mb
;
37 u8 alloc_dealloc_mismatch
;
39 void SetFrom(const Flags
*f
, const CommonFlags
*cf
);
40 void CopyTo(Flags
*f
, CommonFlags
*cf
);
43 void InitializeAllocator(const AllocatorOptions
&options
);
44 void ReInitializeAllocator(const AllocatorOptions
&options
);
45 void GetAllocatorOptions(AllocatorOptions
*options
);
49 explicit AsanChunkView(AsanChunk
*chunk
) : chunk_(chunk
) {}
50 bool IsValid(); // Checks if AsanChunkView points to a valid allocated
51 // or quarantined chunk.
52 uptr
Beg(); // First byte of user memory.
53 uptr
End(); // Last byte of user memory.
54 uptr
UsedSize(); // Size requested by the user.
57 bool Eq(const AsanChunkView
&c
) const { return chunk_
== c
.chunk_
; }
58 StackTrace
GetAllocStack();
59 StackTrace
GetFreeStack();
60 bool AddrIsInside(uptr addr
, uptr access_size
, sptr
*offset
) {
61 if (addr
>= Beg() && (addr
+ access_size
) <= End()) {
62 *offset
= addr
- Beg();
67 bool AddrIsAtLeft(uptr addr
, uptr access_size
, sptr
*offset
) {
70 *offset
= Beg() - addr
;
75 bool AddrIsAtRight(uptr addr
, uptr access_size
, sptr
*offset
) {
76 if (addr
+ access_size
> End()) {
77 *offset
= addr
- End();
84 AsanChunk
*const chunk_
;
87 AsanChunkView
FindHeapChunkByAddress(uptr address
);
89 // List of AsanChunks with total size.
90 class AsanChunkFifoList
: public IntrusiveList
<AsanChunk
> {
92 explicit AsanChunkFifoList(LinkerInitialized
) { }
93 AsanChunkFifoList() { clear(); }
94 void Push(AsanChunk
*n
);
95 void PushList(AsanChunkFifoList
*q
);
97 uptr
size() { return size_
; }
99 IntrusiveList
<AsanChunk
>::clear();
106 struct AsanMapUnmapCallback
{
107 void OnMap(uptr p
, uptr size
) const;
108 void OnUnmap(uptr p
, uptr size
) const;
111 #if SANITIZER_CAN_USE_ALLOCATOR64
112 # if defined(__powerpc64__)
113 const uptr kAllocatorSpace
= 0xa0000000000ULL
;
114 const uptr kAllocatorSize
= 0x20000000000ULL
; // 2T.
115 # elif defined(__aarch64__)
116 // AArch64/SANITIZIER_CAN_USER_ALLOCATOR64 is only for 42-bit VMA
117 // so no need to different values for different VMA.
118 const uptr kAllocatorSpace
= 0x10000000000ULL
;
119 const uptr kAllocatorSize
= 0x10000000000ULL
; // 3T.
121 const uptr kAllocatorSpace
= 0x600000000000ULL
;
122 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
124 typedef DefaultSizeClassMap SizeClassMap
;
125 typedef SizeClassAllocator64
<kAllocatorSpace
, kAllocatorSize
, 0 /*metadata*/,
126 SizeClassMap
, AsanMapUnmapCallback
> PrimaryAllocator
;
127 #else // Fallback to SizeClassAllocator32.
128 static const uptr kRegionSizeLog
= 20;
129 static const uptr kNumRegions
= SANITIZER_MMAP_RANGE_SIZE
>> kRegionSizeLog
;
130 # if SANITIZER_WORDSIZE == 32
131 typedef FlatByteMap
<kNumRegions
> ByteMap
;
132 # elif SANITIZER_WORDSIZE == 64
133 typedef TwoLevelByteMap
<(kNumRegions
>> 12), 1 << 12> ByteMap
;
135 typedef CompactSizeClassMap SizeClassMap
;
136 typedef SizeClassAllocator32
<0, SANITIZER_MMAP_RANGE_SIZE
, 16,
137 SizeClassMap
, kRegionSizeLog
,
139 AsanMapUnmapCallback
> PrimaryAllocator
;
140 #endif // SANITIZER_CAN_USE_ALLOCATOR64
142 static const uptr kNumberOfSizeClasses
= SizeClassMap::kNumClasses
;
143 typedef SizeClassAllocatorLocalCache
<PrimaryAllocator
> AllocatorCache
;
144 typedef LargeMmapAllocator
<AsanMapUnmapCallback
> SecondaryAllocator
;
145 typedef CombinedAllocator
<PrimaryAllocator
, AllocatorCache
,
146 SecondaryAllocator
> AsanAllocator
;
149 struct AsanThreadLocalMallocStorage
{
150 uptr quarantine_cache
[16];
151 AllocatorCache allocator_cache
;
154 // These objects are allocated via mmap() and are zero-initialized.
155 AsanThreadLocalMallocStorage() {}
158 void *asan_memalign(uptr alignment
, uptr size
, BufferedStackTrace
*stack
,
159 AllocType alloc_type
);
160 void asan_free(void *ptr
, BufferedStackTrace
*stack
, AllocType alloc_type
);
161 void asan_sized_free(void *ptr
, uptr size
, BufferedStackTrace
*stack
,
162 AllocType alloc_type
);
164 void *asan_malloc(uptr size
, BufferedStackTrace
*stack
);
165 void *asan_calloc(uptr nmemb
, uptr size
, BufferedStackTrace
*stack
);
166 void *asan_realloc(void *p
, uptr size
, BufferedStackTrace
*stack
);
167 void *asan_valloc(uptr size
, BufferedStackTrace
*stack
);
168 void *asan_pvalloc(uptr size
, BufferedStackTrace
*stack
);
170 int asan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
171 BufferedStackTrace
*stack
);
172 uptr
asan_malloc_usable_size(void *ptr
, uptr pc
, uptr bp
);
174 uptr
asan_mz_size(const void *ptr
);
175 void asan_mz_force_lock();
176 void asan_mz_force_unlock();
178 void PrintInternalAllocatorStats();
179 void AsanSoftRssLimitExceededCallback(bool exceeded
);
181 } // namespace __asan
182 #endif // ASAN_ALLOCATOR_H