Bitcode: Change reader interface to take memory buffers.
As proposed on llvm-dev: http://lists.llvm.org/pipermail/llvm-dev/2016-October/106595.html This change also fixes an API oddity where BitstreamCursor::Read() would return zero for the first read past the end of the bitstream, but would report_fatal_error for subsequent reads. Now we always report_fatal_error for all reads past the end. Updated clients to check for the end of the bitstream before reading from it. I also needed to add padding to the invalid bitcode tests in test/Bitcode/. This is because the streaming interface was not checking that the file size is a multiple of 4. Differential Revision: https://reviews.llvm.org/D26219 llvm-svn: 285773
This commit is contained in:
@@ -18,11 +18,9 @@
|
||||
#include "llvm/IR/LLVMContext.h"
|
||||
#include "llvm/IR/Module.h"
|
||||
#include "llvm/IR/Verifier.h"
|
||||
#include "llvm/Support/DataStream.h"
|
||||
#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;
|
||||
@@ -62,84 +60,6 @@ static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
|
||||
return std::move(ModuleOrErr.get());
|
||||
}
|
||||
|
||||
class BufferDataStreamer : public DataStreamer {
|
||||
std::unique_ptr<MemoryBuffer> Buffer;
|
||||
unsigned Pos = 0;
|
||||
size_t GetBytes(unsigned char *Out, size_t Len) override {
|
||||
StringRef Buf = Buffer->getBuffer();
|
||||
size_t Left = Buf.size() - Pos;
|
||||
Len = std::min(Left, Len);
|
||||
memcpy(Out, Buffer->getBuffer().substr(Pos).data(), Len);
|
||||
Pos += Len;
|
||||
return Len;
|
||||
}
|
||||
|
||||
public:
|
||||
BufferDataStreamer(std::unique_ptr<MemoryBuffer> Buffer)
|
||||
: Buffer(std::move(Buffer)) {}
|
||||
};
|
||||
|
||||
static std::unique_ptr<Module>
|
||||
getStreamedModuleFromAssembly(LLVMContext &Context, SmallString<1024> &Mem,
|
||||
const char *Assembly) {
|
||||
writeModuleToBuffer(parseAssembly(Context, Assembly), Mem);
|
||||
std::unique_ptr<MemoryBuffer> Buffer =
|
||||
MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
|
||||
auto Streamer = llvm::make_unique<BufferDataStreamer>(std::move(Buffer));
|
||||
ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
|
||||
getStreamedBitcodeModule("test", std::move(Streamer), Context);
|
||||
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.
|
||||
const size_t ReadErrorValue = 0;
|
||||
EXPECT_EQ(ReadErrorValue, Cursor.Read(32));
|
||||
// Should be at eof now.
|
||||
EXPECT_TRUE(Cursor.AtEndOfStream());
|
||||
|
||||
delete[] Text;
|
||||
}
|
||||
|
||||
TEST(BitReaderTest, MateralizeForwardRefWithStream) {
|
||||
SmallString<1024> Mem;
|
||||
|
||||
LLVMContext Context;
|
||||
std::unique_ptr<Module> M = getStreamedModuleFromAssembly(
|
||||
Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
|
||||
"define void @func() {\n"
|
||||
" unreachable\n"
|
||||
"bb:\n"
|
||||
" unreachable\n"
|
||||
"}\n");
|
||||
EXPECT_FALSE(M->getFunction("func")->empty());
|
||||
}
|
||||
|
||||
// Tests that lazy evaluation can parse functions out of order.
|
||||
TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) {
|
||||
SmallString<1024> Mem;
|
||||
@@ -216,6 +136,7 @@ TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
|
||||
" unreachable\n"
|
||||
"}\n");
|
||||
EXPECT_FALSE(verifyModule(*M, &dbgs()));
|
||||
EXPECT_FALSE(M->getFunction("func")->empty());
|
||||
}
|
||||
|
||||
TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) {
|
||||
|
||||
Reference in New Issue
Block a user