blob: b7d394729bfcb8a3f1b2e393bee62ca290287beb [file] [log] [blame]
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001//===-- msan_allocator.cc --------------------------- ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of MemorySanitizer.
11//
12// MemorySanitizer allocator.
13//===----------------------------------------------------------------------===//
14
15#include "sanitizer_common/sanitizer_allocator.h"
Stephen Hines6a211c52014-07-21 00:49:56 -070016#include "sanitizer_common/sanitizer_allocator_interface.h"
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000017#include "msan.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070018#include "msan_allocator.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070019#include "msan_origin.h"
20#include "msan_thread.h"
Stephen Hines86277eb2015-03-23 12:06:32 -070021#include "msan_poisoning.h"
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000022
23namespace __msan {
24
25struct Metadata {
26 uptr requested_size;
27};
28
Stephen Hines2d1fdb22014-05-28 23:58:16 -070029struct MsanMapUnmapCallback {
30 void OnMap(uptr p, uptr size) const {}
31 void OnUnmap(uptr p, uptr size) const {
32 __msan_unpoison((void *)p, size);
33
34 // We are about to unmap a chunk of user memory.
35 // Mark the corresponding shadow memory as not needed.
36 FlushUnneededShadowMemory(MEM_TO_SHADOW(p), size);
37 if (__msan_get_track_origins())
38 FlushUnneededShadowMemory(MEM_TO_ORIGIN(p), size);
39 }
40};
41
Stephen Hines6d186232014-11-26 17:56:19 -080042#if defined(__mips64)
43 static const uptr kMaxAllowedMallocSize = 2UL << 30;
44 static const uptr kRegionSizeLog = 20;
45 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
46 typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
47 typedef CompactSizeClassMap SizeClassMap;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000048
Stephen Hines6d186232014-11-26 17:56:19 -080049 typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, sizeof(Metadata),
50 SizeClassMap, kRegionSizeLog, ByteMap,
51 MsanMapUnmapCallback> PrimaryAllocator;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080052
Stephen Hines6d186232014-11-26 17:56:19 -080053#elif defined(__x86_64__)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080054#if SANITIZER_LINUX && !defined(MSAN_LINUX_X86_64_OLD_MAPPING)
55 static const uptr kAllocatorSpace = 0x700000000000ULL;
56#else
Stephen Hines6d186232014-11-26 17:56:19 -080057 static const uptr kAllocatorSpace = 0x600000000000ULL;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080058#endif
59 static const uptr kAllocatorSize = 0x80000000000; // 8T.
Stephen Hines6d186232014-11-26 17:56:19 -080060 static const uptr kMetadataSize = sizeof(Metadata);
61 static const uptr kMaxAllowedMallocSize = 8UL << 30;
62
63 typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, kMetadataSize,
Stephen Hines2d1fdb22014-05-28 23:58:16 -070064 DefaultSizeClassMap,
65 MsanMapUnmapCallback> PrimaryAllocator;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080066
67#elif defined(__powerpc64__)
68 static const uptr kAllocatorSpace = 0x300000000000;
69 static const uptr kAllocatorSize = 0x020000000000; // 2T
70 static const uptr kMetadataSize = sizeof(Metadata);
71 static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G
72
73 typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, kMetadataSize,
74 DefaultSizeClassMap,
75 MsanMapUnmapCallback> PrimaryAllocator;
76#elif defined(__aarch64__)
77 static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G
78 static const uptr kRegionSizeLog = 20;
79 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
80 typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
81 typedef CompactSizeClassMap SizeClassMap;
82
83 typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, sizeof(Metadata),
84 SizeClassMap, kRegionSizeLog, ByteMap,
85 MsanMapUnmapCallback> PrimaryAllocator;
Stephen Hines6d186232014-11-26 17:56:19 -080086#endif
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000087typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070088typedef LargeMmapAllocator<MsanMapUnmapCallback> SecondaryAllocator;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000089typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
90 SecondaryAllocator> Allocator;
91
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000092static Allocator allocator;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070093static AllocatorCache fallback_allocator_cache;
94static SpinMutex fallback_mutex;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000095
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080096void MsanAllocatorInit() {
Stephen Hines86277eb2015-03-23 12:06:32 -070097 allocator.Init(common_flags()->allocator_may_return_null);
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000098}
99
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700100AllocatorCache *GetAllocatorCache(MsanThreadLocalMallocStorage *ms) {
101 CHECK(ms);
102 CHECK_LE(sizeof(AllocatorCache), sizeof(ms->allocator_cache));
103 return reinterpret_cast<AllocatorCache *>(ms->allocator_cache);
Evgeniy Stepanov7c6bd402013-10-22 14:31:30 +0000104}
105
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700106void MsanThreadLocalMallocStorage::CommitBack() {
107 allocator.SwallowCache(GetAllocatorCache(this));
108}
109
110static void *MsanAllocate(StackTrace *stack, uptr size, uptr alignment,
111 bool zeroise) {
Evgeniy Stepanov600d5162013-10-15 11:33:48 +0000112 if (size > kMaxAllowedMallocSize) {
113 Report("WARNING: MemorySanitizer failed to allocate %p bytes\n",
114 (void *)size);
Stephen Hines86277eb2015-03-23 12:06:32 -0700115 return allocator.ReturnNullOrDie();
Evgeniy Stepanov600d5162013-10-15 11:33:48 +0000116 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700117 MsanThread *t = GetCurrentThread();
118 void *allocated;
119 if (t) {
120 AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
121 allocated = allocator.Allocate(cache, size, alignment, false);
122 } else {
123 SpinMutexLock l(&fallback_mutex);
124 AllocatorCache *cache = &fallback_allocator_cache;
125 allocated = allocator.Allocate(cache, size, alignment, false);
126 }
127 Metadata *meta =
128 reinterpret_cast<Metadata *>(allocator.GetMetaData(allocated));
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000129 meta->requested_size = size;
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000130 if (zeroise) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700131 __msan_clear_and_unpoison(allocated, size);
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000132 } else if (flags()->poison_in_malloc) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700133 __msan_poison(allocated, size);
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000134 if (__msan_get_track_origins()) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700135 stack->tag = StackTrace::TAG_ALLOC;
136 Origin o = Origin::CreateHeapOrigin(stack);
137 __msan_set_origin(allocated, size, o.raw_id());
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000138 }
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000139 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700140 MSAN_MALLOC_HOOK(allocated, size);
141 return allocated;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000142}
143
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000144void MsanDeallocate(StackTrace *stack, void *p) {
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000145 CHECK(p);
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000146 MSAN_FREE_HOOK(p);
Stephen Hines6a211c52014-07-21 00:49:56 -0700147 Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(p));
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000148 uptr size = meta->requested_size;
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000149 meta->requested_size = 0;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000150 // This memory will not be reused by anyone else, so we are free to keep it
151 // poisoned.
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000152 if (flags()->poison_in_free) {
153 __msan_poison(p, size);
154 if (__msan_get_track_origins()) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700155 stack->tag = StackTrace::TAG_DEALLOC;
156 Origin o = Origin::CreateHeapOrigin(stack);
157 __msan_set_origin(p, size, o.raw_id());
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000158 }
159 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700160 MsanThread *t = GetCurrentThread();
161 if (t) {
162 AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
163 allocator.Deallocate(cache, p);
164 } else {
165 SpinMutexLock l(&fallback_mutex);
166 AllocatorCache *cache = &fallback_allocator_cache;
167 allocator.Deallocate(cache, p);
168 }
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000169}
170
Stephen Hines86277eb2015-03-23 12:06:32 -0700171void *MsanCalloc(StackTrace *stack, uptr nmemb, uptr size) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700172 if (CallocShouldReturnNullDueToOverflow(size, nmemb))
173 return allocator.ReturnNullOrDie();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800174 return MsanReallocate(stack, nullptr, nmemb * size, sizeof(u64), true);
Stephen Hines86277eb2015-03-23 12:06:32 -0700175}
176
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000177void *MsanReallocate(StackTrace *stack, void *old_p, uptr new_size,
178 uptr alignment, bool zeroise) {
179 if (!old_p)
180 return MsanAllocate(stack, new_size, alignment, zeroise);
181 if (!new_size) {
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +0000182 MsanDeallocate(stack, old_p);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800183 return nullptr;
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000184 }
185 Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(old_p));
186 uptr old_size = meta->requested_size;
187 uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p);
188 if (new_size <= actually_allocated_size) {
189 // We are not reallocating here.
190 meta->requested_size = new_size;
Stephen Hines86277eb2015-03-23 12:06:32 -0700191 if (new_size > old_size) {
192 if (zeroise) {
193 __msan_clear_and_unpoison((char *)old_p + old_size,
194 new_size - old_size);
195 } else if (flags()->poison_in_malloc) {
196 stack->tag = StackTrace::TAG_ALLOC;
197 PoisonMemory((char *)old_p + old_size, new_size - old_size, stack);
198 }
199 }
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000200 return old_p;
201 }
202 uptr memcpy_size = Min(new_size, old_size);
203 void *new_p = MsanAllocate(stack, new_size, alignment, zeroise);
204 // Printf("realloc: old_size %zd new_size %zd\n", old_size, new_size);
Evgeniy Stepanov600d5162013-10-15 11:33:48 +0000205 if (new_p) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700206 CopyMemory(new_p, old_p, memcpy_size, stack);
Evgeniy Stepanov600d5162013-10-15 11:33:48 +0000207 MsanDeallocate(stack, old_p);
208 }
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +0000209 return new_p;
210}
211
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000212static uptr AllocationSize(const void *p) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800213 if (!p) return 0;
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000214 const void *beg = allocator.GetBlockBegin(p);
Stephen Hines6a211c52014-07-21 00:49:56 -0700215 if (beg != p) return 0;
216 Metadata *b = (Metadata *)allocator.GetMetaData(p);
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000217 return b->requested_size;
218}
219
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800220} // namespace __msan
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000221
222using namespace __msan;
223
Stephen Hines6a211c52014-07-21 00:49:56 -0700224uptr __sanitizer_get_current_allocated_bytes() {
225 uptr stats[AllocatorStatCount];
226 allocator.GetStats(stats);
227 return stats[AllocatorStatAllocated];
228}
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000229
Stephen Hines6a211c52014-07-21 00:49:56 -0700230uptr __sanitizer_get_heap_size() {
231 uptr stats[AllocatorStatCount];
232 allocator.GetStats(stats);
233 return stats[AllocatorStatMapped];
234}
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000235
Stephen Hines6a211c52014-07-21 00:49:56 -0700236uptr __sanitizer_get_free_bytes() { return 1; }
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000237
Stephen Hines6a211c52014-07-21 00:49:56 -0700238uptr __sanitizer_get_unmapped_bytes() { return 1; }
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000239
Stephen Hines6a211c52014-07-21 00:49:56 -0700240uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000241
Stephen Hines6a211c52014-07-21 00:49:56 -0700242int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; }
Evgeniy Stepanov5c48a8c2013-08-02 14:26:58 +0000243
Stephen Hines6a211c52014-07-21 00:49:56 -0700244uptr __sanitizer_get_allocated_size(const void *p) { return AllocationSize(p); }