1 //===-- asan_rtems.cc -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // RTEMS-specific details.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_rtems.h"
18 #include "asan_internal.h"
19 #include "asan_interceptors.h"
20 #include "asan_mapping.h"
21 #include "asan_poisoning.h"
22 #include "asan_report.h"
23 #include "asan_stack.h"
24 #include "sanitizer_common/sanitizer_common.h"
25 #include "sanitizer_common/sanitizer_libc.h"
32 static void ResetShadowMemory() {
33 uptr shadow_start = SHADOW_OFFSET;
34 uptr shadow_end = MEM_TO_SHADOW(kMyriadMemoryEnd32);
35 uptr gap_start = MEM_TO_SHADOW(shadow_start);
36 uptr gap_end = MEM_TO_SHADOW(shadow_end);
38 REAL(memset)((void *)shadow_start, 0, shadow_end - shadow_start);
39 REAL(memset)((void *)gap_start, kAsanShadowGap, gap_end - gap_start);
42 void InitializeShadowMemory() {
50 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
54 void AsanCheckDynamicRTPrereqs() {}
55 void AsanCheckIncompatibleRT() {}
56 void InitializeAsanInterceptors() {}
57 void InitializePlatformInterceptors() {}
58 void InitializePlatformExceptionHandlers() {}
60 // RTEMS only support static linking; it sufficies to return with no
62 void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
64 void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
69 // Provide early initialization of shadow memory so that
70 // instrumented code running before full initialzation will not
71 // report spurious errors.
75 // We can use a plain thread_local variable for TSD.
76 static thread_local void *per_thread;
78 void *AsanTSDGet() { return per_thread; }
80 void AsanTSDSet(void *tsd) { per_thread = tsd; }
82 // There's no initialization needed, and the passed-in destructor
83 // will never be called. Instead, our own thread destruction hook
84 // (below) will call AsanThread::TSDDtor directly.
85 void AsanTSDInit(void (*destructor)(void *tsd)) {
86 DCHECK(destructor == &PlatformTSDDtor);
89 void PlatformTSDDtor(void *tsd) { UNREACHABLE(__func__); }
92 // Thread registration. We provide an API similar to the Fushia port.
95 struct AsanThread::InitOptions {
96 uptr stack_bottom, stack_size, tls_bottom, tls_size;
99 // Shared setup between thread creation and startup for the initial thread.
100 static AsanThread *CreateAsanThread(StackTrace *stack, u32 parent_tid,
101 uptr user_id, bool detached,
102 uptr stack_bottom, uptr stack_size,
103 uptr tls_bottom, uptr tls_size) {
104 // In lieu of AsanThread::Create.
105 AsanThread *thread = (AsanThread *)MmapOrDie(sizeof(AsanThread), __func__);
106 AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
107 asanThreadRegistry().CreateThread(user_id, detached, parent_tid, &args);
109 // On other systems, AsanThread::Init() is called from the new
110 // thread itself. But on RTEMS we already know the stack address
111 // range beforehand, so we can do most of the setup right now.
112 const AsanThread::InitOptions options = {stack_bottom, stack_size,
113 tls_bottom, tls_size};
114 thread->Init(&options);
118 // This gets the same arguments passed to Init by CreateAsanThread, above.
119 // We're in the creator thread before the new thread is actually started, but
120 // its stack and tls address range are already known.
121 void AsanThread::SetThreadStackAndTls(const AsanThread::InitOptions *options) {
122 DCHECK_NE(GetCurrentThread(), this);
123 DCHECK_NE(GetCurrentThread(), nullptr);
124 CHECK_NE(options->stack_bottom, 0);
125 CHECK_NE(options->stack_size, 0);
126 stack_bottom_ = options->stack_bottom;
127 stack_top_ = options->stack_bottom + options->stack_size;
128 tls_begin_ = options->tls_bottom;
129 tls_end_ = options->tls_bottom + options->tls_size;
132 // Called by __asan::AsanInitInternal (asan_rtl.c). Unlike other ports, the
133 // main thread on RTEMS does not require special treatment; its AsanThread is
134 // already created by the provided hooks. This function simply looks up and
135 // returns the created thread.
136 AsanThread *CreateMainThread() {
137 return GetThreadContextByTidLocked(0)->thread;
140 // This is called before each thread creation is attempted. So, in
141 // its first call, the calling thread is the initial and sole thread.
142 static void *BeforeThreadCreateHook(uptr user_id, bool detached,
143 uptr stack_bottom, uptr stack_size,
144 uptr tls_bottom, uptr tls_size) {
145 EnsureMainThreadIDIsCorrect();
146 // Strict init-order checking is thread-hostile.
147 if (flags()->strict_init_order) StopInitOrderChecking();
149 GET_STACK_TRACE_THREAD;
150 u32 parent_tid = GetCurrentTidOrInvalid();
152 return CreateAsanThread(&stack, parent_tid, user_id, detached,
153 stack_bottom, stack_size, tls_bottom, tls_size);
156 // This is called after creating a new thread (in the creating thread),
157 // with the pointer returned by BeforeThreadCreateHook (above).
158 static void ThreadCreateHook(void *hook, bool aborted) {
159 AsanThread *thread = static_cast<AsanThread *>(hook);
161 // The thread was created successfully.
162 // ThreadStartHook is already running in the new thread.
164 // The thread wasn't created after all.
165 // Clean up everything we set up in BeforeThreadCreateHook.
166 asanThreadRegistry().FinishThread(thread->tid());
167 UnmapOrDie(thread, sizeof(AsanThread));
171 // This is called (1) in the newly-created thread before it runs anything else,
172 // with the pointer returned by BeforeThreadCreateHook (above). (2) before a
174 static void ThreadStartHook(void *hook, uptr os_id) {
178 AsanThread *thread = static_cast<AsanThread *>(hook);
179 SetCurrentThread(thread);
181 ThreadStatus status =
182 asanThreadRegistry().GetThreadLocked(thread->tid())->status;
183 DCHECK(status == ThreadStatusCreated || status == ThreadStatusRunning);
184 // Determine whether we are starting or restarting the thread.
185 if (status == ThreadStatusCreated)
186 // In lieu of AsanThread::ThreadStart.
187 asanThreadRegistry().StartThread(thread->tid(), os_id,
188 /*workerthread*/ false, nullptr);
190 // In a thread restart, a thread may resume execution at an
191 // arbitrary function entry point, with its stack and TLS state
192 // reset. We unpoison the stack in that case.
193 PoisonShadow(thread->stack_bottom(), thread->stack_size(), 0);
197 // Each thread runs this just before it exits,
198 // with the pointer returned by BeforeThreadCreateHook (above).
199 // All per-thread destructors have already been called.
200 static void ThreadExitHook(void *hook, uptr os_id) {
201 AsanThread *thread = static_cast<AsanThread *>(hook);
203 AsanThread::TSDDtor(thread->context());
206 static void HandleExit() {
207 // Disable ASan by setting it to uninitialized. Also reset the
208 // shadow memory to avoid reporting errors after the run-time has
216 } // namespace __asan
218 // These are declared (in extern "C") by <some_path/sanitizer.h>.
219 // The system runtime will call our definitions directly.
222 void __sanitizer_early_init() {
226 void *__sanitizer_before_thread_create_hook(uptr thread, bool detached,
228 void *stack_base, size_t stack_size,
229 void *tls_base, size_t tls_size) {
230 return __asan::BeforeThreadCreateHook(
232 reinterpret_cast<uptr>(stack_base), stack_size,
233 reinterpret_cast<uptr>(tls_base), tls_size);
236 void __sanitizer_thread_create_hook(void *handle, uptr thread, int status) {
237 __asan::ThreadCreateHook(handle, status != 0);
240 void __sanitizer_thread_start_hook(void *handle, uptr self) {
241 __asan::ThreadStartHook(handle, self);
244 void __sanitizer_thread_exit_hook(void *handle, uptr self) {
245 __asan::ThreadExitHook(handle, self);
248 void __sanitizer_exit() {
249 __asan::HandleExit();
253 #endif // SANITIZER_RTEMS