2017-10-10 Jakub Jelinek <jakub@redhat.com>
[official-gcc.git] / libsanitizer / tsan / tsan_platform_mac.cc
blobff5131e75bb3c5737a81e5029495a72fa99e5942
1 //===-- tsan_platform_mac.cc ----------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 // Mac-specific code.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_platform.h"
14 #if SANITIZER_MAC
16 #include "sanitizer_common/sanitizer_atomic.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_libc.h"
19 #include "sanitizer_common/sanitizer_posix.h"
20 #include "sanitizer_common/sanitizer_procmaps.h"
21 #include "tsan_platform.h"
22 #include "tsan_rtl.h"
23 #include "tsan_flags.h"
25 #include <pthread.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <sys/mman.h>
32 #include <sys/syscall.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <sys/resource.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <sched.h>
41 namespace __tsan {
43 #if !SANITIZER_GO
44 static void *SignalSafeGetOrAllocate(uptr *dst, uptr size) {
45 atomic_uintptr_t *a = (atomic_uintptr_t *)dst;
46 void *val = (void *)atomic_load_relaxed(a);
47 atomic_signal_fence(memory_order_acquire); // Turns the previous load into
48 // acquire wrt signals.
49 if (UNLIKELY(val == nullptr)) {
50 val = (void *)internal_mmap(nullptr, size, PROT_READ | PROT_WRITE,
51 MAP_PRIVATE | MAP_ANON, -1, 0);
52 CHECK(val);
53 void *cmp = nullptr;
54 if (!atomic_compare_exchange_strong(a, (uintptr_t *)&cmp, (uintptr_t)val,
55 memory_order_acq_rel)) {
56 internal_munmap(val, size);
57 val = cmp;
60 return val;
63 // On OS X, accessing TLVs via __thread or manually by using pthread_key_* is
64 // problematic, because there are several places where interceptors are called
65 // when TLVs are not accessible (early process startup, thread cleanup, ...).
66 // The following provides a "poor man's TLV" implementation, where we use the
67 // shadow memory of the pointer returned by pthread_self() to store a pointer to
68 // the ThreadState object. The main thread's ThreadState is stored separately
69 // in a static variable, because we need to access it even before the
70 // shadow memory is set up.
71 static uptr main_thread_identity = 0;
72 ALIGNED(64) static char main_thread_state[sizeof(ThreadState)];
74 ThreadState *cur_thread() {
75 uptr thread_identity = (uptr)pthread_self();
76 if (thread_identity == main_thread_identity || main_thread_identity == 0) {
77 return (ThreadState *)&main_thread_state;
79 ThreadState **fake_tls = (ThreadState **)MemToShadow(thread_identity);
80 ThreadState *thr = (ThreadState *)SignalSafeGetOrAllocate(
81 (uptr *)fake_tls, sizeof(ThreadState));
82 return thr;
85 // TODO(kuba.brecka): This is not async-signal-safe. In particular, we call
86 // munmap first and then clear `fake_tls`; if we receive a signal in between,
87 // handler will try to access the unmapped ThreadState.
88 void cur_thread_finalize() {
89 uptr thread_identity = (uptr)pthread_self();
90 if (thread_identity == main_thread_identity) {
91 // Calling dispatch_main() or xpc_main() actually invokes pthread_exit to
92 // exit the main thread. Let's keep the main thread's ThreadState.
93 return;
95 ThreadState **fake_tls = (ThreadState **)MemToShadow(thread_identity);
96 internal_munmap(*fake_tls, sizeof(ThreadState));
97 *fake_tls = nullptr;
99 #endif
101 uptr GetShadowMemoryConsumption() {
102 return 0;
105 void FlushShadowMemory() {
108 void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) {
111 #if !SANITIZER_GO
112 void InitializeShadowMemoryPlatform() { }
114 // On OS X, GCD worker threads are created without a call to pthread_create. We
115 // need to properly register these threads with ThreadCreate and ThreadStart.
116 // These threads don't have a parent thread, as they are created "spuriously".
117 // We're using a libpthread API that notifies us about a newly created thread.
118 // The `thread == pthread_self()` check indicates this is actually a worker
119 // thread. If it's just a regular thread, this hook is called on the parent
120 // thread.
121 typedef void (*pthread_introspection_hook_t)(unsigned int event,
122 pthread_t thread, void *addr,
123 size_t size);
124 extern "C" pthread_introspection_hook_t pthread_introspection_hook_install(
125 pthread_introspection_hook_t hook);
126 static const uptr PTHREAD_INTROSPECTION_THREAD_CREATE = 1;
127 static const uptr PTHREAD_INTROSPECTION_THREAD_TERMINATE = 3;
128 static pthread_introspection_hook_t prev_pthread_introspection_hook;
129 static void my_pthread_introspection_hook(unsigned int event, pthread_t thread,
130 void *addr, size_t size) {
131 if (event == PTHREAD_INTROSPECTION_THREAD_CREATE) {
132 if (thread == pthread_self()) {
133 // The current thread is a newly created GCD worker thread.
134 ThreadState *thr = cur_thread();
135 Processor *proc = ProcCreate();
136 ProcWire(proc, thr);
137 ThreadState *parent_thread_state = nullptr; // No parent.
138 int tid = ThreadCreate(parent_thread_state, 0, (uptr)thread, true);
139 CHECK_NE(tid, 0);
140 ThreadStart(thr, tid, GetTid());
142 } else if (event == PTHREAD_INTROSPECTION_THREAD_TERMINATE) {
143 if (thread == pthread_self()) {
144 ThreadState *thr = cur_thread();
145 if (thr->tctx) {
146 DestroyThreadState();
151 if (prev_pthread_introspection_hook != nullptr)
152 prev_pthread_introspection_hook(event, thread, addr, size);
154 #endif
156 void InitializePlatformEarly() {
159 void InitializePlatform() {
160 DisableCoreDumperIfNecessary();
161 #if !SANITIZER_GO
162 CheckAndProtect();
164 CHECK_EQ(main_thread_identity, 0);
165 main_thread_identity = (uptr)pthread_self();
167 prev_pthread_introspection_hook =
168 pthread_introspection_hook_install(&my_pthread_introspection_hook);
169 #endif
172 #if !SANITIZER_GO
173 // Note: this function runs with async signals enabled,
174 // so it must not touch any tsan state.
175 int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
176 void *abstime), void *c, void *m, void *abstime,
177 void(*cleanup)(void *arg), void *arg) {
178 // pthread_cleanup_push/pop are hardcore macros mess.
179 // We can't intercept nor call them w/o including pthread.h.
180 int res;
181 pthread_cleanup_push(cleanup, arg);
182 res = fn(c, m, abstime);
183 pthread_cleanup_pop(0);
184 return res;
186 #endif
188 } // namespace __tsan
190 #endif // SANITIZER_MAC