blob: 8d8529b474ffdadba4c213767e2f94641a058ac0 [file] [log] [blame]
Daniel Dunbar19a3b372010-07-28 15:40:20 +00001//===--- CrashRecoveryContext.cpp - Crash Recovery ------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Daniel Dunbar19a3b372010-07-28 15:40:20 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/CrashRecoveryContext.h"
Nico Weber432a3882018-04-30 14:59:11 +000010#include "llvm/Config/llvm-config.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/Support/ErrorHandling.h"
Filip Pizlof2189bf2013-09-12 17:46:57 +000012#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000013#include "llvm/Support/ThreadLocal.h"
Benjamin Kramer928071a2019-08-19 19:49:57 +000014#include <mutex>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000015#include <setjmp.h>
Daniel Dunbar19a3b372010-07-28 15:40:20 +000016using namespace llvm;
17
18namespace {
19
20struct CrashRecoveryContextImpl;
21
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000022static ManagedStatic<
Zachary Turnera40ccf62014-06-10 18:03:04 +000023 sys::ThreadLocal<const CrashRecoveryContextImpl> > CurrentContext;
Daniel Dunbaraf77e222010-07-29 01:21:47 +000024
Daniel Dunbar19a3b372010-07-28 15:40:20 +000025struct CrashRecoveryContextImpl {
Nico Weber26928112015-08-07 17:32:06 +000026 // 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.
Nico Weber28dc4172015-08-06 19:21:25 +000030 const CrashRecoveryContextImpl *Next;
31
Daniel Dunbarb30266e2010-08-17 22:32:37 +000032 CrashRecoveryContext *CRC;
Daniel Dunbar19a3b372010-07-28 15:40:20 +000033 ::jmp_buf JumpBuffer;
34 volatile unsigned Failed : 1;
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +000035 unsigned SwitchedThread : 1;
Daniel Dunbar19a3b372010-07-28 15:40:20 +000036
37public:
Daniel Dunbarb30266e2010-08-17 22:32:37 +000038 CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC),
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +000039 Failed(false),
40 SwitchedThread(false) {
Nico Weber28dc4172015-08-06 19:21:25 +000041 Next = CurrentContext->get();
Filip Pizlof2189bf2013-09-12 17:46:57 +000042 CurrentContext->set(this);
Daniel Dunbaraf77e222010-07-29 01:21:47 +000043 }
44 ~CrashRecoveryContextImpl() {
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +000045 if (!SwitchedThread)
Nico Weber28dc4172015-08-06 19:21:25 +000046 CurrentContext->set(Next);
Daniel Dunbaraf77e222010-07-29 01:21:47 +000047 }
Daniel Dunbar19a3b372010-07-28 15:40:20 +000048
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000049 /// Called when the separate crash-recovery thread was finished, to
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +000050 /// indicate that we don't need to clear the thread-local CurrentContext.
Fangrui Songf78650a2018-07-30 19:41:25 +000051 void setSwitchedThread() {
Nico Weber28dc4172015-08-06 19:21:25 +000052#if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0
53 SwitchedThread = true;
54#endif
55 }
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +000056
Daniel Dunbar19a3b372010-07-28 15:40:20 +000057 void HandleCrash() {
Daniel Dunbar09b0c782010-08-17 22:32:39 +000058 // Eliminate the current context entry, to avoid re-entering in case the
59 // cleanup code crashes.
Nico Weber28dc4172015-08-06 19:21:25 +000060 CurrentContext->set(Next);
Daniel Dunbar09b0c782010-08-17 22:32:39 +000061
Daniel Dunbar19a3b372010-07-28 15:40:20 +000062 assert(!Failed && "Crash recovery context already failed!");
63 Failed = true;
64
65 // FIXME: Stash the backtrace.
66
67 // Jump back to the RunSafely we were called under.
68 longjmp(JumpBuffer, 1);
69 }
70};
71
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000072}
Daniel Dunbar19a3b372010-07-28 15:40:20 +000073
Benjamin Kramer928071a2019-08-19 19:49:57 +000074static ManagedStatic<std::mutex> gCrashRecoveryContextMutex;
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000075static bool gCrashRecoveryEnabled = false;
76
77static ManagedStatic<sys::ThreadLocal<const CrashRecoveryContext>>
Ted Kremenekab1a2422011-03-21 18:38:03 +000078 tlIsRecoveringFromCrash;
79
Reid Kleckner710c1ce2017-05-17 18:16:17 +000080static void installExceptionOrSignalHandlers();
81static void uninstallExceptionOrSignalHandlers();
82
Ted Kremenekc44d3cf2011-03-18 02:05:11 +000083CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {}
84
Daniel Dunbar19a3b372010-07-28 15:40:20 +000085CrashRecoveryContext::~CrashRecoveryContext() {
Ted Kremenekc44d3cf2011-03-18 02:05:11 +000086 // Reclaim registered resources.
87 CrashRecoveryContextCleanup *i = head;
Nico Weber28dc4172015-08-06 19:21:25 +000088 const CrashRecoveryContext *PC = tlIsRecoveringFromCrash->get();
89 tlIsRecoveringFromCrash->set(this);
Ted Kremenekc44d3cf2011-03-18 02:05:11 +000090 while (i) {
91 CrashRecoveryContextCleanup *tmp = i;
92 i = tmp->next;
Ted Kremenek32aea2e2011-03-19 00:59:37 +000093 tmp->cleanupFired = true;
Ted Kremenek857e5352011-03-22 04:33:13 +000094 tmp->recoverResources();
Ted Kremenekc44d3cf2011-03-18 02:05:11 +000095 delete tmp;
96 }
Nico Weber28dc4172015-08-06 19:21:25 +000097 tlIsRecoveringFromCrash->set(PC);
Fangrui Songf78650a2018-07-30 19:41:25 +000098
Daniel Dunbar19a3b372010-07-28 15:40:20 +000099 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
100 delete CRCI;
101}
102
Ted Kremenekab1a2422011-03-21 18:38:03 +0000103bool CrashRecoveryContext::isRecoveringFromCrash() {
Craig Topperc10719f2014-04-07 04:17:22 +0000104 return tlIsRecoveringFromCrash->get() != nullptr;
Ted Kremenekab1a2422011-03-21 18:38:03 +0000105}
106
Daniel Dunbarb30266e2010-08-17 22:32:37 +0000107CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
Ted Kremenek794a0712011-03-19 00:59:33 +0000108 if (!gCrashRecoveryEnabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000109 return nullptr;
Ted Kremenek794a0712011-03-19 00:59:33 +0000110
Filip Pizlof2189bf2013-09-12 17:46:57 +0000111 const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
Daniel Dunbarb30266e2010-08-17 22:32:37 +0000112 if (!CRCI)
Craig Topperc10719f2014-04-07 04:17:22 +0000113 return nullptr;
Daniel Dunbarb30266e2010-08-17 22:32:37 +0000114
115 return CRCI->CRC;
116}
117
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000118void CrashRecoveryContext::Enable() {
Benjamin Kramer928071a2019-08-19 19:49:57 +0000119 std::lock_guard<std::mutex> L(*gCrashRecoveryContextMutex);
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000120 // FIXME: Shouldn't this be a refcount or something?
121 if (gCrashRecoveryEnabled)
122 return;
123 gCrashRecoveryEnabled = true;
124 installExceptionOrSignalHandlers();
125}
126
127void CrashRecoveryContext::Disable() {
Benjamin Kramer928071a2019-08-19 19:49:57 +0000128 std::lock_guard<std::mutex> L(*gCrashRecoveryContextMutex);
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000129 if (!gCrashRecoveryEnabled)
130 return;
131 gCrashRecoveryEnabled = false;
132 uninstallExceptionOrSignalHandlers();
133}
134
Ted Kremenekc44d3cf2011-03-18 02:05:11 +0000135void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup)
136{
137 if (!cleanup)
138 return;
139 if (head)
140 head->prev = cleanup;
141 cleanup->next = head;
142 head = cleanup;
143}
144
145void
146CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) {
147 if (!cleanup)
148 return;
149 if (cleanup == head) {
150 head = cleanup->next;
151 if (head)
Craig Topperc10719f2014-04-07 04:17:22 +0000152 head->prev = nullptr;
Ted Kremenekc44d3cf2011-03-18 02:05:11 +0000153 }
154 else {
155 cleanup->prev->next = cleanup->next;
156 if (cleanup->next)
157 cleanup->next->prev = cleanup->prev;
158 }
159 delete cleanup;
160}
161
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000162#if defined(_MSC_VER)
163// If _MSC_VER is defined, we must have SEH. Use it if it's available. It's way
164// better than VEH. Vectored exception handling catches all exceptions happening
165// on the thread with installed exception handlers, so it can interfere with
166// internal exception handling of other libraries on that thread. SEH works
167// exactly as you would expect normal exception handling to work: it only
168// catches exceptions if they would bubble out from the stack frame with __try /
169// __except.
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000170
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000171static void installExceptionOrSignalHandlers() {}
172static void uninstallExceptionOrSignalHandlers() {}
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000173
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000174bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
175 if (!gCrashRecoveryEnabled) {
176 Fn();
177 return true;
178 }
179
180 bool Result = true;
181 __try {
182 Fn();
183 } __except (1) { // Catch any exception.
184 Result = false;
185 }
186 return Result;
187}
188
189#else // !_MSC_VER
190
Nico Weber712e8d22018-04-29 00:45:03 +0000191#if defined(_WIN32)
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000192// This is a non-MSVC compiler, probably mingw gcc or clang without
193// -fms-extensions. Use vectored exception handling (VEH).
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000194//
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000195// On Windows, we can make use of vectored exception handling to catch most
196// crashing situations. Note that this does mean we will be alerted of
197// exceptions *before* structured exception handling has the opportunity to
198// catch it. Unfortunately, this causes problems in practice with other code
199// running on threads with LLVM crash recovery contexts, so we would like to
200// eventually move away from VEH.
201//
202// Vectored works on a per-thread basis, which is an advantage over
203// SetUnhandledExceptionFilter. SetUnhandledExceptionFilter also doesn't have
204// any native support for chaining exception handlers, but VEH allows more than
205// one.
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000206//
207// The vectored exception handler functionality was added in Windows
208// XP, so if support for older versions of Windows is required,
209// it will have to be added.
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000210
211#include "Windows/WindowsSupport.h"
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000212
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000213static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000214{
Zachary Turner0daa7072017-05-17 16:39:33 +0000215 // DBG_PRINTEXCEPTION_WIDE_C is not properly defined on all supported
216 // compilers and platforms, so we define it manually.
217 constexpr ULONG DbgPrintExceptionWideC = 0x4001000AL;
Zachary Turner13e87f42017-05-16 22:50:32 +0000218 switch (ExceptionInfo->ExceptionRecord->ExceptionCode)
219 {
220 case DBG_PRINTEXCEPTION_C:
Zachary Turner0daa7072017-05-17 16:39:33 +0000221 case DbgPrintExceptionWideC:
Zachary Turner13e87f42017-05-16 22:50:32 +0000222 case 0x406D1388: // set debugger thread name
223 return EXCEPTION_CONTINUE_EXECUTION;
224 }
225
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000226 // Lookup the current thread local recovery object.
Filip Pizlof2189bf2013-09-12 17:46:57 +0000227 const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000228
229 if (!CRCI) {
230 // Something has gone horribly wrong, so let's just tell everyone
231 // to keep searching
232 CrashRecoveryContext::Disable();
233 return EXCEPTION_CONTINUE_SEARCH;
234 }
235
236 // TODO: We can capture the stack backtrace here and store it on the
237 // implementation if we so choose.
238
239 // Handle the crash
240 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
241
242 // Note that we don't actually get here because HandleCrash calls
243 // longjmp, which means the HandleCrash function never returns.
244 llvm_unreachable("Handled the crash, should have longjmp'ed out of here");
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000245}
246
247// Because the Enable and Disable calls are static, it means that
248// there may not actually be an Impl available, or even a current
249// CrashRecoveryContext at all. So we make use of a thread-local
250// exception table. The handles contained in here will either be
251// non-NULL, valid VEH handles, or NULL.
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000252static sys::ThreadLocal<const void> sCurrentExceptionHandle;
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000253
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000254static void installExceptionOrSignalHandlers() {
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000255 // We can set up vectored exception handling now. We will install our
256 // handler as the front of the list, though there's no assurances that
257 // it will remain at the front (another call could install itself before
258 // our handler). This 1) isn't likely, and 2) shouldn't cause problems.
259 PVOID handle = ::AddVectoredExceptionHandler(1, ExceptionHandler);
260 sCurrentExceptionHandle.set(handle);
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000261}
262
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000263static void uninstallExceptionOrSignalHandlers() {
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000264 PVOID currentHandle = const_cast<PVOID>(sCurrentExceptionHandle.get());
265 if (currentHandle) {
266 // Now we can remove the vectored exception handler from the chain
267 ::RemoveVectoredExceptionHandler(currentHandle);
268
269 // Reset the handle in our thread-local set.
270 sCurrentExceptionHandle.set(NULL);
271 }
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000272}
273
Nico Weber712e8d22018-04-29 00:45:03 +0000274#else // !_WIN32
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000275
276// Generic POSIX implementation.
277//
278// This implementation relies on synchronous signals being delivered to the
279// current thread. We use a thread local object to keep track of the active
280// crash recovery context, and install signal handlers to invoke HandleCrash on
281// the active object.
282//
283// This implementation does not to attempt to chain signal handlers in any
284// reliable fashion -- if we get a signal outside of a crash recovery context we
285// simply disable crash recovery and raise the signal again.
286
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000287#include <signal.h>
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000288
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000289static const int Signals[] =
290 { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
291static const unsigned NumSignals = array_lengthof(Signals);
292static struct sigaction PrevActions[NumSignals];
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000293
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000294static void CrashRecoverySignalHandler(int Signal) {
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000295 // Lookup the current thread local recovery object.
Filip Pizlof2189bf2013-09-12 17:46:57 +0000296 const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000297
298 if (!CRCI) {
299 // We didn't find a crash recovery context -- this means either we got a
300 // signal on a thread we didn't expect it on, the application got a signal
301 // outside of a crash recovery context, or something else went horribly
302 // wrong.
303 //
304 // Disable crash recovery and raise the signal again. The assumption here is
305 // that the enclosing application will terminate soon, and we won't want to
306 // attempt crash recovery again.
307 //
308 // This call of Disable isn't thread safe, but it doesn't actually matter.
309 CrashRecoveryContext::Disable();
310 raise(Signal);
Daniel Dunbar418e7042010-10-18 21:55:18 +0000311
312 // The signal will be thrown once the signal mask is restored.
313 return;
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000314 }
315
316 // Unblock the signal we received.
317 sigset_t SigMask;
318 sigemptyset(&SigMask);
319 sigaddset(&SigMask, Signal);
Craig Topperc10719f2014-04-07 04:17:22 +0000320 sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000321
322 if (CRCI)
323 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
324}
325
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000326static void installExceptionOrSignalHandlers() {
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000327 // Setup the signal handler.
328 struct sigaction Handler;
329 Handler.sa_handler = CrashRecoverySignalHandler;
330 Handler.sa_flags = 0;
331 sigemptyset(&Handler.sa_mask);
332
333 for (unsigned i = 0; i != NumSignals; ++i) {
Daniel Dunbarc90e82a2010-07-30 17:49:04 +0000334 sigaction(Signals[i], &Handler, &PrevActions[i]);
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000335 }
336}
337
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000338static void uninstallExceptionOrSignalHandlers() {
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000339 // Restore the previous signal handlers.
340 for (unsigned i = 0; i != NumSignals; ++i)
Craig Topperc10719f2014-04-07 04:17:22 +0000341 sigaction(Signals[i], &PrevActions[i], nullptr);
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000342}
343
Nico Weber712e8d22018-04-29 00:45:03 +0000344#endif // !_WIN32
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000345
Richard Smithc167d652014-05-06 01:44:26 +0000346bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000347 // If crash recovery is disabled, do nothing.
348 if (gCrashRecoveryEnabled) {
349 assert(!Impl && "Crash recovery context already initialized!");
Daniel Dunbarb30266e2010-08-17 22:32:37 +0000350 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000351 Impl = CRCI;
352
353 if (setjmp(CRCI->JumpBuffer) != 0) {
354 return false;
355 }
356 }
357
Richard Smithc167d652014-05-06 01:44:26 +0000358 Fn();
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000359 return true;
360}
361
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000362#endif // !_MSC_VER
363
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000364void CrashRecoveryContext::HandleCrash() {
365 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
366 assert(CRCI && "Crash recovery context never initialized!");
367 CRCI->HandleCrash();
368}
369
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000370// FIXME: Portability.
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000371static void setThreadBackgroundPriority() {
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000372#ifdef __APPLE__
373 setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG);
374#endif
375}
376
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000377static bool hasThreadBackgroundPriority() {
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000378#ifdef __APPLE__
379 return getpriority(PRIO_DARWIN_THREAD, 0) == 1;
380#else
381 return false;
382#endif
383}
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000384
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000385namespace {
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000386struct RunSafelyOnThreadInfo {
Richard Smithc167d652014-05-06 01:44:26 +0000387 function_ref<void()> Fn;
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000388 CrashRecoveryContext *CRC;
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000389 bool UseBackgroundPriority;
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000390 bool Result;
391};
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000392}
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000393
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000394static void RunSafelyOnThread_Dispatch(void *UserData) {
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000395 RunSafelyOnThreadInfo *Info =
396 reinterpret_cast<RunSafelyOnThreadInfo*>(UserData);
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000397
398 if (Info->UseBackgroundPriority)
399 setThreadBackgroundPriority();
400
Richard Smithc167d652014-05-06 01:44:26 +0000401 Info->Result = Info->CRC->RunSafely(Info->Fn);
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000402}
Richard Smithc167d652014-05-06 01:44:26 +0000403bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn,
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000404 unsigned RequestedStackSize) {
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000405 bool UseBackgroundPriority = hasThreadBackgroundPriority();
406 RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false };
Sam McCalla9c3c172019-10-23 15:34:48 +0200407 llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info,
408 RequestedStackSize == 0
409 ? llvm::None
410 : llvm::Optional<unsigned>(RequestedStackSize));
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +0000411 if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl)
412 CRC->setSwitchedThread();
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000413 return Info.Result;
414}