Fix build on sparc64-linux-gnu.
[official-gcc.git] / libsanitizer / asan / asan_fuchsia.cc
blobf8207ecccd13425a7ab6b837ff41978532c0d22f
1 //===-- asan_fuchsia.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 AddressSanitizer, an address sanity checker.
9 //
10 // Fuchsia-specific details.
11 //===---------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_fuchsia.h"
14 #if SANITIZER_FUCHSIA
16 #include "asan_interceptors.h"
17 #include "asan_internal.h"
18 #include "asan_stack.h"
19 #include "asan_thread.h"
21 #include <limits.h>
22 #include <zircon/sanitizer.h>
23 #include <zircon/syscalls.h>
24 #include <zircon/threads.h>
26 namespace __asan {
28 // The system already set up the shadow memory for us.
29 // __sanitizer::GetMaxUserVirtualAddress has already been called by
30 // AsanInitInternal->InitializeHighMemEnd (asan_rtl.cc).
31 // Just do some additional sanity checks here.
32 void InitializeShadowMemory() {
33 if (Verbosity()) PrintAddressSpaceLayout();
35 // Make sure SHADOW_OFFSET doesn't use __asan_shadow_memory_dynamic_address.
36 __asan_shadow_memory_dynamic_address = kDefaultShadowSentinel;
37 DCHECK(kLowShadowBeg != kDefaultShadowSentinel);
38 __asan_shadow_memory_dynamic_address = kLowShadowBeg;
40 CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1);
41 CHECK_EQ(kHighMemEnd, __sanitizer::ShadowBounds.memory_limit - 1);
42 CHECK_EQ(kHighMemBeg, __sanitizer::ShadowBounds.shadow_limit);
43 CHECK_EQ(kHighShadowBeg, __sanitizer::ShadowBounds.shadow_base);
44 CHECK_EQ(kShadowGapEnd, __sanitizer::ShadowBounds.shadow_base - 1);
45 CHECK_EQ(kLowShadowEnd, 0);
46 CHECK_EQ(kLowShadowBeg, 0);
49 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
50 UNIMPLEMENTED();
53 void AsanCheckDynamicRTPrereqs() {}
54 void AsanCheckIncompatibleRT() {}
55 void InitializeAsanInterceptors() {}
57 void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
59 void InitializePlatformExceptionHandlers() {}
60 void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
61 UNIMPLEMENTED();
64 // We can use a plain thread_local variable for TSD.
65 static thread_local void *per_thread;
67 void *AsanTSDGet() { return per_thread; }
69 void AsanTSDSet(void *tsd) { per_thread = tsd; }
71 // There's no initialization needed, and the passed-in destructor
72 // will never be called. Instead, our own thread destruction hook
73 // (below) will call AsanThread::TSDDtor directly.
74 void AsanTSDInit(void (*destructor)(void *tsd)) {
75 DCHECK(destructor == &PlatformTSDDtor);
78 void PlatformTSDDtor(void *tsd) { UNREACHABLE(__func__); }
80 static inline size_t AsanThreadMmapSize() {
81 return RoundUpTo(sizeof(AsanThread), PAGE_SIZE);
84 struct AsanThread::InitOptions {
85 uptr stack_bottom, stack_size;
88 // Shared setup between thread creation and startup for the initial thread.
89 static AsanThread *CreateAsanThread(StackTrace *stack, u32 parent_tid,
90 uptr user_id, bool detached,
91 const char *name, uptr stack_bottom,
92 uptr stack_size) {
93 // In lieu of AsanThread::Create.
94 AsanThread *thread = (AsanThread *)MmapOrDie(AsanThreadMmapSize(), __func__);
96 AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
97 u32 tid =
98 asanThreadRegistry().CreateThread(user_id, detached, parent_tid, &args);
99 asanThreadRegistry().SetThreadName(tid, name);
101 // On other systems, AsanThread::Init() is called from the new
102 // thread itself. But on Fuchsia we already know the stack address
103 // range beforehand, so we can do most of the setup right now.
104 const AsanThread::InitOptions options = {stack_bottom, stack_size};
105 thread->Init(&options);
107 return thread;
110 // This gets the same arguments passed to Init by CreateAsanThread, above.
111 // We're in the creator thread before the new thread is actually started,
112 // but its stack address range is already known. We don't bother tracking
113 // the static TLS address range because the system itself already uses an
114 // ASan-aware allocator for that.
115 void AsanThread::SetThreadStackAndTls(const AsanThread::InitOptions *options) {
116 DCHECK_NE(GetCurrentThread(), this);
117 DCHECK_NE(GetCurrentThread(), nullptr);
118 CHECK_NE(options->stack_bottom, 0);
119 CHECK_NE(options->stack_size, 0);
120 stack_bottom_ = options->stack_bottom;
121 stack_top_ = options->stack_bottom + options->stack_size;
124 // Called by __asan::AsanInitInternal (asan_rtl.c).
125 AsanThread *CreateMainThread() {
126 thrd_t self = thrd_current();
127 char name[ZX_MAX_NAME_LEN];
128 CHECK_NE(__sanitizer::MainThreadStackBase, 0);
129 CHECK_GT(__sanitizer::MainThreadStackSize, 0);
130 AsanThread *t = CreateAsanThread(
131 nullptr, 0, reinterpret_cast<uptr>(self), true,
132 _zx_object_get_property(thrd_get_zx_handle(self), ZX_PROP_NAME, name,
133 sizeof(name)) == ZX_OK
134 ? name
135 : nullptr,
136 __sanitizer::MainThreadStackBase, __sanitizer::MainThreadStackSize);
137 SetCurrentThread(t);
138 return t;
141 // This is called before each thread creation is attempted. So, in
142 // its first call, the calling thread is the initial and sole thread.
143 static void *BeforeThreadCreateHook(uptr user_id, bool detached,
144 const char *name, uptr stack_bottom,
145 uptr stack_size) {
146 EnsureMainThreadIDIsCorrect();
147 // Strict init-order checking is thread-hostile.
148 if (flags()->strict_init_order) StopInitOrderChecking();
150 GET_STACK_TRACE_THREAD;
151 u32 parent_tid = GetCurrentTidOrInvalid();
153 return CreateAsanThread(&stack, parent_tid, user_id, detached, name,
154 stack_bottom, stack_size);
157 // This is called after creating a new thread (in the creating thread),
158 // with the pointer returned by BeforeThreadCreateHook (above).
159 static void ThreadCreateHook(void *hook, bool aborted) {
160 AsanThread *thread = static_cast<AsanThread *>(hook);
161 if (!aborted) {
162 // The thread was created successfully.
163 // ThreadStartHook is already running in the new thread.
164 } else {
165 // The thread wasn't created after all.
166 // Clean up everything we set up in BeforeThreadCreateHook.
167 asanThreadRegistry().FinishThread(thread->tid());
168 UnmapOrDie(thread, AsanThreadMmapSize());
172 // This is called in the newly-created thread before it runs anything else,
173 // with the pointer returned by BeforeThreadCreateHook (above).
174 // cf. asan_interceptors.cc:asan_thread_start
175 static void ThreadStartHook(void *hook, uptr os_id) {
176 AsanThread *thread = static_cast<AsanThread *>(hook);
177 SetCurrentThread(thread);
179 // In lieu of AsanThread::ThreadStart.
180 asanThreadRegistry().StartThread(thread->tid(), os_id, /*workerthread*/ false,
181 nullptr);
184 // Each thread runs this just before it exits,
185 // with the pointer returned by BeforeThreadCreateHook (above).
186 // All per-thread destructors have already been called.
187 static void ThreadExitHook(void *hook, uptr os_id) {
188 AsanThread::TSDDtor(per_thread);
191 } // namespace __asan
193 // These are declared (in extern "C") by <zircon/sanitizer.h>.
194 // The system runtime will call our definitions directly.
196 void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,
197 const char *name, void *stack_base,
198 size_t stack_size) {
199 return __asan::BeforeThreadCreateHook(
200 reinterpret_cast<uptr>(thread), detached, name,
201 reinterpret_cast<uptr>(stack_base), stack_size);
204 void __sanitizer_thread_create_hook(void *hook, thrd_t thread, int error) {
205 __asan::ThreadCreateHook(hook, error != thrd_success);
208 void __sanitizer_thread_start_hook(void *hook, thrd_t self) {
209 __asan::ThreadStartHook(hook, reinterpret_cast<uptr>(self));
212 void __sanitizer_thread_exit_hook(void *hook, thrd_t self) {
213 __asan::ThreadExitHook(hook, reinterpret_cast<uptr>(self));
216 #endif // SANITIZER_FUCHSIA