ExprEngineObjC.cpp revision 226586
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 14226586Sdim#include "clang/StaticAnalyzer/Core/CheckerManager.h" 15226586Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 16226586Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h" 17226586Sdim#include "clang/Analysis/Support/SaveAndRestore.h" 18226586Sdim 19226586Sdimusing namespace clang; 20226586Sdimusing namespace ento; 21226586Sdim 22226586Sdimvoid ExprEngine::VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr *Ex, 23226586Sdim ExplodedNode *Pred, 24226586Sdim ExplodedNodeSet &Dst) { 25226586Sdim 26226586Sdim const ProgramState *state = Pred->getState(); 27226586Sdim SVal baseVal = state->getSVal(Ex->getBase()); 28226586Sdim SVal location = state->getLValue(Ex->getDecl(), baseVal); 29226586Sdim 30226586Sdim ExplodedNodeSet dstIvar; 31226586Sdim MakeNode(dstIvar, Ex, Pred, state->BindExpr(Ex, location)); 32226586Sdim 33226586Sdim // Perform the post-condition check of the ObjCIvarRefExpr and store 34226586Sdim // the created nodes in 'Dst'. 35226586Sdim getCheckerManager().runCheckersForPostStmt(Dst, dstIvar, Ex, *this); 36226586Sdim} 37226586Sdim 38226586Sdimvoid ExprEngine::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S, 39226586Sdim ExplodedNode *Pred, 40226586Sdim ExplodedNodeSet &Dst) { 41226586Sdim getCheckerManager().runCheckersForPreStmt(Dst, Pred, S, *this); 42226586Sdim} 43226586Sdim 44226586Sdimvoid ExprEngine::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S, 45226586Sdim ExplodedNode *Pred, 46226586Sdim ExplodedNodeSet &Dst) { 47226586Sdim 48226586Sdim // ObjCForCollectionStmts are processed in two places. This method 49226586Sdim // handles the case where an ObjCForCollectionStmt* occurs as one of the 50226586Sdim // statements within a basic block. This transfer function does two things: 51226586Sdim // 52226586Sdim // (1) binds the next container value to 'element'. This creates a new 53226586Sdim // node in the ExplodedGraph. 54226586Sdim // 55226586Sdim // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating 56226586Sdim // whether or not the container has any more elements. This value 57226586Sdim // will be tested in ProcessBranch. We need to explicitly bind 58226586Sdim // this value because a container can contain nil elements. 59226586Sdim // 60226586Sdim // FIXME: Eventually this logic should actually do dispatches to 61226586Sdim // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration). 62226586Sdim // This will require simulating a temporary NSFastEnumerationState, either 63226586Sdim // through an SVal or through the use of MemRegions. This value can 64226586Sdim // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop 65226586Sdim // terminates we reclaim the temporary (it goes out of scope) and we 66226586Sdim // we can test if the SVal is 0 or if the MemRegion is null (depending 67226586Sdim // on what approach we take). 68226586Sdim // 69226586Sdim // For now: simulate (1) by assigning either a symbol or nil if the 70226586Sdim // container is empty. Thus this transfer function will by default 71226586Sdim // result in state splitting. 72226586Sdim 73226586Sdim const Stmt *elem = S->getElement(); 74226586Sdim const ProgramState *state = Pred->getState(); 75226586Sdim SVal elementV; 76226586Sdim 77226586Sdim if (const DeclStmt *DS = dyn_cast<DeclStmt>(elem)) { 78226586Sdim const VarDecl *elemD = cast<VarDecl>(DS->getSingleDecl()); 79226586Sdim assert(elemD->getInit() == 0); 80226586Sdim elementV = state->getLValue(elemD, Pred->getLocationContext()); 81226586Sdim } 82226586Sdim else { 83226586Sdim elementV = state->getSVal(elem); 84226586Sdim } 85226586Sdim 86226586Sdim ExplodedNodeSet dstLocation; 87226586Sdim evalLocation(dstLocation, elem, Pred, state, elementV, NULL, false); 88226586Sdim 89226586Sdim if (dstLocation.empty()) 90226586Sdim return; 91226586Sdim 92226586Sdim for (ExplodedNodeSet::iterator NI = dstLocation.begin(), 93226586Sdim NE = dstLocation.end(); NI!=NE; ++NI) { 94226586Sdim Pred = *NI; 95226586Sdim const ProgramState *state = Pred->getState(); 96226586Sdim 97226586Sdim // Handle the case where the container still has elements. 98226586Sdim SVal TrueV = svalBuilder.makeTruthVal(1); 99226586Sdim const ProgramState *hasElems = state->BindExpr(S, TrueV); 100226586Sdim 101226586Sdim // Handle the case where the container has no elements. 102226586Sdim SVal FalseV = svalBuilder.makeTruthVal(0); 103226586Sdim const ProgramState *noElems = state->BindExpr(S, FalseV); 104226586Sdim 105226586Sdim if (loc::MemRegionVal *MV = dyn_cast<loc::MemRegionVal>(&elementV)) 106226586Sdim if (const TypedValueRegion *R = 107226586Sdim dyn_cast<TypedValueRegion>(MV->getRegion())) { 108226586Sdim // FIXME: The proper thing to do is to really iterate over the 109226586Sdim // container. We will do this with dispatch logic to the store. 110226586Sdim // For now, just 'conjure' up a symbolic value. 111226586Sdim QualType T = R->getValueType(); 112226586Sdim assert(Loc::isLocType(T)); 113226586Sdim unsigned Count = Builder->getCurrentBlockCount(); 114226586Sdim SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count); 115226586Sdim SVal V = svalBuilder.makeLoc(Sym); 116226586Sdim hasElems = hasElems->bindLoc(elementV, V); 117226586Sdim 118226586Sdim // Bind the location to 'nil' on the false branch. 119226586Sdim SVal nilV = svalBuilder.makeIntVal(0, T); 120226586Sdim noElems = noElems->bindLoc(elementV, nilV); 121226586Sdim } 122226586Sdim 123226586Sdim // Create the new nodes. 124226586Sdim MakeNode(Dst, S, Pred, hasElems); 125226586Sdim MakeNode(Dst, S, Pred, noElems); 126226586Sdim } 127226586Sdim} 128226586Sdim 129226586Sdimvoid ExprEngine::VisitObjCMessage(const ObjCMessage &msg, 130226586Sdim ExplodedNode *Pred, 131226586Sdim ExplodedNodeSet &Dst) { 132226586Sdim 133226586Sdim // Handle the previsits checks. 134226586Sdim ExplodedNodeSet dstPrevisit; 135226586Sdim getCheckerManager().runCheckersForPreObjCMessage(dstPrevisit, Pred, 136226586Sdim msg, *this); 137226586Sdim 138226586Sdim // Proceed with evaluate the message expression. 139226586Sdim ExplodedNodeSet dstEval; 140226586Sdim 141226586Sdim for (ExplodedNodeSet::iterator DI = dstPrevisit.begin(), 142226586Sdim DE = dstPrevisit.end(); DI != DE; ++DI) { 143226586Sdim 144226586Sdim ExplodedNode *Pred = *DI; 145226586Sdim bool RaisesException = false; 146226586Sdim SaveAndRestore<bool> OldSink(Builder->BuildSinks); 147226586Sdim SaveOr OldHasGen(Builder->hasGeneratedNode); 148226586Sdim 149226586Sdim if (const Expr *Receiver = msg.getInstanceReceiver()) { 150226586Sdim const ProgramState *state = Pred->getState(); 151226586Sdim SVal recVal = state->getSVal(Receiver); 152226586Sdim if (!recVal.isUndef()) { 153226586Sdim // Bifurcate the state into nil and non-nil ones. 154226586Sdim DefinedOrUnknownSVal receiverVal = cast<DefinedOrUnknownSVal>(recVal); 155226586Sdim 156226586Sdim const ProgramState *notNilState, *nilState; 157226586Sdim llvm::tie(notNilState, nilState) = state->assume(receiverVal); 158226586Sdim 159226586Sdim // There are three cases: can be nil or non-nil, must be nil, must be 160226586Sdim // non-nil. We ignore must be nil, and merge the rest two into non-nil. 161226586Sdim if (nilState && !notNilState) { 162226586Sdim dstEval.insert(Pred); 163226586Sdim continue; 164226586Sdim } 165226586Sdim 166226586Sdim // Check if the "raise" message was sent. 167226586Sdim assert(notNilState); 168226586Sdim if (msg.getSelector() == RaiseSel) 169226586Sdim RaisesException = true; 170226586Sdim 171226586Sdim // Check if we raise an exception. For now treat these as sinks. 172226586Sdim // Eventually we will want to handle exceptions properly. 173226586Sdim if (RaisesException) 174226586Sdim Builder->BuildSinks = true; 175226586Sdim 176226586Sdim // Dispatch to plug-in transfer function. 177226586Sdim evalObjCMessage(dstEval, msg, Pred, notNilState); 178226586Sdim } 179226586Sdim } 180226586Sdim else if (const ObjCInterfaceDecl *Iface = msg.getReceiverInterface()) { 181226586Sdim IdentifierInfo* ClsName = Iface->getIdentifier(); 182226586Sdim Selector S = msg.getSelector(); 183226586Sdim 184226586Sdim // Check for special instance methods. 185226586Sdim if (!NSExceptionII) { 186226586Sdim ASTContext &Ctx = getContext(); 187226586Sdim NSExceptionII = &Ctx.Idents.get("NSException"); 188226586Sdim } 189226586Sdim 190226586Sdim if (ClsName == NSExceptionII) { 191226586Sdim enum { NUM_RAISE_SELECTORS = 2 }; 192226586Sdim 193226586Sdim // Lazily create a cache of the selectors. 194226586Sdim if (!NSExceptionInstanceRaiseSelectors) { 195226586Sdim ASTContext &Ctx = getContext(); 196226586Sdim NSExceptionInstanceRaiseSelectors = 197226586Sdim new Selector[NUM_RAISE_SELECTORS]; 198226586Sdim SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II; 199226586Sdim unsigned idx = 0; 200226586Sdim 201226586Sdim // raise:format: 202226586Sdim II.push_back(&Ctx.Idents.get("raise")); 203226586Sdim II.push_back(&Ctx.Idents.get("format")); 204226586Sdim NSExceptionInstanceRaiseSelectors[idx++] = 205226586Sdim Ctx.Selectors.getSelector(II.size(), &II[0]); 206226586Sdim 207226586Sdim // raise:format::arguments: 208226586Sdim II.push_back(&Ctx.Idents.get("arguments")); 209226586Sdim NSExceptionInstanceRaiseSelectors[idx++] = 210226586Sdim Ctx.Selectors.getSelector(II.size(), &II[0]); 211226586Sdim } 212226586Sdim 213226586Sdim for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i) 214226586Sdim if (S == NSExceptionInstanceRaiseSelectors[i]) { 215226586Sdim RaisesException = true; 216226586Sdim break; 217226586Sdim } 218226586Sdim } 219226586Sdim 220226586Sdim // Check if we raise an exception. For now treat these as sinks. 221226586Sdim // Eventually we will want to handle exceptions properly. 222226586Sdim if (RaisesException) 223226586Sdim Builder->BuildSinks = true; 224226586Sdim 225226586Sdim // Dispatch to plug-in transfer function. 226226586Sdim evalObjCMessage(dstEval, msg, Pred, Pred->getState()); 227226586Sdim } 228226586Sdim 229226586Sdim assert(Builder->BuildSinks || Builder->hasGeneratedNode); 230226586Sdim } 231226586Sdim 232226586Sdim // Finally, perform the post-condition check of the ObjCMessageExpr and store 233226586Sdim // the created nodes in 'Dst'. 234226586Sdim getCheckerManager().runCheckersForPostObjCMessage(Dst, dstEval, msg, *this); 235226586Sdim} 236226586Sdim 237226586Sdimvoid ExprEngine::evalObjCMessage(ExplodedNodeSet &Dst, const ObjCMessage &msg, 238226586Sdim ExplodedNode *Pred, 239226586Sdim const ProgramState *state) { 240226586Sdim assert (Builder && "StmtNodeBuilder must be defined."); 241226586Sdim 242226586Sdim // First handle the return value. 243226586Sdim SVal ReturnValue = UnknownVal(); 244226586Sdim 245226586Sdim // Some method families have known return values. 246226586Sdim switch (msg.getMethodFamily()) { 247226586Sdim default: 248226586Sdim break; 249226586Sdim case OMF_autorelease: 250226586Sdim case OMF_retain: 251226586Sdim case OMF_self: { 252226586Sdim // These methods return their receivers. 253226586Sdim const Expr *ReceiverE = msg.getInstanceReceiver(); 254226586Sdim if (ReceiverE) 255226586Sdim ReturnValue = state->getSVal(ReceiverE); 256226586Sdim break; 257226586Sdim } 258226586Sdim } 259226586Sdim 260226586Sdim // If we failed to figure out the return value, use a conjured value instead. 261226586Sdim if (ReturnValue.isUnknown()) { 262226586Sdim SValBuilder &SVB = getSValBuilder(); 263226586Sdim QualType ResultTy = msg.getResultType(getContext()); 264226586Sdim unsigned Count = Builder->getCurrentBlockCount(); 265226586Sdim const Expr *CurrentE = cast<Expr>(currentStmt); 266226586Sdim ReturnValue = SVB.getConjuredSymbolVal(NULL, CurrentE, ResultTy, Count); 267226586Sdim } 268226586Sdim 269226586Sdim // Bind the return value. 270226586Sdim state = state->BindExpr(currentStmt, ReturnValue); 271226586Sdim 272226586Sdim // Invalidate the arguments (and the receiver) 273226586Sdim const LocationContext *LC = Pred->getLocationContext(); 274226586Sdim state = invalidateArguments(state, CallOrObjCMessage(msg, state), LC); 275226586Sdim 276226586Sdim // And create the new node. 277226586Sdim MakeNode(Dst, msg.getOriginExpr(), Pred, state); 278226586Sdim} 279226586Sdim 280