[all-commits] [llvm/llvm-project] cb4fdc: [Support] Fix thread safety issue in raw_null_ostr...
Scott Pillow via All-commits
all-commits at lists.llvm.org
Fri Oct 10 18:03:09 PDT 2025
Branch: refs/heads/main
Home: https://github.com/llvm/llvm-project
Commit: cb4fdc00102f351606c57afc497d939103cac026
https://github.com/llvm/llvm-project/commit/cb4fdc00102f351606c57afc497d939103cac026
Author: Scott Pillow <scott.pillow at intel.com>
Date: 2025-10-10 (Fri, 10 Oct 2025)
Changed paths:
M llvm/include/llvm/Support/raw_ostream.h
M llvm/unittests/Support/raw_ostream_test.cpp
Log Message:
-----------
[Support] Fix thread safety issue in raw_null_ostream (#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.
To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications
More information about the All-commits
mailing list