#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ExitCodes.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/thread.h"
#include <cassert>
#include <mutex>
#include <setjmp.h>
usingnamespacellvm;
namespace {
struct CrashRecoveryContextImpl;
static LLVM_THREAD_LOCAL const CrashRecoveryContextImpl *CurrentContext;
struct CrashRecoveryContextImpl { … };
std::mutex &getCrashRecoveryContextMutex() { … }
static bool gCrashRecoveryEnabled = …;
static LLVM_THREAD_LOCAL const CrashRecoveryContext *IsRecoveringFromCrash;
}
static void installExceptionOrSignalHandlers();
static void uninstallExceptionOrSignalHandlers();
CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() = default;
CrashRecoveryContext::CrashRecoveryContext() { … }
CrashRecoveryContext::~CrashRecoveryContext() { … }
bool CrashRecoveryContext::isRecoveringFromCrash() { … }
CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { … }
void CrashRecoveryContext::Enable() { … }
void CrashRecoveryContext::Disable() { … }
void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup)
{ … }
void
CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) { … }
#if defined(_MSC_VER)
#include <windows.h>
static void installExceptionOrSignalHandlers() {}
static void uninstallExceptionOrSignalHandlers() {}
static int ExceptionFilter(_EXCEPTION_POINTERS *Except) {
const CrashRecoveryContextImpl *CRCI = CurrentContext;
if (!CRCI) {
CrashRecoveryContext::Disable();
return EXCEPTION_CONTINUE_SEARCH;
}
int RetCode = (int)Except->ExceptionRecord->ExceptionCode;
if ((RetCode & 0xF0000000) == 0xE0000000)
RetCode &= ~0xF0000000;
const_cast<CrashRecoveryContextImpl *>(CRCI)->HandleCrash(
RetCode, reinterpret_cast<uintptr_t>(Except));
return EXCEPTION_EXECUTE_HANDLER;
}
#if defined(__clang__) && defined(_M_IX86)
__attribute__((optnone))
#endif
bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
if (!gCrashRecoveryEnabled) {
Fn();
return true;
}
assert(!Impl && "Crash recovery context already initialized!");
Impl = new CrashRecoveryContextImpl(this);
__try {
Fn();
} __except (ExceptionFilter(GetExceptionInformation())) {
return false;
}
return true;
}
#else
#if defined(_WIN32)
#include "llvm/Support/Windows/WindowsSupport.h"
static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
{
constexpr ULONG DbgPrintExceptionWideC = 0x4001000AL;
switch (ExceptionInfo->ExceptionRecord->ExceptionCode)
{
case DBG_PRINTEXCEPTION_C:
case DbgPrintExceptionWideC:
case 0x406D1388:
return EXCEPTION_CONTINUE_EXECUTION;
}
const CrashRecoveryContextImpl *CRCI = CurrentContext;
if (!CRCI) {
CrashRecoveryContext::Disable();
return EXCEPTION_CONTINUE_SEARCH;
}
int RetCode = (int)ExceptionInfo->ExceptionRecord->ExceptionCode;
if ((RetCode & 0xF0000000) == 0xE0000000)
RetCode &= ~0xF0000000;
const_cast<CrashRecoveryContextImpl *>(CRCI)->HandleCrash(
RetCode, reinterpret_cast<uintptr_t>(ExceptionInfo));
llvm_unreachable("Handled the crash, should have longjmp'ed out of here");
}
static LLVM_THREAD_LOCAL const void* sCurrentExceptionHandle;
static void installExceptionOrSignalHandlers() {
PVOID handle = ::AddVectoredExceptionHandler(1, ExceptionHandler);
sCurrentExceptionHandle = handle;
}
static void uninstallExceptionOrSignalHandlers() {
PVOID currentHandle = const_cast<PVOID>(sCurrentExceptionHandle);
if (currentHandle) {
::RemoveVectoredExceptionHandler(currentHandle);
sCurrentExceptionHandle = NULL;
}
}
#else
#include <signal.h>
static const int Signals[] = …;
static const unsigned NumSignals = …;
static struct sigaction PrevActions[NumSignals];
static void CrashRecoverySignalHandler(int Signal) { … }
static void installExceptionOrSignalHandlers() { … }
static void uninstallExceptionOrSignalHandlers() { … }
#endif
bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) { … }
#endif
[[noreturn]] void CrashRecoveryContext::HandleExit(int RetCode) { … }
bool CrashRecoveryContext::isCrash(int RetCode) { … }
bool CrashRecoveryContext::throwIfCrash(int RetCode) { … }
static void setThreadBackgroundPriority() { … }
static bool hasThreadBackgroundPriority() { … }
namespace {
struct RunSafelyOnThreadInfo { … };
}
static void RunSafelyOnThread_Dispatch(void *UserData) { … }
bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn,
unsigned RequestedStackSize) { … }