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/DenseMap.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/Casting.h"
25 using namespace clang;
28 #define DEBUG_TYPE "CoreEngine"
31 "The # of steps executed.");
32 STATISTIC(NumReachedMaxSteps,
33 "The # of times we reached the max number of steps.");
34 STATISTIC(NumPathsExplored,
35 "The # of paths explored by the analyzer.");
37 //===----------------------------------------------------------------------===//
38 // Worklist classes for exploration of reachable states.
39 //===----------------------------------------------------------------------===//
41 WorkList::Visitor::~Visitor() {}
44 class DFS : public WorkList {
45 SmallVector<WorkListUnit,20> Stack;
47 bool hasWork() const override {
48 return !Stack.empty();
51 void enqueue(const WorkListUnit& U) override {
55 WorkListUnit dequeue() override {
56 assert (!Stack.empty());
57 const WorkListUnit& U = Stack.back();
58 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
62 bool visitItemsInWorkList(Visitor &V) override {
63 for (SmallVectorImpl<WorkListUnit>::iterator
64 I = Stack.begin(), E = Stack.end(); I != E; ++I) {
72 class BFS : public WorkList {
73 std::deque<WorkListUnit> Queue;
75 bool hasWork() const override {
76 return !Queue.empty();
79 void enqueue(const WorkListUnit& U) override {
83 WorkListUnit dequeue() override {
84 WorkListUnit U = Queue.front();
89 bool visitItemsInWorkList(Visitor &V) override {
90 for (std::deque<WorkListUnit>::iterator
91 I = Queue.begin(), E = Queue.end(); I != E; ++I) {
99 } // end anonymous namespace
101 // Place the dstor for WorkList here because it contains virtual member
102 // functions, and we the code for the dstor generated in one compilation unit.
103 WorkList::~WorkList() {}
105 WorkList *WorkList::makeDFS() { return new DFS(); }
106 WorkList *WorkList::makeBFS() { return new BFS(); }
109 class BFSBlockDFSContents : public WorkList {
110 std::deque<WorkListUnit> Queue;
111 SmallVector<WorkListUnit,20> Stack;
113 bool hasWork() const override {
114 return !Queue.empty() || !Stack.empty();
117 void enqueue(const WorkListUnit& U) override {
118 if (U.getNode()->getLocation().getAs<BlockEntrance>())
124 WorkListUnit dequeue() override {
125 // Process all basic blocks to completion.
126 if (!Stack.empty()) {
127 const WorkListUnit& U = Stack.back();
128 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
132 assert(!Queue.empty());
133 // Don't use const reference. The subsequent pop_back() might make it
135 WorkListUnit U = Queue.front();
139 bool visitItemsInWorkList(Visitor &V) override {
140 for (SmallVectorImpl<WorkListUnit>::iterator
141 I = Stack.begin(), E = Stack.end(); I != E; ++I) {
145 for (std::deque<WorkListUnit>::iterator
146 I = Queue.begin(), E = Queue.end(); I != E; ++I) {
154 } // end anonymous namespace
156 WorkList* WorkList::makeBFSBlockDFSContents() {
157 return new BFSBlockDFSContents();
160 //===----------------------------------------------------------------------===//
161 // Core analysis engine.
162 //===----------------------------------------------------------------------===//
164 /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
165 bool CoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps,
166 ProgramStateRef InitState) {
168 if (G.num_roots() == 0) { // Initialize the analysis by constructing
169 // the root if none exists.
171 const CFGBlock *Entry = &(L->getCFG()->getEntry());
173 assert (Entry->empty() &&
174 "Entry block must be empty.");
176 assert (Entry->succ_size() == 1 &&
177 "Entry block must have 1 successor.");
179 // Mark the entry block as visited.
180 FunctionSummaries->markVisitedBasicBlock(Entry->getBlockID(),
182 L->getCFG()->getNumBlockIDs());
184 // Get the solitary successor.
185 const CFGBlock *Succ = *(Entry->succ_begin());
187 // Construct an edge representing the
188 // starting location in the function.
189 BlockEdge StartLoc(Entry, Succ, L);
191 // Set the current block counter to being empty.
192 WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
195 InitState = SubEng.getInitialState(L);
198 ExplodedNode *Node = G.getNode(StartLoc, InitState, false, &IsNew);
202 NodeBuilderContext BuilderCtx(*this, StartLoc.getDst(), Node);
203 ExplodedNodeSet DstBegin;
204 SubEng.processBeginOfFunction(BuilderCtx, Node, DstBegin, StartLoc);
209 // Check if we have a steps limit
210 bool UnlimitedSteps = Steps == 0;
211 // Cap our pre-reservation in the event that the user specifies
212 // a very large number of maximum steps.
213 const unsigned PreReservationCap = 4000000;
215 G.reserve(std::min(Steps,PreReservationCap));
217 while (WList->hasWork()) {
218 if (!UnlimitedSteps) {
220 NumReachedMaxSteps++;
228 const WorkListUnit& WU = WList->dequeue();
230 // Set the current block counter.
231 WList->setBlockCounter(WU.getBlockCounter());
233 // Retrieve the node.
234 ExplodedNode *Node = WU.getNode();
236 dispatchWorkItem(Node, Node->getLocation(), WU);
238 SubEng.processEndWorklist(hasWorkRemaining());
239 return WList->hasWork();
242 void CoreEngine::dispatchWorkItem(ExplodedNode* Pred, ProgramPoint Loc,
243 const WorkListUnit& WU) {
244 // Dispatch on the location type.
245 switch (Loc.getKind()) {
246 case ProgramPoint::BlockEdgeKind:
247 HandleBlockEdge(Loc.castAs<BlockEdge>(), Pred);
250 case ProgramPoint::BlockEntranceKind:
251 HandleBlockEntrance(Loc.castAs<BlockEntrance>(), Pred);
254 case ProgramPoint::BlockExitKind:
255 assert (false && "BlockExit location never occur in forward analysis.");
258 case ProgramPoint::CallEnterKind: {
259 HandleCallEnter(Loc.castAs<CallEnter>(), Pred);
263 case ProgramPoint::CallExitBeginKind:
264 SubEng.processCallExit(Pred);
267 case ProgramPoint::EpsilonKind: {
268 assert(Pred->hasSinglePred() &&
269 "Assume epsilon has exactly one predecessor by construction");
270 ExplodedNode *PNode = Pred->getFirstPred();
271 dispatchWorkItem(Pred, PNode->getLocation(), WU);
275 assert(Loc.getAs<PostStmt>() ||
276 Loc.getAs<PostInitializer>() ||
277 Loc.getAs<PostImplicitCall>() ||
278 Loc.getAs<CallExitEnd>());
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 // Process the final state transition.
314 SubEng.processEndOfFunction(BuilderCtx, Pred);
316 // This path is done. Don't enqueue any more nodes.
320 // Call into the SubEngine to process entering the CFGBlock.
321 ExplodedNodeSet dstNodes;
322 BlockEntrance BE(Blk, Pred->getLocationContext());
323 NodeBuilderWithSinks nodeBuilder(Pred, dstNodes, BuilderCtx, BE);
324 SubEng.processCFGBlockEntrance(L, nodeBuilder, Pred);
326 // Auto-generate a node.
327 if (!nodeBuilder.hasGeneratedNodes()) {
328 nodeBuilder.generateNode(Pred->State, Pred);
331 // Enqueue nodes onto the worklist.
335 void CoreEngine::HandleBlockEntrance(const BlockEntrance &L,
336 ExplodedNode *Pred) {
338 // Increment the block counter.
339 const LocationContext *LC = Pred->getLocationContext();
340 unsigned BlockId = L.getBlock()->getBlockID();
341 BlockCounter Counter = WList->getBlockCounter();
342 Counter = BCounterFactory.IncrementCount(Counter, LC->getCurrentStackFrame(),
344 WList->setBlockCounter(Counter);
346 // Process the entrance of the block.
347 if (Optional<CFGElement> E = L.getFirstElement()) {
348 NodeBuilderContext Ctx(*this, L.getBlock(), Pred);
349 SubEng.processCFGElement(*E, Pred, 0, &Ctx);
352 HandleBlockExit(L.getBlock(), Pred);
355 void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) {
357 if (const Stmt *Term = B->getTerminator()) {
358 switch (Term->getStmtClass()) {
360 llvm_unreachable("Analysis for this terminator not implemented.");
362 case Stmt::CXXBindTemporaryExprClass:
363 HandleCleanupTemporaryBranch(
364 cast<CXXBindTemporaryExpr>(B->getTerminator().getStmt()), B, Pred);
367 // Model static initializers.
368 case Stmt::DeclStmtClass:
369 HandleStaticInit(cast<DeclStmt>(Term), B, Pred);
372 case Stmt::BinaryOperatorClass: // '&&' and '||'
373 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
376 case Stmt::BinaryConditionalOperatorClass:
377 case Stmt::ConditionalOperatorClass:
378 HandleBranch(cast<AbstractConditionalOperator>(Term)->getCond(),
382 // FIXME: Use constant-folding in CFG construction to simplify this
385 case Stmt::ChooseExprClass:
386 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
389 case Stmt::CXXTryStmtClass: {
390 // Generate a node for each of the successors.
391 // Our logic for EH analysis can certainly be improved.
392 for (CFGBlock::const_succ_iterator it = B->succ_begin(),
393 et = B->succ_end(); it != et; ++it) {
394 if (const CFGBlock *succ = *it) {
395 generateNode(BlockEdge(B, succ, Pred->getLocationContext()),
402 case Stmt::DoStmtClass:
403 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
406 case Stmt::CXXForRangeStmtClass:
407 HandleBranch(cast<CXXForRangeStmt>(Term)->getCond(), Term, B, Pred);
410 case Stmt::ForStmtClass:
411 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
414 case Stmt::ContinueStmtClass:
415 case Stmt::BreakStmtClass:
416 case Stmt::GotoStmtClass:
419 case Stmt::IfStmtClass:
420 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
423 case Stmt::IndirectGotoStmtClass: {
424 // Only 1 successor: the indirect goto dispatch block.
425 assert (B->succ_size() == 1);
427 IndirectGotoNodeBuilder
428 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
429 *(B->succ_begin()), this);
431 SubEng.processIndirectGoto(builder);
435 case Stmt::ObjCForCollectionStmtClass: {
436 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
438 // (1) inside a basic block, which represents the binding of the
439 // 'element' variable to a value.
440 // (2) in a terminator, which represents the branch.
442 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
443 // whether or not collection contains any more elements. We cannot
444 // just test to see if the element is nil because a container can
445 // contain nil elements.
446 HandleBranch(Term, Term, B, Pred);
450 case Stmt::SwitchStmtClass: {
451 SwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
454 SubEng.processSwitch(builder);
458 case Stmt::WhileStmtClass:
459 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
464 assert (B->succ_size() == 1 &&
465 "Blocks with no terminator should have at most 1 successor.");
467 generateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
471 void CoreEngine::HandleCallEnter(const CallEnter &CE, ExplodedNode *Pred) {
472 NodeBuilderContext BuilderCtx(*this, CE.getEntry(), Pred);
473 SubEng.processCallEnter(BuilderCtx, CE, Pred);
476 void CoreEngine::HandleBranch(const Stmt *Cond, const Stmt *Term,
477 const CFGBlock * B, ExplodedNode *Pred) {
478 assert(B->succ_size() == 2);
479 NodeBuilderContext Ctx(*this, B, Pred);
481 SubEng.processBranch(Cond, Term, Ctx, Pred, Dst,
482 *(B->succ_begin()), *(B->succ_begin()+1));
483 // Enqueue the new frontier onto the worklist.
487 void CoreEngine::HandleCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE,
489 ExplodedNode *Pred) {
490 assert(B->succ_size() == 2);
491 NodeBuilderContext Ctx(*this, B, Pred);
493 SubEng.processCleanupTemporaryBranch(BTE, Ctx, Pred, Dst, *(B->succ_begin()),
494 *(B->succ_begin() + 1));
495 // Enqueue the new frontier onto the worklist.
499 void CoreEngine::HandleStaticInit(const DeclStmt *DS, const CFGBlock *B,
500 ExplodedNode *Pred) {
501 assert(B->succ_size() == 2);
502 NodeBuilderContext Ctx(*this, B, Pred);
504 SubEng.processStaticInitializer(DS, Ctx, Pred, Dst,
505 *(B->succ_begin()), *(B->succ_begin()+1));
506 // Enqueue the new frontier onto the worklist.
511 void CoreEngine::HandlePostStmt(const CFGBlock *B, unsigned StmtIdx,
512 ExplodedNode *Pred) {
516 if (StmtIdx == B->size())
517 HandleBlockExit(B, Pred);
519 NodeBuilderContext Ctx(*this, B, Pred);
520 SubEng.processCFGElement((*B)[StmtIdx], Pred, StmtIdx, &Ctx);
524 /// generateNode - Utility method to generate nodes, hook up successors,
525 /// and add nodes to the worklist.
526 void CoreEngine::generateNode(const ProgramPoint &Loc,
527 ProgramStateRef State,
528 ExplodedNode *Pred) {
531 ExplodedNode *Node = G.getNode(Loc, State, false, &IsNew);
534 Node->addPredecessor(Pred, G); // Link 'Node' with its predecessor.
537 G.addRoot(Node); // 'Node' has no predecessor. Make it a root.
540 // Only add 'Node' to the worklist if it was freshly generated.
541 if (IsNew) WList->enqueue(Node);
544 void CoreEngine::enqueueStmtNode(ExplodedNode *N,
545 const CFGBlock *Block, unsigned Idx) {
547 assert (!N->isSink());
549 // Check if this node entered a callee.
550 if (N->getLocation().getAs<CallEnter>()) {
551 // Still use the index of the CallExpr. It's needed to create the callee
552 // StackFrameContext.
553 WList->enqueue(N, Block, Idx);
557 // Do not create extra nodes. Move to the next CFG element.
558 if (N->getLocation().getAs<PostInitializer>() ||
559 N->getLocation().getAs<PostImplicitCall>()) {
560 WList->enqueue(N, Block, Idx+1);
564 if (N->getLocation().getAs<EpsilonPoint>()) {
565 WList->enqueue(N, Block, Idx);
569 if ((*Block)[Idx].getKind() == CFGElement::NewAllocator) {
570 WList->enqueue(N, Block, Idx+1);
574 // At this point, we know we're processing a normal statement.
575 CFGStmt CS = (*Block)[Idx].castAs<CFGStmt>();
576 PostStmt Loc(CS.getStmt(), N->getLocationContext());
578 if (Loc == N->getLocation().withTag(nullptr)) {
579 // Note: 'N' should be a fresh node because otherwise it shouldn't be
580 // a member of Deferred.
581 WList->enqueue(N, Block, Idx+1);
586 ExplodedNode *Succ = G.getNode(Loc, N->getState(), false, &IsNew);
587 Succ->addPredecessor(N, G);
590 WList->enqueue(Succ, Block, Idx+1);
593 ExplodedNode *CoreEngine::generateCallExitBeginNode(ExplodedNode *N) {
594 // Create a CallExitBegin node and enqueue it.
595 const StackFrameContext *LocCtx
596 = cast<StackFrameContext>(N->getLocationContext());
598 // Use the callee location context.
599 CallExitBegin Loc(LocCtx);
602 ExplodedNode *Node = G.getNode(Loc, N->getState(), false, &isNew);
603 Node->addPredecessor(N, G);
604 return isNew ? Node : nullptr;
608 void CoreEngine::enqueue(ExplodedNodeSet &Set) {
609 for (ExplodedNodeSet::iterator I = Set.begin(),
610 E = Set.end(); I != E; ++I) {
615 void CoreEngine::enqueue(ExplodedNodeSet &Set,
616 const CFGBlock *Block, unsigned Idx) {
617 for (ExplodedNodeSet::iterator I = Set.begin(),
618 E = Set.end(); I != E; ++I) {
619 enqueueStmtNode(*I, Block, Idx);
623 void CoreEngine::enqueueEndOfFunction(ExplodedNodeSet &Set) {
624 for (ExplodedNodeSet::iterator I = Set.begin(), E = Set.end(); I != E; ++I) {
625 ExplodedNode *N = *I;
626 // If we are in an inlined call, generate CallExitBegin node.
627 if (N->getLocationContext()->getParent()) {
628 N = generateCallExitBeginNode(N);
632 // TODO: We should run remove dead bindings here.
640 void NodeBuilder::anchor() { }
642 ExplodedNode* NodeBuilder::generateNodeImpl(const ProgramPoint &Loc,
643 ProgramStateRef State,
646 HasGeneratedNodes = true;
648 ExplodedNode *N = C.Eng.G.getNode(Loc, State, MarkAsSink, &IsNew);
649 N->addPredecessor(FromN, C.Eng.G);
650 Frontier.erase(FromN);
661 void NodeBuilderWithSinks::anchor() { }
663 StmtNodeBuilder::~StmtNodeBuilder() {
665 for (ExplodedNodeSet::iterator I = Frontier.begin(),
666 E = Frontier.end(); I != E; ++I )
667 EnclosingBldr->addNodes(*I);
670 void BranchNodeBuilder::anchor() { }
672 ExplodedNode *BranchNodeBuilder::generateNode(ProgramStateRef State,
674 ExplodedNode *NodePred) {
675 // If the branch has been marked infeasible we should not generate a node.
676 if (!isFeasible(branch))
679 ProgramPoint Loc = BlockEdge(C.Block, branch ? DstT:DstF,
680 NodePred->getLocationContext());
681 ExplodedNode *Succ = generateNodeImpl(Loc, State, NodePred);
686 IndirectGotoNodeBuilder::generateNode(const iterator &I,
691 Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()),
693 Succ->addPredecessor(Pred, Eng.G);
699 Eng.WList->enqueue(Succ);
706 SwitchNodeBuilder::generateCaseStmtNode(const iterator &I,
707 ProgramStateRef St) {
711 Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()),
713 Succ->addPredecessor(Pred, Eng.G);
717 Eng.WList->enqueue(Succ);
723 SwitchNodeBuilder::generateDefaultCaseNode(ProgramStateRef St,
725 // Get the block for the default case.
726 assert(Src->succ_rbegin() != Src->succ_rend());
727 CFGBlock *DefaultBlock = *Src->succ_rbegin();
729 // Sanity check for default blocks that are unreachable and not caught
730 // by earlier stages.
736 Eng.G.getNode(BlockEdge(Src, DefaultBlock, Pred->getLocationContext()),
738 Succ->addPredecessor(Pred, Eng.G);
744 Eng.WList->enqueue(Succ);