1 //===-- asan_allocator.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // ASan-private header for asan_allocator.cc.
13 //===----------------------------------------------------------------------===//
15 #ifndef ASAN_ALLOCATOR_H
16 #define ASAN_ALLOCATOR_H
18 #include "asan_flags.h"
19 #include "asan_internal.h"
20 #include "asan_interceptors.h"
21 #include "sanitizer_common/sanitizer_allocator.h"
22 #include "sanitizer_common/sanitizer_list.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;
39 u8 alloc_dealloc_mismatch;
41 void SetFrom(const Flags *f, const CommonFlags *cf);
42 void CopyTo(Flags *f, CommonFlags *cf);
45 void InitializeAllocator(const AllocatorOptions &options);
46 void ReInitializeAllocator(const AllocatorOptions &options);
47 void GetAllocatorOptions(AllocatorOptions *options);
51 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
52 bool IsValid(); // Checks if AsanChunkView points to a valid allocated
53 // or quarantined chunk.
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 StackTrace GetAllocStack();
61 StackTrace GetFreeStack();
62 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
63 if (addr >= Beg() && (addr + access_size) <= End()) {
64 *offset = addr - Beg();
69 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
72 *offset = Beg() - addr;
77 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
78 if (addr + access_size > End()) {
79 *offset = addr - End();
86 AsanChunk *const chunk_;
89 AsanChunkView FindHeapChunkByAddress(uptr address);
91 // List of AsanChunks with total size.
92 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
94 explicit AsanChunkFifoList(LinkerInitialized) { }
95 AsanChunkFifoList() { clear(); }
96 void Push(AsanChunk *n);
97 void PushList(AsanChunkFifoList *q);
99 uptr size() { return size_; }
101 IntrusiveList<AsanChunk>::clear();
108 struct AsanMapUnmapCallback {
109 void OnMap(uptr p, uptr size) const;
110 void OnUnmap(uptr p, uptr size) const;
113 #if SANITIZER_CAN_USE_ALLOCATOR64
114 # if defined(__powerpc64__)
115 const uptr kAllocatorSpace = 0xa0000000000ULL;
116 const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
117 # elif defined(__aarch64__)
118 // AArch64/SANITIZIER_CAN_USER_ALLOCATOR64 is only for 42-bit VMA
119 // so no need to different values for different VMA.
120 const uptr kAllocatorSpace = 0x10000000000ULL;
121 const uptr kAllocatorSize = 0x10000000000ULL; // 3T.
123 const uptr kAllocatorSpace = 0x600000000000ULL;
124 const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
126 typedef DefaultSizeClassMap SizeClassMap;
127 typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 0 /*metadata*/,
128 SizeClassMap, AsanMapUnmapCallback> PrimaryAllocator;
129 #else // Fallback to SizeClassAllocator32.
130 static const uptr kRegionSizeLog = 20;
131 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
132 # if SANITIZER_WORDSIZE == 32
133 typedef FlatByteMap<kNumRegions> ByteMap;
134 # elif SANITIZER_WORDSIZE == 64
135 typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
137 typedef CompactSizeClassMap SizeClassMap;
138 typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, 16,
139 SizeClassMap, kRegionSizeLog,
141 AsanMapUnmapCallback> PrimaryAllocator;
142 #endif // SANITIZER_CAN_USE_ALLOCATOR64
144 static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
145 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
146 typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
147 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
148 SecondaryAllocator> AsanAllocator;
151 struct AsanThreadLocalMallocStorage {
152 uptr quarantine_cache[16];
153 AllocatorCache allocator_cache;
156 // These objects are allocated via mmap() and are zero-initialized.
157 AsanThreadLocalMallocStorage() {}
160 void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
161 AllocType alloc_type);
162 void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
163 void asan_sized_free(void *ptr, uptr size, BufferedStackTrace *stack,
164 AllocType alloc_type);
166 void *asan_malloc(uptr size, BufferedStackTrace *stack);
167 void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
168 void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
169 void *asan_valloc(uptr size, BufferedStackTrace *stack);
170 void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
172 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
173 BufferedStackTrace *stack);
174 uptr asan_malloc_usable_size(void *ptr, uptr pc, uptr bp);
176 uptr asan_mz_size(const void *ptr);
177 void asan_mz_force_lock();
178 void asan_mz_force_unlock();
180 void PrintInternalAllocatorStats();
181 void AsanSoftRssLimitExceededCallback(bool exceeded);
183 } // namespace __asan
184 #endif // ASAN_ALLOCATOR_H