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/Module.h" | 20 #include "llvm/IR/Module.h" |
21 #include "llvm/Support/raw_ostream.h" | 21 #include "llvm/Support/raw_ostream.h" |
22 | 22 |
23 #include "PNaClABITypeChecker.h" | 23 #include "PNaClABITypeChecker.h" |
24 using namespace llvm; | 24 using namespace llvm; |
25 | 25 |
| 26 namespace llvm { |
| 27 cl::opt<bool> |
| 28 PNaClABIAllowDebugMetadata("pnaclabi-allow-debug-metadata", |
| 29 cl::desc("Allow debug metadata during PNaCl ABI verification."), |
| 30 cl::init(false)); |
| 31 } |
| 32 |
26 namespace { | 33 namespace { |
27 // This pass should not touch function bodies, to stay streaming-friendly | 34 // This pass should not touch function bodies, to stay streaming-friendly |
28 class PNaClABIVerifyModule : public ModulePass { | 35 class PNaClABIVerifyModule : public ModulePass { |
29 public: | 36 public: |
30 static char ID; | 37 static char ID; |
31 PNaClABIVerifyModule() : | 38 PNaClABIVerifyModule() : |
32 ModulePass(ID), | 39 ModulePass(ID), |
33 Reporter(new PNaClABIErrorReporter), | 40 Reporter(new PNaClABIErrorReporter), |
34 ReporterIsOwned(true) { | 41 ReporterIsOwned(true) { |
35 initializePNaClABIVerifyModulePass(*PassRegistry::getPassRegistry()); | 42 initializePNaClABIVerifyModulePass(*PassRegistry::getPassRegistry()); |
36 } | 43 } |
37 explicit PNaClABIVerifyModule(PNaClABIErrorReporter *Reporter_) : | 44 explicit PNaClABIVerifyModule(PNaClABIErrorReporter *Reporter_) : |
38 ModulePass(ID), | 45 ModulePass(ID), |
39 Reporter(Reporter_), | 46 Reporter(Reporter_), |
40 ReporterIsOwned(false) { | 47 ReporterIsOwned(false) { |
41 initializePNaClABIVerifyModulePass(*PassRegistry::getPassRegistry()); | 48 initializePNaClABIVerifyModulePass(*PassRegistry::getPassRegistry()); |
42 } | 49 } |
43 ~PNaClABIVerifyModule() { | 50 ~PNaClABIVerifyModule() { |
44 if (ReporterIsOwned) | 51 if (ReporterIsOwned) |
45 delete Reporter; | 52 delete Reporter; |
46 } | 53 } |
47 bool runOnModule(Module &M); | 54 bool runOnModule(Module &M); |
48 virtual void print(raw_ostream &O, const Module *M) const; | 55 virtual void print(raw_ostream &O, const Module *M) const; |
49 private: | 56 private: |
50 void CheckGlobalValueCommon(const GlobalValue *GV); | 57 void CheckGlobalValueCommon(const GlobalValue *GV); |
| 58 bool IsWhitelistedMetadata(const NamedMDNode *MD); |
51 PNaClABITypeChecker TC; | 59 PNaClABITypeChecker TC; |
52 PNaClABIErrorReporter *Reporter; | 60 PNaClABIErrorReporter *Reporter; |
53 bool ReporterIsOwned; | 61 bool ReporterIsOwned; |
54 }; | 62 }; |
55 | 63 |
56 static const char *linkageName(GlobalValue::LinkageTypes LT) { | 64 static const char *linkageName(GlobalValue::LinkageTypes LT) { |
57 // This logic is taken from PrintLinkage in lib/VMCore/AsmWriter.cpp | 65 // This logic is taken from PrintLinkage in lib/VMCore/AsmWriter.cpp |
58 switch (LT) { | 66 switch (LT) { |
59 case GlobalValue::ExternalLinkage: return "external"; | 67 case GlobalValue::ExternalLinkage: return "external"; |
60 case GlobalValue::PrivateLinkage: return "private "; | 68 case GlobalValue::PrivateLinkage: return "private "; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 Reporter->addError() << GVTypeName << GV->getName() | 106 Reporter->addError() << GVTypeName << GV->getName() |
99 << " has disallowed linkage type: " | 107 << " has disallowed linkage type: " |
100 << linkageName(GV->getLinkage()) << "\n"; | 108 << linkageName(GV->getLinkage()) << "\n"; |
101 } | 109 } |
102 if (GV->hasSection()) { | 110 if (GV->hasSection()) { |
103 Reporter->addError() << GVTypeName << GV->getName() << | 111 Reporter->addError() << GVTypeName << GV->getName() << |
104 " has disallowed \"section\" attribute\n"; | 112 " has disallowed \"section\" attribute\n"; |
105 } | 113 } |
106 } | 114 } |
107 | 115 |
| 116 bool PNaClABIVerifyModule::IsWhitelistedMetadata(const NamedMDNode* MD) { |
| 117 return MD->getName().startswith("llvm.dbg.") && PNaClABIAllowDebugMetadata; |
| 118 } |
| 119 |
108 bool PNaClABIVerifyModule::runOnModule(Module &M) { | 120 bool PNaClABIVerifyModule::runOnModule(Module &M) { |
109 for (Module::const_global_iterator MI = M.global_begin(), ME = M.global_end(); | 121 for (Module::const_global_iterator MI = M.global_begin(), ME = M.global_end(); |
110 MI != ME; ++MI) { | 122 MI != ME; ++MI) { |
111 // Check types of global variables and their initializers | 123 // Check types of global variables and their initializers |
112 if (!TC.isValidType(MI->getType())) { | 124 if (!TC.isValidType(MI->getType())) { |
113 // GVs are pointers, so print the pointed-to type for clarity | 125 // GVs are pointers, so print the pointed-to type for clarity |
114 Reporter->addError() << "Variable " << MI->getName() << | 126 Reporter->addError() << "Variable " << MI->getName() << |
115 " has disallowed type: " << | 127 " has disallowed type: " << |
116 PNaClABITypeChecker::getTypeName(MI->getType()->getContainedType(0)) | 128 PNaClABITypeChecker::getTypeName(MI->getType()->getContainedType(0)) |
117 + "\n"; | 129 + "\n"; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 179 |
168 if (MI->hasGC()) { | 180 if (MI->hasGC()) { |
169 Reporter->addError() << "Function " << MI->getName() << | 181 Reporter->addError() << "Function " << MI->getName() << |
170 " has disallowed \"gc\" attribute\n"; | 182 " has disallowed \"gc\" attribute\n"; |
171 } | 183 } |
172 } | 184 } |
173 | 185 |
174 // Check named metadata nodes | 186 // Check named metadata nodes |
175 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), | 187 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), |
176 E = M.named_metadata_end(); I != E; ++I) { | 188 E = M.named_metadata_end(); I != E; ++I) { |
177 for (unsigned i = 0, e = I->getNumOperands(); i != e; i++) { | 189 if (!IsWhitelistedMetadata(I)) { |
178 if (Type *T = TC.checkTypesInMDNode(I->getOperand(i))) { | 190 Reporter->addError() << "Named metadata node " << I->getName() |
179 Reporter->addError() << "Named metadata node " << I->getName() << | 191 << " is disallowed\n"; |
180 " refers to disallowed type: " << | 192 } else { |
181 PNaClABITypeChecker::getTypeName(T) << "\n"; | 193 // Check the types in the metadata. |
| 194 for (unsigned i = 0, e = I->getNumOperands(); i != e; i++) { |
| 195 if (Type *T = TC.checkTypesInMDNode(I->getOperand(i))) { |
| 196 Reporter->addError() << "Named metadata node " << I->getName() |
| 197 << " refers to disallowed type: " |
| 198 << PNaClABITypeChecker::getTypeName(T) << "\n"; |
| 199 } |
182 } | 200 } |
183 } | 201 } |
184 } | 202 } |
185 | 203 |
186 Reporter->checkForFatalErrors(); | 204 Reporter->checkForFatalErrors(); |
187 return false; | 205 return false; |
188 } | 206 } |
189 | 207 |
190 // This method exists so that the passes can easily be run with opt -analyze. | 208 // This method exists so that the passes can easily be run with opt -analyze. |
191 // In this case the default constructor is used and we want to reset the error | 209 // In this case the default constructor is used and we want to reset the error |
192 // messages after each print (this is more of an issue for the FunctionPass | 210 // messages after each print (this is more of an issue for the FunctionPass |
193 // than the ModulePass) | 211 // than the ModulePass) |
194 void PNaClABIVerifyModule::print(llvm::raw_ostream &O, const Module *M) const { | 212 void PNaClABIVerifyModule::print(llvm::raw_ostream &O, const Module *M) const { |
195 Reporter->printErrors(O); | 213 Reporter->printErrors(O); |
196 Reporter->reset(); | 214 Reporter->reset(); |
197 } | 215 } |
198 | 216 |
199 char PNaClABIVerifyModule::ID = 0; | 217 char PNaClABIVerifyModule::ID = 0; |
200 INITIALIZE_PASS(PNaClABIVerifyModule, "verify-pnaclabi-module", | 218 INITIALIZE_PASS(PNaClABIVerifyModule, "verify-pnaclabi-module", |
201 "Verify module for PNaCl", false, true) | 219 "Verify module for PNaCl", false, true) |
202 | 220 |
203 ModulePass *llvm::createPNaClABIVerifyModulePass( | 221 ModulePass *llvm::createPNaClABIVerifyModulePass( |
204 PNaClABIErrorReporter *Reporter) { | 222 PNaClABIErrorReporter *Reporter) { |
205 return new PNaClABIVerifyModule(Reporter); | 223 return new PNaClABIVerifyModule(Reporter); |
206 } | 224 } |
OLD | NEW |