xref: /openbsd-src/gnu/llvm/compiler-rt/lib/lsan/lsan_allocator.h (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick //=-- lsan_allocator.h ----------------------------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is a part of LeakSanitizer.
103cab2bb3Spatrick // Allocator for standalone LSan.
113cab2bb3Spatrick //
123cab2bb3Spatrick //===----------------------------------------------------------------------===//
133cab2bb3Spatrick 
143cab2bb3Spatrick #ifndef LSAN_ALLOCATOR_H
153cab2bb3Spatrick #define LSAN_ALLOCATOR_H
163cab2bb3Spatrick 
173cab2bb3Spatrick #include "sanitizer_common/sanitizer_allocator.h"
183cab2bb3Spatrick #include "sanitizer_common/sanitizer_common.h"
193cab2bb3Spatrick #include "sanitizer_common/sanitizer_internal_defs.h"
203cab2bb3Spatrick #include "lsan_common.h"
213cab2bb3Spatrick 
223cab2bb3Spatrick namespace __lsan {
233cab2bb3Spatrick 
243cab2bb3Spatrick void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
253cab2bb3Spatrick                bool cleared);
263cab2bb3Spatrick void Deallocate(void *p);
273cab2bb3Spatrick void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
283cab2bb3Spatrick                  uptr alignment);
293cab2bb3Spatrick uptr GetMallocUsableSize(const void *p);
303cab2bb3Spatrick 
313cab2bb3Spatrick template<typename Callable>
323cab2bb3Spatrick void ForEachChunk(const Callable &callback);
333cab2bb3Spatrick 
343cab2bb3Spatrick void GetAllocatorCacheRange(uptr *begin, uptr *end);
353cab2bb3Spatrick void AllocatorThreadFinish();
363cab2bb3Spatrick void InitializeAllocator();
373cab2bb3Spatrick 
383cab2bb3Spatrick const bool kAlwaysClearMemory = true;
393cab2bb3Spatrick 
403cab2bb3Spatrick struct ChunkMetadata {
413cab2bb3Spatrick   u8 allocated : 8;  // Must be first.
423cab2bb3Spatrick   ChunkTag tag : 2;
433cab2bb3Spatrick #if SANITIZER_WORDSIZE == 64
443cab2bb3Spatrick   uptr requested_size : 54;
453cab2bb3Spatrick #else
463cab2bb3Spatrick   uptr requested_size : 32;
473cab2bb3Spatrick   uptr padding : 22;
483cab2bb3Spatrick #endif
493cab2bb3Spatrick   u32 stack_trace_id;
503cab2bb3Spatrick };
513cab2bb3Spatrick 
52*810390e3Srobert #if !SANITIZER_CAN_USE_ALLOCATOR64
533cab2bb3Spatrick template <typename AddressSpaceViewTy>
543cab2bb3Spatrick struct AP32 {
553cab2bb3Spatrick   static const uptr kSpaceBeg = 0;
563cab2bb3Spatrick   static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
573cab2bb3Spatrick   static const uptr kMetadataSize = sizeof(ChunkMetadata);
583cab2bb3Spatrick   typedef __sanitizer::CompactSizeClassMap SizeClassMap;
593cab2bb3Spatrick   static const uptr kRegionSizeLog = 20;
603cab2bb3Spatrick   using AddressSpaceView = AddressSpaceViewTy;
613cab2bb3Spatrick   typedef NoOpMapUnmapCallback MapUnmapCallback;
623cab2bb3Spatrick   static const uptr kFlags = 0;
633cab2bb3Spatrick };
643cab2bb3Spatrick template <typename AddressSpaceView>
653cab2bb3Spatrick using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>;
663cab2bb3Spatrick using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
67*810390e3Srobert #else
68*810390e3Srobert # if SANITIZER_FUCHSIA || defined(__powerpc64__)
691f9cb04fSpatrick const uptr kAllocatorSpace = ~(uptr)0;
701f9cb04fSpatrick const uptr kAllocatorSize  =  0x40000000000ULL;  // 4T.
711f9cb04fSpatrick #elif defined(__s390x__)
721f9cb04fSpatrick const uptr kAllocatorSpace = 0x40000000000ULL;
731f9cb04fSpatrick const uptr kAllocatorSize = 0x40000000000ULL;  // 4T.
743cab2bb3Spatrick # else
753cab2bb3Spatrick const uptr kAllocatorSpace = 0x600000000000ULL;
763cab2bb3Spatrick const uptr kAllocatorSize  = 0x40000000000ULL;  // 4T.
773cab2bb3Spatrick # endif
783cab2bb3Spatrick template <typename AddressSpaceViewTy>
793cab2bb3Spatrick struct AP64 {  // Allocator64 parameters. Deliberately using a short name.
803cab2bb3Spatrick   static const uptr kSpaceBeg = kAllocatorSpace;
813cab2bb3Spatrick   static const uptr kSpaceSize = kAllocatorSize;
823cab2bb3Spatrick   static const uptr kMetadataSize = sizeof(ChunkMetadata);
833cab2bb3Spatrick   typedef DefaultSizeClassMap SizeClassMap;
843cab2bb3Spatrick   typedef NoOpMapUnmapCallback MapUnmapCallback;
853cab2bb3Spatrick   static const uptr kFlags = 0;
863cab2bb3Spatrick   using AddressSpaceView = AddressSpaceViewTy;
873cab2bb3Spatrick };
883cab2bb3Spatrick 
893cab2bb3Spatrick template <typename AddressSpaceView>
903cab2bb3Spatrick using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
913cab2bb3Spatrick using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
923cab2bb3Spatrick #endif
933cab2bb3Spatrick 
943cab2bb3Spatrick template <typename AddressSpaceView>
953cab2bb3Spatrick using AllocatorASVT = CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;
963cab2bb3Spatrick using Allocator = AllocatorASVT<LocalAddressSpaceView>;
973cab2bb3Spatrick using AllocatorCache = Allocator::AllocatorCache;
983cab2bb3Spatrick 
993cab2bb3Spatrick Allocator::AllocatorCache *GetAllocatorCache();
1003cab2bb3Spatrick 
1013cab2bb3Spatrick int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,
1023cab2bb3Spatrick                         const StackTrace &stack);
1033cab2bb3Spatrick void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack);
1043cab2bb3Spatrick void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
1053cab2bb3Spatrick void *lsan_malloc(uptr size, const StackTrace &stack);
1063cab2bb3Spatrick void lsan_free(void *p);
1073cab2bb3Spatrick void *lsan_realloc(void *p, uptr size, const StackTrace &stack);
1083cab2bb3Spatrick void *lsan_reallocarray(void *p, uptr nmemb, uptr size,
1093cab2bb3Spatrick                         const StackTrace &stack);
1103cab2bb3Spatrick void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack);
1113cab2bb3Spatrick void *lsan_valloc(uptr size, const StackTrace &stack);
1123cab2bb3Spatrick void *lsan_pvalloc(uptr size, const StackTrace &stack);
1133cab2bb3Spatrick uptr lsan_mz_size(const void *p);
1143cab2bb3Spatrick 
1153cab2bb3Spatrick }  // namespace __lsan
1163cab2bb3Spatrick 
1173cab2bb3Spatrick #endif  // LSAN_ALLOCATOR_H
118