]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/msan/msan_allocator.cc
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304460, and update
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / msan / msan_allocator.cc
1 //===-- 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 "msan.h"
16 #include "msan_allocator.h"
17 #include "msan_origin.h"
18 #include "msan_thread.h"
19 #include "msan_poisoning.h"
20
21 namespace __msan {
22
23 static Allocator allocator;
24 static AllocatorCache fallback_allocator_cache;
25 static SpinMutex fallback_mutex;
26
27 Allocator &get_allocator() { return allocator; }
28
29 void MsanAllocatorInit() {
30   allocator.Init(
31       common_flags()->allocator_may_return_null,
32       common_flags()->allocator_release_to_os_interval_ms);
33 }
34
35 AllocatorCache *GetAllocatorCache(MsanThreadLocalMallocStorage *ms) {
36   CHECK(ms);
37   CHECK_LE(sizeof(AllocatorCache), sizeof(ms->allocator_cache));
38   return reinterpret_cast<AllocatorCache *>(ms->allocator_cache);
39 }
40
41 void MsanThreadLocalMallocStorage::CommitBack() {
42   allocator.SwallowCache(GetAllocatorCache(this));
43 }
44
45 static void *MsanAllocate(StackTrace *stack, uptr size, uptr alignment,
46                           bool zeroise) {
47   if (size > kMaxAllowedMallocSize) {
48     Report("WARNING: MemorySanitizer failed to allocate %p bytes\n",
49            (void *)size);
50     return allocator.ReturnNullOrDieOnBadRequest();
51   }
52   MsanThread *t = GetCurrentThread();
53   void *allocated;
54   if (t) {
55     AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
56     allocated = allocator.Allocate(cache, size, alignment, false);
57   } else {
58     SpinMutexLock l(&fallback_mutex);
59     AllocatorCache *cache = &fallback_allocator_cache;
60     allocated = allocator.Allocate(cache, size, alignment, false);
61   }
62   Metadata *meta =
63       reinterpret_cast<Metadata *>(allocator.GetMetaData(allocated));
64   meta->requested_size = size;
65   if (zeroise) {
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());
73     }
74   }
75   MSAN_MALLOC_HOOK(allocated, size);
76   return allocated;
77 }
78
79 void MsanDeallocate(StackTrace *stack, void *p) {
80   CHECK(p);
81   MSAN_FREE_HOOK(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
86   // poisoned.
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());
93     }
94   }
95   MsanThread *t = GetCurrentThread();
96   if (t) {
97     AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
98     allocator.Deallocate(cache, p);
99   } else {
100     SpinMutexLock l(&fallback_mutex);
101     AllocatorCache *cache = &fallback_allocator_cache;
102     allocator.Deallocate(cache, p);
103   }
104 }
105
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);
110 }
111
112 void *MsanReallocate(StackTrace *stack, void *old_p, uptr new_size,
113                      uptr alignment, bool zeroise) {
114   if (!old_p)
115     return MsanAllocate(stack, new_size, alignment, zeroise);
116   if (!new_size) {
117     MsanDeallocate(stack, old_p);
118     return nullptr;
119   }
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) {
127       if (zeroise) {
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);
133       }
134     }
135     return old_p;
136   }
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);
140   if (new_p) {
141     CopyMemory(new_p, old_p, memcpy_size, stack);
142     MsanDeallocate(stack, old_p);
143   }
144   return new_p;
145 }
146
147 static uptr AllocationSize(const void *p) {
148   if (!p) return 0;
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;
153 }
154
155 } // namespace __msan
156
157 using namespace __msan;
158
159 uptr __sanitizer_get_current_allocated_bytes() {
160   uptr stats[AllocatorStatCount];
161   allocator.GetStats(stats);
162   return stats[AllocatorStatAllocated];
163 }
164
165 uptr __sanitizer_get_heap_size() {
166   uptr stats[AllocatorStatCount];
167   allocator.GetStats(stats);
168   return stats[AllocatorStatMapped];
169 }
170
171 uptr __sanitizer_get_free_bytes() { return 1; }
172
173 uptr __sanitizer_get_unmapped_bytes() { return 1; }
174
175 uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
176
177 int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; }
178
179 uptr __sanitizer_get_allocated_size(const void *p) { return AllocationSize(p); }