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   if (!IsInSymbolizerOrUnwider()) {
198     UnpoisonParam(2);
199     RunMallocHooks(allocated, size);
200   }
201   return allocated;
202 }
203 
204 void MsanDeallocate(StackTrace *stack, void *p) {
205   CHECK(p);
206   if (!IsInSymbolizerOrUnwider()) {
207     UnpoisonParam(1);
208     RunFreeHooks(p);
209   }
210   Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(p));
211   uptr size = meta->requested_size;
212   meta->requested_size = 0;
213   // This memory will not be reused by anyone else, so we are free to keep it
214   // poisoned.
215   if (flags()->poison_in_free) {
216     __msan_poison(p, size);
217     if (__msan_get_track_origins()) {
218       stack->tag = StackTrace::TAG_DEALLOC;
219       Origin o = Origin::CreateHeapOrigin(stack);
220       __msan_set_origin(p, size, o.raw_id());
221     }
222   }
223   MsanThread *t = GetCurrentThread();
224   if (t) {
225     AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
226     allocator.Deallocate(cache, p);
227   } else {
228     SpinMutexLock l(&fallback_mutex);
229     AllocatorCache *cache = &fallback_allocator_cache;
230     allocator.Deallocate(cache, p);
231   }
232 }
233 
234 static void *MsanReallocate(StackTrace *stack, void *old_p, uptr new_size,
235                             uptr alignment) {
236   Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(old_p));
237   uptr old_size = meta->requested_size;
238   uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p);
239   if (new_size <= actually_allocated_size) {
240     // We are not reallocating here.
241     meta->requested_size = new_size;
242     if (new_size > old_size) {
243       if (flags()->poison_in_malloc) {
244         stack->tag = StackTrace::TAG_ALLOC;
245         PoisonMemory((char *)old_p + old_size, new_size - old_size, stack);
246       }
247     }
248     return old_p;
249   }
250   uptr memcpy_size = Min(new_size, old_size);
251   void *new_p = MsanAllocate(stack, new_size, alignment, false /*zeroise*/);
252   if (new_p) {
253     CopyMemory(new_p, old_p, memcpy_size, stack);
254     MsanDeallocate(stack, old_p);
255   }
256   return new_p;
257 }
258 
259 static void *MsanCalloc(StackTrace *stack, uptr nmemb, uptr size) {
260   if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
261     if (AllocatorMayReturnNull())
262       return nullptr;
263     ReportCallocOverflow(nmemb, size, stack);
264   }
265   return MsanAllocate(stack, nmemb * size, sizeof(u64), true);
266 }
267 
268 static uptr AllocationSize(const void *p) {
269   if (!p) return 0;
270   const void *beg = allocator.GetBlockBegin(p);
271   if (beg != p) return 0;
272   Metadata *b = (Metadata *)allocator.GetMetaData(p);
273   return b->requested_size;
274 }
275 
276 void *msan_malloc(uptr size, StackTrace *stack) {
277   return SetErrnoOnNull(MsanAllocate(stack, size, sizeof(u64), false));
278 }
279 
280 void *msan_calloc(uptr nmemb, uptr size, StackTrace *stack) {
281   return SetErrnoOnNull(MsanCalloc(stack, nmemb, size));
282 }
283 
284 void *msan_realloc(void *ptr, uptr size, StackTrace *stack) {
285   if (!ptr)
286     return SetErrnoOnNull(MsanAllocate(stack, size, sizeof(u64), false));
287   if (size == 0) {
288     MsanDeallocate(stack, ptr);
289     return nullptr;
290   }
291   return SetErrnoOnNull(MsanReallocate(stack, ptr, size, sizeof(u64)));
292 }
293 
294 void *msan_reallocarray(void *ptr, uptr nmemb, uptr size, StackTrace *stack) {
295   if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
296     errno = errno_ENOMEM;
297     if (AllocatorMayReturnNull())
298       return nullptr;
299     ReportReallocArrayOverflow(nmemb, size, stack);
300   }
301   return msan_realloc(ptr, nmemb * size, stack);
302 }
303 
304 void *msan_valloc(uptr size, StackTrace *stack) {
305   return SetErrnoOnNull(MsanAllocate(stack, size, GetPageSizeCached(), false));
306 }
307 
308 void *msan_pvalloc(uptr size, StackTrace *stack) {
309   uptr PageSize = GetPageSizeCached();
310   if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {
311     errno = errno_ENOMEM;
312     if (AllocatorMayReturnNull())
313       return nullptr;
314     ReportPvallocOverflow(size, stack);
315   }
316   // pvalloc(0) should allocate one page.
317   size = size ? RoundUpTo(size, PageSize) : PageSize;
318   return SetErrnoOnNull(MsanAllocate(stack, size, PageSize, false));
319 }
320 
321 void *msan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack) {
322   if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
323     errno = errno_EINVAL;
324     if (AllocatorMayReturnNull())
325       return nullptr;
326     ReportInvalidAlignedAllocAlignment(size, alignment, stack);
327   }
328   return SetErrnoOnNull(MsanAllocate(stack, size, alignment, false));
329 }
330 
331 void *msan_memalign(uptr alignment, uptr size, StackTrace *stack) {
332   if (UNLIKELY(!IsPowerOfTwo(alignment))) {
333     errno = errno_EINVAL;
334     if (AllocatorMayReturnNull())
335       return nullptr;
336     ReportInvalidAllocationAlignment(alignment, stack);
337   }
338   return SetErrnoOnNull(MsanAllocate(stack, size, alignment, false));
339 }
340 
341 int msan_posix_memalign(void **memptr, uptr alignment, uptr size,
342                         StackTrace *stack) {
343   if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {
344     if (AllocatorMayReturnNull())
345       return errno_EINVAL;
346     ReportInvalidPosixMemalignAlignment(alignment, stack);
347   }
348   void *ptr = MsanAllocate(stack, size, alignment, false);
349   if (UNLIKELY(!ptr))
350     // OOM error is already taken care of by MsanAllocate.
351     return errno_ENOMEM;
352   CHECK(IsAligned((uptr)ptr, alignment));
353   *memptr = ptr;
354   return 0;
355 }
356 
357 } // namespace __msan
358 
359 using namespace __msan;
360 
361 uptr __sanitizer_get_current_allocated_bytes() {
362   uptr stats[AllocatorStatCount];
363   allocator.GetStats(stats);
364   return stats[AllocatorStatAllocated];
365 }
366 
367 uptr __sanitizer_get_heap_size() {
368   uptr stats[AllocatorStatCount];
369   allocator.GetStats(stats);
370   return stats[AllocatorStatMapped];
371 }
372 
373 uptr __sanitizer_get_free_bytes() { return 1; }
374 
375 uptr __sanitizer_get_unmapped_bytes() { return 1; }
376 
377 uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
378 
379 int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; }
380 
381 uptr __sanitizer_get_allocated_size(const void *p) { return AllocationSize(p); }
382