If you're curious, The unit test writes a string followed by an integer at an unaligned address.  I have a fix in mind, just can't do it until I get in<br><div class="gmail_quote"><div dir="ltr">On Fri, Jun 10, 2016 at 8:50 AM Zachary Turner <<a href="mailto:zturner@google.com">zturner@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The bug is definitely in the unit test, fairly certain I already know the problem, but I'm not at a computer for ~3 more hours <br><div class="gmail_quote"><div dir="ltr">On Fri, Jun 10, 2016 at 8:48 AM Adrian Prantl <<a href="mailto:aprantl@apple.com" target="_blank">aprantl@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
> On Jun 10, 2016, at 8:42 AM, Zachary Turner <<a href="mailto:zturner@google.com" target="_blank">zturner@google.com</a>> wrote:<br>
><br>
> Ok. You can comment this unit test out if you need to get the tree green<br>
<br>
Just commenting out the unit test sounds a bit dangerous to me :-)<br>
Would you mind reverting atomically the commit until we can be sure that the bug is in the unit test and not in the new code?<br>
<br>
Let me know if I can help you gathering any diagnostics if you’re having trouble reproducing the failure.<br>
<br>
thanks,<br>
adrian<br>
<br>
<br>
> On Fri, Jun 10, 2016 at 8:33 AM Adrian Prantl <<a href="mailto:aprantl@apple.com" target="_blank">aprantl@apple.com</a>> wrote:<br>
> Hi Zachary,<br>
><br>
> it looks like the<br>
>   DebugInfo/PDB/DebugInfoPDBTests/MappedBlockStreamTest.TestWriteThenRead<br>
> unit test is failing under ASAN.<br>
><br>
> <a href="http://lab.llvm.org:8080/green/job/clang-stage2-cmake-RgSan_check/1892/consoleFull#5318301998254eaf0-7326-4999-85b0-388101f2d404" rel="noreferrer" target="_blank">http://lab.llvm.org:8080/green/job/clang-stage2-cmake-RgSan_check/1892/consoleFull#5318301998254eaf0-7326-4999-85b0-388101f2d404</a><br>
><br>
> > Note: Google Test filter = MappedBlockStreamTest.TestWriteThenRead<br>
> > [==========] Running 1 test from 1 test case.<br>
> > [----------] Global test environment set-up.<br>
> > [----------] 1 test from MappedBlockStreamTest<br>
> > [ RUN      ] MappedBlockStreamTest.TestWriteThenRead<br>
> > /Users/buildslave/jenkins/sharedspace/clang-stage2-cmake-RgSan@2/host-compiler/bin/../include/c++/v1/algorithm:1199:32: runtime error: reference binding to misaligned address 0x62100001cd06 for type 'const unsigned int', which requires 4 byte alignment<br>
> > 0x62100001cd06: note: pointer points here<br>
> >  50 5c 17 35 50 5c  17 35 7a c2 bb 01 50 5c  17 35 7a c2 bb 01 72 be  be be be be be be be be  be be<br>
> >              ^<br>
> > SUMMARY: AddressSanitizer: undefined-behavior /Users/buildslave/jenkins/sharedspace/clang-stage2-cmake-RgSan@2/host-compiler/bin/../include/c++/v1/algorithm:1199:32 in<br>
><br>
><br>
> Could you please investigate?<br>
><br>
> thanks,<br>
> adrian<br>
><br>
> > On Jun 9, 2016, at 10:09 PM, Zachary Turner via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br>
> ><br>
> > Author: zturner<br>
> > Date: Fri Jun 10 00:09:12 2016<br>
> > New Revision: 272369<br>
> ><br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project?rev=272369&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=272369&view=rev</a><br>
> > Log:<br>
> > Add support for writing through StreamInterface.<br>
> ><br>
> > This adds method and tests for writing to a PDB stream.  With<br>
> > this, even a PDB stream which is discontiguous can be treated<br>
> > as a sequential stream of bytes for the purposes of writing.<br>
> ><br>
> > Reviewed By: ruiu<br>
> > Differential Revision: <a href="http://reviews.llvm.org/D21157" rel="noreferrer" target="_blank">http://reviews.llvm.org/D21157</a><br>
> ><br>
> > Added:<br>
> >    llvm/trunk/include/llvm/DebugInfo/CodeView/StreamWriter.h<br>
> >    llvm/trunk/lib/DebugInfo/CodeView/StreamWriter.cpp<br>
> > Modified:<br>
> >    llvm/trunk/include/llvm/DebugInfo/CodeView/ByteStream.h<br>
> >    llvm/trunk/include/llvm/DebugInfo/CodeView/CodeViewError.h<br>
> >    llvm/trunk/include/llvm/DebugInfo/CodeView/StreamArray.h<br>
> >    llvm/trunk/include/llvm/DebugInfo/CodeView/StreamInterface.h<br>
> >    llvm/trunk/include/llvm/DebugInfo/CodeView/StreamReader.h<br>
> >    llvm/trunk/include/llvm/DebugInfo/CodeView/StreamRef.h<br>
> >    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/IPDBFile.h<br>
> >    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h<br>
> >    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBFile.h<br>
> >    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/RawError.h<br>
> >    llvm/trunk/include/llvm/Support/MathExtras.h<br>
> >    llvm/trunk/lib/DebugInfo/CodeView/ByteStream.cpp<br>
> >    llvm/trunk/lib/DebugInfo/CodeView/CMakeLists.txt<br>
> >    llvm/trunk/lib/DebugInfo/CodeView/CodeViewError.cpp<br>
> >    llvm/trunk/lib/DebugInfo/CodeView/StreamReader.cpp<br>
> >    llvm/trunk/lib/DebugInfo/CodeView/TypeDumper.cpp<br>
> >    llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp<br>
> >    llvm/trunk/lib/DebugInfo/PDB/Raw/PDBFile.cpp<br>
> >    llvm/trunk/lib/DebugInfo/PDB/Raw/RawError.cpp<br>
> >    llvm/trunk/tools/llvm-readobj/COFFDumper.cpp<br>
> >    llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp<br>
> ><br>
> > Modified: llvm/trunk/include/llvm/DebugInfo/CodeView/ByteStream.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/ByteStream.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/ByteStream.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/CodeView/ByteStream.h (original)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/CodeView/ByteStream.h Fri Jun 10 00:09:12 2016<br>
> > @@ -16,19 +16,27 @@<br>
> > #include "llvm/Support/Error.h"<br>
> > #include <cstdint><br>
> > #include <memory><br>
> > +#include <type_traits><br>
> ><br>
> > namespace llvm {<br>
> > namespace codeview {<br>
> > class StreamReader;<br>
> ><br>
> > -class ByteStream : public StreamInterface {<br>
> > +template <bool Writable = false> class ByteStream : public StreamInterface {<br>
> > +  typedef typename std::conditional<Writable, MutableArrayRef<uint8_t>,<br>
> > +                                    ArrayRef<uint8_t>>::type ArrayType;<br>
> > +<br>
> > public:<br>
> > -  ByteStream();<br>
> > -  explicit ByteStream(ArrayRef<uint8_t> Data);<br>
> > -  ~ByteStream() override;<br>
> > +  ByteStream() {}<br>
> > +  explicit ByteStream(ArrayType Data) : Data(Data) {}<br>
> > +  ~ByteStream() override {}<br>
> ><br>
> >   Error readBytes(uint32_t Offset, uint32_t Size,<br>
> >                   ArrayRef<uint8_t> &Buffer) const override;<br>
> > +  Error readLongestContiguousChunk(uint32_t Offset,<br>
> > +                                   ArrayRef<uint8_t> &Buffer) const override;<br>
> > +<br>
> > +  Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Buffer) const override;<br>
> ><br>
> >   uint32_t getLength() const override;<br>
> ><br>
> > @@ -36,9 +44,12 @@ public:<br>
> >   StringRef str() const;<br>
> ><br>
> > private:<br>
> > -  ArrayRef<uint8_t> Data;<br>
> > +  ArrayType Data;<br>
> > };<br>
> ><br>
> > +extern template class ByteStream<true>;<br>
> > +extern template class ByteStream<false>;<br>
> > +<br>
> > } // end namespace pdb<br>
> > } // end namespace llvm<br>
> ><br>
> ><br>
> > Modified: llvm/trunk/include/llvm/DebugInfo/CodeView/CodeViewError.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/CodeViewError.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/CodeViewError.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/CodeView/CodeViewError.h (original)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/CodeView/CodeViewError.h Fri Jun 10 00:09:12 2016<br>
> > @@ -19,6 +19,7 @@ namespace codeview {<br>
> > enum class cv_error_code {<br>
> >   unspecified = 1,<br>
> >   insufficient_buffer,<br>
> > +  operation_unsupported,<br>
> >   corrupt_record,<br>
> > };<br>
> ><br>
> ><br>
> > Modified: llvm/trunk/include/llvm/DebugInfo/CodeView/StreamArray.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/StreamArray.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/StreamArray.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/CodeView/StreamArray.h (original)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/CodeView/StreamArray.h Fri Jun 10 00:09:12 2016<br>
> > @@ -97,6 +97,8 @@ public:<br>
> ><br>
> >   const Extractor &getExtractor() const { return E; }<br>
> ><br>
> > +  StreamRef getUnderlyingStream() const { return Stream; }<br>
> > +<br>
> > private:<br>
> >   StreamRef Stream;<br>
> >   Extractor E;<br>
> ><br>
> > Modified: llvm/trunk/include/llvm/DebugInfo/CodeView/StreamInterface.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/StreamInterface.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/StreamInterface.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/CodeView/StreamInterface.h (original)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/CodeView/StreamInterface.h Fri Jun 10 00:09:12 2016<br>
> > @@ -28,9 +28,22 @@ class StreamInterface {<br>
> > public:<br>
> >   virtual ~StreamInterface() {}<br>
> ><br>
> > +  // Given an offset into the stream and a number of bytes, attempt to read<br>
> > +  // the bytes and set the output ArrayRef to point to a reference into the<br>
> > +  // stream, without copying any data.<br>
> >   virtual Error readBytes(uint32_t Offset, uint32_t Size,<br>
> >                           ArrayRef<uint8_t> &Buffer) const = 0;<br>
> ><br>
> > +  // Given an offset into the stream, read as much as possible without copying<br>
> > +  // any data.<br>
> > +  virtual Error readLongestContiguousChunk(uint32_t Offset,<br>
> > +                                           ArrayRef<uint8_t> &Buffer) const = 0;<br>
> > +<br>
> > +  // Attempt to write the given bytes into the stream at the desired offset.<br>
> > +  // This will always necessitate a copy.  Cannot shrink or grow the stream,<br>
> > +  // only writes into existing allocated space.<br>
> > +  virtual Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) const = 0;<br>
> > +<br>
> >   virtual uint32_t getLength() const = 0;<br>
> > };<br>
> ><br>
> ><br>
> > Modified: llvm/trunk/include/llvm/DebugInfo/CodeView/StreamReader.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/StreamReader.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/StreamReader.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/CodeView/StreamReader.h (original)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/CodeView/StreamReader.h Fri Jun 10 00:09:12 2016<br>
> > @@ -28,6 +28,7 @@ class StreamReader {<br>
> > public:<br>
> >   StreamReader(StreamRef Stream);<br>
> ><br>
> > +  Error readLongestContiguousChunk(ArrayRef<uint8_t> &Buffer);<br>
> >   Error readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size);<br>
> >   Error readInteger(uint16_t &Dest);<br>
> >   Error readInteger(uint32_t &Dest);<br>
> ><br>
> > Modified: llvm/trunk/include/llvm/DebugInfo/CodeView/StreamRef.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/StreamRef.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/StreamRef.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/CodeView/StreamRef.h (original)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/CodeView/StreamRef.h Fri Jun 10 00:09:12 2016<br>
> > @@ -24,7 +24,8 @@ public:<br>
> >   StreamRef(const StreamInterface &Stream, uint32_t Offset, uint32_t Length)<br>
> >       : Stream(&Stream), ViewOffset(Offset), Length(Length) {}<br>
> ><br>
> > -  StreamRef(const StreamRef &Stream, uint32_t Offset, uint32_t Length) = delete;<br>
> > +  // Use StreamRef.slice() instead.<br>
> > +  StreamRef(const StreamRef &S, uint32_t Offset, uint32_t Length) = delete;<br>
> ><br>
> >   Error readBytes(uint32_t Offset, uint32_t Size,<br>
> >                   ArrayRef<uint8_t> &Buffer) const override {<br>
> > @@ -33,7 +34,32 @@ public:<br>
> >     return Stream->readBytes(ViewOffset + Offset, Size, Buffer);<br>
> >   }<br>
> ><br>
> > +  // Given an offset into the stream, read as much as possible without copying<br>
> > +  // any data.<br>
> > +  Error readLongestContiguousChunk(uint32_t Offset,<br>
> > +                                   ArrayRef<uint8_t> &Buffer) const override {<br>
> > +    if (Offset >= Length)<br>
> > +      return make_error<CodeViewError>(cv_error_code::insufficient_buffer);<br>
> > +<br>
> > +    if (auto EC = Stream->readLongestContiguousChunk(Offset, Buffer))<br>
> > +      return EC;<br>
> > +    // This StreamRef might refer to a smaller window over a larger stream.  In<br>
> > +    // that case we will have read out more bytes than we should return, because<br>
> > +    // we should not read past the end of the current view.<br>
> > +    uint32_t MaxLength = Length - Offset;<br>
> > +    if (Buffer.size() > MaxLength)<br>
> > +      Buffer = Buffer.slice(0, MaxLength);<br>
> > +    return Error::success();<br>
> > +  }<br>
> > +<br>
> > +  Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) const override {<br>
> > +    if (Data.size() + Offset > Length)<br>
> > +      return make_error<CodeViewError>(cv_error_code::insufficient_buffer);<br>
> > +    return Stream->writeBytes(ViewOffset + Offset, Data);<br>
> > +  }<br>
> > +<br>
> >   uint32_t getLength() const override { return Length; }<br>
> > +<br>
> >   StreamRef drop_front(uint32_t N) const {<br>
> >     if (!Stream)<br>
> >       return StreamRef();<br>
> ><br>
> > Added: llvm/trunk/include/llvm/DebugInfo/CodeView/StreamWriter.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/StreamWriter.h?rev=272369&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/StreamWriter.h?rev=272369&view=auto</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/CodeView/StreamWriter.h (added)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/CodeView/StreamWriter.h Fri Jun 10 00:09:12 2016<br>
> > @@ -0,0 +1,82 @@<br>
> > +//===- StreamWriter.h - Writes bytes and objects to a stream ----*- C++ -*-===//<br>
> > +//<br>
> > +//                     The LLVM Compiler Infrastructure<br>
> > +//<br>
> > +// This file is distributed under the University of Illinois Open Source<br>
> > +// License. See LICENSE.TXT for details.<br>
> > +//<br>
> > +//===----------------------------------------------------------------------===//<br>
> > +<br>
> > +#ifndef LLVM_DEBUGINFO_CODEVIEW_STREAMWRITER_H<br>
> > +#define LLVM_DEBUGINFO_CODEVIEW_STREAMWRITER_H<br>
> > +<br>
> > +#include "llvm/ADT/ArrayRef.h"<br>
> > +#include "llvm/DebugInfo/CodeView/CodeViewError.h"<br>
> > +#include "llvm/DebugInfo/CodeView/StreamArray.h"<br>
> > +#include "llvm/DebugInfo/CodeView/StreamInterface.h"<br>
> > +#include "llvm/Support/Endian.h"<br>
> > +#include "llvm/Support/Error.h"<br>
> > +<br>
> > +#include <string><br>
> > +<br>
> > +namespace llvm {<br>
> > +namespace codeview {<br>
> > +<br>
> > +class StreamRef;<br>
> > +<br>
> > +class StreamWriter {<br>
> > +public:<br>
> > +  StreamWriter(StreamRef Stream);<br>
> > +<br>
> > +  Error writeBytes(ArrayRef<uint8_t> Buffer);<br>
> > +  Error writeInteger(uint16_t Dest);<br>
> > +  Error writeInteger(uint32_t Dest);<br>
> > +  Error writeZeroString(StringRef Str);<br>
> > +  Error writeFixedString(StringRef Str);<br>
> > +  Error writeStreamRef(StreamRef Ref);<br>
> > +  Error writeStreamRef(StreamRef Ref, uint32_t Size);<br>
> > +<br>
> > +  template <typename T> Error writeEnum(T Num) {<br>
> > +    return writeInteger(<br>
> > +        static_cast<typename std::underlying_type<T>::type>(Num));<br>
> > +  }<br>
> > +<br>
> > +  template <typename T> Error writeObject(const T &Obj) {<br>
> > +    return writeBytes(<br>
> > +        ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(&Obj), sizeof(T)));<br>
> > +  }<br>
> > +<br>
> > +  template <typename T> Error writeArray(ArrayRef<T> Array) {<br>
> > +    if (Array.size() == 0)<br>
> > +      return Error::success();<br>
> > +<br>
> > +    if (Array.size() > UINT32_MAX / sizeof(T))<br>
> > +      return make_error<CodeViewError>(cv_error_code::insufficient_buffer);<br>
> > +<br>
> > +    return writeBytes(<br>
> > +        ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Array.data()),<br>
> > +                          Array.size() * sizeof(T)));<br>
> > +  }<br>
> > +<br>
> > +  template <typename T, typename U><br>
> > +  Error writeArray(VarStreamArray<T, U> Array) {<br>
> > +    return writeStreamRef(Array.getUnderlyingStream());<br>
> > +  }<br>
> > +<br>
> > +  template <typename T> Error writeArray(FixedStreamArray<T> Array) {<br>
> > +    return writeStreamRef(Array.getUnderlyingStream());<br>
> > +  }<br>
> > +<br>
> > +  void setOffset(uint32_t Off) { Offset = Off; }<br>
> > +  uint32_t getOffset() const { return Offset; }<br>
> > +  uint32_t getLength() const { return Stream.getLength(); }<br>
> > +  uint32_t bytesRemaining() const { return getLength() - getOffset(); }<br>
> > +<br>
> > +private:<br>
> > +  StreamRef Stream;<br>
> > +  uint32_t Offset;<br>
> > +};<br>
> > +} // namespace codeview<br>
> > +} // namespace llvm<br>
> > +<br>
> > +#endif // LLVM_DEBUGINFO_CODEVIEW_STREAMREADER_H<br>
> ><br>
> > Modified: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/IPDBFile.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/IPDBFile.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/IPDBFile.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/IPDBFile.h (original)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/IPDBFile.h Fri Jun 10 00:09:12 2016<br>
> > @@ -14,6 +14,7 @@<br>
> > #include "llvm/ADT/StringRef.h"<br>
> > #include "llvm/DebugInfo/CodeView/StreamArray.h"<br>
> > #include "llvm/Support/Endian.h"<br>
> > +#include "llvm/Support/Error.h"<br>
> ><br>
> > #include <stdint.h><br>
> ><br>
> > @@ -34,6 +35,8 @@ public:<br>
> ><br>
> >   virtual ArrayRef<uint8_t> getBlockData(uint32_t BlockIndex,<br>
> >                                          uint32_t NumBytes) const = 0;<br>
> > +  virtual Error setBlockData(uint32_t BlockIndex, uint32_t Offset,<br>
> > +                             ArrayRef<uint8_t> Data) const = 0;<br>
> > };<br>
> > }<br>
> > }<br>
> ><br>
> > Modified: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h (original)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/MappedBlockStream.h Fri Jun 10 00:09:12 2016<br>
> > @@ -31,6 +31,9 @@ class MappedBlockStream : public codevie<br>
> > public:<br>
> >   Error readBytes(uint32_t Offset, uint32_t Size,<br>
> >                   ArrayRef<uint8_t> &Buffer) const override;<br>
> > +  Error readLongestContiguousChunk(uint32_t Offset,<br>
> > +                                   ArrayRef<uint8_t> &Buffer) const override;<br>
> > +  Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Buffer) const override;<br>
> ><br>
> >   uint32_t getLength() const override;<br>
> ><br>
> > @@ -51,8 +54,9 @@ protected:<br>
> >   const IPDBFile &Pdb;<br>
> >   std::unique_ptr<IPDBStreamData> Data;<br>
> ><br>
> > +  typedef MutableArrayRef<uint8_t> CacheEntry;<br>
> >   mutable llvm::BumpPtrAllocator Pool;<br>
> > -  mutable DenseMap<uint32_t, uint8_t *> CacheMap;<br>
> > +  mutable DenseMap<uint32_t, std::vector<CacheEntry>> CacheMap;<br>
> > };<br>
> ><br>
> > } // end namespace pdb<br>
> ><br>
> > Modified: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBFile.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBFile.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBFile.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBFile.h (original)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/PDBFile.h Fri Jun 10 00:09:12 2016<br>
> > @@ -54,6 +54,8 @@ public:<br>
> ><br>
> >   ArrayRef<uint8_t> getBlockData(uint32_t BlockIndex,<br>
> >                                  uint32_t NumBytes) const override;<br>
> > +  Error setBlockData(uint32_t BlockIndex, uint32_t Offset,<br>
> > +                     ArrayRef<uint8_t> Data) const override;<br>
> ><br>
> >   ArrayRef<support::ulittle32_t> getDirectoryBlockArray() const;<br>
> ><br>
> ><br>
> > Modified: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/RawError.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/RawError.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/RawError.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/RawError.h (original)<br>
> > +++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/RawError.h Fri Jun 10 00:09:12 2016<br>
> > @@ -22,7 +22,9 @@ enum class raw_error_code {<br>
> >   corrupt_file,<br>
> >   insufficient_buffer,<br>
> >   no_stream,<br>
> > -  index_out_of_bounds<br>
> > +  index_out_of_bounds,<br>
> > +  invalid_block_address,<br>
> > +  not_writable,<br>
> > };<br>
> ><br>
> > /// Base class for errors originating when parsing raw PDB files<br>
> ><br>
> > Modified: llvm/trunk/include/llvm/Support/MathExtras.h<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/include/llvm/Support/MathExtras.h (original)<br>
> > +++ llvm/trunk/include/llvm/Support/MathExtras.h Fri Jun 10 00:09:12 2016<br>
> > @@ -681,6 +681,14 @@ inline int64_t SignExtend64(uint64_t X,<br>
> >   return int64_t(X << (64 - B)) >> (64 - B);<br>
> > }<br>
> ><br>
> > +/// \brief Subtract two unsigned integers, X and Y, of type T and return their<br>
> > +/// absolute value.<br>
> > +template <typename T><br>
> > +typename std::enable_if<std::is_unsigned<T>::value, T>::type<br>
> > +AbsoluteDifference(T X, T Y) {<br>
> > +  return std::max(X, Y) - std::min(X, Y);<br>
> > +}<br>
> > +<br>
> > /// \brief Add two unsigned integers, X and Y, of type T.<br>
> > /// Clamp the result to the maximum representable value of T on overflow.<br>
> > /// ResultOverflowed indicates if the result is larger than the maximum<br>
> ><br>
> > Modified: llvm/trunk/lib/DebugInfo/CodeView/ByteStream.cpp<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/ByteStream.cpp?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/ByteStream.cpp?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/lib/DebugInfo/CodeView/ByteStream.cpp (original)<br>
> > +++ llvm/trunk/lib/DebugInfo/CodeView/ByteStream.cpp Fri Jun 10 00:09:12 2016<br>
> > @@ -15,23 +15,61 @@<br>
> > using namespace llvm;<br>
> > using namespace llvm::codeview;<br>
> ><br>
> > -ByteStream::ByteStream() {}<br>
> > +static Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Src,<br>
> > +                        ArrayRef<uint8_t> Dest) {<br>
> > +  return make_error<CodeViewError>(cv_error_code::operation_unsupported,<br>
> > +                                   "ByteStream is immutable.");<br>
> > +}<br>
> ><br>
> > -ByteStream::ByteStream(ArrayRef<uint8_t> Data) : Data(Data) {}<br>
> > +static Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Src,<br>
> > +                        MutableArrayRef<uint8_t> Dest) {<br>
> > +  if (Dest.size() < Src.size())<br>
> > +    return make_error<CodeViewError>(cv_error_code::insufficient_buffer);<br>
> > +  if (Offset > Src.size() - Dest.size())<br>
> > +    return make_error<CodeViewError>(cv_error_code::insufficient_buffer);<br>
> ><br>
> > -ByteStream::~ByteStream() {}<br>
> > +  ::memcpy(Dest.data() + Offset, Src.data(), Src.size());<br>
> > +  return Error::success();<br>
> > +}<br>
> ><br>
> > -Error ByteStream::readBytes(uint32_t Offset, uint32_t Size,<br>
> > -                            ArrayRef<uint8_t> &Buffer) const {<br>
> > +template <bool Writable><br>
> > +Error ByteStream<Writable>::readBytes(uint32_t Offset, uint32_t Size,<br>
> > +                                      ArrayRef<uint8_t> &Buffer) const {<br>
> > +  if (Offset > Data.size())<br>
> > +    return make_error<CodeViewError>(cv_error_code::insufficient_buffer);<br>
> >   if (Data.size() < Size + Offset)<br>
> >     return make_error<CodeViewError>(cv_error_code::insufficient_buffer);<br>
> >   Buffer = Data.slice(Offset, Size);<br>
> >   return Error::success();<br>
> > }<br>
> ><br>
> > -uint32_t ByteStream::getLength() const { return Data.size(); }<br>
> > +template <bool Writable><br>
> > +Error ByteStream<Writable>::readLongestContiguousChunk(<br>
> > +    uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {<br>
> > +  if (Offset >= Data.size())<br>
> > +    return make_error<CodeViewError>(cv_error_code::insufficient_buffer);<br>
> > +  Buffer = Data.slice(Offset);<br>
> > +  return Error::success();<br>
> > +}<br>
> > +<br>
> > +template <bool Writable><br>
> > +Error ByteStream<Writable>::writeBytes(uint32_t Offset,<br>
> > +                                       ArrayRef<uint8_t> Buffer) const {<br>
> > +  return ::writeBytes(Offset, Buffer, Data);<br>
> > +}<br>
> > +<br>
> > +template <bool Writable> uint32_t ByteStream<Writable>::getLength() const {<br>
> > +  return Data.size();<br>
> > +}<br>
> ><br>
> > -StringRef ByteStream::str() const {<br>
> > +template <bool Writable> StringRef ByteStream<Writable>::str() const {<br>
> >   const char *CharData = reinterpret_cast<const char *>(Data.data());<br>
> >   return StringRef(CharData, Data.size());<br>
> > }<br>
> > +<br>
> > +namespace llvm {<br>
> > +namespace codeview {<br>
> > +template class ByteStream<true>;<br>
> > +template class ByteStream<false>;<br>
> > +}<br>
> > +}<br>
> ><br>
> > Modified: llvm/trunk/lib/DebugInfo/CodeView/CMakeLists.txt<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/CMakeLists.txt?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/CMakeLists.txt?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/lib/DebugInfo/CodeView/CMakeLists.txt (original)<br>
> > +++ llvm/trunk/lib/DebugInfo/CodeView/CMakeLists.txt Fri Jun 10 00:09:12 2016<br>
> > @@ -11,6 +11,7 @@ add_llvm_library(LLVMDebugInfoCodeView<br>
> >   ModuleSubstreamVisitor.cpp<br>
> >   RecordSerialization.cpp<br>
> >   StreamReader.cpp<br>
> > +  StreamWriter.cpp<br>
> >   SymbolDumper.cpp<br>
> >   TypeDumper.cpp<br>
> >   TypeRecord.cpp<br>
> ><br>
> > Modified: llvm/trunk/lib/DebugInfo/CodeView/CodeViewError.cpp<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/CodeViewError.cpp?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/CodeViewError.cpp?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/lib/DebugInfo/CodeView/CodeViewError.cpp (original)<br>
> > +++ llvm/trunk/lib/DebugInfo/CodeView/CodeViewError.cpp Fri Jun 10 00:09:12 2016<br>
> > @@ -31,6 +31,8 @@ public:<br>
> >              "bytes.";<br>
> >     case cv_error_code::corrupt_record:<br>
> >       return "The CodeView record is corrupted.";<br>
> > +    case cv_error_code::operation_unsupported:<br>
> > +      return "The requested operation is not supported.";<br>
> >     }<br>
> >     llvm_unreachable("Unrecognized cv_error_code");<br>
> >   }<br>
> ><br>
> > Modified: llvm/trunk/lib/DebugInfo/CodeView/StreamReader.cpp<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/StreamReader.cpp?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/StreamReader.cpp?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/lib/DebugInfo/CodeView/StreamReader.cpp (original)<br>
> > +++ llvm/trunk/lib/DebugInfo/CodeView/StreamReader.cpp Fri Jun 10 00:09:12 2016<br>
> > @@ -17,6 +17,13 @@ using namespace llvm::codeview;<br>
> ><br>
> > StreamReader::StreamReader(StreamRef S) : Stream(S), Offset(0) {}<br>
> ><br>
> > +Error StreamReader::readLongestContiguousChunk(ArrayRef<uint8_t> &Buffer) {<br>
> > +  if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))<br>
> > +    return EC;<br>
> > +  Offset += Buffer.size();<br>
> > +  return Error::success();<br>
> > +}<br>
> > +<br>
> > Error StreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {<br>
> >   if (auto EC = Stream.readBytes(Offset, Size, Buffer))<br>
> >     return EC;<br>
> ><br>
> > Added: llvm/trunk/lib/DebugInfo/CodeView/StreamWriter.cpp<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/StreamWriter.cpp?rev=272369&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/StreamWriter.cpp?rev=272369&view=auto</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/lib/DebugInfo/CodeView/StreamWriter.cpp (added)<br>
> > +++ llvm/trunk/lib/DebugInfo/CodeView/StreamWriter.cpp Fri Jun 10 00:09:12 2016<br>
> > @@ -0,0 +1,77 @@<br>
> > +//===- StreamWrite.cpp - Writes bytes and objects to a stream -------------===//<br>
> > +//<br>
> > +//                     The LLVM Compiler Infrastructure<br>
> > +//<br>
> > +// This file is distributed under the University of Illinois Open Source<br>
> > +// License. See LICENSE.TXT for details.<br>
> > +//<br>
> > +//===----------------------------------------------------------------------===//<br>
> > +<br>
> > +#include "llvm/DebugInfo/CodeView/StreamWriter.h"<br>
> > +<br>
> > +#include "llvm/DebugInfo/CodeView/CodeViewError.h"<br>
> > +#include "llvm/DebugInfo/CodeView/StreamReader.h"<br>
> > +#include "llvm/DebugInfo/CodeView/StreamRef.h"<br>
> > +<br>
> > +using namespace llvm;<br>
> > +using namespace llvm::codeview;<br>
> > +<br>
> > +StreamWriter::StreamWriter(StreamRef S) : Stream(S), Offset(0) {}<br>
> > +<br>
> > +Error StreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) {<br>
> > +  if (auto EC = Stream.writeBytes(Offset, Buffer))<br>
> > +    return EC;<br>
> > +  Offset += Buffer.size();<br>
> > +  return Error::success();<br>
> > +}<br>
> > +<br>
> > +Error StreamWriter::writeInteger(uint16_t Int) {<br>
> > +  return writeObject(support::ulittle16_t(Int));<br>
> > +}<br>
> > +<br>
> > +Error StreamWriter::writeInteger(uint32_t Int) {<br>
> > +  return writeObject(support::ulittle32_t(Int));<br>
> > +}<br>
> > +<br>
> > +Error StreamWriter::writeZeroString(StringRef Str) {<br>
> > +  if (auto EC = writeFixedString(Str))<br>
> > +    return EC;<br>
> > +  if (auto EC = writeObject('\0'))<br>
> > +    return EC;<br>
> > +<br>
> > +  return Error::success();<br>
> > +}<br>
> > +<br>
> > +Error StreamWriter::writeFixedString(StringRef Str) {<br>
> > +  ArrayRef<uint8_t> Bytes(Str.bytes_begin(), Str.bytes_end());<br>
> > +  if (auto EC = Stream.writeBytes(Offset, Bytes))<br>
> > +    return EC;<br>
> > +<br>
> > +  Offset += Str.size();<br>
> > +  return Error::success();<br>
> > +}<br>
> > +<br>
> > +Error StreamWriter::writeStreamRef(StreamRef Ref) {<br>
> > +  if (auto EC = writeStreamRef(Ref, Ref.getLength()))<br>
> > +    return EC;<br>
> > +  Offset += Ref.getLength();<br>
> > +  return Error::success();<br>
> > +}<br>
> > +<br>
> > +Error StreamWriter::writeStreamRef(StreamRef Ref, uint32_t Length) {<br>
> > +  Ref = Ref.slice(0, Length);<br>
> > +<br>
> > +  StreamReader SrcReader(Ref);<br>
> > +  // This is a bit tricky.  If we just call readBytes, we are requiring that it<br>
> > +  // return us the entire stream as a contiguous buffer.  For large streams this<br>
> > +  // will allocate a huge amount of space from the pool.  Instead, iterate over<br>
> > +  // each contiguous chunk until we've consumed the entire stream.<br>
> > +  while (SrcReader.bytesRemaining() > 0) {<br>
> > +    ArrayRef<uint8_t> Chunk;<br>
> > +    if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))<br>
> > +      return EC;<br>
> > +    if (auto EC = writeBytes(Chunk))<br>
> > +      return EC;<br>
> > +  }<br>
> > +  return Error::success();<br>
> > +}<br>
> ><br>
> > Modified: llvm/trunk/lib/DebugInfo/CodeView/TypeDumper.cpp<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/TypeDumper.cpp?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/TypeDumper.cpp?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/lib/DebugInfo/CodeView/TypeDumper.cpp (original)<br>
> > +++ llvm/trunk/lib/DebugInfo/CodeView/TypeDumper.cpp Fri Jun 10 00:09:12 2016<br>
> > @@ -692,7 +692,7 @@ bool CVTypeDumper::dump(const CVTypeArra<br>
> > }<br>
> ><br>
> > bool CVTypeDumper::dump(ArrayRef<uint8_t> Data) {<br>
> > -  ByteStream Stream(Data);<br>
> > +  ByteStream<> Stream(Data);<br>
> >   CVTypeArray Types;<br>
> >   StreamReader Reader(Stream);<br>
> >   if (auto EC = Reader.readArray(Types, Reader.getLength())) {<br>
> ><br>
> > Modified: llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp (original)<br>
> > +++ llvm/trunk/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp Fri Jun 10 00:09:12 2016<br>
> > @@ -29,6 +29,12 @@ public:<br>
> > };<br>
> > }<br>
> ><br>
> > +typedef std::pair<uint32_t, uint32_t> Interval;<br>
> > +static Interval intersect(const Interval &I1, const Interval &I2) {<br>
> > +  return std::make_pair(std::max(I1.first, I2.first),<br>
> > +                        std::min(I1.second, I2.second));<br>
> > +}<br>
> > +<br>
> > MappedBlockStream::MappedBlockStream(std::unique_ptr<IPDBStreamData> Data,<br>
> >                                      const IPDBFile &Pdb)<br>
> >     : Pdb(Pdb), Data(std::move(Data)) {}<br>
> > @@ -46,26 +52,99 @@ Error MappedBlockStream::readBytes(uint3<br>
> ><br>
> >   auto CacheIter = CacheMap.find(Offset);<br>
> >   if (CacheIter != CacheMap.end()) {<br>
> > -    // In a more general solution, we would need to guarantee that the<br>
> > -    // cached allocation is at least the requested size.  In practice, since<br>
> > -    // these are CodeView / PDB records, we know they are always formatted<br>
> > -    // the same way and never change, so we should never be requesting two<br>
> > -    // allocations from the same address with different sizes.<br>
> > -    Buffer = ArrayRef<uint8_t>(CacheIter->second, Size);<br>
> > +    // Try to find an alloc that was large enough for this request.<br>
> > +    for (auto &Entry : CacheIter->second) {<br>
> > +      if (Entry.size() >= Size) {<br>
> > +        Buffer = Entry.slice(0, Size);<br>
> > +        return Error::success();<br>
> > +      }<br>
> > +    }<br>
> > +  }<br>
> > +<br>
> > +  // We couldn't find a buffer that started at the correct offset (the most<br>
> > +  // common scenario).  Try to see if there is a buffer that starts at some<br>
> > +  // other offset but overlaps the desired range.<br>
> > +  for (auto &CacheItem : CacheMap) {<br>
> > +    Interval RequestExtent = std::make_pair(Offset, Offset + Size);<br>
> > +<br>
> > +    // We already checked this one on the fast path above.<br>
> > +    if (CacheItem.first == Offset)<br>
> > +      continue;<br>
> > +    // If the initial extent of the cached item is beyond the ending extent<br>
> > +    // of the request, there is no overlap.<br>
> > +    if (CacheItem.first >= Offset + Size)<br>
> > +      continue;<br>
> > +<br>
> > +    // We really only have to check the last item in the list, since we append<br>
> > +    // in order of increasing length.<br>
> > +    if (CacheItem.second.empty())<br>
> > +      continue;<br>
> > +<br>
> > +    auto CachedAlloc = CacheItem.second.back();<br>
> > +    // If the initial extent of the request is beyond the ending extent of<br>
> > +    // the cached item, there is no overlap.<br>
> > +    Interval CachedExtent =<br>
> > +        std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());<br>
> > +    if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)<br>
> > +      continue;<br>
> > +<br>
> > +    Interval Intersection = intersect(CachedExtent, RequestExtent);<br>
> > +    // Only use this if the entire request extent is contained in the cached<br>
> > +    // extent.<br>
> > +    if (Intersection != RequestExtent)<br>
> > +      continue;<br>
> > +<br>
> > +    uint32_t CacheRangeOffset =<br>
> > +        AbsoluteDifference(CachedExtent.first, Intersection.first);<br>
> > +    Buffer = CachedAlloc.slice(CacheRangeOffset, Size);<br>
> >     return Error::success();<br>
> >   }<br>
> ><br>
> >   // Otherwise allocate a large enough buffer in the pool, memcpy the data<br>
> > -  // into it, and return an ArrayRef to that.<br>
> > +  // into it, and return an ArrayRef to that.  Do not touch existing pool<br>
> > +  // allocations, as existing clients may be holding a pointer which must<br>
> > +  // not be invalidated.<br>
> >   uint8_t *WriteBuffer = Pool.Allocate<uint8_t>(Size);<br>
> > -<br>
> >   if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))<br>
> >     return EC;<br>
> > -  CacheMap.insert(std::make_pair(Offset, WriteBuffer));<br>
> > +<br>
> > +  if (CacheIter != CacheMap.end()) {<br>
> > +    CacheIter->second.emplace_back(WriteBuffer, Size);<br>
> > +  } else {<br>
> > +    std::vector<CacheEntry> List;<br>
> > +    List.emplace_back(WriteBuffer, Size);<br>
> > +    CacheMap.insert(std::make_pair(Offset, List));<br>
> > +  }<br>
> >   Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);<br>
> >   return Error::success();<br>
> > }<br>
> ><br>
> > +Error MappedBlockStream::readLongestContiguousChunk(<br>
> > +    uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {<br>
> > +  // Make sure we aren't trying to read beyond the end of the stream.<br>
> > +  if (Offset >= Data->getLength())<br>
> > +    return make_error<RawError>(raw_error_code::insufficient_buffer);<br>
> > +  uint32_t First = Offset / Pdb.getBlockSize();<br>
> > +  uint32_t Last = First;<br>
> > +<br>
> > +  auto BlockList = Data->getStreamBlocks();<br>
> > +  while (Last < Pdb.getBlockCount() - 1) {<br>
> > +    if (BlockList[Last] != BlockList[Last + 1] - 1)<br>
> > +      break;<br>
> > +    ++Last;<br>
> > +  }<br>
> > +<br>
> > +  uint32_t OffsetInFirstBlock = Offset % Pdb.getBlockSize();<br>
> > +  uint32_t BytesFromFirstBlock = Pdb.getBlockSize() - OffsetInFirstBlock;<br>
> > +  uint32_t BlockSpan = Last - First + 1;<br>
> > +  uint32_t ByteSpan =<br>
> > +      BytesFromFirstBlock + (BlockSpan - 1) * Pdb.getBlockSize();<br>
> > +  Buffer = Pdb.getBlockData(BlockList[First], Pdb.getBlockSize());<br>
> > +  Buffer = Buffer.drop_front(OffsetInFirstBlock);<br>
> > +  Buffer = ArrayRef<uint8_t>(Buffer.data(), ByteSpan);<br>
> > +  return Error::success();<br>
> > +}<br>
> > +<br>
> > uint32_t MappedBlockStream::getLength() const { return Data->getLength(); }<br>
> ><br>
> > bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,<br>
> > @@ -130,7 +209,73 @@ Error MappedBlockStream::readBytes(uint3<br>
> >   }<br>
> ><br>
> >   return Error::success();<br>
> > +}<br>
> > +<br>
> > +Error MappedBlockStream::writeBytes(uint32_t Offset,<br>
> > +                                    ArrayRef<uint8_t> Buffer) const {<br>
> > +  // Make sure we aren't trying to write beyond the end of the stream.<br>
> > +  if (Buffer.size() > Data->getLength())<br>
> > +    return make_error<RawError>(raw_error_code::insufficient_buffer);<br>
> ><br>
> > +  if (Offset > Data->getLength() - Buffer.size())<br>
> > +    return make_error<RawError>(raw_error_code::insufficient_buffer);<br>
> > +<br>
> > +  uint32_t BlockNum = Offset / Pdb.getBlockSize();<br>
> > +  uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();<br>
> > +<br>
> > +  uint32_t BytesLeft = Buffer.size();<br>
> > +  auto BlockList = Data->getStreamBlocks();<br>
> > +  uint32_t BytesWritten = 0;<br>
> > +  while (BytesLeft > 0) {<br>
> > +    uint32_t StreamBlockAddr = BlockList[BlockNum];<br>
> > +    uint32_t BytesToWriteInChunk =<br>
> > +        std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock);<br>
> > +<br>
> > +    const uint8_t *Chunk = Buffer.data() + BytesWritten;<br>
> > +    ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk);<br>
> > +    if (auto EC = Pdb.setBlockData(StreamBlockAddr, OffsetInBlock, ChunkData))<br>
> > +      return EC;<br>
> > +<br>
> > +    BytesLeft -= BytesToWriteInChunk;<br>
> > +    BytesWritten += BytesToWriteInChunk;<br>
> > +    ++BlockNum;<br>
> > +    OffsetInBlock = 0;<br>
> > +  }<br>
> > +<br>
> > +  // If this write overlapped a read which previously came from the pool,<br>
> > +  // someone may still be holding a pointer to that alloc which is now invalid.<br>
> > +  // Compute the overlapping range and update the cache entry, so any<br>
> > +  // outstanding buffers are automatically updated.<br>
> > +  for (const auto &MapEntry : CacheMap) {<br>
> > +    // If the end of the written extent precedes the beginning of the cached<br>
> > +    // extent, ignore this map entry.<br>
> > +    if (Offset + BytesWritten < MapEntry.first)<br>
> > +      continue;<br>
> > +    for (const auto &Alloc : MapEntry.second) {<br>
> > +      // If the end of the cached extent precedes the beginning of the written<br>
> > +      // extent, ignore this alloc.<br>
> > +      if (MapEntry.first + Alloc.size() < Offset)<br>
> > +        continue;<br>
> > +<br>
> > +      // If we get here, they are guaranteed to overlap.<br>
> > +      Interval WriteInterval = std::make_pair(Offset, Offset + BytesWritten);<br>
> > +      Interval CachedInterval =<br>
> > +          std::make_pair(MapEntry.first, MapEntry.first + Alloc.size());<br>
> > +      // If they overlap, we need to write the new data into the overlapping<br>
> > +      // range.<br>
> > +      auto Intersection = intersect(WriteInterval, CachedInterval);<br>
> > +      assert(Intersection.first <= Intersection.second);<br>
> > +<br>
> > +      uint32_t Length = Intersection.second - Intersection.first;<br>
> > +      uint32_t SrcOffset =<br>
> > +          AbsoluteDifference(WriteInterval.first, Intersection.first);<br>
> > +      uint32_t DestOffset =<br>
> > +          AbsoluteDifference(CachedInterval.first, Intersection.first);<br>
> > +      ::memcpy(Alloc.data() + DestOffset, Buffer.data() + SrcOffset, Length);<br>
> > +    }<br>
> > +  }<br>
> > +<br>
> > +  return Error::success();<br>
> > }<br>
> ><br>
> > uint32_t MappedBlockStream::getNumBytesCopied() const {<br>
> ><br>
> > Modified: llvm/trunk/lib/DebugInfo/PDB/Raw/PDBFile.cpp<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/PDBFile.cpp?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/PDBFile.cpp?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/lib/DebugInfo/PDB/Raw/PDBFile.cpp (original)<br>
> > +++ llvm/trunk/lib/DebugInfo/PDB/Raw/PDBFile.cpp Fri Jun 10 00:09:12 2016<br>
> > @@ -136,6 +136,17 @@ ArrayRef<uint8_t> PDBFile::getBlockData(<br>
> >       NumBytes);<br>
> > }<br>
> ><br>
> > +Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,<br>
> > +                            ArrayRef<uint8_t> Data) const {<br>
> > +  if (BlockIndex >= getBlockCount())<br>
> > +    return make_error<RawError>(raw_error_code::invalid_block_address);<br>
> > +<br>
> > +  if (Offset > getBlockSize() - Data.size())<br>
> > +    return make_error<RawError>(raw_error_code::insufficient_buffer);<br>
> > +<br>
> > +  return make_error<RawError>(raw_error_code::not_writable);<br>
> > +}<br>
> > +<br>
> > Error PDBFile::parseFileHeaders() {<br>
> >   std::error_code EC;<br>
> >   MemoryBufferRef BufferRef = *Context->Buffer;<br>
> ><br>
> > Modified: llvm/trunk/lib/DebugInfo/PDB/Raw/RawError.cpp<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/RawError.cpp?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/RawError.cpp?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/lib/DebugInfo/PDB/Raw/RawError.cpp (original)<br>
> > +++ llvm/trunk/lib/DebugInfo/PDB/Raw/RawError.cpp Fri Jun 10 00:09:12 2016<br>
> > @@ -28,6 +28,10 @@ public:<br>
> >       return "The specified stream could not be loaded.";<br>
> >     case raw_error_code::index_out_of_bounds:<br>
> >       return "The specified item does not exist in the array.";<br>
> > +    case raw_error_code::invalid_block_address:<br>
> > +      return "The specified block address is not valid.";<br>
> > +    case raw_error_code::not_writable:<br>
> > +      return "The PDB does not support writing.";<br>
> >     }<br>
> >     llvm_unreachable("Unrecognized raw_error_code");<br>
> >   }<br>
> ><br>
> > Modified: llvm/trunk/tools/llvm-readobj/COFFDumper.cpp<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/COFFDumper.cpp?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/COFFDumper.cpp?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/tools/llvm-readobj/COFFDumper.cpp (original)<br>
> > +++ llvm/trunk/tools/llvm-readobj/COFFDumper.cpp Fri Jun 10 00:09:12 2016<br>
> > @@ -969,7 +969,7 @@ void COFFDumper::printCodeViewSymbolsSub<br>
> >                                                         SectionContents);<br>
> ><br>
> >   CVSymbolDumper CVSD(W, CVTD, std::move(CODD), opts::CodeViewSubsectionBytes);<br>
> > -  ByteStream Stream(BinaryData);<br>
> > +  ByteStream<> Stream(BinaryData);<br>
> >   CVSymbolArray Symbols;<br>
> >   StreamReader Reader(Stream);<br>
> >   if (auto EC = Reader.readArray(Symbols, Reader.getLength())) {<br>
> > @@ -1077,7 +1077,7 @@ void COFFDumper::mergeCodeViewTypes(Memo<br>
> >         error(object_error::parse_failed);<br>
> >       ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(Data.data()),<br>
> >                               Data.size());<br>
> > -      ByteStream Stream(Bytes);<br>
> > +      ByteStream<> Stream(Bytes);<br>
> >       CVTypeArray Types;<br>
> >       StreamReader Reader(Stream);<br>
> >       if (auto EC = Reader.readArray(Types, Reader.getLength())) {<br>
> ><br>
> > Modified: llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp<br>
> > URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp?rev=272369&r1=272368&r2=272369&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp?rev=272369&r1=272368&r2=272369&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp (original)<br>
> > +++ llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp Fri Jun 10 00:09:12 2016<br>
> > @@ -9,8 +9,10 @@<br>
> ><br>
> > #include <unordered_map><br>
> ><br>
> > +#include "llvm/DebugInfo/CodeView/ByteStream.h"<br>
> > #include "llvm/DebugInfo/CodeView/StreamReader.h"<br>
> > #include "llvm/DebugInfo/CodeView/StreamRef.h"<br>
> > +#include "llvm/DebugInfo/CodeView/StreamWriter.h"<br>
> > #include "llvm/DebugInfo/PDB/Raw/IPDBFile.h"<br>
> > #include "llvm/DebugInfo/PDB/Raw/IPDBStreamData.h"<br>
> > #include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"<br>
> > @@ -40,35 +42,43 @@ namespace {<br>
> >   }<br>
> ><br>
> > static const uint32_t BlocksAry[] = {0, 1, 2, 5, 4, 3, 6, 7, 8, 9};<br>
> > -static const char DataAry[] = {'A', 'B', 'C', 'F', 'E',<br>
> > -                               'D', 'G', 'H', 'I', 'J'};<br>
> > +static uint8_t DataAry[] = {'A', 'B', 'C', 'F', 'E', 'D', 'G', 'H', 'I', 'J'};<br>
> ><br>
> > class DiscontiguousFile : public IPDBFile {<br>
> > public:<br>
> > -  DiscontiguousFile()<br>
> > -      : Blocks(std::begin(BlocksAry), std::end(BlocksAry)),<br>
> > -        Data(std::begin(DataAry), std::end(DataAry)) {}<br>
> > -<br>
> > -  virtual uint32_t getBlockSize() const override { return 1; }<br>
> > -  virtual uint32_t getBlockCount() const override { return 10; }<br>
> > -  virtual uint32_t getNumStreams() const override { return 1; }<br>
> > -  virtual uint32_t getStreamByteSize(uint32_t StreamIndex) const override {<br>
> > +  DiscontiguousFile(ArrayRef<uint32_t> Blocks, MutableArrayRef<uint8_t> Data)<br>
> > +      : Blocks(Blocks.begin(), Blocks.end()), Data(Data.begin(), Data.end()) {}<br>
> > +<br>
> > +  uint32_t getBlockSize() const override { return 1; }<br>
> > +  uint32_t getBlockCount() const override { return Blocks.size(); }<br>
> > +  uint32_t getNumStreams() const override { return 1; }<br>
> > +  uint32_t getStreamByteSize(uint32_t StreamIndex) const override {<br>
> >     return getBlockCount() * getBlockSize();<br>
> >   }<br>
> > -  virtual ArrayRef<support::ulittle32_t><br>
> > +  ArrayRef<support::ulittle32_t><br>
> >   getStreamBlockList(uint32_t StreamIndex) const override {<br>
> >     if (StreamIndex != 0)<br>
> >       return ArrayRef<support::ulittle32_t>();<br>
> >     return Blocks;<br>
> >   }<br>
> > -  virtual ArrayRef<uint8_t> getBlockData(uint32_t BlockIndex,<br>
> > -                                         uint32_t NumBytes) const override {<br>
> > +  ArrayRef<uint8_t> getBlockData(uint32_t BlockIndex,<br>
> > +                                 uint32_t NumBytes) const override {<br>
> >     return ArrayRef<uint8_t>(&Data[BlockIndex], NumBytes);<br>
> >   }<br>
> ><br>
> > +  Error setBlockData(uint32_t BlockIndex, uint32_t Offset,<br>
> > +                     ArrayRef<uint8_t> SrcData) const override {<br>
> > +    if (BlockIndex >= Blocks.size())<br>
> > +      return make_error<CodeViewError>(cv_error_code::insufficient_buffer);<br>
> > +    if (Offset > getBlockSize() - SrcData.size())<br>
> > +      return make_error<CodeViewError>(cv_error_code::insufficient_buffer);<br>
> > +    ::memcpy(&Data[BlockIndex] + Offset, SrcData.data(), SrcData.size());<br>
> > +    return Error::success();<br>
> > +  }<br>
> > +<br>
> > private:<br>
> >   std::vector<support::ulittle32_t> Blocks;<br>
> > -  std::vector<uint8_t> Data;<br>
> > +  MutableArrayRef<uint8_t> Data;<br>
> > };<br>
> ><br>
> > class MappedBlockStreamImpl : public MappedBlockStream {<br>
> > @@ -81,7 +91,7 @@ public:<br>
> > // Tests that a read which is entirely contained within a single block works<br>
> > // and does not allocate.<br>
> > TEST(MappedBlockStreamTest, ReadBeyondEndOfStreamRef) {<br>
> > -  DiscontiguousFile F;<br>
> > +  DiscontiguousFile F(BlocksAry, DataAry);<br>
> >   MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> >   StreamReader R(S);<br>
> >   StreamRef SR;<br>
> > @@ -95,7 +105,7 @@ TEST(MappedBlockStreamTest, ReadBeyondEn<br>
> > // Tests that a read which outputs into a full destination buffer works and<br>
> > // does not fail due to the length of the output buffer.<br>
> > TEST(MappedBlockStreamTest, ReadOntoNonEmptyBuffer) {<br>
> > -  DiscontiguousFile F;<br>
> > +  DiscontiguousFile F(BlocksAry, DataAry);<br>
> >   MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> >   StreamReader R(S);<br>
> >   StringRef Str = "ZYXWVUTSRQPONMLKJIHGFEDCBA";<br>
> > @@ -108,7 +118,7 @@ TEST(MappedBlockStreamTest, ReadOntoNonE<br>
> > // blocks are still contiguous in memory to the previous block works and does<br>
> > // not allocate memory.<br>
> > TEST(MappedBlockStreamTest, ZeroCopyReadContiguousBreak) {<br>
> > -  DiscontiguousFile F;<br>
> > +  DiscontiguousFile F(BlocksAry, DataAry);<br>
> >   MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> >   StreamReader R(S);<br>
> >   StringRef Str;<br>
> > @@ -126,7 +136,7 @@ TEST(MappedBlockStreamTest, ZeroCopyRead<br>
> > // contiguously works and allocates only the precise amount of bytes<br>
> > // requested.<br>
> > TEST(MappedBlockStreamTest, CopyReadNonContiguousBreak) {<br>
> > -  DiscontiguousFile F;<br>
> > +  DiscontiguousFile F(BlocksAry, DataAry);<br>
> >   MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> >   StreamReader R(S);<br>
> >   StringRef Str;<br>
> > @@ -138,7 +148,7 @@ TEST(MappedBlockStreamTest, CopyReadNonC<br>
> > // Test that an out of bounds read which doesn't cross a block boundary<br>
> > // fails and allocates no memory.<br>
> > TEST(MappedBlockStreamTest, InvalidReadSizeNoBreak) {<br>
> > -  DiscontiguousFile F;<br>
> > +  DiscontiguousFile F(BlocksAry, DataAry);<br>
> >   MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> >   StreamReader R(S);<br>
> >   StringRef Str;<br>
> > @@ -151,7 +161,7 @@ TEST(MappedBlockStreamTest, InvalidReadS<br>
> > // Test that an out of bounds read which crosses a contiguous block boundary<br>
> > // fails and allocates no memory.<br>
> > TEST(MappedBlockStreamTest, InvalidReadSizeContiguousBreak) {<br>
> > -  DiscontiguousFile F;<br>
> > +  DiscontiguousFile F(BlocksAry, DataAry);<br>
> >   MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> >   StreamReader R(S);<br>
> >   StringRef Str;<br>
> > @@ -164,7 +174,7 @@ TEST(MappedBlockStreamTest, InvalidReadS<br>
> > // Test that an out of bounds read which crosses a discontiguous block<br>
> > // boundary fails and allocates no memory.<br>
> > TEST(MappedBlockStreamTest, InvalidReadSizeNonContiguousBreak) {<br>
> > -  DiscontiguousFile F;<br>
> > +  DiscontiguousFile F(BlocksAry, DataAry);<br>
> >   MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> >   StreamReader R(S);<br>
> >   StringRef Str;<br>
> > @@ -176,7 +186,7 @@ TEST(MappedBlockStreamTest, InvalidReadS<br>
> > // Tests that a read which is entirely contained within a single block but<br>
> > // beyond the end of a StreamRef fails.<br>
> > TEST(MappedBlockStreamTest, ZeroCopyReadNoBreak) {<br>
> > -  DiscontiguousFile F;<br>
> > +  DiscontiguousFile F(BlocksAry, DataAry);<br>
> >   MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> >   StreamReader R(S);<br>
> >   StringRef Str;<br>
> > @@ -185,4 +195,257 @@ TEST(MappedBlockStreamTest, ZeroCopyRead<br>
> >   EXPECT_EQ(0U, S.getNumBytesCopied());<br>
> > }<br>
> ><br>
> > +// Tests that a read which is not aligned on the same boundary as a previous<br>
> > +// cached request, but which is known to overlap that request, shares the<br>
> > +// previous allocation.<br>
> > +TEST(MappedBlockStreamTest, UnalignedOverlappingRead) {<br>
> > +  DiscontiguousFile F(BlocksAry, DataAry);<br>
> > +  MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> > +  StreamReader R(S);<br>
> > +  StringRef Str1;<br>
> > +  StringRef Str2;<br>
> > +  EXPECT_NO_ERROR(R.readFixedString(Str1, 7));<br>
> > +  EXPECT_EQ(Str1, StringRef("ABCDEFG"));<br>
> > +  EXPECT_EQ(7U, S.getNumBytesCopied());<br>
> > +<br>
> > +  R.setOffset(2);<br>
> > +  EXPECT_NO_ERROR(R.readFixedString(Str2, 3));<br>
> > +  EXPECT_EQ(Str2, StringRef("CDE"));<br>
> > +  EXPECT_EQ(Str1.data() + 2, Str2.data());<br>
> > +  EXPECT_EQ(7U, S.getNumBytesCopied());<br>
> > +}<br>
> > +<br>
> > +// Tests that a read which is not aligned on the same boundary as a previous<br>
> > +// cached request, but which only partially overlaps a previous cached request,<br>
> > +// still works correctly and allocates again from the shared pool.<br>
> > +TEST(MappedBlockStreamTest, UnalignedOverlappingReadFail) {<br>
> > +  DiscontiguousFile F(BlocksAry, DataAry);<br>
> > +  MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> > +  StreamReader R(S);<br>
> > +  StringRef Str1;<br>
> > +  StringRef Str2;<br>
> > +  EXPECT_NO_ERROR(R.readFixedString(Str1, 6));<br>
> > +  EXPECT_EQ(Str1, StringRef("ABCDEF"));<br>
> > +  EXPECT_EQ(6U, S.getNumBytesCopied());<br>
> > +<br>
> > +  R.setOffset(4);<br>
> > +  EXPECT_NO_ERROR(R.readFixedString(Str2, 4));<br>
> > +  EXPECT_EQ(Str2, StringRef("EFGH"));<br>
> > +  EXPECT_EQ(10U, S.getNumBytesCopied());<br>
> > +}<br>
> > +<br>
> > +TEST(MappedBlockStreamTest, WriteBeyondEndOfStream) {<br>
> > +  static uint8_t Data[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};<br>
> > +  static uint8_t LargeBuffer[] = {'0', '1', '2', '3', '4', '5',<br>
> > +                                  '6', '7', '8', '9', 'A'};<br>
> > +  static uint8_t SmallBuffer[] = {'0', '1', '2'};<br>
> > +  static_assert(sizeof(LargeBuffer) > sizeof(Data),<br>
> > +                "LargeBuffer is not big enough");<br>
> > +<br>
> > +  DiscontiguousFile F(BlocksAry, Data);<br>
> > +  MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> > +  ArrayRef<uint8_t> Buffer;<br>
> > +<br>
> > +  EXPECT_ERROR(S.writeBytes(0, ArrayRef<uint8_t>(LargeBuffer)));<br>
> > +  EXPECT_NO_ERROR(S.writeBytes(0, ArrayRef<uint8_t>(SmallBuffer)));<br>
> > +  EXPECT_NO_ERROR(S.writeBytes(7, ArrayRef<uint8_t>(SmallBuffer)));<br>
> > +  EXPECT_ERROR(S.writeBytes(8, ArrayRef<uint8_t>(SmallBuffer)));<br>
> > +}<br>
> > +<br>
> > +TEST(MappedBlockStreamTest, TestWriteBytesNoBreakBoundary) {<br>
> > +  static uint8_t Data[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};<br>
> > +  DiscontiguousFile F(BlocksAry, Data);<br>
> > +  MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> > +  ArrayRef<uint8_t> Buffer;<br>
> > +<br>
> > +  EXPECT_NO_ERROR(S.readBytes(0, 1, Buffer));<br>
> > +  EXPECT_EQ(Buffer, ArrayRef<uint8_t>('A'));<br>
> > +  EXPECT_NO_ERROR(S.readBytes(9, 1, Buffer));<br>
> > +  EXPECT_EQ(Buffer, ArrayRef<uint8_t>('J'));<br>
> > +<br>
> > +  EXPECT_NO_ERROR(S.writeBytes(0, ArrayRef<uint8_t>('J')));<br>
> > +  EXPECT_NO_ERROR(S.writeBytes(9, ArrayRef<uint8_t>('A')));<br>
> > +<br>
> > +  EXPECT_NO_ERROR(S.readBytes(0, 1, Buffer));<br>
> > +  EXPECT_EQ(Buffer, ArrayRef<uint8_t>('J'));<br>
> > +  EXPECT_NO_ERROR(S.readBytes(9, 1, Buffer));<br>
> > +  EXPECT_EQ(Buffer, ArrayRef<uint8_t>('A'));<br>
> > +<br>
> > +  EXPECT_NO_ERROR(S.writeBytes(0, ArrayRef<uint8_t>('A')));<br>
> > +  EXPECT_NO_ERROR(S.writeBytes(9, ArrayRef<uint8_t>('J')));<br>
> > +<br>
> > +  EXPECT_NO_ERROR(S.readBytes(0, 1, Buffer));<br>
> > +  EXPECT_EQ(Buffer, ArrayRef<uint8_t>('A'));<br>
> > +  EXPECT_NO_ERROR(S.readBytes(9, 1, Buffer));<br>
> > +  EXPECT_EQ(Buffer, ArrayRef<uint8_t>('J'));<br>
> > +}<br>
> > +<br>
> > +TEST(MappedBlockStreamTest, TestWriteBytesBreakBoundary) {<br>
> > +  static uint8_t Data[] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};<br>
> > +  static uint8_t TestData[] = {'T', 'E', 'S', 'T', 'I', 'N', 'G', '.'};<br>
> > +  static uint8_t Expected[] = {'T', 'E', 'S', 'N', 'I',<br>
> > +                               'T', 'G', '.', '0', '0'};<br>
> > +<br>
> > +  DiscontiguousFile F(BlocksAry, Data);<br>
> > +  MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> > +  ArrayRef<uint8_t> Buffer;<br>
> > +<br>
> > +  EXPECT_NO_ERROR(S.writeBytes(0, TestData));<br>
> > +  // First just compare the memory, then compare the result of reading the<br>
> > +  // string out.<br>
> > +  EXPECT_EQ(ArrayRef<uint8_t>(Data), ArrayRef<uint8_t>(Expected));<br>
> > +<br>
> > +  EXPECT_NO_ERROR(S.readBytes(0, 8, Buffer));<br>
> > +  EXPECT_EQ(Buffer, ArrayRef<uint8_t>(TestData));<br>
> > +}<br>
> > +<br>
> > +TEST(MappedBlockStreamTest, TestWriteThenRead) {<br>
> > +  std::vector<uint8_t> DataBytes(10);<br>
> > +  MutableArrayRef<uint8_t> Data(DataBytes);<br>
> > +  const uint32_t Blocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};<br>
> > +<br>
> > +  DiscontiguousFile F(Blocks, Data);<br>
> > +  MappedBlockStreamImpl S(llvm::make_unique<IndexedStreamData>(0, F), F);<br>
> > +<br>
> > +  enum class MyEnum : uint32_t { Val1 = 2908234, Val2 = 120891234 };<br>
> > +<br>
> > +  uint16_t u16[] = {31468, 0};<br>
> > +  uint32_t u32[] = {890723408, 0};<br>
> > +  MyEnum Enum[] = {MyEnum::Val1, MyEnum::Val2};<br>
> > +  StringRef ZStr[] = {"Zero Str", ""};<br>
> > +  StringRef FStr[] = {"Fixed Str", ""};<br>
> > +  ArrayRef<uint8_t> byteArray[] = {{'1', '2'}, {'0', '0'}};<br>
> > +  ArrayRef<uint32_t> intArray[] = {{890723408, 29082234}, {0, 0}};<br>
> > +<br>
> > +  StreamReader Reader(S);<br>
> > +  StreamWriter Writer(S);<br>
> > +  EXPECT_NO_ERROR(Writer.writeInteger(u16[0]));<br>
> > +  EXPECT_NO_ERROR(Reader.readInteger(u16[1]));<br>
> > +  EXPECT_EQ(u16[0], u16[1]);<br>
> > +  EXPECT_EQ(DataBytes,<br>
> > +            std::vector<uint8_t>({0, 0x7A, 0xEC, 0, 0, 0, 0, 0, 0, 0}));<br>
> > +<br>
> > +  Reader.setOffset(0);<br>
> > +  Writer.setOffset(0);<br>
> > +  ::memset(DataBytes.data(), 0, 10);<br>
> > +  EXPECT_NO_ERROR(Writer.writeInteger(u32[0]));<br>
> > +  EXPECT_NO_ERROR(Reader.readInteger(u32[1]));<br>
> > +  EXPECT_EQ(u32[0], u32[1]);<br>
> > +  EXPECT_EQ(DataBytes,<br>
> > +            std::vector<uint8_t>({0x17, 0x5C, 0x50, 0, 0, 0, 0x35, 0, 0, 0}));<br>
> > +<br>
> > +  Reader.setOffset(0);<br>
> > +  Writer.setOffset(0);<br>
> > +  ::memset(DataBytes.data(), 0, 10);<br>
> > +  EXPECT_NO_ERROR(Writer.writeEnum(Enum[0]));<br>
> > +  EXPECT_NO_ERROR(Reader.readEnum(Enum[1]));<br>
> > +  EXPECT_EQ(Enum[0], Enum[1]);<br>
> > +  EXPECT_EQ(DataBytes,<br>
> > +            std::vector<uint8_t>({0x2C, 0x60, 0x4A, 0, 0, 0, 0, 0, 0, 0}));<br>
> > +<br>
> > +  Reader.setOffset(0);<br>
> > +  Writer.setOffset(0);<br>
> > +  ::memset(DataBytes.data(), 0, 10);<br>
> > +  EXPECT_NO_ERROR(Writer.writeZeroString(ZStr[0]));<br>
> > +  EXPECT_NO_ERROR(Reader.readZeroString(ZStr[1]));<br>
> > +  EXPECT_EQ(ZStr[0], ZStr[1]);<br>
> > +  EXPECT_EQ(DataBytes, std::vector<uint8_t>(<br>
> > +                           {'r', 'e', 'Z', ' ', 'S', 't', 'o', 'r', 0, 0}));<br>
> > +<br>
> > +  Reader.setOffset(0);<br>
> > +  Writer.setOffset(0);<br>
> > +  ::memset(DataBytes.data(), 0, 10);<br>
> > +  EXPECT_NO_ERROR(Writer.writeFixedString(FStr[0]));<br>
> > +  EXPECT_NO_ERROR(Reader.readFixedString(FStr[1], FStr[0].size()));<br>
> > +  EXPECT_EQ(FStr[0], FStr[1]);<br>
> > +  EXPECT_EQ(DataBytes, std::vector<uint8_t>(<br>
> > +                           {'x', 'i', 'F', 'd', ' ', 'S', 'e', 't', 0, 'r'}));<br>
> > +<br>
> > +  Reader.setOffset(0);<br>
> > +  Writer.setOffset(0);<br>
> > +  ::memset(DataBytes.data(), 0, 10);<br>
> > +  EXPECT_NO_ERROR(Writer.writeArray(byteArray[0]));<br>
> > +  EXPECT_NO_ERROR(Reader.readArray(byteArray[1], byteArray[0].size()));<br>
> > +  EXPECT_EQ(byteArray[0], byteArray[1]);<br>
> > +  EXPECT_EQ(DataBytes,<br>
> > +            std::vector<uint8_t>({0, 0x32, 0x31, 0, 0, 0, 0, 0, 0, 0}));<br>
> > +<br>
> > +  Reader.setOffset(0);<br>
> > +  Writer.setOffset(0);<br>
> > +  ::memset(DataBytes.data(), 0, 10);<br>
> > +  EXPECT_NO_ERROR(Writer.writeArray(intArray[0]));<br>
> > +  EXPECT_NO_ERROR(Reader.readArray(intArray[1], intArray[0].size()));<br>
> > +  EXPECT_EQ(intArray[0], intArray[1]);<br>
> > +  EXPECT_EQ(DataBytes, std::vector<uint8_t>({0x17, 0x5C, 0x50, 0x7A, 0xC2, 0xBB,<br>
> > +                                             0x35, 0x01, 0, 0}));<br>
> > +}<br>
> > +<br>
> > +TEST(MappedBlockStreamTest, TestWriteContiguousStreamRef) {<br>
> > +  std::vector<uint8_t> DestDataBytes(10);<br>
> > +  MutableArrayRef<uint8_t> DestData(DestDataBytes);<br>
> > +  const uint32_t DestBlocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};<br>
> > +<br>
> > +  std::vector<uint8_t> SrcDataBytes(10);<br>
> > +  MutableArrayRef<uint8_t> SrcData(SrcDataBytes);<br>
> > +<br>
> > +  DiscontiguousFile F(DestBlocks, DestData);<br>
> > +  MappedBlockStreamImpl DestStream(llvm::make_unique<IndexedStreamData>(0, F),<br>
> > +                                   F);<br>
> > +<br>
> > +  // First write "Test Str" into the source stream.<br>
> > +  ByteStream<true> SourceStream(SrcData);<br>
> > +  StreamWriter SourceWriter(SourceStream);<br>
> > +  EXPECT_NO_ERROR(SourceWriter.writeZeroString("Test Str"));<br>
> > +  EXPECT_EQ(SrcDataBytes, std::vector<uint8_t>(<br>
> > +                              {'T', 'e', 's', 't', ' ', 'S', 't', 'r', 0, 0}));<br>
> > +<br>
> > +  // Then write the source stream into the dest stream.<br>
> > +  StreamWriter DestWriter(DestStream);<br>
> > +  EXPECT_NO_ERROR(DestWriter.writeStreamRef(SourceStream));<br>
> > +  EXPECT_EQ(DestDataBytes, std::vector<uint8_t>(<br>
> > +                               {'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0}));<br>
> > +<br>
> > +  // Then read the string back out of the dest stream.<br>
> > +  StringRef Result;<br>
> > +  StreamReader DestReader(DestStream);<br>
> > +  EXPECT_NO_ERROR(DestReader.readZeroString(Result));<br>
> > +  EXPECT_EQ(Result, "Test Str");<br>
> > +}<br>
> > +<br>
> > +TEST(MappedBlockStreamTest, TestWriteDiscontiguousStreamRef) {<br>
> > +  std::vector<uint8_t> DestDataBytes(10);<br>
> > +  MutableArrayRef<uint8_t> DestData(DestDataBytes);<br>
> > +  const uint32_t DestBlocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};<br>
> > +<br>
> > +  std::vector<uint8_t> SrcDataBytes(10);<br>
> > +  MutableArrayRef<uint8_t> SrcData(SrcDataBytes);<br>
> > +  const uint32_t SrcBlocks[] = {1, 0, 6, 3, 4, 5, 2, 7, 8, 9};<br>
> > +<br>
> > +  DiscontiguousFile DestFile(DestBlocks, DestData);<br>
> > +  DiscontiguousFile SrcFile(SrcBlocks, SrcData);<br>
> > +<br>
> > +  MappedBlockStreamImpl DestStream(<br>
> > +      llvm::make_unique<IndexedStreamData>(0, DestFile), DestFile);<br>
> > +  MappedBlockStreamImpl SrcStream(<br>
> > +      llvm::make_unique<IndexedStreamData>(0, SrcFile), SrcFile);<br>
> > +<br>
> > +  // First write "Test Str" into the source stream.<br>
> > +  StreamWriter SourceWriter(SrcStream);<br>
> > +  EXPECT_NO_ERROR(SourceWriter.writeZeroString("Test Str"));<br>
> > +  EXPECT_EQ(SrcDataBytes, std::vector<uint8_t>(<br>
> > +                              {'e', 'T', 't', 't', ' ', 'S', 's', 'r', 0, 0}));<br>
> > +<br>
> > +  // Then write the source stream into the dest stream.<br>
> > +  StreamWriter DestWriter(DestStream);<br>
> > +  EXPECT_NO_ERROR(DestWriter.writeStreamRef(SrcStream));<br>
> > +  EXPECT_EQ(DestDataBytes, std::vector<uint8_t>(<br>
> > +                               {'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0}));<br>
> > +<br>
> > +  // Then read the string back out of the dest stream.<br>
> > +  StringRef Result;<br>
> > +  StreamReader DestReader(DestStream);<br>
> > +  EXPECT_NO_ERROR(DestReader.readZeroString(Result));<br>
> > +  EXPECT_EQ(Result, "Test Str");<br>
> > +}<br>
> > +<br>
> > } // end anonymous namespace<br>
> ><br>
> ><br>
> > _______________________________________________<br>
> > llvm-commits mailing list<br>
> > <a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
> > <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
><br>
<br>
</blockquote></div></blockquote></div>