]> git.proxmox.com Git - rustc.git/blob - src/compiler-rt/lib/tsan/rtl/tsan_mman.cc
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / compiler-rt / lib / tsan / rtl / tsan_mman.cc
1 //===-- tsan_mman.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 ThreadSanitizer (TSan), a race detector.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_allocator_interface.h"
14 #include "sanitizer_common/sanitizer_common.h"
15 #include "sanitizer_common/sanitizer_placement_new.h"
16 #include "tsan_mman.h"
17 #include "tsan_rtl.h"
18 #include "tsan_report.h"
19 #include "tsan_flags.h"
20
21 // May be overriden by front-end.
22 extern "C" void WEAK __sanitizer_malloc_hook(void *ptr, uptr size) {
23 (void)ptr;
24 (void)size;
25 }
26
27 extern "C" void WEAK __sanitizer_free_hook(void *ptr) {
28 (void)ptr;
29 }
30
31 namespace __tsan {
32
33 struct MapUnmapCallback {
34 void OnMap(uptr p, uptr size) const { }
35 void OnUnmap(uptr p, uptr size) const {
36 // We are about to unmap a chunk of user memory.
37 // Mark the corresponding shadow memory as not needed.
38 DontNeedShadowFor(p, size);
39 // Mark the corresponding meta shadow memory as not needed.
40 // Note the block does not contain any meta info at this point
41 // (this happens after free).
42 const uptr kMetaRatio = kMetaShadowCell / kMetaShadowSize;
43 const uptr kPageSize = GetPageSizeCached() * kMetaRatio;
44 // Block came from LargeMmapAllocator, so must be large.
45 // We rely on this in the calculations below.
46 CHECK_GE(size, 2 * kPageSize);
47 uptr diff = RoundUp(p, kPageSize) - p;
48 if (diff != 0) {
49 p += diff;
50 size -= diff;
51 }
52 diff = p + size - RoundDown(p + size, kPageSize);
53 if (diff != 0)
54 size -= diff;
55 FlushUnneededShadowMemory((uptr)MemToMeta(p), size / kMetaRatio);
56 }
57 };
58
59 static char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);
60 Allocator *allocator() {
61 return reinterpret_cast<Allocator*>(&allocator_placeholder);
62 }
63
64 void InitializeAllocator() {
65 allocator()->Init(common_flags()->allocator_may_return_null);
66 }
67
68 void AllocatorThreadStart(ThreadState *thr) {
69 allocator()->InitCache(&thr->alloc_cache);
70 internal_allocator()->InitCache(&thr->internal_alloc_cache);
71 }
72
73 void AllocatorThreadFinish(ThreadState *thr) {
74 allocator()->DestroyCache(&thr->alloc_cache);
75 internal_allocator()->DestroyCache(&thr->internal_alloc_cache);
76 }
77
78 void AllocatorPrintStats() {
79 allocator()->PrintStats();
80 }
81
82 static void SignalUnsafeCall(ThreadState *thr, uptr pc) {
83 if (atomic_load_relaxed(&thr->in_signal_handler) == 0 ||
84 !flags()->report_signal_unsafe)
85 return;
86 VarSizeStackTrace stack;
87 ObtainCurrentStack(thr, pc, &stack);
88 if (IsFiredSuppression(ctx, ReportTypeSignalUnsafe, stack))
89 return;
90 ThreadRegistryLock l(ctx->thread_registry);
91 ScopedReport rep(ReportTypeSignalUnsafe);
92 rep.AddStack(stack, true);
93 OutputReport(thr, rep);
94 }
95
96 void *user_alloc(ThreadState *thr, uptr pc, uptr sz, uptr align, bool signal) {
97 if ((sz >= (1ull << 40)) || (align >= (1ull << 40)))
98 return allocator()->ReturnNullOrDie();
99 void *p = allocator()->Allocate(&thr->alloc_cache, sz, align);
100 if (p == 0)
101 return 0;
102 if (ctx && ctx->initialized)
103 OnUserAlloc(thr, pc, (uptr)p, sz, true);
104 if (signal)
105 SignalUnsafeCall(thr, pc);
106 return p;
107 }
108
109 void *user_calloc(ThreadState *thr, uptr pc, uptr size, uptr n) {
110 if (CallocShouldReturnNullDueToOverflow(size, n))
111 return allocator()->ReturnNullOrDie();
112 void *p = user_alloc(thr, pc, n * size);
113 if (p)
114 internal_memset(p, 0, n * size);
115 return p;
116 }
117
118 void user_free(ThreadState *thr, uptr pc, void *p, bool signal) {
119 if (ctx && ctx->initialized)
120 OnUserFree(thr, pc, (uptr)p, true);
121 allocator()->Deallocate(&thr->alloc_cache, p);
122 if (signal)
123 SignalUnsafeCall(thr, pc);
124 }
125
126 void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write) {
127 DPrintf("#%d: alloc(%zu) = %p\n", thr->tid, sz, p);
128 ctx->metamap.AllocBlock(thr, pc, p, sz);
129 if (write && thr->ignore_reads_and_writes == 0)
130 MemoryRangeImitateWrite(thr, pc, (uptr)p, sz);
131 else
132 MemoryResetRange(thr, pc, (uptr)p, sz);
133 }
134
135 void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write) {
136 CHECK_NE(p, (void*)0);
137 uptr sz = ctx->metamap.FreeBlock(thr, pc, p);
138 DPrintf("#%d: free(%p, %zu)\n", thr->tid, p, sz);
139 if (write && thr->ignore_reads_and_writes == 0)
140 MemoryRangeFreed(thr, pc, (uptr)p, sz);
141 }
142
143 void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
144 void *p2 = 0;
145 // FIXME: Handle "shrinking" more efficiently,
146 // it seems that some software actually does this.
147 if (sz) {
148 p2 = user_alloc(thr, pc, sz);
149 if (p2 == 0)
150 return 0;
151 if (p) {
152 uptr oldsz = user_alloc_usable_size(p);
153 internal_memcpy(p2, p, min(oldsz, sz));
154 }
155 }
156 if (p)
157 user_free(thr, pc, p);
158 return p2;
159 }
160
161 uptr user_alloc_usable_size(const void *p) {
162 if (p == 0)
163 return 0;
164 MBlock *b = ctx->metamap.GetBlock((uptr)p);
165 return b ? b->siz : 0;
166 }
167
168 void invoke_malloc_hook(void *ptr, uptr size) {
169 ThreadState *thr = cur_thread();
170 if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
171 return;
172 __sanitizer_malloc_hook(ptr, size);
173 }
174
175 void invoke_free_hook(void *ptr) {
176 ThreadState *thr = cur_thread();
177 if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
178 return;
179 __sanitizer_free_hook(ptr);
180 }
181
182 void *internal_alloc(MBlockType typ, uptr sz) {
183 ThreadState *thr = cur_thread();
184 if (thr->nomalloc) {
185 thr->nomalloc = 0; // CHECK calls internal_malloc().
186 CHECK(0);
187 }
188 return InternalAlloc(sz, &thr->internal_alloc_cache);
189 }
190
191 void internal_free(void *p) {
192 ThreadState *thr = cur_thread();
193 if (thr->nomalloc) {
194 thr->nomalloc = 0; // CHECK calls internal_malloc().
195 CHECK(0);
196 }
197 InternalFree(p, &thr->internal_alloc_cache);
198 }
199
200 } // namespace __tsan
201
202 using namespace __tsan;
203
204 extern "C" {
205 uptr __sanitizer_get_current_allocated_bytes() {
206 uptr stats[AllocatorStatCount];
207 allocator()->GetStats(stats);
208 return stats[AllocatorStatAllocated];
209 }
210
211 uptr __sanitizer_get_heap_size() {
212 uptr stats[AllocatorStatCount];
213 allocator()->GetStats(stats);
214 return stats[AllocatorStatMapped];
215 }
216
217 uptr __sanitizer_get_free_bytes() {
218 return 1;
219 }
220
221 uptr __sanitizer_get_unmapped_bytes() {
222 return 1;
223 }
224
225 uptr __sanitizer_get_estimated_allocated_size(uptr size) {
226 return size;
227 }
228
229 int __sanitizer_get_ownership(const void *p) {
230 return allocator()->GetBlockBegin(p) != 0;
231 }
232
233 uptr __sanitizer_get_allocated_size(const void *p) {
234 return user_alloc_usable_size(p);
235 }
236
237 void __tsan_on_thread_idle() {
238 ThreadState *thr = cur_thread();
239 allocator()->SwallowCache(&thr->alloc_cache);
240 internal_allocator()->SwallowCache(&thr->internal_alloc_cache);
241 ctx->metamap.OnThreadIdle(thr);
242 }
243 } // extern "C"