1*9880d681SAndroid Build Coastguard Worker //===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines an API used to indicate fatal error conditions. Non-fatal
11*9880d681SAndroid Build Coastguard Worker // errors (most of them) should be handled through LLVMContext.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm-c/ErrorHandling.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Twine.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Config/config.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Errc.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Error.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ManagedStatic.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Mutex.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MutexGuard.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Signals.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Threading.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/WindowsError.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
30*9880d681SAndroid Build Coastguard Worker #include <cassert>
31*9880d681SAndroid Build Coastguard Worker #include <cstdlib>
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker #if defined(HAVE_UNISTD_H)
34*9880d681SAndroid Build Coastguard Worker # include <unistd.h>
35*9880d681SAndroid Build Coastguard Worker #endif
36*9880d681SAndroid Build Coastguard Worker #if defined(_MSC_VER)
37*9880d681SAndroid Build Coastguard Worker # include <io.h>
38*9880d681SAndroid Build Coastguard Worker # include <fcntl.h>
39*9880d681SAndroid Build Coastguard Worker #endif
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker using namespace llvm;
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker static fatal_error_handler_t ErrorHandler = nullptr;
44*9880d681SAndroid Build Coastguard Worker static void *ErrorHandlerUserData = nullptr;
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker static ManagedStatic<sys::Mutex> ErrorHandlerMutex;
47*9880d681SAndroid Build Coastguard Worker
install_fatal_error_handler(fatal_error_handler_t handler,void * user_data)48*9880d681SAndroid Build Coastguard Worker void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
49*9880d681SAndroid Build Coastguard Worker void *user_data) {
50*9880d681SAndroid Build Coastguard Worker llvm::MutexGuard Lock(*ErrorHandlerMutex);
51*9880d681SAndroid Build Coastguard Worker assert(!ErrorHandler && "Error handler already registered!\n");
52*9880d681SAndroid Build Coastguard Worker ErrorHandler = handler;
53*9880d681SAndroid Build Coastguard Worker ErrorHandlerUserData = user_data;
54*9880d681SAndroid Build Coastguard Worker }
55*9880d681SAndroid Build Coastguard Worker
remove_fatal_error_handler()56*9880d681SAndroid Build Coastguard Worker void llvm::remove_fatal_error_handler() {
57*9880d681SAndroid Build Coastguard Worker llvm::MutexGuard Lock(*ErrorHandlerMutex);
58*9880d681SAndroid Build Coastguard Worker ErrorHandler = nullptr;
59*9880d681SAndroid Build Coastguard Worker ErrorHandlerUserData = nullptr;
60*9880d681SAndroid Build Coastguard Worker }
61*9880d681SAndroid Build Coastguard Worker
report_fatal_error(const char * Reason,bool GenCrashDiag)62*9880d681SAndroid Build Coastguard Worker void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) {
63*9880d681SAndroid Build Coastguard Worker report_fatal_error(Twine(Reason), GenCrashDiag);
64*9880d681SAndroid Build Coastguard Worker }
65*9880d681SAndroid Build Coastguard Worker
report_fatal_error(const std::string & Reason,bool GenCrashDiag)66*9880d681SAndroid Build Coastguard Worker void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
67*9880d681SAndroid Build Coastguard Worker report_fatal_error(Twine(Reason), GenCrashDiag);
68*9880d681SAndroid Build Coastguard Worker }
69*9880d681SAndroid Build Coastguard Worker
report_fatal_error(StringRef Reason,bool GenCrashDiag)70*9880d681SAndroid Build Coastguard Worker void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
71*9880d681SAndroid Build Coastguard Worker report_fatal_error(Twine(Reason), GenCrashDiag);
72*9880d681SAndroid Build Coastguard Worker }
73*9880d681SAndroid Build Coastguard Worker
report_fatal_error(const Twine & Reason,bool GenCrashDiag)74*9880d681SAndroid Build Coastguard Worker void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
75*9880d681SAndroid Build Coastguard Worker llvm::fatal_error_handler_t handler = nullptr;
76*9880d681SAndroid Build Coastguard Worker void* handlerData = nullptr;
77*9880d681SAndroid Build Coastguard Worker {
78*9880d681SAndroid Build Coastguard Worker // Only acquire the mutex while reading the handler, so as not to invoke a
79*9880d681SAndroid Build Coastguard Worker // user-supplied callback under a lock.
80*9880d681SAndroid Build Coastguard Worker llvm::MutexGuard Lock(*ErrorHandlerMutex);
81*9880d681SAndroid Build Coastguard Worker handler = ErrorHandler;
82*9880d681SAndroid Build Coastguard Worker handlerData = ErrorHandlerUserData;
83*9880d681SAndroid Build Coastguard Worker }
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker if (handler) {
86*9880d681SAndroid Build Coastguard Worker handler(handlerData, Reason.str(), GenCrashDiag);
87*9880d681SAndroid Build Coastguard Worker } else {
88*9880d681SAndroid Build Coastguard Worker // Blast the result out to stderr. We don't try hard to make sure this
89*9880d681SAndroid Build Coastguard Worker // succeeds (e.g. handling EINTR) and we can't use errs() here because
90*9880d681SAndroid Build Coastguard Worker // raw ostreams can call report_fatal_error.
91*9880d681SAndroid Build Coastguard Worker SmallVector<char, 64> Buffer;
92*9880d681SAndroid Build Coastguard Worker raw_svector_ostream OS(Buffer);
93*9880d681SAndroid Build Coastguard Worker OS << "LLVM ERROR: " << Reason << "\n";
94*9880d681SAndroid Build Coastguard Worker StringRef MessageStr = OS.str();
95*9880d681SAndroid Build Coastguard Worker ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
96*9880d681SAndroid Build Coastguard Worker (void)written; // If something went wrong, we deliberately just give up.
97*9880d681SAndroid Build Coastguard Worker }
98*9880d681SAndroid Build Coastguard Worker
99*9880d681SAndroid Build Coastguard Worker // If we reached here, we are failing ungracefully. Run the interrupt handlers
100*9880d681SAndroid Build Coastguard Worker // to make sure any special cleanups get done, in particular that we remove
101*9880d681SAndroid Build Coastguard Worker // files registered with RemoveFileOnSignal.
102*9880d681SAndroid Build Coastguard Worker sys::RunInterruptHandlers();
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker exit(1);
105*9880d681SAndroid Build Coastguard Worker }
106*9880d681SAndroid Build Coastguard Worker
llvm_unreachable_internal(const char * msg,const char * file,unsigned line)107*9880d681SAndroid Build Coastguard Worker void llvm::llvm_unreachable_internal(const char *msg, const char *file,
108*9880d681SAndroid Build Coastguard Worker unsigned line) {
109*9880d681SAndroid Build Coastguard Worker // This code intentionally doesn't call the ErrorHandler callback, because
110*9880d681SAndroid Build Coastguard Worker // llvm_unreachable is intended to be used to indicate "impossible"
111*9880d681SAndroid Build Coastguard Worker // situations, and not legitimate runtime errors.
112*9880d681SAndroid Build Coastguard Worker if (msg)
113*9880d681SAndroid Build Coastguard Worker dbgs() << msg << "\n";
114*9880d681SAndroid Build Coastguard Worker dbgs() << "UNREACHABLE executed";
115*9880d681SAndroid Build Coastguard Worker if (file)
116*9880d681SAndroid Build Coastguard Worker dbgs() << " at " << file << ":" << line;
117*9880d681SAndroid Build Coastguard Worker dbgs() << "!\n";
118*9880d681SAndroid Build Coastguard Worker abort();
119*9880d681SAndroid Build Coastguard Worker #ifdef LLVM_BUILTIN_UNREACHABLE
120*9880d681SAndroid Build Coastguard Worker // Windows systems and possibly others don't declare abort() to be noreturn,
121*9880d681SAndroid Build Coastguard Worker // so use the unreachable builtin to avoid a Clang self-host warning.
122*9880d681SAndroid Build Coastguard Worker LLVM_BUILTIN_UNREACHABLE;
123*9880d681SAndroid Build Coastguard Worker #endif
124*9880d681SAndroid Build Coastguard Worker }
125*9880d681SAndroid Build Coastguard Worker
bindingsErrorHandler(void * user_data,const std::string & reason,bool gen_crash_diag)126*9880d681SAndroid Build Coastguard Worker static void bindingsErrorHandler(void *user_data, const std::string& reason,
127*9880d681SAndroid Build Coastguard Worker bool gen_crash_diag) {
128*9880d681SAndroid Build Coastguard Worker LLVMFatalErrorHandler handler =
129*9880d681SAndroid Build Coastguard Worker LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data);
130*9880d681SAndroid Build Coastguard Worker handler(reason.c_str());
131*9880d681SAndroid Build Coastguard Worker }
132*9880d681SAndroid Build Coastguard Worker
LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler)133*9880d681SAndroid Build Coastguard Worker void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) {
134*9880d681SAndroid Build Coastguard Worker install_fatal_error_handler(bindingsErrorHandler,
135*9880d681SAndroid Build Coastguard Worker LLVM_EXTENSION reinterpret_cast<void *>(Handler));
136*9880d681SAndroid Build Coastguard Worker }
137*9880d681SAndroid Build Coastguard Worker
LLVMResetFatalErrorHandler()138*9880d681SAndroid Build Coastguard Worker void LLVMResetFatalErrorHandler() {
139*9880d681SAndroid Build Coastguard Worker remove_fatal_error_handler();
140*9880d681SAndroid Build Coastguard Worker }
141*9880d681SAndroid Build Coastguard Worker
142*9880d681SAndroid Build Coastguard Worker #ifdef LLVM_ON_WIN32
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker #include <winerror.h>
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker // I'd rather not double the line count of the following.
147*9880d681SAndroid Build Coastguard Worker #define MAP_ERR_TO_COND(x, y) \
148*9880d681SAndroid Build Coastguard Worker case x: \
149*9880d681SAndroid Build Coastguard Worker return make_error_code(errc::y)
150*9880d681SAndroid Build Coastguard Worker
mapWindowsError(unsigned EV)151*9880d681SAndroid Build Coastguard Worker std::error_code llvm::mapWindowsError(unsigned EV) {
152*9880d681SAndroid Build Coastguard Worker switch (EV) {
153*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_ACCESS_DENIED, permission_denied);
154*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS, file_exists);
155*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_BAD_UNIT, no_such_device);
156*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW, filename_too_long);
157*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_BUSY, device_or_resource_busy);
158*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_BUSY_DRIVE, device_or_resource_busy);
159*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_CANNOT_MAKE, permission_denied);
160*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_CANTOPEN, io_error);
161*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_CANTREAD, io_error);
162*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_CANTWRITE, io_error);
163*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY, permission_denied);
164*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST, no_such_device);
165*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE, device_or_resource_busy);
166*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY, directory_not_empty);
167*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_DIRECTORY, invalid_argument);
168*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_DISK_FULL, no_space_on_device);
169*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_FILE_EXISTS, file_exists);
170*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND, no_such_file_or_directory);
171*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL, no_space_on_device);
172*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_INVALID_ACCESS, permission_denied);
173*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_INVALID_DRIVE, no_such_device);
174*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION, function_not_supported);
175*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_INVALID_HANDLE, invalid_argument);
176*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_INVALID_NAME, invalid_argument);
177*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION, no_lock_available);
178*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_LOCKED, no_lock_available);
179*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK, invalid_argument);
180*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_NOACCESS, permission_denied);
181*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY, not_enough_memory);
182*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_NOT_READY, resource_unavailable_try_again);
183*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_OPEN_FAILED, io_error);
184*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_OPEN_FILES, device_or_resource_busy);
185*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_OUTOFMEMORY, not_enough_memory);
186*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND, no_such_file_or_directory);
187*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_BAD_NETPATH, no_such_file_or_directory);
188*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_READ_FAULT, io_error);
189*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_RETRY, resource_unavailable_try_again);
190*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_SEEK, io_error);
191*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION, permission_denied);
192*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES, too_many_files_open);
193*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_WRITE_FAULT, io_error);
194*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(ERROR_WRITE_PROTECT, permission_denied);
195*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(WSAEACCES, permission_denied);
196*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(WSAEBADF, bad_file_descriptor);
197*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(WSAEFAULT, bad_address);
198*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(WSAEINTR, interrupted);
199*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(WSAEINVAL, invalid_argument);
200*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(WSAEMFILE, too_many_files_open);
201*9880d681SAndroid Build Coastguard Worker MAP_ERR_TO_COND(WSAENAMETOOLONG, filename_too_long);
202*9880d681SAndroid Build Coastguard Worker default:
203*9880d681SAndroid Build Coastguard Worker return std::error_code(EV, std::system_category());
204*9880d681SAndroid Build Coastguard Worker }
205*9880d681SAndroid Build Coastguard Worker }
206*9880d681SAndroid Build Coastguard Worker
207*9880d681SAndroid Build Coastguard Worker #endif
208