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 15 #include "llvm/Support/ErrorHandling.h" 16 #include "llvm-c/ErrorHandling.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/Twine.h" 19 #include "llvm/Config/config.h" 20 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/Errc.h" 22 #include "llvm/Support/Error.h" 23 #include "llvm/Support/ManagedStatic.h" 24 #include "llvm/Support/Mutex.h" 25 #include "llvm/Support/MutexGuard.h" 26 #include "llvm/Support/Signals.h" 27 #include "llvm/Support/Threading.h" 28 #include "llvm/Support/WindowsError.h" 29 #include "llvm/Support/raw_ostream.h" 30 #include <cassert> 31 #include <cstdlib> 32 #include <new> 33 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 42 using namespace llvm; 43 44 static fatal_error_handler_t ErrorHandler = nullptr; 45 static void *ErrorHandlerUserData = nullptr; 46 static ManagedStatic<sys::Mutex> ErrorHandlerMutex; 47 48 static fatal_error_handler_t BadAllocErrorHandler = nullptr; 49 static void *BadAllocErrorHandlerUserData = nullptr; 50 static ManagedStatic<sys::Mutex> BadAllocErrorHandlerMutex; 51 52 void llvm::install_fatal_error_handler(fatal_error_handler_t handler, 53 void *user_data) { 54 llvm::MutexGuard Lock(*ErrorHandlerMutex); 55 assert(!ErrorHandler && "Error handler already registered!\n"); 56 ErrorHandler = handler; 57 ErrorHandlerUserData = user_data; 58 } 59 60 void llvm::remove_fatal_error_handler() { 61 llvm::MutexGuard Lock(*ErrorHandlerMutex); 62 ErrorHandler = nullptr; 63 ErrorHandlerUserData = nullptr; 64 } 65 66 void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) { 67 report_fatal_error(Twine(Reason), GenCrashDiag); 68 } 69 70 void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) { 71 report_fatal_error(Twine(Reason), GenCrashDiag); 72 } 73 74 void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) { 75 report_fatal_error(Twine(Reason), GenCrashDiag); 76 } 77 78 void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { 79 llvm::fatal_error_handler_t handler = nullptr; 80 void* handlerData = nullptr; 81 { 82 // Only acquire the mutex while reading the handler, so as not to invoke a 83 // user-supplied callback under a lock. 84 llvm::MutexGuard Lock(*ErrorHandlerMutex); 85 handler = ErrorHandler; 86 handlerData = ErrorHandlerUserData; 87 } 88 89 if (handler) { 90 handler(handlerData, Reason.str(), GenCrashDiag); 91 } else { 92 // Blast the result out to stderr. We don't try hard to make sure this 93 // succeeds (e.g. handling EINTR) and we can't use errs() here because 94 // raw ostreams can call report_fatal_error. 95 SmallVector<char, 64> Buffer; 96 raw_svector_ostream OS(Buffer); 97 OS << "LLVM ERROR: " << Reason << "\n"; 98 StringRef MessageStr = OS.str(); 99 ssize_t written = ::write(2, MessageStr.data(), MessageStr.size()); 100 (void)written; // If something went wrong, we deliberately just give up. 101 } 102 103 // If we reached here, we are failing ungracefully. Run the interrupt handlers 104 // to make sure any special cleanups get done, in particular that we remove 105 // files registered with RemoveFileOnSignal. 106 sys::RunInterruptHandlers(); 107 108 exit(1); 109 } 110 111 void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler, 112 void *user_data) { 113 MutexGuard Lock(*BadAllocErrorHandlerMutex); 114 assert(!ErrorHandler && "Bad alloc error handler already registered!\n"); 115 BadAllocErrorHandler = handler; 116 BadAllocErrorHandlerUserData = user_data; 117 } 118 119 void llvm::remove_bad_alloc_error_handler() { 120 MutexGuard Lock(*BadAllocErrorHandlerMutex); 121 BadAllocErrorHandler = nullptr; 122 BadAllocErrorHandlerUserData = nullptr; 123 } 124 125 void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { 126 fatal_error_handler_t Handler = nullptr; 127 void *HandlerData = nullptr; 128 { 129 // Only acquire the mutex while reading the handler, so as not to invoke a 130 // user-supplied callback under a lock. 131 MutexGuard Lock(*BadAllocErrorHandlerMutex); 132 Handler = BadAllocErrorHandler; 133 HandlerData = BadAllocErrorHandlerUserData; 134 } 135 136 if (Handler) { 137 Handler(HandlerData, Reason, GenCrashDiag); 138 llvm_unreachable("bad alloc handler should not return"); 139 } 140 141 #ifdef LLVM_ENABLE_EXCEPTIONS 142 // If exceptions are enabled, make OOM in malloc look like OOM in new. 143 throw std::bad_alloc(); 144 #else 145 // Otherwise, fall back to the normal fatal error handler. 146 report_fatal_error("out of memory: " + Twine(Reason)); 147 #endif 148 } 149 150 void llvm::llvm_unreachable_internal(const char *msg, const char *file, 151 unsigned line) { 152 // This code intentionally doesn't call the ErrorHandler callback, because 153 // llvm_unreachable is intended to be used to indicate "impossible" 154 // situations, and not legitimate runtime errors. 155 if (msg) 156 dbgs() << msg << "\n"; 157 dbgs() << "UNREACHABLE executed"; 158 if (file) 159 dbgs() << " at " << file << ":" << line; 160 dbgs() << "!\n"; 161 abort(); 162 #ifdef LLVM_BUILTIN_UNREACHABLE 163 // Windows systems and possibly others don't declare abort() to be noreturn, 164 // so use the unreachable builtin to avoid a Clang self-host warning. 165 LLVM_BUILTIN_UNREACHABLE; 166 #endif 167 } 168 169 static void bindingsErrorHandler(void *user_data, const std::string& reason, 170 bool gen_crash_diag) { 171 LLVMFatalErrorHandler handler = 172 LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data); 173 handler(reason.c_str()); 174 } 175 176 void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) { 177 install_fatal_error_handler(bindingsErrorHandler, 178 LLVM_EXTENSION reinterpret_cast<void *>(Handler)); 179 } 180 181 void LLVMResetFatalErrorHandler() { 182 remove_fatal_error_handler(); 183 } 184 185 #ifdef LLVM_ON_WIN32 186 187 #include <winerror.h> 188 189 // I'd rather not double the line count of the following. 190 #define MAP_ERR_TO_COND(x, y) \ 191 case x: \ 192 return make_error_code(errc::y) 193 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 250 #endif 251