[PATCH] D94324: [InitLLVM] Ensure SIGPIPE handler installed before sigaction()

Vedant Kumar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 8 15:13:26 PST 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rGe05baf40de8a: [InitLLVM] Ensure SIGPIPE handler installed before sigaction() (authored by vsk).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D94324/new/

https://reviews.llvm.org/D94324

Files:
  clang/test/Driver/sigpipe-handling.c
  llvm/include/llvm/Support/InitLLVM.h
  llvm/lib/Support/InitLLVM.cpp


Index: llvm/lib/Support/InitLLVM.cpp
===================================================================
--- llvm/lib/Support/InitLLVM.cpp
+++ llvm/lib/Support/InitLLVM.cpp
@@ -22,10 +22,17 @@
 using namespace llvm::sys;
 
 InitLLVM::InitLLVM(int &Argc, const char **&Argv,
-                   bool InstallPipeSignalExitHandler)
-    : StackPrinter(Argc, Argv) {
+                   bool InstallPipeSignalExitHandler) {
   if (InstallPipeSignalExitHandler)
+    // The pipe signal handler must be installed before any other handlers are
+    // registered. This is because the Unix \ref RegisterHandlers function does
+    // not perform a sigaction() for SIGPIPE unless a one-shot handler is
+    // present, to allow long-lived processes (like lldb) to fully opt-out of
+    // llvm's SIGPIPE handling and ignore the signal safely.
     sys::SetOneShotPipeSignalFunction(sys::DefaultOneShotPipeSignalHandler);
+  // Initialize the stack printer after installing the one-shot pipe signal
+  // handler, so we can perform a sigaction() for SIGPIPE on Unix if requested.
+  StackPrinter.emplace(Argc, Argv);
   sys::PrintStackTraceOnErrorSignal(Argv[0]);
   install_out_of_memory_new_handler();
 
Index: llvm/include/llvm/Support/InitLLVM.h
===================================================================
--- llvm/include/llvm/Support/InitLLVM.h
+++ llvm/include/llvm/Support/InitLLVM.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_SUPPORT_LLVM_H
 #define LLVM_SUPPORT_LLVM_H
 
+#include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/PrettyStackTrace.h"
@@ -44,7 +45,7 @@
 private:
   BumpPtrAllocator Alloc;
   SmallVector<const char *, 0> Args;
-  PrettyStackTraceProgram StackPrinter;
+  Optional<PrettyStackTraceProgram> StackPrinter;
 };
 } // namespace llvm
 
Index: clang/test/Driver/sigpipe-handling.c
===================================================================
--- /dev/null
+++ clang/test/Driver/sigpipe-handling.c
@@ -0,0 +1,9 @@
+// REQUIRES: shell
+// RUN: %clang -E -fno-integrated-cc1 %s | head | FileCheck %s
+
+// Test that the parent clang driver process doesn't crash when the child cc1
+// process receives a SIGPIPE (Unix-only).
+//
+// The child should exit with IO_ERR, and the parent should exit cleanly.
+
+// CHECK: sigpipe-handling.c


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94324.315529.patch
Type: text/x-patch
Size: 2329 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210108/fdd99ddf/attachment.bin>


More information about the cfe-commits mailing list