LLVM  6.0.0svn
CrashRecoveryContext.h
Go to the documentation of this file.
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 
15 namespace llvm {
16 class CrashRecoveryContextCleanup;
17 
18 /// \brief Crash recovery helper object.
19 ///
20 /// This class implements support for running operations in a safe context so
21 /// that crashes (memory errors, stack overflow, assertion violations) can be
22 /// detected and control restored to the crashing thread. Crash detection is
23 /// purely "best effort", the exact set of failures which can be recovered from
24 /// is platform dependent.
25 ///
26 /// Clients make use of this code by first calling
27 /// CrashRecoveryContext::Enable(), and then executing unsafe operations via a
28 /// CrashRecoveryContext object. For example:
29 ///
30 /// void actual_work(void *);
31 ///
32 /// void foo() {
33 /// CrashRecoveryContext CRC;
34 ///
35 /// if (!CRC.RunSafely(actual_work, 0)) {
36 /// ... a crash was detected, report error to user ...
37 /// }
38 ///
39 /// ... no crash was detected ...
40 /// }
42  void *Impl;
44 
45 public:
46  CrashRecoveryContext() : Impl(nullptr), head(nullptr) {}
48 
51 
52  /// \brief Enable crash recovery.
53  static void Enable();
54 
55  /// \brief Disable crash recovery.
56  static void Disable();
57 
58  /// \brief Return the active context, if the code is currently executing in a
59  /// thread which is in a protected context.
61 
62  /// \brief Return true if the current thread is recovering from a
63  /// crash.
64  static bool isRecoveringFromCrash();
65 
66  /// \brief Execute the provide callback function (with the given arguments) in
67  /// a protected context.
68  ///
69  /// \return True if the function completed successfully, and false if the
70  /// function crashed (or HandleCrash was called explicitly). Clients should
71  /// make as little assumptions as possible about the program state when
72  /// RunSafely has returned false.
73  bool RunSafely(function_ref<void()> Fn);
74  bool RunSafely(void (*Fn)(void*), void *UserData) {
75  return RunSafely([&]() { Fn(UserData); });
76  }
77 
78  /// \brief Execute the provide callback function (with the given arguments) in
79  /// a protected context which is run in another thread (optionally with a
80  /// requested stack size).
81  ///
82  /// See RunSafely() and llvm_execute_on_thread().
83  ///
84  /// On Darwin, if PRIO_DARWIN_BG is set on the calling thread, it will be
85  /// propagated to the new thread as well.
86  bool RunSafelyOnThread(function_ref<void()>, unsigned RequestedStackSize = 0);
87  bool RunSafelyOnThread(void (*Fn)(void*), void *UserData,
88  unsigned RequestedStackSize = 0) {
89  return RunSafelyOnThread([&]() { Fn(UserData); }, RequestedStackSize);
90  }
91 
92  /// \brief Explicitly trigger a crash recovery in the current process, and
93  /// return failure from RunSafely(). This function does not return.
94  void HandleCrash();
95 };
96 
98 protected:
101  : context(context), cleanupFired(false) {}
102 
103 public:
105 
106  virtual ~CrashRecoveryContextCleanup();
107  virtual void recoverResources() = 0;
108 
110  return context;
111  }
112 
113 private:
114  friend class CrashRecoveryContext;
115  CrashRecoveryContextCleanup *prev, *next;
116 };
117 
118 template<typename DERIVED, typename T>
120 protected:
123  : CrashRecoveryContextCleanup(context), resource(resource) {}
124 
125 public:
126  static DERIVED *create(T *x) {
127  if (x) {
129  return new DERIVED(context, x);
130  }
131  return nullptr;
132  }
133 };
134 
135 template <typename T>
137  CrashRecoveryContextCleanupBase<CrashRecoveryContextDestructorCleanup<T>, T> {
138 public:
140  T *resource)
142  CrashRecoveryContextDestructorCleanup<T>, T>(context, resource) {}
143 
144  virtual void recoverResources() {
145  this->resource->~T();
146  }
147 };
148 
149 template <typename T>
151  CrashRecoveryContextCleanupBase<CrashRecoveryContextDeleteCleanup<T>, T> {
152 public:
155  CrashRecoveryContextDeleteCleanup<T>, T>(context, resource) {}
156 
157  void recoverResources() override { delete this->resource; }
158 };
159 
160 template <typename T>
162  CrashRecoveryContextCleanupBase<CrashRecoveryContextReleaseRefCleanup<T>, T>
163 {
164 public:
166  T *resource)
168  T>(context, resource) {}
169 
170  void recoverResources() override { this->resource->Release(); }
171 };
172 
173 template <typename T, typename Cleanup = CrashRecoveryContextDeleteCleanup<T> >
176 
177 public:
179  : cleanup(Cleanup::create(x)) {
180  if (cleanup)
181  cleanup->getContext()->registerCleanup(cleanup);
182  }
183 
185 
186  void unregister() {
187  if (cleanup && !cleanup->cleanupFired)
188  cleanup->getContext()->unregisterCleanup(cleanup);
189  cleanup = nullptr;
190  }
191 };
192 } // end namespace llvm
193 
194 #endif // LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
static CrashRecoveryContext * GetCurrent()
Return the active context, if the code is currently executing in a thread which is in a protected con...
static void cleanup(BlockFrequencyInfoImplBase &BFI)
Clear all memory not needed downstream.
void HandleCrash()
Explicitly trigger a crash recovery in the current process, and return failure from RunSafely()...
CrashRecoveryContextDeleteCleanup(CrashRecoveryContext *context, T *resource)
void unregisterCleanup(CrashRecoveryContextCleanup *cleanup)
static void Enable()
Enable crash recovery.
CrashRecoveryContextCleanup(CrashRecoveryContext *context)
bool RunSafelyOnThread(function_ref< void()>, unsigned RequestedStackSize=0)
Execute the provide callback function (with the given arguments) in a protected context which is run ...
bool RunSafelyOnThread(void(*Fn)(void *), void *UserData, unsigned RequestedStackSize=0)
void registerCleanup(CrashRecoveryContextCleanup *cleanup)
bool RunSafely(void(*Fn)(void *), void *UserData)
CrashRecoveryContextReleaseRefCleanup(CrashRecoveryContext *context, T *resource)
bool RunSafely(function_ref< void()> Fn)
Execute the provide callback function (with the given arguments) in a protected context.
Crash recovery helper object.
CrashRecoveryContext * getContext() const
CrashRecoveryContextCleanupBase(CrashRecoveryContext *context, T *resource)
static bool isRecoveringFromCrash()
Return true if the current thread is recovering from a crash.
static void Disable()
Disable crash recovery.
CrashRecoveryContextDestructorCleanup(CrashRecoveryContext *context, T *resource)