DebugContainerModeling.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //==-- DebugContainerModeling.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 DebugContainerModeling
  24. : public Checker<eval::Call> {
  25. std::unique_ptr<BugType> DebugMsgBugType;
  26. template <typename Getter>
  27. void analyzerContainerDataField(const CallExpr *CE, CheckerContext &C,
  28. Getter get) const;
  29. void analyzerContainerBegin(const CallExpr *CE, CheckerContext &C) const;
  30. void analyzerContainerEnd(const CallExpr *CE, CheckerContext &C) const;
  31. ExplodedNode *reportDebugMsg(llvm::StringRef Msg, CheckerContext &C) const;
  32. typedef void (DebugContainerModeling::*FnCheck)(const CallExpr *,
  33. CheckerContext &) const;
  34. CallDescriptionMap<FnCheck> Callbacks = {
  35. {{"clang_analyzer_container_begin", 1},
  36. &DebugContainerModeling::analyzerContainerBegin},
  37. {{"clang_analyzer_container_end", 1},
  38. &DebugContainerModeling::analyzerContainerEnd},
  39. };
  40. public:
  41. DebugContainerModeling();
  42. bool evalCall(const CallEvent &Call, CheckerContext &C) const;
  43. };
  44. } //namespace
  45. DebugContainerModeling::DebugContainerModeling() {
  46. DebugMsgBugType.reset(
  47. new BugType(this, "Checking analyzer assumptions", "debug",
  48. /*SuppressOnSink=*/true));
  49. }
  50. bool DebugContainerModeling::evalCall(const CallEvent &Call,
  51. CheckerContext &C) const {
  52. const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
  53. if (!CE)
  54. return false;
  55. const FnCheck *Handler = Callbacks.lookup(Call);
  56. if (!Handler)
  57. return false;
  58. (this->**Handler)(CE, C);
  59. return true;
  60. }
  61. template <typename Getter>
  62. void DebugContainerModeling::analyzerContainerDataField(const CallExpr *CE,
  63. CheckerContext &C,
  64. Getter get) const {
  65. if (CE->getNumArgs() == 0) {
  66. reportDebugMsg("Missing container argument", C);
  67. return;
  68. }
  69. auto State = C.getState();
  70. const MemRegion *Cont = C.getSVal(CE->getArg(0)).getAsRegion();
  71. if (Cont) {
  72. const auto *Data = getContainerData(State, Cont);
  73. if (Data) {
  74. SymbolRef Field = get(Data);
  75. if (Field) {
  76. State = State->BindExpr(CE, C.getLocationContext(),
  77. nonloc::SymbolVal(Field));
  78. // Progpagate interestingness from the container's data (marked
  79. // interesting by an `ExprInspection` debug call to the container
  80. // itself.
  81. const NoteTag *InterestingTag =
  82. C.getNoteTag(
  83. [Cont, Field](PathSensitiveBugReport &BR) -> std::string {
  84. if (BR.isInteresting(Field)) {
  85. BR.markInteresting(Cont);
  86. }
  87. return "";
  88. });
  89. C.addTransition(State, InterestingTag);
  90. return;
  91. }
  92. }
  93. }
  94. auto &BVF = C.getSValBuilder().getBasicValueFactory();
  95. State = State->BindExpr(CE, C.getLocationContext(),
  96. nonloc::ConcreteInt(BVF.getValue(llvm::APSInt::get(0))));
  97. }
  98. void DebugContainerModeling::analyzerContainerBegin(const CallExpr *CE,
  99. CheckerContext &C) const {
  100. analyzerContainerDataField(CE, C, [](const ContainerData *D) {
  101. return D->getBegin();
  102. });
  103. }
  104. void DebugContainerModeling::analyzerContainerEnd(const CallExpr *CE,
  105. CheckerContext &C) const {
  106. analyzerContainerDataField(CE, C, [](const ContainerData *D) {
  107. return D->getEnd();
  108. });
  109. }
  110. ExplodedNode *DebugContainerModeling::reportDebugMsg(llvm::StringRef Msg,
  111. CheckerContext &C) const {
  112. ExplodedNode *N = C.generateNonFatalErrorNode();
  113. if (!N)
  114. return nullptr;
  115. auto &BR = C.getBugReporter();
  116. BR.emitReport(std::make_unique<PathSensitiveBugReport>(*DebugMsgBugType,
  117. Msg, N));
  118. return N;
  119. }
  120. void ento::registerDebugContainerModeling(CheckerManager &mgr) {
  121. mgr.registerChecker<DebugContainerModeling>();
  122. }
  123. bool ento::shouldRegisterDebugContainerModeling(const CheckerManager &mgr) {
  124. return true;
  125. }