1 //===- llvm/Support/ErrorHandling.h - Fatal error handling ------*- C++ -*-===// 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 #ifndef LLVM_SUPPORT_ERRORHANDLING_H 15 #define LLVM_SUPPORT_ERRORHANDLING_H 16 17 #include "llvm/Support/Compiler.h" 18 19 namespace llvm { 20 class StringRef; 21 class Twine; 22 23 /// An error handler callback. 24 typedef void (*fatal_error_handler_t)(void *user_data, 25 const char *reason, 26 bool gen_crash_diag); 27 28 /// install_fatal_error_handler - Installs a new error handler to be used 29 /// whenever a serious (non-recoverable) error is encountered by LLVM. 30 /// 31 /// If no error handler is installed the default is to print the error message 32 /// to stderr, and call exit(1). If an error handler is installed then it is 33 /// the handler's responsibility to log the message, it will no longer be 34 /// printed to stderr. If the error handler returns, then exit(1) will be 35 /// called. 36 /// 37 /// It is dangerous to naively use an error handler which throws an exception. 38 /// Even though some applications desire to gracefully recover from arbitrary 39 /// faults, blindly throwing exceptions through unfamiliar code isn't a way to 40 /// achieve this. 41 /// 42 /// \param user_data - An argument which will be passed to the install error 43 /// handler. 44 void install_fatal_error_handler(fatal_error_handler_t handler, 45 void *user_data = nullptr); 46 47 /// Restores default error handling behaviour. 48 void remove_fatal_error_handler(); 49 50 /// ScopedFatalErrorHandler - This is a simple helper class which just 51 /// calls install_fatal_error_handler in its constructor and 52 /// remove_fatal_error_handler in its destructor. 53 struct ScopedFatalErrorHandler { 54 explicit ScopedFatalErrorHandler(fatal_error_handler_t handler, 55 void *user_data = nullptr) { 56 install_fatal_error_handler(handler, user_data); 57 } 58 ~ScopedFatalErrorHandlerScopedFatalErrorHandler59 ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); } 60 }; 61 62 /// Reports a serious error, calling any installed error handler. These 63 /// functions are intended to be used for error conditions which are outside 64 /// the control of the compiler (I/O errors, invalid user input, etc.) 65 /// 66 /// If no error handler is installed the default is to print the message to 67 /// standard error, followed by a newline. 68 /// After the error handler is called this function will call abort(), it 69 /// does not return. 70 /// NOTE: The std::string variant was removed to avoid a <string> dependency. 71 [[noreturn]] void report_fatal_error(const char *reason, 72 bool gen_crash_diag = true); 73 [[noreturn]] void report_fatal_error(StringRef reason, 74 bool gen_crash_diag = true); 75 [[noreturn]] void report_fatal_error(const Twine &reason, 76 bool gen_crash_diag = true); 77 78 /// Installs a new bad alloc error handler that should be used whenever a 79 /// bad alloc error, e.g. failing malloc/calloc, is encountered by LLVM. 80 /// 81 /// The user can install a bad alloc handler, in order to define the behavior 82 /// in case of failing allocations, e.g. throwing an exception. Note that this 83 /// handler must not trigger any additional allocations itself. 84 /// 85 /// If no error handler is installed the default is to print the error message 86 /// to stderr, and call exit(1). If an error handler is installed then it is 87 /// the handler's responsibility to log the message, it will no longer be 88 /// printed to stderr. If the error handler returns, then exit(1) will be 89 /// called. 90 /// 91 /// 92 /// \param user_data - An argument which will be passed to the installed error 93 /// handler. 94 void install_bad_alloc_error_handler(fatal_error_handler_t handler, 95 void *user_data = nullptr); 96 97 /// Restores default bad alloc error handling behavior. 98 void remove_bad_alloc_error_handler(); 99 100 void install_out_of_memory_new_handler(); 101 102 /// Reports a bad alloc error, calling any user defined bad alloc 103 /// error handler. In contrast to the generic 'report_fatal_error' 104 /// functions, this function might not terminate, e.g. the user 105 /// defined error handler throws an exception, but it won't return. 106 /// 107 /// Note: When throwing an exception in the bad alloc handler, make sure that 108 /// the following unwind succeeds, e.g. do not trigger additional allocations 109 /// in the unwind chain. 110 /// 111 /// If no error handler is installed (default), throws a bad_alloc exception 112 /// if LLVM is compiled with exception support. Otherwise prints the error 113 /// to standard error and calls abort(). 114 [[noreturn]] void report_bad_alloc_error(const char *Reason, 115 bool GenCrashDiag = true); 116 117 /// This function calls abort(), and prints the optional message to stderr. 118 /// Use the llvm_unreachable macro (that adds location info), instead of 119 /// calling this function directly. 120 [[noreturn]] void 121 llvm_unreachable_internal(const char *msg = nullptr, const char *file = nullptr, 122 unsigned line = 0); 123 } 124 125 /// Marks that the current location is not supposed to be reachable. 126 /// In !NDEBUG builds, prints the message and location info to stderr. 127 /// In NDEBUG builds, if the platform does not support a builtin unreachable 128 /// then we call an internal LLVM runtime function. Otherwise the behavior is 129 /// controlled by the CMake flag 130 /// -DLLVM_UNREACHABLE_OPTIMIZE 131 /// * When "ON" (default) llvm_unreachable() becomes an optimizer hint 132 /// that the current location is not supposed to be reachable: the hint 133 /// turns such code path into undefined behavior. On compilers that don't 134 /// support such hints, prints a reduced message instead and aborts the 135 /// program. 136 /// * When "OFF", a builtin_trap is emitted instead of an 137 // optimizer hint or printing a reduced message. 138 /// 139 /// Use this instead of assert(0). It conveys intent more clearly, suppresses 140 /// diagnostics for unreachable code paths, and allows compilers to omit 141 /// unnecessary code. 142 #ifndef NDEBUG 143 #define llvm_unreachable(msg) \ 144 ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__) 145 #elif !defined(LLVM_BUILTIN_UNREACHABLE) 146 #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal() 147 #elif LLVM_UNREACHABLE_OPTIMIZE 148 #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE 149 #else 150 #define llvm_unreachable(msg) \ 151 do { \ 152 LLVM_BUILTIN_TRAP; \ 153 LLVM_BUILTIN_UNREACHABLE; \ 154 } while (false) 155 #endif 156 157 #endif 158