Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 1 | //===--- 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 Dunbar | 9789f81 | 2010-07-29 01:52:04 +0000 | [diff] [blame] | 11 | #include "llvm/Config/config.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ErrorHandling.h" |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 13 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Mutex.h" |
| 15 | #include "llvm/Support/ThreadLocal.h" |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 16 | #include <setjmp.h> |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | struct CrashRecoveryContextImpl; |
| 22 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 23 | static ManagedStatic< |
Zachary Turner | a40ccf6 | 2014-06-10 18:03:04 +0000 | [diff] [blame] | 24 | sys::ThreadLocal<const CrashRecoveryContextImpl> > CurrentContext; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 25 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 26 | struct CrashRecoveryContextImpl { |
Nico Weber | 2692811 | 2015-08-07 17:32:06 +0000 | [diff] [blame] | 27 | // 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 Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 31 | const CrashRecoveryContextImpl *Next; |
| 32 | |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 33 | CrashRecoveryContext *CRC; |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 34 | ::jmp_buf JumpBuffer; |
| 35 | volatile unsigned Failed : 1; |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 36 | unsigned SwitchedThread : 1; |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 37 | |
| 38 | public: |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 39 | CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC), |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 40 | Failed(false), |
| 41 | SwitchedThread(false) { |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 42 | Next = CurrentContext->get(); |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 43 | CurrentContext->set(this); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 44 | } |
| 45 | ~CrashRecoveryContextImpl() { |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 46 | if (!SwitchedThread) |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 47 | CurrentContext->set(Next); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 48 | } |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 49 | |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 50 | /// \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 Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 52 | void setSwitchedThread() { |
| 53 | #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0 |
| 54 | SwitchedThread = true; |
| 55 | #endif |
| 56 | } |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 57 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 58 | void HandleCrash() { |
Daniel Dunbar | 09b0c78 | 2010-08-17 22:32:39 +0000 | [diff] [blame] | 59 | // Eliminate the current context entry, to avoid re-entering in case the |
| 60 | // cleanup code crashes. |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 61 | CurrentContext->set(Next); |
Daniel Dunbar | 09b0c78 | 2010-08-17 22:32:39 +0000 | [diff] [blame] | 62 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 63 | 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 Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 73 | } |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 74 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 75 | static ManagedStatic<sys::Mutex> gCrashRecoveryContextMutex; |
| 76 | static bool gCrashRecoveryEnabled = false; |
| 77 | |
| 78 | static ManagedStatic<sys::ThreadLocal<const CrashRecoveryContext>> |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 79 | tlIsRecoveringFromCrash; |
| 80 | |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 81 | CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {} |
| 82 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 83 | CrashRecoveryContext::~CrashRecoveryContext() { |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 84 | // Reclaim registered resources. |
| 85 | CrashRecoveryContextCleanup *i = head; |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 86 | const CrashRecoveryContext *PC = tlIsRecoveringFromCrash->get(); |
| 87 | tlIsRecoveringFromCrash->set(this); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 88 | while (i) { |
| 89 | CrashRecoveryContextCleanup *tmp = i; |
| 90 | i = tmp->next; |
Ted Kremenek | 32aea2e | 2011-03-19 00:59:37 +0000 | [diff] [blame] | 91 | tmp->cleanupFired = true; |
Ted Kremenek | 857e535 | 2011-03-22 04:33:13 +0000 | [diff] [blame] | 92 | tmp->recoverResources(); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 93 | delete tmp; |
| 94 | } |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 95 | tlIsRecoveringFromCrash->set(PC); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 97 | CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; |
| 98 | delete CRCI; |
| 99 | } |
| 100 | |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 101 | bool CrashRecoveryContext::isRecoveringFromCrash() { |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 102 | return tlIsRecoveringFromCrash->get() != nullptr; |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 105 | CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { |
Ted Kremenek | 794a071 | 2011-03-19 00:59:33 +0000 | [diff] [blame] | 106 | if (!gCrashRecoveryEnabled) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 107 | return nullptr; |
Ted Kremenek | 794a071 | 2011-03-19 00:59:33 +0000 | [diff] [blame] | 108 | |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 109 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 110 | if (!CRCI) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 111 | return nullptr; |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 112 | |
| 113 | return CRCI->CRC; |
| 114 | } |
| 115 | |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 116 | void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup) |
| 117 | { |
| 118 | if (!cleanup) |
| 119 | return; |
| 120 | if (head) |
| 121 | head->prev = cleanup; |
| 122 | cleanup->next = head; |
| 123 | head = cleanup; |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) { |
| 128 | if (!cleanup) |
| 129 | return; |
| 130 | if (cleanup == head) { |
| 131 | head = cleanup->next; |
| 132 | if (head) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 133 | head->prev = nullptr; |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 134 | } |
| 135 | else { |
| 136 | cleanup->prev->next = cleanup->next; |
| 137 | if (cleanup->next) |
| 138 | cleanup->next->prev = cleanup->prev; |
| 139 | } |
| 140 | delete cleanup; |
| 141 | } |
| 142 | |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 143 | #ifdef LLVM_ON_WIN32 |
| 144 | |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 145 | #include "Windows/WindowsSupport.h" |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 146 | |
| 147 | // On Windows, we can make use of vectored exception handling to |
| 148 | // catch most crashing situations. Note that this does mean |
| 149 | // we will be alerted of exceptions *before* structured exception |
| 150 | // handling has the opportunity to catch it. But that isn't likely |
| 151 | // to cause problems because nowhere in the project is SEH being |
| 152 | // used. |
| 153 | // |
| 154 | // Vectored exception handling is built on top of SEH, and so it |
| 155 | // works on a per-thread basis. |
| 156 | // |
| 157 | // The vectored exception handler functionality was added in Windows |
| 158 | // XP, so if support for older versions of Windows is required, |
| 159 | // it will have to be added. |
| 160 | // |
| 161 | // If we want to support as far back as Win2k, we could use the |
| 162 | // SetUnhandledExceptionFilter API, but there's a risk of that |
| 163 | // being entirely overwritten (it's not a chain). |
| 164 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 165 | static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 166 | { |
| 167 | // Lookup the current thread local recovery object. |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 168 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 169 | |
| 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 | |
| 177 | // TODO: We can capture the stack backtrace here and store it on the |
| 178 | // implementation if we so choose. |
| 179 | |
| 180 | // Handle the crash |
| 181 | const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash(); |
| 182 | |
| 183 | // Note that we don't actually get here because HandleCrash calls |
| 184 | // longjmp, which means the HandleCrash function never returns. |
| 185 | llvm_unreachable("Handled the crash, should have longjmp'ed out of here"); |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Because the Enable and Disable calls are static, it means that |
| 189 | // there may not actually be an Impl available, or even a current |
| 190 | // CrashRecoveryContext at all. So we make use of a thread-local |
| 191 | // exception table. The handles contained in here will either be |
| 192 | // non-NULL, valid VEH handles, or NULL. |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 193 | static sys::ThreadLocal<const void> sCurrentExceptionHandle; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 194 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 195 | void CrashRecoveryContext::Enable() { |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 196 | sys::ScopedLock L(*gCrashRecoveryContextMutex); |
Daniel Dunbar | ff32994 | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 197 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 198 | if (gCrashRecoveryEnabled) |
| 199 | return; |
| 200 | |
| 201 | gCrashRecoveryEnabled = true; |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 202 | |
| 203 | // We can set up vectored exception handling now. We will install our |
| 204 | // handler as the front of the list, though there's no assurances that |
| 205 | // it will remain at the front (another call could install itself before |
| 206 | // our handler). This 1) isn't likely, and 2) shouldn't cause problems. |
| 207 | PVOID handle = ::AddVectoredExceptionHandler(1, ExceptionHandler); |
| 208 | sCurrentExceptionHandle.set(handle); |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void CrashRecoveryContext::Disable() { |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 212 | sys::ScopedLock L(*gCrashRecoveryContextMutex); |
Daniel Dunbar | ff32994 | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 213 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 214 | if (!gCrashRecoveryEnabled) |
| 215 | return; |
| 216 | |
| 217 | gCrashRecoveryEnabled = false; |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 218 | |
| 219 | PVOID currentHandle = const_cast<PVOID>(sCurrentExceptionHandle.get()); |
| 220 | if (currentHandle) { |
| 221 | // Now we can remove the vectored exception handler from the chain |
| 222 | ::RemoveVectoredExceptionHandler(currentHandle); |
| 223 | |
| 224 | // Reset the handle in our thread-local set. |
| 225 | sCurrentExceptionHandle.set(NULL); |
| 226 | } |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 229 | #else |
| 230 | |
| 231 | // Generic POSIX implementation. |
| 232 | // |
| 233 | // This implementation relies on synchronous signals being delivered to the |
| 234 | // current thread. We use a thread local object to keep track of the active |
| 235 | // crash recovery context, and install signal handlers to invoke HandleCrash on |
| 236 | // the active object. |
| 237 | // |
| 238 | // This implementation does not to attempt to chain signal handlers in any |
| 239 | // reliable fashion -- if we get a signal outside of a crash recovery context we |
| 240 | // simply disable crash recovery and raise the signal again. |
| 241 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 242 | #include <signal.h> |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 243 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 244 | static const int Signals[] = |
| 245 | { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP }; |
| 246 | static const unsigned NumSignals = array_lengthof(Signals); |
| 247 | static struct sigaction PrevActions[NumSignals]; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 248 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 249 | static void CrashRecoverySignalHandler(int Signal) { |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 250 | // Lookup the current thread local recovery object. |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 251 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 252 | |
| 253 | if (!CRCI) { |
| 254 | // We didn't find a crash recovery context -- this means either we got a |
| 255 | // signal on a thread we didn't expect it on, the application got a signal |
| 256 | // outside of a crash recovery context, or something else went horribly |
| 257 | // wrong. |
| 258 | // |
| 259 | // Disable crash recovery and raise the signal again. The assumption here is |
| 260 | // that the enclosing application will terminate soon, and we won't want to |
| 261 | // attempt crash recovery again. |
| 262 | // |
| 263 | // This call of Disable isn't thread safe, but it doesn't actually matter. |
| 264 | CrashRecoveryContext::Disable(); |
| 265 | raise(Signal); |
Daniel Dunbar | 418e704 | 2010-10-18 21:55:18 +0000 | [diff] [blame] | 266 | |
| 267 | // The signal will be thrown once the signal mask is restored. |
| 268 | return; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // Unblock the signal we received. |
| 272 | sigset_t SigMask; |
| 273 | sigemptyset(&SigMask); |
| 274 | sigaddset(&SigMask, Signal); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 275 | sigprocmask(SIG_UNBLOCK, &SigMask, nullptr); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 276 | |
| 277 | if (CRCI) |
| 278 | const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash(); |
| 279 | } |
| 280 | |
| 281 | void CrashRecoveryContext::Enable() { |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 282 | sys::ScopedLock L(*gCrashRecoveryContextMutex); |
Daniel Dunbar | ff32994 | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 283 | |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 284 | if (gCrashRecoveryEnabled) |
| 285 | return; |
| 286 | |
| 287 | gCrashRecoveryEnabled = true; |
| 288 | |
| 289 | // Setup the signal handler. |
| 290 | struct sigaction Handler; |
| 291 | Handler.sa_handler = CrashRecoverySignalHandler; |
| 292 | Handler.sa_flags = 0; |
| 293 | sigemptyset(&Handler.sa_mask); |
| 294 | |
| 295 | for (unsigned i = 0; i != NumSignals; ++i) { |
Daniel Dunbar | c90e82a | 2010-07-30 17:49:04 +0000 | [diff] [blame] | 296 | sigaction(Signals[i], &Handler, &PrevActions[i]); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | void CrashRecoveryContext::Disable() { |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 301 | sys::ScopedLock L(*gCrashRecoveryContextMutex); |
Daniel Dunbar | ff32994 | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 302 | |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 303 | if (!gCrashRecoveryEnabled) |
| 304 | return; |
| 305 | |
| 306 | gCrashRecoveryEnabled = false; |
| 307 | |
| 308 | // Restore the previous signal handlers. |
| 309 | for (unsigned i = 0; i != NumSignals; ++i) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 310 | sigaction(Signals[i], &PrevActions[i], nullptr); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | #endif |
| 314 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 315 | bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) { |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 316 | // If crash recovery is disabled, do nothing. |
| 317 | if (gCrashRecoveryEnabled) { |
| 318 | assert(!Impl && "Crash recovery context already initialized!"); |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 319 | CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this); |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 320 | Impl = CRCI; |
| 321 | |
| 322 | if (setjmp(CRCI->JumpBuffer) != 0) { |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 327 | Fn(); |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 328 | return true; |
| 329 | } |
| 330 | |
| 331 | void CrashRecoveryContext::HandleCrash() { |
| 332 | CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; |
| 333 | assert(CRCI && "Crash recovery context never initialized!"); |
| 334 | CRCI->HandleCrash(); |
| 335 | } |
| 336 | |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 337 | // FIXME: Portability. |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 338 | static void setThreadBackgroundPriority() { |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 339 | #ifdef __APPLE__ |
| 340 | setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); |
| 341 | #endif |
| 342 | } |
| 343 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 344 | static bool hasThreadBackgroundPriority() { |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 345 | #ifdef __APPLE__ |
| 346 | return getpriority(PRIO_DARWIN_THREAD, 0) == 1; |
| 347 | #else |
| 348 | return false; |
| 349 | #endif |
| 350 | } |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 351 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 352 | namespace { |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 353 | struct RunSafelyOnThreadInfo { |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 354 | function_ref<void()> Fn; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 355 | CrashRecoveryContext *CRC; |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 356 | bool UseBackgroundPriority; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 357 | bool Result; |
| 358 | }; |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 359 | } |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 360 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 361 | static void RunSafelyOnThread_Dispatch(void *UserData) { |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 362 | RunSafelyOnThreadInfo *Info = |
| 363 | reinterpret_cast<RunSafelyOnThreadInfo*>(UserData); |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 364 | |
| 365 | if (Info->UseBackgroundPriority) |
| 366 | setThreadBackgroundPriority(); |
| 367 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 368 | Info->Result = Info->CRC->RunSafely(Info->Fn); |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 369 | } |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 370 | bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn, |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 371 | unsigned RequestedStackSize) { |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 372 | bool UseBackgroundPriority = hasThreadBackgroundPriority(); |
| 373 | RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false }; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 374 | llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize); |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 375 | if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl) |
| 376 | CRC->setSwitchedThread(); |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 377 | return Info.Result; |
| 378 | } |