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(unsigned Kind, |
| 62 const SmallVectorImpl<StringRef>& MDNames) { |
| 63 std::string MDName; |
| 64 raw_string_ostream N(MDName); |
| 65 if (Kind < MDNames.size()) { |
| 66 N << "!" << MDNames[Kind]; |
| 67 } else { |
| 68 N << "!<unknown kind #" << Kind << ">"; |
| 69 } |
| 70 return N.str(); |
| 71 } |
| 72 |
57 } // and anonymous namespace | 73 } // and anonymous namespace |
58 | 74 |
| 75 bool PNaClABIVerifyFunctions::IsWhitelistedMetadata(unsigned MDKind) { |
| 76 return MDKind == LLVMContext::MD_dbg && PNaClABIAllowDebugMetadata; |
| 77 } |
| 78 |
59 bool PNaClABIVerifyFunctions::runOnFunction(Function &F) { | 79 bool PNaClABIVerifyFunctions::runOnFunction(Function &F) { |
| 80 SmallVector<StringRef, 8> MDNames; |
| 81 F.getContext().getMDKindNames(MDNames); |
| 82 |
60 // TODO: only report one error per instruction? | 83 // TODO: only report one error per instruction? |
61 for (Function::const_iterator FI = F.begin(), FE = F.end(); | 84 for (Function::const_iterator FI = F.begin(), FE = F.end(); |
62 FI != FE; ++FI) { | 85 FI != FE; ++FI) { |
63 for (BasicBlock::const_iterator BBI = FI->begin(), BBE = FI->end(); | 86 for (BasicBlock::const_iterator BBI = FI->begin(), BBE = FI->end(); |
64 BBI != BBE; ++BBI) { | 87 BBI != BBE; ++BBI) { |
65 switch (BBI->getOpcode()) { | 88 switch (BBI->getOpcode()) { |
66 // Disallowed instructions. Default is to disallow. | 89 // Disallowed instructions. Default is to disallow. |
67 default: | 90 default: |
68 // We expand GetElementPtr out into arithmetic. | 91 // We expand GetElementPtr out into arithmetic. |
69 case Instruction::GetElementPtr: | 92 case Instruction::GetElementPtr: |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 } | 197 } |
175 | 198 |
176 for (User::const_op_iterator OI = BBI->op_begin(), OE = BBI->op_end(); | 199 for (User::const_op_iterator OI = BBI->op_begin(), OE = BBI->op_end(); |
177 OI != OE; OI++) { | 200 OI != OE; OI++) { |
178 if (isa<ConstantExpr>(OI)) { | 201 if (isa<ConstantExpr>(OI)) { |
179 Reporter->addError() << "Function " << F.getName() << | 202 Reporter->addError() << "Function " << F.getName() << |
180 " contains disallowed ConstantExpr\n"; | 203 " contains disallowed ConstantExpr\n"; |
181 } | 204 } |
182 } | 205 } |
183 | 206 |
184 // Get types hiding in metadata attached to the instruction | 207 // Check instruction attachment metadata. |
185 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; | 208 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; |
186 BBI->getAllMetadataOtherThanDebugLoc(MDForInst); | 209 BBI->getAllMetadata(MDForInst); |
| 210 |
187 for (unsigned i = 0, e = MDForInst.size(); i != e; i++) { | 211 for (unsigned i = 0, e = MDForInst.size(); i != e; i++) { |
188 Type *T = TC.checkTypesInMDNode(MDForInst[i].second); | 212 if (!IsWhitelistedMetadata(MDForInst[i].first)) { |
189 if (T) { | 213 Reporter->addError() |
190 Reporter->addError() << "Function " << F.getName() << | 214 << "Function " << F.getName() |
191 " has instruction metadata containing disallowed type: " << | 215 << " has disallowed instruction metadata: " |
192 PNaClABITypeChecker::getTypeName(T) << "\n"; | 216 << getMDNodeString(MDForInst[i].first, MDNames) << "\n"; |
| 217 } else { |
| 218 // If allowed, check the types hiding in the metadata. |
| 219 Type *T = TC.checkTypesInMDNode(MDForInst[i].second); |
| 220 if (T) { |
| 221 Reporter->addError() |
| 222 << "Function " << F.getName() |
| 223 << " has instruction metadata containing disallowed type: " |
| 224 << PNaClABITypeChecker::getTypeName(T) << "\n"; |
| 225 } |
193 } | 226 } |
194 } | 227 } |
195 } | 228 } |
196 } | 229 } |
197 | 230 |
198 Reporter->checkForFatalErrors(); | 231 Reporter->checkForFatalErrors(); |
199 return false; | 232 return false; |
200 } | 233 } |
201 | 234 |
202 // This method exists so that the passes can easily be run with opt -analyze. | 235 // 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 | 236 // In this case the default constructor is used and we want to reset the error |
204 // messages after each print. | 237 // messages after each print. |
205 void PNaClABIVerifyFunctions::print(llvm::raw_ostream &O, const Module *M) | 238 void PNaClABIVerifyFunctions::print(llvm::raw_ostream &O, const Module *M) |
206 const { | 239 const { |
207 Reporter->printErrors(O); | 240 Reporter->printErrors(O); |
208 Reporter->reset(); | 241 Reporter->reset(); |
209 } | 242 } |
210 | 243 |
211 char PNaClABIVerifyFunctions::ID = 0; | 244 char PNaClABIVerifyFunctions::ID = 0; |
212 INITIALIZE_PASS(PNaClABIVerifyFunctions, "verify-pnaclabi-functions", | 245 INITIALIZE_PASS(PNaClABIVerifyFunctions, "verify-pnaclabi-functions", |
213 "Verify functions for PNaCl", false, true) | 246 "Verify functions for PNaCl", false, true) |
214 | 247 |
215 FunctionPass *llvm::createPNaClABIVerifyFunctionsPass( | 248 FunctionPass *llvm::createPNaClABIVerifyFunctionsPass( |
216 PNaClABIErrorReporter *Reporter) { | 249 PNaClABIErrorReporter *Reporter) { |
217 return new PNaClABIVerifyFunctions(Reporter); | 250 return new PNaClABIVerifyFunctions(Reporter); |
218 } | 251 } |
OLD | NEW |