This is an archive of the discontinued LLVM Phabricator instance.

[Support] Fix crash when multiple raw_fd_ostreams to stdout are created
ClosedPublic

Authored by jhenderson on Mar 2 2017, 9:25 AM.

Details

Summary

If raw_fd_ostream is constructed with the path of "-", it claims ownership of the stdout file descriptor. This means that it closes stdout when it is destroyed. If there are multiple users of raw_fd_ostream wrapped around stdout, then a crash can occur (at least on Windows) because of operations on a closed stream.

An example of this would be running something like "clang -S -o - -MD -MF - test.cpp". Alternatively, using outs() (which creates a local version of raw_fd_stream to stdout) anywhere combined with such a stream usage would cause the crash.

The fix duplicates the stdout file descriptor when used within raw_fd_ostream, so that only that particular descriptor is closed when the stream is destroyed.

Diff Detail

Event Timeline

jhenderson created this revision.Mar 2 2017, 9:25 AM
jhenderson edited the summary of this revision. (Show Details)Mar 3 2017, 8:41 AM
jhenderson updated this revision to Diff 91360.Mar 10 2017, 9:16 AM

Added a lit test to demonstrate the issue in one of the existing tools.

@rafael - I don't have commit access yet, so if you are happy with this still, please would you commit it?

fhahn accepted this revision.Mar 15 2017, 3:44 AM
fhahn added a subscriber: fhahn.
This revision is now accepted and ready to land.Mar 15 2017, 3:44 AM
jhenderson closed this revision.Mar 15 2017, 3:54 AM