Skip to content

Register LLVM handlers for bad-alloc / OOM #122574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ extern "C" {

#[link(name = "llvm-wrapper", kind = "static")]
extern "C" {
pub fn LLVMRustInstallFatalErrorHandler();
pub fn LLVMRustInstallErrorHandlers();
pub fn LLVMRustDisableSystemDialogsOnCrash();

// Create and destroy contexts.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ unsafe fn configure_llvm(sess: &Session) {
let mut llvm_c_strs = Vec::with_capacity(n_args + 1);
let mut llvm_args = Vec::with_capacity(n_args + 1);

llvm::LLVMRustInstallFatalErrorHandler();
llvm::LLVMRustInstallErrorHandlers();
// On Windows, an LLVM assertion will open an Abort/Retry/Ignore dialog
// box for the purpose of launching a debugger. However, on CI this will
// cause it to hang until it times out, which can take several hours.
Expand Down
25 changes: 24 additions & 1 deletion compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down