1 //=-- lsan_allocator.h ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of LeakSanitizer.
10 // Allocator for standalone LSan.
12 //===----------------------------------------------------------------------===//
14 #ifndef LSAN_ALLOCATOR_H
15 #define LSAN_ALLOCATOR_H
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"
24 void *Allocate(const StackTrace
&stack
, uptr size
, uptr alignment
,
26 void Deallocate(void *p
);
27 void *Reallocate(const StackTrace
&stack
, void *p
, uptr new_size
,
29 uptr
GetMallocUsableSize(const void *p
);
31 template<typename Callable
>
32 void ForEachChunk(const Callable
&callback
);
34 void GetAllocatorCacheRange(uptr
*begin
, uptr
*end
);
35 void AllocatorThreadStart();
36 void AllocatorThreadFinish();
37 void InitializeAllocator();
39 const bool kAlwaysClearMemory
= true;
41 struct ChunkMetadata
{
42 u8 allocated
: 8; // Must be first.
44 #if SANITIZER_WORDSIZE == 64
45 uptr requested_size
: 54;
47 uptr requested_size
: 32;
53 #if !SANITIZER_CAN_USE_ALLOCATOR64
54 template <typename AddressSpaceViewTy
>
56 static const uptr kSpaceBeg
= 0;
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;
65 template <typename AddressSpaceView
>
66 using PrimaryAllocatorASVT
= SizeClassAllocator32
<AP32
<AddressSpaceView
>>;
67 using PrimaryAllocator
= PrimaryAllocatorASVT
<LocalAddressSpaceView
>;
69 # if SANITIZER_FUCHSIA || defined(__powerpc64__)
70 const uptr kAllocatorSpace
= ~(uptr
)0;
71 # if SANITIZER_RISCV64
72 // See the comments in compiler-rt/lib/asan/asan_allocator.h for why these
73 // values were chosen.
74 const uptr kAllocatorSize
= UINT64_C(1) << 33; // 8GB
75 using LSanSizeClassMap
= SizeClassMap
</*kNumBits=*/2,
79 /*kNumCachedHintT=*/8,
80 /*kMaxBytesCachedLog=*/10>;
81 static_assert(LSanSizeClassMap::kNumClassesRounded
<= 32,
82 "32 size classes is the optimal number to ensure tests run "
83 "effieciently on Fuchsia.");
85 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
86 using LSanSizeClassMap
= DefaultSizeClassMap
;
88 # elif SANITIZER_RISCV64
89 const uptr kAllocatorSpace
= ~(uptr
)0;
90 const uptr kAllocatorSize
= 0x2000000000ULL
; // 128G.
91 using LSanSizeClassMap
= DefaultSizeClassMap
;
92 # elif SANITIZER_APPLE
93 const uptr kAllocatorSpace
= 0x600000000000ULL
;
94 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
95 using LSanSizeClassMap
= DefaultSizeClassMap
;
97 const uptr kAllocatorSpace
= 0x500000000000ULL
;
98 const uptr kAllocatorSize
= 0x40000000000ULL
; // 4T.
99 using LSanSizeClassMap
= DefaultSizeClassMap
;
101 template <typename AddressSpaceViewTy
>
102 struct AP64
{ // Allocator64 parameters. Deliberately using a short name.
103 static const uptr kSpaceBeg
= kAllocatorSpace
;
104 static const uptr kSpaceSize
= kAllocatorSize
;
105 static const uptr kMetadataSize
= sizeof(ChunkMetadata
);
106 using SizeClassMap
= LSanSizeClassMap
;
107 typedef NoOpMapUnmapCallback MapUnmapCallback
;
108 static const uptr kFlags
= 0;
109 using AddressSpaceView
= AddressSpaceViewTy
;
112 template <typename AddressSpaceView
>
113 using PrimaryAllocatorASVT
= SizeClassAllocator64
<AP64
<AddressSpaceView
>>;
114 using PrimaryAllocator
= PrimaryAllocatorASVT
<LocalAddressSpaceView
>;
117 template <typename AddressSpaceView
>
118 using AllocatorASVT
= CombinedAllocator
<PrimaryAllocatorASVT
<AddressSpaceView
>>;
119 using Allocator
= AllocatorASVT
<LocalAddressSpaceView
>;
120 using AllocatorCache
= Allocator::AllocatorCache
;
122 Allocator::AllocatorCache
*GetAllocatorCache();
124 int lsan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
125 const StackTrace
&stack
);
126 void *lsan_aligned_alloc(uptr alignment
, uptr size
, const StackTrace
&stack
);
127 void *lsan_memalign(uptr alignment
, uptr size
, const StackTrace
&stack
);
128 void *lsan_malloc(uptr size
, const StackTrace
&stack
);
129 void lsan_free(void *p
);
130 void *lsan_realloc(void *p
, uptr size
, const StackTrace
&stack
);
131 void *lsan_reallocarray(void *p
, uptr nmemb
, uptr size
,
132 const StackTrace
&stack
);
133 void *lsan_calloc(uptr nmemb
, uptr size
, const StackTrace
&stack
);
134 void *lsan_valloc(uptr size
, const StackTrace
&stack
);
135 void *lsan_pvalloc(uptr size
, const StackTrace
&stack
);
136 uptr
lsan_mz_size(const void *p
);
138 } // namespace __lsan
140 #endif // LSAN_ALLOCATOR_H