Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I install a signal handler for an access violation error on Windows, in C?

Tags:

c

windows

winapi

I have a bad application raising an access violation on windows. This pops up the "crash dialog" on windows and I don't want that to appear on the user's computers. On Linux, I would install a signal handler for SIGSEGV, and just exit() in the sighandler function, but I am on Windows, and I know close to nothing of the Windows API.

As far as I understand, Windows throws an exception ACCESS_VIOLATION when a segfault happens. I assume it's a regular C++ exception and one can catch it, but the program I need to patch is in C, not C++. How does one install a "signal handler" on windows? (assuming the concept of signal exists, considering that signal() and friends are a POSIX API)? Is this API implemented as a core API, or is it part of a POSIX compatibility layer that may not be present on a vanilla deployment?

I am working with VS2008 on Win7

like image 723
Stefano Borini Avatar asked Oct 31 '14 14:10

Stefano Borini


People also ask

How do I fix access violation at my address Windows 10?

Reinstall the Problematic Software If you get the Access Violation at Address error when running a certain application, try reinstalling the affected program. There is a good chance that parts of the app you are trying to run have been damaged from crashes or improper saves. Perhaps, a buggy update has caused issues.

What is access violation error?

An access violation is a non-specific error that occurs while installing, loading, or playing a game. This error can be caused by the following: an interfering software program (usually an antivirus application), an outdated video card driver, or an outdated version of DirectX.

Can you catch an access violation?

Recovering from access violation may be possible. Recovering from EIP jump voilation is never possible unless you are dodgy and keep assembly level instruction pointers. However, catching Access violation is good for spawning another process for bug reporting GUI feature.

Does signal () call the signal handler?

signal() sets the disposition of the signal signum to handler, which is either SIG_IGN, SIG_DFL, or the address of a programmer- defined function (a "signal handler"). If the signal signum is delivered to the process, then one of the following happens: * If the disposition is set to SIG_IGN, then the signal is ignored.


1 Answers

It's not1 a "regular C++ exception". It's an OS trap.

On Linux and other Unix-like systems, OS traps are called "signals".

On Windows, they are called "Structured Exceptions". You'll find a ton on information online about SEH, which stands for structured exception handling. The official documentation is on MSDN

Windows C and C++ compilers have special keywords for working with structured exceptions

__try {
}
__except( MyExceptionFilter(GetExceptionCode(), GetExceptionInfo()) ) {
}

Inside the exception filter, you can inspect the exception code and discriminate between access violations, divide by zero, floating point signals, etc. You can find out where the exception happened and log a stack trace (with some extra work). And you can choose whether or not the handler code block runs.

Underneath, these keywords get translated into code that sets up trap handlers, using data structures like vectored exception handler tables. But you really want the compiler's help rather than doing it yourself. Seeing the internal implementation is only of interest to compiler developers or if something goes wrong and you have to debug through it.


1Depending on your compile options, in Visual C++ it's the /EHa and /EHs options, C++ exceptions might be built on top of SEH (there's one particular exception code which means "Microsoft C++ exception", and a pointer to the C++ exception object is stored in the SEH parameters). When that happens, stack unwinding of C++ try/catch and SEH __try/__except/__finally is unified -- both exceptions unwind through both handler styles. The __set_se_translator function is also interesting to people using both C++ exception and SEH in the same program.

like image 127
Ben Voigt Avatar answered Nov 15 '22 03:11

Ben Voigt