| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [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 |  | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 31 | #include <inttypes.h> | 
 | 32 | #include <stdio.h> | 
 | 33 | #include <sys/resource.h> | 
| Elliott Hughes | 9afb2f2 | 2014-10-09 14:01:47 -0700 | [diff] [blame] | 34 | #include <unistd.h> | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 35 |  | 
 | 36 | #include "private/bionic_string_utils.h" | 
 | 37 | #include "private/ErrnoRestorer.h" | 
 | 38 | #include "private/libc_logging.h" | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 39 | #include "pthread_internal.h" | 
 | 40 |  | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 41 | int pthread_attr_init(pthread_attr_t* attr) { | 
| Elliott Hughes | 6d33918 | 2013-02-12 16:36:04 -0800 | [diff] [blame] | 42 |   attr->flags = 0; | 
 | 43 |   attr->stack_base = NULL; | 
| Brian Carlstrom | 50af69e | 2013-09-13 16:34:43 -0700 | [diff] [blame] | 44 |   attr->stack_size = PTHREAD_STACK_SIZE_DEFAULT; | 
| Elliott Hughes | 6d33918 | 2013-02-12 16:36:04 -0800 | [diff] [blame] | 45 |   attr->guard_size = PAGE_SIZE; | 
 | 46 |   attr->sched_policy = SCHED_NORMAL; | 
 | 47 |   attr->sched_priority = 0; | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 48 |   return 0; | 
 | 49 | } | 
 | 50 |  | 
 | 51 | int pthread_attr_destroy(pthread_attr_t* attr) { | 
 | 52 |   memset(attr, 0x42, sizeof(pthread_attr_t)); | 
 | 53 |   return 0; | 
 | 54 | } | 
 | 55 |  | 
 | 56 | int pthread_attr_setdetachstate(pthread_attr_t* attr, int state) { | 
 | 57 |   if (state == PTHREAD_CREATE_DETACHED) { | 
 | 58 |     attr->flags |= PTHREAD_ATTR_FLAG_DETACHED; | 
 | 59 |   } else if (state == PTHREAD_CREATE_JOINABLE) { | 
 | 60 |     attr->flags &= ~PTHREAD_ATTR_FLAG_DETACHED; | 
 | 61 |   } else { | 
 | 62 |     return EINVAL; | 
 | 63 |   } | 
 | 64 |   return 0; | 
 | 65 | } | 
 | 66 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 67 | int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* state) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 68 |   *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE; | 
 | 69 |   return 0; | 
 | 70 | } | 
 | 71 |  | 
 | 72 | int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy) { | 
 | 73 |   attr->sched_policy = policy; | 
 | 74 |   return 0; | 
 | 75 | } | 
 | 76 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 77 | int pthread_attr_getschedpolicy(const pthread_attr_t* attr, int* policy) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 78 |   *policy = attr->sched_policy; | 
 | 79 |   return 0; | 
 | 80 | } | 
 | 81 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 82 | int pthread_attr_setschedparam(pthread_attr_t* attr, const sched_param* param) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 83 |   attr->sched_priority = param->sched_priority; | 
 | 84 |   return 0; | 
 | 85 | } | 
 | 86 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 87 | int pthread_attr_getschedparam(const pthread_attr_t* attr, sched_param* param) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 88 |   param->sched_priority = attr->sched_priority; | 
 | 89 |   return 0; | 
 | 90 | } | 
 | 91 |  | 
 | 92 | int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) { | 
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 93 |   if (stack_size < PTHREAD_STACK_MIN) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 94 |     return EINVAL; | 
 | 95 |   } | 
 | 96 |   attr->stack_size = stack_size; | 
 | 97 |   return 0; | 
 | 98 | } | 
 | 99 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 100 | int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* stack_size) { | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 101 |   void* unused; | 
 | 102 |   return pthread_attr_getstack(attr, &unused, stack_size); | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 103 | } | 
 | 104 |  | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 105 | int pthread_attr_setstack(pthread_attr_t* attr, void* stack_base, size_t stack_size) { | 
 | 106 |   if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) { | 
 | 107 |     return EINVAL; | 
 | 108 |   } | 
| Elliott Hughes | 405f855 | 2013-10-01 17:25:28 -0700 | [diff] [blame] | 109 |   if (reinterpret_cast<uintptr_t>(stack_base) & (PAGE_SIZE - 1)) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 110 |     return EINVAL; | 
 | 111 |   } | 
 | 112 |   attr->stack_base = stack_base; | 
 | 113 |   attr->stack_size = stack_size; | 
 | 114 |   return 0; | 
 | 115 | } | 
 | 116 |  | 
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 117 | static uintptr_t __get_main_stack_startstack() { | 
 | 118 |   FILE* fp = fopen("/proc/self/stat", "re"); | 
 | 119 |   if (fp == nullptr) { | 
 | 120 |     __libc_fatal("couldn't open /proc/self/stat: %s", strerror(errno)); | 
 | 121 |   } | 
 | 122 |  | 
 | 123 |   char line[BUFSIZ]; | 
 | 124 |   if (fgets(line, sizeof(line), fp) == nullptr) { | 
 | 125 |     __libc_fatal("couldn't read /proc/self/stat: %s", strerror(errno)); | 
 | 126 |   } | 
 | 127 |  | 
 | 128 |   fclose(fp); | 
 | 129 |  | 
 | 130 |   // See man 5 proc. There's no reason comm can't contain ' ' or ')', | 
 | 131 |   // so we search backwards for the end of it. We're looking for this field: | 
 | 132 |   // | 
 | 133 |   //  startstack %lu (28) The address of the start (i.e., bottom) of the stack. | 
 | 134 |   uintptr_t startstack = 0; | 
 | 135 |   const char* end_of_comm = strrchr(line, ')'); | 
 | 136 |   if (sscanf(end_of_comm + 1, " %*c " | 
 | 137 |              "%*d %*d %*d %*d %*d " | 
 | 138 |              "%*u %*u %*u %*u %*u %*u %*u " | 
 | 139 |              "%*d %*d %*d %*d %*d %*d " | 
 | 140 |              "%*u %*u %*d %*u %*u %*u %" SCNuPTR, &startstack) != 1) { | 
 | 141 |     __libc_fatal("couldn't parse /proc/self/stat"); | 
 | 142 |   } | 
 | 143 |  | 
 | 144 |   return startstack; | 
 | 145 | } | 
 | 146 |  | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 147 | static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_size) { | 
 | 148 |   ErrnoRestorer errno_restorer; | 
 | 149 |  | 
| Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 150 |   rlimit stack_limit; | 
 | 151 |   if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) { | 
 | 152 |     return errno; | 
 | 153 |   } | 
 | 154 |  | 
 | 155 |   // If the current RLIMIT_STACK is RLIM_INFINITY, only admit to an 8MiB stack for sanity's sake. | 
 | 156 |   if (stack_limit.rlim_cur == RLIM_INFINITY) { | 
 | 157 |     stack_limit.rlim_cur = 8 * 1024 * 1024; | 
 | 158 |   } | 
 | 159 |  | 
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 160 |   // Ask the kernel where our main thread's stack started. | 
 | 161 |   uintptr_t startstack = __get_main_stack_startstack(); | 
 | 162 |  | 
 | 163 |   // Hunt for the region that contains that address. | 
 | 164 |   FILE* fp = fopen("/proc/self/maps", "re"); | 
 | 165 |   if (fp == nullptr) { | 
 | 166 |     __libc_fatal("couldn't open /proc/self/maps"); | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 167 |   } | 
 | 168 |   char line[BUFSIZ]; | 
 | 169 |   while (fgets(line, sizeof(line), fp) != NULL) { | 
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 170 |     uintptr_t lo, hi; | 
 | 171 |     if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR, &lo, &hi) == 2) { | 
 | 172 |       if (lo <= startstack && startstack <= hi) { | 
| Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 173 |         *stack_size = stack_limit.rlim_cur; | 
 | 174 |         *stack_base = reinterpret_cast<void*>(hi - *stack_size); | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 175 |         fclose(fp); | 
 | 176 |         return 0; | 
 | 177 |       } | 
 | 178 |     } | 
 | 179 |   } | 
| Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 180 |   __libc_fatal("Stack not found in /proc/self/maps"); | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 181 | } | 
 | 182 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 183 | int pthread_attr_getstack(const pthread_attr_t* attr, void** stack_base, size_t* stack_size) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 184 |   *stack_base = attr->stack_base; | 
 | 185 |   *stack_size = attr->stack_size; | 
 | 186 |   return 0; | 
 | 187 | } | 
 | 188 |  | 
 | 189 | int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 190 |   attr->guard_size = guard_size; | 
 | 191 |   return 0; | 
 | 192 | } | 
 | 193 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 194 | int pthread_attr_getguardsize(const pthread_attr_t* attr, size_t* guard_size) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 195 |   *guard_size = attr->guard_size; | 
 | 196 |   return 0; | 
 | 197 | } | 
 | 198 |  | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 199 | int pthread_getattr_np(pthread_t t, pthread_attr_t* attr) { | 
| Yabin Cui | 9d0c793 | 2015-03-06 13:48:58 -0800 | [diff] [blame] | 200 |   pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(t); | 
 | 201 |   *attr = thread->attr; | 
| Yabin Cui | 58cf31b | 2015-03-06 17:23:53 -0800 | [diff] [blame] | 202 |   // We prefer reading join_state here to setting thread->attr.flags in pthread_detach. | 
 | 203 |   // Because data race exists in the latter case. | 
 | 204 |   if (atomic_load(&thread->join_state) == THREAD_DETACHED) { | 
 | 205 |     attr->flags |= PTHREAD_ATTR_FLAG_DETACHED; | 
 | 206 |   } | 
| Yabin Cui | 9d0c793 | 2015-03-06 13:48:58 -0800 | [diff] [blame] | 207 |   // The main thread's stack information is not stored in thread->attr, and we need to | 
 | 208 |   // collect that at runtime. | 
 | 209 |   if (thread->tid == getpid()) { | 
 | 210 |     return __pthread_attr_getstack_main_thread(&attr->stack_base, &attr->stack_size); | 
 | 211 |   } | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 212 |   return 0; | 
 | 213 | } | 
 | 214 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 215 | int pthread_attr_setscope(pthread_attr_t*, int scope) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 216 |   if (scope == PTHREAD_SCOPE_SYSTEM) { | 
 | 217 |     return 0; | 
 | 218 |   } | 
 | 219 |   if (scope == PTHREAD_SCOPE_PROCESS) { | 
 | 220 |     return ENOTSUP; | 
 | 221 |   } | 
 | 222 |   return EINVAL; | 
 | 223 | } | 
 | 224 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 225 | int pthread_attr_getscope(const pthread_attr_t*, int* scope) { | 
 | 226 |   *scope = PTHREAD_SCOPE_SYSTEM; | 
 | 227 |   return 0; | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 228 | } |