1 //===-- asan_rtems.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of AddressSanitizer, an address sanity checker.
10 //
11 // RTEMS-specific details.
12 //===----------------------------------------------------------------------===//
13
14 #include "sanitizer_common/sanitizer_rtems.h"
15 #if SANITIZER_RTEMS
16
17 #include "asan_internal.h"
18 #include "asan_interceptors.h"
19 #include "asan_mapping.h"
20 #include "asan_poisoning.h"
21 #include "asan_report.h"
22 #include "asan_stack.h"
23 #include "sanitizer_common/sanitizer_common.h"
24 #include "sanitizer_common/sanitizer_libc.h"
25
26 #include <pthread.h>
27 #include <stdlib.h>
28
29 namespace __asan {
30
ResetShadowMemory()31 static void ResetShadowMemory() {
32 uptr shadow_start = SHADOW_OFFSET;
33 uptr shadow_end = MEM_TO_SHADOW(kMyriadMemoryEnd32);
34 uptr gap_start = MEM_TO_SHADOW(shadow_start);
35 uptr gap_end = MEM_TO_SHADOW(shadow_end);
36
37 REAL(memset)((void *)shadow_start, 0, shadow_end - shadow_start);
38 REAL(memset)((void *)gap_start, kAsanShadowGap, gap_end - gap_start);
39 }
40
InitializeShadowMemory()41 void InitializeShadowMemory() {
42 kHighMemEnd = 0;
43 kMidMemBeg = 0;
44 kMidMemEnd = 0;
45
46 ResetShadowMemory();
47 }
48
AsanApplyToGlobals(globals_op_fptr op,const void * needle)49 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
50 UNIMPLEMENTED();
51 }
52
FlushUnneededASanShadowMemory(uptr p,uptr size)53 void FlushUnneededASanShadowMemory(uptr p, uptr size) {
54 // Since asan's mapping is compacting, the shadow chunk may be
55 // not page-aligned, so we only flush the page-aligned portion.
56 ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size));
57 }
58
AsanCheckDynamicRTPrereqs()59 void AsanCheckDynamicRTPrereqs() {}
AsanCheckIncompatibleRT()60 void AsanCheckIncompatibleRT() {}
InitializeAsanInterceptors()61 void InitializeAsanInterceptors() {}
InitializePlatformInterceptors()62 void InitializePlatformInterceptors() {}
InitializePlatformExceptionHandlers()63 void InitializePlatformExceptionHandlers() {}
64
65 // RTEMS only support static linking; it sufficies to return with no
66 // error.
AsanDoesNotSupportStaticLinkage()67 void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
68
AsanOnDeadlySignal(int signo,void * siginfo,void * context)69 void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
70 UNIMPLEMENTED();
71 }
72
PlatformUnpoisonStacks()73 bool PlatformUnpoisonStacks() { return false; }
74
EarlyInit()75 void EarlyInit() {
76 // Provide early initialization of shadow memory so that
77 // instrumented code running before full initialzation will not
78 // report spurious errors.
79 ResetShadowMemory();
80 }
81
82 // We can use a plain thread_local variable for TSD.
83 static thread_local void *per_thread;
84
AsanTSDGet()85 void *AsanTSDGet() { return per_thread; }
86
AsanTSDSet(void * tsd)87 void AsanTSDSet(void *tsd) { per_thread = tsd; }
88
89 // There's no initialization needed, and the passed-in destructor
90 // will never be called. Instead, our own thread destruction hook
91 // (below) will call AsanThread::TSDDtor directly.
AsanTSDInit(void (* destructor)(void * tsd))92 void AsanTSDInit(void (*destructor)(void *tsd)) {
93 DCHECK(destructor == &PlatformTSDDtor);
94 }
95
PlatformTSDDtor(void * tsd)96 void PlatformTSDDtor(void *tsd) { UNREACHABLE(__func__); }
97
98 //
99 // Thread registration. We provide an API similar to the Fushia port.
100 //
101
102 struct AsanThread::InitOptions {
103 uptr stack_bottom, stack_size, tls_bottom, tls_size;
104 };
105
106 // Shared setup between thread creation and startup for the initial thread.
CreateAsanThread(StackTrace * stack,u32 parent_tid,uptr user_id,bool detached,uptr stack_bottom,uptr stack_size,uptr tls_bottom,uptr tls_size)107 static AsanThread *CreateAsanThread(StackTrace *stack, u32 parent_tid,
108 uptr user_id, bool detached,
109 uptr stack_bottom, uptr stack_size,
110 uptr tls_bottom, uptr tls_size) {
111 // In lieu of AsanThread::Create.
112 AsanThread *thread = (AsanThread *)MmapOrDie(sizeof(AsanThread), __func__);
113 AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
114 asanThreadRegistry().CreateThread(user_id, detached, parent_tid, &args);
115
116 // On other systems, AsanThread::Init() is called from the new
117 // thread itself. But on RTEMS we already know the stack address
118 // range beforehand, so we can do most of the setup right now.
119 const AsanThread::InitOptions options = {stack_bottom, stack_size,
120 tls_bottom, tls_size};
121 thread->Init(&options);
122 return thread;
123 }
124
125 // This gets the same arguments passed to Init by CreateAsanThread, above.
126 // We're in the creator thread before the new thread is actually started, but
127 // its stack and tls address range are already known.
SetThreadStackAndTls(const AsanThread::InitOptions * options)128 void AsanThread::SetThreadStackAndTls(const AsanThread::InitOptions *options) {
129 DCHECK_NE(GetCurrentThread(), this);
130 DCHECK_NE(GetCurrentThread(), nullptr);
131 CHECK_NE(options->stack_bottom, 0);
132 CHECK_NE(options->stack_size, 0);
133 stack_bottom_ = options->stack_bottom;
134 stack_top_ = options->stack_bottom + options->stack_size;
135 tls_begin_ = options->tls_bottom;
136 tls_end_ = options->tls_bottom + options->tls_size;
137 }
138
139 // Called by __asan::AsanInitInternal (asan_rtl.c). Unlike other ports, the
140 // main thread on RTEMS does not require special treatment; its AsanThread is
141 // already created by the provided hooks. This function simply looks up and
142 // returns the created thread.
CreateMainThread()143 AsanThread *CreateMainThread() {
144 return GetThreadContextByTidLocked(0)->thread;
145 }
146
147 // This is called before each thread creation is attempted. So, in
148 // its first call, the calling thread is the initial and sole thread.
BeforeThreadCreateHook(uptr user_id,bool detached,uptr stack_bottom,uptr stack_size,uptr tls_bottom,uptr tls_size)149 static void *BeforeThreadCreateHook(uptr user_id, bool detached,
150 uptr stack_bottom, uptr stack_size,
151 uptr tls_bottom, uptr tls_size) {
152 EnsureMainThreadIDIsCorrect();
153 // Strict init-order checking is thread-hostile.
154 if (flags()->strict_init_order) StopInitOrderChecking();
155
156 GET_STACK_TRACE_THREAD;
157 u32 parent_tid = GetCurrentTidOrInvalid();
158
159 return CreateAsanThread(&stack, parent_tid, user_id, detached,
160 stack_bottom, stack_size, tls_bottom, tls_size);
161 }
162
163 // This is called after creating a new thread (in the creating thread),
164 // with the pointer returned by BeforeThreadCreateHook (above).
ThreadCreateHook(void * hook,bool aborted)165 static void ThreadCreateHook(void *hook, bool aborted) {
166 AsanThread *thread = static_cast<AsanThread *>(hook);
167 if (!aborted) {
168 // The thread was created successfully.
169 // ThreadStartHook is already running in the new thread.
170 } else {
171 // The thread wasn't created after all.
172 // Clean up everything we set up in BeforeThreadCreateHook.
173 asanThreadRegistry().FinishThread(thread->tid());
174 UnmapOrDie(thread, sizeof(AsanThread));
175 }
176 }
177
178 // This is called (1) in the newly-created thread before it runs anything else,
179 // with the pointer returned by BeforeThreadCreateHook (above). (2) before a
180 // thread restart.
ThreadStartHook(void * hook,uptr os_id)181 static void ThreadStartHook(void *hook, uptr os_id) {
182 if (!hook)
183 return;
184
185 AsanThread *thread = static_cast<AsanThread *>(hook);
186 SetCurrentThread(thread);
187
188 ThreadStatus status =
189 asanThreadRegistry().GetThreadLocked(thread->tid())->status;
190 DCHECK(status == ThreadStatusCreated || status == ThreadStatusRunning);
191 // Determine whether we are starting or restarting the thread.
192 if (status == ThreadStatusCreated) {
193 // In lieu of AsanThread::ThreadStart.
194 asanThreadRegistry().StartThread(thread->tid(), os_id, ThreadType::Regular,
195 nullptr);
196 } else {
197 // In a thread restart, a thread may resume execution at an
198 // arbitrary function entry point, with its stack and TLS state
199 // reset. We unpoison the stack in that case.
200 PoisonShadow(thread->stack_bottom(), thread->stack_size(), 0);
201 }
202 }
203
204 // Each thread runs this just before it exits,
205 // with the pointer returned by BeforeThreadCreateHook (above).
206 // All per-thread destructors have already been called.
ThreadExitHook(void * hook,uptr os_id)207 static void ThreadExitHook(void *hook, uptr os_id) {
208 AsanThread *thread = static_cast<AsanThread *>(hook);
209 if (thread)
210 AsanThread::TSDDtor(thread->context());
211 }
212
HandleExit()213 static void HandleExit() {
214 // Disable ASan by setting it to uninitialized. Also reset the
215 // shadow memory to avoid reporting errors after the run-time has
216 // been desroyed.
217 if (asan_inited) {
218 asan_inited = false;
219 ResetShadowMemory();
220 }
221 }
222
HandleDlopenInit()223 bool HandleDlopenInit() {
224 // Not supported on this platform.
225 static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
226 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
227 return false;
228 }
229 } // namespace __asan
230
231 // These are declared (in extern "C") by <some_path/sanitizer.h>.
232 // The system runtime will call our definitions directly.
233
234 extern "C" {
__sanitizer_early_init()235 void __sanitizer_early_init() {
236 __asan::EarlyInit();
237 }
238
__sanitizer_before_thread_create_hook(uptr thread,bool detached,const char * name,void * stack_base,size_t stack_size,void * tls_base,size_t tls_size)239 void *__sanitizer_before_thread_create_hook(uptr thread, bool detached,
240 const char *name,
241 void *stack_base, size_t stack_size,
242 void *tls_base, size_t tls_size) {
243 return __asan::BeforeThreadCreateHook(
244 thread, detached,
245 reinterpret_cast<uptr>(stack_base), stack_size,
246 reinterpret_cast<uptr>(tls_base), tls_size);
247 }
248
__sanitizer_thread_create_hook(void * handle,uptr thread,int status)249 void __sanitizer_thread_create_hook(void *handle, uptr thread, int status) {
250 __asan::ThreadCreateHook(handle, status != 0);
251 }
252
__sanitizer_thread_start_hook(void * handle,uptr self)253 void __sanitizer_thread_start_hook(void *handle, uptr self) {
254 __asan::ThreadStartHook(handle, self);
255 }
256
__sanitizer_thread_exit_hook(void * handle,uptr self)257 void __sanitizer_thread_exit_hook(void *handle, uptr self) {
258 __asan::ThreadExitHook(handle, self);
259 }
260
__sanitizer_exit()261 void __sanitizer_exit() {
262 __asan::HandleExit();
263 }
264 } // "C"
265
266 #endif // SANITIZER_RTEMS
267