147 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of LeakSanitizer.10// Allocator for standalone LSan.11//12//===----------------------------------------------------------------------===//13 14#ifndef LSAN_ALLOCATOR_H15#define LSAN_ALLOCATOR_H16 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 22namespace __lsan {23 24void *Allocate(const StackTrace &stack, uptr size, uptr alignment,25 bool cleared);26void Deallocate(void *p);27void *Reallocate(const StackTrace &stack, void *p, uptr new_size,28 uptr alignment);29uptr GetMallocUsableSize(const void *p);30 31template<typename Callable>32void ForEachChunk(const Callable &callback);33 34void GetAllocatorCacheRange(uptr *begin, uptr *end);35void AllocatorThreadStart();36void AllocatorThreadFinish();37void InitializeAllocator();38 39const bool kAlwaysClearMemory = true;40 41struct ChunkMetadata {42 u8 allocated : 8; // Must be first.43 ChunkTag tag : 2;44#if SANITIZER_WORDSIZE == 6445 uptr requested_size : 54;46#else47 uptr requested_size : 32;48 uptr padding : 22;49#endif50 u32 stack_trace_id;51};52 53#if !SANITIZER_CAN_USE_ALLOCATOR6454template <typename AddressSpaceViewTy>55struct AP32 {56 static const uptr kSpaceBeg = SANITIZER_MMAP_BEGIN;57 static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;58 static const uptr kMetadataSize = sizeof(ChunkMetadata);59 typedef __sanitizer::CompactSizeClassMap SizeClassMap;60 static const uptr kRegionSizeLog = 20;61 using AddressSpaceView = AddressSpaceViewTy;62 typedef NoOpMapUnmapCallback MapUnmapCallback;63 static const uptr kFlags = 0;64};65template <typename AddressSpaceView>66using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>;67using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;68#else69# if SANITIZER_FUCHSIA || defined(__powerpc64__)70const uptr kAllocatorSpace = ~(uptr)0;71# if SANITIZER_RISCV6472// See the comments in compiler-rt/lib/asan/asan_allocator.h for why these73// values were chosen.74const uptr kAllocatorSize = UINT64_C(1) << 33; // 8GB75using LSanSizeClassMap = SizeClassMap</*kNumBits=*/2,76 /*kMinSizeLog=*/5,77 /*kMidSizeLog=*/8,78 /*kMaxSizeLog=*/18,79 /*kNumCachedHintT=*/8,80 /*kMaxBytesCachedLog=*/10>;81static_assert(LSanSizeClassMap::kNumClassesRounded <= 32,82 "32 size classes is the optimal number to ensure tests run "83 "effieciently on Fuchsia.");84# else85const uptr kAllocatorSize = 0x40000000000ULL; // 4T.86using LSanSizeClassMap = DefaultSizeClassMap;87# endif88# elif SANITIZER_RISCV6489const uptr kAllocatorSpace = ~(uptr)0;90const uptr kAllocatorSize = 0x2000000000ULL; // 128G.91using LSanSizeClassMap = DefaultSizeClassMap;92# elif SANITIZER_APPLE93const uptr kAllocatorSpace = 0x600000000000ULL;94const uptr kAllocatorSize = 0x40000000000ULL; // 4T.95using LSanSizeClassMap = DefaultSizeClassMap;96# elif SANITIZER_ANDROID && defined(__aarch64__)97const uptr kAllocatorSpace = 0x3000000000ULL;98const uptr kAllocatorSize = 0x2000000000ULL;99using LSanSizeClassMap = VeryCompactSizeClassMap;100# else101const uptr kAllocatorSpace = 0x500000000000ULL;102const uptr kAllocatorSize = 0x40000000000ULL; // 4T.103using LSanSizeClassMap = DefaultSizeClassMap;104# endif105template <typename AddressSpaceViewTy>106struct AP64 { // Allocator64 parameters. Deliberately using a short name.107 static const uptr kSpaceBeg = kAllocatorSpace;108 static const uptr kSpaceSize = kAllocatorSize;109 static const uptr kMetadataSize = sizeof(ChunkMetadata);110 using SizeClassMap = LSanSizeClassMap;111 typedef NoOpMapUnmapCallback MapUnmapCallback;112 static const uptr kFlags = 0;113 using AddressSpaceView = AddressSpaceViewTy;114};115 116template <typename AddressSpaceView>117using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;118using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;119#endif120 121template <typename AddressSpaceView>122using AllocatorASVT = CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;123using Allocator = AllocatorASVT<LocalAddressSpaceView>;124using AllocatorCache = Allocator::AllocatorCache;125 126Allocator::AllocatorCache *GetAllocatorCache();127 128int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,129 const StackTrace &stack);130void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack);131void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);132void *lsan_malloc(uptr size, const StackTrace &stack);133void lsan_free(void *p);134void lsan_free_sized(void *p, uptr size);135void lsan_free_aligned_sized(void *p, uptr alignment, uptr size);136void *lsan_realloc(void *p, uptr size, const StackTrace &stack);137void *lsan_reallocarray(void *p, uptr nmemb, uptr size,138 const StackTrace &stack);139void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack);140void *lsan_valloc(uptr size, const StackTrace &stack);141void *lsan_pvalloc(uptr size, const StackTrace &stack);142uptr lsan_mz_size(const void *p);143 144} // namespace __lsan145 146#endif // LSAN_ALLOCATOR_H147