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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 9 | #include "llvm/IR/Verifier.h" |
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" |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 16 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/LLVMContext.h" |
| 19 | #include "llvm/IR/Module.h" |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | namespace { |
| 24 | |
| 25 | TEST(VerifierTest, Branch_i1) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 26 | LLVMContext C; |
Rafael Espindola | 55fdcff | 2013-10-30 22:37:51 +0000 | [diff] [blame] | 27 | Module M("M", C); |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 28 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 29 | Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M); |
Rafael Espindola | 55fdcff | 2013-10-30 22:37:51 +0000 | [diff] [blame] | 30 | BasicBlock *Entry = BasicBlock::Create(C, "entry", F); |
| 31 | BasicBlock *Exit = BasicBlock::Create(C, "exit", F); |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 32 | ReturnInst::Create(C, Exit); |
| 33 | |
| 34 | // To avoid triggering an assertion in BranchInst::Create, we first create |
| 35 | // a branch with an 'i1' condition ... |
| 36 | |
| 37 | Constant *False = ConstantInt::getFalse(C); |
| 38 | BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry); |
| 39 | |
| 40 | // ... then use setOperand to redirect it to a value of different type. |
| 41 | |
| 42 | Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0); |
| 43 | BI->setOperand(0, Zero32); |
| 44 | |
Chandler Carruth | 043949d | 2014-01-19 02:22:18 +0000 | [diff] [blame] | 45 | EXPECT_TRUE(verifyFunction(*F)); |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Bill Wendling | e0d97df | 2013-06-19 19:26:44 +0000 | [diff] [blame] | 48 | TEST(VerifierTest, InvalidRetAttribute) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 49 | LLVMContext C; |
Bill Wendling | e0d97df | 2013-06-19 19:26:44 +0000 | [diff] [blame] | 50 | Module M("M", C); |
| 51 | FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 52 | Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 53 | AttributeList AS = F->getAttributes(); |
| 54 | F->setAttributes( |
| 55 | AS.addAttribute(C, AttributeList::ReturnIndex, Attribute::UWTable)); |
Bill Wendling | e0d97df | 2013-06-19 19:26:44 +0000 | [diff] [blame] | 56 | |
| 57 | std::string Error; |
Chandler Carruth | 043949d | 2014-01-19 02:22:18 +0000 | [diff] [blame] | 58 | raw_string_ostream ErrorOS(Error); |
| 59 | EXPECT_TRUE(verifyModule(M, &ErrorOS)); |
| 60 | EXPECT_TRUE(StringRef(ErrorOS.str()).startswith( |
| 61 | "Attribute 'uwtable' only applies to functions!")); |
Bill Wendling | e0d97df | 2013-06-19 19:26:44 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Keno Fischer | a6c4ce4 | 2015-12-01 19:06:36 +0000 | [diff] [blame] | 64 | TEST(VerifierTest, CrossModuleRef) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 65 | LLVMContext C; |
Keno Fischer | a6c4ce4 | 2015-12-01 19:06:36 +0000 | [diff] [blame] | 66 | Module M1("M1", C); |
| 67 | Module M2("M2", C); |
Keno Fischer | 60f82a2 | 2016-01-14 22:20:56 +0000 | [diff] [blame] | 68 | Module M3("M3", C); |
Keno Fischer | a6c4ce4 | 2015-12-01 19:06:36 +0000 | [diff] [blame] | 69 | FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 70 | Function *F1 = Function::Create(FTy, Function::ExternalLinkage, "foo1", M1); |
| 71 | Function *F2 = Function::Create(FTy, Function::ExternalLinkage, "foo2", M2); |
| 72 | Function *F3 = Function::Create(FTy, Function::ExternalLinkage, "foo3", M3); |
Keno Fischer | a6c4ce4 | 2015-12-01 19:06:36 +0000 | [diff] [blame] | 73 | |
| 74 | BasicBlock *Entry1 = BasicBlock::Create(C, "entry", F1); |
| 75 | BasicBlock *Entry3 = BasicBlock::Create(C, "entry", F3); |
| 76 | |
| 77 | // BAD: Referencing function in another module |
| 78 | CallInst::Create(F2,"call",Entry1); |
| 79 | |
| 80 | // BAD: Referencing personality routine in another module |
| 81 | F3->setPersonalityFn(F2); |
| 82 | |
| 83 | // Fill in the body |
| 84 | Constant *ConstZero = ConstantInt::get(Type::getInt32Ty(C), 0); |
| 85 | ReturnInst::Create(C, ConstZero, Entry1); |
| 86 | ReturnInst::Create(C, ConstZero, Entry3); |
| 87 | |
| 88 | std::string Error; |
| 89 | raw_string_ostream ErrorOS(Error); |
Keno Fischer | 60f82a2 | 2016-01-14 22:20:56 +0000 | [diff] [blame] | 90 | EXPECT_TRUE(verifyModule(M2, &ErrorOS)); |
| 91 | EXPECT_TRUE(StringRef(ErrorOS.str()) |
| 92 | .equals("Global is used by function in a different module\n" |
| 93 | "i32 ()* @foo2\n" |
| 94 | "; ModuleID = 'M2'\n" |
| 95 | "i32 ()* @foo3\n" |
| 96 | "; ModuleID = 'M3'\n" |
| 97 | "Global is referenced in a different module!\n" |
| 98 | "i32 ()* @foo2\n" |
| 99 | "; ModuleID = 'M2'\n" |
| 100 | " %call = call i32 @foo2()\n" |
| 101 | "i32 ()* @foo1\n" |
| 102 | "; ModuleID = 'M1'\n")); |
| 103 | |
| 104 | Error.clear(); |
Keno Fischer | a6c4ce4 | 2015-12-01 19:06:36 +0000 | [diff] [blame] | 105 | EXPECT_TRUE(verifyModule(M1, &ErrorOS)); |
| 106 | EXPECT_TRUE(StringRef(ErrorOS.str()).equals( |
| 107 | "Referencing function in another module!\n" |
| 108 | " %call = call i32 @foo2()\n" |
| 109 | "; ModuleID = 'M1'\n" |
| 110 | "i32 ()* @foo2\n" |
| 111 | "; ModuleID = 'M2'\n")); |
| 112 | |
| 113 | Error.clear(); |
| 114 | EXPECT_TRUE(verifyModule(M3, &ErrorOS)); |
| 115 | EXPECT_TRUE(StringRef(ErrorOS.str()).startswith( |
| 116 | "Referencing personality function in another module!")); |
| 117 | |
| 118 | // Erase bad methods to avoid triggering an assertion failure on destruction |
| 119 | F1->eraseFromParent(); |
| 120 | F3->eraseFromParent(); |
| 121 | } |
| 122 | |
Rafael Espindola | cc8900f | 2016-05-11 13:23:52 +0000 | [diff] [blame] | 123 | TEST(VerifierTest, InvalidVariableLinkage) { |
| 124 | LLVMContext C; |
| 125 | Module M("M", C); |
| 126 | new GlobalVariable(M, Type::getInt8Ty(C), false, |
| 127 | GlobalValue::LinkOnceODRLinkage, nullptr, "Some Global"); |
| 128 | std::string Error; |
| 129 | raw_string_ostream ErrorOS(Error); |
| 130 | EXPECT_TRUE(verifyModule(M, &ErrorOS)); |
| 131 | EXPECT_TRUE( |
| 132 | StringRef(ErrorOS.str()).startswith("Global is external, but doesn't " |
| 133 | "have external or weak linkage!")); |
| 134 | } |
| 135 | |
| 136 | TEST(VerifierTest, InvalidFunctionLinkage) { |
| 137 | LLVMContext C; |
| 138 | Module M("M", C); |
| 139 | |
| 140 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); |
| 141 | Function::Create(FTy, GlobalValue::LinkOnceODRLinkage, "foo", &M); |
| 142 | std::string Error; |
| 143 | raw_string_ostream ErrorOS(Error); |
| 144 | EXPECT_TRUE(verifyModule(M, &ErrorOS)); |
| 145 | EXPECT_TRUE( |
| 146 | StringRef(ErrorOS.str()).startswith("Global is external, but doesn't " |
| 147 | "have external or weak linkage!")); |
| 148 | } |
| 149 | |
Adrian Prantl | a8b2ddb | 2017-10-02 18:31:29 +0000 | [diff] [blame] | 150 | TEST(VerifierTest, DetectInvalidDebugInfo) { |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 151 | { |
| 152 | LLVMContext C; |
| 153 | Module M("M", C); |
| 154 | DIBuilder DIB(M); |
Amjad Aboud | 43c8b6b | 2016-12-14 20:24:54 +0000 | [diff] [blame] | 155 | DIB.createCompileUnit(dwarf::DW_LANG_C89, DIB.createFile("broken.c", "/"), |
| 156 | "unittest", false, "", 0); |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 157 | DIB.finalize(); |
| 158 | EXPECT_FALSE(verifyModule(M)); |
Adrian Prantl | e365618 | 2016-05-09 19:57:29 +0000 | [diff] [blame] | 159 | |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 160 | // Now break it by inserting non-CU node to the list of CUs. |
| 161 | auto *File = DIB.createFile("not-a-CU.f", "."); |
| 162 | NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu"); |
| 163 | NMD->addOperand(File); |
| 164 | EXPECT_TRUE(verifyModule(M)); |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 165 | } |
| 166 | { |
| 167 | LLVMContext C; |
| 168 | Module M("M", C); |
| 169 | DIBuilder DIB(M); |
Amjad Aboud | 43c8b6b | 2016-12-14 20:24:54 +0000 | [diff] [blame] | 170 | auto *CU = DIB.createCompileUnit(dwarf::DW_LANG_C89, |
| 171 | DIB.createFile("broken.c", "/"), |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 172 | "unittest", false, "", 0); |
| 173 | new GlobalVariable(M, Type::getInt8Ty(C), false, |
| 174 | GlobalValue::ExternalLinkage, nullptr, "g"); |
| 175 | |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 176 | auto *F = Function::Create(FunctionType::get(Type::getVoidTy(C), false), |
| 177 | Function::ExternalLinkage, "f", M); |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 178 | IRBuilder<> Builder(BasicBlock::Create(C, "", F)); |
| 179 | Builder.CreateUnreachable(); |
Paul Robinson | cda5421 | 2018-11-19 18:29:28 +0000 | [diff] [blame] | 180 | F->setSubprogram(DIB.createFunction( |
| 181 | CU, "f", "f", DIB.createFile("broken.c", "/"), 1, nullptr, 1, |
| 182 | DINode::FlagZero, |
| 183 | DISubprogram::SPFlagLocalToUnit | DISubprogram::SPFlagDefinition)); |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 184 | DIB.finalize(); |
| 185 | EXPECT_FALSE(verifyModule(M)); |
| 186 | |
| 187 | // Now break it by not listing the CU at all. |
| 188 | M.eraseNamedMetadata(M.getOrInsertNamedMetadata("llvm.dbg.cu")); |
| 189 | EXPECT_TRUE(verifyModule(M)); |
Adrian Prantl | a2ef047 | 2016-09-14 17:30:37 +0000 | [diff] [blame] | 190 | } |
Adrian Prantl | e365618 | 2016-05-09 19:57:29 +0000 | [diff] [blame] | 191 | } |
Adrian Prantl | e365618 | 2016-05-09 19:57:29 +0000 | [diff] [blame] | 192 | |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 193 | } // end anonymous namespace |
| 194 | } // end namespace llvm |