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/Twine.h" | 17 #include "llvm/ADT/Twine.h" |
18 #include "llvm/Analysis/NaCl.h" | 18 #include "llvm/Analysis/NaCl.h" |
19 #include "llvm/IR/DerivedTypes.h" | 19 #include "llvm/IR/DerivedTypes.h" |
20 #include "llvm/IR/Intrinsics.h" | |
20 #include "llvm/IR/Module.h" | 21 #include "llvm/IR/Module.h" |
22 #include "llvm/Support/Debug.h" | |
21 #include "llvm/Support/raw_ostream.h" | 23 #include "llvm/Support/raw_ostream.h" |
22 | 24 |
23 #include "PNaClABITypeChecker.h" | 25 #include "PNaClABITypeChecker.h" |
24 using namespace llvm; | 26 using namespace llvm; |
25 | 27 |
26 namespace llvm { | 28 namespace llvm { |
27 cl::opt<bool> | 29 cl::opt<bool> |
28 PNaClABIAllowDebugMetadata("pnaclabi-allow-debug-metadata", | 30 PNaClABIAllowDebugMetadata("pnaclabi-allow-debug-metadata", |
29 cl::desc("Allow debug metadata during PNaCl ABI verification."), | 31 cl::desc("Allow debug metadata during PNaCl ABI verification."), |
30 cl::init(false)); | 32 cl::init(false)); |
33 | |
31 } | 34 } |
32 | 35 |
36 static cl::opt<bool> | |
37 PNaClABIAllowDevIntrinsics("pnaclabi-allow-dev-intrinsics", | |
38 cl::desc("Allow all LLVM intrinsics duing PNaCl ABI verification."), | |
eliben
2013/05/07 17:49:36
duing
jvoung (off chromium)
2013/05/07 21:24:41
Done.
| |
39 cl::init(true)); // TODO(jvoung): Make this false by default. | |
40 | |
33 namespace { | 41 namespace { |
34 // This pass should not touch function bodies, to stay streaming-friendly | 42 // This pass should not touch function bodies, to stay streaming-friendly |
35 class PNaClABIVerifyModule : public ModulePass { | 43 class PNaClABIVerifyModule : public ModulePass { |
36 public: | 44 public: |
37 static char ID; | 45 static char ID; |
38 PNaClABIVerifyModule() : | 46 PNaClABIVerifyModule() : |
39 ModulePass(ID), | 47 ModulePass(ID), |
40 Reporter(new PNaClABIErrorReporter), | 48 Reporter(new PNaClABIErrorReporter), |
41 ReporterIsOwned(true) { | 49 ReporterIsOwned(true) { |
42 initializePNaClABIVerifyModulePass(*PassRegistry::getPassRegistry()); | 50 initializePNaClABIVerifyModulePass(*PassRegistry::getPassRegistry()); |
43 } | 51 } |
44 explicit PNaClABIVerifyModule(PNaClABIErrorReporter *Reporter_) : | 52 explicit PNaClABIVerifyModule(PNaClABIErrorReporter *Reporter_) : |
45 ModulePass(ID), | 53 ModulePass(ID), |
46 Reporter(Reporter_), | 54 Reporter(Reporter_), |
47 ReporterIsOwned(false) { | 55 ReporterIsOwned(false) { |
48 initializePNaClABIVerifyModulePass(*PassRegistry::getPassRegistry()); | 56 initializePNaClABIVerifyModulePass(*PassRegistry::getPassRegistry()); |
49 } | 57 } |
50 ~PNaClABIVerifyModule() { | 58 ~PNaClABIVerifyModule() { |
51 if (ReporterIsOwned) | 59 if (ReporterIsOwned) |
52 delete Reporter; | 60 delete Reporter; |
53 } | 61 } |
54 bool runOnModule(Module &M); | 62 bool runOnModule(Module &M); |
55 virtual void print(raw_ostream &O, const Module *M) const; | 63 virtual void print(raw_ostream &O, const Module *M) const; |
56 private: | 64 private: |
57 void CheckGlobalValueCommon(const GlobalValue *GV); | 65 void CheckGlobalValueCommon(const GlobalValue *GV); |
66 bool IsWhitelistedIntrinsic(const Function* F, unsigned ID); | |
58 bool IsWhitelistedMetadata(const NamedMDNode *MD); | 67 bool IsWhitelistedMetadata(const NamedMDNode *MD); |
59 PNaClABITypeChecker TC; | 68 PNaClABITypeChecker TC; |
60 PNaClABIErrorReporter *Reporter; | 69 PNaClABIErrorReporter *Reporter; |
61 bool ReporterIsOwned; | 70 bool ReporterIsOwned; |
62 }; | 71 }; |
63 | 72 |
64 static const char *linkageName(GlobalValue::LinkageTypes LT) { | 73 static const char *linkageName(GlobalValue::LinkageTypes LT) { |
65 // This logic is taken from PrintLinkage in lib/VMCore/AsmWriter.cpp | 74 // This logic is taken from PrintLinkage in lib/VMCore/AsmWriter.cpp |
66 switch (LT) { | 75 switch (LT) { |
67 case GlobalValue::ExternalLinkage: return "external"; | 76 case GlobalValue::ExternalLinkage: return "external"; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 Reporter->addError() << GVTypeName << GV->getName() | 115 Reporter->addError() << GVTypeName << GV->getName() |
107 << " has disallowed linkage type: " | 116 << " has disallowed linkage type: " |
108 << linkageName(GV->getLinkage()) << "\n"; | 117 << linkageName(GV->getLinkage()) << "\n"; |
109 } | 118 } |
110 if (GV->hasSection()) { | 119 if (GV->hasSection()) { |
111 Reporter->addError() << GVTypeName << GV->getName() << | 120 Reporter->addError() << GVTypeName << GV->getName() << |
112 " has disallowed \"section\" attribute\n"; | 121 " has disallowed \"section\" attribute\n"; |
113 } | 122 } |
114 } | 123 } |
115 | 124 |
125 bool PNaClABIVerifyModule::IsWhitelistedIntrinsic(const Function* F, | |
126 unsigned ID) { | |
127 // Keep 3 categories of intrinsics for now. | |
128 // (1) Allowed always | |
129 // (2) Never allowed | |
130 // (3) "Dev" intrinsics, which may or may not be allowed. | |
131 // "Dev" intrinsics are controlled by the PNaClABIAllowDevIntrinsics flag. | |
132 // Please keep these sorted within each category. | |
133 switch(ID) { | |
134 default: { | |
135 StringRef name = F->getName(); | |
136 // Disallow target-specific intrinsics | |
137 // (see list of target TD includes in Intrinsics.td). | |
138 if (name.startswith("llvm.arm.") | |
139 || name.startswith("llvm.hexagon.") | |
140 || name.startswith("llvm.mips.") | |
141 || name.startswith("llvm.nvvm.") | |
142 || name.startswith("llvm.ppc.") | |
143 || name.startswith("llvm.r600.") | |
144 || name.startswith("llvm.x86.") | |
145 || name.startswith("llvm.xcore.")) { | |
146 return false; | |
147 } else { | |
148 DEBUG(dbgs() << "Unknown intrinsic: " << F->getName() << "\n"); | |
eliben
2013/05/07 17:49:36
Again, I'm not sure why we can't:
Detect (1) and
jvoung (off chromium)
2013/05/07 21:24:41
Added the list of stuff triggered by our scons + g
| |
149 // TODO(jvoung): Dev intrinsics should be listed out, instead | |
150 // of being caught by the default case. | |
151 return PNaClABIAllowDevIntrinsics; | |
152 } | |
153 } | |
154 // (1) Always allowed. | |
155 case Intrinsic::invariant_end: | |
156 case Intrinsic::invariant_start: | |
157 case Intrinsic::lifetime_end: | |
158 case Intrinsic::lifetime_start: | |
159 case Intrinsic::memcpy: | |
160 case Intrinsic::memmove: | |
161 case Intrinsic::memset: | |
162 case Intrinsic::nacl_read_tp: | |
163 case Intrinsic::trap: | |
164 return true; | |
165 // (2) Never allowed. | |
166 case Intrinsic::not_intrinsic: | |
167 // Trampoline intrinsics depend on target-specific-sized buffer. | |
168 // Perhaps if the allocator was target-independent, then this could | |
169 // be accepted. | |
170 case Intrinsic::adjust_trampoline: | |
171 case Intrinsic::init_trampoline: | |
172 // Var-args code is expanded out, so we shouldn't need va_arg intrinsics. | |
173 case Intrinsic::vacopy: | |
174 case Intrinsic::vaend: | |
175 case Intrinsic::vastart: | |
176 return false; | |
177 // (3) Dev intrinsics. | |
178 case Intrinsic::dbg_declare: | |
179 case Intrinsic::dbg_value: | |
180 case Intrinsic::frameaddress: // Support for 0-level or not? | |
181 case Intrinsic::returnaddress: // Support for 0-level or not? | |
182 return PNaClABIAllowDevIntrinsics || PNaClABIAllowDebugMetadata; | |
183 } | |
184 } | |
185 | |
116 bool PNaClABIVerifyModule::IsWhitelistedMetadata(const NamedMDNode* MD) { | 186 bool PNaClABIVerifyModule::IsWhitelistedMetadata(const NamedMDNode* MD) { |
117 return MD->getName().startswith("llvm.dbg.") && PNaClABIAllowDebugMetadata; | 187 return MD->getName().startswith("llvm.dbg.") && PNaClABIAllowDebugMetadata; |
118 } | 188 } |
119 | 189 |
120 bool PNaClABIVerifyModule::runOnModule(Module &M) { | 190 bool PNaClABIVerifyModule::runOnModule(Module &M) { |
121 for (Module::const_global_iterator MI = M.global_begin(), ME = M.global_end(); | 191 for (Module::const_global_iterator MI = M.global_begin(), ME = M.global_end(); |
122 MI != ME; ++MI) { | 192 MI != ME; ++MI) { |
123 // Check types of global variables and their initializers | 193 // Check types of global variables and their initializers |
124 if (!TC.isValidType(MI->getType())) { | 194 if (!TC.isValidType(MI->getType())) { |
125 // GVs are pointers, so print the pointed-to type for clarity | 195 // GVs are pointers, so print the pointed-to type for clarity |
(...skipping 20 matching lines...) Expand all Loading... | |
146 } | 216 } |
147 | 217 |
148 // No aliases allowed for now. | 218 // No aliases allowed for now. |
149 for (Module::alias_iterator MI = M.alias_begin(), | 219 for (Module::alias_iterator MI = M.alias_begin(), |
150 E = M.alias_end(); MI != E; ++MI) { | 220 E = M.alias_end(); MI != E; ++MI) { |
151 Reporter->addError() << "Variable " << MI->getName() << | 221 Reporter->addError() << "Variable " << MI->getName() << |
152 " is an alias (disallowed)\n"; | 222 " is an alias (disallowed)\n"; |
153 } | 223 } |
154 | 224 |
155 for (Module::const_iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) { | 225 for (Module::const_iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) { |
226 // Check intrinsics. | |
227 if (MI->isIntrinsic()) { | |
eliben
2013/05/07 17:49:36
Any reason not to combine into a single condition?
jvoung (off chromium)
2013/05/07 21:24:41
Done.
| |
228 if (!IsWhitelistedIntrinsic(MI, MI->getIntrinsicID())) { | |
229 Reporter->addError() << "Function " << MI->getName() | |
230 << " is a disallowed LLVM intrinsic\n"; | |
231 } | |
232 } | |
233 | |
156 // Check types of functions and their arguments | 234 // Check types of functions and their arguments |
157 FunctionType *FT = MI->getFunctionType(); | 235 FunctionType *FT = MI->getFunctionType(); |
158 if (!TC.isValidType(FT->getReturnType())) { | 236 if (!TC.isValidType(FT->getReturnType())) { |
159 Reporter->addError() << "Function " << MI->getName() << | 237 Reporter->addError() << "Function " << MI->getName() << |
160 " has disallowed return type: " << | 238 " has disallowed return type: " << |
161 PNaClABITypeChecker::getTypeName(FT->getReturnType()) << "\n"; | 239 PNaClABITypeChecker::getTypeName(FT->getReturnType()) << "\n"; |
162 } | 240 } |
163 for (unsigned I = 0, E = FT->getNumParams(); I < E; ++I) { | 241 for (unsigned I = 0, E = FT->getNumParams(); I < E; ++I) { |
164 Type *PT = FT->getParamType(I); | 242 Type *PT = FT->getParamType(I); |
165 if (!TC.isValidType(PT)) { | 243 if (!TC.isValidType(PT)) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 } | 293 } |
216 | 294 |
217 char PNaClABIVerifyModule::ID = 0; | 295 char PNaClABIVerifyModule::ID = 0; |
218 INITIALIZE_PASS(PNaClABIVerifyModule, "verify-pnaclabi-module", | 296 INITIALIZE_PASS(PNaClABIVerifyModule, "verify-pnaclabi-module", |
219 "Verify module for PNaCl", false, true) | 297 "Verify module for PNaCl", false, true) |
220 | 298 |
221 ModulePass *llvm::createPNaClABIVerifyModulePass( | 299 ModulePass *llvm::createPNaClABIVerifyModulePass( |
222 PNaClABIErrorReporter *Reporter) { | 300 PNaClABIErrorReporter *Reporter) { |
223 return new PNaClABIVerifyModule(Reporter); | 301 return new PNaClABIVerifyModule(Reporter); |
224 } | 302 } |
OLD | NEW |