Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(867)

Side by Side Diff: lib/Analysis/NaCl/PNaClABIVerifyModule.cpp

Issue 12449014: ABI verifier: Add standalone tool pnacl-abicheck (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: cleanup, use << for error streams uniformly Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/Analysis/NaCl/PNaClABIVerifyFunctions.cpp ('k') | tools/CMakeLists.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/Analysis/NaCl.h" 18 #include "llvm/Analysis/NaCl.h"
18 #include "llvm/ADT/Twine.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 { 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() : ModulePass(ID),
32 Reporter(new PNaClABIErrorReporter),
33 ReporterIsOwned(true) {}
34 explicit PNaClABIVerifyModule(PNaClABIErrorReporter *Reporter_) :
35 ModulePass(ID),
36 Reporter(Reporter_),
37 ReporterIsOwned(false) {}
38 ~PNaClABIVerifyModule() {
39 if (ReporterIsOwned)
40 delete Reporter;
41 }
32 bool runOnModule(Module &M); 42 bool runOnModule(Module &M);
33 virtual void print(raw_ostream &O, const Module *M) const; 43 virtual void print(raw_ostream &O, const Module *M) const;
34 private: 44 private:
35 PNaClABITypeChecker TC; 45 PNaClABITypeChecker TC;
36 std::string ErrorsString; 46 PNaClABIErrorReporter *Reporter;
37 raw_string_ostream Errors; 47 bool ReporterIsOwned;
38 }; 48 };
39 49
40 static const char *linkageName(GlobalValue::LinkageTypes LT) { 50 static const char *linkageName(GlobalValue::LinkageTypes LT) {
41 // This logic is taken from PrintLinkage in lib/VMCore/AsmWriter.cpp 51 // This logic is taken from PrintLinkage in lib/VMCore/AsmWriter.cpp
42 switch (LT) { 52 switch (LT) {
43 case GlobalValue::ExternalLinkage: return "external"; 53 case GlobalValue::ExternalLinkage: return "external";
44 case GlobalValue::PrivateLinkage: return "private "; 54 case GlobalValue::PrivateLinkage: return "private ";
45 case GlobalValue::LinkerPrivateLinkage: return "linker_private "; 55 case GlobalValue::LinkerPrivateLinkage: return "linker_private ";
46 case GlobalValue::LinkerPrivateWeakLinkage: return "linker_private_weak "; 56 case GlobalValue::LinkerPrivateWeakLinkage: return "linker_private_weak ";
47 case GlobalValue::InternalLinkage: return "internal "; 57 case GlobalValue::InternalLinkage: return "internal ";
(...skipping 10 matching lines...) Expand all
58 case GlobalValue::ExternalWeakLinkage: return "extern_weak "; 68 case GlobalValue::ExternalWeakLinkage: return "extern_weak ";
59 case GlobalValue::AvailableExternallyLinkage: 69 case GlobalValue::AvailableExternallyLinkage:
60 return "available_externally "; 70 return "available_externally ";
61 default: 71 default:
62 return "unknown"; 72 return "unknown";
63 } 73 }
64 } 74 }
65 75
66 } // end anonymous namespace 76 } // end anonymous namespace
67 77
68 PNaClABIVerifyModule::PNaClABIVerifyModule() : ModulePass(ID),
69 Errors(ErrorsString) {}
70
71 bool PNaClABIVerifyModule::runOnModule(Module &M) { 78 bool PNaClABIVerifyModule::runOnModule(Module &M) {
72 for (Module::const_global_iterator MI = M.global_begin(), ME = M.global_end(); 79 for (Module::const_global_iterator MI = M.global_begin(), ME = M.global_end();
73 MI != ME; ++MI) { 80 MI != ME; ++MI) {
74 // Check types of global variables and their initializers 81 // Check types of global variables and their initializers
75 if (!TC.isValidType(MI->getType())) { 82 if (!TC.isValidType(MI->getType())) {
76 // GVs are pointers, so print the pointed-to type for clarity 83 // GVs are pointers, so print the pointed-to type for clarity
77 Errors << "Variable " + MI->getName() + 84 Reporter->addError() << "Variable " << MI->getName() <<
78 " has disallowed type: " + 85 " has disallowed type: " <<
79 PNaClABITypeChecker::getTypeName(MI->getType()->getContainedType(0)) 86 PNaClABITypeChecker::getTypeName(MI->getType()->getContainedType(0))
80 + "\n"; 87 + "\n";
81 } else if (MI->hasInitializer()) { 88 } else if (MI->hasInitializer()) {
82 // If the type of the global is bad, no point in checking its initializer 89 // If the type of the global is bad, no point in checking its initializer
83 Type *T = TC.checkTypesInConstant(MI->getInitializer()); 90 Type *T = TC.checkTypesInConstant(MI->getInitializer());
84 if (T) 91 if (T) {
85 Errors << "Initializer for " + MI->getName() + 92 Reporter->addError() << "Initializer for " << MI->getName() <<
86 " has disallowed type: " + 93 " has disallowed type: " <<
87 PNaClABITypeChecker::getTypeName(T) + "\n"; 94 PNaClABITypeChecker::getTypeName(T) << "\n";
95 }
88 } 96 }
89 97
90 // Check GV linkage types 98 // Check GV linkage types
91 switch (MI->getLinkage()) { 99 switch (MI->getLinkage()) {
92 case GlobalValue::ExternalLinkage: 100 case GlobalValue::ExternalLinkage:
93 case GlobalValue::AvailableExternallyLinkage: 101 case GlobalValue::AvailableExternallyLinkage:
94 case GlobalValue::InternalLinkage: 102 case GlobalValue::InternalLinkage:
95 case GlobalValue::PrivateLinkage: 103 case GlobalValue::PrivateLinkage:
96 break; 104 break;
97 default: 105 default:
98 Errors << "Variable " + MI->getName() + 106 Reporter->addError() << "Variable " << MI->getName() <<
99 " has disallowed linkage type: " + 107 " has disallowed linkage type: " <<
100 linkageName(MI->getLinkage()) + "\n"; 108 linkageName(MI->getLinkage()) << "\n";
101 } 109 }
102 } 110 }
103 // No aliases allowed for now. 111 // No aliases allowed for now.
104 for (Module::alias_iterator MI = M.alias_begin(), 112 for (Module::alias_iterator MI = M.alias_begin(),
105 E = M.alias_end(); MI != E; ++MI) 113 E = M.alias_end(); MI != E; ++MI) {
106 Errors << "Variable " + MI->getName() + " is an alias (disallowed)\n"; 114 Reporter->addError() << "Variable " << MI->getName() <<
115 " is an alias (disallowed)\n";
116 }
107 117
108 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) { 118 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) {
109 // Check types of functions and their arguments 119 // Check types of functions and their arguments
110 FunctionType *FT = MI->getFunctionType(); 120 FunctionType *FT = MI->getFunctionType();
111 if (!TC.isValidType(FT->getReturnType())) 121 if (!TC.isValidType(FT->getReturnType())) {
112 Errors << "Function " + MI->getName() + " has disallowed return type: " + 122 Reporter->addError() << "Function " << MI->getName() <<
113 PNaClABITypeChecker::getTypeName(FT->getReturnType()) + "\n"; 123 " has disallowed return type: " <<
124 PNaClABITypeChecker::getTypeName(FT->getReturnType()) << "\n";
125 }
114 for (unsigned I = 0, E = FT->getNumParams(); I < E; ++I) { 126 for (unsigned I = 0, E = FT->getNumParams(); I < E; ++I) {
115 Type *PT = FT->getParamType(I); 127 Type *PT = FT->getParamType(I);
116 if (!TC.isValidType(PT)) 128 if (!TC.isValidType(PT)) {
117 Errors << "Function " << MI->getName() << " argument " << I + 1 << 129 Reporter->addError() << "Function " << MI->getName() << " argument " <<
118 " has disallowed type: " << 130 I + 1 << " has disallowed type: " <<
119 PNaClABITypeChecker::getTypeName(PT) + "\n"; 131 PNaClABITypeChecker::getTypeName(PT) << "\n";
132 }
120 } 133 }
121 } 134 }
122 135
123 // Check named metadata nodes 136 // Check named metadata nodes
124 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), 137 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
125 E = M.named_metadata_end(); I != E; ++I) { 138 E = M.named_metadata_end(); I != E; ++I) {
126 for (unsigned i = 0, e = I->getNumOperands(); i != e; i++) { 139 for (unsigned i = 0, e = I->getNumOperands(); i != e; i++) {
127 if (Type *T = TC.checkTypesInMDNode(I->getOperand(i))) 140 if (Type *T = TC.checkTypesInMDNode(I->getOperand(i))) {
128 Errors << "Named metadata node " + I->getName() + 141 Reporter->addError() << "Named metadata node " << I->getName() <<
129 " refers to disallowed type: " + 142 " refers to disallowed type: " <<
130 PNaClABITypeChecker::getTypeName(T) + "\n"; 143 PNaClABITypeChecker::getTypeName(T) << "\n";
144 }
131 } 145 }
132 } 146 }
133 Errors.flush();
134 return false; 147 return false;
135 } 148 }
136 149
150 // This method exists so that the passes can easily be run with opt -analyze.
151 // In this case the default constructor is used and we want to reset the error
152 // messages after each print (this is more of an issue for the FunctionPass
153 // than the ModulePass)
137 void PNaClABIVerifyModule::print(llvm::raw_ostream &O, const Module *M) const { 154 void PNaClABIVerifyModule::print(llvm::raw_ostream &O, const Module *M) const {
138 O << ErrorsString; 155 Reporter->printErrors(O);
156 Reporter->reset();
139 } 157 }
140 158
141 char PNaClABIVerifyModule::ID = 0; 159 char PNaClABIVerifyModule::ID = 0;
142 160
143 static RegisterPass<PNaClABIVerifyModule> X("verify-pnaclabi-module", 161 static RegisterPass<PNaClABIVerifyModule> X("verify-pnaclabi-module",
144 "Verify module for PNaCl", false, false); 162 "Verify module for PNaCl", false, false);
145 163
146 ModulePass *llvm::createPNaClABIVerifyModulePass() { 164 ModulePass *llvm::createPNaClABIVerifyModulePass(
147 return new PNaClABIVerifyModule(); 165 PNaClABIErrorReporter *Reporter) {
166 return new PNaClABIVerifyModule(Reporter);
148 } 167 }
OLDNEW
« no previous file with comments | « lib/Analysis/NaCl/PNaClABIVerifyFunctions.cpp ('k') | tools/CMakeLists.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698