1226586Sdim//=-- ExprEngineObjC.cpp - ExprEngine support for Objective-C ---*- C++ -*-===// 2226586Sdim// 3226586Sdim// The LLVM Compiler Infrastructure 4226586Sdim// 5226586Sdim// This file is distributed under the University of Illinois Open Source 6226586Sdim// License. See LICENSE.TXT for details. 7226586Sdim// 8226586Sdim//===----------------------------------------------------------------------===// 9226586Sdim// 10226586Sdim// This file defines ExprEngine's support for Objective-C expressions. 11226586Sdim// 12226586Sdim//===----------------------------------------------------------------------===// 13226586Sdim 14235633Sdim#include "clang/AST/StmtObjC.h" 15226586Sdim#include "clang/StaticAnalyzer/Core/CheckerManager.h" 16245431Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 17226586Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 18226586Sdim 19226586Sdimusing namespace clang; 20226586Sdimusing namespace ento; 21226586Sdim 22226586Sdimvoid ExprEngine::VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr *Ex, 23226586Sdim ExplodedNode *Pred, 24226586Sdim ExplodedNodeSet &Dst) { 25235633Sdim ProgramStateRef state = Pred->getState(); 26235633Sdim const LocationContext *LCtx = Pred->getLocationContext(); 27235633Sdim SVal baseVal = state->getSVal(Ex->getBase(), LCtx); 28226586Sdim SVal location = state->getLValue(Ex->getDecl(), baseVal); 29226586Sdim 30226586Sdim ExplodedNodeSet dstIvar; 31245431Sdim StmtNodeBuilder Bldr(Pred, dstIvar, *currBldrCtx); 32235633Sdim Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, location)); 33226586Sdim 34226586Sdim // Perform the post-condition check of the ObjCIvarRefExpr and store 35226586Sdim // the created nodes in 'Dst'. 36226586Sdim getCheckerManager().runCheckersForPostStmt(Dst, dstIvar, Ex, *this); 37226586Sdim} 38226586Sdim 39226586Sdimvoid ExprEngine::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S, 40226586Sdim ExplodedNode *Pred, 41226586Sdim ExplodedNodeSet &Dst) { 42226586Sdim getCheckerManager().runCheckersForPreStmt(Dst, Pred, S, *this); 43226586Sdim} 44226586Sdim 45226586Sdimvoid ExprEngine::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S, 46226586Sdim ExplodedNode *Pred, 47226586Sdim ExplodedNodeSet &Dst) { 48226586Sdim 49226586Sdim // ObjCForCollectionStmts are processed in two places. This method 50226586Sdim // handles the case where an ObjCForCollectionStmt* occurs as one of the 51226586Sdim // statements within a basic block. This transfer function does two things: 52226586Sdim // 53226586Sdim // (1) binds the next container value to 'element'. This creates a new 54226586Sdim // node in the ExplodedGraph. 55226586Sdim // 56226586Sdim // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating 57226586Sdim // whether or not the container has any more elements. This value 58226586Sdim // will be tested in ProcessBranch. We need to explicitly bind 59226586Sdim // this value because a container can contain nil elements. 60226586Sdim // 61226586Sdim // FIXME: Eventually this logic should actually do dispatches to 62226586Sdim // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration). 63226586Sdim // This will require simulating a temporary NSFastEnumerationState, either 64226586Sdim // through an SVal or through the use of MemRegions. This value can 65226586Sdim // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop 66226586Sdim // terminates we reclaim the temporary (it goes out of scope) and we 67226586Sdim // we can test if the SVal is 0 or if the MemRegion is null (depending 68226586Sdim // on what approach we take). 69226586Sdim // 70226586Sdim // For now: simulate (1) by assigning either a symbol or nil if the 71226586Sdim // container is empty. Thus this transfer function will by default 72226586Sdim // result in state splitting. 73235633Sdim 74226586Sdim const Stmt *elem = S->getElement(); 75235633Sdim ProgramStateRef state = Pred->getState(); 76226586Sdim SVal elementV; 77226586Sdim 78226586Sdim if (const DeclStmt *DS = dyn_cast<DeclStmt>(elem)) { 79226586Sdim const VarDecl *elemD = cast<VarDecl>(DS->getSingleDecl()); 80226586Sdim assert(elemD->getInit() == 0); 81226586Sdim elementV = state->getLValue(elemD, Pred->getLocationContext()); 82226586Sdim } 83226586Sdim else { 84235633Sdim elementV = state->getSVal(elem, Pred->getLocationContext()); 85226586Sdim } 86226586Sdim 87226586Sdim ExplodedNodeSet dstLocation; 88235633Sdim evalLocation(dstLocation, S, elem, Pred, state, elementV, NULL, false); 89245431Sdim 90245431Sdim ExplodedNodeSet Tmp; 91245431Sdim StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx); 92245431Sdim 93226586Sdim for (ExplodedNodeSet::iterator NI = dstLocation.begin(), 94226586Sdim NE = dstLocation.end(); NI!=NE; ++NI) { 95226586Sdim Pred = *NI; 96235633Sdim ProgramStateRef state = Pred->getState(); 97235633Sdim const LocationContext *LCtx = Pred->getLocationContext(); 98226586Sdim 99226586Sdim // Handle the case where the container still has elements. 100226586Sdim SVal TrueV = svalBuilder.makeTruthVal(1); 101235633Sdim ProgramStateRef hasElems = state->BindExpr(S, LCtx, TrueV); 102226586Sdim 103226586Sdim // Handle the case where the container has no elements. 104226586Sdim SVal FalseV = svalBuilder.makeTruthVal(0); 105235633Sdim ProgramStateRef noElems = state->BindExpr(S, LCtx, FalseV); 106252723Sdim 107252723Sdim if (Optional<loc::MemRegionVal> MV = elementV.getAs<loc::MemRegionVal>()) 108226586Sdim if (const TypedValueRegion *R = 109226586Sdim dyn_cast<TypedValueRegion>(MV->getRegion())) { 110226586Sdim // FIXME: The proper thing to do is to really iterate over the 111226586Sdim // container. We will do this with dispatch logic to the store. 112226586Sdim // For now, just 'conjure' up a symbolic value. 113226586Sdim QualType T = R->getValueType(); 114226586Sdim assert(Loc::isLocType(T)); 115245431Sdim SymbolRef Sym = SymMgr.conjureSymbol(elem, LCtx, T, 116245431Sdim currBldrCtx->blockCount()); 117226586Sdim SVal V = svalBuilder.makeLoc(Sym); 118226586Sdim hasElems = hasElems->bindLoc(elementV, V); 119226586Sdim 120226586Sdim // Bind the location to 'nil' on the false branch. 121226586Sdim SVal nilV = svalBuilder.makeIntVal(0, T); 122226586Sdim noElems = noElems->bindLoc(elementV, nilV); 123226586Sdim } 124226586Sdim 125226586Sdim // Create the new nodes. 126235633Sdim Bldr.generateNode(S, Pred, hasElems); 127235633Sdim Bldr.generateNode(S, Pred, noElems); 128226586Sdim } 129245431Sdim 130245431Sdim // Finally, run any custom checkers. 131245431Sdim // FIXME: Eventually all pre- and post-checks should live in VisitStmt. 132245431Sdim getCheckerManager().runCheckersForPostStmt(Dst, Tmp, S, *this); 133226586Sdim} 134226586Sdim 135245431Sdimvoid ExprEngine::VisitObjCMessage(const ObjCMessageExpr *ME, 136226586Sdim ExplodedNode *Pred, 137226586Sdim ExplodedNodeSet &Dst) { 138245431Sdim CallEventManager &CEMgr = getStateManager().getCallEventManager(); 139245431Sdim CallEventRef<ObjCMethodCall> Msg = 140245431Sdim CEMgr.getObjCMethodCall(ME, Pred->getState(), Pred->getLocationContext()); 141245431Sdim 142226586Sdim // Handle the previsits checks. 143226586Sdim ExplodedNodeSet dstPrevisit; 144245431Sdim getCheckerManager().runCheckersForPreObjCMessage(dstPrevisit, Pred, 145245431Sdim *Msg, *this); 146245431Sdim ExplodedNodeSet dstGenericPrevisit; 147245431Sdim getCheckerManager().runCheckersForPreCall(dstGenericPrevisit, dstPrevisit, 148245431Sdim *Msg, *this); 149245431Sdim 150226586Sdim // Proceed with evaluate the message expression. 151226586Sdim ExplodedNodeSet dstEval; 152245431Sdim StmtNodeBuilder Bldr(dstGenericPrevisit, dstEval, *currBldrCtx); 153235633Sdim 154245431Sdim for (ExplodedNodeSet::iterator DI = dstGenericPrevisit.begin(), 155245431Sdim DE = dstGenericPrevisit.end(); DI != DE; ++DI) { 156226586Sdim ExplodedNode *Pred = *DI; 157245431Sdim ProgramStateRef State = Pred->getState(); 158245431Sdim CallEventRef<ObjCMethodCall> UpdatedMsg = Msg.cloneWithState(State); 159226586Sdim 160245431Sdim if (UpdatedMsg->isInstanceMessage()) { 161245431Sdim SVal recVal = UpdatedMsg->getReceiverSVal(); 162226586Sdim if (!recVal.isUndef()) { 163226586Sdim // Bifurcate the state into nil and non-nil ones. 164252723Sdim DefinedOrUnknownSVal receiverVal = 165252723Sdim recVal.castAs<DefinedOrUnknownSVal>(); 166252723Sdim 167235633Sdim ProgramStateRef notNilState, nilState; 168245431Sdim llvm::tie(notNilState, nilState) = State->assume(receiverVal); 169226586Sdim 170226586Sdim // There are three cases: can be nil or non-nil, must be nil, must be 171226586Sdim // non-nil. We ignore must be nil, and merge the rest two into non-nil. 172245431Sdim // FIXME: This ignores many potential bugs (<rdar://problem/11733396>). 173245431Sdim // Revisit once we have lazier constraints. 174226586Sdim if (nilState && !notNilState) { 175226586Sdim continue; 176226586Sdim } 177226586Sdim 178226586Sdim // Check if the "raise" message was sent. 179226586Sdim assert(notNilState); 180245431Sdim if (ObjCNoRet.isImplicitNoReturn(ME)) { 181245431Sdim // If we raise an exception, for now treat it as a sink. 182245431Sdim // Eventually we will want to handle exceptions properly. 183252723Sdim Bldr.generateSink(ME, Pred, State); 184245431Sdim continue; 185245431Sdim } 186226586Sdim 187245431Sdim // Generate a transition to non-Nil state. 188245431Sdim if (notNilState != State) { 189252723Sdim Pred = Bldr.generateNode(ME, Pred, notNilState); 190245431Sdim assert(Pred && "Should have cached out already!"); 191245431Sdim } 192245431Sdim } 193245431Sdim } else { 194245431Sdim // Check for special class methods that are known to not return 195245431Sdim // and that we should treat as a sink. 196245431Sdim if (ObjCNoRet.isImplicitNoReturn(ME)) { 197235633Sdim // If we raise an exception, for now treat it as a sink. 198226586Sdim // Eventually we will want to handle exceptions properly. 199252723Sdim Bldr.generateSink(ME, Pred, Pred->getState()); 200245431Sdim continue; 201226586Sdim } 202226586Sdim } 203245431Sdim 204245431Sdim defaultEvalCall(Bldr, Pred, *UpdatedMsg); 205226586Sdim } 206226586Sdim 207245431Sdim ExplodedNodeSet dstPostvisit; 208245431Sdim getCheckerManager().runCheckersForPostCall(dstPostvisit, dstEval, 209245431Sdim *Msg, *this); 210245431Sdim 211226586Sdim // Finally, perform the post-condition check of the ObjCMessageExpr and store 212226586Sdim // the created nodes in 'Dst'. 213245431Sdim getCheckerManager().runCheckersForPostObjCMessage(Dst, dstPostvisit, 214245431Sdim *Msg, *this); 215226586Sdim} 216