From 82d55239eae14af084fb35f7692d463b5834fb6d Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 13 Mar 2017 14:45:06 +0000 Subject: [PATCH] Fix crash when multiple raw_fd_ostreams to stdout are created. 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 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. Patch by James Henderson! llvm-svn: 297624 --- llvm/lib/Support/raw_ostream.cpp | 2 +- llvm/test/Other/writing-to-stdout.ll | 14 ++++++++++++++ llvm/unittests/Support/raw_ostream_test.cpp | 8 ++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 llvm/test/Other/writing-to-stdout.ll diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp index d073802db932..5deab5de4f6a 100644 --- a/llvm/lib/Support/raw_ostream.cpp +++ b/llvm/lib/Support/raw_ostream.cpp @@ -473,7 +473,7 @@ static int getFD(StringRef Filename, std::error_code &EC, // possible. if (!(Flags & sys::fs::F_Text)) sys::ChangeStdoutToBinary(); - return STDOUT_FILENO; + return dup(STDOUT_FILENO); } int FD; diff --git a/llvm/test/Other/writing-to-stdout.ll b/llvm/test/Other/writing-to-stdout.ll new file mode 100644 index 000000000000..701a6877e467 --- /dev/null +++ b/llvm/test/Other/writing-to-stdout.ll @@ -0,0 +1,14 @@ +; Often LLVM tools use "-" to indicate that output should be written to stdout +; instead of a file. This behaviour is implemented by the raw_fd_ostream class. +; This test verifies that when doing so multiple times we don't try to access a +; closed STDOUT_FILENO. The exact options used in this test are unimportant, as +; long as they write to stdout using raw_fd_ostream. +; RUN: llc %s -o=- -pass-remarks-output=- -filetype=asm | FileCheck %s +; foobar should appear as a function somewhere in the assembly file. +; CHECK: foobar +; !Analysis appears at the start of pass-remarks-output. +; CHECK: !Analysis + +define void @foobar() { + ret void +} diff --git a/llvm/unittests/Support/raw_ostream_test.cpp b/llvm/unittests/Support/raw_ostream_test.cpp index f87d2f60d169..777e555949ee 100644 --- a/llvm/unittests/Support/raw_ostream_test.cpp +++ b/llvm/unittests/Support/raw_ostream_test.cpp @@ -9,6 +9,7 @@ #include "gtest/gtest.h" #include "llvm/ADT/SmallString.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" @@ -330,4 +331,11 @@ TEST(raw_ostreamTest, FormattedHexBytes) { "0007: 68 69 6a 6b 6c |hijkl|", format_bytes_with_ascii_str(B.take_front(12), 0, 7, 1)); } + +TEST(raw_fd_ostreamTest, multiple_raw_fd_ostream_to_stdout) { + std::error_code EC; + + { raw_fd_ostream("-", EC, sys::fs::OpenFlags::F_None); } + { raw_fd_ostream("-", EC, sys::fs::OpenFlags::F_None); } +} }