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 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 81 | static void installExceptionOrSignalHandlers(); |
| 82 | static void uninstallExceptionOrSignalHandlers(); |
| 83 | |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 84 | CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {} |
| 85 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 86 | CrashRecoveryContext::~CrashRecoveryContext() { |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 87 | // Reclaim registered resources. |
| 88 | CrashRecoveryContextCleanup *i = head; |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 89 | const CrashRecoveryContext *PC = tlIsRecoveringFromCrash->get(); |
| 90 | tlIsRecoveringFromCrash->set(this); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 91 | while (i) { |
| 92 | CrashRecoveryContextCleanup *tmp = i; |
| 93 | i = tmp->next; |
Ted Kremenek | 32aea2e | 2011-03-19 00:59:37 +0000 | [diff] [blame] | 94 | tmp->cleanupFired = true; |
Ted Kremenek | 857e535 | 2011-03-22 04:33:13 +0000 | [diff] [blame] | 95 | tmp->recoverResources(); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 96 | delete tmp; |
| 97 | } |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 98 | tlIsRecoveringFromCrash->set(PC); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 99 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 100 | CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; |
| 101 | delete CRCI; |
| 102 | } |
| 103 | |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 104 | bool CrashRecoveryContext::isRecoveringFromCrash() { |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 105 | return tlIsRecoveringFromCrash->get() != nullptr; |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 108 | CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { |
Ted Kremenek | 794a071 | 2011-03-19 00:59:33 +0000 | [diff] [blame] | 109 | if (!gCrashRecoveryEnabled) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 110 | return nullptr; |
Ted Kremenek | 794a071 | 2011-03-19 00:59:33 +0000 | [diff] [blame] | 111 | |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 112 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 113 | if (!CRCI) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 114 | return nullptr; |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 115 | |
| 116 | return CRCI->CRC; |
| 117 | } |
| 118 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 119 | void 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 | |
| 128 | void CrashRecoveryContext::Disable() { |
| 129 | sys::ScopedLock L(*gCrashRecoveryContextMutex); |
| 130 | if (!gCrashRecoveryEnabled) |
| 131 | return; |
| 132 | gCrashRecoveryEnabled = false; |
| 133 | uninstallExceptionOrSignalHandlers(); |
| 134 | } |
| 135 | |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 136 | void 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 | |
| 146 | void |
| 147 | CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) { |
| 148 | if (!cleanup) |
| 149 | return; |
| 150 | if (cleanup == head) { |
| 151 | head = cleanup->next; |
| 152 | if (head) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 153 | head->prev = nullptr; |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 154 | } |
| 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 Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 163 | #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 Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 171 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 172 | static void installExceptionOrSignalHandlers() {} |
| 173 | static void uninstallExceptionOrSignalHandlers() {} |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 174 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 175 | bool 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 Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 195 | // |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 196 | // 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 Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 207 | // |
| 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 Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 211 | |
| 212 | #include "Windows/WindowsSupport.h" |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 213 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 214 | static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 215 | { |
Zachary Turner | 0daa707 | 2017-05-17 16:39:33 +0000 | [diff] [blame] | 216 | // 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 Turner | 13e87f4 | 2017-05-16 22:50:32 +0000 | [diff] [blame] | 219 | switch (ExceptionInfo->ExceptionRecord->ExceptionCode) |
| 220 | { |
| 221 | case DBG_PRINTEXCEPTION_C: |
Zachary Turner | 0daa707 | 2017-05-17 16:39:33 +0000 | [diff] [blame] | 222 | case DbgPrintExceptionWideC: |
Zachary Turner | 13e87f4 | 2017-05-16 22:50:32 +0000 | [diff] [blame] | 223 | case 0x406D1388: // set debugger thread name |
| 224 | return EXCEPTION_CONTINUE_EXECUTION; |
| 225 | } |
| 226 | |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 227 | // Lookup the current thread local recovery object. |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 228 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 229 | |
| 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 Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 246 | } |
| 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 Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 253 | static sys::ThreadLocal<const void> sCurrentExceptionHandle; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 254 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 255 | static void installExceptionOrSignalHandlers() { |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 256 | // 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 Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 264 | static void uninstallExceptionOrSignalHandlers() { |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 265 | 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 Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 275 | #else // !LLVM_ON_WIN32 |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 276 | |
| 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 Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 288 | #include <signal.h> |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 289 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 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]; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 294 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 295 | static void CrashRecoverySignalHandler(int Signal) { |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 296 | // Lookup the current thread local recovery object. |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 297 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 298 | |
| 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 Dunbar | 418e704 | 2010-10-18 21:55:18 +0000 | [diff] [blame] | 312 | |
| 313 | // The signal will be thrown once the signal mask is restored. |
| 314 | return; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Unblock the signal we received. |
| 318 | sigset_t SigMask; |
| 319 | sigemptyset(&SigMask); |
| 320 | sigaddset(&SigMask, Signal); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 321 | sigprocmask(SIG_UNBLOCK, &SigMask, nullptr); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 322 | |
| 323 | if (CRCI) |
| 324 | const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash(); |
| 325 | } |
| 326 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 327 | static void installExceptionOrSignalHandlers() { |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 328 | // 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 Dunbar | c90e82a | 2010-07-30 17:49:04 +0000 | [diff] [blame] | 335 | sigaction(Signals[i], &Handler, &PrevActions[i]); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 339 | static void uninstallExceptionOrSignalHandlers() { |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 340 | // Restore the previous signal handlers. |
| 341 | for (unsigned i = 0; i != NumSignals; ++i) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 342 | sigaction(Signals[i], &PrevActions[i], nullptr); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 345 | #endif // !LLVM_ON_WIN32 |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 346 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 347 | bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) { |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 348 | // If crash recovery is disabled, do nothing. |
| 349 | if (gCrashRecoveryEnabled) { |
| 350 | assert(!Impl && "Crash recovery context already initialized!"); |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 351 | CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this); |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 352 | Impl = CRCI; |
| 353 | |
| 354 | if (setjmp(CRCI->JumpBuffer) != 0) { |
| 355 | return false; |
| 356 | } |
| 357 | } |
| 358 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 359 | Fn(); |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 360 | return true; |
| 361 | } |
| 362 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 363 | #endif // !_MSC_VER |
| 364 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 365 | void CrashRecoveryContext::HandleCrash() { |
| 366 | CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; |
| 367 | assert(CRCI && "Crash recovery context never initialized!"); |
| 368 | CRCI->HandleCrash(); |
| 369 | } |
| 370 | |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 371 | // FIXME: Portability. |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 372 | static void setThreadBackgroundPriority() { |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 373 | #ifdef __APPLE__ |
| 374 | setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); |
| 375 | #endif |
| 376 | } |
| 377 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 378 | static bool hasThreadBackgroundPriority() { |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 379 | #ifdef __APPLE__ |
| 380 | return getpriority(PRIO_DARWIN_THREAD, 0) == 1; |
| 381 | #else |
| 382 | return false; |
| 383 | #endif |
| 384 | } |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 385 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 386 | namespace { |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 387 | struct RunSafelyOnThreadInfo { |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 388 | function_ref<void()> Fn; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 389 | CrashRecoveryContext *CRC; |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 390 | bool UseBackgroundPriority; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 391 | bool Result; |
| 392 | }; |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 393 | } |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 394 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 395 | static void RunSafelyOnThread_Dispatch(void *UserData) { |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 396 | RunSafelyOnThreadInfo *Info = |
| 397 | reinterpret_cast<RunSafelyOnThreadInfo*>(UserData); |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 398 | |
| 399 | if (Info->UseBackgroundPriority) |
| 400 | setThreadBackgroundPriority(); |
| 401 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 402 | Info->Result = Info->CRC->RunSafely(Info->Fn); |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 403 | } |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 404 | bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn, |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 405 | unsigned RequestedStackSize) { |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 406 | bool UseBackgroundPriority = hasThreadBackgroundPriority(); |
| 407 | RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false }; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 408 | llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize); |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 409 | if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl) |
| 410 | CRC->setSwitchedThread(); |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 411 | return Info.Result; |
| 412 | } |