[PATCH] D26809: [lli] Handle SIGSEGV in Jit'ed code more gracefully.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 17 12:22:45 PST 2016


davide created this revision.
davide added reviewers: lhames, Bigcheese, rnk.
davide added a subscriber: llvm-commits.

Before, this just crashed lli. Now, instead, SIGSEGV is intercepted and an error message is emitted.
*TODO: test on windows (the signal interface seems to be the same, but I haven't a machine to try on ATM.
Fixes https://llvm.org/bugs/show_bug.cgi?id=30650


https://reviews.llvm.org/D26809

Files:
  include/llvm/Support/Process.h
  lib/Support/Unix/Process.inc
  lib/Support/Windows/Process.inc
  tools/lli/lli.cpp


Index: tools/lli/lli.cpp
===================================================================
--- tools/lli/lli.cpp
+++ tools/lli/lli.cpp
@@ -357,6 +357,14 @@
   llvm_unreachable("Unrecognized opt level.");
 }
 
+void segfaultHandler(int arg0) {
+  // FIXME: is llvm::errs() signal-safe? If not,
+  // maybe we should just set a flag and return/exit
+  // in the main loop.
+  llvm::errs() << "SIGSEGV in JIT'ed code\n";
+  exit(1);
+}
+
 //===----------------------------------------------------------------------===//
 // main Driver function
 //
@@ -366,6 +374,11 @@
 
   atexit(llvm_shutdown); // Call llvm_shutdown() on exit.
 
+  // We can have undefined behaviour in JIT'ed code, e.g.
+  // unreachable, and that causes lli to crash. Try to
+  // instead intercept the signal and output a sane diagnostic.
+  sys::Process::setSIGSEGVHandler(segfaultHandler);
+
   if (argc > 1)
     ExitOnErr.setBanner(std::string(argv[0]) + ": ");
 
Index: lib/Support/Windows/Process.inc
===================================================================
--- lib/Support/Windows/Process.inc
+++ lib/Support/Windows/Process.inc
@@ -272,6 +272,9 @@
   return std::error_code();
 }
 
+// FIXME: just a stub for now.
+void Process::setSIGSEGVHandler(void (*handler)(int)) {}
+
 std::error_code Process::SafelyCloseFileDescriptor(int FD) {
   if (::close(FD) < 0)
     return std::error_code(errno, std::generic_category());
Index: lib/Support/Unix/Process.inc
===================================================================
--- lib/Support/Unix/Process.inc
+++ lib/Support/Unix/Process.inc
@@ -237,6 +237,10 @@
   return std::error_code();
 }
 
+void Process::setSIGSEGVHandler(void (*handler)(int)) {
+  signal(SIGSEGV, handler);
+}
+
 std::error_code Process::SafelyCloseFileDescriptor(int FD) {
   // Create a signal set filled with *all* signals.
   sigset_t FullSet;
Index: include/llvm/Support/Process.h
===================================================================
--- include/llvm/Support/Process.h
+++ include/llvm/Support/Process.h
@@ -98,6 +98,9 @@
   // components should not call this.
   static std::error_code FixupStandardFileDescriptors();
 
+  // This function sets a signal handler for SIGSEGV.
+  static void setSIGSEGVHandler(void (*handler)(int));
+
   // This function safely closes a file descriptor.  It is not safe to retry
   // close(2) when it returns with errno equivalent to EINTR; this is because
   // *nixen cannot agree if the file descriptor is, in fact, closed when this


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26809.78400.patch
Type: text/x-patch
Size: 2507 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161117/74f16998/attachment.bin>


More information about the llvm-commits mailing list