Coverage Report

Created: 2019-07-24 05:18

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