1 //===-- asan_rtems.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // RTEMS-specific details.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_rtems.h"
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"
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
);
37 REAL(memset
)((void *)shadow_start
, 0, shadow_end
- shadow_start
);
38 REAL(memset
)((void *)gap_start
, kAsanShadowGap
, gap_end
- gap_start
);
41 void InitializeShadowMemory() {
49 void AsanApplyToGlobals(globals_op_fptr op
, const void *needle
) {
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
));
59 void AsanCheckDynamicRTPrereqs() {}
60 void AsanCheckIncompatibleRT() {}
61 void InitializeAsanInterceptors() {}
62 void InitializePlatformInterceptors() {}
63 void InitializePlatformExceptionHandlers() {}
65 // RTEMS only support static linking; it sufficies to return with no
67 void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
69 void AsanOnDeadlySignal(int signo
, void *siginfo
, void *context
) {
73 bool PlatformUnpoisonStacks() { return false; }
76 // Provide early initialization of shadow memory so that
77 // instrumented code running before full initialzation will not
78 // report spurious errors.
82 // We can use a plain thread_local variable for TSD.
83 static thread_local
void *per_thread
;
85 void *AsanTSDGet() { return per_thread
; }
87 void AsanTSDSet(void *tsd
) { per_thread
= tsd
; }
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.
92 void AsanTSDInit(void (*destructor
)(void *tsd
)) {
93 DCHECK(destructor
== &PlatformTSDDtor
);
96 void PlatformTSDDtor(void *tsd
) { UNREACHABLE(__func__
); }
99 // Thread registration. We provide an API similar to the Fushia port.
102 struct AsanThread::InitOptions
{
103 uptr stack_bottom
, stack_size
, tls_bottom
, tls_size
;
106 // Shared setup between thread creation and startup for the initial thread.
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
);
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
);
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.
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
;
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.
143 AsanThread
*CreateMainThread() {
144 return GetThreadContextByTidLocked(0)->thread
;
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.
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();
156 GET_STACK_TRACE_THREAD
;
157 u32 parent_tid
= GetCurrentTidOrInvalid();
159 return CreateAsanThread(&stack
, parent_tid
, user_id
, detached
,
160 stack_bottom
, stack_size
, tls_bottom
, tls_size
);
163 // This is called after creating a new thread (in the creating thread),
164 // with the pointer returned by BeforeThreadCreateHook (above).
165 static void ThreadCreateHook(void *hook
, bool aborted
) {
166 AsanThread
*thread
= static_cast<AsanThread
*>(hook
);
168 // The thread was created successfully.
169 // ThreadStartHook is already running in the new thread.
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
));
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
181 static void ThreadStartHook(void *hook
, uptr os_id
) {
185 AsanThread
*thread
= static_cast<AsanThread
*>(hook
);
186 SetCurrentThread(thread
);
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
,
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);
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.
207 static void ThreadExitHook(void *hook
, uptr os_id
) {
208 AsanThread
*thread
= static_cast<AsanThread
*>(hook
);
210 AsanThread::TSDDtor(thread
->context());
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
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");
229 } // namespace __asan
231 // These are declared (in extern "C") by <some_path/sanitizer.h>.
232 // The system runtime will call our definitions directly.
235 void __sanitizer_early_init() {
239 void *__sanitizer_before_thread_create_hook(uptr thread
, bool detached
,
241 void *stack_base
, size_t stack_size
,
242 void *tls_base
, size_t tls_size
) {
243 return __asan::BeforeThreadCreateHook(
245 reinterpret_cast<uptr
>(stack_base
), stack_size
,
246 reinterpret_cast<uptr
>(tls_base
), tls_size
);
249 void __sanitizer_thread_create_hook(void *handle
, uptr thread
, int status
) {
250 __asan::ThreadCreateHook(handle
, status
!= 0);
253 void __sanitizer_thread_start_hook(void *handle
, uptr self
) {
254 __asan::ThreadStartHook(handle
, self
);
257 void __sanitizer_thread_exit_hook(void *handle
, uptr self
) {
258 __asan::ThreadExitHook(handle
, self
);
261 void __sanitizer_exit() {
262 __asan::HandleExit();
266 #endif // SANITIZER_RTEMS