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;
38 s32 release_to_os_interval_ms;
40 void SetFrom(const Flags *f, const CommonFlags *cf);
41 void CopyTo(Flags *f, CommonFlags *cf);
44 void InitializeAllocator(const AllocatorOptions &options);
45 void ReInitializeAllocator(const AllocatorOptions &options);
46 void GetAllocatorOptions(AllocatorOptions *options);
50 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
51 bool IsValid(); // Checks if AsanChunkView points to a valid allocated
52 // or quarantined chunk.
53 bool IsAllocated(); // Checks if the memory is currently allocated.
54 uptr Beg(); // First byte of user memory.
55 uptr End(); // Last byte of user memory.
56 uptr UsedSize(); // Size requested by the user.
59 bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
60 u32 GetAllocStackId();
62 StackTrace GetAllocStack();
63 StackTrace GetFreeStack();
64 AllocType GetAllocType();
65 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
66 if (addr >= Beg() && (addr + access_size) <= End()) {
67 *offset = addr - Beg();
72 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
75 *offset = Beg() - addr;
80 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
81 if (addr + access_size > End()) {
82 *offset = addr - End();
89 AsanChunk *const chunk_;
92 AsanChunkView FindHeapChunkByAddress(uptr address);
93 AsanChunkView FindHeapChunkByAllocBeg(uptr address);
95 // List of AsanChunks with total size.
96 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
98 explicit AsanChunkFifoList(LinkerInitialized) { }
99 AsanChunkFifoList() { clear(); }
100 void Push(AsanChunk *n);
101 void PushList(AsanChunkFifoList *q);
103 uptr size() { return size_; }
105 IntrusiveList<AsanChunk>::clear();
112 struct AsanMapUnmapCallback {
113 void OnMap(uptr p, uptr size) const;
114 void OnUnmap(uptr p, uptr size) const;
117 #if SANITIZER_CAN_USE_ALLOCATOR64
118 # if defined(__powerpc64__)
119 const uptr kAllocatorSpace = 0xa0000000000ULL;
120 const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
121 typedef DefaultSizeClassMap SizeClassMap;
122 # elif defined(__aarch64__) && SANITIZER_ANDROID
123 const uptr kAllocatorSpace = 0x3000000000ULL;
124 const uptr kAllocatorSize = 0x2000000000ULL; // 128G.
125 typedef VeryCompactSizeClassMap SizeClassMap;
126 # elif defined(__aarch64__)
127 // AArch64/SANITIZER_CAN_USER_ALLOCATOR64 is only for 42-bit VMA
128 // so no need to different values for different VMA.
129 const uptr kAllocatorSpace = 0x10000000000ULL;
130 const uptr kAllocatorSize = 0x10000000000ULL; // 3T.
131 typedef DefaultSizeClassMap SizeClassMap;
132 # elif SANITIZER_WINDOWS
133 const uptr kAllocatorSpace = ~(uptr)0;
134 const uptr kAllocatorSize = 0x8000000000ULL; // 500G
135 typedef DefaultSizeClassMap SizeClassMap;
137 const uptr kAllocatorSpace = 0x600000000000ULL;
138 const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
139 typedef DefaultSizeClassMap SizeClassMap;
141 struct AP64 { // Allocator64 parameters. Deliberately using a short name.
142 static const uptr kSpaceBeg = kAllocatorSpace;
143 static const uptr kSpaceSize = kAllocatorSize;
144 static const uptr kMetadataSize = 0;
145 typedef __asan::SizeClassMap SizeClassMap;
146 typedef AsanMapUnmapCallback MapUnmapCallback;
147 static const uptr kFlags = 0;
150 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
151 #else // Fallback to SizeClassAllocator32.
152 static const uptr kRegionSizeLog = 17;
153 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
154 # if SANITIZER_WORDSIZE == 32
155 typedef FlatByteMap<kNumRegions> ByteMap;
156 # elif SANITIZER_WORDSIZE == 64
157 typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
159 typedef VeryCompactSizeClassMap SizeClassMap;
160 typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, 16,
161 SizeClassMap, kRegionSizeLog,
163 AsanMapUnmapCallback> PrimaryAllocator;
164 #endif // SANITIZER_CAN_USE_ALLOCATOR64
166 static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
167 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
168 typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
169 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
170 SecondaryAllocator> AsanAllocator;
173 struct AsanThreadLocalMallocStorage {
174 uptr quarantine_cache[16];
175 AllocatorCache allocator_cache;
178 // These objects are allocated via mmap() and are zero-initialized.
179 AsanThreadLocalMallocStorage() {}
182 void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
183 AllocType alloc_type);
184 void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
185 void asan_sized_free(void *ptr, uptr size, BufferedStackTrace *stack,
186 AllocType alloc_type);
188 void *asan_malloc(uptr size, BufferedStackTrace *stack);
189 void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
190 void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
191 void *asan_valloc(uptr size, BufferedStackTrace *stack);
192 void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
194 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
195 BufferedStackTrace *stack);
196 uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
198 uptr asan_mz_size(const void *ptr);
199 void asan_mz_force_lock();
200 void asan_mz_force_unlock();
202 void PrintInternalAllocatorStats();
203 void AsanSoftRssLimitExceededCallback(bool exceeded);
205 } // namespace __asan
206 #endif // ASAN_ALLOCATOR_H