// Note that this does not provide full 'java style' security and verifications,
// instead it just tries to ensure that code is well formed.
//
-// . There are no duplicated names in a symbol table... ie there !exist a val
-// with the same name as something in the symbol table, but with a different
-// address as what is in the symbol table...
// * Both of a binary operator's parameters are the same type
// * Verify that the indices of mem access instructions match other operands
-// . Verify that arithmetic and other things are only performed on first class
-// types. No adding structures or arrays.
+// * Verify that arithmetic and other things are only performed on first class
+// types. Verify that shifts & logicals only happen on integrals f.e.
// . All of the constants in a switch statement are of the correct type
-// . The code is in valid SSA form
+// * The code is in valid SSA form
// . It should be illegal to put a label into any other type (like a structure)
// or to return one. [except constant arrays!]
// * Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad
// * All basic blocks should only end with terminator insts, not contain them
// * The entry node to a function must not have predecessors
// * All Instructions must be embeded into a basic block
-// . Verify that none of the Value getType()'s are null.
// . Function's cannot take a void typed parameter
// * Verify that a function's argument list agrees with it's declared type.
// . Verify that arrays and structures have fixed elements: No unsized arrays.
#include "llvm/iPHINode.h"
#include "llvm/iTerminators.h"
#include "llvm/iOther.h"
+#include "llvm/iOperators.h"
#include "llvm/iMemory.h"
#include "llvm/SymbolTable.h"
+#include "llvm/PassManager.h"
+#include "llvm/Analysis/Dominators.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/InstVisitor.h"
#include "Support/STLExtras.h"
#include <algorithm>
-#include <iostream>
namespace { // Anonymous namespace for class
struct Verifier : public FunctionPass, InstVisitor<Verifier> {
- bool Broken;
+ bool Broken; // Is this module found to be broken?
+ bool RealPass; // Are we not being run by a PassManager?
+ bool AbortBroken; // If broken, should it or should it not abort?
+
+ DominatorSet *DS; // Dominator set, caution can be null!
- Verifier() : Broken(false) {}
+ Verifier() : Broken(false), RealPass(true), AbortBroken(true), DS(0) {}
+ Verifier(bool AB) : Broken(false), RealPass(true), AbortBroken(AB), DS(0) {}
+ Verifier(DominatorSet &ds)
+ : Broken(false), RealPass(false), AbortBroken(false), DS(&ds) {}
- virtual const char *getPassName() const { return "Module Verifier"; }
bool doInitialization(Module &M) {
verifySymbolTable(M.getSymbolTable());
+
+ // If this is a real pass, in a pass manager, we must abort before
+ // returning back to the pass manager, or else the pass manager may try to
+ // run other passes on the broken module.
+ //
+ if (RealPass)
+ abortIfBroken();
return false;
}
bool runOnFunction(Function &F) {
+ // Get dominator information if we are being run by PassManager
+ if (RealPass) DS = &getAnalysis<DominatorSet>();
visit(F);
+
+ // If this is a real pass, in a pass manager, we must abort before
+ // returning back to the pass manager, or else the pass manager may try to
+ // run other passes on the broken module.
+ //
+ if (RealPass)
+ abortIfBroken();
+
return false;
}
if (I->isExternal() && I->hasInternalLinkage())
CheckFailed("Function Declaration has Internal Linkage!", I);
- if (Broken) {
- std::cerr << "Broken module found, compilation aborted!\n";
- abort();
- }
+ // If the module is broken, abort at this time.
+ abortIfBroken();
return false;
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
+ if (RealPass)
+ AU.addRequired<DominatorSet>();
+ }
+
+ // abortIfBroken - If the module is broken and we are supposed to abort on
+ // this condition, do so.
+ //
+ void abortIfBroken() const {
+ if (Broken && AbortBroken) {
+ std::cerr << "Broken module found, compilation aborted!\n";
+ abort();
+ }
}
// Verification methods...
void visitBasicBlock(BasicBlock &BB);
void visitPHINode(PHINode &PN);
void visitBinaryOperator(BinaryOperator &B);
+ void visitShiftInst(ShiftInst &SI);
void visitCallInst(CallInst &CI);
void visitGetElementPtrInst(GetElementPtrInst &GEP);
void visitLoadInst(LoadInst &LI);
Broken = true;
}
};
+
+ RegisterPass<Verifier> X("verify", "Module Verifier");
}
// Assert - We know that cond should be true, if not print an error message.
// Ensure that terminators only exist at the end of the basic block.
Assert1(&I == I.getParent()->getTerminator(),
"Terminator found in the middle of a basic block!", I.getParent());
+ visitInstruction(I);
}
void Verifier::visitReturnInst(ReturnInst &RI) {
Assert2(CI.getOperand(i+1)->getType() == FTy->getParamType(i),
"Call parameter type does not match function signature!",
CI.getOperand(i+1), FTy->getParamType(i));
+
+ visitInstruction(CI);
}
// visitBinaryOperator - Check that both arguments to the binary operator are
// of the same type!
//
void Verifier::visitBinaryOperator(BinaryOperator &B) {
- Assert2(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
- "Both operands to a binary operator are not of the same type!",
- B.getOperand(0), B.getOperand(1));
-
+ Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
+ "Both operands to a binary operator are not of the same type!", &B);
+
+ // Check that logical operators are only used with integral operands.
+ if (B.getOpcode() == Instruction::And || B.getOpcode() == Instruction::Or ||
+ B.getOpcode() == Instruction::Xor) {
+ Assert1(B.getType()->isIntegral(),
+ "Logical operators only work with integral types!", &B);
+ Assert1(B.getType() == B.getOperand(0)->getType(),
+ "Logical operators must have same type for operands and result!",
+ &B);
+ } else if (isa<SetCondInst>(B)) {
+ // Check that setcc instructions return bool
+ Assert1(B.getType() == Type::BoolTy,
+ "setcc instructions must return boolean values!", &B);
+ } else {
+ // Arithmetic operators only work on integer or fp values
+ Assert1(B.getType() == B.getOperand(0)->getType(),
+ "Arithmetic operators must have same type for operands and result!",
+ &B);
+ Assert1(B.getType()->isInteger() || B.getType()->isFloatingPoint(),
+ "Arithmetic operators must have integer or fp type!", &B);
+ }
+
visitInstruction(B);
}
+void Verifier::visitShiftInst(ShiftInst &SI) {
+ Assert1(SI.getType()->isInteger(),
+ "Shift must return an integer result!", &SI);
+ Assert1(SI.getType() == SI.getOperand(0)->getType(),
+ "Shift return type must be same as first operand!", &SI);
+ Assert1(SI.getOperand(1)->getType() == Type::UByteTy,
+ "Second operand to shift must be ubyte type!", &SI);
+ visitInstruction(SI);
+}
+
+
+
void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
- const Type *ElTy = MemAccessInst::getIndexedType(GEP.getOperand(0)->getType(),
- GEP.copyIndices(), true);
+ const Type *ElTy =
+ GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(),
+ std::vector<Value*>(GEP.idx_begin(), GEP.idx_end()), true);
Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
Assert2(PointerType::get(ElTy) == GEP.getType(),
"GEP is not of right type for indices!", &GEP, ElTy);
}
void Verifier::visitLoadInst(LoadInst &LI) {
- const Type *ElTy = LoadInst::getIndexedType(LI.getOperand(0)->getType(),
- LI.copyIndices());
- Assert1(ElTy, "Invalid indices for load pointer type!", &LI);
+ const Type *ElTy =
+ cast<PointerType>(LI.getOperand(0)->getType())->getElementType();
Assert2(ElTy == LI.getType(),
"Load is not of right type for indices!", &LI, ElTy);
visitInstruction(LI);
}
void Verifier::visitStoreInst(StoreInst &SI) {
- const Type *ElTy = StoreInst::getIndexedType(SI.getOperand(1)->getType(),
- SI.copyIndices());
- Assert1(ElTy, "Invalid indices for store pointer type!", &SI);
+ const Type *ElTy =
+ cast<PointerType>(SI.getOperand(1)->getType())->getElementType();
Assert2(ElTy == SI.getOperand(0)->getType(),
"Stored value is not of right type for indices!", &SI, ElTy);
visitInstruction(SI);
}
-// verifyInstruction - Verify that a non-terminator instruction is well formed.
+// verifyInstruction - Verify that an instruction is well formed.
//
void Verifier::visitInstruction(Instruction &I) {
- Assert1(I.getParent(), "Instruction not embedded in basic block!", &I);
+ BasicBlock *BB = I.getParent();
+ Assert1(BB, "Instruction not embedded in basic block!", &I);
// Check that all uses of the instruction, if they are instructions
// themselves, actually have parent basic blocks. If the use is not an
"Only PHI nodes may reference their own value!", &I);
}
+ // Check that void typed values don't have names
Assert1(I.getType() != Type::VoidTy || !I.hasName(),
"Instruction has a name, but provides a void value!", &I);
+
+ // Check that a definition dominates all of its uses.
+ //
+ for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
+ UI != UE; ++UI) {
+ Instruction *Use = cast<Instruction>(*UI);
+
+ // PHI nodes are more difficult than other nodes because they actually
+ // "use" the value in the predecessor basic blocks they correspond to.
+ if (PHINode *PN = dyn_cast<PHINode>(Use)) {
+ for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
+ if (&I == PN->getIncomingValue(i)) {
+ // Make sure that I dominates the end of pred(i)
+ BasicBlock *Pred = PN->getIncomingBlock(i);
+
+ // Use must be dominated by by definition unless use is unreachable!
+ Assert2(DS->dominates(BB, Pred) ||
+ !DS->dominates(&BB->getParent()->getEntryNode(), Pred),
+ "Instruction does not dominate all uses!",
+ &I, PN);
+ }
+
+ } else {
+ // Use must be dominated by by definition unless use is unreachable!
+ Assert2(DS->dominates(&I, Use) ||
+ !DS->dominates(&BB->getParent()->getEntryNode(),Use->getParent()),
+ "Instruction does not dominate all uses!", &I, Use);
+ }
+ }
}
return new Verifier();
}
-bool verifyFunction(const Function &F) {
- Verifier V;
- V.visit((Function&)F);
+
+// verifyFunction - Create
+bool verifyFunction(const Function &f) {
+ Function &F = (Function&)f;
+ assert(!F.isExternal() && "Cannot verify external functions");
+
+ DominatorSet DS;
+ DS.doInitialization(*F.getParent());
+ DS.runOnFunction(F);
+
+ Verifier V(DS);
+ V.runOnFunction(F);
+
+ DS.doFinalization(*F.getParent());
+
return V.Broken;
}
// Return true if the module is corrupt.
//
bool verifyModule(const Module &M) {
- Verifier V;
- V.run((Module&)M);
- return V.Broken;
+ PassManager PM;
+ Verifier *V = new Verifier();
+ PM.add(V);
+ PM.run((Module&)M);
+ return V->Broken;
}