1 //===- CoreEngine.cpp - Path-Sensitive Dataflow Engine --------------------===//
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/Stmt.h"
19 #include "clang/AST/StmtCXX.h"
20 #include "clang/Analysis/AnalysisDeclContext.h"
21 #include "clang/Analysis/CFG.h"
22 #include "clang/Analysis/ProgramPoint.h"
23 #include "clang/Basic/LLVM.h"
24 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/BlockCounter.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/FunctionSummary.h"
28 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
29 #include "clang/StaticAnalyzer/Core/PathSensitive/WorkList.h"
30 #include "llvm/ADT/Optional.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/ErrorHandling.h"
40 using namespace clang;
43 #define DEBUG_TYPE "CoreEngine"
46 "The # of steps executed.");
47 STATISTIC(NumReachedMaxSteps,
48 "The # of times we reached the max number of steps.");
49 STATISTIC(NumPathsExplored,
50 "The # of paths explored by the analyzer.");
52 //===----------------------------------------------------------------------===//
53 // Core analysis engine.
54 //===----------------------------------------------------------------------===//
56 static std::unique_ptr<WorkList> generateWorkList(AnalyzerOptions &Opts) {
57 switch (Opts.getExplorationStrategy()) {
58 case AnalyzerOptions::ExplorationStrategyKind::DFS:
59 return WorkList::makeDFS();
60 case AnalyzerOptions::ExplorationStrategyKind::BFS:
61 return WorkList::makeBFS();
62 case AnalyzerOptions::ExplorationStrategyKind::BFSBlockDFSContents:
63 return WorkList::makeBFSBlockDFSContents();
64 case AnalyzerOptions::ExplorationStrategyKind::UnexploredFirst:
65 return WorkList::makeUnexploredFirst();
66 case AnalyzerOptions::ExplorationStrategyKind::UnexploredFirstQueue:
67 return WorkList::makeUnexploredFirstPriorityQueue();
69 llvm_unreachable("Unexpected case");
73 CoreEngine::CoreEngine(SubEngine &subengine, FunctionSummariesTy *FS,
74 AnalyzerOptions &Opts)
75 : SubEng(subengine), WList(generateWorkList(Opts)),
76 BCounterFactory(G.getAllocator()), FunctionSummaries(FS) {}
78 /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
79 bool CoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps,
80 ProgramStateRef InitState) {
81 if (G.num_roots() == 0) { // Initialize the analysis by constructing
82 // the root if none exists.
84 const CFGBlock *Entry = &(L->getCFG()->getEntry());
86 assert(Entry->empty() && "Entry block must be empty.");
88 assert(Entry->succ_size() == 1 && "Entry block must have 1 successor.");
90 // Mark the entry block as visited.
91 FunctionSummaries->markVisitedBasicBlock(Entry->getBlockID(),
93 L->getCFG()->getNumBlockIDs());
95 // Get the solitary successor.
96 const CFGBlock *Succ = *(Entry->succ_begin());
98 // Construct an edge representing the
99 // starting location in the function.
100 BlockEdge StartLoc(Entry, Succ, L);
102 // Set the current block counter to being empty.
103 WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
106 InitState = SubEng.getInitialState(L);
109 ExplodedNode *Node = G.getNode(StartLoc, InitState, false, &IsNew);
113 NodeBuilderContext BuilderCtx(*this, StartLoc.getDst(), Node);
114 ExplodedNodeSet DstBegin;
115 SubEng.processBeginOfFunction(BuilderCtx, Node, DstBegin, StartLoc);
120 // Check if we have a steps limit
121 bool UnlimitedSteps = Steps == 0;
122 // Cap our pre-reservation in the event that the user specifies
123 // a very large number of maximum steps.
124 const unsigned PreReservationCap = 4000000;
126 G.reserve(std::min(Steps,PreReservationCap));
128 while (WList->hasWork()) {
129 if (!UnlimitedSteps) {
131 NumReachedMaxSteps++;
139 const WorkListUnit& WU = WList->dequeue();
141 // Set the current block counter.
142 WList->setBlockCounter(WU.getBlockCounter());
144 // Retrieve the node.
145 ExplodedNode *Node = WU.getNode();
147 dispatchWorkItem(Node, Node->getLocation(), WU);
149 SubEng.processEndWorklist(hasWorkRemaining());
150 return WList->hasWork();
153 void CoreEngine::dispatchWorkItem(ExplodedNode* Pred, ProgramPoint Loc,
154 const WorkListUnit& WU) {
155 // Dispatch on the location type.
156 switch (Loc.getKind()) {
157 case ProgramPoint::BlockEdgeKind:
158 HandleBlockEdge(Loc.castAs<BlockEdge>(), Pred);
161 case ProgramPoint::BlockEntranceKind:
162 HandleBlockEntrance(Loc.castAs<BlockEntrance>(), Pred);
165 case ProgramPoint::BlockExitKind:
166 assert(false && "BlockExit location never occur in forward analysis.");
169 case ProgramPoint::CallEnterKind:
170 HandleCallEnter(Loc.castAs<CallEnter>(), Pred);
173 case ProgramPoint::CallExitBeginKind:
174 SubEng.processCallExit(Pred);
177 case ProgramPoint::EpsilonKind: {
178 assert(Pred->hasSinglePred() &&
179 "Assume epsilon has exactly one predecessor by construction");
180 ExplodedNode *PNode = Pred->getFirstPred();
181 dispatchWorkItem(Pred, PNode->getLocation(), WU);
185 assert(Loc.getAs<PostStmt>() ||
186 Loc.getAs<PostInitializer>() ||
187 Loc.getAs<PostImplicitCall>() ||
188 Loc.getAs<CallExitEnd>() ||
189 Loc.getAs<LoopExit>() ||
190 Loc.getAs<PostAllocatorCall>());
191 HandlePostStmt(WU.getBlock(), WU.getIndex(), Pred);
196 bool CoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L,
198 ProgramStateRef InitState,
199 ExplodedNodeSet &Dst) {
200 bool DidNotFinish = ExecuteWorkList(L, Steps, InitState);
201 for (ExplodedGraph::eop_iterator I = G.eop_begin(), E = G.eop_end(); I != E;
208 void CoreEngine::HandleBlockEdge(const BlockEdge &L, ExplodedNode *Pred) {
209 const CFGBlock *Blk = L.getDst();
210 NodeBuilderContext BuilderCtx(*this, Blk, Pred);
212 // Mark this block as visited.
213 const LocationContext *LC = Pred->getLocationContext();
214 FunctionSummaries->markVisitedBasicBlock(Blk->getBlockID(),
216 LC->getCFG()->getNumBlockIDs());
218 // Check if we are entering the EXIT block.
219 if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
220 assert(L.getLocationContext()->getCFG()->getExit().empty() &&
221 "EXIT block cannot contain Stmts.");
223 // Get return statement..
224 const ReturnStmt *RS = nullptr;
225 if (!L.getSrc()->empty()) {
226 if (Optional<CFGStmt> LastStmt = L.getSrc()->back().getAs<CFGStmt>()) {
227 RS = dyn_cast<ReturnStmt>(LastStmt->getStmt());
231 // Process the final state transition.
232 SubEng.processEndOfFunction(BuilderCtx, Pred, RS);
234 // This path is done. Don't enqueue any more nodes.
238 // Call into the SubEngine to process entering the CFGBlock.
239 ExplodedNodeSet dstNodes;
240 BlockEntrance BE(Blk, Pred->getLocationContext());
241 NodeBuilderWithSinks nodeBuilder(Pred, dstNodes, BuilderCtx, BE);
242 SubEng.processCFGBlockEntrance(L, nodeBuilder, Pred);
244 // Auto-generate a node.
245 if (!nodeBuilder.hasGeneratedNodes()) {
246 nodeBuilder.generateNode(Pred->State, Pred);
249 // Enqueue nodes onto the worklist.
253 void CoreEngine::HandleBlockEntrance(const BlockEntrance &L,
254 ExplodedNode *Pred) {
255 // Increment the block counter.
256 const LocationContext *LC = Pred->getLocationContext();
257 unsigned BlockId = L.getBlock()->getBlockID();
258 BlockCounter Counter = WList->getBlockCounter();
259 Counter = BCounterFactory.IncrementCount(Counter, LC->getStackFrame(),
261 WList->setBlockCounter(Counter);
263 // Process the entrance of the block.
264 if (Optional<CFGElement> E = L.getFirstElement()) {
265 NodeBuilderContext Ctx(*this, L.getBlock(), Pred);
266 SubEng.processCFGElement(*E, Pred, 0, &Ctx);
269 HandleBlockExit(L.getBlock(), Pred);
272 void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) {
273 if (const Stmt *Term = B->getTerminator()) {
274 switch (Term->getStmtClass()) {
276 llvm_unreachable("Analysis for this terminator not implemented.");
278 case Stmt::CXXBindTemporaryExprClass:
279 HandleCleanupTemporaryBranch(
280 cast<CXXBindTemporaryExpr>(B->getTerminator().getStmt()), B, Pred);
283 // Model static initializers.
284 case Stmt::DeclStmtClass:
285 HandleStaticInit(cast<DeclStmt>(Term), B, Pred);
288 case Stmt::BinaryOperatorClass: // '&&' and '||'
289 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
292 case Stmt::BinaryConditionalOperatorClass:
293 case Stmt::ConditionalOperatorClass:
294 HandleBranch(cast<AbstractConditionalOperator>(Term)->getCond(),
298 // FIXME: Use constant-folding in CFG construction to simplify this
301 case Stmt::ChooseExprClass:
302 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
305 case Stmt::CXXTryStmtClass:
306 // Generate a node for each of the successors.
307 // Our logic for EH analysis can certainly be improved.
308 for (CFGBlock::const_succ_iterator it = B->succ_begin(),
309 et = B->succ_end(); it != et; ++it) {
310 if (const CFGBlock *succ = *it) {
311 generateNode(BlockEdge(B, succ, Pred->getLocationContext()),
317 case Stmt::DoStmtClass:
318 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
321 case Stmt::CXXForRangeStmtClass:
322 HandleBranch(cast<CXXForRangeStmt>(Term)->getCond(), Term, B, Pred);
325 case Stmt::ForStmtClass:
326 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
329 case Stmt::ContinueStmtClass:
330 case Stmt::BreakStmtClass:
331 case Stmt::GotoStmtClass:
334 case Stmt::IfStmtClass:
335 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
338 case Stmt::IndirectGotoStmtClass: {
339 // Only 1 successor: the indirect goto dispatch block.
340 assert(B->succ_size() == 1);
342 IndirectGotoNodeBuilder
343 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
344 *(B->succ_begin()), this);
346 SubEng.processIndirectGoto(builder);
350 case Stmt::ObjCForCollectionStmtClass:
351 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
353 // (1) inside a basic block, which represents the binding of the
354 // 'element' variable to a value.
355 // (2) in a terminator, which represents the branch.
357 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
358 // whether or not collection contains any more elements. We cannot
359 // just test to see if the element is nil because a container can
360 // contain nil elements.
361 HandleBranch(Term, Term, B, Pred);
364 case Stmt::SwitchStmtClass: {
365 SwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
368 SubEng.processSwitch(builder);
372 case Stmt::WhileStmtClass:
373 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
378 assert(B->succ_size() == 1 &&
379 "Blocks with no terminator should have at most 1 successor.");
381 generateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
385 void CoreEngine::HandleCallEnter(const CallEnter &CE, ExplodedNode *Pred) {
386 NodeBuilderContext BuilderCtx(*this, CE.getEntry(), Pred);
387 SubEng.processCallEnter(BuilderCtx, CE, Pred);
390 void CoreEngine::HandleBranch(const Stmt *Cond, const Stmt *Term,
391 const CFGBlock * B, ExplodedNode *Pred) {
392 assert(B->succ_size() == 2);
393 NodeBuilderContext Ctx(*this, B, Pred);
395 SubEng.processBranch(Cond, Term, Ctx, Pred, Dst,
396 *(B->succ_begin()), *(B->succ_begin()+1));
397 // Enqueue the new frontier onto the worklist.
401 void CoreEngine::HandleCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE,
403 ExplodedNode *Pred) {
404 assert(B->succ_size() == 2);
405 NodeBuilderContext Ctx(*this, B, Pred);
407 SubEng.processCleanupTemporaryBranch(BTE, Ctx, Pred, Dst, *(B->succ_begin()),
408 *(B->succ_begin() + 1));
409 // Enqueue the new frontier onto the worklist.
413 void CoreEngine::HandleStaticInit(const DeclStmt *DS, const CFGBlock *B,
414 ExplodedNode *Pred) {
415 assert(B->succ_size() == 2);
416 NodeBuilderContext Ctx(*this, B, Pred);
418 SubEng.processStaticInitializer(DS, Ctx, Pred, Dst,
419 *(B->succ_begin()), *(B->succ_begin()+1));
420 // Enqueue the new frontier onto the worklist.
424 void CoreEngine::HandlePostStmt(const CFGBlock *B, unsigned StmtIdx,
425 ExplodedNode *Pred) {
429 if (StmtIdx == B->size())
430 HandleBlockExit(B, Pred);
432 NodeBuilderContext Ctx(*this, B, Pred);
433 SubEng.processCFGElement((*B)[StmtIdx], Pred, StmtIdx, &Ctx);
437 /// generateNode - Utility method to generate nodes, hook up successors,
438 /// and add nodes to the worklist.
439 void CoreEngine::generateNode(const ProgramPoint &Loc,
440 ProgramStateRef State,
441 ExplodedNode *Pred) {
443 ExplodedNode *Node = G.getNode(Loc, State, false, &IsNew);
446 Node->addPredecessor(Pred, G); // Link 'Node' with its predecessor.
449 G.addRoot(Node); // 'Node' has no predecessor. Make it a root.
452 // Only add 'Node' to the worklist if it was freshly generated.
453 if (IsNew) WList->enqueue(Node);
456 void CoreEngine::enqueueStmtNode(ExplodedNode *N,
457 const CFGBlock *Block, unsigned Idx) {
459 assert(!N->isSink());
461 // Check if this node entered a callee.
462 if (N->getLocation().getAs<CallEnter>()) {
463 // Still use the index of the CallExpr. It's needed to create the callee
464 // StackFrameContext.
465 WList->enqueue(N, Block, Idx);
469 // Do not create extra nodes. Move to the next CFG element.
470 if (N->getLocation().getAs<PostInitializer>() ||
471 N->getLocation().getAs<PostImplicitCall>()||
472 N->getLocation().getAs<LoopExit>()) {
473 WList->enqueue(N, Block, Idx+1);
477 if (N->getLocation().getAs<EpsilonPoint>()) {
478 WList->enqueue(N, Block, Idx);
482 if ((*Block)[Idx].getKind() == CFGElement::NewAllocator) {
483 WList->enqueue(N, Block, Idx+1);
487 // At this point, we know we're processing a normal statement.
488 CFGStmt CS = (*Block)[Idx].castAs<CFGStmt>();
489 PostStmt Loc(CS.getStmt(), N->getLocationContext());
491 if (Loc == N->getLocation().withTag(nullptr)) {
492 // Note: 'N' should be a fresh node because otherwise it shouldn't be
493 // a member of Deferred.
494 WList->enqueue(N, Block, Idx+1);
499 ExplodedNode *Succ = G.getNode(Loc, N->getState(), false, &IsNew);
500 Succ->addPredecessor(N, G);
503 WList->enqueue(Succ, Block, Idx+1);
506 ExplodedNode *CoreEngine::generateCallExitBeginNode(ExplodedNode *N,
507 const ReturnStmt *RS) {
508 // Create a CallExitBegin node and enqueue it.
509 const auto *LocCtx = cast<StackFrameContext>(N->getLocationContext());
511 // Use the callee location context.
512 CallExitBegin Loc(LocCtx, RS);
515 ExplodedNode *Node = G.getNode(Loc, N->getState(), false, &isNew);
516 Node->addPredecessor(N, G);
517 return isNew ? Node : nullptr;
520 void CoreEngine::enqueue(ExplodedNodeSet &Set) {
521 for (const auto I : Set)
525 void CoreEngine::enqueue(ExplodedNodeSet &Set,
526 const CFGBlock *Block, unsigned Idx) {
527 for (const auto I : Set)
528 enqueueStmtNode(I, Block, Idx);
531 void CoreEngine::enqueueEndOfFunction(ExplodedNodeSet &Set, const ReturnStmt *RS) {
533 // If we are in an inlined call, generate CallExitBegin node.
534 if (I->getLocationContext()->getParent()) {
535 I = generateCallExitBeginNode(I, RS);
539 // TODO: We should run remove dead bindings here.
546 void NodeBuilder::anchor() {}
548 ExplodedNode* NodeBuilder::generateNodeImpl(const ProgramPoint &Loc,
549 ProgramStateRef State,
552 HasGeneratedNodes = true;
554 ExplodedNode *N = C.Eng.G.getNode(Loc, State, MarkAsSink, &IsNew);
555 N->addPredecessor(FromN, C.Eng.G);
556 Frontier.erase(FromN);
567 void NodeBuilderWithSinks::anchor() {}
569 StmtNodeBuilder::~StmtNodeBuilder() {
571 for (const auto I : Frontier)
572 EnclosingBldr->addNodes(I);
575 void BranchNodeBuilder::anchor() {}
577 ExplodedNode *BranchNodeBuilder::generateNode(ProgramStateRef State,
579 ExplodedNode *NodePred) {
580 // If the branch has been marked infeasible we should not generate a node.
581 if (!isFeasible(branch))
584 ProgramPoint Loc = BlockEdge(C.Block, branch ? DstT:DstF,
585 NodePred->getLocationContext());
586 ExplodedNode *Succ = generateNodeImpl(Loc, State, NodePred);
591 IndirectGotoNodeBuilder::generateNode(const iterator &I,
596 Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()),
598 Succ->addPredecessor(Pred, Eng.G);
604 Eng.WList->enqueue(Succ);
610 SwitchNodeBuilder::generateCaseStmtNode(const iterator &I,
611 ProgramStateRef St) {
614 Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()),
616 Succ->addPredecessor(Pred, Eng.G);
620 Eng.WList->enqueue(Succ);
625 SwitchNodeBuilder::generateDefaultCaseNode(ProgramStateRef St,
627 // Get the block for the default case.
628 assert(Src->succ_rbegin() != Src->succ_rend());
629 CFGBlock *DefaultBlock = *Src->succ_rbegin();
631 // Sanity check for default blocks that are unreachable and not caught
632 // by earlier stages.
638 Eng.G.getNode(BlockEdge(Src, DefaultBlock, Pred->getLocationContext()),
640 Succ->addPredecessor(Pred, Eng.G);
646 Eng.WList->enqueue(Succ);