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... |
| 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!] |
| 19 | // . Right now 'add bool 0, 0' is valid. This isn't particularly good. |
Chris Lattner | 7704e9f | 2002-03-14 16:53:48 +0000 | [diff] [blame] | 20 | // * 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] | 21 | // * 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 Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 23 | // * The entry node to a function must not have predecessors |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 24 | // * All Instructions must be embeded into a basic block |
Chris Lattner | 68289f0 | 2001-11-06 22:53:11 +0000 | [diff] [blame] | 25 | // . Verify that none of the Value getType()'s are null. |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 26 | // . Function's cannot take a void typed parameter |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 27 | // * 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] | 28 | // . Verify that arrays and structures have fixed elements: No unsized arrays. |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 29 | // * It is illegal to specify a name for a void value. |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 30 | // * 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] | 31 | // * It is illegal to have a ret instruction that returns a value that does not |
| 32 | // agree with the function return value type. |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 33 | // . All other things that are tested by asserts spread about the code... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | // |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 38 | #include "llvm/Pass.h" |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 39 | #include "llvm/Function.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 40 | #include "llvm/Module.h" |
| 41 | #include "llvm/BasicBlock.h" |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 42 | #include "llvm/DerivedTypes.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 43 | #include "llvm/iPHINode.h" |
Chris Lattner | 486302a | 2002-04-12 18:20:49 +0000 | [diff] [blame] | 44 | #include "llvm/iTerminators.h" |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 45 | #include "llvm/Argument.h" |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 46 | #include "llvm/SymbolTable.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 47 | #include "llvm/Support/CFG.h" |
| 48 | #include "Support/STLExtras.h" |
| 49 | #include <algorithm> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 50 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 51 | #if 0 |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 52 | #define t(x) (1 << (unsigned)Type::x) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 53 | #define SignedIntegralTypes (t(SByteTyID) | t(ShortTyID) | \ |
| 54 | t(IntTyID) | t(LongTyID)) |
| 55 | static long UnsignedIntegralTypes = t(UByteTyID) | t(UShortTyID) | |
| 56 | t(UIntTyID) | t(ULongTyID); |
| 57 | static const long FloatingPointTypes = t(FloatTyID) | t(DoubleTyID); |
| 58 | |
| 59 | static const long IntegralTypes = SignedIntegralTypes | UnsignedIntegralTypes; |
| 60 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | static long ValidTypes[Type::FirstDerivedTyID] = { |
| 62 | [(unsigned)Instruction::UnaryOps::Not] t(BoolTyID), |
| 63 | //[Instruction::UnaryOps::Add] = IntegralTypes, |
| 64 | // [Instruction::Sub] = IntegralTypes, |
| 65 | }; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 66 | #undef t |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 69 | // 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 Lattner | 43373a6 | 2002-02-24 23:01:21 +0000 | [diff] [blame] | 73 | static inline void CheckFailed(const char *Cond, const std::string &Message, |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 74 | const Value *V1 = 0, const Value *V2 = 0) { |
| 75 | std::cerr << Message << "\n"; |
Chris Lattner | 486302a | 2002-04-12 18:20:49 +0000 | [diff] [blame] | 76 | if (V1) { std::cerr << V1 << "\n"; } |
| 77 | if (V2) { std::cerr << V2 << "\n"; } |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 78 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 79 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 80 | // 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 Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 87 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 88 | |
| 89 | // verifyInstruction - Verify that a non-terminator instruction is well formed. |
| 90 | // |
| 91 | static 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 Lattner | 43373a6 | 2002-02-24 23:01:21 +0000 | [diff] [blame] | 109 | std::vector<const BasicBlock*> Preds(pred_begin(I->getParent()), |
| 110 | pred_end(I->getParent())); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 111 | // 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 Lattner | 43373a6 | 2002-02-24 23:01:21 +0000 | [diff] [blame] | 116 | std::vector<const BasicBlock*>::iterator PI = |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 117 | 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 Lattner | 43373a6 | 2002-02-24 23:01:21 +0000 | [diff] [blame] | 124 | for (std::vector<const BasicBlock*>::iterator I = Preds.begin(), |
| 125 | E = Preds.end(); I != E; ++I) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 126 | Assert2(0, "PHI node does not have entry for a predecessor basic block!", |
| 127 | PN, *I); |
Chris Lattner | 7704e9f | 2002-03-14 16:53:48 +0000 | [diff] [blame] | 128 | } 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 Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 134 | } |
Chris Lattner | 7704e9f | 2002-03-14 16:53:48 +0000 | [diff] [blame] | 135 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 136 | return Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 140 | // verifyBasicBlock - Verify that a basic block is well formed... |
| 141 | // |
| 142 | static bool verifyBasicBlock(const BasicBlock *BB) { |
| 143 | bool Broken = false; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 144 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 145 | // Verify all instructions, except the terminator... |
| 146 | Broken |= reduce_apply_bool(BB->begin(), BB->end()-1, verifyInstruction); |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 147 | |
| 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 Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 165 | return Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 168 | // verifySymbolTable - Verify that a function or module symbol table is ok |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 169 | // |
| 170 | static bool verifySymbolTable(const SymbolTable *ST) { |
| 171 | if (ST == 0) return false; |
| 172 | bool Broken = false; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 173 | |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 174 | // 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 Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 191 | // 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 Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 193 | // informed. |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 194 | // |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 195 | bool verifyFunction(const Function *F) { |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 196 | if (F->isExternal()) return false; // Can happen if called by verifyModule |
| 197 | bool Broken = verifySymbolTable(F->getSymbolTable()); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 198 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 199 | // Check linkage of function... |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 200 | Assert1(!F->isExternal() || F->hasExternalLinkage(), |
| 201 | "Function cannot be an 'internal' 'declare'ation!", F); |
| 202 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 203 | // 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 Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 221 | const BasicBlock *Entry = F->getEntryNode(); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 222 | Assert1(pred_begin(Entry) == pred_end(Entry), |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 223 | "Entry block to function must not have predecessors!", Entry); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 225 | Broken |= reduce_apply_bool(F->begin(), F->end(), verifyBasicBlock); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 226 | return Broken; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | namespace { // Anonymous namespace for class |
| 231 | struct VerifierPass : public MethodPass { |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 232 | |
| 233 | bool doInitialization(Module *M) { |
| 234 | verifySymbolTable(M->getSymbolTable()); |
| 235 | return false; |
| 236 | } |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 237 | bool runOnMethod(Function *F) { verifyFunction(F); return false; } |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 238 | }; |
| 239 | } |
| 240 | |
| 241 | Pass *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 Lattner | b67f732 | 2002-02-26 21:45:33 +0000 | [diff] [blame] | 248 | bool verifyModule(const Module *M) { |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 249 | return verifySymbolTable(M->getSymbolTable()) | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame^] | 250 | reduce_apply_bool(M->begin(), M->end(), verifyFunction); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 251 | } |