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

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

Issue 14329025: Check for metadata in PNaCl ABI checker. (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: cleanup Created 7 years, 7 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
OLDNEW
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
30 class PNaClABIVerifyFunctions : public FunctionPass { 31 class PNaClABIVerifyFunctions : public FunctionPass {
31 public: 32 public:
32 static char ID; 33 static char ID;
33 PNaClABIVerifyFunctions() : 34 PNaClABIVerifyFunctions() :
34 FunctionPass(ID), 35 FunctionPass(ID),
35 Reporter(new PNaClABIErrorReporter), 36 Reporter(new PNaClABIErrorReporter),
36 ReporterIsOwned(true) { 37 ReporterIsOwned(true),
38 AllowDebugMetadata(false) {
37 initializePNaClABIVerifyFunctionsPass(*PassRegistry::getPassRegistry()); 39 initializePNaClABIVerifyFunctionsPass(*PassRegistry::getPassRegistry());
38 } 40 }
39 explicit PNaClABIVerifyFunctions(PNaClABIErrorReporter *Reporter_) : 41 PNaClABIVerifyFunctions(PNaClABIErrorReporter *Reporter_,
42 bool AllowDebugMetadata_) :
40 FunctionPass(ID), 43 FunctionPass(ID),
41 Reporter(Reporter_), 44 Reporter(Reporter_),
42 ReporterIsOwned(false) { 45 ReporterIsOwned(false),
46 AllowDebugMetadata(AllowDebugMetadata_) {
Mark Seaborn 2013/04/25 18:04:32 I think using a trailing underscore is not LLVM st
jvoung (off chromium) 2013/04/25 23:22:20 Hmm it's not that consistent =) MCAssembler uses
43 initializePNaClABIVerifyFunctionsPass(*PassRegistry::getPassRegistry()); 47 initializePNaClABIVerifyFunctionsPass(*PassRegistry::getPassRegistry());
44 } 48 }
45 ~PNaClABIVerifyFunctions() { 49 ~PNaClABIVerifyFunctions() {
46 if (ReporterIsOwned) 50 if (ReporterIsOwned)
47 delete Reporter; 51 delete Reporter;
48 } 52 }
49 bool runOnFunction(Function &F); 53 bool runOnFunction(Function &F);
50 virtual void print(raw_ostream &O, const Module *M) const; 54 virtual void print(raw_ostream &O, const Module *M) const;
51 private: 55 private:
56 bool IsWhiteListedMetadata(unsigned MDKind);
Mark Seaborn 2013/04/25 18:04:32 Nit: "IsWhitelisted" ("whitelisting" is one word)
jvoung (off chromium) 2013/04/25 23:22:20 Done.
52 PNaClABITypeChecker TC; 57 PNaClABITypeChecker TC;
53 PNaClABIErrorReporter *Reporter; 58 PNaClABIErrorReporter *Reporter;
54 bool ReporterIsOwned; 59 bool ReporterIsOwned;
60 bool AllowDebugMetadata;
55 }; 61 };
56 62
63 // There's no built-in way to get the name of an MDNode, so use a
64 // string ostream to print it.
65 std::string getMDNodeString(const MDNode *MD) {
66 std::string s;
67 raw_string_ostream N(s);
68 MD->print(N);
69 return N.str();
70 }
71
57 } // and anonymous namespace 72 } // and anonymous namespace
58 73
74 bool PNaClABIVerifyFunctions::IsWhiteListedMetadata(unsigned MDKind) {
75 return MDKind == LLVMContext::MD_dbg && AllowDebugMetadata;
76 }
77
59 bool PNaClABIVerifyFunctions::runOnFunction(Function &F) { 78 bool PNaClABIVerifyFunctions::runOnFunction(Function &F) {
60 // TODO: only report one error per instruction? 79 // TODO: only report one error per instruction?
61 for (Function::const_iterator FI = F.begin(), FE = F.end(); 80 for (Function::const_iterator FI = F.begin(), FE = F.end();
62 FI != FE; ++FI) { 81 FI != FE; ++FI) {
63 for (BasicBlock::const_iterator BBI = FI->begin(), BBE = FI->end(); 82 for (BasicBlock::const_iterator BBI = FI->begin(), BBE = FI->end();
64 BBI != BBE; ++BBI) { 83 BBI != BBE; ++BBI) {
65 switch (BBI->getOpcode()) { 84 switch (BBI->getOpcode()) {
66 // Disallowed instructions. Default is to disallow. 85 // Disallowed instructions. Default is to disallow.
67 default: 86 default:
68 // We expand GetElementPtr out into arithmetic. 87 // We expand GetElementPtr out into arithmetic.
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 } 193 }
175 194
176 for (User::const_op_iterator OI = BBI->op_begin(), OE = BBI->op_end(); 195 for (User::const_op_iterator OI = BBI->op_begin(), OE = BBI->op_end();
177 OI != OE; OI++) { 196 OI != OE; OI++) {
178 if (isa<ConstantExpr>(OI)) { 197 if (isa<ConstantExpr>(OI)) {
179 Reporter->addError() << "Function " << F.getName() << 198 Reporter->addError() << "Function " << F.getName() <<
180 " contains disallowed ConstantExpr\n"; 199 " contains disallowed ConstantExpr\n";
181 } 200 }
182 } 201 }
183 202
184 // Get types hiding in metadata attached to the instruction 203 // Check instruction attachment metadata.
185 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; 204 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
186 BBI->getAllMetadataOtherThanDebugLoc(MDForInst); 205 BBI->getAllMetadata(MDForInst);
206
187 for (unsigned i = 0, e = MDForInst.size(); i != e; i++) { 207 for (unsigned i = 0, e = MDForInst.size(); i != e; i++) {
188 Type *T = TC.checkTypesInMDNode(MDForInst[i].second); 208 if (!IsWhiteListedMetadata(MDForInst[i].first)) {
189 if (T) { 209 Reporter->addError()
Mark Seaborn 2013/04/25 18:04:32 Fix indentation: remove 2 spaces
jvoung (off chromium) 2013/04/25 23:22:20 Done.
190 Reporter->addError() << "Function " << F.getName() << 210 << "Function " << F.getName()
191 " has instruction metadata containing disallowed type: " << 211 << " has disallowed instruction metadata: "
192 PNaClABITypeChecker::getTypeName(T) << "\n"; 212 << getMDNodeString(MDForInst[i].second) << "\n";
213 } else {
214 // If allowed, check the types hiding in the metadata.
215 Type *T = TC.checkTypesInMDNode(MDForInst[i].second);
216 if (T) {
217 Reporter->addError()
218 << "Function " << F.getName()
219 << " has instruction metadata containing disallowed type: "
220 << PNaClABITypeChecker::getTypeName(T) << "\n";
221 }
193 } 222 }
194 } 223 }
195 } 224 }
196 } 225 }
197 226
198 Reporter->checkForFatalErrors(); 227 Reporter->checkForFatalErrors();
199 return false; 228 return false;
200 } 229 }
201 230
202 // This method exists so that the passes can easily be run with opt -analyze. 231 // 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 232 // In this case the default constructor is used and we want to reset the error
204 // messages after each print. 233 // messages after each print.
205 void PNaClABIVerifyFunctions::print(llvm::raw_ostream &O, const Module *M) 234 void PNaClABIVerifyFunctions::print(llvm::raw_ostream &O, const Module *M)
206 const { 235 const {
207 Reporter->printErrors(O); 236 Reporter->printErrors(O);
208 Reporter->reset(); 237 Reporter->reset();
209 } 238 }
210 239
211 char PNaClABIVerifyFunctions::ID = 0; 240 char PNaClABIVerifyFunctions::ID = 0;
212 INITIALIZE_PASS(PNaClABIVerifyFunctions, "verify-pnaclabi-functions", 241 INITIALIZE_PASS(PNaClABIVerifyFunctions, "verify-pnaclabi-functions",
213 "Verify functions for PNaCl", false, true) 242 "Verify functions for PNaCl", false, true)
214 243
215 FunctionPass *llvm::createPNaClABIVerifyFunctionsPass( 244 FunctionPass *llvm::createPNaClABIVerifyFunctionsPass(
216 PNaClABIErrorReporter *Reporter) { 245 PNaClABIErrorReporter *Reporter,
217 return new PNaClABIVerifyFunctions(Reporter); 246 bool AllowDebugMetadata) {
247 return new PNaClABIVerifyFunctions(Reporter, AllowDebugMetadata);
218 } 248 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698