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