Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 1 | //=-- ExprEngineObjC.cpp - ExprEngine support for Objective-C ---*- 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 | // This file defines ExprEngine's support for Objective-C expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | c35fb7d | 2012-01-28 12:06:22 +0000 | [diff] [blame] | 14 | #include "clang/AST/StmtObjC.h" |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Jordan Rose | f540c54 | 2012-07-26 21:39:41 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | using namespace ento; |
| 21 | |
| 22 | void ExprEngine::VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr *Ex, |
| 23 | ExplodedNode *Pred, |
| 24 | ExplodedNodeSet &Dst) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 25 | ProgramStateRef state = Pred->getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 26 | const LocationContext *LCtx = Pred->getLocationContext(); |
| 27 | SVal baseVal = state->getSVal(Ex->getBase(), LCtx); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 28 | SVal location = state->getLValue(Ex->getDecl(), baseVal); |
| 29 | |
| 30 | ExplodedNodeSet dstIvar; |
Jordan Rose | e606e3d | 2012-10-01 19:07:22 +0000 | [diff] [blame] | 31 | StmtNodeBuilder Bldr(Pred, dstIvar, *currBldrCtx); |
| 32 | Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, location)); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 33 | |
| 34 | // Perform the post-condition check of the ObjCIvarRefExpr and store |
| 35 | // the created nodes in 'Dst'. |
| 36 | getCheckerManager().runCheckersForPostStmt(Dst, dstIvar, Ex, *this); |
| 37 | } |
| 38 | |
| 39 | void ExprEngine::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S, |
| 40 | ExplodedNode *Pred, |
| 41 | ExplodedNodeSet &Dst) { |
| 42 | getCheckerManager().runCheckersForPreStmt(Dst, Pred, S, *this); |
| 43 | } |
| 44 | |
| 45 | void ExprEngine::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S, |
| 46 | ExplodedNode *Pred, |
| 47 | ExplodedNodeSet &Dst) { |
| 48 | |
| 49 | // ObjCForCollectionStmts are processed in two places. This method |
| 50 | // handles the case where an ObjCForCollectionStmt* occurs as one of the |
| 51 | // statements within a basic block. This transfer function does two things: |
| 52 | // |
| 53 | // (1) binds the next container value to 'element'. This creates a new |
| 54 | // node in the ExplodedGraph. |
| 55 | // |
| 56 | // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating |
| 57 | // whether or not the container has any more elements. This value |
| 58 | // will be tested in ProcessBranch. We need to explicitly bind |
| 59 | // this value because a container can contain nil elements. |
| 60 | // |
| 61 | // FIXME: Eventually this logic should actually do dispatches to |
| 62 | // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration). |
| 63 | // This will require simulating a temporary NSFastEnumerationState, either |
| 64 | // through an SVal or through the use of MemRegions. This value can |
| 65 | // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop |
| 66 | // terminates we reclaim the temporary (it goes out of scope) and we |
| 67 | // we can test if the SVal is 0 or if the MemRegion is null (depending |
| 68 | // on what approach we take). |
| 69 | // |
| 70 | // For now: simulate (1) by assigning either a symbol or nil if the |
| 71 | // container is empty. Thus this transfer function will by default |
| 72 | // result in state splitting. |
Anna Zaks | ebae6d0 | 2011-10-24 18:26:19 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 74 | const Stmt *elem = S->getElement(); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 75 | ProgramStateRef state = Pred->getState(); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 76 | SVal elementV; |
| 77 | |
| 78 | if (const DeclStmt *DS = dyn_cast<DeclStmt>(elem)) { |
| 79 | const VarDecl *elemD = cast<VarDecl>(DS->getSingleDecl()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 80 | assert(elemD->getInit() == nullptr); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 81 | elementV = state->getLValue(elemD, Pred->getLocationContext()); |
| 82 | } |
| 83 | else { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 84 | elementV = state->getSVal(elem, Pred->getLocationContext()); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | ExplodedNodeSet dstLocation; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 88 | evalLocation(dstLocation, S, elem, Pred, state, elementV, nullptr, false); |
Jordan Rose | 1895a0a | 2012-06-11 16:40:41 +0000 | [diff] [blame] | 89 | |
| 90 | ExplodedNodeSet Tmp; |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 91 | StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx); |
Jordan Rose | 1895a0a | 2012-06-11 16:40:41 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 93 | for (ExplodedNodeSet::iterator NI = dstLocation.begin(), |
| 94 | NE = dstLocation.end(); NI!=NE; ++NI) { |
| 95 | Pred = *NI; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 96 | ProgramStateRef state = Pred->getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 97 | const LocationContext *LCtx = Pred->getLocationContext(); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 98 | |
| 99 | // Handle the case where the container still has elements. |
| 100 | SVal TrueV = svalBuilder.makeTruthVal(1); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 101 | ProgramStateRef hasElems = state->BindExpr(S, LCtx, TrueV); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 102 | |
| 103 | // Handle the case where the container has no elements. |
| 104 | SVal FalseV = svalBuilder.makeTruthVal(0); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 105 | ProgramStateRef noElems = state->BindExpr(S, LCtx, FalseV); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 106 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 107 | if (Optional<loc::MemRegionVal> MV = elementV.getAs<loc::MemRegionVal>()) |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 108 | if (const TypedValueRegion *R = |
| 109 | dyn_cast<TypedValueRegion>(MV->getRegion())) { |
| 110 | // FIXME: The proper thing to do is to really iterate over the |
| 111 | // container. We will do this with dispatch logic to the store. |
| 112 | // For now, just 'conjure' up a symbolic value. |
| 113 | QualType T = R->getValueType(); |
| 114 | assert(Loc::isLocType(T)); |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 115 | SymbolRef Sym = SymMgr.conjureSymbol(elem, LCtx, T, |
| 116 | currBldrCtx->blockCount()); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 117 | SVal V = svalBuilder.makeLoc(Sym); |
| 118 | hasElems = hasElems->bindLoc(elementV, V); |
| 119 | |
| 120 | // Bind the location to 'nil' on the false branch. |
| 121 | SVal nilV = svalBuilder.makeIntVal(0, T); |
| 122 | noElems = noElems->bindLoc(elementV, nilV); |
| 123 | } |
| 124 | |
| 125 | // Create the new nodes. |
Anna Zaks | ebae6d0 | 2011-10-24 18:26:19 +0000 | [diff] [blame] | 126 | Bldr.generateNode(S, Pred, hasElems); |
| 127 | Bldr.generateNode(S, Pred, noElems); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 128 | } |
Jordan Rose | 1895a0a | 2012-06-11 16:40:41 +0000 | [diff] [blame] | 129 | |
| 130 | // Finally, run any custom checkers. |
| 131 | // FIXME: Eventually all pre- and post-checks should live in VisitStmt. |
| 132 | getCheckerManager().runCheckersForPostStmt(Dst, Tmp, S, *this); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Jordan Rose | d563d3f | 2012-07-30 20:22:09 +0000 | [diff] [blame] | 135 | void ExprEngine::VisitObjCMessage(const ObjCMessageExpr *ME, |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 136 | ExplodedNode *Pred, |
| 137 | ExplodedNodeSet &Dst) { |
Jordan Rose | d563d3f | 2012-07-30 20:22:09 +0000 | [diff] [blame] | 138 | CallEventManager &CEMgr = getStateManager().getCallEventManager(); |
| 139 | CallEventRef<ObjCMethodCall> Msg = |
| 140 | CEMgr.getObjCMethodCall(ME, Pred->getState(), Pred->getLocationContext()); |
| 141 | |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 142 | // Handle the previsits checks. |
| 143 | ExplodedNodeSet dstPrevisit; |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 144 | getCheckerManager().runCheckersForPreObjCMessage(dstPrevisit, Pred, |
Jordan Rose | d563d3f | 2012-07-30 20:22:09 +0000 | [diff] [blame] | 145 | *Msg, *this); |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 146 | ExplodedNodeSet dstGenericPrevisit; |
| 147 | getCheckerManager().runCheckersForPreCall(dstGenericPrevisit, dstPrevisit, |
Jordan Rose | d563d3f | 2012-07-30 20:22:09 +0000 | [diff] [blame] | 148 | *Msg, *this); |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 149 | |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 150 | // Proceed with evaluate the message expression. |
| 151 | ExplodedNodeSet dstEval; |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 152 | StmtNodeBuilder Bldr(dstGenericPrevisit, dstEval, *currBldrCtx); |
Anna Zaks | ebae6d0 | 2011-10-24 18:26:19 +0000 | [diff] [blame] | 153 | |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 154 | for (ExplodedNodeSet::iterator DI = dstGenericPrevisit.begin(), |
| 155 | DE = dstGenericPrevisit.end(); DI != DE; ++DI) { |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 156 | ExplodedNode *Pred = *DI; |
Jordan Rose | d563d3f | 2012-07-30 20:22:09 +0000 | [diff] [blame] | 157 | ProgramStateRef State = Pred->getState(); |
| 158 | CallEventRef<ObjCMethodCall> UpdatedMsg = Msg.cloneWithState(State); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 159 | |
Jordan Rose | d563d3f | 2012-07-30 20:22:09 +0000 | [diff] [blame] | 160 | if (UpdatedMsg->isInstanceMessage()) { |
| 161 | SVal recVal = UpdatedMsg->getReceiverSVal(); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 162 | if (!recVal.isUndef()) { |
| 163 | // Bifurcate the state into nil and non-nil ones. |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 164 | DefinedOrUnknownSVal receiverVal = |
| 165 | recVal.castAs<DefinedOrUnknownSVal>(); |
| 166 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 167 | ProgramStateRef notNilState, nilState; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 168 | std::tie(notNilState, nilState) = State->assume(receiverVal); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 169 | |
| 170 | // There are three cases: can be nil or non-nil, must be nil, must be |
| 171 | // non-nil. We ignore must be nil, and merge the rest two into non-nil. |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 172 | // FIXME: This ignores many potential bugs (<rdar://problem/11733396>). |
| 173 | // Revisit once we have lazier constraints. |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 174 | if (nilState && !notNilState) { |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 175 | continue; |
| 176 | } |
| 177 | |
| 178 | // Check if the "raise" message was sent. |
| 179 | assert(notNilState); |
Ted Kremenek | 4ef1920 | 2012-09-13 00:21:31 +0000 | [diff] [blame] | 180 | if (ObjCNoRet.isImplicitNoReturn(ME)) { |
Anna Zaks | e81ce25 | 2012-07-19 23:38:13 +0000 | [diff] [blame] | 181 | // If we raise an exception, for now treat it as a sink. |
| 182 | // Eventually we will want to handle exceptions properly. |
Jordan Rose | 9428723 | 2012-12-06 18:58:26 +0000 | [diff] [blame] | 183 | Bldr.generateSink(ME, Pred, State); |
Anna Zaks | e81ce25 | 2012-07-19 23:38:13 +0000 | [diff] [blame] | 184 | continue; |
| 185 | } |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 186 | |
Anna Zaks | e81ce25 | 2012-07-19 23:38:13 +0000 | [diff] [blame] | 187 | // Generate a transition to non-Nil state. |
Jordan Rose | 82f2ad4 | 2012-09-08 01:47:28 +0000 | [diff] [blame] | 188 | if (notNilState != State) { |
Jordan Rose | 9428723 | 2012-12-06 18:58:26 +0000 | [diff] [blame] | 189 | Pred = Bldr.generateNode(ME, Pred, notNilState); |
Jordan Rose | 82f2ad4 | 2012-09-08 01:47:28 +0000 | [diff] [blame] | 190 | assert(Pred && "Should have cached out already!"); |
| 191 | } |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 192 | } |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 193 | } else { |
Ted Kremenek | 4ef1920 | 2012-09-13 00:21:31 +0000 | [diff] [blame] | 194 | // Check for special class methods that are known to not return |
| 195 | // and that we should treat as a sink. |
| 196 | if (ObjCNoRet.isImplicitNoReturn(ME)) { |
| 197 | // If we raise an exception, for now treat it as a sink. |
| 198 | // Eventually we will want to handle exceptions properly. |
Jordan Rose | 9428723 | 2012-12-06 18:58:26 +0000 | [diff] [blame] | 199 | Bldr.generateSink(ME, Pred, Pred->getState()); |
Ted Kremenek | 4ef1920 | 2012-09-13 00:21:31 +0000 | [diff] [blame] | 200 | continue; |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 201 | } |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 202 | } |
Anna Zaks | 9dc5167 | 2012-07-26 00:27:51 +0000 | [diff] [blame] | 203 | |
Jordan Rose | 82f2ad4 | 2012-09-08 01:47:28 +0000 | [diff] [blame] | 204 | defaultEvalCall(Bldr, Pred, *UpdatedMsg); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 207 | ExplodedNodeSet dstPostvisit; |
Jordan Rose | d563d3f | 2012-07-30 20:22:09 +0000 | [diff] [blame] | 208 | getCheckerManager().runCheckersForPostCall(dstPostvisit, dstEval, |
| 209 | *Msg, *this); |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 211 | // Finally, perform the post-condition check of the ObjCMessageExpr and store |
| 212 | // the created nodes in 'Dst'. |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 213 | getCheckerManager().runCheckersForPostObjCMessage(Dst, dstPostvisit, |
Jordan Rose | d563d3f | 2012-07-30 20:22:09 +0000 | [diff] [blame] | 214 | *Msg, *this); |
Ted Kremenek | 294fd0a | 2011-08-20 06:00:03 +0000 | [diff] [blame] | 215 | } |