Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Ryan Prichard | 4809755 | 2019-01-06 18:24:10 -0800 | [diff] [blame] | 31 | #include <link.h> |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 32 | #include <pthread.h> |
| 33 | #include <stdatomic.h> |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 34 | #include <stdint.h> |
Ryan Prichard | 4809755 | 2019-01-06 18:24:10 -0800 | [diff] [blame] | 35 | #include <sys/cdefs.h> |
| 36 | |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 37 | __LIBC_HIDDEN__ extern _Atomic(size_t) __libc_tls_generation_copy; |
| 38 | |
Ryan Prichard | 4809755 | 2019-01-06 18:24:10 -0800 | [diff] [blame] | 39 | struct TlsSegment { |
| 40 | size_t size = 0; |
| 41 | size_t alignment = 1; |
| 42 | const void* init_ptr = ""; // Field is non-null even when init_size is 0. |
| 43 | size_t init_size = 0; |
| 44 | }; |
| 45 | |
| 46 | __LIBC_HIDDEN__ bool __bionic_get_tls_segment(const ElfW(Phdr)* phdr_table, size_t phdr_count, |
Ryan Prichard | 1988350 | 2019-01-16 23:13:38 -0800 | [diff] [blame] | 47 | ElfW(Addr) load_bias, TlsSegment* out); |
| 48 | |
| 49 | __LIBC_HIDDEN__ bool __bionic_check_tls_alignment(size_t* alignment); |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 50 | |
| 51 | struct StaticTlsLayout { |
| 52 | constexpr StaticTlsLayout() {} |
| 53 | |
| 54 | private: |
| 55 | size_t offset_ = 0; |
| 56 | size_t alignment_ = 1; |
| 57 | bool overflowed_ = false; |
| 58 | |
| 59 | // Offsets to various Bionic TLS structs from the beginning of static TLS. |
| 60 | size_t offset_bionic_tcb_ = SIZE_MAX; |
| 61 | size_t offset_bionic_tls_ = SIZE_MAX; |
| 62 | |
| 63 | public: |
| 64 | size_t offset_bionic_tcb() const { return offset_bionic_tcb_; } |
| 65 | size_t offset_bionic_tls() const { return offset_bionic_tls_; } |
Ryan Prichard | fb8730d | 2019-01-15 00:11:37 -0800 | [diff] [blame] | 66 | size_t offset_thread_pointer() const; |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 67 | |
| 68 | size_t size() const { return offset_; } |
| 69 | size_t alignment() const { return alignment_; } |
| 70 | bool overflowed() const { return overflowed_; } |
| 71 | |
Ryan Prichard | 977e47d | 2019-01-14 21:52:14 -0800 | [diff] [blame] | 72 | size_t reserve_exe_segment_and_tcb(const TlsSegment* exe_segment, const char* progname); |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 73 | void reserve_bionic_tls(); |
Ryan Prichard | 977e47d | 2019-01-14 21:52:14 -0800 | [diff] [blame] | 74 | size_t reserve_solib_segment(const TlsSegment& segment) { |
| 75 | return reserve(segment.size, segment.alignment); |
| 76 | } |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 77 | void finish_layout(); |
| 78 | |
| 79 | private: |
| 80 | size_t reserve(size_t size, size_t alignment); |
| 81 | |
| 82 | template <typename T> size_t reserve_type() { |
| 83 | return reserve(sizeof(T), alignof(T)); |
| 84 | } |
| 85 | |
| 86 | size_t round_up_with_overflow_check(size_t value, size_t alignment); |
| 87 | }; |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 88 | |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 89 | static constexpr size_t kTlsGenerationNone = 0; |
| 90 | static constexpr size_t kTlsGenerationFirst = 1; |
| 91 | |
| 92 | // The first ELF TLS module has ID 1. Zero is reserved for the first word of |
| 93 | // the DTV, a generation count. Unresolved weak symbols also use module ID 0. |
| 94 | static constexpr size_t kTlsUninitializedModuleId = 0; |
| 95 | |
| 96 | static inline size_t __tls_module_id_to_idx(size_t id) { return id - 1; } |
| 97 | static inline size_t __tls_module_idx_to_id(size_t idx) { return idx + 1; } |
| 98 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 99 | // A descriptor for a single ELF TLS module. |
| 100 | struct TlsModule { |
| 101 | TlsSegment segment; |
| 102 | |
| 103 | // Offset into the static TLS block or SIZE_MAX for a dynamic module. |
| 104 | size_t static_offset = SIZE_MAX; |
| 105 | |
| 106 | // The generation in which this module was loaded. Dynamic TLS lookups use |
| 107 | // this field to detect when a module has been unloaded. |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 108 | size_t first_generation = kTlsGenerationNone; |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 109 | |
| 110 | // Used by the dynamic linker to track the associated soinfo* object. |
| 111 | void* soinfo_ptr = nullptr; |
| 112 | }; |
| 113 | |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 114 | // Signature of the callbacks that will be called after DTLS creation and |
| 115 | // before DTLS destruction. |
| 116 | typedef void (*dtls_listener_t)(void* dynamic_tls_begin, void* dynamic_tls_end); |
| 117 | |
| 118 | // Signature of the thread-exit callbacks. |
| 119 | typedef void (*thread_exit_cb_t)(void); |
| 120 | |
| 121 | struct CallbackHolder { |
| 122 | thread_exit_cb_t cb; |
| 123 | CallbackHolder* prev; |
| 124 | }; |
| 125 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 126 | // Table of the ELF TLS modules. Either the dynamic linker or the static |
| 127 | // initialization code prepares this table, and it's then used during thread |
| 128 | // creation and for dynamic TLS lookups. |
| 129 | struct TlsModules { |
| 130 | constexpr TlsModules() {} |
| 131 | |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 132 | // A pointer to the TLS generation counter in libc.so. The counter is |
| 133 | // incremented each time an solib is loaded or unloaded. |
| 134 | _Atomic(size_t) generation = kTlsGenerationFirst; |
| 135 | _Atomic(size_t) *generation_libc_so = nullptr; |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 136 | |
| 137 | // Access to the TlsModule[] table requires taking this lock. |
| 138 | pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; |
| 139 | |
| 140 | // Pointer to a block of TlsModule objects. The first module has ID 1 and |
| 141 | // is stored at index 0 in this table. |
| 142 | size_t module_count = 0; |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 143 | size_t static_module_count = 0; |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 144 | TlsModule* module_table = nullptr; |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 145 | |
| 146 | // Callback to be invoked after a dynamic TLS allocation. |
| 147 | dtls_listener_t on_creation_cb = nullptr; |
| 148 | |
| 149 | // Callback to be invoked before a dynamic TLS deallocation. |
| 150 | dtls_listener_t on_destruction_cb = nullptr; |
| 151 | |
| 152 | // The first thread-exit callback; inlined to avoid allocation. |
| 153 | thread_exit_cb_t first_thread_exit_callback = nullptr; |
| 154 | |
| 155 | // The additional callbacks, if any. |
| 156 | CallbackHolder* thread_exit_callback_tail_node = nullptr; |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 157 | }; |
Ryan Prichard | 361c1b4 | 2019-01-15 13:45:27 -0800 | [diff] [blame] | 158 | |
| 159 | void __init_static_tls(void* static_tls); |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 160 | |
| 161 | // Dynamic Thread Vector. Each thread has a different DTV. For each module |
| 162 | // (executable or solib), the DTV has a pointer to that module's TLS memory. The |
| 163 | // DTV is initially empty and is allocated on-demand. It grows as more modules |
| 164 | // are dlopen'ed. See https://www.akkadia.org/drepper/tls.pdf. |
| 165 | // |
| 166 | // The layout of the DTV is specified in various documents, but it is not part |
| 167 | // of Bionic's public ABI. A compiler can't generate code to access it directly, |
| 168 | // because it can't access libc's global generation counter. |
| 169 | struct TlsDtv { |
| 170 | // Number of elements in this object's modules field. |
| 171 | size_t count; |
| 172 | |
| 173 | // A pointer to an older TlsDtv object that should be freed when the thread |
| 174 | // exits. The objects aren't immediately freed because a DTV could be |
| 175 | // reallocated by a signal handler that interrupted __tls_get_addr's fast |
| 176 | // path. |
| 177 | TlsDtv* next; |
| 178 | |
| 179 | // The DTV slot points at this field, which allows omitting an add instruction |
| 180 | // on the fast path for a TLS lookup. The arm64 tlsdesc_resolver.S depends on |
| 181 | // the layout of fields past this point. |
| 182 | size_t generation; |
| 183 | void* modules[]; |
| 184 | }; |
| 185 | |
| 186 | struct TlsIndex { |
| 187 | size_t module_id; |
| 188 | size_t offset; |
| 189 | }; |
| 190 | |
| 191 | #if defined(__i386__) |
| 192 | #define TLS_GET_ADDR_CCONV __attribute__((regparm(1))) |
| 193 | #define TLS_GET_ADDR ___tls_get_addr |
| 194 | #else |
| 195 | #define TLS_GET_ADDR_CCONV |
| 196 | #define TLS_GET_ADDR __tls_get_addr |
| 197 | #endif |
| 198 | |
| 199 | extern "C" void* TLS_GET_ADDR(const TlsIndex* ti) TLS_GET_ADDR_CCONV; |
| 200 | |
| 201 | struct bionic_tcb; |
| 202 | void __free_dynamic_tls(bionic_tcb* tcb); |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 203 | void __notify_thread_exit_callbacks(); |