bca718
Based on the following commits:
bca718
bca718
commit 2c41b52901331f5c761015af786a3976e225d779
bca718
Author: Florian Weimer <fweimer@redhat.com>
bca718
Date:   Mon Jun 13 13:08:39 2016 +0200
bca718
bca718
    debug/tst-longjmp_chk2: Make signal handler more conservative [BZ #20248]
bca718
    
bca718
    Currently, printf needs more stack space than what is available with
bca718
    SIGSTKSZ.  This commit use the the write system call directly instead.
bca718
    
bca718
    Also use sig_atomic_t for the “pass” variable (for general
bca718
    correctness), and restore signal handlers to their defaults, to avoid
bca718
    masking crashes.
bca718
bca718
commit 5896c8bdd9f73cdc816a96e107ca1f7a6bc6921e
bca718
Author: Mike Frysinger <vapier@gentoo.org>
bca718
Date:   Sun Dec 29 16:30:35 2013 -0500
bca718
bca718
    tst-longjmp_chk2: add comments/sanity check
bca718
    
bca718
    If the longjmp checking code is slightly broken, this code can loop
bca718
    forever which isn't too helpful.  Add a sanity check to keep that
bca718
    from happening.
bca718
    
bca718
    Signed-off-by: Mike Frysinger <vapier@gentoo.org>
bca718
bca718
Index: b/debug/tst-longjmp_chk2.c
bca718
===================================================================
bca718
--- a/debug/tst-longjmp_chk2.c
bca718
+++ b/debug/tst-longjmp_chk2.c
bca718
@@ -4,27 +4,36 @@
bca718
 #include <signal.h>
bca718
 #include <stdio.h>
bca718
 #include <stdlib.h>
bca718
+#include <string.h>
bca718
 #include <sys/types.h>
bca718
 #include <sys/time.h>
bca718
 #include <sys/resource.h>
bca718
+#include <unistd.h>
bca718
 
bca718
 
bca718
 static jmp_buf mainloop;
bca718
 static sigset_t mainsigset;
bca718
-static int pass;
bca718
+static volatile sig_atomic_t pass;
bca718
 
bca718
+static void
bca718
+write_message (const char *message)
bca718
+{
bca718
+  ssize_t unused __attribute__ ((unused));
bca718
+  for (int i = 0; i < pass; ++i)
bca718
+    unused = write (STDOUT_FILENO, " ", 1);
bca718
+  unused = write (STDOUT_FILENO, message, strlen (message));
bca718
+}
bca718
 
bca718
 static void
bca718
 stackoverflow_handler (int sig)
bca718
 {
bca718
   stack_t altstack;
bca718
   pass++;
bca718
+  assert (pass < 5);
bca718
   sigaltstack (NULL, &altstack);
bca718
-  /* Using printf is not really kosher in signal handlers but we know
bca718
-     it will work.  */
bca718
-  printf ("%*sin signal handler\n", pass, "");
bca718
+  write_message ("in signal handler\n");
bca718
   if (altstack.ss_flags & SS_ONSTACK)
bca718
-    printf ("%*son alternate stack\n", pass, "");
bca718
+    write_message ("on alternate stack\n");
bca718
   siglongjmp (mainloop, pass);
bca718
 }
bca718
 
bca718
@@ -107,6 +116,11 @@ do_test (void)
bca718
   else
bca718
     printf ("disabling alternate stack succeeded \n");
bca718
 
bca718
+  /* Restore the signal handlers, in case we trigger a crash after the
bca718
+     tests above.  */
bca718
+  signal (SIGBUS, SIG_DFL);
bca718
+  signal (SIGSEGV, SIG_DFL);
bca718
+
bca718
   return 0;
bca718
 }
bca718