asan_allocator.h revision 360784
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 23namespace __asan { 24 25enum 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 31struct AsanChunk; 32 33struct 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 46void InitializeAllocator(const AllocatorOptions &options); 47void ReInitializeAllocator(const AllocatorOptions &options); 48void GetAllocatorOptions(AllocatorOptions *options); 49 50class 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 96AsanChunkView FindHeapChunkByAddress(uptr address); 97AsanChunkView FindHeapChunkByAllocBeg(uptr address); 98 99// List of AsanChunks with total size. 100class 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 116struct 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 123const uptr kAllocatorSpace = ~(uptr)0; 124const uptr kAllocatorSize = 0x40000000000ULL; // 4T. 125typedef DefaultSizeClassMap SizeClassMap; 126# elif defined(__powerpc64__) 127const uptr kAllocatorSpace = ~(uptr)0; 128const uptr kAllocatorSize = 0x20000000000ULL; // 2T. 129typedef DefaultSizeClassMap SizeClassMap; 130# elif defined(__aarch64__) && SANITIZER_ANDROID 131// Android needs to support 39, 42 and 48 bit VMA. 132const uptr kAllocatorSpace = ~(uptr)0; 133const uptr kAllocatorSize = 0x2000000000ULL; // 128G. 134typedef 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. 138const uptr kAllocatorSpace = 0x10000000000ULL; 139const uptr kAllocatorSize = 0x10000000000ULL; // 3T. 140typedef DefaultSizeClassMap SizeClassMap; 141#elif defined(__sparc__) 142const uptr kAllocatorSpace = ~(uptr)0; 143const uptr kAllocatorSize = 0x20000000000ULL; // 2T. 144typedef DefaultSizeClassMap SizeClassMap; 145# elif SANITIZER_WINDOWS 146const uptr kAllocatorSpace = ~(uptr)0; 147const uptr kAllocatorSize = 0x8000000000ULL; // 500G 148typedef DefaultSizeClassMap SizeClassMap; 149# else 150const uptr kAllocatorSpace = 0x600000000000ULL; 151const uptr kAllocatorSize = 0x40000000000ULL; // 4T. 152typedef DefaultSizeClassMap SizeClassMap; 153# endif 154template <typename AddressSpaceViewTy> 155struct 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 165template <typename AddressSpaceView> 166using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>; 167using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; 168#else // Fallback to SizeClassAllocator32. 169typedef CompactSizeClassMap SizeClassMap; 170template <typename AddressSpaceViewTy> 171struct 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}; 181template <typename AddressSpaceView> 182using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView> >; 183using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; 184#endif // SANITIZER_CAN_USE_ALLOCATOR64 185 186static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses; 187 188template <typename AddressSpaceView> 189using AsanAllocatorASVT = 190 CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>; 191using AsanAllocator = AsanAllocatorASVT<LocalAddressSpaceView>; 192using AllocatorCache = AsanAllocator::AllocatorCache; 193 194struct 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 203void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack, 204 AllocType alloc_type); 205void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type); 206void asan_delete(void *ptr, uptr size, uptr alignment, 207 BufferedStackTrace *stack, AllocType alloc_type); 208 209void *asan_malloc(uptr size, BufferedStackTrace *stack); 210void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack); 211void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack); 212void *asan_reallocarray(void *p, uptr nmemb, uptr size, 213 BufferedStackTrace *stack); 214void *asan_valloc(uptr size, BufferedStackTrace *stack); 215void *asan_pvalloc(uptr size, BufferedStackTrace *stack); 216 217void *asan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack); 218int asan_posix_memalign(void **memptr, uptr alignment, uptr size, 219 BufferedStackTrace *stack); 220uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp); 221 222uptr asan_mz_size(const void *ptr); 223void asan_mz_force_lock(); 224void asan_mz_force_unlock(); 225 226void PrintInternalAllocatorStats(); 227void AsanSoftRssLimitExceededCallback(bool exceeded); 228 229} // namespace __asan 230#endif // ASAN_ALLOCATOR_H 231