fix race condition in rsyscall handler
authorRich Felker <dalias@aerifal.cx>
Sun, 3 Apr 2011 17:03:18 +0000 (13:03 -0400)
committerRich Felker <dalias@aerifal.cx>
Sun, 3 Apr 2011 17:03:18 +0000 (13:03 -0400)
the problem: there is a (single-instruction) race condition window
between a thread flagging itself dead and decrementing itself from the
thread count. if it receives the rsyscall signal at this exact moment,
the rsyscall caller will never succeed in signalling enough flags to
succeed, and will deadlock forever. in previous versions of musl, the
about-to-terminate thread masked all signals prior to decrementing
the thread count, but this cost a whole syscall just to account for
extremely rare races.

the solution is a huge hack: rather than blocking in the signal
handler if the thread is dead, modify the signal mask of the saved
context and return in order to prevent further signal handling by the
dead thread. this allows the dead thread to continue decrementing the
thread count (if it had not yet done so) and exiting, even while the
live part of the program blocks for rsyscall.

src/thread/pthread_create.c

index 9df4f71..344b39f 100644 (file)
@@ -85,7 +85,7 @@ static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
        /* Threads which have already decremented themselves from the
         * thread count must not increment rs.cnt or otherwise act. */
        if (self->dead) {
-               __wait(&rs.hold, 0, 1, 1);
+               sigaddset(&((ucontext_t *)ctx)->uc_sigmask, SIGSYSCALL);
                return;
        }