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_interceptors.h" 19#include "asan_internal.h" 20#include "sanitizer_common/sanitizer_allocator.h" 21#include "sanitizer_common/sanitizer_list.h" 22#include "sanitizer_common/sanitizer_platform.h" 23 24namespace __asan { 25 26enum AllocType { 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 [ ] 30}; 31 32class AsanChunk; 33 34struct AllocatorOptions { 35 u32 quarantine_size_mb; 36 u32 thread_local_quarantine_size_kb; 37 u16 min_redzone; 38 u16 max_redzone; 39 u8 may_return_null; 40 u8 alloc_dealloc_mismatch; 41 s32 release_to_os_interval_ms; 42 43 void SetFrom(const Flags *f, const CommonFlags *cf); 44 void CopyTo(Flags *f, CommonFlags *cf); 45}; 46 47void InitializeAllocator(const AllocatorOptions &options); 48void ReInitializeAllocator(const AllocatorOptions &options); 49void GetAllocatorOptions(AllocatorOptions *options); 50 51class AsanChunkView { 52 public: 53 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {} 54 bool IsValid() const; // Checks if AsanChunkView points to a valid 55 // allocated or quarantined chunk. 56 bool IsAllocated() const; // Checks if the memory is currently allocated. 57 bool IsQuarantined() const; // Checks if the memory is currently quarantined. 58 uptr Beg() const; // First byte of user memory. 59 uptr End() const; // Last byte of user memory. 60 uptr UsedSize() const; // Size requested by the user. 61 u32 UserRequestedAlignment() const; // Originally requested alignment. 62 uptr AllocTid() const; 63 uptr FreeTid() const; 64 bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; } 65 u32 GetAllocStackId() const; 66 u32 GetFreeStackId() const; 67 AllocType GetAllocType() const; 68 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) const { 69 if (addr >= Beg() && (addr + access_size) <= End()) { 70 *offset = addr - Beg(); 71 return true; 72 } 73 return false; 74 } 75 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) const { 76 (void)access_size; 77 if (addr < Beg()) { 78 *offset = Beg() - addr; 79 return true; 80 } 81 return false; 82 } 83 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) const { 84 if (addr + access_size > End()) { 85 *offset = addr - End(); 86 return true; 87 } 88 return false; 89 } 90 91 private: 92 AsanChunk *const chunk_; 93}; 94 95AsanChunkView FindHeapChunkByAddress(uptr address); 96AsanChunkView FindHeapChunkByAllocBeg(uptr address); 97 98// List of AsanChunks with total size. 99class AsanChunkFifoList: public IntrusiveList<AsanChunk> { 100 public: 101 explicit AsanChunkFifoList(LinkerInitialized) { } 102 AsanChunkFifoList() { clear(); } 103 void Push(AsanChunk *n); 104 void PushList(AsanChunkFifoList *q); 105 AsanChunk *Pop(); 106 uptr size() { return size_; } 107 void clear() { 108 IntrusiveList<AsanChunk>::clear(); 109 size_ = 0; 110 } 111 private: 112 uptr size_; 113}; 114 115struct AsanMapUnmapCallback { 116 void OnMap(uptr p, uptr size) const; 117 void OnUnmap(uptr p, uptr size) const; 118}; 119 120#if SANITIZER_CAN_USE_ALLOCATOR64 121# if SANITIZER_FUCHSIA 122const uptr kAllocatorSpace = ~(uptr)0; 123const uptr kAllocatorSize = 0x40000000000ULL; // 4T. 124typedef DefaultSizeClassMap SizeClassMap; 125# elif defined(__powerpc64__) 126const uptr kAllocatorSpace = ~(uptr)0; 127const uptr kAllocatorSize = 0x20000000000ULL; // 2T. 128typedef DefaultSizeClassMap SizeClassMap; 129# elif defined(__aarch64__) && SANITIZER_ANDROID 130// Android needs to support 39, 42 and 48 bit VMA. 131const uptr kAllocatorSpace = ~(uptr)0; 132const uptr kAllocatorSize = 0x2000000000ULL; // 128G. 133typedef VeryCompactSizeClassMap SizeClassMap; 134#elif SANITIZER_RISCV64 135const uptr kAllocatorSpace = ~(uptr)0; 136const uptr kAllocatorSize = 0x2000000000ULL; // 128G. 137typedef VeryDenseSizeClassMap SizeClassMap; 138# elif defined(__aarch64__) 139// AArch64/SANITIZER_CAN_USE_ALLOCATOR64 is only for 42-bit VMA 140// so no need to different values for different VMA. 141const uptr kAllocatorSpace = 0x10000000000ULL; 142const uptr kAllocatorSize = 0x10000000000ULL; // 3T. 143typedef DefaultSizeClassMap SizeClassMap; 144#elif defined(__sparc__) 145const uptr kAllocatorSpace = ~(uptr)0; 146const uptr kAllocatorSize = 0x20000000000ULL; // 2T. 147typedef DefaultSizeClassMap SizeClassMap; 148# elif SANITIZER_WINDOWS 149const uptr kAllocatorSpace = ~(uptr)0; 150const uptr kAllocatorSize = 0x8000000000ULL; // 500G 151typedef DefaultSizeClassMap SizeClassMap; 152# else 153const uptr kAllocatorSpace = 0x600000000000ULL; 154const uptr kAllocatorSize = 0x40000000000ULL; // 4T. 155typedef DefaultSizeClassMap SizeClassMap; 156# endif 157template <typename AddressSpaceViewTy> 158struct AP64 { // Allocator64 parameters. Deliberately using a short name. 159 static const uptr kSpaceBeg = kAllocatorSpace; 160 static const uptr kSpaceSize = kAllocatorSize; 161 static const uptr kMetadataSize = 0; 162 typedef __asan::SizeClassMap SizeClassMap; 163 typedef AsanMapUnmapCallback MapUnmapCallback; 164 static const uptr kFlags = 0; 165 using AddressSpaceView = AddressSpaceViewTy; 166}; 167 168template <typename AddressSpaceView> 169using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>; 170using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; 171#else // Fallback to SizeClassAllocator32. 172typedef CompactSizeClassMap SizeClassMap; 173template <typename AddressSpaceViewTy> 174struct AP32 { 175 static const uptr kSpaceBeg = 0; 176 static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; 177 static const uptr kMetadataSize = 0; 178 typedef __asan::SizeClassMap SizeClassMap; 179 static const uptr kRegionSizeLog = 20; 180 using AddressSpaceView = AddressSpaceViewTy; 181 typedef AsanMapUnmapCallback MapUnmapCallback; 182 static const uptr kFlags = 0; 183}; 184template <typename AddressSpaceView> 185using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView> >; 186using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; 187#endif // SANITIZER_CAN_USE_ALLOCATOR64 188 189static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses; 190 191template <typename AddressSpaceView> 192using AsanAllocatorASVT = 193 CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>; 194using AsanAllocator = AsanAllocatorASVT<LocalAddressSpaceView>; 195using AllocatorCache = AsanAllocator::AllocatorCache; 196 197struct AsanThreadLocalMallocStorage { 198 uptr quarantine_cache[16]; 199 AllocatorCache allocator_cache; 200 void CommitBack(); 201 private: 202 // These objects are allocated via mmap() and are zero-initialized. 203 AsanThreadLocalMallocStorage() {} 204}; 205 206void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack, 207 AllocType alloc_type); 208void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type); 209void asan_delete(void *ptr, uptr size, uptr alignment, 210 BufferedStackTrace *stack, AllocType alloc_type); 211 212void *asan_malloc(uptr size, BufferedStackTrace *stack); 213void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack); 214void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack); 215void *asan_reallocarray(void *p, uptr nmemb, uptr size, 216 BufferedStackTrace *stack); 217void *asan_valloc(uptr size, BufferedStackTrace *stack); 218void *asan_pvalloc(uptr size, BufferedStackTrace *stack); 219 220void *asan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack); 221int asan_posix_memalign(void **memptr, uptr alignment, uptr size, 222 BufferedStackTrace *stack); 223uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp); 224 225uptr asan_mz_size(const void *ptr); 226void asan_mz_force_lock(); 227void asan_mz_force_unlock(); 228 229void PrintInternalAllocatorStats(); 230void AsanSoftRssLimitExceededCallback(bool exceeded); 231 232} // namespace __asan 233#endif // ASAN_ALLOCATOR_H 234