ExprEngineObjC.cpp revision 296417
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 22296417Sdimvoid 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); 29296417Sdim 30226586Sdim ExplodedNodeSet dstIvar; 31243830Sdim StmtNodeBuilder Bldr(Pred, dstIvar, *currBldrCtx); 32234353Sdim Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, location)); 33296417Sdim 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) { 48296417Sdim 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; 77296417Sdim 78226586Sdim if (const DeclStmt *DS = dyn_cast<DeclStmt>(elem)) { 79226586Sdim const VarDecl *elemD = cast<VarDecl>(DS->getSingleDecl()); 80276479Sdim assert(elemD->getInit() == nullptr); 81226586Sdim elementV = state->getLValue(elemD, Pred->getLocationContext()); 82226586Sdim } 83226586Sdim else { 84234353Sdim elementV = state->getSVal(elem, Pred->getLocationContext()); 85226586Sdim } 86296417Sdim 87226586Sdim ExplodedNodeSet dstLocation; 88276479Sdim evalLocation(dstLocation, S, elem, Pred, state, elementV, nullptr, 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(); 98296417Sdim 99226586Sdim // Handle the case where the container still has elements. 100226586Sdim SVal TrueV = svalBuilder.makeTruthVal(1); 101234353Sdim ProgramStateRef hasElems = state->BindExpr(S, LCtx, TrueV); 102296417Sdim 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>()) 108296417Sdim 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); 119296417Sdim 120226586Sdim // Bind the location to 'nil' on the false branch. 121226586Sdim SVal nilV = svalBuilder.makeIntVal(0, T); 122226586Sdim noElems = noElems->bindLoc(elementV, nilV); 123226586Sdim } 124296417Sdim 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 142296417Sdim // There are three cases for the receiver: 143296417Sdim // (1) it is definitely nil, 144296417Sdim // (2) it is definitely non-nil, and 145296417Sdim // (3) we don't know. 146296417Sdim // 147296417Sdim // If the receiver is definitely nil, we skip the pre/post callbacks and 148296417Sdim // instead call the ObjCMessageNil callbacks and return. 149296417Sdim // 150296417Sdim // If the receiver is definitely non-nil, we call the pre- callbacks, 151296417Sdim // evaluate the call, and call the post- callbacks. 152296417Sdim // 153296417Sdim // If we don't know, we drop the potential nil flow and instead 154296417Sdim // continue from the assumed non-nil state as in (2). This approach 155296417Sdim // intentionally drops coverage in order to prevent false alarms 156296417Sdim // in the following scenario: 157296417Sdim // 158296417Sdim // id result = [o someMethod] 159296417Sdim // if (result) { 160296417Sdim // if (!o) { 161296417Sdim // // <-- This program point should be unreachable because if o is nil 162296417Sdim // // it must the case that result is nil as well. 163296417Sdim // } 164296417Sdim // } 165296417Sdim // 166296417Sdim // We could avoid dropping coverage by performing an explicit case split 167296417Sdim // on each method call -- but this would get very expensive. An alternative 168296417Sdim // would be to introduce lazy constraints. 169296417Sdim // FIXME: This ignores many potential bugs (<rdar://problem/11733396>). 170296417Sdim // Revisit once we have lazier constraints. 171296417Sdim if (Msg->isInstanceMessage()) { 172296417Sdim SVal recVal = Msg->getReceiverSVal(); 173296417Sdim if (!recVal.isUndef()) { 174296417Sdim // Bifurcate the state into nil and non-nil ones. 175296417Sdim DefinedOrUnknownSVal receiverVal = 176296417Sdim recVal.castAs<DefinedOrUnknownSVal>(); 177296417Sdim ProgramStateRef State = Pred->getState(); 178296417Sdim 179296417Sdim ProgramStateRef notNilState, nilState; 180296417Sdim std::tie(notNilState, nilState) = State->assume(receiverVal); 181296417Sdim 182296417Sdim // Receiver is definitely nil, so run ObjCMessageNil callbacks and return. 183296417Sdim if (nilState && !notNilState) { 184296417Sdim StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 185296417Sdim bool HasTag = Pred->getLocation().getTag(); 186296417Sdim Pred = Bldr.generateNode(ME, Pred, nilState, nullptr, 187296417Sdim ProgramPoint::PreStmtKind); 188296417Sdim assert((Pred || HasTag) && "Should have cached out already!"); 189296417Sdim (void)HasTag; 190296417Sdim if (!Pred) 191296417Sdim return; 192296417Sdim getCheckerManager().runCheckersForObjCMessageNil(Dst, Pred, 193296417Sdim *Msg, *this); 194296417Sdim return; 195296417Sdim } 196296417Sdim 197296417Sdim ExplodedNodeSet dstNonNil; 198296417Sdim StmtNodeBuilder Bldr(Pred, dstNonNil, *currBldrCtx); 199296417Sdim // Generate a transition to the non-nil state, dropping any potential 200296417Sdim // nil flow. 201296417Sdim if (notNilState != State) { 202296417Sdim bool HasTag = Pred->getLocation().getTag(); 203296417Sdim Pred = Bldr.generateNode(ME, Pred, notNilState); 204296417Sdim assert((Pred || HasTag) && "Should have cached out already!"); 205296417Sdim (void)HasTag; 206296417Sdim if (!Pred) 207296417Sdim return; 208296417Sdim } 209296417Sdim } 210296417Sdim } 211296417Sdim 212226586Sdim // Handle the previsits checks. 213226586Sdim ExplodedNodeSet dstPrevisit; 214239462Sdim getCheckerManager().runCheckersForPreObjCMessage(dstPrevisit, Pred, 215239462Sdim *Msg, *this); 216239462Sdim ExplodedNodeSet dstGenericPrevisit; 217239462Sdim getCheckerManager().runCheckersForPreCall(dstGenericPrevisit, dstPrevisit, 218239462Sdim *Msg, *this); 219239462Sdim 220226586Sdim // Proceed with evaluate the message expression. 221226586Sdim ExplodedNodeSet dstEval; 222243830Sdim StmtNodeBuilder Bldr(dstGenericPrevisit, dstEval, *currBldrCtx); 223234353Sdim 224239462Sdim for (ExplodedNodeSet::iterator DI = dstGenericPrevisit.begin(), 225239462Sdim DE = dstGenericPrevisit.end(); DI != DE; ++DI) { 226226586Sdim ExplodedNode *Pred = *DI; 227239462Sdim ProgramStateRef State = Pred->getState(); 228239462Sdim CallEventRef<ObjCMethodCall> UpdatedMsg = Msg.cloneWithState(State); 229296417Sdim 230239462Sdim if (UpdatedMsg->isInstanceMessage()) { 231239462Sdim SVal recVal = UpdatedMsg->getReceiverSVal(); 232226586Sdim if (!recVal.isUndef()) { 233243830Sdim if (ObjCNoRet.isImplicitNoReturn(ME)) { 234239462Sdim // If we raise an exception, for now treat it as a sink. 235239462Sdim // Eventually we will want to handle exceptions properly. 236249423Sdim Bldr.generateSink(ME, Pred, State); 237239462Sdim continue; 238239462Sdim } 239226586Sdim } 240239462Sdim } else { 241243830Sdim // Check for special class methods that are known to not return 242243830Sdim // and that we should treat as a sink. 243243830Sdim if (ObjCNoRet.isImplicitNoReturn(ME)) { 244243830Sdim // If we raise an exception, for now treat it as a sink. 245243830Sdim // Eventually we will want to handle exceptions properly. 246249423Sdim Bldr.generateSink(ME, Pred, Pred->getState()); 247243830Sdim continue; 248226586Sdim } 249226586Sdim } 250239462Sdim 251239462Sdim defaultEvalCall(Bldr, Pred, *UpdatedMsg); 252226586Sdim } 253296417Sdim 254239462Sdim ExplodedNodeSet dstPostvisit; 255239462Sdim getCheckerManager().runCheckersForPostCall(dstPostvisit, dstEval, 256239462Sdim *Msg, *this); 257239462Sdim 258226586Sdim // Finally, perform the post-condition check of the ObjCMessageExpr and store 259226586Sdim // the created nodes in 'Dst'. 260239462Sdim getCheckerManager().runCheckersForPostObjCMessage(Dst, dstPostvisit, 261239462Sdim *Msg, *this); 262226586Sdim} 263