blob: d09678c7392648f549902fde8ac896346ce873a6 [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 Lattner338a4622002-05-08 19:49:50 +000033// * Function call argument types match the function prototype
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000034// * All other things that are tested by asserts spread about the code...
Chris Lattner2f7c9632001-06-06 20:29:01 +000035//
36//===----------------------------------------------------------------------===//
37
38#include "llvm/Analysis/Verifier.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000039#include "llvm/Pass.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000040#include "llvm/Module.h"
Chris Lattneraf95e582002-04-13 22:48:46 +000041#include "llvm/DerivedTypes.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000042#include "llvm/iPHINode.h"
Chris Lattner486302a2002-04-12 18:20:49 +000043#include "llvm/iTerminators.h"
Chris Lattner21ea83b2002-04-18 22:11:52 +000044#include "llvm/iOther.h"
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000045#include "llvm/iMemory.h"
Chris Lattneraf95e582002-04-13 22:48:46 +000046#include "llvm/Argument.h"
Chris Lattnerfbf5be52002-03-15 20:25:09 +000047#include "llvm/SymbolTable.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000048#include "llvm/Support/CFG.h"
Chris Lattner0e851da2002-04-18 20:37:37 +000049#include "llvm/Support/InstVisitor.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000050#include "Support/STLExtras.h"
51#include <algorithm>
Chris Lattner7f74a562002-01-20 22:54:45 +000052
Chris Lattner0e851da2002-04-18 20:37:37 +000053namespace { // Anonymous namespace for class
Chris Lattner2f7c9632001-06-06 20:29:01 +000054
Chris Lattnerc8e66542002-04-27 06:56:12 +000055 struct Verifier : public FunctionPass, InstVisitor<Verifier> {
Chris Lattner0e851da2002-04-18 20:37:37 +000056 bool Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +000057
Chris Lattner0e851da2002-04-18 20:37:37 +000058 Verifier() : Broken(false) {}
Chris Lattner2f7c9632001-06-06 20:29:01 +000059
Chris Lattner37104aa2002-04-29 14:57:45 +000060 virtual const char *getPassName() const { return "Module Verifier"; }
61
Chris Lattner0e851da2002-04-18 20:37:37 +000062 bool doInitialization(Module *M) {
63 verifySymbolTable(M->getSymbolTable());
64 return false;
65 }
66
Chris Lattnerc8e66542002-04-27 06:56:12 +000067 bool runOnFunction(Function *F) {
Chris Lattner0e851da2002-04-18 20:37:37 +000068 visit(F);
69 return false;
70 }
71
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000072 bool doFinalization(Module *M) {
Chris Lattner9713b842002-04-28 16:04:26 +000073 // Scan through, checking all of the external function's linkage now...
74 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
75 if ((*I)->isExternal() && (*I)->hasInternalLinkage())
Chris Lattner412d2772002-04-28 16:06:24 +000076 CheckFailed("Function Declaration has Internal Linkage!", (*I));
Chris Lattner9713b842002-04-28 16:04:26 +000077
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000078 if (Broken) {
79 cerr << "Broken module found, compilation aborted!\n";
80 abort();
81 }
82 return false;
83 }
84
Chris Lattnerf12cc842002-04-28 21:27:06 +000085 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
86 AU.setPreservesAll();
87 }
88
Chris Lattner0e851da2002-04-18 20:37:37 +000089 // Verification methods...
90 void verifySymbolTable(SymbolTable *ST);
91 void visitFunction(Function *F);
92 void visitBasicBlock(BasicBlock *BB);
93 void visitPHINode(PHINode *PN);
94 void visitBinaryOperator(BinaryOperator *B);
Chris Lattner21ea83b2002-04-18 22:11:52 +000095 void visitCallInst(CallInst *CI);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000096 void visitGetElementPtrInst(GetElementPtrInst *GEP);
97 void visitLoadInst(LoadInst *LI);
98 void visitStoreInst(StoreInst *SI);
Chris Lattner0e851da2002-04-18 20:37:37 +000099 void visitInstruction(Instruction *I);
100
101 // CheckFailed - A check failed, so print out the condition and the message
102 // that failed. This provides a nice place to put a breakpoint if you want
103 // to see why something is not correct.
104 //
Chris Lattner412d2772002-04-28 16:06:24 +0000105 inline void CheckFailed(const std::string &Message,
Chris Lattner0e851da2002-04-18 20:37:37 +0000106 const Value *V1 = 0, const Value *V2 = 0) {
107 std::cerr << Message << "\n";
108 if (V1) { std::cerr << V1 << "\n"; }
109 if (V2) { std::cerr << V2 << "\n"; }
110 Broken = true;
111 }
112 };
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000113}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000114
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000115// Assert - We know that cond should be true, if not print an error message.
116#define Assert(C, M) \
Chris Lattner412d2772002-04-28 16:06:24 +0000117 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000118#define Assert1(C, M, V1) \
Chris Lattner412d2772002-04-28 16:06:24 +0000119 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000120#define Assert2(C, M, V1, V2) \
Chris Lattner412d2772002-04-28 16:06:24 +0000121 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000122
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000123
Chris Lattneraf95e582002-04-13 22:48:46 +0000124// verifySymbolTable - Verify that a function or module symbol table is ok
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000125//
Chris Lattner0e851da2002-04-18 20:37:37 +0000126void Verifier::verifySymbolTable(SymbolTable *ST) {
127 if (ST == 0) return; // No symbol table to process
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000129 // Loop over all of the types in the symbol table...
Chris Lattner0e851da2002-04-18 20:37:37 +0000130 for (SymbolTable::iterator TI = ST->begin(), TE = ST->end(); TI != TE; ++TI)
131 for (SymbolTable::type_iterator I = TI->second.begin(),
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000132 E = TI->second.end(); I != E; ++I) {
133 Value *V = I->second;
134
135 // Check that there are no void typed values in the symbol table. Values
136 // with a void type cannot be put into symbol tables because they cannot
137 // have names!
138 Assert1(V->getType() != Type::VoidTy,
Chris Lattner412d2772002-04-28 16:06:24 +0000139 "Values with void type are not allowed to have names!", V);
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000140 }
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000141}
142
Chris Lattner0e851da2002-04-18 20:37:37 +0000143
144// visitFunction - Verify that a function is ok.
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000145//
Chris Lattner0e851da2002-04-18 20:37:37 +0000146void Verifier::visitFunction(Function *F) {
147 if (F->isExternal()) return;
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000148
Chris Lattner9713b842002-04-28 16:04:26 +0000149 verifySymbolTable(F->getSymbolTable());
Chris Lattner3e6e3e62002-03-29 19:06:18 +0000150
Chris Lattneraf95e582002-04-13 22:48:46 +0000151 // Check function arguments...
152 const FunctionType *FT = F->getFunctionType();
153 const Function::ArgumentListType &ArgList = F->getArgumentList();
154
155 Assert2(!FT->isVarArg(), "Cannot define varargs functions in LLVM!", F, FT);
156 Assert2(FT->getParamTypes().size() == ArgList.size(),
157 "# formal arguments must match # of arguments for function type!",
158 F, FT);
159
160 // Check that the argument values match the function type for this function...
161 if (FT->getParamTypes().size() == ArgList.size()) {
162 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
163 Assert2(ArgList[i]->getType() == FT->getParamType(i),
164 "Argument value does not match function argument type!",
165 ArgList[i], FT->getParamType(i));
166 }
167
168 // Check the entry node
Chris Lattner0e851da2002-04-18 20:37:37 +0000169 BasicBlock *Entry = F->getEntryNode();
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000170 Assert1(pred_begin(Entry) == pred_end(Entry),
Chris Lattneraf95e582002-04-13 22:48:46 +0000171 "Entry block to function must not have predecessors!", Entry);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000172}
173
174
Chris Lattner0e851da2002-04-18 20:37:37 +0000175// verifyBasicBlock - Verify that a basic block is well formed...
176//
177void Verifier::visitBasicBlock(BasicBlock *BB) {
Chris Lattner412d2772002-04-28 16:06:24 +0000178 Assert1(BB->getTerminator(), "Basic Block does not have terminator!", BB);
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000179
Chris Lattner0e851da2002-04-18 20:37:37 +0000180 // Check that the terminator is ok as well...
Chris Lattner21ea83b2002-04-18 22:11:52 +0000181 if (isa<ReturnInst>(BB->getTerminator())) {
Chris Lattner0e851da2002-04-18 20:37:37 +0000182 Instruction *I = BB->getTerminator();
183 Function *F = I->getParent()->getParent();
184 if (I->getNumOperands() == 0)
185 Assert1(F->getReturnType() == Type::VoidTy,
186 "Function returns no value, but ret instruction found that does!",
187 I);
188 else
189 Assert2(F->getReturnType() == I->getOperand(0)->getType(),
190 "Function return type does not match operand "
191 "type of return inst!", I, F->getReturnType());
192 }
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000193}
194
Chris Lattner0e851da2002-04-18 20:37:37 +0000195
196// visitPHINode - Ensure that a PHI node is well formed.
197void Verifier::visitPHINode(PHINode *PN) {
198 std::vector<BasicBlock*> Preds(pred_begin(PN->getParent()),
199 pred_end(PN->getParent()));
200 // Loop over all of the incoming values, make sure that there are
201 // predecessors for each one...
202 //
203 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
204 // Make sure all of the incoming values are the right types...
205 Assert2(PN->getType() == PN->getIncomingValue(i)->getType(),
206 "PHI node argument type does not agree with PHI node type!",
207 PN, PN->getIncomingValue(i));
208
209 BasicBlock *BB = PN->getIncomingBlock(i);
210 std::vector<BasicBlock*>::iterator PI =
211 find(Preds.begin(), Preds.end(), BB);
212 Assert2(PI != Preds.end(), "PHI node has entry for basic block that"
213 " is not a predecessor!", PN, BB);
Chris Lattner21ea83b2002-04-18 22:11:52 +0000214 Preds.erase(PI);
Chris Lattner0e851da2002-04-18 20:37:37 +0000215 }
216
217 // There should be no entries left in the predecessor list...
218 for (std::vector<BasicBlock*>::iterator I = Preds.begin(),
219 E = Preds.end(); I != E; ++I)
220 Assert2(0, "PHI node does not have entry for a predecessor basic block!",
221 PN, *I);
222
223 visitInstruction(PN);
224}
225
Chris Lattner21ea83b2002-04-18 22:11:52 +0000226void Verifier::visitCallInst(CallInst *CI) {
227 Assert1(isa<PointerType>(CI->getOperand(0)->getType()),
228 "Called function must be a pointer!", CI);
229 PointerType *FPTy = cast<PointerType>(CI->getOperand(0)->getType());
230 Assert1(isa<FunctionType>(FPTy->getElementType()),
231 "Called function is not pointer to function type!", CI);
Chris Lattner338a4622002-05-08 19:49:50 +0000232
233 FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
234
235 // Verify that the correct number of arguments are being passed
236 if (FTy->isVarArg())
237 Assert1(CI->getNumOperands()-1 >= FTy->getNumParams(),
238 "Called function requires more parameters than were provided!", CI);
239 else
240 Assert1(CI->getNumOperands()-1 == FTy->getNumParams(),
241 "Incorrect number of arguments passed to called function!", CI);
242
243 // Verify that all arguments to the call match the function type...
244 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
245 Assert2(CI->getOperand(i+1)->getType() == FTy->getParamType(i),
246 "Call parameter type does not match function signature!",
247 CI->getOperand(i+1), FTy->getParamType(i));
Chris Lattner21ea83b2002-04-18 22:11:52 +0000248}
Chris Lattner0e851da2002-04-18 20:37:37 +0000249
250// visitBinaryOperator - Check that both arguments to the binary operator are
251// of the same type!
252//
253void Verifier::visitBinaryOperator(BinaryOperator *B) {
254 Assert2(B->getOperand(0)->getType() == B->getOperand(1)->getType(),
255 "Both operands to a binary operator are not of the same type!",
256 B->getOperand(0), B->getOperand(1));
257
258 visitInstruction(B);
259}
260
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000261void Verifier::visitGetElementPtrInst(GetElementPtrInst *GEP) {
262 const Type *ElTy =MemAccessInst::getIndexedType(GEP->getOperand(0)->getType(),
263 GEP->copyIndices(), true);
264 Assert1(ElTy, "Invalid indices for GEP pointer type!", GEP);
265 Assert2(PointerType::get(ElTy) == GEP->getType(),
Chris Lattner412d2772002-04-28 16:06:24 +0000266 "GEP is not of right type for indices!", GEP, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000267 visitInstruction(GEP);
268}
269
270void Verifier::visitLoadInst(LoadInst *LI) {
271 const Type *ElTy = LoadInst::getIndexedType(LI->getOperand(0)->getType(),
272 LI->copyIndices());
273 Assert1(ElTy, "Invalid indices for load pointer type!", LI);
274 Assert2(ElTy == LI->getType(),
Chris Lattner412d2772002-04-28 16:06:24 +0000275 "Load is not of right type for indices!", LI, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000276 visitInstruction(LI);
277}
278
279void Verifier::visitStoreInst(StoreInst *SI) {
280 const Type *ElTy = StoreInst::getIndexedType(SI->getOperand(1)->getType(),
281 SI->copyIndices());
282 Assert1(ElTy, "Invalid indices for store pointer type!", SI);
283 Assert2(ElTy == SI->getOperand(0)->getType(),
Chris Lattner412d2772002-04-28 16:06:24 +0000284 "Stored value is not of right type for indices!", SI, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000285 visitInstruction(SI);
286}
287
Chris Lattner0e851da2002-04-18 20:37:37 +0000288
289// verifyInstruction - Verify that a non-terminator instruction is well formed.
290//
291void Verifier::visitInstruction(Instruction *I) {
292 assert(I->getParent() && "Instruction not embedded in basic block!");
293
294 // Check that all uses of the instruction, if they are instructions
295 // themselves, actually have parent basic blocks. If the use is not an
296 // instruction, it is an error!
297 //
298 for (User::use_iterator UI = I->use_begin(), UE = I->use_end();
299 UI != UE; ++UI) {
300 Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!",
301 *UI);
Chris Lattner21ea83b2002-04-18 22:11:52 +0000302 Instruction *Used = cast<Instruction>(*UI);
303 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
304 " embeded in a basic block!", I, Used);
Chris Lattner0e851da2002-04-18 20:37:37 +0000305 }
306
307 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
308 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
309 UI != UE; ++UI)
310 Assert1(*UI != (User*)I,
311 "Only PHI nodes may reference their own value!", I);
312 }
313
314 Assert1(I->getType() != Type::VoidTy || !I->hasName(),
315 "Instruction has a name, but provides a void value!", I);
316}
317
318
319//===----------------------------------------------------------------------===//
320// Implement the public interfaces to this file...
321//===----------------------------------------------------------------------===//
322
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000323Pass *createVerifierPass() {
Chris Lattner0e851da2002-04-18 20:37:37 +0000324 return new Verifier();
325}
326
327bool verifyFunction(const Function *F) {
328 Verifier V;
329 V.visit((Function*)F);
330 return V.Broken;
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000331}
332
333// verifyModule - Check a module for errors, printing messages on stderr.
334// Return true if the module is corrupt.
335//
Chris Lattnerb67f7322002-02-26 21:45:33 +0000336bool verifyModule(const Module *M) {
Chris Lattner0e851da2002-04-18 20:37:37 +0000337 Verifier V;
338 V.run((Module*)M);
339 return V.Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000340}