Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorAlan Jowett2021-12-15 09:51:04 -0700
committerMartin Hořeňovský2022-01-03 22:50:32 +0100
commit98a6c69e1e429a86140301dfa5563b1d7263e813 (patch)
treeb42e769a5151387098628f37d1739ac05047436c /src
parentd3199c42c2af0f69e43686c2b47189c8bad04026 (diff)
downloadcatch3-98a6c69e1e429a86140301dfa5563b1d7263e813.tar.gz
Switch from AddVectoredExceptionHandler to SetUnhandledExceptionFilter
This avoids issues with Catch2's handler firing too early, on structured exceptions that would be handled later. This issue meant that the old attempts at structured exception handling were incompatible with Windows's ASan, because it throws continuable `C0000005` exception, which it then handles. With the new handling, Catch2 is only notified if nothing else, including the debugger, has handled the exception. Signed-off-by: Alan Jowett <alanjo@microsoft.com> Closes #2332 Closes #2286 Closes #898
Diffstat (limited to 'src')
-rw-r--r--src/catch2/internal/catch_fatal_condition_handler.cpp17
1 files changed, 7 insertions, 10 deletions
diff --git a/src/catch2/internal/catch_fatal_condition_handler.cpp b/src/catch2/internal/catch_fatal_condition_handler.cpp
index 9383257c..7d3534ca 100644
--- a/src/catch2/internal/catch_fatal_condition_handler.cpp
+++ b/src/catch2/internal/catch_fatal_condition_handler.cpp
@@ -84,7 +84,7 @@ namespace Catch {
{ static_cast<DWORD>(EXCEPTION_INT_DIVIDE_BY_ZERO), "Divide by zero error" },
};
- static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) {
+ static LONG CALLBACK topLevelExceptionFilter(PEXCEPTION_POINTERS ExceptionInfo) {
for (auto const& def : signalDefs) {
if (ExceptionInfo->ExceptionRecord->ExceptionCode == def.id) {
reportFatal(def.name);
@@ -98,7 +98,7 @@ namespace Catch {
// Since we do not support multiple instantiations, we put these
// into global variables and rely on cleaning them up in outlined
// constructors/destructors
- static PVOID exceptionHandlerHandle = nullptr;
+ static LPTOP_LEVEL_EXCEPTION_FILTER previousTopLevelExceptionFilter = nullptr;
// For MSVC, we reserve part of the stack memory for handling
@@ -120,18 +120,15 @@ namespace Catch {
void FatalConditionHandler::engage_platform() {
- // Register as first handler in current chain
- exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException);
- if (!exceptionHandlerHandle) {
- CATCH_RUNTIME_ERROR("Could not register vectored exception handler");
- }
+ // Register as a the top level exception filter.
+ previousTopLevelExceptionFilter = SetUnhandledExceptionFilter(topLevelExceptionFilter);
}
void FatalConditionHandler::disengage_platform() {
- if (!RemoveVectoredExceptionHandler(exceptionHandlerHandle)) {
- CATCH_RUNTIME_ERROR("Could not unregister vectored exception handler");
+ if (SetUnhandledExceptionFilter(reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>(previousTopLevelExceptionFilter)) != topLevelExceptionFilter) {
+ CATCH_RUNTIME_ERROR("Could not restore previous top level exception filter");
}
- exceptionHandlerHandle = nullptr;
+ previousTopLevelExceptionFilter = nullptr;
}
} // end namespace Catch