re PR rtl-optimization/89795 (wrong code with -O2 -fno-dce -fno-forward-propagate...
[official-gcc.git] / libsanitizer / asan / asan_rtems.cpp
blob360d5780a3f2e8f621ea1e710620c13f09004d7e
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.
11 // RTEMS-specific details.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_rtems.h"
15 #if SANITIZER_RTEMS
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"
26 #include <pthread.h>
27 #include <stdlib.h>
29 namespace __asan {
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() {
42 kHighMemEnd = 0;
43 kMidMemBeg = 0;
44 kMidMemEnd = 0;
46 ResetShadowMemory();
49 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
50 UNIMPLEMENTED();
53 void AsanCheckDynamicRTPrereqs() {}
54 void AsanCheckIncompatibleRT() {}
55 void InitializeAsanInterceptors() {}
56 void InitializePlatformInterceptors() {}
57 void InitializePlatformExceptionHandlers() {}
59 // RTEMS only support static linking; it sufficies to return with no
60 // error.
61 void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
63 void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
64 UNIMPLEMENTED();
67 void EarlyInit() {
68 // Provide early initialization of shadow memory so that
69 // instrumented code running before full initialzation will not
70 // report spurious errors.
71 ResetShadowMemory();
74 // We can use a plain thread_local variable for TSD.
75 static thread_local void *per_thread;
77 void *AsanTSDGet() { return per_thread; }
79 void AsanTSDSet(void *tsd) { per_thread = tsd; }
81 // There's no initialization needed, and the passed-in destructor
82 // will never be called. Instead, our own thread destruction hook
83 // (below) will call AsanThread::TSDDtor directly.
84 void AsanTSDInit(void (*destructor)(void *tsd)) {
85 DCHECK(destructor == &PlatformTSDDtor);
88 void PlatformTSDDtor(void *tsd) { UNREACHABLE(__func__); }
91 // Thread registration. We provide an API similar to the Fushia port.
94 struct AsanThread::InitOptions {
95 uptr stack_bottom, stack_size, tls_bottom, tls_size;
98 // Shared setup between thread creation and startup for the initial thread.
99 static AsanThread *CreateAsanThread(StackTrace *stack, u32 parent_tid,
100 uptr user_id, bool detached,
101 uptr stack_bottom, uptr stack_size,
102 uptr tls_bottom, uptr tls_size) {
103 // In lieu of AsanThread::Create.
104 AsanThread *thread = (AsanThread *)MmapOrDie(sizeof(AsanThread), __func__);
105 AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
106 asanThreadRegistry().CreateThread(user_id, detached, parent_tid, &args);
108 // On other systems, AsanThread::Init() is called from the new
109 // thread itself. But on RTEMS we already know the stack address
110 // range beforehand, so we can do most of the setup right now.
111 const AsanThread::InitOptions options = {stack_bottom, stack_size,
112 tls_bottom, tls_size};
113 thread->Init(&options);
114 return thread;
117 // This gets the same arguments passed to Init by CreateAsanThread, above.
118 // We're in the creator thread before the new thread is actually started, but
119 // its stack and tls address range are already known.
120 void AsanThread::SetThreadStackAndTls(const AsanThread::InitOptions *options) {
121 DCHECK_NE(GetCurrentThread(), this);
122 DCHECK_NE(GetCurrentThread(), nullptr);
123 CHECK_NE(options->stack_bottom, 0);
124 CHECK_NE(options->stack_size, 0);
125 stack_bottom_ = options->stack_bottom;
126 stack_top_ = options->stack_bottom + options->stack_size;
127 tls_begin_ = options->tls_bottom;
128 tls_end_ = options->tls_bottom + options->tls_size;
131 // Called by __asan::AsanInitInternal (asan_rtl.c). Unlike other ports, the
132 // main thread on RTEMS does not require special treatment; its AsanThread is
133 // already created by the provided hooks. This function simply looks up and
134 // returns the created thread.
135 AsanThread *CreateMainThread() {
136 return GetThreadContextByTidLocked(0)->thread;
139 // This is called before each thread creation is attempted. So, in
140 // its first call, the calling thread is the initial and sole thread.
141 static void *BeforeThreadCreateHook(uptr user_id, bool detached,
142 uptr stack_bottom, uptr stack_size,
143 uptr tls_bottom, uptr tls_size) {
144 EnsureMainThreadIDIsCorrect();
145 // Strict init-order checking is thread-hostile.
146 if (flags()->strict_init_order) StopInitOrderChecking();
148 GET_STACK_TRACE_THREAD;
149 u32 parent_tid = GetCurrentTidOrInvalid();
151 return CreateAsanThread(&stack, parent_tid, user_id, detached,
152 stack_bottom, stack_size, tls_bottom, tls_size);
155 // This is called after creating a new thread (in the creating thread),
156 // with the pointer returned by BeforeThreadCreateHook (above).
157 static void ThreadCreateHook(void *hook, bool aborted) {
158 AsanThread *thread = static_cast<AsanThread *>(hook);
159 if (!aborted) {
160 // The thread was created successfully.
161 // ThreadStartHook is already running in the new thread.
162 } else {
163 // The thread wasn't created after all.
164 // Clean up everything we set up in BeforeThreadCreateHook.
165 asanThreadRegistry().FinishThread(thread->tid());
166 UnmapOrDie(thread, sizeof(AsanThread));
170 // This is called (1) in the newly-created thread before it runs anything else,
171 // with the pointer returned by BeforeThreadCreateHook (above). (2) before a
172 // thread restart.
173 static void ThreadStartHook(void *hook, uptr os_id) {
174 if (!hook)
175 return;
177 AsanThread *thread = static_cast<AsanThread *>(hook);
178 SetCurrentThread(thread);
180 ThreadStatus status =
181 asanThreadRegistry().GetThreadLocked(thread->tid())->status;
182 DCHECK(status == ThreadStatusCreated || status == ThreadStatusRunning);
183 // Determine whether we are starting or restarting the thread.
184 if (status == ThreadStatusCreated)
185 // In lieu of AsanThread::ThreadStart.
186 asanThreadRegistry().StartThread(thread->tid(), os_id, ThreadType::Regular,
187 nullptr);
188 else {
189 // In a thread restart, a thread may resume execution at an
190 // arbitrary function entry point, with its stack and TLS state
191 // reset. We unpoison the stack in that case.
192 PoisonShadow(thread->stack_bottom(), thread->stack_size(), 0);
196 // Each thread runs this just before it exits,
197 // with the pointer returned by BeforeThreadCreateHook (above).
198 // All per-thread destructors have already been called.
199 static void ThreadExitHook(void *hook, uptr os_id) {
200 AsanThread *thread = static_cast<AsanThread *>(hook);
201 if (thread)
202 AsanThread::TSDDtor(thread->context());
205 static void HandleExit() {
206 // Disable ASan by setting it to uninitialized. Also reset the
207 // shadow memory to avoid reporting errors after the run-time has
208 // been desroyed.
209 if (asan_inited) {
210 asan_inited = false;
211 ResetShadowMemory();
215 bool HandleDlopenInit() {
216 // Not supported on this platform.
217 static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
218 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
219 return false;
221 } // namespace __asan
223 // These are declared (in extern "C") by <some_path/sanitizer.h>.
224 // The system runtime will call our definitions directly.
226 extern "C" {
227 void __sanitizer_early_init() {
228 __asan::EarlyInit();
231 void *__sanitizer_before_thread_create_hook(uptr thread, bool detached,
232 const char *name,
233 void *stack_base, size_t stack_size,
234 void *tls_base, size_t tls_size) {
235 return __asan::BeforeThreadCreateHook(
236 thread, detached,
237 reinterpret_cast<uptr>(stack_base), stack_size,
238 reinterpret_cast<uptr>(tls_base), tls_size);
241 void __sanitizer_thread_create_hook(void *handle, uptr thread, int status) {
242 __asan::ThreadCreateHook(handle, status != 0);
245 void __sanitizer_thread_start_hook(void *handle, uptr self) {
246 __asan::ThreadStartHook(handle, self);
249 void __sanitizer_thread_exit_hook(void *handle, uptr self) {
250 __asan::ThreadExitHook(handle, self);
253 void __sanitizer_exit() {
254 __asan::HandleExit();
256 } // "C"
258 #endif // SANITIZER_RTEMS