pthread_create.cpp revision 8cf1b305670123aed7638d984ca39bfd22388440
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>
324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <sys/mman.h>
337086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes#include <unistd.h>
344b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
354b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "pthread_internal.h"
364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
3703eebcb6e8762e668a0d3af6bb303cccb88c5b81Christopher Ferris#include "private/bionic_macros.h"
388cf1b305670123aed7638d984ca39bfd22388440Yabin Cui#include "private/bionic_prctl.h"
394b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/bionic_ssp.h"
404b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/bionic_tls.h"
418f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#include "private/libc_logging.h"
424b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/ErrnoRestorer.h"
434b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/ScopedPthreadMutexLocker.h"
444b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
450d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes// x86 uses segment descriptors rather than a direct pointer to TLS.
460d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes#if __i386__
470d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes#include <asm/ldt.h>
480d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughesextern "C" __LIBC_HIDDEN__ void __init_user_desc(struct user_desc*, int, void*);
490d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes#endif
500d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes
51f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughesextern "C" int __isthreaded;
52f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughes
5370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes// This code is used both by each new pthread and the code that initializes the main thread.
54af8aebebb52d73ea38c604525a6a5857618861cfElliott Hughesvoid __init_tls(pthread_internal_t* thread) {
5540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  if (thread->user_allocated_stack()) {
5640a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes    // We don't know where the user got their stack, so assume the worst and zero the TLS area.
5740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes    memset(&thread->tls[0], 0, BIONIC_TLS_SLOTS * sizeof(void*));
584b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
594b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
604b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Slot 0 must point to itself. The x86 Linux kernel reads the TLS from %fs:0.
6140eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_SELF] = thread->tls;
6240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_THREAD_ID] = thread;
634b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // GCC looks in the TLS for the stack guard on x86, so copy it there from our global.
6440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_STACK_GUARD] = (void*) __stack_chk_guard;
6570b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes}
664b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
6770b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughesvoid __init_alternate_signal_stack(pthread_internal_t* thread) {
6884114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // Create and set an alternate signal stack.
6984114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  stack_t ss;
7040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ss.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
7184114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  if (ss.ss_sp != MAP_FAILED) {
7284114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    ss.ss_size = SIGSTKSZ;
7384114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    ss.ss_flags = 0;
7484114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    sigaltstack(&ss, NULL);
7584114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    thread->alternate_signal_stack = ss.ss_sp;
768cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
778cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    // We can only use const static allocated string for mapped region name, as Android kernel
788cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    // uses the string pointer directly when dumping /proc/pid/maps.
798cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ss.ss_sp, ss.ss_size, "thread signal stack");
8084114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  }
814b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
824b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
83cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughesint __init_thread(pthread_internal_t* thread, bool add_to_thread_list) {
844b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int error = 0;
854b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
864b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Set the scheduling policy/priority of the thread.
874b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (thread->attr.sched_policy != SCHED_NORMAL) {
88c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    sched_param param;
894b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    param.sched_priority = thread->attr.sched_priority;
9040eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    if (sched_setscheduler(thread->tid, thread->attr.sched_policy, &param) == -1) {
9198624c374646a050556bdc402b55b792fefa7e55Elliott Hughes#if __LP64__
9298624c374646a050556bdc402b55b792fefa7e55Elliott Hughes      // For backwards compatibility reasons, we only report failures on 64-bit devices.
9398624c374646a050556bdc402b55b792fefa7e55Elliott Hughes      error = errno;
9498624c374646a050556bdc402b55b792fefa7e55Elliott Hughes#endif
95b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes      __libc_format_log(ANDROID_LOG_WARN, "libc",
96b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                        "pthread_create sched_setscheduler call failed: %s", strerror(errno));
974b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
984b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
994b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1004b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  thread->cleanup_stack = NULL;
1014b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1024b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (add_to_thread_list) {
1034b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    _pthread_internal_add(thread);
1044b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1054b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1064b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return error;
1074b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1084b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1098cf1b305670123aed7638d984ca39bfd22388440Yabin Cuistatic void* __create_thread_stack(const pthread_attr_t& attr) {
1104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Create a new private anonymous map.
1114b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int prot = PROT_READ | PROT_WRITE;
1124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
1138cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  void* stack = mmap(NULL, attr.stack_size, prot, flags, -1, 0);
1144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (stack == MAP_FAILED) {
115b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    __libc_format_log(ANDROID_LOG_WARN,
116b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      "libc",
117b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      "pthread_create failed: couldn't allocate %zd-byte stack: %s",
1188cf1b305670123aed7638d984ca39bfd22388440Yabin Cui                      attr.stack_size, strerror(errno));
1194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return NULL;
1204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1214b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Set the guard region at the end of the stack to PROT_NONE.
1238cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  if (mprotect(stack, attr.guard_size, PROT_NONE) == -1) {
124b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    __libc_format_log(ANDROID_LOG_WARN, "libc",
125b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      "pthread_create failed: couldn't mprotect PROT_NONE %zd-byte stack guard region: %s",
1268cf1b305670123aed7638d984ca39bfd22388440Yabin Cui                      attr.guard_size, strerror(errno));
1278cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    munmap(stack, attr.stack_size);
1284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return NULL;
1294b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1304b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1314b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return stack;
1324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1348cf1b305670123aed7638d984ca39bfd22388440Yabin Cuistatic int __allocate_thread(pthread_attr_t* attr, pthread_internal_t** threadp, void** child_stack) {
1358cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  if (attr->stack_base == NULL) {
1368cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    // The caller didn't provide a stack, so allocate one.
1378cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    // Make sure the stack size and guard size are multiples of PAGE_SIZE.
1388cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    attr->stack_size = BIONIC_ALIGN(attr->stack_size, PAGE_SIZE);
1398cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    attr->guard_size = BIONIC_ALIGN(attr->guard_size, PAGE_SIZE);
1408cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    attr->stack_base = __create_thread_stack(*attr);
1418cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    if (attr->stack_base == NULL) {
1428cf1b305670123aed7638d984ca39bfd22388440Yabin Cui      return EAGAIN;
1438cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    }
1448cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  } else {
1458cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    // The caller did provide a stack, so remember we're not supposed to free it.
1468cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    attr->flags |= PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK;
1478cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  }
1488cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
1498cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  // Thread stack is used for two sections:
1508cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  //   pthread_internal_t.
1518cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  //   regular stack, from top to down.
1528cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  uint8_t* stack_top = reinterpret_cast<uint8_t*>(attr->stack_base) + attr->stack_size;
1538cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  stack_top -= sizeof(pthread_internal_t);
1548cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(stack_top);
1558cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
1568cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  // No need to check stack_top alignment. The size of pthread_internal_t is 16-bytes aligned,
1578cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  // and user allocated stack is guaranteed by pthread_attr_setstack.
1588cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
1598cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  thread->attr = *attr;
1608cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  __init_tls(thread);
1618cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
1628cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  *threadp = thread;
1638cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  *child_stack = stack_top;
1648cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  return 0;
1658cf1b305670123aed7638d984ca39bfd22388440Yabin Cui}
1668cf1b305670123aed7638d984ca39bfd22388440Yabin Cui
167e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughesstatic int __pthread_start(void* arg) {
168e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(arg);
169e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
170e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // Wait for our creating thread to release us. This lets it have time to
171e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // notify gdb about this thread before we start doing anything.
172e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // This also provides the memory barrier needed to ensure that all memory
173e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // accesses previously made by the creating thread are visible to us.
174b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_lock(&thread->startup_handshake_mutex);
175b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_destroy(&thread->startup_handshake_mutex);
176e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
177e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  __init_alternate_signal_stack(thread);
178e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
179e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  void* result = thread->start_routine(thread->start_routine_arg);
180e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_exit(result);
181e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
182e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  return 0;
183e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes}
184e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
185cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// A dummy start routine for pthread_create failures where we've created a thread but aren't
186cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// going to run user code on it. We swap out the user's start routine for this and take advantage
187cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// of the regular thread teardown to free up resources.
188cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughesstatic void* __do_nothing(void*) {
189cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes  return NULL;
190cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes}
191cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes
1924b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesint pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
1934b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                   void* (*start_routine)(void*), void* arg) {
1944b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  ErrnoRestorer errno_restorer;
1954b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
196f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughes  // Inform the rest of the C library that at least one thread was created.
1974b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  __isthreaded = 1;
1984b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1998cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  pthread_attr_t thread_attr;
2004b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (attr == NULL) {
2018cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    pthread_attr_init(&thread_attr);
2024b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  } else {
2038cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    thread_attr = *attr;
2044b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    attr = NULL; // Prevent misuse below.
2054b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2064b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2078cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  pthread_internal_t* thread = NULL;
2088cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  void* child_stack = NULL;
2098cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  int result = __allocate_thread(&thread_attr, &thread, &child_stack);
2108cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  if (result != 0) {
2118cf1b305670123aed7638d984ca39bfd22388440Yabin Cui    return result;
2124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
21470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // Create a mutex for the thread in TLS to wait on once it starts so we can keep
2154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // it from doing anything until after we notify the debugger about it
2164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  //
2174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // This also provides the memory barrier we need to ensure that all
2184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // memory accesses previously performed by this thread are visible to
2194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // the new thread.
220b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_init(&thread->startup_handshake_mutex, NULL);
221b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_lock(&thread->startup_handshake_mutex);
2224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
22370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  thread->start_routine = start_routine;
22470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  thread->start_routine_arg = arg;
2254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2267086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes  thread->set_cached_pid(getpid());
2277086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes
228877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  int flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM |
229877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes      CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID;
2308cf1b305670123aed7638d984ca39bfd22388440Yabin Cui  void* tls = reinterpret_cast<void*>(thread->tls);
23180906141f79be8be63fc915bfab467029b442ca1Elliott Hughes#if defined(__i386__)
23280906141f79be8be63fc915bfab467029b442ca1Elliott Hughes  // On x86 (but not x86-64), CLONE_SETTLS takes a pointer to a struct user_desc rather than
2330d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  // a pointer to the TLS itself.
2340d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  user_desc tls_descriptor;
2350d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  __init_user_desc(&tls_descriptor, false, tls);
2360d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  tls = &tls_descriptor;
23780906141f79be8be63fc915bfab467029b442ca1Elliott Hughes#endif
2380d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  int rc = clone(__pthread_start, child_stack, flags, thread, &(thread->tid), tls, &(thread->tid));
239877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  if (rc == -1) {
2404b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    int clone_errno = errno;
241877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // We don't have to unlock the mutex at all because clone(2) failed so there's no child waiting to
242877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // be unblocked, but we're about to unmap the memory the mutex is stored in, so this serves as a
243877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // reminder that you can't rewrite this function to use a ScopedPthreadMutexLocker.
244b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes    pthread_mutex_unlock(&thread->startup_handshake_mutex);
24540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes    if (!thread->user_allocated_stack()) {
246b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes      munmap(thread->attr.stack_base, thread->attr.stack_size);
2474b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
248cfa089df23ff50fcd5ed3854c54991d30be5fc7eElliott Hughes    __libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: clone failed: %s", strerror(errno));
2494b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return clone_errno;
2504b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2514b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
252cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes  int init_errno = __init_thread(thread, true);
2534b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (init_errno != 0) {
254cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    // Mark the thread detached and replace its start_routine with a no-op.
255cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    // Letting the thread run is the easiest way to clean up its resources.
2564b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED;
257cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    thread->start_routine = __do_nothing;
258b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes    pthread_mutex_unlock(&thread->startup_handshake_mutex);
2594b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return init_errno;
2604b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2614b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
262877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  // Publish the pthread_t and unlock the mutex to let the new thread start running.
263877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  *thread_out = reinterpret_cast<pthread_t>(thread);
264b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_unlock(&thread->startup_handshake_mutex);
2654b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2664b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return 0;
2674b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
268