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/Analysis/NaCl.h" | 17 #include "llvm/Analysis/NaCl.h" |
18 #include "llvm/ADT/Twine.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, public PNaClABIVerifierPass { |
jvoung (off chromium)
2013/03/12 22:16:21
Only the module Pass seems to inherit from PNaClAB
Derek Schuff
2013/03/13 21:51:01
Both should inherit, and use is demonstrated in ll
| |
29 public: | 29 public: |
30 static char ID; | 30 static char ID; |
31 PNaClABIVerifyModule(); | 31 PNaClABIVerifyModule(); |
32 bool runOnModule(Module &M); | 32 bool runOnModule(Module &M); |
33 virtual void print(raw_ostream &O, const Module *M) const; | 33 virtual void print(raw_ostream &O, const Module *M) const; |
34 virtual void setStrict(bool Strict_) { Strict = Strict_; } | |
35 virtual int getErrorCount() { return ErrorCount; } | |
34 private: | 36 private: |
35 PNaClABITypeChecker TC; | 37 PNaClABITypeChecker TC; |
36 std::string ErrorsString; | 38 std::string ErrorsString; |
37 raw_string_ostream Errors; | 39 raw_string_ostream Errors; |
40 bool Strict; | |
41 int ErrorCount; | |
38 }; | 42 }; |
39 | 43 |
40 static const char *linkageName(GlobalValue::LinkageTypes LT) { | 44 static const char *linkageName(GlobalValue::LinkageTypes LT) { |
41 // This logic is taken from PrintLinkage in lib/VMCore/AsmWriter.cpp | 45 // This logic is taken from PrintLinkage in lib/VMCore/AsmWriter.cpp |
42 switch (LT) { | 46 switch (LT) { |
43 case GlobalValue::ExternalLinkage: return "external"; | 47 case GlobalValue::ExternalLinkage: return "external"; |
44 case GlobalValue::PrivateLinkage: return "private "; | 48 case GlobalValue::PrivateLinkage: return "private "; |
45 case GlobalValue::LinkerPrivateLinkage: return "linker_private "; | 49 case GlobalValue::LinkerPrivateLinkage: return "linker_private "; |
46 case GlobalValue::LinkerPrivateWeakLinkage: return "linker_private_weak "; | 50 case GlobalValue::LinkerPrivateWeakLinkage: return "linker_private_weak "; |
47 case GlobalValue::InternalLinkage: return "internal "; | 51 case GlobalValue::InternalLinkage: return "internal "; |
(...skipping 11 matching lines...) Expand all Loading... | |
59 case GlobalValue::AvailableExternallyLinkage: | 63 case GlobalValue::AvailableExternallyLinkage: |
60 return "available_externally "; | 64 return "available_externally "; |
61 default: | 65 default: |
62 return "unknown"; | 66 return "unknown"; |
63 } | 67 } |
64 } | 68 } |
65 | 69 |
66 } // end anonymous namespace | 70 } // end anonymous namespace |
67 | 71 |
68 PNaClABIVerifyModule::PNaClABIVerifyModule() : ModulePass(ID), | 72 PNaClABIVerifyModule::PNaClABIVerifyModule() : ModulePass(ID), |
69 Errors(ErrorsString) {} | 73 Errors(ErrorsString), |
74 Strict(false), | |
75 ErrorCount(0) {} | |
70 | 76 |
71 bool PNaClABIVerifyModule::runOnModule(Module &M) { | 77 bool PNaClABIVerifyModule::runOnModule(Module &M) { |
78 ErrorsString.clear(); | |
79 ErrorCount = 0; | |
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 Errors << "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"; |
89 ErrorCount++; | |
jvoung (off chromium)
2013/03/12 22:16:21
Perhaps just combine in one method the adding of a
Derek Schuff
2013/03/13 21:51:01
Yeah i thought about that but the problem is that
jvoung (off chromium)
2013/03/13 22:40:09
The new version seems good. If you are going to u
Derek Schuff
2013/03/13 22:52:23
Done.
| |
81 } else if (MI->hasInitializer()) { | 90 } else if (MI->hasInitializer()) { |
82 // If the type of the global is bad, no point in checking its initializer | 91 // If the type of the global is bad, no point in checking its initializer |
83 Type *T = TC.checkTypesInConstant(MI->getInitializer()); | 92 Type *T = TC.checkTypesInConstant(MI->getInitializer()); |
84 if (T) | 93 if (T) { |
85 Errors << "Initializer for " + MI->getName() + | 94 Errors << "Initializer for " + MI->getName() + |
86 " has disallowed type: " + | 95 " has disallowed type: " + |
87 PNaClABITypeChecker::getTypeName(T) + "\n"; | 96 PNaClABITypeChecker::getTypeName(T) + "\n"; |
97 ErrorCount++; | |
98 } | |
88 } | 99 } |
89 | 100 |
90 // Check GV linkage types | 101 // Check GV linkage types |
91 switch (MI->getLinkage()) { | 102 switch (MI->getLinkage()) { |
92 case GlobalValue::ExternalLinkage: | 103 case GlobalValue::ExternalLinkage: |
93 case GlobalValue::AvailableExternallyLinkage: | 104 case GlobalValue::AvailableExternallyLinkage: |
94 case GlobalValue::InternalLinkage: | 105 case GlobalValue::InternalLinkage: |
95 case GlobalValue::PrivateLinkage: | 106 case GlobalValue::PrivateLinkage: |
96 break; | 107 break; |
97 default: | 108 default: |
98 Errors << "Variable " + MI->getName() + | 109 Errors << "Variable " + MI->getName() + |
99 " has disallowed linkage type: " + | 110 " has disallowed linkage type: " + |
100 linkageName(MI->getLinkage()) + "\n"; | 111 linkageName(MI->getLinkage()) + "\n"; |
112 ErrorCount++; | |
101 } | 113 } |
102 } | 114 } |
103 // No aliases allowed for now. | 115 // No aliases allowed for now. |
104 for (Module::alias_iterator MI = M.alias_begin(), | 116 for (Module::alias_iterator MI = M.alias_begin(), |
105 E = M.alias_end(); MI != E; ++MI) | 117 E = M.alias_end(); MI != E; ++MI) { |
106 Errors << "Variable " + MI->getName() + " is an alias (disallowed)\n"; | 118 Errors << "Variable " + MI->getName() + " is an alias (disallowed)\n"; |
119 ErrorCount++; | |
120 } | |
107 | 121 |
108 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) { | 122 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) { |
109 // Check types of functions and their arguments | 123 // Check types of functions and their arguments |
110 FunctionType *FT = MI->getFunctionType(); | 124 FunctionType *FT = MI->getFunctionType(); |
111 if (!TC.isValidType(FT->getReturnType())) | 125 if (!TC.isValidType(FT->getReturnType())) { |
112 Errors << "Function " + MI->getName() + " has disallowed return type: " + | 126 Errors << "Function " + MI->getName() + " has disallowed return type: " + |
113 PNaClABITypeChecker::getTypeName(FT->getReturnType()) + "\n"; | 127 PNaClABITypeChecker::getTypeName(FT->getReturnType()) + "\n"; |
128 ErrorCount++; | |
129 } | |
114 for (unsigned I = 0, E = FT->getNumParams(); I < E; ++I) { | 130 for (unsigned I = 0, E = FT->getNumParams(); I < E; ++I) { |
115 Type *PT = FT->getParamType(I); | 131 Type *PT = FT->getParamType(I); |
116 if (!TC.isValidType(PT)) | 132 if (!TC.isValidType(PT)) { |
117 Errors << "Function " << MI->getName() << " argument " << I + 1 << | 133 Errors << "Function " << MI->getName() << " argument " << I + 1 << |
118 " has disallowed type: " << | 134 " has disallowed type: " << |
119 PNaClABITypeChecker::getTypeName(PT) + "\n"; | 135 PNaClABITypeChecker::getTypeName(PT) + "\n"; |
136 ErrorCount++; | |
137 } | |
120 } | 138 } |
121 } | 139 } |
122 | 140 |
123 // Check named metadata nodes | 141 // Check named metadata nodes |
124 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), | 142 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), |
125 E = M.named_metadata_end(); I != E; ++I) { | 143 E = M.named_metadata_end(); I != E; ++I) { |
126 for (unsigned i = 0, e = I->getNumOperands(); i != e; i++) { | 144 for (unsigned i = 0, e = I->getNumOperands(); i != e; i++) { |
127 if (Type *T = TC.checkTypesInMDNode(I->getOperand(i))) | 145 if (Type *T = TC.checkTypesInMDNode(I->getOperand(i))) { |
128 Errors << "Named metadata node " + I->getName() + | 146 Errors << "Named metadata node " + I->getName() + |
129 " refers to disallowed type: " + | 147 " refers to disallowed type: " + |
130 PNaClABITypeChecker::getTypeName(T) + "\n"; | 148 PNaClABITypeChecker::getTypeName(T) + "\n"; |
149 ErrorCount++; | |
150 } | |
131 } | 151 } |
132 } | 152 } |
133 Errors.flush(); | 153 Errors.flush(); |
154 if (Strict && ErrorCount > 0) { | |
155 report_fatal_error("Module is not valid PNaCl bitcode:\n" + ErrorsString); | |
156 } | |
134 return false; | 157 return false; |
135 } | 158 } |
136 | 159 |
137 void PNaClABIVerifyModule::print(llvm::raw_ostream &O, const Module *M) const { | 160 void PNaClABIVerifyModule::print(llvm::raw_ostream &O, const Module *M) const { |
138 O << ErrorsString; | 161 O << ErrorsString; |
139 } | 162 } |
140 | 163 |
141 char PNaClABIVerifyModule::ID = 0; | 164 char PNaClABIVerifyModule::ID = 0; |
142 | 165 |
143 static RegisterPass<PNaClABIVerifyModule> X("verify-pnaclabi-module", | 166 static RegisterPass<PNaClABIVerifyModule> X("verify-pnaclabi-module", |
144 "Verify module for PNaCl", false, false); | 167 "Verify module for PNaCl", false, false); |
145 | 168 |
146 ModulePass *llvm::createPNaClABIVerifyModulePass() { | 169 ModulePass *llvm::createPNaClABIVerifyModulePass(bool Strict) { |
147 return new PNaClABIVerifyModule(); | 170 PNaClABIVerifyModule * P = new PNaClABIVerifyModule(); |
Mark Seaborn
2013/03/12 22:25:56
Spacing should be "*P"
Derek Schuff
2013/03/13 21:51:01
Done.
| |
171 P->setStrict(Strict); | |
172 return P; | |
148 } | 173 } |
OLD | NEW |