10 #include "llvm/Config/llvm-config.h" 25 struct CrashRecoveryContextImpl;
30 struct CrashRecoveryContextImpl {
35 const CrashRecoveryContextImpl *Next;
39 volatile unsigned Failed : 1;
40 unsigned SwitchedThread : 1;
41 unsigned ValidJumpBuffer : 1;
45 : CRC(CRC),
Failed(
false), SwitchedThread(
false), ValidJumpBuffer(
false) {
46 Next = CurrentContext->get();
47 CurrentContext->set(
this);
49 ~CrashRecoveryContextImpl() {
51 CurrentContext->set(Next);
56 void setSwitchedThread() {
57 #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0 58 SwitchedThread =
true;
66 void HandleCrash(
int RetCode, uintptr_t
Context) {
69 CurrentContext->set(Next);
71 assert(!
Failed &&
"Crash recovery context already failed!");
81 longjmp(JumpBuffer, 1);
114 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
123 if (!gCrashRecoveryEnabled)
126 const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
134 std::lock_guard<std::mutex> L(*gCrashRecoveryContextMutex);
136 if (gCrashRecoveryEnabled)
138 gCrashRecoveryEnabled =
true;
143 std::lock_guard<std::mutex> L(*gCrashRecoveryContextMutex);
144 if (!gCrashRecoveryEnabled)
146 gCrashRecoveryEnabled =
false;
156 cleanup->next = head;
164 if (cleanup == head) {
165 head = cleanup->next;
167 head->prev =
nullptr;
170 cleanup->prev->next = cleanup->next;
172 cleanup->next->prev = cleanup->prev;
177 #if defined(_MSC_VER) 194 static int ExceptionFilter(_EXCEPTION_POINTERS *Except) {
196 const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
202 return EXCEPTION_CONTINUE_SEARCH;
205 int RetCode = (int)Except->ExceptionRecord->ExceptionCode;
206 if ((RetCode & 0xF0000000) == 0xE0000000)
207 RetCode &= ~0xF0000000;
210 const_cast<CrashRecoveryContextImpl *
>(CRCI)->HandleCrash(
211 RetCode, reinterpret_cast<uintptr_t>(Except));
213 return EXCEPTION_EXECUTE_HANDLER;
216 #if defined(__clang__) && defined(_M_IX86) 218 __attribute__((optnone))
221 if (!gCrashRecoveryEnabled) {
225 assert(!Impl &&
"Crash recovery context already initialized!");
226 Impl =
new CrashRecoveryContextImpl(
this);
229 } __except (ExceptionFilter(GetExceptionInformation())) {
259 static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
263 constexpr ULONG DbgPrintExceptionWideC = 0x4001000AL;
264 switch (ExceptionInfo->ExceptionRecord->ExceptionCode)
266 case DBG_PRINTEXCEPTION_C:
267 case DbgPrintExceptionWideC:
269 return EXCEPTION_CONTINUE_EXECUTION;
273 const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
279 return EXCEPTION_CONTINUE_SEARCH;
285 int RetCode = (int)ExceptionInfo->ExceptionRecord->ExceptionCode;
286 if ((RetCode & 0xF0000000) == 0xE0000000)
287 RetCode &= ~0xF0000000;
290 const_cast<CrashRecoveryContextImpl *
>(CRCI)->HandleCrash(
291 RetCode, reinterpret_cast<uintptr_t>(ExceptionInfo));
310 PVOID handle = ::AddVectoredExceptionHandler(1, ExceptionHandler);
311 sCurrentExceptionHandle.
set(handle);
315 PVOID currentHandle =
const_cast<PVOID
>(sCurrentExceptionHandle.
get());
318 ::RemoveVectoredExceptionHandler(currentHandle);
321 sCurrentExceptionHandle.
set(NULL);
341 { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
347 const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
369 sigemptyset(&SigMask);
370 sigaddset(&SigMask, Signal);
371 sigprocmask(SIG_UNBLOCK, &SigMask,
nullptr);
378 if (Signal == SIGPIPE)
382 const_cast<CrashRecoveryContextImpl *
>(CRCI)->HandleCrash(RetCode, Signal);
387 struct sigaction Handler;
389 Handler.sa_flags = 0;
390 sigemptyset(&Handler.sa_mask);
407 if (gCrashRecoveryEnabled) {
408 assert(!Impl &&
"Crash recovery context already initialized!");
409 CrashRecoveryContextImpl *CRCI =
new CrashRecoveryContextImpl(
this);
412 CRCI->ValidJumpBuffer =
true;
413 if (setjmp(CRCI->JumpBuffer) != 0) {
428 ::RaiseException(0xE0000000 | RetCode, 0, 0, NULL);
432 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *)Impl;
433 assert(CRCI &&
"Crash recovery context never initialized!");
434 CRCI->HandleCrash(RetCode, 0 );
442 setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG);
448 return getpriority(PRIO_DARWIN_THREAD, 0) == 1;
455 struct RunSafelyOnThreadInfo {
458 bool UseBackgroundPriority;
464 RunSafelyOnThreadInfo *
Info =
465 reinterpret_cast<RunSafelyOnThreadInfo*
>(UserData);
467 if (Info->UseBackgroundPriority)
470 Info->Result = Info->CRC->RunSafely(Info->Fn);
473 unsigned RequestedStackSize) {
475 RunSafelyOnThreadInfo
Info = { Fn,
this, UseBackgroundPriority,
false };
477 RequestedStackSize == 0
480 if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl)
481 CRC->setSwitchedThread();
static ManagedStatic< std::mutex > gCrashRecoveryContextMutex
This class represents lattice values for constants.
ThreadLocal - A class used to abstract thread-local storage.
static const int Signals[]
bool DumpStackAndCleanupOnFailure
Selects whether handling of failures should be done in the same way as for regular crashes...
An efficient, type-erasing, non-owning reference to a callable.
static CrashRecoveryContext * GetCurrent()
Return the active context, if the code is currently executing in a thread which is in a protected con...
static bool hasThreadBackgroundPriority()
static void cleanup(BlockFrequencyInfoImplBase &BFI)
Clear all memory not needed downstream.
void llvm_execute_on_thread(void(*UserFn)(void *), void *UserData, llvm::Optional< unsigned > StackSizeInBytes=llvm::None)
Execute the given UserFn on a separate thread, passing it the provided UserData and waits for thread ...
void unregisterCleanup(CrashRecoveryContextCleanup *cleanup)
virtual ~CrashRecoveryContextCleanup()
static void Enable()
Enable crash recovery.
Abstract base class of cleanup handlers.
Analysis containing CSE Info
T * get()
get - Fetches a pointer to the object associated with the current thread.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
bool RunSafelyOnThread(function_ref< void()>, unsigned RequestedStackSize=0)
Execute the provide callback function (with the given arguments) in a protected context which is run ...
static void uninstallExceptionOrSignalHandlers()
void registerCleanup(CrashRecoveryContextCleanup *cleanup)
Register cleanup handler, which is used when the recovery context is finished.
static void CrashRecoverySignalHandler(int Signal)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
virtual void recoverResources()=0
testing::Matcher< const detail::ErrorHolder & > Failed()
static ManagedStatic< sys::ThreadLocal< const CrashRecoveryContext > > tlIsRecoveringFromCrash
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
#define LLVM_ATTRIBUTE_NORETURN
static const unsigned NumSignals
bool RunSafely(function_ref< void()> Fn)
Execute the provided callback function (with the given arguments) in a protected context.
static void installExceptionOrSignalHandlers()
Crash recovery helper object.
LLVM_ATTRIBUTE_NORETURN void HandleExit(int RetCode)
Explicitly trigger a crash recovery in the current process, and return failure from RunSafely()...
void CleanupOnSignal(uintptr_t Context)
This function does the following:
static void setThreadBackgroundPriority()
static bool gCrashRecoveryEnabled
int RetCode
In case of a crash, this is the crash identifier.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
static bool isRecoveringFromCrash()
Return true if the current thread is recovering from a crash.
static void RunSafelyOnThread_Dispatch(void *UserData)
static void Disable()
Disable crash recovery.
static struct sigaction PrevActions[NumSignals]