TaintTesterChecker.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //== TaintTesterChecker.cpp ----------------------------------- -*- C++ -*--=//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This checker can be used for testing how taint data is propagated.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "Taint.h"
  13. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.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. using namespace clang;
  19. using namespace ento;
  20. using namespace taint;
  21. namespace {
  22. class TaintTesterChecker : public Checker< check::PostStmt<Expr> > {
  23. mutable std::unique_ptr<BugType> BT;
  24. void initBugType() const;
  25. /// Given a pointer argument, get the symbol of the value it contains
  26. /// (points to).
  27. SymbolRef getPointedToSymbol(CheckerContext &C,
  28. const Expr* Arg,
  29. bool IssueWarning = true) const;
  30. public:
  31. void checkPostStmt(const Expr *E, CheckerContext &C) const;
  32. };
  33. }
  34. inline void TaintTesterChecker::initBugType() const {
  35. if (!BT)
  36. BT.reset(new BugType(this, "Tainted data", "General"));
  37. }
  38. void TaintTesterChecker::checkPostStmt(const Expr *E,
  39. CheckerContext &C) const {
  40. ProgramStateRef State = C.getState();
  41. if (!State)
  42. return;
  43. if (isTainted(State, E, C.getLocationContext())) {
  44. if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
  45. initBugType();
  46. auto report = std::make_unique<PathSensitiveBugReport>(*BT, "tainted", N);
  47. report->addRange(E->getSourceRange());
  48. C.emitReport(std::move(report));
  49. }
  50. }
  51. }
  52. void ento::registerTaintTesterChecker(CheckerManager &mgr) {
  53. mgr.registerChecker<TaintTesterChecker>();
  54. }
  55. bool ento::shouldRegisterTaintTesterChecker(const CheckerManager &mgr) {
  56. return true;
  57. }