TaintTesterChecker.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //== TaintTesterChecker.cpp ----------------------------------- -*- C++ -*--=//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This checker can be used for testing how taint data is propagated.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ClangSACheckers.h"
  14. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  15. #include "clang/StaticAnalyzer/Core/Checker.h"
  16. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  18. // //
  19. ///////////////////////////////////////////////////////////////////////////////
  20. using namespace clang;
  21. using namespace ento;
  22. namespace {
  23. class TaintTesterChecker : public Checker< check::PostStmt<Expr> > {
  24. mutable std::unique_ptr<BugType> BT;
  25. void initBugType() const;
  26. /// Given a pointer argument, get the symbol of the value it contains
  27. /// (points to).
  28. SymbolRef getPointedToSymbol(CheckerContext &C,
  29. const Expr* Arg,
  30. bool IssueWarning = true) const;
  31. public:
  32. void checkPostStmt(const Expr *E, CheckerContext &C) const;
  33. };
  34. }
  35. inline void TaintTesterChecker::initBugType() const {
  36. if (!BT)
  37. BT.reset(new BugType(this, "Tainted data", "General"));
  38. }
  39. void TaintTesterChecker::checkPostStmt(const Expr *E,
  40. CheckerContext &C) const {
  41. ProgramStateRef State = C.getState();
  42. if (!State)
  43. return;
  44. if (State->isTainted(E, C.getLocationContext())) {
  45. if (ExplodedNode *N = C.addTransition()) {
  46. initBugType();
  47. auto report = llvm::make_unique<BugReport>(*BT, "tainted",N);
  48. report->addRange(E->getSourceRange());
  49. C.emitReport(std::move(report));
  50. }
  51. }
  52. }
  53. void ento::registerTaintTesterChecker(CheckerManager &mgr) {
  54. mgr.registerChecker<TaintTesterChecker>();
  55. }