]>
Commit | Line | Data |
---|---|---|
d614a753 | 1 | /* Copyright (C) 2002-2020 Free Software Foundation, Inc. |
76a50749 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
76a50749 UD |
18 | |
19 | #include <assert.h> | |
cbd8aeb8 | 20 | #include <errno.h> |
76a50749 UD |
21 | #include <limits.h> |
22 | #include <signal.h> | |
23 | #include <stdlib.h> | |
24 | #include <unistd.h> | |
25 | #include <sys/param.h> | |
26 | #include <sys/resource.h> | |
27 | #include <pthreadP.h> | |
28 | #include <atomic.h> | |
29 | #include <ldsodefs.h> | |
30 | #include <tls.h> | |
7a775e6b | 31 | #include <list.h> |
76a50749 UD |
32 | #include <fork.h> |
33 | #include <version.h> | |
bf293afe | 34 | #include <shlib-compat.h> |
2edb61e3 | 35 | #include <lowlevellock.h> |
a2f0363f | 36 | #include <futex-internal.h> |
f8de5057 | 37 | #include <kernel-features.h> |
9090848d | 38 | #include <libc-pointer-arith.h> |
7cea6212 | 39 | #include <pthread-pids.h> |
6310e6be | 40 | #include <pthread_mutex_conf.h> |
76a50749 | 41 | |
c6aab2cb RM |
42 | #ifndef TLS_MULTIPLE_THREADS_IN_TCB |
43 | /* Pointer to the corresponding variable in libc. */ | |
44 | int *__libc_multiple_threads_ptr attribute_hidden; | |
45 | #endif | |
46 | ||
76a50749 UD |
47 | /* Size and alignment of static TLS block. */ |
48 | size_t __static_tls_size; | |
923e02ea | 49 | size_t __static_tls_align_m1; |
76a50749 | 50 | |
0f6699ea UD |
51 | #ifndef __ASSUME_SET_ROBUST_LIST |
52 | /* Negative if we do not have the system call and we can use it. */ | |
53 | int __set_robust_list_avail; | |
54 | # define set_robust_list_not_avail() \ | |
55 | __set_robust_list_avail = -1 | |
56 | #else | |
57 | # define set_robust_list_not_avail() do { } while (0) | |
58 | #endif | |
59 | ||
76a50749 | 60 | /* Version of the library, used in libthread_db to detect mismatches. */ |
e3b22ad3 | 61 | static const char nptl_version[] __attribute_used__ = VERSION; |
76a50749 UD |
62 | |
63 | ||
6f8326ca UD |
64 | #ifdef SHARED |
65 | static | |
66 | #else | |
67 | extern | |
68 | #endif | |
69 | void __nptl_set_robust (struct pthread *); | |
70 | ||
30991b8b | 71 | #ifdef SHARED |
630d93a7 | 72 | static const struct pthread_functions pthread_functions = |
8454830b | 73 | { |
bf293afe | 74 | .ptr___pthread_cond_broadcast = __pthread_cond_broadcast, |
bf293afe UD |
75 | .ptr___pthread_cond_signal = __pthread_cond_signal, |
76 | .ptr___pthread_cond_wait = __pthread_cond_wait, | |
c503d3dc | 77 | .ptr___pthread_cond_timedwait = __pthread_cond_timedwait, |
73e9ae88 | 78 | # if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2) |
bf293afe | 79 | .ptr___pthread_cond_broadcast_2_0 = __pthread_cond_broadcast_2_0, |
bf293afe UD |
80 | .ptr___pthread_cond_signal_2_0 = __pthread_cond_signal_2_0, |
81 | .ptr___pthread_cond_wait_2_0 = __pthread_cond_wait_2_0, | |
c503d3dc | 82 | .ptr___pthread_cond_timedwait_2_0 = __pthread_cond_timedwait_2_0, |
73e9ae88 | 83 | # endif |
49e9f864 | 84 | .ptr___pthread_exit = __pthread_exit, |
4d17e683 AS |
85 | .ptr_pthread_mutex_destroy = __pthread_mutex_destroy, |
86 | .ptr_pthread_mutex_init = __pthread_mutex_init, | |
87 | .ptr_pthread_mutex_lock = __pthread_mutex_lock, | |
88 | .ptr_pthread_mutex_unlock = __pthread_mutex_unlock, | |
e5d19c08 | 89 | .ptr___pthread_setcancelstate = __pthread_setcancelstate, |
73e9ae88 UD |
90 | .ptr_pthread_setcanceltype = __pthread_setcanceltype, |
91 | .ptr___pthread_cleanup_upto = __pthread_cleanup_upto, | |
4d17e683 AS |
92 | .ptr___pthread_once = __pthread_once, |
93 | .ptr___pthread_rwlock_rdlock = __pthread_rwlock_rdlock, | |
94 | .ptr___pthread_rwlock_wrlock = __pthread_rwlock_wrlock, | |
95 | .ptr___pthread_rwlock_unlock = __pthread_rwlock_unlock, | |
96 | .ptr___pthread_key_create = __pthread_key_create, | |
97 | .ptr___pthread_getspecific = __pthread_getspecific, | |
98 | .ptr___pthread_setspecific = __pthread_setspecific, | |
73e9ae88 | 99 | .ptr__pthread_cleanup_push_defer = __pthread_cleanup_push_defer, |
47202270 | 100 | .ptr__pthread_cleanup_pop_restore = __pthread_cleanup_pop_restore, |
6efd4814 | 101 | .ptr_nthreads = &__nptl_nthreads, |
3fa21fd8 | 102 | .ptr___pthread_unwind = &__pthread_unwind, |
2edb61e3 | 103 | .ptr__nptl_deallocate_tsd = __nptl_deallocate_tsd, |
ba408f84 | 104 | .ptr__nptl_setxid = __nptl_setxid, |
6f8326ca | 105 | .ptr_set_robust = __nptl_set_robust |
8454830b UD |
106 | }; |
107 | # define ptr_pthread_functions &pthread_functions | |
108 | #else | |
109 | # define ptr_pthread_functions NULL | |
110 | #endif | |
111 | ||
112 | ||
30991b8b | 113 | #ifdef SHARED |
6f8326ca | 114 | static |
30991b8b | 115 | #endif |
6f8326ca UD |
116 | void |
117 | __nptl_set_robust (struct pthread *self) | |
118 | { | |
bc2eb932 AZ |
119 | INTERNAL_SYSCALL_CALL (set_robust_list, &self->robust_head, |
120 | sizeof (struct robust_list_head)); | |
6f8326ca | 121 | } |
cca50323 UD |
122 | |
123 | ||
76a50749 UD |
124 | /* For asynchronous cancellation we use a signal. This is the handler. */ |
125 | static void | |
a1ed6b4c | 126 | sigcancel_handler (int sig, siginfo_t *si, void *ctx) |
76a50749 | 127 | { |
a1ed6b4c | 128 | /* Safety check. It would be possible to call this function for |
2edb61e3 | 129 | other signals and send a signal from another process. This is not |
a1ed6b4c UD |
130 | correct and might even be a security problem. Try to catch as |
131 | many incorrect invocations as possible. */ | |
132 | if (sig != SIGCANCEL | |
c579f48e | 133 | || si->si_pid != __getpid() |
a1ed6b4c | 134 | || si->si_code != SI_TKILL) |
a1ed6b4c UD |
135 | return; |
136 | ||
76a50749 UD |
137 | struct pthread *self = THREAD_SELF; |
138 | ||
b22d701b | 139 | int oldval = THREAD_GETMEM (self, cancelhandling); |
76a50749 UD |
140 | while (1) |
141 | { | |
142 | /* We are canceled now. When canceled by another thread this flag | |
143 | is already set but if the signal is directly send (internally or | |
144 | from another process) is has to be done here. */ | |
e320ef46 | 145 | int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK; |
76a50749 UD |
146 | |
147 | if (oldval == newval || (oldval & EXITING_BITMASK) != 0) | |
148 | /* Already canceled or exiting. */ | |
149 | break; | |
150 | ||
b22d701b UD |
151 | int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval, |
152 | oldval); | |
153 | if (curval == oldval) | |
76a50749 UD |
154 | { |
155 | /* Set the return value. */ | |
156 | THREAD_SETMEM (self, result, PTHREAD_CANCELED); | |
157 | ||
158 | /* Make sure asynchronous cancellation is still enabled. */ | |
159 | if ((newval & CANCELTYPE_BITMASK) != 0) | |
d9eb687b UD |
160 | /* Run the registered destructors and terminate the thread. */ |
161 | __do_cancel (); | |
76a50749 UD |
162 | |
163 | break; | |
164 | } | |
b22d701b UD |
165 | |
166 | oldval = curval; | |
76a50749 UD |
167 | } |
168 | } | |
169 | ||
170 | ||
2edb61e3 UD |
171 | struct xid_command *__xidcmd attribute_hidden; |
172 | ||
a9843058 SP |
173 | /* We use the SIGSETXID signal in the setuid, setgid, etc. implementations to |
174 | tell each thread to call the respective setxid syscall on itself. This is | |
175 | the handler. */ | |
2edb61e3 UD |
176 | static void |
177 | sighandler_setxid (int sig, siginfo_t *si, void *ctx) | |
178 | { | |
13f7fe35 | 179 | int result; |
7960f2a7 | 180 | |
2edb61e3 UD |
181 | /* Safety check. It would be possible to call this function for |
182 | other signals and send a signal from another process. This is not | |
183 | correct and might even be a security problem. Try to catch as | |
184 | many incorrect invocations as possible. */ | |
185 | if (sig != SIGSETXID | |
c579f48e | 186 | || si->si_pid != __getpid () |
2edb61e3 UD |
187 | || si->si_code != SI_TKILL) |
188 | return; | |
189 | ||
bc2eb932 | 190 | result = INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, 3, __xidcmd->id[0], |
13f7fe35 | 191 | __xidcmd->id[1], __xidcmd->id[2]); |
771eb141 | 192 | int error = 0; |
bc2eb932 AZ |
193 | if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (result))) |
194 | error = INTERNAL_SYSCALL_ERRNO (result); | |
771eb141 | 195 | __nptl_setxid_error (__xidcmd, error); |
2edb61e3 | 196 | |
dff9a7a1 UD |
197 | /* Reset the SETXID flag. */ |
198 | struct pthread *self = THREAD_SELF; | |
25db0f6c DJ |
199 | int flags, newval; |
200 | do | |
201 | { | |
202 | flags = THREAD_GETMEM (self, cancelhandling); | |
203 | newval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, | |
204 | flags & ~SETXID_BITMASK, flags); | |
205 | } | |
206 | while (flags != newval); | |
dff9a7a1 UD |
207 | |
208 | /* And release the futex. */ | |
209 | self->setxid_futex = 1; | |
a2f0363f | 210 | futex_wake (&self->setxid_futex, 1, FUTEX_PRIVATE); |
25db0f6c DJ |
211 | |
212 | if (atomic_decrement_val (&__xidcmd->cntr) == 0) | |
a2f0363f | 213 | futex_wake ((unsigned int *) &__xidcmd->cntr, 1, FUTEX_PRIVATE); |
2edb61e3 UD |
214 | } |
215 | ||
216 | ||
b1531183 UD |
217 | /* When using __thread for this, we do it in libc so as not |
218 | to give libpthread its own TLS segment just for this. */ | |
219 | extern void **__libc_dl_error_tsd (void) __attribute__ ((const)); | |
220 | ||
76a50749 | 221 | |
0ecf9c10 | 222 | /* This can be set by the debugger before initialization is complete. */ |
e965d514 | 223 | static bool __nptl_initial_report_events __attribute_used__; |
0ecf9c10 | 224 | |
76a50749 | 225 | void |
4e9b5995 | 226 | __pthread_initialize_minimal_internal (void) |
76a50749 | 227 | { |
76a50749 | 228 | /* Minimal initialization of the thread descriptor. */ |
75cddafe | 229 | struct pthread *pd = THREAD_SELF; |
7cea6212 | 230 | __pthread_initialize_pids (pd); |
76a50749 UD |
231 | THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]); |
232 | THREAD_SETMEM (pd, user_stack, true); | |
76a50749 | 233 | |
0f6699ea | 234 | /* Initialize the robust mutex data. */ |
992328e5 | 235 | { |
06be6368 | 236 | #if __PTHREAD_MUTEX_HAVE_PREV |
992328e5 | 237 | pd->robust_prev = &pd->robust_head; |
0f6699ea | 238 | #endif |
992328e5 | 239 | pd->robust_head.list = &pd->robust_head; |
992328e5 RM |
240 | pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock) |
241 | - offsetof (pthread_mutex_t, | |
242 | __data.__list.__next)); | |
bc2eb932 AZ |
243 | int res = INTERNAL_SYSCALL_CALL (set_robust_list, &pd->robust_head, |
244 | sizeof (struct robust_list_head)); | |
245 | if (INTERNAL_SYSCALL_ERROR_P (res)) | |
992328e5 RM |
246 | set_robust_list_not_avail (); |
247 | } | |
0f6699ea | 248 | |
675620f7 UD |
249 | /* Set initial thread's stack block from 0 up to __libc_stack_end. |
250 | It will be bigger than it actually is, but for unwind.c/pt-longjmp.c | |
251 | purposes this is good enough. */ | |
252 | THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end); | |
253 | ||
1daccf40 FW |
254 | /* Before initializing GL (dl_stack_user), the debugger could not |
255 | find us and had to set __nptl_initial_report_events. Propagate | |
256 | its setting. */ | |
0ecf9c10 | 257 | THREAD_SETMEM (pd, report_events, __nptl_initial_report_events); |
76a50749 | 258 | |
327ae257 RM |
259 | struct sigaction sa; |
260 | __sigemptyset (&sa.sa_mask); | |
261 | ||
76a50749 UD |
262 | /* Install the cancellation signal handler. If for some reason we |
263 | cannot install the handler we do not abort. Maybe we should, but | |
264 | it is only asynchronous cancellation which is affected. */ | |
a1ed6b4c UD |
265 | sa.sa_sigaction = sigcancel_handler; |
266 | sa.sa_flags = SA_SIGINFO; | |
76a50749 UD |
267 | (void) __libc_sigaction (SIGCANCEL, &sa, NULL); |
268 | ||
2edb61e3 UD |
269 | /* Install the handle to change the threads' uid/gid. */ |
270 | sa.sa_sigaction = sighandler_setxid; | |
271 | sa.sa_flags = SA_SIGINFO | SA_RESTART; | |
2edb61e3 UD |
272 | (void) __libc_sigaction (SIGSETXID, &sa, NULL); |
273 | ||
708bfb9a | 274 | /* The parent process might have left the signals blocked. Just in |
f006d3a0 UD |
275 | case, unblock it. We reuse the signal mask in the sigaction |
276 | structure. It is already cleared. */ | |
277 | __sigaddset (&sa.sa_mask, SIGCANCEL); | |
708bfb9a | 278 | __sigaddset (&sa.sa_mask, SIGSETXID); |
bc2eb932 | 279 | INTERNAL_SYSCALL_CALL (rt_sigprocmask, SIG_UNBLOCK, &sa.sa_mask, |
f26d456b | 280 | NULL, __NSIG_BYTES); |
f006d3a0 | 281 | |
b399707e RM |
282 | /* Get the size of the static and alignment requirements for the TLS |
283 | block. */ | |
284 | size_t static_tls_align; | |
285 | _dl_get_tls_static_info (&__static_tls_size, &static_tls_align); | |
286 | ||
287 | /* Make sure the size takes all the alignments into account. */ | |
288 | if (STACK_ALIGN > static_tls_align) | |
289 | static_tls_align = STACK_ALIGN; | |
290 | __static_tls_align_m1 = static_tls_align - 1; | |
291 | ||
292 | __static_tls_size = roundup (__static_tls_size, static_tls_align); | |
76a50749 | 293 | |
4e9b5995 CD |
294 | /* Determine the default allowed stack size. This is the size used |
295 | in case the user does not specify one. */ | |
296 | struct rlimit limit; | |
c5c2b7c3 | 297 | if (__getrlimit (RLIMIT_STACK, &limit) != 0 |
4e9b5995 CD |
298 | || limit.rlim_cur == RLIM_INFINITY) |
299 | /* The system limit is not usable. Use an architecture-specific | |
300 | default. */ | |
301 | limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE; | |
302 | else if (limit.rlim_cur < PTHREAD_STACK_MIN) | |
303 | /* The system limit is unusably small. | |
304 | Use the minimal size acceptable. */ | |
305 | limit.rlim_cur = PTHREAD_STACK_MIN; | |
e23872c8 | 306 | |
4e9b5995 CD |
307 | /* Make sure it meets the minimum size that allocate_stack |
308 | (allocatestack.c) will demand, which depends on the page size. */ | |
309 | const uintptr_t pagesz = GLRO(dl_pagesize); | |
310 | const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK; | |
311 | if (limit.rlim_cur < minstack) | |
312 | limit.rlim_cur = minstack; | |
e23872c8 | 313 | |
4e9b5995 | 314 | /* Round the resource limit up to page size. */ |
6d03458e | 315 | limit.rlim_cur = ALIGN_UP (limit.rlim_cur, pagesz); |
61dd6208 | 316 | lll_lock (__default_pthread_attr_lock, LLL_PRIVATE); |
c2322a56 FW |
317 | __default_pthread_attr.internal.stacksize = limit.rlim_cur; |
318 | __default_pthread_attr.internal.guardsize = GLRO (dl_pagesize); | |
61dd6208 | 319 | lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE); |
76a50749 | 320 | |
b1531183 | 321 | #ifdef SHARED |
334fcf2a UD |
322 | /* Make __rtld_lock_{,un}lock_recursive use pthread_mutex_{,un}lock, |
323 | keep the lock count from the ld.so implementation. */ | |
4d17e683 AS |
324 | GL(dl_rtld_lock_recursive) = (void *) __pthread_mutex_lock; |
325 | GL(dl_rtld_unlock_recursive) = (void *) __pthread_mutex_unlock; | |
334fcf2a UD |
326 | unsigned int rtld_lock_count = GL(dl_load_lock).mutex.__data.__count; |
327 | GL(dl_load_lock).mutex.__data.__count = 0; | |
328 | while (rtld_lock_count-- > 0) | |
4d17e683 | 329 | __pthread_mutex_lock (&GL(dl_load_lock).mutex); |
54ee14b3 UD |
330 | |
331 | GL(dl_make_stack_executable_hook) = &__make_stacks_executable; | |
b1531183 UD |
332 | #endif |
333 | ||
adc12574 UD |
334 | GL(dl_init_static_tls) = &__pthread_init_static_tls; |
335 | ||
76a50749 | 336 | /* Register the fork generation counter with the libc. */ |
5a03acfe UD |
337 | #ifndef TLS_MULTIPLE_THREADS_IN_TCB |
338 | __libc_multiple_threads_ptr = | |
339 | #endif | |
340 | __libc_pthread_init (&__fork_generation, __reclaim_stacks, | |
341 | ptr_pthread_functions); | |
2c0b891a | 342 | |
6310e6be KW |
343 | #if HAVE_TUNABLES |
344 | __pthread_tunables_init (); | |
345 | #endif | |
76a50749 | 346 | } |
2ae920ed UD |
347 | strong_alias (__pthread_initialize_minimal_internal, |
348 | __pthread_initialize_minimal) | |
2c1094bd UD |
349 | |
350 | ||
85188d82 FW |
351 | /* This function is internal (it has a GLIBC_PRIVATE) version, but it |
352 | is widely used (either via weak symbol, or dlsym) to obtain the | |
353 | __static_tls_size value. This value is then used to adjust the | |
354 | value of the stack size attribute, so that applications receive the | |
355 | full requested stack size, not diminished by the TCB and static TLS | |
356 | allocation on the stack. Once the TCB is separately allocated, | |
357 | this function should be removed or renamed (if it is still | |
358 | necessary at that point). */ | |
2c1094bd UD |
359 | size_t |
360 | __pthread_get_minstack (const pthread_attr_t *attr) | |
361 | { | |
630f4cc3 | 362 | return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN; |
2c1094bd | 363 | } |