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 14234353Sdim#include "clang/AST/StmtObjC.h" 15226586Sdim#include "clang/StaticAnalyzer/Core/CheckerManager.h" 16239462Sdim#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) { 25234353Sdim ProgramStateRef state = Pred->getState(); 26234353Sdim const LocationContext *LCtx = Pred->getLocationContext(); 27234353Sdim SVal baseVal = state->getSVal(Ex->getBase(), LCtx); 28226586Sdim SVal location = state->getLValue(Ex->getDecl(), baseVal); 29226586Sdim 30226586Sdim ExplodedNodeSet dstIvar; 31243830Sdim StmtNodeBuilder Bldr(Pred, dstIvar, *currBldrCtx); 32234353Sdim 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. 73234353Sdim 74226586Sdim const Stmt *elem = S->getElement(); 75234353Sdim 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 { 84234353Sdim elementV = state->getSVal(elem, Pred->getLocationContext()); 85226586Sdim } 86226586Sdim 87226586Sdim ExplodedNodeSet dstLocation; 88234353Sdim evalLocation(dstLocation, S, elem, Pred, state, elementV, NULL, false); 89239462Sdim 90239462Sdim ExplodedNodeSet Tmp; 91243830Sdim StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx); 92239462Sdim 93226586Sdim for (ExplodedNodeSet::iterator NI = dstLocation.begin(), 94226586Sdim NE = dstLocation.end(); NI!=NE; ++NI) { 95226586Sdim Pred = *NI; 96234353Sdim ProgramStateRef state = Pred->getState(); 97234353Sdim const LocationContext *LCtx = Pred->getLocationContext(); 98226586Sdim 99226586Sdim // Handle the case where the container still has elements. 100226586Sdim SVal TrueV = svalBuilder.makeTruthVal(1); 101234353Sdim ProgramStateRef hasElems = state->BindExpr(S, LCtx, TrueV); 102226586Sdim 103226586Sdim // Handle the case where the container has no elements. 104226586Sdim SVal FalseV = svalBuilder.makeTruthVal(0); 105234353Sdim ProgramStateRef noElems = state->BindExpr(S, LCtx, FalseV); 106249423Sdim 107249423Sdim 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)); 115243830Sdim SymbolRef Sym = SymMgr.conjureSymbol(elem, LCtx, T, 116243830Sdim 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. 126234353Sdim Bldr.generateNode(S, Pred, hasElems); 127234353Sdim Bldr.generateNode(S, Pred, noElems); 128226586Sdim } 129239462Sdim 130239462Sdim // Finally, run any custom checkers. 131239462Sdim // FIXME: Eventually all pre- and post-checks should live in VisitStmt. 132239462Sdim getCheckerManager().runCheckersForPostStmt(Dst, Tmp, S, *this); 133226586Sdim} 134226586Sdim 135239462Sdimvoid ExprEngine::VisitObjCMessage(const ObjCMessageExpr *ME, 136226586Sdim ExplodedNode *Pred, 137226586Sdim ExplodedNodeSet &Dst) { 138239462Sdim CallEventManager &CEMgr = getStateManager().getCallEventManager(); 139239462Sdim CallEventRef<ObjCMethodCall> Msg = 140239462Sdim CEMgr.getObjCMethodCall(ME, Pred->getState(), Pred->getLocationContext()); 141239462Sdim 142226586Sdim // Handle the previsits checks. 143226586Sdim ExplodedNodeSet dstPrevisit; 144239462Sdim getCheckerManager().runCheckersForPreObjCMessage(dstPrevisit, Pred, 145239462Sdim *Msg, *this); 146239462Sdim ExplodedNodeSet dstGenericPrevisit; 147239462Sdim getCheckerManager().runCheckersForPreCall(dstGenericPrevisit, dstPrevisit, 148239462Sdim *Msg, *this); 149239462Sdim 150226586Sdim // Proceed with evaluate the message expression. 151226586Sdim ExplodedNodeSet dstEval; 152243830Sdim StmtNodeBuilder Bldr(dstGenericPrevisit, dstEval, *currBldrCtx); 153234353Sdim 154239462Sdim for (ExplodedNodeSet::iterator DI = dstGenericPrevisit.begin(), 155239462Sdim DE = dstGenericPrevisit.end(); DI != DE; ++DI) { 156226586Sdim ExplodedNode *Pred = *DI; 157239462Sdim ProgramStateRef State = Pred->getState(); 158239462Sdim CallEventRef<ObjCMethodCall> UpdatedMsg = Msg.cloneWithState(State); 159226586Sdim 160239462Sdim if (UpdatedMsg->isInstanceMessage()) { 161239462Sdim SVal recVal = UpdatedMsg->getReceiverSVal(); 162226586Sdim if (!recVal.isUndef()) { 163226586Sdim // Bifurcate the state into nil and non-nil ones. 164249423Sdim DefinedOrUnknownSVal receiverVal = 165249423Sdim recVal.castAs<DefinedOrUnknownSVal>(); 166249423Sdim 167234353Sdim ProgramStateRef notNilState, nilState; 168239462Sdim 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. 172239462Sdim // FIXME: This ignores many potential bugs (<rdar://problem/11733396>). 173239462Sdim // 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); 180243830Sdim if (ObjCNoRet.isImplicitNoReturn(ME)) { 181239462Sdim // If we raise an exception, for now treat it as a sink. 182239462Sdim // Eventually we will want to handle exceptions properly. 183249423Sdim Bldr.generateSink(ME, Pred, State); 184239462Sdim continue; 185239462Sdim } 186226586Sdim 187239462Sdim // Generate a transition to non-Nil state. 188243830Sdim if (notNilState != State) { 189249423Sdim Pred = Bldr.generateNode(ME, Pred, notNilState); 190243830Sdim assert(Pred && "Should have cached out already!"); 191243830Sdim } 192226586Sdim } 193239462Sdim } else { 194243830Sdim // Check for special class methods that are known to not return 195243830Sdim // and that we should treat as a sink. 196243830Sdim if (ObjCNoRet.isImplicitNoReturn(ME)) { 197243830Sdim // If we raise an exception, for now treat it as a sink. 198243830Sdim // Eventually we will want to handle exceptions properly. 199249423Sdim Bldr.generateSink(ME, Pred, Pred->getState()); 200243830Sdim continue; 201226586Sdim } 202226586Sdim } 203239462Sdim 204239462Sdim defaultEvalCall(Bldr, Pred, *UpdatedMsg); 205226586Sdim } 206226586Sdim 207239462Sdim ExplodedNodeSet dstPostvisit; 208239462Sdim getCheckerManager().runCheckersForPostCall(dstPostvisit, dstEval, 209239462Sdim *Msg, *this); 210239462Sdim 211226586Sdim // Finally, perform the post-condition check of the ObjCMessageExpr and store 212226586Sdim // the created nodes in 'Dst'. 213239462Sdim getCheckerManager().runCheckersForPostObjCMessage(Dst, dstPostvisit, 214239462Sdim *Msg, *this); 215226586Sdim} 216