Index: include/llvm/Support/Compiler.h =================================================================== --- include/llvm/Support/Compiler.h +++ include/llvm/Support/Compiler.h @@ -493,4 +493,17 @@ #define LLVM_THREAD_LOCAL #endif +/// \macro LLVM_ENABLE_EXCEPTIONS +/// \brief Whether LLVM is built with exception support. +#if defined(__GNUC__) || defined(__clang__) + #ifdef __EXCEPTIONS + #define LLVM_ENABLE_EXCEPTIONS 1 + #endif +#elif defined(_MSC_VER) + #ifdef _CPPUNWIND + #define LLVM_ENABLE_EXCEPTIONS 1 + #endif +#endif + + #endif Index: include/llvm/Support/ErrorHandling.h =================================================================== --- include/llvm/Support/ErrorHandling.h +++ include/llvm/Support/ErrorHandling.h @@ -78,6 +78,45 @@ LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const Twine &reason, bool gen_crash_diag = true); + +/// Installs a new bad alloc error handler that should be used whenever a +/// bad alloc error, e.g. failing malloc/calloc, is encountered by LLVM. +/// +/// The user can install a bad alloc handler, in order to define the behavior +/// in case of failing allocations, e.g. throwing an exception. Note that this +/// handler must not trigger any additional allocations itself. +/// +/// If no error handler is installed the default is to print the error message +/// to stderr, and call exit(1). If an error handler is installed then it is +/// the handler's responsibility to log the message, it will no longer be +/// printed to stderr. If the error handler returns, then exit(1) will be +/// called. +/// +/// +/// \param user_data - An argument which will be passed to the installed error +/// handler. +void install_bad_alloc_error_handler(fatal_error_handler_t handler, + void *user_data = nullptr); + +/// Restores default bad alloc error handling behavior. +void remove_bad_alloc_error_handler(); + +/// Reports a bad alloc error, calling any user defined bad alloc +/// error handler. In contrast to the generic 'report_fatal_error' +/// functions, this function is expected to return, e.g. the user +/// defined error handler throws an exception. +/// +/// Note: When throwing an exception in the bad alloc handler, make sure that +/// the following unwind succeeds, e.g. do not trigger additional allocations +/// in the unwind chain. +/// +/// If no error handler is installed (default), then a bad_alloc exception +/// is thrown if LLVM is compiled with exception support, otherwise an assertion +/// is called. +void report_bad_alloc_error(const char *reason, + bool gen_crash_diag = true); + + /// This function calls abort(), and prints the optional message to stderr. /// Use the llvm_unreachable macro (that adds location info), instead of /// calling this function directly. Index: lib/Support/ErrorHandling.cpp =================================================================== --- lib/Support/ErrorHandling.cpp +++ lib/Support/ErrorHandling.cpp @@ -29,6 +29,9 @@ #include "llvm/Support/raw_ostream.h" #include #include +#ifdef LLVM_ENABLE_EXCEPTIONS +#include +#endif #if defined(HAVE_UNISTD_H) # include @@ -42,9 +45,13 @@ static fatal_error_handler_t ErrorHandler = nullptr; static void *ErrorHandlerUserData = nullptr; - static ManagedStatic ErrorHandlerMutex; +static fatal_error_handler_t BadAllocErrorHandler = nullptr; +static void *BadAllocErrorHandlerUserData = nullptr; +static ManagedStatic BadAllocErrorHandlerMutex; + + void llvm::install_fatal_error_handler(fatal_error_handler_t handler, void *user_data) { llvm::MutexGuard Lock(*ErrorHandlerMutex); @@ -104,6 +111,34 @@ exit(1); } +void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler, + void *user_data) { + llvm::MutexGuard Lock(*BadAllocErrorHandlerMutex); + assert(!ErrorHandler && "Bad alloc error handler already registered!\n"); + BadAllocErrorHandler = handler; + BadAllocErrorHandlerUserData = user_data; +} + +void llvm::remove_bad_alloc_error_handler() { + llvm::MutexGuard Lock(*BadAllocErrorHandlerMutex); + BadAllocErrorHandler = nullptr; + BadAllocErrorHandlerUserData = nullptr; +} + + +void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { + if (BadAllocErrorHandler) { + BadAllocErrorHandler(BadAllocErrorHandlerUserData, Reason, GenCrashDiag); + } else { +#ifdef LLVM_ENABLE_EXCEPTIONS + throw std::bad_alloc(); +#else + assert(false && "Bad alloc error: Unable to allocate memory.\n"); +#endif + } +} + + void llvm::llvm_unreachable_internal(const char *msg, const char *file, unsigned line) { // This code intentionally doesn't call the ErrorHandler callback, because Index: lib/Support/Mutex.cpp =================================================================== --- lib/Support/Mutex.cpp +++ lib/Support/Mutex.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/Mutex.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Config/config.h" //===----------------------------------------------------------------------===// @@ -47,6 +48,10 @@ // Declare the pthread_mutex data structures pthread_mutex_t* mutex = static_cast(malloc(sizeof(pthread_mutex_t))); + + if (mutex == nullptr) + report_bad_alloc_error("Mutex allocation failed"); + pthread_mutexattr_t attr; // Initialize the mutex attributes