1 //===--- CrashRecoveryContext.h - Crash Recovery ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
11 #define LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
15 #include "llvm/ADT/STLExtras.h"
20 class CrashRecoveryContextCleanup;
22 /// \brief Crash recovery helper object.
24 /// This class implements support for running operations in a safe context so
25 /// that crashes (memory errors, stack overflow, assertion violations) can be
26 /// detected and control restored to the crashing thread. Crash detection is
27 /// purely "best effort", the exact set of failures which can be recovered from
28 /// is platform dependent.
30 /// Clients make use of this code by first calling
31 /// CrashRecoveryContext::Enable(), and then executing unsafe operations via a
32 /// CrashRecoveryContext object. For example:
34 /// void actual_work(void *);
37 /// CrashRecoveryContext CRC;
39 /// if (!CRC.RunSafely(actual_work, 0)) {
40 /// ... a crash was detected, report error to user ...
43 /// ... no crash was detected ...
46 /// Crash recovery contexts may not be nested.
47 class CrashRecoveryContext {
49 CrashRecoveryContextCleanup *head;
52 CrashRecoveryContext() : Impl(nullptr), head(nullptr) {}
53 ~CrashRecoveryContext();
55 void registerCleanup(CrashRecoveryContextCleanup *cleanup);
56 void unregisterCleanup(CrashRecoveryContextCleanup *cleanup);
58 /// \brief Enable crash recovery.
61 /// \brief Disable crash recovery.
62 static void Disable();
64 /// \brief Return the active context, if the code is currently executing in a
65 /// thread which is in a protected context.
66 static CrashRecoveryContext *GetCurrent();
68 /// \brief Return true if the current thread is recovering from a
70 static bool isRecoveringFromCrash();
72 /// \brief Execute the provide callback function (with the given arguments) in
73 /// a protected context.
75 /// \return True if the function completed successfully, and false if the
76 /// function crashed (or HandleCrash was called explicitly). Clients should
77 /// make as little assumptions as possible about the program state when
78 /// RunSafely has returned false. Clients can use getBacktrace() to retrieve
79 /// the backtrace of the crash on failures.
80 bool RunSafely(function_ref<void()> Fn);
81 bool RunSafely(void (*Fn)(void*), void *UserData) {
82 return RunSafely([&]() { Fn(UserData); });
85 /// \brief Execute the provide callback function (with the given arguments) in
86 /// a protected context which is run in another thread (optionally with a
87 /// requested stack size).
89 /// See RunSafely() and llvm_execute_on_thread().
91 /// On Darwin, if PRIO_DARWIN_BG is set on the calling thread, it will be
92 /// propagated to the new thread as well.
93 bool RunSafelyOnThread(function_ref<void()>, unsigned RequestedStackSize = 0);
94 bool RunSafelyOnThread(void (*Fn)(void*), void *UserData,
95 unsigned RequestedStackSize = 0) {
96 return RunSafelyOnThread([&]() { Fn(UserData); }, RequestedStackSize);
99 /// \brief Explicitly trigger a crash recovery in the current process, and
100 /// return failure from RunSafely(). This function does not return.
103 /// \brief Return a string containing the backtrace where the crash was
104 /// detected; or empty if the backtrace wasn't recovered.
106 /// This function is only valid when a crash has been detected (i.e.,
107 /// RunSafely() has returned false.
108 const std::string &getBacktrace() const;
111 class CrashRecoveryContextCleanup {
113 CrashRecoveryContext *context;
114 CrashRecoveryContextCleanup(CrashRecoveryContext *context)
115 : context(context), cleanupFired(false) {}
119 virtual ~CrashRecoveryContextCleanup();
120 virtual void recoverResources() = 0;
122 CrashRecoveryContext *getContext() const {
127 friend class CrashRecoveryContext;
128 CrashRecoveryContextCleanup *prev, *next;
131 template<typename DERIVED, typename T>
132 class CrashRecoveryContextCleanupBase : public CrashRecoveryContextCleanup {
135 CrashRecoveryContextCleanupBase(CrashRecoveryContext *context, T* resource)
136 : CrashRecoveryContextCleanup(context), resource(resource) {}
138 static DERIVED *create(T *x) {
140 if (CrashRecoveryContext *context = CrashRecoveryContext::GetCurrent())
141 return new DERIVED(context, x);
147 template <typename T>
148 class CrashRecoveryContextDestructorCleanup : public
149 CrashRecoveryContextCleanupBase<CrashRecoveryContextDestructorCleanup<T>, T> {
151 CrashRecoveryContextDestructorCleanup(CrashRecoveryContext *context,
153 : CrashRecoveryContextCleanupBase<
154 CrashRecoveryContextDestructorCleanup<T>, T>(context, resource) {}
156 virtual void recoverResources() {
157 this->resource->~T();
161 template <typename T>
162 class CrashRecoveryContextDeleteCleanup : public
163 CrashRecoveryContextCleanupBase<CrashRecoveryContextDeleteCleanup<T>, T> {
165 CrashRecoveryContextDeleteCleanup(CrashRecoveryContext *context, T *resource)
166 : CrashRecoveryContextCleanupBase<
167 CrashRecoveryContextDeleteCleanup<T>, T>(context, resource) {}
169 virtual void recoverResources() {
170 delete this->resource;
174 template <typename T>
175 class CrashRecoveryContextReleaseRefCleanup : public
176 CrashRecoveryContextCleanupBase<CrashRecoveryContextReleaseRefCleanup<T>, T>
179 CrashRecoveryContextReleaseRefCleanup(CrashRecoveryContext *context,
181 : CrashRecoveryContextCleanupBase<CrashRecoveryContextReleaseRefCleanup<T>,
182 T>(context, resource) {}
184 virtual void recoverResources() {
185 this->resource->Release();
189 template <typename T, typename Cleanup = CrashRecoveryContextDeleteCleanup<T> >
190 class CrashRecoveryContextCleanupRegistrar {
191 CrashRecoveryContextCleanup *cleanup;
193 CrashRecoveryContextCleanupRegistrar(T *x)
194 : cleanup(Cleanup::create(x)) {
196 cleanup->getContext()->registerCleanup(cleanup);
199 ~CrashRecoveryContextCleanupRegistrar() {
204 if (cleanup && !cleanup->cleanupFired)
205 cleanup->getContext()->unregisterCleanup(cleanup);