ObjCContainersChecker.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //== ObjCContainersChecker.cpp - Path sensitive checker for CFArray *- 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. // Performs path sensitive checks of Core Foundation static containers like
  10. // CFArray.
  11. // 1) Check for buffer overflows:
  12. // In CFArrayGetArrayAtIndex( myArray, index), if the index is outside the
  13. // index space of theArray (0 to N-1 inclusive (where N is the count of
  14. // theArray), the behavior is undefined.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  18. #include "clang/AST/ParentMap.h"
  19. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  20. #include "clang/StaticAnalyzer/Core/Checker.h"
  21. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  22. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  23. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  24. using namespace clang;
  25. using namespace ento;
  26. namespace {
  27. class ObjCContainersChecker : public Checker< check::PreStmt<CallExpr>,
  28. check::PostStmt<CallExpr>,
  29. check::PointerEscape> {
  30. mutable std::unique_ptr<BugType> BT;
  31. inline void initBugType() const {
  32. if (!BT)
  33. BT.reset(new BugType(this, "CFArray API",
  34. categories::CoreFoundationObjectiveC));
  35. }
  36. inline SymbolRef getArraySym(const Expr *E, CheckerContext &C) const {
  37. SVal ArrayRef = C.getSVal(E);
  38. SymbolRef ArraySym = ArrayRef.getAsSymbol();
  39. return ArraySym;
  40. }
  41. void addSizeInfo(const Expr *Array, const Expr *Size,
  42. CheckerContext &C) const;
  43. public:
  44. void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
  45. void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
  46. ProgramStateRef checkPointerEscape(ProgramStateRef State,
  47. const InvalidatedSymbols &Escaped,
  48. const CallEvent *Call,
  49. PointerEscapeKind Kind) const;
  50. void printState(raw_ostream &OS, ProgramStateRef State,
  51. const char *NL, const char *Sep) const override;
  52. };
  53. } // end anonymous namespace
  54. // ProgramState trait - a map from array symbol to its state.
  55. REGISTER_MAP_WITH_PROGRAMSTATE(ArraySizeMap, SymbolRef, DefinedSVal)
  56. void ObjCContainersChecker::addSizeInfo(const Expr *Array, const Expr *Size,
  57. CheckerContext &C) const {
  58. ProgramStateRef State = C.getState();
  59. SVal SizeV = C.getSVal(Size);
  60. // Undefined is reported by another checker.
  61. if (SizeV.isUnknownOrUndef())
  62. return;
  63. // Get the ArrayRef symbol.
  64. SVal ArrayRef = C.getSVal(Array);
  65. SymbolRef ArraySym = ArrayRef.getAsSymbol();
  66. if (!ArraySym)
  67. return;
  68. C.addTransition(
  69. State->set<ArraySizeMap>(ArraySym, SizeV.castAs<DefinedSVal>()));
  70. }
  71. void ObjCContainersChecker::checkPostStmt(const CallExpr *CE,
  72. CheckerContext &C) const {
  73. StringRef Name = C.getCalleeName(CE);
  74. if (Name.empty() || CE->getNumArgs() < 1)
  75. return;
  76. // Add array size information to the state.
  77. if (Name.equals("CFArrayCreate")) {
  78. if (CE->getNumArgs() < 3)
  79. return;
  80. // Note, we can visit the Create method in the post-visit because
  81. // the CFIndex parameter is passed in by value and will not be invalidated
  82. // by the call.
  83. addSizeInfo(CE, CE->getArg(2), C);
  84. return;
  85. }
  86. if (Name.equals("CFArrayGetCount")) {
  87. addSizeInfo(CE->getArg(0), CE, C);
  88. return;
  89. }
  90. }
  91. void ObjCContainersChecker::checkPreStmt(const CallExpr *CE,
  92. CheckerContext &C) const {
  93. StringRef Name = C.getCalleeName(CE);
  94. if (Name.empty() || CE->getNumArgs() < 2)
  95. return;
  96. // Check the array access.
  97. if (Name.equals("CFArrayGetValueAtIndex")) {
  98. ProgramStateRef State = C.getState();
  99. // Retrieve the size.
  100. // Find out if we saw this array symbol before and have information about
  101. // it.
  102. const Expr *ArrayExpr = CE->getArg(0);
  103. SymbolRef ArraySym = getArraySym(ArrayExpr, C);
  104. if (!ArraySym)
  105. return;
  106. const DefinedSVal *Size = State->get<ArraySizeMap>(ArraySym);
  107. if (!Size)
  108. return;
  109. // Get the index.
  110. const Expr *IdxExpr = CE->getArg(1);
  111. SVal IdxVal = C.getSVal(IdxExpr);
  112. if (IdxVal.isUnknownOrUndef())
  113. return;
  114. DefinedSVal Idx = IdxVal.castAs<DefinedSVal>();
  115. // Now, check if 'Idx in [0, Size-1]'.
  116. const QualType T = IdxExpr->getType();
  117. ProgramStateRef StInBound, StOutBound;
  118. std::tie(StInBound, StOutBound) = State->assumeInBoundDual(Idx, *Size, T);
  119. if (StOutBound && !StInBound) {
  120. ExplodedNode *N = C.generateErrorNode(StOutBound);
  121. if (!N)
  122. return;
  123. initBugType();
  124. auto R = std::make_unique<PathSensitiveBugReport>(
  125. *BT, "Index is out of bounds", N);
  126. R->addRange(IdxExpr->getSourceRange());
  127. bugreporter::trackExpressionValue(N, IdxExpr, *R,
  128. {bugreporter::TrackingKind::Thorough,
  129. /*EnableNullFPSuppression=*/false});
  130. C.emitReport(std::move(R));
  131. return;
  132. }
  133. }
  134. }
  135. ProgramStateRef
  136. ObjCContainersChecker::checkPointerEscape(ProgramStateRef State,
  137. const InvalidatedSymbols &Escaped,
  138. const CallEvent *Call,
  139. PointerEscapeKind Kind) const {
  140. for (const auto &Sym : Escaped) {
  141. // When a symbol for a mutable array escapes, we can't reason precisely
  142. // about its size any more -- so remove it from the map.
  143. // Note that we aren't notified here when a CFMutableArrayRef escapes as a
  144. // CFArrayRef. This is because CFArrayRef is typedef'd as a pointer to a
  145. // const-qualified type.
  146. State = State->remove<ArraySizeMap>(Sym);
  147. }
  148. return State;
  149. }
  150. void ObjCContainersChecker::printState(raw_ostream &OS, ProgramStateRef State,
  151. const char *NL, const char *Sep) const {
  152. ArraySizeMapTy Map = State->get<ArraySizeMap>();
  153. if (Map.isEmpty())
  154. return;
  155. OS << Sep << "ObjC container sizes :" << NL;
  156. for (auto I : Map) {
  157. OS << I.first << " : " << I.second << NL;
  158. }
  159. }
  160. /// Register checker.
  161. void ento::registerObjCContainersChecker(CheckerManager &mgr) {
  162. mgr.registerChecker<ObjCContainersChecker>();
  163. }
  164. bool ento::shouldRegisterObjCContainersChecker(const CheckerManager &mgr) {
  165. return true;
  166. }