ErrorHandling.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===//
  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. // This file defines an API used to indicate fatal error conditions. Non-fatal
  11. // errors (most of them) should be handled through LLVMContext.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Support/ErrorHandling.h"
  15. #include "llvm-c/ErrorHandling.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/Config/config.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/Errc.h"
  21. #include "llvm/Support/Error.h"
  22. #include "llvm/Support/Signals.h"
  23. #include "llvm/Support/Threading.h"
  24. #include "llvm/Support/WindowsError.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <cassert>
  27. #include <cstdlib>
  28. #include <mutex>
  29. #include <new>
  30. #if defined(HAVE_UNISTD_H)
  31. # include <unistd.h>
  32. #endif
  33. #if defined(_MSC_VER)
  34. # include <io.h>
  35. # include <fcntl.h>
  36. #endif
  37. using namespace llvm;
  38. static fatal_error_handler_t ErrorHandler = nullptr;
  39. static void *ErrorHandlerUserData = nullptr;
  40. static fatal_error_handler_t BadAllocErrorHandler = nullptr;
  41. static void *BadAllocErrorHandlerUserData = nullptr;
  42. #if LLVM_ENABLE_THREADS == 1
  43. // Mutexes to synchronize installing error handlers and calling error handlers.
  44. // Do not use ManagedStatic, or that may allocate memory while attempting to
  45. // report an OOM.
  46. //
  47. // This usage of std::mutex has to be conditionalized behind ifdefs because
  48. // of this script:
  49. // compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
  50. // That script attempts to statically link the LLVM symbolizer library with the
  51. // STL and hide all of its symbols with 'opt -internalize'. To reduce size, it
  52. // cuts out the threading portions of the hermetic copy of libc++ that it
  53. // builds. We can remove these ifdefs if that script goes away.
  54. static std::mutex ErrorHandlerMutex;
  55. static std::mutex BadAllocErrorHandlerMutex;
  56. #endif
  57. void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
  58. void *user_data) {
  59. #if LLVM_ENABLE_THREADS == 1
  60. std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
  61. #endif
  62. assert(!ErrorHandler && "Error handler already registered!\n");
  63. ErrorHandler = handler;
  64. ErrorHandlerUserData = user_data;
  65. }
  66. void llvm::remove_fatal_error_handler() {
  67. #if LLVM_ENABLE_THREADS == 1
  68. std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
  69. #endif
  70. ErrorHandler = nullptr;
  71. ErrorHandlerUserData = nullptr;
  72. }
  73. void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) {
  74. report_fatal_error(Twine(Reason), GenCrashDiag);
  75. }
  76. void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
  77. report_fatal_error(Twine(Reason), GenCrashDiag);
  78. }
  79. void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
  80. report_fatal_error(Twine(Reason), GenCrashDiag);
  81. }
  82. void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
  83. llvm::fatal_error_handler_t handler = nullptr;
  84. void* handlerData = nullptr;
  85. {
  86. // Only acquire the mutex while reading the handler, so as not to invoke a
  87. // user-supplied callback under a lock.
  88. #if LLVM_ENABLE_THREADS == 1
  89. std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
  90. #endif
  91. handler = ErrorHandler;
  92. handlerData = ErrorHandlerUserData;
  93. }
  94. if (handler) {
  95. handler(handlerData, Reason.str(), GenCrashDiag);
  96. } else {
  97. // Blast the result out to stderr. We don't try hard to make sure this
  98. // succeeds (e.g. handling EINTR) and we can't use errs() here because
  99. // raw ostreams can call report_fatal_error.
  100. SmallVector<char, 64> Buffer;
  101. raw_svector_ostream OS(Buffer);
  102. OS << "LLVM ERROR: " << Reason << "\n";
  103. StringRef MessageStr = OS.str();
  104. ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
  105. (void)written; // If something went wrong, we deliberately just give up.
  106. }
  107. // If we reached here, we are failing ungracefully. Run the interrupt handlers
  108. // to make sure any special cleanups get done, in particular that we remove
  109. // files registered with RemoveFileOnSignal.
  110. sys::RunInterruptHandlers();
  111. exit(1);
  112. }
  113. void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler,
  114. void *user_data) {
  115. #if LLVM_ENABLE_THREADS == 1
  116. std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
  117. #endif
  118. assert(!ErrorHandler && "Bad alloc error handler already registered!\n");
  119. BadAllocErrorHandler = handler;
  120. BadAllocErrorHandlerUserData = user_data;
  121. }
  122. void llvm::remove_bad_alloc_error_handler() {
  123. #if LLVM_ENABLE_THREADS == 1
  124. std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
  125. #endif
  126. BadAllocErrorHandler = nullptr;
  127. BadAllocErrorHandlerUserData = nullptr;
  128. }
  129. void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) {
  130. fatal_error_handler_t Handler = nullptr;
  131. void *HandlerData = nullptr;
  132. {
  133. // Only acquire the mutex while reading the handler, so as not to invoke a
  134. // user-supplied callback under a lock.
  135. #if LLVM_ENABLE_THREADS == 1
  136. std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
  137. #endif
  138. Handler = BadAllocErrorHandler;
  139. HandlerData = BadAllocErrorHandlerUserData;
  140. }
  141. if (Handler) {
  142. Handler(HandlerData, Reason, GenCrashDiag);
  143. llvm_unreachable("bad alloc handler should not return");
  144. }
  145. #ifdef LLVM_ENABLE_EXCEPTIONS
  146. // If exceptions are enabled, make OOM in malloc look like OOM in new.
  147. throw std::bad_alloc();
  148. #else
  149. // Don't call the normal error handler. It may allocate memory. Directly write
  150. // an OOM to stderr and abort.
  151. char OOMMessage[] = "LLVM ERROR: out of memory\n";
  152. ssize_t written = ::write(2, OOMMessage, strlen(OOMMessage));
  153. (void)written;
  154. abort();
  155. #endif
  156. }
  157. void llvm::llvm_unreachable_internal(const char *msg, const char *file,
  158. unsigned line) {
  159. // This code intentionally doesn't call the ErrorHandler callback, because
  160. // llvm_unreachable is intended to be used to indicate "impossible"
  161. // situations, and not legitimate runtime errors.
  162. if (msg)
  163. dbgs() << msg << "\n";
  164. dbgs() << "UNREACHABLE executed";
  165. if (file)
  166. dbgs() << " at " << file << ":" << line;
  167. dbgs() << "!\n";
  168. abort();
  169. #ifdef LLVM_BUILTIN_UNREACHABLE
  170. // Windows systems and possibly others don't declare abort() to be noreturn,
  171. // so use the unreachable builtin to avoid a Clang self-host warning.
  172. LLVM_BUILTIN_UNREACHABLE;
  173. #endif
  174. }
  175. static void bindingsErrorHandler(void *user_data, const std::string& reason,
  176. bool gen_crash_diag) {
  177. LLVMFatalErrorHandler handler =
  178. LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data);
  179. handler(reason.c_str());
  180. }
  181. void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) {
  182. install_fatal_error_handler(bindingsErrorHandler,
  183. LLVM_EXTENSION reinterpret_cast<void *>(Handler));
  184. }
  185. void LLVMResetFatalErrorHandler() {
  186. remove_fatal_error_handler();
  187. }
  188. #ifdef LLVM_ON_WIN32
  189. #include <winerror.h>
  190. // I'd rather not double the line count of the following.
  191. #define MAP_ERR_TO_COND(x, y) \
  192. case x: \
  193. return make_error_code(errc::y)
  194. std::error_code llvm::mapWindowsError(unsigned EV) {
  195. switch (EV) {
  196. MAP_ERR_TO_COND(ERROR_ACCESS_DENIED, permission_denied);
  197. MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS, file_exists);
  198. MAP_ERR_TO_COND(ERROR_BAD_UNIT, no_such_device);
  199. MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW, filename_too_long);
  200. MAP_ERR_TO_COND(ERROR_BUSY, device_or_resource_busy);
  201. MAP_ERR_TO_COND(ERROR_BUSY_DRIVE, device_or_resource_busy);
  202. MAP_ERR_TO_COND(ERROR_CANNOT_MAKE, permission_denied);
  203. MAP_ERR_TO_COND(ERROR_CANTOPEN, io_error);
  204. MAP_ERR_TO_COND(ERROR_CANTREAD, io_error);
  205. MAP_ERR_TO_COND(ERROR_CANTWRITE, io_error);
  206. MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY, permission_denied);
  207. MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST, no_such_device);
  208. MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE, device_or_resource_busy);
  209. MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY, directory_not_empty);
  210. MAP_ERR_TO_COND(ERROR_DIRECTORY, invalid_argument);
  211. MAP_ERR_TO_COND(ERROR_DISK_FULL, no_space_on_device);
  212. MAP_ERR_TO_COND(ERROR_FILE_EXISTS, file_exists);
  213. MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND, no_such_file_or_directory);
  214. MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL, no_space_on_device);
  215. MAP_ERR_TO_COND(ERROR_INVALID_ACCESS, permission_denied);
  216. MAP_ERR_TO_COND(ERROR_INVALID_DRIVE, no_such_device);
  217. MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION, function_not_supported);
  218. MAP_ERR_TO_COND(ERROR_INVALID_HANDLE, invalid_argument);
  219. MAP_ERR_TO_COND(ERROR_INVALID_NAME, invalid_argument);
  220. MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION, no_lock_available);
  221. MAP_ERR_TO_COND(ERROR_LOCKED, no_lock_available);
  222. MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK, invalid_argument);
  223. MAP_ERR_TO_COND(ERROR_NOACCESS, permission_denied);
  224. MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY, not_enough_memory);
  225. MAP_ERR_TO_COND(ERROR_NOT_READY, resource_unavailable_try_again);
  226. MAP_ERR_TO_COND(ERROR_OPEN_FAILED, io_error);
  227. MAP_ERR_TO_COND(ERROR_OPEN_FILES, device_or_resource_busy);
  228. MAP_ERR_TO_COND(ERROR_OUTOFMEMORY, not_enough_memory);
  229. MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND, no_such_file_or_directory);
  230. MAP_ERR_TO_COND(ERROR_BAD_NETPATH, no_such_file_or_directory);
  231. MAP_ERR_TO_COND(ERROR_READ_FAULT, io_error);
  232. MAP_ERR_TO_COND(ERROR_RETRY, resource_unavailable_try_again);
  233. MAP_ERR_TO_COND(ERROR_SEEK, io_error);
  234. MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION, permission_denied);
  235. MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES, too_many_files_open);
  236. MAP_ERR_TO_COND(ERROR_WRITE_FAULT, io_error);
  237. MAP_ERR_TO_COND(ERROR_WRITE_PROTECT, permission_denied);
  238. MAP_ERR_TO_COND(WSAEACCES, permission_denied);
  239. MAP_ERR_TO_COND(WSAEBADF, bad_file_descriptor);
  240. MAP_ERR_TO_COND(WSAEFAULT, bad_address);
  241. MAP_ERR_TO_COND(WSAEINTR, interrupted);
  242. MAP_ERR_TO_COND(WSAEINVAL, invalid_argument);
  243. MAP_ERR_TO_COND(WSAEMFILE, too_many_files_open);
  244. MAP_ERR_TO_COND(WSAENAMETOOLONG, filename_too_long);
  245. default:
  246. return std::error_code(EV, std::system_category());
  247. }
  248. }
  249. #endif