1 /*
2  * Copyright (C) 2020 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 #include <sys/thread_properties.h>
30 
31 #include <async_safe/CHECK.h>
32 #include <async_safe/log.h>
33 
34 #include <elf.h>
35 #include <pthread.h>
36 #include <unistd.h>
37 
38 #include <sys/ptrace.h>
39 #include <sys/uio.h>
40 #include <sys/user.h>
41 
42 #if defined(__i386__)
43 #include <asm/ldt.h>
44 #endif
45 
46 #include "private/ErrnoRestorer.h"
47 #include "private/bionic_elf_tls.h"
48 #include "private/bionic_globals.h"
49 #include "private/bionic_tls.h"
50 #include "pthread_internal.h"
51 
__libc_get_static_tls_bounds(void ** stls_begin,void ** stls_end)52 void __libc_get_static_tls_bounds(void** stls_begin, void** stls_end) {
53   const StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
54   *stls_begin = reinterpret_cast<char*>(__get_bionic_tcb()) - layout.offset_bionic_tcb();
55   *stls_end = reinterpret_cast<char*>(*stls_begin) + layout.size();
56 }
57 
__libc_register_thread_exit_callback(thread_exit_cb_t cb)58 void __libc_register_thread_exit_callback(thread_exit_cb_t cb) {
59   TlsModules& modules = __libc_shared_globals()->tls_modules;
60 
61   if (modules.first_thread_exit_callback == nullptr) {
62     modules.first_thread_exit_callback = cb;
63     return;
64   }
65 
66   BionicAllocator& allocator = __libc_shared_globals()->tls_allocator;
67   CallbackHolder* new_node =
68       reinterpret_cast<CallbackHolder*>(allocator.alloc(sizeof(CallbackHolder)));
69   new_node->cb = cb;
70   new_node->prev = modules.thread_exit_callback_tail_node;
71   modules.thread_exit_callback_tail_node = new_node;
72 }
73 
__get_bionic_tcb_for_thread(pid_t tid)74 static inline __always_inline bionic_tcb* __get_bionic_tcb_for_thread(pid_t tid) {
75   // If tid is same as self, then we don't need ptrace.
76   if (gettid() == tid) return __get_bionic_tcb();
77 
78   // Find the thread-pointer register for the given thread.
79   void** tp_reg = nullptr;
80 #if defined(__aarch64__)
81   uint64_t reg;
82   struct iovec pt_iov { .iov_base = &reg, .iov_len = sizeof(reg) };
83   if (ptrace(PTRACE_GETREGSET, tid, NT_ARM_TLS, &pt_iov) == 0) {
84     tp_reg = reinterpret_cast<void**>(reg);
85   }
86 #elif defined(__arm__)
87   if (ptrace(PTRACE_GET_THREAD_AREA, tid, nullptr, &tp_reg) != 0) {
88     // Reset the tp_reg if ptrace was unsuccessful.
89     tp_reg = nullptr;
90   }
91 #elif defined(__i386__)
92   struct user_regs_struct regs;
93   struct iovec pt_iov = { .iov_base = &regs, .iov_len = sizeof(regs) };
94   if (ptrace(PTRACE_GETREGSET, tid, NT_PRSTATUS, &pt_iov) == 0) {
95     struct user_desc u_info;
96     u_info.entry_number = regs.xgs >> 3;
97     if (ptrace(PTRACE_GET_THREAD_AREA, tid, u_info.entry_number, &u_info) == 0) {
98       tp_reg = reinterpret_cast<void**>(u_info.base_addr);
99     }
100   }
101 #elif defined(__riscv)
102   struct user_regs_struct regs;
103   struct iovec pt_iov = { .iov_base = &regs, .iov_len = sizeof(regs) };
104   if (ptrace(PTRACE_GETREGSET, tid, NT_PRSTATUS, &pt_iov) == 0) {
105     tp_reg = reinterpret_cast<void**>(regs.tp);
106   }
107 #elif defined(__x86_64__)
108   {
109     ErrnoRestorer errno_restorer;
110     errno = 0;
111     uintptr_t fs_base = ptrace(PTRACE_PEEKUSER, tid, offsetof(user_regs_struct, fs_base), nullptr);
112     if (errno == 0) {
113       tp_reg = reinterpret_cast<void**>(fs_base);
114     }
115   }
116 #endif
117 
118   if (tp_reg == nullptr) {
119     async_safe_write_log(ANDROID_LOG_FATAL, "libc",
120                          "__get_bionic_tcb_for_thread failed to read thread register.");
121   }
122 
123   return reinterpret_cast<bionic_tcb*>(&tp_reg[MIN_TLS_SLOT]);
124 }
125 
__libc_iterate_dynamic_tls(pid_t tid,void (* cb)(void * __dynamic_tls_begin,void * __dynamic_tls_end,size_t __dso_id,void * __arg),void * arg)126 void __libc_iterate_dynamic_tls(pid_t tid,
127                                 void (*cb)(void* __dynamic_tls_begin, void* __dynamic_tls_end,
128                                            size_t __dso_id, void* __arg),
129                                 void* arg) {
130   TlsModules& modules = __libc_shared_globals()->tls_modules;
131   bionic_tcb* const tcb = __get_bionic_tcb_for_thread(tid);
132   TlsDtv* const dtv = __get_tcb_dtv(tcb);
133   BionicAllocator& allocator = __libc_shared_globals()->tls_allocator;
134 
135   for (size_t i = modules.static_module_count; i < dtv->count; ++i) {
136     void* dtls_begin = dtv->modules[i];
137     if (dtls_begin == nullptr) continue;
138     void* dtls_end =
139         static_cast<void*>(static_cast<char*>(dtls_begin) + allocator.get_chunk_size(dtls_begin));
140     size_t dso_id = __tls_module_idx_to_id(i);
141 
142     cb(dtls_begin, dtls_end, dso_id, arg);
143   }
144 }
145 
__libc_register_dynamic_tls_listeners(dtls_listener_t on_creation,dtls_listener_t on_destruction)146 void __libc_register_dynamic_tls_listeners(dtls_listener_t on_creation,
147                                            dtls_listener_t on_destruction) {
148   TlsModules& tls_modules = __libc_shared_globals()->tls_modules;
149   tls_modules.on_creation_cb = on_creation;
150   tls_modules.on_destruction_cb = on_destruction;
151 }
152