From f0419e6a10740a672b28e112c409ae24f5e890ab Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Thu, 4 Mar 2021 15:15:33 +0100 Subject: [PATCH] pthread_once hangs when init routine throws an exception [BZ #18435] This is another attempt at making pthread_once handle throwing exceptions from the init routine callback. As the new testcases show, just switching to the cleanup attribute based cleanup does fix the tst-once5 test, but breaks the new tst-oncey3 test. That is because when throwing exceptions, only the unwind info registered cleanups (i.e. C++ destructors or cleanup attribute), when cancelling threads and there has been unwind info from the cancellation point up to whatever needs cleanup both unwind info registered cleanups and THREAD_SETMEM (self, cleanup, ...) registered cleanups are invoked, but once we hit some frame with no unwind info, only the THREAD_SETMEM (self, cleanup, ...) registered cleanups are invoked. So, to stay fully backwards compatible (allow init routines without unwind info which encounter cancellation points) and handle exception throwing we actually need to register the pthread_once cleanups in both unwind info and in the THREAD_SETMEM (self, cleanup, ...) way. If an exception is thrown, only the former will happen and we in that case need to also unregister the THREAD_SETMEM (self, cleanup, ...) registered handler, because otherwise after catching the exception the user code could call deeper into the stack some cancellation point, get cancelled and then a stale cleanup handler would clobber stack and probably crash. If a thread calling init routine is cancelled and unwind info ends before the pthread_once frame, it will be cleaned up through self->cleanup as before. And if unwind info is present, unwind_stop first calls the self->cleanup registered handler for the frame, then it will call the unwind info registered handler but that will already see __do_it == 0 and do nothing. --- sysdeps/pthread/Makefile | 4 +++- sysdeps/pthread/tst-oncey3.c | 1 + sysdeps/pthread/tst-oncey4.c | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 sysdeps/pthread/tst-oncey3.c create mode 100644 sysdeps/pthread/tst-oncey4.c (limited to 'sysdeps/pthread') diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index eeb64f9..53b65ef 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -231,7 +231,7 @@ generated += $(objpfx)tst-atfork2.mtrace \ tests-internal += tst-cancel25 tst-robust8 -tests += tst-oncex3 tst-oncex4 +tests += tst-oncex3 tst-oncex4 tst-oncey3 tst-oncey4 modules-names += tst-join7mod @@ -242,6 +242,8 @@ endif CFLAGS-tst-oncex3.c += -fexceptions CFLAGS-tst-oncex4.c += -fexceptions +CFLAGS-tst-oncey3.c += -fno-exceptions -fno-asynchronous-unwind-tables +CFLAGS-tst-oncey4.c += -fno-exceptions -fno-asynchronous-unwind-tables $(objpfx)tst-join7: $(libdl) $(shared-thread-library) $(objpfx)tst-join7.out: $(objpfx)tst-join7mod.so diff --git a/sysdeps/pthread/tst-oncey3.c b/sysdeps/pthread/tst-oncey3.c new file mode 100644 index 0000000..08225b8 --- /dev/null +++ b/sysdeps/pthread/tst-oncey3.c @@ -0,0 +1 @@ +#include "tst-once3.c" diff --git a/sysdeps/pthread/tst-oncey4.c b/sysdeps/pthread/tst-oncey4.c new file mode 100644 index 0000000..9b4d98f --- /dev/null +++ b/sysdeps/pthread/tst-oncey4.c @@ -0,0 +1 @@ +#include "tst-once4.c" -- cgit v1.1