1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <pthread.h>
32 #include <stdatomic.h>
33 
34 #if __has_feature(hwaddress_sanitizer)
35 #include <sanitizer/hwasan_interface.h>
36 #else
37 #define __hwasan_thread_enter()
38 #define __hwasan_thread_exit()
39 #endif
40 
41 #include "private/bionic_elf_tls.h"
42 #include "private/bionic_lock.h"
43 #include "private/bionic_tls.h"
44 
45 // Has the thread been detached by a pthread_join or pthread_detach call?
46 #define PTHREAD_ATTR_FLAG_DETACHED 0x00000001
47 
48 // Has the thread been joined by another thread?
49 #define PTHREAD_ATTR_FLAG_JOINED 0x00000002
50 
51 // Used for pthread_attr_setinheritsched. We need two flags for this apparent
52 // boolean because our historical behavior matches neither of the POSIX choices.
53 #define PTHREAD_ATTR_FLAG_INHERIT 0x00000004
54 #define PTHREAD_ATTR_FLAG_EXPLICIT 0x00000008
55 
56 enum ThreadJoinState {
57   THREAD_NOT_JOINED,
58   THREAD_EXITED_NOT_JOINED,
59   THREAD_JOINED,
60   THREAD_DETACHED
61 };
62 
63 class thread_local_dtor;
64 
65 class pthread_internal_t {
66  public:
67   class pthread_internal_t* next;
68   class pthread_internal_t* prev;
69 
70   pid_t tid;
71 
72  private:
73   uint32_t cached_pid_ : 31;
74   uint32_t vforked_ : 1;
75 
76  public:
is_vforked()77   bool is_vforked() { return vforked_; }
78 
invalidate_cached_pid()79   pid_t invalidate_cached_pid() {
80     pid_t old_value;
81     get_cached_pid(&old_value);
82     set_cached_pid(0);
83     return old_value;
84   }
85 
set_cached_pid(pid_t value)86   void set_cached_pid(pid_t value) {
87     cached_pid_ = value;
88   }
89 
get_cached_pid(pid_t * cached_pid)90   bool get_cached_pid(pid_t* cached_pid) {
91     *cached_pid = cached_pid_;
92     return (*cached_pid != 0);
93   }
94 
95   pthread_attr_t attr;
96 
97   _Atomic(ThreadJoinState) join_state;
98 
99   __pthread_cleanup_t* cleanup_stack;
100 
101   void* (*start_routine)(void*);
102   void* start_routine_arg;
103   void* return_value;
104   sigset64_t start_mask;
105 
106   void* alternate_signal_stack;
107 
108   // The start address of the shadow call stack's guard region (arm64 only).
109   // This address is only used to deallocate the shadow call stack on thread
110   // exit; the address of the stack itself is stored only in the x18 register.
111   // Because the protection offered by SCS relies on the secrecy of the stack
112   // address, storing the address here weakens the protection, but only
113   // slightly, because it is relatively easy for an attacker to discover the
114   // address of the guard region anyway (e.g. it can be discovered by reference
115   // to other allocations), but not the stack itself, which is <0.1% of the size
116   // of the guard region.
117   //
118   // There are at least two other options for discovering the start address of
119   // the guard region on thread exit, but they are not as simple as storing in
120   // TLS.
121   // 1) Derive it from the value of the x18 register. This is only possible in
122   //    processes that do not contain legacy code that might clobber x18,
123   //    therefore each process must declare early during process startup whether
124   //    it might load legacy code.
125   // 2) Mark the guard region as such using prctl(PR_SET_VMA_ANON_NAME) and
126   //    discover its address by reading /proc/self/maps. One issue with this is
127   //    that reading /proc/self/maps can race with allocations, so we may need
128   //    code to handle retries.
129   void* shadow_call_stack_guard_region;
130 
131   // A pointer to the top of the stack. This lets android_unsafe_frame_pointer_chase determine the
132   // top of the stack quickly, which would otherwise require special logic for the main thread.
133   uintptr_t stack_top;
134 
135   // Whether the thread is in the process of terminating (has blocked signals), or has already
136   // terminated. This is used by android_run_on_all_threads() to avoid sending a signal to a thread
137   // that will never receive it.
138   _Atomic(bool) terminating;
139 
140   Lock startup_handshake_lock;
141 
142   void* mmap_base;
143   size_t mmap_size;
144 
145   // The location of the VMA to label as the thread's stack_and_tls.
146   void* mmap_base_unguarded;
147   size_t mmap_size_unguarded;
148   char vma_name_buffer[32];
149 
150   thread_local_dtor* thread_local_dtors;
151 
152   /*
153    * The dynamic linker implements dlerror(3), which makes it hard for us to implement this
154    * per-thread buffer by simply using malloc(3) and free(3).
155    */
156   char* current_dlerror;
157 #define __BIONIC_DLERROR_BUFFER_SIZE 512
158   char dlerror_buffer[__BIONIC_DLERROR_BUFFER_SIZE];
159 
160   bionic_tls* bionic_tls;
161 
162   int errno_value;
163 };
164 
165 struct ThreadMapping {
166   char* mmap_base;
167   size_t mmap_size;
168   char* mmap_base_unguarded;
169   size_t mmap_size_unguarded;
170 
171   char* static_tls;
172   char* stack_base;
173   char* stack_top;
174 };
175 
176 __LIBC_HIDDEN__ void __init_tcb(bionic_tcb* tcb, pthread_internal_t* thread);
177 __LIBC_HIDDEN__ void __init_tcb_stack_guard(bionic_tcb* tcb);
178 __LIBC_HIDDEN__ void __init_tcb_dtv(bionic_tcb* tcb);
179 __LIBC_HIDDEN__ void __init_bionic_tls_ptrs(bionic_tcb* tcb, bionic_tls* tls);
180 __LIBC_HIDDEN__ bionic_tls* __allocate_temp_bionic_tls();
181 __LIBC_HIDDEN__ void __free_temp_bionic_tls(bionic_tls* tls);
182 __LIBC_HIDDEN__ void __init_additional_stacks(pthread_internal_t*);
183 __LIBC_HIDDEN__ int __init_thread(pthread_internal_t* thread);
184 __LIBC_HIDDEN__ ThreadMapping __allocate_thread_mapping(size_t stack_size, size_t stack_guard_size);
185 __LIBC_HIDDEN__ void __set_stack_and_tls_vma_name(bool is_main_thread);
186 
187 __LIBC_HIDDEN__ pthread_t __pthread_internal_add(pthread_internal_t* thread);
188 __LIBC_HIDDEN__ pthread_internal_t* __pthread_internal_find(pthread_t pthread_id, const char* caller);
189 __LIBC_HIDDEN__ pid_t __pthread_internal_gettid(pthread_t pthread_id, const char* caller);
190 __LIBC_HIDDEN__ void __pthread_internal_remove(pthread_internal_t* thread);
191 __LIBC_HIDDEN__ void __pthread_internal_remove_and_free(pthread_internal_t* thread);
192 
__get_bionic_tcb()193 static inline __always_inline bionic_tcb* __get_bionic_tcb() {
194   return reinterpret_cast<bionic_tcb*>(&__get_tls()[MIN_TLS_SLOT]);
195 }
196 
197 // Make __get_thread() inlined for performance reason. See http://b/19825434.
__get_thread()198 static inline __always_inline pthread_internal_t* __get_thread() {
199   return static_cast<pthread_internal_t*>(__get_tls()[TLS_SLOT_THREAD_ID]);
200 }
201 
__get_bionic_tls()202 static inline __always_inline bionic_tls& __get_bionic_tls() {
203   return *static_cast<bionic_tls*>(__get_tls()[TLS_SLOT_BIONIC_TLS]);
204 }
205 
__get_tcb_dtv(bionic_tcb * tcb)206 static inline __always_inline TlsDtv* __get_tcb_dtv(bionic_tcb* tcb) {
207   uintptr_t dtv_slot = reinterpret_cast<uintptr_t>(tcb->tls_slot(TLS_SLOT_DTV));
208   return reinterpret_cast<TlsDtv*>(dtv_slot - offsetof(TlsDtv, generation));
209 }
210 
__set_tcb_dtv(bionic_tcb * tcb,TlsDtv * val)211 static inline void __set_tcb_dtv(bionic_tcb* tcb, TlsDtv* val) {
212   tcb->tls_slot(TLS_SLOT_DTV) = &val->generation;
213 }
214 
215 extern "C" __LIBC_HIDDEN__ int __set_tls(void* ptr);
216 
217 __LIBC_HIDDEN__ void pthread_key_clean_all(void);
218 
219 // Address space is precious on LP32, so use the minimum unit: one page.
220 // On LP64, we could use more but there's no obvious advantage to doing
221 // so, and the various media processes use RLIMIT_AS as a way to limit
222 // the amount of allocation they'll do.
223 #define PTHREAD_GUARD_SIZE PAGE_SIZE
224 
225 // SIGSTKSZ (8KiB) is not big enough.
226 // An snprintf to a stack buffer of size PATH_MAX consumes ~7KiB of stack.
227 // On 64-bit, logging uses more than 8KiB by itself, ucontext is comically
228 // large on aarch64, and we have effectively infinite address space, so double
229 // the signal stack size.
230 #if defined(__LP64__)
231 #define SIGNAL_STACK_SIZE_WITHOUT_GUARD (32 * 1024)
232 #else
233 #define SIGNAL_STACK_SIZE_WITHOUT_GUARD (16 * 1024)
234 #endif
235 
236 // Traditionally we gave threads a 1MiB stack. When we started
237 // allocating per-thread alternate signal stacks to ease debugging of
238 // stack overflows, we subtracted the same amount we were using there
239 // from the default thread stack size. This should keep memory usage
240 // roughly constant.
241 #define PTHREAD_STACK_SIZE_DEFAULT ((1 * 1024 * 1024) - SIGNAL_STACK_SIZE_WITHOUT_GUARD)
242 
243 // Leave room for a guard page in the internally created signal stacks.
244 #define SIGNAL_STACK_SIZE (SIGNAL_STACK_SIZE_WITHOUT_GUARD + PTHREAD_GUARD_SIZE)
245 
246 // Needed by fork.
247 __LIBC_HIDDEN__ extern void __bionic_atfork_run_prepare();
248 __LIBC_HIDDEN__ extern void __bionic_atfork_run_child();
249 __LIBC_HIDDEN__ extern void __bionic_atfork_run_parent();
250 
251 extern "C" bool android_run_on_all_threads(bool (*func)(void*), void* arg);
252 
253 extern pthread_rwlock_t g_thread_creation_lock;
254