blob: f6c0401bb13090ff51d4e2e263b168271d18a40a [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
Elliott Hughes57b7a612014-08-25 17:26:50 -0700158static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_size) {
159 ErrnoRestorer errno_restorer;
160
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700161 rlimit stack_limit;
162 if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) {
163 return errno;
164 }
165
Elliott Hughes68ae6ad2020-07-21 16:11:30 -0700166 // 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 Hughes9e4ffa72014-08-27 15:32:01 -0700168 if (stack_limit.rlim_cur == RLIM_INFINITY) {
169 stack_limit.rlim_cur = 8 * 1024 * 1024;
170 }
Florian Mayere65e1932024-02-15 22:20:54 +0000171 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 Hughes57b7a612014-08-25 17:26:50 -0700176}
177
dimitryfa432522017-10-25 13:07:45 +0200178__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700179int pthread_attr_getstack(const pthread_attr_t* attr, void** stack_base, size_t* stack_size) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000180 *stack_base = attr->stack_base;
181 *stack_size = attr->stack_size;
182 return 0;
183}
184
dimitryfa432522017-10-25 13:07:45 +0200185__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +0000186int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000187 attr->guard_size = guard_size;
188 return 0;
189}
190
dimitryfa432522017-10-25 13:07:45 +0200191__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700192int pthread_attr_getguardsize(const pthread_attr_t* attr, size_t* guard_size) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000193 *guard_size = attr->guard_size;
194 return 0;
195}
196
dimitryfa432522017-10-25 13:07:45 +0200197__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes57b7a612014-08-25 17:26:50 -0700198int pthread_getattr_np(pthread_t t, pthread_attr_t* attr) {
Yabin Cui9d0c7932015-03-06 13:48:58 -0800199 pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(t);
200 *attr = thread->attr;
Yabin Cui58cf31b2015-03-06 17:23:53 -0800201 // 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 Cui9d0c7932015-03-06 13:48:58 -0800206 // 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 Hughes3e898472013-02-12 16:40:24 +0000211 return 0;
212}
213
dimitryfa432522017-10-25 13:07:45 +0200214__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700215int pthread_attr_setscope(pthread_attr_t*, int scope) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000216 if (scope == PTHREAD_SCOPE_SYSTEM) {
217 return 0;
218 }
219 if (scope == PTHREAD_SCOPE_PROCESS) {
220 return ENOTSUP;
221 }
222 return EINVAL;
223}
224
dimitryfa432522017-10-25 13:07:45 +0200225__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700226int pthread_attr_getscope(const pthread_attr_t*, int* scope) {
227 *scope = PTHREAD_SCOPE_SYSTEM;
228 return 0;
Elliott Hughes3e898472013-02-12 16:40:24 +0000229}