| Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 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 |  | 
| Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame] | 42 | #if defined(__i386__) | 
 | 43 | #include <asm/ldt.h> | 
 | 44 | #endif | 
 | 45 |  | 
 | 46 | #include "private/ErrnoRestorer.h" | 
| Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 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 |  | 
 | 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 |  | 
 | 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 |  | 
 | 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; | 
| Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame] | 80 | #if defined(__x86_64__) | 
 | 81 |   { | 
 | 82 |     ErrnoRestorer errno_restorer; | 
 | 83 |     errno = 0; | 
 | 84 |     uintptr_t fs_base = ptrace(PTRACE_PEEKUSER, tid, offsetof(user_regs_struct, fs_base), nullptr); | 
 | 85 |     if (errno == 0) { | 
 | 86 |       tp_reg = reinterpret_cast<void**>(fs_base); | 
 | 87 |     } | 
 | 88 |   } | 
 | 89 | #elif defined(__i386__) | 
| Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 90 |   struct user_regs_struct regs; | 
 | 91 |   struct iovec pt_iov = { | 
 | 92 |       .iov_base = ®s, | 
 | 93 |       .iov_len = sizeof(regs), | 
 | 94 |   }; | 
| Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame] | 95 |  | 
| Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 96 |   if (ptrace(PTRACE_GETREGSET, tid, NT_PRSTATUS, &pt_iov) == 0) { | 
| Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame] | 97 |     struct user_desc u_info; | 
 | 98 |     u_info.entry_number = regs.xgs >> 3; | 
 | 99 |     if (ptrace(PTRACE_GET_THREAD_AREA, tid, u_info.entry_number, &u_info) == 0) { | 
 | 100 |       tp_reg = reinterpret_cast<void**>(u_info.base_addr); | 
 | 101 |     } | 
| Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 102 |   } | 
| Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame] | 103 | #elif defined(__aarch64__) | 
| Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 104 |   uint64_t reg; | 
 | 105 |   struct iovec pt_iov { | 
 | 106 |     .iov_base = ®, .iov_len = sizeof(reg), | 
 | 107 |   }; | 
 | 108 |  | 
 | 109 |   if (ptrace(PTRACE_GETREGSET, tid, NT_ARM_TLS, &pt_iov) == 0) { | 
 | 110 |     tp_reg = reinterpret_cast<void**>(reg); | 
 | 111 |   } | 
| Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame] | 112 | #elif defined(__arm__) | 
 | 113 |   if (ptrace(PTRACE_GET_THREAD_AREA, tid, nullptr, &tp_reg) != 0) { | 
 | 114 |     // Reset the tp_reg if ptrace was unsuccessful. | 
 | 115 |     tp_reg = nullptr; | 
 | 116 |   } | 
| Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 117 | #endif | 
 | 118 |  | 
 | 119 |   if (tp_reg == nullptr) { | 
 | 120 |     async_safe_write_log(ANDROID_LOG_FATAL, "libc", | 
 | 121 |                          "__get_bionic_tcb_for_thread failed to read thread register."); | 
 | 122 |   } | 
 | 123 |  | 
 | 124 |   return reinterpret_cast<bionic_tcb*>(&tp_reg[MIN_TLS_SLOT]); | 
 | 125 | } | 
 | 126 |  | 
 | 127 | void __libc_iterate_dynamic_tls(pid_t tid, | 
 | 128 |                                 void (*cb)(void* __dynamic_tls_begin, void* __dynamic_tls_end, | 
 | 129 |                                            size_t __dso_id, void* __arg), | 
 | 130 |                                 void* arg) { | 
 | 131 |   TlsModules& modules = __libc_shared_globals()->tls_modules; | 
 | 132 |   bionic_tcb* const tcb = __get_bionic_tcb_for_thread(tid); | 
 | 133 |   TlsDtv* const dtv = __get_tcb_dtv(tcb); | 
 | 134 |   BionicAllocator& allocator = __libc_shared_globals()->tls_allocator; | 
 | 135 |  | 
 | 136 |   for (size_t i = modules.static_module_count; i < dtv->count; ++i) { | 
 | 137 |     void* dtls_begin = dtv->modules[i]; | 
 | 138 |     if (dtls_begin == nullptr) continue; | 
 | 139 |     void* dtls_end = | 
 | 140 |         static_cast<void*>(static_cast<char*>(dtls_begin) + allocator.get_chunk_size(dtls_begin)); | 
 | 141 |     size_t dso_id = __tls_module_idx_to_id(i); | 
 | 142 |  | 
 | 143 |     cb(dtls_begin, dtls_end, dso_id, arg); | 
 | 144 |   } | 
 | 145 | } | 
 | 146 |  | 
 | 147 | void __libc_register_dynamic_tls_listeners(dtls_listener_t on_creation, | 
 | 148 |                                            dtls_listener_t on_destruction) { | 
 | 149 |   TlsModules& tls_modules = __libc_shared_globals()->tls_modules; | 
 | 150 |   tls_modules.on_creation_cb = on_creation; | 
 | 151 |   tls_modules.on_destruction_cb = on_destruction; | 
 | 152 | } |