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 | |
| 42 | #include "private/bionic_elf_tls.h" |
| 43 | #include "private/bionic_globals.h" |
| 44 | #include "private/bionic_tls.h" |
| 45 | #include "pthread_internal.h" |
| 46 | |
| 47 | void __libc_get_static_tls_bounds(void** stls_begin, void** stls_end) { |
| 48 | const StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout; |
| 49 | *stls_begin = reinterpret_cast<char*>(__get_bionic_tcb()) - layout.offset_bionic_tcb(); |
| 50 | *stls_end = reinterpret_cast<char*>(*stls_begin) + layout.size(); |
| 51 | } |
| 52 | |
| 53 | void __libc_register_thread_exit_callback(thread_exit_cb_t cb) { |
| 54 | TlsModules& modules = __libc_shared_globals()->tls_modules; |
| 55 | |
| 56 | if (modules.first_thread_exit_callback == nullptr) { |
| 57 | modules.first_thread_exit_callback = cb; |
| 58 | return; |
| 59 | }; |
| 60 | |
| 61 | BionicAllocator& allocator = __libc_shared_globals()->tls_allocator; |
| 62 | CallbackHolder* new_node = |
| 63 | reinterpret_cast<CallbackHolder*>(allocator.alloc(sizeof(CallbackHolder))); |
| 64 | new_node->cb = cb; |
| 65 | new_node->prev = modules.thread_exit_callback_tail_node; |
| 66 | modules.thread_exit_callback_tail_node = new_node; |
| 67 | } |
| 68 | |
| 69 | static inline __always_inline bionic_tcb* __get_bionic_tcb_for_thread(pid_t tid) { |
| 70 | // If tid is same as self, then we don't need ptrace. |
| 71 | if (gettid() == tid) return __get_bionic_tcb(); |
| 72 | |
| 73 | // Find the thread-pointer register for the given thread. |
| 74 | void** tp_reg = nullptr; |
| 75 | |
| 76 | #if defined(__x86_64__) || defined(__i386__) |
| 77 | struct user_regs_struct regs; |
| 78 | struct iovec pt_iov = { |
| 79 | .iov_base = ®s, |
| 80 | .iov_len = sizeof(regs), |
| 81 | }; |
| 82 | if (ptrace(PTRACE_GETREGSET, tid, NT_PRSTATUS, &pt_iov) == 0) { |
| 83 | #if defined(__x86_64__) |
| 84 | tp_reg = reinterpret_cast<void**>(regs.fs); |
| 85 | #elif defined(__i386__) |
| 86 | tp_reg = reinterpret_cast<void**>(regs.xgs); |
| 87 | #endif |
| 88 | } |
| 89 | #elif defined(__aarch64__) || defined(__arm__) |
| 90 | uint64_t reg; |
| 91 | struct iovec pt_iov { |
| 92 | .iov_base = ®, .iov_len = sizeof(reg), |
| 93 | }; |
| 94 | |
| 95 | if (ptrace(PTRACE_GETREGSET, tid, NT_ARM_TLS, &pt_iov) == 0) { |
| 96 | tp_reg = reinterpret_cast<void**>(reg); |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | if (tp_reg == nullptr) { |
| 101 | async_safe_write_log(ANDROID_LOG_FATAL, "libc", |
| 102 | "__get_bionic_tcb_for_thread failed to read thread register."); |
| 103 | } |
| 104 | |
| 105 | return reinterpret_cast<bionic_tcb*>(&tp_reg[MIN_TLS_SLOT]); |
| 106 | } |
| 107 | |
| 108 | void __libc_iterate_dynamic_tls(pid_t tid, |
| 109 | void (*cb)(void* __dynamic_tls_begin, void* __dynamic_tls_end, |
| 110 | size_t __dso_id, void* __arg), |
| 111 | void* arg) { |
| 112 | TlsModules& modules = __libc_shared_globals()->tls_modules; |
| 113 | bionic_tcb* const tcb = __get_bionic_tcb_for_thread(tid); |
| 114 | TlsDtv* const dtv = __get_tcb_dtv(tcb); |
| 115 | BionicAllocator& allocator = __libc_shared_globals()->tls_allocator; |
| 116 | |
| 117 | for (size_t i = modules.static_module_count; i < dtv->count; ++i) { |
| 118 | void* dtls_begin = dtv->modules[i]; |
| 119 | if (dtls_begin == nullptr) continue; |
| 120 | void* dtls_end = |
| 121 | static_cast<void*>(static_cast<char*>(dtls_begin) + allocator.get_chunk_size(dtls_begin)); |
| 122 | size_t dso_id = __tls_module_idx_to_id(i); |
| 123 | |
| 124 | cb(dtls_begin, dtls_end, dso_id, arg); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void __libc_register_dynamic_tls_listeners(dtls_listener_t on_creation, |
| 129 | dtls_listener_t on_destruction) { |
| 130 | TlsModules& tls_modules = __libc_shared_globals()->tls_modules; |
| 131 | tls_modules.on_creation_cb = on_creation; |
| 132 | tls_modules.on_destruction_cb = on_destruction; |
| 133 | } |