about summary refs log tree commit diff
path: root/src/rustllvm/RustWrapper.cpp
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-10-16 02:06:26 +0000
committerbors <bors@rust-lang.org>2018-10-16 02:06:26 +0000
commitdf0d6ad9c1510630b234f6bb96a69a081de0b307 (patch)
tree3888a6b11bf0f8628561ab18c06d774d468258d8 /src/rustllvm/RustWrapper.cpp
parent46880f41b7aeb897b8245474196bba9dc11f0e88 (diff)
parent1811f1322a46658c1b5b765b5cf6d52aace34e30 (diff)
downloadrust-df0d6ad9c1510630b234f6bb96a69a081de0b307.tar.gz
rust-df0d6ad9c1510630b234f6bb96a69a081de0b307.zip
Auto merge of #55023 - euclio:llvm-error-handler, r=cuviper
Exit with code 101 on fatal codegen errors

Fixes #54992.

This PR installs a custom fatal error handler that prints the error from LLVM and exits with 101. There should be no visible change in the output from LLVM. This allows distinguishing a fatal LLVM error with a compilation error by exit code.

This PR also modifies the LLVM codegen backend to ICE instead of emitting a fatal error when encountering a LLVM worker thread panic for the same reason.

r? @cuviper
Diffstat (limited to 'src/rustllvm/RustWrapper.cpp')
-rw-r--r--src/rustllvm/RustWrapper.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp
index 2b1bf1c0290..bf7afa1b6c0 100644
--- a/src/rustllvm/RustWrapper.cpp
+++ b/src/rustllvm/RustWrapper.cpp
@@ -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 =