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 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false); 55 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context); 56 return std::unique_ptr<Module>(ModuleOrErr.get()); 57 } 58 59 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677 60 SmallString<1024> Mem; 61 62 LLVMContext Context; 63 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 64 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n" 65 "define void @func() {\n" 66 " unreachable\n" 67 "bb:\n" 68 " unreachable\n" 69 "}\n"); 70 EXPECT_FALSE(verifyModule(*M, &dbgs())); 71 72 // Try (and fail) to dematerialize @func. 73 M->getFunction("func")->Dematerialize(); 74 EXPECT_FALSE(M->getFunction("func")->empty()); 75 } 76 77 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) { 78 SmallString<1024> Mem; 79 80 LLVMContext Context; 81 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 82 Context, Mem, "define i8* @before() {\n" 83 " ret i8* blockaddress(@func, %bb)\n" 84 "}\n" 85 "define void @other() {\n" 86 " unreachable\n" 87 "}\n" 88 "define void @func() {\n" 89 " unreachable\n" 90 "bb:\n" 91 " unreachable\n" 92 "}\n"); 93 EXPECT_TRUE(M->getFunction("before")->empty()); 94 EXPECT_TRUE(M->getFunction("func")->empty()); 95 EXPECT_FALSE(verifyModule(*M, &dbgs())); 96 97 // Materialize @before, pulling in @func. 98 EXPECT_FALSE(M->getFunction("before")->Materialize()); 99 EXPECT_FALSE(M->getFunction("func")->empty()); 100 EXPECT_TRUE(M->getFunction("other")->empty()); 101 EXPECT_FALSE(verifyModule(*M, &dbgs())); 102 103 // Try (and fail) to dematerialize @func. 104 M->getFunction("func")->Dematerialize(); 105 EXPECT_FALSE(M->getFunction("func")->empty()); 106 EXPECT_FALSE(verifyModule(*M, &dbgs())); 107 } 108 109 TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) { 110 SmallString<1024> Mem; 111 112 LLVMContext Context; 113 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 114 Context, Mem, "define void @func() {\n" 115 " unreachable\n" 116 "bb:\n" 117 " unreachable\n" 118 "}\n" 119 "define void @other() {\n" 120 " unreachable\n" 121 "}\n" 122 "define i8* @after() {\n" 123 " ret i8* blockaddress(@func, %bb)\n" 124 "}\n"); 125 EXPECT_TRUE(M->getFunction("after")->empty()); 126 EXPECT_TRUE(M->getFunction("func")->empty()); 127 EXPECT_FALSE(verifyModule(*M, &dbgs())); 128 129 // Materialize @after, pulling in @func. 130 EXPECT_FALSE(M->getFunction("after")->Materialize()); 131 EXPECT_FALSE(M->getFunction("func")->empty()); 132 EXPECT_TRUE(M->getFunction("other")->empty()); 133 EXPECT_FALSE(verifyModule(*M, &dbgs())); 134 135 // Try (and fail) to dematerialize @func. 136 M->getFunction("func")->Dematerialize(); 137 EXPECT_FALSE(M->getFunction("func")->empty()); 138 EXPECT_FALSE(verifyModule(*M, &dbgs())); 139 } 140 141 } // end namespace 142