This patch to libgcc blocks signals when releasing split-stack memory
due to a thread exiting.  Without this, if a signal arrives, the
signal handler may try to split the stack itself, which won't work as
the data structures won't be in a stable state.  We just leave signals
blocked while completing the exit; this should do no harm, and
prevents a signal handler from jumping in and allocating new
split-stack structures which will then never be freed.  I will shortly
check in a test for this case, as part of updating libgo to the Go
1.15.6 release.  Bootstrapped this patch and ran Go and split-stack
tests on x86_64-pc-linux-gnu.  Committed to mainline.

Ian

* generic-morestack-thread.c (free_segments): Block signals during
thread exit.
f41dd93ade24f22f8cd1863129ab20c821000134
diff --git a/libgcc/generic-morestack-thread.c 
b/libgcc/generic-morestack-thread.c
index 83a65501272..fd391bb2e1f 100644
--- a/libgcc/generic-morestack-thread.c
+++ b/libgcc/generic-morestack-thread.c
@@ -38,6 +38,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If 
not, see
 #ifndef inhibit_libc
 
 #include <errno.h>
+#include <signal.h>
 #include <pthread.h>
 
 #include "generic-morestack.h"
@@ -54,6 +55,9 @@ extern int pthread_key_create (pthread_key_t *, void (*) 
(void *))
 extern int pthread_setspecific (pthread_key_t, const void *)
   __attribute__ ((weak));
 
+extern int pthread_sigmask (int, const sigset_t *, sigset_t *)
+  __attribute__ ((weak));
+
 /* The key for the list of stack segments to free when the thread
    exits.  This is created by pthread_key_create.  */
 
@@ -70,6 +74,16 @@ static pthread_once_t create_key_once = PTHREAD_ONCE_INIT;
 static void
 free_segments (void* arg)
 {
+  /* We must block signals in case the signal handler tries to split
+     the stack.  We leave them blocked while the thread exits.  */
+  if (pthread_sigmask)
+    {
+      sigset_t mask;
+
+      sigfillset (&mask);
+      pthread_sigmask (SIG_BLOCK, &mask, NULL);
+    }
+
   __morestack_release_segments ((struct stack_segment **) arg, 1);
 }
 

Reply via email to