1 //===- FindUnsafePointerTypes.cpp - Check pointer usage safety --------------=//
3 // This file defines a pass that can be used to determine, interprocedurally,
4 // which pointer types are accessed unsafely in a program. If there is an
5 // "unsafe" access to a specific pointer type, transformations that depend on
6 // type safety cannot be permitted.
8 // The result of running this analysis over a program is a set of unsafe pointer
9 // types that cannot be transformed. Safe pointer types are not tracked.
11 // Additionally, this analysis exports a hidden command line argument that (when
12 // enabled) prints out the reasons a type was determined to be unsafe.
14 // Currently, the only allowed operations on pointer types are:
15 // alloca, malloc, free, getelementptr, load, and store
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Analysis/FindUnsafePointerTypes.h"
20 #include "llvm/Assembly/CachedWriter.h"
21 #include "llvm/Type.h"
22 #include "llvm/Module.h"
23 #include "llvm/Support/InstIterator.h"
24 #include "Support/CommandLine.h"
26 static RegisterAnalysis<FindUnsafePointerTypes>
27 X("unsafepointertypes", "Find Unsafe Pointer Types");
28 AnalysisID FindUnsafePointerTypes::ID(AnalysisID::create<FindUnsafePointerTypes>());
30 // Provide a command line option to turn on printing of which instructions cause
31 // a type to become invalid
34 PrintFailures("printunsafeptrinst", cl::Hidden,
35 cl::desc("Print Unsafe Pointer Access Instructions"));
37 static inline bool isSafeInstruction(const Instruction *I) {
38 switch (I->getOpcode()) {
39 case Instruction::Alloca:
40 case Instruction::Malloc:
41 case Instruction::Free:
42 case Instruction::Load:
43 case Instruction::Store:
44 case Instruction::GetElementPtr:
45 case Instruction::Call:
46 case Instruction::Invoke:
47 case Instruction::PHINode:
54 bool FindUnsafePointerTypes::run(Module &Mod) {
55 for (Module::iterator FI = Mod.begin(), E = Mod.end();
57 const Function *F = FI; // We don't need/want write access
58 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
59 const Type *ITy = I->getType();
60 if (isa<PointerType>(ITy) && !UnsafeTypes.count((PointerType*)ITy))
61 if (!isSafeInstruction(*I)) {
62 UnsafeTypes.insert((PointerType*)ITy);
65 CachedWriter CW(F->getParent(), std::cerr);
66 CW << "FindUnsafePointerTypes: Type '" << ITy
67 << "' marked unsafe in '" << F->getName() << "' by:\n" << **I;
77 // printResults - Loop over the results of the analysis, printing out unsafe
80 void FindUnsafePointerTypes::printResults(const Module *M,
81 std::ostream &o) const {
82 if (UnsafeTypes.empty()) {
83 o << "SafePointerAccess Analysis: No unsafe types found!\n";
87 CachedWriter CW(M, o);
89 CW << "SafePointerAccess Analysis: Found these unsafe types:\n";
91 for (std::set<PointerType*>::const_iterator I = getUnsafeTypes().begin(),
92 E = getUnsafeTypes().end(); I != E; ++I, ++Counter) {
94 CW << " #" << Counter << ". " << (Value*)*I << "\n";