Skip to content

Commit cb4fdc0

Browse files
authored
[Support] Fix thread safety issue in raw_null_ostream (llvm#162787)
The global raw_null_ostream singleton returned by llvm::nulls() is marked as InternalBuffer rather than Unbuffered, causing it to allocate a buffer when first written to. In multithreaded environments, multiple threads can simultaneously trigger buffer allocation via SetBuffered(), leading to race conditions on the buffer pointer fields (OutBufCur, OutBufEnd). For example: raw_ostream::write(const char *Ptr, size_t Size) -> raw_ostream::SetBuffered() -> raw_ostream::SetBufferSize(size_t Size) -> raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size, BufferKind Mode) This can manifest as a heap corruption when multiple threads write to the null stream concurrently, as the buffer pointers will become corrupted during the race. The fix is to explicitly pass Unbuffered=true to the raw_pwrite_stream constructor, ensuring the null stream never allocates a buffer and all writes go directly to the no-op write_impl(). For example, this can fix multithreaded applications using MCELFStreamer where getCommentOS() returns the shared nulls() singleton.
1 parent 720007e commit cb4fdc0

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

llvm/include/llvm/Support/raw_ostream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ class LLVM_ABI raw_null_ostream : public raw_pwrite_stream {
739739
uint64_t current_pos() const override;
740740

741741
public:
742-
explicit raw_null_ostream() = default;
742+
explicit raw_null_ostream() : raw_pwrite_stream(/*Unbuffered=*/true) {}
743743
~raw_null_ostream() override;
744744
};
745745

llvm/unittests/Support/raw_ostream_test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,11 @@ TEST(raw_ostreamTest, writeToDevNull) {
626626
EXPECT_TRUE(DevNullIsUsed);
627627
}
628628

629+
TEST(raw_ostreamTest, nullStreamZeroBufferSize) {
630+
raw_ostream &NullStream = nulls();
631+
EXPECT_EQ(NullStream.GetBufferSize(), 0u);
632+
}
633+
629634
TEST(raw_ostreamTest, writeToStdOut) {
630635
outs().flush();
631636
testing::internal::CaptureStdout();

0 commit comments

Comments
 (0)