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