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 <sys/resource.h>
34 #include <unistd.h>
35 
36 #include "private/bionic_string_utils.h"
37 #include "private/ErrnoRestorer.h"
38 #include "private/libc_logging.h"
39 #include "pthread_internal.h"
40 
pthread_attr_init(pthread_attr_t * attr)41 int pthread_attr_init(pthread_attr_t* attr) {
42   attr->flags = 0;
43   attr->stack_base = NULL;
44   attr->stack_size = PTHREAD_STACK_SIZE_DEFAULT;
45   attr->guard_size = PAGE_SIZE;
46   attr->sched_policy = SCHED_NORMAL;
47   attr->sched_priority = 0;
48   return 0;
49 }
50 
pthread_attr_destroy(pthread_attr_t * attr)51 int pthread_attr_destroy(pthread_attr_t* attr) {
52   memset(attr, 0x42, sizeof(pthread_attr_t));
53   return 0;
54 }
55 
pthread_attr_setdetachstate(pthread_attr_t * attr,int state)56 int pthread_attr_setdetachstate(pthread_attr_t* attr, int state) {
57   if (state == PTHREAD_CREATE_DETACHED) {
58     attr->flags |= PTHREAD_ATTR_FLAG_DETACHED;
59   } else if (state == PTHREAD_CREATE_JOINABLE) {
60     attr->flags &= ~PTHREAD_ATTR_FLAG_DETACHED;
61   } else {
62     return EINVAL;
63   }
64   return 0;
65 }
66 
pthread_attr_getdetachstate(const pthread_attr_t * attr,int * state)67 int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* state) {
68   *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
69   return 0;
70 }
71 
pthread_attr_setschedpolicy(pthread_attr_t * attr,int policy)72 int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy) {
73   attr->sched_policy = policy;
74   return 0;
75 }
76 
pthread_attr_getschedpolicy(const pthread_attr_t * attr,int * policy)77 int pthread_attr_getschedpolicy(const pthread_attr_t* attr, int* policy) {
78   *policy = attr->sched_policy;
79   return 0;
80 }
81 
pthread_attr_setschedparam(pthread_attr_t * attr,const sched_param * param)82 int pthread_attr_setschedparam(pthread_attr_t* attr, const sched_param* param) {
83   attr->sched_priority = param->sched_priority;
84   return 0;
85 }
86 
pthread_attr_getschedparam(const pthread_attr_t * attr,sched_param * param)87 int pthread_attr_getschedparam(const pthread_attr_t* attr, sched_param* param) {
88   param->sched_priority = attr->sched_priority;
89   return 0;
90 }
91 
pthread_attr_setstacksize(pthread_attr_t * attr,size_t stack_size)92 int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) {
93   if (stack_size < PTHREAD_STACK_MIN) {
94     return EINVAL;
95   }
96   attr->stack_size = stack_size;
97   return 0;
98 }
99 
pthread_attr_getstacksize(const pthread_attr_t * attr,size_t * stack_size)100 int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* stack_size) {
101   void* unused;
102   return pthread_attr_getstack(attr, &unused, stack_size);
103 }
104 
pthread_attr_setstack(pthread_attr_t * attr,void * stack_base,size_t stack_size)105 int pthread_attr_setstack(pthread_attr_t* attr, void* stack_base, size_t stack_size) {
106   if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) {
107     return EINVAL;
108   }
109   if (reinterpret_cast<uintptr_t>(stack_base) & (PAGE_SIZE - 1)) {
110     return EINVAL;
111   }
112   attr->stack_base = stack_base;
113   attr->stack_size = stack_size;
114   return 0;
115 }
116 
__pthread_attr_getstack_main_thread(void ** stack_base,size_t * stack_size)117 static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_size) {
118   ErrnoRestorer errno_restorer;
119 
120   rlimit stack_limit;
121   if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) {
122     return errno;
123   }
124 
125   // If the current RLIMIT_STACK is RLIM_INFINITY, only admit to an 8MiB stack for sanity's sake.
126   if (stack_limit.rlim_cur == RLIM_INFINITY) {
127     stack_limit.rlim_cur = 8 * 1024 * 1024;
128   }
129 
130   // It shouldn't matter which thread we are because we're just looking for "[stack]", but
131   // valgrind seems to mess with the stack enough that the kernel will report "[stack:pid]"
132   // instead if you look in /proc/self/maps, so we need to look in /proc/pid/task/pid/maps.
133   char path[64];
134   snprintf(path, sizeof(path), "/proc/self/task/%d/maps", getpid());
135   FILE* fp = fopen(path, "re");
136   if (fp == NULL) {
137     return errno;
138   }
139   char line[BUFSIZ];
140   while (fgets(line, sizeof(line), fp) != NULL) {
141     if (ends_with(line, " [stack]\n")) {
142       uintptr_t lo, hi;
143       if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR, &lo, &hi) == 2) {
144         *stack_size = stack_limit.rlim_cur;
145         *stack_base = reinterpret_cast<void*>(hi - *stack_size);
146         fclose(fp);
147         return 0;
148       }
149     }
150   }
151   __libc_fatal("No [stack] line found in \"%s\"!", path);
152 }
153 
pthread_attr_getstack(const pthread_attr_t * attr,void ** stack_base,size_t * stack_size)154 int pthread_attr_getstack(const pthread_attr_t* attr, void** stack_base, size_t* stack_size) {
155   *stack_base = attr->stack_base;
156   *stack_size = attr->stack_size;
157   return 0;
158 }
159 
pthread_attr_setguardsize(pthread_attr_t * attr,size_t guard_size)160 int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) {
161   attr->guard_size = guard_size;
162   return 0;
163 }
164 
pthread_attr_getguardsize(const pthread_attr_t * attr,size_t * guard_size)165 int pthread_attr_getguardsize(const pthread_attr_t* attr, size_t* guard_size) {
166   *guard_size = attr->guard_size;
167   return 0;
168 }
169 
pthread_getattr_np(pthread_t t,pthread_attr_t * attr)170 int pthread_getattr_np(pthread_t t, pthread_attr_t* attr) {
171   pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(t);
172   *attr = thread->attr;
173   // We prefer reading join_state here to setting thread->attr.flags in pthread_detach.
174   // Because data race exists in the latter case.
175   if (atomic_load(&thread->join_state) == THREAD_DETACHED) {
176     attr->flags |= PTHREAD_ATTR_FLAG_DETACHED;
177   }
178   // The main thread's stack information is not stored in thread->attr, and we need to
179   // collect that at runtime.
180   if (thread->tid == getpid()) {
181     return __pthread_attr_getstack_main_thread(&attr->stack_base, &attr->stack_size);
182   }
183   return 0;
184 }
185 
pthread_attr_setscope(pthread_attr_t *,int scope)186 int pthread_attr_setscope(pthread_attr_t*, int scope) {
187   if (scope == PTHREAD_SCOPE_SYSTEM) {
188     return 0;
189   }
190   if (scope == PTHREAD_SCOPE_PROCESS) {
191     return ENOTSUP;
192   }
193   return EINVAL;
194 }
195 
pthread_attr_getscope(const pthread_attr_t *,int * scope)196 int pthread_attr_getscope(const pthread_attr_t*, int* scope) {
197   *scope = PTHREAD_SCOPE_SYSTEM;
198   return 0;
199 }
200