1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <pthread.h>
18 #include <sys/types.h> // pid_t
19 #include <cstddef> // size_t
20
21 #include "berberis/base/checks.h"
22 #include "berberis/base/tracing.h"
23 #include "berberis/guest_os_primitives/guest_thread.h"
24 #include "berberis/guest_os_primitives/guest_thread_manager.h"
25 #include "berberis/instrument/guest_thread.h"
26 #include "berberis/runtime_primitives/code_pool.h" // ResetAllExecRegions
27 #include "guest_thread_manager_impl.h"
28 #include "guest_thread_map.h"
29 #include "scoped_signal_blocker.h"
30
31 namespace berberis {
32
33 // Manages thread local storage (TLS) for the current thread's GuestThread instance.
34 pthread_key_t g_guest_thread_key;
35
36 // Tracks GuestThread instances across all threads.
37 GuestThreadMap g_guest_thread_map_;
38
39 namespace {
40
GuestThreadDtor(void *)41 void GuestThreadDtor(void* /* arg */) {
42 // TLS cache was cleared by pthread_exit.
43 // TODO(b/280671643): Postpone detach to last pthread destructor iteration.
44 // On previous iterations, simply restore TLS cache and return.
45 DetachCurrentThread();
46 }
47
48 } // namespace
49
50 // Not thread safe, not async signals safe!
InitGuestThreadManager()51 void InitGuestThreadManager() {
52 // Here we don't need pthread_once, which is not reentrant due to spinlocks.
53 CHECK_EQ(0, pthread_key_create(&g_guest_thread_key, GuestThreadDtor));
54 }
55
GetCurrentGuestThread()56 GuestThread* GetCurrentGuestThread() {
57 bool attached;
58 return AttachCurrentThread(true, &attached);
59 }
60
ResetCurrentGuestThreadAfterFork(GuestThread * thread)61 void ResetCurrentGuestThreadAfterFork(GuestThread* thread) {
62 g_guest_thread_map_.ResetThreadTable(GettidSyscall(), thread);
63 #if defined(__BIONIC__)
64 // Force (host) bionic to update cached tid if necessary
65 // 1. Bionic `clone` implementation resets cached `tid` before syscall
66 // so that it does not get accidentally propagate to the child.
67 // 2. pthread_lock/unlock implementations do not call `gettid()` they
68 // instead access cached value directly from TLS. Which leads to
69 // a situation where cached `tid` is updated in the middle of
70 // `dlopen` and it fails to unlock the mutex because the
71 // ownership check fails. Subsequent `dlsym` (or any other
72 // dl* call) stops on locked mutex.
73 //
74 // By calling `gettid()` here we force bionic to set the cached
75 // value to the correct one.
76 CHECK_NE(gettid(), -1);
77 #endif
78 ResetAllExecRegions();
79 }
80
GetGuestThreadAttr(pid_t tid,GuestAddr * stack_base,size_t * stack_size,size_t * guard_size,int * error)81 bool GetGuestThreadAttr(pid_t tid,
82 GuestAddr* stack_base,
83 size_t* stack_size,
84 size_t* guard_size,
85 int* error) {
86 GuestThread* thread = g_guest_thread_map_.FindThread(tid);
87 if (thread) {
88 thread->GetAttr(stack_base, stack_size, guard_size);
89 return true;
90 }
91 *error = ESRCH;
92 return false;
93 }
94
ExitCurrentThread(int status)95 void ExitCurrentThread(int status) {
96 pid_t tid = GettidSyscall();
97
98 // The following code is not reentrant!
99 ScopedSignalBlocker signal_blocker;
100
101 // Remove thread from global table.
102 GuestThread* thread = g_guest_thread_map_.RemoveThread(tid);
103 if (kInstrumentGuestThread) {
104 OnRemoveGuestThread(tid, thread);
105 }
106
107 TRACE("guest thread exited %d", tid);
108 GuestThread::Exit(thread, status);
109 }
110
111 // We assume translation cache is already modified. If any thread still runs
112 // a region that is already obsolete, we should force the thread to dispatcher
113 // to re-read from translation cache. We should also wait for that thread to
114 // acknowledge the dispatch, so code that called cache invalidation can be sure
115 // that obsolete code is never run after this point.
FlushGuestCodeCache()116 void FlushGuestCodeCache() {
117 // TODO(b/28081995): at the moment we don't know what range was flushed, so
118 // we have to force ALL guest threads to dispatcher. This is really, really,
119 // REALLY bad for performance.
120 // TODO(b/28081995): at the moment we don't wait for acknowledgment. This
121 // might cause subtle guest logic failures.
122 pid_t current_tid = GettidSyscall();
123 g_guest_thread_map_.ForEachThread([current_tid](pid_t tid, GuestThread* thread) {
124 // ATTENTION: we probably don't want to force current thread to dispatcher
125 // and to wait for it to acknowledge :) Assume caller of this function
126 // (syscall emulation or trampoline) will force re-read from translation
127 // cache before continuing to guest code.
128 if (tid != current_tid) {
129 // Set thread's pending signals to present to force it to dispatcher.
130 // ATTENTION! this is the only place we access pending_signals_status
131 // from other thread!
132 uint8_t old_status = kPendingSignalsEnabled;
133 GetPendingSignalsStatusAtomic(*thread->state())
134 .compare_exchange_strong(old_status, kPendingSignalsPresent, std::memory_order_acq_rel);
135 }
136 });
137 }
138
139 // Common guest thread function attaches GuestThread lazily on first call and detaches in pthread
140 // key destructor (register_dtor = true).
141 //
142 // Guest signal handlers and guest pthread key destructors are special as they might be called when
143 // GuestThread is not yet attached or is already detached. Moreover, they cannot determine between
144 // latter cases. Thus, signal handlers and key destructors reuse GuestThread if it is attached,
145 // otherwise they attach AND detach themselves, so GuestThread attach state is preserved and
146 // GuestThread is never leaked (register_dtor = false).
147 //
148 // ATTENTION: When signal handler or key destructor attach GuestThread themselves, they might get
149 // GuestThread stack different from one used in thread function. It might confuse several
150 // (ill-formed?) apks, so we issue a warning.
151 //
152 // ATTENTION: Can be interrupted!
AttachCurrentThread(bool register_dtor,bool * attached)153 GuestThread* AttachCurrentThread(bool register_dtor, bool* attached) {
154 // The following code is not reentrant!
155 ScopedSignalBlocker signal_blocker;
156
157 pid_t tid = GettidSyscall();
158 GuestThread* thread = g_guest_thread_map_.FindThread(tid);
159 if (thread) {
160 // Thread was already attached.
161 *attached = false;
162 return thread;
163 }
164
165 // Copy host stack size attributes.
166 size_t stack_size;
167 size_t guard_size;
168 pthread_attr_t attr;
169 CHECK_EQ(0, pthread_getattr_np(pthread_self(), &attr));
170 CHECK_EQ(0, pthread_attr_getstacksize(&attr, &stack_size));
171 CHECK_EQ(0, pthread_attr_getguardsize(&attr, &guard_size));
172 thread = GuestThread::CreatePthread(nullptr, stack_size, guard_size);
173 CHECK(thread);
174
175 InsertCurrentThread(thread, register_dtor);
176 thread->InitStaticTls();
177
178 // If thread is attached in HandleHostSignal we must run guest handler
179 // immediately because we detach guest thread before exit from HandleHostSignal.
180 // All non-reentrant code in runtime must be protected with ScopedPendingSignalsEnabler.
181 GetPendingSignalsStatusAtomic(*thread->state()) = kPendingSignalsDisabled;
182 // AttachCurrentThread is never called from generated code.
183 SetResidence(*thread->state(), kOutsideGeneratedCode);
184
185 *attached = true;
186 return thread;
187 }
188
InsertCurrentThread(GuestThread * thread,bool register_dtor)189 void InsertCurrentThread(GuestThread* thread, bool register_dtor) {
190 pid_t tid = GettidSyscall();
191
192 // The following code is not reentrant!
193 ScopedSignalBlocker signal_blocker;
194
195 // Thread should not be already in the table!
196 // If signal came after we checked tls cache or table but before we blocked signals, it should
197 // have attached AND detached the thread!
198 g_guest_thread_map_.InsertThread(tid, thread);
199 if (register_dtor) {
200 CHECK_EQ(0, pthread_setspecific(g_guest_thread_key, thread));
201 }
202 if (kInstrumentGuestThread) {
203 OnInsertGuestThread(tid, thread);
204 }
205
206 TRACE("guest thread attached %d", tid);
207 }
208
209 // ATTENTION: Can be interrupted!
DetachCurrentThread()210 void DetachCurrentThread() {
211 pid_t tid = GettidSyscall();
212
213 // The following code is not reentrant!
214 ScopedSignalBlocker signal_blocker;
215
216 // Remove thread from global table.
217 GuestThread* thread = g_guest_thread_map_.RemoveThread(tid);
218 if (kInstrumentGuestThread) {
219 OnRemoveGuestThread(tid, thread);
220 }
221
222 TRACE("guest thread detached %d", tid);
223 GuestThread::Destroy(thread);
224 }
225
226 } // namespace berberis
227