1 //===-- msan_allocator.cc --------------------------- ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of MemorySanitizer.
12 // MemorySanitizer allocator.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_allocator.h"
16 #include "sanitizer_common/sanitizer_stackdepot.h"
25 static const uptr kAllocatorSpace = 0x600000000000ULL;
26 static const uptr kAllocatorSize = 0x80000000000; // 8T.
27 static const uptr kMetadataSize = sizeof(Metadata);
29 typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, kMetadataSize,
30 DefaultSizeClassMap> PrimaryAllocator;
31 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
32 typedef LargeMmapAllocator<> SecondaryAllocator;
33 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
34 SecondaryAllocator> Allocator;
36 static THREADLOCAL AllocatorCache cache;
37 static Allocator allocator;
39 static int inited = 0;
41 static inline void Init() {
44 inited = true; // this must happen before any threads are created.
48 static void *MsanAllocate(StackTrace *stack, uptr size,
49 uptr alignment, bool zeroise) {
51 void *res = allocator.Allocate(&cache, size, alignment, false);
52 Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(res));
53 meta->requested_size = size;
55 __msan_clear_and_unpoison(res, size);
56 else if (flags()->poison_in_malloc)
57 __msan_poison(res, size);
58 if (__msan_get_track_origins()) {
59 u32 stack_id = StackDepotPut(stack->trace, stack->size);
61 CHECK_EQ((stack_id >> 31), 0); // Higher bit is occupied by stack origins.
62 __msan_set_origin(res, size, stack_id);
67 void MsanDeallocate(void *p) {
70 Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(p));
71 uptr size = meta->requested_size;
72 // This memory will not be reused by anyone else, so we are free to keep it
74 __msan_poison(p, size);
75 if (__msan_get_track_origins())
76 __msan_set_origin(p, size, -1);
77 allocator.Deallocate(&cache, p);
80 void *MsanReallocate(StackTrace *stack, void *old_p, uptr new_size,
81 uptr alignment, bool zeroise) {
83 return MsanAllocate(stack, new_size, alignment, zeroise);
85 MsanDeallocate(old_p);
88 Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(old_p));
89 uptr old_size = meta->requested_size;
90 uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p);
91 if (new_size <= actually_allocated_size) {
92 // We are not reallocating here.
93 meta->requested_size = new_size;
94 if (new_size > old_size)
95 __msan_poison((char*)old_p + old_size, new_size - old_size);
98 uptr memcpy_size = Min(new_size, old_size);
99 void *new_p = MsanAllocate(stack, new_size, alignment, zeroise);
100 // Printf("realloc: old_size %zd new_size %zd\n", old_size, new_size);
102 __msan_memcpy(new_p, old_p, memcpy_size);
103 MsanDeallocate(old_p);
107 } // namespace __msan