mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-23 05:46:11 +01:00
Significantly refactor fatal error handling
Because new glibc has changed `MINSIGSTKSZ` to be a syscall instead of being constant, the signal posix handling needed changes, as it used the value in constexpr context, for deciding size of an array. It would be simple to fix it by having the handler determine the signal handling stack size and allocate the memory every time the handler is being installed, but that would add another allocation and a syscall every time a test case is entered. Instead, I split apart the idea of preparing fatal error handlers, and engaging them, so that the memory can be allocated only once and still be guarded by RAII. Also turns out that Catch2's use of `MINSIGSTKSZ` was wrong, and we should've been using `SIGSTKSZ` the whole time, which we use now. Closes #2178
This commit is contained in:
parent
4cb3220a8a
commit
8f277a54c0
@ -7,30 +7,72 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/** \file
|
||||||
|
* This file provides platform specific implementations of FatalConditionHandler
|
||||||
|
*
|
||||||
|
* This means that there is a lot of conditional compilation, and platform
|
||||||
|
* specific code. Currently, Catch2 supports a dummy handler (if no
|
||||||
|
* handler is desired), and 2 platform specific handlers:
|
||||||
|
* * Windows' SEH
|
||||||
|
* * POSIX signals
|
||||||
|
*
|
||||||
|
* Consequently, various pieces of code below are compiled if either of
|
||||||
|
* the platform specific handlers is enabled, or if none of them are
|
||||||
|
* enabled. It is assumed that both cannot be enabled at the same time,
|
||||||
|
* and doing so should cause a compilation error.
|
||||||
|
*
|
||||||
|
* If another platform specific handler is added, the compile guards
|
||||||
|
* below will need to be updated taking these assumptions into account.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "catch_fatal_condition.h"
|
#include "catch_fatal_condition.h"
|
||||||
|
|
||||||
#include "catch_context.h"
|
#include "catch_context.h"
|
||||||
#include "catch_interfaces_capture.h"
|
#include "catch_enforce.h"
|
||||||
|
#include "catch_run_context.h"
|
||||||
|
#include "catch_windows_h_proxy.h"
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#include <algorithm>
|
||||||
# pragma GCC diagnostic push
|
|
||||||
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
#if !defined( CATCH_CONFIG_WINDOWS_SEH ) && !defined( CATCH_CONFIG_POSIX_SIGNALS )
|
||||||
#endif
|
|
||||||
|
namespace Catch {
|
||||||
|
|
||||||
|
// If neither SEH nor signal handling is required, the handler impls
|
||||||
|
// do not have to do anything, and can be empty.
|
||||||
|
FatalConditionHandler::engage_platform() {}
|
||||||
|
FatalConditionHandler::disengage_platform() {}
|
||||||
|
FatalConditionHandler::FatalConditionHandler() = default;
|
||||||
|
FatalConditionHandler::~FatalConditionHandler() = default;
|
||||||
|
|
||||||
|
} // end namespace Catch
|
||||||
|
|
||||||
|
#endif // !CATCH_CONFIG_WINDOWS_SEH && !CATCH_CONFIG_POSIX_SIGNALS
|
||||||
|
|
||||||
|
#if defined( CATCH_CONFIG_WINDOWS_SEH ) && defined( CATCH_CONFIG_POSIX_SIGNALS )
|
||||||
|
#error "Inconsistent configuration: Windows' SEH handling and POSIX signals cannot be enabled at the same time"
|
||||||
|
#endif // CATCH_CONFIG_WINDOWS_SEH && CATCH_CONFIG_POSIX_SIGNALS
|
||||||
|
|
||||||
#if defined( CATCH_CONFIG_WINDOWS_SEH ) || defined( CATCH_CONFIG_POSIX_SIGNALS )
|
#if defined( CATCH_CONFIG_WINDOWS_SEH ) || defined( CATCH_CONFIG_POSIX_SIGNALS )
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
// Report the error condition
|
//! Signals fatal error message to the run context
|
||||||
void reportFatal( char const * const message ) {
|
void reportFatal( char const * const message ) {
|
||||||
Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( message );
|
Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( message );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#endif // signals/SEH handling
|
//! Minimal size Catch2 needs for its own fatal error handling.
|
||||||
|
//! Picked anecdotally, so it might not be sufficient on all
|
||||||
|
//! platforms, and for all configurations.
|
||||||
|
constexpr std::size_t minStackSizeForErrors = 32 * 1024;
|
||||||
|
} // end unnamed namespace
|
||||||
|
|
||||||
|
#endif // CATCH_CONFIG_WINDOWS_SEH || CATCH_CONFIG_POSIX_SIGNALS
|
||||||
|
|
||||||
#if defined( CATCH_CONFIG_WINDOWS_SEH )
|
#if defined( CATCH_CONFIG_WINDOWS_SEH )
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
struct SignalDefs { DWORD id; const char* name; };
|
struct SignalDefs { DWORD id; const char* name; };
|
||||||
|
|
||||||
// There is no 1-1 mapping between signals and windows exceptions.
|
// There is no 1-1 mapping between signals and windows exceptions.
|
||||||
@ -43,7 +85,7 @@ namespace Catch {
|
|||||||
{ static_cast<DWORD>(EXCEPTION_INT_DIVIDE_BY_ZERO), "Divide by zero error" },
|
{ static_cast<DWORD>(EXCEPTION_INT_DIVIDE_BY_ZERO), "Divide by zero error" },
|
||||||
};
|
};
|
||||||
|
|
||||||
LONG CALLBACK FatalConditionHandler::handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) {
|
static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) {
|
||||||
for (auto const& def : signalDefs) {
|
for (auto const& def : signalDefs) {
|
||||||
if (ExceptionInfo->ExceptionRecord->ExceptionCode == def.id) {
|
if (ExceptionInfo->ExceptionRecord->ExceptionCode == def.id) {
|
||||||
reportFatal(def.name);
|
reportFatal(def.name);
|
||||||
@ -54,39 +96,52 @@ namespace Catch {
|
|||||||
return EXCEPTION_CONTINUE_SEARCH;
|
return EXCEPTION_CONTINUE_SEARCH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
|
||||||
|
// For MSVC, we reserve part of the stack memory for handling
|
||||||
|
// memory overflow structured exception.
|
||||||
FatalConditionHandler::FatalConditionHandler() {
|
FatalConditionHandler::FatalConditionHandler() {
|
||||||
isSet = true;
|
ULONG guaranteeSize = static_cast<ULONG>(minStackSizeForErrors);
|
||||||
// 32k seems enough for Catch to handle stack overflow,
|
if (!SetThreadStackGuarantee(&guaranteeSize)) {
|
||||||
// but the value was found experimentally, so there is no strong guarantee
|
// We do not want to fully error out, because needing
|
||||||
guaranteeSize = 32 * 1024;
|
// the stack reserve should be rare enough anyway.
|
||||||
exceptionHandlerHandle = nullptr;
|
Catch::cerr()
|
||||||
|
<< "Failed to reserve piece of stack."
|
||||||
|
<< " Stack overflows will not be reported successfully.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We do not attempt to unset the stack guarantee, because
|
||||||
|
// Windows does not support lowering the stack size guarantee.
|
||||||
|
FatalConditionHandler::~FatalConditionHandler() = default;
|
||||||
|
|
||||||
|
|
||||||
|
void FatalConditionHandler::engage_platform() {
|
||||||
// Register as first handler in current chain
|
// Register as first handler in current chain
|
||||||
exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException);
|
exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException);
|
||||||
// Pass in guarantee size to be filled
|
if (!exceptionHandlerHandle) {
|
||||||
SetThreadStackGuarantee(&guaranteeSize);
|
CATCH_RUNTIME_ERROR("Could not register vectored exception handler");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FatalConditionHandler::reset() {
|
void FatalConditionHandler::disengage_platform() {
|
||||||
if (isSet) {
|
if (!RemoveVectoredExceptionHandler(exceptionHandlerHandle)) {
|
||||||
RemoveVectoredExceptionHandler(exceptionHandlerHandle);
|
CATCH_RUNTIME_ERROR("Could not unregister vectored exception handler");
|
||||||
SetThreadStackGuarantee(&guaranteeSize);
|
}
|
||||||
exceptionHandlerHandle = nullptr;
|
exceptionHandlerHandle = nullptr;
|
||||||
isSet = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FatalConditionHandler::~FatalConditionHandler() {
|
} // end namespace Catch
|
||||||
reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool FatalConditionHandler::isSet = false;
|
#endif // CATCH_CONFIG_WINDOWS_SEH
|
||||||
ULONG FatalConditionHandler::guaranteeSize = 0;
|
|
||||||
PVOID FatalConditionHandler::exceptionHandlerHandle = nullptr;
|
|
||||||
|
|
||||||
|
#if defined( CATCH_CONFIG_POSIX_SIGNALS )
|
||||||
|
|
||||||
} // namespace Catch
|
#include <signal.h>
|
||||||
|
|
||||||
#elif defined( CATCH_CONFIG_POSIX_SIGNALS )
|
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
@ -95,10 +150,6 @@ namespace Catch {
|
|||||||
const char* name;
|
const char* name;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 32kb for the alternate stack seems to be sufficient. However, this value
|
|
||||||
// is experimentally determined, so that's not guaranteed.
|
|
||||||
static constexpr std::size_t sigStackSize = 32768 >= MINSIGSTKSZ ? 32768 : MINSIGSTKSZ;
|
|
||||||
|
|
||||||
static SignalDefs signalDefs[] = {
|
static SignalDefs signalDefs[] = {
|
||||||
{ SIGINT, "SIGINT - Terminal interrupt signal" },
|
{ SIGINT, "SIGINT - Terminal interrupt signal" },
|
||||||
{ SIGILL, "SIGILL - Illegal instruction signal" },
|
{ SIGILL, "SIGILL - Illegal instruction signal" },
|
||||||
@ -108,8 +159,32 @@ namespace Catch {
|
|||||||
{ SIGABRT, "SIGABRT - Abort (abnormal termination) signal" }
|
{ SIGABRT, "SIGABRT - Abort (abnormal termination) signal" }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Older GCCs trigger -Wmissing-field-initializers for T foo = {}
|
||||||
|
// which is zero initialization, but not explicit. We want to avoid
|
||||||
|
// that.
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
# pragma GCC diagnostic push
|
||||||
|
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
||||||
|
#endif
|
||||||
|
|
||||||
void FatalConditionHandler::handleSignal( int sig ) {
|
static char* altStackMem = nullptr;
|
||||||
|
static std::size_t altStackSize = 0;
|
||||||
|
static stack_t oldSigStack{};
|
||||||
|
static struct sigaction oldSigActions[sizeof(signalDefs) / sizeof(SignalDefs)]{};
|
||||||
|
|
||||||
|
static void restorePreviousSignalHandlers() {
|
||||||
|
// We set signal handlers back to the previous ones. Hopefully
|
||||||
|
// nobody overwrote them in the meantime, and doesn't expect
|
||||||
|
// their signal handlers to live past ours given that they
|
||||||
|
// installed them after ours..
|
||||||
|
for (std::size_t i = 0; i < sizeof(signalDefs) / sizeof(SignalDefs); ++i) {
|
||||||
|
sigaction(signalDefs[i].id, &oldSigActions[i], nullptr);
|
||||||
|
}
|
||||||
|
// Return the old stack
|
||||||
|
sigaltstack(&oldSigStack, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handleSignal( int sig ) {
|
||||||
char const * name = "<unknown signal>";
|
char const * name = "<unknown signal>";
|
||||||
for (auto const& def : signalDefs) {
|
for (auto const& def : signalDefs) {
|
||||||
if (sig == def.id) {
|
if (sig == def.id) {
|
||||||
@ -117,16 +192,33 @@ namespace Catch {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reset();
|
// We need to restore previous signal handlers and let them do
|
||||||
|
// their thing, so that the users can have the debugger break
|
||||||
|
// when a signal is raised, and so on.
|
||||||
|
restorePreviousSignalHandlers();
|
||||||
reportFatal( name );
|
reportFatal( name );
|
||||||
raise( sig );
|
raise( sig );
|
||||||
}
|
}
|
||||||
|
|
||||||
FatalConditionHandler::FatalConditionHandler() {
|
FatalConditionHandler::FatalConditionHandler() {
|
||||||
isSet = true;
|
assert(!altStackMem && "Cannot initialize POSIX signal handler when one already exists");
|
||||||
|
if (altStackSize == 0) {
|
||||||
|
altStackSize = std::max(static_cast<size_t>(SIGSTKSZ), minStackSizeForErrors);
|
||||||
|
}
|
||||||
|
altStackMem = new char[altStackSize]();
|
||||||
|
}
|
||||||
|
|
||||||
|
FatalConditionHandler::~FatalConditionHandler() {
|
||||||
|
delete[] altStackMem;
|
||||||
|
// We signal that another instance can be constructed by zeroing
|
||||||
|
// out the pointer.
|
||||||
|
altStackMem = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FatalConditionHandler::engage_platform() {
|
||||||
stack_t sigStack;
|
stack_t sigStack;
|
||||||
sigStack.ss_sp = altStackMem;
|
sigStack.ss_sp = altStackMem;
|
||||||
sigStack.ss_size = sigStackSize;
|
sigStack.ss_size = altStackSize;
|
||||||
sigStack.ss_flags = 0;
|
sigStack.ss_flags = 0;
|
||||||
sigaltstack(&sigStack, &oldSigStack);
|
sigaltstack(&sigStack, &oldSigStack);
|
||||||
struct sigaction sa = { };
|
struct sigaction sa = { };
|
||||||
@ -138,39 +230,15 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FatalConditionHandler::~FatalConditionHandler() {
|
|
||||||
reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
void FatalConditionHandler::reset() {
|
|
||||||
if( isSet ) {
|
|
||||||
// Set signals back to previous values -- hopefully nobody overwrote them in the meantime
|
|
||||||
for( std::size_t i = 0; i < sizeof(signalDefs)/sizeof(SignalDefs); ++i ) {
|
|
||||||
sigaction(signalDefs[i].id, &oldSigActions[i], nullptr);
|
|
||||||
}
|
|
||||||
// Return the old stack
|
|
||||||
sigaltstack(&oldSigStack, nullptr);
|
|
||||||
isSet = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool FatalConditionHandler::isSet = false;
|
|
||||||
struct sigaction FatalConditionHandler::oldSigActions[sizeof(signalDefs)/sizeof(SignalDefs)] = {};
|
|
||||||
stack_t FatalConditionHandler::oldSigStack = {};
|
|
||||||
char FatalConditionHandler::altStackMem[sigStackSize] = {};
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace Catch
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
namespace Catch {
|
|
||||||
void FatalConditionHandler::reset() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // signals/SEH handling
|
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
# pragma GCC diagnostic pop
|
# pragma GCC diagnostic pop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
void FatalConditionHandler::disengage_platform() {
|
||||||
|
restorePreviousSignalHandlers();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end namespace Catch
|
||||||
|
|
||||||
|
#endif // CATCH_CONFIG_POSIX_SIGNALS
|
||||||
|
@ -11,59 +11,60 @@
|
|||||||
|
|
||||||
#include "catch_platform.h"
|
#include "catch_platform.h"
|
||||||
#include "catch_compiler_capabilities.h"
|
#include "catch_compiler_capabilities.h"
|
||||||
#include "catch_windows_h_proxy.h"
|
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#if defined( CATCH_CONFIG_WINDOWS_SEH )
|
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
struct FatalConditionHandler {
|
/**
|
||||||
|
* Wrapper for platform-specific fatal error (signals/SEH) handlers
|
||||||
static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo);
|
*
|
||||||
FatalConditionHandler();
|
* Tries to be cooperative with other handlers, and not step over
|
||||||
static void reset();
|
* other handlers. This means that unknown structured exceptions
|
||||||
~FatalConditionHandler();
|
* are passed on, previous signal handlers are called, and so on.
|
||||||
|
*
|
||||||
private:
|
* Can only be instantiated once, and assumes that once a signal
|
||||||
static bool isSet;
|
* is caught, the binary will end up terminating. Thus, there
|
||||||
static ULONG guaranteeSize;
|
*/
|
||||||
static PVOID exceptionHandlerHandle;
|
class FatalConditionHandler {
|
||||||
};
|
bool m_started = false;
|
||||||
|
|
||||||
} // namespace Catch
|
|
||||||
|
|
||||||
#elif defined ( CATCH_CONFIG_POSIX_SIGNALS )
|
|
||||||
|
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
namespace Catch {
|
|
||||||
|
|
||||||
struct FatalConditionHandler {
|
|
||||||
|
|
||||||
static bool isSet;
|
|
||||||
static struct sigaction oldSigActions[];
|
|
||||||
static stack_t oldSigStack;
|
|
||||||
static char altStackMem[];
|
|
||||||
|
|
||||||
static void handleSignal( int sig );
|
|
||||||
|
|
||||||
|
// Install/disengage implementation for specific platform.
|
||||||
|
// Should be if-defed to work on current platform, can assume
|
||||||
|
// engage-disengage 1:1 pairing.
|
||||||
|
void engage_platform();
|
||||||
|
void disengage_platform();
|
||||||
|
public:
|
||||||
|
// Should also have platform-specific implementations as needed
|
||||||
FatalConditionHandler();
|
FatalConditionHandler();
|
||||||
~FatalConditionHandler();
|
~FatalConditionHandler();
|
||||||
static void reset();
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Catch
|
void engage() {
|
||||||
|
assert(!m_started && "Handler cannot be installed twice.");
|
||||||
|
m_started = true;
|
||||||
#else
|
engage_platform();
|
||||||
|
|
||||||
namespace Catch {
|
|
||||||
struct FatalConditionHandler {
|
|
||||||
void reset();
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
void disengage() {
|
||||||
|
assert(m_started && "Handler cannot be uninstalled without being installed first");
|
||||||
|
m_started = false;
|
||||||
|
disengage_platform();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Simple RAII guard for (dis)engaging the FatalConditionHandler
|
||||||
|
class FatalConditionHandlerGuard {
|
||||||
|
FatalConditionHandler* m_handler;
|
||||||
|
public:
|
||||||
|
FatalConditionHandlerGuard(FatalConditionHandler* handler):
|
||||||
|
m_handler(handler) {
|
||||||
|
m_handler->engage();
|
||||||
|
}
|
||||||
|
~FatalConditionHandlerGuard() {
|
||||||
|
m_handler->disengage();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // end namespace Catch
|
||||||
|
|
||||||
#endif // TWOBLUECUBES_CATCH_FATAL_CONDITION_H_INCLUDED
|
#endif // TWOBLUECUBES_CATCH_FATAL_CONDITION_H_INCLUDED
|
||||||
|
@ -452,9 +452,8 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RunContext::invokeActiveTestCase() {
|
void RunContext::invokeActiveTestCase() {
|
||||||
FatalConditionHandler fatalConditionHandler; // Handle signals
|
FatalConditionHandlerGuard _(&m_fatalConditionhandler);
|
||||||
m_activeTestCase->invoke();
|
m_activeTestCase->invoke();
|
||||||
fatalConditionHandler.reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RunContext::handleUnfinishedSections() {
|
void RunContext::handleUnfinishedSections() {
|
||||||
|
@ -146,6 +146,7 @@ namespace Catch {
|
|||||||
std::vector<SectionEndInfo> m_unfinishedSections;
|
std::vector<SectionEndInfo> m_unfinishedSections;
|
||||||
std::vector<ITracker*> m_activeSections;
|
std::vector<ITracker*> m_activeSections;
|
||||||
TrackerContext m_trackerContext;
|
TrackerContext m_trackerContext;
|
||||||
|
FatalConditionHandler m_fatalConditionhandler;
|
||||||
bool m_lastAssertionPassed = false;
|
bool m_lastAssertionPassed = false;
|
||||||
bool m_shouldReportUnexpected = true;
|
bool m_shouldReportUnexpected = true;
|
||||||
bool m_includeSuccessfulResults;
|
bool m_includeSuccessfulResults;
|
||||||
|
Loading…
Reference in New Issue
Block a user