OLD | NEW |
---|---|
1 //===- PNaClABIVerifyModule.cpp - Verify PNaCl ABI rules --------===// | 1 //===- PNaClABIVerifyModule.cpp - Verify PNaCl ABI rules --------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // Verify module-level PNaCl ABI requirements (specifically those that do not | 10 // Verify module-level PNaCl ABI requirements (specifically those that do not |
11 // require looking at the function bodies) | 11 // require looking at the function bodies) |
12 // | 12 // |
13 // | 13 // |
14 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
15 | 15 |
16 #include "llvm/Pass.h" | 16 #include "llvm/Pass.h" |
17 #include "llvm/ADT/OwningPtr.h" | |
jvoung (off chromium)
2013/03/13 22:40:09
where is OwningPtr used? Was that replaced with t
Derek Schuff
2013/03/13 22:52:23
Ah yes, removed.
On 2013/03/13 22:40:09, jvoung (c
| |
18 #include "llvm/ADT/Twine.h" | |
17 #include "llvm/Analysis/NaCl.h" | 19 #include "llvm/Analysis/NaCl.h" |
18 #include "llvm/ADT/Twine.h" | |
19 #include "llvm/IR/DerivedTypes.h" | 20 #include "llvm/IR/DerivedTypes.h" |
20 #include "llvm/IR/Module.h" | 21 #include "llvm/IR/Module.h" |
21 #include "llvm/Support/raw_ostream.h" | 22 #include "llvm/Support/raw_ostream.h" |
22 | 23 |
23 #include "PNaClABITypeChecker.h" | 24 #include "PNaClABITypeChecker.h" |
24 using namespace llvm; | 25 using namespace llvm; |
25 | 26 |
26 namespace { | 27 namespace { |
27 // This pass should not touch function bodies, to stay streaming-friendly | 28 // This pass should not touch function bodies, to stay streaming-friendly |
28 class PNaClABIVerifyModule : public ModulePass { | 29 class PNaClABIVerifyModule : public ModulePass { |
29 public: | 30 public: |
30 static char ID; | 31 static char ID; |
31 PNaClABIVerifyModule(); | 32 PNaClABIVerifyModule() : ModulePass(ID), |
33 Reporter(new PNaClABIErrorReporter), | |
34 ReporterIsOwned(true) {} | |
35 explicit PNaClABIVerifyModule(PNaClABIErrorReporter *Reporter_) : | |
36 ModulePass(ID), | |
37 Reporter(Reporter_), | |
38 ReporterIsOwned(false) {} | |
39 ~PNaClABIVerifyModule() { | |
40 if (ReporterIsOwned) | |
41 delete Reporter; | |
42 } | |
32 bool runOnModule(Module &M); | 43 bool runOnModule(Module &M); |
33 virtual void print(raw_ostream &O, const Module *M) const; | 44 virtual void print(raw_ostream &O, const Module *M) const; |
34 private: | 45 private: |
35 PNaClABITypeChecker TC; | 46 PNaClABITypeChecker TC; |
36 std::string ErrorsString; | 47 PNaClABIErrorReporter *Reporter; |
37 raw_string_ostream Errors; | 48 bool ReporterIsOwned; |
38 }; | 49 }; |
39 | 50 |
40 static const char *linkageName(GlobalValue::LinkageTypes LT) { | 51 static const char *linkageName(GlobalValue::LinkageTypes LT) { |
41 // This logic is taken from PrintLinkage in lib/VMCore/AsmWriter.cpp | 52 // This logic is taken from PrintLinkage in lib/VMCore/AsmWriter.cpp |
42 switch (LT) { | 53 switch (LT) { |
43 case GlobalValue::ExternalLinkage: return "external"; | 54 case GlobalValue::ExternalLinkage: return "external"; |
44 case GlobalValue::PrivateLinkage: return "private "; | 55 case GlobalValue::PrivateLinkage: return "private "; |
45 case GlobalValue::LinkerPrivateLinkage: return "linker_private "; | 56 case GlobalValue::LinkerPrivateLinkage: return "linker_private "; |
46 case GlobalValue::LinkerPrivateWeakLinkage: return "linker_private_weak "; | 57 case GlobalValue::LinkerPrivateWeakLinkage: return "linker_private_weak "; |
47 case GlobalValue::InternalLinkage: return "internal "; | 58 case GlobalValue::InternalLinkage: return "internal "; |
(...skipping 10 matching lines...) Expand all Loading... | |
58 case GlobalValue::ExternalWeakLinkage: return "extern_weak "; | 69 case GlobalValue::ExternalWeakLinkage: return "extern_weak "; |
59 case GlobalValue::AvailableExternallyLinkage: | 70 case GlobalValue::AvailableExternallyLinkage: |
60 return "available_externally "; | 71 return "available_externally "; |
61 default: | 72 default: |
62 return "unknown"; | 73 return "unknown"; |
63 } | 74 } |
64 } | 75 } |
65 | 76 |
66 } // end anonymous namespace | 77 } // end anonymous namespace |
67 | 78 |
68 PNaClABIVerifyModule::PNaClABIVerifyModule() : ModulePass(ID), | |
69 Errors(ErrorsString) {} | |
70 | |
71 bool PNaClABIVerifyModule::runOnModule(Module &M) { | 79 bool PNaClABIVerifyModule::runOnModule(Module &M) { |
72 for (Module::const_global_iterator MI = M.global_begin(), ME = M.global_end(); | 80 for (Module::const_global_iterator MI = M.global_begin(), ME = M.global_end(); |
73 MI != ME; ++MI) { | 81 MI != ME; ++MI) { |
74 // Check types of global variables and their initializers | 82 // Check types of global variables and their initializers |
75 if (!TC.isValidType(MI->getType())) { | 83 if (!TC.isValidType(MI->getType())) { |
76 // GVs are pointers, so print the pointed-to type for clarity | 84 // GVs are pointers, so print the pointed-to type for clarity |
77 Errors << "Variable " + MI->getName() + | 85 Reporter->addError() << "Variable " + MI->getName() + |
78 " has disallowed type: " + | 86 " has disallowed type: " + |
79 PNaClABITypeChecker::getTypeName(MI->getType()->getContainedType(0)) | 87 PNaClABITypeChecker::getTypeName(MI->getType()->getContainedType(0)) |
80 + "\n"; | 88 + "\n"; |
81 } else if (MI->hasInitializer()) { | 89 } else if (MI->hasInitializer()) { |
82 // If the type of the global is bad, no point in checking its initializer | 90 // If the type of the global is bad, no point in checking its initializer |
83 Type *T = TC.checkTypesInConstant(MI->getInitializer()); | 91 Type *T = TC.checkTypesInConstant(MI->getInitializer()); |
84 if (T) | 92 if (T) { |
85 Errors << "Initializer for " + MI->getName() + | 93 Reporter->addError() << "Initializer for " + MI->getName() + |
86 " has disallowed type: " + | 94 " has disallowed type: " + |
87 PNaClABITypeChecker::getTypeName(T) + "\n"; | 95 PNaClABITypeChecker::getTypeName(T) + "\n"; |
96 } | |
88 } | 97 } |
89 | 98 |
90 // Check GV linkage types | 99 // Check GV linkage types |
91 switch (MI->getLinkage()) { | 100 switch (MI->getLinkage()) { |
92 case GlobalValue::ExternalLinkage: | 101 case GlobalValue::ExternalLinkage: |
93 case GlobalValue::AvailableExternallyLinkage: | 102 case GlobalValue::AvailableExternallyLinkage: |
94 case GlobalValue::InternalLinkage: | 103 case GlobalValue::InternalLinkage: |
95 case GlobalValue::PrivateLinkage: | 104 case GlobalValue::PrivateLinkage: |
96 break; | 105 break; |
97 default: | 106 default: |
98 Errors << "Variable " + MI->getName() + | 107 Reporter->addError() << "Variable " + MI->getName() + |
99 " has disallowed linkage type: " + | 108 " has disallowed linkage type: " + |
100 linkageName(MI->getLinkage()) + "\n"; | 109 linkageName(MI->getLinkage()) + "\n"; |
101 } | 110 } |
102 } | 111 } |
103 // No aliases allowed for now. | 112 // No aliases allowed for now. |
104 for (Module::alias_iterator MI = M.alias_begin(), | 113 for (Module::alias_iterator MI = M.alias_begin(), |
105 E = M.alias_end(); MI != E; ++MI) | 114 E = M.alias_end(); MI != E; ++MI) { |
106 Errors << "Variable " + MI->getName() + " is an alias (disallowed)\n"; | 115 Reporter->addError() << "Variable " + MI->getName() + |
116 " is an alias (disallowed)\n"; | |
117 } | |
107 | 118 |
108 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) { | 119 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) { |
109 // Check types of functions and their arguments | 120 // Check types of functions and their arguments |
110 FunctionType *FT = MI->getFunctionType(); | 121 FunctionType *FT = MI->getFunctionType(); |
111 if (!TC.isValidType(FT->getReturnType())) | 122 if (!TC.isValidType(FT->getReturnType())) { |
112 Errors << "Function " + MI->getName() + " has disallowed return type: " + | 123 Reporter->addError() << "Function " + MI->getName() + |
124 " has disallowed return type: " + | |
113 PNaClABITypeChecker::getTypeName(FT->getReturnType()) + "\n"; | 125 PNaClABITypeChecker::getTypeName(FT->getReturnType()) + "\n"; |
126 } | |
114 for (unsigned I = 0, E = FT->getNumParams(); I < E; ++I) { | 127 for (unsigned I = 0, E = FT->getNumParams(); I < E; ++I) { |
115 Type *PT = FT->getParamType(I); | 128 Type *PT = FT->getParamType(I); |
116 if (!TC.isValidType(PT)) | 129 if (!TC.isValidType(PT)) { |
117 Errors << "Function " << MI->getName() << " argument " << I + 1 << | 130 Reporter->addError() << "Function " << MI->getName() << " argument " << |
118 " has disallowed type: " << | 131 I + 1 << " has disallowed type: " << |
119 PNaClABITypeChecker::getTypeName(PT) + "\n"; | 132 PNaClABITypeChecker::getTypeName(PT) + "\n"; |
133 } | |
120 } | 134 } |
121 } | 135 } |
122 | 136 |
123 // Check named metadata nodes | 137 // Check named metadata nodes |
124 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), | 138 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), |
125 E = M.named_metadata_end(); I != E; ++I) { | 139 E = M.named_metadata_end(); I != E; ++I) { |
126 for (unsigned i = 0, e = I->getNumOperands(); i != e; i++) { | 140 for (unsigned i = 0, e = I->getNumOperands(); i != e; i++) { |
127 if (Type *T = TC.checkTypesInMDNode(I->getOperand(i))) | 141 if (Type *T = TC.checkTypesInMDNode(I->getOperand(i))) { |
128 Errors << "Named metadata node " + I->getName() + | 142 Reporter->addError() << "Named metadata node " + I->getName() + |
129 " refers to disallowed type: " + | 143 " refers to disallowed type: " + |
130 PNaClABITypeChecker::getTypeName(T) + "\n"; | 144 PNaClABITypeChecker::getTypeName(T) + "\n"; |
145 } | |
131 } | 146 } |
132 } | 147 } |
133 Errors.flush(); | |
134 return false; | 148 return false; |
135 } | 149 } |
136 | 150 |
151 // This method exists so that the passes can easily be run with opt -analyze. | |
152 // In this case the default constructor is used and we want to reset the error | |
153 // messages after each print (this is more of an issue for the FunctionPass | |
154 // than the ModulePass) | |
137 void PNaClABIVerifyModule::print(llvm::raw_ostream &O, const Module *M) const { | 155 void PNaClABIVerifyModule::print(llvm::raw_ostream &O, const Module *M) const { |
138 O << ErrorsString; | 156 Reporter->printErrors(O); |
157 Reporter->reset(); | |
139 } | 158 } |
140 | 159 |
141 char PNaClABIVerifyModule::ID = 0; | 160 char PNaClABIVerifyModule::ID = 0; |
142 | 161 |
143 static RegisterPass<PNaClABIVerifyModule> X("verify-pnaclabi-module", | 162 static RegisterPass<PNaClABIVerifyModule> X("verify-pnaclabi-module", |
144 "Verify module for PNaCl", false, false); | 163 "Verify module for PNaCl", false, false); |
145 | 164 |
146 ModulePass *llvm::createPNaClABIVerifyModulePass() { | 165 ModulePass *llvm::createPNaClABIVerifyModulePass( |
147 return new PNaClABIVerifyModule(); | 166 PNaClABIErrorReporter *Reporter) { |
167 return new PNaClABIVerifyModule(Reporter); | |
148 } | 168 } |
OLD | NEW |