Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

libsigsegv self test hanging



libsigsegv in pkgsrc hangs for me during pbulk builds in the configure
step on NetBSD-8.99.7/amd64 (and 8.99.5, but it worked not too far
ago):

checking whether a signal handler can be left through longjmp...

The message comes from the appended m4 file.
I tried extracting the corresponding c code (also attached).

When I compile this with 

gcc -DRLIMIT_STACK=1 -DHAVE_SETRLIMIT=1 -o test test.c

(or even without the defines) and run the executable, I get a core
dump: "Illegal instruction (core dumped)". Run in gdb, I see a
sigsegfault with:

(gdb) bt
#0  0x0000000000400ac9 in recurse_1 ()
#1  0x0000000000400ace in recurse_1 ()
#2  0x0000000000400ace in recurse_1 ()
#3  0x0000000000400ace in recurse_1 ()
#4  0x0000000000400ace in recurse_1 ()
(ad infinitum, or at least 15000 frames)

Interestingly, in a pbulk bulk build with an unprivileged user, this
just seems to hang with 99% CPU instead.


I have a couple open questions:

* why does it hang now?
* why didn't it hang before?
* why does it behave differently when run from directly?
* does the test make sense?

Insights welcome.

Thanks,
 Thomas

# sigaltstack-longjmp.m4 serial 6 (libsigsegv-2.7)
dnl Copyright (C) 2002-2003, 2006, 2008 Bruno Haible <bruno%clisp.org@localhost>
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License.  As a special exception to the GNU General
dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.

dnl How to longjmp out of a signal handler, in such a way that the
dnl alternate signal stack remains functional.
dnl SV_TRY_LEAVE_HANDLER_LONGJMP(KIND, CACHESYMBOL, KNOWN-SYSTEMS,
dnl                              INCLUDES, RESETCODE)
AC_DEFUN([SV_TRY_LEAVE_HANDLER_LONGJMP],
[
  AC_REQUIRE([AC_PROG_CC])
  AC_REQUIRE([AC_CANONICAL_HOST])

  AC_CACHE_CHECK([whether a signal handler can be left through longjmp$1], [$2], [
    AC_RUN_IFELSE([
      AC_LANG_SOURCE([[
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
$4
#if HAVE_SETRLIMIT
# include <sys/types.h>
# include <sys/time.h>
# include <sys/resource.h>
#endif
#ifndef SIGSTKSZ
# define SIGSTKSZ 16384
#endif
jmp_buf mainloop;
sigset_t mainsigset;
int pass = 0;
void stackoverflow_handler (int sig)
{
  pass++;
  sigprocmask (SIG_SETMASK, &mainsigset, NULL);
  { $5 }
  longjmp (mainloop, pass);
}
volatile int * recurse_1 (volatile int n, volatile int *p)
{
  if (n >= 0)
    *recurse_1 (n + 1, p) += n;
  return p;
}
int recurse (volatile int n)
{
  int sum = 0;
  return *recurse_1 (n, &sum);
}
char mystack[2 * SIGSTKSZ];
int main ()
{
  stack_t altstack;
  struct sigaction action;
  sigset_t emptyset;
#if defined HAVE_SETRLIMIT && defined RLIMIT_STACK
  /* Before starting the endless recursion, try to be friendly to the user's
     machine.  On some Linux 2.2.x systems, there is no stack limit for user
     processes at all.  We don't want to kill such systems.  */
  struct rlimit rl;
  rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
  setrlimit (RLIMIT_STACK, &rl);
#endif
  /* Install the alternate stack.  Use the midpoint of mystack, to guard
     against a buggy interpretation of ss_sp on IRIX.  */
  altstack.ss_sp = mystack + SIGSTKSZ;
  altstack.ss_size = SIGSTKSZ;
  altstack.ss_flags = 0; /* no SS_DISABLE */
  if (sigaltstack (&altstack, NULL) < 0)
    exit (1);
  /* Install the SIGSEGV handler.  */
  sigemptyset (&action.sa_mask);
  action.sa_handler = &stackoverflow_handler;
  action.sa_flags = SA_ONSTACK;
  sigaction (SIGSEGV, &action, (struct sigaction *) NULL);
  sigaction (SIGBUS, &action, (struct sigaction *) NULL);
  /* Save the current signal mask.  */
  sigemptyset (&emptyset);
  sigprocmask (SIG_BLOCK, &emptyset, &mainsigset);
  /* Provoke two stack overflows in a row.  */
  if (setjmp (mainloop) < 2)
    {
      recurse (0);
      exit (2);
    }
  exit (0);
}]])],
      [$2=yes],
      [$2=no],
      [case "$host" in
         m4_if([$3], [], [], [[$3]) $2=yes ;;])
         *) $2="guessing no" ;;
       esac
      ])
  ])
])
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>

#if HAVE_SETRLIMIT
# include <sys/types.h>
# include <sys/time.h>
# include <sys/resource.h>
#endif
#ifndef SIGSTKSZ
# define SIGSTKSZ 16384
#endif
jmp_buf mainloop;
sigset_t mainsigset;
int pass = 0;
void stackoverflow_handler (int sig)
{
  pass++;
  sigprocmask (SIG_SETMASK, &mainsigset, NULL);

  longjmp (mainloop, pass);
}
volatile int * recurse_1 (volatile int n, volatile int *p)
{
  if (n >= 0)
    *recurse_1 (n + 1, p) += n;
  return p;
}
int recurse (volatile int n)
{
  int sum = 0;
  return *recurse_1 (n, &sum);
}
char mystack[2 * SIGSTKSZ];
int main ()
{
  stack_t altstack;
  struct sigaction action;
  sigset_t emptyset;
#if defined HAVE_SETRLIMIT && defined RLIMIT_STACK
  /* Before starting the endless recursion, try to be friendly to the user's
     machine.  On some Linux 2.2.x systems, there is no stack limit for user
     processes at all.  We don't want to kill such systems.  */
  struct rlimit rl;
  rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
  setrlimit (RLIMIT_STACK, &rl);
#endif
  /* Install the alternate stack.  Use the midpoint of mystack, to guard
     against a buggy interpretation of ss_sp on IRIX.  */
  altstack.ss_sp = mystack + SIGSTKSZ;
  altstack.ss_size = SIGSTKSZ;
  altstack.ss_flags = 0; /* no SS_DISABLE */
  if (sigaltstack (&altstack, NULL) < 0)
    exit (1);
  /* Install the SIGSEGV handler.  */
  sigemptyset (&action.sa_mask);
  action.sa_handler = &stackoverflow_handler;
  action.sa_flags = SA_ONSTACK;
  sigaction (SIGSEGV, &action, (struct sigaction *) NULL);
  sigaction (SIGBUS, &action, (struct sigaction *) NULL);
  /* Save the current signal mask.  */
  sigemptyset (&emptyset);
  sigprocmask (SIG_BLOCK, &emptyset, &mainsigset);
  /* Provoke two stack overflows in a row.  */
  if (setjmp (mainloop) < 2)
    {
      recurse (0);
      exit (2);
    }
  exit (0);
}


Home | Main Index | Thread Index | Old Index