Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching access violations on Windows

I am trying to catch all unhandled exceptions in my application so I can save a log file when they occurr. This is a 64-bit Windows application compiled using Visual Studio 2013, written in C++. For testing I am using the default C++ Win32 project generated by VS.

I am catching all exceptions by registering a handler using SetUnhandledExceptionFilter. This works fine for /most/ cases, but not all. All throw()-n exceptions are caught, and most hardware exceptions like floating point or access violations as well. The code that doesn't trigger the handler is:

std::vector<int> foo(5, 0);
for (auto& f : foo)
    foo.erase(foo.begin() + 1);

Instead I just get the standard windows crash dialog box, without my exception handler getting called. If I run it in Visual Studio with debugger attached however, it correctly reports an access violation exception. Other types of access violations trigger the handler as well:

float* ptr = nullptr;
float value = *ptr;

Code above triggers the exception handler.

I have tried using try/catch or catching SIGSEGV signal too, but neither get triggered with the first example. abort/terminate signals don't get called either. In short I am in no way notified when that crash happens.

I want to know is there any way I can get some kind of a notification in my application before it crashes due to the access violation caused by the first example? Since VS seems to be able to detect it I'm assuming there's a way.

EDIT: I just want to make it clear I'm running the code in release mode and the error in the first example isn't caused by iterator out of bounds check done in debug mode.

EDIT2: I included the simplest example I can come up with using a win32 console app. See it here: http://pastebin.com/8L1SN5PQ

Make sure to run it in release mode with no debugger attached.

like image 864
Uberyon Avatar asked Oct 09 '15 11:10

Uberyon


People also ask

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.

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 causes a read access violation?

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.


1 Answers

These kind of runtime errors are handled differently, they don't produce an SEH exception. Roughly classified somewhere between "programming bug" and "malware attack". And about as informative as a uncaught C++ exception if you don't have a debugger attached, the default handler invokes instant death with __fastfail().

You'll have to call _set_invalid_parameter_handler() in your main() function to alter the way they are handled. You could throw a C++ exception in your custom handler or call RaiseException() to trigger your catch-em-all handler or just report them right there. Favor the latter, you want to ensure that the process is always terminated.

Do beware that your snippet is not the best possible example. This is UB when you build your program without iterator debugging enabled, like the Release build with default settings. UB does not guarantee you'll get an SEH exception. And if it does then you'll have to write your exception filter very carefully, it will be called with the heap lock still taken so basic stuff cannot work. Best way is to wakeup a guard process with a named event. Which then takes a minidump and terminates the program.

like image 156
Hans Passant Avatar answered Sep 30 '22 13:09

Hans Passant