Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 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 | #include <pthread.h> |
| 30 | |
| 31 | #include <errno.h> |
Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 32 | #include <string.h> |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 33 | #include <sys/mman.h> |
Elliott Hughes | 99d5465 | 2018-08-22 10:36:23 -0700 | [diff] [blame] | 34 | #include <sys/prctl.h> |
Peter Collingbourne | da772e2 | 2018-09-06 22:20:44 -0700 | [diff] [blame] | 35 | #include <sys/random.h> |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 36 | #include <unistd.h> |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 37 | |
| 38 | #include "pthread_internal.h" |
| 39 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 40 | #include <async_safe/log.h> |
| 41 | |
Peter Collingbourne | 734beec | 2018-11-14 12:41:41 -0800 | [diff] [blame] | 42 | #include "private/bionic_constants.h" |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 43 | #include "private/bionic_defs.h" |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 44 | #include "private/bionic_globals.h" |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 45 | #include "private/bionic_macros.h" |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 46 | #include "private/bionic_ssp.h" |
| 47 | #include "private/bionic_tls.h" |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 48 | #include "private/ErrnoRestorer.h" |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 50 | // x86 uses segment descriptors rather than a direct pointer to TLS. |
Josh Gao | cb728e6 | 2016-09-15 13:56:37 -0700 | [diff] [blame] | 51 | #if defined(__i386__) |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 52 | #include <asm/ldt.h> |
Elliott Hughes | 01b85d5 | 2016-02-09 22:44:16 -0800 | [diff] [blame] | 53 | void __init_user_desc(struct user_desc*, bool, void*); |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 54 | #endif |
| 55 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 56 | // This code is used both by each new pthread and the code that initializes the main thread. |
Ryan Prichard | 9cfca86 | 2018-11-22 02:44:09 -0800 | [diff] [blame] | 57 | __attribute__((no_stack_protector)) |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 58 | void __init_tcb(bionic_tcb* tcb, pthread_internal_t* thread) { |
| 59 | #ifdef TLS_SLOT_SELF |
| 60 | // On x86, slot 0 must point to itself so code can read the thread pointer by |
| 61 | // loading %fs:0 or %gs:0. |
| 62 | tcb->tls_slot(TLS_SLOT_SELF) = &tcb->tls_slot(TLS_SLOT_SELF); |
| 63 | #endif |
| 64 | tcb->tls_slot(TLS_SLOT_THREAD_ID) = thread; |
Ryan Prichard | 9cfca86 | 2018-11-22 02:44:09 -0800 | [diff] [blame] | 65 | } |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 66 | |
Ryan Prichard | 9cfca86 | 2018-11-22 02:44:09 -0800 | [diff] [blame] | 67 | __attribute__((no_stack_protector)) |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 68 | void __init_tcb_stack_guard(bionic_tcb* tcb) { |
Ryan Prichard | 9cfca86 | 2018-11-22 02:44:09 -0800 | [diff] [blame] | 69 | // GCC looks in the TLS for the stack guard on x86, so copy it there from our global. |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 70 | tcb->tls_slot(TLS_SLOT_STACK_GUARD) = reinterpret_cast<void*>(__stack_chk_guard); |
Ryan Prichard | 9cfca86 | 2018-11-22 02:44:09 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 73 | void __init_bionic_tls_ptrs(bionic_tcb* tcb, bionic_tls* tls) { |
| 74 | tcb->thread()->bionic_tls = tls; |
| 75 | tcb->tls_slot(TLS_SLOT_BIONIC_TLS) = tls; |
| 76 | } |
| 77 | |
| 78 | // Allocate a temporary bionic_tls that the dynamic linker's main thread can |
| 79 | // use while it's loading the initial set of ELF modules. |
| 80 | bionic_tls* __allocate_temp_bionic_tls() { |
| 81 | size_t allocation_size = __BIONIC_ALIGN(sizeof(bionic_tls), PAGE_SIZE); |
| 82 | void* allocation = mmap(nullptr, allocation_size, |
| 83 | PROT_READ | PROT_WRITE, |
| 84 | MAP_PRIVATE | MAP_ANONYMOUS, |
| 85 | -1, 0); |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 86 | if (allocation == MAP_FAILED) { |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 87 | // Avoid strerror because it might need bionic_tls. |
| 88 | async_safe_fatal("failed to allocate bionic_tls: error %d", errno); |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 89 | } |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 90 | return static_cast<bionic_tls*>(allocation); |
| 91 | } |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 92 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 93 | void __free_temp_bionic_tls(bionic_tls* tls) { |
| 94 | munmap(tls, __BIONIC_ALIGN(sizeof(bionic_tls), PAGE_SIZE)); |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 95 | } |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 96 | |
Peter Collingbourne | da772e2 | 2018-09-06 22:20:44 -0700 | [diff] [blame] | 97 | static void __init_alternate_signal_stack(pthread_internal_t* thread) { |
Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 98 | // Create and set an alternate signal stack. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 99 | void* stack_base = mmap(nullptr, SIGNAL_STACK_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
Yabin Cui | ef11500 | 2015-03-30 20:03:57 -0700 | [diff] [blame] | 100 | if (stack_base != MAP_FAILED) { |
Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 101 | // Create a guard to catch stack overflows in signal handlers. |
| 102 | if (mprotect(stack_base, PTHREAD_GUARD_SIZE, PROT_NONE) == -1) { |
Yabin Cui | ef11500 | 2015-03-30 20:03:57 -0700 | [diff] [blame] | 103 | munmap(stack_base, SIGNAL_STACK_SIZE); |
| 104 | return; |
| 105 | } |
| 106 | stack_t ss; |
Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 107 | ss.ss_sp = reinterpret_cast<uint8_t*>(stack_base) + PTHREAD_GUARD_SIZE; |
| 108 | ss.ss_size = SIGNAL_STACK_SIZE - PTHREAD_GUARD_SIZE; |
Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 109 | ss.ss_flags = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 110 | sigaltstack(&ss, nullptr); |
Yabin Cui | ef11500 | 2015-03-30 20:03:57 -0700 | [diff] [blame] | 111 | thread->alternate_signal_stack = stack_base; |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 112 | |
| 113 | // We can only use const static allocated string for mapped region name, as Android kernel |
| 114 | // uses the string pointer directly when dumping /proc/pid/maps. |
Elliott Hughes | a3125fd | 2015-03-31 02:42:39 +0000 | [diff] [blame] | 115 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ss.ss_sp, ss.ss_size, "thread signal stack"); |
Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 116 | } |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Peter Collingbourne | da772e2 | 2018-09-06 22:20:44 -0700 | [diff] [blame] | 119 | static void __init_shadow_call_stack(pthread_internal_t* thread __unused) { |
| 120 | #ifdef __aarch64__ |
Peter Collingbourne | 734beec | 2018-11-14 12:41:41 -0800 | [diff] [blame] | 121 | // Allocate the stack and store its address in register x18. The address is aligned to SCS_SIZE so |
| 122 | // that we only need to store the lower log2(SCS_SIZE) bits in jmp_buf. |
| 123 | // TODO(pcc): We ought to allocate a larger guard region here and then allocate the SCS at a |
| 124 | // random location within it. This will provide greater security since it would mean that an |
| 125 | // attacker who can read the pthread_internal_t won't be able to discover the address of the SCS. |
| 126 | // However, doing so is blocked on a solution to b/118642754. |
| 127 | char* scs_guard_region = reinterpret_cast<char*>( |
| 128 | mmap(nullptr, SCS_GUARD_REGION_SIZE, 0, MAP_PRIVATE | MAP_ANON, -1, 0)); |
| 129 | thread->shadow_call_stack_guard_region = scs_guard_region; |
| 130 | |
| 131 | char* scs = |
| 132 | reinterpret_cast<char*>(align_up(reinterpret_cast<uintptr_t>(scs_guard_region), SCS_SIZE)); |
| 133 | mprotect(scs, SCS_SIZE, PROT_READ | PROT_WRITE); |
Peter Collingbourne | da772e2 | 2018-09-06 22:20:44 -0700 | [diff] [blame] | 134 | __asm__ __volatile__("mov x18, %0" ::"r"(scs)); |
| 135 | #endif |
| 136 | } |
| 137 | |
| 138 | void __init_additional_stacks(pthread_internal_t* thread) { |
| 139 | __init_alternate_signal_stack(thread); |
| 140 | __init_shadow_call_stack(thread); |
| 141 | } |
| 142 | |
Yabin Cui | 673b15e | 2015-03-19 14:19:19 -0700 | [diff] [blame] | 143 | int __init_thread(pthread_internal_t* thread) { |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 144 | thread->cleanup_stack = nullptr; |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 145 | |
Yabin Cui | 58cf31b | 2015-03-06 17:23:53 -0800 | [diff] [blame] | 146 | if (__predict_true((thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) == 0)) { |
| 147 | atomic_init(&thread->join_state, THREAD_NOT_JOINED); |
| 148 | } else { |
| 149 | atomic_init(&thread->join_state, THREAD_DETACHED); |
| 150 | } |
| 151 | |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 152 | // Set the scheduling policy/priority of the thread if necessary. |
| 153 | bool need_set = true; |
| 154 | int policy; |
| 155 | sched_param param; |
Elliott Hughes | 38f01e0 | 2017-10-27 15:28:54 -0700 | [diff] [blame] | 156 | if ((thread->attr.flags & PTHREAD_ATTR_FLAG_INHERIT) != 0) { |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 157 | // Unless the parent has SCHED_RESET_ON_FORK set, we've already inherited from the parent. |
| 158 | policy = sched_getscheduler(0); |
| 159 | need_set = ((policy & SCHED_RESET_ON_FORK) != 0); |
| 160 | if (need_set) { |
| 161 | if (policy == -1) { |
| 162 | async_safe_format_log(ANDROID_LOG_WARN, "libc", |
| 163 | "pthread_create sched_getscheduler failed: %s", strerror(errno)); |
| 164 | return errno; |
| 165 | } |
| 166 | if (sched_getparam(0, ¶m) == -1) { |
| 167 | async_safe_format_log(ANDROID_LOG_WARN, "libc", |
| 168 | "pthread_create sched_getparam failed: %s", strerror(errno)); |
| 169 | return errno; |
| 170 | } |
| 171 | } |
| 172 | } else { |
| 173 | policy = thread->attr.sched_policy; |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 174 | param.sched_priority = thread->attr.sched_priority; |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 175 | } |
Elliott Hughes | 38f01e0 | 2017-10-27 15:28:54 -0700 | [diff] [blame] | 176 | // Backwards compatibility: before P, Android didn't have pthread_attr_setinheritsched, |
| 177 | // and our behavior was neither of the POSIX behaviors. |
| 178 | if ((thread->attr.flags & (PTHREAD_ATTR_FLAG_INHERIT|PTHREAD_ATTR_FLAG_EXPLICIT)) == 0) { |
| 179 | need_set = (thread->attr.sched_policy != SCHED_NORMAL); |
| 180 | } |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 181 | if (need_set) { |
| 182 | if (sched_setscheduler(thread->tid, policy, ¶m) == -1) { |
| 183 | async_safe_format_log(ANDROID_LOG_WARN, "libc", |
| 184 | "pthread_create sched_setscheduler(%d, {%d}) call failed: %s", policy, |
| 185 | param.sched_priority, strerror(errno)); |
Josh Gao | b36efa4 | 2016-09-15 13:55:41 -0700 | [diff] [blame] | 186 | #if defined(__LP64__) |
Elliott Hughes | 98624c3 | 2013-10-15 16:51:17 -0700 | [diff] [blame] | 187 | // For backwards compatibility reasons, we only report failures on 64-bit devices. |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 188 | return errno; |
Elliott Hughes | 98624c3 | 2013-10-15 16:51:17 -0700 | [diff] [blame] | 189 | #endif |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 193 | return 0; |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 196 | |
| 197 | // Allocate a thread's primary mapping. This mapping includes static TLS and |
| 198 | // optionally a stack. Static TLS includes ELF TLS segments and the bionic_tls |
| 199 | // struct. |
| 200 | // |
| 201 | // The stack_guard_size must be a multiple of the PAGE_SIZE. |
| 202 | ThreadMapping __allocate_thread_mapping(size_t stack_size, size_t stack_guard_size) { |
| 203 | const StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout; |
| 204 | |
| 205 | // Allocate in order: stack guard, stack, static TLS, guard page. |
| 206 | size_t mmap_size; |
| 207 | if (__builtin_add_overflow(stack_size, stack_guard_size, &mmap_size)) return {}; |
| 208 | if (__builtin_add_overflow(mmap_size, layout.size(), &mmap_size)) return {}; |
| 209 | if (__builtin_add_overflow(mmap_size, PTHREAD_GUARD_SIZE, &mmap_size)) return {}; |
| 210 | |
| 211 | // Align the result to a page size. |
| 212 | const size_t unaligned_size = mmap_size; |
| 213 | mmap_size = __BIONIC_ALIGN(mmap_size, PAGE_SIZE); |
| 214 | if (mmap_size < unaligned_size) return {}; |
| 215 | |
| 216 | // Create a new private anonymous map. Make the entire mapping PROT_NONE, then carve out a |
| 217 | // read+write area in the middle. |
| 218 | const int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE; |
| 219 | char* const space = static_cast<char*>(mmap(nullptr, mmap_size, PROT_NONE, flags, -1, 0)); |
Yabin Cui | ba8dfc2 | 2015-01-06 09:31:00 -0800 | [diff] [blame] | 220 | if (space == MAP_FAILED) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 221 | async_safe_format_log(ANDROID_LOG_WARN, |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 222 | "libc", |
| 223 | "pthread_create failed: couldn't allocate %zu-bytes mapped space: %s", |
| 224 | mmap_size, strerror(errno)); |
| 225 | return {}; |
| 226 | } |
| 227 | const size_t writable_size = mmap_size - stack_guard_size - PTHREAD_GUARD_SIZE; |
| 228 | if (mprotect(space + stack_guard_size, |
| 229 | writable_size, |
| 230 | PROT_READ | PROT_WRITE) != 0) { |
| 231 | async_safe_format_log(ANDROID_LOG_WARN, "libc", |
| 232 | "pthread_create failed: couldn't mprotect R+W %zu-byte thread mapping region: %s", |
| 233 | writable_size, strerror(errno)); |
| 234 | munmap(space, mmap_size); |
| 235 | return {}; |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 238 | ThreadMapping result = {}; |
| 239 | result.mmap_base = space; |
| 240 | result.mmap_size = mmap_size; |
| 241 | result.static_tls = space + mmap_size - PTHREAD_GUARD_SIZE - layout.size(); |
| 242 | result.stack_base = space; |
| 243 | result.stack_top = result.static_tls; |
| 244 | return result; |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 247 | static int __allocate_thread(pthread_attr_t* attr, bionic_tcb** tcbp, void** child_stack) { |
| 248 | ThreadMapping mapping; |
| 249 | char* stack_top; |
| 250 | bool stack_clean = false; |
Yabin Cui | 6a7aaf4 | 2014-12-22 19:17:33 -0800 | [diff] [blame] | 251 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 252 | if (attr->stack_base == nullptr) { |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 253 | // The caller didn't provide a stack, so allocate one. |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 254 | |
| 255 | // Make sure the guard size is a multiple of PAGE_SIZE. |
| 256 | const size_t unaligned_guard_size = attr->guard_size; |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 257 | attr->guard_size = __BIONIC_ALIGN(attr->guard_size, PAGE_SIZE); |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 258 | if (attr->guard_size < unaligned_guard_size) return EAGAIN; |
| 259 | |
| 260 | mapping = __allocate_thread_mapping(attr->stack_size, attr->guard_size); |
| 261 | if (mapping.mmap_base == nullptr) return EAGAIN; |
| 262 | |
| 263 | stack_top = mapping.stack_top; |
| 264 | attr->stack_base = mapping.stack_base; |
| 265 | stack_clean = true; |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 266 | } else { |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 267 | mapping = __allocate_thread_mapping(0, PTHREAD_GUARD_SIZE); |
| 268 | if (mapping.mmap_base == nullptr) return EAGAIN; |
| 269 | |
| 270 | stack_top = static_cast<char*>(attr->stack_base) + attr->stack_size; |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 273 | // Carve out space from the stack for the thread's pthread_internal_t. This |
| 274 | // memory isn't counted in pthread_attr_getstacksize. |
Yabin Cui | a2db50d | 2015-03-20 10:58:04 -0700 | [diff] [blame] | 275 | |
| 276 | // To safely access the pthread_internal_t and thread stack, we need to find a 16-byte aligned boundary. |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 277 | stack_top = align_down(stack_top - sizeof(pthread_internal_t), 16); |
Yabin Cui | a2db50d | 2015-03-20 10:58:04 -0700 | [diff] [blame] | 278 | |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 279 | pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(stack_top); |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 280 | if (!stack_clean) { |
Yabin Cui | 304348a | 2015-12-03 13:01:42 -0800 | [diff] [blame] | 281 | // If thread was not allocated by mmap(), it may not have been cleared to zero. |
| 282 | // So assume the worst and zero it. |
| 283 | memset(thread, 0, sizeof(pthread_internal_t)); |
| 284 | } |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 285 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 286 | // Locate static TLS structures within the mapped region. |
| 287 | const StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout; |
| 288 | auto tcb = reinterpret_cast<bionic_tcb*>(mapping.static_tls + layout.offset_bionic_tcb()); |
| 289 | auto tls = reinterpret_cast<bionic_tls*>(mapping.static_tls + layout.offset_bionic_tls()); |
| 290 | |
| 291 | // (Re)initialize TLS pointers. |
| 292 | __init_tcb(tcb, thread); |
| 293 | __init_tcb_stack_guard(tcb); |
| 294 | __init_bionic_tls_ptrs(tcb, tls); |
| 295 | |
| 296 | attr->stack_size = stack_top - static_cast<char*>(attr->stack_base); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 297 | thread->attr = *attr; |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 298 | thread->mmap_base = mapping.mmap_base; |
| 299 | thread->mmap_size = mapping.mmap_size; |
Ryan Prichard | 9cfca86 | 2018-11-22 02:44:09 -0800 | [diff] [blame] | 300 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 301 | *tcbp = tcb; |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 302 | *child_stack = stack_top; |
| 303 | return 0; |
| 304 | } |
| 305 | |
Evgenii Stepanov | 13e8dcb | 2018-09-19 16:29:12 -0700 | [diff] [blame] | 306 | __attribute__((no_sanitize("hwaddress"))) |
Elliott Hughes | e48b685 | 2013-11-15 14:57:45 -0800 | [diff] [blame] | 307 | static int __pthread_start(void* arg) { |
| 308 | pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(arg); |
| 309 | |
Evgenii Stepanov | 13e8dcb | 2018-09-19 16:29:12 -0700 | [diff] [blame] | 310 | __hwasan_thread_enter(); |
| 311 | |
Elliott Hughes | e48b685 | 2013-11-15 14:57:45 -0800 | [diff] [blame] | 312 | // Wait for our creating thread to release us. This lets it have time to |
| 313 | // notify gdb about this thread before we start doing anything. |
| 314 | // This also provides the memory barrier needed to ensure that all memory |
| 315 | // accesses previously made by the creating thread are visible to us. |
Yabin Cui | d26e780 | 2015-10-22 20:07:56 -0700 | [diff] [blame] | 316 | thread->startup_handshake_lock.lock(); |
Elliott Hughes | e48b685 | 2013-11-15 14:57:45 -0800 | [diff] [blame] | 317 | |
Peter Collingbourne | da772e2 | 2018-09-06 22:20:44 -0700 | [diff] [blame] | 318 | __init_additional_stacks(thread); |
Elliott Hughes | e48b685 | 2013-11-15 14:57:45 -0800 | [diff] [blame] | 319 | |
Elliott Hughes | e48b685 | 2013-11-15 14:57:45 -0800 | [diff] [blame] | 320 | void* result = thread->start_routine(thread->start_routine_arg); |
| 321 | pthread_exit(result); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Elliott Hughes | cef3fae | 2013-11-19 16:52:24 -0800 | [diff] [blame] | 326 | // A dummy start routine for pthread_create failures where we've created a thread but aren't |
| 327 | // going to run user code on it. We swap out the user's start routine for this and take advantage |
| 328 | // of the regular thread teardown to free up resources. |
| 329 | static void* __do_nothing(void*) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 330 | return nullptr; |
Elliott Hughes | cef3fae | 2013-11-19 16:52:24 -0800 | [diff] [blame] | 331 | } |
| 332 | |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 333 | |
| 334 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 335 | int pthread_create(pthread_t* thread_out, pthread_attr_t const* attr, |
| 336 | void* (*start_routine)(void*), void* arg) { |
| 337 | ErrnoRestorer errno_restorer; |
| 338 | |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 339 | pthread_attr_t thread_attr; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 340 | if (attr == nullptr) { |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 341 | pthread_attr_init(&thread_attr); |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 342 | } else { |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 343 | thread_attr = *attr; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 344 | attr = nullptr; // Prevent misuse below. |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 347 | bionic_tcb* tcb = nullptr; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 348 | void* child_stack = nullptr; |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 349 | int result = __allocate_thread(&thread_attr, &tcb, &child_stack); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 350 | if (result != 0) { |
| 351 | return result; |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 354 | pthread_internal_t* thread = tcb->thread(); |
| 355 | |
Yabin Cui | d26e780 | 2015-10-22 20:07:56 -0700 | [diff] [blame] | 356 | // Create a lock for the thread to wait on once it starts so we can keep |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 357 | // it from doing anything until after we notify the debugger about it |
| 358 | // |
| 359 | // This also provides the memory barrier we need to ensure that all |
| 360 | // memory accesses previously performed by this thread are visible to |
| 361 | // the new thread. |
Yabin Cui | d26e780 | 2015-10-22 20:07:56 -0700 | [diff] [blame] | 362 | thread->startup_handshake_lock.init(false); |
| 363 | thread->startup_handshake_lock.lock(); |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 364 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 365 | thread->start_routine = start_routine; |
| 366 | thread->start_routine_arg = arg; |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 367 | |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 368 | thread->set_cached_pid(getpid()); |
| 369 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 370 | int flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM | |
| 371 | CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID; |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 372 | void* tls = &tcb->tls_slot(0); |
Elliott Hughes | 8090614 | 2013-11-26 13:57:21 -0800 | [diff] [blame] | 373 | #if defined(__i386__) |
| 374 | // On x86 (but not x86-64), CLONE_SETTLS takes a pointer to a struct user_desc rather than |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 375 | // a pointer to the TLS itself. |
| 376 | user_desc tls_descriptor; |
| 377 | __init_user_desc(&tls_descriptor, false, tls); |
| 378 | tls = &tls_descriptor; |
Elliott Hughes | 8090614 | 2013-11-26 13:57:21 -0800 | [diff] [blame] | 379 | #endif |
Elliott Hughes | 0d236aa | 2014-05-09 14:42:16 -0700 | [diff] [blame] | 380 | int rc = clone(__pthread_start, child_stack, flags, thread, &(thread->tid), tls, &(thread->tid)); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 381 | if (rc == -1) { |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 382 | int clone_errno = errno; |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 383 | // We don't have to unlock the mutex at all because clone(2) failed so there's no child waiting to |
| 384 | // be unblocked, but we're about to unmap the memory the mutex is stored in, so this serves as a |
| 385 | // reminder that you can't rewrite this function to use a ScopedPthreadMutexLocker. |
Yabin Cui | d26e780 | 2015-10-22 20:07:56 -0700 | [diff] [blame] | 386 | thread->startup_handshake_lock.unlock(); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 387 | if (thread->mmap_size != 0) { |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame^] | 388 | munmap(thread->mmap_base, thread->mmap_size); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 389 | } |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 390 | async_safe_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: clone failed: %s", |
dimitry | 6de6087 | 2017-08-14 14:42:19 +0200 | [diff] [blame] | 391 | strerror(clone_errno)); |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 392 | return clone_errno; |
| 393 | } |
| 394 | |
Yabin Cui | 673b15e | 2015-03-19 14:19:19 -0700 | [diff] [blame] | 395 | int init_errno = __init_thread(thread); |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 396 | if (init_errno != 0) { |
Elliott Hughes | cef3fae | 2013-11-19 16:52:24 -0800 | [diff] [blame] | 397 | // Mark the thread detached and replace its start_routine with a no-op. |
| 398 | // Letting the thread run is the easiest way to clean up its resources. |
Yabin Cui | 58cf31b | 2015-03-06 17:23:53 -0800 | [diff] [blame] | 399 | atomic_store(&thread->join_state, THREAD_DETACHED); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 400 | __pthread_internal_add(thread); |
Elliott Hughes | cef3fae | 2013-11-19 16:52:24 -0800 | [diff] [blame] | 401 | thread->start_routine = __do_nothing; |
Yabin Cui | d26e780 | 2015-10-22 20:07:56 -0700 | [diff] [blame] | 402 | thread->startup_handshake_lock.unlock(); |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 403 | return init_errno; |
| 404 | } |
| 405 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 406 | // Publish the pthread_t and unlock the mutex to let the new thread start running. |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 407 | *thread_out = __pthread_internal_add(thread); |
Yabin Cui | d26e780 | 2015-10-22 20:07:56 -0700 | [diff] [blame] | 408 | thread->startup_handshake_lock.unlock(); |
Elliott Hughes | 4b4a882 | 2013-02-12 17:15:59 -0800 | [diff] [blame] | 409 | |
| 410 | return 0; |
| 411 | } |