Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==// |
| 2 | // |
| 3 | // This file defines the method verifier interface, that can be used for some |
| 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 | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 20 | // . Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad |
| 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 |
| 23 | // * The entry node to a method must not have predecessors |
| 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 | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 26 | // . Method's cannot take a void typed parameter |
| 27 | // . Verify that a method's argument list agrees with it's declared type. |
| 28 | // . Verify that arrays and structures have fixed elements: No unsized arrays. |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 29 | // . All other things that are tested by asserts spread about the code... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | // |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 34 | #include "llvm/Assembly/Writer.h" |
| 35 | #include "llvm/Pass.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | #include "llvm/Method.h" |
| 37 | #include "llvm/Module.h" |
| 38 | #include "llvm/BasicBlock.h" |
| 39 | #include "llvm/Type.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 40 | #include "llvm/iPHINode.h" |
| 41 | #include "llvm/Support/CFG.h" |
| 42 | #include "Support/STLExtras.h" |
| 43 | #include <algorithm> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 44 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 45 | #if 0 |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 46 | #define t(x) (1 << (unsigned)Type::x) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 47 | #define SignedIntegralTypes (t(SByteTyID) | t(ShortTyID) | \ |
| 48 | t(IntTyID) | t(LongTyID)) |
| 49 | static long UnsignedIntegralTypes = t(UByteTyID) | t(UShortTyID) | |
| 50 | t(UIntTyID) | t(ULongTyID); |
| 51 | static const long FloatingPointTypes = t(FloatTyID) | t(DoubleTyID); |
| 52 | |
| 53 | static const long IntegralTypes = SignedIntegralTypes | UnsignedIntegralTypes; |
| 54 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 55 | static long ValidTypes[Type::FirstDerivedTyID] = { |
| 56 | [(unsigned)Instruction::UnaryOps::Not] t(BoolTyID), |
| 57 | //[Instruction::UnaryOps::Add] = IntegralTypes, |
| 58 | // [Instruction::Sub] = IntegralTypes, |
| 59 | }; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 60 | #undef t |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 63 | // CheckFailed - A check failed, so print out the condition and the message that |
| 64 | // failed. This provides a nice place to put a breakpoint if you want to see |
| 65 | // why something is not correct. |
| 66 | // |
Chris Lattner | 43373a6 | 2002-02-24 23:01:21 +0000 | [diff] [blame^] | 67 | static inline void CheckFailed(const char *Cond, const std::string &Message, |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 68 | const Value *V1 = 0, const Value *V2 = 0) { |
| 69 | std::cerr << Message << "\n"; |
| 70 | if (V1) std::cerr << V1 << "\n"; |
| 71 | if (V2) std::cerr << V2 << "\n"; |
| 72 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 73 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 74 | // Assert - We know that cond should be true, if not print an error message. |
| 75 | #define Assert(C, M) \ |
| 76 | do { if (!(C)) { CheckFailed(#C, M); Broken = true; } } while (0) |
| 77 | #define Assert1(C, M, V1) \ |
| 78 | do { if (!(C)) { CheckFailed(#C, M, V1); Broken = true; } } while (0) |
| 79 | #define Assert2(C, M, V1, V2) \ |
| 80 | do { if (!(C)) { CheckFailed(#C, M, V1, V2); Broken = true; } } while (0) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 81 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 82 | |
| 83 | // verifyInstruction - Verify that a non-terminator instruction is well formed. |
| 84 | // |
| 85 | static bool verifyInstruction(const Instruction *I) { |
| 86 | bool Broken = false; |
| 87 | assert(I->getParent() && "Instruction not embedded in basic block!"); |
| 88 | Assert1(!isa<TerminatorInst>(I), |
| 89 | "Terminator instruction found embedded in basic block!\n", I); |
| 90 | |
| 91 | // Check that all uses of the instruction, if they are instructions |
| 92 | // themselves, actually have parent basic blocks. |
| 93 | // |
| 94 | for (User::use_const_iterator UI = I->use_begin(), UE = I->use_end(); |
| 95 | UI != UE; ++UI) { |
| 96 | if (Instruction *Used = dyn_cast<Instruction>(*UI)) |
| 97 | Assert2(Used->getParent() != 0, "Instruction referencing instruction not" |
| 98 | " embeded in a basic block!", I, Used); |
| 99 | } |
| 100 | |
| 101 | // Check that PHI nodes look ok |
| 102 | if (const PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | 43373a6 | 2002-02-24 23:01:21 +0000 | [diff] [blame^] | 103 | std::vector<const BasicBlock*> Preds(pred_begin(I->getParent()), |
| 104 | pred_end(I->getParent())); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 105 | // Loop over all of the incoming values, make sure that there are |
| 106 | // predecessors for each one... |
| 107 | // |
| 108 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 109 | const BasicBlock *BB = PN->getIncomingBlock(i); |
Chris Lattner | 43373a6 | 2002-02-24 23:01:21 +0000 | [diff] [blame^] | 110 | std::vector<const BasicBlock*>::iterator PI = |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 111 | find(Preds.begin(), Preds.end(), BB); |
| 112 | Assert2(PI != Preds.end(), "PHI node has entry for basic block that" |
| 113 | " is not a predecessor!", PN, BB); |
| 114 | if (PI != Preds.end()) Preds.erase(PI); |
| 115 | } |
| 116 | |
| 117 | // There should be no entries left in the predecessor list... |
Chris Lattner | 43373a6 | 2002-02-24 23:01:21 +0000 | [diff] [blame^] | 118 | for (std::vector<const BasicBlock*>::iterator I = Preds.begin(), |
| 119 | E = Preds.end(); I != E; ++I) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 120 | Assert2(0, "PHI node does not have entry for a predecessor basic block!", |
| 121 | PN, *I); |
| 122 | } |
| 123 | return Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 127 | // verifyBasicBlock - Verify that a basic block is well formed... |
| 128 | // |
| 129 | static bool verifyBasicBlock(const BasicBlock *BB) { |
| 130 | bool Broken = false; |
| 131 | Assert1(BB->getTerminator(), "Basic Block does not have terminator!\n", BB); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 132 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 133 | // Verify all instructions, except the terminator... |
| 134 | Broken |= reduce_apply_bool(BB->begin(), BB->end()-1, verifyInstruction); |
| 135 | return Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 138 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 139 | // verifyMethod - Verify that a method is ok. |
| 140 | // |
| 141 | static bool verifyMethod(const Method *M) { |
| 142 | if (M->isExternal()) return false; // Can happen if called by verifyModule |
| 143 | |
| 144 | bool Broken = false; |
| 145 | const BasicBlock *Entry = M->front(); |
| 146 | Assert1(pred_begin(Entry) == pred_end(Entry), |
| 147 | "Entry block to method must not have predecessors!", Entry); |
| 148 | |
| 149 | Broken |= reduce_apply_bool(M->begin(), M->end(), verifyBasicBlock); |
| 150 | return Broken; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | namespace { // Anonymous namespace for class |
| 155 | struct VerifierPass : public MethodPass { |
| 156 | bool runOnMethod(Method *M) { verifyMethod(M); return false; } |
| 157 | }; |
| 158 | } |
| 159 | |
| 160 | Pass *createVerifierPass() { |
| 161 | return new VerifierPass(); |
| 162 | } |
| 163 | |
| 164 | // verifyModule - Check a module for errors, printing messages on stderr. |
| 165 | // Return true if the module is corrupt. |
| 166 | // |
| 167 | bool verifyModule(Module *M) { |
| 168 | return reduce_apply_bool(M->begin(), M->end(), verifyMethod); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 169 | } |