xref: /freebsd/contrib/llvm-project/compiler-rt/lib/asan/asan_allocator.h (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
1 //===-- asan_allocator.h ----------------------------------------*- C++ -*-===//
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 AddressSanitizer, an address sanity checker.
10 //
11 // ASan-private header for asan_allocator.cpp.
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef ASAN_ALLOCATOR_H
15 #define ASAN_ALLOCATOR_H
16 
17 #include "asan_flags.h"
18 #include "asan_interceptors.h"
19 #include "asan_internal.h"
20 #include "sanitizer_common/sanitizer_allocator.h"
21 #include "sanitizer_common/sanitizer_list.h"
22 #include "sanitizer_common/sanitizer_platform.h"
23 
24 namespace __asan {
25 
26 enum AllocType {
27   FROM_MALLOC = 1,  // Memory block came from malloc, calloc, realloc, etc.
28   FROM_NEW = 2,     // Memory block came from operator new.
29   FROM_NEW_BR = 3   // Memory block came from operator new [ ]
30 };
31 
32 class AsanChunk;
33 
34 struct AllocatorOptions {
35   u32 quarantine_size_mb;
36   u32 thread_local_quarantine_size_kb;
37   u16 min_redzone;
38   u16 max_redzone;
39   u8 may_return_null;
40   u8 alloc_dealloc_mismatch;
41   s32 release_to_os_interval_ms;
42 
43   void SetFrom(const Flags *f, const CommonFlags *cf);
44   void CopyTo(Flags *f, CommonFlags *cf);
45 };
46 
47 void InitializeAllocator(const AllocatorOptions &options);
48 void ReInitializeAllocator(const AllocatorOptions &options);
49 void GetAllocatorOptions(AllocatorOptions *options);
50 
51 class AsanChunkView {
52  public:
53   explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
54   bool IsValid() const;        // Checks if AsanChunkView points to a valid
55                                // allocated or quarantined chunk.
56   bool IsAllocated() const;    // Checks if the memory is currently allocated.
57   bool IsQuarantined() const;  // Checks if the memory is currently quarantined.
58   uptr Beg() const;            // First byte of user memory.
59   uptr End() const;            // Last byte of user memory.
60   uptr UsedSize() const;       // Size requested by the user.
61   u32 UserRequestedAlignment() const;  // Originally requested alignment.
62   uptr AllocTid() const;
63   uptr FreeTid() const;
64   bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
65   u32 GetAllocStackId() const;
66   u32 GetFreeStackId() const;
67   StackTrace GetAllocStack() const;
68   StackTrace GetFreeStack() const;
69   AllocType GetAllocType() const;
70   bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) const {
71     if (addr >= Beg() && (addr + access_size) <= End()) {
72       *offset = addr - Beg();
73       return true;
74     }
75     return false;
76   }
77   bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) const {
78     (void)access_size;
79     if (addr < Beg()) {
80       *offset = Beg() - addr;
81       return true;
82     }
83     return false;
84   }
85   bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) const {
86     if (addr + access_size > End()) {
87       *offset = addr - End();
88       return true;
89     }
90     return false;
91   }
92 
93  private:
94   AsanChunk *const chunk_;
95 };
96 
97 AsanChunkView FindHeapChunkByAddress(uptr address);
98 AsanChunkView FindHeapChunkByAllocBeg(uptr address);
99 
100 // List of AsanChunks with total size.
101 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
102  public:
103   explicit AsanChunkFifoList(LinkerInitialized) { }
104   AsanChunkFifoList() { clear(); }
105   void Push(AsanChunk *n);
106   void PushList(AsanChunkFifoList *q);
107   AsanChunk *Pop();
108   uptr size() { return size_; }
109   void clear() {
110     IntrusiveList<AsanChunk>::clear();
111     size_ = 0;
112   }
113  private:
114   uptr size_;
115 };
116 
117 struct AsanMapUnmapCallback {
118   void OnMap(uptr p, uptr size) const;
119   void OnUnmap(uptr p, uptr size) const;
120 };
121 
122 #if SANITIZER_CAN_USE_ALLOCATOR64
123 # if SANITIZER_FUCHSIA
124 const uptr kAllocatorSpace = ~(uptr)0;
125 const uptr kAllocatorSize  =  0x40000000000ULL;  // 4T.
126 typedef DefaultSizeClassMap SizeClassMap;
127 # elif defined(__powerpc64__)
128 const uptr kAllocatorSpace = ~(uptr)0;
129 const uptr kAllocatorSize  =  0x20000000000ULL;  // 2T.
130 typedef DefaultSizeClassMap SizeClassMap;
131 # elif defined(__aarch64__) && SANITIZER_ANDROID
132 // Android needs to support 39, 42 and 48 bit VMA.
133 const uptr kAllocatorSpace =  ~(uptr)0;
134 const uptr kAllocatorSize  =  0x2000000000ULL;  // 128G.
135 typedef VeryCompactSizeClassMap SizeClassMap;
136 #elif SANITIZER_RISCV64
137 const uptr kAllocatorSpace = ~(uptr)0;
138 const uptr kAllocatorSize = 0x2000000000ULL;  // 128G.
139 typedef VeryDenseSizeClassMap SizeClassMap;
140 # elif defined(__aarch64__)
141 // AArch64/SANITIZER_CAN_USE_ALLOCATOR64 is only for 42-bit VMA
142 // so no need to different values for different VMA.
143 const uptr kAllocatorSpace =  0x10000000000ULL;
144 const uptr kAllocatorSize  =  0x10000000000ULL;  // 3T.
145 typedef DefaultSizeClassMap SizeClassMap;
146 #elif defined(__sparc__)
147 const uptr kAllocatorSpace = ~(uptr)0;
148 const uptr kAllocatorSize = 0x20000000000ULL;  // 2T.
149 typedef DefaultSizeClassMap SizeClassMap;
150 # elif SANITIZER_WINDOWS
151 const uptr kAllocatorSpace = ~(uptr)0;
152 const uptr kAllocatorSize  =  0x8000000000ULL;  // 500G
153 typedef DefaultSizeClassMap SizeClassMap;
154 # else
155 const uptr kAllocatorSpace = 0x600000000000ULL;
156 const uptr kAllocatorSize  =  0x40000000000ULL;  // 4T.
157 typedef DefaultSizeClassMap SizeClassMap;
158 # endif
159 template <typename AddressSpaceViewTy>
160 struct AP64 {  // Allocator64 parameters. Deliberately using a short name.
161   static const uptr kSpaceBeg = kAllocatorSpace;
162   static const uptr kSpaceSize = kAllocatorSize;
163   static const uptr kMetadataSize = 0;
164   typedef __asan::SizeClassMap SizeClassMap;
165   typedef AsanMapUnmapCallback MapUnmapCallback;
166   static const uptr kFlags = 0;
167   using AddressSpaceView = AddressSpaceViewTy;
168 };
169 
170 template <typename AddressSpaceView>
171 using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
172 using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
173 #else  // Fallback to SizeClassAllocator32.
174 typedef CompactSizeClassMap SizeClassMap;
175 template <typename AddressSpaceViewTy>
176 struct AP32 {
177   static const uptr kSpaceBeg = 0;
178   static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
179   static const uptr kMetadataSize = 0;
180   typedef __asan::SizeClassMap SizeClassMap;
181   static const uptr kRegionSizeLog = 20;
182   using AddressSpaceView = AddressSpaceViewTy;
183   typedef AsanMapUnmapCallback MapUnmapCallback;
184   static const uptr kFlags = 0;
185 };
186 template <typename AddressSpaceView>
187 using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView> >;
188 using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
189 #endif  // SANITIZER_CAN_USE_ALLOCATOR64
190 
191 static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
192 
193 template <typename AddressSpaceView>
194 using AsanAllocatorASVT =
195     CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;
196 using AsanAllocator = AsanAllocatorASVT<LocalAddressSpaceView>;
197 using AllocatorCache = AsanAllocator::AllocatorCache;
198 
199 struct AsanThreadLocalMallocStorage {
200   uptr quarantine_cache[16];
201   AllocatorCache allocator_cache;
202   void CommitBack();
203  private:
204   // These objects are allocated via mmap() and are zero-initialized.
205   AsanThreadLocalMallocStorage() {}
206 };
207 
208 void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
209                     AllocType alloc_type);
210 void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
211 void asan_delete(void *ptr, uptr size, uptr alignment,
212                  BufferedStackTrace *stack, AllocType alloc_type);
213 
214 void *asan_malloc(uptr size, BufferedStackTrace *stack);
215 void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
216 void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
217 void *asan_reallocarray(void *p, uptr nmemb, uptr size,
218                         BufferedStackTrace *stack);
219 void *asan_valloc(uptr size, BufferedStackTrace *stack);
220 void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
221 
222 void *asan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack);
223 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
224                         BufferedStackTrace *stack);
225 uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
226 
227 uptr asan_mz_size(const void *ptr);
228 void asan_mz_force_lock();
229 void asan_mz_force_unlock();
230 
231 void PrintInternalAllocatorStats();
232 void AsanSoftRssLimitExceededCallback(bool exceeded);
233 
234 }  // namespace __asan
235 #endif  // ASAN_ALLOCATOR_H
236