ExprEngineCallAndReturn.cpp revision e13056a8bb532ddfdc07952a13169aa422bacd3b
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/Analysis/Analyses/LiveVariables.h" 15#include "clang/StaticAnalyzer/Core/CheckerManager.h" 16#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 17#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 18#include "clang/AST/DeclCXX.h" 19#include "llvm/ADT/SmallSet.h" 20#include "llvm/Support/SaveAndRestore.h" 21 22using namespace clang; 23using namespace ento; 24 25void ExprEngine::processCallEnter(CallEnter CE, ExplodedNode *Pred) { 26 // Get the entry block in the CFG of the callee. 27 const StackFrameContext *calleeCtx = CE.getCalleeContext(); 28 const CFG *CalleeCFG = calleeCtx->getCFG(); 29 const CFGBlock *Entry = &(CalleeCFG->getEntry()); 30 31 // Validate the CFG. 32 assert(Entry->empty()); 33 assert(Entry->succ_size() == 1); 34 35 // Get the solitary sucessor. 36 const CFGBlock *Succ = *(Entry->succ_begin()); 37 38 // Construct an edge representing the starting location in the callee. 39 BlockEdge Loc(Entry, Succ, calleeCtx); 40 41 ProgramStateRef state = Pred->getState(); 42 43 // Construct a new node and add it to the worklist. 44 bool isNew; 45 ExplodedNode *Node = G.getNode(Loc, state, false, &isNew); 46 Node->addPredecessor(Pred, G); 47 if (isNew) 48 Engine.getWorkList()->enqueue(Node); 49} 50 51// Find the last statement on the path to the exploded node and the 52// corresponding Block. 53static std::pair<const Stmt*, 54 const CFGBlock*> getLastStmt(const ExplodedNode *Node) { 55 const Stmt *S = 0; 56 const StackFrameContext *SF = 57 Node->getLocation().getLocationContext()->getCurrentStackFrame(); 58 59 // Back up through the ExplodedGraph until we reach a statement node. 60 while (Node) { 61 const ProgramPoint &PP = Node->getLocation(); 62 63 if (const StmtPoint *SP = dyn_cast<StmtPoint>(&PP)) { 64 S = SP->getStmt(); 65 break; 66 } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&PP)) { 67 S = CEE->getCalleeContext()->getCallSite(); 68 if (S) 69 break; 70 // If we have an implicit call, we'll probably end up with a 71 // StmtPoint inside the callee, which is acceptable. 72 // (It's possible a function ONLY contains implicit calls -- such as an 73 // implicitly-generated destructor -- so we shouldn't just skip back to 74 // the CallEnter node and keep going.) 75 } else if (const CallEnter *CE = dyn_cast<CallEnter>(&PP)) { 76 // If we reached the CallEnter for this function, it has no statements. 77 if (CE->getCalleeContext() == SF) 78 break; 79 } 80 81 Node = *Node->pred_begin(); 82 } 83 84 const CFGBlock *Blk = 0; 85 if (S) { 86 // Now, get the enclosing basic block. 87 while (Node && Node->pred_size() >=1 ) { 88 const ProgramPoint &PP = Node->getLocation(); 89 if (isa<BlockEdge>(PP) && 90 (PP.getLocationContext()->getCurrentStackFrame() == SF)) { 91 BlockEdge &EPP = cast<BlockEdge>(PP); 92 Blk = EPP.getDst(); 93 break; 94 } 95 Node = *Node->pred_begin(); 96 } 97 } 98 99 return std::pair<const Stmt*, const CFGBlock*>(S, Blk); 100} 101 102/// The call exit is simulated with a sequence of nodes, which occur between 103/// CallExitBegin and CallExitEnd. The following operations occur between the 104/// two program points: 105/// 1. CallExitBegin (triggers the start of call exit sequence) 106/// 2. Bind the return value 107/// 3. Run Remove dead bindings to clean up the dead symbols from the callee. 108/// 4. CallExitEnd (switch to the caller context) 109/// 5. PostStmt<CallExpr> 110void ExprEngine::processCallExit(ExplodedNode *CEBNode) { 111 // Step 1 CEBNode was generated before the call. 112 113 const StackFrameContext *calleeCtx = 114 CEBNode->getLocationContext()->getCurrentStackFrame(); 115 116 // The parent context might not be a stack frame, so make sure we 117 // look up the first enclosing stack frame. 118 const StackFrameContext *callerCtx = 119 calleeCtx->getParent()->getCurrentStackFrame(); 120 121 const Stmt *CE = calleeCtx->getCallSite(); 122 ProgramStateRef state = CEBNode->getState(); 123 // Find the last statement in the function and the corresponding basic block. 124 const Stmt *LastSt = 0; 125 const CFGBlock *Blk = 0; 126 llvm::tie(LastSt, Blk) = getLastStmt(CEBNode); 127 128 // Step 2: generate node with bound return value: CEBNode -> BindedRetNode. 129 130 // If the callee returns an expression, bind its value to CallExpr. 131 if (CE) { 132 if (const ReturnStmt *RS = dyn_cast_or_null<ReturnStmt>(LastSt)) { 133 const LocationContext *LCtx = CEBNode->getLocationContext(); 134 SVal V = state->getSVal(RS, LCtx); 135 state = state->BindExpr(CE, calleeCtx->getParent(), V); 136 } 137 138 // Bind the constructed object value to CXXConstructExpr. 139 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(CE)) { 140 loc::MemRegionVal This = 141 svalBuilder.getCXXThis(CCE->getConstructor()->getParent(), calleeCtx); 142 SVal ThisV = state->getSVal(This); 143 144 // Always bind the region to the CXXConstructExpr. 145 state = state->BindExpr(CCE, calleeCtx->getParent(), ThisV); 146 } 147 } 148 149 // Step 3: BindedRetNode -> CleanedNodes 150 // If we can find a statement and a block in the inlined function, run remove 151 // dead bindings before returning from the call. This is important to ensure 152 // that we report the issues such as leaks in the stack contexts in which 153 // they occurred. 154 ExplodedNodeSet CleanedNodes; 155 if (LastSt && Blk) { 156 static SimpleProgramPointTag retValBind("ExprEngine : Bind Return Value"); 157 PostStmt Loc(LastSt, calleeCtx, &retValBind); 158 bool isNew; 159 ExplodedNode *BindedRetNode = G.getNode(Loc, state, false, &isNew); 160 BindedRetNode->addPredecessor(CEBNode, G); 161 if (!isNew) 162 return; 163 164 NodeBuilderContext Ctx(getCoreEngine(), Blk, BindedRetNode); 165 currentBuilderContext = &Ctx; 166 // Here, we call the Symbol Reaper with 0 statement and caller location 167 // context, telling it to clean up everything in the callee's context 168 // (and it's children). We use LastStmt as a diagnostic statement, which 169 // which the PreStmtPurge Dead point will be associated. 170 removeDead(BindedRetNode, CleanedNodes, 0, callerCtx, LastSt, 171 ProgramPoint::PostStmtPurgeDeadSymbolsKind); 172 currentBuilderContext = 0; 173 } else { 174 CleanedNodes.Add(CEBNode); 175 } 176 177 for (ExplodedNodeSet::iterator I = CleanedNodes.begin(), 178 E = CleanedNodes.end(); I != E; ++I) { 179 180 // Step 4: Generate the CallExit and leave the callee's context. 181 // CleanedNodes -> CEENode 182 CallExitEnd Loc(calleeCtx, callerCtx); 183 bool isNew; 184 ProgramStateRef CEEState = (*I == CEBNode) ? state : (*I)->getState(); 185 ExplodedNode *CEENode = G.getNode(Loc, CEEState, false, &isNew); 186 CEENode->addPredecessor(*I, G); 187 if (!isNew) 188 return; 189 190 // Step 5: Perform the post-condition check of the CallExpr and enqueue the 191 // result onto the work list. 192 // CEENode -> Dst -> WorkList 193 ExplodedNodeSet Dst; 194 NodeBuilderContext Ctx(Engine, calleeCtx->getCallSiteBlock(), CEENode); 195 SaveAndRestore<const NodeBuilderContext*> NBCSave(currentBuilderContext, 196 &Ctx); 197 SaveAndRestore<unsigned> CBISave(currentStmtIdx, calleeCtx->getIndex()); 198 199 // FIXME: This needs to call PostCall. 200 // FIXME: If/when we inline Objective-C messages, this also needs to call 201 // PostObjCMessage. 202 if (CE) 203 getCheckerManager().runCheckersForPostStmt(Dst, CEENode, CE, *this, true); 204 else 205 Dst.Add(CEENode); 206 207 // Enqueue the next element in the block. 208 for (ExplodedNodeSet::iterator PSI = Dst.begin(), PSE = Dst.end(); 209 PSI != PSE; ++PSI) { 210 Engine.getWorkList()->enqueue(*PSI, calleeCtx->getCallSiteBlock(), 211 calleeCtx->getIndex()+1); 212 } 213 } 214} 215 216static unsigned getNumberStackFrames(const LocationContext *LCtx) { 217 unsigned count = 0; 218 while (LCtx) { 219 if (isa<StackFrameContext>(LCtx)) 220 ++count; 221 LCtx = LCtx->getParent(); 222 } 223 return count; 224} 225 226// Determine if we should inline the call. 227bool ExprEngine::shouldInlineDecl(const Decl *D, ExplodedNode *Pred) { 228 AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D); 229 const CFG *CalleeCFG = CalleeADC->getCFG(); 230 231 // It is possible that the CFG cannot be constructed. 232 // Be safe, and check if the CalleeCFG is valid. 233 if (!CalleeCFG) 234 return false; 235 236 if (getNumberStackFrames(Pred->getLocationContext()) 237 == AMgr.InlineMaxStackDepth) 238 return false; 239 240 if (Engine.FunctionSummaries->hasReachedMaxBlockCount(D)) 241 return false; 242 243 if (CalleeCFG->getNumBlockIDs() > AMgr.InlineMaxFunctionSize) 244 return false; 245 246 // Do not inline variadic calls (for now). 247 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { 248 if (BD->isVariadic()) 249 return false; 250 } 251 else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 252 if (FD->isVariadic()) 253 return false; 254 } 255 256 // It is possible that the live variables analysis cannot be 257 // run. If so, bail out. 258 if (!CalleeADC->getAnalysis<RelaxedLiveVariables>()) 259 return false; 260 261 return true; 262} 263 264bool ExprEngine::inlineCall(const CallEvent &Call, 265 ExplodedNode *Pred) { 266 if (!getAnalysisManager().shouldInlineCall()) 267 return false; 268 269 const Decl *D = Call.getRuntimeDefinition(); 270 if (!D) 271 return false; 272 273 const LocationContext *CurLC = Pred->getLocationContext(); 274 const StackFrameContext *CallerSFC = CurLC->getCurrentStackFrame(); 275 const LocationContext *ParentOfCallee = 0; 276 277 switch (Call.getKind()) { 278 case CE_Function: 279 case CE_CXXMember: 280 case CE_CXXMemberOperator: 281 // These are always at least possible to inline. 282 break; 283 case CE_CXXConstructor: 284 case CE_CXXDestructor: { 285 // Only inline constructors and destructors if we built the CFGs for them 286 // properly. 287 const AnalysisDeclContext *ADC = CallerSFC->getAnalysisDeclContext(); 288 if (!ADC->getCFGBuildOptions().AddImplicitDtors || 289 !ADC->getCFGBuildOptions().AddInitializers) 290 return false; 291 292 // FIXME: We don't handle constructors or destructors for arrays properly. 293 const MemRegion *Target = Call.getCXXThisVal().getAsRegion(); 294 if (Target && isa<ElementRegion>(Target)) 295 return false; 296 297 // FIXME: This is a hack. We don't handle temporary destructors 298 // right now, so we shouldn't inline their constructors. 299 if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) { 300 const CXXConstructExpr *CtorExpr = Ctor->getOriginExpr(); 301 if (CtorExpr->getConstructionKind() == CXXConstructExpr::CK_Complete) 302 if (!Target || !isa<DeclRegion>(Target)) 303 return false; 304 } 305 break; 306 } 307 case CE_CXXAllocator: 308 // Do not inline allocators until we model deallocators. 309 // This is unfortunate, but basically necessary for smart pointers and such. 310 return false; 311 case CE_Block: { 312 const BlockDataRegion *BR = cast<BlockCall>(Call).getBlockRegion(); 313 assert(BR && "If we have the block definition we should have its region"); 314 AnalysisDeclContext *BlockCtx = AMgr.getAnalysisDeclContext(D); 315 ParentOfCallee = BlockCtx->getBlockInvocationContext(CallerSFC, 316 cast<BlockDecl>(D), 317 BR); 318 break; 319 } 320 case CE_ObjCMessage: 321 if (getAnalysisManager().IPAMode != DynamicDispatch) 322 return false; 323 break; 324 } 325 326 if (!shouldInlineDecl(D, Pred)) 327 return false; 328 329 if (!ParentOfCallee) 330 ParentOfCallee = CallerSFC; 331 332 // This may be NULL, but that's fine. 333 const Expr *CallE = Call.getOriginExpr(); 334 335 // Construct a new stack frame for the callee. 336 AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D); 337 const StackFrameContext *CalleeSFC = 338 CalleeADC->getStackFrame(ParentOfCallee, CallE, 339 currentBuilderContext->getBlock(), 340 currentStmtIdx); 341 342 CallEnter Loc(CallE, CalleeSFC, CurLC); 343 344 // Construct a new state which contains the mapping from actual to 345 // formal arguments. 346 ProgramStateRef State = Pred->getState()->enterStackFrame(Call, CalleeSFC); 347 348 bool isNew; 349 if (ExplodedNode *N = G.getNode(Loc, State, false, &isNew)) { 350 N->addPredecessor(Pred, G); 351 if (isNew) 352 Engine.getWorkList()->enqueue(N); 353 } 354 return true; 355} 356 357static ProgramStateRef getInlineFailedState(ProgramStateRef State, 358 const Stmt *CallE) { 359 void *ReplayState = State->get<ReplayWithoutInlining>(); 360 if (!ReplayState) 361 return 0; 362 363 assert(ReplayState == (const void*)CallE && "Backtracked to the wrong call."); 364 (void)CallE; 365 366 return State->remove<ReplayWithoutInlining>(); 367} 368 369void ExprEngine::VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred, 370 ExplodedNodeSet &dst) { 371 // Perform the previsit of the CallExpr. 372 ExplodedNodeSet dstPreVisit; 373 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, CE, *this); 374 375 // Get the call in its initial state. We use this as a template to perform 376 // all the checks. 377 CallEventManager &CEMgr = getStateManager().getCallEventManager(); 378 CallEventRef<SimpleCall> CallTemplate 379 = CEMgr.getSimpleCall(CE, Pred->getState(), Pred->getLocationContext()); 380 381 // Evaluate the function call. We try each of the checkers 382 // to see if the can evaluate the function call. 383 ExplodedNodeSet dstCallEvaluated; 384 for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end(); 385 I != E; ++I) { 386 evalCall(dstCallEvaluated, *I, *CallTemplate); 387 } 388 389 // Finally, perform the post-condition check of the CallExpr and store 390 // the created nodes in 'Dst'. 391 // Note that if the call was inlined, dstCallEvaluated will be empty. 392 // The post-CallExpr check will occur in processCallExit. 393 getCheckerManager().runCheckersForPostStmt(dst, dstCallEvaluated, CE, 394 *this); 395} 396 397void ExprEngine::evalCall(ExplodedNodeSet &Dst, ExplodedNode *Pred, 398 const SimpleCall &Call) { 399 // WARNING: At this time, the state attached to 'Call' may be older than the 400 // state in 'Pred'. This is a minor optimization since CheckerManager will 401 // use an updated CallEvent instance when calling checkers, but if 'Call' is 402 // ever used directly in this function all callers should be updated to pass 403 // the most recent state. (It is probably not worth doing the work here since 404 // for some callers this will not be necessary.) 405 406 // Run any pre-call checks using the generic call interface. 407 ExplodedNodeSet dstPreVisit; 408 getCheckerManager().runCheckersForPreCall(dstPreVisit, Pred, Call, *this); 409 410 // Actually evaluate the function call. We try each of the checkers 411 // to see if the can evaluate the function call, and get a callback at 412 // defaultEvalCall if all of them fail. 413 ExplodedNodeSet dstCallEvaluated; 414 getCheckerManager().runCheckersForEvalCall(dstCallEvaluated, dstPreVisit, 415 Call, *this); 416 417 // Finally, run any post-call checks. 418 getCheckerManager().runCheckersForPostCall(Dst, dstCallEvaluated, 419 Call, *this); 420} 421 422ProgramStateRef ExprEngine::bindReturnValue(const CallEvent &Call, 423 const LocationContext *LCtx, 424 ProgramStateRef State) { 425 const Expr *E = Call.getOriginExpr(); 426 if (!E) 427 return State; 428 429 // Some method families have known return values. 430 if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(&Call)) { 431 switch (Msg->getMethodFamily()) { 432 default: 433 break; 434 case OMF_autorelease: 435 case OMF_retain: 436 case OMF_self: { 437 // These methods return their receivers. 438 return State->BindExpr(E, LCtx, Msg->getReceiverSVal()); 439 } 440 } 441 } else if (const CXXConstructorCall *C = dyn_cast<CXXConstructorCall>(&Call)){ 442 return State->BindExpr(E, LCtx, C->getCXXThisVal()); 443 } 444 445 // Conjure a symbol if the return value is unknown. 446 QualType ResultTy = Call.getResultType(); 447 SValBuilder &SVB = getSValBuilder(); 448 unsigned Count = currentBuilderContext->getCurrentBlockCount(); 449 SVal R = SVB.getConjuredSymbolVal(0, E, LCtx, ResultTy, Count); 450 return State->BindExpr(E, LCtx, R); 451} 452 453void ExprEngine::defaultEvalCall(NodeBuilder &Bldr, ExplodedNode *Pred, 454 const CallEvent &CallTemplate) { 455 // Make sure we have the most recent state attached to the call. 456 ProgramStateRef State = Pred->getState(); 457 CallEventRef<> Call = CallTemplate.cloneWithState(State); 458 459 // Try to inline the call. 460 // The origin expression here is just used as a kind of checksum; 461 // this should still be safe even for CallEvents that don't come from exprs. 462 const Expr *E = Call->getOriginExpr(); 463 ProgramStateRef InlinedFailedState = getInlineFailedState(State, E); 464 465 if (InlinedFailedState) { 466 // If we already tried once and failed, make sure we don't retry later. 467 State = InlinedFailedState; 468 } else if (inlineCall(*Call, Pred)) { 469 // If we decided to inline the call, the successor has been manually 470 // added onto the work list and we should not perform our generic 471 // call-handling steps. 472 Bldr.takeNodes(Pred); 473 return; 474 } 475 476 // If we can't inline it, handle the return value and invalidate the regions. 477 unsigned Count = currentBuilderContext->getCurrentBlockCount(); 478 State = Call->invalidateRegions(Count, State); 479 State = bindReturnValue(*Call, Pred->getLocationContext(), State); 480 481 // And make the result node. 482 Bldr.generateNode(Call->getProgramPoint(), State, Pred); 483} 484 485void ExprEngine::VisitReturnStmt(const ReturnStmt *RS, ExplodedNode *Pred, 486 ExplodedNodeSet &Dst) { 487 488 ExplodedNodeSet dstPreVisit; 489 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, RS, *this); 490 491 StmtNodeBuilder B(dstPreVisit, Dst, *currentBuilderContext); 492 493 if (RS->getRetValue()) { 494 for (ExplodedNodeSet::iterator it = dstPreVisit.begin(), 495 ei = dstPreVisit.end(); it != ei; ++it) { 496 B.generateNode(RS, *it, (*it)->getState()); 497 } 498 } 499} 500