]> git.proxmox.com Git - rustc.git/blame - src/compiler-rt/lib/tsan/rtl/tsan_platform_mac.cc
New upstream version 1.19.0+dfsg3
[rustc.git] / src / compiler-rt / lib / tsan / rtl / tsan_platform_mac.cc
CommitLineData
1a4d82fc
JJ
1//===-- tsan_platform_mac.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// Mac-specific code.
13//===----------------------------------------------------------------------===//
14
15#include "sanitizer_common/sanitizer_platform.h"
16#if SANITIZER_MAC
17
92a42be0 18#include "sanitizer_common/sanitizer_atomic.h"
1a4d82fc
JJ
19#include "sanitizer_common/sanitizer_common.h"
20#include "sanitizer_common/sanitizer_libc.h"
92a42be0 21#include "sanitizer_common/sanitizer_posix.h"
1a4d82fc 22#include "sanitizer_common/sanitizer_procmaps.h"
7cac9316 23#include "sanitizer_common/sanitizer_stackdepot.h"
1a4d82fc
JJ
24#include "tsan_platform.h"
25#include "tsan_rtl.h"
26#include "tsan_flags.h"
27
7cac9316 28#include <mach/mach.h>
1a4d82fc
JJ
29#include <pthread.h>
30#include <signal.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <stdarg.h>
35#include <sys/mman.h>
36#include <sys/syscall.h>
37#include <sys/time.h>
38#include <sys/types.h>
39#include <sys/resource.h>
40#include <sys/stat.h>
41#include <unistd.h>
42#include <errno.h>
43#include <sched.h>
44
45namespace __tsan {
46
7cac9316 47#if !SANITIZER_GO
92a42be0
SL
48static void *SignalSafeGetOrAllocate(uptr *dst, uptr size) {
49 atomic_uintptr_t *a = (atomic_uintptr_t *)dst;
50 void *val = (void *)atomic_load_relaxed(a);
51 atomic_signal_fence(memory_order_acquire); // Turns the previous load into
52 // acquire wrt signals.
53 if (UNLIKELY(val == nullptr)) {
54 val = (void *)internal_mmap(nullptr, size, PROT_READ | PROT_WRITE,
55 MAP_PRIVATE | MAP_ANON, -1, 0);
56 CHECK(val);
57 void *cmp = nullptr;
58 if (!atomic_compare_exchange_strong(a, (uintptr_t *)&cmp, (uintptr_t)val,
59 memory_order_acq_rel)) {
60 internal_munmap(val, size);
61 val = cmp;
62 }
63 }
64 return val;
1a4d82fc
JJ
65}
66
92a42be0
SL
67// On OS X, accessing TLVs via __thread or manually by using pthread_key_* is
68// problematic, because there are several places where interceptors are called
69// when TLVs are not accessible (early process startup, thread cleanup, ...).
70// The following provides a "poor man's TLV" implementation, where we use the
71// shadow memory of the pointer returned by pthread_self() to store a pointer to
5bcae85e
SL
72// the ThreadState object. The main thread's ThreadState is stored separately
73// in a static variable, because we need to access it even before the
92a42be0
SL
74// shadow memory is set up.
75static uptr main_thread_identity = 0;
5bcae85e 76ALIGNED(64) static char main_thread_state[sizeof(ThreadState)];
92a42be0
SL
77
78ThreadState *cur_thread() {
92a42be0
SL
79 uptr thread_identity = (uptr)pthread_self();
80 if (thread_identity == main_thread_identity || main_thread_identity == 0) {
5bcae85e 81 return (ThreadState *)&main_thread_state;
92a42be0 82 }
5bcae85e 83 ThreadState **fake_tls = (ThreadState **)MemToShadow(thread_identity);
92a42be0
SL
84 ThreadState *thr = (ThreadState *)SignalSafeGetOrAllocate(
85 (uptr *)fake_tls, sizeof(ThreadState));
86 return thr;
1a4d82fc
JJ
87}
88
92a42be0
SL
89// TODO(kuba.brecka): This is not async-signal-safe. In particular, we call
90// munmap first and then clear `fake_tls`; if we receive a signal in between,
91// handler will try to access the unmapped ThreadState.
92void cur_thread_finalize() {
93 uptr thread_identity = (uptr)pthread_self();
5bcae85e
SL
94 if (thread_identity == main_thread_identity) {
95 // Calling dispatch_main() or xpc_main() actually invokes pthread_exit to
96 // exit the main thread. Let's keep the main thread's ThreadState.
97 return;
98 }
92a42be0
SL
99 ThreadState **fake_tls = (ThreadState **)MemToShadow(thread_identity);
100 internal_munmap(*fake_tls, sizeof(ThreadState));
101 *fake_tls = nullptr;
1a4d82fc 102}
92a42be0 103#endif
1a4d82fc 104
7cac9316 105void FlushShadowMemory() {
1a4d82fc
JJ
106}
107
7cac9316
XL
108static void RegionMemUsage(uptr start, uptr end, uptr *res, uptr *dirty) {
109 vm_address_t address = start;
110 vm_address_t end_address = end;
111 uptr resident_pages = 0;
112 uptr dirty_pages = 0;
113 while (address < end_address) {
114 vm_size_t vm_region_size;
115 mach_msg_type_number_t count = VM_REGION_EXTENDED_INFO_COUNT;
116 vm_region_extended_info_data_t vm_region_info;
117 mach_port_t object_name;
118 kern_return_t ret = vm_region_64(
119 mach_task_self(), &address, &vm_region_size, VM_REGION_EXTENDED_INFO,
120 (vm_region_info_t)&vm_region_info, &count, &object_name);
121 if (ret != KERN_SUCCESS) break;
122
123 resident_pages += vm_region_info.pages_resident;
124 dirty_pages += vm_region_info.pages_dirtied;
125
126 address += vm_region_size;
127 }
128 *res = resident_pages * GetPageSizeCached();
129 *dirty = dirty_pages * GetPageSizeCached();
92a42be0
SL
130}
131
132void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) {
7cac9316
XL
133 uptr shadow_res, shadow_dirty;
134 uptr meta_res, meta_dirty;
135 uptr trace_res, trace_dirty;
136 RegionMemUsage(ShadowBeg(), ShadowEnd(), &shadow_res, &shadow_dirty);
137 RegionMemUsage(MetaShadowBeg(), MetaShadowEnd(), &meta_res, &meta_dirty);
138 RegionMemUsage(TraceMemBeg(), TraceMemEnd(), &trace_res, &trace_dirty);
139
140#if !SANITIZER_GO
141 uptr low_res, low_dirty;
142 uptr high_res, high_dirty;
143 uptr heap_res, heap_dirty;
144 RegionMemUsage(LoAppMemBeg(), LoAppMemEnd(), &low_res, &low_dirty);
145 RegionMemUsage(HiAppMemBeg(), HiAppMemEnd(), &high_res, &high_dirty);
146 RegionMemUsage(HeapMemBeg(), HeapMemEnd(), &heap_res, &heap_dirty);
147#else // !SANITIZER_GO
148 uptr app_res, app_dirty;
149 RegionMemUsage(AppMemBeg(), AppMemEnd(), &app_res, &app_dirty);
150#endif
151
152 StackDepotStats *stacks = StackDepotGetStats();
153 internal_snprintf(buf, buf_size,
154 "shadow (0x%016zx-0x%016zx): resident %zd kB, dirty %zd kB\n"
155 "meta (0x%016zx-0x%016zx): resident %zd kB, dirty %zd kB\n"
156 "traces (0x%016zx-0x%016zx): resident %zd kB, dirty %zd kB\n"
157#if !SANITIZER_GO
158 "low app (0x%016zx-0x%016zx): resident %zd kB, dirty %zd kB\n"
159 "high app (0x%016zx-0x%016zx): resident %zd kB, dirty %zd kB\n"
160 "heap (0x%016zx-0x%016zx): resident %zd kB, dirty %zd kB\n"
161#else // !SANITIZER_GO
162 "app (0x%016zx-0x%016zx): resident %zd kB, dirty %zd kB\n"
163#endif
164 "stacks: %ld unique IDs, %ld kB allocated\n"
165 "threads: %ld total, %ld live\n"
166 "------------------------------\n",
167 ShadowBeg(), ShadowEnd(), shadow_res / 1024, shadow_dirty / 1024,
168 MetaShadowBeg(), MetaShadowEnd(), meta_res / 1024, meta_dirty / 1024,
169 TraceMemBeg(), TraceMemEnd(), trace_res / 1024, trace_dirty / 1024,
170#if !SANITIZER_GO
171 LoAppMemBeg(), LoAppMemEnd(), low_res / 1024, low_dirty / 1024,
172 HiAppMemBeg(), HiAppMemEnd(), high_res / 1024, high_dirty / 1024,
173 HeapMemBeg(), HeapMemEnd(), heap_res / 1024, heap_dirty / 1024,
174#else // !SANITIZER_GO
175 AppMemBeg(), AppMemEnd(), app_res / 1024, app_dirty / 1024,
176#endif
177 stacks->n_uniq_ids, stacks->allocated / 1024,
178 nthread, nlive);
1a4d82fc 179}
92a42be0 180
7cac9316 181#if !SANITIZER_GO
92a42be0 182void InitializeShadowMemoryPlatform() { }
1a4d82fc 183
92a42be0
SL
184// On OS X, GCD worker threads are created without a call to pthread_create. We
185// need to properly register these threads with ThreadCreate and ThreadStart.
186// These threads don't have a parent thread, as they are created "spuriously".
187// We're using a libpthread API that notifies us about a newly created thread.
188// The `thread == pthread_self()` check indicates this is actually a worker
189// thread. If it's just a regular thread, this hook is called on the parent
190// thread.
191typedef void (*pthread_introspection_hook_t)(unsigned int event,
192 pthread_t thread, void *addr,
193 size_t size);
194extern "C" pthread_introspection_hook_t pthread_introspection_hook_install(
195 pthread_introspection_hook_t hook);
196static const uptr PTHREAD_INTROSPECTION_THREAD_CREATE = 1;
3157f602 197static const uptr PTHREAD_INTROSPECTION_THREAD_TERMINATE = 3;
92a42be0
SL
198static pthread_introspection_hook_t prev_pthread_introspection_hook;
199static void my_pthread_introspection_hook(unsigned int event, pthread_t thread,
200 void *addr, size_t size) {
201 if (event == PTHREAD_INTROSPECTION_THREAD_CREATE) {
202 if (thread == pthread_self()) {
203 // The current thread is a newly created GCD worker thread.
5bcae85e
SL
204 ThreadState *thr = cur_thread();
205 Processor *proc = ProcCreate();
206 ProcWire(proc, thr);
92a42be0
SL
207 ThreadState *parent_thread_state = nullptr; // No parent.
208 int tid = ThreadCreate(parent_thread_state, 0, (uptr)thread, true);
209 CHECK_NE(tid, 0);
92a42be0
SL
210 ThreadStart(thr, tid, GetTid());
211 }
3157f602
XL
212 } else if (event == PTHREAD_INTROSPECTION_THREAD_TERMINATE) {
213 if (thread == pthread_self()) {
214 ThreadState *thr = cur_thread();
215 if (thr->tctx) {
216 DestroyThreadState();
217 }
92a42be0 218 }
1a4d82fc
JJ
219 }
220
92a42be0
SL
221 if (prev_pthread_introspection_hook != nullptr)
222 prev_pthread_introspection_hook(event, thread, addr, size);
1a4d82fc 223}
3157f602
XL
224#endif
225
226void InitializePlatformEarly() {
227}
1a4d82fc 228
92a42be0
SL
229void InitializePlatform() {
230 DisableCoreDumperIfNecessary();
7cac9316 231#if !SANITIZER_GO
92a42be0
SL
232 CheckAndProtect();
233
234 CHECK_EQ(main_thread_identity, 0);
235 main_thread_identity = (uptr)pthread_self();
92a42be0
SL
236
237 prev_pthread_introspection_hook =
238 pthread_introspection_hook_install(&my_pthread_introspection_hook);
3157f602 239#endif
1a4d82fc
JJ
240}
241
7cac9316 242#if !SANITIZER_GO
92a42be0
SL
243// Note: this function runs with async signals enabled,
244// so it must not touch any tsan state.
1a4d82fc
JJ
245int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
246 void *abstime), void *c, void *m, void *abstime,
247 void(*cleanup)(void *arg), void *arg) {
248 // pthread_cleanup_push/pop are hardcore macros mess.
249 // We can't intercept nor call them w/o including pthread.h.
250 int res;
251 pthread_cleanup_push(cleanup, arg);
252 res = fn(c, m, abstime);
253 pthread_cleanup_pop(0);
254 return res;
255}
256#endif
257
258} // namespace __tsan
259
260#endif // SANITIZER_MAC