1 //===-- asan_allocator.h ----------------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of AddressSanitizer, an address sanity checker. 10 // 11 // ASan-private header for asan_allocator.cpp. 12 //===----------------------------------------------------------------------===// 13 14 #ifndef ASAN_ALLOCATOR_H 15 #define ASAN_ALLOCATOR_H 16 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" 22 23 namespace __asan { 24 25 enum AllocType { 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 [ ] 29 }; 30 31 struct AsanChunk; 32 33 struct AllocatorOptions { 34 u32 quarantine_size_mb; 35 u32 thread_local_quarantine_size_kb; 36 u16 min_redzone; 37 u16 max_redzone; 38 u8 may_return_null; 39 u8 alloc_dealloc_mismatch; 40 s32 release_to_os_interval_ms; 41 42 void SetFrom(const Flags *f, const CommonFlags *cf); 43 void CopyTo(Flags *f, CommonFlags *cf); 44 }; 45 46 void InitializeAllocator(const AllocatorOptions &options); 47 void ReInitializeAllocator(const AllocatorOptions &options); 48 void GetAllocatorOptions(AllocatorOptions *options); 49 50 class AsanChunkView { 51 public: 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; 62 uptr FreeTid() 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(); 72 return true; 73 } 74 return false; 75 } 76 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) const { 77 (void)access_size; 78 if (addr < Beg()) { 79 *offset = Beg() - addr; 80 return true; 81 } 82 return false; 83 } 84 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) const { 85 if (addr + access_size > End()) { 86 *offset = addr - End(); 87 return true; 88 } 89 return false; 90 } 91 92 private: 93 AsanChunk *const chunk_; 94 }; 95 96 AsanChunkView FindHeapChunkByAddress(uptr address); 97 AsanChunkView FindHeapChunkByAllocBeg(uptr address); 98 99 // List of AsanChunks with total size. 100 class AsanChunkFifoList: public IntrusiveList<AsanChunk> { 101 public: 102 explicit AsanChunkFifoList(LinkerInitialized) { } 103 AsanChunkFifoList() { clear(); } 104 void Push(AsanChunk *n); 105 void PushList(AsanChunkFifoList *q); 106 AsanChunk *Pop(); 107 uptr size() { return size_; } 108 void clear() { 109 IntrusiveList<AsanChunk>::clear(); 110 size_ = 0; 111 } 112 private: 113 uptr size_; 114 }; 115 116 struct AsanMapUnmapCallback { 117 void OnMap(uptr p, uptr size) const; 118 void OnUnmap(uptr p, uptr size) const; 119 }; 120 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; 149 # else 150 const uptr kAllocatorSpace = 0x600000000000ULL; 151 const uptr kAllocatorSize = 0x40000000000ULL; // 4T. 152 typedef DefaultSizeClassMap SizeClassMap; 153 # endif 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; 163 }; 164 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> 171 struct AP32 { 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; 180 }; 181 template <typename AddressSpaceView> 182 using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView> >; 183 using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; 184 #endif // SANITIZER_CAN_USE_ALLOCATOR64 185 186 static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses; 187 188 template <typename AddressSpaceView> 189 using AsanAllocatorASVT = 190 CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>; 191 using AsanAllocator = AsanAllocatorASVT<LocalAddressSpaceView>; 192 using AllocatorCache = AsanAllocator::AllocatorCache; 193 194 struct AsanThreadLocalMallocStorage { 195 uptr quarantine_cache[16]; 196 AllocatorCache allocator_cache; 197 void CommitBack(); 198 private: 199 // These objects are allocated via mmap() and are zero-initialized. 200 AsanThreadLocalMallocStorage() {} 201 }; 202 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); 208 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); 216 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); 221 222 uptr asan_mz_size(const void *ptr); 223 void asan_mz_force_lock(); 224 void asan_mz_force_unlock(); 225 226 void PrintInternalAllocatorStats(); 227 void AsanSoftRssLimitExceededCallback(bool exceeded); 228 229 } // namespace __asan 230 #endif // ASAN_ALLOCATOR_H 231