always initialize thread pointer at program start
[musl] / src / env / __stack_chk_fail.c
1 #include <string.h>
2 #include <stdint.h>
3 #include "pthread_impl.h"
4 #include "atomic.h"
5
6 uintptr_t __stack_chk_guard;
7
8 void __init_ssp(void *entropy)
9 {
10         /* Here the thread pointer is used without checking whether
11          * it is available; this will crash if it's not. However,
12          * this function is only meant to be called if the program
13          * being run uses stack protector, and in that case, it would
14          * crash without a thread pointer anyway, so it's better to
15          * crash early before there is state to be lost on crash. */
16         pthread_t self = __pthread_self();
17         uintptr_t canary;
18         if (entropy) memcpy(&canary, entropy, sizeof canary);
19         else canary = (uintptr_t)&canary * 1103515245;
20         a_cas_l(&__stack_chk_guard, 0, canary);
21         self->canary = __stack_chk_guard;
22 }
23
24 void __stack_chk_fail(void)
25 {
26         a_crash();
27 }