Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==// |
| 2 | // |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 3 | // This file defines the function verifier interface, that can be used for some |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 4 | // 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 Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 12 | // * Both of a binary operator's parameters are the same type |
Chris Lattner | 68289f0 | 2001-11-06 22:53:11 +0000 | [diff] [blame] | 13 | // . Verify that arithmetic and other things are only performed on first class |
| 14 | // types. No adding structures or arrays. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | // . 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 Lattner | 7704e9f | 2002-03-14 16:53:48 +0000 | [diff] [blame] | 19 | // * Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 20 | // * PHI nodes must have an entry for each predecessor, with no extras. |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 21 | // . All basic blocks should only end with terminator insts, not contain them |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 22 | // * The entry node to a function must not have predecessors |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 23 | // * All Instructions must be embeded into a basic block |
Chris Lattner | 68289f0 | 2001-11-06 22:53:11 +0000 | [diff] [blame] | 24 | // . Verify that none of the Value getType()'s are null. |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 25 | // . Function's cannot take a void typed parameter |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 26 | // * Verify that a function's argument list agrees with it's declared type. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 27 | // . Verify that arrays and structures have fixed elements: No unsized arrays. |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 28 | // * It is illegal to specify a name for a void value. |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 29 | // * It is illegal to have a internal function that is just a declaration |
Chris Lattner | 486302a | 2002-04-12 18:20:49 +0000 | [diff] [blame] | 30 | // * It is illegal to have a ret instruction that returns a value that does not |
| 31 | // agree with the function return value type. |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 32 | // . All other things that are tested by asserts spread about the code... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | // |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 37 | #include "llvm/Pass.h" |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 38 | #include "llvm/Function.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | #include "llvm/Module.h" |
| 40 | #include "llvm/BasicBlock.h" |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 41 | #include "llvm/DerivedTypes.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 42 | #include "llvm/iPHINode.h" |
Chris Lattner | 486302a | 2002-04-12 18:20:49 +0000 | [diff] [blame] | 43 | #include "llvm/iTerminators.h" |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 44 | #include "llvm/Argument.h" |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 45 | #include "llvm/SymbolTable.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CFG.h" |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 47 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 48 | #include "Support/STLExtras.h" |
| 49 | #include <algorithm> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 51 | namespace { // Anonymous namespace for class |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 53 | struct Verifier : public MethodPass, InstVisitor<Verifier> { |
| 54 | bool Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 56 | Verifier() : Broken(false) {} |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 58 | 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 Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 88 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 89 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 90 | // Assert - We know that cond should be true, if not print an error message. |
| 91 | #define Assert(C, M) \ |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 92 | do { if (!(C)) { CheckFailed(#C, M); } } while (0) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 93 | #define Assert1(C, M, V1) \ |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 94 | do { if (!(C)) { CheckFailed(#C, M, V1); } } while (0) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 95 | #define Assert2(C, M, V1, V2) \ |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 96 | do { if (!(C)) { CheckFailed(#C, M, V1, V2); } } while (0) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 97 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 98 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 99 | // verifySymbolTable - Verify that a function or module symbol table is ok |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 100 | // |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 101 | void Verifier::verifySymbolTable(SymbolTable *ST) { |
| 102 | if (ST == 0) return; // No symbol table to process |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 103 | |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 104 | // Loop over all of the types in the symbol table... |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 105 | for (SymbolTable::iterator TI = ST->begin(), TE = ST->end(); TI != TE; ++TI) |
| 106 | for (SymbolTable::type_iterator I = TI->second.begin(), |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 107 | 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 Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 118 | |
| 119 | // visitFunction - Verify that a function is ok. |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 120 | // |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 121 | void Verifier::visitFunction(Function *F) { |
| 122 | if (F->isExternal()) return; |
| 123 | verifySymbolTable(F->getSymbolTable()); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 124 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 125 | // Check linkage of function... |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 126 | Assert1(!F->isExternal() || F->hasExternalLinkage(), |
| 127 | "Function cannot be an 'internal' 'declare'ation!", F); |
| 128 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 129 | // 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 Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 147 | BasicBlock *Entry = F->getEntryNode(); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 148 | Assert1(pred_begin(Entry) == pred_end(Entry), |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 149 | "Entry block to function must not have predecessors!", Entry); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 153 | // verifyBasicBlock - Verify that a basic block is well formed... |
| 154 | // |
| 155 | void Verifier::visitBasicBlock(BasicBlock *BB) { |
| 156 | Assert1(BB->getTerminator(), "Basic Block does not have terminator!\n", BB); |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 158 | // 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 Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 173 | |
| 174 | // visitPHINode - Ensure that a PHI node is well formed. |
| 175 | void 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 | // |
| 208 | void 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 | // |
| 219 | void 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 Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 251 | Pass *createVerifierPass() { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 252 | return new Verifier(); |
| 253 | } |
| 254 | |
| 255 | bool verifyFunction(const Function *F) { |
| 256 | Verifier V; |
| 257 | V.visit((Function*)F); |
| 258 | return V.Broken; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // verifyModule - Check a module for errors, printing messages on stderr. |
| 262 | // Return true if the module is corrupt. |
| 263 | // |
Chris Lattner | b67f732 | 2002-02-26 21:45:33 +0000 | [diff] [blame] | 264 | bool verifyModule(const Module *M) { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame^] | 265 | Verifier V; |
| 266 | V.run((Module*)M); |
| 267 | return V.Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 268 | } |