blob: 32babc759b5fef7f44c47c4d404baea7597b7db2 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==//
2//
Chris Lattner3e6e3e62002-03-29 19:06:18 +00003// This file defines the function verifier interface, that can be used for some
Chris Lattner2f7c9632001-06-06 20:29:01 +00004// sanity checking of input to the system.
5//
6// Note that this does not provide full 'java style' security and verifications,
7// instead it just tries to ensure that code is well formed.
8//
9// . There are no duplicated names in a symbol table... ie there !exist a val
10// with the same name as something in the symbol table, but with a different
11// address as what is in the symbol table...
12// . Both of a binary operator's parameters are the same type
Chris Lattner68289f02001-11-06 22:53:11 +000013// . Verify that arithmetic and other things are only performed on first class
14// types. No adding structures or arrays.
Chris Lattner2f7c9632001-06-06 20:29:01 +000015// . All of the constants in a switch statement are of the correct type
16// . The code is in valid SSA form
17// . It should be illegal to put a label into any other type (like a structure)
18// or to return one. [except constant arrays!]
19// . Right now 'add bool 0, 0' is valid. This isn't particularly good.
Chris Lattner7704e9f2002-03-14 16:53:48 +000020// * Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad
Chris Lattnerd02f08d2002-02-20 17:55:43 +000021// * PHI nodes must have an entry for each predecessor, with no extras.
22// * All basic blocks should only end with terminator insts, not contain them
Chris Lattner3e6e3e62002-03-29 19:06:18 +000023// * The entry node to a function must not have predecessors
Chris Lattnerd02f08d2002-02-20 17:55:43 +000024// * All Instructions must be embeded into a basic block
Chris Lattner68289f02001-11-06 22:53:11 +000025// . Verify that none of the Value getType()'s are null.
Chris Lattner3e6e3e62002-03-29 19:06:18 +000026// . Function's cannot take a void typed parameter
Chris Lattneraf95e582002-04-13 22:48:46 +000027// * Verify that a function's argument list agrees with it's declared type.
Chris Lattner2f7c9632001-06-06 20:29:01 +000028// . Verify that arrays and structures have fixed elements: No unsized arrays.
Chris Lattnerfbf5be52002-03-15 20:25:09 +000029// * It is illegal to specify a name for a void value.
Chris Lattner3e6e3e62002-03-29 19:06:18 +000030// * It is illegal to have a internal function that is just a declaration
Chris Lattner486302a2002-04-12 18:20:49 +000031// * It is illegal to have a ret instruction that returns a value that does not
32// agree with the function return value type.
Chris Lattnerd02f08d2002-02-20 17:55:43 +000033// . All other things that are tested by asserts spread about the code...
Chris Lattner2f7c9632001-06-06 20:29:01 +000034//
35//===----------------------------------------------------------------------===//
36
37#include "llvm/Analysis/Verifier.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000038#include "llvm/Pass.h"
Chris Lattner3e6e3e62002-03-29 19:06:18 +000039#include "llvm/Function.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000040#include "llvm/Module.h"
41#include "llvm/BasicBlock.h"
Chris Lattneraf95e582002-04-13 22:48:46 +000042#include "llvm/DerivedTypes.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000043#include "llvm/iPHINode.h"
Chris Lattner486302a2002-04-12 18:20:49 +000044#include "llvm/iTerminators.h"
Chris Lattneraf95e582002-04-13 22:48:46 +000045#include "llvm/Argument.h"
Chris Lattnerfbf5be52002-03-15 20:25:09 +000046#include "llvm/SymbolTable.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000047#include "llvm/Support/CFG.h"
48#include "Support/STLExtras.h"
49#include <algorithm>
Chris Lattner7f74a562002-01-20 22:54:45 +000050
Chris Lattnerd02f08d2002-02-20 17:55:43 +000051#if 0
Chris Lattner2f7c9632001-06-06 20:29:01 +000052#define t(x) (1 << (unsigned)Type::x)
Chris Lattner2f7c9632001-06-06 20:29:01 +000053#define SignedIntegralTypes (t(SByteTyID) | t(ShortTyID) | \
54 t(IntTyID) | t(LongTyID))
55static long UnsignedIntegralTypes = t(UByteTyID) | t(UShortTyID) |
56 t(UIntTyID) | t(ULongTyID);
57static const long FloatingPointTypes = t(FloatTyID) | t(DoubleTyID);
58
59static const long IntegralTypes = SignedIntegralTypes | UnsignedIntegralTypes;
60
Chris Lattner2f7c9632001-06-06 20:29:01 +000061static long ValidTypes[Type::FirstDerivedTyID] = {
62 [(unsigned)Instruction::UnaryOps::Not] t(BoolTyID),
63 //[Instruction::UnaryOps::Add] = IntegralTypes,
64 // [Instruction::Sub] = IntegralTypes,
65};
Chris Lattnerd02f08d2002-02-20 17:55:43 +000066#undef t
Chris Lattner2f7c9632001-06-06 20:29:01 +000067#endif
68
Chris Lattnerd02f08d2002-02-20 17:55:43 +000069// CheckFailed - A check failed, so print out the condition and the message that
70// failed. This provides a nice place to put a breakpoint if you want to see
71// why something is not correct.
72//
Chris Lattner43373a62002-02-24 23:01:21 +000073static inline void CheckFailed(const char *Cond, const std::string &Message,
Chris Lattnerd02f08d2002-02-20 17:55:43 +000074 const Value *V1 = 0, const Value *V2 = 0) {
75 std::cerr << Message << "\n";
Chris Lattner486302a2002-04-12 18:20:49 +000076 if (V1) { std::cerr << V1 << "\n"; }
77 if (V2) { std::cerr << V2 << "\n"; }
Chris Lattnerd02f08d2002-02-20 17:55:43 +000078}
Chris Lattner2f7c9632001-06-06 20:29:01 +000079
Chris Lattnerd02f08d2002-02-20 17:55:43 +000080// Assert - We know that cond should be true, if not print an error message.
81#define Assert(C, M) \
82 do { if (!(C)) { CheckFailed(#C, M); Broken = true; } } while (0)
83#define Assert1(C, M, V1) \
84 do { if (!(C)) { CheckFailed(#C, M, V1); Broken = true; } } while (0)
85#define Assert2(C, M, V1, V2) \
86 do { if (!(C)) { CheckFailed(#C, M, V1, V2); Broken = true; } } while (0)
Chris Lattner2f7c9632001-06-06 20:29:01 +000087
Chris Lattnerd02f08d2002-02-20 17:55:43 +000088
89// verifyInstruction - Verify that a non-terminator instruction is well formed.
90//
91static bool verifyInstruction(const Instruction *I) {
92 bool Broken = false;
93 assert(I->getParent() && "Instruction not embedded in basic block!");
94 Assert1(!isa<TerminatorInst>(I),
95 "Terminator instruction found embedded in basic block!\n", I);
96
97 // Check that all uses of the instruction, if they are instructions
98 // themselves, actually have parent basic blocks.
99 //
100 for (User::use_const_iterator UI = I->use_begin(), UE = I->use_end();
101 UI != UE; ++UI) {
102 if (Instruction *Used = dyn_cast<Instruction>(*UI))
103 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
104 " embeded in a basic block!", I, Used);
105 }
106
107 // Check that PHI nodes look ok
108 if (const PHINode *PN = dyn_cast<PHINode>(I)) {
Chris Lattner43373a62002-02-24 23:01:21 +0000109 std::vector<const BasicBlock*> Preds(pred_begin(I->getParent()),
110 pred_end(I->getParent()));
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000111 // Loop over all of the incoming values, make sure that there are
112 // predecessors for each one...
113 //
114 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
115 const BasicBlock *BB = PN->getIncomingBlock(i);
Chris Lattner43373a62002-02-24 23:01:21 +0000116 std::vector<const BasicBlock*>::iterator PI =
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000117 find(Preds.begin(), Preds.end(), BB);
118 Assert2(PI != Preds.end(), "PHI node has entry for basic block that"
119 " is not a predecessor!", PN, BB);
120 if (PI != Preds.end()) Preds.erase(PI);
121 }
122
123 // There should be no entries left in the predecessor list...
Chris Lattner43373a62002-02-24 23:01:21 +0000124 for (std::vector<const BasicBlock*>::iterator I = Preds.begin(),
125 E = Preds.end(); I != E; ++I)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000126 Assert2(0, "PHI node does not have entry for a predecessor basic block!",
127 PN, *I);
Chris Lattner7704e9f2002-03-14 16:53:48 +0000128 } else {
129 // Check that non-phi nodes are not self referential...
130 for (Value::use_const_iterator UI = I->use_begin(), UE = I->use_end();
131 UI != UE; ++UI)
132 Assert1(*UI != (const User*)I,
133 "Only PHI nodes may reference their own value!", I);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000134 }
Chris Lattner7704e9f2002-03-14 16:53:48 +0000135
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000136 return Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000137}
138
139
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000140// verifyBasicBlock - Verify that a basic block is well formed...
141//
142static bool verifyBasicBlock(const BasicBlock *BB) {
143 bool Broken = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000144
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000145 // Verify all instructions, except the terminator...
146 Broken |= reduce_apply_bool(BB->begin(), BB->end()-1, verifyInstruction);
Chris Lattneraf95e582002-04-13 22:48:46 +0000147
148 Assert1(BB->getTerminator(), "Basic Block does not have terminator!\n", BB);
149
150 // Check that the terminator is ok as well...
151 if (BB->getTerminator() && isa<ReturnInst>(BB->getTerminator())) {
152 const Instruction *I = BB->getTerminator();
153 const Function *F = I->getParent()->getParent();
154 if (I->getNumOperands() == 0)
155 Assert1(F->getReturnType() == Type::VoidTy,
156 "Function returns no value, but ret instruction found that does!",
157 I);
158 else
159 Assert2(F->getReturnType() == I->getOperand(0)->getType(),
160 "Function return type does not match operand "
161 "type of return inst!", I, F->getReturnType());
162 }
163
164
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000165 return Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166}
167
Chris Lattneraf95e582002-04-13 22:48:46 +0000168// verifySymbolTable - Verify that a function or module symbol table is ok
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000169//
170static bool verifySymbolTable(const SymbolTable *ST) {
171 if (ST == 0) return false;
172 bool Broken = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000174 // Loop over all of the types in the symbol table...
175 for (SymbolTable::const_iterator TI = ST->begin(), TE = ST->end();
176 TI != TE; ++TI)
177 for (SymbolTable::type_const_iterator I = TI->second.begin(),
178 E = TI->second.end(); I != E; ++I) {
179 Value *V = I->second;
180
181 // Check that there are no void typed values in the symbol table. Values
182 // with a void type cannot be put into symbol tables because they cannot
183 // have names!
184 Assert1(V->getType() != Type::VoidTy,
185 "Values with void type are not allowed to have names!\n", V);
186 }
187
188 return Broken;
189}
190
Chris Lattneraf95e582002-04-13 22:48:46 +0000191// verifyFunction - Verify that a function is ok. Return true if not so that
192// verifyModule and direct clients of the verifyFunction function are correctly
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000193// informed.
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000194//
Chris Lattneraf95e582002-04-13 22:48:46 +0000195bool verifyFunction(const Function *F) {
Chris Lattner3e6e3e62002-03-29 19:06:18 +0000196 if (F->isExternal()) return false; // Can happen if called by verifyModule
197 bool Broken = verifySymbolTable(F->getSymbolTable());
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000198
Chris Lattneraf95e582002-04-13 22:48:46 +0000199 // Check linkage of function...
Chris Lattner3e6e3e62002-03-29 19:06:18 +0000200 Assert1(!F->isExternal() || F->hasExternalLinkage(),
201 "Function cannot be an 'internal' 'declare'ation!", F);
202
Chris Lattneraf95e582002-04-13 22:48:46 +0000203 // Check function arguments...
204 const FunctionType *FT = F->getFunctionType();
205 const Function::ArgumentListType &ArgList = F->getArgumentList();
206
207 Assert2(!FT->isVarArg(), "Cannot define varargs functions in LLVM!", F, FT);
208 Assert2(FT->getParamTypes().size() == ArgList.size(),
209 "# formal arguments must match # of arguments for function type!",
210 F, FT);
211
212 // Check that the argument values match the function type for this function...
213 if (FT->getParamTypes().size() == ArgList.size()) {
214 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
215 Assert2(ArgList[i]->getType() == FT->getParamType(i),
216 "Argument value does not match function argument type!",
217 ArgList[i], FT->getParamType(i));
218 }
219
220 // Check the entry node
Chris Lattner3e6e3e62002-03-29 19:06:18 +0000221 const BasicBlock *Entry = F->getEntryNode();
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000222 Assert1(pred_begin(Entry) == pred_end(Entry),
Chris Lattneraf95e582002-04-13 22:48:46 +0000223 "Entry block to function must not have predecessors!", Entry);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000224
Chris Lattner3e6e3e62002-03-29 19:06:18 +0000225 Broken |= reduce_apply_bool(F->begin(), F->end(), verifyBasicBlock);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000226 return Broken;
227}
228
229
230namespace { // Anonymous namespace for class
231 struct VerifierPass : public MethodPass {
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000232
233 bool doInitialization(Module *M) {
234 verifySymbolTable(M->getSymbolTable());
235 return false;
236 }
Chris Lattneraf95e582002-04-13 22:48:46 +0000237 bool runOnMethod(Function *F) { verifyFunction(F); return false; }
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000238 };
239}
240
241Pass *createVerifierPass() {
242 return new VerifierPass();
243}
244
245// verifyModule - Check a module for errors, printing messages on stderr.
246// Return true if the module is corrupt.
247//
Chris Lattnerb67f7322002-02-26 21:45:33 +0000248bool verifyModule(const Module *M) {
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000249 return verifySymbolTable(M->getSymbolTable()) |
Chris Lattneraf95e582002-04-13 22:48:46 +0000250 reduce_apply_bool(M->begin(), M->end(), verifyFunction);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000251}