xref: /freebsd/contrib/llvm-project/compiler-rt/lib/msan/msan_allocator.cpp (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 //===-- msan_allocator.cpp -------------------------- ---------------------===//
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 MemorySanitizer.
10 //
11 // MemorySanitizer allocator.
12 //===----------------------------------------------------------------------===//
13 
14 #include "sanitizer_common/sanitizer_allocator.h"
15 #include "sanitizer_common/sanitizer_allocator_checks.h"
16 #include "sanitizer_common/sanitizer_allocator_interface.h"
17 #include "sanitizer_common/sanitizer_allocator_report.h"
18 #include "sanitizer_common/sanitizer_errno.h"
19 #include "msan.h"
20 #include "msan_allocator.h"
21 #include "msan_origin.h"
22 #include "msan_thread.h"
23 #include "msan_poisoning.h"
24 
25 namespace __msan {
26 
27 struct Metadata {
28   uptr requested_size;
29 };
30 
31 struct MsanMapUnmapCallback {
32   void OnMap(uptr p, uptr size) const {}
33   void OnUnmap(uptr p, uptr size) const {
34     __msan_unpoison((void *)p, size);
35 
36     // We are about to unmap a chunk of user memory.
37     // Mark the corresponding shadow memory as not needed.
38     uptr shadow_p = MEM_TO_SHADOW(p);
39     ReleaseMemoryPagesToOS(shadow_p, shadow_p + size);
40     if (__msan_get_track_origins()) {
41       uptr origin_p = MEM_TO_ORIGIN(p);
42       ReleaseMemoryPagesToOS(origin_p, origin_p + size);
43     }
44   }
45 };
46 
47 #if defined(__mips64)
48 static const uptr kMaxAllowedMallocSize = 2UL << 30;
49 
50 struct AP32 {
51   static const uptr kSpaceBeg = 0;
52   static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
53   static const uptr kMetadataSize = sizeof(Metadata);
54   typedef __sanitizer::CompactSizeClassMap SizeClassMap;
55   static const uptr kRegionSizeLog = 20;
56   using AddressSpaceView = LocalAddressSpaceView;
57   typedef MsanMapUnmapCallback MapUnmapCallback;
58   static const uptr kFlags = 0;
59 };
60 typedef SizeClassAllocator32<AP32> PrimaryAllocator;
61 #elif defined(__x86_64__)
62 #if SANITIZER_NETBSD || \
63     (SANITIZER_LINUX && !defined(MSAN_LINUX_X86_64_OLD_MAPPING))
64 static const uptr kAllocatorSpace = 0x700000000000ULL;
65 #else
66 static const uptr kAllocatorSpace = 0x600000000000ULL;
67 #endif
68 static const uptr kMaxAllowedMallocSize = 8UL << 30;
69 
70 struct AP64 {  // Allocator64 parameters. Deliberately using a short name.
71   static const uptr kSpaceBeg = kAllocatorSpace;
72   static const uptr kSpaceSize = 0x40000000000;  // 4T.
73   static const uptr kMetadataSize = sizeof(Metadata);
74   typedef DefaultSizeClassMap SizeClassMap;
75   typedef MsanMapUnmapCallback MapUnmapCallback;
76   static const uptr kFlags = 0;
77   using AddressSpaceView = LocalAddressSpaceView;
78 };
79 
80 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
81 
82 #elif defined(__powerpc64__)
83 static const uptr kMaxAllowedMallocSize = 2UL << 30;  // 2G
84 
85 struct AP64 {  // Allocator64 parameters. Deliberately using a short name.
86   static const uptr kSpaceBeg = 0x300000000000;
87   static const uptr kSpaceSize = 0x020000000000;  // 2T.
88   static const uptr kMetadataSize = sizeof(Metadata);
89   typedef DefaultSizeClassMap SizeClassMap;
90   typedef MsanMapUnmapCallback MapUnmapCallback;
91   static const uptr kFlags = 0;
92   using AddressSpaceView = LocalAddressSpaceView;
93 };
94 
95 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
96 #elif defined(__s390x__)
97 static const uptr kMaxAllowedMallocSize = 2UL << 30;  // 2G
98 
99 struct AP64 {  // Allocator64 parameters. Deliberately using a short name.
100   static const uptr kSpaceBeg = 0x440000000000;
101   static const uptr kSpaceSize = 0x020000000000;  // 2T.
102   static const uptr kMetadataSize = sizeof(Metadata);
103   typedef DefaultSizeClassMap SizeClassMap;
104   typedef MsanMapUnmapCallback MapUnmapCallback;
105   static const uptr kFlags = 0;
106   using AddressSpaceView = LocalAddressSpaceView;
107 };
108 
109 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
110 #elif defined(__aarch64__)
111 static const uptr kMaxAllowedMallocSize = 2UL << 30;  // 2G
112 
113 struct AP32 {
114   static const uptr kSpaceBeg = 0;
115   static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
116   static const uptr kMetadataSize = sizeof(Metadata);
117   typedef __sanitizer::CompactSizeClassMap SizeClassMap;
118   static const uptr kRegionSizeLog = 20;
119   using AddressSpaceView = LocalAddressSpaceView;
120   typedef MsanMapUnmapCallback MapUnmapCallback;
121   static const uptr kFlags = 0;
122 };
123 typedef SizeClassAllocator32<AP32> PrimaryAllocator;
124 #endif
125 typedef CombinedAllocator<PrimaryAllocator> Allocator;
126 typedef Allocator::AllocatorCache AllocatorCache;
127 
128 static Allocator allocator;
129 static AllocatorCache fallback_allocator_cache;
130 static StaticSpinMutex fallback_mutex;
131 
132 static uptr max_malloc_size;
133 
134 void MsanAllocatorInit() {
135   SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
136   allocator.Init(common_flags()->allocator_release_to_os_interval_ms);
137   if (common_flags()->max_allocation_size_mb)
138     max_malloc_size = Min(common_flags()->max_allocation_size_mb << 20,
139                           kMaxAllowedMallocSize);
140   else
141     max_malloc_size = kMaxAllowedMallocSize;
142 }
143 
144 AllocatorCache *GetAllocatorCache(MsanThreadLocalMallocStorage *ms) {
145   CHECK(ms);
146   CHECK_LE(sizeof(AllocatorCache), sizeof(ms->allocator_cache));
147   return reinterpret_cast<AllocatorCache *>(ms->allocator_cache);
148 }
149 
150 void MsanThreadLocalMallocStorage::CommitBack() {
151   allocator.SwallowCache(GetAllocatorCache(this));
152 }
153 
154 static void *MsanAllocate(StackTrace *stack, uptr size, uptr alignment,
155                           bool zeroise) {
156   if (size > max_malloc_size) {
157     if (AllocatorMayReturnNull()) {
158       Report("WARNING: MemorySanitizer failed to allocate 0x%zx bytes\n", size);
159       return nullptr;
160     }
161     ReportAllocationSizeTooBig(size, max_malloc_size, stack);
162   }
163   if (UNLIKELY(IsRssLimitExceeded())) {
164     if (AllocatorMayReturnNull())
165       return nullptr;
166     ReportRssLimitExceeded(stack);
167   }
168   MsanThread *t = GetCurrentThread();
169   void *allocated;
170   if (t) {
171     AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
172     allocated = allocator.Allocate(cache, size, alignment);
173   } else {
174     SpinMutexLock l(&fallback_mutex);
175     AllocatorCache *cache = &fallback_allocator_cache;
176     allocated = allocator.Allocate(cache, size, alignment);
177   }
178   if (UNLIKELY(!allocated)) {
179     SetAllocatorOutOfMemory();
180     if (AllocatorMayReturnNull())
181       return nullptr;
182     ReportOutOfMemory(size, stack);
183   }
184   Metadata *meta =
185       reinterpret_cast<Metadata *>(allocator.GetMetaData(allocated));
186   meta->requested_size = size;
187   if (zeroise) {
188     __msan_clear_and_unpoison(allocated, size);
189   } else if (flags()->poison_in_malloc) {
190     __msan_poison(allocated, size);
191     if (__msan_get_track_origins()) {
192       stack->tag = StackTrace::TAG_ALLOC;
193       Origin o = Origin::CreateHeapOrigin(stack);
194       __msan_set_origin(allocated, size, o.raw_id());
195     }
196   }
197   MSAN_MALLOC_HOOK(allocated, size);
198   return allocated;
199 }
200 
201 void MsanDeallocate(StackTrace *stack, void *p) {
202   CHECK(p);
203   MSAN_FREE_HOOK(p);
204   Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(p));
205   uptr size = meta->requested_size;
206   meta->requested_size = 0;
207   // This memory will not be reused by anyone else, so we are free to keep it
208   // poisoned.
209   if (flags()->poison_in_free) {
210     __msan_poison(p, size);
211     if (__msan_get_track_origins()) {
212       stack->tag = StackTrace::TAG_DEALLOC;
213       Origin o = Origin::CreateHeapOrigin(stack);
214       __msan_set_origin(p, size, o.raw_id());
215     }
216   }
217   MsanThread *t = GetCurrentThread();
218   if (t) {
219     AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
220     allocator.Deallocate(cache, p);
221   } else {
222     SpinMutexLock l(&fallback_mutex);
223     AllocatorCache *cache = &fallback_allocator_cache;
224     allocator.Deallocate(cache, p);
225   }
226 }
227 
228 static void *MsanReallocate(StackTrace *stack, void *old_p, uptr new_size,
229                             uptr alignment) {
230   Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(old_p));
231   uptr old_size = meta->requested_size;
232   uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p);
233   if (new_size <= actually_allocated_size) {
234     // We are not reallocating here.
235     meta->requested_size = new_size;
236     if (new_size > old_size) {
237       if (flags()->poison_in_malloc) {
238         stack->tag = StackTrace::TAG_ALLOC;
239         PoisonMemory((char *)old_p + old_size, new_size - old_size, stack);
240       }
241     }
242     return old_p;
243   }
244   uptr memcpy_size = Min(new_size, old_size);
245   void *new_p = MsanAllocate(stack, new_size, alignment, false /*zeroise*/);
246   if (new_p) {
247     CopyMemory(new_p, old_p, memcpy_size, stack);
248     MsanDeallocate(stack, old_p);
249   }
250   return new_p;
251 }
252 
253 static void *MsanCalloc(StackTrace *stack, uptr nmemb, uptr size) {
254   if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
255     if (AllocatorMayReturnNull())
256       return nullptr;
257     ReportCallocOverflow(nmemb, size, stack);
258   }
259   return MsanAllocate(stack, nmemb * size, sizeof(u64), true);
260 }
261 
262 static uptr AllocationSize(const void *p) {
263   if (!p) return 0;
264   const void *beg = allocator.GetBlockBegin(p);
265   if (beg != p) return 0;
266   Metadata *b = (Metadata *)allocator.GetMetaData(p);
267   return b->requested_size;
268 }
269 
270 void *msan_malloc(uptr size, StackTrace *stack) {
271   return SetErrnoOnNull(MsanAllocate(stack, size, sizeof(u64), false));
272 }
273 
274 void *msan_calloc(uptr nmemb, uptr size, StackTrace *stack) {
275   return SetErrnoOnNull(MsanCalloc(stack, nmemb, size));
276 }
277 
278 void *msan_realloc(void *ptr, uptr size, StackTrace *stack) {
279   if (!ptr)
280     return SetErrnoOnNull(MsanAllocate(stack, size, sizeof(u64), false));
281   if (size == 0) {
282     MsanDeallocate(stack, ptr);
283     return nullptr;
284   }
285   return SetErrnoOnNull(MsanReallocate(stack, ptr, size, sizeof(u64)));
286 }
287 
288 void *msan_reallocarray(void *ptr, uptr nmemb, uptr size, StackTrace *stack) {
289   if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
290     errno = errno_ENOMEM;
291     if (AllocatorMayReturnNull())
292       return nullptr;
293     ReportReallocArrayOverflow(nmemb, size, stack);
294   }
295   return msan_realloc(ptr, nmemb * size, stack);
296 }
297 
298 void *msan_valloc(uptr size, StackTrace *stack) {
299   return SetErrnoOnNull(MsanAllocate(stack, size, GetPageSizeCached(), false));
300 }
301 
302 void *msan_pvalloc(uptr size, StackTrace *stack) {
303   uptr PageSize = GetPageSizeCached();
304   if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {
305     errno = errno_ENOMEM;
306     if (AllocatorMayReturnNull())
307       return nullptr;
308     ReportPvallocOverflow(size, stack);
309   }
310   // pvalloc(0) should allocate one page.
311   size = size ? RoundUpTo(size, PageSize) : PageSize;
312   return SetErrnoOnNull(MsanAllocate(stack, size, PageSize, false));
313 }
314 
315 void *msan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack) {
316   if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
317     errno = errno_EINVAL;
318     if (AllocatorMayReturnNull())
319       return nullptr;
320     ReportInvalidAlignedAllocAlignment(size, alignment, stack);
321   }
322   return SetErrnoOnNull(MsanAllocate(stack, size, alignment, false));
323 }
324 
325 void *msan_memalign(uptr alignment, uptr size, StackTrace *stack) {
326   if (UNLIKELY(!IsPowerOfTwo(alignment))) {
327     errno = errno_EINVAL;
328     if (AllocatorMayReturnNull())
329       return nullptr;
330     ReportInvalidAllocationAlignment(alignment, stack);
331   }
332   return SetErrnoOnNull(MsanAllocate(stack, size, alignment, false));
333 }
334 
335 int msan_posix_memalign(void **memptr, uptr alignment, uptr size,
336                         StackTrace *stack) {
337   if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {
338     if (AllocatorMayReturnNull())
339       return errno_EINVAL;
340     ReportInvalidPosixMemalignAlignment(alignment, stack);
341   }
342   void *ptr = MsanAllocate(stack, size, alignment, false);
343   if (UNLIKELY(!ptr))
344     // OOM error is already taken care of by MsanAllocate.
345     return errno_ENOMEM;
346   CHECK(IsAligned((uptr)ptr, alignment));
347   *memptr = ptr;
348   return 0;
349 }
350 
351 } // namespace __msan
352 
353 using namespace __msan;
354 
355 uptr __sanitizer_get_current_allocated_bytes() {
356   uptr stats[AllocatorStatCount];
357   allocator.GetStats(stats);
358   return stats[AllocatorStatAllocated];
359 }
360 
361 uptr __sanitizer_get_heap_size() {
362   uptr stats[AllocatorStatCount];
363   allocator.GetStats(stats);
364   return stats[AllocatorStatMapped];
365 }
366 
367 uptr __sanitizer_get_free_bytes() { return 1; }
368 
369 uptr __sanitizer_get_unmapped_bytes() { return 1; }
370 
371 uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
372 
373 int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; }
374 
375 uptr __sanitizer_get_allocated_size(const void *p) { return AllocationSize(p); }
376