1 //===-- tsan_mman.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 ThreadSanitizer (TSan), a race detector.
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"
18 #include "tsan_report.h"
19 #include "tsan_flags.h"
21 // May be overriden by front-end.
22 SANITIZER_WEAK_DEFAULT_IMPL
23 void __sanitizer_malloc_hook(void *ptr, uptr size) {
28 SANITIZER_WEAK_DEFAULT_IMPL
29 void __sanitizer_free_hook(void *ptr) {
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;
54 diff = p + size - RoundDown(p + size, kPageSize);
57 FlushUnneededShadowMemory((uptr)MemToMeta(p), size / kMetaRatio);
61 static char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);
62 Allocator *allocator() {
63 return reinterpret_cast<Allocator*>(&allocator_placeholder);
71 : mtx(MutexTypeGlobalProc, StatMtxGlobalProc)
72 , proc(ProcCreate()) {
76 static char global_proc_placeholder[sizeof(GlobalProc)] ALIGNED(64);
77 GlobalProc *global_proc() {
78 return reinterpret_cast<GlobalProc*>(&global_proc_placeholder);
81 ScopedGlobalProcessor::ScopedGlobalProcessor() {
82 GlobalProc *gp = global_proc();
83 ThreadState *thr = cur_thread();
86 // If we don't have a proc, use the global one.
87 // There are currently only two known case where this path is triggered:
89 // __nptl_deallocate_tsd
94 // __interceptor_munmap
98 // Ideally, we destroy thread state (and unwire proc) when a thread actually
99 // exits (i.e. when we join/wait it). Then we would not need the global proc
101 ProcWire(gp->proc, thr);
104 ScopedGlobalProcessor::~ScopedGlobalProcessor() {
105 GlobalProc *gp = global_proc();
106 ThreadState *thr = cur_thread();
107 if (thr->proc() != gp->proc)
109 ProcUnwire(gp->proc, thr);
113 void InitializeAllocator() {
114 allocator()->Init(common_flags()->allocator_may_return_null);
117 void InitializeAllocatorLate() {
118 new(global_proc()) GlobalProc();
121 void AllocatorProcStart(Processor *proc) {
122 allocator()->InitCache(&proc->alloc_cache);
123 internal_allocator()->InitCache(&proc->internal_alloc_cache);
126 void AllocatorProcFinish(Processor *proc) {
127 allocator()->DestroyCache(&proc->alloc_cache);
128 internal_allocator()->DestroyCache(&proc->internal_alloc_cache);
131 void AllocatorPrintStats() {
132 allocator()->PrintStats();
135 static void SignalUnsafeCall(ThreadState *thr, uptr pc) {
136 if (atomic_load_relaxed(&thr->in_signal_handler) == 0 ||
137 !flags()->report_signal_unsafe)
139 VarSizeStackTrace stack;
140 ObtainCurrentStack(thr, pc, &stack);
141 if (IsFiredSuppression(ctx, ReportTypeSignalUnsafe, stack))
143 ThreadRegistryLock l(ctx->thread_registry);
144 ScopedReport rep(ReportTypeSignalUnsafe);
145 rep.AddStack(stack, true);
146 OutputReport(thr, rep);
149 void *user_alloc(ThreadState *thr, uptr pc, uptr sz, uptr align, bool signal) {
150 if ((sz >= (1ull << 40)) || (align >= (1ull << 40)))
151 return allocator()->ReturnNullOrDie();
152 void *p = allocator()->Allocate(&thr->proc()->alloc_cache, sz, align);
155 if (ctx && ctx->initialized)
156 OnUserAlloc(thr, pc, (uptr)p, sz, true);
158 SignalUnsafeCall(thr, pc);
162 void *user_calloc(ThreadState *thr, uptr pc, uptr size, uptr n) {
163 if (CallocShouldReturnNullDueToOverflow(size, n))
164 return allocator()->ReturnNullOrDie();
165 void *p = user_alloc(thr, pc, n * size);
167 internal_memset(p, 0, n * size);
171 void user_free(ThreadState *thr, uptr pc, void *p, bool signal) {
172 ScopedGlobalProcessor sgp;
173 if (ctx && ctx->initialized)
174 OnUserFree(thr, pc, (uptr)p, true);
175 allocator()->Deallocate(&thr->proc()->alloc_cache, p);
177 SignalUnsafeCall(thr, pc);
180 void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write) {
181 DPrintf("#%d: alloc(%zu) = %p\n", thr->tid, sz, p);
182 ctx->metamap.AllocBlock(thr, pc, p, sz);
183 if (write && thr->ignore_reads_and_writes == 0)
184 MemoryRangeImitateWrite(thr, pc, (uptr)p, sz);
186 MemoryResetRange(thr, pc, (uptr)p, sz);
189 void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write) {
190 CHECK_NE(p, (void*)0);
191 uptr sz = ctx->metamap.FreeBlock(thr->proc(), p);
192 DPrintf("#%d: free(%p, %zu)\n", thr->tid, p, sz);
193 if (write && thr->ignore_reads_and_writes == 0)
194 MemoryRangeFreed(thr, pc, (uptr)p, sz);
197 void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
199 // FIXME: Handle "shrinking" more efficiently,
200 // it seems that some software actually does this.
202 p2 = user_alloc(thr, pc, sz);
206 uptr oldsz = user_alloc_usable_size(p);
207 internal_memcpy(p2, p, min(oldsz, sz));
211 user_free(thr, pc, p);
215 uptr user_alloc_usable_size(const void *p) {
218 MBlock *b = ctx->metamap.GetBlock((uptr)p);
220 return 0; // Not a valid pointer.
222 return 1; // Zero-sized allocations are actually 1 byte.
226 void invoke_malloc_hook(void *ptr, uptr size) {
227 ThreadState *thr = cur_thread();
228 if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
230 __sanitizer_malloc_hook(ptr, size);
231 RunMallocHooks(ptr, size);
234 void invoke_free_hook(void *ptr) {
235 ThreadState *thr = cur_thread();
236 if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
238 __sanitizer_free_hook(ptr);
242 void *internal_alloc(MBlockType typ, uptr sz) {
243 ThreadState *thr = cur_thread();
245 thr->nomalloc = 0; // CHECK calls internal_malloc().
248 return InternalAlloc(sz, &thr->proc()->internal_alloc_cache);
251 void internal_free(void *p) {
252 ThreadState *thr = cur_thread();
254 thr->nomalloc = 0; // CHECK calls internal_malloc().
257 InternalFree(p, &thr->proc()->internal_alloc_cache);
260 } // namespace __tsan
262 using namespace __tsan;
265 uptr __sanitizer_get_current_allocated_bytes() {
266 uptr stats[AllocatorStatCount];
267 allocator()->GetStats(stats);
268 return stats[AllocatorStatAllocated];
271 uptr __sanitizer_get_heap_size() {
272 uptr stats[AllocatorStatCount];
273 allocator()->GetStats(stats);
274 return stats[AllocatorStatMapped];
277 uptr __sanitizer_get_free_bytes() {
281 uptr __sanitizer_get_unmapped_bytes() {
285 uptr __sanitizer_get_estimated_allocated_size(uptr size) {
289 int __sanitizer_get_ownership(const void *p) {
290 return allocator()->GetBlockBegin(p) != 0;
293 uptr __sanitizer_get_allocated_size(const void *p) {
294 return user_alloc_usable_size(p);
297 void __tsan_on_thread_idle() {
298 ThreadState *thr = cur_thread();
299 allocator()->SwallowCache(&thr->proc()->alloc_cache);
300 internal_allocator()->SwallowCache(&thr->proc()->internal_alloc_cache);
301 ctx->metamap.OnProcIdle(thr->proc());