CrashRecoveryContext.cpp 17 KB

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