blob: 21712c5c039e1f3b5907c2a02ff391ff660f1099 [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
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +000048#if LLVM_ENABLE_THREADS == 1
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +000049// 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.
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +000052//
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.
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +000060static std::mutex ErrorHandlerMutex;
61static std::mutex BadAllocErrorHandlerMutex;
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +000062#endif
Reid Klecknerada8c392017-07-11 16:45:30 +000063
Chris Lattner59c57532010-04-07 23:12:29 +000064void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
65 void *user_data) {
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +000066#if LLVM_ENABLE_THREADS == 1
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +000067 std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +000068#endif
Torok Edwin6c2d2332009-07-07 17:32:34 +000069 assert(!ErrorHandler && "Error handler already registered!\n");
70 ErrorHandler = handler;
Daniel Dunbar1bf60c22009-08-10 03:36:26 +000071 ErrorHandlerUserData = user_data;
Torok Edwin6c2d2332009-07-07 17:32:34 +000072}
73
Chris Lattner59c57532010-04-07 23:12:29 +000074void llvm::remove_fatal_error_handler() {
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +000075#if LLVM_ENABLE_THREADS == 1
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +000076 std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +000077#endif
Craig Topperc10719f2014-04-07 04:17:22 +000078 ErrorHandler = nullptr;
Zachary Turner586fd742014-06-13 21:20:44 +000079 ErrorHandlerUserData = nullptr;
Torok Edwin6c2d2332009-07-07 17:32:34 +000080}
81
Chad Rosier583b4d32013-03-27 18:27:54 +000082void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) {
83 report_fatal_error(Twine(Reason), GenCrashDiag);
Daniel Dunbarcd51ea52009-07-24 07:58:10 +000084}
85
Chad Rosier583b4d32013-03-27 18:27:54 +000086void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
87 report_fatal_error(Twine(Reason), GenCrashDiag);
Daniel Dunbarcd51ea52009-07-24 07:58:10 +000088}
89
Alp Toker17d4e982014-01-27 05:24:39 +000090void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
Chad Rosier583b4d32013-03-27 18:27:54 +000091 report_fatal_error(Twine(Reason), GenCrashDiag);
Daniel Dunbar2ed3fe02010-11-13 02:48:51 +000092}
93
Chad Rosier583b4d32013-03-27 18:27:54 +000094void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
Zachary Turner586fd742014-06-13 21:20:44 +000095 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.
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +0000100#if LLVM_ENABLE_THREADS == 1
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +0000101 std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +0000102#endif
Zachary Turner586fd742014-06-13 21:20:44 +0000103 handler = ErrorHandler;
104 handlerData = ErrorHandlerUserData;
105 }
106
107 if (handler) {
108 handler(handlerData, Reason.str(), GenCrashDiag);
Torok Edwin6c2d2332009-07-07 17:32:34 +0000109 } else {
Chris Lattner62170822010-08-17 23:03:53 +0000110 // 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;
Dan Gohman3490ff42010-08-18 22:04:43 +0000114 raw_svector_ostream OS(Buffer);
115 OS << "LLVM ERROR: " << Reason << "\n";
116 StringRef MessageStr = OS.str();
Duncan Sandsc8ba8d22010-09-16 08:20:49 +0000117 ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
118 (void)written; // If something went wrong, we deliberately just give up.
Torok Edwin6c2d2332009-07-07 17:32:34 +0000119 }
Daniel Dunbar401d4c92010-05-08 02:10:36 +0000120
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
Chad Rosier379574f2012-11-13 16:42:19 +0000126 exit(1);
Torok Edwin6c2d2332009-07-07 17:32:34 +0000127}
128
Reid Klecknerada8c392017-07-11 16:45:30 +0000129void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler,
130 void *user_data) {
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +0000131#if LLVM_ENABLE_THREADS == 1
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +0000132 std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +0000133#endif
Reid Klecknerada8c392017-07-11 16:45:30 +0000134 assert(!ErrorHandler && "Bad alloc error handler already registered!\n");
135 BadAllocErrorHandler = handler;
136 BadAllocErrorHandlerUserData = user_data;
137}
138
139void llvm::remove_bad_alloc_error_handler() {
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +0000140#if LLVM_ENABLE_THREADS == 1
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +0000141 std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +0000142#endif
Reid Klecknerada8c392017-07-11 16:45:30 +0000143 BadAllocErrorHandler = nullptr;
144 BadAllocErrorHandlerUserData = nullptr;
145}
146
147void 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.
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +0000153#if LLVM_ENABLE_THREADS == 1
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +0000154 std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
Reid Kleckner1c0f6ed2017-07-13 16:56:24 +0000155#endif
Reid Klecknerada8c392017-07-11 16:45:30 +0000156 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
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +0000169 // 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";
Hans Wennborg82765562017-07-19 15:03:38 +0000172 ssize_t written = ::write(2, OOMMessage, strlen(OOMMessage));
173 (void)written;
Reid Kleckner5ae1bfe2017-07-12 18:23:06 +0000174 abort();
Reid Klecknerada8c392017-07-11 16:45:30 +0000175#endif
176}
177
Serge Pavlovd48042e2018-02-17 10:21:33 +0000178#ifdef LLVM_ENABLE_EXCEPTIONS
179// Do not set custom new handler if exceptions are enabled. In this case OOM
180// errors are handled by throwing 'std::bad_alloc'.
181void llvm::install_out_of_memory_new_handler() {
182}
183#else
184// Causes crash on allocation failure. It is called prior to the handler set by
185// 'install_bad_alloc_error_handler'.
186static void out_of_memory_new_handler() {
187 llvm::report_bad_alloc_error("Allocation failed");
188}
189
190// Installs new handler that causes crash on allocation failure. It does not
191// need to be called explicitly, if this file is linked to application, because
192// in this case it is called during construction of 'new_handler_installer'.
193void llvm::install_out_of_memory_new_handler() {
194 static bool out_of_memory_new_handler_installed = false;
195 if (!out_of_memory_new_handler_installed) {
196 std::set_new_handler(out_of_memory_new_handler);
197 out_of_memory_new_handler_installed = true;
198 }
199}
200
201// Static object that causes installation of 'out_of_memory_new_handler' before
202// execution of 'main'.
203static class NewHandlerInstaller {
204public:
205 NewHandlerInstaller() {
206 install_out_of_memory_new_handler();
207 }
208} new_handler_installer;
209#endif
210
Chris Lattner59c57532010-04-07 23:12:29 +0000211void llvm::llvm_unreachable_internal(const char *msg, const char *file,
212 unsigned line) {
Dan Gohmane392e622009-08-20 17:15:19 +0000213 // This code intentionally doesn't call the ErrorHandler callback, because
214 // llvm_unreachable is intended to be used to indicate "impossible"
215 // situations, and not legitimate runtime errors.
Torok Edwin56d06592009-07-11 20:10:48 +0000216 if (msg)
David Greene80604d52010-01-05 01:28:29 +0000217 dbgs() << msg << "\n";
218 dbgs() << "UNREACHABLE executed";
Torok Edwin1ab40be2009-07-14 12:49:22 +0000219 if (file)
David Greene80604d52010-01-05 01:28:29 +0000220 dbgs() << " at " << file << ":" << line;
221 dbgs() << "!\n";
Torok Edwin6c2d2332009-07-07 17:32:34 +0000222 abort();
Reid Kleckner5f4535b2013-07-16 14:04:08 +0000223#ifdef LLVM_BUILTIN_UNREACHABLE
224 // Windows systems and possibly others don't declare abort() to be noreturn,
225 // so use the unreachable builtin to avoid a Clang self-host warning.
226 LLVM_BUILTIN_UNREACHABLE;
227#endif
Torok Edwin6c2d2332009-07-07 17:32:34 +0000228}
Filip Pizloa535b142013-10-17 01:38:28 +0000229
230static void bindingsErrorHandler(void *user_data, const std::string& reason,
231 bool gen_crash_diag) {
232 LLVMFatalErrorHandler handler =
Alp Tokeracf49f32013-10-22 12:30:55 +0000233 LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data);
Filip Pizloa535b142013-10-17 01:38:28 +0000234 handler(reason.c_str());
235}
236
237void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) {
Alp Tokeracf49f32013-10-22 12:30:55 +0000238 install_fatal_error_handler(bindingsErrorHandler,
239 LLVM_EXTENSION reinterpret_cast<void *>(Handler));
Filip Pizloa535b142013-10-17 01:38:28 +0000240}
241
242void LLVMResetFatalErrorHandler() {
243 remove_fatal_error_handler();
244}
Rafael Espindola58cb7452014-06-17 18:06:45 +0000245
Nico Weber712e8d22018-04-29 00:45:03 +0000246#ifdef _WIN32
Rafael Espindola58cb7452014-06-17 18:06:45 +0000247
248#include <winerror.h>
249
250// I'd rather not double the line count of the following.
251#define MAP_ERR_TO_COND(x, y) \
252 case x: \
253 return make_error_code(errc::y)
254
255std::error_code llvm::mapWindowsError(unsigned EV) {
256 switch (EV) {
257 MAP_ERR_TO_COND(ERROR_ACCESS_DENIED, permission_denied);
258 MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS, file_exists);
259 MAP_ERR_TO_COND(ERROR_BAD_UNIT, no_such_device);
260 MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW, filename_too_long);
261 MAP_ERR_TO_COND(ERROR_BUSY, device_or_resource_busy);
262 MAP_ERR_TO_COND(ERROR_BUSY_DRIVE, device_or_resource_busy);
263 MAP_ERR_TO_COND(ERROR_CANNOT_MAKE, permission_denied);
264 MAP_ERR_TO_COND(ERROR_CANTOPEN, io_error);
265 MAP_ERR_TO_COND(ERROR_CANTREAD, io_error);
266 MAP_ERR_TO_COND(ERROR_CANTWRITE, io_error);
267 MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY, permission_denied);
268 MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST, no_such_device);
269 MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE, device_or_resource_busy);
270 MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY, directory_not_empty);
271 MAP_ERR_TO_COND(ERROR_DIRECTORY, invalid_argument);
272 MAP_ERR_TO_COND(ERROR_DISK_FULL, no_space_on_device);
273 MAP_ERR_TO_COND(ERROR_FILE_EXISTS, file_exists);
274 MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND, no_such_file_or_directory);
275 MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL, no_space_on_device);
276 MAP_ERR_TO_COND(ERROR_INVALID_ACCESS, permission_denied);
277 MAP_ERR_TO_COND(ERROR_INVALID_DRIVE, no_such_device);
278 MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION, function_not_supported);
279 MAP_ERR_TO_COND(ERROR_INVALID_HANDLE, invalid_argument);
280 MAP_ERR_TO_COND(ERROR_INVALID_NAME, invalid_argument);
281 MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION, no_lock_available);
282 MAP_ERR_TO_COND(ERROR_LOCKED, no_lock_available);
283 MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK, invalid_argument);
284 MAP_ERR_TO_COND(ERROR_NOACCESS, permission_denied);
285 MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY, not_enough_memory);
286 MAP_ERR_TO_COND(ERROR_NOT_READY, resource_unavailable_try_again);
287 MAP_ERR_TO_COND(ERROR_OPEN_FAILED, io_error);
288 MAP_ERR_TO_COND(ERROR_OPEN_FILES, device_or_resource_busy);
289 MAP_ERR_TO_COND(ERROR_OUTOFMEMORY, not_enough_memory);
290 MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND, no_such_file_or_directory);
291 MAP_ERR_TO_COND(ERROR_BAD_NETPATH, no_such_file_or_directory);
292 MAP_ERR_TO_COND(ERROR_READ_FAULT, io_error);
293 MAP_ERR_TO_COND(ERROR_RETRY, resource_unavailable_try_again);
294 MAP_ERR_TO_COND(ERROR_SEEK, io_error);
295 MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION, permission_denied);
296 MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES, too_many_files_open);
297 MAP_ERR_TO_COND(ERROR_WRITE_FAULT, io_error);
298 MAP_ERR_TO_COND(ERROR_WRITE_PROTECT, permission_denied);
299 MAP_ERR_TO_COND(WSAEACCES, permission_denied);
300 MAP_ERR_TO_COND(WSAEBADF, bad_file_descriptor);
301 MAP_ERR_TO_COND(WSAEFAULT, bad_address);
302 MAP_ERR_TO_COND(WSAEINTR, interrupted);
303 MAP_ERR_TO_COND(WSAEINVAL, invalid_argument);
304 MAP_ERR_TO_COND(WSAEMFILE, too_many_files_open);
305 MAP_ERR_TO_COND(WSAENAMETOOLONG, filename_too_long);
306 default:
307 return std::error_code(EV, std::system_category());
308 }
309}
310
311#endif