ErrorHandling.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/Core.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/ManagedStatic.h"
  22. #include "llvm/Support/Mutex.h"
  23. #include "llvm/Support/MutexGuard.h"
  24. #include "llvm/Support/Signals.h"
  25. #include "llvm/Support/Threading.h"
  26. #include "llvm/Support/WindowsError.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <cassert>
  29. #include <cstdlib>
  30. #ifdef _WIN32
  31. #include "windows.h" // HLSL Change
  32. #endif
  33. #include "dxc/Support/exception.h" // HLSL Change
  34. #if defined(HAVE_UNISTD_H)
  35. # include <unistd.h>
  36. #endif
  37. #if defined(_MSC_VER)
  38. # include <io.h>
  39. # include <fcntl.h>
  40. #endif
  41. using namespace llvm;
  42. // HLSL Change - mark these as thread_local
  43. thread_local static fatal_error_handler_t ErrorHandler = nullptr;
  44. thread_local static void *ErrorHandlerUserData = nullptr;
  45. // HLSL Change - no mutex needed, handlers are thread_local
  46. //static ManagedStatic<sys::Mutex> ErrorHandlerMutex;
  47. void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
  48. void *user_data) {
  49. // llvm::MutexGuard Lock(*ErrorHandlerMutex); // HLSL Change - ErrorHandler and user data already thread-local
  50. assert(!ErrorHandler && "Error handler already registered!\n");
  51. ErrorHandler = handler;
  52. ErrorHandlerUserData = user_data;
  53. }
  54. void llvm::remove_fatal_error_handler() {
  55. // llvm::MutexGuard Lock(*ErrorHandlerMutex); // HLSL Change - ErrorHandler and user data already thread-local
  56. ErrorHandler = nullptr;
  57. ErrorHandlerUserData = nullptr;
  58. }
  59. void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) {
  60. report_fatal_error(Twine(Reason), GenCrashDiag);
  61. }
  62. void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
  63. report_fatal_error(Twine(Reason), GenCrashDiag);
  64. }
  65. void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
  66. report_fatal_error(Twine(Reason), GenCrashDiag);
  67. }
  68. void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
  69. llvm::fatal_error_handler_t handler = nullptr;
  70. void* handlerData = nullptr;
  71. {
  72. // Only acquire the mutex while reading the handler, so as not to invoke a
  73. // user-supplied callback under a lock.
  74. // llvm::MutexGuard Lock(*ErrorHandlerMutex); // HLSL Change - ErrorHandler and user data already thread-local
  75. handler = ErrorHandler;
  76. handlerData = ErrorHandlerUserData;
  77. }
  78. #ifndef LLVM_ON_WIN32 // HLSL Change - unwind if necessary, but don't terminate the process
  79. if (handler) {
  80. handler(handlerData, Reason.str(), GenCrashDiag);
  81. } else {
  82. // Blast the result out to stderr. We don't try hard to make sure this
  83. // succeeds (e.g. handling EINTR) and we can't use errs() here because
  84. // raw ostreams can call report_fatal_error.
  85. SmallVector<char, 64> Buffer;
  86. raw_svector_ostream OS(Buffer);
  87. OS << "LLVM ERROR: " << Reason << "\n";
  88. StringRef MessageStr = OS.str();
  89. ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
  90. (void)written; // If something went wrong, we deliberately just give up.
  91. }
  92. // If we reached here, we are failing ungracefully. Run the interrupt handlers
  93. // to make sure any special cleanups get done, in particular that we remove
  94. // files registered with RemoveFileOnSignal.
  95. sys::RunInterruptHandlers();
  96. exit(1);
  97. #else
  98. if (handler) {
  99. handler(handlerData, Reason.str(), GenCrashDiag);
  100. }
  101. throw hlsl::Exception(DXC_E_LLVM_FATAL_ERROR, std::string("LLVM ERROR: ") + Reason.str() + "\n");
  102. #endif
  103. }
  104. void llvm::llvm_unreachable_internal(const char *msg, const char *file,
  105. unsigned line) {
  106. // This code intentionally doesn't call the ErrorHandler callback, because
  107. // llvm_unreachable is intended to be used to indicate "impossible"
  108. // situations, and not legitimate runtime errors.
  109. // HLSL Change - collect full message in string
  110. SmallVector<char, 64> Buffer;
  111. raw_svector_ostream OS(Buffer);
  112. if (msg)
  113. OS << msg << "\n";
  114. OS << "UNREACHABLE executed";
  115. if (file)
  116. OS << " at " << file << ":" << line;
  117. OS << "!\n";
  118. #ifndef LLVM_ON_WIN32 // HLSL Change - unwind if necessary, but don't terminate the process
  119. dbgs() << OS.str();
  120. abort();
  121. #else
  122. throw hlsl::Exception(DXC_E_LLVM_UNREACHABLE, OS.str());
  123. #endif
  124. }
  125. void llvm::llvm_cast_assert_internal(const char *func) {
  126. throw hlsl::Exception(DXC_E_LLVM_CAST_ERROR, std::string(func) + "<X>() argument of incompatible type!\n");
  127. }
  128. static void bindingsErrorHandler(void *user_data, const std::string& reason,
  129. bool gen_crash_diag) {
  130. LLVMFatalErrorHandler handler =
  131. LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data);
  132. handler(reason.c_str());
  133. }
  134. void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) {
  135. install_fatal_error_handler(bindingsErrorHandler,
  136. LLVM_EXTENSION reinterpret_cast<void *>(Handler));
  137. }
  138. void LLVMResetFatalErrorHandler() {
  139. remove_fatal_error_handler();
  140. }
  141. #ifdef LLVM_ON_WIN32
  142. #include <winerror.h>
  143. // I'd rather not double the line count of the following.
  144. #define MAP_ERR_TO_COND(x, y) \
  145. case x: \
  146. return make_error_code(errc::y)
  147. std::error_code llvm::mapWindowsError(unsigned EV) {
  148. switch (EV) {
  149. MAP_ERR_TO_COND(ERROR_ACCESS_DENIED, permission_denied);
  150. MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS, file_exists);
  151. MAP_ERR_TO_COND(ERROR_BAD_UNIT, no_such_device);
  152. MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW, filename_too_long);
  153. MAP_ERR_TO_COND(ERROR_BUSY, device_or_resource_busy);
  154. MAP_ERR_TO_COND(ERROR_BUSY_DRIVE, device_or_resource_busy);
  155. MAP_ERR_TO_COND(ERROR_CANNOT_MAKE, permission_denied);
  156. MAP_ERR_TO_COND(ERROR_CANTOPEN, io_error);
  157. MAP_ERR_TO_COND(ERROR_CANTREAD, io_error);
  158. MAP_ERR_TO_COND(ERROR_CANTWRITE, io_error);
  159. MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY, permission_denied);
  160. MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST, no_such_device);
  161. MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE, device_or_resource_busy);
  162. MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY, directory_not_empty);
  163. MAP_ERR_TO_COND(ERROR_DIRECTORY, invalid_argument);
  164. MAP_ERR_TO_COND(ERROR_DISK_FULL, no_space_on_device);
  165. MAP_ERR_TO_COND(ERROR_FILE_EXISTS, file_exists);
  166. MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND, no_such_file_or_directory);
  167. MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL, no_space_on_device);
  168. MAP_ERR_TO_COND(ERROR_INVALID_ACCESS, permission_denied);
  169. MAP_ERR_TO_COND(ERROR_INVALID_DRIVE, no_such_device);
  170. MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION, function_not_supported);
  171. MAP_ERR_TO_COND(ERROR_INVALID_HANDLE, invalid_argument);
  172. MAP_ERR_TO_COND(ERROR_INVALID_NAME, invalid_argument);
  173. MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION, no_lock_available);
  174. MAP_ERR_TO_COND(ERROR_LOCKED, no_lock_available);
  175. MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK, invalid_argument);
  176. MAP_ERR_TO_COND(ERROR_NOACCESS, permission_denied);
  177. MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY, not_enough_memory);
  178. MAP_ERR_TO_COND(ERROR_NOT_READY, resource_unavailable_try_again);
  179. MAP_ERR_TO_COND(ERROR_OPEN_FAILED, io_error);
  180. MAP_ERR_TO_COND(ERROR_OPEN_FILES, device_or_resource_busy);
  181. MAP_ERR_TO_COND(ERROR_OUTOFMEMORY, not_enough_memory);
  182. MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND, no_such_file_or_directory);
  183. MAP_ERR_TO_COND(ERROR_BAD_NETPATH, no_such_file_or_directory);
  184. MAP_ERR_TO_COND(ERROR_READ_FAULT, io_error);
  185. MAP_ERR_TO_COND(ERROR_RETRY, resource_unavailable_try_again);
  186. MAP_ERR_TO_COND(ERROR_SEEK, io_error);
  187. MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION, permission_denied);
  188. MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES, too_many_files_open);
  189. MAP_ERR_TO_COND(ERROR_WRITE_FAULT, io_error);
  190. MAP_ERR_TO_COND(ERROR_WRITE_PROTECT, permission_denied);
  191. MAP_ERR_TO_COND(WSAEACCES, permission_denied);
  192. MAP_ERR_TO_COND(WSAEBADF, bad_file_descriptor);
  193. MAP_ERR_TO_COND(WSAEFAULT, bad_address);
  194. MAP_ERR_TO_COND(WSAEINTR, interrupted);
  195. MAP_ERR_TO_COND(WSAEINVAL, invalid_argument);
  196. MAP_ERR_TO_COND(WSAEMFILE, too_many_files_open);
  197. MAP_ERR_TO_COND(WSAENAMETOOLONG, filename_too_long);
  198. default:
  199. return std::error_code(EV, std::system_category());
  200. }
  201. }
  202. #endif