/home/arjun/llvm-project/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/Process.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 | 0 | void *user_data) { |
66 | 0 | #if LLVM_ENABLE_THREADS == 1 |
67 | 0 | std::lock_guard<std::mutex> Lock(ErrorHandlerMutex); |
68 | 0 | #endif |
69 | 0 | assert(!ErrorHandler && "Error handler already registered!\n"); |
70 | 0 | ErrorHandler = handler; |
71 | 0 | ErrorHandlerUserData = user_data; |
72 | 0 | } |
73 | | |
74 | 0 | void llvm::remove_fatal_error_handler() { |
75 | 0 | #if LLVM_ENABLE_THREADS == 1 |
76 | 0 | std::lock_guard<std::mutex> Lock(ErrorHandlerMutex); |
77 | 0 | #endif |
78 | 0 | ErrorHandler = nullptr; |
79 | 0 | ErrorHandlerUserData = nullptr; |
80 | 0 | } |
81 | | |
82 | 0 | void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) { |
83 | 0 | report_fatal_error(Twine(Reason), GenCrashDiag); |
84 | 0 | } |
85 | | |
86 | 0 | void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) { |
87 | 0 | report_fatal_error(Twine(Reason), GenCrashDiag); |
88 | 0 | } |
89 | | |
90 | 0 | void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) { |
91 | 0 | report_fatal_error(Twine(Reason), GenCrashDiag); |
92 | 0 | } |
93 | | |
94 | 0 | void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { |
95 | 0 | llvm::fatal_error_handler_t handler = nullptr; |
96 | 0 | void* handlerData = nullptr; |
97 | 0 | { |
98 | 0 | // Only acquire the mutex while reading the handler, so as not to invoke a |
99 | 0 | // user-supplied callback under a lock. |
100 | 0 | #if LLVM_ENABLE_THREADS == 1 |
101 | 0 | std::lock_guard<std::mutex> Lock(ErrorHandlerMutex); |
102 | 0 | #endif |
103 | 0 | handler = ErrorHandler; |
104 | 0 | handlerData = ErrorHandlerUserData; |
105 | 0 | } |
106 | 0 |
|
107 | 0 | if (handler) { |
108 | 0 | handler(handlerData, Reason.str(), GenCrashDiag); |
109 | 0 | } else { |
110 | 0 | // Blast the result out to stderr. We don't try hard to make sure this |
111 | 0 | // succeeds (e.g. handling EINTR) and we can't use errs() here because |
112 | 0 | // raw ostreams can call report_fatal_error. |
113 | 0 | SmallVector<char, 64> Buffer; |
114 | 0 | raw_svector_ostream OS(Buffer); |
115 | 0 | OS << "LLVM ERROR: " << Reason << "\n"; |
116 | 0 | StringRef MessageStr = OS.str(); |
117 | 0 | ssize_t written = ::write(2, MessageStr.data(), MessageStr.size()); |
118 | 0 | (void)written; // If something went wrong, we deliberately just give up. |
119 | 0 | } |
120 | 0 |
|
121 | 0 | // If we reached here, we are failing ungracefully. Run the interrupt handlers |
122 | 0 | // to make sure any special cleanups get done, in particular that we remove |
123 | 0 | // files registered with RemoveFileOnSignal. |
124 | 0 | sys::RunInterruptHandlers(); |
125 | 0 |
|
126 | 0 | abort(); |
127 | 0 | } |
128 | | |
129 | | void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler, |
130 | 0 | void *user_data) { |
131 | 0 | #if LLVM_ENABLE_THREADS == 1 |
132 | 0 | std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex); |
133 | 0 | #endif |
134 | 0 | assert(!ErrorHandler && "Bad alloc error handler already registered!\n"); |
135 | 0 | BadAllocErrorHandler = handler; |
136 | 0 | BadAllocErrorHandlerUserData = user_data; |
137 | 0 | } |
138 | | |
139 | 0 | void llvm::remove_bad_alloc_error_handler() { |
140 | 0 | #if LLVM_ENABLE_THREADS == 1 |
141 | 0 | std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex); |
142 | 0 | #endif |
143 | 0 | BadAllocErrorHandler = nullptr; |
144 | 0 | BadAllocErrorHandlerUserData = nullptr; |
145 | 0 | } |
146 | | |
147 | 0 | void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { |
148 | 0 | fatal_error_handler_t Handler = nullptr; |
149 | 0 | void *HandlerData = nullptr; |
150 | 0 | { |
151 | 0 | // Only acquire the mutex while reading the handler, so as not to invoke a |
152 | 0 | // user-supplied callback under a lock. |
153 | 0 | #if LLVM_ENABLE_THREADS == 1 |
154 | 0 | std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex); |
155 | 0 | #endif |
156 | 0 | Handler = BadAllocErrorHandler; |
157 | 0 | HandlerData = BadAllocErrorHandlerUserData; |
158 | 0 | } |
159 | 0 |
|
160 | 0 | if (Handler) { |
161 | 0 | Handler(HandlerData, Reason, GenCrashDiag); |
162 | 0 | llvm_unreachable("bad alloc handler should not return"); |
163 | 0 | } |
164 | 0 |
|
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 | 0 | // an OOM to stderr and abort. |
171 | 0 | char OOMMessage[] = "LLVM ERROR: out of memory\n"; |
172 | 0 | ssize_t written = ::write(2, OOMMessage, strlen(OOMMessage)); |
173 | 0 | (void)written; |
174 | 0 | abort(); |
175 | 0 | #endif |
176 | 0 | } |
177 | | |
178 | | #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'. |
181 | | void 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'. |
186 | 0 | static void out_of_memory_new_handler() { |
187 | 0 | llvm::report_bad_alloc_error("Allocation failed"); |
188 | 0 | } |
189 | | |
190 | | // Installs new handler that causes crash on allocation failure. It is called by |
191 | | // InitLLVM. |
192 | 0 | void llvm::install_out_of_memory_new_handler() { |
193 | 0 | std::new_handler old = std::set_new_handler(out_of_memory_new_handler); |
194 | 0 | (void)old; |
195 | 0 | assert(old == nullptr && "new-handler already installed"); |
196 | 0 | } |
197 | | #endif |
198 | | |
199 | | void llvm::llvm_unreachable_internal(const char *msg, const char *file, |
200 | 0 | unsigned line) { |
201 | 0 | // This code intentionally doesn't call the ErrorHandler callback, because |
202 | 0 | // llvm_unreachable is intended to be used to indicate "impossible" |
203 | 0 | // situations, and not legitimate runtime errors. |
204 | 0 | if (msg) |
205 | 0 | dbgs() << msg << "\n"; |
206 | 0 | dbgs() << "UNREACHABLE executed"; |
207 | 0 | if (file) |
208 | 0 | dbgs() << " at " << file << ":" << line; |
209 | 0 | dbgs() << "!\n"; |
210 | 0 | abort(); |
211 | 0 | #ifdef LLVM_BUILTIN_UNREACHABLE |
212 | 0 | // Windows systems and possibly others don't declare abort() to be noreturn, |
213 | 0 | // so use the unreachable builtin to avoid a Clang self-host warning. |
214 | 0 | LLVM_BUILTIN_UNREACHABLE; |
215 | 0 | #endif |
216 | 0 | } |
217 | | |
218 | | static void bindingsErrorHandler(void *user_data, const std::string& reason, |
219 | 0 | bool gen_crash_diag) { |
220 | 0 | LLVMFatalErrorHandler handler = |
221 | 0 | LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data); |
222 | 0 | handler(reason.c_str()); |
223 | 0 | } |
224 | | |
225 | 0 | void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) { |
226 | 0 | install_fatal_error_handler(bindingsErrorHandler, |
227 | 0 | LLVM_EXTENSION reinterpret_cast<void *>(Handler)); |
228 | 0 | } |
229 | | |
230 | 0 | void LLVMResetFatalErrorHandler() { |
231 | 0 | remove_fatal_error_handler(); |
232 | 0 | } |
233 | | |
234 | | #ifdef _WIN32 |
235 | | |
236 | | #include <winerror.h> |
237 | | |
238 | | // I'd rather not double the line count of the following. |
239 | | #define MAP_ERR_TO_COND(x, y) \ |
240 | | case x: \ |
241 | | return make_error_code(errc::y) |
242 | | |
243 | | std::error_code llvm::mapWindowsError(unsigned EV) { |
244 | | switch (EV) { |
245 | | MAP_ERR_TO_COND(ERROR_ACCESS_DENIED, permission_denied); |
246 | | MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS, file_exists); |
247 | | MAP_ERR_TO_COND(ERROR_BAD_UNIT, no_such_device); |
248 | | MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW, filename_too_long); |
249 | | MAP_ERR_TO_COND(ERROR_BUSY, device_or_resource_busy); |
250 | | MAP_ERR_TO_COND(ERROR_BUSY_DRIVE, device_or_resource_busy); |
251 | | MAP_ERR_TO_COND(ERROR_CANNOT_MAKE, permission_denied); |
252 | | MAP_ERR_TO_COND(ERROR_CANTOPEN, io_error); |
253 | | MAP_ERR_TO_COND(ERROR_CANTREAD, io_error); |
254 | | MAP_ERR_TO_COND(ERROR_CANTWRITE, io_error); |
255 | | MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY, permission_denied); |
256 | | MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST, no_such_device); |
257 | | MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE, device_or_resource_busy); |
258 | | MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY, directory_not_empty); |
259 | | MAP_ERR_TO_COND(ERROR_DIRECTORY, invalid_argument); |
260 | | MAP_ERR_TO_COND(ERROR_DISK_FULL, no_space_on_device); |
261 | | MAP_ERR_TO_COND(ERROR_FILE_EXISTS, file_exists); |
262 | | MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND, no_such_file_or_directory); |
263 | | MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL, no_space_on_device); |
264 | | MAP_ERR_TO_COND(ERROR_INVALID_ACCESS, permission_denied); |
265 | | MAP_ERR_TO_COND(ERROR_INVALID_DRIVE, no_such_device); |
266 | | MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION, function_not_supported); |
267 | | MAP_ERR_TO_COND(ERROR_INVALID_HANDLE, invalid_argument); |
268 | | MAP_ERR_TO_COND(ERROR_INVALID_NAME, invalid_argument); |
269 | | MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION, no_lock_available); |
270 | | MAP_ERR_TO_COND(ERROR_LOCKED, no_lock_available); |
271 | | MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK, invalid_argument); |
272 | | MAP_ERR_TO_COND(ERROR_NOACCESS, permission_denied); |
273 | | MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY, not_enough_memory); |
274 | | MAP_ERR_TO_COND(ERROR_NOT_READY, resource_unavailable_try_again); |
275 | | MAP_ERR_TO_COND(ERROR_OPEN_FAILED, io_error); |
276 | | MAP_ERR_TO_COND(ERROR_OPEN_FILES, device_or_resource_busy); |
277 | | MAP_ERR_TO_COND(ERROR_OUTOFMEMORY, not_enough_memory); |
278 | | MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND, no_such_file_or_directory); |
279 | | MAP_ERR_TO_COND(ERROR_BAD_NETPATH, no_such_file_or_directory); |
280 | | MAP_ERR_TO_COND(ERROR_READ_FAULT, io_error); |
281 | | MAP_ERR_TO_COND(ERROR_RETRY, resource_unavailable_try_again); |
282 | | MAP_ERR_TO_COND(ERROR_SEEK, io_error); |
283 | | MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION, permission_denied); |
284 | | MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES, too_many_files_open); |
285 | | MAP_ERR_TO_COND(ERROR_WRITE_FAULT, io_error); |
286 | | MAP_ERR_TO_COND(ERROR_WRITE_PROTECT, permission_denied); |
287 | | MAP_ERR_TO_COND(WSAEACCES, permission_denied); |
288 | | MAP_ERR_TO_COND(WSAEBADF, bad_file_descriptor); |
289 | | MAP_ERR_TO_COND(WSAEFAULT, bad_address); |
290 | | MAP_ERR_TO_COND(WSAEINTR, interrupted); |
291 | | MAP_ERR_TO_COND(WSAEINVAL, invalid_argument); |
292 | | MAP_ERR_TO_COND(WSAEMFILE, too_many_files_open); |
293 | | MAP_ERR_TO_COND(WSAENAMETOOLONG, filename_too_long); |
294 | | default: |
295 | | return std::error_code(EV, std::system_category()); |
296 | | } |
297 | | } |
298 | | |
299 | | #endif |