blob: e6333f71dd36fbad060f2825d6871512a2e8f8af [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- 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 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!]
19// . Right now 'add bool 0, 0' is valid. This isn't particularly good.
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.
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 Lattner68289f02001-11-06 22:53:11 +000025// . Verify that none of the Value getType()'s are null.
Chris Lattner2f7c9632001-06-06 20:29:01 +000026// . 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 Lattnerfbf5be52002-03-15 20:25:09 +000029// * It is illegal to specify a name for a void value.
Chris Lattnerd02f08d2002-02-20 17:55:43 +000030// . All other things that are tested by asserts spread about the code...
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//
32//===----------------------------------------------------------------------===//
33
34#include "llvm/Analysis/Verifier.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000035#include "llvm/Assembly/Writer.h"
36#include "llvm/Pass.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000037#include "llvm/Method.h"
38#include "llvm/Module.h"
39#include "llvm/BasicBlock.h"
40#include "llvm/Type.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000041#include "llvm/iPHINode.h"
Chris Lattnerfbf5be52002-03-15 20:25:09 +000042#include "llvm/SymbolTable.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000043#include "llvm/Support/CFG.h"
44#include "Support/STLExtras.h"
45#include <algorithm>
Chris Lattner7f74a562002-01-20 22:54:45 +000046
Chris Lattnerd02f08d2002-02-20 17:55:43 +000047#if 0
Chris Lattner2f7c9632001-06-06 20:29:01 +000048#define t(x) (1 << (unsigned)Type::x)
Chris Lattner2f7c9632001-06-06 20:29:01 +000049#define SignedIntegralTypes (t(SByteTyID) | t(ShortTyID) | \
50 t(IntTyID) | t(LongTyID))
51static long UnsignedIntegralTypes = t(UByteTyID) | t(UShortTyID) |
52 t(UIntTyID) | t(ULongTyID);
53static const long FloatingPointTypes = t(FloatTyID) | t(DoubleTyID);
54
55static const long IntegralTypes = SignedIntegralTypes | UnsignedIntegralTypes;
56
Chris Lattner2f7c9632001-06-06 20:29:01 +000057static long ValidTypes[Type::FirstDerivedTyID] = {
58 [(unsigned)Instruction::UnaryOps::Not] t(BoolTyID),
59 //[Instruction::UnaryOps::Add] = IntegralTypes,
60 // [Instruction::Sub] = IntegralTypes,
61};
Chris Lattnerd02f08d2002-02-20 17:55:43 +000062#undef t
Chris Lattner2f7c9632001-06-06 20:29:01 +000063#endif
64
Chris Lattnerd02f08d2002-02-20 17:55:43 +000065// CheckFailed - A check failed, so print out the condition and the message that
66// failed. This provides a nice place to put a breakpoint if you want to see
67// why something is not correct.
68//
Chris Lattner43373a62002-02-24 23:01:21 +000069static inline void CheckFailed(const char *Cond, const std::string &Message,
Chris Lattnerd02f08d2002-02-20 17:55:43 +000070 const Value *V1 = 0, const Value *V2 = 0) {
71 std::cerr << Message << "\n";
72 if (V1) std::cerr << V1 << "\n";
73 if (V2) std::cerr << V2 << "\n";
74}
Chris Lattner2f7c9632001-06-06 20:29:01 +000075
Chris Lattnerd02f08d2002-02-20 17:55:43 +000076// Assert - We know that cond should be true, if not print an error message.
77#define Assert(C, M) \
78 do { if (!(C)) { CheckFailed(#C, M); Broken = true; } } while (0)
79#define Assert1(C, M, V1) \
80 do { if (!(C)) { CheckFailed(#C, M, V1); Broken = true; } } while (0)
81#define Assert2(C, M, V1, V2) \
82 do { if (!(C)) { CheckFailed(#C, M, V1, V2); Broken = true; } } while (0)
Chris Lattner2f7c9632001-06-06 20:29:01 +000083
Chris Lattnerd02f08d2002-02-20 17:55:43 +000084
85// verifyInstruction - Verify that a non-terminator instruction is well formed.
86//
87static bool verifyInstruction(const Instruction *I) {
88 bool Broken = false;
89 assert(I->getParent() && "Instruction not embedded in basic block!");
90 Assert1(!isa<TerminatorInst>(I),
91 "Terminator instruction found embedded in basic block!\n", I);
92
93 // Check that all uses of the instruction, if they are instructions
94 // themselves, actually have parent basic blocks.
95 //
96 for (User::use_const_iterator UI = I->use_begin(), UE = I->use_end();
97 UI != UE; ++UI) {
98 if (Instruction *Used = dyn_cast<Instruction>(*UI))
99 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
100 " embeded in a basic block!", I, Used);
101 }
102
103 // Check that PHI nodes look ok
104 if (const PHINode *PN = dyn_cast<PHINode>(I)) {
Chris Lattner43373a62002-02-24 23:01:21 +0000105 std::vector<const BasicBlock*> Preds(pred_begin(I->getParent()),
106 pred_end(I->getParent()));
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000107 // Loop over all of the incoming values, make sure that there are
108 // predecessors for each one...
109 //
110 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
111 const BasicBlock *BB = PN->getIncomingBlock(i);
Chris Lattner43373a62002-02-24 23:01:21 +0000112 std::vector<const BasicBlock*>::iterator PI =
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000113 find(Preds.begin(), Preds.end(), BB);
114 Assert2(PI != Preds.end(), "PHI node has entry for basic block that"
115 " is not a predecessor!", PN, BB);
116 if (PI != Preds.end()) Preds.erase(PI);
117 }
118
119 // There should be no entries left in the predecessor list...
Chris Lattner43373a62002-02-24 23:01:21 +0000120 for (std::vector<const BasicBlock*>::iterator I = Preds.begin(),
121 E = Preds.end(); I != E; ++I)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000122 Assert2(0, "PHI node does not have entry for a predecessor basic block!",
123 PN, *I);
Chris Lattner7704e9f2002-03-14 16:53:48 +0000124 } else {
125 // Check that non-phi nodes are not self referential...
126 for (Value::use_const_iterator UI = I->use_begin(), UE = I->use_end();
127 UI != UE; ++UI)
128 Assert1(*UI != (const User*)I,
129 "Only PHI nodes may reference their own value!", I);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000130 }
Chris Lattner7704e9f2002-03-14 16:53:48 +0000131
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000132 return Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133}
134
135
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000136// verifyBasicBlock - Verify that a basic block is well formed...
137//
138static bool verifyBasicBlock(const BasicBlock *BB) {
139 bool Broken = false;
140 Assert1(BB->getTerminator(), "Basic Block does not have terminator!\n", BB);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000141
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000142 // Verify all instructions, except the terminator...
143 Broken |= reduce_apply_bool(BB->begin(), BB->end()-1, verifyInstruction);
144 return Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145}
146
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000147// verifySymbolTable - Verify that a method or module symbol table is ok
148//
149static bool verifySymbolTable(const SymbolTable *ST) {
150 if (ST == 0) return false;
151 bool Broken = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000152
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000153 // Loop over all of the types in the symbol table...
154 for (SymbolTable::const_iterator TI = ST->begin(), TE = ST->end();
155 TI != TE; ++TI)
156 for (SymbolTable::type_const_iterator I = TI->second.begin(),
157 E = TI->second.end(); I != E; ++I) {
158 Value *V = I->second;
159
160 // Check that there are no void typed values in the symbol table. Values
161 // with a void type cannot be put into symbol tables because they cannot
162 // have names!
163 Assert1(V->getType() != Type::VoidTy,
164 "Values with void type are not allowed to have names!\n", V);
165 }
166
167 return Broken;
168}
169
170// verifyMethod - Verify that a method is ok. Return true if not so that
171// verifyModule and direct clients of the verifyMethod function are correctly
172// informed.
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000173//
Chris Lattnerb67f7322002-02-26 21:45:33 +0000174bool verifyMethod(const Method *M) {
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000175 if (M->isExternal()) return false; // Can happen if called by verifyModule
176
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000177 bool Broken = verifySymbolTable(M->getSymbolTable());
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000178 const BasicBlock *Entry = M->front();
179 Assert1(pred_begin(Entry) == pred_end(Entry),
180 "Entry block to method must not have predecessors!", Entry);
181
182 Broken |= reduce_apply_bool(M->begin(), M->end(), verifyBasicBlock);
183 return Broken;
184}
185
186
187namespace { // Anonymous namespace for class
188 struct VerifierPass : public MethodPass {
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000189
190 bool doInitialization(Module *M) {
191 verifySymbolTable(M->getSymbolTable());
192 return false;
193 }
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000194 bool runOnMethod(Method *M) { verifyMethod(M); return false; }
195 };
196}
197
198Pass *createVerifierPass() {
199 return new VerifierPass();
200}
201
202// verifyModule - Check a module for errors, printing messages on stderr.
203// Return true if the module is corrupt.
204//
Chris Lattnerb67f7322002-02-26 21:45:33 +0000205bool verifyModule(const Module *M) {
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000206 return verifySymbolTable(M->getSymbolTable()) |
207 reduce_apply_bool(M->begin(), M->end(), verifyMethod);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208}