-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[Support] Fix thread safety issue in raw_null_ostream #162787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
254a166
to
1b6cb2c
Compare
@llvm/pr-subscribers-llvm-support Author: Scott Pillow (scottp101) ChangesThe global raw_null_ostream singleton returned by llvm::nulls() is For example: raw_ostream::write(const char *Ptr, size_t Size) This can manifest as a heap corruption when multiple threads write to the The fix is to explicitly pass Unbuffered=true to the raw_pwrite_stream For example, this can fix multithreaded applications using MCELFStreamer Full diff: https://github.com/llvm/llvm-project/pull/162787.diff 2 Files Affected:
diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index f87344e860518..70916d8e4adb0 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -739,7 +739,7 @@ class LLVM_ABI raw_null_ostream : public raw_pwrite_stream {
uint64_t current_pos() const override;
public:
- explicit raw_null_ostream() = default;
+ explicit raw_null_ostream() : raw_pwrite_stream(/*Unbuffered=*/true) {}
~raw_null_ostream() override;
};
diff --git a/llvm/unittests/Support/raw_ostream_test.cpp b/llvm/unittests/Support/raw_ostream_test.cpp
index fbeff37d26a35..a007baa8527b9 100644
--- a/llvm/unittests/Support/raw_ostream_test.cpp
+++ b/llvm/unittests/Support/raw_ostream_test.cpp
@@ -626,6 +626,11 @@ TEST(raw_ostreamTest, writeToDevNull) {
EXPECT_TRUE(DevNullIsUsed);
}
+TEST(raw_ostreamTest, nullStreamZeroBufferSize) {
+ raw_ostream &NullStream = nulls();
+ EXPECT_EQ(NullStream.GetBufferSize(), 0);
+}
+
TEST(raw_ostreamTest, writeToStdOut) {
outs().flush();
testing::internal::CaptureStdout();
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Let's wait for the builds and checks to finish. Tagging @dwblaikie and @kuhar (maintainers for this part of the project) for visibility/review.
fc66401
to
20f30d4
Compare
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.
20f30d4
to
96454bd
Compare
Okay, build succeeded. I don't have merge privileges. Can someone push this through? |
@scottp101 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/154/builds/22604 Here is the relevant piece of the build log for the reference
|
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.
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.