diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-03-17 08:23:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-17 08:23:26 +0100 |
| commit | 33b4ed225a1d6b9f1b3bb25d1f0b0c7254dd1553 (patch) | |
| tree | 7e492337fe0420f6cf2d07eb740963fb195ca81c /compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | |
| parent | ea0745604f1ff03f97e61af234bd56ffb193ea07 (diff) | |
| parent | 8d374b1f2af876423435e47b66c01cd6fa38aaa1 (diff) | |
| download | rust-33b4ed225a1d6b9f1b3bb25d1f0b0c7254dd1553.tar.gz rust-33b4ed225a1d6b9f1b3bb25d1f0b0c7254dd1553.zip | |
Rollup merge of #122574 - cuviper:llvm-oom, r=nikic
Register LLVM handlers for bad-alloc / OOM LLVM's default bad-alloc handler may throw if exceptions are enabled, and `operator new` isn't hooked at all by default. Now we register our own handler that prints a message similar to fatal errors, then aborts. We also call the function that registers the C++ `std::new_handler`. Fixes #121305 Cc llvm/llvm-project#85281 r? ``@nikic``
Diffstat (limited to 'compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp')
| -rw-r--r-- | compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 1632b9e1249..91f54da5c12 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -25,6 +25,13 @@ #include <iostream> +// for raw `write` in the bad-alloc handler +#ifdef _MSC_VER +#include <io.h> +#else +#include <unistd.h> +#endif + //===----------------------------------------------------------------------=== // // This file defines alternate interfaces to core functions that are more @@ -88,8 +95,24 @@ static void FatalErrorHandler(void *UserData, exit(101); } -extern "C" void LLVMRustInstallFatalErrorHandler() { +// Custom error handler for bad-alloc LLVM errors. +// +// It aborts the process without any further allocations, similar to LLVM's +// default except that may be configured to `throw std::bad_alloc()` instead. +static void BadAllocErrorHandler(void *UserData, + const char* Reason, + bool GenCrashDiag) { + const char *OOM = "rustc-LLVM ERROR: out of memory\n"; + (void)!::write(2, OOM, strlen(OOM)); + (void)!::write(2, Reason, strlen(Reason)); + (void)!::write(2, "\n", 1); + abort(); +} + +extern "C" void LLVMRustInstallErrorHandlers() { + install_bad_alloc_error_handler(BadAllocErrorHandler); install_fatal_error_handler(FatalErrorHandler); + install_out_of_memory_new_handler(); } extern "C" void LLVMRustDisableSystemDialogsOnCrash() { |
