Skip to content

Exit with code 101 on fatal codegen errors #55023

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 2 commits into from
Oct 16, 2018
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
6 changes: 2 additions & 4 deletions src/librustc_codegen_llvm/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2031,9 +2031,7 @@ fn start_executing_work(tcx: TyCtxt,
main_thread_worker_state = MainThreadWorkerState::Idle;
}
Message::Done { result: Err(()), worker_id: _ } => {
shared_emitter.fatal("aborting due to worker thread failure");
// Exit the coordinator thread
return Err(())
bug!("worker thread panicked");
}
Message::CodegenItem => {
bug!("the coordinator should not receive codegen requests")
Expand Down Expand Up @@ -2392,7 +2390,7 @@ impl OngoingCodegen {
panic!("expected abort due to worker thread errors")
},
Err(_) => {
sess.fatal("Error during codegen/LLVM phase.");
bug!("panic during codegen/LLVM phase");
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/librustc_codegen_llvm/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ pub mod debuginfo {
extern { pub type ModuleBuffer; }

extern "C" {
pub fn LLVMRustInstallFatalErrorHandler();

// Create and destroy contexts.
pub fn LLVMRustContextCreate(shouldDiscardNames: bool) -> &'static mut Context;
pub fn LLVMContextDispose(C: &'static mut Context);
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_codegen_llvm/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ 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();

{
let mut add = |arg: &str| {
let s = CString::new(arg).unwrap();
Expand Down
24 changes: 24 additions & 0 deletions src/rustllvm/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/Object/Archive.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Bitcode/BitcodeWriterPass.h"
#include "llvm/Support/Signals.h"

#include "llvm/IR/CallSite.h"

Expand All @@ -26,6 +27,8 @@
#include <cstdlib>
#endif

#include <iostream>

//===----------------------------------------------------------------------===
//
// This file defines alternate interfaces to core functions that are more
Expand Down Expand Up @@ -62,6 +65,27 @@ static AtomicOrdering fromRust(LLVMAtomicOrdering Ordering) {

static LLVM_THREAD_LOCAL char *LastError;

// Custom error handler for fatal LLVM errors.
//
// Notably it exits the process with code 101, unlike LLVM's default of 1.
static void FatalErrorHandler(void *UserData,
const std::string& Reason,
bool GenCrashDiag) {
// Do the same thing that the default error handler does.
std::cerr << "LLVM ERROR: " << Reason << std::endl;

// Since this error handler exits the process, we have to run any cleanup that
// LLVM would run after handling the error. This might change with an LLVM
// upgrade.
sys::RunInterruptHandlers();

exit(101);
}

extern "C" void LLVMRustInstallFatalErrorHandler() {
install_fatal_error_handler(FatalErrorHandler);
}

extern "C" LLVMMemoryBufferRef
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOr =
Expand Down