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