[llvm-dev] Race condition in raw_ostream

Viacheslav Nikolaev via llvm-dev llvm-dev at lists.llvm.org
Wed Dec 7 01:52:41 PST 2016


This code from raw_ostream.h is really racy:

  raw_ostream &operator<<(StringRef Str) {
    // Inline fast path, particularly for strings with a known length.
    size_t Size = Str.size();

    // Make sure we can use the fast path.
    if (Size > (size_t)(OutBufEnd - OutBufCur))
      return write(Str.data(), Size);

    if (Size) {
      memcpy(OutBufCur, Str.data(), Size);
      OutBufCur += Size;
    }
    return *this;
  }


Of course, one might wonder why someone would need to output to a stream
from multiple threads at the same time.

But imagine someone might get logs to dbgs() or errs() running the backend
for a target in multiple threads.
Is there any known practice to avoid such a problem?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161207/679440fb/attachment.html>


More information about the llvm-dev mailing list