2
0

CrashRecoveryContext.h 6.6 KB

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