Path: blob/main/contrib/llvm-project/compiler-rt/lib/lsan/lsan_allocator.h
35233 views
//=-- lsan_allocator.h ----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of LeakSanitizer.9// Allocator for standalone LSan.10//11//===----------------------------------------------------------------------===//1213#ifndef LSAN_ALLOCATOR_H14#define LSAN_ALLOCATOR_H1516#include "sanitizer_common/sanitizer_allocator.h"17#include "sanitizer_common/sanitizer_common.h"18#include "sanitizer_common/sanitizer_internal_defs.h"19#include "lsan_common.h"2021namespace __lsan {2223void *Allocate(const StackTrace &stack, uptr size, uptr alignment,24bool cleared);25void Deallocate(void *p);26void *Reallocate(const StackTrace &stack, void *p, uptr new_size,27uptr alignment);28uptr GetMallocUsableSize(const void *p);2930template<typename Callable>31void ForEachChunk(const Callable &callback);3233void GetAllocatorCacheRange(uptr *begin, uptr *end);34void AllocatorThreadStart();35void AllocatorThreadFinish();36void InitializeAllocator();3738const bool kAlwaysClearMemory = true;3940struct ChunkMetadata {41u8 allocated : 8; // Must be first.42ChunkTag tag : 2;43#if SANITIZER_WORDSIZE == 6444uptr requested_size : 54;45#else46uptr requested_size : 32;47uptr padding : 22;48#endif49u32 stack_trace_id;50};5152#if !SANITIZER_CAN_USE_ALLOCATOR6453template <typename AddressSpaceViewTy>54struct AP32 {55static const uptr kSpaceBeg = 0;56static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;57static const uptr kMetadataSize = sizeof(ChunkMetadata);58typedef __sanitizer::CompactSizeClassMap SizeClassMap;59static const uptr kRegionSizeLog = 20;60using AddressSpaceView = AddressSpaceViewTy;61typedef NoOpMapUnmapCallback MapUnmapCallback;62static const uptr kFlags = 0;63};64template <typename AddressSpaceView>65using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>;66using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;67#else68# if SANITIZER_FUCHSIA || defined(__powerpc64__)69const uptr kAllocatorSpace = ~(uptr)0;70# if SANITIZER_RISCV6471// See the comments in compiler-rt/lib/asan/asan_allocator.h for why these72// values were chosen.73const uptr kAllocatorSize = UINT64_C(1) << 33; // 8GB74using LSanSizeClassMap = SizeClassMap</*kNumBits=*/2,75/*kMinSizeLog=*/5,76/*kMidSizeLog=*/8,77/*kMaxSizeLog=*/18,78/*kNumCachedHintT=*/8,79/*kMaxBytesCachedLog=*/10>;80static_assert(LSanSizeClassMap::kNumClassesRounded <= 32,81"32 size classes is the optimal number to ensure tests run "82"effieciently on Fuchsia.");83# else84const uptr kAllocatorSize = 0x40000000000ULL; // 4T.85using LSanSizeClassMap = DefaultSizeClassMap;86# endif87# elif SANITIZER_RISCV6488const uptr kAllocatorSpace = ~(uptr)0;89const uptr kAllocatorSize = 0x2000000000ULL; // 128G.90using LSanSizeClassMap = DefaultSizeClassMap;91# elif SANITIZER_APPLE92const uptr kAllocatorSpace = 0x600000000000ULL;93const uptr kAllocatorSize = 0x40000000000ULL; // 4T.94using LSanSizeClassMap = DefaultSizeClassMap;95# else96const uptr kAllocatorSpace = 0x500000000000ULL;97const uptr kAllocatorSize = 0x40000000000ULL; // 4T.98using LSanSizeClassMap = DefaultSizeClassMap;99# endif100template <typename AddressSpaceViewTy>101struct AP64 { // Allocator64 parameters. Deliberately using a short name.102static const uptr kSpaceBeg = kAllocatorSpace;103static const uptr kSpaceSize = kAllocatorSize;104static const uptr kMetadataSize = sizeof(ChunkMetadata);105using SizeClassMap = LSanSizeClassMap;106typedef NoOpMapUnmapCallback MapUnmapCallback;107static const uptr kFlags = 0;108using AddressSpaceView = AddressSpaceViewTy;109};110111template <typename AddressSpaceView>112using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;113using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;114#endif115116template <typename AddressSpaceView>117using AllocatorASVT = CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;118using Allocator = AllocatorASVT<LocalAddressSpaceView>;119using AllocatorCache = Allocator::AllocatorCache;120121Allocator::AllocatorCache *GetAllocatorCache();122123int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,124const StackTrace &stack);125void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack);126void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);127void *lsan_malloc(uptr size, const StackTrace &stack);128void lsan_free(void *p);129void *lsan_realloc(void *p, uptr size, const StackTrace &stack);130void *lsan_reallocarray(void *p, uptr nmemb, uptr size,131const StackTrace &stack);132void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack);133void *lsan_valloc(uptr size, const StackTrace &stack);134void *lsan_pvalloc(uptr size, const StackTrace &stack);135uptr lsan_mz_size(const void *p);136137} // namespace __lsan138139#endif // LSAN_ALLOCATOR_H140141142