[PATCH] D11391: Fix testing for end of stream in bitstream reader.

Derek Schuff dschuff at google.com
Mon Aug 3 11:02:33 PDT 2015


This revision was automatically updated to reflect the committed changes.
Closed by commit rL243890: Fix testing for end of stream in bitstream reader. (authored by dschuff).

Changed prior to commit:
  http://reviews.llvm.org/D11391?vs=31041&id=31246#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D11391

Files:
  llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
  llvm/trunk/include/llvm/Support/StreamingMemoryObject.h
  llvm/trunk/test/Bitcode/Inputs/invalid-abbrev.bc
  llvm/trunk/unittests/Bitcode/BitReaderTest.cpp

Index: llvm/trunk/unittests/Bitcode/BitReaderTest.cpp
===================================================================
--- llvm/trunk/unittests/Bitcode/BitReaderTest.cpp
+++ llvm/trunk/unittests/Bitcode/BitReaderTest.cpp
@@ -10,6 +10,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/AsmParser/Parser.h"
+#include "llvm/Bitcode/BitstreamReader.h"
 #include "llvm/Bitcode/BitstreamWriter.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/IR/Constants.h"
@@ -21,6 +22,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/StreamingMemoryObject.h"
 #include "gtest/gtest.h"
 
 using namespace llvm;
@@ -89,6 +91,39 @@
   return std::move(ModuleOrErr.get());
 }
 
+// Checks if we correctly detect eof if we try to read N bits when there are not
+// enough bits left on the input stream to read N bits, and we are using a data
+// streamer. In particular, it checks if we properly set the object size when
+// the eof is reached under such conditions.
+TEST(BitReaderTest, TestForEofAfterReadFailureOnDataStreamer) {
+  // Note: Because StreamingMemoryObject does a call to method GetBytes in it's
+  // constructor, using internal constant kChunkSize, we must fill the input
+  // with more characters than that amount.
+  static size_t InputSize = StreamingMemoryObject::kChunkSize + 5;
+  char *Text = new char[InputSize];
+  std::memset(Text, 'a', InputSize);
+  Text[InputSize - 1] = '\0';
+  StringRef Input(Text);
+
+  // Build bitsteam reader using data streamer.
+  auto MemoryBuf = MemoryBuffer::getMemBuffer(Input);
+  std::unique_ptr<DataStreamer> Streamer(
+      new BufferDataStreamer(std::move(MemoryBuf)));
+  auto OwnedBytes =
+      llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
+  auto Reader = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
+  BitstreamCursor Cursor;
+  Cursor.init(Reader.get());
+
+  // Jump to two bytes before end of stream.
+  Cursor.JumpToBit((InputSize - 4) * CHAR_BIT);
+  // Try to read 4 bytes when only 2 are present, resulting in error value 0.
+  constexpr size_t ReadErrorValue = 0;
+  EXPECT_EQ(ReadErrorValue, Cursor.Read(32));
+  // Should be at eof now.
+  EXPECT_TRUE(Cursor.AtEndOfStream());
+}
+
 TEST(BitReaderTest, MateralizeForwardRefWithStream) {
   SmallString<1024> Mem;
 
Index: llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
===================================================================
--- llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
+++ llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
@@ -325,6 +325,8 @@
 
     // If we run out of data, stop at the end of the stream.
     if (BytesRead == 0) {
+      CurWord = 0;
+      BitsInCurWord = 0;
       Size = NextChar;
       return;
     }
Index: llvm/trunk/include/llvm/Support/StreamingMemoryObject.h
===================================================================
--- llvm/trunk/include/llvm/Support/StreamingMemoryObject.h
+++ llvm/trunk/include/llvm/Support/StreamingMemoryObject.h
@@ -50,8 +50,10 @@
   /// starts (although it can be called anytime).
   void setKnownObjectSize(size_t size);
 
+  /// The number of bytes read at a time from the data streamer.
+  static const uint32_t kChunkSize = 4096 * 4;
+
 private:
-  const static uint32_t kChunkSize = 4096 * 4;
   mutable std::vector<unsigned char> Bytes;
   std::unique_ptr<DataStreamer> Streamer;
   mutable size_t BytesRead;   // Bytes read from stream


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11391.31246.patch
Type: text/x-patch
Size: 3518 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150803/1cd84384/attachment.bin>


More information about the llvm-commits mailing list