29#include "llvm/ADT/STLExtras.h"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/Support/Casting.h"
32#include "llvm/Support/ErrorHandling.h"
42#define DEBUG_TYPE "CoreEngine"
45 "The # of steps executed.");
46STATISTIC(NumSTUSteps,
"The # of STU steps executed.");
47STATISTIC(NumCTUSteps,
"The # of CTU steps executed.");
49 "The # of times we reached the max number of steps.");
51 "The # of paths explored by the analyzer.");
59 case ExplorationStrategyKind::DFS:
61 case ExplorationStrategyKind::BFS:
63 case ExplorationStrategyKind::BFSBlockDFSContents:
65 case ExplorationStrategyKind::UnexploredFirst:
67 case ExplorationStrategyKind::UnexploredFirstQueue:
69 case ExplorationStrategyKind::UnexploredFirstLocationQueue:
72 llvm_unreachable(
"Unknown AnalyzerOptions::ExplorationStrategyKind");
79 BCounterFactory(G.getAllocator()), FunctionSummaries(FS) {}
82 WList->setBlockCounter(
C);
84 CTUWList->setBlockCounter(
C);
95 assert(Entry->
empty() &&
"Entry block must be empty.");
97 assert(Entry->
succ_size() == 1 &&
"Entry block must have 1 successor.");
130 bool UnlimitedSteps = MaxSteps == 0;
134 const unsigned PreReservationCap = 4000000;
136 G.
reserve(std::min(MaxSteps, PreReservationCap));
138 auto ProcessWList = [
this, UnlimitedSteps](
unsigned MaxSteps) {
139 unsigned Steps = MaxSteps;
140 while (WList->hasWork()) {
141 if (!UnlimitedSteps) {
143 NumReachedMaxSteps++;
161 return MaxSteps - Steps;
163 const unsigned STUSteps = ProcessWList(MaxSteps);
166 NumSTUSteps += STUSteps;
167 const unsigned MinCTUSteps =
171 unsigned MaxCTUSteps = std::max(STUSteps * Pct / 100, MinCTUSteps);
173 WList = std::move(CTUWList);
174 const unsigned CTUSteps = ProcessWList(MaxCTUSteps);
175 NumCTUSteps += CTUSteps;
179 return WList->hasWork();
195 assert(
false &&
"BlockExit location never occur in forward analysis.");
208 "Assume epsilon has exactly one predecessor by construction");
243 return "Virtual base initialization skipped because "
244 "it has already been handled by the most derived class";
250 Pred = Bldr.generateNode(
P, Pred->
getState(), Pred);
258 "EXIT block cannot contain Stmts.");
264 if (std::optional<CFGStmt> LastStmt = LastElement.
getAs<
CFGStmt>()) {
265 RS = dyn_cast<ReturnStmt>(LastStmt->getStmt());
266 }
else if (std::optional<CFGAutomaticObjDtor> AutoDtor =
268 RS = dyn_cast<ReturnStmt>(AutoDtor->getTriggerStmt());
286 if (!nodeBuilder.hasGeneratedNodes()) {
287 nodeBuilder.generateNode(Pred->State, Pred);
302 setBlockCounter(Counter);
309 HandleBlockExit(L.
getBlock(), Pred);
314 switch (Term->getStmtClass()) {
316 llvm_unreachable(
"Analysis for this terminator not implemented.");
318 case Stmt::CXXBindTemporaryExprClass:
319 HandleCleanupTemporaryBranch(
320 cast<CXXBindTemporaryExpr>(Term), B, Pred);
324 case Stmt::DeclStmtClass:
325 HandleStaticInit(cast<DeclStmt>(Term), B, Pred);
328 case Stmt::BinaryOperatorClass:
329 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
332 case Stmt::BinaryConditionalOperatorClass:
333 case Stmt::ConditionalOperatorClass:
334 HandleBranch(cast<AbstractConditionalOperator>(Term)->getCond(),
341 case Stmt::ChooseExprClass:
342 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
345 case Stmt::CXXTryStmtClass:
349 et = B->
succ_end(); it != et; ++it) {
357 case Stmt::DoStmtClass:
358 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
361 case Stmt::CXXForRangeStmtClass:
362 HandleBranch(cast<CXXForRangeStmt>(Term)->getCond(), Term, B, Pred);
365 case Stmt::ForStmtClass:
366 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
369 case Stmt::SEHLeaveStmtClass:
370 case Stmt::ContinueStmtClass:
371 case Stmt::BreakStmtClass:
372 case Stmt::GotoStmtClass:
375 case Stmt::IfStmtClass:
376 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
379 case Stmt::IndirectGotoStmtClass: {
384 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
391 case Stmt::ObjCForCollectionStmtClass:
402 HandleBranch(Term, Term, B, Pred);
405 case Stmt::SwitchStmtClass: {
413 case Stmt::WhileStmtClass:
414 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
417 case Stmt::GCCAsmStmtClass:
418 assert(cast<GCCAsmStmt>(Term)->isAsmGoto() &&
"Encountered GCCAsmStmt without labels");
425 HandleVirtualBaseBranch(B, Pred);
430 "Blocks with no terminator should have at most 1 successor.");
441void CoreEngine::HandleBranch(
const Stmt *Cond,
const Stmt *Term,
475void CoreEngine::HandlePostStmt(
const CFGBlock *B,
unsigned StmtIdx,
480 if (StmtIdx == B->
size())
481 HandleBlockExit(B, Pred);
488void CoreEngine::HandleVirtualBaseBranch(
const CFGBlock *B,
491 if (
const auto *CallerCtor = dyn_cast_or_null<CXXConstructExpr>(
493 switch (CallerCtor->getConstructionKind()) {
497 HandleBlockEdge(
Loc, Pred);
508 HandleBlockEdge(
Loc, Pred);
520 Node->addPredecessor(Pred, G);
527 if (IsNew) WList->enqueue(
Node);
539 WList->enqueue(N,
Block, Idx);
547 WList->enqueue(N,
Block, Idx+1);
552 WList->enqueue(N,
Block, Idx);
557 WList->enqueue(N,
Block, Idx+1);
568 WList->enqueue(N,
Block, Idx+1);
577 WList->enqueue(Succ,
Block, Idx+1);
590 Node->addPredecessor(N, G);
591 return isNew ?
Node :
nullptr;
595 for (
const auto I :
Set)
601 for (
const auto I :
Set)
606 for (
auto *I :
Set) {
608 if (I->getLocationContext()->getParent()) {
609 I = generateCallExitBeginNode(I, RS);
620void NodeBuilder::anchor() {}
641void NodeBuilderWithSinks::anchor() {}
649void BranchNodeBuilder::anchor() {}
678 Eng.WList->enqueue(Succ);
694 Eng.WList->enqueue(Succ);
720 Eng.WList->enqueue(Succ);
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
static std::unique_ptr< WorkList > generateWorkList(AnalyzerOptions &Opts)
STATISTIC(NumSteps, "The # of steps executed.")
static Decl::Kind getKind(const Decl *D)
Defines the clang::Expr interface and subclasses for C++ expressions.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Stores options for the analyzer from the command line.
ExplorationStrategyKind getExplorationStrategy() const
const CFGBlock * getSrc() const
const CFGBlock * getDst() const
std::optional< CFGElement > getFirstElement() const
const CFGBlock * getBlock() const
Represents C++ object destructor implicitly generated for automatic object or temporary bound to cons...
Represents a single basic block in a source-level CFG.
succ_reverse_iterator succ_rend()
succ_reverse_iterator succ_rbegin()
CFGTerminator getTerminator() const
succ_iterator succ_begin()
Stmt * getTerminatorStmt()
unsigned getBlockID() const
AdjacentBlocks::const_iterator const_succ_iterator
unsigned succ_size() const
Represents a top-level expression in a basic block.
T castAs() const
Convert to the specified CFGElement type, asserting that this CFGElement is of the desired type.
std::optional< T > getAs() const
Convert to the specified CFGElement type, returning std::nullopt if this CFGElement is not of the des...
const Stmt * getStmt() const
bool isVirtualBaseBranch() const
unsigned getNumBlockIDs() const
Returns the total number of BlockIDs allocated (which start at 0).
Represents binding an expression to a temporary.
Represents a point when we begin processing an inlined call.
const CFGBlock * getEntry() const
Returns the entry block in the CFG for the entered function.
Represents a point when we start the call exit sequence (for inlined call).
Represents a point when we finish the call exit sequence (for inlined call).
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
This is a meta program point, which should be skipped by all the diagnostic reasoning etc.
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
const Decl * getDecl() const
const StackFrameContext * getStackFrame() const
Represents a point when we exit a loop.
Represents a program point just after an implicit call event.
ProgramPoint withTag(const ProgramPointTag *tag) const
Create a new ProgramPoint object that is the same as the original except for using the specified tag ...
std::optional< T > getAs() const
Convert to the specified ProgramPoint type, returning std::nullopt if this ProgramPoint is not of the...
const LocationContext * getLocationContext() const
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
const Stmt * getCallSite() const
Stmt - This represents one statement.
AnalyzerOptions & options
BlockCounter IncrementCount(BlockCounter BC, const StackFrameContext *CallSite, unsigned BlockID)
BlockCounter GetEmptyCounter()
An abstract data type used to count the number of times a given block has been visited along a path a...
ExplodedNode * generateNode(ProgramStateRef State, bool branch, ExplodedNode *Pred)
bool isFeasible(bool branch)
CoreEngine(ExprEngine &exprengine, FunctionSummariesTy *FS, AnalyzerOptions &Opts)
Construct a CoreEngine object to analyze the provided CFG.
DataTag::Factory & getDataTags()
void enqueueStmtNode(ExplodedNode *N, const CFGBlock *Block, unsigned Idx)
Enqueue a single node created as a result of statement processing.
void dispatchWorkItem(ExplodedNode *Pred, ProgramPoint Loc, const WorkListUnit &WU)
Dispatch the work list item based on the given location information.
bool ExecuteWorkList(const LocationContext *L, unsigned Steps, ProgramStateRef InitState)
ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
void enqueueEndOfFunction(ExplodedNodeSet &Set, const ReturnStmt *RS)
enqueue the nodes corresponding to the end of function onto the end of path / work list.
void enqueue(ExplodedNodeSet &Set)
Enqueue the given set of nodes onto the work list.
ExplodedNode * addRoot(ExplodedNode *V)
addRoot - Add an untyped node to the set of roots.
void reserve(unsigned NodeCount)
unsigned num_roots() const
ExplodedNode * getNode(const ProgramPoint &L, ProgramStateRef State, bool IsSink=false, bool *IsNew=nullptr)
Retrieve the node associated with a (Location,State) pair, where the 'Location' is a ProgramPoint in ...
ExplodedNode * addEndOfPath(ExplodedNode *V)
addEndOfPath - Add an untyped node to the set of EOP nodes.
bool erase(ExplodedNode *N)
void Add(ExplodedNode *N)
const ProgramStateRef & getState() const
ProgramPoint getLocation() const
getLocation - Returns the edge associated with the given node.
void addPredecessor(ExplodedNode *V, ExplodedGraph &G)
addPredeccessor - Adds a predecessor to the current node, and in tandem add this node as a successor ...
bool hasSinglePred() const
const LocationContext * getLocationContext() const
ExplodedNode * getFirstPred()
void processEndOfFunction(NodeBuilderContext &BC, ExplodedNode *Pred, const ReturnStmt *RS=nullptr)
Called by CoreEngine.
void processCallEnter(NodeBuilderContext &BC, CallEnter CE, ExplodedNode *Pred)
Generate the entry node of the callee.
void processBeginOfFunction(NodeBuilderContext &BC, ExplodedNode *Pred, ExplodedNodeSet &Dst, const BlockEdge &L)
Called by CoreEngine.
ProgramStateRef getInitialState(const LocationContext *InitLoc)
getInitialState - Return the initial state used for the root vertex in the ExplodedGraph.
void processCallExit(ExplodedNode *Pred)
Generate the sequence of nodes that simulate the call exit and the post visit for CallExpr.
void processCFGBlockEntrance(const BlockEdge &L, NodeBuilderWithSinks &nodeBuilder, ExplodedNode *Pred)
Called by CoreEngine when processing the entrance of a CFGBlock.
void processBranch(const Stmt *Condition, NodeBuilderContext &BuilderCtx, ExplodedNode *Pred, ExplodedNodeSet &Dst, const CFGBlock *DstT, const CFGBlock *DstF)
ProcessBranch - Called by CoreEngine.
void processCFGElement(const CFGElement E, ExplodedNode *Pred, unsigned StmtIdx, NodeBuilderContext *Ctx)
processCFGElement - Called by CoreEngine.
void processStaticInitializer(const DeclStmt *DS, NodeBuilderContext &BuilderCtx, ExplodedNode *Pred, ExplodedNodeSet &Dst, const CFGBlock *DstT, const CFGBlock *DstF)
Called by CoreEngine.
void processSwitch(SwitchNodeBuilder &builder)
ProcessSwitch - Called by CoreEngine.
void processEndWorklist()
Called by CoreEngine when the analysis worklist has terminated.
void processCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE, NodeBuilderContext &BldCtx, ExplodedNode *Pred, ExplodedNodeSet &Dst, const CFGBlock *DstT, const CFGBlock *DstF)
Called by CoreEngine.
AnalysisManager & getAnalysisManager()
void processIndirectGoto(IndirectGotoNodeBuilder &builder)
processIndirectGoto - Called by CoreEngine.
void markVisitedBasicBlock(unsigned ID, const Decl *D, unsigned TotalIDs)
const CFGBlock * getBlock() const
ExplodedNode * generateNode(const iterator &I, ProgramStateRef State, bool isSink=false)
const CoreEngine & getEngine() const
Return the CoreEngine associated with this builder.
const CFGBlock * getBlock() const
Return the CFGBlock associated with this builder.
This node builder keeps track of the generated sink nodes.
This is the simplest builder which generates nodes in the ExplodedGraph.
const NodeBuilderContext & C
ExplodedNodeSet & Frontier
The frontier set - a set of nodes which need to be propagated after the builder dies.
void addNodes(const ExplodedNodeSet &S)
ExplodedNode * generateNodeImpl(const ProgramPoint &PP, ProgramStateRef State, ExplodedNode *Pred, bool MarkAsSink=false)
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
~StmtNodeBuilder() override
const CFGBlock * getBlock() const
ExplodedNode * generateDefaultCaseNode(ProgramStateRef State, bool isSink=false)
ExplodedNode * generateCaseStmtNode(const iterator &I, ProgramStateRef State)
ExplodedNode * getNode() const
Returns the node associated with the worklist unit.
unsigned getIndex() const
Return the index within the CFGBlock for the worklist unit.
const CFGBlock * getBlock() const
Returns the CFGblock associated with the worklist unit.
BlockCounter getBlockCounter() const
Returns the block counter map associated with the worklist unit.
static std::unique_ptr< WorkList > makeUnexploredFirstPriorityLocationQueue()
static std::unique_ptr< WorkList > makeUnexploredFirstPriorityQueue()
static std::unique_ptr< WorkList > makeBFSBlockDFSContents()
static std::unique_ptr< WorkList > makeBFS()
static std::unique_ptr< WorkList > makeDFS()
static std::unique_ptr< WorkList > makeUnexploredFirst()
The JSON file list parser is used to communicate input to InstallAPI.