aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Bitcode/BitReaderTest.cpp
diff options
context:
space:
mode:
authorDerek Schuff <dschuff@google.com>2015-08-03 18:01:50 +0000
committerDerek Schuff <dschuff@google.com>2015-08-03 18:01:50 +0000
commitb4c1c28c6e1b8d9b7a1c6a9e62e46bb93bd3ef40 (patch)
tree3ac2af2e1fa9ce7ca8d18107b93d73b384b6c4fc /llvm/unittests/Bitcode/BitReaderTest.cpp
parentac3a95f347f6f0780bf353d313a77e7a358f4864 (diff)
downloadllvm-b4c1c28c6e1b8d9b7a1c6a9e62e46bb93bd3ef40.zip
llvm-b4c1c28c6e1b8d9b7a1c6a9e62e46bb93bd3ef40.tar.gz
llvm-b4c1c28c6e1b8d9b7a1c6a9e62e46bb93bd3ef40.tar.bz2
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 llvm-svn: 243890
Diffstat (limited to 'llvm/unittests/Bitcode/BitReaderTest.cpp')
-rw-r--r--llvm/unittests/Bitcode/BitReaderTest.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/llvm/unittests/Bitcode/BitReaderTest.cpp b/llvm/unittests/Bitcode/BitReaderTest.cpp
index 3c56ea0..d9e207e9 100644
--- a/llvm/unittests/Bitcode/BitReaderTest.cpp
+++ b/llvm/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 @@ getStreamedModuleFromAssembly(LLVMContext &Context, SmallString<1024> &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<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;