1 //===- FindUnsafePointerTypes.cpp - Check pointer usage safety ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a pass that can be used to determine, interprocedurally,
11 // which pointer types are accessed unsafely in a program. If there is an
12 // "unsafe" access to a specific pointer type, transformations that depend on
13 // type safety cannot be permitted.
15 // The result of running this analysis over a program is a set of unsafe pointer
16 // types that cannot be transformed. Safe pointer types are not tracked.
18 // Additionally, this analysis exports a hidden command line argument that (when
19 // enabled) prints out the reasons a type was determined to be unsafe.
21 // Currently, the only allowed operations on pointer types are:
22 // alloca, malloc, free, getelementptr, load, and store
24 //===----------------------------------------------------------------------===//
26 #include "llvm/Analysis/FindUnsafePointerTypes.h"
27 #include "llvm/Assembly/CachedWriter.h"
28 #include "llvm/Type.h"
29 #include "llvm/Module.h"
30 #include "llvm/Support/InstIterator.h"
31 #include "Support/CommandLine.h"
35 static RegisterAnalysis<FindUnsafePointerTypes>
36 X("unsafepointertypes", "Find Unsafe Pointer Types");
38 // Provide a command line option to turn on printing of which instructions cause
39 // a type to become invalid
42 PrintFailures("printunsafeptrinst", cl::Hidden,
43 cl::desc("Print Unsafe Pointer Access Instructions"));
45 static inline bool isSafeInstruction(const Instruction &I) {
46 switch (I.getOpcode()) {
47 case Instruction::Alloca:
48 case Instruction::Malloc:
49 case Instruction::Free:
50 case Instruction::Load:
51 case Instruction::Store:
52 case Instruction::GetElementPtr:
53 case Instruction::Call:
54 case Instruction::Invoke:
55 case Instruction::PHI:
62 bool FindUnsafePointerTypes::run(Module &Mod) {
63 for (Module::iterator FI = Mod.begin(), E = Mod.end();
65 const Function *F = FI; // We don't need/want write access
66 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
67 const Type *ITy = I->getType();
68 if (isa<PointerType>(ITy) && !UnsafeTypes.count((PointerType*)ITy))
69 if (!isSafeInstruction(*I)) {
70 UnsafeTypes.insert((PointerType*)ITy);
73 CachedWriter CW(F->getParent(), std::cerr);
74 std::cerr << "FindUnsafePointerTypes: Type '";
76 std::cerr << "' marked unsafe in '" << F->getName() << "' by:\n";
87 // printResults - Loop over the results of the analysis, printing out unsafe
90 void FindUnsafePointerTypes::print(std::ostream &o, const Module *M) const {
91 if (UnsafeTypes.empty()) {
92 o << "SafePointerAccess Analysis: No unsafe types found!\n";
96 CachedWriter CW(M, o);
98 o << "SafePointerAccess Analysis: Found these unsafe types:\n";
100 for (std::set<PointerType*>::const_iterator I = getUnsafeTypes().begin(),
101 E = getUnsafeTypes().end(); I != E; ++I, ++Counter) {
103 o << " #" << Counter << ". ";
104 CW << (Type*)*I << "\n";
108 } // End llvm namespace