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" 11 #include "llvm/ADT/SmallString.h" 12 #include "llvm/Config/config.h" 13 #include "llvm/Support/Mutex.h" 14 #include "llvm/Support/ThreadLocal.h" 15 #include <setjmp.h> 16 #include <cstdio> 17 using namespace llvm; 18 19 namespace { 20 21 struct CrashRecoveryContextImpl; 22 23 static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext; 24 25 struct CrashRecoveryContextImpl { 26 CrashRecoveryContext *CRC; 27 std::string Backtrace; 28 ::jmp_buf JumpBuffer; 29 volatile unsigned Failed : 1; 30 31 public: 32 CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC), 33 Failed(false) { 34 CurrentContext.set(this); 35 } 36 ~CrashRecoveryContextImpl() { 37 CurrentContext.erase(); 38 } 39 40 void HandleCrash() { 41 // Eliminate the current context entry, to avoid re-entering in case the 42 // cleanup code crashes. 43 CurrentContext.erase(); 44 45 assert(!Failed && "Crash recovery context already failed!"); 46 Failed = true; 47 48 // FIXME: Stash the backtrace. 49 50 // Jump back to the RunSafely we were called under. 51 longjmp(JumpBuffer, 1); 52 } 53 }; 54 55 } 56 57 static sys::Mutex gCrashRecoveryContexMutex; 58 static bool gCrashRecoveryEnabled = false; 59 60 CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {} 61 62 CrashRecoveryContext::~CrashRecoveryContext() { 63 // Reclaim registered resources. 64 CrashRecoveryContextCleanup *i = head; 65 while (i) { 66 CrashRecoveryContextCleanup *tmp = i; 67 i = tmp->next; 68 tmp->recoverResources(); 69 delete tmp; 70 } 71 72 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; 73 delete CRCI; 74 } 75 76 CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { 77 const CrashRecoveryContextImpl *CRCI = CurrentContext.get(); 78 if (!CRCI) 79 return 0; 80 81 return CRCI->CRC; 82 } 83 84 void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup) 85 { 86 if (!cleanup) 87 return; 88 if (head) 89 head->prev = cleanup; 90 cleanup->next = head; 91 head = cleanup; 92 } 93 94 void 95 CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) { 96 if (!cleanup) 97 return; 98 if (cleanup == head) { 99 head = cleanup->next; 100 if (head) 101 head->prev = 0; 102 } 103 else { 104 cleanup->prev->next = cleanup->next; 105 if (cleanup->next) 106 cleanup->next->prev = cleanup->prev; 107 } 108 delete cleanup; 109 } 110 111 #ifdef LLVM_ON_WIN32 112 113 // FIXME: No real Win32 implementation currently. 114 115 void CrashRecoveryContext::Enable() { 116 sys::ScopedLock L(gCrashRecoveryContexMutex); 117 118 if (gCrashRecoveryEnabled) 119 return; 120 121 gCrashRecoveryEnabled = true; 122 } 123 124 void CrashRecoveryContext::Disable() { 125 sys::ScopedLock L(gCrashRecoveryContexMutex); 126 127 if (!gCrashRecoveryEnabled) 128 return; 129 130 gCrashRecoveryEnabled = false; 131 } 132 133 #else 134 135 // Generic POSIX implementation. 136 // 137 // This implementation relies on synchronous signals being delivered to the 138 // current thread. We use a thread local object to keep track of the active 139 // crash recovery context, and install signal handlers to invoke HandleCrash on 140 // the active object. 141 // 142 // This implementation does not to attempt to chain signal handlers in any 143 // reliable fashion -- if we get a signal outside of a crash recovery context we 144 // simply disable crash recovery and raise the signal again. 145 146 #include <signal.h> 147 148 static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP }; 149 static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]); 150 static struct sigaction PrevActions[NumSignals]; 151 152 static void CrashRecoverySignalHandler(int Signal) { 153 // Lookup the current thread local recovery object. 154 const CrashRecoveryContextImpl *CRCI = CurrentContext.get(); 155 156 if (!CRCI) { 157 // We didn't find a crash recovery context -- this means either we got a 158 // signal on a thread we didn't expect it on, the application got a signal 159 // outside of a crash recovery context, or something else went horribly 160 // wrong. 161 // 162 // Disable crash recovery and raise the signal again. The assumption here is 163 // that the enclosing application will terminate soon, and we won't want to 164 // attempt crash recovery again. 165 // 166 // This call of Disable isn't thread safe, but it doesn't actually matter. 167 CrashRecoveryContext::Disable(); 168 raise(Signal); 169 170 // The signal will be thrown once the signal mask is restored. 171 return; 172 } 173 174 // Unblock the signal we received. 175 sigset_t SigMask; 176 sigemptyset(&SigMask); 177 sigaddset(&SigMask, Signal); 178 sigprocmask(SIG_UNBLOCK, &SigMask, 0); 179 180 if (CRCI) 181 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash(); 182 } 183 184 void CrashRecoveryContext::Enable() { 185 sys::ScopedLock L(gCrashRecoveryContexMutex); 186 187 if (gCrashRecoveryEnabled) 188 return; 189 190 gCrashRecoveryEnabled = true; 191 192 // Setup the signal handler. 193 struct sigaction Handler; 194 Handler.sa_handler = CrashRecoverySignalHandler; 195 Handler.sa_flags = 0; 196 sigemptyset(&Handler.sa_mask); 197 198 for (unsigned i = 0; i != NumSignals; ++i) { 199 sigaction(Signals[i], &Handler, &PrevActions[i]); 200 } 201 } 202 203 void CrashRecoveryContext::Disable() { 204 sys::ScopedLock L(gCrashRecoveryContexMutex); 205 206 if (!gCrashRecoveryEnabled) 207 return; 208 209 gCrashRecoveryEnabled = false; 210 211 // Restore the previous signal handlers. 212 for (unsigned i = 0; i != NumSignals; ++i) 213 sigaction(Signals[i], &PrevActions[i], 0); 214 } 215 216 #endif 217 218 bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) { 219 // If crash recovery is disabled, do nothing. 220 if (gCrashRecoveryEnabled) { 221 assert(!Impl && "Crash recovery context already initialized!"); 222 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this); 223 Impl = CRCI; 224 225 if (setjmp(CRCI->JumpBuffer) != 0) { 226 return false; 227 } 228 } 229 230 Fn(UserData); 231 return true; 232 } 233 234 void CrashRecoveryContext::HandleCrash() { 235 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; 236 assert(CRCI && "Crash recovery context never initialized!"); 237 CRCI->HandleCrash(); 238 } 239 240 const std::string &CrashRecoveryContext::getBacktrace() const { 241 CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl; 242 assert(CRC && "Crash recovery context never initialized!"); 243 assert(CRC->Failed && "No crash was detected!"); 244 return CRC->Backtrace; 245 } 246 247 // 248 249 namespace { 250 struct RunSafelyOnThreadInfo { 251 void (*UserFn)(void*); 252 void *UserData; 253 CrashRecoveryContext *CRC; 254 bool Result; 255 }; 256 } 257 258 static void RunSafelyOnThread_Dispatch(void *UserData) { 259 RunSafelyOnThreadInfo *Info = 260 reinterpret_cast<RunSafelyOnThreadInfo*>(UserData); 261 Info->Result = Info->CRC->RunSafely(Info->UserFn, Info->UserData); 262 } 263 bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData, 264 unsigned RequestedStackSize) { 265 RunSafelyOnThreadInfo Info = { Fn, UserData, this, false }; 266 llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize); 267 return Info.Result; 268 } 269