1 //===- CoreEngine.cpp - Path-Sensitive Dataflow Engine --------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines a generic engine for intraprocedural, path-sensitive,
10 // dataflow analysis via graph reachability engine.
12 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
15 #include "clang/AST/Expr.h"
16 #include "clang/AST/ExprCXX.h"
17 #include "clang/AST/Stmt.h"
18 #include "clang/AST/StmtCXX.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/AnalyzerOptions.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/BlockCounter.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/FunctionSummary.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
28 #include "clang/StaticAnalyzer/Core/PathSensitive/WorkList.h"
29 #include "llvm/ADT/Optional.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/ErrorHandling.h"
39 using namespace clang;
42 #define DEBUG_TYPE "CoreEngine"
45 "The # of steps executed.");
46 STATISTIC(NumReachedMaxSteps,
47 "The # of times we reached the max number of steps.");
48 STATISTIC(NumPathsExplored,
49 "The # of paths explored by the analyzer.");
51 //===----------------------------------------------------------------------===//
52 // Core analysis engine.
53 //===----------------------------------------------------------------------===//
55 static std::unique_ptr<WorkList> generateWorkList(AnalyzerOptions &Opts,
56 SubEngine &subengine) {
57 switch (Opts.getExplorationStrategy()) {
58 case ExplorationStrategyKind::DFS:
59 return WorkList::makeDFS();
60 case ExplorationStrategyKind::BFS:
61 return WorkList::makeBFS();
62 case ExplorationStrategyKind::BFSBlockDFSContents:
63 return WorkList::makeBFSBlockDFSContents();
64 case ExplorationStrategyKind::UnexploredFirst:
65 return WorkList::makeUnexploredFirst();
66 case ExplorationStrategyKind::UnexploredFirstQueue:
67 return WorkList::makeUnexploredFirstPriorityQueue();
68 case ExplorationStrategyKind::UnexploredFirstLocationQueue:
69 return WorkList::makeUnexploredFirstPriorityLocationQueue();
71 llvm_unreachable("Unknown AnalyzerOptions::ExplorationStrategyKind");
74 CoreEngine::CoreEngine(SubEngine &subengine, FunctionSummariesTy *FS,
75 AnalyzerOptions &Opts)
76 : SubEng(subengine), WList(generateWorkList(Opts, subengine)),
77 BCounterFactory(G.getAllocator()), FunctionSummaries(FS) {}
79 /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
80 bool CoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps,
81 ProgramStateRef InitState) {
82 if (G.num_roots() == 0) { // Initialize the analysis by constructing
83 // the root if none exists.
85 const CFGBlock *Entry = &(L->getCFG()->getEntry());
87 assert(Entry->empty() && "Entry block must be empty.");
89 assert(Entry->succ_size() == 1 && "Entry block must have 1 successor.");
91 // Mark the entry block as visited.
92 FunctionSummaries->markVisitedBasicBlock(Entry->getBlockID(),
94 L->getCFG()->getNumBlockIDs());
96 // Get the solitary successor.
97 const CFGBlock *Succ = *(Entry->succ_begin());
99 // Construct an edge representing the
100 // starting location in the function.
101 BlockEdge StartLoc(Entry, Succ, L);
103 // Set the current block counter to being empty.
104 WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
107 InitState = SubEng.getInitialState(L);
110 ExplodedNode *Node = G.getNode(StartLoc, InitState, false, &IsNew);
114 NodeBuilderContext BuilderCtx(*this, StartLoc.getDst(), Node);
115 ExplodedNodeSet DstBegin;
116 SubEng.processBeginOfFunction(BuilderCtx, Node, DstBegin, StartLoc);
121 // Check if we have a steps limit
122 bool UnlimitedSteps = Steps == 0;
123 // Cap our pre-reservation in the event that the user specifies
124 // a very large number of maximum steps.
125 const unsigned PreReservationCap = 4000000;
127 G.reserve(std::min(Steps,PreReservationCap));
129 while (WList->hasWork()) {
130 if (!UnlimitedSteps) {
132 NumReachedMaxSteps++;
140 const WorkListUnit& WU = WList->dequeue();
142 // Set the current block counter.
143 WList->setBlockCounter(WU.getBlockCounter());
145 // Retrieve the node.
146 ExplodedNode *Node = WU.getNode();
148 dispatchWorkItem(Node, Node->getLocation(), WU);
150 SubEng.processEndWorklist();
151 return WList->hasWork();
154 void CoreEngine::dispatchWorkItem(ExplodedNode* Pred, ProgramPoint Loc,
155 const WorkListUnit& WU) {
156 // Dispatch on the location type.
157 switch (Loc.getKind()) {
158 case ProgramPoint::BlockEdgeKind:
159 HandleBlockEdge(Loc.castAs<BlockEdge>(), Pred);
162 case ProgramPoint::BlockEntranceKind:
163 HandleBlockEntrance(Loc.castAs<BlockEntrance>(), Pred);
166 case ProgramPoint::BlockExitKind:
167 assert(false && "BlockExit location never occur in forward analysis.");
170 case ProgramPoint::CallEnterKind:
171 HandleCallEnter(Loc.castAs<CallEnter>(), Pred);
174 case ProgramPoint::CallExitBeginKind:
175 SubEng.processCallExit(Pred);
178 case ProgramPoint::EpsilonKind: {
179 assert(Pred->hasSinglePred() &&
180 "Assume epsilon has exactly one predecessor by construction");
181 ExplodedNode *PNode = Pred->getFirstPred();
182 dispatchWorkItem(Pred, PNode->getLocation(), WU);
186 assert(Loc.getAs<PostStmt>() ||
187 Loc.getAs<PostInitializer>() ||
188 Loc.getAs<PostImplicitCall>() ||
189 Loc.getAs<CallExitEnd>() ||
190 Loc.getAs<LoopExit>() ||
191 Loc.getAs<PostAllocatorCall>());
192 HandlePostStmt(WU.getBlock(), WU.getIndex(), Pred);
197 bool CoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L,
199 ProgramStateRef InitState,
200 ExplodedNodeSet &Dst) {
201 bool DidNotFinish = ExecuteWorkList(L, Steps, InitState);
202 for (ExplodedGraph::eop_iterator I = G.eop_begin(), E = G.eop_end(); I != E;
209 void CoreEngine::HandleBlockEdge(const BlockEdge &L, ExplodedNode *Pred) {
210 const CFGBlock *Blk = L.getDst();
211 NodeBuilderContext BuilderCtx(*this, Blk, Pred);
213 // Mark this block as visited.
214 const LocationContext *LC = Pred->getLocationContext();
215 FunctionSummaries->markVisitedBasicBlock(Blk->getBlockID(),
217 LC->getCFG()->getNumBlockIDs());
219 // Display a prunable path note to the user if it's a virtual bases branch
220 // and we're taking the path that skips virtual base constructors.
221 if (L.getSrc()->getTerminator().isVirtualBaseBranch() &&
222 L.getDst() == *L.getSrc()->succ_begin()) {
223 ProgramPoint P = L.withTag(getNoteTags().makeNoteTag(
224 [](BugReporterContext &, BugReport &) -> std::string {
225 // TODO: Just call out the name of the most derived class
227 return "Virtual base initialization skipped because "
228 "it has already been handled by the most derived class";
229 }, /*IsPrunable=*/true));
230 // Perform the transition.
232 NodeBuilder Bldr(Pred, Dst, BuilderCtx);
233 Pred = Bldr.generateNode(P, Pred->getState(), Pred);
238 // Check if we are entering the EXIT block.
239 if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
240 assert(L.getLocationContext()->getCFG()->getExit().empty() &&
241 "EXIT block cannot contain Stmts.");
243 // Get return statement..
244 const ReturnStmt *RS = nullptr;
245 if (!L.getSrc()->empty()) {
246 CFGElement LastElement = L.getSrc()->back();
247 if (Optional<CFGStmt> LastStmt = LastElement.getAs<CFGStmt>()) {
248 RS = dyn_cast<ReturnStmt>(LastStmt->getStmt());
249 } else if (Optional<CFGAutomaticObjDtor> AutoDtor =
250 LastElement.getAs<CFGAutomaticObjDtor>()) {
251 RS = dyn_cast<ReturnStmt>(AutoDtor->getTriggerStmt());
255 // Process the final state transition.
256 SubEng.processEndOfFunction(BuilderCtx, Pred, RS);
258 // This path is done. Don't enqueue any more nodes.
262 // Call into the SubEngine to process entering the CFGBlock.
263 ExplodedNodeSet dstNodes;
264 BlockEntrance BE(Blk, Pred->getLocationContext());
265 NodeBuilderWithSinks nodeBuilder(Pred, dstNodes, BuilderCtx, BE);
266 SubEng.processCFGBlockEntrance(L, nodeBuilder, Pred);
268 // Auto-generate a node.
269 if (!nodeBuilder.hasGeneratedNodes()) {
270 nodeBuilder.generateNode(Pred->State, Pred);
273 // Enqueue nodes onto the worklist.
277 void CoreEngine::HandleBlockEntrance(const BlockEntrance &L,
278 ExplodedNode *Pred) {
279 // Increment the block counter.
280 const LocationContext *LC = Pred->getLocationContext();
281 unsigned BlockId = L.getBlock()->getBlockID();
282 BlockCounter Counter = WList->getBlockCounter();
283 Counter = BCounterFactory.IncrementCount(Counter, LC->getStackFrame(),
285 WList->setBlockCounter(Counter);
287 // Process the entrance of the block.
288 if (Optional<CFGElement> E = L.getFirstElement()) {
289 NodeBuilderContext Ctx(*this, L.getBlock(), Pred);
290 SubEng.processCFGElement(*E, Pred, 0, &Ctx);
293 HandleBlockExit(L.getBlock(), Pred);
296 void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) {
297 if (const Stmt *Term = B->getTerminatorStmt()) {
298 switch (Term->getStmtClass()) {
300 llvm_unreachable("Analysis for this terminator not implemented.");
302 case Stmt::CXXBindTemporaryExprClass:
303 HandleCleanupTemporaryBranch(
304 cast<CXXBindTemporaryExpr>(Term), B, Pred);
307 // Model static initializers.
308 case Stmt::DeclStmtClass:
309 HandleStaticInit(cast<DeclStmt>(Term), B, Pred);
312 case Stmt::BinaryOperatorClass: // '&&' and '||'
313 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
316 case Stmt::BinaryConditionalOperatorClass:
317 case Stmt::ConditionalOperatorClass:
318 HandleBranch(cast<AbstractConditionalOperator>(Term)->getCond(),
322 // FIXME: Use constant-folding in CFG construction to simplify this
325 case Stmt::ChooseExprClass:
326 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
329 case Stmt::CXXTryStmtClass:
330 // Generate a node for each of the successors.
331 // Our logic for EH analysis can certainly be improved.
332 for (CFGBlock::const_succ_iterator it = B->succ_begin(),
333 et = B->succ_end(); it != et; ++it) {
334 if (const CFGBlock *succ = *it) {
335 generateNode(BlockEdge(B, succ, Pred->getLocationContext()),
341 case Stmt::DoStmtClass:
342 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
345 case Stmt::CXXForRangeStmtClass:
346 HandleBranch(cast<CXXForRangeStmt>(Term)->getCond(), Term, B, Pred);
349 case Stmt::ForStmtClass:
350 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
353 case Stmt::ContinueStmtClass:
354 case Stmt::BreakStmtClass:
355 case Stmt::GotoStmtClass:
358 case Stmt::IfStmtClass:
359 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
362 case Stmt::IndirectGotoStmtClass: {
363 // Only 1 successor: the indirect goto dispatch block.
364 assert(B->succ_size() == 1);
366 IndirectGotoNodeBuilder
367 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
368 *(B->succ_begin()), this);
370 SubEng.processIndirectGoto(builder);
374 case Stmt::ObjCForCollectionStmtClass:
375 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
377 // (1) inside a basic block, which represents the binding of the
378 // 'element' variable to a value.
379 // (2) in a terminator, which represents the branch.
381 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
382 // whether or not collection contains any more elements. We cannot
383 // just test to see if the element is nil because a container can
384 // contain nil elements.
385 HandleBranch(Term, Term, B, Pred);
388 case Stmt::SwitchStmtClass: {
389 SwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
392 SubEng.processSwitch(builder);
396 case Stmt::WhileStmtClass:
397 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
400 case Stmt::GCCAsmStmtClass:
401 assert(cast<GCCAsmStmt>(Term)->isAsmGoto() && "Encountered GCCAsmStmt without labels");
402 // TODO: Handle jumping to labels
407 if (B->getTerminator().isVirtualBaseBranch()) {
408 HandleVirtualBaseBranch(B, Pred);
412 assert(B->succ_size() == 1 &&
413 "Blocks with no terminator should have at most 1 successor.");
415 generateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
419 void CoreEngine::HandleCallEnter(const CallEnter &CE, ExplodedNode *Pred) {
420 NodeBuilderContext BuilderCtx(*this, CE.getEntry(), Pred);
421 SubEng.processCallEnter(BuilderCtx, CE, Pred);
424 void CoreEngine::HandleBranch(const Stmt *Cond, const Stmt *Term,
425 const CFGBlock * B, ExplodedNode *Pred) {
426 assert(B->succ_size() == 2);
427 NodeBuilderContext Ctx(*this, B, Pred);
429 SubEng.processBranch(Cond, Ctx, Pred, Dst, *(B->succ_begin()),
430 *(B->succ_begin() + 1));
431 // Enqueue the new frontier onto the worklist.
435 void CoreEngine::HandleCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE,
437 ExplodedNode *Pred) {
438 assert(B->succ_size() == 2);
439 NodeBuilderContext Ctx(*this, B, Pred);
441 SubEng.processCleanupTemporaryBranch(BTE, Ctx, Pred, Dst, *(B->succ_begin()),
442 *(B->succ_begin() + 1));
443 // Enqueue the new frontier onto the worklist.
447 void CoreEngine::HandleStaticInit(const DeclStmt *DS, const CFGBlock *B,
448 ExplodedNode *Pred) {
449 assert(B->succ_size() == 2);
450 NodeBuilderContext Ctx(*this, B, Pred);
452 SubEng.processStaticInitializer(DS, Ctx, Pred, Dst,
453 *(B->succ_begin()), *(B->succ_begin()+1));
454 // Enqueue the new frontier onto the worklist.
458 void CoreEngine::HandlePostStmt(const CFGBlock *B, unsigned StmtIdx,
459 ExplodedNode *Pred) {
463 if (StmtIdx == B->size())
464 HandleBlockExit(B, Pred);
466 NodeBuilderContext Ctx(*this, B, Pred);
467 SubEng.processCFGElement((*B)[StmtIdx], Pred, StmtIdx, &Ctx);
471 void CoreEngine::HandleVirtualBaseBranch(const CFGBlock *B,
472 ExplodedNode *Pred) {
473 const LocationContext *LCtx = Pred->getLocationContext();
474 if (const auto *CallerCtor = dyn_cast_or_null<CXXConstructExpr>(
475 LCtx->getStackFrame()->getCallSite())) {
476 switch (CallerCtor->getConstructionKind()) {
477 case CXXConstructExpr::CK_NonVirtualBase:
478 case CXXConstructExpr::CK_VirtualBase: {
479 BlockEdge Loc(B, *B->succ_begin(), LCtx);
480 HandleBlockEdge(Loc, Pred);
488 // We either don't see a parent stack frame because we're in the top frame,
489 // or the parent stack frame doesn't initialize our virtual bases.
490 BlockEdge Loc(B, *(B->succ_begin() + 1), LCtx);
491 HandleBlockEdge(Loc, Pred);
494 /// generateNode - Utility method to generate nodes, hook up successors,
495 /// and add nodes to the worklist.
496 void CoreEngine::generateNode(const ProgramPoint &Loc,
497 ProgramStateRef State,
498 ExplodedNode *Pred) {
500 ExplodedNode *Node = G.getNode(Loc, State, false, &IsNew);
503 Node->addPredecessor(Pred, G); // Link 'Node' with its predecessor.
506 G.addRoot(Node); // 'Node' has no predecessor. Make it a root.
509 // Only add 'Node' to the worklist if it was freshly generated.
510 if (IsNew) WList->enqueue(Node);
513 void CoreEngine::enqueueStmtNode(ExplodedNode *N,
514 const CFGBlock *Block, unsigned Idx) {
516 assert(!N->isSink());
518 // Check if this node entered a callee.
519 if (N->getLocation().getAs<CallEnter>()) {
520 // Still use the index of the CallExpr. It's needed to create the callee
521 // StackFrameContext.
522 WList->enqueue(N, Block, Idx);
526 // Do not create extra nodes. Move to the next CFG element.
527 if (N->getLocation().getAs<PostInitializer>() ||
528 N->getLocation().getAs<PostImplicitCall>()||
529 N->getLocation().getAs<LoopExit>()) {
530 WList->enqueue(N, Block, Idx+1);
534 if (N->getLocation().getAs<EpsilonPoint>()) {
535 WList->enqueue(N, Block, Idx);
539 if ((*Block)[Idx].getKind() == CFGElement::NewAllocator) {
540 WList->enqueue(N, Block, Idx+1);
544 // At this point, we know we're processing a normal statement.
545 CFGStmt CS = (*Block)[Idx].castAs<CFGStmt>();
546 PostStmt Loc(CS.getStmt(), N->getLocationContext());
548 if (Loc == N->getLocation().withTag(nullptr)) {
549 // Note: 'N' should be a fresh node because otherwise it shouldn't be
550 // a member of Deferred.
551 WList->enqueue(N, Block, Idx+1);
556 ExplodedNode *Succ = G.getNode(Loc, N->getState(), false, &IsNew);
557 Succ->addPredecessor(N, G);
560 WList->enqueue(Succ, Block, Idx+1);
563 ExplodedNode *CoreEngine::generateCallExitBeginNode(ExplodedNode *N,
564 const ReturnStmt *RS) {
565 // Create a CallExitBegin node and enqueue it.
566 const auto *LocCtx = cast<StackFrameContext>(N->getLocationContext());
568 // Use the callee location context.
569 CallExitBegin Loc(LocCtx, RS);
572 ExplodedNode *Node = G.getNode(Loc, N->getState(), false, &isNew);
573 Node->addPredecessor(N, G);
574 return isNew ? Node : nullptr;
577 void CoreEngine::enqueue(ExplodedNodeSet &Set) {
578 for (const auto I : Set)
582 void CoreEngine::enqueue(ExplodedNodeSet &Set,
583 const CFGBlock *Block, unsigned Idx) {
584 for (const auto I : Set)
585 enqueueStmtNode(I, Block, Idx);
588 void CoreEngine::enqueueEndOfFunction(ExplodedNodeSet &Set, const ReturnStmt *RS) {
590 // If we are in an inlined call, generate CallExitBegin node.
591 if (I->getLocationContext()->getParent()) {
592 I = generateCallExitBeginNode(I, RS);
596 // TODO: We should run remove dead bindings here.
603 void NodeBuilder::anchor() {}
605 ExplodedNode* NodeBuilder::generateNodeImpl(const ProgramPoint &Loc,
606 ProgramStateRef State,
609 HasGeneratedNodes = true;
611 ExplodedNode *N = C.Eng.G.getNode(Loc, State, MarkAsSink, &IsNew);
612 N->addPredecessor(FromN, C.Eng.G);
613 Frontier.erase(FromN);
624 void NodeBuilderWithSinks::anchor() {}
626 StmtNodeBuilder::~StmtNodeBuilder() {
628 for (const auto I : Frontier)
629 EnclosingBldr->addNodes(I);
632 void BranchNodeBuilder::anchor() {}
634 ExplodedNode *BranchNodeBuilder::generateNode(ProgramStateRef State,
636 ExplodedNode *NodePred) {
637 // If the branch has been marked infeasible we should not generate a node.
638 if (!isFeasible(branch))
641 ProgramPoint Loc = BlockEdge(C.Block, branch ? DstT:DstF,
642 NodePred->getLocationContext());
643 ExplodedNode *Succ = generateNodeImpl(Loc, State, NodePred);
648 IndirectGotoNodeBuilder::generateNode(const iterator &I,
653 Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()),
655 Succ->addPredecessor(Pred, Eng.G);
661 Eng.WList->enqueue(Succ);
667 SwitchNodeBuilder::generateCaseStmtNode(const iterator &I,
668 ProgramStateRef St) {
671 Eng.G.getNode(BlockEdge(Src, I.getBlock(), Pred->getLocationContext()),
673 Succ->addPredecessor(Pred, Eng.G);
677 Eng.WList->enqueue(Succ);
682 SwitchNodeBuilder::generateDefaultCaseNode(ProgramStateRef St,
684 // Get the block for the default case.
685 assert(Src->succ_rbegin() != Src->succ_rend());
686 CFGBlock *DefaultBlock = *Src->succ_rbegin();
688 // Sanity check for default blocks that are unreachable and not caught
689 // by earlier stages.
695 Eng.G.getNode(BlockEdge(Src, DefaultBlock, Pred->getLocationContext()),
697 Succ->addPredecessor(Pred, Eng.G);
703 Eng.WList->enqueue(Succ);