xref: /llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp (revision ff329942cd7af409e923c55f34c646736b0c275f)
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/System/Mutex.h"
14 #include "llvm/System/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   std::string Backtrace;
27   ::jmp_buf JumpBuffer;
28   volatile unsigned Failed : 1;
29 
30 public:
31   CrashRecoveryContextImpl() : Failed(false) {
32     CurrentContext.set(this);
33   }
34   ~CrashRecoveryContextImpl() {
35     CurrentContext.erase();
36   }
37 
38   void HandleCrash() {
39     assert(!Failed && "Crash recovery context already failed!");
40     Failed = true;
41 
42     // FIXME: Stash the backtrace.
43 
44     // Jump back to the RunSafely we were called under.
45     longjmp(JumpBuffer, 1);
46   }
47 };
48 
49 }
50 
51 static sys::Mutex gCrashRecoveryContexMutex;
52 static bool gCrashRecoveryEnabled = false;
53 
54 CrashRecoveryContext::~CrashRecoveryContext() {
55   CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
56   delete CRCI;
57 }
58 
59 #ifdef LLVM_ON_WIN32
60 
61 // FIXME: No real Win32 implementation currently.
62 
63 void CrashRecoveryContext::Enable() {
64   sys::ScopedLock L(gCrashRecoveryContexMutex);
65 
66   if (gCrashRecoveryEnabled)
67     return;
68 
69   gCrashRecoveryEnabled = true;
70 }
71 
72 void CrashRecoveryContext::Disable() {
73   sys::ScopedLock L(gCrashRecoveryContexMutex);
74 
75   if (!gCrashRecoveryEnabled)
76     return;
77 
78   gCrashRecoveryEnabled = false;
79 }
80 
81 #else
82 
83 // Generic POSIX implementation.
84 //
85 // This implementation relies on synchronous signals being delivered to the
86 // current thread. We use a thread local object to keep track of the active
87 // crash recovery context, and install signal handlers to invoke HandleCrash on
88 // the active object.
89 //
90 // This implementation does not to attempt to chain signal handlers in any
91 // reliable fashion -- if we get a signal outside of a crash recovery context we
92 // simply disable crash recovery and raise the signal again.
93 
94 #include <signal.h>
95 
96 static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
97 static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
98 static struct sigaction PrevActions[NumSignals];
99 
100 static void CrashRecoverySignalHandler(int Signal) {
101   // Lookup the current thread local recovery object.
102   const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
103 
104   if (!CRCI) {
105     // We didn't find a crash recovery context -- this means either we got a
106     // signal on a thread we didn't expect it on, the application got a signal
107     // outside of a crash recovery context, or something else went horribly
108     // wrong.
109     //
110     // Disable crash recovery and raise the signal again. The assumption here is
111     // that the enclosing application will terminate soon, and we won't want to
112     // attempt crash recovery again.
113     //
114     // This call of Disable isn't thread safe, but it doesn't actually matter.
115     CrashRecoveryContext::Disable();
116     raise(Signal);
117   }
118 
119   // Unblock the signal we received.
120   sigset_t SigMask;
121   sigemptyset(&SigMask);
122   sigaddset(&SigMask, Signal);
123   sigprocmask(SIG_UNBLOCK, &SigMask, 0);
124 
125   if (CRCI)
126     const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
127 }
128 
129 void CrashRecoveryContext::Enable() {
130   sys::ScopedLock L(gCrashRecoveryContexMutex);
131 
132   if (gCrashRecoveryEnabled)
133     return;
134 
135   gCrashRecoveryEnabled = true;
136 
137   // Setup the signal handler.
138   struct sigaction Handler;
139   Handler.sa_handler = CrashRecoverySignalHandler;
140   Handler.sa_flags = 0;
141   sigemptyset(&Handler.sa_mask);
142 
143   for (unsigned i = 0; i != NumSignals; ++i) {
144     sigaction(Signals[i], &Handler, &PrevActions[i]);
145   }
146 }
147 
148 void CrashRecoveryContext::Disable() {
149   sys::ScopedLock L(gCrashRecoveryContexMutex);
150 
151   if (!gCrashRecoveryEnabled)
152     return;
153 
154   gCrashRecoveryEnabled = false;
155 
156   // Restore the previous signal handlers.
157   for (unsigned i = 0; i != NumSignals; ++i)
158     sigaction(Signals[i], &PrevActions[i], 0);
159 }
160 
161 #endif
162 
163 bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
164   // If crash recovery is disabled, do nothing.
165   if (gCrashRecoveryEnabled) {
166     assert(!Impl && "Crash recovery context already initialized!");
167     CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl;
168     Impl = CRCI;
169 
170     if (setjmp(CRCI->JumpBuffer) != 0) {
171       return false;
172     }
173   }
174 
175   Fn(UserData);
176   return true;
177 }
178 
179 void CrashRecoveryContext::HandleCrash() {
180   CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
181   assert(CRCI && "Crash recovery context never initialized!");
182   CRCI->HandleCrash();
183 }
184 
185 const std::string &CrashRecoveryContext::getBacktrace() const {
186   CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl;
187   assert(CRC && "Crash recovery context never initialized!");
188   assert(CRC->Failed && "No crash was detected!");
189   return CRC->Backtrace;
190 }
191