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
31 #include <inttypes.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/resource.h>
35 #include <unistd.h>
36
37 #include <async_safe/log.h>
38
39 #include "platform/bionic/page.h"
40 #include "private/ErrnoRestorer.h"
41 #include "private/bionic_defs.h"
42 #include "pthread_internal.h"
43
44 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_init(pthread_attr_t * attr)45 int pthread_attr_init(pthread_attr_t* attr) {
46 attr->flags = 0;
47 attr->stack_base = nullptr;
48 attr->stack_size = PTHREAD_STACK_SIZE_DEFAULT;
49 attr->guard_size = PTHREAD_GUARD_SIZE;
50 attr->sched_policy = SCHED_NORMAL;
51 attr->sched_priority = 0;
52 return 0;
53 }
54
55 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_destroy(pthread_attr_t * attr)56 int pthread_attr_destroy(pthread_attr_t* attr) {
57 memset(attr, 0x42, sizeof(pthread_attr_t));
58 return 0;
59 }
60
61 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_setinheritsched(pthread_attr_t * attr,int flag)62 int pthread_attr_setinheritsched(pthread_attr_t* attr, int flag) {
63 if (flag == PTHREAD_EXPLICIT_SCHED) {
64 attr->flags &= ~PTHREAD_ATTR_FLAG_INHERIT;
65 attr->flags |= PTHREAD_ATTR_FLAG_EXPLICIT;
66 } else if (flag == PTHREAD_INHERIT_SCHED) {
67 attr->flags |= PTHREAD_ATTR_FLAG_INHERIT;
68 attr->flags &= ~PTHREAD_ATTR_FLAG_EXPLICIT;
69 } else {
70 return EINVAL;
71 }
72 return 0;
73 }
74
75 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_getinheritsched(const pthread_attr_t * attr,int * flag)76 int pthread_attr_getinheritsched(const pthread_attr_t* attr, int* flag) {
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 }
85 return 0;
86 }
87
88 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_setdetachstate(pthread_attr_t * attr,int state)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
100 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_getdetachstate(const pthread_attr_t * attr,int * state)101 int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* state) {
102 *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
103 return 0;
104 }
105
106 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_setschedpolicy(pthread_attr_t * attr,int policy)107 int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy) {
108 attr->sched_policy = policy;
109 return 0;
110 }
111
112 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_getschedpolicy(const pthread_attr_t * attr,int * policy)113 int pthread_attr_getschedpolicy(const pthread_attr_t* attr, int* policy) {
114 *policy = attr->sched_policy;
115 return 0;
116 }
117
118 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_setschedparam(pthread_attr_t * attr,const sched_param * param)119 int pthread_attr_setschedparam(pthread_attr_t* attr, const sched_param* param) {
120 attr->sched_priority = param->sched_priority;
121 return 0;
122 }
123
124 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_getschedparam(const pthread_attr_t * attr,sched_param * param)125 int pthread_attr_getschedparam(const pthread_attr_t* attr, sched_param* param) {
126 param->sched_priority = attr->sched_priority;
127 return 0;
128 }
129
130 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_setstacksize(pthread_attr_t * attr,size_t stack_size)131 int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) {
132 if (stack_size < PTHREAD_STACK_MIN) {
133 return EINVAL;
134 }
135 attr->stack_size = stack_size;
136 return 0;
137 }
138
139 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_getstacksize(const pthread_attr_t * attr,size_t * stack_size)140 int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* stack_size) {
141 void* unused;
142 return pthread_attr_getstack(attr, &unused, stack_size);
143 }
144
145 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_setstack(pthread_attr_t * attr,void * stack_base,size_t stack_size)146 int pthread_attr_setstack(pthread_attr_t* attr, void* stack_base, size_t stack_size) {
147 if ((stack_size & (page_size() - 1) || stack_size < PTHREAD_STACK_MIN)) {
148 return EINVAL;
149 }
150 if (reinterpret_cast<uintptr_t>(stack_base) & (page_size() - 1)) {
151 return EINVAL;
152 }
153 attr->stack_base = stack_base;
154 attr->stack_size = stack_size;
155 return 0;
156 }
157
__pthread_attr_getstack_main_thread(void ** stack_base,size_t * stack_size)158 static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_size) {
159 ErrnoRestorer errno_restorer;
160
161 rlimit stack_limit;
162 if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) {
163 return errno;
164 }
165
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.
168 if (stack_limit.rlim_cur == RLIM_INFINITY) {
169 stack_limit.rlim_cur = 8 * 1024 * 1024;
170 }
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;
176 }
177
178 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_getstack(const pthread_attr_t * attr,void ** stack_base,size_t * stack_size)179 int pthread_attr_getstack(const pthread_attr_t* attr, void** stack_base, size_t* stack_size) {
180 *stack_base = attr->stack_base;
181 *stack_size = attr->stack_size;
182 return 0;
183 }
184
185 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_setguardsize(pthread_attr_t * attr,size_t guard_size)186 int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) {
187 attr->guard_size = guard_size;
188 return 0;
189 }
190
191 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_getguardsize(const pthread_attr_t * attr,size_t * guard_size)192 int pthread_attr_getguardsize(const pthread_attr_t* attr, size_t* guard_size) {
193 *guard_size = attr->guard_size;
194 return 0;
195 }
196
197 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_getattr_np(pthread_t t,pthread_attr_t * attr)198 int pthread_getattr_np(pthread_t t, pthread_attr_t* attr) {
199 pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(t);
200 *attr = thread->attr;
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 }
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 }
211 return 0;
212 }
213
214 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_setscope(pthread_attr_t *,int scope)215 int pthread_attr_setscope(pthread_attr_t*, int scope) {
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
225 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_attr_getscope(const pthread_attr_t *,int * scope)226 int pthread_attr_getscope(const pthread_attr_t*, int* scope) {
227 *scope = PTHREAD_SCOPE_SYSTEM;
228 return 0;
229 }
230