blob: 8b951739e4710558fcc5cd16f72bcfc469870330 [file] [log] [blame]
Eugene Zelenko6ac3f732016-01-26 18:48:36 +00001//===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests --*- C++ -*-===//
Nick Lewycky1d9a8152010-02-15 22:09:09 +00002//
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 Carruth5ad5f152014-01-13 09:26:24 +000010#include "llvm/IR/Verifier.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
Keno Fischerf6d17b92016-01-14 22:42:02 +000012#include "llvm/IR/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/DerivedTypes.h"
14#include "llvm/IR/Function.h"
15#include "llvm/IR/GlobalAlias.h"
16#include "llvm/IR/GlobalVariable.h"
17#include "llvm/IR/Instructions.h"
18#include "llvm/IR/LLVMContext.h"
19#include "llvm/IR/Module.h"
Nick Lewycky1d9a8152010-02-15 22:09:09 +000020#include "gtest/gtest.h"
21
22namespace llvm {
23namespace {
24
25TEST(VerifierTest, Branch_i1) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000026 LLVMContext C;
Rafael Espindola55fdcff2013-10-30 22:37:51 +000027 Module M("M", C);
Nick Lewycky1d9a8152010-02-15 22:09:09 +000028 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
Rafael Espindola55fdcff2013-10-30 22:37:51 +000029 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
30 BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
31 BasicBlock *Exit = BasicBlock::Create(C, "exit", F);
Nick Lewycky1d9a8152010-02-15 22:09:09 +000032 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 Carruth043949d2014-01-19 02:22:18 +000045 EXPECT_TRUE(verifyFunction(*F));
Nick Lewycky1d9a8152010-02-15 22:09:09 +000046}
47
Bill Wendlinge0d97df2013-06-19 19:26:44 +000048TEST(VerifierTest, InvalidRetAttribute) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000049 LLVMContext C;
Bill Wendlinge0d97df2013-06-19 19:26:44 +000050 Module M("M", C);
51 FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
52 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
53 AttributeSet AS = F->getAttributes();
54 F->setAttributes(AS.addAttribute(C, AttributeSet::ReturnIndex,
55 Attribute::UWTable));
56
57 std::string Error;
Chandler Carruth043949d2014-01-19 02:22:18 +000058 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 Wendlinge0d97df2013-06-19 19:26:44 +000062}
63
Keno Fischera6c4ce42015-12-01 19:06:36 +000064TEST(VerifierTest, CrossModuleRef) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000065 LLVMContext C;
Keno Fischera6c4ce42015-12-01 19:06:36 +000066 Module M1("M1", C);
67 Module M2("M2", C);
Keno Fischer60f82a22016-01-14 22:20:56 +000068 Module M3("M3", C);
Keno Fischera6c4ce42015-12-01 19:06:36 +000069 FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
70 Function *F1 = cast<Function>(M1.getOrInsertFunction("foo1", FTy));
71 Function *F2 = cast<Function>(M2.getOrInsertFunction("foo2", FTy));
72 Function *F3 = cast<Function>(M3.getOrInsertFunction("foo3", FTy));
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 Fischer60f82a22016-01-14 22:20:56 +000090 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 Fischera6c4ce42015-12-01 19:06:36 +0000105 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
Keno Fischerf6d17b92016-01-14 22:42:02 +0000123TEST(VerifierTest, CrossModuleMetadataRef) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000124 LLVMContext C;
Keno Fischerf6d17b92016-01-14 22:42:02 +0000125 Module M1("M1", C);
126 Module M2("M2", C);
127 GlobalVariable *newGV =
128 new GlobalVariable(M1, Type::getInt8Ty(C), false,
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000129 GlobalVariable::ExternalLinkage, nullptr,
130 "Some Global");
Keno Fischera6c4ce42015-12-01 19:06:36 +0000131
Keno Fischerf6d17b92016-01-14 22:42:02 +0000132 DIBuilder dbuilder(M2);
133 auto CU = dbuilder.createCompileUnit(dwarf::DW_LANG_Julia, "test.jl", ".",
134 "unittest", false, "", 0);
135 auto File = dbuilder.createFile("test.jl", ".");
136 auto Ty = dbuilder.createBasicType("Int8", 8, 8, dwarf::DW_ATE_signed);
137 dbuilder.createGlobalVariable(CU, "_SOME_GLOBAL", "_SOME_GLOBAL", File, 1, Ty,
138 false, newGV);
139 dbuilder.finalize();
Keno Fischera6c4ce42015-12-01 19:06:36 +0000140
Keno Fischerf6d17b92016-01-14 22:42:02 +0000141 std::string Error;
142 raw_string_ostream ErrorOS(Error);
143 EXPECT_TRUE(verifyModule(M2, &ErrorOS));
144 EXPECT_TRUE(StringRef(ErrorOS.str())
145 .startswith("Referencing global in another module!"));
146}
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000147} // end anonymous namespace
148} // end namespace llvm