Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests --*- C++ -*-===// |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +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 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 10 | #include "llvm/IR/Constants.h" |
Keno Fischer | f6d17b9 | 2016-01-14 22:42:02 +0000 | [diff] [blame] | 11 | #include "llvm/IR/DIBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 12 | #include "llvm/IR/DerivedTypes.h" |
| 13 | #include "llvm/IR/Function.h" |
| 14 | #include "llvm/IR/GlobalAlias.h" |
| 15 | #include "llvm/IR/GlobalVariable.h" |
| 16 | #include "llvm/IR/Instructions.h" |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 17 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/LLVMContext.h" |
Adrian Prantl | 94a903e | 2016-05-25 21:33:20 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
Adrian Prantl | 94a903e | 2016-05-25 21:33:20 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Verifier.h" |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 22 | #include "gtest/gtest.h" |
| 23 | |
| 24 | namespace llvm { |
| 25 | namespace { |
| 26 | |
| 27 | TEST(VerifierTest, Branch_i1) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 28 | LLVMContext C; |
Rafael Espindola | 55fdcff | 2013-10-30 22:37:51 +0000 | [diff] [blame] | 29 | Module M("M", C); |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 30 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); |
Rafael Espindola | 55fdcff | 2013-10-30 22:37:51 +0000 | [diff] [blame] | 31 | Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy)); |
| 32 | BasicBlock *Entry = BasicBlock::Create(C, "entry", F); |
| 33 | BasicBlock *Exit = BasicBlock::Create(C, "exit", F); |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 34 | ReturnInst::Create(C, Exit); |
| 35 | |
| 36 | // To avoid triggering an assertion in BranchInst::Create, we first create |
| 37 | // a branch with an 'i1' condition ... |
| 38 | |
| 39 | Constant *False = ConstantInt::getFalse(C); |
| 40 | BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry); |
| 41 | |
| 42 | // ... then use setOperand to redirect it to a value of different type. |
| 43 | |
| 44 | Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0); |
| 45 | BI->setOperand(0, Zero32); |
| 46 | |
Chandler Carruth | 043949d | 2014-01-19 02:22:18 +0000 | [diff] [blame] | 47 | EXPECT_TRUE(verifyFunction(*F)); |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Bill Wendling | e0d97df | 2013-06-19 19:26:44 +0000 | [diff] [blame] | 50 | TEST(VerifierTest, InvalidRetAttribute) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 51 | LLVMContext C; |
Bill Wendling | e0d97df | 2013-06-19 19:26:44 +0000 | [diff] [blame] | 52 | Module M("M", C); |
| 53 | FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false); |
| 54 | Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy)); |
| 55 | AttributeSet AS = F->getAttributes(); |
| 56 | F->setAttributes(AS.addAttribute(C, AttributeSet::ReturnIndex, |
| 57 | Attribute::UWTable)); |
| 58 | |
| 59 | std::string Error; |
Chandler Carruth | 043949d | 2014-01-19 02:22:18 +0000 | [diff] [blame] | 60 | raw_string_ostream ErrorOS(Error); |
| 61 | EXPECT_TRUE(verifyModule(M, &ErrorOS)); |
| 62 | EXPECT_TRUE(StringRef(ErrorOS.str()).startswith( |
| 63 | "Attribute 'uwtable' only applies to functions!")); |
Bill Wendling | e0d97df | 2013-06-19 19:26:44 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Keno Fischer | a6c4ce4 | 2015-12-01 19:06:36 +0000 | [diff] [blame] | 66 | TEST(VerifierTest, CrossModuleRef) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 67 | LLVMContext C; |
Keno Fischer | a6c4ce4 | 2015-12-01 19:06:36 +0000 | [diff] [blame] | 68 | Module M1("M1", C); |
| 69 | Module M2("M2", C); |
Keno Fischer | 60f82a2 | 2016-01-14 22:20:56 +0000 | [diff] [blame] | 70 | Module M3("M3", C); |
Keno Fischer | a6c4ce4 | 2015-12-01 19:06:36 +0000 | [diff] [blame] | 71 | FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false); |
| 72 | Function *F1 = cast<Function>(M1.getOrInsertFunction("foo1", FTy)); |
| 73 | Function *F2 = cast<Function>(M2.getOrInsertFunction("foo2", FTy)); |
| 74 | Function *F3 = cast<Function>(M3.getOrInsertFunction("foo3", FTy)); |
| 75 | |
| 76 | BasicBlock *Entry1 = BasicBlock::Create(C, "entry", F1); |
| 77 | BasicBlock *Entry3 = BasicBlock::Create(C, "entry", F3); |
| 78 | |
| 79 | // BAD: Referencing function in another module |
| 80 | CallInst::Create(F2,"call",Entry1); |
| 81 | |
| 82 | // BAD: Referencing personality routine in another module |
| 83 | F3->setPersonalityFn(F2); |
| 84 | |
| 85 | // Fill in the body |
| 86 | Constant *ConstZero = ConstantInt::get(Type::getInt32Ty(C), 0); |
| 87 | ReturnInst::Create(C, ConstZero, Entry1); |
| 88 | ReturnInst::Create(C, ConstZero, Entry3); |
| 89 | |
| 90 | std::string Error; |
| 91 | raw_string_ostream ErrorOS(Error); |
Keno Fischer | 60f82a2 | 2016-01-14 22:20:56 +0000 | [diff] [blame] | 92 | EXPECT_TRUE(verifyModule(M2, &ErrorOS)); |
| 93 | EXPECT_TRUE(StringRef(ErrorOS.str()) |
| 94 | .equals("Global is used by function in a different module\n" |
| 95 | "i32 ()* @foo2\n" |
| 96 | "; ModuleID = 'M2'\n" |
| 97 | "i32 ()* @foo3\n" |
| 98 | "; ModuleID = 'M3'\n" |
| 99 | "Global is referenced in a different module!\n" |
| 100 | "i32 ()* @foo2\n" |
| 101 | "; ModuleID = 'M2'\n" |
| 102 | " %call = call i32 @foo2()\n" |
| 103 | "i32 ()* @foo1\n" |
| 104 | "; ModuleID = 'M1'\n")); |
| 105 | |
| 106 | Error.clear(); |
Keno Fischer | a6c4ce4 | 2015-12-01 19:06:36 +0000 | [diff] [blame] | 107 | EXPECT_TRUE(verifyModule(M1, &ErrorOS)); |
| 108 | EXPECT_TRUE(StringRef(ErrorOS.str()).equals( |
| 109 | "Referencing function in another module!\n" |
| 110 | " %call = call i32 @foo2()\n" |
| 111 | "; ModuleID = 'M1'\n" |
| 112 | "i32 ()* @foo2\n" |
| 113 | "; ModuleID = 'M2'\n")); |
| 114 | |
| 115 | Error.clear(); |
| 116 | EXPECT_TRUE(verifyModule(M3, &ErrorOS)); |
| 117 | EXPECT_TRUE(StringRef(ErrorOS.str()).startswith( |
| 118 | "Referencing personality function in another module!")); |
| 119 | |
| 120 | // Erase bad methods to avoid triggering an assertion failure on destruction |
| 121 | F1->eraseFromParent(); |
| 122 | F3->eraseFromParent(); |
| 123 | } |
| 124 | |
Rafael Espindola | cc8900f | 2016-05-11 13:23:52 +0000 | [diff] [blame] | 125 | TEST(VerifierTest, InvalidVariableLinkage) { |
| 126 | LLVMContext C; |
| 127 | Module M("M", C); |
| 128 | new GlobalVariable(M, Type::getInt8Ty(C), false, |
| 129 | GlobalValue::LinkOnceODRLinkage, nullptr, "Some Global"); |
| 130 | std::string Error; |
| 131 | raw_string_ostream ErrorOS(Error); |
| 132 | EXPECT_TRUE(verifyModule(M, &ErrorOS)); |
| 133 | EXPECT_TRUE( |
| 134 | StringRef(ErrorOS.str()).startswith("Global is external, but doesn't " |
| 135 | "have external or weak linkage!")); |
| 136 | } |
| 137 | |
| 138 | TEST(VerifierTest, InvalidFunctionLinkage) { |
| 139 | LLVMContext C; |
| 140 | Module M("M", C); |
| 141 | |
| 142 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); |
| 143 | Function::Create(FTy, GlobalValue::LinkOnceODRLinkage, "foo", &M); |
| 144 | std::string Error; |
| 145 | raw_string_ostream ErrorOS(Error); |
| 146 | EXPECT_TRUE(verifyModule(M, &ErrorOS)); |
| 147 | EXPECT_TRUE( |
| 148 | StringRef(ErrorOS.str()).startswith("Global is external, but doesn't " |
| 149 | "have external or weak linkage!")); |
| 150 | } |
| 151 | |
Adrian Prantl | e365618 | 2016-05-09 19:57:29 +0000 | [diff] [blame] | 152 | TEST(VerifierTest, StripInvalidDebugInfo) { |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 153 | { |
| 154 | LLVMContext C; |
| 155 | Module M("M", C); |
| 156 | DIBuilder DIB(M); |
Amjad Aboud | 43c8b6b | 2016-12-14 20:24:54 +0000 | [diff] [blame] | 157 | DIB.createCompileUnit(dwarf::DW_LANG_C89, DIB.createFile("broken.c", "/"), |
| 158 | "unittest", false, "", 0); |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 159 | DIB.finalize(); |
| 160 | EXPECT_FALSE(verifyModule(M)); |
Adrian Prantl | e365618 | 2016-05-09 19:57:29 +0000 | [diff] [blame] | 161 | |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 162 | // Now break it by inserting non-CU node to the list of CUs. |
| 163 | auto *File = DIB.createFile("not-a-CU.f", "."); |
| 164 | NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu"); |
| 165 | NMD->addOperand(File); |
| 166 | EXPECT_TRUE(verifyModule(M)); |
Adrian Prantl | e365618 | 2016-05-09 19:57:29 +0000 | [diff] [blame] | 167 | |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 168 | ModulePassManager MPM(true); |
| 169 | MPM.addPass(VerifierPass(false)); |
| 170 | ModuleAnalysisManager MAM(true); |
| 171 | MAM.registerPass([&] { return VerifierAnalysis(); }); |
| 172 | MPM.run(M, MAM); |
| 173 | EXPECT_FALSE(verifyModule(M)); |
| 174 | } |
| 175 | { |
| 176 | LLVMContext C; |
| 177 | Module M("M", C); |
| 178 | DIBuilder DIB(M); |
Amjad Aboud | 43c8b6b | 2016-12-14 20:24:54 +0000 | [diff] [blame] | 179 | auto *CU = DIB.createCompileUnit(dwarf::DW_LANG_C89, |
| 180 | DIB.createFile("broken.c", "/"), |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 181 | "unittest", false, "", 0); |
| 182 | new GlobalVariable(M, Type::getInt8Ty(C), false, |
| 183 | GlobalValue::ExternalLinkage, nullptr, "g"); |
| 184 | |
| 185 | auto *F = cast<Function>(M.getOrInsertFunction( |
| 186 | "f", FunctionType::get(Type::getVoidTy(C), false))); |
| 187 | IRBuilder<> Builder(BasicBlock::Create(C, "", F)); |
| 188 | Builder.CreateUnreachable(); |
| 189 | F->setSubprogram(DIB.createFunction(CU, "f", "f", |
| 190 | DIB.createFile("broken.c", "/"), 1, |
| 191 | nullptr, true, true, 1)); |
| 192 | DIB.finalize(); |
| 193 | EXPECT_FALSE(verifyModule(M)); |
| 194 | |
| 195 | // Now break it by not listing the CU at all. |
| 196 | M.eraseNamedMetadata(M.getOrInsertNamedMetadata("llvm.dbg.cu")); |
| 197 | EXPECT_TRUE(verifyModule(M)); |
| 198 | |
| 199 | ModulePassManager MPM(true); |
| 200 | MPM.addPass(VerifierPass(false)); |
| 201 | ModuleAnalysisManager MAM(true); |
| 202 | MAM.registerPass([&] { return VerifierAnalysis(); }); |
| 203 | MPM.run(M, MAM); |
| 204 | EXPECT_FALSE(verifyModule(M)); |
| 205 | } |
Adrian Prantl | e365618 | 2016-05-09 19:57:29 +0000 | [diff] [blame] | 206 | } |
Adrian Prantl | e365618 | 2016-05-09 19:57:29 +0000 | [diff] [blame] | 207 | |
Adrian Prantl | 94a903e | 2016-05-25 21:33:20 +0000 | [diff] [blame] | 208 | TEST(VerifierTest, StripInvalidDebugInfoLegacy) { |
| 209 | LLVMContext C; |
| 210 | Module M("M", C); |
| 211 | DIBuilder DIB(M); |
Amjad Aboud | 43c8b6b | 2016-12-14 20:24:54 +0000 | [diff] [blame] | 212 | DIB.createCompileUnit(dwarf::DW_LANG_C89, DIB.createFile("broken.c", "/"), |
Adrian Prantl | 94a903e | 2016-05-25 21:33:20 +0000 | [diff] [blame] | 213 | "unittest", false, "", 0); |
| 214 | DIB.finalize(); |
| 215 | EXPECT_FALSE(verifyModule(M)); |
| 216 | |
| 217 | // Now break it. |
| 218 | auto *File = DIB.createFile("not-a-CU.f", "."); |
| 219 | NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu"); |
| 220 | NMD->addOperand(File); |
| 221 | EXPECT_TRUE(verifyModule(M)); |
| 222 | |
| 223 | legacy::PassManager Passes; |
| 224 | Passes.add(createVerifierPass(false)); |
| 225 | Passes.run(M); |
| 226 | EXPECT_FALSE(verifyModule(M)); |
| 227 | } |
| 228 | |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 229 | } // end anonymous namespace |
| 230 | } // end namespace llvm |