From f96e27f256cc922633b1ff8c13a61b3ff28ae7e1 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Mon, 3 Aug 2015 18:01:50 +0000 Subject: [PATCH] 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 --- include/llvm/Bitcode/BitstreamReader.h | 2 ++ include/llvm/Support/StreamingMemoryObject.h | 4 ++- test/Bitcode/Inputs/invalid-abbrev.bc | Bin 129 -> 132 bytes unittests/Bitcode/BitReaderTest.cpp | 35 +++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/include/llvm/Bitcode/BitstreamReader.h b/include/llvm/Bitcode/BitstreamReader.h index 4c040a7f3e2..c0cf6cde887 100644 --- a/include/llvm/Bitcode/BitstreamReader.h +++ b/include/llvm/Bitcode/BitstreamReader.h @@ -325,6 +325,8 @@ public: // If we run out of data, stop at the end of the stream. if (BytesRead == 0) { + CurWord = 0; + BitsInCurWord = 0; Size = NextChar; return; } diff --git a/include/llvm/Support/StreamingMemoryObject.h b/include/llvm/Support/StreamingMemoryObject.h index 7cb6438d134..a5980c23594 100644 --- a/include/llvm/Support/StreamingMemoryObject.h +++ b/include/llvm/Support/StreamingMemoryObject.h @@ -50,8 +50,10 @@ public: /// 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 Bytes; std::unique_ptr Streamer; mutable size_t BytesRead; // Bytes read from stream diff --git a/test/Bitcode/Inputs/invalid-abbrev.bc b/test/Bitcode/Inputs/invalid-abbrev.bc index 4e8f3944b84b9b1e6d8e604f89ecefe9dee4503e..06c2fd38cb27f343ce6d4f20d5729f3c695f1fc2 100644 GIT binary patch delta 11 ScmZo &Mem, 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 Streamer( + new BufferDataStreamer(std::move(MemoryBuf))); + auto OwnedBytes = + llvm::make_unique(std::move(Streamer)); + auto Reader = llvm::make_unique(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; -- 2.34.1