Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 1 | //===--- CrashRecoveryContext.cpp - Crash Recovery ------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Support/CrashRecoveryContext.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 10 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "llvm/Support/ErrorHandling.h" |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 13 | #include "llvm/Support/ThreadLocal.h" |
Benjamin Kramer | 928071a | 2019-08-19 19:49:57 +0000 | [diff] [blame] | 14 | #include <mutex> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 15 | #include <setjmp.h> |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | struct CrashRecoveryContextImpl; |
| 21 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 22 | static ManagedStatic< |
Zachary Turner | a40ccf6 | 2014-06-10 18:03:04 +0000 | [diff] [blame] | 23 | sys::ThreadLocal<const CrashRecoveryContextImpl> > CurrentContext; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 24 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 25 | struct CrashRecoveryContextImpl { |
Nico Weber | 2692811 | 2015-08-07 17:32:06 +0000 | [diff] [blame] | 26 | // When threads are disabled, this links up all active |
| 27 | // CrashRecoveryContextImpls. When threads are enabled there's one thread |
| 28 | // per CrashRecoveryContext and CurrentContext is a thread-local, so only one |
| 29 | // CrashRecoveryContextImpl is active per thread and this is always null. |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 30 | const CrashRecoveryContextImpl *Next; |
| 31 | |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 32 | CrashRecoveryContext *CRC; |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 33 | ::jmp_buf JumpBuffer; |
| 34 | volatile unsigned Failed : 1; |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 35 | unsigned SwitchedThread : 1; |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 36 | |
| 37 | public: |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 38 | CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC), |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 39 | Failed(false), |
| 40 | SwitchedThread(false) { |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 41 | Next = CurrentContext->get(); |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 42 | CurrentContext->set(this); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 43 | } |
| 44 | ~CrashRecoveryContextImpl() { |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 45 | if (!SwitchedThread) |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 46 | CurrentContext->set(Next); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 47 | } |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 48 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 49 | /// Called when the separate crash-recovery thread was finished, to |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 50 | /// indicate that we don't need to clear the thread-local CurrentContext. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 51 | void setSwitchedThread() { |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 52 | #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0 |
| 53 | SwitchedThread = true; |
| 54 | #endif |
| 55 | } |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 56 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 57 | void HandleCrash() { |
Daniel Dunbar | 09b0c78 | 2010-08-17 22:32:39 +0000 | [diff] [blame] | 58 | // Eliminate the current context entry, to avoid re-entering in case the |
| 59 | // cleanup code crashes. |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 60 | CurrentContext->set(Next); |
Daniel Dunbar | 09b0c78 | 2010-08-17 22:32:39 +0000 | [diff] [blame] | 61 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 62 | 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 Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 72 | } |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 73 | |
Benjamin Kramer | 928071a | 2019-08-19 19:49:57 +0000 | [diff] [blame] | 74 | static ManagedStatic<std::mutex> gCrashRecoveryContextMutex; |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 75 | static bool gCrashRecoveryEnabled = false; |
| 76 | |
| 77 | static ManagedStatic<sys::ThreadLocal<const CrashRecoveryContext>> |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 78 | tlIsRecoveringFromCrash; |
| 79 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 80 | static void installExceptionOrSignalHandlers(); |
| 81 | static void uninstallExceptionOrSignalHandlers(); |
| 82 | |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 83 | CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {} |
| 84 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 85 | CrashRecoveryContext::~CrashRecoveryContext() { |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 86 | // Reclaim registered resources. |
| 87 | CrashRecoveryContextCleanup *i = head; |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 88 | const CrashRecoveryContext *PC = tlIsRecoveringFromCrash->get(); |
| 89 | tlIsRecoveringFromCrash->set(this); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 90 | while (i) { |
| 91 | CrashRecoveryContextCleanup *tmp = i; |
| 92 | i = tmp->next; |
Ted Kremenek | 32aea2e | 2011-03-19 00:59:37 +0000 | [diff] [blame] | 93 | tmp->cleanupFired = true; |
Ted Kremenek | 857e535 | 2011-03-22 04:33:13 +0000 | [diff] [blame] | 94 | tmp->recoverResources(); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 95 | delete tmp; |
| 96 | } |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 97 | tlIsRecoveringFromCrash->set(PC); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 98 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 99 | CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; |
| 100 | delete CRCI; |
| 101 | } |
| 102 | |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 103 | bool CrashRecoveryContext::isRecoveringFromCrash() { |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 104 | return tlIsRecoveringFromCrash->get() != nullptr; |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 107 | CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { |
Ted Kremenek | 794a071 | 2011-03-19 00:59:33 +0000 | [diff] [blame] | 108 | if (!gCrashRecoveryEnabled) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 109 | return nullptr; |
Ted Kremenek | 794a071 | 2011-03-19 00:59:33 +0000 | [diff] [blame] | 110 | |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 111 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 112 | if (!CRCI) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 113 | return nullptr; |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 114 | |
| 115 | return CRCI->CRC; |
| 116 | } |
| 117 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 118 | void CrashRecoveryContext::Enable() { |
Benjamin Kramer | 928071a | 2019-08-19 19:49:57 +0000 | [diff] [blame] | 119 | std::lock_guard<std::mutex> L(*gCrashRecoveryContextMutex); |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 120 | // FIXME: Shouldn't this be a refcount or something? |
| 121 | if (gCrashRecoveryEnabled) |
| 122 | return; |
| 123 | gCrashRecoveryEnabled = true; |
| 124 | installExceptionOrSignalHandlers(); |
| 125 | } |
| 126 | |
| 127 | void CrashRecoveryContext::Disable() { |
Benjamin Kramer | 928071a | 2019-08-19 19:49:57 +0000 | [diff] [blame] | 128 | std::lock_guard<std::mutex> L(*gCrashRecoveryContextMutex); |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 129 | if (!gCrashRecoveryEnabled) |
| 130 | return; |
| 131 | gCrashRecoveryEnabled = false; |
| 132 | uninstallExceptionOrSignalHandlers(); |
| 133 | } |
| 134 | |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 135 | void 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 | |
| 145 | void |
| 146 | CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) { |
| 147 | if (!cleanup) |
| 148 | return; |
| 149 | if (cleanup == head) { |
| 150 | head = cleanup->next; |
| 151 | if (head) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 152 | head->prev = nullptr; |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 153 | } |
| 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 Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 162 | #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 Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 170 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 171 | static void installExceptionOrSignalHandlers() {} |
| 172 | static void uninstallExceptionOrSignalHandlers() {} |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 173 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 174 | bool 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 Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 191 | #if defined(_WIN32) |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 192 | // This is a non-MSVC compiler, probably mingw gcc or clang without |
| 193 | // -fms-extensions. Use vectored exception handling (VEH). |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 194 | // |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 195 | // 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 Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 206 | // |
| 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 Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 210 | |
| 211 | #include "Windows/WindowsSupport.h" |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 212 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 213 | static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 214 | { |
Zachary Turner | 0daa707 | 2017-05-17 16:39:33 +0000 | [diff] [blame] | 215 | // 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 Turner | 13e87f4 | 2017-05-16 22:50:32 +0000 | [diff] [blame] | 218 | switch (ExceptionInfo->ExceptionRecord->ExceptionCode) |
| 219 | { |
| 220 | case DBG_PRINTEXCEPTION_C: |
Zachary Turner | 0daa707 | 2017-05-17 16:39:33 +0000 | [diff] [blame] | 221 | case DbgPrintExceptionWideC: |
Zachary Turner | 13e87f4 | 2017-05-16 22:50:32 +0000 | [diff] [blame] | 222 | case 0x406D1388: // set debugger thread name |
| 223 | return EXCEPTION_CONTINUE_EXECUTION; |
| 224 | } |
| 225 | |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 226 | // Lookup the current thread local recovery object. |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 227 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 228 | |
| 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 Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 245 | } |
| 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 Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 252 | static sys::ThreadLocal<const void> sCurrentExceptionHandle; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 253 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 254 | static void installExceptionOrSignalHandlers() { |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 255 | // 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 Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 263 | static void uninstallExceptionOrSignalHandlers() { |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 264 | 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 Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 274 | #else // !_WIN32 |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 275 | |
| 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 Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 287 | #include <signal.h> |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 288 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 289 | static const int Signals[] = |
| 290 | { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP }; |
| 291 | static const unsigned NumSignals = array_lengthof(Signals); |
| 292 | static struct sigaction PrevActions[NumSignals]; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 293 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 294 | static void CrashRecoverySignalHandler(int Signal) { |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 295 | // Lookup the current thread local recovery object. |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 296 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 297 | |
| 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 Dunbar | 418e704 | 2010-10-18 21:55:18 +0000 | [diff] [blame] | 311 | |
| 312 | // The signal will be thrown once the signal mask is restored. |
| 313 | return; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Unblock the signal we received. |
| 317 | sigset_t SigMask; |
| 318 | sigemptyset(&SigMask); |
| 319 | sigaddset(&SigMask, Signal); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 320 | sigprocmask(SIG_UNBLOCK, &SigMask, nullptr); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 321 | |
| 322 | if (CRCI) |
| 323 | const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash(); |
| 324 | } |
| 325 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 326 | static void installExceptionOrSignalHandlers() { |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 327 | // 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 Dunbar | c90e82a | 2010-07-30 17:49:04 +0000 | [diff] [blame] | 334 | sigaction(Signals[i], &Handler, &PrevActions[i]); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 338 | static void uninstallExceptionOrSignalHandlers() { |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 339 | // Restore the previous signal handlers. |
| 340 | for (unsigned i = 0; i != NumSignals; ++i) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 341 | sigaction(Signals[i], &PrevActions[i], nullptr); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 344 | #endif // !_WIN32 |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 345 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 346 | bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) { |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 347 | // If crash recovery is disabled, do nothing. |
| 348 | if (gCrashRecoveryEnabled) { |
| 349 | assert(!Impl && "Crash recovery context already initialized!"); |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 350 | CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this); |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 351 | Impl = CRCI; |
| 352 | |
| 353 | if (setjmp(CRCI->JumpBuffer) != 0) { |
| 354 | return false; |
| 355 | } |
| 356 | } |
| 357 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 358 | Fn(); |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 359 | return true; |
| 360 | } |
| 361 | |
Reid Kleckner | 710c1ce | 2017-05-17 18:16:17 +0000 | [diff] [blame] | 362 | #endif // !_MSC_VER |
| 363 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 364 | void CrashRecoveryContext::HandleCrash() { |
| 365 | CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; |
| 366 | assert(CRCI && "Crash recovery context never initialized!"); |
| 367 | CRCI->HandleCrash(); |
| 368 | } |
| 369 | |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 370 | // FIXME: Portability. |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 371 | static void setThreadBackgroundPriority() { |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 372 | #ifdef __APPLE__ |
| 373 | setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); |
| 374 | #endif |
| 375 | } |
| 376 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 377 | static bool hasThreadBackgroundPriority() { |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 378 | #ifdef __APPLE__ |
| 379 | return getpriority(PRIO_DARWIN_THREAD, 0) == 1; |
| 380 | #else |
| 381 | return false; |
| 382 | #endif |
| 383 | } |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 384 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 385 | namespace { |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 386 | struct RunSafelyOnThreadInfo { |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 387 | function_ref<void()> Fn; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 388 | CrashRecoveryContext *CRC; |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 389 | bool UseBackgroundPriority; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 390 | bool Result; |
| 391 | }; |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 392 | } |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 393 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 394 | static void RunSafelyOnThread_Dispatch(void *UserData) { |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 395 | RunSafelyOnThreadInfo *Info = |
| 396 | reinterpret_cast<RunSafelyOnThreadInfo*>(UserData); |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 397 | |
| 398 | if (Info->UseBackgroundPriority) |
| 399 | setThreadBackgroundPriority(); |
| 400 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 401 | Info->Result = Info->CRC->RunSafely(Info->Fn); |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 402 | } |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 403 | bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn, |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 404 | unsigned RequestedStackSize) { |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 405 | bool UseBackgroundPriority = hasThreadBackgroundPriority(); |
| 406 | RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false }; |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 407 | llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, |
| 408 | RequestedStackSize == 0 |
| 409 | ? llvm::None |
| 410 | : llvm::Optional<unsigned>(RequestedStackSize)); |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 411 | if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl) |
| 412 | CRC->setSwitchedThread(); |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 413 | return Info.Result; |
| 414 | } |