about summary refs log tree commit diff
path: root/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-03-17 08:44:44 +0000
committerbors <bors@rust-lang.org>2024-03-17 08:44:44 +0000
commitecdea9e9438cdb8cf707e7f60e066eda02344d05 (patch)
tree75a8849ebe559afe5e37ec67f9ce100221d159eb /compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
parent1eb882ef6d8630109c7b7ea5dcc5ef1c504bb976 (diff)
parent325678c979f3614a342e3439be72524b1b00dd8f (diff)
downloadrust-ecdea9e9438cdb8cf707e7f60e066eda02344d05.tar.gz
rust-ecdea9e9438cdb8cf707e7f60e066eda02344d05.zip
Auto merge of #122625 - matthiaskrgr:rollup-ue4dmnx, r=matthiaskrgr
Rollup of 5 pull requests

Successful merges:

 - #119411 (Add as_(mut_)ptr and as_(mut_)slice to raw array pointers)
 - #122248 (Respect stage0 sysroot when compiling rmake.rs with COMPILETEST_FORCE_STAGE0)
 - #122295 (mir-opt: always run tests for the current target)
 - #122574 (Register LLVM handlers for bad-alloc / OOM)
 - #122608 (Move check-cfg diagnostic logic into a separate file)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp')
-rw-r--r--compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp25
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() {