Fix nested CrashRecoveryContexts with LLVM_ENABLE_THREADS=OFF, allow them.
[oota-llvm.git] / include / llvm / Support / CrashRecoveryContext.h
1 //===--- CrashRecoveryContext.h - Crash Recovery ----------------*- C++ -*-===//
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 #ifndef LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
11 #define LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
12
13 #include "llvm/ADT/STLExtras.h"
14 #include <string>
15
16 namespace llvm {
17 class CrashRecoveryContextCleanup;
18
19 /// \brief Crash recovery helper object.
20 ///
21 /// This class implements support for running operations in a safe context so
22 /// that crashes (memory errors, stack overflow, assertion violations) can be
23 /// detected and control restored to the crashing thread. Crash detection is
24 /// purely "best effort", the exact set of failures which can be recovered from
25 /// is platform dependent.
26 ///
27 /// Clients make use of this code by first calling
28 /// CrashRecoveryContext::Enable(), and then executing unsafe operations via a
29 /// CrashRecoveryContext object. For example:
30 ///
31 ///    void actual_work(void *);
32 ///
33 ///    void foo() {
34 ///      CrashRecoveryContext CRC;
35 ///
36 ///      if (!CRC.RunSafely(actual_work, 0)) {
37 ///         ... a crash was detected, report error to user ...
38 ///      }
39 ///
40 ///      ... no crash was detected ...
41 ///    }
42 class CrashRecoveryContext {
43   void *Impl;
44   CrashRecoveryContextCleanup *head;
45
46 public:
47   CrashRecoveryContext() : Impl(nullptr), head(nullptr) {}
48   ~CrashRecoveryContext();
49
50   void registerCleanup(CrashRecoveryContextCleanup *cleanup);
51   void unregisterCleanup(CrashRecoveryContextCleanup *cleanup);
52
53   /// \brief Enable crash recovery.
54   static void Enable();
55
56   /// \brief Disable crash recovery.
57   static void Disable();
58
59   /// \brief Return the active context, if the code is currently executing in a
60   /// thread which is in a protected context.
61   static CrashRecoveryContext *GetCurrent();
62
63   /// \brief Return true if the current thread is recovering from a
64   /// crash.
65   static bool isRecoveringFromCrash();
66
67   /// \brief Execute the provide callback function (with the given arguments) in
68   /// a protected context.
69   ///
70   /// \return True if the function completed successfully, and false if the
71   /// function crashed (or HandleCrash was called explicitly). Clients should
72   /// make as little assumptions as possible about the program state when
73   /// RunSafely has returned false. Clients can use getBacktrace() to retrieve
74   /// the backtrace of the crash on failures.
75   bool RunSafely(function_ref<void()> Fn);
76   bool RunSafely(void (*Fn)(void*), void *UserData) {
77     return RunSafely([&]() { Fn(UserData); });
78   }
79
80   /// \brief Execute the provide callback function (with the given arguments) in
81   /// a protected context which is run in another thread (optionally with a
82   /// requested stack size).
83   ///
84   /// See RunSafely() and llvm_execute_on_thread().
85   ///
86   /// On Darwin, if PRIO_DARWIN_BG is set on the calling thread, it will be
87   /// propagated to the new thread as well.
88   bool RunSafelyOnThread(function_ref<void()>, unsigned RequestedStackSize = 0);
89   bool RunSafelyOnThread(void (*Fn)(void*), void *UserData,
90                          unsigned RequestedStackSize = 0) {
91     return RunSafelyOnThread([&]() { Fn(UserData); }, RequestedStackSize);
92   }
93
94   /// \brief Explicitly trigger a crash recovery in the current process, and
95   /// return failure from RunSafely(). This function does not return.
96   void HandleCrash();
97
98   /// \brief Return a string containing the backtrace where the crash was
99   /// detected; or empty if the backtrace wasn't recovered.
100   ///
101   /// This function is only valid when a crash has been detected (i.e.,
102   /// RunSafely() has returned false.
103   const std::string &getBacktrace() const;
104 };
105
106 class CrashRecoveryContextCleanup {
107 protected:
108   CrashRecoveryContext *context;
109   CrashRecoveryContextCleanup(CrashRecoveryContext *context)
110     : context(context), cleanupFired(false) {}
111 public:
112   bool cleanupFired;
113   
114   virtual ~CrashRecoveryContextCleanup();
115   virtual void recoverResources() = 0;
116
117   CrashRecoveryContext *getContext() const {
118     return context;
119   }
120
121 private:
122   friend class CrashRecoveryContext;
123   CrashRecoveryContextCleanup *prev, *next;
124 };
125
126 template<typename DERIVED, typename T>
127 class CrashRecoveryContextCleanupBase : public CrashRecoveryContextCleanup {
128 protected:
129   T *resource;
130   CrashRecoveryContextCleanupBase(CrashRecoveryContext *context, T* resource)
131     : CrashRecoveryContextCleanup(context), resource(resource) {}
132 public:
133   static DERIVED *create(T *x) {
134     if (x) {
135       if (CrashRecoveryContext *context = CrashRecoveryContext::GetCurrent())
136         return new DERIVED(context, x);
137     }
138     return 0;
139   }
140 };
141
142 template <typename T>
143 class CrashRecoveryContextDestructorCleanup : public
144   CrashRecoveryContextCleanupBase<CrashRecoveryContextDestructorCleanup<T>, T> {
145 public:
146   CrashRecoveryContextDestructorCleanup(CrashRecoveryContext *context,
147                                         T *resource) 
148     : CrashRecoveryContextCleanupBase<
149         CrashRecoveryContextDestructorCleanup<T>, T>(context, resource) {}
150
151   virtual void recoverResources() {
152     this->resource->~T();
153   }
154 };
155
156 template <typename T>
157 class CrashRecoveryContextDeleteCleanup : public
158   CrashRecoveryContextCleanupBase<CrashRecoveryContextDeleteCleanup<T>, T> {
159 public:
160   CrashRecoveryContextDeleteCleanup(CrashRecoveryContext *context, T *resource)
161     : CrashRecoveryContextCleanupBase<
162         CrashRecoveryContextDeleteCleanup<T>, T>(context, resource) {}
163
164   void recoverResources() override { delete this->resource; }
165 };
166
167 template <typename T>
168 class CrashRecoveryContextReleaseRefCleanup : public
169   CrashRecoveryContextCleanupBase<CrashRecoveryContextReleaseRefCleanup<T>, T>
170 {
171 public:
172   CrashRecoveryContextReleaseRefCleanup(CrashRecoveryContext *context, 
173                                         T *resource)
174     : CrashRecoveryContextCleanupBase<CrashRecoveryContextReleaseRefCleanup<T>,
175           T>(context, resource) {}
176
177   void recoverResources() override { this->resource->Release(); }
178 };
179
180 template <typename T, typename Cleanup = CrashRecoveryContextDeleteCleanup<T> >
181 class CrashRecoveryContextCleanupRegistrar {
182   CrashRecoveryContextCleanup *cleanup;
183 public:
184   CrashRecoveryContextCleanupRegistrar(T *x)
185     : cleanup(Cleanup::create(x)) {
186     if (cleanup)
187       cleanup->getContext()->registerCleanup(cleanup);
188   }
189
190   ~CrashRecoveryContextCleanupRegistrar() {
191     unregister();
192   }
193   
194   void unregister() {
195     if (cleanup && !cleanup->cleanupFired)
196       cleanup->getContext()->unregisterCleanup(cleanup);
197     cleanup = 0;
198   }
199 };
200 }
201
202 #endif