1 //===- CoreEngine.h - 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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_COREENGINE_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_COREENGINE_H
18 #include "clang/AST/Stmt.h"
19 #include "clang/Analysis/AnalysisDeclContext.h"
20 #include "clang/Analysis/CFG.h"
21 #include "clang/Analysis/ProgramPoint.h"
22 #include "clang/Basic/LLVM.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/BlockCounter.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/WorkList.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/Support/Casting.h"
36 class AnalyzerOptions;
37 class CXXBindTemporaryExpr;
43 class FunctionSummariesTy;
46 //===----------------------------------------------------------------------===//
47 /// CoreEngine - Implements the core logic of the graph-reachability
48 /// analysis. It traverses the CFG and generates the ExplodedGraph.
49 /// Program "states" are treated as opaque void pointers.
50 /// The template class CoreEngine (which subclasses CoreEngine)
51 /// provides the matching component to the engine that knows the actual types
52 /// for states. Note that this engine only dispatches to transfer functions
53 /// at the statement and block-level. The analyses themselves must implement
54 /// any transfer function logic and the sub-expression level (if any).
56 friend class CommonNodeBuilder;
57 friend class EndOfFunctionNodeBuilder;
58 friend class ExprEngine;
59 friend class IndirectGotoNodeBuilder;
60 friend class NodeBuilder;
61 friend struct NodeBuilderContext;
62 friend class SwitchNodeBuilder;
65 using BlocksExhausted =
66 std::vector<std::pair<BlockEdge, const ExplodedNode *>>;
69 std::vector<std::pair<const CFGBlock *, const ExplodedNode *>>;
74 /// G - The simulation graph. Each node is a (location,state) pair.
75 mutable ExplodedGraph G;
77 /// WList - A set of queued nodes that need to be processed by the
78 /// worklist algorithm. It is up to the implementation of WList to decide
79 /// the order that nodes are processed.
80 std::unique_ptr<WorkList> WList;
82 /// BCounterFactory - A factory object for created BlockCounter objects.
83 /// These are used to record for key nodes in the ExplodedGraph the
84 /// number of times different CFGBlocks have been visited along a path.
85 BlockCounter::Factory BCounterFactory;
87 /// The locations where we stopped doing work because we visited a location
89 BlocksExhausted blocksExhausted;
91 /// The locations where we stopped because the engine aborted analysis,
92 /// usually because it could not reason about something.
93 BlocksAborted blocksAborted;
95 /// The information about functions shared by the whole translation unit.
96 /// (This data is owned by AnalysisConsumer.)
97 FunctionSummariesTy *FunctionSummaries;
99 void generateNode(const ProgramPoint &Loc,
100 ProgramStateRef State,
103 void HandleBlockEdge(const BlockEdge &E, ExplodedNode *Pred);
104 void HandleBlockEntrance(const BlockEntrance &E, ExplodedNode *Pred);
105 void HandleBlockExit(const CFGBlock *B, ExplodedNode *Pred);
107 void HandleCallEnter(const CallEnter &CE, ExplodedNode *Pred);
109 void HandlePostStmt(const CFGBlock *B, unsigned StmtIdx, ExplodedNode *Pred);
111 void HandleBranch(const Stmt *Cond, const Stmt *Term, const CFGBlock *B,
113 void HandleCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE,
114 const CFGBlock *B, ExplodedNode *Pred);
116 /// Handle conditional logic for running static initializers.
117 void HandleStaticInit(const DeclStmt *DS, const CFGBlock *B,
121 ExplodedNode *generateCallExitBeginNode(ExplodedNode *N,
122 const ReturnStmt *RS);
125 /// Construct a CoreEngine object to analyze the provided CFG.
126 CoreEngine(SubEngine &subengine,
127 FunctionSummariesTy *FS,
128 AnalyzerOptions &Opts);
130 CoreEngine(const CoreEngine &) = delete;
131 CoreEngine &operator=(const CoreEngine &) = delete;
133 /// getGraph - Returns the exploded graph.
134 ExplodedGraph &getGraph() { return G; }
136 /// ExecuteWorkList - Run the worklist algorithm for a maximum number of
137 /// steps. Returns true if there is still simulation state on the worklist.
138 bool ExecuteWorkList(const LocationContext *L, unsigned Steps,
139 ProgramStateRef InitState);
141 /// Returns true if there is still simulation state on the worklist.
142 bool ExecuteWorkListWithInitialState(const LocationContext *L,
144 ProgramStateRef InitState,
145 ExplodedNodeSet &Dst);
147 /// Dispatch the work list item based on the given location information.
148 /// Use Pred parameter as the predecessor state.
149 void dispatchWorkItem(ExplodedNode* Pred, ProgramPoint Loc,
150 const WorkListUnit& WU);
152 // Functions for external checking of whether we have unfinished work
153 bool wasBlockAborted() const { return !blocksAborted.empty(); }
154 bool wasBlocksExhausted() const { return !blocksExhausted.empty(); }
155 bool hasWorkRemaining() const { return wasBlocksExhausted() ||
159 /// Inform the CoreEngine that a basic block was aborted because
160 /// it could not be completely analyzed.
161 void addAbortedBlock(const ExplodedNode *node, const CFGBlock *block) {
162 blocksAborted.push_back(std::make_pair(block, node));
165 WorkList *getWorkList() const { return WList.get(); }
167 BlocksExhausted::const_iterator blocks_exhausted_begin() const {
168 return blocksExhausted.begin();
171 BlocksExhausted::const_iterator blocks_exhausted_end() const {
172 return blocksExhausted.end();
175 BlocksAborted::const_iterator blocks_aborted_begin() const {
176 return blocksAborted.begin();
179 BlocksAborted::const_iterator blocks_aborted_end() const {
180 return blocksAborted.end();
183 /// Enqueue the given set of nodes onto the work list.
184 void enqueue(ExplodedNodeSet &Set);
186 /// Enqueue nodes that were created as a result of processing
187 /// a statement onto the work list.
188 void enqueue(ExplodedNodeSet &Set, const CFGBlock *Block, unsigned Idx);
190 /// enqueue the nodes corresponding to the end of function onto the
191 /// end of path / work list.
192 void enqueueEndOfFunction(ExplodedNodeSet &Set, const ReturnStmt *RS);
194 /// Enqueue a single node created as a result of statement processing.
195 void enqueueStmtNode(ExplodedNode *N, const CFGBlock *Block, unsigned Idx);
198 // TODO: Turn into a calss.
199 struct NodeBuilderContext {
200 const CoreEngine &Eng;
201 const CFGBlock *Block;
202 const LocationContext *LC;
204 NodeBuilderContext(const CoreEngine &E, const CFGBlock *B, ExplodedNode *N)
205 : Eng(E), Block(B), LC(N->getLocationContext()) { assert(B); }
207 /// Return the CFGBlock associated with this builder.
208 const CFGBlock *getBlock() const { return Block; }
210 /// Returns the number of times the current basic block has been
211 /// visited on the exploded graph path.
212 unsigned blockCount() const {
213 return Eng.WList->getBlockCounter().getNumVisited(
215 Block->getBlockID());
219 /// \class NodeBuilder
220 /// This is the simplest builder which generates nodes in the
223 /// The main benefit of the builder is that it automatically tracks the
224 /// frontier nodes (or destination set). This is the set of nodes which should
225 /// be propagated to the next step / builder. They are the nodes which have been
226 /// added to the builder (either as the input node set or as the newly
227 /// constructed nodes) but did not have any outgoing transitions added.
229 virtual void anchor();
232 const NodeBuilderContext &C;
234 /// Specifies if the builder results have been finalized. For example, if it
235 /// is set to false, autotransitions are yet to be generated.
238 bool HasGeneratedNodes = false;
240 /// The frontier set - a set of nodes which need to be propagated after
241 /// the builder dies.
242 ExplodedNodeSet &Frontier;
244 /// Checks if the results are ready.
245 virtual bool checkResults() {
249 bool hasNoSinksInFrontier() {
250 for (const auto I : Frontier)
256 /// Allow subclasses to finalize results before result_begin() is executed.
257 virtual void finalizeResults() {}
259 ExplodedNode *generateNodeImpl(const ProgramPoint &PP,
260 ProgramStateRef State,
262 bool MarkAsSink = false);
265 NodeBuilder(ExplodedNode *SrcNode, ExplodedNodeSet &DstSet,
266 const NodeBuilderContext &Ctx, bool F = true)
267 : C(Ctx), Finalized(F), Frontier(DstSet) {
268 Frontier.Add(SrcNode);
271 NodeBuilder(const ExplodedNodeSet &SrcSet, ExplodedNodeSet &DstSet,
272 const NodeBuilderContext &Ctx, bool F = true)
273 : C(Ctx), Finalized(F), Frontier(DstSet) {
274 Frontier.insert(SrcSet);
275 assert(hasNoSinksInFrontier());
278 virtual ~NodeBuilder() = default;
280 /// Generates a node in the ExplodedGraph.
281 ExplodedNode *generateNode(const ProgramPoint &PP,
282 ProgramStateRef State,
283 ExplodedNode *Pred) {
284 return generateNodeImpl(PP, State, Pred, false);
287 /// Generates a sink in the ExplodedGraph.
289 /// When a node is marked as sink, the exploration from the node is stopped -
290 /// the node becomes the last node on the path and certain kinds of bugs are
292 ExplodedNode *generateSink(const ProgramPoint &PP,
293 ProgramStateRef State,
294 ExplodedNode *Pred) {
295 return generateNodeImpl(PP, State, Pred, true);
298 const ExplodedNodeSet &getResults() {
300 assert(checkResults());
304 using iterator = ExplodedNodeSet::iterator;
306 /// Iterators through the results frontier.
309 assert(checkResults());
310 return Frontier.begin();
315 return Frontier.end();
318 const NodeBuilderContext &getContext() { return C; }
319 bool hasGeneratedNodes() { return HasGeneratedNodes; }
321 void takeNodes(const ExplodedNodeSet &S) {
322 for (const auto I : S)
326 void takeNodes(ExplodedNode *N) { Frontier.erase(N); }
327 void addNodes(const ExplodedNodeSet &S) { Frontier.insert(S); }
328 void addNodes(ExplodedNode *N) { Frontier.Add(N); }
331 /// \class NodeBuilderWithSinks
332 /// This node builder keeps track of the generated sink nodes.
333 class NodeBuilderWithSinks: public NodeBuilder {
334 void anchor() override;
337 SmallVector<ExplodedNode*, 2> sinksGenerated;
338 ProgramPoint &Location;
341 NodeBuilderWithSinks(ExplodedNode *Pred, ExplodedNodeSet &DstSet,
342 const NodeBuilderContext &Ctx, ProgramPoint &L)
343 : NodeBuilder(Pred, DstSet, Ctx), Location(L) {}
345 ExplodedNode *generateNode(ProgramStateRef State,
347 const ProgramPointTag *Tag = nullptr) {
348 const ProgramPoint &LocalLoc = (Tag ? Location.withTag(Tag) : Location);
349 return NodeBuilder::generateNode(LocalLoc, State, Pred);
352 ExplodedNode *generateSink(ProgramStateRef State, ExplodedNode *Pred,
353 const ProgramPointTag *Tag = nullptr) {
354 const ProgramPoint &LocalLoc = (Tag ? Location.withTag(Tag) : Location);
355 ExplodedNode *N = NodeBuilder::generateSink(LocalLoc, State, Pred);
356 if (N && N->isSink())
357 sinksGenerated.push_back(N);
361 const SmallVectorImpl<ExplodedNode*> &getSinks() const {
362 return sinksGenerated;
366 /// \class StmtNodeBuilder
367 /// This builder class is useful for generating nodes that resulted from
368 /// visiting a statement. The main difference from its parent NodeBuilder is
369 /// that it creates a statement specific ProgramPoint.
370 class StmtNodeBuilder: public NodeBuilder {
371 NodeBuilder *EnclosingBldr;
374 /// Constructs a StmtNodeBuilder. If the builder is going to process
375 /// nodes currently owned by another builder(with larger scope), use
376 /// Enclosing builder to transfer ownership.
377 StmtNodeBuilder(ExplodedNode *SrcNode, ExplodedNodeSet &DstSet,
378 const NodeBuilderContext &Ctx,
379 NodeBuilder *Enclosing = nullptr)
380 : NodeBuilder(SrcNode, DstSet, Ctx), EnclosingBldr(Enclosing) {
382 EnclosingBldr->takeNodes(SrcNode);
385 StmtNodeBuilder(ExplodedNodeSet &SrcSet, ExplodedNodeSet &DstSet,
386 const NodeBuilderContext &Ctx,
387 NodeBuilder *Enclosing = nullptr)
388 : NodeBuilder(SrcSet, DstSet, Ctx), EnclosingBldr(Enclosing) {
390 for (const auto I : SrcSet)
391 EnclosingBldr->takeNodes(I);
394 ~StmtNodeBuilder() override;
396 using NodeBuilder::generateNode;
397 using NodeBuilder::generateSink;
399 ExplodedNode *generateNode(const Stmt *S,
402 const ProgramPointTag *tag = nullptr,
403 ProgramPoint::Kind K = ProgramPoint::PostStmtKind){
404 const ProgramPoint &L = ProgramPoint::getProgramPoint(S, K,
405 Pred->getLocationContext(), tag);
406 return NodeBuilder::generateNode(L, St, Pred);
409 ExplodedNode *generateSink(const Stmt *S,
412 const ProgramPointTag *tag = nullptr,
413 ProgramPoint::Kind K = ProgramPoint::PostStmtKind){
414 const ProgramPoint &L = ProgramPoint::getProgramPoint(S, K,
415 Pred->getLocationContext(), tag);
416 return NodeBuilder::generateSink(L, St, Pred);
420 /// BranchNodeBuilder is responsible for constructing the nodes
421 /// corresponding to the two branches of the if statement - true and false.
422 class BranchNodeBuilder: public NodeBuilder {
423 const CFGBlock *DstT;
424 const CFGBlock *DstF;
427 bool InFeasibleFalse;
429 void anchor() override;
432 BranchNodeBuilder(ExplodedNode *SrcNode, ExplodedNodeSet &DstSet,
433 const NodeBuilderContext &C,
434 const CFGBlock *dstT, const CFGBlock *dstF)
435 : NodeBuilder(SrcNode, DstSet, C), DstT(dstT), DstF(dstF),
436 InFeasibleTrue(!DstT), InFeasibleFalse(!DstF) {
437 // The branch node builder does not generate autotransitions.
438 // If there are no successors it means that both branches are infeasible.
442 BranchNodeBuilder(const ExplodedNodeSet &SrcSet, ExplodedNodeSet &DstSet,
443 const NodeBuilderContext &C,
444 const CFGBlock *dstT, const CFGBlock *dstF)
445 : NodeBuilder(SrcSet, DstSet, C), DstT(dstT), DstF(dstF),
446 InFeasibleTrue(!DstT), InFeasibleFalse(!DstF) {
450 ExplodedNode *generateNode(ProgramStateRef State, bool branch,
453 const CFGBlock *getTargetBlock(bool branch) const {
454 return branch ? DstT : DstF;
457 void markInfeasible(bool branch) {
459 InFeasibleTrue = true;
461 InFeasibleFalse = true;
464 bool isFeasible(bool branch) {
465 return branch ? !InFeasibleTrue : !InFeasibleFalse;
469 class IndirectGotoNodeBuilder {
472 const CFGBlock &DispatchBlock;
477 IndirectGotoNodeBuilder(ExplodedNode *pred, const CFGBlock *src,
478 const Expr *e, const CFGBlock *dispatch, CoreEngine* eng)
479 : Eng(*eng), Src(src), DispatchBlock(*dispatch), E(e), Pred(pred) {}
482 friend class IndirectGotoNodeBuilder;
484 CFGBlock::const_succ_iterator I;
486 iterator(CFGBlock::const_succ_iterator i) : I(i) {}
489 iterator &operator++() { ++I; return *this; }
490 bool operator!=(const iterator &X) const { return I != X.I; }
492 const LabelDecl *getLabel() const {
493 return cast<LabelStmt>((*I)->getLabel())->getDecl();
496 const CFGBlock *getBlock() const {
501 iterator begin() { return iterator(DispatchBlock.succ_begin()); }
502 iterator end() { return iterator(DispatchBlock.succ_end()); }
504 ExplodedNode *generateNode(const iterator &I,
505 ProgramStateRef State,
506 bool isSink = false);
508 const Expr *getTarget() const { return E; }
510 ProgramStateRef getState() const { return Pred->State; }
512 const LocationContext *getLocationContext() const {
513 return Pred->getLocationContext();
517 class SwitchNodeBuilder {
520 const Expr *Condition;
524 SwitchNodeBuilder(ExplodedNode *pred, const CFGBlock *src,
525 const Expr *condition, CoreEngine* eng)
526 : Eng(*eng), Src(src), Condition(condition), Pred(pred) {}
529 friend class SwitchNodeBuilder;
531 CFGBlock::const_succ_reverse_iterator I;
533 iterator(CFGBlock::const_succ_reverse_iterator i) : I(i) {}
536 iterator &operator++() { ++I; return *this; }
537 bool operator!=(const iterator &X) const { return I != X.I; }
538 bool operator==(const iterator &X) const { return I == X.I; }
540 const CaseStmt *getCase() const {
541 return cast<CaseStmt>((*I)->getLabel());
544 const CFGBlock *getBlock() const {
549 iterator begin() { return iterator(Src->succ_rbegin()+1); }
550 iterator end() { return iterator(Src->succ_rend()); }
552 const SwitchStmt *getSwitch() const {
553 return cast<SwitchStmt>(Src->getTerminator());
556 ExplodedNode *generateCaseStmtNode(const iterator &I,
557 ProgramStateRef State);
559 ExplodedNode *generateDefaultCaseNode(ProgramStateRef State,
560 bool isSink = false);
562 const Expr *getCondition() const { return Condition; }
564 ProgramStateRef getState() const { return Pred->State; }
566 const LocationContext *getLocationContext() const {
567 return Pred->getLocationContext();
575 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_COREENGINE_H