1 //===--- CrashRecoveryContext.cpp - Crash Recovery ------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Support/CrashRecoveryContext.h" 10 #include "llvm/Config/llvm-config.h" 11 #include "llvm/Support/ErrorHandling.h" 12 #include "llvm/Support/ManagedStatic.h" 13 #include "llvm/Support/Signals.h" 14 #include "llvm/Support/ThreadLocal.h" 15 #include <mutex> 16 #include <setjmp.h> 17 #ifdef _WIN32 18 #include <excpt.h> // for GetExceptionInformation 19 #endif 20 #if LLVM_ON_UNIX 21 #include <sysexits.h> // EX_IOERR 22 #endif 23 24 using namespace llvm; 25 26 namespace { 27 28 struct CrashRecoveryContextImpl; 29 30 static ManagedStatic< 31 sys::ThreadLocal<const CrashRecoveryContextImpl> > CurrentContext; 32 33 struct CrashRecoveryContextImpl { 34 // When threads are disabled, this links up all active 35 // CrashRecoveryContextImpls. When threads are enabled there's one thread 36 // per CrashRecoveryContext and CurrentContext is a thread-local, so only one 37 // CrashRecoveryContextImpl is active per thread and this is always null. 38 const CrashRecoveryContextImpl *Next; 39 40 CrashRecoveryContext *CRC; 41 ::jmp_buf JumpBuffer; 42 volatile unsigned Failed : 1; 43 unsigned SwitchedThread : 1; 44 45 public: 46 CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC), 47 Failed(false), 48 SwitchedThread(false) { 49 Next = CurrentContext->get(); 50 CurrentContext->set(this); 51 } 52 ~CrashRecoveryContextImpl() { 53 if (!SwitchedThread) 54 CurrentContext->set(Next); 55 } 56 57 /// Called when the separate crash-recovery thread was finished, to 58 /// indicate that we don't need to clear the thread-local CurrentContext. 59 void setSwitchedThread() { 60 #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0 61 SwitchedThread = true; 62 #endif 63 } 64 65 // If the function ran by the CrashRecoveryContext crashes or fails, then 66 // 'RetCode' represents the returned error code, as if it was returned by a 67 // process. 'Context' represents the signal type on Unix; on Windows, it is 68 // the ExceptionContext. 69 void HandleCrash(int RetCode, uintptr_t Context) { 70 // Eliminate the current context entry, to avoid re-entering in case the 71 // cleanup code crashes. 72 CurrentContext->set(Next); 73 74 assert(!Failed && "Crash recovery context already failed!"); 75 Failed = true; 76 77 if (CRC->DumpStackAndCleanupOnFailure) 78 sys::CleanupOnSignal(Context); 79 80 CRC->RetCode = RetCode; 81 82 // Jump back to the RunSafely we were called under. 83 longjmp(JumpBuffer, 1); 84 } 85 }; 86 87 } 88 89 static ManagedStatic<std::mutex> gCrashRecoveryContextMutex; 90 static bool gCrashRecoveryEnabled = false; 91 92 static ManagedStatic<sys::ThreadLocal<const CrashRecoveryContext>> 93 tlIsRecoveringFromCrash; 94 95 static void installExceptionOrSignalHandlers(); 96 static void uninstallExceptionOrSignalHandlers(); 97 98 CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {} 99 100 CrashRecoveryContext::~CrashRecoveryContext() { 101 // Reclaim registered resources. 102 CrashRecoveryContextCleanup *i = head; 103 const CrashRecoveryContext *PC = tlIsRecoveringFromCrash->get(); 104 tlIsRecoveringFromCrash->set(this); 105 while (i) { 106 CrashRecoveryContextCleanup *tmp = i; 107 i = tmp->next; 108 tmp->cleanupFired = true; 109 tmp->recoverResources(); 110 delete tmp; 111 } 112 tlIsRecoveringFromCrash->set(PC); 113 114 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; 115 delete CRCI; 116 } 117 118 bool CrashRecoveryContext::isRecoveringFromCrash() { 119 return tlIsRecoveringFromCrash->get() != nullptr; 120 } 121 122 CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { 123 if (!gCrashRecoveryEnabled) 124 return nullptr; 125 126 const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); 127 if (!CRCI) 128 return nullptr; 129 130 return CRCI->CRC; 131 } 132 133 void CrashRecoveryContext::Enable() { 134 std::lock_guard<std::mutex> L(*gCrashRecoveryContextMutex); 135 // FIXME: Shouldn't this be a refcount or something? 136 if (gCrashRecoveryEnabled) 137 return; 138 gCrashRecoveryEnabled = true; 139 installExceptionOrSignalHandlers(); 140 } 141 142 void CrashRecoveryContext::Disable() { 143 std::lock_guard<std::mutex> L(*gCrashRecoveryContextMutex); 144 if (!gCrashRecoveryEnabled) 145 return; 146 gCrashRecoveryEnabled = false; 147 uninstallExceptionOrSignalHandlers(); 148 } 149 150 void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup) 151 { 152 if (!cleanup) 153 return; 154 if (head) 155 head->prev = cleanup; 156 cleanup->next = head; 157 head = cleanup; 158 } 159 160 void 161 CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) { 162 if (!cleanup) 163 return; 164 if (cleanup == head) { 165 head = cleanup->next; 166 if (head) 167 head->prev = nullptr; 168 } 169 else { 170 cleanup->prev->next = cleanup->next; 171 if (cleanup->next) 172 cleanup->next->prev = cleanup->prev; 173 } 174 delete cleanup; 175 } 176 177 #if defined(_MSC_VER) 178 // If _MSC_VER is defined, we must have SEH. Use it if it's available. It's way 179 // better than VEH. Vectored exception handling catches all exceptions happening 180 // on the thread with installed exception handlers, so it can interfere with 181 // internal exception handling of other libraries on that thread. SEH works 182 // exactly as you would expect normal exception handling to work: it only 183 // catches exceptions if they would bubble out from the stack frame with __try / 184 // __except. 185 186 static void installExceptionOrSignalHandlers() {} 187 static void uninstallExceptionOrSignalHandlers() {} 188 189 // We need this function because the call to GetExceptionInformation() can only 190 // occur inside the __except evaluation block 191 static int ExceptionFilter(bool DumpStackAndCleanup, 192 _EXCEPTION_POINTERS *Except) { 193 if (DumpStackAndCleanup) 194 sys::CleanupOnSignal((uintptr_t)Except); 195 return EXCEPTION_EXECUTE_HANDLER; 196 } 197 198 #if defined(__clang__) && defined(_M_IX86) 199 // Work around PR44697. 200 __attribute__((optnone)) 201 static bool InvokeFunctionCall(function_ref<void()> Fn, 202 bool DumpStackAndCleanup, int &RetCode) { 203 #else 204 static bool InvokeFunctionCall(function_ref<void()> Fn, 205 bool DumpStackAndCleanup, int &RetCode) { 206 #endif 207 __try { 208 Fn(); 209 } __except (ExceptionFilter(DumpStackAndCleanup, GetExceptionInformation())) { 210 RetCode = GetExceptionCode(); 211 return false; 212 } 213 return true; 214 } 215 216 bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) { 217 if (!gCrashRecoveryEnabled) { 218 Fn(); 219 return true; 220 } 221 return InvokeFunctionCall(Fn, DumpStackAndCleanupOnFailure, RetCode); 222 } 223 224 #else // !_MSC_VER 225 226 #if defined(_WIN32) 227 // This is a non-MSVC compiler, probably mingw gcc or clang without 228 // -fms-extensions. Use vectored exception handling (VEH). 229 // 230 // On Windows, we can make use of vectored exception handling to catch most 231 // crashing situations. Note that this does mean we will be alerted of 232 // exceptions *before* structured exception handling has the opportunity to 233 // catch it. Unfortunately, this causes problems in practice with other code 234 // running on threads with LLVM crash recovery contexts, so we would like to 235 // eventually move away from VEH. 236 // 237 // Vectored works on a per-thread basis, which is an advantage over 238 // SetUnhandledExceptionFilter. SetUnhandledExceptionFilter also doesn't have 239 // any native support for chaining exception handlers, but VEH allows more than 240 // one. 241 // 242 // The vectored exception handler functionality was added in Windows 243 // XP, so if support for older versions of Windows is required, 244 // it will have to be added. 245 246 #include "Windows/WindowsSupport.h" 247 248 static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) 249 { 250 // DBG_PRINTEXCEPTION_WIDE_C is not properly defined on all supported 251 // compilers and platforms, so we define it manually. 252 constexpr ULONG DbgPrintExceptionWideC = 0x4001000AL; 253 switch (ExceptionInfo->ExceptionRecord->ExceptionCode) 254 { 255 case DBG_PRINTEXCEPTION_C: 256 case DbgPrintExceptionWideC: 257 case 0x406D1388: // set debugger thread name 258 return EXCEPTION_CONTINUE_EXECUTION; 259 } 260 261 // Lookup the current thread local recovery object. 262 const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); 263 264 if (!CRCI) { 265 // Something has gone horribly wrong, so let's just tell everyone 266 // to keep searching 267 CrashRecoveryContext::Disable(); 268 return EXCEPTION_CONTINUE_SEARCH; 269 } 270 271 // TODO: We can capture the stack backtrace here and store it on the 272 // implementation if we so choose. 273 274 // Handle the crash 275 const_cast<CrashRecoveryContextImpl *>(CRCI)->HandleCrash( 276 (int)ExceptionInfo->ExceptionRecord->ExceptionCode, 277 reinterpret_cast<uintptr_t>(ExceptionInfo)); 278 279 // Note that we don't actually get here because HandleCrash calls 280 // longjmp, which means the HandleCrash function never returns. 281 llvm_unreachable("Handled the crash, should have longjmp'ed out of here"); 282 } 283 284 // Because the Enable and Disable calls are static, it means that 285 // there may not actually be an Impl available, or even a current 286 // CrashRecoveryContext at all. So we make use of a thread-local 287 // exception table. The handles contained in here will either be 288 // non-NULL, valid VEH handles, or NULL. 289 static sys::ThreadLocal<const void> sCurrentExceptionHandle; 290 291 static void installExceptionOrSignalHandlers() { 292 // We can set up vectored exception handling now. We will install our 293 // handler as the front of the list, though there's no assurances that 294 // it will remain at the front (another call could install itself before 295 // our handler). This 1) isn't likely, and 2) shouldn't cause problems. 296 PVOID handle = ::AddVectoredExceptionHandler(1, ExceptionHandler); 297 sCurrentExceptionHandle.set(handle); 298 } 299 300 static void uninstallExceptionOrSignalHandlers() { 301 PVOID currentHandle = const_cast<PVOID>(sCurrentExceptionHandle.get()); 302 if (currentHandle) { 303 // Now we can remove the vectored exception handler from the chain 304 ::RemoveVectoredExceptionHandler(currentHandle); 305 306 // Reset the handle in our thread-local set. 307 sCurrentExceptionHandle.set(NULL); 308 } 309 } 310 311 #else // !_WIN32 312 313 // Generic POSIX implementation. 314 // 315 // This implementation relies on synchronous signals being delivered to the 316 // current thread. We use a thread local object to keep track of the active 317 // crash recovery context, and install signal handlers to invoke HandleCrash on 318 // the active object. 319 // 320 // This implementation does not attempt to chain signal handlers in any 321 // reliable fashion -- if we get a signal outside of a crash recovery context we 322 // simply disable crash recovery and raise the signal again. 323 324 #include <signal.h> 325 326 static const int Signals[] = 327 { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP }; 328 static const unsigned NumSignals = array_lengthof(Signals); 329 static struct sigaction PrevActions[NumSignals]; 330 331 static void CrashRecoverySignalHandler(int Signal) { 332 // Lookup the current thread local recovery object. 333 const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); 334 335 if (!CRCI) { 336 // We didn't find a crash recovery context -- this means either we got a 337 // signal on a thread we didn't expect it on, the application got a signal 338 // outside of a crash recovery context, or something else went horribly 339 // wrong. 340 // 341 // Disable crash recovery and raise the signal again. The assumption here is 342 // that the enclosing application will terminate soon, and we won't want to 343 // attempt crash recovery again. 344 // 345 // This call of Disable isn't thread safe, but it doesn't actually matter. 346 CrashRecoveryContext::Disable(); 347 raise(Signal); 348 349 // The signal will be thrown once the signal mask is restored. 350 return; 351 } 352 353 // Unblock the signal we received. 354 sigset_t SigMask; 355 sigemptyset(&SigMask); 356 sigaddset(&SigMask, Signal); 357 sigprocmask(SIG_UNBLOCK, &SigMask, nullptr); 358 359 // As per convention, -2 indicates a crash or timeout as opposed to failure to 360 // execute (see llvm/include/llvm/Support/Program.h) 361 int RetCode = -2; 362 363 // Don't consider a broken pipe as a crash (see clang/lib/Driver/Driver.cpp) 364 if (Signal == SIGPIPE) 365 RetCode = EX_IOERR; 366 367 if (CRCI) 368 const_cast<CrashRecoveryContextImpl *>(CRCI)->HandleCrash(RetCode, Signal); 369 } 370 371 static void installExceptionOrSignalHandlers() { 372 // Setup the signal handler. 373 struct sigaction Handler; 374 Handler.sa_handler = CrashRecoverySignalHandler; 375 Handler.sa_flags = 0; 376 sigemptyset(&Handler.sa_mask); 377 378 for (unsigned i = 0; i != NumSignals; ++i) { 379 sigaction(Signals[i], &Handler, &PrevActions[i]); 380 } 381 } 382 383 static void uninstallExceptionOrSignalHandlers() { 384 // Restore the previous signal handlers. 385 for (unsigned i = 0; i != NumSignals; ++i) 386 sigaction(Signals[i], &PrevActions[i], nullptr); 387 } 388 389 #endif // !_WIN32 390 391 bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) { 392 // If crash recovery is disabled, do nothing. 393 if (gCrashRecoveryEnabled) { 394 assert(!Impl && "Crash recovery context already initialized!"); 395 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this); 396 Impl = CRCI; 397 398 if (setjmp(CRCI->JumpBuffer) != 0) { 399 return false; 400 } 401 } 402 403 Fn(); 404 return true; 405 } 406 407 #endif // !_MSC_VER 408 409 // FIXME: Portability. 410 static void setThreadBackgroundPriority() { 411 #ifdef __APPLE__ 412 setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); 413 #endif 414 } 415 416 static bool hasThreadBackgroundPriority() { 417 #ifdef __APPLE__ 418 return getpriority(PRIO_DARWIN_THREAD, 0) == 1; 419 #else 420 return false; 421 #endif 422 } 423 424 namespace { 425 struct RunSafelyOnThreadInfo { 426 function_ref<void()> Fn; 427 CrashRecoveryContext *CRC; 428 bool UseBackgroundPriority; 429 bool Result; 430 }; 431 } 432 433 static void RunSafelyOnThread_Dispatch(void *UserData) { 434 RunSafelyOnThreadInfo *Info = 435 reinterpret_cast<RunSafelyOnThreadInfo*>(UserData); 436 437 if (Info->UseBackgroundPriority) 438 setThreadBackgroundPriority(); 439 440 Info->Result = Info->CRC->RunSafely(Info->Fn); 441 } 442 bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn, 443 unsigned RequestedStackSize) { 444 bool UseBackgroundPriority = hasThreadBackgroundPriority(); 445 RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false }; 446 llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, 447 RequestedStackSize == 0 448 ? llvm::None 449 : llvm::Optional<unsigned>(RequestedStackSize)); 450 if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl) 451 CRC->setSwitchedThread(); 452 return Info.Result; 453 } 454