Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 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 | |
| 19 | #include "ClangSACheckers.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ParentMap.h" |
| 21 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 23 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 25 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | using namespace ento; |
| 29 | |
| 30 | namespace { |
| 31 | class ObjCContainersChecker : public Checker< check::PreStmt<CallExpr>, |
Devin Coughlin | 0bee1d7 | 2015-06-15 01:00:42 +0000 | [diff] [blame] | 32 | check::PostStmt<CallExpr>, |
| 33 | check::PointerEscape> { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 34 | mutable std::unique_ptr<BugType> BT; |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 35 | inline void initBugType() const { |
| 36 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 37 | BT.reset(new BugType(this, "CFArray API", |
Ted Kremenek | b45d198 | 2012-04-05 20:43:28 +0000 | [diff] [blame] | 38 | categories::CoreFoundationObjectiveC)); |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | inline SymbolRef getArraySym(const Expr *E, CheckerContext &C) const { |
| 42 | SVal ArrayRef = C.getState()->getSVal(E, C.getLocationContext()); |
| 43 | SymbolRef ArraySym = ArrayRef.getAsSymbol(); |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 44 | return ArraySym; |
| 45 | } |
| 46 | |
| 47 | void addSizeInfo(const Expr *Array, const Expr *Size, |
| 48 | CheckerContext &C) const; |
| 49 | |
| 50 | public: |
| 51 | /// A tag to id this checker. |
| 52 | static void *getTag() { static int Tag; return &Tag; } |
| 53 | |
| 54 | void checkPostStmt(const CallExpr *CE, CheckerContext &C) const; |
| 55 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
Devin Coughlin | 0bee1d7 | 2015-06-15 01:00:42 +0000 | [diff] [blame] | 56 | ProgramStateRef checkPointerEscape(ProgramStateRef State, |
| 57 | const InvalidatedSymbols &Escaped, |
| 58 | const CallEvent *Call, |
| 59 | PointerEscapeKind Kind) const; |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 60 | }; |
| 61 | } // end anonymous namespace |
| 62 | |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 63 | // ProgramState trait - a map from array symbol to its state. |
| 64 | REGISTER_MAP_WITH_PROGRAMSTATE(ArraySizeMap, SymbolRef, DefinedSVal) |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 65 | |
| 66 | void ObjCContainersChecker::addSizeInfo(const Expr *Array, const Expr *Size, |
| 67 | CheckerContext &C) const { |
Ted Kremenek | f8c36e0 | 2012-01-31 01:19:57 +0000 | [diff] [blame] | 68 | ProgramStateRef State = C.getState(); |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 69 | SVal SizeV = State->getSVal(Size, C.getLocationContext()); |
| 70 | // Undefined is reported by another checker. |
| 71 | if (SizeV.isUnknownOrUndef()) |
| 72 | return; |
| 73 | |
| 74 | // Get the ArrayRef symbol. |
| 75 | SVal ArrayRef = State->getSVal(Array, C.getLocationContext()); |
| 76 | SymbolRef ArraySym = ArrayRef.getAsSymbol(); |
| 77 | if (!ArraySym) |
| 78 | return; |
| 79 | |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 80 | C.addTransition( |
| 81 | State->set<ArraySizeMap>(ArraySym, SizeV.castAs<DefinedSVal>())); |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void ObjCContainersChecker::checkPostStmt(const CallExpr *CE, |
| 85 | CheckerContext &C) const { |
| 86 | StringRef Name = C.getCalleeName(CE); |
Anna Zaks | 8859824 | 2012-02-04 06:40:52 +0000 | [diff] [blame] | 87 | if (Name.empty() || CE->getNumArgs() < 1) |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 88 | return; |
| 89 | |
| 90 | // Add array size information to the state. |
| 91 | if (Name.equals("CFArrayCreate")) { |
Anna Zaks | 8859824 | 2012-02-04 06:40:52 +0000 | [diff] [blame] | 92 | if (CE->getNumArgs() < 3) |
| 93 | return; |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 94 | // Note, we can visit the Create method in the post-visit because |
| 95 | // the CFIndex parameter is passed in by value and will not be invalidated |
| 96 | // by the call. |
| 97 | addSizeInfo(CE, CE->getArg(2), C); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if (Name.equals("CFArrayGetCount")) { |
| 102 | addSizeInfo(CE->getArg(0), CE, C); |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void ObjCContainersChecker::checkPreStmt(const CallExpr *CE, |
| 108 | CheckerContext &C) const { |
| 109 | StringRef Name = C.getCalleeName(CE); |
Anna Zaks | 8859824 | 2012-02-04 06:40:52 +0000 | [diff] [blame] | 110 | if (Name.empty() || CE->getNumArgs() < 2) |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 111 | return; |
| 112 | |
| 113 | // Check the array access. |
| 114 | if (Name.equals("CFArrayGetValueAtIndex")) { |
Ted Kremenek | f8c36e0 | 2012-01-31 01:19:57 +0000 | [diff] [blame] | 115 | ProgramStateRef State = C.getState(); |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 116 | // Retrieve the size. |
Devin Coughlin | d7112c3 | 2015-06-04 00:18:10 +0000 | [diff] [blame] | 117 | // Find out if we saw this array symbol before and have information about |
| 118 | // it. |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 119 | const Expr *ArrayExpr = CE->getArg(0); |
Ted Kremenek | 00fa596 | 2012-04-05 05:18:05 +0000 | [diff] [blame] | 120 | SymbolRef ArraySym = getArraySym(ArrayExpr, C); |
| 121 | if (!ArraySym) |
| 122 | return; |
| 123 | |
| 124 | const DefinedSVal *Size = State->get<ArraySizeMap>(ArraySym); |
| 125 | |
Anna Zaks | 8859824 | 2012-02-04 06:40:52 +0000 | [diff] [blame] | 126 | if (!Size) |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 127 | return; |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 128 | |
| 129 | // Get the index. |
| 130 | const Expr *IdxExpr = CE->getArg(1); |
| 131 | SVal IdxVal = State->getSVal(IdxExpr, C.getLocationContext()); |
Anna Zaks | 8859824 | 2012-02-04 06:40:52 +0000 | [diff] [blame] | 132 | if (IdxVal.isUnknownOrUndef()) |
| 133 | return; |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 134 | DefinedSVal Idx = IdxVal.castAs<DefinedSVal>(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 135 | |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 136 | // Now, check if 'Idx in [0, Size-1]'. |
| 137 | const QualType T = IdxExpr->getType(); |
Anna Zaks | 8859824 | 2012-02-04 06:40:52 +0000 | [diff] [blame] | 138 | ProgramStateRef StInBound = State->assumeInBound(Idx, *Size, true, T); |
| 139 | ProgramStateRef StOutBound = State->assumeInBound(Idx, *Size, false, T); |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 140 | if (StOutBound && !StInBound) { |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 141 | ExplodedNode *N = C.generateErrorNode(StOutBound); |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 142 | if (!N) |
| 143 | return; |
| 144 | initBugType(); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 145 | auto R = llvm::make_unique<BugReport>(*BT, "Index is out of bounds", N); |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 146 | R->addRange(IdxExpr->getSourceRange()); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 147 | C.emitReport(std::move(R)); |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Devin Coughlin | 0bee1d7 | 2015-06-15 01:00:42 +0000 | [diff] [blame] | 153 | ProgramStateRef |
| 154 | ObjCContainersChecker::checkPointerEscape(ProgramStateRef State, |
| 155 | const InvalidatedSymbols &Escaped, |
| 156 | const CallEvent *Call, |
| 157 | PointerEscapeKind Kind) const { |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 158 | for (const auto &Sym : Escaped) { |
Devin Coughlin | 0bee1d7 | 2015-06-15 01:00:42 +0000 | [diff] [blame] | 159 | // When a symbol for a mutable array escapes, we can't reason precisely |
| 160 | // about its size any more -- so remove it from the map. |
| 161 | // Note that we aren't notified here when a CFMutableArrayRef escapes as a |
| 162 | // CFArrayRef. This is because CFArrayRef is typedef'd as a pointer to a |
| 163 | // const-qualified type. |
| 164 | State = State->remove<ArraySizeMap>(Sym); |
| 165 | } |
| 166 | return State; |
| 167 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 168 | |
Anna Zaks | 4f870e6 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 169 | /// Register checker. |
| 170 | void ento::registerObjCContainersChecker(CheckerManager &mgr) { |
| 171 | mgr.registerChecker<ObjCContainersChecker>(); |
| 172 | } |