1 //=-- lsan_allocator.h ----------------------------------------------------===// 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 LeakSanitizer. 10 // Allocator for standalone LSan. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LSAN_ALLOCATOR_H 15 #define LSAN_ALLOCATOR_H 16 17 #include "sanitizer_common/sanitizer_allocator.h" 18 #include "sanitizer_common/sanitizer_common.h" 19 #include "sanitizer_common/sanitizer_internal_defs.h" 20 #include "lsan_common.h" 21 22 namespace __lsan { 23 24 void *Allocate(const StackTrace &stack, uptr size, uptr alignment, 25 bool cleared); 26 void Deallocate(void *p); 27 void *Reallocate(const StackTrace &stack, void *p, uptr new_size, 28 uptr alignment); 29 uptr GetMallocUsableSize(const void *p); 30 31 template<typename Callable> 32 void ForEachChunk(const Callable &callback); 33 34 void GetAllocatorCacheRange(uptr *begin, uptr *end); 35 void AllocatorThreadFinish(); 36 void InitializeAllocator(); 37 38 const bool kAlwaysClearMemory = true; 39 40 struct ChunkMetadata { 41 u8 allocated : 8; // Must be first. 42 ChunkTag tag : 2; 43 #if SANITIZER_WORDSIZE == 64 44 uptr requested_size : 54; 45 #else 46 uptr requested_size : 32; 47 uptr padding : 22; 48 #endif 49 u32 stack_trace_id; 50 }; 51 52 #if defined(__mips64) || defined(__aarch64__) || defined(__i386__) || \ 53 defined(__arm__) 54 static const uptr kRegionSizeLog = 20; 55 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog; 56 template <typename AddressSpaceView> 57 using ByteMapASVT = 58 TwoLevelByteMap<(kNumRegions >> 12), 1 << 12, AddressSpaceView>; 59 60 template <typename AddressSpaceViewTy> 61 struct AP32 { 62 static const uptr kSpaceBeg = 0; 63 static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; 64 static const uptr kMetadataSize = sizeof(ChunkMetadata); 65 typedef __sanitizer::CompactSizeClassMap SizeClassMap; 66 static const uptr kRegionSizeLog = __lsan::kRegionSizeLog; 67 using AddressSpaceView = AddressSpaceViewTy; 68 using ByteMap = __lsan::ByteMapASVT<AddressSpaceView>; 69 typedef NoOpMapUnmapCallback MapUnmapCallback; 70 static const uptr kFlags = 0; 71 }; 72 template <typename AddressSpaceView> 73 using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>; 74 using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; 75 #elif defined(__x86_64__) || defined(__powerpc64__) 76 # if defined(__powerpc64__) 77 const uptr kAllocatorSpace = 0xa0000000000ULL; 78 const uptr kAllocatorSize = 0x20000000000ULL; // 2T. 79 # else 80 const uptr kAllocatorSpace = 0x600000000000ULL; 81 const uptr kAllocatorSize = 0x40000000000ULL; // 4T. 82 # endif 83 template <typename AddressSpaceViewTy> 84 struct AP64 { // Allocator64 parameters. Deliberately using a short name. 85 static const uptr kSpaceBeg = kAllocatorSpace; 86 static const uptr kSpaceSize = kAllocatorSize; 87 static const uptr kMetadataSize = sizeof(ChunkMetadata); 88 typedef DefaultSizeClassMap SizeClassMap; 89 typedef NoOpMapUnmapCallback MapUnmapCallback; 90 static const uptr kFlags = 0; 91 using AddressSpaceView = AddressSpaceViewTy; 92 }; 93 94 template <typename AddressSpaceView> 95 using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>; 96 using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>; 97 #endif 98 99 template <typename AddressSpaceView> 100 using AllocatorCacheASVT = 101 SizeClassAllocatorLocalCache<PrimaryAllocatorASVT<AddressSpaceView>>; 102 using AllocatorCache = AllocatorCacheASVT<LocalAddressSpaceView>; 103 104 template <typename AddressSpaceView> 105 using SecondaryAllocatorASVT = 106 LargeMmapAllocator<NoOpMapUnmapCallback, DefaultLargeMmapAllocatorPtrArray, 107 AddressSpaceView>; 108 109 template <typename AddressSpaceView> 110 using AllocatorASVT = 111 CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>, 112 AllocatorCacheASVT<AddressSpaceView>, 113 SecondaryAllocatorASVT<AddressSpaceView>, 114 AddressSpaceView>; 115 using Allocator = AllocatorASVT<LocalAddressSpaceView>; 116 117 AllocatorCache *GetAllocatorCache(); 118 119 int lsan_posix_memalign(void **memptr, uptr alignment, uptr size, 120 const StackTrace &stack); 121 void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack); 122 void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack); 123 void *lsan_malloc(uptr size, const StackTrace &stack); 124 void lsan_free(void *p); 125 void *lsan_realloc(void *p, uptr size, const StackTrace &stack); 126 void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack); 127 void *lsan_valloc(uptr size, const StackTrace &stack); 128 void *lsan_pvalloc(uptr size, const StackTrace &stack); 129 uptr lsan_mz_size(const void *p); 130 131 } // namespace __lsan 132 133 #endif // LSAN_ALLOCATOR_H 134