OLD | NEW |
(Empty) | |
| 1 //===- llvm/unittest/Bitcode/NaClBitReaderTest.cpp - Tests for BitReader --===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 |
| 10 #include "llvm/ADT/SmallString.h" |
| 11 #include "llvm/Bitcode/BitstreamWriter.h" |
| 12 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
| 13 #include "llvm/IR/Constants.h" |
| 14 #include "llvm/IR/Instructions.h" |
| 15 #include "llvm/IR/LLVMContext.h" |
| 16 #include "llvm/IR/Module.h" |
| 17 #include "llvm/IR/Verifier.h" |
| 18 #include "llvm/PassManager.h" |
| 19 #include "llvm/Support/Debug.h" |
| 20 #include "llvm/Support/MemoryBuffer.h" |
| 21 #include "gtest/gtest.h" |
| 22 |
| 23 namespace llvm { |
| 24 namespace { |
| 25 |
| 26 static std::unique_ptr<Module> makeLLVMModule() { |
| 27 std::unique_ptr<Module> Mod(new Module("test-mem", getGlobalContext())); |
| 28 |
| 29 FunctionType* FuncTy = |
| 30 FunctionType::get(Type::getVoidTy(Mod->getContext()), false); |
| 31 Function* Func = Function::Create(FuncTy,GlobalValue::ExternalLinkage, |
| 32 "func", Mod.get()); |
| 33 |
| 34 BasicBlock* Entry = BasicBlock::Create(Mod->getContext(), "entry", Func); |
| 35 new UnreachableInst(Mod->getContext(), Entry); |
| 36 |
| 37 BasicBlock* BB = BasicBlock::Create(Mod->getContext(), "bb", Func); |
| 38 new UnreachableInst(Mod->getContext(), BB); |
| 39 |
| 40 return Mod; |
| 41 } |
| 42 |
| 43 static void writeModuleToBuffer(SmallVectorImpl<char> &Buffer) { |
| 44 std::unique_ptr<Module> Mod = makeLLVMModule(); |
| 45 raw_svector_ostream OS(Buffer); |
| 46 NaClWriteBitcodeToFile(Mod.get(), OS); |
| 47 } |
| 48 |
| 49 // Check that we can parse a good bitcode file. |
| 50 TEST(NaClBitReaderTest, MaterializeSimpleModule) { |
| 51 SmallString<1024> Mem; |
| 52 writeModuleToBuffer(Mem); |
| 53 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "
test", false); |
| 54 ErrorOr<Module *> ModuleOrErr = |
| 55 getNaClLazyBitcodeModule(std::move(Buffer), getGlobalContext()); |
| 56 EXPECT_EQ(true, bool(ModuleOrErr)); |
| 57 // Do something with the module just to make sure it was built. |
| 58 std::unique_ptr<Module> M(ModuleOrErr.get()); |
| 59 EXPECT_NE((Module *)nullptr, M.get()); |
| 60 M->getFunction("func")->materialize(); |
| 61 EXPECT_FALSE(verifyModule(*M, &dbgs())); |
| 62 } |
| 63 |
| 64 // Test that we catch bad stuff at the end of a bitcode file. |
| 65 TEST(NaClBitReaderTest, BadDataAfterModule) { |
| 66 SmallString<1024> Mem; |
| 67 writeModuleToBuffer(Mem); |
| 68 Mem.append("more"); // Length must be divisible by 4! |
| 69 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "
test", false); |
| 70 ErrorOr<Module *> ModuleOrErr = |
| 71 getNaClLazyBitcodeModule(std::move(Buffer), getGlobalContext()); |
| 72 EXPECT_EQ(false, bool(ModuleOrErr)); |
| 73 std::string BadMessage("Invalid data after module"); |
| 74 EXPECT_EQ(BadMessage, ModuleOrErr.getError().message()); |
| 75 } |
| 76 |
| 77 } |
| 78 } |
OLD | NEW |