[llvm] b89bcef - Reapply: Add an error message to the default SIGPIPE handler

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 9 12:13:49 PDT 2022


Author: Tim Northover
Date: 2022-06-09T20:13:45+01:00
New Revision: b89bcefa6202e310eb3167dd1c37f1807377ec8d

URL: https://github.com/llvm/llvm-project/commit/b89bcefa6202e310eb3167dd1c37f1807377ec8d
DIFF: https://github.com/llvm/llvm-project/commit/b89bcefa6202e310eb3167dd1c37f1807377ec8d.diff

LOG: Reapply: Add an error message to the default SIGPIPE handler

UNIX03 conformance requires utilities to flush stdout before exiting and raise
an error if writing fails. Flushing already happens on a call to exit
and thus automatically on a return from main. Write failure is then
detected by LLVM's default SIGPIPE handler. The handler already exits with
a non-zero code, but conformance additionally requires an error message.

First reapply attempt I hadn't noticed the test had changed, hopefully this
goes better.

Added: 
    llvm/test/Support/unix03-sigpipe-exit.test

Modified: 
    llvm/lib/Support/Unix/Signals.inc

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/Unix/Signals.inc b/llvm/lib/Support/Unix/Signals.inc
index 23ac012b9e005..bf145bffe8bf2 100644
--- a/llvm/lib/Support/Unix/Signals.inc
+++ b/llvm/lib/Support/Unix/Signals.inc
@@ -432,6 +432,10 @@ void llvm::sys::SetOneShotPipeSignalFunction(void (*Handler)()) {
 }
 
 void llvm::sys::DefaultOneShotPipeSignalHandler() {
+  // UNIX03 conformance requires a non-zero exit code and an error message
+  // to stderr when writing to a closed stdout fails.
+  errs() << "error: write on a pipe with no reader\n";
+
   // Send a special return code that drivers can check for, from sysexits.h.
   exit(EX_IOERR);
 }

diff  --git a/llvm/test/Support/unix03-sigpipe-exit.test b/llvm/test/Support/unix03-sigpipe-exit.test
new file mode 100644
index 0000000000000..01680841db001
--- /dev/null
+++ b/llvm/test/Support/unix03-sigpipe-exit.test
@@ -0,0 +1,26 @@
+## Test that when writing to a closed stdout, LLVM tools finish with a non-zero
+## exit code and an error message on stderr. The test uses llvm-cxxfilt, but
+## it's a logic from the default SIGPIPE handler, so it applies to all the tools.
+## This is required for UNIX03 conformance.
+
+# UNSUPPORTED: system-windows
+
+# RUN: not %python %s llvm-cxxfilt 2>&1 | FileCheck %s
+# CHECK: error: write on a pipe with no reader
+
+import subprocess
+import sys
+
+with subprocess.Popen([sys.argv[1]], stdout=subprocess.PIPE, stdin=subprocess.PIPE) as process:
+  process.stdout.close()
+
+  # llvm-cxxfilt with no extra arguments runs interactively and writes input
+  # to output. Writing continuously to stdin should trigger SIGPIPE when the
+  # subprocess attempts to write out bytes to a closed stdout.
+  try:
+    while True:
+      process.stdin.write("foo\n".encode("utf-8"))
+  except BrokenPipeError:
+    # Clear stdin, pipe is broken and closing it on cleanup will raise an exception.
+    process.stdin = None
+sys.exit(process.returncode)


        


More information about the llvm-commits mailing list