blob: 04d8112e9ef766e59913e8a3284bd0fe95977b82 [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>
33#include <sys/resource.h>
Elliott Hughes9afb2f22014-10-09 14:01:47 -070034#include <unistd.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070035
Christopher Ferris7a3681e2017-04-24 17:48:32 -070036#include <async_safe/log.h>
37
dimitryfa432522017-10-25 13:07:45 +020038#include "private/bionic_defs.h"
Elliott Hughes57b7a612014-08-25 17:26:50 -070039#include "private/bionic_string_utils.h"
40#include "private/ErrnoRestorer.h"
Elliott Hughes3e898472013-02-12 16:40:24 +000041#include "pthread_internal.h"
42
dimitryfa432522017-10-25 13:07:45 +020043__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +000044int pthread_attr_init(pthread_attr_t* attr) {
Elliott Hughes6d339182013-02-12 16:36:04 -080045 attr->flags = 0;
46 attr->stack_base = NULL;
Brian Carlstrom50af69e2013-09-13 16:34:43 -070047 attr->stack_size = PTHREAD_STACK_SIZE_DEFAULT;
Elliott Hughesd6c678c2017-06-27 17:01:57 -070048 attr->guard_size = PTHREAD_GUARD_SIZE;
Elliott Hughes6d339182013-02-12 16:36:04 -080049 attr->sched_policy = SCHED_NORMAL;
50 attr->sched_priority = 0;
Elliott Hughes3e898472013-02-12 16:40:24 +000051 return 0;
52}
53
dimitryfa432522017-10-25 13:07:45 +020054__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +000055int pthread_attr_destroy(pthread_attr_t* attr) {
56 memset(attr, 0x42, sizeof(pthread_attr_t));
57 return 0;
58}
59
dimitryfa432522017-10-25 13:07:45 +020060__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes8aecba72017-10-17 15:34:41 -070061int pthread_attr_setinheritsched(pthread_attr_t* attr, int flag) {
62 if (flag == PTHREAD_EXPLICIT_SCHED) {
63 attr->flags &= ~PTHREAD_ATTR_FLAG_INHERIT;
64 } else if (flag == PTHREAD_INHERIT_SCHED) {
65 attr->flags |= PTHREAD_ATTR_FLAG_INHERIT;
66 } else {
67 return EINVAL;
68 }
69 return 0;
70}
71
dimitryfa432522017-10-25 13:07:45 +020072__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes8aecba72017-10-17 15:34:41 -070073int pthread_attr_getinheritsched(const pthread_attr_t* attr, int* flag) {
74 *flag = (attr->flags & PTHREAD_ATTR_FLAG_INHERIT) ? PTHREAD_INHERIT_SCHED
75 : PTHREAD_EXPLICIT_SCHED;
76 return 0;
77}
78
dimitryfa432522017-10-25 13:07:45 +020079__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +000080int pthread_attr_setdetachstate(pthread_attr_t* attr, int state) {
81 if (state == PTHREAD_CREATE_DETACHED) {
82 attr->flags |= PTHREAD_ATTR_FLAG_DETACHED;
83 } else if (state == PTHREAD_CREATE_JOINABLE) {
84 attr->flags &= ~PTHREAD_ATTR_FLAG_DETACHED;
85 } else {
86 return EINVAL;
87 }
88 return 0;
89}
90
dimitryfa432522017-10-25 13:07:45 +020091__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -070092int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* state) {
Elliott Hughes3e898472013-02-12 16:40:24 +000093 *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
94 return 0;
95}
96
dimitryfa432522017-10-25 13:07:45 +020097__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +000098int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy) {
99 attr->sched_policy = policy;
100 return 0;
101}
102
dimitryfa432522017-10-25 13:07:45 +0200103__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700104int pthread_attr_getschedpolicy(const pthread_attr_t* attr, int* policy) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000105 *policy = attr->sched_policy;
106 return 0;
107}
108
dimitryfa432522017-10-25 13:07:45 +0200109__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700110int pthread_attr_setschedparam(pthread_attr_t* attr, const sched_param* param) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000111 attr->sched_priority = param->sched_priority;
112 return 0;
113}
114
dimitryfa432522017-10-25 13:07:45 +0200115__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700116int pthread_attr_getschedparam(const pthread_attr_t* attr, sched_param* param) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000117 param->sched_priority = attr->sched_priority;
118 return 0;
119}
120
dimitryfa432522017-10-25 13:07:45 +0200121__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +0000122int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) {
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700123 if (stack_size < PTHREAD_STACK_MIN) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000124 return EINVAL;
125 }
126 attr->stack_size = stack_size;
127 return 0;
128}
129
dimitryfa432522017-10-25 13:07:45 +0200130__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700131int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* stack_size) {
Elliott Hughes57b7a612014-08-25 17:26:50 -0700132 void* unused;
133 return pthread_attr_getstack(attr, &unused, stack_size);
Elliott Hughes3e898472013-02-12 16:40:24 +0000134}
135
dimitryfa432522017-10-25 13:07:45 +0200136__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +0000137int pthread_attr_setstack(pthread_attr_t* attr, void* stack_base, size_t stack_size) {
138 if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) {
139 return EINVAL;
140 }
Elliott Hughes405f8552013-10-01 17:25:28 -0700141 if (reinterpret_cast<uintptr_t>(stack_base) & (PAGE_SIZE - 1)) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000142 return EINVAL;
143 }
144 attr->stack_base = stack_base;
145 attr->stack_size = stack_size;
146 return 0;
147}
148
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000149static uintptr_t __get_main_stack_startstack() {
150 FILE* fp = fopen("/proc/self/stat", "re");
151 if (fp == nullptr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700152 async_safe_fatal("couldn't open /proc/self/stat: %s", strerror(errno));
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000153 }
154
155 char line[BUFSIZ];
156 if (fgets(line, sizeof(line), fp) == nullptr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700157 async_safe_fatal("couldn't read /proc/self/stat: %s", strerror(errno));
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000158 }
159
160 fclose(fp);
161
162 // See man 5 proc. There's no reason comm can't contain ' ' or ')',
163 // so we search backwards for the end of it. We're looking for this field:
164 //
165 // startstack %lu (28) The address of the start (i.e., bottom) of the stack.
166 uintptr_t startstack = 0;
167 const char* end_of_comm = strrchr(line, ')');
168 if (sscanf(end_of_comm + 1, " %*c "
169 "%*d %*d %*d %*d %*d "
170 "%*u %*u %*u %*u %*u %*u %*u "
171 "%*d %*d %*d %*d %*d %*d "
172 "%*u %*u %*d %*u %*u %*u %" SCNuPTR, &startstack) != 1) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700173 async_safe_fatal("couldn't parse /proc/self/stat");
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000174 }
175
176 return startstack;
177}
178
Elliott Hughes57b7a612014-08-25 17:26:50 -0700179static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_size) {
180 ErrnoRestorer errno_restorer;
181
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700182 rlimit stack_limit;
183 if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) {
184 return errno;
185 }
186
187 // If the current RLIMIT_STACK is RLIM_INFINITY, only admit to an 8MiB stack for sanity's sake.
188 if (stack_limit.rlim_cur == RLIM_INFINITY) {
189 stack_limit.rlim_cur = 8 * 1024 * 1024;
190 }
191
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000192 // Ask the kernel where our main thread's stack started.
193 uintptr_t startstack = __get_main_stack_startstack();
194
195 // Hunt for the region that contains that address.
196 FILE* fp = fopen("/proc/self/maps", "re");
197 if (fp == nullptr) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700198 async_safe_fatal("couldn't open /proc/self/maps: %s", strerror(errno));
Elliott Hughes57b7a612014-08-25 17:26:50 -0700199 }
200 char line[BUFSIZ];
201 while (fgets(line, sizeof(line), fp) != NULL) {
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +0000202 uintptr_t lo, hi;
203 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR, &lo, &hi) == 2) {
204 if (lo <= startstack && startstack <= hi) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -0700205 *stack_size = stack_limit.rlim_cur;
206 *stack_base = reinterpret_cast<void*>(hi - *stack_size);
Elliott Hughes57b7a612014-08-25 17:26:50 -0700207 fclose(fp);
208 return 0;
209 }
210 }
211 }
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700212 async_safe_fatal("Stack not found in /proc/self/maps");
Elliott Hughes57b7a612014-08-25 17:26:50 -0700213}
214
dimitryfa432522017-10-25 13:07:45 +0200215__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700216int pthread_attr_getstack(const pthread_attr_t* attr, void** stack_base, size_t* stack_size) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000217 *stack_base = attr->stack_base;
218 *stack_size = attr->stack_size;
219 return 0;
220}
221
dimitryfa432522017-10-25 13:07:45 +0200222__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes3e898472013-02-12 16:40:24 +0000223int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000224 attr->guard_size = guard_size;
225 return 0;
226}
227
dimitryfa432522017-10-25 13:07:45 +0200228__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700229int pthread_attr_getguardsize(const pthread_attr_t* attr, size_t* guard_size) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000230 *guard_size = attr->guard_size;
231 return 0;
232}
233
dimitryfa432522017-10-25 13:07:45 +0200234__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes57b7a612014-08-25 17:26:50 -0700235int pthread_getattr_np(pthread_t t, pthread_attr_t* attr) {
Yabin Cui9d0c7932015-03-06 13:48:58 -0800236 pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(t);
237 *attr = thread->attr;
Yabin Cui58cf31b2015-03-06 17:23:53 -0800238 // We prefer reading join_state here to setting thread->attr.flags in pthread_detach.
239 // Because data race exists in the latter case.
240 if (atomic_load(&thread->join_state) == THREAD_DETACHED) {
241 attr->flags |= PTHREAD_ATTR_FLAG_DETACHED;
242 }
Yabin Cui9d0c7932015-03-06 13:48:58 -0800243 // The main thread's stack information is not stored in thread->attr, and we need to
244 // collect that at runtime.
245 if (thread->tid == getpid()) {
246 return __pthread_attr_getstack_main_thread(&attr->stack_base, &attr->stack_size);
247 }
Elliott Hughes3e898472013-02-12 16:40:24 +0000248 return 0;
249}
250
dimitryfa432522017-10-25 13:07:45 +0200251__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700252int pthread_attr_setscope(pthread_attr_t*, int scope) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000253 if (scope == PTHREAD_SCOPE_SYSTEM) {
254 return 0;
255 }
256 if (scope == PTHREAD_SCOPE_PROCESS) {
257 return ENOTSUP;
258 }
259 return EINVAL;
260}
261
dimitryfa432522017-10-25 13:07:45 +0200262__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -0700263int pthread_attr_getscope(const pthread_attr_t*, int* scope) {
264 *scope = PTHREAD_SCOPE_SYSTEM;
265 return 0;
Elliott Hughes3e898472013-02-12 16:40:24 +0000266}