1 //===- llvm/unittest/Bitcode/BitReaderTest.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/ReaderWriter.h" 13 #include "llvm/AsmParser/Parser.h" 14 #include "llvm/IR/Constants.h" 15 #include "llvm/IR/Instructions.h" 16 #include "llvm/IR/LLVMContext.h" 17 #include "llvm/IR/Module.h" 18 #include "llvm/IR/Verifier.h" 19 #include "llvm/Support/Debug.h" 20 #include "llvm/Support/MemoryBuffer.h" 21 #include "llvm/Support/SourceMgr.h" 22 #include "gtest/gtest.h" 23 24 using namespace llvm; 25 26 namespace { 27 28 std::unique_ptr<Module> parseAssembly(const char *Assembly) { 29 SMDiagnostic Error; 30 std::unique_ptr<Module> M = 31 parseAssemblyString(Assembly, Error, getGlobalContext()); 32 33 std::string ErrMsg; 34 raw_string_ostream OS(ErrMsg); 35 Error.print("", OS); 36 37 // A failure here means that the test itself is buggy. 38 if (!M) 39 report_fatal_error(OS.str().c_str()); 40 41 return M; 42 } 43 44 static void writeModuleToBuffer(std::unique_ptr<Module> Mod, 45 SmallVectorImpl<char> &Buffer) { 46 raw_svector_ostream OS(Buffer); 47 WriteBitcodeToFile(Mod.get(), OS); 48 } 49 50 static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context, 51 SmallString<1024> &Mem, 52 const char *Assembly) { 53 writeModuleToBuffer(parseAssembly(Assembly), Mem); 54 std::unique_ptr<MemoryBuffer> Buffer = 55 MemoryBuffer::getMemBuffer(Mem.str(), "test", false); 56 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context); 57 return std::unique_ptr<Module>(ModuleOrErr.get()); 58 } 59 60 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677 61 SmallString<1024> Mem; 62 63 LLVMContext Context; 64 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 65 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n" 66 "define void @func() {\n" 67 " unreachable\n" 68 "bb:\n" 69 " unreachable\n" 70 "}\n"); 71 EXPECT_FALSE(verifyModule(*M, &dbgs())); 72 73 // Try (and fail) to dematerialize @func. 74 M->getFunction("func")->Dematerialize(); 75 EXPECT_FALSE(M->getFunction("func")->empty()); 76 } 77 78 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) { 79 SmallString<1024> Mem; 80 81 LLVMContext Context; 82 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 83 Context, Mem, "define i8* @before() {\n" 84 " ret i8* blockaddress(@func, %bb)\n" 85 "}\n" 86 "define void @other() {\n" 87 " unreachable\n" 88 "}\n" 89 "define void @func() {\n" 90 " unreachable\n" 91 "bb:\n" 92 " unreachable\n" 93 "}\n"); 94 EXPECT_TRUE(M->getFunction("before")->empty()); 95 EXPECT_TRUE(M->getFunction("func")->empty()); 96 EXPECT_FALSE(verifyModule(*M, &dbgs())); 97 98 // Materialize @before, pulling in @func. 99 EXPECT_FALSE(M->getFunction("before")->Materialize()); 100 EXPECT_FALSE(M->getFunction("func")->empty()); 101 EXPECT_TRUE(M->getFunction("other")->empty()); 102 EXPECT_FALSE(verifyModule(*M, &dbgs())); 103 104 // Try (and fail) to dematerialize @func. 105 M->getFunction("func")->Dematerialize(); 106 EXPECT_FALSE(M->getFunction("func")->empty()); 107 EXPECT_FALSE(verifyModule(*M, &dbgs())); 108 } 109 110 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) { 111 SmallString<1024> Mem; 112 113 LLVMContext Context; 114 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 115 Context, Mem, "define void @func() {\n" 116 " unreachable\n" 117 "bb:\n" 118 " unreachable\n" 119 "}\n" 120 "define void @other() {\n" 121 " unreachable\n" 122 "}\n" 123 "define i8* @after() {\n" 124 " ret i8* blockaddress(@func, %bb)\n" 125 "}\n"); 126 EXPECT_TRUE(M->getFunction("after")->empty()); 127 EXPECT_TRUE(M->getFunction("func")->empty()); 128 EXPECT_FALSE(verifyModule(*M, &dbgs())); 129 130 // Materialize @after, pulling in @func. 131 EXPECT_FALSE(M->getFunction("after")->Materialize()); 132 EXPECT_FALSE(M->getFunction("func")->empty()); 133 EXPECT_TRUE(M->getFunction("other")->empty()); 134 EXPECT_FALSE(verifyModule(*M, &dbgs())); 135 136 // Try (and fail) to dematerialize @func. 137 M->getFunction("func")->Dematerialize(); 138 EXPECT_FALSE(M->getFunction("func")->empty()); 139 EXPECT_FALSE(verifyModule(*M, &dbgs())); 140 } 141 142 } // end namespace 143