2016-08-31 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libsanitizer / tsan / tsan_platform_mac.cc
blobe1405ffeabf1ffac9e82843f98b9f9be9a156780
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 #ifndef 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 pointer is stored
69 // separately 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 static ThreadState *main_thread_state = nullptr;
74 ThreadState *cur_thread() {
75 ThreadState **fake_tls;
76 uptr thread_identity = (uptr)pthread_self();
77 if (thread_identity == main_thread_identity || main_thread_identity == 0) {
78 fake_tls = &main_thread_state;
79 } else {
80 fake_tls = (ThreadState **)MemToShadow(thread_identity);
82 ThreadState *thr = (ThreadState *)SignalSafeGetOrAllocate(
83 (uptr *)fake_tls, sizeof(ThreadState));
84 return thr;
87 // TODO(kuba.brecka): This is not async-signal-safe. In particular, we call
88 // munmap first and then clear `fake_tls`; if we receive a signal in between,
89 // handler will try to access the unmapped ThreadState.
90 void cur_thread_finalize() {
91 uptr thread_identity = (uptr)pthread_self();
92 CHECK_NE(thread_identity, main_thread_identity);
93 ThreadState **fake_tls = (ThreadState **)MemToShadow(thread_identity);
94 internal_munmap(*fake_tls, sizeof(ThreadState));
95 *fake_tls = nullptr;
97 #endif
99 uptr GetShadowMemoryConsumption() {
100 return 0;
103 void FlushShadowMemory() {
106 void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) {
109 #ifndef SANITIZER_GO
110 void InitializeShadowMemoryPlatform() { }
112 // On OS X, GCD worker threads are created without a call to pthread_create. We
113 // need to properly register these threads with ThreadCreate and ThreadStart.
114 // These threads don't have a parent thread, as they are created "spuriously".
115 // We're using a libpthread API that notifies us about a newly created thread.
116 // The `thread == pthread_self()` check indicates this is actually a worker
117 // thread. If it's just a regular thread, this hook is called on the parent
118 // thread.
119 typedef void (*pthread_introspection_hook_t)(unsigned int event,
120 pthread_t thread, void *addr,
121 size_t size);
122 extern "C" pthread_introspection_hook_t pthread_introspection_hook_install(
123 pthread_introspection_hook_t hook);
124 static const uptr PTHREAD_INTROSPECTION_THREAD_CREATE = 1;
125 static const uptr PTHREAD_INTROSPECTION_THREAD_DESTROY = 4;
126 static pthread_introspection_hook_t prev_pthread_introspection_hook;
127 static void my_pthread_introspection_hook(unsigned int event, pthread_t thread,
128 void *addr, size_t size) {
129 if (event == PTHREAD_INTROSPECTION_THREAD_CREATE) {
130 if (thread == pthread_self()) {
131 // The current thread is a newly created GCD worker thread.
132 ThreadState *parent_thread_state = nullptr; // No parent.
133 int tid = ThreadCreate(parent_thread_state, 0, (uptr)thread, true);
134 CHECK_NE(tid, 0);
135 ThreadState *thr = cur_thread();
136 ThreadStart(thr, tid, GetTid());
138 } else if (event == PTHREAD_INTROSPECTION_THREAD_DESTROY) {
139 ThreadState *thr = cur_thread();
140 if (thr->tctx && thr->tctx->parent_tid == kInvalidTid) {
141 DestroyThreadState();
145 if (prev_pthread_introspection_hook != nullptr)
146 prev_pthread_introspection_hook(event, thread, addr, size);
148 #endif
150 void InitializePlatform() {
151 DisableCoreDumperIfNecessary();
152 #ifndef SANITIZER_GO
153 CheckAndProtect();
155 CHECK_EQ(main_thread_identity, 0);
156 main_thread_identity = (uptr)pthread_self();
158 prev_pthread_introspection_hook =
159 pthread_introspection_hook_install(&my_pthread_introspection_hook);
160 #endif
163 #ifndef SANITIZER_GO
164 // Note: this function runs with async signals enabled,
165 // so it must not touch any tsan state.
166 int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
167 void *abstime), void *c, void *m, void *abstime,
168 void(*cleanup)(void *arg), void *arg) {
169 // pthread_cleanup_push/pop are hardcore macros mess.
170 // We can't intercept nor call them w/o including pthread.h.
171 int res;
172 pthread_cleanup_push(cleanup, arg);
173 res = fn(c, m, abstime);
174 pthread_cleanup_pop(0);
175 return res;
177 #endif
179 bool IsGlobalVar(uptr addr) {
180 return false;
183 } // namespace __tsan
185 #endif // SANITIZER_MAC