ExprEngineCallAndReturn.cpp revision 3b1df8bb941a18c4a7256d7cfcbccb9de7e39995
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#define DEBUG_TYPE "ExprEngine" 15 16#include "clang/Analysis/Analyses/LiveVariables.h" 17#include "clang/StaticAnalyzer/Core/CheckerManager.h" 18#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 19#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 20#include "clang/AST/DeclCXX.h" 21#include "llvm/ADT/SmallSet.h" 22#include "llvm/ADT/Statistic.h" 23#include "llvm/Support/SaveAndRestore.h" 24 25using namespace clang; 26using namespace ento; 27 28STATISTIC(NumOfDynamicDispatchPathSplits, 29 "The # of times we split the path due to imprecise dynamic dispatch info"); 30 31void ExprEngine::processCallEnter(CallEnter CE, ExplodedNode *Pred) { 32 // Get the entry block in the CFG of the callee. 33 const StackFrameContext *calleeCtx = CE.getCalleeContext(); 34 const CFG *CalleeCFG = calleeCtx->getCFG(); 35 const CFGBlock *Entry = &(CalleeCFG->getEntry()); 36 37 // Validate the CFG. 38 assert(Entry->empty()); 39 assert(Entry->succ_size() == 1); 40 41 // Get the solitary sucessor. 42 const CFGBlock *Succ = *(Entry->succ_begin()); 43 44 // Construct an edge representing the starting location in the callee. 45 BlockEdge Loc(Entry, Succ, calleeCtx); 46 47 ProgramStateRef state = Pred->getState(); 48 49 // Construct a new node and add it to the worklist. 50 bool isNew; 51 ExplodedNode *Node = G.getNode(Loc, state, false, &isNew); 52 Node->addPredecessor(Pred, G); 53 if (isNew) 54 Engine.getWorkList()->enqueue(Node); 55} 56 57// Find the last statement on the path to the exploded node and the 58// corresponding Block. 59static std::pair<const Stmt*, 60 const CFGBlock*> getLastStmt(const ExplodedNode *Node) { 61 const Stmt *S = 0; 62 const StackFrameContext *SF = 63 Node->getLocation().getLocationContext()->getCurrentStackFrame(); 64 65 // Back up through the ExplodedGraph until we reach a statement node. 66 while (Node) { 67 const ProgramPoint &PP = Node->getLocation(); 68 69 if (const StmtPoint *SP = dyn_cast<StmtPoint>(&PP)) { 70 S = SP->getStmt(); 71 break; 72 } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&PP)) { 73 S = CEE->getCalleeContext()->getCallSite(); 74 if (S) 75 break; 76 // If we have an implicit call, we'll probably end up with a 77 // StmtPoint inside the callee, which is acceptable. 78 // (It's possible a function ONLY contains implicit calls -- such as an 79 // implicitly-generated destructor -- so we shouldn't just skip back to 80 // the CallEnter node and keep going.) 81 } else if (const CallEnter *CE = dyn_cast<CallEnter>(&PP)) { 82 // If we reached the CallEnter for this function, it has no statements. 83 if (CE->getCalleeContext() == SF) 84 break; 85 } 86 87 Node = *Node->pred_begin(); 88 } 89 90 const CFGBlock *Blk = 0; 91 if (S) { 92 // Now, get the enclosing basic block. 93 while (Node && Node->pred_size() >=1 ) { 94 const ProgramPoint &PP = Node->getLocation(); 95 if (isa<BlockEdge>(PP) && 96 (PP.getLocationContext()->getCurrentStackFrame() == SF)) { 97 BlockEdge &EPP = cast<BlockEdge>(PP); 98 Blk = EPP.getDst(); 99 break; 100 } 101 Node = *Node->pred_begin(); 102 } 103 } 104 105 return std::pair<const Stmt*, const CFGBlock*>(S, Blk); 106} 107 108/// The call exit is simulated with a sequence of nodes, which occur between 109/// CallExitBegin and CallExitEnd. The following operations occur between the 110/// two program points: 111/// 1. CallExitBegin (triggers the start of call exit sequence) 112/// 2. Bind the return value 113/// 3. Run Remove dead bindings to clean up the dead symbols from the callee. 114/// 4. CallExitEnd (switch to the caller context) 115/// 5. PostStmt<CallExpr> 116void ExprEngine::processCallExit(ExplodedNode *CEBNode) { 117 // Step 1 CEBNode was generated before the call. 118 119 const StackFrameContext *calleeCtx = 120 CEBNode->getLocationContext()->getCurrentStackFrame(); 121 122 // The parent context might not be a stack frame, so make sure we 123 // look up the first enclosing stack frame. 124 const StackFrameContext *callerCtx = 125 calleeCtx->getParent()->getCurrentStackFrame(); 126 127 const Stmt *CE = calleeCtx->getCallSite(); 128 ProgramStateRef state = CEBNode->getState(); 129 // Find the last statement in the function and the corresponding basic block. 130 const Stmt *LastSt = 0; 131 const CFGBlock *Blk = 0; 132 llvm::tie(LastSt, Blk) = getLastStmt(CEBNode); 133 134 // Step 2: generate node with bound return value: CEBNode -> BindedRetNode. 135 136 // If the callee returns an expression, bind its value to CallExpr. 137 if (CE) { 138 if (const ReturnStmt *RS = dyn_cast_or_null<ReturnStmt>(LastSt)) { 139 const LocationContext *LCtx = CEBNode->getLocationContext(); 140 SVal V = state->getSVal(RS, LCtx); 141 state = state->BindExpr(CE, callerCtx, V); 142 } 143 144 // Bind the constructed object value to CXXConstructExpr. 145 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(CE)) { 146 loc::MemRegionVal This = 147 svalBuilder.getCXXThis(CCE->getConstructor()->getParent(), calleeCtx); 148 SVal ThisV = state->getSVal(This); 149 150 // Always bind the region to the CXXConstructExpr. 151 state = state->BindExpr(CCE, callerCtx, ThisV); 152 } 153 } 154 155 // Generate a CallEvent /before/ cleaning the state, so that we can get the 156 // correct value for 'this' (if necessary). 157 CallEventManager &CEMgr = getStateManager().getCallEventManager(); 158 CallEventRef<> Call = CEMgr.getCaller(calleeCtx, state); 159 160 // Step 3: BindedRetNode -> CleanedNodes 161 // If we can find a statement and a block in the inlined function, run remove 162 // dead bindings before returning from the call. This is important to ensure 163 // that we report the issues such as leaks in the stack contexts in which 164 // they occurred. 165 ExplodedNodeSet CleanedNodes; 166 if (LastSt && Blk) { 167 static SimpleProgramPointTag retValBind("ExprEngine : Bind Return Value"); 168 PostStmt Loc(LastSt, calleeCtx, &retValBind); 169 bool isNew; 170 ExplodedNode *BindedRetNode = G.getNode(Loc, state, false, &isNew); 171 BindedRetNode->addPredecessor(CEBNode, G); 172 if (!isNew) 173 return; 174 175 NodeBuilderContext Ctx(getCoreEngine(), Blk, BindedRetNode); 176 currentBuilderContext = &Ctx; 177 // Here, we call the Symbol Reaper with 0 statement and caller location 178 // context, telling it to clean up everything in the callee's context 179 // (and it's children). We use LastStmt as a diagnostic statement, which 180 // which the PreStmtPurge Dead point will be associated. 181 removeDead(BindedRetNode, CleanedNodes, 0, callerCtx, LastSt, 182 ProgramPoint::PostStmtPurgeDeadSymbolsKind); 183 currentBuilderContext = 0; 184 } else { 185 CleanedNodes.Add(CEBNode); 186 } 187 188 for (ExplodedNodeSet::iterator I = CleanedNodes.begin(), 189 E = CleanedNodes.end(); I != E; ++I) { 190 191 // Step 4: Generate the CallExit and leave the callee's context. 192 // CleanedNodes -> CEENode 193 CallExitEnd Loc(calleeCtx, callerCtx); 194 bool isNew; 195 ProgramStateRef CEEState = (*I == CEBNode) ? state : (*I)->getState(); 196 ExplodedNode *CEENode = G.getNode(Loc, CEEState, false, &isNew); 197 CEENode->addPredecessor(*I, G); 198 if (!isNew) 199 return; 200 201 // Step 5: Perform the post-condition check of the CallExpr and enqueue the 202 // result onto the work list. 203 // CEENode -> Dst -> WorkList 204 NodeBuilderContext Ctx(Engine, calleeCtx->getCallSiteBlock(), CEENode); 205 SaveAndRestore<const NodeBuilderContext*> NBCSave(currentBuilderContext, 206 &Ctx); 207 SaveAndRestore<unsigned> CBISave(currentStmtIdx, calleeCtx->getIndex()); 208 209 CallEventRef<> UpdatedCall = Call.cloneWithState(CEEState); 210 211 ExplodedNodeSet DstPostCall; 212 getCheckerManager().runCheckersForPostCall(DstPostCall, CEENode, 213 *UpdatedCall, *this, 214 /*WasInlined=*/true); 215 216 ExplodedNodeSet Dst; 217 if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(Call)) { 218 getCheckerManager().runCheckersForPostObjCMessage(Dst, DstPostCall, *Msg, 219 *this, 220 /*WasInlined=*/true); 221 } else if (CE) { 222 getCheckerManager().runCheckersForPostStmt(Dst, DstPostCall, CE, 223 *this, /*WasInlined=*/true); 224 } else { 225 Dst.insert(DstPostCall); 226 } 227 228 // Enqueue the next element in the block. 229 for (ExplodedNodeSet::iterator PSI = Dst.begin(), PSE = Dst.end(); 230 PSI != PSE; ++PSI) { 231 Engine.getWorkList()->enqueue(*PSI, calleeCtx->getCallSiteBlock(), 232 calleeCtx->getIndex()+1); 233 } 234 } 235} 236 237static unsigned getNumberStackFrames(const LocationContext *LCtx) { 238 unsigned count = 0; 239 while (LCtx) { 240 if (isa<StackFrameContext>(LCtx)) 241 ++count; 242 LCtx = LCtx->getParent(); 243 } 244 return count; 245} 246 247// Determine if we should inline the call. 248bool ExprEngine::shouldInlineDecl(const Decl *D, ExplodedNode *Pred) { 249 AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D); 250 const CFG *CalleeCFG = CalleeADC->getCFG(); 251 252 // It is possible that the CFG cannot be constructed. 253 // Be safe, and check if the CalleeCFG is valid. 254 if (!CalleeCFG) 255 return false; 256 257 if (getNumberStackFrames(Pred->getLocationContext()) 258 == AMgr.InlineMaxStackDepth) 259 return false; 260 261 if (Engine.FunctionSummaries->hasReachedMaxBlockCount(D)) 262 return false; 263 264 if (CalleeCFG->getNumBlockIDs() > AMgr.InlineMaxFunctionSize) 265 return false; 266 267 // Do not inline variadic calls (for now). 268 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { 269 if (BD->isVariadic()) 270 return false; 271 } 272 else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 273 if (FD->isVariadic()) 274 return false; 275 } 276 277 // It is possible that the live variables analysis cannot be 278 // run. If so, bail out. 279 if (!CalleeADC->getAnalysis<RelaxedLiveVariables>()) 280 return false; 281 282 return true; 283} 284 285/// The GDM component containing the dynamic dispatch bifurcation info. When 286/// the exact type of the receiver is not known, we want to explore both paths - 287/// one on which we do inline it and the other one on which we don't. This is 288/// done to ensure we do not drop coverage. 289/// This is the map from the receiver region to a bool, specifying either we 290/// consider this region's information precise or not along the given path. 291namespace clang { 292namespace ento { 293enum DynamicDispatchMode { DynamicDispatchModeInlined = 1, 294 DynamicDispatchModeConservative }; 295 296struct DynamicDispatchBifurcationMap {}; 297typedef llvm::ImmutableMap<const MemRegion*, 298 unsigned int> DynamicDispatchBifur; 299template<> struct ProgramStateTrait<DynamicDispatchBifurcationMap> 300 : public ProgramStatePartialTrait<DynamicDispatchBifur> { 301 static void *GDMIndex() { static int index; return &index; } 302}; 303 304}} 305 306static bool shouldInlineCXX(AnalysisManager &AMgr) { 307 switch (AMgr.IPAMode) { 308 case None: 309 case BasicInlining: 310 return false; 311 case Inlining: 312 case DynamicDispatch: 313 case DynamicDispatchBifurcate: 314 return true; 315 case NumIPAModes: 316 llvm_unreachable("not actually a valid option"); 317 } 318 llvm_unreachable("bogus IPAMode"); 319} 320 321bool ExprEngine::inlineCall(const CallEvent &Call, const Decl *D, 322 NodeBuilder &Bldr, ExplodedNode *Pred, 323 ProgramStateRef State) { 324 assert(D); 325 326 const LocationContext *CurLC = Pred->getLocationContext(); 327 const StackFrameContext *CallerSFC = CurLC->getCurrentStackFrame(); 328 const LocationContext *ParentOfCallee = 0; 329 330 // FIXME: Refactor this check into a hypothetical CallEvent::canInline. 331 switch (Call.getKind()) { 332 case CE_Function: 333 break; 334 case CE_CXXMember: 335 case CE_CXXMemberOperator: 336 if (!shouldInlineCXX(getAnalysisManager())) 337 return false; 338 break; 339 case CE_CXXConstructor: { 340 if (!shouldInlineCXX(getAnalysisManager())) 341 return false; 342 343 // Only inline constructors and destructors if we built the CFGs for them 344 // properly. 345 const AnalysisDeclContext *ADC = CallerSFC->getAnalysisDeclContext(); 346 if (!ADC->getCFGBuildOptions().AddImplicitDtors || 347 !ADC->getCFGBuildOptions().AddInitializers) 348 return false; 349 350 const CXXConstructorCall &Ctor = cast<CXXConstructorCall>(Call); 351 352 // FIXME: We don't handle constructors or destructors for arrays properly. 353 const MemRegion *Target = Ctor.getCXXThisVal().getAsRegion(); 354 if (Target && isa<ElementRegion>(Target)) 355 return false; 356 357 // FIXME: This is a hack. We don't handle temporary destructors 358 // right now, so we shouldn't inline their constructors. 359 const CXXConstructExpr *CtorExpr = Ctor.getOriginExpr(); 360 if (CtorExpr->getConstructionKind() == CXXConstructExpr::CK_Complete) 361 if (!Target || !isa<DeclRegion>(Target)) 362 return false; 363 364 break; 365 } 366 case CE_CXXDestructor: { 367 if (!shouldInlineCXX(getAnalysisManager())) 368 return false; 369 370 // Only inline constructors and destructors if we built the CFGs for them 371 // properly. 372 const AnalysisDeclContext *ADC = CallerSFC->getAnalysisDeclContext(); 373 if (!ADC->getCFGBuildOptions().AddImplicitDtors || 374 !ADC->getCFGBuildOptions().AddInitializers) 375 return false; 376 377 const CXXDestructorCall &Dtor = cast<CXXDestructorCall>(Call); 378 379 // FIXME: We don't handle constructors or destructors for arrays properly. 380 const MemRegion *Target = Dtor.getCXXThisVal().getAsRegion(); 381 if (Target && isa<ElementRegion>(Target)) 382 return false; 383 384 break; 385 } 386 case CE_CXXAllocator: 387 if (!shouldInlineCXX(getAnalysisManager())) 388 return false; 389 390 // Do not inline allocators until we model deallocators. 391 // This is unfortunate, but basically necessary for smart pointers and such. 392 return false; 393 case CE_Block: { 394 const BlockDataRegion *BR = cast<BlockCall>(Call).getBlockRegion(); 395 assert(BR && "If we have the block definition we should have its region"); 396 AnalysisDeclContext *BlockCtx = AMgr.getAnalysisDeclContext(D); 397 ParentOfCallee = BlockCtx->getBlockInvocationContext(CallerSFC, 398 cast<BlockDecl>(D), 399 BR); 400 break; 401 } 402 case CE_ObjCMessage: 403 if (!(getAnalysisManager().IPAMode == DynamicDispatch || 404 getAnalysisManager().IPAMode == DynamicDispatchBifurcate)) 405 return false; 406 break; 407 } 408 409 if (!shouldInlineDecl(D, Pred)) 410 return false; 411 412 if (!ParentOfCallee) 413 ParentOfCallee = CallerSFC; 414 415 // This may be NULL, but that's fine. 416 const Expr *CallE = Call.getOriginExpr(); 417 418 // Construct a new stack frame for the callee. 419 AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D); 420 const StackFrameContext *CalleeSFC = 421 CalleeADC->getStackFrame(ParentOfCallee, CallE, 422 currentBuilderContext->getBlock(), 423 currentStmtIdx); 424 425 CallEnter Loc(CallE, CalleeSFC, CurLC); 426 427 // Construct a new state which contains the mapping from actual to 428 // formal arguments. 429 State = State->enterStackFrame(Call, CalleeSFC); 430 431 bool isNew; 432 if (ExplodedNode *N = G.getNode(Loc, State, false, &isNew)) { 433 N->addPredecessor(Pred, G); 434 if (isNew) 435 Engine.getWorkList()->enqueue(N); 436 } 437 438 // If we decided to inline the call, the successor has been manually 439 // added onto the work list so remove it from the node builder. 440 Bldr.takeNodes(Pred); 441 442 return true; 443} 444 445static ProgramStateRef getInlineFailedState(ProgramStateRef State, 446 const Stmt *CallE) { 447 void *ReplayState = State->get<ReplayWithoutInlining>(); 448 if (!ReplayState) 449 return 0; 450 451 assert(ReplayState == (const void*)CallE && "Backtracked to the wrong call."); 452 (void)CallE; 453 454 return State->remove<ReplayWithoutInlining>(); 455} 456 457void ExprEngine::VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred, 458 ExplodedNodeSet &dst) { 459 // Perform the previsit of the CallExpr. 460 ExplodedNodeSet dstPreVisit; 461 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, CE, *this); 462 463 // Get the call in its initial state. We use this as a template to perform 464 // all the checks. 465 CallEventManager &CEMgr = getStateManager().getCallEventManager(); 466 CallEventRef<> CallTemplate 467 = CEMgr.getSimpleCall(CE, Pred->getState(), Pred->getLocationContext()); 468 469 // Evaluate the function call. We try each of the checkers 470 // to see if the can evaluate the function call. 471 ExplodedNodeSet dstCallEvaluated; 472 for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end(); 473 I != E; ++I) { 474 evalCall(dstCallEvaluated, *I, *CallTemplate); 475 } 476 477 // Finally, perform the post-condition check of the CallExpr and store 478 // the created nodes in 'Dst'. 479 // Note that if the call was inlined, dstCallEvaluated will be empty. 480 // The post-CallExpr check will occur in processCallExit. 481 getCheckerManager().runCheckersForPostStmt(dst, dstCallEvaluated, CE, 482 *this); 483} 484 485void ExprEngine::evalCall(ExplodedNodeSet &Dst, ExplodedNode *Pred, 486 const CallEvent &Call) { 487 // WARNING: At this time, the state attached to 'Call' may be older than the 488 // state in 'Pred'. This is a minor optimization since CheckerManager will 489 // use an updated CallEvent instance when calling checkers, but if 'Call' is 490 // ever used directly in this function all callers should be updated to pass 491 // the most recent state. (It is probably not worth doing the work here since 492 // for some callers this will not be necessary.) 493 494 // Run any pre-call checks using the generic call interface. 495 ExplodedNodeSet dstPreVisit; 496 getCheckerManager().runCheckersForPreCall(dstPreVisit, Pred, Call, *this); 497 498 // Actually evaluate the function call. We try each of the checkers 499 // to see if the can evaluate the function call, and get a callback at 500 // defaultEvalCall if all of them fail. 501 ExplodedNodeSet dstCallEvaluated; 502 getCheckerManager().runCheckersForEvalCall(dstCallEvaluated, dstPreVisit, 503 Call, *this); 504 505 // Finally, run any post-call checks. 506 getCheckerManager().runCheckersForPostCall(Dst, dstCallEvaluated, 507 Call, *this); 508} 509 510ProgramStateRef ExprEngine::bindReturnValue(const CallEvent &Call, 511 const LocationContext *LCtx, 512 ProgramStateRef State) { 513 const Expr *E = Call.getOriginExpr(); 514 if (!E) 515 return State; 516 517 // Some method families have known return values. 518 if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(&Call)) { 519 switch (Msg->getMethodFamily()) { 520 default: 521 break; 522 case OMF_autorelease: 523 case OMF_retain: 524 case OMF_self: { 525 // These methods return their receivers. 526 return State->BindExpr(E, LCtx, Msg->getReceiverSVal()); 527 } 528 } 529 } else if (const CXXConstructorCall *C = dyn_cast<CXXConstructorCall>(&Call)){ 530 return State->BindExpr(E, LCtx, C->getCXXThisVal()); 531 } 532 533 // Conjure a symbol if the return value is unknown. 534 QualType ResultTy = Call.getResultType(); 535 SValBuilder &SVB = getSValBuilder(); 536 unsigned Count = currentBuilderContext->getCurrentBlockCount(); 537 SVal R = SVB.conjureSymbolVal(0, E, LCtx, ResultTy, Count); 538 return State->BindExpr(E, LCtx, R); 539} 540 541// Conservatively evaluate call by invalidating regions and binding 542// a conjured return value. 543void ExprEngine::conservativeEvalCall(const CallEvent &Call, NodeBuilder &Bldr, 544 ExplodedNode *Pred, ProgramStateRef State) { 545 unsigned Count = currentBuilderContext->getCurrentBlockCount(); 546 State = Call.invalidateRegions(Count, State); 547 State = bindReturnValue(Call, Pred->getLocationContext(), State); 548 549 // And make the result node. 550 Bldr.generateNode(Call.getProgramPoint(), State, Pred); 551} 552 553void ExprEngine::defaultEvalCall(NodeBuilder &Bldr, ExplodedNode *Pred, 554 const CallEvent &CallTemplate) { 555 // Make sure we have the most recent state attached to the call. 556 ProgramStateRef State = Pred->getState(); 557 CallEventRef<> Call = CallTemplate.cloneWithState(State); 558 559 if (!getAnalysisManager().shouldInlineCall()) { 560 conservativeEvalCall(*Call, Bldr, Pred, State); 561 return; 562 } 563 // Try to inline the call. 564 // The origin expression here is just used as a kind of checksum; 565 // this should still be safe even for CallEvents that don't come from exprs. 566 const Expr *E = Call->getOriginExpr(); 567 ProgramStateRef InlinedFailedState = getInlineFailedState(State, E); 568 569 if (InlinedFailedState) { 570 // If we already tried once and failed, make sure we don't retry later. 571 State = InlinedFailedState; 572 } else { 573 RuntimeDefinition RD = Call->getRuntimeDefinition(); 574 const Decl *D = RD.getDecl(); 575 if (D) { 576 if (RD.mayHaveOtherDefinitions()) { 577 // Explore with and without inlining the call. 578 if (getAnalysisManager().IPAMode == DynamicDispatchBifurcate) { 579 BifurcateCall(RD.getDispatchRegion(), *Call, D, Bldr, Pred); 580 return; 581 } 582 583 // Don't inline if we're not in any dynamic dispatch mode. 584 if (getAnalysisManager().IPAMode != DynamicDispatch) { 585 conservativeEvalCall(*Call, Bldr, Pred, State); 586 return; 587 } 588 } 589 590 // We are not bifurcating and we do have a Decl, so just inline. 591 if (inlineCall(*Call, D, Bldr, Pred, State)) 592 return; 593 } 594 } 595 596 // If we can't inline it, handle the return value and invalidate the regions. 597 conservativeEvalCall(*Call, Bldr, Pred, State); 598} 599 600void ExprEngine::BifurcateCall(const MemRegion *BifurReg, 601 const CallEvent &Call, const Decl *D, 602 NodeBuilder &Bldr, ExplodedNode *Pred) { 603 assert(BifurReg); 604 BifurReg = BifurReg->StripCasts(); 605 606 // Check if we've performed the split already - note, we only want 607 // to split the path once per memory region. 608 ProgramStateRef State = Pred->getState(); 609 const unsigned int *BState = 610 State->get<DynamicDispatchBifurcationMap>(BifurReg); 611 if (BState) { 612 // If we are on "inline path", keep inlining if possible. 613 if (*BState == DynamicDispatchModeInlined) 614 if (inlineCall(Call, D, Bldr, Pred, State)) 615 return; 616 // If inline failed, or we are on the path where we assume we 617 // don't have enough info about the receiver to inline, conjure the 618 // return value and invalidate the regions. 619 conservativeEvalCall(Call, Bldr, Pred, State); 620 return; 621 } 622 623 // If we got here, this is the first time we process a message to this 624 // region, so split the path. 625 ProgramStateRef IState = 626 State->set<DynamicDispatchBifurcationMap>(BifurReg, 627 DynamicDispatchModeInlined); 628 inlineCall(Call, D, Bldr, Pred, IState); 629 630 ProgramStateRef NoIState = 631 State->set<DynamicDispatchBifurcationMap>(BifurReg, 632 DynamicDispatchModeConservative); 633 conservativeEvalCall(Call, Bldr, Pred, NoIState); 634 635 NumOfDynamicDispatchPathSplits++; 636 return; 637} 638 639 640void ExprEngine::VisitReturnStmt(const ReturnStmt *RS, ExplodedNode *Pred, 641 ExplodedNodeSet &Dst) { 642 643 ExplodedNodeSet dstPreVisit; 644 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, RS, *this); 645 646 StmtNodeBuilder B(dstPreVisit, Dst, *currentBuilderContext); 647 648 if (RS->getRetValue()) { 649 for (ExplodedNodeSet::iterator it = dstPreVisit.begin(), 650 ei = dstPreVisit.end(); it != ei; ++it) { 651 B.generateNode(RS, *it, (*it)->getState()); 652 } 653 } 654} 655