ObjCContainersChecker.cpp 5.9 KB

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