llvm.org GIT mirror llvm / f96e27f
Fix testing for end of stream in bitstream reader. This fixes a bug found while working on the bitcode reader. In particular, the method BitstreamReader::AtEndOfStream doesn't always behave correctly when processing a data streamer. The method fillCurWord doesn't properly set CurWord/BitsInCurWord if the data streamer was already at eof, but GetBytes had not yet set the ObjectSize field of the streaming memory object. This patch fixes this problem, and provides a test to show that this problem has been fixed. Patch by Karl Schimpf. Differential Revision: http://reviews.llvm.org/D11391 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243890 91177308-0d34-0410-b5e6-96231b3b80d8 Derek Schuff 4 years ago
4 changed file(s) with 40 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
324324
325325 // If we run out of data, stop at the end of the stream.
326326 if (BytesRead == 0) {
327 CurWord = 0;
328 BitsInCurWord = 0;
327329 Size = NextChar;
328330 return;
329331 }
4949 /// starts (although it can be called anytime).
5050 void setKnownObjectSize(size_t size);
5151
52 /// The number of bytes read at a time from the data streamer.
53 static const uint32_t kChunkSize = 4096 * 4;
54
5255 private:
53 const static uint32_t kChunkSize = 4096 * 4;
5456 mutable std::vector Bytes;
5557 std::unique_ptr Streamer;
5658 mutable size_t BytesRead; // Bytes read from stream
99 #include "llvm/ADT/SmallString.h"
1010 #include "llvm/ADT/STLExtras.h"
1111 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/Bitcode/BitstreamReader.h"
1213 #include "llvm/Bitcode/BitstreamWriter.h"
1314 #include "llvm/Bitcode/ReaderWriter.h"
1415 #include "llvm/IR/Constants.h"
2021 #include "llvm/Support/Debug.h"
2122 #include "llvm/Support/MemoryBuffer.h"
2223 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/StreamingMemoryObject.h"
2325 #include "gtest/gtest.h"
2426
2527 using namespace llvm;
8890 return std::move(ModuleOrErr.get());
8991 }
9092
93 // Checks if we correctly detect eof if we try to read N bits when there are not
94 // enough bits left on the input stream to read N bits, and we are using a data
95 // streamer. In particular, it checks if we properly set the object size when
96 // the eof is reached under such conditions.
97 TEST(BitReaderTest, TestForEofAfterReadFailureOnDataStreamer) {
98 // Note: Because StreamingMemoryObject does a call to method GetBytes in it's
99 // constructor, using internal constant kChunkSize, we must fill the input
100 // with more characters than that amount.
101 static size_t InputSize = StreamingMemoryObject::kChunkSize + 5;
102 char *Text = new char[InputSize];
103 std::memset(Text, 'a', InputSize);
104 Text[InputSize - 1] = '\0';
105 StringRef Input(Text);
106
107 // Build bitsteam reader using data streamer.
108 auto MemoryBuf = MemoryBuffer::getMemBuffer(Input);
109 std::unique_ptr Streamer(
110 new BufferDataStreamer(std::move(MemoryBuf)));
111 auto OwnedBytes =
112 llvm::make_unique(std::move(Streamer));
113 auto Reader = llvm::make_unique(std::move(OwnedBytes));
114 BitstreamCursor Cursor;
115 Cursor.init(Reader.get());
116
117 // Jump to two bytes before end of stream.
118 Cursor.JumpToBit((InputSize - 4) * CHAR_BIT);
119 // Try to read 4 bytes when only 2 are present, resulting in error value 0.
120 constexpr size_t ReadErrorValue = 0;
121 EXPECT_EQ(ReadErrorValue, Cursor.Read(32));
122 // Should be at eof now.
123 EXPECT_TRUE(Cursor.AtEndOfStream());
124 }
125
91126 TEST(BitReaderTest, MateralizeForwardRefWithStream) {
92127 SmallString<1024> Mem;
93128