1 //===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests --*- C++ -*-===// 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/IR/Constants.h" 11 #include "llvm/IR/DIBuilder.h" 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" 17 #include "llvm/IR/IRBuilder.h" 18 #include "llvm/IR/LLVMContext.h" 19 #include "llvm/IR/LegacyPassManager.h" 20 #include "llvm/IR/Module.h" 21 #include "llvm/IR/Verifier.h" 22 #include "gtest/gtest.h" 23 24 namespace llvm { 25 namespace { 26 27 TEST(VerifierTest, Branch_i1) { 28 LLVMContext C; 29 Module M("M", C); 30 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); 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); 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 47 EXPECT_TRUE(verifyFunction(*F)); 48 } 49 50 TEST(VerifierTest, InvalidRetAttribute) { 51 LLVMContext C; 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; 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!")); 64 } 65 66 TEST(VerifierTest, CrossModuleRef) { 67 LLVMContext C; 68 Module M1("M1", C); 69 Module M2("M2", C); 70 Module M3("M3", C); 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); 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(); 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 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 152 #ifndef _MSC_VER 153 // FIXME: This test causes an ICE in MSVC 2013. 154 TEST(VerifierTest, StripInvalidDebugInfo) { 155 { 156 LLVMContext C; 157 Module M("M", C); 158 DIBuilder DIB(M); 159 DIB.createCompileUnit(dwarf::DW_LANG_C89, "broken.c", "/", "unittest", 160 false, "", 0); 161 DIB.finalize(); 162 EXPECT_FALSE(verifyModule(M)); 163 164 // Now break it by inserting non-CU node to the list of CUs. 165 auto *File = DIB.createFile("not-a-CU.f", "."); 166 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu"); 167 NMD->addOperand(File); 168 EXPECT_TRUE(verifyModule(M)); 169 170 ModulePassManager MPM(true); 171 MPM.addPass(VerifierPass(false)); 172 ModuleAnalysisManager MAM(true); 173 MAM.registerPass([&] { return VerifierAnalysis(); }); 174 MPM.run(M, MAM); 175 EXPECT_FALSE(verifyModule(M)); 176 } 177 { 178 LLVMContext C; 179 Module M("M", C); 180 DIBuilder DIB(M); 181 auto *CU = DIB.createCompileUnit(dwarf::DW_LANG_C89, "broken.c", "/", 182 "unittest", false, "", 0); 183 new GlobalVariable(M, Type::getInt8Ty(C), false, 184 GlobalValue::ExternalLinkage, nullptr, "g"); 185 186 auto *F = cast<Function>(M.getOrInsertFunction( 187 "f", FunctionType::get(Type::getVoidTy(C), false))); 188 IRBuilder<> Builder(BasicBlock::Create(C, "", F)); 189 Builder.CreateUnreachable(); 190 F->setSubprogram(DIB.createFunction(CU, "f", "f", 191 DIB.createFile("broken.c", "/"), 1, 192 nullptr, true, true, 1)); 193 DIB.finalize(); 194 EXPECT_FALSE(verifyModule(M)); 195 196 // Now break it by not listing the CU at all. 197 M.eraseNamedMetadata(M.getOrInsertNamedMetadata("llvm.dbg.cu")); 198 EXPECT_TRUE(verifyModule(M)); 199 200 ModulePassManager MPM(true); 201 MPM.addPass(VerifierPass(false)); 202 ModuleAnalysisManager MAM(true); 203 MAM.registerPass([&] { return VerifierAnalysis(); }); 204 MPM.run(M, MAM); 205 EXPECT_FALSE(verifyModule(M)); 206 } 207 } 208 #endif 209 210 TEST(VerifierTest, StripInvalidDebugInfoLegacy) { 211 LLVMContext C; 212 Module M("M", C); 213 DIBuilder DIB(M); 214 DIB.createCompileUnit(dwarf::DW_LANG_C89, "broken.c", "/", 215 "unittest", false, "", 0); 216 DIB.finalize(); 217 EXPECT_FALSE(verifyModule(M)); 218 219 // Now break it. 220 auto *File = DIB.createFile("not-a-CU.f", "."); 221 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu"); 222 NMD->addOperand(File); 223 EXPECT_TRUE(verifyModule(M)); 224 225 legacy::PassManager Passes; 226 Passes.add(createVerifierPass(false)); 227 Passes.run(M); 228 EXPECT_FALSE(verifyModule(M)); 229 } 230 231 } // end anonymous namespace 232 } // end namespace llvm 233