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