ExprEngineCallAndReturn.cpp revision 62a5c34ddc54696725683f6c5af1c8e1592c5c38
1//=-- ExprEngineCallAndReturn.cpp - Support for call/return -----*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines ExprEngine's support for calls and returns. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/StaticAnalyzer/Core/CheckerManager.h" 15#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 16#include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h" 17#include "llvm/Support/SaveAndRestore.h" 18#include "clang/AST/DeclCXX.h" 19 20using namespace clang; 21using namespace ento; 22 23void ExprEngine::processCallEnter(CallEnter CE, ExplodedNode *Pred) { 24 // Get the entry block in the CFG of the callee. 25 const StackFrameContext *calleeCtx = CE.getCalleeContext(); 26 const CFG *CalleeCFG = calleeCtx->getCFG(); 27 const CFGBlock *Entry = &(CalleeCFG->getEntry()); 28 29 // Validate the CFG. 30 assert(Entry->empty()); 31 assert(Entry->succ_size() == 1); 32 33 // Get the solitary sucessor. 34 const CFGBlock *Succ = *(Entry->succ_begin()); 35 36 // Construct an edge representing the starting location in the callee. 37 BlockEdge Loc(Entry, Succ, calleeCtx); 38 39 // Construct a new state which contains the mapping from actual to 40 // formal arguments. 41 const LocationContext *callerCtx = Pred->getLocationContext(); 42 ProgramStateRef state = Pred->getState()->enterStackFrame(callerCtx, 43 calleeCtx); 44 45 // Construct a new node and add it to the worklist. 46 bool isNew; 47 ExplodedNode *Node = G.getNode(Loc, state, false, &isNew); 48 Node->addPredecessor(Pred, G); 49 if (isNew) 50 Engine.getWorkList()->enqueue(Node); 51} 52 53static const ReturnStmt *getReturnStmt(const ExplodedNode *Node) { 54 while (Node) { 55 const ProgramPoint &PP = Node->getLocation(); 56 // Skip any BlockEdges. 57 if (isa<BlockEdge>(PP) || isa<CallExit>(PP)) { 58 assert(Node->pred_size() == 1); 59 Node = *Node->pred_begin(); 60 continue; 61 } 62 if (const StmtPoint *SP = dyn_cast<StmtPoint>(&PP)) { 63 const Stmt *S = SP->getStmt(); 64 return dyn_cast<ReturnStmt>(S); 65 } 66 break; 67 } 68 return 0; 69} 70 71void ExprEngine::processCallExit(ExplodedNode *Pred) { 72 ProgramStateRef state = Pred->getState(); 73 const StackFrameContext *calleeCtx = 74 Pred->getLocationContext()->getCurrentStackFrame(); 75 const LocationContext *callerCtx = calleeCtx->getParent(); 76 const Stmt *CE = calleeCtx->getCallSite(); 77 78 // If the callee returns an expression, bind its value to CallExpr. 79 if (const ReturnStmt *RS = getReturnStmt(Pred)) { 80 const LocationContext *LCtx = Pred->getLocationContext(); 81 SVal V = state->getSVal(RS, LCtx); 82 state = state->BindExpr(CE, callerCtx, V); 83 } 84 85 // Bind the constructed object value to CXXConstructExpr. 86 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(CE)) { 87 const CXXThisRegion *ThisR = 88 getCXXThisRegion(CCE->getConstructor()->getParent(), calleeCtx); 89 90 SVal ThisV = state->getSVal(ThisR); 91 // Always bind the region to the CXXConstructExpr. 92 state = state->BindExpr(CCE, Pred->getLocationContext(), ThisV); 93 } 94 95 static SimpleProgramPointTag returnTag("ExprEngine : Call Return"); 96 PostStmt Loc(CE, callerCtx, &returnTag); 97 bool isNew; 98 ExplodedNode *N = G.getNode(Loc, state, false, &isNew); 99 N->addPredecessor(Pred, G); 100 if (!isNew) 101 return; 102 103 // Perform the post-condition check of the CallExpr. 104 ExplodedNodeSet Dst; 105 NodeBuilderContext Ctx(Engine, calleeCtx->getCallSiteBlock(), N); 106 SaveAndRestore<const NodeBuilderContext*> NBCSave(currentBuilderContext, 107 &Ctx); 108 SaveAndRestore<unsigned> CBISave(currentStmtIdx, calleeCtx->getIndex()); 109 110 getCheckerManager().runCheckersForPostStmt(Dst, N, CE, *this, 111 /* wasInlined */ true); 112 113 // Enqueue the next element in the block. 114 for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end(); I != E; ++I) { 115 Engine.getWorkList()->enqueue(*I, 116 calleeCtx->getCallSiteBlock(), 117 calleeCtx->getIndex()+1); 118 } 119} 120 121static unsigned getNumberStackFrames(const LocationContext *LCtx) { 122 unsigned count = 0; 123 while (LCtx) { 124 if (isa<StackFrameContext>(LCtx)) 125 ++count; 126 LCtx = LCtx->getParent(); 127 } 128 return count; 129} 130 131// Determine if we should inline the call. 132bool ExprEngine::shouldInlineDecl(const FunctionDecl *FD, ExplodedNode *Pred) { 133 AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(FD); 134 const CFG *CalleeCFG = CalleeADC->getCFG(); 135 136 if (getNumberStackFrames(Pred->getLocationContext()) 137 == AMgr.InlineMaxStackDepth) 138 return false; 139 140 if (FunctionSummaries->hasReachedMaxBlockCount(FD)) 141 return false; 142 143 if (CalleeCFG->getNumBlockIDs() > AMgr.InlineMaxFunctionSize) 144 return false; 145 146 return true; 147} 148 149// For now, skip inlining variadic functions. 150// We also don't inline blocks. 151static bool shouldInlineCallExpr(const CallExpr *CE, ExprEngine *E) { 152 if (!E->getAnalysisManager().shouldInlineCall()) 153 return false; 154 QualType callee = CE->getCallee()->getType(); 155 const FunctionProtoType *FT = 0; 156 if (const PointerType *PT = callee->getAs<PointerType>()) 157 FT = dyn_cast<FunctionProtoType>(PT->getPointeeType()); 158 else if (const BlockPointerType *BT = callee->getAs<BlockPointerType>()) { 159 // FIXME: inline blocks. 160 // FT = dyn_cast<FunctionProtoType>(BT->getPointeeType()); 161 (void) BT; 162 return false; 163 } 164 // If we have no prototype, assume the function is okay. 165 if (!FT) 166 return true; 167 168 // Skip inlining of variadic functions. 169 return !FT->isVariadic(); 170} 171 172bool ExprEngine::InlineCall(ExplodedNodeSet &Dst, 173 const CallExpr *CE, 174 ExplodedNode *Pred) { 175 if (!shouldInlineCallExpr(CE, this)) 176 return false; 177 178 ProgramStateRef state = Pred->getState(); 179 const Expr *Callee = CE->getCallee(); 180 const FunctionDecl *FD = 181 state->getSVal(Callee, Pred->getLocationContext()).getAsFunctionDecl(); 182 if (!FD || !FD->hasBody(FD)) 183 return false; 184 185 switch (CE->getStmtClass()) { 186 default: 187 // FIXME: Handle C++. 188 break; 189 case Stmt::CallExprClass: { 190 if (!shouldInlineDecl(FD, Pred)) 191 return false; 192 193 // Construct a new stack frame for the callee. 194 AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(FD); 195 const StackFrameContext *CallerSFC = 196 Pred->getLocationContext()->getCurrentStackFrame(); 197 const StackFrameContext *CalleeSFC = 198 CalleeADC->getStackFrame(CallerSFC, CE, 199 currentBuilderContext->getBlock(), 200 currentStmtIdx); 201 202 CallEnter Loc(CE, CalleeSFC, Pred->getLocationContext()); 203 bool isNew; 204 ExplodedNode *N = G.getNode(Loc, state, false, &isNew); 205 N->addPredecessor(Pred, G); 206 if (isNew) 207 Engine.getWorkList()->enqueue(N); 208 return true; 209 } 210 } 211 return false; 212} 213 214static bool isPointerToConst(const ParmVarDecl *ParamDecl) { 215 QualType PointeeTy = ParamDecl->getOriginalType()->getPointeeType(); 216 if (PointeeTy != QualType() && PointeeTy.isConstQualified() && 217 !PointeeTy->isAnyPointerType() && !PointeeTy->isReferenceType()) { 218 return true; 219 } 220 return false; 221} 222 223// Try to retrieve the function declaration and find the function parameter 224// types which are pointers/references to a non-pointer const. 225// We do not invalidate the corresponding argument regions. 226static void findPtrToConstParams(llvm::SmallSet<unsigned, 1> &PreserveArgs, 227 const CallOrObjCMessage &Call) { 228 const Decl *CallDecl = Call.getDecl(); 229 if (!CallDecl) 230 return; 231 232 if (const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(CallDecl)) { 233 const IdentifierInfo *II = FDecl->getIdentifier(); 234 235 // List the cases, where the region should be invalidated even if the 236 // argument is const. 237 if (II) { 238 StringRef FName = II->getName(); 239 // - 'int pthread_setspecific(ptheread_key k, const void *)' stores a 240 // value into thread local storage. The value can later be retrieved with 241 // 'void *ptheread_getspecific(pthread_key)'. So even thought the 242 // parameter is 'const void *', the region escapes through the call. 243 // - funopen - sets a buffer for future IO calls. 244 // - ObjC functions that end with "NoCopy" can free memory, of the passed 245 // in buffer. 246 // - Many CF containers allow objects to escape through custom 247 // allocators/deallocators upon container construction. 248 // - NSXXInsertXX, for example NSMapInsertIfAbsent, since they can 249 // be deallocated by NSMapRemove. 250 if (FName == "pthread_setspecific" || 251 FName == "funopen" || 252 FName.endswith("NoCopy") || 253 (FName.startswith("NS") && 254 (FName.find("Insert") != StringRef::npos)) || 255 Call.isCFCGAllowingEscape(FName)) 256 return; 257 } 258 259 for (unsigned Idx = 0, E = Call.getNumArgs(); Idx != E; ++Idx) { 260 if (FDecl && Idx < FDecl->getNumParams()) { 261 if (isPointerToConst(FDecl->getParamDecl(Idx))) 262 PreserveArgs.insert(Idx); 263 } 264 } 265 return; 266 } 267 268 if (const ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(CallDecl)) { 269 assert(MDecl->param_size() <= Call.getNumArgs()); 270 unsigned Idx = 0; 271 for (clang::ObjCMethodDecl::param_const_iterator 272 I = MDecl->param_begin(), E = MDecl->param_end(); I != E; ++I, ++Idx) { 273 if (isPointerToConst(*I)) 274 PreserveArgs.insert(Idx); 275 } 276 return; 277 } 278} 279 280ProgramStateRef 281ExprEngine::invalidateArguments(ProgramStateRef State, 282 const CallOrObjCMessage &Call, 283 const LocationContext *LC) { 284 SmallVector<const MemRegion *, 8> RegionsToInvalidate; 285 286 if (Call.isObjCMessage()) { 287 // Invalidate all instance variables of the receiver of an ObjC message. 288 // FIXME: We should be able to do better with inter-procedural analysis. 289 if (const MemRegion *MR = Call.getInstanceMessageReceiver(LC).getAsRegion()) 290 RegionsToInvalidate.push_back(MR); 291 292 } else if (Call.isCXXCall()) { 293 // Invalidate all instance variables for the callee of a C++ method call. 294 // FIXME: We should be able to do better with inter-procedural analysis. 295 // FIXME: We can probably do better for const versus non-const methods. 296 if (const MemRegion *Callee = Call.getCXXCallee().getAsRegion()) 297 RegionsToInvalidate.push_back(Callee); 298 299 } else if (Call.isFunctionCall()) { 300 // Block calls invalidate all captured-by-reference values. 301 SVal CalleeVal = Call.getFunctionCallee(); 302 if (const MemRegion *Callee = CalleeVal.getAsRegion()) { 303 if (isa<BlockDataRegion>(Callee)) 304 RegionsToInvalidate.push_back(Callee); 305 } 306 } 307 308 // Indexes of arguments whose values will be preserved by the call. 309 llvm::SmallSet<unsigned, 1> PreserveArgs; 310 findPtrToConstParams(PreserveArgs, Call); 311 312 for (unsigned idx = 0, e = Call.getNumArgs(); idx != e; ++idx) { 313 if (PreserveArgs.count(idx)) 314 continue; 315 316 SVal V = Call.getArgSVal(idx); 317 318 // If we are passing a location wrapped as an integer, unwrap it and 319 // invalidate the values referred by the location. 320 if (nonloc::LocAsInteger *Wrapped = dyn_cast<nonloc::LocAsInteger>(&V)) 321 V = Wrapped->getLoc(); 322 else if (!isa<Loc>(V)) 323 continue; 324 325 if (const MemRegion *R = V.getAsRegion()) { 326 // Invalidate the value of the variable passed by reference. 327 328 // Are we dealing with an ElementRegion? If the element type is 329 // a basic integer type (e.g., char, int) and the underlying region 330 // is a variable region then strip off the ElementRegion. 331 // FIXME: We really need to think about this for the general case 332 // as sometimes we are reasoning about arrays and other times 333 // about (char*), etc., is just a form of passing raw bytes. 334 // e.g., void *p = alloca(); foo((char*)p); 335 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { 336 // Checking for 'integral type' is probably too promiscuous, but 337 // we'll leave it in for now until we have a systematic way of 338 // handling all of these cases. Eventually we need to come up 339 // with an interface to StoreManager so that this logic can be 340 // appropriately delegated to the respective StoreManagers while 341 // still allowing us to do checker-specific logic (e.g., 342 // invalidating reference counts), probably via callbacks. 343 if (ER->getElementType()->isIntegralOrEnumerationType()) { 344 const MemRegion *superReg = ER->getSuperRegion(); 345 if (isa<VarRegion>(superReg) || isa<FieldRegion>(superReg) || 346 isa<ObjCIvarRegion>(superReg)) 347 R = cast<TypedRegion>(superReg); 348 } 349 // FIXME: What about layers of ElementRegions? 350 } 351 352 // Mark this region for invalidation. We batch invalidate regions 353 // below for efficiency. 354 RegionsToInvalidate.push_back(R); 355 } else { 356 // Nuke all other arguments passed by reference. 357 // FIXME: is this necessary or correct? This handles the non-Region 358 // cases. Is it ever valid to store to these? 359 State = State->unbindLoc(cast<Loc>(V)); 360 } 361 } 362 363 // Invalidate designated regions using the batch invalidation API. 364 365 // FIXME: We can have collisions on the conjured symbol if the 366 // expression *I also creates conjured symbols. We probably want 367 // to identify conjured symbols by an expression pair: the enclosing 368 // expression (the context) and the expression itself. This should 369 // disambiguate conjured symbols. 370 unsigned Count = currentBuilderContext->getCurrentBlockCount(); 371 StoreManager::InvalidatedSymbols IS; 372 373 // NOTE: Even if RegionsToInvalidate is empty, we may still invalidate 374 // global variables. 375 return State->invalidateRegions(RegionsToInvalidate, 376 Call.getOriginExpr(), Count, LC, 377 &IS, &Call); 378 379} 380 381static ProgramStateRef getReplayWithoutInliningState(ExplodedNode *&N, 382 const CallExpr *CE) { 383 void *ReplayState = N->getState()->get<ReplayWithoutInlining>(); 384 if (!ReplayState) 385 return 0; 386 const CallExpr *ReplayCE = reinterpret_cast<const CallExpr*>(ReplayState); 387 if (CE == ReplayCE) { 388 return N->getState()->remove<ReplayWithoutInlining>(); 389 } 390 return 0; 391} 392 393void ExprEngine::VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred, 394 ExplodedNodeSet &dst) { 395 // Perform the previsit of the CallExpr. 396 ExplodedNodeSet dstPreVisit; 397 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, CE, *this); 398 399 // Now evaluate the call itself. 400 class DefaultEval : public GraphExpander { 401 ExprEngine &Eng; 402 const CallExpr *CE; 403 public: 404 405 DefaultEval(ExprEngine &eng, const CallExpr *ce) 406 : Eng(eng), CE(ce) {} 407 virtual void expandGraph(ExplodedNodeSet &Dst, ExplodedNode *Pred) { 408 409 ProgramStateRef state = getReplayWithoutInliningState(Pred, CE); 410 411 // First, try to inline the call. 412 if (state == 0 && Eng.InlineCall(Dst, CE, Pred)) 413 return; 414 415 // First handle the return value. 416 StmtNodeBuilder Bldr(Pred, Dst, *Eng.currentBuilderContext); 417 418 // Get the callee. 419 const Expr *Callee = CE->getCallee()->IgnoreParens(); 420 if (state == 0) 421 state = Pred->getState(); 422 SVal L = state->getSVal(Callee, Pred->getLocationContext()); 423 424 // Figure out the result type. We do this dance to handle references. 425 QualType ResultTy; 426 if (const FunctionDecl *FD = L.getAsFunctionDecl()) 427 ResultTy = FD->getResultType(); 428 else 429 ResultTy = CE->getType(); 430 431 if (CE->isLValue()) 432 ResultTy = Eng.getContext().getPointerType(ResultTy); 433 434 // Conjure a symbol value to use as the result. 435 SValBuilder &SVB = Eng.getSValBuilder(); 436 unsigned Count = Eng.currentBuilderContext->getCurrentBlockCount(); 437 const LocationContext *LCtx = Pred->getLocationContext(); 438 SVal RetVal = SVB.getConjuredSymbolVal(0, CE, LCtx, ResultTy, Count); 439 440 // Generate a new state with the return value set. 441 state = state->BindExpr(CE, LCtx, RetVal); 442 443 // Invalidate the arguments. 444 state = Eng.invalidateArguments(state, CallOrObjCMessage(CE, state, LCtx), 445 LCtx); 446 447 // And make the result node. 448 Bldr.generateNode(CE, Pred, state); 449 } 450 }; 451 452 // Finally, evaluate the function call. We try each of the checkers 453 // to see if the can evaluate the function call. 454 ExplodedNodeSet dstCallEvaluated; 455 DefaultEval defEval(*this, CE); 456 getCheckerManager().runCheckersForEvalCall(dstCallEvaluated, 457 dstPreVisit, 458 CE, *this, &defEval); 459 460 // Finally, perform the post-condition check of the CallExpr and store 461 // the created nodes in 'Dst'. 462 getCheckerManager().runCheckersForPostStmt(dst, dstCallEvaluated, CE, 463 *this); 464} 465 466void ExprEngine::VisitReturnStmt(const ReturnStmt *RS, ExplodedNode *Pred, 467 ExplodedNodeSet &Dst) { 468 469 ExplodedNodeSet dstPreVisit; 470 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, RS, *this); 471 472 StmtNodeBuilder B(dstPreVisit, Dst, *currentBuilderContext); 473 474 if (RS->getRetValue()) { 475 for (ExplodedNodeSet::iterator it = dstPreVisit.begin(), 476 ei = dstPreVisit.end(); it != ei; ++it) { 477 B.generateNode(RS, *it, (*it)->getState()); 478 } 479 } 480} 481