STLAlgorithmModeling.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //===-- STLAlgorithmModeling.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. // Models STL algorithms.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  13. #include "clang/StaticAnalyzer/Core/Checker.h"
  14. #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  17. #include "Iterator.h"
  18. using namespace clang;
  19. using namespace ento;
  20. using namespace iterator;
  21. namespace {
  22. class STLAlgorithmModeling : public Checker<eval::Call> {
  23. bool evalFind(CheckerContext &C, const CallExpr *CE) const;
  24. void Find(CheckerContext &C, const CallExpr *CE, unsigned paramNum) const;
  25. using FnCheck = bool (STLAlgorithmModeling::*)(CheckerContext &,
  26. const CallExpr *) const;
  27. const CallDescriptionMap<FnCheck> Callbacks = {
  28. {{{"std", "find"}, 3}, &STLAlgorithmModeling::evalFind},
  29. {{{"std", "find"}, 4}, &STLAlgorithmModeling::evalFind},
  30. {{{"std", "find_if"}, 3}, &STLAlgorithmModeling::evalFind},
  31. {{{"std", "find_if"}, 4}, &STLAlgorithmModeling::evalFind},
  32. {{{"std", "find_if_not"}, 3}, &STLAlgorithmModeling::evalFind},
  33. {{{"std", "find_if_not"}, 4}, &STLAlgorithmModeling::evalFind},
  34. {{{"std", "find_first_of"}, 4}, &STLAlgorithmModeling::evalFind},
  35. {{{"std", "find_first_of"}, 5}, &STLAlgorithmModeling::evalFind},
  36. {{{"std", "find_first_of"}, 6}, &STLAlgorithmModeling::evalFind},
  37. {{{"std", "find_end"}, 4}, &STLAlgorithmModeling::evalFind},
  38. {{{"std", "find_end"}, 5}, &STLAlgorithmModeling::evalFind},
  39. {{{"std", "find_end"}, 6}, &STLAlgorithmModeling::evalFind},
  40. {{{"std", "lower_bound"}, 3}, &STLAlgorithmModeling::evalFind},
  41. {{{"std", "lower_bound"}, 4}, &STLAlgorithmModeling::evalFind},
  42. {{{"std", "upper_bound"}, 3}, &STLAlgorithmModeling::evalFind},
  43. {{{"std", "upper_bound"}, 4}, &STLAlgorithmModeling::evalFind},
  44. {{{"std", "search"}, 3}, &STLAlgorithmModeling::evalFind},
  45. {{{"std", "search"}, 4}, &STLAlgorithmModeling::evalFind},
  46. {{{"std", "search"}, 5}, &STLAlgorithmModeling::evalFind},
  47. {{{"std", "search"}, 6}, &STLAlgorithmModeling::evalFind},
  48. {{{"std", "search_n"}, 4}, &STLAlgorithmModeling::evalFind},
  49. {{{"std", "search_n"}, 5}, &STLAlgorithmModeling::evalFind},
  50. {{{"std", "search_n"}, 6}, &STLAlgorithmModeling::evalFind},
  51. };
  52. public:
  53. STLAlgorithmModeling() = default;
  54. bool AggressiveStdFindModeling;
  55. bool evalCall(const CallEvent &Call, CheckerContext &C) const;
  56. }; //
  57. bool STLAlgorithmModeling::evalCall(const CallEvent &Call,
  58. CheckerContext &C) const {
  59. const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
  60. if (!CE)
  61. return false;
  62. const FnCheck *Handler = Callbacks.lookup(Call);
  63. if (!Handler)
  64. return false;
  65. return (this->**Handler)(C, CE);
  66. }
  67. bool STLAlgorithmModeling::evalFind(CheckerContext &C,
  68. const CallExpr *CE) const {
  69. // std::find()-like functions either take their primary range in the first
  70. // two parameters, or if the first parameter is "execution policy" then in
  71. // the second and third. This means that the second parameter must always be
  72. // an iterator.
  73. if (!isIteratorType(CE->getArg(1)->getType()))
  74. return false;
  75. // If no "execution policy" parameter is used then the first argument is the
  76. // beginning of the range.
  77. if (isIteratorType(CE->getArg(0)->getType())) {
  78. Find(C, CE, 0);
  79. return true;
  80. }
  81. // If "execution policy" parameter is used then the second argument is the
  82. // beginning of the range.
  83. if (isIteratorType(CE->getArg(2)->getType())) {
  84. Find(C, CE, 1);
  85. return true;
  86. }
  87. return false;
  88. }
  89. void STLAlgorithmModeling::Find(CheckerContext &C, const CallExpr *CE,
  90. unsigned paramNum) const {
  91. auto State = C.getState();
  92. auto &SVB = C.getSValBuilder();
  93. const auto *LCtx = C.getLocationContext();
  94. SVal RetVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
  95. SVal Param = State->getSVal(CE->getArg(paramNum), LCtx);
  96. auto StateFound = State->BindExpr(CE, LCtx, RetVal);
  97. // If we have an iterator position for the range-begin argument then we can
  98. // assume that in case of successful search the position of the found element
  99. // is not ahead of it.
  100. // FIXME: Reverse iterators
  101. const auto *Pos = getIteratorPosition(State, Param);
  102. if (Pos) {
  103. StateFound = createIteratorPosition(StateFound, RetVal, Pos->getContainer(),
  104. CE, LCtx, C.blockCount());
  105. const auto *NewPos = getIteratorPosition(StateFound, RetVal);
  106. assert(NewPos && "Failed to create new iterator position.");
  107. SVal GreaterOrEqual = SVB.evalBinOp(StateFound, BO_GE,
  108. nonloc::SymbolVal(NewPos->getOffset()),
  109. nonloc::SymbolVal(Pos->getOffset()),
  110. SVB.getConditionType());
  111. assert(GreaterOrEqual.getAs<DefinedSVal>() &&
  112. "Symbol comparison must be a `DefinedSVal`");
  113. StateFound = StateFound->assume(GreaterOrEqual.castAs<DefinedSVal>(), true);
  114. }
  115. Param = State->getSVal(CE->getArg(paramNum + 1), LCtx);
  116. // If we have an iterator position for the range-end argument then we can
  117. // assume that in case of successful search the position of the found element
  118. // is ahead of it.
  119. // FIXME: Reverse iterators
  120. Pos = getIteratorPosition(State, Param);
  121. if (Pos) {
  122. StateFound = createIteratorPosition(StateFound, RetVal, Pos->getContainer(),
  123. CE, LCtx, C.blockCount());
  124. const auto *NewPos = getIteratorPosition(StateFound, RetVal);
  125. assert(NewPos && "Failed to create new iterator position.");
  126. SVal Less = SVB.evalBinOp(StateFound, BO_LT,
  127. nonloc::SymbolVal(NewPos->getOffset()),
  128. nonloc::SymbolVal(Pos->getOffset()),
  129. SVB.getConditionType());
  130. assert(Less.getAs<DefinedSVal>() &&
  131. "Symbol comparison must be a `DefinedSVal`");
  132. StateFound = StateFound->assume(Less.castAs<DefinedSVal>(), true);
  133. }
  134. C.addTransition(StateFound);
  135. if (AggressiveStdFindModeling) {
  136. auto StateNotFound = State->BindExpr(CE, LCtx, Param);
  137. C.addTransition(StateNotFound);
  138. }
  139. }
  140. } // namespace
  141. void ento::registerSTLAlgorithmModeling(CheckerManager &Mgr) {
  142. auto *Checker = Mgr.registerChecker<STLAlgorithmModeling>();
  143. Checker->AggressiveStdFindModeling =
  144. Mgr.getAnalyzerOptions().getCheckerBooleanOption(Checker,
  145. "AggressiveStdFindModeling");
  146. }
  147. bool ento::shouldRegisterSTLAlgorithmModeling(const CheckerManager &mgr) {
  148. return true;
  149. }