xref: /freebsd/contrib/llvm-project/compiler-rt/lib/lsan/lsan_allocator.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 AllocatorThreadStart();
36 void AllocatorThreadFinish();
37 void InitializeAllocator();
38 
39 const bool kAlwaysClearMemory = true;
40 
41 struct ChunkMetadata {
42   u8 allocated : 8;  // Must be first.
43   ChunkTag tag : 2;
44 #if SANITIZER_WORDSIZE == 64
45   uptr requested_size : 54;
46 #else
47   uptr requested_size : 32;
48   uptr padding : 22;
49 #endif
50   u32 stack_trace_id;
51 };
52 
53 #if !SANITIZER_CAN_USE_ALLOCATOR64
54 template <typename AddressSpaceViewTy>
55 struct AP32 {
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;
64 };
65 template <typename AddressSpaceView>
66 using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>;
67 using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
68 #else
69 # if SANITIZER_FUCHSIA || defined(__powerpc64__)
70 const uptr kAllocatorSpace = ~(uptr)0;
71 const uptr kAllocatorSize  =  0x40000000000ULL;  // 4T.
72 #  elif SANITIZER_APPLE
73 const uptr kAllocatorSpace = 0x600000000000ULL;
74 const uptr kAllocatorSize  = 0x40000000000ULL;  // 4T.
75 #  else
76 const uptr kAllocatorSpace = 0x500000000000ULL;
77 const uptr kAllocatorSize = 0x40000000000ULL;  // 4T.
78 #  endif
79 template <typename AddressSpaceViewTy>
80 struct AP64 {  // Allocator64 parameters. Deliberately using a short name.
81   static const uptr kSpaceBeg = kAllocatorSpace;
82   static const uptr kSpaceSize = kAllocatorSize;
83   static const uptr kMetadataSize = sizeof(ChunkMetadata);
84   typedef DefaultSizeClassMap SizeClassMap;
85   typedef NoOpMapUnmapCallback MapUnmapCallback;
86   static const uptr kFlags = 0;
87   using AddressSpaceView = AddressSpaceViewTy;
88 };
89 
90 template <typename AddressSpaceView>
91 using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
92 using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
93 #endif
94 
95 template <typename AddressSpaceView>
96 using AllocatorASVT = CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;
97 using Allocator = AllocatorASVT<LocalAddressSpaceView>;
98 using AllocatorCache = Allocator::AllocatorCache;
99 
100 Allocator::AllocatorCache *GetAllocatorCache();
101 
102 int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,
103                         const StackTrace &stack);
104 void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack);
105 void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
106 void *lsan_malloc(uptr size, const StackTrace &stack);
107 void lsan_free(void *p);
108 void *lsan_realloc(void *p, uptr size, const StackTrace &stack);
109 void *lsan_reallocarray(void *p, uptr nmemb, uptr size,
110                         const StackTrace &stack);
111 void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack);
112 void *lsan_valloc(uptr size, const StackTrace &stack);
113 void *lsan_pvalloc(uptr size, const StackTrace &stack);
114 uptr lsan_mz_size(const void *p);
115 
116 }  // namespace __lsan
117 
118 #endif  // LSAN_ALLOCATOR_H
119