]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/tsan/rtl/tsan_mman.cc
Pull down pjdfstest 0.1
[FreeBSD/FreeBSD.git] / contrib / 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 SANITIZER_WEAK_DEFAULT_IMPL
23 void __sanitizer_malloc_hook(void *ptr, uptr size) {
24   (void)ptr;
25   (void)size;
26 }
27
28 SANITIZER_WEAK_DEFAULT_IMPL
29 void __sanitizer_free_hook(void *ptr) {
30   (void)ptr;
31 }
32
33 namespace __tsan {
34
35 struct MapUnmapCallback {
36   void OnMap(uptr p, uptr size) const { }
37   void OnUnmap(uptr p, uptr size) const {
38     // We are about to unmap a chunk of user memory.
39     // Mark the corresponding shadow memory as not needed.
40     DontNeedShadowFor(p, size);
41     // Mark the corresponding meta shadow memory as not needed.
42     // Note the block does not contain any meta info at this point
43     // (this happens after free).
44     const uptr kMetaRatio = kMetaShadowCell / kMetaShadowSize;
45     const uptr kPageSize = GetPageSizeCached() * kMetaRatio;
46     // Block came from LargeMmapAllocator, so must be large.
47     // We rely on this in the calculations below.
48     CHECK_GE(size, 2 * kPageSize);
49     uptr diff = RoundUp(p, kPageSize) - p;
50     if (diff != 0) {
51       p += diff;
52       size -= diff;
53     }
54     diff = p + size - RoundDown(p + size, kPageSize);
55     if (diff != 0)
56       size -= diff;
57     uptr p_meta = (uptr)MemToMeta(p);
58     ReleaseMemoryPagesToOS(p_meta, p_meta + size / kMetaRatio);
59   }
60 };
61
62 static char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);
63 Allocator *allocator() {
64   return reinterpret_cast<Allocator*>(&allocator_placeholder);
65 }
66
67 struct GlobalProc {
68   Mutex mtx;
69   Processor *proc;
70
71   GlobalProc()
72       : mtx(MutexTypeGlobalProc, StatMtxGlobalProc)
73       , proc(ProcCreate()) {
74   }
75 };
76
77 static char global_proc_placeholder[sizeof(GlobalProc)] ALIGNED(64);
78 GlobalProc *global_proc() {
79   return reinterpret_cast<GlobalProc*>(&global_proc_placeholder);
80 }
81
82 ScopedGlobalProcessor::ScopedGlobalProcessor() {
83   GlobalProc *gp = global_proc();
84   ThreadState *thr = cur_thread();
85   if (thr->proc())
86     return;
87   // If we don't have a proc, use the global one.
88   // There are currently only two known case where this path is triggered:
89   //   __interceptor_free
90   //   __nptl_deallocate_tsd
91   //   start_thread
92   //   clone
93   // and:
94   //   ResetRange
95   //   __interceptor_munmap
96   //   __deallocate_stack
97   //   start_thread
98   //   clone
99   // Ideally, we destroy thread state (and unwire proc) when a thread actually
100   // exits (i.e. when we join/wait it). Then we would not need the global proc
101   gp->mtx.Lock();
102   ProcWire(gp->proc, thr);
103 }
104
105 ScopedGlobalProcessor::~ScopedGlobalProcessor() {
106   GlobalProc *gp = global_proc();
107   ThreadState *thr = cur_thread();
108   if (thr->proc() != gp->proc)
109     return;
110   ProcUnwire(gp->proc, thr);
111   gp->mtx.Unlock();
112 }
113
114 void InitializeAllocator() {
115   allocator()->Init(
116       common_flags()->allocator_may_return_null,
117       common_flags()->allocator_release_to_os_interval_ms);
118 }
119
120 void InitializeAllocatorLate() {
121   new(global_proc()) GlobalProc();
122 }
123
124 void AllocatorProcStart(Processor *proc) {
125   allocator()->InitCache(&proc->alloc_cache);
126   internal_allocator()->InitCache(&proc->internal_alloc_cache);
127 }
128
129 void AllocatorProcFinish(Processor *proc) {
130   allocator()->DestroyCache(&proc->alloc_cache);
131   internal_allocator()->DestroyCache(&proc->internal_alloc_cache);
132 }
133
134 void AllocatorPrintStats() {
135   allocator()->PrintStats();
136 }
137
138 static void SignalUnsafeCall(ThreadState *thr, uptr pc) {
139   if (atomic_load_relaxed(&thr->in_signal_handler) == 0 ||
140       !flags()->report_signal_unsafe)
141     return;
142   VarSizeStackTrace stack;
143   ObtainCurrentStack(thr, pc, &stack);
144   if (IsFiredSuppression(ctx, ReportTypeSignalUnsafe, stack))
145     return;
146   ThreadRegistryLock l(ctx->thread_registry);
147   ScopedReport rep(ReportTypeSignalUnsafe);
148   rep.AddStack(stack, true);
149   OutputReport(thr, rep);
150 }
151
152 void *user_alloc(ThreadState *thr, uptr pc, uptr sz, uptr align, bool signal) {
153   if ((sz >= (1ull << 40)) || (align >= (1ull << 40)))
154     return allocator()->ReturnNullOrDieOnBadRequest();
155   void *p = allocator()->Allocate(&thr->proc()->alloc_cache, sz, align);
156   if (p == 0)
157     return 0;
158   if (ctx && ctx->initialized)
159     OnUserAlloc(thr, pc, (uptr)p, sz, true);
160   if (signal)
161     SignalUnsafeCall(thr, pc);
162   return p;
163 }
164
165 void *user_calloc(ThreadState *thr, uptr pc, uptr size, uptr n) {
166   if (CallocShouldReturnNullDueToOverflow(size, n))
167     return allocator()->ReturnNullOrDieOnBadRequest();
168   void *p = user_alloc(thr, pc, n * size);
169   if (p)
170     internal_memset(p, 0, n * size);
171   return p;
172 }
173
174 void user_free(ThreadState *thr, uptr pc, void *p, bool signal) {
175   ScopedGlobalProcessor sgp;
176   if (ctx && ctx->initialized)
177     OnUserFree(thr, pc, (uptr)p, true);
178   allocator()->Deallocate(&thr->proc()->alloc_cache, p);
179   if (signal)
180     SignalUnsafeCall(thr, pc);
181 }
182
183 void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write) {
184   DPrintf("#%d: alloc(%zu) = %p\n", thr->tid, sz, p);
185   ctx->metamap.AllocBlock(thr, pc, p, sz);
186   if (write && thr->ignore_reads_and_writes == 0)
187     MemoryRangeImitateWrite(thr, pc, (uptr)p, sz);
188   else
189     MemoryResetRange(thr, pc, (uptr)p, sz);
190 }
191
192 void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write) {
193   CHECK_NE(p, (void*)0);
194   uptr sz = ctx->metamap.FreeBlock(thr->proc(), p);
195   DPrintf("#%d: free(%p, %zu)\n", thr->tid, p, sz);
196   if (write && thr->ignore_reads_and_writes == 0)
197     MemoryRangeFreed(thr, pc, (uptr)p, sz);
198 }
199
200 void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
201   // FIXME: Handle "shrinking" more efficiently,
202   // it seems that some software actually does this.
203   void *p2 = user_alloc(thr, pc, sz);
204   if (p2 == 0)
205     return 0;
206   if (p) {
207     uptr oldsz = user_alloc_usable_size(p);
208     internal_memcpy(p2, p, min(oldsz, sz));
209     user_free(thr, pc, p);
210   }
211   return p2;
212 }
213
214 uptr user_alloc_usable_size(const void *p) {
215   if (p == 0)
216     return 0;
217   MBlock *b = ctx->metamap.GetBlock((uptr)p);
218   if (!b)
219     return 0;  // Not a valid pointer.
220   if (b->siz == 0)
221     return 1;  // Zero-sized allocations are actually 1 byte.
222   return b->siz;
223 }
224
225 void invoke_malloc_hook(void *ptr, uptr size) {
226   ThreadState *thr = cur_thread();
227   if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
228     return;
229   __sanitizer_malloc_hook(ptr, size);
230   RunMallocHooks(ptr, size);
231 }
232
233 void invoke_free_hook(void *ptr) {
234   ThreadState *thr = cur_thread();
235   if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
236     return;
237   __sanitizer_free_hook(ptr);
238   RunFreeHooks(ptr);
239 }
240
241 void *internal_alloc(MBlockType typ, uptr sz) {
242   ThreadState *thr = cur_thread();
243   if (thr->nomalloc) {
244     thr->nomalloc = 0;  // CHECK calls internal_malloc().
245     CHECK(0);
246   }
247   return InternalAlloc(sz, &thr->proc()->internal_alloc_cache);
248 }
249
250 void internal_free(void *p) {
251   ThreadState *thr = cur_thread();
252   if (thr->nomalloc) {
253     thr->nomalloc = 0;  // CHECK calls internal_malloc().
254     CHECK(0);
255   }
256   InternalFree(p, &thr->proc()->internal_alloc_cache);
257 }
258
259 }  // namespace __tsan
260
261 using namespace __tsan;
262
263 extern "C" {
264 uptr __sanitizer_get_current_allocated_bytes() {
265   uptr stats[AllocatorStatCount];
266   allocator()->GetStats(stats);
267   return stats[AllocatorStatAllocated];
268 }
269
270 uptr __sanitizer_get_heap_size() {
271   uptr stats[AllocatorStatCount];
272   allocator()->GetStats(stats);
273   return stats[AllocatorStatMapped];
274 }
275
276 uptr __sanitizer_get_free_bytes() {
277   return 1;
278 }
279
280 uptr __sanitizer_get_unmapped_bytes() {
281   return 1;
282 }
283
284 uptr __sanitizer_get_estimated_allocated_size(uptr size) {
285   return size;
286 }
287
288 int __sanitizer_get_ownership(const void *p) {
289   return allocator()->GetBlockBegin(p) != 0;
290 }
291
292 uptr __sanitizer_get_allocated_size(const void *p) {
293   return user_alloc_usable_size(p);
294 }
295
296 void __tsan_on_thread_idle() {
297   ThreadState *thr = cur_thread();
298   allocator()->SwallowCache(&thr->proc()->alloc_cache);
299   internal_allocator()->SwallowCache(&thr->proc()->internal_alloc_cache);
300   ctx->metamap.OnProcIdle(thr->proc());
301 }
302 }  // extern "C"