don't trust siginfo in rsyscall handler
[musl] / src / thread / pthread_create.c
1 #include "pthread_impl.h"
2
3 static void dummy_1(pthread_t self)
4 {
5 }
6 weak_alias(dummy_1, __pthread_tsd_run_dtors);
7 weak_alias(dummy_1, __sigtimer_handler);
8
9 #ifdef __pthread_unwind_next
10 #undef __pthread_unwind_next
11 #define __pthread_unwind_next __pthread_unwind_next_3
12 #endif
13
14 void __pthread_unwind_next(struct __ptcb *cb)
15 {
16         pthread_t self;
17
18         if (cb->__next) longjmp((void *)cb->__next->__jb, 1);
19
20         self = pthread_self();
21
22         LOCK(&self->exitlock);
23
24         __pthread_tsd_run_dtors(self);
25
26         /* Mark this thread dead before decrementing count */
27         self->dead = 1;
28
29         if (!a_fetch_add(&libc.threads_minus_1, -1))
30                 exit(0);
31
32         if (self->detached && self->map_base) {
33                 syscall(__NR_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
34                 __unmapself(self->map_base, self->map_size);
35         }
36
37         syscall(SYS_exit, 0);
38 }
39
40 static void docancel(struct pthread *self)
41 {
42         struct __ptcb cb = { .__next = self->cancelbuf };
43         self->canceldisable = 1;
44         self->cancelasync = 0;
45         __pthread_unwind_next(&cb);
46 }
47
48 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
49 {
50         struct pthread *self = __pthread_self();
51         if (si->si_code == SI_TIMER) __sigtimer_handler(self);
52         if (self->cancel && !self->canceldisable &&
53             (self->cancelasync || (self->cancelpoint==1 && PC_AT_SYS(ctx))))
54                 docancel(self);
55 }
56
57 static void cancelpt(int x)
58 {
59         struct pthread *self = __pthread_self();
60         if (self->canceldisable) return;
61         if ((self->cancelpoint+=x)==1 && x>=0 && self->cancel)
62                 docancel(self);
63 }
64
65 /* "rsyscall" is a mechanism by which a thread can synchronously force all
66  * other threads to perform an arbitrary syscall. It is necessary to work
67  * around the non-conformant implementation of setuid() et al on Linux,
68  * which affect only the calling thread and not the whole process. This
69  * implementation performs some tricks with signal delivery to work around
70  * the fact that it does not keep any list of threads in userspace. */
71
72 static struct {
73         volatile int lock, hold, blocks, cnt;
74         unsigned long arg[6];
75         int nr;
76         int err;
77 } rs;
78
79 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
80 {
81         struct pthread *self = __pthread_self();
82
83         if (!rs.hold || rs.cnt == libc.threads_minus_1) return;
84
85         /* Threads which have already decremented themselves from the
86          * thread count must not increment rs.cnt or otherwise act. */
87         if (self->dead) {
88                 __wait(&rs.hold, 0, 1, 1);
89                 return;
90         }
91
92         if (syscall(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
93                 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
94
95         a_inc(&rs.cnt);
96         __wake(&rs.cnt, 1, 1);
97         while(rs.hold)
98                 __wait(&rs.hold, 0, 1, 1);
99         a_dec(&rs.cnt);
100         if (!rs.cnt) __wake(&rs.cnt, 1, 1);
101 }
102
103 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
104 {
105         int i, ret;
106         sigset_t set = { 0 };
107         struct pthread *self = __pthread_self();
108         sigaddset(&set, SIGSYSCALL);
109
110         LOCK(&rs.lock);
111         while ((i=rs.blocks))
112                 __wait(&rs.blocks, 0, i, 1);
113
114         __libc_sigprocmask(SIG_BLOCK, &set, 0);
115
116         rs.nr = nr;
117         rs.arg[0] = a; rs.arg[1] = b;
118         rs.arg[2] = c; rs.arg[3] = d;
119         rs.arg[4] = d; rs.arg[5] = f;
120         rs.err = 0;
121         rs.cnt = 0;
122         rs.hold = 1;
123
124         /* Dispatch signals until all threads respond */
125         for (i=libc.threads_minus_1; i; i--)
126                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
127         while ((i=rs.cnt) < libc.threads_minus_1) {
128                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
129                 __wait(&rs.cnt, 0, i, 1);
130         }
131
132         /* Handle any lingering signals with no-op */
133         __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
134
135         /* Resume other threads' signal handlers and wait for them */
136         rs.hold = 0;
137         __wake(&rs.hold, -1, 0);
138         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
139
140         if (rs.err) errno = rs.err, ret = -1;
141         else ret = syscall(nr, a, b, c, d, e, f);
142
143         UNLOCK(&rs.lock);
144         return ret;
145 }
146
147 static void init_threads()
148 {
149         struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
150         libc.lock = __lock;
151         libc.lockfile = __lockfile;
152         libc.cancelpt = cancelpt;
153         libc.rsyscall = rsyscall;
154         sa.sa_sigaction = cancel_handler;
155         __libc_sigaction(SIGCANCEL, &sa, 0);
156         sigaddset(&sa.sa_mask, SIGSYSCALL);
157         sigaddset(&sa.sa_mask, SIGCANCEL);
158         sa.sa_sigaction = rsyscall_handler;
159         __libc_sigaction(SIGSYSCALL, &sa, 0);
160         sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
161 }
162
163 static int start(void *p)
164 {
165         struct pthread *self = p;
166         if (self->unblock_cancel) {
167                 sigset_t set;
168                 sigemptyset(&set);
169                 sigaddset(&set, SIGCANCEL);
170                 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
171         }
172         pthread_exit(self->start(self->start_arg));
173         return 0;
174 }
175
176 int __uniclone(void *, int (*)(), void *);
177
178 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
179
180 /* pthread_key_create.c overrides this */
181 static const size_t dummy = 0;
182 weak_alias(dummy, __pthread_tsd_size);
183
184 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
185 {
186         static int init;
187         int ret;
188         size_t size, guard;
189         struct pthread *self = pthread_self(), *new;
190         unsigned char *map, *stack, *tsd;
191         static const pthread_attr_t default_attr;
192
193         if (!self) return errno = ENOSYS;
194         if (!init && ++init) init_threads();
195
196         if (!attr) attr = &default_attr;
197         guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
198         size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
199         size += __pthread_tsd_size;
200         map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
201         if (!map) return EAGAIN;
202         if (guard) mprotect(map, guard, PROT_NONE);
203
204         tsd = map + size - __pthread_tsd_size;
205         new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
206         new->map_base = map;
207         new->map_size = size;
208         new->pid = self->pid;
209         new->errno_ptr = &new->errno_val;
210         new->start = entry;
211         new->start_arg = arg;
212         new->self = new;
213         new->tsd = (void *)tsd;
214         new->detached = attr->_a_detach;
215         new->attr = *attr;
216         new->unblock_cancel = self->cancel;
217         new->result = PTHREAD_CANCELED;
218         memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
219         new->tlsdesc[1] = (uintptr_t)new;
220         stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
221
222         /* We must synchronize new thread creation with rsyscall
223          * delivery. This looks to be the least expensive way: */
224         a_inc(&rs.blocks);
225         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
226
227         a_inc(&libc.threads_minus_1);
228         ret = __uniclone(stack, start, new);
229
230         a_dec(&rs.blocks);
231         if (rs.lock) __wake(&rs.blocks, 1, 1);
232
233         if (ret < 0) {
234                 a_dec(&libc.threads_minus_1);
235                 munmap(map, size);
236                 return EAGAIN;
237         }
238         *res = new;
239         return 0;
240 }
241
242 void pthread_exit(void *result)
243 {
244         struct pthread *self = pthread_self();
245         self->result = result;
246         docancel(self);
247 }