Jeroen Demeyer on Thu, 22 Feb 2018 16:10:06 +0100


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

Re: mfinit --> Illegal instruction: 4


On this system:

Darwin osx 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64

In some other application I am seeing a SIGILL when the stack overflows after setting up sigaltstack and then forking. This looks like an OS X bug which might relate to this reported PARI/GP crash.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>

static void handler(int sig)
{
    fprintf(stderr, "got signal %i\n", sig);
    fflush(stderr);
    _exit(0);
}

volatile int yes;
static void stack_overflow(void)
{
    yes = 1;
    if (yes) stack_overflow();
    if (yes) stack_overflow();
}

int main(int argc, char** argv)
{
    static char alt_stack_space[1 << 16];
    stack_t ss;
    ss.ss_sp = alt_stack_space;
    ss.ss_size = sizeof(alt_stack_space);
    ss.ss_flags = 0;
    if (sigaltstack(&ss, NULL)) {perror("sigaltstack"); exit(1);}

    struct sigaction sa;
    sa.sa_handler = handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_ONSTACK;
    if (sigaction(SIGSEGV, &sa, NULL)) {perror("sigaction"); exit(1);}
    if (sigaction(SIGBUS, &sa, NULL)) {perror("sigaction"); exit(1);}
    if (sigaction(SIGILL, &sa, NULL)) {perror("sigaction"); exit(1);}

    pid_t child = fork();

    if (!child)
    {
        /* Child process */
        stack_overflow();
    }

    int status;
    if (wait(&status) != child) {fputs("wait() did not return child\n", stderr); exit(1);}

    printf("status = %i\n", status);
    return 0;
}