CoreEngine.cpp revision e62f048960645b79363408fdead53fec2a063c52
1//==- CoreEngine.cpp - Path-Sensitive Dataflow Engine ------------*- 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 a generic engine for intraprocedural, path-sensitive,
11//  dataflow analysis via graph reachability engine.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "CoreEngine"
16
17#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
20#include "clang/Index/TranslationUnit.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/StmtCXX.h"
23#include "llvm/Support/Casting.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/Statistic.h"
26
27using namespace clang;
28using namespace ento;
29
30STATISTIC(NumReachedMaxSteps,
31            "The # of times we reached the max number of steps.");
32STATISTIC(NumPathsExplored,
33            "The # of paths explored by the analyzer.");
34
35//===----------------------------------------------------------------------===//
36// Worklist classes for exploration of reachable states.
37//===----------------------------------------------------------------------===//
38
39WorkList::Visitor::~Visitor() {}
40
41namespace {
42class DFS : public WorkList {
43  SmallVector<WorkListUnit,20> Stack;
44public:
45  virtual bool hasWork() const {
46    return !Stack.empty();
47  }
48
49  virtual void enqueue(const WorkListUnit& U) {
50    Stack.push_back(U);
51  }
52
53  virtual WorkListUnit dequeue() {
54    assert (!Stack.empty());
55    const WorkListUnit& U = Stack.back();
56    Stack.pop_back(); // This technically "invalidates" U, but we are fine.
57    return U;
58  }
59
60  virtual bool visitItemsInWorkList(Visitor &V) {
61    for (SmallVectorImpl<WorkListUnit>::iterator
62         I = Stack.begin(), E = Stack.end(); I != E; ++I) {
63      if (V.visit(*I))
64        return true;
65    }
66    return false;
67  }
68};
69
70class BFS : public WorkList {
71  std::deque<WorkListUnit> Queue;
72public:
73  virtual bool hasWork() const {
74    return !Queue.empty();
75  }
76
77  virtual void enqueue(const WorkListUnit& U) {
78    Queue.push_front(U);
79  }
80
81  virtual WorkListUnit dequeue() {
82    WorkListUnit U = Queue.front();
83    Queue.pop_front();
84    return U;
85  }
86
87  virtual bool visitItemsInWorkList(Visitor &V) {
88    for (std::deque<WorkListUnit>::iterator
89         I = Queue.begin(), E = Queue.end(); I != E; ++I) {
90      if (V.visit(*I))
91        return true;
92    }
93    return false;
94  }
95};
96
97} // end anonymous namespace
98
99// Place the dstor for WorkList here because it contains virtual member
100// functions, and we the code for the dstor generated in one compilation unit.
101WorkList::~WorkList() {}
102
103WorkList *WorkList::makeDFS() { return new DFS(); }
104WorkList *WorkList::makeBFS() { return new BFS(); }
105
106namespace {
107  class BFSBlockDFSContents : public WorkList {
108    std::deque<WorkListUnit> Queue;
109    SmallVector<WorkListUnit,20> Stack;
110  public:
111    virtual bool hasWork() const {
112      return !Queue.empty() || !Stack.empty();
113    }
114
115    virtual void enqueue(const WorkListUnit& U) {
116      if (isa<BlockEntrance>(U.getNode()->getLocation()))
117        Queue.push_front(U);
118      else
119        Stack.push_back(U);
120    }
121
122    virtual WorkListUnit dequeue() {
123      // Process all basic blocks to completion.
124      if (!Stack.empty()) {
125        const WorkListUnit& U = Stack.back();
126        Stack.pop_back(); // This technically "invalidates" U, but we are fine.
127        return U;
128      }
129
130      assert(!Queue.empty());
131      // Don't use const reference.  The subsequent pop_back() might make it
132      // unsafe.
133      WorkListUnit U = Queue.front();
134      Queue.pop_front();
135      return U;
136    }
137    virtual bool visitItemsInWorkList(Visitor &V) {
138      for (SmallVectorImpl<WorkListUnit>::iterator
139           I = Stack.begin(), E = Stack.end(); I != E; ++I) {
140        if (V.visit(*I))
141          return true;
142      }
143      for (std::deque<WorkListUnit>::iterator
144           I = Queue.begin(), E = Queue.end(); I != E; ++I) {
145        if (V.visit(*I))
146          return true;
147      }
148      return false;
149    }
150
151  };
152} // end anonymous namespace
153
154WorkList* WorkList::makeBFSBlockDFSContents() {
155  return new BFSBlockDFSContents();
156}
157
158//===----------------------------------------------------------------------===//
159// Core analysis engine.
160//===----------------------------------------------------------------------===//
161
162/// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
163bool CoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps,
164                                   ProgramStateRef InitState) {
165
166  if (G->num_roots() == 0) { // Initialize the analysis by constructing
167    // the root if none exists.
168
169    const CFGBlock *Entry = &(L->getCFG()->getEntry());
170
171    assert (Entry->empty() &&
172            "Entry block must be empty.");
173
174    assert (Entry->succ_size() == 1 &&
175            "Entry block must have 1 successor.");
176
177    // Mark the entry block as visited.
178    FunctionSummaries->markVisitedBasicBlock(Entry->getBlockID(),
179                                             L->getDecl(),
180                                             L->getCFG()->getNumBlockIDs());
181
182    // Get the solitary successor.
183    const CFGBlock *Succ = *(Entry->succ_begin());
184
185    // Construct an edge representing the
186    // starting location in the function.
187    BlockEdge StartLoc(Entry, Succ, L);
188
189    // Set the current block counter to being empty.
190    WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
191
192    if (!InitState)
193      // Generate the root.
194      generateNode(StartLoc, SubEng.getInitialState(L), 0);
195    else
196      generateNode(StartLoc, InitState, 0);
197  }
198
199  // Check if we have a steps limit
200  bool UnlimitedSteps = Steps == 0;
201
202  while (WList->hasWork()) {
203    if (!UnlimitedSteps) {
204      if (Steps == 0) {
205        NumReachedMaxSteps++;
206        break;
207      }
208      --Steps;
209    }
210
211    const WorkListUnit& WU = WList->dequeue();
212
213    // Set the current block counter.
214    WList->setBlockCounter(WU.getBlockCounter());
215
216    // Retrieve the node.
217    ExplodedNode *Node = WU.getNode();
218
219    dispatchWorkItem(Node, Node->getLocation(), WU);
220  }
221  SubEng.processEndWorklist(hasWorkRemaining());
222  return WList->hasWork();
223}
224
225void CoreEngine::dispatchWorkItem(ExplodedNode* Pred, ProgramPoint Loc,
226                                  const WorkListUnit& WU) {
227  // Dispatch on the location type.
228  switch (Loc.getKind()) {
229    case ProgramPoint::BlockEdgeKind:
230      HandleBlockEdge(cast<BlockEdge>(Loc), Pred);
231      break;
232
233    case ProgramPoint::BlockEntranceKind:
234      HandleBlockEntrance(cast<BlockEntrance>(Loc), Pred);
235      break;
236
237    case ProgramPoint::BlockExitKind:
238      assert (false && "BlockExit location never occur in forward analysis.");
239      break;
240
241    case ProgramPoint::CallEnterKind: {
242      CallEnter CEnter = cast<CallEnter>(Loc);
243      if (AnalyzedCallees)
244        if (const CallExpr* CE =
245            dyn_cast_or_null<CallExpr>(CEnter.getCallExpr()))
246          if (const Decl *CD = CE->getCalleeDecl())
247            AnalyzedCallees->insert(CD);
248      SubEng.processCallEnter(CEnter, Pred);
249      break;
250    }
251
252    case ProgramPoint::CallExitKind:
253      SubEng.processCallExit(Pred);
254      break;
255
256    case ProgramPoint::EpsilonKind: {
257      assert(Pred->hasSinglePred() &&
258             "Assume epsilon has exactly one predecessor by construction");
259      ExplodedNode *PNode = Pred->getFirstPred();
260      dispatchWorkItem(Pred, PNode->getLocation(), WU);
261      break;
262    }
263    default:
264      assert(isa<PostStmt>(Loc) ||
265             isa<PostInitializer>(Loc));
266      HandlePostStmt(WU.getBlock(), WU.getIndex(), Pred);
267      break;
268  }
269}
270
271bool CoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L,
272                                                 unsigned Steps,
273                                                 ProgramStateRef InitState,
274                                                 ExplodedNodeSet &Dst) {
275  bool DidNotFinish = ExecuteWorkList(L, Steps, InitState);
276  for (ExplodedGraph::eop_iterator I = G->eop_begin(),
277                                   E = G->eop_end(); I != E; ++I) {
278    Dst.Add(*I);
279  }
280  return DidNotFinish;
281}
282
283void CoreEngine::HandleBlockEdge(const BlockEdge &L, ExplodedNode *Pred) {
284
285  const CFGBlock *Blk = L.getDst();
286  NodeBuilderContext BuilderCtx(*this, Blk, Pred);
287
288  // Mark this block as visited.
289  const LocationContext *LC = Pred->getLocationContext();
290  FunctionSummaries->markVisitedBasicBlock(Blk->getBlockID(),
291                                           LC->getDecl(),
292                                           LC->getCFG()->getNumBlockIDs());
293
294  // Check if we are entering the EXIT block.
295  if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
296
297    assert (L.getLocationContext()->getCFG()->getExit().size() == 0
298            && "EXIT block cannot contain Stmts.");
299
300    // Process the final state transition.
301    SubEng.processEndOfFunction(BuilderCtx);
302
303    // This path is done. Don't enqueue any more nodes.
304    return;
305  }
306
307  // Call into the SubEngine to process entering the CFGBlock.
308  ExplodedNodeSet dstNodes;
309  BlockEntrance BE(Blk, Pred->getLocationContext());
310  NodeBuilderWithSinks nodeBuilder(Pred, dstNodes, BuilderCtx, BE);
311  SubEng.processCFGBlockEntrance(L, nodeBuilder);
312
313  // Auto-generate a node.
314  if (!nodeBuilder.hasGeneratedNodes()) {
315    nodeBuilder.generateNode(Pred->State, Pred);
316  }
317
318  // Enqueue nodes onto the worklist.
319  enqueue(dstNodes);
320}
321
322void CoreEngine::HandleBlockEntrance(const BlockEntrance &L,
323                                       ExplodedNode *Pred) {
324
325  // Increment the block counter.
326  const LocationContext *LC = Pred->getLocationContext();
327  unsigned BlockId = L.getBlock()->getBlockID();
328  BlockCounter Counter = WList->getBlockCounter();
329  Counter = BCounterFactory.IncrementCount(Counter, LC->getCurrentStackFrame(),
330                                           BlockId);
331  WList->setBlockCounter(Counter);
332
333  // Process the entrance of the block.
334  if (CFGElement E = L.getFirstElement()) {
335    NodeBuilderContext Ctx(*this, L.getBlock(), Pred);
336    SubEng.processCFGElement(E, Pred, 0, &Ctx);
337  }
338  else
339    HandleBlockExit(L.getBlock(), Pred);
340}
341
342void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) {
343
344  if (const Stmt *Term = B->getTerminator()) {
345    switch (Term->getStmtClass()) {
346      default:
347        llvm_unreachable("Analysis for this terminator not implemented.");
348
349      case Stmt::BinaryOperatorClass: // '&&' and '||'
350        HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
351        return;
352
353      case Stmt::BinaryConditionalOperatorClass:
354      case Stmt::ConditionalOperatorClass:
355        HandleBranch(cast<AbstractConditionalOperator>(Term)->getCond(),
356                     Term, B, Pred);
357        return;
358
359        // FIXME: Use constant-folding in CFG construction to simplify this
360        // case.
361
362      case Stmt::ChooseExprClass:
363        HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
364        return;
365
366      case Stmt::CXXTryStmtClass: {
367        // Generate a node for each of the successors.
368        // Our logic for EH analysis can certainly be improved.
369        for (CFGBlock::const_succ_iterator it = B->succ_begin(),
370             et = B->succ_end(); it != et; ++it) {
371          if (const CFGBlock *succ = *it) {
372            generateNode(BlockEdge(B, succ, Pred->getLocationContext()),
373                         Pred->State, Pred);
374          }
375        }
376        return;
377      }
378
379      case Stmt::DoStmtClass:
380        HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
381        return;
382
383      case Stmt::CXXForRangeStmtClass:
384        HandleBranch(cast<CXXForRangeStmt>(Term)->getCond(), Term, B, Pred);
385        return;
386
387      case Stmt::ForStmtClass:
388        HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
389        return;
390
391      case Stmt::ContinueStmtClass:
392      case Stmt::BreakStmtClass:
393      case Stmt::GotoStmtClass:
394        break;
395
396      case Stmt::IfStmtClass:
397        HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
398        return;
399
400      case Stmt::IndirectGotoStmtClass: {
401        // Only 1 successor: the indirect goto dispatch block.
402        assert (B->succ_size() == 1);
403
404        IndirectGotoNodeBuilder
405           builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
406                   *(B->succ_begin()), this);
407
408        SubEng.processIndirectGoto(builder);
409        return;
410      }
411
412      case Stmt::ObjCForCollectionStmtClass: {
413        // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
414        //
415        //  (1) inside a basic block, which represents the binding of the
416        //      'element' variable to a value.
417        //  (2) in a terminator, which represents the branch.
418        //
419        // For (1), subengines will bind a value (i.e., 0 or 1) indicating
420        // whether or not collection contains any more elements.  We cannot
421        // just test to see if the element is nil because a container can
422        // contain nil elements.
423        HandleBranch(Term, Term, B, Pred);
424        return;
425      }
426
427      case Stmt::SwitchStmtClass: {
428        SwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
429                                    this);
430
431        SubEng.processSwitch(builder);
432        return;
433      }
434
435      case Stmt::WhileStmtClass:
436        HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
437        return;
438    }
439  }
440
441  assert (B->succ_size() == 1 &&
442          "Blocks with no terminator should have at most 1 successor.");
443
444  generateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
445               Pred->State, Pred);
446}
447
448void CoreEngine::HandleBranch(const Stmt *Cond, const Stmt *Term,
449                                const CFGBlock * B, ExplodedNode *Pred) {
450  assert(B->succ_size() == 2);
451  NodeBuilderContext Ctx(*this, B, Pred);
452  ExplodedNodeSet Dst;
453  SubEng.processBranch(Cond, Term, Ctx, Pred, Dst,
454                       *(B->succ_begin()), *(B->succ_begin()+1));
455  // Enqueue the new frontier onto the worklist.
456  enqueue(Dst);
457}
458
459void CoreEngine::HandlePostStmt(const CFGBlock *B, unsigned StmtIdx,
460                                  ExplodedNode *Pred) {
461  assert(B);
462  assert(!B->empty());
463
464  if (StmtIdx == B->size())
465    HandleBlockExit(B, Pred);
466  else {
467    NodeBuilderContext Ctx(*this, B, Pred);
468    SubEng.processCFGElement((*B)[StmtIdx], Pred, StmtIdx, &Ctx);
469  }
470}
471
472/// generateNode - Utility method to generate nodes, hook up successors,
473///  and add nodes to the worklist.
474void CoreEngine::generateNode(const ProgramPoint &Loc,
475                              ProgramStateRef State,
476                              ExplodedNode *Pred) {
477
478  bool IsNew;
479  ExplodedNode *Node = G->getNode(Loc, State, false, &IsNew);
480
481  if (Pred)
482    Node->addPredecessor(Pred, *G);  // Link 'Node' with its predecessor.
483  else {
484    assert (IsNew);
485    G->addRoot(Node);  // 'Node' has no predecessor.  Make it a root.
486  }
487
488  // Only add 'Node' to the worklist if it was freshly generated.
489  if (IsNew) WList->enqueue(Node);
490}
491
492void CoreEngine::enqueueStmtNode(ExplodedNode *N,
493                                 const CFGBlock *Block, unsigned Idx) {
494  assert(Block);
495  assert (!N->isSink());
496
497  // Check if this node entered a callee.
498  if (isa<CallEnter>(N->getLocation())) {
499    // Still use the index of the CallExpr. It's needed to create the callee
500    // StackFrameContext.
501    WList->enqueue(N, Block, Idx);
502    return;
503  }
504
505  // Do not create extra nodes. Move to the next CFG element.
506  if (isa<PostInitializer>(N->getLocation())) {
507    WList->enqueue(N, Block, Idx+1);
508    return;
509  }
510
511  if (isa<EpsilonPoint>(N->getLocation())) {
512    WList->enqueue(N, Block, Idx);
513    return;
514  }
515
516  const CFGStmt *CS = (*Block)[Idx].getAs<CFGStmt>();
517  const Stmt *St = CS ? CS->getStmt() : 0;
518  PostStmt Loc(St, N->getLocationContext());
519
520  if (Loc == N->getLocation()) {
521    // Note: 'N' should be a fresh node because otherwise it shouldn't be
522    // a member of Deferred.
523    WList->enqueue(N, Block, Idx+1);
524    return;
525  }
526
527  bool IsNew;
528  ExplodedNode *Succ = G->getNode(Loc, N->getState(), false, &IsNew);
529  Succ->addPredecessor(N, *G);
530
531  if (IsNew)
532    WList->enqueue(Succ, Block, Idx+1);
533}
534
535ExplodedNode *CoreEngine::generateCallExitNode(ExplodedNode *N) {
536  // Create a CallExit node and enqueue it.
537  const StackFrameContext *LocCtx
538                         = cast<StackFrameContext>(N->getLocationContext());
539  const Stmt *CE = LocCtx->getCallSite();
540
541  // Use the the callee location context.
542  CallExit Loc(CE, LocCtx);
543
544  bool isNew;
545  ExplodedNode *Node = G->getNode(Loc, N->getState(), false, &isNew);
546  Node->addPredecessor(N, *G);
547  return isNew ? Node : 0;
548}
549
550
551void CoreEngine::enqueue(ExplodedNodeSet &Set) {
552  for (ExplodedNodeSet::iterator I = Set.begin(),
553                                 E = Set.end(); I != E; ++I) {
554    WList->enqueue(*I);
555  }
556}
557
558void CoreEngine::enqueue(ExplodedNodeSet &Set,
559                         const CFGBlock *Block, unsigned Idx) {
560  for (ExplodedNodeSet::iterator I = Set.begin(),
561                                 E = Set.end(); I != E; ++I) {
562    enqueueStmtNode(*I, Block, Idx);
563  }
564}
565
566void CoreEngine::enqueueEndOfFunction(ExplodedNodeSet &Set) {
567  for (ExplodedNodeSet::iterator I = Set.begin(), E = Set.end(); I != E; ++I) {
568    ExplodedNode *N = *I;
569    // If we are in an inlined call, generate CallExit node.
570    if (N->getLocationContext()->getParent()) {
571      N = generateCallExitNode(N);
572      if (N)
573        WList->enqueue(N);
574    } else {
575      G->addEndOfPath(N);
576      NumPathsExplored++;
577    }
578  }
579}
580
581
582void NodeBuilder::anchor() { }
583
584ExplodedNode* NodeBuilder::generateNodeImpl(const ProgramPoint &Loc,
585                                            ProgramStateRef State,
586                                            ExplodedNode *FromN,
587                                            bool MarkAsSink) {
588  HasGeneratedNodes = true;
589  bool IsNew;
590  ExplodedNode *N = C.Eng.G->getNode(Loc, State, MarkAsSink, &IsNew);
591  N->addPredecessor(FromN, *C.Eng.G);
592  Frontier.erase(FromN);
593
594  if (!IsNew)
595    return 0;
596
597  if (!MarkAsSink)
598    Frontier.Add(N);
599
600  return N;
601}
602
603void NodeBuilderWithSinks::anchor() { }
604
605StmtNodeBuilder::~StmtNodeBuilder() {
606  if (EnclosingBldr)
607    for (ExplodedNodeSet::iterator I = Frontier.begin(),
608                                   E = Frontier.end(); I != E; ++I )
609      EnclosingBldr->addNodes(*I);
610}
611
612void BranchNodeBuilder::anchor() { }
613
614ExplodedNode *BranchNodeBuilder::generateNode(ProgramStateRef State,
615                                              bool branch,
616                                              ExplodedNode *NodePred) {
617  // If the branch has been marked infeasible we should not generate a node.
618  if (!isFeasible(branch))
619    return NULL;
620
621  ProgramPoint Loc = BlockEdge(C.Block, branch ? DstT:DstF,
622                               NodePred->getLocationContext());
623  ExplodedNode *Succ = generateNodeImpl(Loc, State, NodePred);
624  return Succ;
625}
626
627ExplodedNode*
628IndirectGotoNodeBuilder::generateNode(const iterator &I,
629                                      ProgramStateRef St,
630                                      bool IsSink) {
631  bool IsNew;
632  ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
633                                      Pred->getLocationContext()), St,
634                                      IsSink, &IsNew);
635  Succ->addPredecessor(Pred, *Eng.G);
636
637  if (!IsNew)
638    return 0;
639
640  if (!IsSink)
641    Eng.WList->enqueue(Succ);
642
643  return Succ;
644}
645
646
647ExplodedNode*
648SwitchNodeBuilder::generateCaseStmtNode(const iterator &I,
649                                        ProgramStateRef St) {
650
651  bool IsNew;
652  ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
653                                      Pred->getLocationContext()), St,
654                                      false, &IsNew);
655  Succ->addPredecessor(Pred, *Eng.G);
656  if (!IsNew)
657    return 0;
658
659  Eng.WList->enqueue(Succ);
660  return Succ;
661}
662
663
664ExplodedNode*
665SwitchNodeBuilder::generateDefaultCaseNode(ProgramStateRef St,
666                                           bool IsSink) {
667  // Get the block for the default case.
668  assert(Src->succ_rbegin() != Src->succ_rend());
669  CFGBlock *DefaultBlock = *Src->succ_rbegin();
670
671  // Sanity check for default blocks that are unreachable and not caught
672  // by earlier stages.
673  if (!DefaultBlock)
674    return NULL;
675
676  bool IsNew;
677  ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
678                                      Pred->getLocationContext()), St,
679                                      IsSink, &IsNew);
680  Succ->addPredecessor(Pred, *Eng.G);
681
682  if (!IsNew)
683    return 0;
684
685  if (!IsSink)
686    Eng.WList->enqueue(Succ);
687
688  return Succ;
689}
690