2003-10-13 03:32:08 +00:00
|
|
|
//===- FindUnsafePointerTypes.cpp - Check pointer usage safety ------------===//
|
2001-11-07 21:16:12 +00:00
|
|
|
//
|
|
|
|
// This file defines a pass that can be used to determine, interprocedurally,
|
|
|
|
// which pointer types are accessed unsafely in a program. If there is an
|
|
|
|
// "unsafe" access to a specific pointer type, transformations that depend on
|
|
|
|
// type safety cannot be permitted.
|
|
|
|
//
|
|
|
|
// The result of running this analysis over a program is a set of unsafe pointer
|
|
|
|
// types that cannot be transformed. Safe pointer types are not tracked.
|
|
|
|
//
|
|
|
|
// Additionally, this analysis exports a hidden command line argument that (when
|
|
|
|
// enabled) prints out the reasons a type was determined to be unsafe.
|
|
|
|
//
|
|
|
|
// Currently, the only allowed operations on pointer types are:
|
|
|
|
// alloca, malloc, free, getelementptr, load, and store
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/FindUnsafePointerTypes.h"
|
|
|
|
#include "llvm/Assembly/CachedWriter.h"
|
|
|
|
#include "llvm/Type.h"
|
2002-01-31 00:42:27 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-02-12 21:07:25 +00:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2001-11-07 21:16:12 +00:00
|
|
|
|
2002-07-26 21:12:46 +00:00
|
|
|
static RegisterAnalysis<FindUnsafePointerTypes>
|
|
|
|
X("unsafepointertypes", "Find Unsafe Pointer Types");
|
2002-01-31 00:42:27 +00:00
|
|
|
|
2001-11-07 21:16:12 +00:00
|
|
|
// Provide a command line option to turn on printing of which instructions cause
|
|
|
|
// a type to become invalid
|
|
|
|
//
|
2002-07-22 02:08:59 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
PrintFailures("printunsafeptrinst", cl::Hidden,
|
|
|
|
cl::desc("Print Unsafe Pointer Access Instructions"));
|
2001-11-07 21:16:12 +00:00
|
|
|
|
|
|
|
static inline bool isSafeInstruction(const Instruction *I) {
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case Instruction::Alloca:
|
|
|
|
case Instruction::Malloc:
|
|
|
|
case Instruction::Free:
|
|
|
|
case Instruction::Load:
|
|
|
|
case Instruction::Store:
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
case Instruction::Call:
|
|
|
|
case Instruction::Invoke:
|
|
|
|
case Instruction::PHINode:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
bool FindUnsafePointerTypes::run(Module &Mod) {
|
|
|
|
for (Module::iterator FI = Mod.begin(), E = Mod.end();
|
|
|
|
FI != E; ++FI) {
|
|
|
|
const Function *F = FI; // We don't need/want write access
|
|
|
|
for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
|
|
|
|
const Type *ITy = I->getType();
|
2002-05-06 16:15:30 +00:00
|
|
|
if (isa<PointerType>(ITy) && !UnsafeTypes.count((PointerType*)ITy))
|
2002-06-25 16:13:24 +00:00
|
|
|
if (!isSafeInstruction(*I)) {
|
2002-01-31 00:42:27 +00:00
|
|
|
UnsafeTypes.insert((PointerType*)ITy);
|
2001-11-07 21:16:12 +00:00
|
|
|
|
2002-01-31 00:42:27 +00:00
|
|
|
if (PrintFailures) {
|
2002-06-25 16:13:24 +00:00
|
|
|
CachedWriter CW(F->getParent(), std::cerr);
|
2002-01-31 00:42:27 +00:00
|
|
|
CW << "FindUnsafePointerTypes: Type '" << ITy
|
2002-06-25 16:13:24 +00:00
|
|
|
<< "' marked unsafe in '" << F->getName() << "' by:\n" << **I;
|
2002-01-31 00:42:27 +00:00
|
|
|
}
|
2001-11-07 21:16:12 +00:00
|
|
|
}
|
2002-01-31 00:42:27 +00:00
|
|
|
}
|
2001-11-07 21:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// printResults - Loop over the results of the analysis, printing out unsafe
|
|
|
|
// types.
|
|
|
|
//
|
2002-07-27 01:12:17 +00:00
|
|
|
void FindUnsafePointerTypes::print(std::ostream &o, const Module *M) const {
|
2001-11-07 21:16:12 +00:00
|
|
|
if (UnsafeTypes.empty()) {
|
|
|
|
o << "SafePointerAccess Analysis: No unsafe types found!\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedWriter CW(M, o);
|
|
|
|
|
|
|
|
CW << "SafePointerAccess Analysis: Found these unsafe types:\n";
|
|
|
|
unsigned Counter = 1;
|
2002-01-20 22:54:45 +00:00
|
|
|
for (std::set<PointerType*>::const_iterator I = getUnsafeTypes().begin(),
|
2001-11-07 21:16:12 +00:00
|
|
|
E = getUnsafeTypes().end(); I != E; ++I, ++Counter) {
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
CW << " #" << Counter << ". " << (Value*)*I << "\n";
|
2001-11-07 21:16:12 +00:00
|
|
|
}
|
|
|
|
}
|