This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* Written by Paul Eggert. */
/* This module assumes that each stack frame is smaller than a page.
If you use alloca, dynamic arrays, or large local variables, your
program may extend the stack by more than a page at a time. If so,
the code below may incorrectly report a program error, or worse
yet, may not detect the overflow at all. To avoid this problem,
don't use large local arrays. */
/* Storage for the alternate signal stack. */
static union
{
char buffer[SIGSTKSZ];
/* These other members are for proper alignment. There's no
standard way to guarantee stack alignment, but this seems enough
in practice. */
long double ld;
uintmax_t u;
void *p;
} alternate_signal_stack;
/* Direction of the C runtime stack. This function is
async-signal-safe. */
/* Handle a segmentation violation and exit. This function is
async-signal-safe. */
static void
segv_handler (int signo, siginfo_t *info, void *context)
{
/* Clear SIGNO if it seems to have been a stack overflow. */
if (0 < info->si_code)
{
/* If the faulting address is within the stack, or within one
page of the stack end, assume that it is a stack
overflow. */
ucontext_t const *user_context = context;
char const *stack_min = user_context->uc_stack.ss_sp;
size_t stack_size = user_context->uc_stack.ss_size;
char const *faulting_address = info->si_addr;
size_t s = faulting_address - stack_min;
size_t page_size = sysconf (_SC_PAGESIZE);
if (find_stack_direction (0) < 0)
s += page_size;
if (s < stack_size + page_size)
signo = 0;
}
segv_action (signo, info, context);
}
#endif /* HAVE_XSI_STACK_OVERFLOW_HEURISTIC */
/* Translated messages for program errors and stack overflow. Do not
translate them in the signal handler, since gettext is not
async-signal-safe. */
static char const * volatile program_error_message;
static char const * volatile stack_overflow_message;
/* Output an error message, then exit with status EXIT_FAILURE if it
appears to have been a stack overflow, or with a core dump
otherwise. This function is async-signal-safe. */
/* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but this
is not true on Solaris 8 at least. It doesn't hurt to use
SA_NODEFER here, so leave it in. */
act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;