blob: bd38dd88201fdbbc2c894fac8792049659df3c28 [file] [log] [blame]
Daniel Dunbar19a3b372010-07-28 15:40:20 +00001//===--- CrashRecoveryContext.cpp - Crash Recovery ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbar9789f812010-07-29 01:52:04 +000011#include "llvm/Config/config.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/Support/ErrorHandling.h"
Filip Pizlof2189bf2013-09-12 17:46:57 +000013#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000014#include "llvm/Support/Mutex.h"
15#include "llvm/Support/ThreadLocal.h"
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000016#include <setjmp.h>
Daniel Dunbar19a3b372010-07-28 15:40:20 +000017using namespace llvm;
18
19namespace {
20
21struct CrashRecoveryContextImpl;
22
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000023static ManagedStatic<
Zachary Turnera40ccf62014-06-10 18:03:04 +000024 sys::ThreadLocal<const CrashRecoveryContextImpl> > CurrentContext;
Daniel Dunbaraf77e222010-07-29 01:21:47 +000025
Daniel Dunbar19a3b372010-07-28 15:40:20 +000026struct CrashRecoveryContextImpl {
Nico Weber26928112015-08-07 17:32:06 +000027 // When threads are disabled, this links up all active
28 // CrashRecoveryContextImpls. When threads are enabled there's one thread
29 // per CrashRecoveryContext and CurrentContext is a thread-local, so only one
30 // CrashRecoveryContextImpl is active per thread and this is always null.
Nico Weber28dc4172015-08-06 19:21:25 +000031 const CrashRecoveryContextImpl *Next;
32
Daniel Dunbarb30266e2010-08-17 22:32:37 +000033 CrashRecoveryContext *CRC;
Daniel Dunbar19a3b372010-07-28 15:40:20 +000034 ::jmp_buf JumpBuffer;
35 volatile unsigned Failed : 1;
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +000036 unsigned SwitchedThread : 1;
Daniel Dunbar19a3b372010-07-28 15:40:20 +000037
38public:
Daniel Dunbarb30266e2010-08-17 22:32:37 +000039 CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC),
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +000040 Failed(false),
41 SwitchedThread(false) {
Nico Weber28dc4172015-08-06 19:21:25 +000042 Next = CurrentContext->get();
Filip Pizlof2189bf2013-09-12 17:46:57 +000043 CurrentContext->set(this);
Daniel Dunbaraf77e222010-07-29 01:21:47 +000044 }
45 ~CrashRecoveryContextImpl() {
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +000046 if (!SwitchedThread)
Nico Weber28dc4172015-08-06 19:21:25 +000047 CurrentContext->set(Next);
Daniel Dunbaraf77e222010-07-29 01:21:47 +000048 }
Daniel Dunbar19a3b372010-07-28 15:40:20 +000049
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +000050 /// \brief Called when the separate crash-recovery thread was finished, to
51 /// indicate that we don't need to clear the thread-local CurrentContext.
Nico Weber28dc4172015-08-06 19:21:25 +000052 void setSwitchedThread() {
53#if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0
54 SwitchedThread = true;
55#endif
56 }
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +000057
Daniel Dunbar19a3b372010-07-28 15:40:20 +000058 void HandleCrash() {
Daniel Dunbar09b0c782010-08-17 22:32:39 +000059 // Eliminate the current context entry, to avoid re-entering in case the
60 // cleanup code crashes.
Nico Weber28dc4172015-08-06 19:21:25 +000061 CurrentContext->set(Next);
Daniel Dunbar09b0c782010-08-17 22:32:39 +000062
Daniel Dunbar19a3b372010-07-28 15:40:20 +000063 assert(!Failed && "Crash recovery context already failed!");
64 Failed = true;
65
66 // FIXME: Stash the backtrace.
67
68 // Jump back to the RunSafely we were called under.
69 longjmp(JumpBuffer, 1);
70 }
71};
72
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000073}
Daniel Dunbar19a3b372010-07-28 15:40:20 +000074
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000075static ManagedStatic<sys::Mutex> gCrashRecoveryContextMutex;
76static bool gCrashRecoveryEnabled = false;
77
78static ManagedStatic<sys::ThreadLocal<const CrashRecoveryContext>>
Ted Kremenekab1a2422011-03-21 18:38:03 +000079 tlIsRecoveringFromCrash;
80
Reid Kleckner710c1ce2017-05-17 18:16:17 +000081static void installExceptionOrSignalHandlers();
82static void uninstallExceptionOrSignalHandlers();
83
Ted Kremenekc44d3cf2011-03-18 02:05:11 +000084CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {}
85
Daniel Dunbar19a3b372010-07-28 15:40:20 +000086CrashRecoveryContext::~CrashRecoveryContext() {
Ted Kremenekc44d3cf2011-03-18 02:05:11 +000087 // Reclaim registered resources.
88 CrashRecoveryContextCleanup *i = head;
Nico Weber28dc4172015-08-06 19:21:25 +000089 const CrashRecoveryContext *PC = tlIsRecoveringFromCrash->get();
90 tlIsRecoveringFromCrash->set(this);
Ted Kremenekc44d3cf2011-03-18 02:05:11 +000091 while (i) {
92 CrashRecoveryContextCleanup *tmp = i;
93 i = tmp->next;
Ted Kremenek32aea2e2011-03-19 00:59:37 +000094 tmp->cleanupFired = true;
Ted Kremenek857e5352011-03-22 04:33:13 +000095 tmp->recoverResources();
Ted Kremenekc44d3cf2011-03-18 02:05:11 +000096 delete tmp;
97 }
Nico Weber28dc4172015-08-06 19:21:25 +000098 tlIsRecoveringFromCrash->set(PC);
Ted Kremenekc44d3cf2011-03-18 02:05:11 +000099
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000100 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
101 delete CRCI;
102}
103
Ted Kremenekab1a2422011-03-21 18:38:03 +0000104bool CrashRecoveryContext::isRecoveringFromCrash() {
Craig Topperc10719f2014-04-07 04:17:22 +0000105 return tlIsRecoveringFromCrash->get() != nullptr;
Ted Kremenekab1a2422011-03-21 18:38:03 +0000106}
107
Daniel Dunbarb30266e2010-08-17 22:32:37 +0000108CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
Ted Kremenek794a0712011-03-19 00:59:33 +0000109 if (!gCrashRecoveryEnabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000110 return nullptr;
Ted Kremenek794a0712011-03-19 00:59:33 +0000111
Filip Pizlof2189bf2013-09-12 17:46:57 +0000112 const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
Daniel Dunbarb30266e2010-08-17 22:32:37 +0000113 if (!CRCI)
Craig Topperc10719f2014-04-07 04:17:22 +0000114 return nullptr;
Daniel Dunbarb30266e2010-08-17 22:32:37 +0000115
116 return CRCI->CRC;
117}
118
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000119void CrashRecoveryContext::Enable() {
120 sys::ScopedLock L(*gCrashRecoveryContextMutex);
121 // FIXME: Shouldn't this be a refcount or something?
122 if (gCrashRecoveryEnabled)
123 return;
124 gCrashRecoveryEnabled = true;
125 installExceptionOrSignalHandlers();
126}
127
128void CrashRecoveryContext::Disable() {
129 sys::ScopedLock L(*gCrashRecoveryContextMutex);
130 if (!gCrashRecoveryEnabled)
131 return;
132 gCrashRecoveryEnabled = false;
133 uninstallExceptionOrSignalHandlers();
134}
135
Ted Kremenekc44d3cf2011-03-18 02:05:11 +0000136void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup)
137{
138 if (!cleanup)
139 return;
140 if (head)
141 head->prev = cleanup;
142 cleanup->next = head;
143 head = cleanup;
144}
145
146void
147CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) {
148 if (!cleanup)
149 return;
150 if (cleanup == head) {
151 head = cleanup->next;
152 if (head)
Craig Topperc10719f2014-04-07 04:17:22 +0000153 head->prev = nullptr;
Ted Kremenekc44d3cf2011-03-18 02:05:11 +0000154 }
155 else {
156 cleanup->prev->next = cleanup->next;
157 if (cleanup->next)
158 cleanup->next->prev = cleanup->prev;
159 }
160 delete cleanup;
161}
162
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000163#if defined(_MSC_VER)
164// If _MSC_VER is defined, we must have SEH. Use it if it's available. It's way
165// better than VEH. Vectored exception handling catches all exceptions happening
166// on the thread with installed exception handlers, so it can interfere with
167// internal exception handling of other libraries on that thread. SEH works
168// exactly as you would expect normal exception handling to work: it only
169// catches exceptions if they would bubble out from the stack frame with __try /
170// __except.
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000171
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000172static void installExceptionOrSignalHandlers() {}
173static void uninstallExceptionOrSignalHandlers() {}
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000174
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000175bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
176 if (!gCrashRecoveryEnabled) {
177 Fn();
178 return true;
179 }
180
181 bool Result = true;
182 __try {
183 Fn();
184 } __except (1) { // Catch any exception.
185 Result = false;
186 }
187 return Result;
188}
189
190#else // !_MSC_VER
191
192#if defined(LLVM_ON_WIN32)
193// This is a non-MSVC compiler, probably mingw gcc or clang without
194// -fms-extensions. Use vectored exception handling (VEH).
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000195//
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000196// On Windows, we can make use of vectored exception handling to catch most
197// crashing situations. Note that this does mean we will be alerted of
198// exceptions *before* structured exception handling has the opportunity to
199// catch it. Unfortunately, this causes problems in practice with other code
200// running on threads with LLVM crash recovery contexts, so we would like to
201// eventually move away from VEH.
202//
203// Vectored works on a per-thread basis, which is an advantage over
204// SetUnhandledExceptionFilter. SetUnhandledExceptionFilter also doesn't have
205// any native support for chaining exception handlers, but VEH allows more than
206// one.
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000207//
208// The vectored exception handler functionality was added in Windows
209// XP, so if support for older versions of Windows is required,
210// it will have to be added.
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000211
212#include "Windows/WindowsSupport.h"
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000213
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000214static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000215{
Zachary Turner0daa7072017-05-17 16:39:33 +0000216 // DBG_PRINTEXCEPTION_WIDE_C is not properly defined on all supported
217 // compilers and platforms, so we define it manually.
218 constexpr ULONG DbgPrintExceptionWideC = 0x4001000AL;
Zachary Turner13e87f42017-05-16 22:50:32 +0000219 switch (ExceptionInfo->ExceptionRecord->ExceptionCode)
220 {
221 case DBG_PRINTEXCEPTION_C:
Zachary Turner0daa7072017-05-17 16:39:33 +0000222 case DbgPrintExceptionWideC:
Zachary Turner13e87f42017-05-16 22:50:32 +0000223 case 0x406D1388: // set debugger thread name
224 return EXCEPTION_CONTINUE_EXECUTION;
225 }
226
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000227 // Lookup the current thread local recovery object.
Filip Pizlof2189bf2013-09-12 17:46:57 +0000228 const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000229
230 if (!CRCI) {
231 // Something has gone horribly wrong, so let's just tell everyone
232 // to keep searching
233 CrashRecoveryContext::Disable();
234 return EXCEPTION_CONTINUE_SEARCH;
235 }
236
237 // TODO: We can capture the stack backtrace here and store it on the
238 // implementation if we so choose.
239
240 // Handle the crash
241 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
242
243 // Note that we don't actually get here because HandleCrash calls
244 // longjmp, which means the HandleCrash function never returns.
245 llvm_unreachable("Handled the crash, should have longjmp'ed out of here");
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000246}
247
248// Because the Enable and Disable calls are static, it means that
249// there may not actually be an Impl available, or even a current
250// CrashRecoveryContext at all. So we make use of a thread-local
251// exception table. The handles contained in here will either be
252// non-NULL, valid VEH handles, or NULL.
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000253static sys::ThreadLocal<const void> sCurrentExceptionHandle;
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000254
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000255static void installExceptionOrSignalHandlers() {
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000256 // We can set up vectored exception handling now. We will install our
257 // handler as the front of the list, though there's no assurances that
258 // it will remain at the front (another call could install itself before
259 // our handler). This 1) isn't likely, and 2) shouldn't cause problems.
260 PVOID handle = ::AddVectoredExceptionHandler(1, ExceptionHandler);
261 sCurrentExceptionHandle.set(handle);
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000262}
263
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000264static void uninstallExceptionOrSignalHandlers() {
NAKAMURA Takumi1eae12c2011-08-20 06:35:36 +0000265 PVOID currentHandle = const_cast<PVOID>(sCurrentExceptionHandle.get());
266 if (currentHandle) {
267 // Now we can remove the vectored exception handler from the chain
268 ::RemoveVectoredExceptionHandler(currentHandle);
269
270 // Reset the handle in our thread-local set.
271 sCurrentExceptionHandle.set(NULL);
272 }
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000273}
274
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000275#else // !LLVM_ON_WIN32
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000276
277// Generic POSIX implementation.
278//
279// This implementation relies on synchronous signals being delivered to the
280// current thread. We use a thread local object to keep track of the active
281// crash recovery context, and install signal handlers to invoke HandleCrash on
282// the active object.
283//
284// This implementation does not to attempt to chain signal handlers in any
285// reliable fashion -- if we get a signal outside of a crash recovery context we
286// simply disable crash recovery and raise the signal again.
287
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000288#include <signal.h>
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000289
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000290static const int Signals[] =
291 { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
292static const unsigned NumSignals = array_lengthof(Signals);
293static struct sigaction PrevActions[NumSignals];
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000294
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000295static void CrashRecoverySignalHandler(int Signal) {
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000296 // Lookup the current thread local recovery object.
Filip Pizlof2189bf2013-09-12 17:46:57 +0000297 const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000298
299 if (!CRCI) {
300 // We didn't find a crash recovery context -- this means either we got a
301 // signal on a thread we didn't expect it on, the application got a signal
302 // outside of a crash recovery context, or something else went horribly
303 // wrong.
304 //
305 // Disable crash recovery and raise the signal again. The assumption here is
306 // that the enclosing application will terminate soon, and we won't want to
307 // attempt crash recovery again.
308 //
309 // This call of Disable isn't thread safe, but it doesn't actually matter.
310 CrashRecoveryContext::Disable();
311 raise(Signal);
Daniel Dunbar418e7042010-10-18 21:55:18 +0000312
313 // The signal will be thrown once the signal mask is restored.
314 return;
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000315 }
316
317 // Unblock the signal we received.
318 sigset_t SigMask;
319 sigemptyset(&SigMask);
320 sigaddset(&SigMask, Signal);
Craig Topperc10719f2014-04-07 04:17:22 +0000321 sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000322
323 if (CRCI)
324 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
325}
326
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000327static void installExceptionOrSignalHandlers() {
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000328 // Setup the signal handler.
329 struct sigaction Handler;
330 Handler.sa_handler = CrashRecoverySignalHandler;
331 Handler.sa_flags = 0;
332 sigemptyset(&Handler.sa_mask);
333
334 for (unsigned i = 0; i != NumSignals; ++i) {
Daniel Dunbarc90e82a2010-07-30 17:49:04 +0000335 sigaction(Signals[i], &Handler, &PrevActions[i]);
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000336 }
337}
338
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000339static void uninstallExceptionOrSignalHandlers() {
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000340 // Restore the previous signal handlers.
341 for (unsigned i = 0; i != NumSignals; ++i)
Craig Topperc10719f2014-04-07 04:17:22 +0000342 sigaction(Signals[i], &PrevActions[i], nullptr);
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000343}
344
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000345#endif // !LLVM_ON_WIN32
Daniel Dunbaraf77e222010-07-29 01:21:47 +0000346
Richard Smithc167d652014-05-06 01:44:26 +0000347bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000348 // If crash recovery is disabled, do nothing.
349 if (gCrashRecoveryEnabled) {
350 assert(!Impl && "Crash recovery context already initialized!");
Daniel Dunbarb30266e2010-08-17 22:32:37 +0000351 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000352 Impl = CRCI;
353
354 if (setjmp(CRCI->JumpBuffer) != 0) {
355 return false;
356 }
357 }
358
Richard Smithc167d652014-05-06 01:44:26 +0000359 Fn();
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000360 return true;
361}
362
Reid Kleckner710c1ce2017-05-17 18:16:17 +0000363#endif // !_MSC_VER
364
Daniel Dunbar19a3b372010-07-28 15:40:20 +0000365void CrashRecoveryContext::HandleCrash() {
366 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
367 assert(CRCI && "Crash recovery context never initialized!");
368 CRCI->HandleCrash();
369}
370
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000371// FIXME: Portability.
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000372static void setThreadBackgroundPriority() {
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000373#ifdef __APPLE__
374 setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG);
375#endif
376}
377
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000378static bool hasThreadBackgroundPriority() {
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000379#ifdef __APPLE__
380 return getpriority(PRIO_DARWIN_THREAD, 0) == 1;
381#else
382 return false;
383#endif
384}
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000385
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000386namespace {
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000387struct RunSafelyOnThreadInfo {
Richard Smithc167d652014-05-06 01:44:26 +0000388 function_ref<void()> Fn;
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000389 CrashRecoveryContext *CRC;
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000390 bool UseBackgroundPriority;
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000391 bool Result;
392};
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000393}
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000394
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000395static void RunSafelyOnThread_Dispatch(void *UserData) {
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000396 RunSafelyOnThreadInfo *Info =
397 reinterpret_cast<RunSafelyOnThreadInfo*>(UserData);
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000398
399 if (Info->UseBackgroundPriority)
400 setThreadBackgroundPriority();
401
Richard Smithc167d652014-05-06 01:44:26 +0000402 Info->Result = Info->CRC->RunSafely(Info->Fn);
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000403}
Richard Smithc167d652014-05-06 01:44:26 +0000404bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn,
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000405 unsigned RequestedStackSize) {
Argyrios Kyrtzidise9012b02014-06-25 23:54:50 +0000406 bool UseBackgroundPriority = hasThreadBackgroundPriority();
407 RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false };
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000408 llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize);
Argyrios Kyrtzidisf1d8f522013-06-19 22:53:45 +0000409 if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl)
410 CRC->setSwitchedThread();
Daniel Dunbarf4d90ba2010-11-05 07:19:09 +0000411 return Info.Result;
412}