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