exit with status code 101 on fatal LLVM error

Fixes #54992.
This commit is contained in:
Andy Russell 2018-10-12 15:35:55 -04:00
parent e9e27e6a62
commit 00e1f5b8df
No known key found for this signature in database
GPG Key ID: BE2221033EDBC374
3 changed files with 28 additions and 0 deletions

View File

@ -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);

View File

@ -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();

View File

@ -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"
@ -26,6 +27,8 @@
#include <cstdlib>
#endif
#include <iostream>
//===----------------------------------------------------------------------===
//
// This file defines alternate interfaces to core functions that are more
@ -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 =