blob: fe69151665c6884fbfeed8fbe46bf719142c2844 [file] [log] [blame]
Chris Lattner59c57532010-04-07 23:12:29 +00001//===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===//
Torok Edwin6c2d2332009-07-07 17:32:34 +00002//
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//
Chris Lattner59c57532010-04-07 23:12:29 +000010// This file defines an API used to indicate fatal error conditions. Non-fatal
11// errors (most of them) should be handled through LLVMContext.
12//
Torok Edwin6c2d2332009-07-07 17:32:34 +000013//===----------------------------------------------------------------------===//
14
Torok Edwin6c2d2332009-07-07 17:32:34 +000015#include "llvm/Support/ErrorHandling.h"
Eric Christophera6b96002015-12-18 01:46:52 +000016#include "llvm-c/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/Config/config.h"
20#include "llvm/Support/Debug.h"
Rafael Espindola58cb7452014-06-17 18:06:45 +000021#include "llvm/Support/Errc.h"
Lang Hamesf7f6d3e2016-03-16 01:02:46 +000022#include "llvm/Support/Error.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000023#include "llvm/Support/Signals.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000024#include "llvm/Support/Threading.h"
Rafael Espindola58cb7452014-06-17 18:06:45 +000025#include "llvm/Support/WindowsError.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/raw_ostream.h"
Torok Edwin6c2d2332009-07-07 17:32:34 +000027#include <cassert>
28#include <cstdlib>
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +000029#include <mutex>
Reid Klecknerada8c392017-07-11 16:45:30 +000030#include <new>
Chris Lattner62170822010-08-17 23:03:53 +000031
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
Torok Edwin6c2d2332009-07-07 17:32:34 +000040using namespace llvm;
Torok Edwin6c2d2332009-07-07 17:32:34 +000041
Craig Topperc10719f2014-04-07 04:17:22 +000042static fatal_error_handler_t ErrorHandler = nullptr;
43static void *ErrorHandlerUserData = nullptr;
Zachary Turner586fd742014-06-13 21:20:44 +000044
Reid Klecknerada8c392017-07-11 16:45:30 +000045static fatal_error_handler_t BadAllocErrorHandler = nullptr;
46static void *BadAllocErrorHandlerUserData = nullptr;
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +000047
48// Mutexes to synchronize installing error handlers and calling error handlers.
49// Do not use ManagedStatic, or that may allocate memory while attempting to
50// report an OOM.
51static std::mutex ErrorHandlerMutex;
52static std::mutex BadAllocErrorHandlerMutex;
Reid Klecknerada8c392017-07-11 16:45:30 +000053
Chris Lattner59c57532010-04-07 23:12:29 +000054void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
55 void *user_data) {
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +000056 std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
Torok Edwin6c2d2332009-07-07 17:32:34 +000057 assert(!ErrorHandler && "Error handler already registered!\n");
58 ErrorHandler = handler;
Daniel Dunbar1bf60c22009-08-10 03:36:26 +000059 ErrorHandlerUserData = user_data;
Torok Edwin6c2d2332009-07-07 17:32:34 +000060}
61
Chris Lattner59c57532010-04-07 23:12:29 +000062void llvm::remove_fatal_error_handler() {
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +000063 std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
Craig Topperc10719f2014-04-07 04:17:22 +000064 ErrorHandler = nullptr;
Zachary Turner586fd742014-06-13 21:20:44 +000065 ErrorHandlerUserData = nullptr;
Torok Edwin6c2d2332009-07-07 17:32:34 +000066}
67
Chad Rosier583b4d32013-03-27 18:27:54 +000068void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) {
69 report_fatal_error(Twine(Reason), GenCrashDiag);
Daniel Dunbarcd51ea52009-07-24 07:58:10 +000070}
71
Chad Rosier583b4d32013-03-27 18:27:54 +000072void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
73 report_fatal_error(Twine(Reason), GenCrashDiag);
Daniel Dunbarcd51ea52009-07-24 07:58:10 +000074}
75
Alp Toker17d4e982014-01-27 05:24:39 +000076void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
Chad Rosier583b4d32013-03-27 18:27:54 +000077 report_fatal_error(Twine(Reason), GenCrashDiag);
Daniel Dunbar2ed3fe02010-11-13 02:48:51 +000078}
79
Chad Rosier583b4d32013-03-27 18:27:54 +000080void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
Zachary Turner586fd742014-06-13 21:20:44 +000081 llvm::fatal_error_handler_t handler = nullptr;
82 void* handlerData = nullptr;
83 {
84 // Only acquire the mutex while reading the handler, so as not to invoke a
85 // user-supplied callback under a lock.
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +000086 std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
Zachary Turner586fd742014-06-13 21:20:44 +000087 handler = ErrorHandler;
88 handlerData = ErrorHandlerUserData;
89 }
90
91 if (handler) {
92 handler(handlerData, Reason.str(), GenCrashDiag);
Torok Edwin6c2d2332009-07-07 17:32:34 +000093 } else {
Chris Lattner62170822010-08-17 23:03:53 +000094 // Blast the result out to stderr. We don't try hard to make sure this
95 // succeeds (e.g. handling EINTR) and we can't use errs() here because
96 // raw ostreams can call report_fatal_error.
97 SmallVector<char, 64> Buffer;
Dan Gohman3490ff42010-08-18 22:04:43 +000098 raw_svector_ostream OS(Buffer);
99 OS << "LLVM ERROR: " << Reason << "\n";
100 StringRef MessageStr = OS.str();
Duncan Sandsc8ba8d22010-09-16 08:20:49 +0000101 ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
102 (void)written; // If something went wrong, we deliberately just give up.
Torok Edwin6c2d2332009-07-07 17:32:34 +0000103 }
Daniel Dunbar401d4c92010-05-08 02:10:36 +0000104
105 // If we reached here, we are failing ungracefully. Run the interrupt handlers
106 // to make sure any special cleanups get done, in particular that we remove
107 // files registered with RemoveFileOnSignal.
108 sys::RunInterruptHandlers();
109
Chad Rosier379574f2012-11-13 16:42:19 +0000110 exit(1);
Torok Edwin6c2d2332009-07-07 17:32:34 +0000111}
112
Reid Klecknerada8c392017-07-11 16:45:30 +0000113void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler,
114 void *user_data) {
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +0000115 std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
Reid Klecknerada8c392017-07-11 16:45:30 +0000116 assert(!ErrorHandler && "Bad alloc error handler already registered!\n");
117 BadAllocErrorHandler = handler;
118 BadAllocErrorHandlerUserData = user_data;
119}
120
121void llvm::remove_bad_alloc_error_handler() {
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +0000122 std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
Reid Klecknerada8c392017-07-11 16:45:30 +0000123 BadAllocErrorHandler = nullptr;
124 BadAllocErrorHandlerUserData = nullptr;
125}
126
127void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) {
128 fatal_error_handler_t Handler = nullptr;
129 void *HandlerData = nullptr;
130 {
131 // Only acquire the mutex while reading the handler, so as not to invoke a
132 // user-supplied callback under a lock.
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +0000133 std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
Reid Klecknerada8c392017-07-11 16:45:30 +0000134 Handler = BadAllocErrorHandler;
135 HandlerData = BadAllocErrorHandlerUserData;
136 }
137
138 if (Handler) {
139 Handler(HandlerData, Reason, GenCrashDiag);
140 llvm_unreachable("bad alloc handler should not return");
141 }
142
143#ifdef LLVM_ENABLE_EXCEPTIONS
144 // If exceptions are enabled, make OOM in malloc look like OOM in new.
145 throw std::bad_alloc();
146#else
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +0000147 // Don't call the normal error handler. It may allocate memory. Directly write
148 // an OOM to stderr and abort.
149 char OOMMessage[] = "LLVM ERROR: out of memory\n";
150 (void)::write(2, OOMMessage, strlen(OOMMessage));
151 abort();
Reid Klecknerada8c392017-07-11 16:45:30 +0000152#endif
153}
154
Chris Lattner59c57532010-04-07 23:12:29 +0000155void llvm::llvm_unreachable_internal(const char *msg, const char *file,
156 unsigned line) {
Dan Gohmane392e622009-08-20 17:15:19 +0000157 // This code intentionally doesn't call the ErrorHandler callback, because
158 // llvm_unreachable is intended to be used to indicate "impossible"
159 // situations, and not legitimate runtime errors.
Torok Edwin56d06592009-07-11 20:10:48 +0000160 if (msg)
David Greene80604d52010-01-05 01:28:29 +0000161 dbgs() << msg << "\n";
162 dbgs() << "UNREACHABLE executed";
Torok Edwin1ab40be2009-07-14 12:49:22 +0000163 if (file)
David Greene80604d52010-01-05 01:28:29 +0000164 dbgs() << " at " << file << ":" << line;
165 dbgs() << "!\n";
Torok Edwin6c2d2332009-07-07 17:32:34 +0000166 abort();
Reid Kleckner5f4535b2013-07-16 14:04:08 +0000167#ifdef LLVM_BUILTIN_UNREACHABLE
168 // Windows systems and possibly others don't declare abort() to be noreturn,
169 // so use the unreachable builtin to avoid a Clang self-host warning.
170 LLVM_BUILTIN_UNREACHABLE;
171#endif
Torok Edwin6c2d2332009-07-07 17:32:34 +0000172}
Filip Pizloa535b142013-10-17 01:38:28 +0000173
174static void bindingsErrorHandler(void *user_data, const std::string& reason,
175 bool gen_crash_diag) {
176 LLVMFatalErrorHandler handler =
Alp Tokeracf49f32013-10-22 12:30:55 +0000177 LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data);
Filip Pizloa535b142013-10-17 01:38:28 +0000178 handler(reason.c_str());
179}
180
181void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) {
Alp Tokeracf49f32013-10-22 12:30:55 +0000182 install_fatal_error_handler(bindingsErrorHandler,
183 LLVM_EXTENSION reinterpret_cast<void *>(Handler));
Filip Pizloa535b142013-10-17 01:38:28 +0000184}
185
186void LLVMResetFatalErrorHandler() {
187 remove_fatal_error_handler();
188}
Rafael Espindola58cb7452014-06-17 18:06:45 +0000189
190#ifdef LLVM_ON_WIN32
191
192#include <winerror.h>
193
194// I'd rather not double the line count of the following.
195#define MAP_ERR_TO_COND(x, y) \
196 case x: \
197 return make_error_code(errc::y)
198
199std::error_code llvm::mapWindowsError(unsigned EV) {
200 switch (EV) {
201 MAP_ERR_TO_COND(ERROR_ACCESS_DENIED, permission_denied);
202 MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS, file_exists);
203 MAP_ERR_TO_COND(ERROR_BAD_UNIT, no_such_device);
204 MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW, filename_too_long);
205 MAP_ERR_TO_COND(ERROR_BUSY, device_or_resource_busy);
206 MAP_ERR_TO_COND(ERROR_BUSY_DRIVE, device_or_resource_busy);
207 MAP_ERR_TO_COND(ERROR_CANNOT_MAKE, permission_denied);
208 MAP_ERR_TO_COND(ERROR_CANTOPEN, io_error);
209 MAP_ERR_TO_COND(ERROR_CANTREAD, io_error);
210 MAP_ERR_TO_COND(ERROR_CANTWRITE, io_error);
211 MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY, permission_denied);
212 MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST, no_such_device);
213 MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE, device_or_resource_busy);
214 MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY, directory_not_empty);
215 MAP_ERR_TO_COND(ERROR_DIRECTORY, invalid_argument);
216 MAP_ERR_TO_COND(ERROR_DISK_FULL, no_space_on_device);
217 MAP_ERR_TO_COND(ERROR_FILE_EXISTS, file_exists);
218 MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND, no_such_file_or_directory);
219 MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL, no_space_on_device);
220 MAP_ERR_TO_COND(ERROR_INVALID_ACCESS, permission_denied);
221 MAP_ERR_TO_COND(ERROR_INVALID_DRIVE, no_such_device);
222 MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION, function_not_supported);
223 MAP_ERR_TO_COND(ERROR_INVALID_HANDLE, invalid_argument);
224 MAP_ERR_TO_COND(ERROR_INVALID_NAME, invalid_argument);
225 MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION, no_lock_available);
226 MAP_ERR_TO_COND(ERROR_LOCKED, no_lock_available);
227 MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK, invalid_argument);
228 MAP_ERR_TO_COND(ERROR_NOACCESS, permission_denied);
229 MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY, not_enough_memory);
230 MAP_ERR_TO_COND(ERROR_NOT_READY, resource_unavailable_try_again);
231 MAP_ERR_TO_COND(ERROR_OPEN_FAILED, io_error);
232 MAP_ERR_TO_COND(ERROR_OPEN_FILES, device_or_resource_busy);
233 MAP_ERR_TO_COND(ERROR_OUTOFMEMORY, not_enough_memory);
234 MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND, no_such_file_or_directory);
235 MAP_ERR_TO_COND(ERROR_BAD_NETPATH, no_such_file_or_directory);
236 MAP_ERR_TO_COND(ERROR_READ_FAULT, io_error);
237 MAP_ERR_TO_COND(ERROR_RETRY, resource_unavailable_try_again);
238 MAP_ERR_TO_COND(ERROR_SEEK, io_error);
239 MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION, permission_denied);
240 MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES, too_many_files_open);
241 MAP_ERR_TO_COND(ERROR_WRITE_FAULT, io_error);
242 MAP_ERR_TO_COND(ERROR_WRITE_PROTECT, permission_denied);
243 MAP_ERR_TO_COND(WSAEACCES, permission_denied);
244 MAP_ERR_TO_COND(WSAEBADF, bad_file_descriptor);
245 MAP_ERR_TO_COND(WSAEFAULT, bad_address);
246 MAP_ERR_TO_COND(WSAEINTR, interrupted);
247 MAP_ERR_TO_COND(WSAEINVAL, invalid_argument);
248 MAP_ERR_TO_COND(WSAEMFILE, too_many_files_open);
249 MAP_ERR_TO_COND(WSAENAMETOOLONG, filename_too_long);
250 default:
251 return std::error_code(EV, std::system_category());
252 }
253}
254
255#endif