blob: 0196a99ce6f907286938c42a75adc35e43a6afb8 [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...
Chris Lattner0e851da2002-04-18 20:37:37 +000012// * Both of a binary operator's parameters are the same type
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000013// * Verify that the indices of mem access instructions match other operands
Chris Lattner68289f02001-11-06 22:53:11 +000014// . Verify that arithmetic and other things are only performed on first class
15// types. No adding structures or arrays.
Chris Lattner2f7c9632001-06-06 20:29:01 +000016// . All of the constants in a switch statement are of the correct type
17// . The code is in valid SSA form
18// . It should be illegal to put a label into any other type (like a structure)
19// or to return one. [except constant arrays!]
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.
Chris Lattner0e851da2002-04-18 20:37:37 +000022// . 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 Lattnerd46bb6e2002-04-24 19:12:21 +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 Lattner21ea83b2002-04-18 22:11:52 +000045#include "llvm/iOther.h"
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000046#include "llvm/iMemory.h"
Chris Lattneraf95e582002-04-13 22:48:46 +000047#include "llvm/Argument.h"
Chris Lattnerfbf5be52002-03-15 20:25:09 +000048#include "llvm/SymbolTable.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000049#include "llvm/Support/CFG.h"
Chris Lattner0e851da2002-04-18 20:37:37 +000050#include "llvm/Support/InstVisitor.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000051#include "Support/STLExtras.h"
52#include <algorithm>
Chris Lattner7f74a562002-01-20 22:54:45 +000053
Chris Lattner0e851da2002-04-18 20:37:37 +000054namespace { // Anonymous namespace for class
Chris Lattner2f7c9632001-06-06 20:29:01 +000055
Chris Lattnerc8e66542002-04-27 06:56:12 +000056 struct Verifier : public FunctionPass, InstVisitor<Verifier> {
Chris Lattner0e851da2002-04-18 20:37:37 +000057 bool Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +000058
Chris Lattner0e851da2002-04-18 20:37:37 +000059 Verifier() : Broken(false) {}
Chris Lattner2f7c9632001-06-06 20:29:01 +000060
Chris Lattner37104aa2002-04-29 14:57:45 +000061 virtual const char *getPassName() const { return "Module Verifier"; }
62
Chris Lattner0e851da2002-04-18 20:37:37 +000063 bool doInitialization(Module *M) {
64 verifySymbolTable(M->getSymbolTable());
65 return false;
66 }
67
Chris Lattnerc8e66542002-04-27 06:56:12 +000068 bool runOnFunction(Function *F) {
Chris Lattner0e851da2002-04-18 20:37:37 +000069 visit(F);
70 return false;
71 }
72
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000073 bool doFinalization(Module *M) {
Chris Lattner9713b842002-04-28 16:04:26 +000074 // Scan through, checking all of the external function's linkage now...
75 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
76 if ((*I)->isExternal() && (*I)->hasInternalLinkage())
Chris Lattner412d2772002-04-28 16:06:24 +000077 CheckFailed("Function Declaration has Internal Linkage!", (*I));
Chris Lattner9713b842002-04-28 16:04:26 +000078
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000079 if (Broken) {
80 cerr << "Broken module found, compilation aborted!\n";
81 abort();
82 }
83 return false;
84 }
85
Chris Lattnerf12cc842002-04-28 21:27:06 +000086 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
87 AU.setPreservesAll();
88 }
89
Chris Lattner0e851da2002-04-18 20:37:37 +000090 // Verification methods...
91 void verifySymbolTable(SymbolTable *ST);
92 void visitFunction(Function *F);
93 void visitBasicBlock(BasicBlock *BB);
94 void visitPHINode(PHINode *PN);
95 void visitBinaryOperator(BinaryOperator *B);
Chris Lattner21ea83b2002-04-18 22:11:52 +000096 void visitCallInst(CallInst *CI);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000097 void visitGetElementPtrInst(GetElementPtrInst *GEP);
98 void visitLoadInst(LoadInst *LI);
99 void visitStoreInst(StoreInst *SI);
Chris Lattner0e851da2002-04-18 20:37:37 +0000100 void visitInstruction(Instruction *I);
101
102 // CheckFailed - A check failed, so print out the condition and the message
103 // that failed. This provides a nice place to put a breakpoint if you want
104 // to see why something is not correct.
105 //
Chris Lattner412d2772002-04-28 16:06:24 +0000106 inline void CheckFailed(const std::string &Message,
Chris Lattner0e851da2002-04-18 20:37:37 +0000107 const Value *V1 = 0, const Value *V2 = 0) {
108 std::cerr << Message << "\n";
109 if (V1) { std::cerr << V1 << "\n"; }
110 if (V2) { std::cerr << V2 << "\n"; }
111 Broken = true;
112 }
113 };
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000114}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000116// Assert - We know that cond should be true, if not print an error message.
117#define Assert(C, M) \
Chris Lattner412d2772002-04-28 16:06:24 +0000118 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000119#define Assert1(C, M, V1) \
Chris Lattner412d2772002-04-28 16:06:24 +0000120 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000121#define Assert2(C, M, V1, V2) \
Chris Lattner412d2772002-04-28 16:06:24 +0000122 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000124
Chris Lattneraf95e582002-04-13 22:48:46 +0000125// verifySymbolTable - Verify that a function or module symbol table is ok
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000126//
Chris Lattner0e851da2002-04-18 20:37:37 +0000127void Verifier::verifySymbolTable(SymbolTable *ST) {
128 if (ST == 0) return; // No symbol table to process
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000130 // Loop over all of the types in the symbol table...
Chris Lattner0e851da2002-04-18 20:37:37 +0000131 for (SymbolTable::iterator TI = ST->begin(), TE = ST->end(); TI != TE; ++TI)
132 for (SymbolTable::type_iterator I = TI->second.begin(),
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000133 E = TI->second.end(); I != E; ++I) {
134 Value *V = I->second;
135
136 // Check that there are no void typed values in the symbol table. Values
137 // with a void type cannot be put into symbol tables because they cannot
138 // have names!
139 Assert1(V->getType() != Type::VoidTy,
Chris Lattner412d2772002-04-28 16:06:24 +0000140 "Values with void type are not allowed to have names!", V);
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000141 }
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000142}
143
Chris Lattner0e851da2002-04-18 20:37:37 +0000144
145// visitFunction - Verify that a function is ok.
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000146//
Chris Lattner0e851da2002-04-18 20:37:37 +0000147void Verifier::visitFunction(Function *F) {
148 if (F->isExternal()) return;
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000149
Chris Lattner9713b842002-04-28 16:04:26 +0000150 verifySymbolTable(F->getSymbolTable());
Chris Lattner3e6e3e62002-03-29 19:06:18 +0000151
Chris Lattneraf95e582002-04-13 22:48:46 +0000152 // Check function arguments...
153 const FunctionType *FT = F->getFunctionType();
154 const Function::ArgumentListType &ArgList = F->getArgumentList();
155
156 Assert2(!FT->isVarArg(), "Cannot define varargs functions in LLVM!", F, FT);
157 Assert2(FT->getParamTypes().size() == ArgList.size(),
158 "# formal arguments must match # of arguments for function type!",
159 F, FT);
160
161 // Check that the argument values match the function type for this function...
162 if (FT->getParamTypes().size() == ArgList.size()) {
163 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
164 Assert2(ArgList[i]->getType() == FT->getParamType(i),
165 "Argument value does not match function argument type!",
166 ArgList[i], FT->getParamType(i));
167 }
168
169 // Check the entry node
Chris Lattner0e851da2002-04-18 20:37:37 +0000170 BasicBlock *Entry = F->getEntryNode();
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000171 Assert1(pred_begin(Entry) == pred_end(Entry),
Chris Lattneraf95e582002-04-13 22:48:46 +0000172 "Entry block to function must not have predecessors!", Entry);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000173}
174
175
Chris Lattner0e851da2002-04-18 20:37:37 +0000176// verifyBasicBlock - Verify that a basic block is well formed...
177//
178void Verifier::visitBasicBlock(BasicBlock *BB) {
Chris Lattner412d2772002-04-28 16:06:24 +0000179 Assert1(BB->getTerminator(), "Basic Block does not have terminator!", BB);
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000180
Chris Lattner0e851da2002-04-18 20:37:37 +0000181 // Check that the terminator is ok as well...
Chris Lattner21ea83b2002-04-18 22:11:52 +0000182 if (isa<ReturnInst>(BB->getTerminator())) {
Chris Lattner0e851da2002-04-18 20:37:37 +0000183 Instruction *I = BB->getTerminator();
184 Function *F = I->getParent()->getParent();
185 if (I->getNumOperands() == 0)
186 Assert1(F->getReturnType() == Type::VoidTy,
187 "Function returns no value, but ret instruction found that does!",
188 I);
189 else
190 Assert2(F->getReturnType() == I->getOperand(0)->getType(),
191 "Function return type does not match operand "
192 "type of return inst!", I, F->getReturnType());
193 }
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000194}
195
Chris Lattner0e851da2002-04-18 20:37:37 +0000196
197// visitPHINode - Ensure that a PHI node is well formed.
198void Verifier::visitPHINode(PHINode *PN) {
199 std::vector<BasicBlock*> Preds(pred_begin(PN->getParent()),
200 pred_end(PN->getParent()));
201 // Loop over all of the incoming values, make sure that there are
202 // predecessors for each one...
203 //
204 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
205 // Make sure all of the incoming values are the right types...
206 Assert2(PN->getType() == PN->getIncomingValue(i)->getType(),
207 "PHI node argument type does not agree with PHI node type!",
208 PN, PN->getIncomingValue(i));
209
210 BasicBlock *BB = PN->getIncomingBlock(i);
211 std::vector<BasicBlock*>::iterator PI =
212 find(Preds.begin(), Preds.end(), BB);
213 Assert2(PI != Preds.end(), "PHI node has entry for basic block that"
214 " is not a predecessor!", PN, BB);
Chris Lattner21ea83b2002-04-18 22:11:52 +0000215 Preds.erase(PI);
Chris Lattner0e851da2002-04-18 20:37:37 +0000216 }
217
218 // There should be no entries left in the predecessor list...
219 for (std::vector<BasicBlock*>::iterator I = Preds.begin(),
220 E = Preds.end(); I != E; ++I)
221 Assert2(0, "PHI node does not have entry for a predecessor basic block!",
222 PN, *I);
223
224 visitInstruction(PN);
225}
226
Chris Lattner21ea83b2002-04-18 22:11:52 +0000227void Verifier::visitCallInst(CallInst *CI) {
228 Assert1(isa<PointerType>(CI->getOperand(0)->getType()),
229 "Called function must be a pointer!", CI);
230 PointerType *FPTy = cast<PointerType>(CI->getOperand(0)->getType());
231 Assert1(isa<FunctionType>(FPTy->getElementType()),
232 "Called function is not pointer to function type!", CI);
233}
Chris Lattner0e851da2002-04-18 20:37:37 +0000234
235// visitBinaryOperator - Check that both arguments to the binary operator are
236// of the same type!
237//
238void Verifier::visitBinaryOperator(BinaryOperator *B) {
239 Assert2(B->getOperand(0)->getType() == B->getOperand(1)->getType(),
240 "Both operands to a binary operator are not of the same type!",
241 B->getOperand(0), B->getOperand(1));
242
243 visitInstruction(B);
244}
245
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000246void Verifier::visitGetElementPtrInst(GetElementPtrInst *GEP) {
247 const Type *ElTy =MemAccessInst::getIndexedType(GEP->getOperand(0)->getType(),
248 GEP->copyIndices(), true);
249 Assert1(ElTy, "Invalid indices for GEP pointer type!", GEP);
250 Assert2(PointerType::get(ElTy) == GEP->getType(),
Chris Lattner412d2772002-04-28 16:06:24 +0000251 "GEP is not of right type for indices!", GEP, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000252 visitInstruction(GEP);
253}
254
255void Verifier::visitLoadInst(LoadInst *LI) {
256 const Type *ElTy = LoadInst::getIndexedType(LI->getOperand(0)->getType(),
257 LI->copyIndices());
258 Assert1(ElTy, "Invalid indices for load pointer type!", LI);
259 Assert2(ElTy == LI->getType(),
Chris Lattner412d2772002-04-28 16:06:24 +0000260 "Load is not of right type for indices!", LI, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000261 visitInstruction(LI);
262}
263
264void Verifier::visitStoreInst(StoreInst *SI) {
265 const Type *ElTy = StoreInst::getIndexedType(SI->getOperand(1)->getType(),
266 SI->copyIndices());
267 Assert1(ElTy, "Invalid indices for store pointer type!", SI);
268 Assert2(ElTy == SI->getOperand(0)->getType(),
Chris Lattner412d2772002-04-28 16:06:24 +0000269 "Stored value is not of right type for indices!", SI, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000270 visitInstruction(SI);
271}
272
Chris Lattner0e851da2002-04-18 20:37:37 +0000273
274// verifyInstruction - Verify that a non-terminator instruction is well formed.
275//
276void Verifier::visitInstruction(Instruction *I) {
277 assert(I->getParent() && "Instruction not embedded in basic block!");
278
279 // Check that all uses of the instruction, if they are instructions
280 // themselves, actually have parent basic blocks. If the use is not an
281 // instruction, it is an error!
282 //
283 for (User::use_iterator UI = I->use_begin(), UE = I->use_end();
284 UI != UE; ++UI) {
285 Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!",
286 *UI);
Chris Lattner21ea83b2002-04-18 22:11:52 +0000287 Instruction *Used = cast<Instruction>(*UI);
288 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
289 " embeded in a basic block!", I, Used);
Chris Lattner0e851da2002-04-18 20:37:37 +0000290 }
291
292 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
293 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
294 UI != UE; ++UI)
295 Assert1(*UI != (User*)I,
296 "Only PHI nodes may reference their own value!", I);
297 }
298
299 Assert1(I->getType() != Type::VoidTy || !I->hasName(),
300 "Instruction has a name, but provides a void value!", I);
301}
302
303
304//===----------------------------------------------------------------------===//
305// Implement the public interfaces to this file...
306//===----------------------------------------------------------------------===//
307
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000308Pass *createVerifierPass() {
Chris Lattner0e851da2002-04-18 20:37:37 +0000309 return new Verifier();
310}
311
312bool verifyFunction(const Function *F) {
313 Verifier V;
314 V.visit((Function*)F);
315 return V.Broken;
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000316}
317
318// verifyModule - Check a module for errors, printing messages on stderr.
319// Return true if the module is corrupt.
320//
Chris Lattnerb67f7322002-02-26 21:45:33 +0000321bool verifyModule(const Module *M) {
Chris Lattner0e851da2002-04-18 20:37:37 +0000322 Verifier V;
323 V.run((Module*)M);
324 return V.Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000325}