Thursday, June 2, 2016

coredumping with invalid stack

Today have a problem on Linux:
you have sighandler on SIGSEGV like
signal(SIGSEGV, myhandle)
void myhandle(int) {
// generate core-dump
}
if in code you overflowed process stack-size or corrupted stack
myhandle() will be processed on faulty stack and
 with big probability your process will be finished with 'Segmentation fault (coredump)'

So the solution is  to execute your sighandler on alternate stack:
    stack_t myaltstack;
    myaltstack.ss_sp = malloc(1024*1024);
    myaltstack.ss_flags = 0;
    myaltstack.ss_size = 1024*1024;
    sigaltstack(&myaltstack, NULL);

    struct sigaction myaction;
    memset(&myaction, 0, sizeof(myaction));
    myaction.sa_sigaction = mysighandler_long;
    myaction.sa_flags = SA_SIGINFO | SA_ONSTACK;
    sigaction(SIGSEGV, &myaction, NULL);

No comments:

Post a Comment