pthread_create.cpp revision ef115003012f61cf5539fdfeb201b98e4a92f610
14b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes/*
24b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * Copyright (C) 2008 The Android Open Source Project
34b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * All rights reserved.
44b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *
54b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * Redistribution and use in source and binary forms, with or without
64b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * modification, are permitted provided that the following conditions
74b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * are met:
84b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *  * Redistributions of source code must retain the above copyright
94b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *    notice, this list of conditions and the following disclaimer.
104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *  * Redistributions in binary form must reproduce the above copyright
114b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *    notice, this list of conditions and the following disclaimer in
124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *    the documentation and/or other materials provided with the
134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *    distribution.
144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *
154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
214b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
234b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * SUCH DAMAGE.
274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes */
284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
294b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <pthread.h>
304b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
314b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <errno.h>
3205fc1d7050d5451aea08dc5f504d2670287b2d43Elliott Hughes#include <string.h>
334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <sys/mman.h>
347086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes#include <unistd.h>
354b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "pthread_internal.h"
374b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
3803eebcb6e8762e668a0d3af6bb303cccb88c5b81Christopher Ferris#include "private/bionic_macros.h"
398cf1b305670123aed7638d984ca39bfd22388440Yabin Cui#include "private/bionic_prctl.h"
404b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/bionic_ssp.h"
414b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/bionic_tls.h"
428f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#include "private/libc_logging.h"
434b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/ErrnoRestorer.h"
444b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/ScopedPthreadMutexLocker.h"
454b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
460d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes// x86 uses segment descriptors rather than a direct pointer to TLS.
470d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes#if __i386__
480d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes#include <asm/ldt.h>
490d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughesextern "C" __LIBC_HIDDEN__ void __init_user_desc(struct user_desc*, int, void*);
500d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes#endif
510d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes
52f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughesextern "C" int __isthreaded;
53f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughes
5470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes// This code is used both by each new pthread and the code that initializes the main thread.
55af8aebebb52d73ea38c604525a6a5857618861cfElliott Hughesvoid __init_tls(pthread_internal_t* thread) {
56ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  if (thread->mmap_size == 0) {
57ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    // If the TLS area was not allocated by mmap(), it may not have been cleared to zero.
58ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    // So assume the worst and zero the TLS area.
595e2bd719d7dd19afe55f8d4f24366c0230e0e6c7Yabin Cui    memset(thread->tls, 0, sizeof(thread->tls));
605e2bd719d7dd19afe55f8d4f24366c0230e0e6c7Yabin Cui    memset(thread->key_data, 0, sizeof(thread->key_data));
614b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
624b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
634b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Slot 0 must point to itself. The x86 Linux kernel reads the TLS from %fs:0.
6440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_SELF] = thread->tls;
6540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_THREAD_ID] = thread;
664b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // GCC looks in the TLS for the stack guard on x86, so copy it there from our global.
678b5df3920f2843c9cdf04160517c1e8b77c992f5Elliott Hughes  thread->tls[TLS_SLOT_STACK_GUARD] = reinterpret_cast<void*>(__stack_chk_guard);
6870b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes}
694b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
7070b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughesvoid __init_alternate_signal_stack(pthread_internal_t* thread) {
7184114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // Create and set an alternate signal stack.
72ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui  void* stack_base = mmap(NULL, SIGNAL_STACK_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
73ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui  if (stack_base != MAP_FAILED) {
74ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui
75ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui    // Create a guard page to catch stack overflows in signal handlers.
76ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui    if (mprotect(stack_base, PAGE_SIZE, PROT_NONE) == -1) {
77ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui      munmap(stack_base, SIGNAL_STACK_SIZE);
78ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui      return;
79ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui    }
80ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui    stack_t ss;
81ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui    ss.ss_sp = reinterpret_cast<uint8_t*>(stack_base) + PAGE_SIZE;
82ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui    ss.ss_size = SIGNAL_STACK_SIZE - PAGE_SIZE;
8384114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    ss.ss_flags = 0;
8484114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    sigaltstack(&ss, NULL);
85ef115003012f61cf5539fdfeb201b98e4a92f610Yabin Cui    thread->alternate_signal_stack = stack_base;
868cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
878cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    // We can only use const static allocated string for mapped region name, as Android kernel
888cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    // uses the string pointer directly when dumping /proc/pid/maps.
89a3125fd1396a09a7fc4872dc4653f342150a3debElliott Hughes    prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ss.ss_sp, ss.ss_size, "thread signal stack");
9084114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  }
914b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
924b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
93673b15e4ee2c6d99b150aedddc0f389e29f98e1bYabin Cuiint __init_thread(pthread_internal_t* thread) {
944b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int error = 0;
954b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
9658cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui  if (__predict_true((thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) == 0)) {
9758cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui    atomic_init(&thread->join_state, THREAD_NOT_JOINED);
9858cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui  } else {
9958cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui    atomic_init(&thread->join_state, THREAD_DETACHED);
10058cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui  }
10158cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui
1024b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Set the scheduling policy/priority of the thread.
1034b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (thread->attr.sched_policy != SCHED_NORMAL) {
104c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    sched_param param;
1054b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    param.sched_priority = thread->attr.sched_priority;
10640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    if (sched_setscheduler(thread->tid, thread->attr.sched_policy, &param) == -1) {
10798624c374646a050556bdc402b55b792fefa7e55Elliott Hughes#if __LP64__
10898624c374646a050556bdc402b55b792fefa7e55Elliott Hughes      // For backwards compatibility reasons, we only report failures on 64-bit devices.
10998624c374646a050556bdc402b55b792fefa7e55Elliott Hughes      error = errno;
11098624c374646a050556bdc402b55b792fefa7e55Elliott Hughes#endif
111b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes      __libc_format_log(ANDROID_LOG_WARN, "libc",
112b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                        "pthread_create sched_setscheduler call failed: %s", strerror(errno));
1134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  thread->cleanup_stack = NULL;
1174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return error;
1194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
121ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cuistatic void* __create_thread_mapped_space(size_t mmap_size, size_t stack_guard_size) {
1224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Create a new private anonymous map.
1234b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int prot = PROT_READ | PROT_WRITE;
1244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
125ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  void* space = mmap(NULL, mmap_size, prot, flags, -1, 0);
126ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  if (space == MAP_FAILED) {
127b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    __libc_format_log(ANDROID_LOG_WARN,
128b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      "libc",
129ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui                      "pthread_create failed: couldn't allocate %zu-bytes mapped space: %s",
130ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui                      mmap_size, strerror(errno));
1314b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return NULL;
1324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
134ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  // Stack is at the lower end of mapped space, stack guard region is at the lower end of stack.
135ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  // Set the stack guard region to PROT_NONE, so we can detect thread stack overflow.
136ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  if (mprotect(space, stack_guard_size, PROT_NONE) == -1) {
137b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    __libc_format_log(ANDROID_LOG_WARN, "libc",
138ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui                      "pthread_create failed: couldn't mprotect PROT_NONE %zu-byte stack guard region: %s",
139ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui                      stack_guard_size, strerror(errno));
140ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    munmap(space, mmap_size);
1414b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return NULL;
1424b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1434b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
144ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  return space;
1454b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1464b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1478cf1b305670123aed7638d984ca39bfd22388440Yabin Cuistatic int __allocate_thread(pthread_attr_t* attr, pthread_internal_t** threadp, void** child_stack) {
148ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  size_t mmap_size;
1496a7aaf46759db32c6ed0eb953a4a230dc96af0d9Yabin Cui  uint8_t* stack_top;
1506a7aaf46759db32c6ed0eb953a4a230dc96af0d9Yabin Cui
1518cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  if (attr->stack_base == NULL) {
1528cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    // The caller didn't provide a stack, so allocate one.
1538cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    // Make sure the stack size and guard size are multiples of PAGE_SIZE.
154ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    mmap_size = BIONIC_ALIGN(attr->stack_size + sizeof(pthread_internal_t), PAGE_SIZE);
1558cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    attr->guard_size = BIONIC_ALIGN(attr->guard_size, PAGE_SIZE);
156ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    attr->stack_base = __create_thread_mapped_space(mmap_size, attr->guard_size);
1578cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    if (attr->stack_base == NULL) {
1588cf1b305670123aed7638d984ca39bfd22388440Yabin Cui      return EAGAIN;
1598cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    }
160ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    stack_top = reinterpret_cast<uint8_t*>(attr->stack_base) + mmap_size;
1618cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  } else {
162ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    // Remember the mmap size is zero and we don't need to free it.
163ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    mmap_size = 0;
1646a7aaf46759db32c6ed0eb953a4a230dc96af0d9Yabin Cui    stack_top = reinterpret_cast<uint8_t*>(attr->stack_base) + attr->stack_size;
1658cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  }
1668cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
167ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  // Mapped space(or user allocated stack) is used for:
168a2db50d5d7fa67b297eddd1c0549f08ea4b6a950Yabin Cui  //   pthread_internal_t
169ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  //   thread stack (including guard page)
170a2db50d5d7fa67b297eddd1c0549f08ea4b6a950Yabin Cui
171a2db50d5d7fa67b297eddd1c0549f08ea4b6a950Yabin Cui  // To safely access the pthread_internal_t and thread stack, we need to find a 16-byte aligned boundary.
172a2db50d5d7fa67b297eddd1c0549f08ea4b6a950Yabin Cui  stack_top = reinterpret_cast<uint8_t*>(
173a2db50d5d7fa67b297eddd1c0549f08ea4b6a950Yabin Cui                (reinterpret_cast<uintptr_t>(stack_top) - sizeof(pthread_internal_t)) & ~0xf);
174a2db50d5d7fa67b297eddd1c0549f08ea4b6a950Yabin Cui
1758cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(stack_top);
176917d390510e442b9b030d54992ebf41cc1e7f853Yabin Cui  attr->stack_size = stack_top - reinterpret_cast<uint8_t*>(attr->stack_base);
1778cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
178ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui  thread->mmap_size = mmap_size;
1798cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  thread->attr = *attr;
1808cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  __init_tls(thread);
1818cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
1828cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  *threadp = thread;
1838cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  *child_stack = stack_top;
1848cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  return 0;
1858cf1b305670123aed7638d984ca39bfd22388440Yabin Cui}
1868cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
187e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughesstatic int __pthread_start(void* arg) {
188e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(arg);
189e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
190e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // Wait for our creating thread to release us. This lets it have time to
191e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // notify gdb about this thread before we start doing anything.
192e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // This also provides the memory barrier needed to ensure that all memory
193e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // accesses previously made by the creating thread are visible to us.
194b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_lock(&thread->startup_handshake_mutex);
195b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_destroy(&thread->startup_handshake_mutex);
196e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
197e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  __init_alternate_signal_stack(thread);
198e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
199e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  void* result = thread->start_routine(thread->start_routine_arg);
200e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_exit(result);
201e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
202e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  return 0;
203e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes}
204e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
205cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// A dummy start routine for pthread_create failures where we've created a thread but aren't
206cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// going to run user code on it. We swap out the user's start routine for this and take advantage
207cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// of the regular thread teardown to free up resources.
208cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughesstatic void* __do_nothing(void*) {
209cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes  return NULL;
210cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes}
211cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes
2124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesint pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
2134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                   void* (*start_routine)(void*), void* arg) {
2144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  ErrnoRestorer errno_restorer;
2154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
216f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughes  // Inform the rest of the C library that at least one thread was created.
2174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  __isthreaded = 1;
2184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2198cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  pthread_attr_t thread_attr;
2204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (attr == NULL) {
2218cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    pthread_attr_init(&thread_attr);
2224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  } else {
2238cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    thread_attr = *attr;
2244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    attr = NULL; // Prevent misuse below.
2254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2264b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2278cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  pthread_internal_t* thread = NULL;
2288cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  void* child_stack = NULL;
2298cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  int result = __allocate_thread(&thread_attr, &thread, &child_stack);
2308cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  if (result != 0) {
2318cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    return result;
2324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
23470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // Create a mutex for the thread in TLS to wait on once it starts so we can keep
2354b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // it from doing anything until after we notify the debugger about it
2364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  //
2374b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // This also provides the memory barrier we need to ensure that all
2384b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // memory accesses previously performed by this thread are visible to
2394b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // the new thread.
240b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_init(&thread->startup_handshake_mutex, NULL);
241b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_lock(&thread->startup_handshake_mutex);
2424b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
24370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  thread->start_routine = start_routine;
24470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  thread->start_routine_arg = arg;
2454b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2467086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes  thread->set_cached_pid(getpid());
2477086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes
248877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  int flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM |
249877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes      CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID;
2508cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  void* tls = reinterpret_cast<void*>(thread->tls);
25180906141f79be8be63fc915bfab467029b442ca1Elliott Hughes#if defined(__i386__)
25280906141f79be8be63fc915bfab467029b442ca1Elliott Hughes  // On x86 (but not x86-64), CLONE_SETTLS takes a pointer to a struct user_desc rather than
2530d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  // a pointer to the TLS itself.
2540d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  user_desc tls_descriptor;
2550d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  __init_user_desc(&tls_descriptor, false, tls);
2560d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  tls = &tls_descriptor;
25780906141f79be8be63fc915bfab467029b442ca1Elliott Hughes#endif
2580d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  int rc = clone(__pthread_start, child_stack, flags, thread, &(thread->tid), tls, &(thread->tid));
259877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  if (rc == -1) {
2604b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    int clone_errno = errno;
261877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // We don't have to unlock the mutex at all because clone(2) failed so there's no child waiting to
262877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // be unblocked, but we're about to unmap the memory the mutex is stored in, so this serves as a
263877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // reminder that you can't rewrite this function to use a ScopedPthreadMutexLocker.
264b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes    pthread_mutex_unlock(&thread->startup_handshake_mutex);
265ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui    if (thread->mmap_size != 0) {
266ba8dfc2669d658dc340eb8f9c9b40ca074f05047Yabin Cui      munmap(thread->attr.stack_base, thread->mmap_size);
2674b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
268cfa089df23ff50fcd5ed3854c54991d30be5fc7eElliott Hughes    __libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: clone failed: %s", strerror(errno));
2694b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return clone_errno;
2704b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2714b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
272673b15e4ee2c6d99b150aedddc0f389e29f98e1bYabin Cui  int init_errno = __init_thread(thread);
2734b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (init_errno != 0) {
274cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    // Mark the thread detached and replace its start_routine with a no-op.
275cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    // Letting the thread run is the easiest way to clean up its resources.
27658cf31b50699ed9f523de38c8e943f3bbd1ced9eYabin Cui    atomic_store(&thread->join_state, THREAD_DETACHED);
277799cb35f45a161de96b272de38724f77e988f5f3Yabin Cui    __pthread_internal_add(thread);
278cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    thread->start_routine = __do_nothing;
279b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes    pthread_mutex_unlock(&thread->startup_handshake_mutex);
2804b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return init_errno;
2814b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2824b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
283877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  // Publish the pthread_t and unlock the mutex to let the new thread start running.
284673b15e4ee2c6d99b150aedddc0f389e29f98e1bYabin Cui  *thread_out = __pthread_internal_add(thread);
285b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_unlock(&thread->startup_handshake_mutex);
2864b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2874b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return 0;
2884b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
289