1 //==- CoreEngine.cpp - Path-Sensitive Dataflow Engine ------------*- C++ -*-//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a generic engine for intraprocedural, path-sensitive,
11 // dataflow analysis via graph reachability engine.
13 //===----------------------------------------------------------------------===//
15 #include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "clang/AST/StmtCXX.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Support/Casting.h"
24 using namespace clang;
27 #define DEBUG_TYPE "CoreEngine"
30 "The # of steps executed.");
31 STATISTIC(NumReachedMaxSteps,
32 "The # of times we reached the max number of steps.");
33 STATISTIC(NumPathsExplored,
34 "The # of paths explored by the analyzer.");
36 //===----------------------------------------------------------------------===//
37 // Worklist classes for exploration of reachable states.
38 //===----------------------------------------------------------------------===//
40 WorkList::Visitor::~Visitor() {}
43 class DFS : public WorkList {
44 SmallVector<WorkListUnit,20> Stack;
46 bool hasWork() const override {
47 return !Stack.empty();
50 void enqueue(const WorkListUnit& U) override {
54 WorkListUnit dequeue() override {
55 assert (!Stack.empty());
56 const WorkListUnit& U = Stack.back();
57 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
61 bool visitItemsInWorkList(Visitor &V) override {
62 for (SmallVectorImpl<WorkListUnit>::iterator
63 I = Stack.begin(), E = Stack.end(); I != E; ++I) {
71 class BFS : public WorkList {
72 std::deque<WorkListUnit> Queue;
74 bool hasWork() const override {
75 return !Queue.empty();
78 void enqueue(const WorkListUnit& U) override {
82 WorkListUnit dequeue() override {
83 WorkListUnit U = Queue.front();
88 bool visitItemsInWorkList(Visitor &V) override {
89 for (std::deque<WorkListUnit>::iterator
90 I = Queue.begin(), E = Queue.end(); I != E; ++I) {
98 } // end anonymous namespace
100 // Place the dstor for WorkList here because it contains virtual member
101 // functions, and we the code for the dstor generated in one compilation unit.
102 WorkList::~WorkList() {}
104 WorkList *WorkList::makeDFS() { return new DFS(); }
105 WorkList *WorkList::makeBFS() { return new BFS(); }
108 class BFSBlockDFSContents : public WorkList {
109 std::deque<WorkListUnit> Queue;
110 SmallVector<WorkListUnit,20> Stack;
112 bool hasWork() const override {
113 return !Queue.empty() || !Stack.empty();
116 void enqueue(const WorkListUnit& U) override {
117 if (U.getNode()->getLocation().getAs<BlockEntrance>())
123 WorkListUnit dequeue() override {
124 // Process all basic blocks to completion.
125 if (!Stack.empty()) {
126 const WorkListUnit& U = Stack.back();
127 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
131 assert(!Queue.empty());
132 // Don't use const reference. The subsequent pop_back() might make it
134 WorkListUnit U = Queue.front();
138 bool visitItemsInWorkList(Visitor &V) override {
139 for (SmallVectorImpl<WorkListUnit>::iterator
140 I = Stack.begin(), E = Stack.end(); I != E; ++I) {
144 for (std::deque<WorkListUnit>::iterator
145 I = Queue.begin(), E = Queue.end(); I != E; ++I) {
153 } // end anonymous namespace
155 WorkList* WorkList::makeBFSBlockDFSContents() {
156 return new BFSBlockDFSContents();
159 //===----------------------------------------------------------------------===//
160 // Core analysis engine.
161 //===----------------------------------------------------------------------===//
163 /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
164 bool CoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps,
165 ProgramStateRef InitState) {
167 if (G.num_roots() == 0) { // Initialize the analysis by constructing
168 // the root if none exists.
170 const CFGBlock *Entry = &(L->getCFG()->getEntry());
172 assert (Entry->empty() &&
173 "Entry block must be empty.");
175 assert (Entry->succ_size() == 1 &&
176 "Entry block must have 1 successor.");
178 // Mark the entry block as visited.
179 FunctionSummaries->markVisitedBasicBlock(Entry->getBlockID(),
181 L->getCFG()->getNumBlockIDs());
183 // Get the solitary successor.
184 const CFGBlock *Succ = *(Entry->succ_begin());
186 // Construct an edge representing the
187 // starting location in the function.
188 BlockEdge StartLoc(Entry, Succ, L);
190 // Set the current block counter to being empty.
191 WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
194 InitState = SubEng.getInitialState(L);
197 ExplodedNode *Node = G.getNode(StartLoc, InitState, false, &IsNew);
201 NodeBuilderContext BuilderCtx(*this, StartLoc.getDst(), Node);
202 ExplodedNodeSet DstBegin;
203 SubEng.processBeginOfFunction(BuilderCtx, Node, DstBegin, StartLoc);
208 // Check if we have a steps limit
209 bool UnlimitedSteps = Steps == 0;
210 // Cap our pre-reservation in the event that the user specifies
211 // a very large number of maximum steps.
212 const unsigned PreReservationCap = 4000000;
214 G.reserve(std::min(Steps,PreReservationCap));
216 while (WList->hasWork()) {
217 if (!UnlimitedSteps) {
219 NumReachedMaxSteps++;
227 const WorkListUnit& WU = WList->dequeue();
229 // Set the current block counter.
230 WList->setBlockCounter(WU.getBlockCounter());
232 // Retrieve the node.
233 ExplodedNode *Node = WU.getNode();
235 dispatchWorkItem(Node, Node->getLocation(), WU);
237 SubEng.processEndWorklist(hasWorkRemaining());
238 return WList->hasWork();
241 void CoreEngine::dispatchWorkItem(ExplodedNode* Pred, ProgramPoint Loc,
242 const WorkListUnit& WU) {
243 // Dispatch on the location type.
244 switch (Loc.getKind()) {
245 case ProgramPoint::BlockEdgeKind:
246 HandleBlockEdge(Loc.castAs<BlockEdge>(), Pred);
249 case ProgramPoint::BlockEntranceKind:
250 HandleBlockEntrance(Loc.castAs<BlockEntrance>(), Pred);
253 case ProgramPoint::BlockExitKind:
254 assert (false && "BlockExit location never occur in forward analysis.");
257 case ProgramPoint::CallEnterKind: {
258 HandleCallEnter(Loc.castAs<CallEnter>(), Pred);
262 case ProgramPoint::CallExitBeginKind:
263 SubEng.processCallExit(Pred);
266 case ProgramPoint::EpsilonKind: {
267 assert(Pred->hasSinglePred() &&
268 "Assume epsilon has exactly one predecessor by construction");
269 ExplodedNode *PNode = Pred->getFirstPred();
270 dispatchWorkItem(Pred, PNode->getLocation(), WU);
274 assert(Loc.getAs<PostStmt>() ||
275 Loc.getAs<PostInitializer>() ||
276 Loc.getAs<PostImplicitCall>() ||
277 Loc.getAs<CallExitEnd>() ||
278 Loc.getAs<LoopExit>());
279 HandlePostStmt(WU.getBlock(), WU.getIndex(), Pred);
284 bool CoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L,
286 ProgramStateRef InitState,
287 ExplodedNodeSet &Dst) {
288 bool DidNotFinish = ExecuteWorkList(L, Steps, InitState);
289 for (ExplodedGraph::eop_iterator I = G.eop_begin(), E = G.eop_end(); I != E;
296 void CoreEngine::HandleBlockEdge(const BlockEdge &L, ExplodedNode *Pred) {
298 const CFGBlock *Blk = L.getDst();
299 NodeBuilderContext BuilderCtx(*this, Blk, Pred);
301 // Mark this block as visited.
302 const LocationContext *LC = Pred->getLocationContext();
303 FunctionSummaries->markVisitedBasicBlock(Blk->getBlockID(),
305 LC->getCFG()->getNumBlockIDs());
307 // Check if we are entering the EXIT block.
308 if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
310 assert (L.getLocationContext()->getCFG()->getExit().size() == 0
311 && "EXIT block cannot contain Stmts.");
313 // Get return statement..
314 const ReturnStmt *RS = nullptr;
315 if (!L.getSrc()->empty()) {
316 if (Optional<CFGStmt> LastStmt = L.getSrc()->back().getAs<CFGStmt>()) {
317 if ((RS = dyn_cast<ReturnStmt>(LastStmt->getStmt()))) {
318 if (!RS->getRetValue())
324 // Process the final state transition.
325 SubEng.processEndOfFunction(BuilderCtx, Pred, RS);
327 // This path is done. Don't enqueue any more nodes.
331 // Call into the SubEngine to process entering the CFGBlock.
332 ExplodedNodeSet dstNodes;
333 BlockEntrance BE(Blk, Pred->getLocationContext());
334 NodeBuilderWithSinks nodeBuilder(Pred, dstNodes, BuilderCtx, BE);
335 SubEng.processCFGBlockEntrance(L, nodeBuilder, Pred);
337 // Auto-generate a node.
338 if (!nodeBuilder.hasGeneratedNodes()) {
339 nodeBuilder.generateNode(Pred->State, Pred);
342 // Enqueue nodes onto the worklist.
346 void CoreEngine::HandleBlockEntrance(const BlockEntrance &L,
347 ExplodedNode *Pred) {
349 // Increment the block counter.
350 const LocationContext *LC = Pred->getLocationContext();
351 unsigned BlockId = L.getBlock()->getBlockID();
352 BlockCounter Counter = WList->getBlockCounter();
353 Counter = BCounterFactory.IncrementCount(Counter, LC->getCurrentStackFrame(),
355 WList->setBlockCounter(Counter);
357 // Process the entrance of the block.
358 if (Optional<CFGElement> E = L.getFirstElement()) {
359 NodeBuilderContext Ctx(*this, L.getBlock(), Pred);
360 SubEng.processCFGElement(*E, Pred, 0, &Ctx);
363 HandleBlockExit(L.getBlock(), Pred);
366 void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) {
368 if (const Stmt *Term = B->getTerminator()) {
369 switch (Term->getStmtClass()) {
371 llvm_unreachable("Analysis for this terminator not implemented.");
373 case Stmt::CXXBindTemporaryExprClass:
374 HandleCleanupTemporaryBranch(
375 cast<CXXBindTemporaryExpr>(B->getTerminator().getStmt()), B, Pred);
378 // Model static initializers.
379 case Stmt::DeclStmtClass:
380 HandleStaticInit(cast<DeclStmt>(Term), B, Pred);
383 case Stmt::BinaryOperatorClass: // '&&' and '||'
384 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
387 case Stmt::BinaryConditionalOperatorClass:
388 case Stmt::ConditionalOperatorClass:
389 HandleBranch(cast<AbstractConditionalOperator>(Term)->getCond(),
393 // FIXME: Use constant-folding in CFG construction to simplify this
396 case Stmt::ChooseExprClass:
397 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
400 case Stmt::CXXTryStmtClass: {
401 // Generate a node for each of the successors.
402 // Our logic for EH analysis can certainly be improved.
403 for (CFGBlock::const_succ_iterator it = B->succ_begin(),
404 et = B->succ_end(); it != et; ++it) {
405 if (const CFGBlock *succ = *it) {
406 generateNode(BlockEdge(B, succ, Pred->getLocationContext()),
413 case Stmt::DoStmtClass:
414 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
417 case Stmt::CXXForRangeStmtClass:
418 HandleBranch(cast<CXXForRangeStmt>(Term)->getCond(), Term, B, Pred);
421 case Stmt::ForStmtClass:
422 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
425 case Stmt::ContinueStmtClass:
426 case Stmt::BreakStmtClass:
427 case Stmt::GotoStmtClass:
430 case Stmt::IfStmtClass:
431 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
434 case Stmt::IndirectGotoStmtClass: {
435 // Only 1 successor: the indirect goto dispatch block.
436 assert (B->succ_size() == 1);
438 IndirectGotoNodeBuilder
439 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
440 *(B->succ_begin()), this);
442 SubEng.processIndirectGoto(builder);
446 case Stmt::ObjCForCollectionStmtClass: {
447 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
449 // (1) inside a basic block, which represents the binding of the
450 // 'element' variable to a value.
451 // (2) in a terminator, which represents the branch.
453 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
454 // whether or not collection contains any more elements. We cannot
455 // just test to see if the element is nil because a container can
456 // contain nil elements.
457 HandleBranch(Term, Term, B, Pred);
461 case Stmt::SwitchStmtClass: {
462 SwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
465 SubEng.processSwitch(builder);
469 case Stmt::WhileStmtClass:
470 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
475 assert (B->succ_size() == 1 &&
476 "Blocks with no terminator should have at most 1 successor.");
478 generateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
482 void CoreEngine::HandleCallEnter(const CallEnter &CE, ExplodedNode *Pred) {
483 NodeBuilderContext BuilderCtx(*this, CE.getEntry(), Pred);
484 SubEng.processCallEnter(BuilderCtx, CE, Pred);
487 void CoreEngine::HandleBranch(const Stmt *Cond, const Stmt *Term,
488 const CFGBlock * B, ExplodedNode *Pred) {
489 assert(B->succ_size() == 2);
490 NodeBuilderContext Ctx(*this, B, Pred);
492 SubEng.processBranch(Cond, Term, Ctx, Pred, Dst,
493 *(B->succ_begin()), *(B->succ_begin()+1));
494 // Enqueue the new frontier onto the worklist.
498 void CoreEngine::HandleCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE,
500 ExplodedNode *Pred) {
501 assert(B->succ_size() == 2);
502 NodeBuilderContext Ctx(*this, B, Pred);
504 SubEng.processCleanupTemporaryBranch(BTE, Ctx, Pred, Dst, *(B->succ_begin()),
505 *(B->succ_begin() + 1));
506 // Enqueue the new frontier onto the worklist.
510 void CoreEngine::HandleStaticInit(const DeclStmt *DS, const CFGBlock *B,
511 ExplodedNode *Pred) {
512 assert(B->succ_size() == 2);
513 NodeBuilderContext Ctx(*this, B, Pred);
515 SubEng.processStaticInitializer(DS, Ctx, Pred, Dst,
516 *(B->succ_begin()), *(B->succ_begin()+1));
517 // Enqueue the new frontier onto the worklist.
522 void CoreEngine::HandlePostStmt(const CFGBlock *B, unsigned StmtIdx,
523 ExplodedNode *Pred) {
527 if (StmtIdx == B->size())
528 HandleBlockExit(B, Pred);
530 NodeBuilderContext Ctx(*this, B, Pred);
531 SubEng.processCFGElement((*B)[StmtIdx], Pred, StmtIdx, &Ctx);
535 /// generateNode - Utility method to generate nodes, hook up successors,
536 /// and add nodes to the worklist.
537 void CoreEngine::generateNode(const ProgramPoint &Loc,
538 ProgramStateRef State,
539 ExplodedNode *Pred) {
542 ExplodedNode *Node = G.getNode(Loc, State, false, &IsNew);
545 Node->addPredecessor(Pred, G); // Link 'Node' with its predecessor.
548 G.addRoot(Node); // 'Node' has no predecessor. Make it a root.
551 // Only add 'Node' to the worklist if it was freshly generated.
552 if (IsNew) WList->enqueue(Node);
555 void CoreEngine::enqueueStmtNode(ExplodedNode *N,
556 const CFGBlock *Block, unsigned Idx) {
558 assert (!N->isSink());
560 // Check if this node entered a callee.
561 if (N->getLocation().getAs<CallEnter>()) {
562 // Still use the index of the CallExpr. It's needed to create the callee
563 // StackFrameContext.
564 WList->enqueue(N, Block, Idx);
568 // Do not create extra nodes. Move to the next CFG element.
569 if (N->getLocation().getAs<PostInitializer>() ||
570 N->getLocation().getAs<PostImplicitCall>()||
571 N->getLocation().getAs<LoopExit>()) {
572 WList->enqueue(N, Block, Idx+1);
576 if (N->getLocation().getAs<EpsilonPoint>()) {
577 WList->enqueue(N, Block, Idx);
581 if ((*Block)[Idx].getKind() == CFGElement::NewAllocator) {
582 WList->enqueue(N, Block, Idx+1);
586 // At this point, we know we're processing a normal statement.
587 CFGStmt CS = (*Block)[Idx].castAs<CFGStmt>();
588 PostStmt Loc(CS.getStmt(), N->getLocationContext());
590 if (Loc == N->getLocation().withTag(nullptr)) {
591 // Note: 'N' should be a fresh node because otherwise it shouldn't be
592 // a member of Deferred.
593 WList->enqueue(N, Block, Idx+1);
598 ExplodedNode *Succ = G.getNode(Loc, N->getState(), false, &IsNew);
599 Succ->addPredecessor(N, G);
602 WList->enqueue(Succ, Block, Idx+1);
605 ExplodedNode *CoreEngine::generateCallExitBeginNode(ExplodedNode *N,
606 const ReturnStmt *RS) {
607 // Create a CallExitBegin node and enqueue it.
608 const StackFrameContext *LocCtx
609 = cast<StackFrameContext>(N->getLocationContext());
611 // Use the callee location context.
612 CallExitBegin Loc(LocCtx, RS);
615 ExplodedNode *Node = G.getNode(Loc, N->getState(), false, &isNew);
616 Node->addPredecessor(N, G);
617 return isNew ? Node : nullptr;
621 void CoreEngine::enqueue(ExplodedNodeSet &Set) {
622 for (ExplodedNodeSet::iterator I = Set.begin(),
623 E = Set.end(); I != E; ++I) {
628 void CoreEngine::enqueue(ExplodedNodeSet &Set,
629 const CFGBlock *Block, unsigned Idx) {
630 for (ExplodedNodeSet::iterator I = Set.begin(),
631 E = Set.end(); I != E; ++I) {
632 enqueueStmtNode(*I, Block, Idx);
636 void CoreEngine::enqueueEndOfFunction(ExplodedNodeSet &Set, const ReturnStmt *RS) {
637 for (ExplodedNodeSet::iterator I = Set.begin(), E = Set.end(); I != E; ++I) {
638 ExplodedNode *N = *I;
639 // If we are in an inlined call, generate CallExitBegin node.
640 if (N->getLocationContext()->getParent()) {
641 N = generateCallExitBeginNode(N, RS);
645 // TODO: We should run remove dead bindings here.
653 void NodeBuilder::anchor() { }
655 ExplodedNode* NodeBuilder::generateNodeImpl(const ProgramPoint &Loc,
656 ProgramStateRef State,
659 HasGeneratedNodes = true;
661 ExplodedNode *N = C.Eng.G.getNode(Loc, State, MarkAsSink, &IsNew);
662 N->addPredecessor(FromN, C.Eng.G);
663 Frontier.erase(FromN);
674 void NodeBuilderWithSinks::anchor() { }
676 StmtNodeBuilder::~StmtNodeBuilder() {
678 for (ExplodedNodeSet::iterator I = Frontier.begin(),
679 E = Frontier.end(); I != E; ++I )
680 EnclosingBldr->addNodes(*I);
683 void BranchNodeBuilder::anchor() { }
685 ExplodedNode *BranchNodeBuilder::generateNode(ProgramStateRef State,
687 ExplodedNode *NodePred) {
688 // If the branch has been marked infeasible we should not generate a node.
689 if (!isFeasible(branch))
692 ProgramPoint Loc = BlockEdge(C.Block, branch ? DstT:DstF,
693 NodePred->getLocationContext());
694 ExplodedNode *Succ = generateNodeImpl(Loc, State, NodePred);
699 IndirectGotoNodeBuilder::generateNode(const iterator &I,
704 Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()),
706 Succ->addPredecessor(Pred, Eng.G);
712 Eng.WList->enqueue(Succ);
719 SwitchNodeBuilder::generateCaseStmtNode(const iterator &I,
720 ProgramStateRef St) {
724 Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()),
726 Succ->addPredecessor(Pred, Eng.G);
730 Eng.WList->enqueue(Succ);
736 SwitchNodeBuilder::generateDefaultCaseNode(ProgramStateRef St,
738 // Get the block for the default case.
739 assert(Src->succ_rbegin() != Src->succ_rend());
740 CFGBlock *DefaultBlock = *Src->succ_rbegin();
742 // Sanity check for default blocks that are unreachable and not caught
743 // by earlier stages.
749 Eng.G.getNode(BlockEdge(Src, DefaultBlock, Pred->getLocationContext()),
751 Succ->addPredecessor(Pred, Eng.G);
757 Eng.WList->enqueue(Succ);