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
13 #include "llvm/ADT/STLExtras.h"
19 class CrashRecoveryContextCleanup;
21 /// \brief Crash recovery helper object.
23 /// This class implements support for running operations in a safe context so
24 /// that crashes (memory errors, stack overflow, assertion violations) can be
25 /// detected and control restored to the crashing thread. Crash detection is
26 /// purely "best effort", the exact set of failures which can be recovered from
27 /// is platform dependent.
29 /// Clients make use of this code by first calling
30 /// CrashRecoveryContext::Enable(), and then executing unsafe operations via a
31 /// CrashRecoveryContext object. For example:
33 /// void actual_work(void *);
36 /// CrashRecoveryContext CRC;
38 /// if (!CRC.RunSafely(actual_work, 0)) {
39 /// ... a crash was detected, report error to user ...
42 /// ... no crash was detected ...
45 /// Crash recovery contexts may not be nested.
46 class CrashRecoveryContext {
48 CrashRecoveryContextCleanup *head;
51 CrashRecoveryContext() : Impl(nullptr), head(nullptr) {}
52 ~CrashRecoveryContext();
54 void registerCleanup(CrashRecoveryContextCleanup *cleanup);
55 void unregisterCleanup(CrashRecoveryContextCleanup *cleanup);
57 /// \brief Enable crash recovery.
60 /// \brief Disable crash recovery.
61 static void Disable();
63 /// \brief Return the active context, if the code is currently executing in a
64 /// thread which is in a protected context.
65 static CrashRecoveryContext *GetCurrent();
67 /// \brief Return true if the current thread is recovering from a
69 static bool isRecoveringFromCrash();
71 /// \brief Execute the provide callback function (with the given arguments) in
72 /// a protected context.
74 /// \return True if the function completed successfully, and false if the
75 /// function crashed (or HandleCrash was called explicitly). Clients should
76 /// make as little assumptions as possible about the program state when
77 /// RunSafely has returned false. Clients can use getBacktrace() to retrieve
78 /// the backtrace of the crash on failures.
79 bool RunSafely(function_ref<void()> Fn);
80 bool RunSafely(void (*Fn)(void*), void *UserData) {
81 return RunSafely([&]() { Fn(UserData); });
84 /// \brief Execute the provide callback function (with the given arguments) in
85 /// a protected context which is run in another thread (optionally with a
86 /// requested stack size).
88 /// See RunSafely() and llvm_execute_on_thread().
90 /// On Darwin, if PRIO_DARWIN_BG is set on the calling thread, it will be
91 /// propagated to the new thread as well.
92 bool RunSafelyOnThread(function_ref<void()>, unsigned RequestedStackSize = 0);
93 bool RunSafelyOnThread(void (*Fn)(void*), void *UserData,
94 unsigned RequestedStackSize = 0) {
95 return RunSafelyOnThread([&]() { Fn(UserData); }, RequestedStackSize);
98 /// \brief Explicitly trigger a crash recovery in the current process, and
99 /// return failure from RunSafely(). This function does not return.
102 /// \brief Return a string containing the backtrace where the crash was
103 /// detected; or empty if the backtrace wasn't recovered.
105 /// This function is only valid when a crash has been detected (i.e.,
106 /// RunSafely() has returned false.
107 const std::string &getBacktrace() const;
110 class CrashRecoveryContextCleanup {
112 CrashRecoveryContext *context;
113 CrashRecoveryContextCleanup(CrashRecoveryContext *context)
114 : context(context), cleanupFired(false) {}
118 virtual ~CrashRecoveryContextCleanup();
119 virtual void recoverResources() = 0;
121 CrashRecoveryContext *getContext() const {
126 friend class CrashRecoveryContext;
127 CrashRecoveryContextCleanup *prev, *next;
130 template<typename DERIVED, typename T>
131 class CrashRecoveryContextCleanupBase : public CrashRecoveryContextCleanup {
134 CrashRecoveryContextCleanupBase(CrashRecoveryContext *context, T* resource)
135 : CrashRecoveryContextCleanup(context), resource(resource) {}
137 static DERIVED *create(T *x) {
139 if (CrashRecoveryContext *context = CrashRecoveryContext::GetCurrent())
140 return new DERIVED(context, x);
146 template <typename T>
147 class CrashRecoveryContextDestructorCleanup : public
148 CrashRecoveryContextCleanupBase<CrashRecoveryContextDestructorCleanup<T>, T> {
150 CrashRecoveryContextDestructorCleanup(CrashRecoveryContext *context,
152 : CrashRecoveryContextCleanupBase<
153 CrashRecoveryContextDestructorCleanup<T>, T>(context, resource) {}
155 virtual void recoverResources() {
156 this->resource->~T();
160 template <typename T>
161 class CrashRecoveryContextDeleteCleanup : public
162 CrashRecoveryContextCleanupBase<CrashRecoveryContextDeleteCleanup<T>, T> {
164 CrashRecoveryContextDeleteCleanup(CrashRecoveryContext *context, T *resource)
165 : CrashRecoveryContextCleanupBase<
166 CrashRecoveryContextDeleteCleanup<T>, T>(context, resource) {}
168 void recoverResources() override { delete this->resource; }
171 template <typename T>
172 class CrashRecoveryContextReleaseRefCleanup : public
173 CrashRecoveryContextCleanupBase<CrashRecoveryContextReleaseRefCleanup<T>, T>
176 CrashRecoveryContextReleaseRefCleanup(CrashRecoveryContext *context,
178 : CrashRecoveryContextCleanupBase<CrashRecoveryContextReleaseRefCleanup<T>,
179 T>(context, resource) {}
181 void recoverResources() override { this->resource->Release(); }
184 template <typename T, typename Cleanup = CrashRecoveryContextDeleteCleanup<T> >
185 class CrashRecoveryContextCleanupRegistrar {
186 CrashRecoveryContextCleanup *cleanup;
188 CrashRecoveryContextCleanupRegistrar(T *x)
189 : cleanup(Cleanup::create(x)) {
191 cleanup->getContext()->registerCleanup(cleanup);
194 ~CrashRecoveryContextCleanupRegistrar() {
199 if (cleanup && !cleanup->cleanupFired)
200 cleanup->getContext()->unregisterCleanup(cleanup);