fix async thread cancellation stack alignment
authorRich Felker <dalias@aerifal.cx>
Sat, 5 Nov 2022 22:53:11 +0000 (18:53 -0400)
committerRich Felker <dalias@aerifal.cx>
Sat, 5 Nov 2022 22:59:53 +0000 (18:59 -0400)
if async cancellation is enabled and acted upon, the stack pointer is
not necessarily pointing to a __syscall_cp_asm stack frame. the
contents of the stack being wrong don't really matter, but if the
stack pointer is not suitably aligned, the procedure call ABI is
violated when calling back into C code via __cancel, and pthread_exit,
cancellation cleanup handlers, TSD destructors, etc. may malfunction
or crash.

for the async cancel case, just call __cancel directly like we did
prior to commit 102f6a01e249ce4495f1119ae6d963a2a4a53ce5. restore the
signal mask prior to doing this since the cancellation handler runs
with all signals blocked.

src/thread/pthread_cancel.c

index 2d3a98e..139a6fc 100644 (file)
@@ -56,7 +56,12 @@ static void cancel_handler(int sig, siginfo_t *si, void *ctx)
 
        _sigaddset(&uc->uc_sigmask, SIGCANCEL);
 
-       if (self->cancelasync || pc >= (uintptr_t)__cp_begin && pc < (uintptr_t)__cp_end) {
+       if (self->cancelasync) {
+               pthread_sigmask(SIG_SETMASK, &uc->uc_sigmask, 0);
+               __cancel();
+       }
+
+       if (pc >= (uintptr_t)__cp_begin && pc < (uintptr_t)__cp_end) {
                uc->uc_mcontext.MC_PC = (uintptr_t)__cp_cancel;
 #ifdef CANCEL_GOT
                uc->uc_mcontext.MC_GOT = CANCEL_GOT;