Processor exception handling in C ++

Is there a cross-platform way to handle CPU exceptions, such as segmentation errors, or division by zero? Suppose I need to call some potentially unsafe functions (for example, from a include file) that can cause segfault or some other problems that I cannot check before executing it. I know that the standard C library has signal processing functions, but I don’t know how to use them to solve the problem in order to avoid program termination (I think I can’t just go to the location before performing the problem functions, or can I?). Under windows, I could use SEH exception handlers, but I cannot do it under Linux or any other OS. How about using my own exception handler to solve these problems, how much is it different between Windows / Linux? Would that be possible (via assembler - say, only on the x86 platform)?

I ask mainly out of curiosity, I am not trying to solve an existing problem (for now). Thanks

+7
source share
6 answers

libsigsegv is a cross-platform library for handling segmentation errors and. However, in the vast majority of cases when you find a segmentation error, the right thing is to stop execution as quickly as possible, and not try to recover it. A segfact usually indicates an error or damaged memory, and once you have a damaged memory, it is almost impossible to recover from it.

+5
source

The problem is that if the seg plugin fails, you cannot guarantee in what state your main program will work. Even if you can catch SIGSEGV (which I suppose you can), you would not have a good way to recover in your application.

What you need to do is start the plugin in the fork ed process so that in case of a failure your main program will not be reset either. For example, you can communicate between processes using a channel.

+2
source

This is not covered in standard C ++, however, regular desktop OS provides the opportunity for this. Windows has structured exception handling (SEH) for which appropriate compiler extensions are available, and POSIX provides signal processing.

As a rule, I would say that you should not catch processor exceptions - they occur only if your program is listening, and at this moment it is time to crack the debugger, and not continue.

You cannot use the same approach - even in assembler. These funds are provided by the OS - when the CPU throws an exception, it goes to the OS to decide what to do with it, and not in user mode. Not only that, but I would say that SEH and signal processing are easily different from each other to guarantee fundamentally different approaches when used in code, so a simple #ifdef will not cut it off.

setjmp and longjmp can only work for “signals” raised by user mode code, not the OS level.

+2
source

I don't think there is a true cross-platform solution.

under windows you can use _set_se_translator to translate SEH exceptions to C ++ exceptions.

See the following C ++ exception handling techniques for Linux on how to do the same in Linux

+2
source

No, there is no standard way. In C ++, such “processor exceptions” are manifestations of Undefined Behavior, that is, the C ++ standard does not indicate anything about its behavior or anything that happens after that. Even the concept of "segfault" does not exist in standard C ++. Calling the NULL pointer can cause the computer to crash, and, obviously, it remains to catch a little at this point.

C also did not solve this: SIGSEGV is not a standard C signal; This is a POSIX extension. For example, Windows does not have SIGSEGV.

+1
source

Signal handlers can correct program execution to some point; what exactly is allowed is documented on the manual page (7) .

There are implementations that will

  • go back to the instructions on failures from SIGSEGV handlers (this allows you to change the memory card and come back) and

  • go to the instruction following the error for SIGFPE (so that your signal handler should find the instruction and give the result)

Note that this is best defined in the implementation. The manual says that you should not rely on this. You have been warned. :)

0
source

All Articles