blob: cdae72cef61963b61242a3267f7a8c6b8cbb17b3 [file] [log] [blame]
Elliott Hughes3e898472013-02-12 16:40:24 +00001/*
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 Hughes57b7a612014-08-25 17:26:50 -070031#include <inttypes.h>
32#include <stdio.h>
Elliott Hughes92537572020-06-08 08:33:54 -070033#include <string.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070034#include <sys/resource.h>
Elliott Hughes9afb2f22014-10-09 14:01:47 -070035#include <unistd.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070036
Christopher Ferris7a3681e2017-04-24 17:48:32 -070037#include <async_safe/log.h>
38
Peter Collingbournebb11ee62022-05-02 12:26:16 -070039#include "platform/bionic/page.h"
Elliott Hughes57b7a612014-08-25 17:26:50 -070040#include "private/ErrnoRestorer.h"
Peter Collingbournebb11ee62022-05-02 12:26:16 -070041#include "private/bionic_defs.h"
Elliott Hughes3e898472013-02-12 16:40:24 +000042#include "pthread_internal.h"
43
dimitryfa432522017-10-25 13:07:45 +020044__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +000045int pthread_attr_init(pthread_attr_t* attr) {
Elliott Hughes6d339182013-02-12 16:36:04 -080046 attr->flags = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070047 attr->stack_base = nullptr;
Brian Carlstrom50af69e2013-09-13 16:34:43 -070048 attr->stack_size = PTHREAD_STACK_SIZE_DEFAULT;
Elliott Hughesd6c678c2017-06-27 17:01:57 -070049 attr->guard_size = PTHREAD_GUARD_SIZE;
Elliott Hughes6d339182013-02-12 16:36:04 -080050 attr->sched_policy = SCHED_NORMAL;
51 attr->sched_priority = 0;
Elliott Hughes3e898472013-02-12 16:40:24 +000052 return 0;
53}
54
dimitryfa432522017-10-25 13:07:45 +020055__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +000056int pthread_attr_destroy(pthread_attr_t* attr) {
57 memset(attr, 0x42, sizeof(pthread_attr_t));
58 return 0;
59}
60
dimitryfa432522017-10-25 13:07:45 +020061__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes8aecba72017-10-17 15:34:41 -070062int pthread_attr_setinheritsched(pthread_attr_t* attr, int flag) {
63 if (flag == PTHREAD_EXPLICIT_SCHED) {
64 attr->flags &= ~PTHREAD_ATTR_FLAG_INHERIT;
Elliott Hughes38f01e02017-10-27 15:28:54 -070065 attr->flags |= PTHREAD_ATTR_FLAG_EXPLICIT;
Elliott Hughes8aecba72017-10-17 15:34:41 -070066 } else if (flag == PTHREAD_INHERIT_SCHED) {
67 attr->flags |= PTHREAD_ATTR_FLAG_INHERIT;
Elliott Hughes38f01e02017-10-27 15:28:54 -070068 attr->flags &= ~PTHREAD_ATTR_FLAG_EXPLICIT;
Elliott Hughes8aecba72017-10-17 15:34:41 -070069 } else {
70 return EINVAL;
71 }
72 return 0;
73}
74
dimitryfa432522017-10-25 13:07:45 +020075__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes8aecba72017-10-17 15:34:41 -070076int pthread_attr_getinheritsched(const pthread_attr_t* attr, int* flag) {
Elliott Hughes38f01e02017-10-27 15:28:54 -070077 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 Hughes8aecba72017-10-17 15:34:41 -070085 return 0;
86}
87
dimitryfa432522017-10-25 13:07:45 +020088__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +000089int 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
dimitryfa432522017-10-25 13:07:45 +0200100__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700101int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* state) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000102 *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
103 return 0;
104}
105
dimitryfa432522017-10-25 13:07:45 +0200106__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +0000107int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy) {
108 attr->sched_policy = policy;
109 return 0;
110}
111
dimitryfa432522017-10-25 13:07:45 +0200112__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700113int pthread_attr_getschedpolicy(const pthread_attr_t* attr, int* policy) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000114 *policy = attr->sched_policy;
115 return 0;
116}
117
dimitryfa432522017-10-25 13:07:45 +0200118__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700119int pthread_attr_setschedparam(pthread_attr_t* attr, const sched_param* param) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000120 attr->sched_priority = param->sched_priority;
121 return 0;
122}
123
dimitryfa432522017-10-25 13:07:45 +0200124__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700125int pthread_attr_getschedparam(const pthread_attr_t* attr, sched_param* param) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000126 param->sched_priority = attr->sched_priority;
127 return 0;
128}
129
dimitryfa432522017-10-25 13:07:45 +0200130__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +0000131int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) {
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700132 if (stack_size < PTHREAD_STACK_MIN) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000133 return EINVAL;
134 }
135 attr->stack_size = stack_size;
136 return 0;
137}
138
dimitryfa432522017-10-25 13:07:45 +0200139__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700140int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* stack_size) {
Elliott Hughes57b7a612014-08-25 17:26:50 -0700141 void* unused;
142 return pthread_attr_getstack(attr, &unused, stack_size);
Elliott Hughes3e898472013-02-12 16:40:24 +0000143}
144
dimitryfa432522017-10-25 13:07:45 +0200145__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +0000146int pthread_attr_setstack(pthread_attr_t* attr, void* stack_base, size_t stack_size) {
Peter Collingbournebb11ee62022-05-02 12:26:16 -0700147 if ((stack_size & (page_size() - 1) || stack_size < PTHREAD_STACK_MIN)) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000148 return EINVAL;
149 }
Peter Collingbournebb11ee62022-05-02 12:26:16 -0700150 if (reinterpret_cast<uintptr_t>(stack_base) & (page_size() - 1)) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000151 return EINVAL;
152 }
153 attr->stack_base = stack_base;
154 attr->stack_size = stack_size;
155 return 0;
156}
157
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000158static uintptr_t __get_main_stack_startstack() {
159 FILE* fp = fopen("/proc/self/stat", "re");
160 if (fp == nullptr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700161 async_safe_fatal("couldn't open /proc/self/stat: %s", strerror(errno));
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000162 }
163
164 char line[BUFSIZ];
165 if (fgets(line, sizeof(line), fp) == nullptr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700166 async_safe_fatal("couldn't read /proc/self/stat: %s", strerror(errno));
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000167 }
168
169 fclose(fp);
170
171 // See man 5 proc. There's no reason comm can't contain ' ' or ')',
172 // so we search backwards for the end of it. We're looking for this field:
173 //
174 // startstack %lu (28) The address of the start (i.e., bottom) of the stack.
175 uintptr_t startstack = 0;
176 const char* end_of_comm = strrchr(line, ')');
177 if (sscanf(end_of_comm + 1, " %*c "
178 "%*d %*d %*d %*d %*d "
179 "%*u %*u %*u %*u %*u %*u %*u "
180 "%*d %*d %*d %*d %*d %*d "
181 "%*u %*u %*d %*u %*u %*u %" SCNuPTR, &startstack) != 1) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700182 async_safe_fatal("couldn't parse /proc/self/stat");
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000183 }
184
185 return startstack;
186}
187
Elliott Hughes57b7a612014-08-25 17:26:50 -0700188static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_size) {
189 ErrnoRestorer errno_restorer;
190
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700191 rlimit stack_limit;
192 if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) {
193 return errno;
194 }
195
Elliott Hughes68ae6ad2020-07-21 16:11:30 -0700196 // If the current RLIMIT_STACK is RLIM_INFINITY, only admit to an 8MiB stack
197 // in case callers such as ART take infinity too literally.
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700198 if (stack_limit.rlim_cur == RLIM_INFINITY) {
199 stack_limit.rlim_cur = 8 * 1024 * 1024;
200 }
201
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000202 // Ask the kernel where our main thread's stack started.
203 uintptr_t startstack = __get_main_stack_startstack();
204
205 // Hunt for the region that contains that address.
206 FILE* fp = fopen("/proc/self/maps", "re");
207 if (fp == nullptr) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700208 async_safe_fatal("couldn't open /proc/self/maps: %s", strerror(errno));
Elliott Hughes57b7a612014-08-25 17:26:50 -0700209 }
210 char line[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700211 while (fgets(line, sizeof(line), fp) != nullptr) {
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000212 uintptr_t lo, hi;
213 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR, &lo, &hi) == 2) {
214 if (lo <= startstack && startstack <= hi) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700215 *stack_size = stack_limit.rlim_cur;
216 *stack_base = reinterpret_cast<void*>(hi - *stack_size);
Elliott Hughes57b7a612014-08-25 17:26:50 -0700217 fclose(fp);
218 return 0;
219 }
220 }
221 }
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700222 async_safe_fatal("Stack not found in /proc/self/maps");
Elliott Hughes57b7a612014-08-25 17:26:50 -0700223}
224
dimitryfa432522017-10-25 13:07:45 +0200225__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700226int pthread_attr_getstack(const pthread_attr_t* attr, void** stack_base, size_t* stack_size) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000227 *stack_base = attr->stack_base;
228 *stack_size = attr->stack_size;
229 return 0;
230}
231
dimitryfa432522017-10-25 13:07:45 +0200232__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +0000233int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000234 attr->guard_size = guard_size;
235 return 0;
236}
237
dimitryfa432522017-10-25 13:07:45 +0200238__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700239int pthread_attr_getguardsize(const pthread_attr_t* attr, size_t* guard_size) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000240 *guard_size = attr->guard_size;
241 return 0;
242}
243
dimitryfa432522017-10-25 13:07:45 +0200244__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes57b7a612014-08-25 17:26:50 -0700245int pthread_getattr_np(pthread_t t, pthread_attr_t* attr) {
Yabin Cui9d0c7932015-03-06 13:48:58 -0800246 pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(t);
247 *attr = thread->attr;
Yabin Cui58cf31b2015-03-06 17:23:53 -0800248 // We prefer reading join_state here to setting thread->attr.flags in pthread_detach.
249 // Because data race exists in the latter case.
250 if (atomic_load(&thread->join_state) == THREAD_DETACHED) {
251 attr->flags |= PTHREAD_ATTR_FLAG_DETACHED;
252 }
Yabin Cui9d0c7932015-03-06 13:48:58 -0800253 // The main thread's stack information is not stored in thread->attr, and we need to
254 // collect that at runtime.
255 if (thread->tid == getpid()) {
256 return __pthread_attr_getstack_main_thread(&attr->stack_base, &attr->stack_size);
257 }
Elliott Hughes3e898472013-02-12 16:40:24 +0000258 return 0;
259}
260
dimitryfa432522017-10-25 13:07:45 +0200261__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700262int pthread_attr_setscope(pthread_attr_t*, int scope) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000263 if (scope == PTHREAD_SCOPE_SYSTEM) {
264 return 0;
265 }
266 if (scope == PTHREAD_SCOPE_PROCESS) {
267 return ENOTSUP;
268 }
269 return EINVAL;
270}
271
dimitryfa432522017-10-25 13:07:45 +0200272__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700273int pthread_attr_getscope(const pthread_attr_t*, int* scope) {
274 *scope = PTHREAD_SCOPE_SYSTEM;
275 return 0;
Elliott Hughes3e898472013-02-12 16:40:24 +0000276}