Exceptional shell for Carbon C application on OSX

How can I effectively catch and handle segmentation errors with C in an OSX Carbon app?

Reference Information. I am making an OSX Carbon app. I have to call the library function from a third party. Due to problems with threads, a function can sometimes crash, usually because it is updated from one thread itself, and it gets some internally obsolete pointer or handle when I request it from another. Function is a black box for me. I want to be able to call a function, but to be able to "catch" if it crashed and provided an alternative return. On Windows, I can use the simple Visual C compiler and Intel C __try {} and __except.

/* Working Windows Example */
__try { x=DangerousFunction(y);}
__except(EXCEPTION_EXECUTE_HANDLER) {x=0.0;} /* whups, func crashed! */

I am trying to do the same kind of crash for OSX. I use pure C in a very large application. I call the function millions of times per second, so efficiency is also very important. (Impressive that Windows __try () overhead is immeasurably small!)

Here is what I experimented with:

1) C ++ exceptions. I'm not sure if C ++ exceptions cause segfault to fail. And my application is currently C. I can try wrappers and #ifdefs to make it C ++, but there is a lot of work for the application, and I don't think C ++ exceptions will crash.

2) + setjump + longjmp. , ... . SEGV [ !], . ( ) (SEGV). , , . , CFM BSD, , Mach Real Thing.

3) MPSetExceptionHandler. . . , segfault.

+3
1

, SIGBUS, SIGSEGV?

SIGBUS, 0:

cristi:tmp diciu$ cat test.c

#include <signal.h>

static void sigac(int sig)
{
    printf("sig action here, signal is %d\n", sig);
    exit(1);
}

int main()
{
    (void)signal(SIGSEGV, sigac);
    (void)signal(SIGBUS, sigac);

    printf("Raising\n");
    strcpy(0, "aaksdjkajskd|");
}



cristi:tmp diciu$ ./a.out 
Raising
sig action here, signal is 10
+3

Source: https://habr.com/ru/post/1704268/


All Articles