OLD | NEW |
---|---|
1 //===- PNaClABIVerifyFunctions.cpp - Verify PNaCl ABI rules --------===// | 1 //===- PNaClABIVerifyFunctions.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 function-level PNaCl ABI requirements. | 10 // Verify function-level PNaCl ABI requirements. |
11 // | 11 // |
12 // | 12 // |
13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
14 | 14 |
15 #include "llvm/Pass.h" | 15 #include "llvm/Pass.h" |
16 #include "llvm/ADT/Twine.h" | 16 #include "llvm/ADT/Twine.h" |
17 #include "llvm/Analysis/NaCl.h" | 17 #include "llvm/Analysis/NaCl.h" |
18 #include "llvm/IR/Function.h" | 18 #include "llvm/IR/Function.h" |
19 #include "llvm/IR/Instructions.h" | 19 #include "llvm/IR/Instructions.h" |
20 #include "llvm/IR/LLVMContext.h" | |
20 #include "llvm/IR/Metadata.h" | 21 #include "llvm/IR/Metadata.h" |
21 #include "llvm/Support/raw_ostream.h" | 22 #include "llvm/Support/raw_ostream.h" |
22 | 23 |
23 #include "PNaClABITypeChecker.h" | 24 #include "PNaClABITypeChecker.h" |
24 using namespace llvm; | 25 using namespace llvm; |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 // Checks that examine anything in the function body should be in | 29 // Checks that examine anything in the function body should be in |
29 // FunctionPasses to make them streaming-friendly | 30 // FunctionPasses to make them streaming-friendly |
(...skipping 12 matching lines...) Expand all Loading... | |
42 ReporterIsOwned(false) { | 43 ReporterIsOwned(false) { |
43 initializePNaClABIVerifyFunctionsPass(*PassRegistry::getPassRegistry()); | 44 initializePNaClABIVerifyFunctionsPass(*PassRegistry::getPassRegistry()); |
44 } | 45 } |
45 ~PNaClABIVerifyFunctions() { | 46 ~PNaClABIVerifyFunctions() { |
46 if (ReporterIsOwned) | 47 if (ReporterIsOwned) |
47 delete Reporter; | 48 delete Reporter; |
48 } | 49 } |
49 bool runOnFunction(Function &F); | 50 bool runOnFunction(Function &F); |
50 virtual void print(raw_ostream &O, const Module *M) const; | 51 virtual void print(raw_ostream &O, const Module *M) const; |
51 private: | 52 private: |
53 bool IsWhitelistedMetadata(unsigned MDKind); | |
52 PNaClABITypeChecker TC; | 54 PNaClABITypeChecker TC; |
53 PNaClABIErrorReporter *Reporter; | 55 PNaClABIErrorReporter *Reporter; |
54 bool ReporterIsOwned; | 56 bool ReporterIsOwned; |
55 }; | 57 }; |
56 | 58 |
59 // There's no built-in way to get the name of an MDNode, so use a | |
60 // string ostream to print it. | |
61 std::string getMDNodeString(const MDNode *MD) { | |
62 std::string s; | |
63 raw_string_ostream N(s); | |
64 MD->print(N); | |
65 return N.str(); | |
66 } | |
67 | |
57 } // and anonymous namespace | 68 } // and anonymous namespace |
58 | 69 |
70 bool PNaClABIVerifyFunctions::IsWhitelistedMetadata(unsigned MDKind) { | |
71 return MDKind == LLVMContext::MD_dbg && llvm::PNaClABIAllowDebugMetadata; | |
Derek Schuff
2013/04/26 00:34:34
llvm:: not necessary if using namespace llvm?
jvoung (off chromium)
2013/04/26 16:51:29
Done.
| |
72 } | |
73 | |
59 bool PNaClABIVerifyFunctions::runOnFunction(Function &F) { | 74 bool PNaClABIVerifyFunctions::runOnFunction(Function &F) { |
60 // TODO: only report one error per instruction? | 75 // TODO: only report one error per instruction? |
61 for (Function::const_iterator FI = F.begin(), FE = F.end(); | 76 for (Function::const_iterator FI = F.begin(), FE = F.end(); |
62 FI != FE; ++FI) { | 77 FI != FE; ++FI) { |
63 for (BasicBlock::const_iterator BBI = FI->begin(), BBE = FI->end(); | 78 for (BasicBlock::const_iterator BBI = FI->begin(), BBE = FI->end(); |
64 BBI != BBE; ++BBI) { | 79 BBI != BBE; ++BBI) { |
65 switch (BBI->getOpcode()) { | 80 switch (BBI->getOpcode()) { |
66 // Disallowed instructions. Default is to disallow. | 81 // Disallowed instructions. Default is to disallow. |
67 default: | 82 default: |
68 // We expand GetElementPtr out into arithmetic. | 83 // We expand GetElementPtr out into arithmetic. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 } | 189 } |
175 | 190 |
176 for (User::const_op_iterator OI = BBI->op_begin(), OE = BBI->op_end(); | 191 for (User::const_op_iterator OI = BBI->op_begin(), OE = BBI->op_end(); |
177 OI != OE; OI++) { | 192 OI != OE; OI++) { |
178 if (isa<ConstantExpr>(OI)) { | 193 if (isa<ConstantExpr>(OI)) { |
179 Reporter->addError() << "Function " << F.getName() << | 194 Reporter->addError() << "Function " << F.getName() << |
180 " contains disallowed ConstantExpr\n"; | 195 " contains disallowed ConstantExpr\n"; |
181 } | 196 } |
182 } | 197 } |
183 | 198 |
184 // Get types hiding in metadata attached to the instruction | 199 // Check instruction attachment metadata. |
185 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; | 200 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; |
186 BBI->getAllMetadataOtherThanDebugLoc(MDForInst); | 201 BBI->getAllMetadata(MDForInst); |
202 | |
187 for (unsigned i = 0, e = MDForInst.size(); i != e; i++) { | 203 for (unsigned i = 0, e = MDForInst.size(); i != e; i++) { |
188 Type *T = TC.checkTypesInMDNode(MDForInst[i].second); | 204 if (!IsWhitelistedMetadata(MDForInst[i].first)) { |
189 if (T) { | 205 Reporter->addError() |
190 Reporter->addError() << "Function " << F.getName() << | 206 << "Function " << F.getName() |
191 " has instruction metadata containing disallowed type: " << | 207 << " has disallowed instruction metadata: " |
192 PNaClABITypeChecker::getTypeName(T) << "\n"; | 208 << getMDNodeString(MDForInst[i].second) << "\n"; |
209 } else { | |
210 // If allowed, check the types hiding in the metadata. | |
211 Type *T = TC.checkTypesInMDNode(MDForInst[i].second); | |
212 if (T) { | |
213 Reporter->addError() | |
214 << "Function " << F.getName() | |
215 << " has instruction metadata containing disallowed type: " | |
216 << PNaClABITypeChecker::getTypeName(T) << "\n"; | |
217 } | |
193 } | 218 } |
194 } | 219 } |
195 } | 220 } |
196 } | 221 } |
197 | 222 |
198 Reporter->checkForFatalErrors(); | 223 Reporter->checkForFatalErrors(); |
199 return false; | 224 return false; |
200 } | 225 } |
201 | 226 |
202 // This method exists so that the passes can easily be run with opt -analyze. | 227 // This method exists so that the passes can easily be run with opt -analyze. |
203 // In this case the default constructor is used and we want to reset the error | 228 // In this case the default constructor is used and we want to reset the error |
204 // messages after each print. | 229 // messages after each print. |
205 void PNaClABIVerifyFunctions::print(llvm::raw_ostream &O, const Module *M) | 230 void PNaClABIVerifyFunctions::print(llvm::raw_ostream &O, const Module *M) |
206 const { | 231 const { |
207 Reporter->printErrors(O); | 232 Reporter->printErrors(O); |
208 Reporter->reset(); | 233 Reporter->reset(); |
209 } | 234 } |
210 | 235 |
211 char PNaClABIVerifyFunctions::ID = 0; | 236 char PNaClABIVerifyFunctions::ID = 0; |
212 INITIALIZE_PASS(PNaClABIVerifyFunctions, "verify-pnaclabi-functions", | 237 INITIALIZE_PASS(PNaClABIVerifyFunctions, "verify-pnaclabi-functions", |
213 "Verify functions for PNaCl", false, true) | 238 "Verify functions for PNaCl", false, true) |
214 | 239 |
215 FunctionPass *llvm::createPNaClABIVerifyFunctionsPass( | 240 FunctionPass *llvm::createPNaClABIVerifyFunctionsPass( |
216 PNaClABIErrorReporter *Reporter) { | 241 PNaClABIErrorReporter *Reporter) { |
217 return new PNaClABIVerifyFunctions(Reporter); | 242 return new PNaClABIVerifyFunctions(Reporter); |
218 } | 243 } |
OLD | NEW |