Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
|
battre
2016/10/27 11:49:06
Do you want to make some comments here what this p
Ramin Halavati
2016/10/27 12:51:20
Done.
| |
| 5 #include <fstream> | |
| 6 #include <memory> | |
| 7 #include <stdio.h> | |
| 8 | |
| 9 #include "clang/ASTMatchers/ASTMatchFinder.h" | |
| 10 #include "clang/ASTMatchers/ASTMatchers.h" | |
| 11 #include "clang/Basic/SourceManager.h" | |
| 12 #include "clang/Frontend/FrontendActions.h" | |
| 13 #include "clang/Lex/Lexer.h" | |
| 14 #include "clang/Tooling/CommonOptionsParser.h" | |
| 15 #include "clang/Tooling/Refactoring.h" | |
| 16 #include "clang/Tooling/Tooling.h" | |
| 17 #include "llvm/Support/CommandLine.h" | |
| 18 //--> TODO: #include "../../traffic_annotation/traffic_annotation.pb.h" | |
| 19 | |
| 20 using namespace clang::ast_matchers; | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Structure to collect instances of network traffic annotation usages. | |
| 25 struct Collector { | |
| 26 // An instance of network traffic annotation usage. This can be either | |
| 27 // a variable defined as NetworkTrafficAnnotationTag or a function that has | |
| 28 // a variable of this type as it's input parameter. | |
| 29 struct Instance { | |
|
battre
2016/10/27 11:49:06
What do you think of moving this struct out of Col
Ramin Halavati
2016/10/27 12:51:20
Done. Also renamed it to NetworkAnnotationInstance
| |
| 30 Instance() | |
| 31 : variable_reference(nullptr), | |
| 32 is_direct_call(false), | |
| 33 transitive_parameter(false) {} | |
| 34 | |
| 35 // Information about where this annotation or call has happened. | |
| 36 struct Location { | |
| 37 Location() : line_number(-1) {} | |
| 38 std::string file_name; | |
| 39 int line_number; | |
| 40 // Name of the function including this instance. | |
| 41 std::string function_name; | |
|
battre
2016/10/27 11:49:06
Can you add examples for this and object_name?
//
Ramin Halavati
2016/10/27 12:51:20
Done.
| |
| 42 // Name of the variable that contains annotation or the function called | |
| 43 // with annotation. | |
| 44 std::string object_name; | |
| 45 }; | |
| 46 | |
| 47 // Annotation content. | |
| 48 struct Annotation { | |
| 49 std::string unique_id; | |
| 50 std::string text; | |
| 51 }; | |
| 52 | |
| 53 Location location; | |
| 54 Annotation annotation; | |
| 55 | |
| 56 // Possible error (empty if no error). | |
| 57 std::string error; | |
| 58 // A reference to the variable containing annotation. Null if not available. | |
| 59 const clang::NamedDecl* variable_reference; | |
| 60 // Flag stating that parameter is directly passed to annotate function here | |
| 61 // or it's through a variable. | |
| 62 bool is_direct_call; | |
| 63 // Flag stating that a variable is a parameter received by upper level | |
| 64 // function. | |
| 65 bool transitive_parameter; | |
| 66 }; | |
| 67 | |
| 68 std::vector<Instance> variable_definitions; | |
| 69 std::vector<Instance> calls; | |
| 70 }; | |
| 71 | |
| 72 // Returns the function that includes the given clang::Decl. | |
| 73 std::string GetCoveringFunction(const clang::Decl* token, | |
| 74 const MatchFinder::MatchResult& result); | |
| 75 | |
| 76 // Checks if a token matches a name, with or without net:: namespace. | |
| 77 bool net_match(const std::string& token, const std::string& name) { | |
| 78 return token == name || token == (std::string("net::") + name); | |
| 79 } | |
| 80 | |
| 81 // Returns the text of a given statement or subclass. | |
|
battre
2016/10/27 11:49:06
Can you give an example of what this means?
Ramin Halavati
2016/10/27 12:51:20
Done, but mostly updated the comment. A clang::Stm
| |
| 82 std::string GetStmtText(const clang::Stmt* token, | |
| 83 const clang::SourceManager& source_manager) { | |
| 84 clang::LangOptions lopt; | |
| 85 // Get text range | |
|
battre
2016/10/27 11:49:05
.
Ramin Halavati
2016/10/27 12:51:20
Done.
| |
| 86 clang::SourceLocation start = token->getLocStart(); | |
| 87 clang::SourceLocation end = token->getLocEnd(); | |
| 88 | |
| 89 // If it's a macro, go to definition. | |
| 90 if (start.isMacroID()) | |
| 91 start = source_manager.getSpellingLoc(start); | |
| 92 if (end.isMacroID()) | |
| 93 end = source_manager.getSpellingLoc(end); | |
| 94 | |
| 95 // Get the real end of the token. | |
| 96 end = clang::Lexer::getLocForEndOfToken(end, 0, source_manager, lopt); | |
| 97 | |
| 98 // Extract text. | |
| 99 std::string output(source_manager.getCharacterData(start), | |
| 100 source_manager.getCharacterData(end) - | |
| 101 source_manager.getCharacterData(start)); | |
|
battre
2016/10/27 11:49:06
std::string also has a constructor template <class
Ramin Halavati
2016/10/27 12:51:20
Done.
| |
| 102 | |
| 103 // Raw string? | |
| 104 if (output == "R") { | |
| 105 if (auto* c1 = clang::dyn_cast<clang::ImplicitCastExpr>(token)) { | |
| 106 if (const clang::StringLiteral* c2 = | |
| 107 clang::dyn_cast<clang::StringLiteral>( | |
| 108 c1->getSubExprAsWritten())) { | |
| 109 output = c2->getString(); | |
| 110 } | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 return output; | |
| 115 } | |
| 116 | |
| 117 // Extracts unique id and annotation text of a call to | |
| 118 // "DefineNetworkTrafficAnnotation" function. Sets the error text if fails. | |
| 119 void GetAnnotationText(const clang::CallExpr* call_expr, | |
| 120 const clang::SourceManager& source_manager, | |
| 121 Collector::Instance* instance) { | |
| 122 if (net_match(GetStmtText(call_expr->getCallee(), source_manager), | |
| 123 "DefineNetworkTrafficAnnotation") && | |
| 124 call_expr->getNumArgs() == 2) { | |
| 125 instance->annotation.unique_id = | |
| 126 GetStmtText(call_expr->getArgs()[0], source_manager); | |
| 127 instance->annotation.text = | |
| 128 GetStmtText(call_expr->getArgs()[1], source_manager); | |
| 129 instance->error = ""; | |
| 130 } else { | |
| 131 instance->annotation.unique_id = ""; | |
| 132 instance->annotation.text = ""; | |
| 133 instance->error = "Unexpected function."; | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 // Returns the function that includes the given clang::Stmt. | |
|
battre
2016/10/27 11:49:06
Example? If token is a statement like "...", the f
Ramin Halavati
2016/10/27 12:51:20
Done.
| |
| 138 std::string GetCoveringFunction(const clang::Stmt* token, | |
| 139 const MatchFinder::MatchResult& result) { | |
| 140 auto parents = result.Context->getParents(*token); | |
|
battre
2016/10/27 11:49:06
Use DynTypedNodeList instead of auto here?
Ramin Halavati
2016/10/27 12:51:20
Done.
| |
| 141 // TODO: What exactly != 1 parent mean? | |
| 142 if (parents.size() == 1) { | |
| 143 if (const clang::Stmt* s = parents[0].get<clang::Stmt>()) | |
| 144 return GetCoveringFunction(s, result); | |
| 145 else if (const clang::Decl* d = parents[0].get<clang::Decl>()) | |
| 146 return GetCoveringFunction(d, result); | |
| 147 } | |
| 148 return "Unknown"; | |
| 149 } | |
| 150 | |
| 151 // Returns the function that includes the given clang::Decl. | |
| 152 std::string GetCoveringFunction(const clang::Decl* token, | |
| 153 const MatchFinder::MatchResult& result) { | |
| 154 if (auto f = clang::dyn_cast<clang::FunctionDecl>(token)) | |
| 155 return f->getQualifiedNameAsString(); | |
| 156 | |
| 157 auto parents = result.Context->getParents(*token); | |
| 158 // TODO: What exactly != 1 parent mean? | |
| 159 if (parents.size() == 1) { | |
| 160 if (const clang::Stmt* s = parents[0].get<clang::Stmt>()) | |
| 161 return GetCoveringFunction(s, result); | |
| 162 else if (const clang::Decl* d = parents[0].get<clang::Decl>()) | |
| 163 return GetCoveringFunction(d, result); | |
| 164 } | |
| 165 return "Unknown"; | |
| 166 } | |
| 167 | |
| 168 // Finds file name and line number of the given token and sets the. | |
|
battre
2016/10/27 11:49:06
sets the $WORD_MISSING
Ramin Halavati
2016/10/27 12:51:20
Done.
| |
| 169 template <class T> | |
| 170 void GetLocation(const T* token, | |
| 171 const MatchFinder::MatchResult& result, | |
| 172 Collector::Instance::Location* location) { | |
| 173 clang::SourceLocation source_location = token->getLocStart(); | |
| 174 location->file_name = result.SourceManager->getFilename(source_location); | |
| 175 location->line_number = | |
| 176 result.SourceManager->getSpellingLineNumber(source_location); | |
| 177 } | |
| 178 | |
| 179 // This class implements the call back functions for AST Matchers. The matchers | |
| 180 // are defined in RunMatchers function and when a pattern is found there, | |
| 181 // the run function in this class is called back with information on the match | |
| 182 // location and description of the match pattern. | |
| 183 class NetworkAnnotationTagCallback : public MatchFinder::MatchCallback { | |
| 184 public: | |
| 185 explicit NetworkAnnotationTagCallback(Collector* collector) | |
| 186 : collector_(collector) {} | |
| 187 ~NetworkAnnotationTagCallback() override = default; | |
| 188 | |
| 189 // Is called on any pattern found by ASTMathers that are defined in RunMathers | |
| 190 // function. | |
| 191 virtual void run(const MatchFinder::MatchResult& result) override { | |
| 192 if (const clang::VarDecl* var_decl = | |
| 193 result.Nodes.getNodeAs<clang::VarDecl>("annotation_variable")) { | |
| 194 AddVariable(var_decl, result); | |
| 195 } else if (const clang::CallExpr* call_expr = | |
| 196 result.Nodes.getNodeAs<clang::CallExpr>("user_function")) { | |
| 197 AddFunction(call_expr, result); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 // Stores an annotation variable defintion. | |
| 202 void AddVariable(const clang::VarDecl* var_decl, | |
| 203 const MatchFinder::MatchResult& result) { | |
| 204 Collector::Instance instance; | |
| 205 | |
| 206 GetLocation(var_decl, result, &instance.location); | |
| 207 instance.location.object_name = var_decl->getQualifiedNameAsString(); | |
| 208 instance.variable_reference = clang::dyn_cast<clang::NamedDecl>(var_decl); | |
| 209 | |
| 210 // Mark it as transitive parameter if it doesn't have initialization but | |
| 211 // it's a function parameter. Otherwise, extract it's content. | |
| 212 if (!var_decl->hasInit() && var_decl->isLocalVarDeclOrParm() && | |
| 213 !var_decl->isLocalVarDecl()) { | |
| 214 instance.transitive_parameter = true; | |
| 215 } else if (auto* init_expr = var_decl->getInit()) { | |
| 216 if (auto* call_expr = clang::dyn_cast<clang::CallExpr>(init_expr)) | |
| 217 GetAnnotationText(call_expr, *result.SourceManager, &instance); | |
| 218 } | |
| 219 // If nothing is set, issue an error. | |
| 220 if (!instance.transitive_parameter && | |
| 221 instance.annotation.unique_id.empty() && instance.error.empty()) { | |
| 222 instance.error = "Could not resolve variable initialization."; | |
| 223 } | |
| 224 | |
| 225 collector_->variable_definitions.push_back(instance); | |
| 226 } | |
| 227 | |
| 228 // Stores a function call that uses annotation variables. | |
| 229 void AddFunction(const clang::CallExpr* call_expr, | |
| 230 const MatchFinder::MatchResult& result) { | |
| 231 Collector::Instance instance; | |
| 232 | |
| 233 GetLocation(call_expr, result, &instance.location); | |
| 234 instance.location.function_name = | |
| 235 GetCoveringFunction(clang::dyn_cast<clang::Stmt>(call_expr), result); | |
| 236 instance.location.object_name = | |
| 237 call_expr->getDirectCallee()->getQualifiedNameAsString(); | |
| 238 | |
| 239 // Get annotation text. | |
| 240 const clang::FunctionDecl* function_decl = call_expr->getDirectCallee(); | |
| 241 unsigned params_count = function_decl->getNumParams(); | |
| 242 unsigned args_count = call_expr->getNumArgs(); | |
| 243 | |
| 244 for (unsigned i = 0; i < params_count; i++) { | |
| 245 if (net_match(clang::QualType::getAsString( | |
| 246 function_decl->getParamDecl(i)->getType().split()), | |
| 247 "NetworkTrafficAnnotationTag")) { | |
| 248 if (i >= args_count) { | |
| 249 instance.error = "Function missing annotation argument."; | |
| 250 } else { | |
| 251 // Get the argument. | |
| 252 const clang::Expr* arg = call_expr->getArgs()[i]; | |
| 253 | |
| 254 // Is it a call to annotate function? | |
| 255 if (auto* inner_call_expr = clang::dyn_cast<clang::CallExpr>(arg)) { | |
| 256 instance.is_direct_call = true; | |
| 257 GetAnnotationText(inner_call_expr, *result.SourceManager, | |
| 258 &instance); | |
| 259 instance.error = ""; | |
| 260 } else { | |
| 261 // Then it's a variable. | |
| 262 instance.is_direct_call = false; | |
| 263 if (auto* pure_arg = | |
| 264 clang::dyn_cast<clang::DeclRefExpr>(arg->IgnoreCasts())) { | |
| 265 instance.variable_reference = pure_arg->getFoundDecl(); | |
| 266 instance.error = ""; | |
| 267 } else { | |
| 268 instance.error = "Unknwon parameter type."; | |
| 269 } | |
| 270 } | |
| 271 } | |
| 272 collector_->calls.push_back(instance); | |
| 273 } | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 private: | |
| 278 Collector* collector_; | |
| 279 }; | |
| 280 | |
| 281 // Sets up ASTMatchers and runs clang tool to populate collector. Returns the | |
| 282 // result of running the clang tool. | |
| 283 int RunMatchers(clang::tooling::ClangTool* clang_tool, Collector* collector) { | |
| 284 NetworkAnnotationTagCallback call_back(collector); | |
| 285 MatchFinder match_finder; | |
| 286 | |
| 287 // Set up a pattern to find variables defined with type | |
| 288 // [net::]NetworkTrafficAnnotationTag. | |
| 289 match_finder.addMatcher( | |
| 290 varDecl(anyOf(hasType(asString("NetworkTrafficAnnotationTag")), | |
| 291 hasType(asString("net::NetworkTrafficAnnotationTag")))) | |
| 292 .bind("annotation_variable"), | |
| 293 &call_back); | |
| 294 | |
| 295 // Set up a pattern to find functions that have a parameter of type | |
| 296 // [net::]NetworkTrafficAnnotationTag. | |
| 297 match_finder.addMatcher( | |
| 298 callExpr(hasDeclaration(functionDecl(hasAnyParameter(anyOf( | |
| 299 hasType(asString("NetworkTrafficAnnotationTag")), | |
| 300 hasType(asString("net::NetworkTrafficAnnotationTag"))))))) | |
| 301 .bind("user_function"), | |
| 302 &call_back); | |
| 303 | |
| 304 std::unique_ptr<clang::tooling::FrontendActionFactory> frontend_factory = | |
| 305 clang::tooling::newFrontendActionFactory(&match_finder); | |
| 306 return clang_tool->run(frontend_factory.get()); | |
| 307 } | |
| 308 | |
| 309 } // namespace | |
| 310 | |
| 311 int main(int argc, const char* argv[]) { | |
| 312 llvm::cl::OptionCategory category("Network Request Audit Extractor Tool"); | |
| 313 clang::tooling::CommonOptionsParser options(argc, argv, category); | |
| 314 clang::tooling::ClangTool tool(options.getCompilations(), | |
| 315 options.getSourcePathList()); | |
| 316 Collector collector; | |
| 317 | |
| 318 // Find output folder. | |
| 319 const std::string kOutputSpecifier("output_dir="); | |
| 320 std::string output_dir; | |
| 321 for (int i = 0; i < argc; i++) { | |
| 322 if (!strncmp(argv[i], kOutputSpecifier.c_str(), | |
| 323 kOutputSpecifier.length())) { | |
| 324 output_dir = argv[i] + kOutputSpecifier.length(); | |
| 325 break; | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 if (output_dir == "") { | |
| 330 llvm::errs() << "Temporary files directory is not specified."; | |
| 331 return -1; | |
| 332 } | |
| 333 | |
| 334 int result = RunMatchers(&tool, &collector); | |
| 335 | |
| 336 if (result != 0) | |
| 337 return result; | |
| 338 | |
| 339 llvm::outs() << "==== BEGIN EDITS ====\n"; | |
| 340 llvm::outs() << "==== END EDITS ====\n"; | |
| 341 | |
| 342 // For each call, if the parameter is not generated by a direct call to | |
| 343 // "DefineNetworkTrafficAnnotation", find the variable that holds the value. | |
| 344 for (auto& c : collector.calls) { | |
| 345 if (!c.is_direct_call) { | |
| 346 // Find the variable. | |
| 347 for (const auto& v : collector.variable_definitions) | |
| 348 if (v.variable_reference == c.variable_reference) { | |
| 349 c.annotation = v.annotation; | |
| 350 c.transitive_parameter = v.transitive_parameter; | |
| 351 c.error = c.error + (c.error.length() ? "\n+" : "") + v.error; | |
| 352 break; | |
| 353 } | |
| 354 if (!c.annotation.unique_id.length()) | |
| 355 c.error = "Variable not found."; | |
| 356 } | |
| 357 | |
| 358 // If the function just receives the variable and passes it to another | |
| 359 // function, ignore it, otherwise write it to file. | |
| 360 if (!c.transitive_parameter) { | |
| 361 std::string s = c.location.file_name; | |
| 362 std::replace(s.begin(), s.end(), '/', '_'); | |
| 363 std::replace(s.begin(), s.end(), '.', '_'); | |
| 364 std::string filename = output_dir + "/" + s + "(" + | |
| 365 std::to_string(c.location.line_number) + ").txt"; | |
| 366 | |
| 367 std::ofstream output_file(filename); | |
| 368 if (output_file.is_open()) { | |
| 369 output_file << c.location.file_name << "\n"; | |
| 370 output_file << c.location.function_name << "\n"; | |
| 371 output_file << c.location.line_number << "\n"; | |
| 372 output_file << c.location.object_name << "\n"; | |
| 373 output_file << c.error << "\n"; | |
| 374 output_file << c.annotation.unique_id << "\n"; | |
| 375 output_file << c.annotation.text << "\n"; | |
| 376 output_file.close(); | |
| 377 } else { | |
| 378 llvm::errs() << "Could not write to file: " << filename << " because " | |
| 379 << strerror(errno) << "\n"; | |
| 380 return 1; | |
| 381 } | |
| 382 } | |
| 383 } | |
| 384 | |
| 385 return 0; | |
| 386 } | |
| OLD | NEW |