1 //===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/Verifier.h"
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/IRBuilder.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Module.h"
20 #include "gtest/gtest.h"
25 TEST(VerifierTest
, Branch_i1
) {
28 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(C
), /*isVarArg=*/false);
29 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "foo", M
);
30 BasicBlock
*Entry
= BasicBlock::Create(C
, "entry", F
);
31 BasicBlock
*Exit
= BasicBlock::Create(C
, "exit", F
);
32 ReturnInst::Create(C
, Exit
);
34 // To avoid triggering an assertion in BranchInst::Create, we first create
35 // a branch with an 'i1' condition ...
37 Constant
*False
= ConstantInt::getFalse(C
);
38 BranchInst
*BI
= BranchInst::Create(Exit
, Exit
, False
, Entry
);
40 // ... then use setOperand to redirect it to a value of different type.
42 Constant
*Zero32
= ConstantInt::get(IntegerType::get(C
, 32), 0);
43 BI
->setOperand(0, Zero32
);
45 EXPECT_TRUE(verifyFunction(*F
));
48 TEST(VerifierTest
, InvalidRetAttribute
) {
51 FunctionType
*FTy
= FunctionType::get(Type::getInt32Ty(C
), /*isVarArg=*/false);
52 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "foo", M
);
53 AttributeList AS
= F
->getAttributes();
55 AS
.addAttribute(C
, AttributeList::ReturnIndex
, Attribute::UWTable
));
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!"));
64 TEST(VerifierTest
, CrossModuleRef
) {
69 FunctionType
*FTy
= FunctionType::get(Type::getInt32Ty(C
), /*isVarArg=*/false);
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
);
74 BasicBlock
*Entry1
= BasicBlock::Create(C
, "entry", F1
);
75 BasicBlock
*Entry3
= BasicBlock::Create(C
, "entry", F3
);
77 // BAD: Referencing function in another module
78 CallInst::Create(F2
,"call",Entry1
);
80 // BAD: Referencing personality routine in another module
81 F3
->setPersonalityFn(F2
);
84 Constant
*ConstZero
= ConstantInt::get(Type::getInt32Ty(C
), 0);
85 ReturnInst::Create(C
, ConstZero
, Entry1
);
86 ReturnInst::Create(C
, ConstZero
, Entry3
);
89 raw_string_ostream
ErrorOS(Error
);
90 EXPECT_TRUE(verifyModule(M2
, &ErrorOS
));
91 EXPECT_TRUE(StringRef(ErrorOS
.str())
92 .equals("Global is used by function in a different module\n"
97 "Global is referenced in a different module!\n"
100 " %call = call i32 @foo2()\n"
102 "; ModuleID = 'M1'\n"));
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"
111 "; ModuleID = 'M2'\n"));
114 EXPECT_TRUE(verifyModule(M3
, &ErrorOS
));
115 EXPECT_TRUE(StringRef(ErrorOS
.str()).startswith(
116 "Referencing personality function in another module!"));
118 // Erase bad methods to avoid triggering an assertion failure on destruction
119 F1
->eraseFromParent();
120 F3
->eraseFromParent();
123 TEST(VerifierTest
, InvalidVariableLinkage
) {
126 new GlobalVariable(M
, Type::getInt8Ty(C
), false,
127 GlobalValue::LinkOnceODRLinkage
, nullptr, "Some Global");
129 raw_string_ostream
ErrorOS(Error
);
130 EXPECT_TRUE(verifyModule(M
, &ErrorOS
));
132 StringRef(ErrorOS
.str()).startswith("Global is external, but doesn't "
133 "have external or weak linkage!"));
136 TEST(VerifierTest
, InvalidFunctionLinkage
) {
140 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(C
), /*isVarArg=*/false);
141 Function::Create(FTy
, GlobalValue::LinkOnceODRLinkage
, "foo", &M
);
143 raw_string_ostream
ErrorOS(Error
);
144 EXPECT_TRUE(verifyModule(M
, &ErrorOS
));
146 StringRef(ErrorOS
.str()).startswith("Global is external, but doesn't "
147 "have external or weak linkage!"));
150 TEST(VerifierTest
, DetectInvalidDebugInfo
) {
155 DIB
.createCompileUnit(dwarf::DW_LANG_C89
, DIB
.createFile("broken.c", "/"),
156 "unittest", false, "", 0);
158 EXPECT_FALSE(verifyModule(M
));
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
));
170 auto *CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C89
,
171 DIB
.createFile("broken.c", "/"),
172 "unittest", false, "", 0);
173 new GlobalVariable(M
, Type::getInt8Ty(C
), false,
174 GlobalValue::ExternalLinkage
, nullptr, "g");
176 auto *F
= Function::Create(FunctionType::get(Type::getVoidTy(C
), false),
177 Function::ExternalLinkage
, "f", M
);
178 IRBuilder
<> Builder(BasicBlock::Create(C
, "", F
));
179 Builder
.CreateUnreachable();
180 F
->setSubprogram(DIB
.createFunction(
181 CU
, "f", "f", DIB
.createFile("broken.c", "/"), 1, nullptr, 1,
183 DISubprogram::SPFlagLocalToUnit
| DISubprogram::SPFlagDefinition
));
185 EXPECT_FALSE(verifyModule(M
));
187 // Now break it by not listing the CU at all.
188 M
.eraseNamedMetadata(M
.getOrInsertNamedMetadata("llvm.dbg.cu"));
189 EXPECT_TRUE(verifyModule(M
));
193 } // end anonymous namespace
194 } // end namespace llvm