2020-05-11 21:52:15 +00:00
|
|
|
#include "error.hh"
|
2022-10-14 10:25:41 +00:00
|
|
|
#include "shared.hh"
|
2013-07-30 21:25:37 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
static void sigsegvHandler(int signo, siginfo_t * info, void * ctx)
|
|
|
|
{
|
|
|
|
/* Detect stack overflows by comparing the faulting address with
|
|
|
|
the stack pointer. Unfortunately, getting the stack pointer is
|
|
|
|
not portable. */
|
|
|
|
bool haveSP = true;
|
2014-10-14 13:11:57 +00:00
|
|
|
char * sp = 0;
|
2013-07-30 21:25:37 +00:00
|
|
|
#if defined(__x86_64__) && defined(REG_RSP)
|
2017-08-31 20:41:36 +00:00
|
|
|
sp = (char *) ((ucontext_t *) ctx)->uc_mcontext.gregs[REG_RSP];
|
2013-07-30 21:25:37 +00:00
|
|
|
#elif defined(REG_ESP)
|
2017-08-31 20:41:36 +00:00
|
|
|
sp = (char *) ((ucontext_t *) ctx)->uc_mcontext.gregs[REG_ESP];
|
2013-07-30 21:25:37 +00:00
|
|
|
#else
|
|
|
|
haveSP = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (haveSP) {
|
|
|
|
ptrdiff_t diff = (char *) info->si_addr - sp;
|
|
|
|
if (diff < 0) diff = -diff;
|
|
|
|
if (diff < 4096) {
|
2022-10-14 10:37:34 +00:00
|
|
|
nix::stackOverflowHandler(info, ctx);
|
2013-07-30 21:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore default behaviour (i.e. segfault and dump core). */
|
|
|
|
struct sigaction act;
|
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
act.sa_handler = SIG_DFL;
|
|
|
|
act.sa_flags = 0;
|
|
|
|
if (sigaction(SIGSEGV, &act, 0)) abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void detectStackOverflow()
|
|
|
|
{
|
|
|
|
#if defined(SA_SIGINFO) && defined (SA_ONSTACK)
|
|
|
|
/* Install a SIGSEGV handler to detect stack overflows. This
|
|
|
|
requires an alternative stack, otherwise the signal cannot be
|
|
|
|
delivered when we're out of stack space. */
|
|
|
|
stack_t stack;
|
2013-08-07 15:44:19 +00:00
|
|
|
stack.ss_size = 4096 * 4 + MINSIGSTKSZ;
|
2017-07-12 17:24:20 +00:00
|
|
|
static auto stackBuf = std::make_unique<std::vector<char>>(stack.ss_size);
|
|
|
|
stack.ss_sp = stackBuf->data();
|
2013-07-30 21:25:37 +00:00
|
|
|
if (!stack.ss_sp) throw Error("cannot allocate alternative stack");
|
|
|
|
stack.ss_flags = 0;
|
|
|
|
if (sigaltstack(&stack, 0) == -1) throw SysError("cannot set alternative stack");
|
|
|
|
|
|
|
|
struct sigaction act;
|
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
act.sa_sigaction = sigsegvHandler;
|
|
|
|
act.sa_flags = SA_SIGINFO | SA_ONSTACK;
|
|
|
|
if (sigaction(SIGSEGV, &act, 0))
|
2018-09-24 13:18:59 +00:00
|
|
|
throw SysError("resetting SIGSEGV");
|
2013-07-30 21:25:37 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-10-14 10:37:34 +00:00
|
|
|
std::function<void(siginfo_t * info, void * ctx)> stackOverflowHandler(defaultStackOverflowHandler);
|
|
|
|
|
|
|
|
void defaultStackOverflowHandler(siginfo_t * info, void * ctx) {
|
|
|
|
char msg[] = "error: stack overflow (possible infinite recursion)\n";
|
|
|
|
[[gnu::unused]] auto res = write(2, msg, strlen(msg));
|
|
|
|
_exit(1); // maybe abort instead?
|
|
|
|
}
|
2013-07-30 21:25:37 +00:00
|
|
|
|
|
|
|
}
|