DebugIteratorModeling.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //===-- DebugIteratorModeling.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. // Defines a checker for debugging iterator modeling.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  13. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  14. #include "clang/StaticAnalyzer/Core/Checker.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  18. #include "Iterator.h"
  19. using namespace clang;
  20. using namespace ento;
  21. using namespace iterator;
  22. namespace {
  23. class DebugIteratorModeling
  24. : public Checker<eval::Call> {
  25. std::unique_ptr<BugType> DebugMsgBugType;
  26. template <typename Getter>
  27. void analyzerIteratorDataField(const CallExpr *CE, CheckerContext &C,
  28. Getter get, SVal Default) const;
  29. void analyzerIteratorPosition(const CallExpr *CE, CheckerContext &C) const;
  30. void analyzerIteratorContainer(const CallExpr *CE, CheckerContext &C) const;
  31. void analyzerIteratorValidity(const CallExpr *CE, CheckerContext &C) const;
  32. ExplodedNode *reportDebugMsg(llvm::StringRef Msg, CheckerContext &C) const;
  33. typedef void (DebugIteratorModeling::*FnCheck)(const CallExpr *,
  34. CheckerContext &) const;
  35. CallDescriptionMap<FnCheck> Callbacks = {
  36. {{"clang_analyzer_iterator_position", 1},
  37. &DebugIteratorModeling::analyzerIteratorPosition},
  38. {{"clang_analyzer_iterator_container", 1},
  39. &DebugIteratorModeling::analyzerIteratorContainer},
  40. {{"clang_analyzer_iterator_validity", 1},
  41. &DebugIteratorModeling::analyzerIteratorValidity},
  42. };
  43. public:
  44. DebugIteratorModeling();
  45. bool evalCall(const CallEvent &Call, CheckerContext &C) const;
  46. };
  47. } //namespace
  48. DebugIteratorModeling::DebugIteratorModeling() {
  49. DebugMsgBugType.reset(
  50. new BugType(this, "Checking analyzer assumptions", "debug",
  51. /*SuppressOnSink=*/true));
  52. }
  53. bool DebugIteratorModeling::evalCall(const CallEvent &Call,
  54. CheckerContext &C) const {
  55. const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
  56. if (!CE)
  57. return false;
  58. const FnCheck *Handler = Callbacks.lookup(Call);
  59. if (!Handler)
  60. return false;
  61. (this->**Handler)(CE, C);
  62. return true;
  63. }
  64. template <typename Getter>
  65. void DebugIteratorModeling::analyzerIteratorDataField(const CallExpr *CE,
  66. CheckerContext &C,
  67. Getter get,
  68. SVal Default) const {
  69. if (CE->getNumArgs() == 0) {
  70. reportDebugMsg("Missing iterator argument", C);
  71. return;
  72. }
  73. auto State = C.getState();
  74. SVal V = C.getSVal(CE->getArg(0));
  75. const auto *Pos = getIteratorPosition(State, V);
  76. if (Pos) {
  77. State = State->BindExpr(CE, C.getLocationContext(), get(Pos));
  78. } else {
  79. State = State->BindExpr(CE, C.getLocationContext(), Default);
  80. }
  81. C.addTransition(State);
  82. }
  83. void DebugIteratorModeling::analyzerIteratorPosition(const CallExpr *CE,
  84. CheckerContext &C) const {
  85. auto &BVF = C.getSValBuilder().getBasicValueFactory();
  86. analyzerIteratorDataField(CE, C, [](const IteratorPosition *P) {
  87. return nonloc::SymbolVal(P->getOffset());
  88. }, nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0))));
  89. }
  90. void DebugIteratorModeling::analyzerIteratorContainer(const CallExpr *CE,
  91. CheckerContext &C) const {
  92. auto &BVF = C.getSValBuilder().getBasicValueFactory();
  93. analyzerIteratorDataField(CE, C, [](const IteratorPosition *P) {
  94. return loc::MemRegionVal(P->getContainer());
  95. }, loc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0))));
  96. }
  97. void DebugIteratorModeling::analyzerIteratorValidity(const CallExpr *CE,
  98. CheckerContext &C) const {
  99. auto &BVF = C.getSValBuilder().getBasicValueFactory();
  100. analyzerIteratorDataField(CE, C, [&BVF](const IteratorPosition *P) {
  101. return
  102. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get((P->isValid()))));
  103. }, nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0))));
  104. }
  105. ExplodedNode *DebugIteratorModeling::reportDebugMsg(llvm::StringRef Msg,
  106. CheckerContext &C) const {
  107. ExplodedNode *N = C.generateNonFatalErrorNode();
  108. if (!N)
  109. return nullptr;
  110. auto &BR = C.getBugReporter();
  111. BR.emitReport(std::make_unique<PathSensitiveBugReport>(*DebugMsgBugType,
  112. Msg, N));
  113. return N;
  114. }
  115. void ento::registerDebugIteratorModeling(CheckerManager &mgr) {
  116. mgr.registerChecker<DebugIteratorModeling>();
  117. }
  118. bool ento::shouldRegisterDebugIteratorModeling(const CheckerManager &mgr) {
  119. return true;
  120. }