1 #define _GNU_SOURCE
2 #define ANON_STACK_NAME_SIZE 50
3 #include "musl_log.h"
4 #include "pthread_impl.h"
5 #include "stdio_impl.h"
6 #include "libc.h"
7 #include "lock.h"
8 #include <sys/mman.h>
9 #include <sys/prctl.h>
10 #include <string.h>
11 #include <stddef.h>
12 #include <stdarg.h>
13
14 pid_t getpid(void);
15
log_print(const char * info,...)16 void log_print(const char* info,...)
17 {
18 va_list ap;
19 va_start(ap, info);
20 vfprintf(stdout,info, ap);
21 va_end(ap);
22 }
23
stack_naming(struct pthread * new)24 void stack_naming(struct pthread *new){
25 size_t size_len;
26 unsigned char *start_addr;
27 char name[ANON_STACK_NAME_SIZE];
28 if (new->guard_size) {
29 snprintf(name, ANON_STACK_NAME_SIZE, "guard:%d", new->tid);
30 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, new->map_base, new->guard_size, name);
31 start_addr = new->map_base + new->guard_size;
32 size_len = new->map_size - new->guard_size;
33 memset(name, 0, ANON_STACK_NAME_SIZE);
34 } else {
35 start_addr = new->map_base;
36 size_len = new->map_size;
37 }
38 snprintf(name, ANON_STACK_NAME_SIZE, "stack:%d", new->tid);
39 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, start_addr, size_len, name);
40 };
41
42 #ifdef RESERVE_SIGNAL_STACK
43 #if defined (__LP64__)
44 #define RESERVE_SIGNAL_STACK_SIZE (32 * 1024)
45 #else
46 #define RESERVE_SIGNAL_STACK_SIZE (20 * 1024)
47 #endif
__pthread_reserve_signal_stack()48 void __pthread_reserve_signal_stack()
49 {
50 void* stack = mmap(NULL, RESERVE_SIGNAL_STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1 , 0);
51 if (stack != MAP_FAILED) {
52 if (mprotect(stack, __default_guardsize, PROT_NONE) == -1) {
53 munmap(stack, RESERVE_SIGNAL_STACK_SIZE);
54 return;
55 }
56 }
57
58 stack_t signal_stack;
59 signal_stack.ss_sp = (uint8_t*)stack + __default_guardsize;
60 signal_stack.ss_size = RESERVE_SIGNAL_STACK_SIZE - __default_guardsize;
61 signal_stack.ss_flags = 0;
62 sigaltstack(&signal_stack, NULL);
63
64 pthread_t self = __pthread_self();
65 self->signal_stack = stack;
66 char name[ANON_STACK_NAME_SIZE];
67 snprintf(name, ANON_STACK_NAME_SIZE, "signal_stack:%d", __pthread_self()->tid);
68 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, signal_stack.ss_sp, signal_stack.ss_size, name);
69 return;
70 }
71
__pthread_release_signal_stack()72 void __pthread_release_signal_stack()
73 {
74 pthread_t self = __pthread_self();
75 if (self->signal_stack == NULL) {
76 return;
77 }
78
79 stack_t signal_stack, old_stack;
80 memset(&signal_stack, 0, sizeof(signal_stack));
81 signal_stack.ss_flags = SS_DISABLE;
82 sigaltstack(&signal_stack, &old_stack);
83 munmap(self->signal_stack, __default_guardsize);
84 if (old_stack.ss_flags != SS_DISABLE) {
85 munmap(old_stack.ss_sp, old_stack.ss_size);
86 }
87 self->signal_stack = NULL;
88 }
89
90 weak_alias(__pthread_reserve_signal_stack, pthread_reserve_signal_stack);
91 weak_alias(__pthread_release_signal_stack, pthread_release_signal_stack);
92 #endif
93
dummy_0()94 static void dummy_0()
95 {
96 }
97 weak_alias(dummy_0, __acquire_ptc);
98 weak_alias(dummy_0, __release_ptc);
99 weak_alias(dummy_0, __pthread_tsd_run_dtors);
100 weak_alias(dummy_0, __do_orphaned_stdio_locks);
101 weak_alias(dummy_0, __dl_thread_cleanup);
102 weak_alias(dummy_0, __membarrier_init);
103
104 static int tl_lock_count;
105 static int tl_lock_waiters;
106
__tl_lock(void)107 void __tl_lock(void)
108 {
109 int tid = __pthread_self()->tid;
110 int val = __thread_list_lock;
111 if (val == tid) {
112 tl_lock_count++;
113 return;
114 }
115 while ((val = a_cas(&__thread_list_lock, 0, tid)))
116 __wait(&__thread_list_lock, &tl_lock_waiters, val, 0);
117 }
118
__tl_unlock(void)119 void __tl_unlock(void)
120 {
121 if (tl_lock_count) {
122 tl_lock_count--;
123 return;
124 }
125 a_store(&__thread_list_lock, 0);
126 if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0);
127 }
128
__tl_sync(pthread_t td)129 void __tl_sync(pthread_t td)
130 {
131 a_barrier();
132 int val = __thread_list_lock;
133 if (!val) return;
134 __wait(&__thread_list_lock, &tl_lock_waiters, val, 0);
135 if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0);
136 }
137
138 #ifdef ENABLE_HWASAN
139 weak void __hwasan_thread_enter();
140 weak void __hwasan_thread_exit();
141
142 __attribute__((no_sanitize("hwaddress")))
143 #endif
__pthread_exit(void * result)144 _Noreturn void __pthread_exit(void *result)
145 {
146 pthread_t self = __pthread_self();
147 sigset_t set;
148
149 #ifdef FEATURE_PTHREAD_CANCEL
150 self->canceldisable = 1;
151 self->cancelasync = 0;
152 #endif
153 self->result = result;
154
155 while (self->cancelbuf) {
156 void (*f)(void *) = self->cancelbuf->__f;
157 void *x = self->cancelbuf->__x;
158 self->cancelbuf = self->cancelbuf->__next;
159 f(x);
160 }
161
162 __pthread_tsd_run_dtors();
163
164 __block_app_sigs(&set);
165
166 /* This atomic potentially competes with a concurrent pthread_detach
167 * call; the loser is responsible for freeing thread resources. */
168 int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING);
169
170 if (state==DT_DETACHED && self->map_base) {
171 /* Since __unmapself bypasses the normal munmap code path,
172 * explicitly wait for vmlock holders first. This must be
173 * done before any locks are taken, to avoid lock ordering
174 * issues that could lead to deadlock. */
175 __vm_wait();
176 }
177
178 /* Access to target the exiting thread with syscalls that use
179 * its kernel tid is controlled by killlock. For detached threads,
180 * any use past this point would have undefined behavior, but for
181 * joinable threads it's a valid usage that must be handled.
182 * Signals must be blocked since pthread_kill must be AS-safe. */
183 LOCK(self->killlock);
184
185 /* The thread list lock must be AS-safe, and thus depends on
186 * application signals being blocked above. */
187 __tl_lock();
188
189 #ifdef RESERVE_SIGNAL_STACK
190 __pthread_release_signal_stack();
191 #endif
192 /* If this is the only thread in the list, don't proceed with
193 * termination of the thread, but restore the previous lock and
194 * signal state to prepare for exit to call atexit handlers. */
195 if (self->next == self) {
196 __tl_unlock();
197 UNLOCK(self->killlock);
198 self->detach_state = state;
199 __restore_sigs(&set);
200 #ifdef ENABLE_HWASAN
201 __hwasan_thread_exit();
202 #endif
203 exit(0);
204 }
205
206 /* At this point we are committed to thread termination. */
207
208 /* Process robust list in userspace to handle non-pshared mutexes
209 * and the detached thread case where the robust list head will
210 * be invalid when the kernel would process it. */
211 __vm_lock();
212 volatile void *volatile *rp;
213 while ((rp=self->robust_list.head) && rp != &self->robust_list.head) {
214 pthread_mutex_t *m = (void *)((char *)rp
215 - offsetof(pthread_mutex_t, _m_next));
216 int waiters = m->_m_waiters;
217 int priv = (m->_m_type & 128) ^ 128;
218 self->robust_list.pending = rp;
219 self->robust_list.head = *rp;
220 int cont = a_swap(&m->_m_lock, 0x40000000);
221 self->robust_list.pending = 0;
222 if (cont < 0 || waiters)
223 __wake(&m->_m_lock, 1, priv);
224 }
225 __vm_unlock();
226
227 __do_orphaned_stdio_locks();
228 __dl_thread_cleanup();
229
230 /* Last, unlink thread from the list. This change will not be visible
231 * until the lock is released, which only happens after SYS_exit
232 * has been called, via the exit futex address pointing at the lock.
233 * This needs to happen after any possible calls to LOCK() that might
234 * skip locking if process appears single-threaded. */
235 if (!--libc.threads_minus_1) libc.need_locks = -1;
236 self->next->prev = self->prev;
237 self->prev->next = self->next;
238 self->prev = self->next = self;
239
240 if (state==DT_DETACHED && self->map_base) {
241 /* Detached threads must block even implementation-internal
242 * signals, since they will not have a stack in their last
243 * moments of existence. */
244 __block_all_sigs(&set);
245
246 /* Robust list will no longer be valid, and was already
247 * processed above, so unregister it with the kernel. */
248 if (self->robust_list.off)
249 __syscall(SYS_set_robust_list, 0, 3*sizeof(long));
250
251 /* The following call unmaps the thread's stack mapping
252 * and then exits without touching the stack. */
253 __unmapself(self->map_base, self->map_size);
254 }
255
256 /* Wake any joiner. */
257 a_store(&self->detach_state, DT_EXITED);
258 __wake(&self->detach_state, 1, 1);
259
260 /* After the kernel thread exits, its tid may be reused. Clear it
261 * to prevent inadvertent use and inform functions that would use
262 * it that it's no longer available. */
263 self->tid = 0;
264 UNLOCK(self->killlock);
265
266 #ifdef ENABLE_HWASAN
267 __hwasan_thread_exit();
268 #endif
269
270 for (;;) __syscall(SYS_exit, 0);
271 }
272
__do_cleanup_push(struct __ptcb * cb)273 void __do_cleanup_push(struct __ptcb *cb)
274 {
275 struct pthread *self = __pthread_self();
276 cb->__next = self->cancelbuf;
277 self->cancelbuf = cb;
278 }
279
__do_cleanup_pop(struct __ptcb * cb)280 void __do_cleanup_pop(struct __ptcb *cb)
281 {
282 __pthread_self()->cancelbuf = cb->__next;
283 }
284
285 struct start_args {
286 void *(*start_func)(void *);
287 void *start_arg;
288 volatile int control;
289 unsigned long sig_mask[_NSIG/8/sizeof(long)];
290 };
291
292 #ifdef ENABLE_HWASAN
293 __attribute__((no_sanitize("hwaddress")))
294 #endif
start(void * p)295 static int start(void *p)
296 {
297 #ifdef ENABLE_HWASAN
298 __hwasan_thread_enter();
299 #endif
300 struct start_args *args = p;
301 int state = args->control;
302 if (state) {
303 if (a_cas(&args->control, 1, 2)==1)
304 __wait(&args->control, 0, 2, 1);
305 if (args->control) {
306 __syscall(SYS_set_tid_address, &args->control);
307 for (;;) __syscall(SYS_exit, 0);
308 }
309 }
310 __syscall(SYS_rt_sigprocmask, SIG_SETMASK, &args->sig_mask, 0, _NSIG/8);
311 #ifdef RESERVE_SIGNAL_STACK
312 __pthread_reserve_signal_stack();
313 #endif
314 __pthread_exit(args->start_func(args->start_arg));
315 return 0;
316 }
317
318 #ifdef ENABLE_HWASAN
319 __attribute__((no_sanitize("hwaddress")))
320 #endif
start_c11(void * p)321 static int start_c11(void *p)
322 {
323 #ifdef RESERVE_SIGNAL_STACK
324 __pthread_reserve_signal_stack();
325 #endif
326 #ifdef ENABLE_HWASAN
327 __hwasan_thread_enter();
328 #endif
329 struct start_args *args = p;
330 int (*start)(void*) = (int(*)(void*)) args->start_func;
331 __pthread_exit((void *)(uintptr_t)start(args->start_arg));
332 return 0;
333 }
334
335 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
336
337 /* pthread_key_create.c overrides this */
338 static volatile size_t dummy = 0;
339 weak_alias(dummy, __pthread_tsd_size);
340 static void *dummy_tsd[1] = { 0 };
341 weak_alias(dummy_tsd, __pthread_tsd_main);
342
343 static FILE *volatile dummy_file = 0;
344 weak_alias(dummy_file, __stdin_used);
345 weak_alias(dummy_file, __stdout_used);
346 weak_alias(dummy_file, __stderr_used);
347
init_file_lock(FILE * f)348 static void init_file_lock(FILE *f)
349 {
350 if (f && f->lock<0) f->lock = 0;
351 }
352
353 #ifdef ENABLE_HWASAN
354 __attribute__((no_sanitize("hwaddress")))
355 #endif
__pthread_create(pthread_t * restrict res,const pthread_attr_t * restrict attrp,void * (* entry)(void *),void * restrict arg)356 int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
357 {
358 int ret, c11 = (attrp == __ATTRP_C11_THREAD);
359 size_t size, guard, size_len;
360 struct pthread *self, *new;
361 unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit, *start_addr;
362 unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
363 | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
364 | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
365 pthread_attr_t attr = { 0 };
366 sigset_t set;
367
368 if (!libc.can_do_threads) {
369 MUSL_LOGE("pthread_create: can't do threads, err: %{public}s", strerror(errno));
370 return ENOSYS;
371 }
372 self = __pthread_self();
373 if (!libc.threaded) {
374 for (FILE *f=*__ofl_lock(); f; f=f->next)
375 init_file_lock(f);
376 __ofl_unlock();
377 init_file_lock(__stdin_used);
378 init_file_lock(__stdout_used);
379 init_file_lock(__stderr_used);
380 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
381 self->tsd = (void **)__pthread_tsd_main;
382 __membarrier_init();
383 libc.threaded = 1;
384 }
385 if (attrp && !c11) attr = *attrp;
386
387 __acquire_ptc();
388 if (!attrp || c11) {
389 attr._a_stacksize = __default_stacksize;
390 attr._a_guardsize = __default_guardsize;
391 }
392
393 if (attr._a_stackaddr) {
394 size_t need = libc.tls_size + __pthread_tsd_size;
395 size = attr._a_stacksize;
396 stack = (void *)(attr._a_stackaddr & -16);
397 stack_limit = (void *)(attr._a_stackaddr - size);
398 /* Use application-provided stack for TLS only when
399 * it does not take more than ~12% or 2k of the
400 * application's stack space. */
401 if (need < size/8 && need < 2048) {
402 tsd = stack - __pthread_tsd_size;
403 stack = tsd - libc.tls_size;
404 memset(stack, 0, need);
405 } else {
406 size = ROUND(need);
407 }
408 guard = 0;
409 } else {
410 guard = ROUND(attr._a_guardsize);
411 size = guard + ROUND(attr._a_stacksize
412 + libc.tls_size + __pthread_tsd_size);
413 }
414
415 if (!tsd) {
416 if (guard) {
417 map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
418 if (map == MAP_FAILED) {
419 MUSL_LOGE("pthread_create: mmap PROT_NONE failed, err:%{public}s", strerror(errno));
420 goto fail;
421 }
422 if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
423 && errno != ENOSYS) {
424 MUSL_LOGE("pthread_create: mprotect failed, err:%{public}s", strerror(errno));
425 __munmap(map, size);
426 goto fail;
427 }
428 } else {
429 map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
430 if (map == MAP_FAILED) {
431 MUSL_LOGE("pthread_create: mmap PROT_READ|PROT_WRITE failed, err:%{public}s", strerror(errno));
432 goto fail;
433 }
434 }
435 tsd = map + size - __pthread_tsd_size;
436 if (!stack) {
437 stack = tsd - libc.tls_size;
438 stack_limit = map + guard;
439 }
440 }
441
442 new = __copy_tls(tsd - libc.tls_size);
443 new->map_base = map;
444 new->map_size = size;
445 new->stack = stack;
446 new->stack_size = stack - stack_limit;
447 new->guard_size = guard;
448 new->self = new;
449 new->pid = getpid();
450 new->proc_tid = -1;
451 new->tsd = (void *)tsd;
452 new->locale = &libc.global_locale;
453 if (attr._a_detach) {
454 new->detach_state = DT_DETACHED;
455 } else {
456 new->detach_state = DT_JOINABLE;
457 }
458 new->robust_list.head = &new->robust_list.head;
459 new->canary = self->canary;
460 new->sysinfo = self->sysinfo;
461
462 /* Setup argument structure for the new thread on its stack.
463 * It's safe to access from the caller only until the thread
464 * list is unlocked. */
465 stack -= (uintptr_t)stack % sizeof(uintptr_t);
466 stack -= sizeof(struct start_args);
467 struct start_args *args = (void *)stack;
468 args->start_func = entry;
469 args->start_arg = arg;
470 args->control = attr._a_sched ? 1 : 0;
471
472 /* Application signals (but not the synccall signal) must be
473 * blocked before the thread list lock can be taken, to ensure
474 * that the lock is AS-safe. */
475 __block_app_sigs(&set);
476
477 /* Ensure SIGCANCEL is unblocked in new thread. This requires
478 * working with a copy of the set so we can restore the
479 * original mask in the calling thread. */
480 memcpy(&args->sig_mask, &set, sizeof args->sig_mask);
481 args->sig_mask[(SIGCANCEL-1)/8/sizeof(long)] &=
482 ~(1UL<<((SIGCANCEL-1)%(8*sizeof(long))));
483
484 __tl_lock();
485 if (!libc.threads_minus_1++) libc.need_locks = 1;
486 ret = __clone((c11 ? start_c11 : start), stack, flags, args, &new->tid, TP_ADJ(new), &__thread_list_lock);
487
488 /* All clone failures translate to EAGAIN. If explicit scheduling
489 * was requested, attempt it before unlocking the thread list so
490 * that the failed thread is never exposed and so that we can
491 * clean up all transient resource usage before returning. */
492 if (ret < 0) {
493 ret = -EAGAIN;
494 } else if (attr._a_sched) {
495 ret = __syscall(SYS_sched_setscheduler,
496 new->tid, attr._a_policy, &attr._a_prio);
497 if (a_swap(&args->control, ret ? 3 : 0)==2)
498 __wake(&args->control, 1, 1);
499 if (ret)
500 __wait(&args->control, 0, 3, 0);
501 }
502
503 if (ret >= 0) {
504 stack_naming(new);
505
506 new->next = self->next;
507 new->prev = self;
508 new->next->prev = new;
509 new->prev->next = new;
510 } else {
511 if (!--libc.threads_minus_1) libc.need_locks = 0;
512 }
513 __tl_unlock();
514 __restore_sigs(&set);
515 __release_ptc();
516
517 if (ret < 0) {
518 if (map) __munmap(map, size);
519 MUSL_LOGE("pthread_create: ret:%{public}d, err:%{public}s", ret, strerror(errno));
520 return -ret;
521 }
522
523 *res = new;
524 return 0;
525 fail:
526 __release_ptc();
527 return EAGAIN;
528 }
529
530 weak_alias(__pthread_exit, pthread_exit);
531 weak_alias(__pthread_create, pthread_create);
532
__pthread_list_find(pthread_t thread_id,const char * info)533 struct pthread* __pthread_list_find(pthread_t thread_id, const char* info)
534 {
535 struct pthread *thread = (struct pthread *)thread_id;
536 if (NULL == thread) {
537 log_print("invalid pthread_t (0) passed to %s\n", info);
538 return NULL;
539 }
540
541 struct pthread *self = __pthread_self();
542 if (thread == self) {
543 return thread;
544 }
545 struct pthread *t = self;
546 t = t->next ;
547 while (t != self) {
548 if (t == thread) return thread;
549 t = t->next ;
550 }
551 log_print("invalid pthread_t %p passed to %s\n", thread, info);
552 return NULL;
553 }
554
__pthread_gettid_np(pthread_t t)555 pid_t __pthread_gettid_np(pthread_t t)
556 {
557 __tl_lock();
558 struct pthread* thread = __pthread_list_find(t, "pthread_gettid_np");
559 __tl_unlock();
560 return thread ? thread->tid : -1;
561 }
562 weak_alias(__pthread_gettid_np, pthread_gettid_np);
563