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