asan_fuchsia.cpp revision 1.1.1.3 1 1.1 mrg //===-- asan_fuchsia.cpp -------------------------------------------------===//
2 1.1 mrg //
3 1.1 mrg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 1.1 mrg // See https://llvm.org/LICENSE.txt for license information.
5 1.1 mrg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 1.1 mrg //
7 1.1 mrg //===---------------------------------------------------------------------===//
8 1.1 mrg //
9 1.1 mrg // This file is a part of AddressSanitizer, an address sanity checker.
10 1.1 mrg //
11 1.1 mrg // Fuchsia-specific details.
12 1.1 mrg //===---------------------------------------------------------------------===//
13 1.1 mrg
14 1.1 mrg #include "sanitizer_common/sanitizer_fuchsia.h"
15 1.1 mrg #if SANITIZER_FUCHSIA
16 1.1 mrg
17 1.1 mrg #include <limits.h>
18 1.1 mrg #include <zircon/sanitizer.h>
19 1.1 mrg #include <zircon/syscalls.h>
20 1.1 mrg #include <zircon/threads.h>
21 1.1 mrg
22 1.1.1.3 mrg # include "asan_interceptors.h"
23 1.1.1.3 mrg # include "asan_internal.h"
24 1.1.1.3 mrg # include "asan_stack.h"
25 1.1.1.3 mrg # include "asan_thread.h"
26 1.1.1.3 mrg # include "lsan/lsan_common.h"
27 1.1.1.3 mrg
28 1.1 mrg namespace __asan {
29 1.1 mrg
30 1.1 mrg // The system already set up the shadow memory for us.
31 1.1 mrg // __sanitizer::GetMaxUserVirtualAddress has already been called by
32 1.1 mrg // AsanInitInternal->InitializeHighMemEnd (asan_rtl.cpp).
33 1.1 mrg // Just do some additional sanity checks here.
34 1.1 mrg void InitializeShadowMemory() {
35 1.1.1.2 mrg if (Verbosity())
36 1.1.1.2 mrg PrintAddressSpaceLayout();
37 1.1 mrg
38 1.1 mrg // Make sure SHADOW_OFFSET doesn't use __asan_shadow_memory_dynamic_address.
39 1.1 mrg __asan_shadow_memory_dynamic_address = kDefaultShadowSentinel;
40 1.1 mrg DCHECK(kLowShadowBeg != kDefaultShadowSentinel);
41 1.1 mrg __asan_shadow_memory_dynamic_address = kLowShadowBeg;
42 1.1 mrg
43 1.1 mrg CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1);
44 1.1 mrg CHECK_EQ(kHighMemEnd, __sanitizer::ShadowBounds.memory_limit - 1);
45 1.1 mrg CHECK_EQ(kHighMemBeg, __sanitizer::ShadowBounds.shadow_limit);
46 1.1 mrg CHECK_EQ(kHighShadowBeg, __sanitizer::ShadowBounds.shadow_base);
47 1.1 mrg CHECK_EQ(kShadowGapEnd, __sanitizer::ShadowBounds.shadow_base - 1);
48 1.1 mrg CHECK_EQ(kLowShadowEnd, 0);
49 1.1 mrg CHECK_EQ(kLowShadowBeg, 0);
50 1.1 mrg }
51 1.1 mrg
52 1.1 mrg void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
53 1.1 mrg UNIMPLEMENTED();
54 1.1 mrg }
55 1.1 mrg
56 1.1 mrg void AsanCheckDynamicRTPrereqs() {}
57 1.1 mrg void AsanCheckIncompatibleRT() {}
58 1.1 mrg void InitializeAsanInterceptors() {}
59 1.1 mrg
60 1.1 mrg void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
61 1.1 mrg
62 1.1 mrg void InitializePlatformExceptionHandlers() {}
63 1.1 mrg void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
64 1.1 mrg UNIMPLEMENTED();
65 1.1 mrg }
66 1.1 mrg
67 1.1.1.2 mrg bool PlatformUnpoisonStacks() {
68 1.1.1.2 mrg // The current sp might not point to the default stack. This
69 1.1.1.2 mrg // could be because we are in a crash stack from fuzzing for example.
70 1.1.1.2 mrg // Unpoison the default stack and the current stack page.
71 1.1.1.2 mrg AsanThread *curr_thread = GetCurrentThread();
72 1.1.1.2 mrg CHECK(curr_thread != nullptr);
73 1.1.1.2 mrg uptr top = curr_thread->stack_top();
74 1.1.1.2 mrg uptr bottom = curr_thread->stack_bottom();
75 1.1.1.2 mrg // The default stack grows from top to bottom. (bottom < top).
76 1.1.1.2 mrg
77 1.1.1.2 mrg uptr local_stack = reinterpret_cast<uptr>(__builtin_frame_address(0));
78 1.1.1.2 mrg if (local_stack >= bottom && local_stack <= top) {
79 1.1.1.2 mrg // The current stack is the default stack.
80 1.1.1.2 mrg // We only need to unpoison from where we are using until the end.
81 1.1.1.2 mrg bottom = RoundDownTo(local_stack, GetPageSize());
82 1.1.1.2 mrg UnpoisonStack(bottom, top, "default");
83 1.1.1.2 mrg } else {
84 1.1.1.2 mrg // The current stack is not the default stack.
85 1.1.1.2 mrg // Unpoison the entire default stack and the current stack page.
86 1.1.1.2 mrg UnpoisonStack(bottom, top, "default");
87 1.1.1.2 mrg bottom = RoundDownTo(local_stack, GetPageSize());
88 1.1.1.2 mrg top = bottom + GetPageSize();
89 1.1.1.2 mrg UnpoisonStack(bottom, top, "unknown");
90 1.1.1.2 mrg return true;
91 1.1.1.2 mrg }
92 1.1.1.2 mrg
93 1.1.1.2 mrg return false;
94 1.1.1.2 mrg }
95 1.1.1.2 mrg
96 1.1 mrg // We can use a plain thread_local variable for TSD.
97 1.1 mrg static thread_local void *per_thread;
98 1.1 mrg
99 1.1 mrg void *AsanTSDGet() { return per_thread; }
100 1.1 mrg
101 1.1 mrg void AsanTSDSet(void *tsd) { per_thread = tsd; }
102 1.1 mrg
103 1.1 mrg // There's no initialization needed, and the passed-in destructor
104 1.1 mrg // will never be called. Instead, our own thread destruction hook
105 1.1 mrg // (below) will call AsanThread::TSDDtor directly.
106 1.1 mrg void AsanTSDInit(void (*destructor)(void *tsd)) {
107 1.1 mrg DCHECK(destructor == &PlatformTSDDtor);
108 1.1 mrg }
109 1.1 mrg
110 1.1 mrg void PlatformTSDDtor(void *tsd) { UNREACHABLE(__func__); }
111 1.1 mrg
112 1.1 mrg static inline size_t AsanThreadMmapSize() {
113 1.1.1.2 mrg return RoundUpTo(sizeof(AsanThread), _zx_system_get_page_size());
114 1.1 mrg }
115 1.1 mrg
116 1.1 mrg struct AsanThread::InitOptions {
117 1.1 mrg uptr stack_bottom, stack_size;
118 1.1 mrg };
119 1.1 mrg
120 1.1 mrg // Shared setup between thread creation and startup for the initial thread.
121 1.1 mrg static AsanThread *CreateAsanThread(StackTrace *stack, u32 parent_tid,
122 1.1.1.3 mrg bool detached, const char *name) {
123 1.1 mrg // In lieu of AsanThread::Create.
124 1.1 mrg AsanThread *thread = (AsanThread *)MmapOrDie(AsanThreadMmapSize(), __func__);
125 1.1 mrg
126 1.1 mrg AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
127 1.1.1.3 mrg u32 tid = asanThreadRegistry().CreateThread(0, detached, parent_tid, &args);
128 1.1 mrg asanThreadRegistry().SetThreadName(tid, name);
129 1.1 mrg
130 1.1 mrg return thread;
131 1.1 mrg }
132 1.1 mrg
133 1.1 mrg // This gets the same arguments passed to Init by CreateAsanThread, above.
134 1.1 mrg // We're in the creator thread before the new thread is actually started,
135 1.1 mrg // but its stack address range is already known. We don't bother tracking
136 1.1 mrg // the static TLS address range because the system itself already uses an
137 1.1 mrg // ASan-aware allocator for that.
138 1.1 mrg void AsanThread::SetThreadStackAndTls(const AsanThread::InitOptions *options) {
139 1.1 mrg DCHECK_NE(GetCurrentThread(), this);
140 1.1 mrg DCHECK_NE(GetCurrentThread(), nullptr);
141 1.1 mrg CHECK_NE(options->stack_bottom, 0);
142 1.1 mrg CHECK_NE(options->stack_size, 0);
143 1.1 mrg stack_bottom_ = options->stack_bottom;
144 1.1 mrg stack_top_ = options->stack_bottom + options->stack_size;
145 1.1 mrg }
146 1.1 mrg
147 1.1 mrg // Called by __asan::AsanInitInternal (asan_rtl.c).
148 1.1 mrg AsanThread *CreateMainThread() {
149 1.1 mrg thrd_t self = thrd_current();
150 1.1 mrg char name[ZX_MAX_NAME_LEN];
151 1.1 mrg CHECK_NE(__sanitizer::MainThreadStackBase, 0);
152 1.1 mrg CHECK_GT(__sanitizer::MainThreadStackSize, 0);
153 1.1 mrg AsanThread *t = CreateAsanThread(
154 1.1.1.3 mrg nullptr, 0, true,
155 1.1 mrg _zx_object_get_property(thrd_get_zx_handle(self), ZX_PROP_NAME, name,
156 1.1 mrg sizeof(name)) == ZX_OK
157 1.1 mrg ? name
158 1.1.1.2 mrg : nullptr);
159 1.1.1.2 mrg // We need to set the current thread before calling AsanThread::Init() below,
160 1.1.1.2 mrg // since it reads the thread ID.
161 1.1 mrg SetCurrentThread(t);
162 1.1.1.2 mrg DCHECK_EQ(t->tid(), 0);
163 1.1.1.2 mrg
164 1.1.1.2 mrg const AsanThread::InitOptions options = {__sanitizer::MainThreadStackBase,
165 1.1.1.2 mrg __sanitizer::MainThreadStackSize};
166 1.1.1.2 mrg t->Init(&options);
167 1.1.1.2 mrg
168 1.1 mrg return t;
169 1.1 mrg }
170 1.1 mrg
171 1.1 mrg // This is called before each thread creation is attempted. So, in
172 1.1 mrg // its first call, the calling thread is the initial and sole thread.
173 1.1 mrg static void *BeforeThreadCreateHook(uptr user_id, bool detached,
174 1.1 mrg const char *name, uptr stack_bottom,
175 1.1 mrg uptr stack_size) {
176 1.1 mrg EnsureMainThreadIDIsCorrect();
177 1.1 mrg // Strict init-order checking is thread-hostile.
178 1.1.1.2 mrg if (flags()->strict_init_order)
179 1.1.1.2 mrg StopInitOrderChecking();
180 1.1 mrg
181 1.1 mrg GET_STACK_TRACE_THREAD;
182 1.1 mrg u32 parent_tid = GetCurrentTidOrInvalid();
183 1.1 mrg
184 1.1.1.3 mrg AsanThread *thread = CreateAsanThread(&stack, parent_tid, detached, name);
185 1.1.1.2 mrg
186 1.1.1.2 mrg // On other systems, AsanThread::Init() is called from the new
187 1.1.1.2 mrg // thread itself. But on Fuchsia we already know the stack address
188 1.1.1.2 mrg // range beforehand, so we can do most of the setup right now.
189 1.1.1.2 mrg const AsanThread::InitOptions options = {stack_bottom, stack_size};
190 1.1.1.2 mrg thread->Init(&options);
191 1.1.1.2 mrg return thread;
192 1.1 mrg }
193 1.1 mrg
194 1.1 mrg // This is called after creating a new thread (in the creating thread),
195 1.1 mrg // with the pointer returned by BeforeThreadCreateHook (above).
196 1.1 mrg static void ThreadCreateHook(void *hook, bool aborted) {
197 1.1 mrg AsanThread *thread = static_cast<AsanThread *>(hook);
198 1.1 mrg if (!aborted) {
199 1.1 mrg // The thread was created successfully.
200 1.1 mrg // ThreadStartHook is already running in the new thread.
201 1.1 mrg } else {
202 1.1 mrg // The thread wasn't created after all.
203 1.1 mrg // Clean up everything we set up in BeforeThreadCreateHook.
204 1.1 mrg asanThreadRegistry().FinishThread(thread->tid());
205 1.1 mrg UnmapOrDie(thread, AsanThreadMmapSize());
206 1.1 mrg }
207 1.1 mrg }
208 1.1 mrg
209 1.1 mrg // This is called in the newly-created thread before it runs anything else,
210 1.1 mrg // with the pointer returned by BeforeThreadCreateHook (above).
211 1.1 mrg // cf. asan_interceptors.cpp:asan_thread_start
212 1.1 mrg static void ThreadStartHook(void *hook, uptr os_id) {
213 1.1 mrg AsanThread *thread = static_cast<AsanThread *>(hook);
214 1.1 mrg SetCurrentThread(thread);
215 1.1 mrg
216 1.1 mrg // In lieu of AsanThread::ThreadStart.
217 1.1 mrg asanThreadRegistry().StartThread(thread->tid(), os_id, ThreadType::Regular,
218 1.1 mrg nullptr);
219 1.1 mrg }
220 1.1 mrg
221 1.1 mrg // Each thread runs this just before it exits,
222 1.1 mrg // with the pointer returned by BeforeThreadCreateHook (above).
223 1.1 mrg // All per-thread destructors have already been called.
224 1.1 mrg static void ThreadExitHook(void *hook, uptr os_id) {
225 1.1 mrg AsanThread::TSDDtor(per_thread);
226 1.1 mrg }
227 1.1 mrg
228 1.1 mrg bool HandleDlopenInit() {
229 1.1 mrg // Not supported on this platform.
230 1.1 mrg static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
231 1.1 mrg "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
232 1.1 mrg return false;
233 1.1 mrg }
234 1.1 mrg
235 1.1.1.2 mrg void FlushUnneededASanShadowMemory(uptr p, uptr size) {
236 1.1.1.2 mrg __sanitizer_fill_shadow(p, size, 0, 0);
237 1.1.1.2 mrg }
238 1.1.1.2 mrg
239 1.1.1.3 mrg // On Fuchsia, leak detection is done by a special hook after atexit hooks.
240 1.1.1.3 mrg // So this doesn't install any atexit hook like on other platforms.
241 1.1.1.3 mrg void InstallAtExitCheckLeaks() {}
242 1.1.1.3 mrg
243 1.1 mrg } // namespace __asan
244 1.1 mrg
245 1.1.1.3 mrg namespace __lsan {
246 1.1.1.3 mrg
247 1.1.1.3 mrg bool UseExitcodeOnLeak() { return __asan::flags()->halt_on_error; }
248 1.1.1.3 mrg
249 1.1.1.3 mrg } // namespace __lsan
250 1.1.1.3 mrg
251 1.1 mrg // These are declared (in extern "C") by <zircon/sanitizer.h>.
252 1.1 mrg // The system runtime will call our definitions directly.
253 1.1 mrg
254 1.1 mrg void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,
255 1.1 mrg const char *name, void *stack_base,
256 1.1 mrg size_t stack_size) {
257 1.1 mrg return __asan::BeforeThreadCreateHook(
258 1.1 mrg reinterpret_cast<uptr>(thread), detached, name,
259 1.1 mrg reinterpret_cast<uptr>(stack_base), stack_size);
260 1.1 mrg }
261 1.1 mrg
262 1.1 mrg void __sanitizer_thread_create_hook(void *hook, thrd_t thread, int error) {
263 1.1 mrg __asan::ThreadCreateHook(hook, error != thrd_success);
264 1.1 mrg }
265 1.1 mrg
266 1.1 mrg void __sanitizer_thread_start_hook(void *hook, thrd_t self) {
267 1.1 mrg __asan::ThreadStartHook(hook, reinterpret_cast<uptr>(self));
268 1.1 mrg }
269 1.1 mrg
270 1.1 mrg void __sanitizer_thread_exit_hook(void *hook, thrd_t self) {
271 1.1 mrg __asan::ThreadExitHook(hook, reinterpret_cast<uptr>(self));
272 1.1 mrg }
273 1.1 mrg
274 1.1 mrg #endif // SANITIZER_FUCHSIA
275