Chandler Carruth | 74b6a77 | 2013-01-07 15:35:46 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests ------------===// |
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 | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 10 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 11 | #include "llvm/IR/Constants.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/LLVMContext.h" |
| 18 | #include "llvm/IR/Module.h" |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | namespace { |
| 23 | |
| 24 | TEST(VerifierTest, Branch_i1) { |
| 25 | LLVMContext &C = getGlobalContext(); |
Rafael Espindola | 55fdcff | 2013-10-30 22:37:51 +0000 | [diff] [blame] | 26 | Module M("M", C); |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 27 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); |
Rafael Espindola | 55fdcff | 2013-10-30 22:37:51 +0000 | [diff] [blame] | 28 | Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy)); |
| 29 | BasicBlock *Entry = BasicBlock::Create(C, "entry", F); |
| 30 | BasicBlock *Exit = BasicBlock::Create(C, "exit", F); |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 31 | ReturnInst::Create(C, Exit); |
| 32 | |
| 33 | // To avoid triggering an assertion in BranchInst::Create, we first create |
| 34 | // a branch with an 'i1' condition ... |
| 35 | |
| 36 | Constant *False = ConstantInt::getFalse(C); |
| 37 | BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry); |
| 38 | |
| 39 | // ... then use setOperand to redirect it to a value of different type. |
| 40 | |
| 41 | Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0); |
| 42 | BI->setOperand(0, Zero32); |
| 43 | |
Chandler Carruth | 043949d | 2014-01-19 02:22:18 +0000 | [diff] [blame] | 44 | EXPECT_TRUE(verifyFunction(*F)); |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Bill Wendling | e0d97df | 2013-06-19 19:26:44 +0000 | [diff] [blame] | 47 | TEST(VerifierTest, InvalidRetAttribute) { |
| 48 | LLVMContext &C = getGlobalContext(); |
| 49 | Module M("M", C); |
| 50 | FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false); |
| 51 | Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy)); |
| 52 | AttributeSet AS = F->getAttributes(); |
| 53 | F->setAttributes(AS.addAttribute(C, AttributeSet::ReturnIndex, |
| 54 | Attribute::UWTable)); |
| 55 | |
| 56 | std::string Error; |
Chandler Carruth | 043949d | 2014-01-19 02:22:18 +0000 | [diff] [blame] | 57 | raw_string_ostream ErrorOS(Error); |
| 58 | EXPECT_TRUE(verifyModule(M, &ErrorOS)); |
| 59 | EXPECT_TRUE(StringRef(ErrorOS.str()).startswith( |
| 60 | "Attribute 'uwtable' only applies to functions!")); |
Bill Wendling | e0d97df | 2013-06-19 19:26:44 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Keno Fischer | a6c4ce4 | 2015-12-01 19:06:36 +0000 | [diff] [blame^] | 63 | TEST(VerifierTest, CrossModuleRef) { |
| 64 | LLVMContext &C = getGlobalContext(); |
| 65 | Module M1("M1", C); |
| 66 | Module M2("M2", C); |
| 67 | Module M3("M2", C); |
| 68 | FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false); |
| 69 | Function *F1 = cast<Function>(M1.getOrInsertFunction("foo1", FTy)); |
| 70 | Function *F2 = cast<Function>(M2.getOrInsertFunction("foo2", FTy)); |
| 71 | Function *F3 = cast<Function>(M3.getOrInsertFunction("foo3", FTy)); |
| 72 | |
| 73 | BasicBlock *Entry1 = BasicBlock::Create(C, "entry", F1); |
| 74 | BasicBlock *Entry3 = BasicBlock::Create(C, "entry", F3); |
| 75 | |
| 76 | // BAD: Referencing function in another module |
| 77 | CallInst::Create(F2,"call",Entry1); |
| 78 | |
| 79 | // BAD: Referencing personality routine in another module |
| 80 | F3->setPersonalityFn(F2); |
| 81 | |
| 82 | // Fill in the body |
| 83 | Constant *ConstZero = ConstantInt::get(Type::getInt32Ty(C), 0); |
| 84 | ReturnInst::Create(C, ConstZero, Entry1); |
| 85 | ReturnInst::Create(C, ConstZero, Entry3); |
| 86 | |
| 87 | std::string Error; |
| 88 | raw_string_ostream ErrorOS(Error); |
| 89 | EXPECT_FALSE(verifyModule(M2, &ErrorOS)); |
| 90 | EXPECT_TRUE(verifyModule(M1, &ErrorOS)); |
| 91 | EXPECT_TRUE(StringRef(ErrorOS.str()).equals( |
| 92 | "Referencing function in another module!\n" |
| 93 | " %call = call i32 @foo2()\n" |
| 94 | "; ModuleID = 'M1'\n" |
| 95 | "i32 ()* @foo2\n" |
| 96 | "; ModuleID = 'M2'\n")); |
| 97 | |
| 98 | Error.clear(); |
| 99 | EXPECT_TRUE(verifyModule(M3, &ErrorOS)); |
| 100 | EXPECT_TRUE(StringRef(ErrorOS.str()).startswith( |
| 101 | "Referencing personality function in another module!")); |
| 102 | |
| 103 | // Erase bad methods to avoid triggering an assertion failure on destruction |
| 104 | F1->eraseFromParent(); |
| 105 | F3->eraseFromParent(); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 110 | } |
| 111 | } |