blob: 285bdb34d91dffd5efc699913575451044608f9a [file] [log] [blame]
Alexey Samsonov603c4be2012-06-04 13:55:19 +00001//===-- tsan_mman.cc ------------------------------------------------------===//
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002//
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//===----------------------------------------------------------------------===//
Stephen Hines6a211c52014-07-21 00:49:56 -070013#include "sanitizer_common/sanitizer_allocator_interface.h"
Alexey Samsonovf7667cc2012-06-07 11:54:08 +000014#include "sanitizer_common/sanitizer_common.h"
Dmitry Vyukov2e870512012-08-15 15:35:15 +000015#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000016#include "tsan_mman.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000017#include "tsan_rtl.h"
18#include "tsan_report.h"
19#include "tsan_flags.h"
20
Alexey Samsonov4f0ea392012-09-24 13:19:47 +000021// May be overriden by front-end.
Stephen Hines6a211c52014-07-21 00:49:56 -070022extern "C" void WEAK __sanitizer_malloc_hook(void *ptr, uptr size) {
23 (void)ptr;
24 (void)size;
25}
Alexey Samsonov4f0ea392012-09-24 13:19:47 +000026
Stephen Hines6a211c52014-07-21 00:49:56 -070027extern "C" void WEAK __sanitizer_free_hook(void *ptr) {
28 (void)ptr;
29}
Alexey Samsonov4f0ea392012-09-24 13:19:47 +000030
Kostya Serebryany7ac41482012-05-10 13:48:04 +000031namespace __tsan {
32
Dmitry Vyukove93e5052013-03-18 10:32:21 +000033struct 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.
Dmitry Vyukov7ac33ac2013-03-18 15:49:07 +000038 DontNeedShadowFor(p, size);
Dmitry Vyukove93e5052013-03-18 10:32:21 +000039 }
40};
41
Dmitry Vyukovff35f1d2012-08-30 13:02:30 +000042static char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);
43Allocator *allocator() {
Dmitry Vyukov2e870512012-08-15 15:35:15 +000044 return reinterpret_cast<Allocator*>(&allocator_placeholder);
45}
46
47void InitializeAllocator() {
48 allocator()->Init();
49}
50
Dmitry Vyukovbdd844c2013-01-24 09:08:03 +000051void AllocatorThreadStart(ThreadState *thr) {
52 allocator()->InitCache(&thr->alloc_cache);
Alexey Samsonov1f3c2fe2013-05-29 09:15:39 +000053 internal_allocator()->InitCache(&thr->internal_alloc_cache);
Dmitry Vyukovbdd844c2013-01-24 09:08:03 +000054}
55
56void AllocatorThreadFinish(ThreadState *thr) {
57 allocator()->DestroyCache(&thr->alloc_cache);
Alexey Samsonov1f3c2fe2013-05-29 09:15:39 +000058 internal_allocator()->DestroyCache(&thr->internal_alloc_cache);
Dmitry Vyukovbdd844c2013-01-24 09:08:03 +000059}
60
61void AllocatorPrintStats() {
62 allocator()->PrintStats();
Dmitry Vyukov2e870512012-08-15 15:35:15 +000063}
64
Kostya Serebryany7ac41482012-05-10 13:48:04 +000065static void SignalUnsafeCall(ThreadState *thr, uptr pc) {
Stephen Hines6d186232014-11-26 17:56:19 -080066 if (atomic_load(&thr->in_signal_handler, memory_order_relaxed) == 0 ||
67 !flags()->report_signal_unsafe)
Kostya Serebryany7ac41482012-05-10 13:48:04 +000068 return;
Stephen Hines6d186232014-11-26 17:56:19 -080069 VarSizeStackTrace stack;
70 ObtainCurrentStack(thr, pc, &stack);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000071 ThreadRegistryLock l(ctx->thread_registry);
Kostya Serebryany7ac41482012-05-10 13:48:04 +000072 ScopedReport rep(ReportTypeSignalUnsafe);
Dmitry Vyukov158c6ac2012-10-05 15:51:32 +000073 if (!IsFiredSuppression(ctx, rep, stack)) {
Stephen Hines6d186232014-11-26 17:56:19 -080074 rep.AddStack(stack, true);
Stephen Hines6a211c52014-07-21 00:49:56 -070075 OutputReport(thr, rep);
Dmitry Vyukov158c6ac2012-10-05 15:51:32 +000076 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +000077}
78
Stephen Hines6d186232014-11-26 17:56:19 -080079void *user_alloc(ThreadState *thr, uptr pc, uptr sz, uptr align, bool signal) {
Dmitry Vyukov7423c782013-03-22 17:06:22 +000080 if ((sz >= (1ull << 40)) || (align >= (1ull << 40)))
Kostya Serebryanybd33d3a2013-09-06 11:04:14 +000081 return AllocatorReturnNull();
Dmitry Vyukov2e870512012-08-15 15:35:15 +000082 void *p = allocator()->Allocate(&thr->alloc_cache, sz, align);
83 if (p == 0)
Dmitry Vyukov7b8bee12012-05-18 09:41:52 +000084 return 0;
Stephen Hines6a211c52014-07-21 00:49:56 -070085 if (ctx && ctx->initialized)
86 OnUserAlloc(thr, pc, (uptr)p, sz, true);
Stephen Hines6d186232014-11-26 17:56:19 -080087 if (signal)
88 SignalUnsafeCall(thr, pc);
Kostya Serebryany7ac41482012-05-10 13:48:04 +000089 return p;
90}
91
Stephen Hines6d186232014-11-26 17:56:19 -080092void user_free(ThreadState *thr, uptr pc, void *p, bool signal) {
Stephen Hines6a211c52014-07-21 00:49:56 -070093 if (ctx && ctx->initialized)
94 OnUserFree(thr, pc, (uptr)p, true);
Dmitry Vyukov2e870512012-08-15 15:35:15 +000095 allocator()->Deallocate(&thr->alloc_cache, p);
Stephen Hines6d186232014-11-26 17:56:19 -080096 if (signal)
97 SignalUnsafeCall(thr, pc);
Kostya Serebryany7ac41482012-05-10 13:48:04 +000098}
99
Stephen Hines6a211c52014-07-21 00:49:56 -0700100void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write) {
101 DPrintf("#%d: alloc(%zu) = %p\n", thr->tid, sz, p);
102 ctx->metamap.AllocBlock(thr, pc, p, sz);
103 if (write && thr->ignore_reads_and_writes == 0)
104 MemoryRangeImitateWrite(thr, pc, (uptr)p, sz);
105 else
106 MemoryResetRange(thr, pc, (uptr)p, sz);
107}
108
109void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write) {
110 CHECK_NE(p, (void*)0);
111 uptr sz = ctx->metamap.FreeBlock(thr, pc, p);
112 DPrintf("#%d: free(%p, %zu)\n", thr->tid, p, sz);
113 if (write && thr->ignore_reads_and_writes == 0)
114 MemoryRangeFreed(thr, pc, (uptr)p, sz);
115}
116
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000117void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000118 void *p2 = 0;
119 // FIXME: Handle "shrinking" more efficiently,
120 // it seems that some software actually does this.
121 if (sz) {
122 p2 = user_alloc(thr, pc, sz);
Dmitry Vyukovefd95822012-05-21 06:46:27 +0000123 if (p2 == 0)
124 return 0;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000125 if (p) {
Stephen Hines6a211c52014-07-21 00:49:56 -0700126 uptr oldsz = user_alloc_usable_size(p);
127 internal_memcpy(p2, p, min(oldsz, sz));
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000128 }
129 }
Dmitry Vyukovf51c3862013-03-18 19:47:36 +0000130 if (p)
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000131 user_free(thr, pc, p);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000132 return p2;
133}
134
Stephen Hines6a211c52014-07-21 00:49:56 -0700135uptr user_alloc_usable_size(const void *p) {
Alexey Samsonov8a6b5e52013-02-25 08:43:10 +0000136 if (p == 0)
137 return 0;
Stephen Hines6a211c52014-07-21 00:49:56 -0700138 MBlock *b = ctx->metamap.GetBlock((uptr)p);
139 return b ? b->siz : 0;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000140}
141
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000142void invoke_malloc_hook(void *ptr, uptr size) {
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000143 ThreadState *thr = cur_thread();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700144 if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000145 return;
Stephen Hines6a211c52014-07-21 00:49:56 -0700146 __sanitizer_malloc_hook(ptr, size);
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000147}
148
149void invoke_free_hook(void *ptr) {
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000150 ThreadState *thr = cur_thread();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700151 if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000152 return;
Stephen Hines6a211c52014-07-21 00:49:56 -0700153 __sanitizer_free_hook(ptr);
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000154}
155
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000156void *internal_alloc(MBlockType typ, uptr sz) {
157 ThreadState *thr = cur_thread();
Dmitry Vyukov9ad7c322012-06-22 11:08:55 +0000158 if (thr->nomalloc) {
159 thr->nomalloc = 0; // CHECK calls internal_malloc().
160 CHECK(0);
161 }
Alexey Samsonov1f3c2fe2013-05-29 09:15:39 +0000162 return InternalAlloc(sz, &thr->internal_alloc_cache);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000163}
164
165void internal_free(void *p) {
166 ThreadState *thr = cur_thread();
Dmitry Vyukov9ad7c322012-06-22 11:08:55 +0000167 if (thr->nomalloc) {
168 thr->nomalloc = 0; // CHECK calls internal_malloc().
169 CHECK(0);
170 }
Alexey Samsonov1f3c2fe2013-05-29 09:15:39 +0000171 InternalFree(p, &thr->internal_alloc_cache);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000172}
173
174} // namespace __tsan
Dmitry Vyukov12530822013-01-23 12:08:03 +0000175
176using namespace __tsan;
177
178extern "C" {
Stephen Hines6a211c52014-07-21 00:49:56 -0700179uptr __sanitizer_get_current_allocated_bytes() {
180 uptr stats[AllocatorStatCount];
181 allocator()->GetStats(stats);
182 return stats[AllocatorStatAllocated];
183}
Dmitry Vyukov12530822013-01-23 12:08:03 +0000184
Stephen Hines6a211c52014-07-21 00:49:56 -0700185uptr __sanitizer_get_heap_size() {
186 uptr stats[AllocatorStatCount];
187 allocator()->GetStats(stats);
188 return stats[AllocatorStatMapped];
189}
Dmitry Vyukov12530822013-01-23 12:08:03 +0000190
Stephen Hines6a211c52014-07-21 00:49:56 -0700191uptr __sanitizer_get_free_bytes() {
192 return 1;
193}
Dmitry Vyukov12530822013-01-23 12:08:03 +0000194
Stephen Hines6a211c52014-07-21 00:49:56 -0700195uptr __sanitizer_get_unmapped_bytes() {
196 return 1;
197}
Dmitry Vyukov12530822013-01-23 12:08:03 +0000198
Stephen Hines6a211c52014-07-21 00:49:56 -0700199uptr __sanitizer_get_estimated_allocated_size(uptr size) {
Dmitry Vyukov12530822013-01-23 12:08:03 +0000200 return size;
201}
Dmitry Vyukov12530822013-01-23 12:08:03 +0000202
Stephen Hines6a211c52014-07-21 00:49:56 -0700203int __sanitizer_get_ownership(const void *p) {
204 return allocator()->GetBlockBegin(p) != 0;
205}
Stephen Hines6a211c52014-07-21 00:49:56 -0700206
207uptr __sanitizer_get_allocated_size(const void *p) {
208 return user_alloc_usable_size(p);
209}
Dmitry Vyukov3ce21702013-03-18 17:21:15 +0000210
211void __tsan_on_thread_idle() {
212 ThreadState *thr = cur_thread();
213 allocator()->SwallowCache(&thr->alloc_cache);
Alexey Samsonov1f3c2fe2013-05-29 09:15:39 +0000214 internal_allocator()->SwallowCache(&thr->internal_alloc_cache);
Stephen Hines6a211c52014-07-21 00:49:56 -0700215 ctx->metamap.OnThreadIdle(thr);
Dmitry Vyukov3ce21702013-03-18 17:21:15 +0000216}
Dmitry Vyukov12530822013-01-23 12:08:03 +0000217} // extern "C"