1fe6060f1SDimitry Andric //===-- hwasan_fuchsia.cpp --------------------------------------*- C++ -*-===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric ///
9fe6060f1SDimitry Andric /// \file
10fe6060f1SDimitry Andric /// This file is a part of HWAddressSanitizer and contains Fuchsia-specific
11fe6060f1SDimitry Andric /// code.
12fe6060f1SDimitry Andric ///
13fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
14fe6060f1SDimitry Andric
15fe6060f1SDimitry Andric #include "sanitizer_common/sanitizer_fuchsia.h"
16fe6060f1SDimitry Andric #if SANITIZER_FUCHSIA
17fe6060f1SDimitry Andric
1881ad6265SDimitry Andric #include <zircon/features.h>
1981ad6265SDimitry Andric #include <zircon/syscalls.h>
2081ad6265SDimitry Andric
21fe6060f1SDimitry Andric #include "hwasan.h"
22fe6060f1SDimitry Andric #include "hwasan_interface_internal.h"
23fe6060f1SDimitry Andric #include "hwasan_report.h"
24fe6060f1SDimitry Andric #include "hwasan_thread.h"
25fe6060f1SDimitry Andric #include "hwasan_thread_list.h"
26fe6060f1SDimitry Andric
27fe6060f1SDimitry Andric // This TLS variable contains the location of the stack ring buffer and can be
28fe6060f1SDimitry Andric // used to always find the hwasan thread object associated with the current
29fe6060f1SDimitry Andric // running thread.
30fe6060f1SDimitry Andric [[gnu::tls_model("initial-exec")]]
31fe6060f1SDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
32fe6060f1SDimitry Andric THREADLOCAL uptr __hwasan_tls;
33fe6060f1SDimitry Andric
34fe6060f1SDimitry Andric namespace __hwasan {
35fe6060f1SDimitry Andric
InitShadow()36fe6060f1SDimitry Andric bool InitShadow() {
37fe6060f1SDimitry Andric __sanitizer::InitShadowBounds();
38fe6060f1SDimitry Andric CHECK_NE(__sanitizer::ShadowBounds.shadow_limit, 0);
39fe6060f1SDimitry Andric
40fe6060f1SDimitry Andric // These variables are used by MemIsShadow for asserting we have a correct
41fe6060f1SDimitry Andric // shadow address. On Fuchsia, we only have one region of shadow, so the
42fe6060f1SDimitry Andric // bounds of Low shadow can be zero while High shadow represents the true
43fe6060f1SDimitry Andric // bounds. Note that these are inclusive ranges.
44fe6060f1SDimitry Andric kLowShadowStart = 0;
45fe6060f1SDimitry Andric kLowShadowEnd = 0;
46fe6060f1SDimitry Andric kHighShadowStart = __sanitizer::ShadowBounds.shadow_base;
47fe6060f1SDimitry Andric kHighShadowEnd = __sanitizer::ShadowBounds.shadow_limit - 1;
48fe6060f1SDimitry Andric
49fe6060f1SDimitry Andric return true;
50fe6060f1SDimitry Andric }
51fe6060f1SDimitry Andric
MemIsApp(uptr p)52fe6060f1SDimitry Andric bool MemIsApp(uptr p) {
53fe6060f1SDimitry Andric CHECK(GetTagFromPointer(p) == 0);
54fe6060f1SDimitry Andric return __sanitizer::ShadowBounds.shadow_limit <= p &&
55fe6060f1SDimitry Andric p <= (__sanitizer::ShadowBounds.memory_limit - 1);
56fe6060f1SDimitry Andric }
57fe6060f1SDimitry Andric
58fe6060f1SDimitry Andric // These are known parameters passed to the hwasan runtime on thread creation.
59fe6060f1SDimitry Andric struct Thread::InitState {
60fe6060f1SDimitry Andric uptr stack_bottom, stack_top;
61fe6060f1SDimitry Andric };
62fe6060f1SDimitry Andric
63fe6060f1SDimitry Andric static void FinishThreadInitialization(Thread *thread);
64fe6060f1SDimitry Andric
InitThreads()65fe6060f1SDimitry Andric void InitThreads() {
66fe6060f1SDimitry Andric // This is the minimal alignment needed for the storage where hwasan threads
67fe6060f1SDimitry Andric // and their stack ring buffers are placed. This alignment is necessary so the
68fe6060f1SDimitry Andric // stack ring buffer can perform a simple calculation to get the next element
69fe6060f1SDimitry Andric // in the RB. The instructions for this calculation are emitted by the
70fe6060f1SDimitry Andric // compiler. (Full explanation in hwasan_thread_list.h.)
71fe6060f1SDimitry Andric uptr alloc_size = UINT64_C(1) << kShadowBaseAlignment;
72fe6060f1SDimitry Andric uptr thread_start = reinterpret_cast<uptr>(
73fe6060f1SDimitry Andric MmapAlignedOrDieOnFatalError(alloc_size, alloc_size, __func__));
74fe6060f1SDimitry Andric
75fe6060f1SDimitry Andric InitThreadList(thread_start, alloc_size);
76fe6060f1SDimitry Andric
77fe6060f1SDimitry Andric // Create the hwasan thread object for the current (main) thread. Stack info
78fe6060f1SDimitry Andric // for this thread is known from information passed via
79fe6060f1SDimitry Andric // __sanitizer_startup_hook.
80fe6060f1SDimitry Andric const Thread::InitState state = {
81fe6060f1SDimitry Andric .stack_bottom = __sanitizer::MainThreadStackBase,
82fe6060f1SDimitry Andric .stack_top =
83fe6060f1SDimitry Andric __sanitizer::MainThreadStackBase + __sanitizer::MainThreadStackSize,
84fe6060f1SDimitry Andric };
85fe6060f1SDimitry Andric FinishThreadInitialization(hwasanThreadList().CreateCurrentThread(&state));
86fe6060f1SDimitry Andric }
87fe6060f1SDimitry Andric
GetCurrentThreadLongPtr()88fe6060f1SDimitry Andric uptr *GetCurrentThreadLongPtr() { return &__hwasan_tls; }
89fe6060f1SDimitry Andric
90fe6060f1SDimitry Andric // This is called from the parent thread before the new thread is created. Here
91fe6060f1SDimitry Andric // we can propagate known info like the stack bounds to Thread::Init before
92fe6060f1SDimitry Andric // jumping into the thread. We cannot initialize the stack ring buffer yet since
93fe6060f1SDimitry Andric // we have not entered the new thread.
BeforeThreadCreateHook(uptr user_id,bool detached,const char * name,uptr stack_bottom,uptr stack_size)94fe6060f1SDimitry Andric static void *BeforeThreadCreateHook(uptr user_id, bool detached,
95fe6060f1SDimitry Andric const char *name, uptr stack_bottom,
96fe6060f1SDimitry Andric uptr stack_size) {
97fe6060f1SDimitry Andric const Thread::InitState state = {
98fe6060f1SDimitry Andric .stack_bottom = stack_bottom,
99fe6060f1SDimitry Andric .stack_top = stack_bottom + stack_size,
100fe6060f1SDimitry Andric };
101fe6060f1SDimitry Andric return hwasanThreadList().CreateCurrentThread(&state);
102fe6060f1SDimitry Andric }
103fe6060f1SDimitry Andric
104fe6060f1SDimitry Andric // This sets the stack top and bottom according to the InitState passed to
105fe6060f1SDimitry Andric // CreateCurrentThread above.
InitStackAndTls(const InitState * state)106fe6060f1SDimitry Andric void Thread::InitStackAndTls(const InitState *state) {
107fe6060f1SDimitry Andric CHECK_NE(state->stack_bottom, 0);
108fe6060f1SDimitry Andric CHECK_NE(state->stack_top, 0);
109fe6060f1SDimitry Andric stack_bottom_ = state->stack_bottom;
110fe6060f1SDimitry Andric stack_top_ = state->stack_top;
111fe6060f1SDimitry Andric tls_end_ = tls_begin_ = 0;
112fe6060f1SDimitry Andric }
113fe6060f1SDimitry Andric
114fe6060f1SDimitry Andric // This is called after creating a new thread with the pointer returned by
115fe6060f1SDimitry Andric // BeforeThreadCreateHook. We are still in the creating thread and should check
116fe6060f1SDimitry Andric // if it was actually created correctly.
ThreadCreateHook(void * hook,bool aborted)117fe6060f1SDimitry Andric static void ThreadCreateHook(void *hook, bool aborted) {
118fe6060f1SDimitry Andric Thread *thread = static_cast<Thread *>(hook);
119fe6060f1SDimitry Andric if (!aborted) {
120fe6060f1SDimitry Andric // The thread was created successfully.
121fe6060f1SDimitry Andric // ThreadStartHook can already be running in the new thread.
122fe6060f1SDimitry Andric } else {
123fe6060f1SDimitry Andric // The thread wasn't created after all.
124fe6060f1SDimitry Andric // Clean up everything we set up in BeforeThreadCreateHook.
125fe6060f1SDimitry Andric atomic_signal_fence(memory_order_seq_cst);
126fe6060f1SDimitry Andric hwasanThreadList().ReleaseThread(thread);
127fe6060f1SDimitry Andric }
128fe6060f1SDimitry Andric }
129fe6060f1SDimitry Andric
130fe6060f1SDimitry Andric // This is called in the newly-created thread before it runs anything else,
131fe6060f1SDimitry Andric // with the pointer returned by BeforeThreadCreateHook (above). Here we can
132fe6060f1SDimitry Andric // setup the stack ring buffer.
ThreadStartHook(void * hook,thrd_t self)133fe6060f1SDimitry Andric static void ThreadStartHook(void *hook, thrd_t self) {
134fe6060f1SDimitry Andric Thread *thread = static_cast<Thread *>(hook);
135fe6060f1SDimitry Andric FinishThreadInitialization(thread);
136349cc55cSDimitry Andric thread->EnsureRandomStateInited();
137fe6060f1SDimitry Andric }
138fe6060f1SDimitry Andric
139fe6060f1SDimitry Andric // This is the function that sets up the stack ring buffer and enables us to use
140fe6060f1SDimitry Andric // GetCurrentThread. This function should only be called while IN the thread
141fe6060f1SDimitry Andric // that we want to create the hwasan thread object for so __hwasan_tls can be
142fe6060f1SDimitry Andric // properly referenced.
FinishThreadInitialization(Thread * thread)143fe6060f1SDimitry Andric static void FinishThreadInitialization(Thread *thread) {
144fe6060f1SDimitry Andric CHECK_NE(thread, nullptr);
145fe6060f1SDimitry Andric
146fe6060f1SDimitry Andric // The ring buffer is located immediately before the thread object.
147fe6060f1SDimitry Andric uptr stack_buffer_size = hwasanThreadList().GetRingBufferSize();
148fe6060f1SDimitry Andric uptr stack_buffer_start = reinterpret_cast<uptr>(thread) - stack_buffer_size;
149fe6060f1SDimitry Andric thread->InitStackRingBuffer(stack_buffer_start, stack_buffer_size);
150fe6060f1SDimitry Andric }
151fe6060f1SDimitry Andric
ThreadExitHook(void * hook,thrd_t self)152fe6060f1SDimitry Andric static void ThreadExitHook(void *hook, thrd_t self) {
153fe6060f1SDimitry Andric Thread *thread = static_cast<Thread *>(hook);
154fe6060f1SDimitry Andric atomic_signal_fence(memory_order_seq_cst);
155fe6060f1SDimitry Andric hwasanThreadList().ReleaseThread(thread);
156fe6060f1SDimitry Andric }
157fe6060f1SDimitry Andric
TagMemoryAligned(uptr p,uptr size,tag_t tag)158fe6060f1SDimitry Andric uptr TagMemoryAligned(uptr p, uptr size, tag_t tag) {
159fe6060f1SDimitry Andric CHECK(IsAligned(p, kShadowAlignment));
160fe6060f1SDimitry Andric CHECK(IsAligned(size, kShadowAlignment));
161fe6060f1SDimitry Andric __sanitizer_fill_shadow(p, size, tag,
162fe6060f1SDimitry Andric common_flags()->clear_shadow_mmap_threshold);
163fe6060f1SDimitry Andric return AddTagToPointer(p, tag);
164fe6060f1SDimitry Andric }
165fe6060f1SDimitry Andric
166fe6060f1SDimitry Andric // Not implemented because Fuchsia does not use signal handlers.
HwasanOnDeadlySignal(int signo,void * info,void * context)167fe6060f1SDimitry Andric void HwasanOnDeadlySignal(int signo, void *info, void *context) {}
168fe6060f1SDimitry Andric
169fe6060f1SDimitry Andric // Not implemented because Fuchsia does not use interceptors.
InitializeInterceptors()170fe6060f1SDimitry Andric void InitializeInterceptors() {}
171fe6060f1SDimitry Andric
172fe6060f1SDimitry Andric // Not implemented because this is only relevant for Android.
AndroidTestTlsSlot()173fe6060f1SDimitry Andric void AndroidTestTlsSlot() {}
174fe6060f1SDimitry Andric
175fe6060f1SDimitry Andric // TSD was normally used on linux as a means of calling the hwasan thread exit
176fe6060f1SDimitry Andric // handler passed to pthread_key_create. This is not needed on Fuchsia because
177fe6060f1SDimitry Andric // we will be using __sanitizer_thread_exit_hook.
HwasanTSDInit()178fe6060f1SDimitry Andric void HwasanTSDInit() {}
HwasanTSDThreadInit()179fe6060f1SDimitry Andric void HwasanTSDThreadInit() {}
180fe6060f1SDimitry Andric
181fe6060f1SDimitry Andric // On linux, this just would call `atexit(HwasanAtExit)`. The functions in
182fe6060f1SDimitry Andric // HwasanAtExit are unimplemented for Fuchsia and effectively no-ops, so this
183fe6060f1SDimitry Andric // function is unneeded.
InstallAtExitHandler()184fe6060f1SDimitry Andric void InstallAtExitHandler() {}
185fe6060f1SDimitry Andric
HwasanInstallAtForkHandler()186349cc55cSDimitry Andric void HwasanInstallAtForkHandler() {}
187349cc55cSDimitry Andric
InstallAtExitCheckLeaks()188*bdd1243dSDimitry Andric void InstallAtExitCheckLeaks() {}
189*bdd1243dSDimitry Andric
InitializeOsSupport()19081ad6265SDimitry Andric void InitializeOsSupport() {
19181ad6265SDimitry Andric #ifdef __aarch64__
19281ad6265SDimitry Andric uint32_t features = 0;
19381ad6265SDimitry Andric CHECK_EQ(zx_system_get_features(ZX_FEATURE_KIND_ADDRESS_TAGGING, &features),
19481ad6265SDimitry Andric ZX_OK);
195753f127fSDimitry Andric if (!(features & ZX_ARM64_FEATURE_ADDRESS_TAGGING_TBI) &&
19681ad6265SDimitry Andric flags()->fail_without_syscall_abi) {
19781ad6265SDimitry Andric Printf(
198753f127fSDimitry Andric "FATAL: HWAddressSanitizer requires "
199753f127fSDimitry Andric "ZX_ARM64_FEATURE_ADDRESS_TAGGING_TBI.\n");
20081ad6265SDimitry Andric Die();
20181ad6265SDimitry Andric }
20281ad6265SDimitry Andric #endif
20381ad6265SDimitry Andric }
204fe6060f1SDimitry Andric
205fe6060f1SDimitry Andric } // namespace __hwasan
206fe6060f1SDimitry Andric
207*bdd1243dSDimitry Andric namespace __lsan {
208*bdd1243dSDimitry Andric
UseExitcodeOnLeak()209*bdd1243dSDimitry Andric bool UseExitcodeOnLeak() { return __hwasan::flags()->halt_on_error; }
210*bdd1243dSDimitry Andric
211*bdd1243dSDimitry Andric } // namespace __lsan
212*bdd1243dSDimitry Andric
213fe6060f1SDimitry Andric extern "C" {
214fe6060f1SDimitry Andric
__sanitizer_before_thread_create_hook(thrd_t thread,bool detached,const char * name,void * stack_base,size_t stack_size)215fe6060f1SDimitry Andric void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,
216fe6060f1SDimitry Andric const char *name, void *stack_base,
217fe6060f1SDimitry Andric size_t stack_size) {
218fe6060f1SDimitry Andric return __hwasan::BeforeThreadCreateHook(
219fe6060f1SDimitry Andric reinterpret_cast<uptr>(thread), detached, name,
220fe6060f1SDimitry Andric reinterpret_cast<uptr>(stack_base), stack_size);
221fe6060f1SDimitry Andric }
222fe6060f1SDimitry Andric
__sanitizer_thread_create_hook(void * hook,thrd_t thread,int error)223fe6060f1SDimitry Andric void __sanitizer_thread_create_hook(void *hook, thrd_t thread, int error) {
224fe6060f1SDimitry Andric __hwasan::ThreadCreateHook(hook, error != thrd_success);
225fe6060f1SDimitry Andric }
226fe6060f1SDimitry Andric
__sanitizer_thread_start_hook(void * hook,thrd_t self)227fe6060f1SDimitry Andric void __sanitizer_thread_start_hook(void *hook, thrd_t self) {
228fe6060f1SDimitry Andric __hwasan::ThreadStartHook(hook, reinterpret_cast<uptr>(self));
229fe6060f1SDimitry Andric }
230fe6060f1SDimitry Andric
__sanitizer_thread_exit_hook(void * hook,thrd_t self)231fe6060f1SDimitry Andric void __sanitizer_thread_exit_hook(void *hook, thrd_t self) {
232fe6060f1SDimitry Andric __hwasan::ThreadExitHook(hook, self);
233fe6060f1SDimitry Andric }
234fe6060f1SDimitry Andric
__sanitizer_module_loaded(const struct dl_phdr_info * info,size_t)235*bdd1243dSDimitry Andric void __sanitizer_module_loaded(const struct dl_phdr_info *info, size_t) {
236*bdd1243dSDimitry Andric __hwasan_library_loaded(info->dlpi_addr, info->dlpi_phdr, info->dlpi_phnum);
237*bdd1243dSDimitry Andric }
238*bdd1243dSDimitry Andric
239fe6060f1SDimitry Andric } // extern "C"
240fe6060f1SDimitry Andric
241fe6060f1SDimitry Andric #endif // SANITIZER_FUCHSIA
242