ExprEngineObjC.cpp revision 226586
1289694Sngie//=-- ExprEngineObjC.cpp - ExprEngine support for Objective-C ---*- C++ -*-===// 2289694Sngie// 3289694Sngie// The LLVM Compiler Infrastructure 4289694Sngie// 5289694Sngie// This file is distributed under the University of Illinois Open Source 6289694Sngie// License. See LICENSE.TXT for details. 7289694Sngie// 8289694Sngie//===----------------------------------------------------------------------===// 9289694Sngie// 10289694Sngie// This file defines ExprEngine's support for Objective-C expressions. 11289694Sngie// 12289694Sngie//===----------------------------------------------------------------------===// 13289694Sngie 14289694Sngie#include "clang/StaticAnalyzer/Core/CheckerManager.h" 15289694Sngie#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 16289694Sngie#include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h" 17289694Sngie#include "clang/Analysis/Support/SaveAndRestore.h" 18289694Sngie 19289694Sngieusing namespace clang; 20289694Sngieusing namespace ento; 21289694Sngie 22289694Sngievoid ExprEngine::VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr *Ex, 23289694Sngie ExplodedNode *Pred, 24289694Sngie ExplodedNodeSet &Dst) { 25289694Sngie 26289694Sngie const ProgramState *state = Pred->getState(); 27289694Sngie SVal baseVal = state->getSVal(Ex->getBase()); 28289694Sngie SVal location = state->getLValue(Ex->getDecl(), baseVal); 29289694Sngie 30289743Sngie ExplodedNodeSet dstIvar; 31289897Sngie MakeNode(dstIvar, Ex, Pred, state->BindExpr(Ex, location)); 32290267Sngie 33290267Sngie // Perform the post-condition check of the ObjCIvarRefExpr and store 34290267Sngie // the created nodes in 'Dst'. 35290267Sngie getCheckerManager().runCheckersForPostStmt(Dst, dstIvar, Ex, *this); 36290267Sngie} 37290267Sngie 38290267Sngievoid ExprEngine::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S, 39289897Sngie ExplodedNode *Pred, 40289897Sngie ExplodedNodeSet &Dst) { 41289897Sngie getCheckerManager().runCheckersForPreStmt(Dst, Pred, S, *this); 42289897Sngie} 43289897Sngie 44289694Sngievoid ExprEngine::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S, 45289897Sngie ExplodedNode *Pred, 46289897Sngie ExplodedNodeSet &Dst) { 47289897Sngie 48289897Sngie // ObjCForCollectionStmts are processed in two places. This method 49290267Sngie // handles the case where an ObjCForCollectionStmt* occurs as one of the 50289897Sngie // statements within a basic block. This transfer function does two things: 51289897Sngie // 52289897Sngie // (1) binds the next container value to 'element'. This creates a new 53289897Sngie // node in the ExplodedGraph. 54289897Sngie // 55289897Sngie // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating 56289897Sngie // whether or not the container has any more elements. This value 57289897Sngie // will be tested in ProcessBranch. We need to explicitly bind 58289897Sngie // this value because a container can contain nil elements. 59289897Sngie // 60289897Sngie // FIXME: Eventually this logic should actually do dispatches to 61289897Sngie // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration). 62289897Sngie // This will require simulating a temporary NSFastEnumerationState, either 63289897Sngie // through an SVal or through the use of MemRegions. This value can 64289897Sngie // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop 65289897Sngie // terminates we reclaim the temporary (it goes out of scope) and we 66289897Sngie // we can test if the SVal is 0 or if the MemRegion is null (depending 67289897Sngie // on what approach we take). 68289897Sngie // 69289897Sngie // For now: simulate (1) by assigning either a symbol or nil if the 70289897Sngie // container is empty. Thus this transfer function will by default 71289897Sngie // result in state splitting. 72289897Sngie 73289897Sngie const Stmt *elem = S->getElement(); 74289897Sngie const ProgramState *state = Pred->getState(); 75289897Sngie SVal elementV; 76289897Sngie 77289897Sngie if (const DeclStmt *DS = dyn_cast<DeclStmt>(elem)) { 78289897Sngie const VarDecl *elemD = cast<VarDecl>(DS->getSingleDecl()); 79289897Sngie assert(elemD->getInit() == 0); 80289897Sngie elementV = state->getLValue(elemD, Pred->getLocationContext()); 81289897Sngie } 82289897Sngie else { 83289897Sngie elementV = state->getSVal(elem); 84289897Sngie } 85289897Sngie 86289897Sngie ExplodedNodeSet dstLocation; 87289897Sngie evalLocation(dstLocation, elem, Pred, state, elementV, NULL, false); 88289897Sngie 89289897Sngie if (dstLocation.empty()) 90289897Sngie return; 91289897Sngie 92289897Sngie for (ExplodedNodeSet::iterator NI = dstLocation.begin(), 93289743Sngie NE = dstLocation.end(); NI!=NE; ++NI) { 94289694Sngie Pred = *NI; 95289694Sngie const ProgramState *state = Pred->getState(); 96289694Sngie 97289694Sngie // Handle the case where the container still has elements. 98289694Sngie SVal TrueV = svalBuilder.makeTruthVal(1); 99289694Sngie const ProgramState *hasElems = state->BindExpr(S, TrueV); 100289694Sngie 101289743Sngie // Handle the case where the container has no elements. 102289694Sngie SVal FalseV = svalBuilder.makeTruthVal(0); 103289743Sngie const ProgramState *noElems = state->BindExpr(S, FalseV); 104289743Sngie 105289694Sngie if (loc::MemRegionVal *MV = dyn_cast<loc::MemRegionVal>(&elementV)) 106289694Sngie if (const TypedValueRegion *R = 107289694Sngie dyn_cast<TypedValueRegion>(MV->getRegion())) { 108289694Sngie // FIXME: The proper thing to do is to really iterate over the 109289694Sngie // container. We will do this with dispatch logic to the store. 110289694Sngie // For now, just 'conjure' up a symbolic value. 111289743Sngie QualType T = R->getValueType(); 112289743Sngie assert(Loc::isLocType(T)); 113289743Sngie unsigned Count = Builder->getCurrentBlockCount(); 114289694Sngie SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count); 115289694Sngie SVal V = svalBuilder.makeLoc(Sym); 116289694Sngie hasElems = hasElems->bindLoc(elementV, V); 117289694Sngie 118289694Sngie // Bind the location to 'nil' on the false branch. 119289694Sngie SVal nilV = svalBuilder.makeIntVal(0, T); 120289694Sngie noElems = noElems->bindLoc(elementV, nilV); 121289694Sngie } 122289694Sngie 123289694Sngie // Create the new nodes. 124289694Sngie MakeNode(Dst, S, Pred, hasElems); 125289694Sngie MakeNode(Dst, S, Pred, noElems); 126289694Sngie } 127289694Sngie} 128289694Sngie 129289694Sngievoid ExprEngine::VisitObjCMessage(const ObjCMessage &msg, 130289694Sngie ExplodedNode *Pred, 131289694Sngie ExplodedNodeSet &Dst) { 132289694Sngie 133289743Sngie // Handle the previsits checks. 134289743Sngie ExplodedNodeSet dstPrevisit; 135289743Sngie getCheckerManager().runCheckersForPreObjCMessage(dstPrevisit, Pred, 136289743Sngie msg, *this); 137289743Sngie 138289743Sngie // Proceed with evaluate the message expression. 139289743Sngie ExplodedNodeSet dstEval; 140289743Sngie 141289743Sngie for (ExplodedNodeSet::iterator DI = dstPrevisit.begin(), 142289743Sngie DE = dstPrevisit.end(); DI != DE; ++DI) { 143289694Sngie 144289897Sngie ExplodedNode *Pred = *DI; 145289897Sngie bool RaisesException = false; 146289897Sngie SaveAndRestore<bool> OldSink(Builder->BuildSinks); 147289897Sngie SaveOr OldHasGen(Builder->hasGeneratedNode); 148289897Sngie 149289897Sngie if (const Expr *Receiver = msg.getInstanceReceiver()) { 150289897Sngie const ProgramState *state = Pred->getState(); 151289897Sngie SVal recVal = state->getSVal(Receiver); 152289897Sngie if (!recVal.isUndef()) { 153 // Bifurcate the state into nil and non-nil ones. 154 DefinedOrUnknownSVal receiverVal = cast<DefinedOrUnknownSVal>(recVal); 155 156 const ProgramState *notNilState, *nilState; 157 llvm::tie(notNilState, nilState) = state->assume(receiverVal); 158 159 // There are three cases: can be nil or non-nil, must be nil, must be 160 // non-nil. We ignore must be nil, and merge the rest two into non-nil. 161 if (nilState && !notNilState) { 162 dstEval.insert(Pred); 163 continue; 164 } 165 166 // Check if the "raise" message was sent. 167 assert(notNilState); 168 if (msg.getSelector() == RaiseSel) 169 RaisesException = true; 170 171 // Check if we raise an exception. For now treat these as sinks. 172 // Eventually we will want to handle exceptions properly. 173 if (RaisesException) 174 Builder->BuildSinks = true; 175 176 // Dispatch to plug-in transfer function. 177 evalObjCMessage(dstEval, msg, Pred, notNilState); 178 } 179 } 180 else if (const ObjCInterfaceDecl *Iface = msg.getReceiverInterface()) { 181 IdentifierInfo* ClsName = Iface->getIdentifier(); 182 Selector S = msg.getSelector(); 183 184 // Check for special instance methods. 185 if (!NSExceptionII) { 186 ASTContext &Ctx = getContext(); 187 NSExceptionII = &Ctx.Idents.get("NSException"); 188 } 189 190 if (ClsName == NSExceptionII) { 191 enum { NUM_RAISE_SELECTORS = 2 }; 192 193 // Lazily create a cache of the selectors. 194 if (!NSExceptionInstanceRaiseSelectors) { 195 ASTContext &Ctx = getContext(); 196 NSExceptionInstanceRaiseSelectors = 197 new Selector[NUM_RAISE_SELECTORS]; 198 SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II; 199 unsigned idx = 0; 200 201 // raise:format: 202 II.push_back(&Ctx.Idents.get("raise")); 203 II.push_back(&Ctx.Idents.get("format")); 204 NSExceptionInstanceRaiseSelectors[idx++] = 205 Ctx.Selectors.getSelector(II.size(), &II[0]); 206 207 // raise:format::arguments: 208 II.push_back(&Ctx.Idents.get("arguments")); 209 NSExceptionInstanceRaiseSelectors[idx++] = 210 Ctx.Selectors.getSelector(II.size(), &II[0]); 211 } 212 213 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i) 214 if (S == NSExceptionInstanceRaiseSelectors[i]) { 215 RaisesException = true; 216 break; 217 } 218 } 219 220 // Check if we raise an exception. For now treat these as sinks. 221 // Eventually we will want to handle exceptions properly. 222 if (RaisesException) 223 Builder->BuildSinks = true; 224 225 // Dispatch to plug-in transfer function. 226 evalObjCMessage(dstEval, msg, Pred, Pred->getState()); 227 } 228 229 assert(Builder->BuildSinks || Builder->hasGeneratedNode); 230 } 231 232 // Finally, perform the post-condition check of the ObjCMessageExpr and store 233 // the created nodes in 'Dst'. 234 getCheckerManager().runCheckersForPostObjCMessage(Dst, dstEval, msg, *this); 235} 236 237void ExprEngine::evalObjCMessage(ExplodedNodeSet &Dst, const ObjCMessage &msg, 238 ExplodedNode *Pred, 239 const ProgramState *state) { 240 assert (Builder && "StmtNodeBuilder must be defined."); 241 242 // First handle the return value. 243 SVal ReturnValue = UnknownVal(); 244 245 // Some method families have known return values. 246 switch (msg.getMethodFamily()) { 247 default: 248 break; 249 case OMF_autorelease: 250 case OMF_retain: 251 case OMF_self: { 252 // These methods return their receivers. 253 const Expr *ReceiverE = msg.getInstanceReceiver(); 254 if (ReceiverE) 255 ReturnValue = state->getSVal(ReceiverE); 256 break; 257 } 258 } 259 260 // If we failed to figure out the return value, use a conjured value instead. 261 if (ReturnValue.isUnknown()) { 262 SValBuilder &SVB = getSValBuilder(); 263 QualType ResultTy = msg.getResultType(getContext()); 264 unsigned Count = Builder->getCurrentBlockCount(); 265 const Expr *CurrentE = cast<Expr>(currentStmt); 266 ReturnValue = SVB.getConjuredSymbolVal(NULL, CurrentE, ResultTy, Count); 267 } 268 269 // Bind the return value. 270 state = state->BindExpr(currentStmt, ReturnValue); 271 272 // Invalidate the arguments (and the receiver) 273 const LocationContext *LC = Pred->getLocationContext(); 274 state = invalidateArguments(state, CallOrObjCMessage(msg, state), LC); 275 276 // And create the new node. 277 MakeNode(Dst, msg.getOriginExpr(), Pred, state); 278} 279 280