Page MenuHomePhabricator

[InitLLVM] Ensure SIGPIPE handler installed before sigaction()
ClosedPublic

Authored by vsk on Jan 8 2021, 11:16 AM.

Details

Summary

The pipe signal handler must be installed before any other handlers are
registered. This is because the Unix 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.

Fixes a bug introduced in D70277.

Tested by running Nick's test case:

% xcrun ./bin/clang -E -fno-integrated-cc1 x.c | tee foo.txt | head

I verified that child cc1 process exits with IO_ERR, and that the parent
recognizes the error code, exiting cleanly.

Diff Detail

Event Timeline

vsk created this revision.Jan 8 2021, 11:16 AM
vsk requested review of this revision.Jan 8 2021, 11:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 8 2021, 11:16 AM

Do you think it would be possible to add test coverage in Clang by using Nick's test?

vsk updated this revision to Diff 315472.Jan 8 2021, 11:41 AM

Add a clang driver test.

Herald added a project: Restricted Project. · View Herald TranscriptJan 8 2021, 11:41 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
vsk updated this revision to Diff 315473.Jan 8 2021, 11:42 AM

Fix a typo in the test.

Much thanks for the patch!

clang/test/Driver/sigpipe-handling.c
3

Looks like

clang -E -fno-integrated-cc1 %s | head

is even enough to repro (no tee needed).

What are the exact conditions to repro Nick's failure? On Fedora 32, I ran py llvm-lit sigpipe-handling.c before this patch and before rGbf401256edd00e921a5d3a0bf4cf6ee66ae51cd6 , the test suceeds, it should not, should it?

vsk updated this revision to Diff 315498.Jan 8 2021, 12:59 PM

Reduce the test a bit further.

vsk added a comment.Jan 8 2021, 1:03 PM

What are the exact conditions to repro Nick's failure? On Fedora 32, I ran py llvm-lit sigpipe-handling.c before this patch and before rGbf401256edd00e921a5d3a0bf4cf6ee66ae51cd6 , the test suceeds, it should not, should it?

In order for the test to fail pre-patch, the OS would need to send a SIGPIPE to clang when it writes to stdout after head exits. It's possible that certain OSes don't do this (perhaps they are patched to SIGPIPE less aggressively? it's the sort of patch an OS maintainer might plausibly ship as a stopgap to appease users).

nickdesaulniers accepted this revision.Jan 8 2021, 1:03 PM
This revision is now accepted and ready to land.Jan 8 2021, 1:03 PM
This revision was automatically updated to reflect the committed changes.