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