TaintTesterChecker.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  13. #include "clang/StaticAnalyzer/Checkers/Taint.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. std::unique_ptr<BugType> BT =
  24. std::make_unique<BugType>(this, "Tainted data", "General");
  25. public:
  26. void checkPostStmt(const Expr *E, CheckerContext &C) const;
  27. };
  28. }
  29. void TaintTesterChecker::checkPostStmt(const Expr *E,
  30. CheckerContext &C) const {
  31. ProgramStateRef State = C.getState();
  32. if (!State)
  33. return;
  34. if (isTainted(State, E, C.getLocationContext())) {
  35. if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
  36. auto report = std::make_unique<PathSensitiveBugReport>(*BT, "tainted", N);
  37. report->addRange(E->getSourceRange());
  38. C.emitReport(std::move(report));
  39. }
  40. }
  41. }
  42. void ento::registerTaintTesterChecker(CheckerManager &mgr) {
  43. mgr.registerChecker<TaintTesterChecker>();
  44. }
  45. bool ento::shouldRegisterTaintTesterChecker(const CheckerManager &mgr) {
  46. return true;
  47. }