blob: e34c484b4028bf2cc6d825f6d0d1dc6d32192e6b [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 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!]
Chris Lattner7704e9f2002-03-14 16:53:48 +000019// * Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad
Chris Lattnerd02f08d2002-02-20 17:55:43 +000020// * PHI nodes must have an entry for each predecessor, with no extras.
Chris Lattner0e851da2002-04-18 20:37:37 +000021// . All basic blocks should only end with terminator insts, not contain them
Chris Lattner3e6e3e62002-03-29 19:06:18 +000022// * The entry node to a function must not have predecessors
Chris Lattnerd02f08d2002-02-20 17:55:43 +000023// * All Instructions must be embeded into a basic block
Chris Lattner68289f02001-11-06 22:53:11 +000024// . Verify that none of the Value getType()'s are null.
Chris Lattner3e6e3e62002-03-29 19:06:18 +000025// . Function's cannot take a void typed parameter
Chris Lattneraf95e582002-04-13 22:48:46 +000026// * Verify that a function's argument list agrees with it's declared type.
Chris Lattner2f7c9632001-06-06 20:29:01 +000027// . Verify that arrays and structures have fixed elements: No unsized arrays.
Chris Lattnerfbf5be52002-03-15 20:25:09 +000028// * It is illegal to specify a name for a void value.
Chris Lattner3e6e3e62002-03-29 19:06:18 +000029// * It is illegal to have a internal function that is just a declaration
Chris Lattner486302a2002-04-12 18:20:49 +000030// * It is illegal to have a ret instruction that returns a value that does not
31// agree with the function return value type.
Chris Lattnerd02f08d2002-02-20 17:55:43 +000032// . All other things that are tested by asserts spread about the code...
Chris Lattner2f7c9632001-06-06 20:29:01 +000033//
34//===----------------------------------------------------------------------===//
35
36#include "llvm/Analysis/Verifier.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000037#include "llvm/Pass.h"
Chris Lattner3e6e3e62002-03-29 19:06:18 +000038#include "llvm/Function.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000039#include "llvm/Module.h"
40#include "llvm/BasicBlock.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 Lattneraf95e582002-04-13 22:48:46 +000044#include "llvm/Argument.h"
Chris Lattnerfbf5be52002-03-15 20:25:09 +000045#include "llvm/SymbolTable.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000046#include "llvm/Support/CFG.h"
Chris Lattner0e851da2002-04-18 20:37:37 +000047#include "llvm/Support/InstVisitor.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000048#include "Support/STLExtras.h"
49#include <algorithm>
Chris Lattner7f74a562002-01-20 22:54:45 +000050
Chris Lattner0e851da2002-04-18 20:37:37 +000051namespace { // Anonymous namespace for class
Chris Lattner2f7c9632001-06-06 20:29:01 +000052
Chris Lattner0e851da2002-04-18 20:37:37 +000053 struct Verifier : public MethodPass, InstVisitor<Verifier> {
54 bool Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +000055
Chris Lattner0e851da2002-04-18 20:37:37 +000056 Verifier() : Broken(false) {}
Chris Lattner2f7c9632001-06-06 20:29:01 +000057
Chris Lattner0e851da2002-04-18 20:37:37 +000058 bool doInitialization(Module *M) {
59 verifySymbolTable(M->getSymbolTable());
60 return false;
61 }
62
63 bool runOnMethod(Function *F) {
64 visit(F);
65 return false;
66 }
67
68 // Verification methods...
69 void verifySymbolTable(SymbolTable *ST);
70 void visitFunction(Function *F);
71 void visitBasicBlock(BasicBlock *BB);
72 void visitPHINode(PHINode *PN);
73 void visitBinaryOperator(BinaryOperator *B);
74 void visitInstruction(Instruction *I);
75
76 // CheckFailed - A check failed, so print out the condition and the message
77 // that failed. This provides a nice place to put a breakpoint if you want
78 // to see why something is not correct.
79 //
80 inline void CheckFailed(const char *Cond, const std::string &Message,
81 const Value *V1 = 0, const Value *V2 = 0) {
82 std::cerr << Message << "\n";
83 if (V1) { std::cerr << V1 << "\n"; }
84 if (V2) { std::cerr << V2 << "\n"; }
85 Broken = true;
86 }
87 };
Chris Lattnerd02f08d2002-02-20 17:55:43 +000088}
Chris Lattner2f7c9632001-06-06 20:29:01 +000089
Chris Lattnerd02f08d2002-02-20 17:55:43 +000090// Assert - We know that cond should be true, if not print an error message.
91#define Assert(C, M) \
Chris Lattner0e851da2002-04-18 20:37:37 +000092 do { if (!(C)) { CheckFailed(#C, M); } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +000093#define Assert1(C, M, V1) \
Chris Lattner0e851da2002-04-18 20:37:37 +000094 do { if (!(C)) { CheckFailed(#C, M, V1); } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +000095#define Assert2(C, M, V1, V2) \
Chris Lattner0e851da2002-04-18 20:37:37 +000096 do { if (!(C)) { CheckFailed(#C, M, V1, V2); } } while (0)
Chris Lattner2f7c9632001-06-06 20:29:01 +000097
Chris Lattnerd02f08d2002-02-20 17:55:43 +000098
Chris Lattneraf95e582002-04-13 22:48:46 +000099// verifySymbolTable - Verify that a function or module symbol table is ok
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000100//
Chris Lattner0e851da2002-04-18 20:37:37 +0000101void Verifier::verifySymbolTable(SymbolTable *ST) {
102 if (ST == 0) return; // No symbol table to process
Chris Lattner2f7c9632001-06-06 20:29:01 +0000103
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000104 // Loop over all of the types in the symbol table...
Chris Lattner0e851da2002-04-18 20:37:37 +0000105 for (SymbolTable::iterator TI = ST->begin(), TE = ST->end(); TI != TE; ++TI)
106 for (SymbolTable::type_iterator I = TI->second.begin(),
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000107 E = TI->second.end(); I != E; ++I) {
108 Value *V = I->second;
109
110 // Check that there are no void typed values in the symbol table. Values
111 // with a void type cannot be put into symbol tables because they cannot
112 // have names!
113 Assert1(V->getType() != Type::VoidTy,
114 "Values with void type are not allowed to have names!\n", V);
115 }
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000116}
117
Chris Lattner0e851da2002-04-18 20:37:37 +0000118
119// visitFunction - Verify that a function is ok.
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000120//
Chris Lattner0e851da2002-04-18 20:37:37 +0000121void Verifier::visitFunction(Function *F) {
122 if (F->isExternal()) return;
123 verifySymbolTable(F->getSymbolTable());
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000124
Chris Lattneraf95e582002-04-13 22:48:46 +0000125 // Check linkage of function...
Chris Lattner3e6e3e62002-03-29 19:06:18 +0000126 Assert1(!F->isExternal() || F->hasExternalLinkage(),
127 "Function cannot be an 'internal' 'declare'ation!", F);
128
Chris Lattneraf95e582002-04-13 22:48:46 +0000129 // Check function arguments...
130 const FunctionType *FT = F->getFunctionType();
131 const Function::ArgumentListType &ArgList = F->getArgumentList();
132
133 Assert2(!FT->isVarArg(), "Cannot define varargs functions in LLVM!", F, FT);
134 Assert2(FT->getParamTypes().size() == ArgList.size(),
135 "# formal arguments must match # of arguments for function type!",
136 F, FT);
137
138 // Check that the argument values match the function type for this function...
139 if (FT->getParamTypes().size() == ArgList.size()) {
140 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
141 Assert2(ArgList[i]->getType() == FT->getParamType(i),
142 "Argument value does not match function argument type!",
143 ArgList[i], FT->getParamType(i));
144 }
145
146 // Check the entry node
Chris Lattner0e851da2002-04-18 20:37:37 +0000147 BasicBlock *Entry = F->getEntryNode();
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000148 Assert1(pred_begin(Entry) == pred_end(Entry),
Chris Lattneraf95e582002-04-13 22:48:46 +0000149 "Entry block to function must not have predecessors!", Entry);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000150}
151
152
Chris Lattner0e851da2002-04-18 20:37:37 +0000153// verifyBasicBlock - Verify that a basic block is well formed...
154//
155void Verifier::visitBasicBlock(BasicBlock *BB) {
156 Assert1(BB->getTerminator(), "Basic Block does not have terminator!\n", BB);
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000157
Chris Lattner0e851da2002-04-18 20:37:37 +0000158 // Check that the terminator is ok as well...
159 if (BB->getTerminator() && isa<ReturnInst>(BB->getTerminator())) {
160 Instruction *I = BB->getTerminator();
161 Function *F = I->getParent()->getParent();
162 if (I->getNumOperands() == 0)
163 Assert1(F->getReturnType() == Type::VoidTy,
164 "Function returns no value, but ret instruction found that does!",
165 I);
166 else
167 Assert2(F->getReturnType() == I->getOperand(0)->getType(),
168 "Function return type does not match operand "
169 "type of return inst!", I, F->getReturnType());
170 }
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000171}
172
Chris Lattner0e851da2002-04-18 20:37:37 +0000173
174// visitPHINode - Ensure that a PHI node is well formed.
175void Verifier::visitPHINode(PHINode *PN) {
176 std::vector<BasicBlock*> Preds(pred_begin(PN->getParent()),
177 pred_end(PN->getParent()));
178 // Loop over all of the incoming values, make sure that there are
179 // predecessors for each one...
180 //
181 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
182 // Make sure all of the incoming values are the right types...
183 Assert2(PN->getType() == PN->getIncomingValue(i)->getType(),
184 "PHI node argument type does not agree with PHI node type!",
185 PN, PN->getIncomingValue(i));
186
187 BasicBlock *BB = PN->getIncomingBlock(i);
188 std::vector<BasicBlock*>::iterator PI =
189 find(Preds.begin(), Preds.end(), BB);
190 Assert2(PI != Preds.end(), "PHI node has entry for basic block that"
191 " is not a predecessor!", PN, BB);
192 if (PI != Preds.end()) Preds.erase(PI);
193 }
194
195 // There should be no entries left in the predecessor list...
196 for (std::vector<BasicBlock*>::iterator I = Preds.begin(),
197 E = Preds.end(); I != E; ++I)
198 Assert2(0, "PHI node does not have entry for a predecessor basic block!",
199 PN, *I);
200
201 visitInstruction(PN);
202}
203
204
205// visitBinaryOperator - Check that both arguments to the binary operator are
206// of the same type!
207//
208void Verifier::visitBinaryOperator(BinaryOperator *B) {
209 Assert2(B->getOperand(0)->getType() == B->getOperand(1)->getType(),
210 "Both operands to a binary operator are not of the same type!",
211 B->getOperand(0), B->getOperand(1));
212
213 visitInstruction(B);
214}
215
216
217// verifyInstruction - Verify that a non-terminator instruction is well formed.
218//
219void Verifier::visitInstruction(Instruction *I) {
220 assert(I->getParent() && "Instruction not embedded in basic block!");
221
222 // Check that all uses of the instruction, if they are instructions
223 // themselves, actually have parent basic blocks. If the use is not an
224 // instruction, it is an error!
225 //
226 for (User::use_iterator UI = I->use_begin(), UE = I->use_end();
227 UI != UE; ++UI) {
228 Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!",
229 *UI);
230 if (Instruction *Used = dyn_cast<Instruction>(*UI))
231 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
232 " embeded in a basic block!", I, Used);
233 }
234
235 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
236 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
237 UI != UE; ++UI)
238 Assert1(*UI != (User*)I,
239 "Only PHI nodes may reference their own value!", I);
240 }
241
242 Assert1(I->getType() != Type::VoidTy || !I->hasName(),
243 "Instruction has a name, but provides a void value!", I);
244}
245
246
247//===----------------------------------------------------------------------===//
248// Implement the public interfaces to this file...
249//===----------------------------------------------------------------------===//
250
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000251Pass *createVerifierPass() {
Chris Lattner0e851da2002-04-18 20:37:37 +0000252 return new Verifier();
253}
254
255bool verifyFunction(const Function *F) {
256 Verifier V;
257 V.visit((Function*)F);
258 return V.Broken;
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000259}
260
261// verifyModule - Check a module for errors, printing messages on stderr.
262// Return true if the module is corrupt.
263//
Chris Lattnerb67f7322002-02-26 21:45:33 +0000264bool verifyModule(const Module *M) {
Chris Lattner0e851da2002-04-18 20:37:37 +0000265 Verifier V;
266 V.run((Module*)M);
267 return V.Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000268}