aboutsummaryrefslogtreecommitdiff
path: root/lib/Analysis/IPA/FindUnsafePointerTypes.cpp
blob: d689d93cd4433f8b29a6779d33d2d2443eb4168b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//===- FindUnsafePointerTypes.cpp - Check pointer usage safety --------------=//
//
// 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"
#include "llvm/Instruction.h"
#include "llvm/Function.h"
#include "llvm/Module.h"
#include "llvm/Support/InstIterator.h"
#include "Support/CommandLine.h"

AnalysisID FindUnsafePointerTypes::ID(AnalysisID::create<FindUnsafePointerTypes>());

// Provide a command line option to turn on printing of which instructions cause
// a type to become invalid
//
static cl::Flag 
PrintFailures("printunsafeptrinst", "Print Unsafe Pointer Access Instructions",
              cl::Hidden, false);

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;
}


bool FindUnsafePointerTypes::run(Module *Mod) {
  for (Module::iterator MI = Mod->begin(), ME = Mod->end();
       MI != ME; ++MI) {
    const Function *M = *MI;  // We don't need/want write access
    for (const_inst_iterator I = inst_begin(M), E = inst_end(M); I != E; ++I) {
      const Instruction *Inst = *I;
      const Type *ITy = Inst->getType();
      if (ITy->isPointerType() && !UnsafeTypes.count((PointerType*)ITy))
        if (!isSafeInstruction(Inst)) {
          UnsafeTypes.insert((PointerType*)ITy);

          if (PrintFailures) {
            CachedWriter CW(M->getParent(), std::cerr);
            CW << "FindUnsafePointerTypes: Type '" << ITy
               << "' marked unsafe in '" << M->getName() << "' by:\n" << Inst;
          }
        }
    }
  }

  return false;
}


// printResults - Loop over the results of the analysis, printing out unsafe
// types.
//
void FindUnsafePointerTypes::printResults(const Module *M,
                                          std::ostream &o) const {
  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;
  for (std::set<PointerType*>::const_iterator I = getUnsafeTypes().begin(), 
         E = getUnsafeTypes().end(); I != E; ++I, ++Counter) {
    
    CW << " #" << Counter << ". " << (Value*)*I << "\n";
  }
}

// getAnalysisUsageInfo - Of course, we provide ourself...
//
void FindUnsafePointerTypes::getAnalysisUsageInfo(Pass::AnalysisSet &Required,
                                                  Pass::AnalysisSet &Destroyed,
                                                  Pass::AnalysisSet &Provided) {
  Provided.push_back(FindUnsafePointerTypes::ID);
}