CoreEngine.h revision 2d950b15b2b2b650b102ecf0c6b50b45e0cb6a8a
1//==- CoreEngine.h - 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.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_GR_COREENGINE
16#define LLVM_CLANG_GR_COREENGINE
17
18#include "clang/AST/Expr.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
20#include "clang/StaticAnalyzer/Core/PathSensitive/WorkList.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/BlockCounter.h"
22#include "llvm/ADT/OwningPtr.h"
23
24namespace clang {
25
26class ProgramPointTag;
27
28namespace ento {
29
30class NodeBuilder;
31
32//===----------------------------------------------------------------------===//
33/// CoreEngine - Implements the core logic of the graph-reachability
34///   analysis. It traverses the CFG and generates the ExplodedGraph.
35///   Program "states" are treated as opaque void pointers.
36///   The template class CoreEngine (which subclasses CoreEngine)
37///   provides the matching component to the engine that knows the actual types
38///   for states.  Note that this engine only dispatches to transfer functions
39///   at the statement and block-level.  The analyses themselves must implement
40///   any transfer function logic and the sub-expression level (if any).
41class CoreEngine {
42  friend struct NodeBuilderContext;
43  friend class NodeBuilder;
44  friend class CommonNodeBuilder;
45  friend class IndirectGotoNodeBuilder;
46  friend class SwitchNodeBuilder;
47  friend class EndOfFunctionNodeBuilder;
48  friend class CallEnterNodeBuilder;
49  friend class CallExitNodeBuilder;
50
51public:
52  typedef std::vector<std::pair<BlockEdge, const ExplodedNode*> >
53            BlocksExhausted;
54
55  typedef std::vector<std::pair<const CFGBlock*, const ExplodedNode*> >
56            BlocksAborted;
57
58private:
59
60  SubEngine& SubEng;
61
62  /// G - The simulation graph.  Each node is a (location,state) pair.
63  llvm::OwningPtr<ExplodedGraph> G;
64
65  /// WList - A set of queued nodes that need to be processed by the
66  ///  worklist algorithm.  It is up to the implementation of WList to decide
67  ///  the order that nodes are processed.
68  WorkList* WList;
69
70  /// BCounterFactory - A factory object for created BlockCounter objects.
71  ///   These are used to record for key nodes in the ExplodedGraph the
72  ///   number of times different CFGBlocks have been visited along a path.
73  BlockCounter::Factory BCounterFactory;
74
75  /// The locations where we stopped doing work because we visited a location
76  ///  too many times.
77  BlocksExhausted blocksExhausted;
78
79  /// The locations where we stopped because the engine aborted analysis,
80  /// usually because it could not reason about something.
81  BlocksAborted blocksAborted;
82
83  void generateNode(const ProgramPoint &Loc,
84                    const ProgramState *State,
85                    ExplodedNode *Pred);
86
87  void HandleBlockEdge(const BlockEdge &E, ExplodedNode *Pred);
88  void HandleBlockEntrance(const BlockEntrance &E, ExplodedNode *Pred);
89  void HandleBlockExit(const CFGBlock *B, ExplodedNode *Pred);
90  void HandlePostStmt(const CFGBlock *B, unsigned StmtIdx, ExplodedNode *Pred);
91
92  void HandleBranch(const Stmt *Cond, const Stmt *Term, const CFGBlock *B,
93                    ExplodedNode *Pred);
94  void HandleCallEnter(const CallEnter &L, const CFGBlock *Block,
95                       unsigned Index, ExplodedNode *Pred);
96  void HandleCallExit(const CallExit &L, ExplodedNode *Pred);
97
98private:
99  CoreEngine(const CoreEngine&); // Do not implement.
100  CoreEngine& operator=(const CoreEngine&);
101
102  void enqueueStmtNode(ExplodedNode *N,
103                       const CFGBlock *Block, unsigned Idx);
104
105  ExplodedNode *generateCallExitNode(ExplodedNode *N);
106
107public:
108  /// Construct a CoreEngine object to analyze the provided CFG using
109  ///  a DFS exploration of the exploded graph.
110  CoreEngine(SubEngine& subengine)
111    : SubEng(subengine), G(new ExplodedGraph()),
112      WList(WorkList::makeBFS()),
113      BCounterFactory(G->getAllocator()) {}
114
115  /// Construct a CoreEngine object to analyze the provided CFG and to
116  ///  use the provided worklist object to execute the worklist algorithm.
117  ///  The CoreEngine object assumes ownership of 'wlist'.
118  CoreEngine(WorkList* wlist, SubEngine& subengine)
119    : SubEng(subengine), G(new ExplodedGraph()), WList(wlist),
120      BCounterFactory(G->getAllocator()) {}
121
122  ~CoreEngine() {
123    delete WList;
124  }
125
126  /// getGraph - Returns the exploded graph.
127  ExplodedGraph& getGraph() { return *G.get(); }
128
129  /// takeGraph - Returns the exploded graph.  Ownership of the graph is
130  ///  transferred to the caller.
131  ExplodedGraph* takeGraph() { return G.take(); }
132
133  /// ExecuteWorkList - Run the worklist algorithm for a maximum number of
134  ///  steps.  Returns true if there is still simulation state on the worklist.
135  bool ExecuteWorkList(const LocationContext *L, unsigned Steps,
136                       const ProgramState *InitState);
137  void ExecuteWorkListWithInitialState(const LocationContext *L,
138                                       unsigned Steps,
139                                       const ProgramState *InitState,
140                                       ExplodedNodeSet &Dst);
141
142  // Functions for external checking of whether we have unfinished work
143  bool wasBlockAborted() const { return !blocksAborted.empty(); }
144  bool wasBlocksExhausted() const { return !blocksExhausted.empty(); }
145  bool hasWorkRemaining() const { return wasBlocksExhausted() ||
146                                         WList->hasWork() ||
147                                         wasBlockAborted(); }
148
149  /// Inform the CoreEngine that a basic block was aborted because
150  /// it could not be completely analyzed.
151  void addAbortedBlock(const ExplodedNode *node, const CFGBlock *block) {
152    blocksAborted.push_back(std::make_pair(block, node));
153  }
154
155  WorkList *getWorkList() const { return WList; }
156
157  BlocksExhausted::const_iterator blocks_exhausted_begin() const {
158    return blocksExhausted.begin();
159  }
160  BlocksExhausted::const_iterator blocks_exhausted_end() const {
161    return blocksExhausted.end();
162  }
163  BlocksAborted::const_iterator blocks_aborted_begin() const {
164    return blocksAborted.begin();
165  }
166  BlocksAborted::const_iterator blocks_aborted_end() const {
167    return blocksAborted.end();
168  }
169
170  /// \brief Enqueue the given set of nodes onto the work list.
171  void enqueue(ExplodedNodeSet &Set);
172
173  /// \brief Enqueue nodes that were created as a result of processing
174  /// a statement onto the work list.
175  void enqueue(ExplodedNodeSet &Set, const CFGBlock *Block, unsigned Idx);
176
177  /// \brief enqueue the nodes corresponding to the end of function onto the
178  /// end of path / work list.
179  void enqueueEndOfFunction(ExplodedNodeSet &Set);
180};
181
182// TODO: Turn into a calss.
183struct NodeBuilderContext {
184  CoreEngine &Eng;
185  const CFGBlock *Block;
186  ExplodedNode *Pred;
187  NodeBuilderContext(CoreEngine &E, const CFGBlock *B, ExplodedNode *N)
188    : Eng(E), Block(B), Pred(N) { assert(B); assert(!N->isSink()); }
189
190  ExplodedNode *getPred() const { return Pred; }
191
192  /// \brief Return the CFGBlock associated with this builder.
193  const CFGBlock *getBlock() const { return Block; }
194
195  /// \brief Returns the number of times the current basic block has been
196  /// visited on the exploded graph path.
197  unsigned getCurrentBlockCount() const {
198    return Eng.WList->getBlockCounter().getNumVisited(
199                    Pred->getLocationContext()->getCurrentStackFrame(),
200                    Block->getBlockID());
201  }
202};
203
204/// \class NodeBuilder
205/// \brief This is the simplest builder which generates nodes in the
206/// ExplodedGraph.
207///
208/// The main benefit of the builder is that it automatically tracks the
209/// frontier nodes (or destination set). This is the set of nodes which should
210/// be propagated to the next step / builder. They are the nodes which have been
211/// added to the builder (either as the input node set or as the newly
212/// constructed nodes) but did not have any outgoing transitions added.
213class NodeBuilder {
214protected:
215  const NodeBuilderContext &C;
216
217  /// Specifies if the builder results have been finalized. For example, if it
218  /// is set to false, autotransitions are yet to be generated.
219  bool Finalized;
220  bool HasGeneratedNodes;
221  /// \brief The frontier set - a set of nodes which need to be propagated after
222  /// the builder dies.
223  ExplodedNodeSet &Frontier;
224
225  /// Checkes if the results are ready.
226  virtual bool checkResults() {
227    if (!Finalized)
228      return false;
229    return true;
230  }
231
232  bool hasNoSinksInFrontier() {
233    for (iterator I = Frontier.begin(), E = Frontier.end(); I != E; ++I) {
234      if ((*I)->isSink())
235        return false;
236    }
237    return true;
238  }
239
240  /// Allow subclasses to finalize results before result_begin() is executed.
241  virtual void finalizeResults() {}
242
243  ExplodedNode *generateNodeImpl(const ProgramPoint &PP,
244                                 const ProgramState *State,
245                                 ExplodedNode *Pred,
246                                 bool MarkAsSink = false);
247
248public:
249  NodeBuilder(ExplodedNode *SrcNode, ExplodedNodeSet &DstSet,
250              const NodeBuilderContext &Ctx, bool F = true)
251    : C(Ctx), Finalized(F), HasGeneratedNodes(false), Frontier(DstSet) {
252    Frontier.Add(SrcNode);
253  }
254
255  NodeBuilder(const ExplodedNodeSet &SrcSet, ExplodedNodeSet &DstSet,
256              const NodeBuilderContext &Ctx, bool F = true)
257    : C(Ctx), Finalized(F), HasGeneratedNodes(false), Frontier(DstSet) {
258    Frontier.insert(SrcSet);
259    assert(hasNoSinksInFrontier());
260  }
261
262  virtual ~NodeBuilder() {}
263
264  /// \brief Generates a node in the ExplodedGraph.
265  ///
266  /// When a node is marked as sink, the exploration from the node is stopped -
267  /// the node becomes the last node on the path.
268  ExplodedNode *generateNode(const ProgramPoint &PP,
269                             const ProgramState *State,
270                             ExplodedNode *Pred,
271                             bool MarkAsSink = false) {
272    return generateNodeImpl(PP, State, Pred, MarkAsSink);
273  }
274
275  const ExplodedNodeSet &getResults() {
276    finalizeResults();
277    assert(checkResults());
278    return Frontier;
279  }
280
281  typedef ExplodedNodeSet::iterator iterator;
282  /// \brief Iterators through the results frontier.
283  inline iterator begin() {
284    finalizeResults();
285    assert(checkResults());
286    return Frontier.begin();
287  }
288  inline iterator end() {
289    finalizeResults();
290    return Frontier.end();
291  }
292
293  const NodeBuilderContext &getContext() { return C; }
294  bool hasGeneratedNodes() { return HasGeneratedNodes; }
295
296  void takeNodes(const ExplodedNodeSet &S) {
297    for (ExplodedNodeSet::iterator I = S.begin(), E = S.end(); I != E; ++I )
298      Frontier.erase(*I);
299  }
300  void takeNodes(ExplodedNode *N) { Frontier.erase(N); }
301  void addNodes(const ExplodedNodeSet &S) { Frontier.insert(S); }
302  void addNodes(ExplodedNode *N) { Frontier.Add(N); }
303};
304
305/// \class NodeBuilderWithSinks
306/// \brief This node builder keeps track of the generated sink nodes.
307class NodeBuilderWithSinks: public NodeBuilder {
308protected:
309  SmallVector<ExplodedNode*, 2> sinksGenerated;
310  ProgramPoint &Location;
311
312public:
313  NodeBuilderWithSinks(ExplodedNode *Pred, ExplodedNodeSet &DstSet,
314                       const NodeBuilderContext &Ctx, ProgramPoint &L)
315    : NodeBuilder(Pred, DstSet, Ctx), Location(L) {}
316  ExplodedNode *generateNode(const ProgramState *State,
317                             ExplodedNode *Pred,
318                             const ProgramPointTag *Tag = 0,
319                             bool MarkAsSink = false) {
320    ProgramPoint LocalLoc = (Tag ? Location.withTag(Tag): Location);
321
322    ExplodedNode *N = generateNodeImpl(LocalLoc, State, Pred, MarkAsSink);
323    if (N && N->isSink())
324      sinksGenerated.push_back(N);
325    return N;
326  }
327
328  const SmallVectorImpl<ExplodedNode*> &getSinks() const {
329    return sinksGenerated;
330  }
331};
332
333/// \class StmtNodeBuilder
334/// \brief This builder class is useful for generating nodes that resulted from
335/// visiting a statement. The main difference from it's parent NodeBuilder is
336/// that it creates a statement specific ProgramPoint.
337class StmtNodeBuilder: public NodeBuilder {
338  NodeBuilder *EnclosingBldr;
339public:
340
341  /// \brief Constructs a StmtNodeBuilder. If the builder is going to process
342  /// nodes currently owned by another builder(with larger scope), use
343  /// Enclosing builder to transfer ownership.
344  StmtNodeBuilder(ExplodedNode *SrcNode, ExplodedNodeSet &DstSet,
345                      const NodeBuilderContext &Ctx, NodeBuilder *Enclosing = 0)
346    : NodeBuilder(SrcNode, DstSet, Ctx), EnclosingBldr(Enclosing) {
347    if (EnclosingBldr)
348      EnclosingBldr->takeNodes(SrcNode);
349  }
350
351  StmtNodeBuilder(ExplodedNodeSet &SrcSet, ExplodedNodeSet &DstSet,
352                      const NodeBuilderContext &Ctx, NodeBuilder *Enclosing = 0)
353    : NodeBuilder(SrcSet, DstSet, Ctx), EnclosingBldr(Enclosing) {
354    if (EnclosingBldr)
355      for (ExplodedNodeSet::iterator I = SrcSet.begin(),
356                                     E = SrcSet.end(); I != E; ++I )
357        EnclosingBldr->takeNodes(*I);
358  }
359
360  virtual ~StmtNodeBuilder();
361
362  ExplodedNode *generateNode(const Stmt *S,
363                             ExplodedNode *Pred,
364                             const ProgramState *St,
365                             bool MarkAsSink = false,
366                             const ProgramPointTag *tag = 0,
367                             ProgramPoint::Kind K = ProgramPoint::PostStmtKind){
368    const ProgramPoint &L = ProgramPoint::getProgramPoint(S, K,
369                                  Pred->getLocationContext(), tag);
370    return generateNodeImpl(L, St, Pred, MarkAsSink);
371  }
372
373  ExplodedNode *generateNode(const ProgramPoint &PP,
374                             ExplodedNode *Pred,
375                             const ProgramState *State,
376                             bool MarkAsSink = false) {
377    return generateNodeImpl(PP, State, Pred, MarkAsSink);
378  }
379};
380
381/// \brief BranchNodeBuilder is responsible for constructing the nodes
382/// corresponding to the two branches of the if statement - true and false.
383class BranchNodeBuilder: public NodeBuilder {
384  const CFGBlock *DstT;
385  const CFGBlock *DstF;
386
387  bool InFeasibleTrue;
388  bool InFeasibleFalse;
389
390public:
391  BranchNodeBuilder(ExplodedNode *SrcNode, ExplodedNodeSet &DstSet,
392                    const NodeBuilderContext &C,
393                    const CFGBlock *dstT, const CFGBlock *dstF)
394  : NodeBuilder(SrcNode, DstSet, C), DstT(dstT), DstF(dstF),
395    InFeasibleTrue(!DstT), InFeasibleFalse(!DstF) {
396    // The branch node builder does not generate autotransitions.
397    // If there are no successors it means that both branches are infeasible.
398    takeNodes(SrcNode);
399  }
400
401  BranchNodeBuilder(const ExplodedNodeSet &SrcSet, ExplodedNodeSet &DstSet,
402                    const NodeBuilderContext &C,
403                    const CFGBlock *dstT, const CFGBlock *dstF)
404  : NodeBuilder(SrcSet, DstSet, C), DstT(dstT), DstF(dstF),
405    InFeasibleTrue(!DstT), InFeasibleFalse(!DstF) {
406    takeNodes(SrcSet);
407  }
408
409  ExplodedNode *generateNode(const ProgramState *State, bool branch,
410                             ExplodedNode *Pred);
411
412  const CFGBlock *getTargetBlock(bool branch) const {
413    return branch ? DstT : DstF;
414  }
415
416  void markInfeasible(bool branch) {
417    if (branch)
418      InFeasibleTrue = true;
419    else
420      InFeasibleFalse = true;
421  }
422
423  bool isFeasible(bool branch) {
424    return branch ? !InFeasibleTrue : !InFeasibleFalse;
425  }
426};
427
428class IndirectGotoNodeBuilder {
429  CoreEngine& Eng;
430  const CFGBlock *Src;
431  const CFGBlock &DispatchBlock;
432  const Expr *E;
433  ExplodedNode *Pred;
434
435public:
436  IndirectGotoNodeBuilder(ExplodedNode *pred, const CFGBlock *src,
437                    const Expr *e, const CFGBlock *dispatch, CoreEngine* eng)
438    : Eng(*eng), Src(src), DispatchBlock(*dispatch), E(e), Pred(pred) {}
439
440  class iterator {
441    CFGBlock::const_succ_iterator I;
442
443    friend class IndirectGotoNodeBuilder;
444    iterator(CFGBlock::const_succ_iterator i) : I(i) {}
445  public:
446
447    iterator &operator++() { ++I; return *this; }
448    bool operator!=(const iterator &X) const { return I != X.I; }
449
450    const LabelDecl *getLabel() const {
451      return llvm::cast<LabelStmt>((*I)->getLabel())->getDecl();
452    }
453
454    const CFGBlock *getBlock() const {
455      return *I;
456    }
457  };
458
459  iterator begin() { return iterator(DispatchBlock.succ_begin()); }
460  iterator end() { return iterator(DispatchBlock.succ_end()); }
461
462  ExplodedNode *generateNode(const iterator &I,
463                             const ProgramState *State,
464                             bool isSink = false);
465
466  const Expr *getTarget() const { return E; }
467
468  const ProgramState *getState() const { return Pred->State; }
469};
470
471class SwitchNodeBuilder {
472  CoreEngine& Eng;
473  const CFGBlock *Src;
474  const Expr *Condition;
475  ExplodedNode *Pred;
476
477public:
478  SwitchNodeBuilder(ExplodedNode *pred, const CFGBlock *src,
479                    const Expr *condition, CoreEngine* eng)
480  : Eng(*eng), Src(src), Condition(condition), Pred(pred) {}
481
482  class iterator {
483    CFGBlock::const_succ_reverse_iterator I;
484
485    friend class SwitchNodeBuilder;
486    iterator(CFGBlock::const_succ_reverse_iterator i) : I(i) {}
487
488  public:
489    iterator &operator++() { ++I; return *this; }
490    bool operator!=(const iterator &X) const { return I != X.I; }
491    bool operator==(const iterator &X) const { return I == X.I; }
492
493    const CaseStmt *getCase() const {
494      return llvm::cast<CaseStmt>((*I)->getLabel());
495    }
496
497    const CFGBlock *getBlock() const {
498      return *I;
499    }
500  };
501
502  iterator begin() { return iterator(Src->succ_rbegin()+1); }
503  iterator end() { return iterator(Src->succ_rend()); }
504
505  const SwitchStmt *getSwitch() const {
506    return llvm::cast<SwitchStmt>(Src->getTerminator());
507  }
508
509  ExplodedNode *generateCaseStmtNode(const iterator &I,
510                                     const ProgramState *State);
511
512  ExplodedNode *generateDefaultCaseNode(const ProgramState *State,
513                                        bool isSink = false);
514
515  const Expr *getCondition() const { return Condition; }
516
517  const ProgramState *getState() const { return Pred->State; }
518};
519
520class CallEnterNodeBuilder {
521  CoreEngine &Eng;
522
523  const ExplodedNode *Pred;
524
525  // The call site. For implicit automatic object dtor, this is the trigger
526  // statement.
527  const Stmt *CE;
528
529  // The context of the callee.
530  const StackFrameContext *CalleeCtx;
531
532  // The parent block of the CallExpr.
533  const CFGBlock *Block;
534
535  // The CFGBlock index of the CallExpr.
536  unsigned Index;
537
538public:
539  CallEnterNodeBuilder(CoreEngine &eng, const ExplodedNode *pred,
540                         const Stmt *s, const StackFrameContext *callee,
541                         const CFGBlock *blk, unsigned idx)
542    : Eng(eng), Pred(pred), CE(s), CalleeCtx(callee), Block(blk), Index(idx) {}
543
544  const ProgramState *getState() const { return Pred->getState(); }
545
546  const LocationContext *getLocationContext() const {
547    return Pred->getLocationContext();
548  }
549
550  const Stmt *getCallExpr() const { return CE; }
551
552  const StackFrameContext *getCalleeContext() const { return CalleeCtx; }
553
554  const CFGBlock *getBlock() const { return Block; }
555
556  unsigned getIndex() const { return Index; }
557
558  void generateNode(const ProgramState *state);
559};
560
561class CallExitNodeBuilder {
562  CoreEngine &Eng;
563  const ExplodedNode *Pred;
564
565public:
566  CallExitNodeBuilder(CoreEngine &eng, const ExplodedNode *pred)
567    : Eng(eng), Pred(pred) {}
568
569  const ExplodedNode *getPredecessor() const { return Pred; }
570
571  const ProgramState *getState() const { return Pred->getState(); }
572
573  void generateNode(const ProgramState *state);
574};
575
576} // end GR namespace
577
578} // end clang namespace
579
580#endif
581