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