| 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> | 
| Elliott Hughes | 9253757 | 2020-06-08 08:33:54 -0700 | [diff] [blame] | 33 | #include <string.h> | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 34 | #include <sys/resource.h> | 
| Elliott Hughes | 9afb2f2 | 2014-10-09 14:01:47 -0700 | [diff] [blame] | 35 | #include <unistd.h> | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 36 |  | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 37 | #include <async_safe/log.h> | 
|  | 38 |  | 
| Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame] | 39 | #include "platform/bionic/page.h" | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 40 | #include "private/ErrnoRestorer.h" | 
| Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame] | 41 | #include "private/bionic_defs.h" | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 42 | #include "pthread_internal.h" | 
|  | 43 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 44 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 45 | int pthread_attr_init(pthread_attr_t* attr) { | 
| Elliott Hughes | 6d33918 | 2013-02-12 16:36:04 -0800 | [diff] [blame] | 46 | attr->flags = 0; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 47 | attr->stack_base = nullptr; | 
| Brian Carlstrom | 50af69e | 2013-09-13 16:34:43 -0700 | [diff] [blame] | 48 | attr->stack_size = PTHREAD_STACK_SIZE_DEFAULT; | 
| Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 49 | attr->guard_size = PTHREAD_GUARD_SIZE; | 
| Elliott Hughes | 6d33918 | 2013-02-12 16:36:04 -0800 | [diff] [blame] | 50 | attr->sched_policy = SCHED_NORMAL; | 
|  | 51 | attr->sched_priority = 0; | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 52 | return 0; | 
|  | 53 | } | 
|  | 54 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 55 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 56 | int pthread_attr_destroy(pthread_attr_t* attr) { | 
|  | 57 | memset(attr, 0x42, sizeof(pthread_attr_t)); | 
|  | 58 | return 0; | 
|  | 59 | } | 
|  | 60 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 61 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 62 | int pthread_attr_setinheritsched(pthread_attr_t* attr, int flag) { | 
|  | 63 | if (flag == PTHREAD_EXPLICIT_SCHED) { | 
|  | 64 | attr->flags &= ~PTHREAD_ATTR_FLAG_INHERIT; | 
| Elliott Hughes | 38f01e0 | 2017-10-27 15:28:54 -0700 | [diff] [blame] | 65 | attr->flags |= PTHREAD_ATTR_FLAG_EXPLICIT; | 
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 66 | } else if (flag == PTHREAD_INHERIT_SCHED) { | 
|  | 67 | attr->flags |= PTHREAD_ATTR_FLAG_INHERIT; | 
| Elliott Hughes | 38f01e0 | 2017-10-27 15:28:54 -0700 | [diff] [blame] | 68 | attr->flags &= ~PTHREAD_ATTR_FLAG_EXPLICIT; | 
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 69 | } else { | 
|  | 70 | return EINVAL; | 
|  | 71 | } | 
|  | 72 | return 0; | 
|  | 73 | } | 
|  | 74 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 75 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 76 | int pthread_attr_getinheritsched(const pthread_attr_t* attr, int* flag) { | 
| Elliott Hughes | 38f01e0 | 2017-10-27 15:28:54 -0700 | [diff] [blame] | 77 | if ((attr->flags & PTHREAD_ATTR_FLAG_INHERIT) != 0) { | 
|  | 78 | *flag = PTHREAD_INHERIT_SCHED; | 
|  | 79 | } else if ((attr->flags & PTHREAD_ATTR_FLAG_EXPLICIT) != 0) { | 
|  | 80 | *flag = PTHREAD_EXPLICIT_SCHED; | 
|  | 81 | } else { | 
|  | 82 | // Historical behavior before P, when pthread_attr_setinheritsched was added. | 
|  | 83 | *flag = (attr->sched_policy != SCHED_NORMAL) ? PTHREAD_EXPLICIT_SCHED : PTHREAD_INHERIT_SCHED; | 
|  | 84 | } | 
| Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 85 | return 0; | 
|  | 86 | } | 
|  | 87 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 88 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 89 | int pthread_attr_setdetachstate(pthread_attr_t* attr, int state) { | 
|  | 90 | if (state == PTHREAD_CREATE_DETACHED) { | 
|  | 91 | attr->flags |= PTHREAD_ATTR_FLAG_DETACHED; | 
|  | 92 | } else if (state == PTHREAD_CREATE_JOINABLE) { | 
|  | 93 | attr->flags &= ~PTHREAD_ATTR_FLAG_DETACHED; | 
|  | 94 | } else { | 
|  | 95 | return EINVAL; | 
|  | 96 | } | 
|  | 97 | return 0; | 
|  | 98 | } | 
|  | 99 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 100 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 101 | int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* state) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 102 | *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE; | 
|  | 103 | return 0; | 
|  | 104 | } | 
|  | 105 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 106 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 107 | int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy) { | 
|  | 108 | attr->sched_policy = policy; | 
|  | 109 | return 0; | 
|  | 110 | } | 
|  | 111 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 112 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 113 | int pthread_attr_getschedpolicy(const pthread_attr_t* attr, int* policy) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 114 | *policy = attr->sched_policy; | 
|  | 115 | return 0; | 
|  | 116 | } | 
|  | 117 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 118 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 119 | int pthread_attr_setschedparam(pthread_attr_t* attr, const sched_param* param) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 120 | attr->sched_priority = param->sched_priority; | 
|  | 121 | return 0; | 
|  | 122 | } | 
|  | 123 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 124 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 125 | int pthread_attr_getschedparam(const pthread_attr_t* attr, sched_param* param) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 126 | param->sched_priority = attr->sched_priority; | 
|  | 127 | return 0; | 
|  | 128 | } | 
|  | 129 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 130 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 131 | int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) { | 
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 132 | if (stack_size < PTHREAD_STACK_MIN) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 133 | return EINVAL; | 
|  | 134 | } | 
|  | 135 | attr->stack_size = stack_size; | 
|  | 136 | return 0; | 
|  | 137 | } | 
|  | 138 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 139 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 140 | 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] | 141 | void* unused; | 
|  | 142 | return pthread_attr_getstack(attr, &unused, stack_size); | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 145 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 146 | int pthread_attr_setstack(pthread_attr_t* attr, void* stack_base, size_t stack_size) { | 
| Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame] | 147 | if ((stack_size & (page_size() - 1) || stack_size < PTHREAD_STACK_MIN)) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 148 | return EINVAL; | 
|  | 149 | } | 
| Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame] | 150 | if (reinterpret_cast<uintptr_t>(stack_base) & (page_size() - 1)) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 151 | return EINVAL; | 
|  | 152 | } | 
|  | 153 | attr->stack_base = stack_base; | 
|  | 154 | attr->stack_size = stack_size; | 
|  | 155 | return 0; | 
|  | 156 | } | 
|  | 157 |  | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 158 | static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_size) { | 
|  | 159 | ErrnoRestorer errno_restorer; | 
|  | 160 |  | 
| Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 161 | rlimit stack_limit; | 
|  | 162 | if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) { | 
|  | 163 | return errno; | 
|  | 164 | } | 
|  | 165 |  | 
| Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 166 | // If the current RLIMIT_STACK is RLIM_INFINITY, only admit to an 8MiB stack | 
|  | 167 | // in case callers such as ART take infinity too literally. | 
| Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 168 | if (stack_limit.rlim_cur == RLIM_INFINITY) { | 
|  | 169 | stack_limit.rlim_cur = 8 * 1024 * 1024; | 
|  | 170 | } | 
| Florian Mayer | e65e193 | 2024-02-15 22:20:54 +0000 | [diff] [blame] | 171 | uintptr_t lo, hi; | 
|  | 172 | __find_main_stack_limits(&lo, &hi); | 
|  | 173 | *stack_size = stack_limit.rlim_cur; | 
|  | 174 | *stack_base = reinterpret_cast<void*>(hi - *stack_size); | 
|  | 175 | return 0; | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 178 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 179 | 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] | 180 | *stack_base = attr->stack_base; | 
|  | 181 | *stack_size = attr->stack_size; | 
|  | 182 | return 0; | 
|  | 183 | } | 
|  | 184 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 185 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 186 | int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 187 | attr->guard_size = guard_size; | 
|  | 188 | return 0; | 
|  | 189 | } | 
|  | 190 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 191 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 192 | 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] | 193 | *guard_size = attr->guard_size; | 
|  | 194 | return 0; | 
|  | 195 | } | 
|  | 196 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 197 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 198 | int pthread_getattr_np(pthread_t t, pthread_attr_t* attr) { | 
| Yabin Cui | 9d0c793 | 2015-03-06 13:48:58 -0800 | [diff] [blame] | 199 | pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(t); | 
|  | 200 | *attr = thread->attr; | 
| Yabin Cui | 58cf31b | 2015-03-06 17:23:53 -0800 | [diff] [blame] | 201 | // We prefer reading join_state here to setting thread->attr.flags in pthread_detach. | 
|  | 202 | // Because data race exists in the latter case. | 
|  | 203 | if (atomic_load(&thread->join_state) == THREAD_DETACHED) { | 
|  | 204 | attr->flags |= PTHREAD_ATTR_FLAG_DETACHED; | 
|  | 205 | } | 
| Yabin Cui | 9d0c793 | 2015-03-06 13:48:58 -0800 | [diff] [blame] | 206 | // The main thread's stack information is not stored in thread->attr, and we need to | 
|  | 207 | // collect that at runtime. | 
|  | 208 | if (thread->tid == getpid()) { | 
|  | 209 | return __pthread_attr_getstack_main_thread(&attr->stack_base, &attr->stack_size); | 
|  | 210 | } | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 211 | return 0; | 
|  | 212 | } | 
|  | 213 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 214 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| 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 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 225 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| 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 | } |