xref: /llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp (revision 5e77ea04f214c7a18bd5c782c8b8a7b7c828ad7a)
1 //===--- CrashRecoveryContext.cpp - Crash Recovery ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Support/CrashRecoveryContext.h"
10 #include "llvm/Config/llvm-config.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/ExitCodes.h"
13 #include "llvm/Support/ManagedStatic.h"
14 #include "llvm/Support/Signals.h"
15 #include "llvm/Support/ThreadLocal.h"
16 #include <mutex>
17 #include <setjmp.h>
18 
19 using namespace llvm;
20 
21 namespace {
22 
23 struct CrashRecoveryContextImpl;
24 
25 static ManagedStatic<
26     sys::ThreadLocal<const CrashRecoveryContextImpl> > CurrentContext;
27 
28 struct CrashRecoveryContextImpl {
29   // When threads are disabled, this links up all active
30   // CrashRecoveryContextImpls.  When threads are enabled there's one thread
31   // per CrashRecoveryContext and CurrentContext is a thread-local, so only one
32   // CrashRecoveryContextImpl is active per thread and this is always null.
33   const CrashRecoveryContextImpl *Next;
34 
35   CrashRecoveryContext *CRC;
36   ::jmp_buf JumpBuffer;
37   volatile unsigned Failed : 1;
38   unsigned SwitchedThread : 1;
39   unsigned ValidJumpBuffer : 1;
40 
41 public:
42   CrashRecoveryContextImpl(CrashRecoveryContext *CRC) noexcept
43       : CRC(CRC), Failed(false), SwitchedThread(false), ValidJumpBuffer(false) {
44     Next = CurrentContext->get();
45     CurrentContext->set(this);
46   }
47   ~CrashRecoveryContextImpl() {
48     if (!SwitchedThread)
49       CurrentContext->set(Next);
50   }
51 
52   /// Called when the separate crash-recovery thread was finished, to
53   /// indicate that we don't need to clear the thread-local CurrentContext.
54   void setSwitchedThread() {
55 #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0
56     SwitchedThread = true;
57 #endif
58   }
59 
60   // If the function ran by the CrashRecoveryContext crashes or fails, then
61   // 'RetCode' represents the returned error code, as if it was returned by a
62   // process. 'Context' represents the signal type on Unix; on Windows, it is
63   // the ExceptionContext.
64   void HandleCrash(int RetCode, uintptr_t Context) {
65     // Eliminate the current context entry, to avoid re-entering in case the
66     // cleanup code crashes.
67     CurrentContext->set(Next);
68 
69     assert(!Failed && "Crash recovery context already failed!");
70     Failed = true;
71 
72     if (CRC->DumpStackAndCleanupOnFailure)
73       sys::CleanupOnSignal(Context);
74 
75     CRC->RetCode = RetCode;
76 
77     // Jump back to the RunSafely we were called under.
78     if (ValidJumpBuffer)
79       longjmp(JumpBuffer, 1);
80 
81     // Otherwise let the caller decide of the outcome of the crash. Currently
82     // this occurs when using SEH on Windows with MSVC or clang-cl.
83   }
84 };
85 } // namespace
86 
87 static LLVM_THREAD_LOCAL bool gCrashRecoveryEnabled = false;
88 
89 static ManagedStatic<sys::ThreadLocal<const CrashRecoveryContext>>
90        tlIsRecoveringFromCrash;
91 
92 static void installExceptionOrSignalHandlers();
93 static void uninstallExceptionOrSignalHandlers();
94 
95 CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {}
96 
97 CrashRecoveryContext::CrashRecoveryContext() {
98   // On Windows, if abort() was previously triggered (and caught by a previous
99   // CrashRecoveryContext) the Windows CRT removes our installed signal handler,
100   // so we need to install it again.
101   sys::DisableSystemDialogsOnCrash();
102 }
103 
104 CrashRecoveryContext::~CrashRecoveryContext() {
105   // Reclaim registered resources.
106   CrashRecoveryContextCleanup *i = head;
107   const CrashRecoveryContext *PC = tlIsRecoveringFromCrash->get();
108   tlIsRecoveringFromCrash->set(this);
109   while (i) {
110     CrashRecoveryContextCleanup *tmp = i;
111     i = tmp->next;
112     tmp->cleanupFired = true;
113     tmp->recoverResources();
114     delete tmp;
115   }
116   tlIsRecoveringFromCrash->set(PC);
117 
118   CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
119   delete CRCI;
120 }
121 
122 bool CrashRecoveryContext::isRecoveringFromCrash() {
123   return tlIsRecoveringFromCrash->get() != nullptr;
124 }
125 
126 CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
127   if (!gCrashRecoveryEnabled)
128     return nullptr;
129 
130   const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
131   if (!CRCI)
132     return nullptr;
133 
134   return CRCI->CRC;
135 }
136 
137 void CrashRecoveryContext::Enable() {
138   if (gCrashRecoveryEnabled)
139     return;
140   gCrashRecoveryEnabled = true;
141   installExceptionOrSignalHandlers();
142 }
143 
144 void CrashRecoveryContext::Disable() {
145   if (!gCrashRecoveryEnabled)
146     return;
147   gCrashRecoveryEnabled = false;
148   uninstallExceptionOrSignalHandlers();
149 }
150 
151 void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup)
152 {
153   if (!cleanup)
154     return;
155   if (head)
156     head->prev = cleanup;
157   cleanup->next = head;
158   head = cleanup;
159 }
160 
161 void
162 CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) {
163   if (!cleanup)
164     return;
165   if (cleanup == head) {
166     head = cleanup->next;
167     if (head)
168       head->prev = nullptr;
169   }
170   else {
171     cleanup->prev->next = cleanup->next;
172     if (cleanup->next)
173       cleanup->next->prev = cleanup->prev;
174   }
175   delete cleanup;
176 }
177 
178 #if defined(_MSC_VER)
179 
180 #include <windows.h> // for GetExceptionInformation
181 
182 // If _MSC_VER is defined, we must have SEH. Use it if it's available. It's way
183 // better than VEH. Vectored exception handling catches all exceptions happening
184 // on the thread with installed exception handlers, so it can interfere with
185 // internal exception handling of other libraries on that thread. SEH works
186 // exactly as you would expect normal exception handling to work: it only
187 // catches exceptions if they would bubble out from the stack frame with __try /
188 // __except.
189 
190 static void installExceptionOrSignalHandlers() {}
191 static void uninstallExceptionOrSignalHandlers() {}
192 
193 // We need this function because the call to GetExceptionInformation() can only
194 // occur inside the __except evaluation block
195 static int ExceptionFilter(_EXCEPTION_POINTERS *Except) {
196   // Lookup the current thread local recovery object.
197   const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
198 
199   if (!CRCI) {
200     // Something has gone horribly wrong, so let's just tell everyone
201     // to keep searching
202     CrashRecoveryContext::Disable();
203     return EXCEPTION_CONTINUE_SEARCH;
204   }
205 
206   int RetCode = (int)Except->ExceptionRecord->ExceptionCode;
207   if ((RetCode & 0xF0000000) == 0xE0000000)
208     RetCode &= ~0xF0000000; // this crash was generated by sys::Process::Exit
209 
210   // Handle the crash
211   const_cast<CrashRecoveryContextImpl *>(CRCI)->HandleCrash(
212       RetCode, reinterpret_cast<uintptr_t>(Except));
213 
214   return EXCEPTION_EXECUTE_HANDLER;
215 }
216 
217 #if defined(__clang__) && defined(_M_IX86)
218 // Work around PR44697.
219 __attribute__((optnone))
220 #endif
221 bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
222   if (!gCrashRecoveryEnabled) {
223     Fn();
224     return true;
225   }
226   assert(!Impl && "Crash recovery context already initialized!");
227   Impl = new CrashRecoveryContextImpl(this);
228   __try {
229     Fn();
230   } __except (ExceptionFilter(GetExceptionInformation())) {
231     return false;
232   }
233   return true;
234 }
235 
236 #else // !_MSC_VER
237 
238 #if defined(_WIN32)
239 // This is a non-MSVC compiler, probably mingw gcc or clang without
240 // -fms-extensions. Use vectored exception handling (VEH).
241 //
242 // On Windows, we can make use of vectored exception handling to catch most
243 // crashing situations.  Note that this does mean we will be alerted of
244 // exceptions *before* structured exception handling has the opportunity to
245 // catch it. Unfortunately, this causes problems in practice with other code
246 // running on threads with LLVM crash recovery contexts, so we would like to
247 // eventually move away from VEH.
248 //
249 // Vectored works on a per-thread basis, which is an advantage over
250 // SetUnhandledExceptionFilter. SetUnhandledExceptionFilter also doesn't have
251 // any native support for chaining exception handlers, but VEH allows more than
252 // one.
253 //
254 // The vectored exception handler functionality was added in Windows
255 // XP, so if support for older versions of Windows is required,
256 // it will have to be added.
257 
258 #include "llvm/Support/Windows/WindowsSupport.h"
259 
260 static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
261 {
262   // DBG_PRINTEXCEPTION_WIDE_C is not properly defined on all supported
263   // compilers and platforms, so we define it manually.
264   constexpr ULONG DbgPrintExceptionWideC = 0x4001000AL;
265   switch (ExceptionInfo->ExceptionRecord->ExceptionCode)
266   {
267   case DBG_PRINTEXCEPTION_C:
268   case DbgPrintExceptionWideC:
269   case 0x406D1388:  // set debugger thread name
270     return EXCEPTION_CONTINUE_EXECUTION;
271   }
272 
273   // Lookup the current thread local recovery object.
274   const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
275 
276   if (!CRCI) {
277     // Something has gone horribly wrong, so let's just tell everyone
278     // to keep searching
279     CrashRecoveryContext::Disable();
280     return EXCEPTION_CONTINUE_SEARCH;
281   }
282 
283   // TODO: We can capture the stack backtrace here and store it on the
284   // implementation if we so choose.
285 
286   int RetCode = (int)ExceptionInfo->ExceptionRecord->ExceptionCode;
287   if ((RetCode & 0xF0000000) == 0xE0000000)
288     RetCode &= ~0xF0000000; // this crash was generated by sys::Process::Exit
289 
290   // Handle the crash
291   const_cast<CrashRecoveryContextImpl *>(CRCI)->HandleCrash(
292       RetCode, reinterpret_cast<uintptr_t>(ExceptionInfo));
293 
294   // Note that we don't actually get here because HandleCrash calls
295   // longjmp, which means the HandleCrash function never returns.
296   llvm_unreachable("Handled the crash, should have longjmp'ed out of here");
297 }
298 
299 // Because the Enable and Disable calls are static, it means that
300 // there may not actually be an Impl available, or even a current
301 // CrashRecoveryContext at all.  So we make use of a thread-local
302 // exception table.  The handles contained in here will either be
303 // non-NULL, valid VEH handles, or NULL.
304 static sys::ThreadLocal<const void> sCurrentExceptionHandle;
305 
306 static void installExceptionOrSignalHandlers() {
307   // We can set up vectored exception handling now.  We will install our
308   // handler as the front of the list, though there's no assurances that
309   // it will remain at the front (another call could install itself before
310   // our handler).  This 1) isn't likely, and 2) shouldn't cause problems.
311   PVOID handle = ::AddVectoredExceptionHandler(1, ExceptionHandler);
312   sCurrentExceptionHandle.set(handle);
313 }
314 
315 static void uninstallExceptionOrSignalHandlers() {
316   PVOID currentHandle = const_cast<PVOID>(sCurrentExceptionHandle.get());
317   if (currentHandle) {
318     // Now we can remove the vectored exception handler from the chain
319     ::RemoveVectoredExceptionHandler(currentHandle);
320 
321     // Reset the handle in our thread-local set.
322     sCurrentExceptionHandle.set(NULL);
323   }
324 }
325 
326 #else // !_WIN32
327 
328 // Generic POSIX implementation.
329 //
330 // This implementation relies on synchronous signals being delivered to the
331 // current thread. We use a thread local object to keep track of the active
332 // crash recovery context, and install signal handlers to invoke HandleCrash on
333 // the active object.
334 //
335 // This implementation does not attempt to chain signal handlers in any
336 // reliable fashion -- if we get a signal outside of a crash recovery context we
337 // simply disable crash recovery and raise the signal again.
338 
339 #include <signal.h>
340 
341 static const int Signals[] =
342     { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
343 static const unsigned NumSignals = array_lengthof(Signals);
344 static struct sigaction PrevActions[NumSignals];
345 
346 static void CrashRecoverySignalHandler(int Signal) {
347   // Lookup the current thread local recovery object.
348   const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
349 
350   if (!CRCI) {
351     // We didn't find a crash recovery context -- this means either we got a
352     // signal on a thread we didn't expect it on, the application got a signal
353     // outside of a crash recovery context, or something else went horribly
354     // wrong.
355     //
356     // Disable crash recovery and raise the signal again. The assumption here is
357     // that the enclosing application will terminate soon, and we won't want to
358     // attempt crash recovery again.
359     //
360     // This call of Disable isn't thread safe, but it doesn't actually matter.
361     CrashRecoveryContext::Disable();
362     raise(Signal);
363 
364     // The signal will be thrown once the signal mask is restored.
365     return;
366   }
367 
368   // Unblock the signal we received.
369   sigset_t SigMask;
370   sigemptyset(&SigMask);
371   sigaddset(&SigMask, Signal);
372   sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
373 
374   // Return the same error code as if the program crashed, as mentioned in the
375   // section "Exit Status for Commands":
376   // https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html
377   int RetCode = 128 + Signal;
378 
379   // Don't consider a broken pipe as a crash (see clang/lib/Driver/Driver.cpp)
380   if (Signal == SIGPIPE)
381     RetCode = EX_IOERR;
382 
383   if (CRCI)
384     const_cast<CrashRecoveryContextImpl *>(CRCI)->HandleCrash(RetCode, Signal);
385 }
386 
387 static void installExceptionOrSignalHandlers() {
388   // Setup the signal handler.
389   struct sigaction Handler;
390   Handler.sa_handler = CrashRecoverySignalHandler;
391   Handler.sa_flags = 0;
392   sigemptyset(&Handler.sa_mask);
393 
394   for (unsigned i = 0; i != NumSignals; ++i) {
395     sigaction(Signals[i], &Handler, &PrevActions[i]);
396   }
397 }
398 
399 static void uninstallExceptionOrSignalHandlers() {
400   // Restore the previous signal handlers.
401   for (unsigned i = 0; i != NumSignals; ++i)
402     sigaction(Signals[i], &PrevActions[i], nullptr);
403 }
404 
405 #endif // !_WIN32
406 
407 bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
408   // If crash recovery is disabled, do nothing.
409   if (gCrashRecoveryEnabled) {
410     assert(!Impl && "Crash recovery context already initialized!");
411     CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
412     Impl = CRCI;
413 
414     CRCI->ValidJumpBuffer = true;
415     if (setjmp(CRCI->JumpBuffer) != 0) {
416       return false;
417     }
418   }
419 
420   Fn();
421   return true;
422 }
423 
424 #endif // !_MSC_VER
425 
426 LLVM_ATTRIBUTE_NORETURN
427 void CrashRecoveryContext::HandleExit(int RetCode) {
428 #if defined(_WIN32)
429   // SEH and VEH
430   ::RaiseException(0xE0000000 | RetCode, 0, 0, NULL);
431 #else
432   // On Unix we don't need to raise an exception, we go directly to
433   // HandleCrash(), then longjmp will unwind the stack for us.
434   CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *)Impl;
435   assert(CRCI && "Crash recovery context never initialized!");
436   CRCI->HandleCrash(RetCode, 0 /*no sig num*/);
437 #endif
438   llvm_unreachable("Most likely setjmp wasn't called!");
439 }
440 
441 bool CrashRecoveryContext::throwIfCrash(int RetCode) {
442 #if defined(_WIN32)
443   // On Windows, the high bits are reserved for kernel return codes. Values
444   // starting with 0x80000000 are reserved for "warnings"; values of 0xC0000000
445   // and up are for "errors". In practice, both are interpreted as a
446   // non-continuable signal.
447   unsigned Code = ((unsigned)RetCode & 0xF0000000) >> 28;
448   if (Code != 0xC && Code != 8)
449     return false;
450   ::RaiseException(RetCode, 0, 0, NULL);
451 #else
452   // On Unix, signals are represented by return codes of 128 or higher.
453   // Exit code 128 is a reserved value and should not be raised as a signal.
454   if (RetCode <= 128)
455     return false;
456   llvm::sys::unregisterHandlers();
457   raise(RetCode - 128);
458 #endif
459   return true;
460 }
461 
462 // FIXME: Portability.
463 static void setThreadBackgroundPriority() {
464 #ifdef __APPLE__
465   setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG);
466 #endif
467 }
468 
469 static bool hasThreadBackgroundPriority() {
470 #ifdef __APPLE__
471   return getpriority(PRIO_DARWIN_THREAD, 0) == 1;
472 #else
473   return false;
474 #endif
475 }
476 
477 namespace {
478 struct RunSafelyOnThreadInfo {
479   function_ref<void()> Fn;
480   CrashRecoveryContext *CRC;
481   bool UseBackgroundPriority;
482   bool Result;
483 };
484 } // namespace
485 
486 static void RunSafelyOnThread_Dispatch(void *UserData) {
487   RunSafelyOnThreadInfo *Info =
488     reinterpret_cast<RunSafelyOnThreadInfo*>(UserData);
489 
490   if (Info->UseBackgroundPriority)
491     setThreadBackgroundPriority();
492 
493   Info->Result = Info->CRC->RunSafely(Info->Fn);
494 }
495 bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn,
496                                              unsigned RequestedStackSize) {
497   bool UseBackgroundPriority = hasThreadBackgroundPriority();
498   RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false };
499   llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info,
500                          RequestedStackSize == 0
501                              ? llvm::None
502                              : llvm::Optional<unsigned>(RequestedStackSize));
503   if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl)
504     CRC->setSwitchedThread();
505   return Info.Result;
506 }
507