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 //===----------------------------------------------------------------------===//
16 #include "msan_allocator.h"
17 #include "msan_origin.h"
18 #include "msan_thread.h"
19 #include "msan_poisoning.h"
23 static Allocator allocator;
24 static AllocatorCache fallback_allocator_cache;
25 static SpinMutex fallback_mutex;
27 Allocator &get_allocator() { return allocator; }
29 void MsanAllocatorInit() {
31 common_flags()->allocator_may_return_null,
32 common_flags()->allocator_release_to_os_interval_ms);
35 AllocatorCache *GetAllocatorCache(MsanThreadLocalMallocStorage *ms) {
37 CHECK_LE(sizeof(AllocatorCache), sizeof(ms->allocator_cache));
38 return reinterpret_cast<AllocatorCache *>(ms->allocator_cache);
41 void MsanThreadLocalMallocStorage::CommitBack() {
42 allocator.SwallowCache(GetAllocatorCache(this));
45 static void *MsanAllocate(StackTrace *stack, uptr size, uptr alignment,
47 if (size > kMaxAllowedMallocSize) {
48 Report("WARNING: MemorySanitizer failed to allocate %p bytes\n",
50 return allocator.ReturnNullOrDieOnBadRequest();
52 MsanThread *t = GetCurrentThread();
55 AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
56 allocated = allocator.Allocate(cache, size, alignment, false);
58 SpinMutexLock l(&fallback_mutex);
59 AllocatorCache *cache = &fallback_allocator_cache;
60 allocated = allocator.Allocate(cache, size, alignment, false);
63 reinterpret_cast<Metadata *>(allocator.GetMetaData(allocated));
64 meta->requested_size = size;
66 __msan_clear_and_unpoison(allocated, size);
67 } else if (flags()->poison_in_malloc) {
68 __msan_poison(allocated, size);
69 if (__msan_get_track_origins()) {
70 stack->tag = StackTrace::TAG_ALLOC;
71 Origin o = Origin::CreateHeapOrigin(stack);
72 __msan_set_origin(allocated, size, o.raw_id());
75 MSAN_MALLOC_HOOK(allocated, size);
79 void MsanDeallocate(StackTrace *stack, void *p) {
82 Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(p));
83 uptr size = meta->requested_size;
84 meta->requested_size = 0;
85 // This memory will not be reused by anyone else, so we are free to keep it
87 if (flags()->poison_in_free) {
88 __msan_poison(p, size);
89 if (__msan_get_track_origins()) {
90 stack->tag = StackTrace::TAG_DEALLOC;
91 Origin o = Origin::CreateHeapOrigin(stack);
92 __msan_set_origin(p, size, o.raw_id());
95 MsanThread *t = GetCurrentThread();
97 AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
98 allocator.Deallocate(cache, p);
100 SpinMutexLock l(&fallback_mutex);
101 AllocatorCache *cache = &fallback_allocator_cache;
102 allocator.Deallocate(cache, p);
106 void *MsanCalloc(StackTrace *stack, uptr nmemb, uptr size) {
107 if (CallocShouldReturnNullDueToOverflow(size, nmemb))
108 return allocator.ReturnNullOrDieOnBadRequest();
109 return MsanReallocate(stack, nullptr, nmemb * size, sizeof(u64), true);
112 void *MsanReallocate(StackTrace *stack, void *old_p, uptr new_size,
113 uptr alignment, bool zeroise) {
115 return MsanAllocate(stack, new_size, alignment, zeroise);
117 MsanDeallocate(stack, old_p);
120 Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(old_p));
121 uptr old_size = meta->requested_size;
122 uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p);
123 if (new_size <= actually_allocated_size) {
124 // We are not reallocating here.
125 meta->requested_size = new_size;
126 if (new_size > old_size) {
128 __msan_clear_and_unpoison((char *)old_p + old_size,
129 new_size - old_size);
130 } else if (flags()->poison_in_malloc) {
131 stack->tag = StackTrace::TAG_ALLOC;
132 PoisonMemory((char *)old_p + old_size, new_size - old_size, stack);
137 uptr memcpy_size = Min(new_size, old_size);
138 void *new_p = MsanAllocate(stack, new_size, alignment, zeroise);
139 // Printf("realloc: old_size %zd new_size %zd\n", old_size, new_size);
141 CopyMemory(new_p, old_p, memcpy_size, stack);
142 MsanDeallocate(stack, old_p);
147 static uptr AllocationSize(const void *p) {
149 const void *beg = allocator.GetBlockBegin(p);
150 if (beg != p) return 0;
151 Metadata *b = (Metadata *)allocator.GetMetaData(p);
152 return b->requested_size;
155 } // namespace __msan
157 using namespace __msan;
159 uptr __sanitizer_get_current_allocated_bytes() {
160 uptr stats[AllocatorStatCount];
161 allocator.GetStats(stats);
162 return stats[AllocatorStatAllocated];
165 uptr __sanitizer_get_heap_size() {
166 uptr stats[AllocatorStatCount];
167 allocator.GetStats(stats);
168 return stats[AllocatorStatMapped];
171 uptr __sanitizer_get_free_bytes() { return 1; }
173 uptr __sanitizer_get_unmapped_bytes() { return 1; }
175 uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
177 int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; }
179 uptr __sanitizer_get_allocated_size(const void *p) { return AllocationSize(p); }