clang 23.0.0git
ExprEngine.cpp
Go to the documentation of this file.
1//===- ExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ----------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a meta-engine for path-sensitive dataflow analysis that
10// is built on CoreEngine, but provides the boilerplate to execute transfer
11// functions and build the ExplodedGraph at the expression level.
12//
13//===----------------------------------------------------------------------===//
14
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclBase.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/ExprCXX.h"
24#include "clang/AST/ExprObjC.h"
25#include "clang/AST/ParentMap.h"
27#include "clang/AST/Stmt.h"
28#include "clang/AST/StmtCXX.h"
29#include "clang/AST/StmtObjC.h"
30#include "clang/AST/Type.h"
32#include "clang/Analysis/CFG.h"
37#include "clang/Basic/LLVM.h"
64#include "llvm/ADT/APSInt.h"
65#include "llvm/ADT/DenseMap.h"
66#include "llvm/ADT/ImmutableMap.h"
67#include "llvm/ADT/ImmutableSet.h"
68#include "llvm/ADT/STLExtras.h"
69#include "llvm/ADT/SmallVector.h"
70#include "llvm/Support/Casting.h"
71#include "llvm/Support/Compiler.h"
72#include "llvm/Support/DOTGraphTraits.h"
73#include "llvm/Support/ErrorHandling.h"
74#include "llvm/Support/GraphWriter.h"
75#include "llvm/Support/IOSandbox.h"
76#include "llvm/Support/TimeProfiler.h"
77#include "llvm/Support/raw_ostream.h"
78#include <cassert>
79#include <cstdint>
80#include <memory>
81#include <optional>
82#include <string>
83#include <tuple>
84#include <utility>
85#include <vector>
86
87using namespace clang;
88using namespace ento;
89
90#define DEBUG_TYPE "ExprEngine"
91
92STAT_COUNTER(NumRemoveDeadBindings,
93 "The # of times RemoveDeadBindings is called");
95 NumMaxBlockCountReached,
96 "The # of aborted paths due to reaching the maximum block count in "
97 "a top level function");
99 NumMaxBlockCountReachedInInlined,
100 "The # of aborted paths due to reaching the maximum block count in "
101 "an inlined function");
102STAT_COUNTER(NumTimesRetriedWithoutInlining,
103 "The # of times we re-evaluated a call without inlining");
104
105//===----------------------------------------------------------------------===//
106// Internal program state traits.
107//===----------------------------------------------------------------------===//
108
109namespace {
110
111// When modeling a C++ constructor, for a variety of reasons we need to track
112// the location of the object for the duration of its ConstructionContext.
113// ObjectsUnderConstruction maps statements within the construction context
114// to the object's location, so that on every such statement the location
115// could have been retrieved.
116
117/// ConstructedObjectKey is used for being able to find the path-sensitive
118/// memory region of a freshly constructed object while modeling the AST node
119/// that syntactically represents the object that is being constructed.
120/// Semantics of such nodes may sometimes require access to the region that's
121/// not otherwise present in the program state, or to the very fact that
122/// the construction context was present and contained references to these
123/// AST nodes.
124class ConstructedObjectKey {
125 using ConstructedObjectKeyImpl =
126 std::pair<ConstructionContextItem, const StackFrame *>;
127 const ConstructedObjectKeyImpl Impl;
128
129public:
130 explicit ConstructedObjectKey(const ConstructionContextItem &Item,
131 const StackFrame *SF)
132 : Impl(Item, SF) {}
133
134 const ConstructionContextItem &getItem() const { return Impl.first; }
135 const StackFrame *getStackFrame() const { return Impl.second; }
136
137 ASTContext &getASTContext() const {
138 return getStackFrame()->getDecl()->getASTContext();
139 }
140
141 void printJson(llvm::raw_ostream &Out, PrinterHelper *Helper,
142 PrintingPolicy &PP) const {
143 const Stmt *S = getItem().getStmtOrNull();
144 const CXXCtorInitializer *I = nullptr;
145 if (!S)
146 I = getItem().getCXXCtorInitializer();
147
148 if (S)
149 Out << "\"stmt_id\": " << S->getID(getASTContext());
150 else
151 Out << "\"init_id\": " << I->getID(getASTContext());
152
153 // Kind
154 Out << ", \"kind\": \"" << getItem().getKindAsString()
155 << "\", \"argument_index\": ";
156
158 Out << getItem().getIndex();
159 else
160 Out << "null";
161
162 // Pretty-print
163 Out << ", \"pretty\": ";
164
165 if (S) {
166 S->printJson(Out, Helper, PP, /*AddQuotes=*/true);
167 } else {
168 Out << '\"' << I->getAnyMember()->getDeclName() << '\"';
169 }
170 }
171
172 void Profile(llvm::FoldingSetNodeID &ID) const {
173 ID.Add(Impl.first);
174 ID.AddPointer(Impl.second);
175 }
176
177 bool operator==(const ConstructedObjectKey &RHS) const {
178 return Impl == RHS.Impl;
179 }
180
181 bool operator<(const ConstructedObjectKey &RHS) const {
182 return Impl < RHS.Impl;
183 }
184};
185} // namespace
186
187typedef llvm::ImmutableMap<ConstructedObjectKey, SVal>
189REGISTER_TRAIT_WITH_PROGRAMSTATE(ObjectsUnderConstruction,
191
192// This trait is responsible for storing the index of the element that is to be
193// constructed in the next iteration. As a result a CXXConstructExpr is only
194// stored if it is array type. Also the index is the index of the continuous
195// memory region, which is important for multi-dimensional arrays. E.g:: int
196// arr[2][2]; assume arr[1][1] will be the next element under construction, so
197// the index is 3.
198typedef llvm::ImmutableMap<
199 std::pair<const CXXConstructExpr *, const StackFrame *>, unsigned>
200 IndexOfElementToConstructMap;
201REGISTER_TRAIT_WITH_PROGRAMSTATE(IndexOfElementToConstruct,
202 IndexOfElementToConstructMap)
203
204// This trait is responsible for holding our pending ArrayInitLoopExprs.
205// It pairs the StackFrame and the initializer CXXConstructExpr with
206// the size of the array that's being copy initialized.
207typedef llvm::ImmutableMap<
208 std::pair<const CXXConstructExpr *, const StackFrame *>, unsigned>
209 PendingInitLoopMap;
210REGISTER_TRAIT_WITH_PROGRAMSTATE(PendingInitLoop, PendingInitLoopMap)
211
212typedef llvm::ImmutableMap<const StackFrame *, unsigned>
214REGISTER_TRAIT_WITH_PROGRAMSTATE(PendingArrayDestruction,
216
217//===----------------------------------------------------------------------===//
218// Engine construction and deletion.
219//===----------------------------------------------------------------------===//
220
221static const char* TagProviderName = "ExprEngine";
222
224 AnalysisManager &mgr, SetOfConstDecls *VisitedCalleesIn,
225 FunctionSummariesTy *FS, InliningModes HowToInlineIn)
226 : CTU(CTU), IsCTUEnabled(mgr.getAnalyzerOptions().IsNaiveCTUEnabled),
227 AMgr(mgr), AnalysisDeclContexts(mgr.getAnalysisDeclContextManager()),
228 Engine(*this, FS, mgr.getAnalyzerOptions()), G(Engine.getGraph()),
229 StateMgr(getContext(), mgr.getStoreManagerCreator(),
230 mgr.getConstraintManagerCreator(), G.getAllocator(), this),
231 SymMgr(StateMgr.getSymbolManager()), MRMgr(StateMgr.getRegionManager()),
232 svalBuilder(StateMgr.getSValBuilder()), ObjCNoRet(mgr.getASTContext()),
233 BR(mgr, *this), VisitedCallees(VisitedCalleesIn),
234 HowToInline(HowToInlineIn) {
235 unsigned TrimInterval = mgr.options.GraphTrimInterval;
236 if (TrimInterval != 0) {
237 // Enable eager node reclamation when constructing the ExplodedGraph.
238 G.enableNodeReclamation(TrimInterval);
239 }
240}
241
242//===----------------------------------------------------------------------===//
243// Utility methods.
244//===----------------------------------------------------------------------===//
245
247 ProgramStateRef state = StateMgr.getInitialState(InitSF);
248 const Decl *D = InitSF->getDecl();
249
250 // Preconditions.
251 // FIXME: It would be nice if we had a more general mechanism to add
252 // such preconditions. Some day.
253 do {
254 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
255 // Precondition: the first argument of 'main' is an integer guaranteed
256 // to be > 0.
257 const IdentifierInfo *II = FD->getIdentifier();
258 if (!II || !(II->getName() == "main" && FD->getNumParams() > 0))
259 break;
260
261 const ParmVarDecl *PD = FD->getParamDecl(0);
262 QualType T = PD->getType();
263 const auto *BT = dyn_cast<BuiltinType>(T);
264 if (!BT || !BT->isInteger())
265 break;
266
267 const MemRegion *R = state->getRegion(PD, InitSF);
268 if (!R)
269 break;
270
271 SVal V = state->getSVal(loc::MemRegionVal(R));
272 SVal Constraint_untested = evalBinOp(state, BO_GT, V,
273 svalBuilder.makeZeroVal(T),
274 svalBuilder.getConditionType());
275
276 std::optional<DefinedOrUnknownSVal> Constraint =
277 Constraint_untested.getAs<DefinedOrUnknownSVal>();
278
279 if (!Constraint)
280 break;
281
282 if (ProgramStateRef newState = state->assume(*Constraint, true))
283 state = newState;
284 }
285 break;
286 }
287 while (false);
288
289 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
290 // Precondition: 'self' is always non-null upon entry to an Objective-C
291 // method.
292 const ImplicitParamDecl *SelfD = MD->getSelfDecl();
293 const MemRegion *R = state->getRegion(SelfD, InitSF);
294 SVal V = state->getSVal(loc::MemRegionVal(R));
295
296 if (std::optional<Loc> LV = V.getAs<Loc>()) {
297 // Assume that the pointer value in 'self' is non-null.
298 state = state->assume(*LV, true);
299 assert(state && "'self' cannot be null");
300 }
301 }
302
303 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
304 if (MD->isImplicitObjectMemberFunction()) {
305 // Precondition: 'this' is always non-null upon entry to the
306 // top-level function. This is our starting assumption for
307 // analyzing an "open" program.
308 const StackFrame *SF = InitSF;
309 if (SF->getParent() == nullptr) {
310 loc::MemRegionVal L = svalBuilder.getCXXThis(MD, SF);
311 SVal V = state->getSVal(L);
312 if (std::optional<Loc> LV = V.getAs<Loc>()) {
313 state = state->assume(*LV, true);
314 assert(state && "'this' cannot be null");
315 }
316 }
317 }
318 }
319
320 return state;
321}
322
323ProgramStateRef ExprEngine::createTemporaryRegionIfNeeded(
324 ProgramStateRef State, const StackFrame *SF,
325 const Expr *InitWithAdjustments, const Expr *Result,
326 const SubRegion **OutRegionWithAdjustments) {
327 // FIXME: This function is a hack that works around the quirky AST
328 // we're often having with respect to C++ temporaries. If only we modelled
329 // the actual execution order of statements properly in the CFG,
330 // all the hassle with adjustments would not be necessary,
331 // and perhaps the whole function would be removed.
332 SVal InitValWithAdjustments = State->getSVal(InitWithAdjustments, SF);
333 if (!Result) {
334 // If we don't have an explicit result expression, we're in "if needed"
335 // mode. Only create a region if the current value is a NonLoc.
336 if (!isa<NonLoc>(InitValWithAdjustments)) {
337 if (OutRegionWithAdjustments)
338 *OutRegionWithAdjustments = nullptr;
339 return State;
340 }
341 Result = InitWithAdjustments;
342 } else {
343 // We need to create a region no matter what. Make sure we don't try to
344 // stuff a Loc into a non-pointer temporary region.
345 assert(!isa<Loc>(InitValWithAdjustments) ||
346 Loc::isLocType(Result->getType()) ||
347 Result->getType()->isMemberPointerType());
348 }
349
350 ProgramStateManager &StateMgr = State->getStateManager();
351 MemRegionManager &MRMgr = StateMgr.getRegionManager();
352 StoreManager &StoreMgr = StateMgr.getStoreManager();
353
354 // MaterializeTemporaryExpr may appear out of place, after a few field and
355 // base-class accesses have been made to the object, even though semantically
356 // it is the whole object that gets materialized and lifetime-extended.
357 //
358 // For example:
359 //
360 // `-MaterializeTemporaryExpr
361 // `-MemberExpr
362 // `-CXXTemporaryObjectExpr
363 //
364 // instead of the more natural
365 //
366 // `-MemberExpr
367 // `-MaterializeTemporaryExpr
368 // `-CXXTemporaryObjectExpr
369 //
370 // Use the usual methods for obtaining the expression of the base object,
371 // and record the adjustments that we need to make to obtain the sub-object
372 // that the whole expression 'Ex' refers to. This trick is usual,
373 // in the sense that CodeGen takes a similar route.
374
375 SmallVector<const Expr *, 2> CommaLHSs;
376 SmallVector<SubobjectAdjustment, 2> Adjustments;
377
378 const Expr *Init = InitWithAdjustments->skipRValueSubobjectAdjustments(
379 CommaLHSs, Adjustments);
380
381 // Take the region for Init, i.e. for the whole object. If we do not remember
382 // the region in which the object originally was constructed, come up with
383 // a new temporary region out of thin air and copy the contents of the object
384 // (which are currently present in the Environment, because Init is an rvalue)
385 // into that region. This is not correct, but it is better than nothing.
386 const TypedValueRegion *TR = nullptr;
387 if (const auto *MT = dyn_cast<MaterializeTemporaryExpr>(Result)) {
388 if (std::optional<SVal> V = getObjectUnderConstruction(State, MT, SF)) {
389 State = finishObjectConstruction(State, MT, SF);
390 State = State->BindExpr(Result, SF, *V);
391 return State;
392 } else if (const ValueDecl *VD = MT->getExtendingDecl()) {
393 StorageDuration SD = MT->getStorageDuration();
394 assert(SD != SD_FullExpression);
395 // If this object is bound to a reference with static storage duration, we
396 // put it in a different region to prevent "address leakage" warnings.
397 if (SD == SD_Static || SD == SD_Thread) {
398 TR = MRMgr.getCXXStaticLifetimeExtendedObjectRegion(Init, VD);
399 } else {
400 TR = MRMgr.getCXXLifetimeExtendedObjectRegion(Init, VD, SF);
401 }
402 } else {
403 assert(MT->getStorageDuration() == SD_FullExpression);
404 TR = MRMgr.getCXXTempObjectRegion(Init, SF);
405 }
406 } else {
407 TR = MRMgr.getCXXTempObjectRegion(Init, SF);
408 }
409
410 SVal Reg = loc::MemRegionVal(TR);
411 SVal BaseReg = Reg;
412
413 // Make the necessary adjustments to obtain the sub-object.
414 for (const SubobjectAdjustment &Adj : llvm::reverse(Adjustments)) {
415 switch (Adj.Kind) {
417 Reg = StoreMgr.evalDerivedToBase(Reg, Adj.DerivedToBase.BasePath);
418 break;
420 Reg = StoreMgr.getLValueField(Adj.Field, Reg);
421 break;
423 // FIXME: Unimplemented.
424 State = State->invalidateRegions(Reg, getCFGElementRef(),
425 getNumVisitedCurrent(), SF, true,
426 nullptr, nullptr, nullptr);
427 return State;
428 }
429 }
430
431 // What remains is to copy the value of the object to the new region.
432 // FIXME: In other words, what we should always do is copy value of the
433 // Init expression (which corresponds to the bigger object) to the whole
434 // temporary region TR. However, this value is often no longer present
435 // in the Environment. If it has disappeared, we instead invalidate TR.
436 // Still, what we can do is assign the value of expression Ex (which
437 // corresponds to the sub-object) to the TR's sub-region Reg. At least,
438 // values inside Reg would be correct.
439 SVal InitVal = State->getSVal(Init, SF);
440 if (InitVal.isUnknown()) {
442 getCFGElementRef(), SF, Init->getType(), getNumVisitedCurrent());
443 State = State->bindLoc(BaseReg.castAs<Loc>(), InitVal, SF, false);
444
445 // Then we'd need to take the value that certainly exists and bind it
446 // over.
447 if (InitValWithAdjustments.isUnknown()) {
448 // Try to recover some path sensitivity in case we couldn't
449 // compute the value.
450 InitValWithAdjustments = getSValBuilder().conjureSymbolVal(
451 getCFGElementRef(), SF, InitWithAdjustments->getType(),
453 }
454 State =
455 State->bindLoc(Reg.castAs<Loc>(), InitValWithAdjustments, SF, false);
456 } else {
457 State = State->bindLoc(BaseReg.castAs<Loc>(), InitVal, SF, false);
458 }
459
460 // The result expression would now point to the correct sub-region of the
461 // newly created temporary region. Do this last in order to getSVal of Init
462 // correctly in case (Result == Init).
463 if (Result->isGLValue()) {
464 State = State->BindExpr(Result, SF, Reg);
465 } else {
466 State = State->BindExpr(Result, SF, InitValWithAdjustments);
467 }
468
469 // Notify checkers once for two bindLoc()s.
470 State = processRegionChange(State, TR, SF);
471
472 if (OutRegionWithAdjustments)
473 *OutRegionWithAdjustments = cast<SubRegion>(Reg.getAsRegion());
474 return State;
475}
476
478ExprEngine::setIndexOfElementToConstruct(ProgramStateRef State,
479 const CXXConstructExpr *E,
480 const StackFrame *SF, unsigned Idx) {
481 auto Key = std::make_pair(E, SF);
482
483 assert(!State->contains<IndexOfElementToConstruct>(Key) || Idx > 0);
484
485 return State->set<IndexOfElementToConstruct>(Key, Idx);
486}
487
488std::optional<unsigned>
490 const StackFrame *SF) {
491 const unsigned *V = State->get<PendingInitLoop>({E, SF});
492 return V ? std::make_optional(*V) : std::nullopt;
493}
494
495ProgramStateRef ExprEngine::removePendingInitLoop(ProgramStateRef State,
496 const CXXConstructExpr *E,
497 const StackFrame *SF) {
498 auto Key = std::make_pair(E, SF);
499
500 assert(E && State->contains<PendingInitLoop>(Key));
501 return State->remove<PendingInitLoop>(Key);
502}
503
504ProgramStateRef ExprEngine::setPendingInitLoop(ProgramStateRef State,
505 const CXXConstructExpr *E,
506 const StackFrame *SF,
507 unsigned Size) {
508 auto Key = std::make_pair(E, SF);
509
510 assert(!State->contains<PendingInitLoop>(Key) && Size > 0);
511
512 return State->set<PendingInitLoop>(Key, Size);
513}
514
516 ProgramStateRef State, const CXXConstructExpr *E, const StackFrame *SF) {
517 const unsigned *V = State->get<IndexOfElementToConstruct>({E, SF});
518 return V ? std::make_optional(*V) : std::nullopt;
519}
520
521ProgramStateRef ExprEngine::removeIndexOfElementToConstruct(
522 ProgramStateRef State, const CXXConstructExpr *E, const StackFrame *SF) {
523 auto Key = std::make_pair(E, SF);
524
525 assert(E && State->contains<IndexOfElementToConstruct>(Key));
526 return State->remove<IndexOfElementToConstruct>(Key);
527}
528
529std::optional<unsigned>
531 const StackFrame *SF) {
532 assert(SF && "StackFrame shouldn't be null!");
533
534 const unsigned *V = State->get<PendingArrayDestruction>(SF);
535 return V ? std::make_optional(*V) : std::nullopt;
536}
537
538ProgramStateRef ExprEngine::setPendingArrayDestruction(ProgramStateRef State,
539 const StackFrame *SF,
540 unsigned Idx) {
541 assert(SF && "StackFrame shouldn't be null!");
542 return State->set<PendingArrayDestruction>(SF, Idx);
543}
544
546ExprEngine::removePendingArrayDestruction(ProgramStateRef State,
547 const StackFrame *SF) {
548 assert(SF && "StackFrame shouldn't be null!");
549 assert(State->contains<PendingArrayDestruction>(SF));
550 return State->remove<PendingArrayDestruction>(SF);
551}
552
554ExprEngine::addObjectUnderConstruction(ProgramStateRef State,
555 const ConstructionContextItem &Item,
556 const StackFrame *SF, SVal V) {
557 ConstructedObjectKey Key(Item, SF);
558
559 const Expr *Init = nullptr;
560
561 if (auto DS = dyn_cast_or_null<DeclStmt>(Item.getStmtOrNull())) {
562 if (auto VD = dyn_cast_or_null<VarDecl>(DS->getSingleDecl()))
563 Init = VD->getInit();
564 }
565
566 if (auto LE = dyn_cast_or_null<LambdaExpr>(Item.getStmtOrNull()))
567 Init = *(LE->capture_init_begin() + Item.getIndex());
568
569 if (!Init && !Item.getStmtOrNull())
571
572 // In an ArrayInitLoopExpr the real initializer is returned by
573 // getSubExpr(). Note that AILEs can be nested in case of
574 // multidimesnional arrays.
575 if (const auto *AILE = dyn_cast_or_null<ArrayInitLoopExpr>(Init))
577
578 // FIXME: Currently the state might already contain the marker due to
579 // incorrect handling of temporaries bound to default parameters.
580 // The state will already contain the marker if we construct elements
581 // in an array, as we visit the same statement multiple times before
582 // the array declaration. The marker is removed when we exit the
583 // constructor call.
584 assert((!State->get<ObjectsUnderConstruction>(Key) ||
585 Key.getItem().getKind() ==
587 State->contains<IndexOfElementToConstruct>(
588 {dyn_cast_or_null<CXXConstructExpr>(Init), SF})) &&
589 "The object is already marked as `UnderConstruction`, when it's not "
590 "supposed to!");
591 return State->set<ObjectsUnderConstruction>(Key, V);
592}
593
594std::optional<SVal>
596 const ConstructionContextItem &Item,
597 const StackFrame *SF) {
598 ConstructedObjectKey Key(Item, SF);
599 const SVal *V = State->get<ObjectsUnderConstruction>(Key);
600 return V ? std::make_optional(*V) : std::nullopt;
601}
602
604ExprEngine::finishObjectConstruction(ProgramStateRef State,
605 const ConstructionContextItem &Item,
606 const StackFrame *SF) {
607 ConstructedObjectKey Key(Item, SF);
608 assert(State->contains<ObjectsUnderConstruction>(Key));
609 return State->remove<ObjectsUnderConstruction>(Key);
610}
611
612ProgramStateRef ExprEngine::elideDestructor(ProgramStateRef State,
613 const CXXBindTemporaryExpr *BTE,
614 const StackFrame *SF) {
615 ConstructedObjectKey Key({BTE, /*IsElided=*/true}, SF);
616 // FIXME: Currently the state might already contain the marker due to
617 // incorrect handling of temporaries bound to default parameters.
618 return State->set<ObjectsUnderConstruction>(Key, UnknownVal());
619}
620
622ExprEngine::cleanupElidedDestructor(ProgramStateRef State,
623 const CXXBindTemporaryExpr *BTE,
624 const StackFrame *SF) {
625 ConstructedObjectKey Key({BTE, /*IsElided=*/true}, SF);
626 assert(State->contains<ObjectsUnderConstruction>(Key));
627 return State->remove<ObjectsUnderConstruction>(Key);
628}
629
630bool ExprEngine::isDestructorElided(ProgramStateRef State,
631 const CXXBindTemporaryExpr *BTE,
632 const StackFrame *SF) {
633 ConstructedObjectKey Key({BTE, /*IsElided=*/true}, SF);
634 return State->contains<ObjectsUnderConstruction>(Key);
635}
636
637bool ExprEngine::areAllObjectsFullyConstructed(ProgramStateRef State,
638 const StackFrame *FromSF,
639 const StackFrame *ToSF) {
640 const StackFrame *SF = FromSF;
641 while (SF != ToSF) {
642 assert(SF && "ToSF must be a parent of FromSF!");
643 for (auto I : State->get<ObjectsUnderConstruction>())
644 if (I.first.getStackFrame() == SF)
645 return false;
646
647 SF = SF->getParent();
648 }
649 return true;
650}
651
652//===----------------------------------------------------------------------===//
653// Top-level transfer function logic (Dispatcher).
654//===----------------------------------------------------------------------===//
655
656/// evalAssume - Called by ConstraintManager. Used to call checker-specific
657/// logic for handling assumptions on symbolic values.
659 SVal cond, bool assumption) {
660 return getCheckerManager().runCheckersForEvalAssume(state, cond, assumption);
661}
662
664 ProgramStateRef state, const InvalidatedSymbols *invalidated,
666 const StackFrame *SF, const CallEvent *Call) {
668 state, invalidated, Explicits, Regions, SF, Call);
669}
670
671static void
673 const char *NL, const StackFrame *SF,
674 unsigned int Space = 0, bool IsDot = false) {
675 PrintingPolicy PP =
677
678 ++Space;
679 bool HasItem = false;
680
681 // Store the last key.
682 const ConstructedObjectKey *LastKey = nullptr;
683 for (const auto &I : State->get<ObjectsUnderConstruction>()) {
684 const ConstructedObjectKey &Key = I.first;
685 if (Key.getStackFrame() != SF)
686 continue;
687
688 if (!HasItem) {
689 Out << '[' << NL;
690 HasItem = true;
691 }
692
693 LastKey = &Key;
694 }
695
696 for (const auto &I : State->get<ObjectsUnderConstruction>()) {
697 const ConstructedObjectKey &Key = I.first;
698 SVal Value = I.second;
699 if (Key.getStackFrame() != SF)
700 continue;
701
702 Indent(Out, Space, IsDot) << "{ ";
703 Key.printJson(Out, nullptr, PP);
704 Out << ", \"value\": \"" << Value << "\" }";
705
706 if (&Key != LastKey)
707 Out << ',';
708 Out << NL;
709 }
710
711 if (HasItem)
712 Indent(Out, --Space, IsDot) << ']'; // End of "location_context".
713 else {
714 Out << "null ";
715 }
716}
717
719 raw_ostream &Out, ProgramStateRef State, const char *NL,
720 const StackFrame *SF, unsigned int Space = 0, bool IsDot = false) {
721 using KeyT = std::pair<const Expr *, const StackFrame *>;
722
723 const auto &Context = SF->getAnalysisDeclContext()->getASTContext();
724 PrintingPolicy PP = Context.getPrintingPolicy();
725
726 ++Space;
727 bool HasItem = false;
728
729 // Store the last key.
730 KeyT LastKey;
731 for (const auto &I : State->get<IndexOfElementToConstruct>()) {
732 const KeyT &Key = I.first;
733 if (Key.second != SF)
734 continue;
735
736 if (!HasItem) {
737 Out << '[' << NL;
738 HasItem = true;
739 }
740
741 LastKey = Key;
742 }
743
744 for (const auto &I : State->get<IndexOfElementToConstruct>()) {
745 const KeyT &Key = I.first;
746 unsigned Value = I.second;
747 if (Key.second != SF)
748 continue;
749
750 Indent(Out, Space, IsDot) << "{ ";
751
752 // Expr
753 const Expr *E = Key.first;
754 Out << "\"stmt_id\": " << E->getID(Context);
755
756 // Kind
757 Out << ", \"kind\": null";
758
759 // Pretty-print
760 Out << ", \"pretty\": ";
761 Out << "\"" << E->getStmtClassName() << ' '
762 << E->getSourceRange().printToString(Context.getSourceManager()) << " '"
763 << QualType::getAsString(E->getType().split(), PP);
764 Out << "'\"";
765
766 Out << ", \"value\": \"Current index: " << Value - 1 << "\" }";
767
768 if (Key != LastKey)
769 Out << ',';
770 Out << NL;
771 }
772
773 if (HasItem)
774 Indent(Out, --Space, IsDot) << ']'; // End of "location_context".
775 else {
776 Out << "null ";
777 }
778}
779
780static void printPendingInitLoopJson(raw_ostream &Out, ProgramStateRef State,
781 const char *NL, const StackFrame *SF,
782 unsigned int Space = 0,
783 bool IsDot = false) {
784 using KeyT = std::pair<const CXXConstructExpr *, const StackFrame *>;
785
786 const auto &Context = SF->getAnalysisDeclContext()->getASTContext();
787 PrintingPolicy PP = Context.getPrintingPolicy();
788
789 ++Space;
790 bool HasItem = false;
791
792 // Store the last key.
793 KeyT LastKey;
794 for (const auto &I : State->get<PendingInitLoop>()) {
795 const KeyT &Key = I.first;
796 if (Key.second != SF)
797 continue;
798
799 if (!HasItem) {
800 Out << '[' << NL;
801 HasItem = true;
802 }
803
804 LastKey = Key;
805 }
806
807 for (const auto &I : State->get<PendingInitLoop>()) {
808 const KeyT &Key = I.first;
809 unsigned Value = I.second;
810 if (Key.second != SF)
811 continue;
812
813 Indent(Out, Space, IsDot) << "{ ";
814
815 const CXXConstructExpr *E = Key.first;
816 Out << "\"stmt_id\": " << E->getID(Context);
817
818 Out << ", \"kind\": null";
819 Out << ", \"pretty\": ";
820 Out << '\"' << E->getStmtClassName() << ' '
821 << E->getSourceRange().printToString(Context.getSourceManager()) << " '"
822 << QualType::getAsString(E->getType().split(), PP);
823 Out << "'\"";
824
825 Out << ", \"value\": \"Flattened size: " << Value << "\"}";
826
827 if (Key != LastKey)
828 Out << ',';
829 Out << NL;
830 }
831
832 if (HasItem)
833 Indent(Out, --Space, IsDot) << ']'; // End of "location_context".
834 else {
835 Out << "null ";
836 }
837}
838
839static void
841 const char *NL, const StackFrame *SF,
842 unsigned int Space = 0, bool IsDot = false) {
843 using KeyT = const StackFrame *;
844
845 ++Space;
846 bool HasItem = false;
847
848 // Store the last key.
849 KeyT LastKey = nullptr;
850 for (const auto &I : State->get<PendingArrayDestruction>()) {
851 const KeyT &Key = I.first;
852 if (Key != SF)
853 continue;
854
855 if (!HasItem) {
856 Out << '[' << NL;
857 HasItem = true;
858 }
859
860 LastKey = Key;
861 }
862
863 for (const auto &I : State->get<PendingArrayDestruction>()) {
864 const KeyT &Key = I.first;
865 if (Key != SF)
866 continue;
867
868 Indent(Out, Space, IsDot) << "{ ";
869
870 Out << "\"stmt_id\": null";
871 Out << ", \"kind\": null";
872 Out << ", \"pretty\": \"Current index: \"";
873 Out << ", \"value\": \"" << I.second << "\" }";
874
875 if (Key != LastKey)
876 Out << ',';
877 Out << NL;
878 }
879
880 if (HasItem)
881 Indent(Out, --Space, IsDot) << ']'; // End of "location_context".
882 else {
883 Out << "null ";
884 }
885}
886
887/// A helper function to generalize program state trait printing.
888/// The function invokes Printer as 'Printer(Out, State, NL, SF, Space, IsDot,
889/// std::forward<Args>(args)...)'. \n One possible type for Printer is
890/// 'void()(raw_ostream &, ProgramStateRef, const char *, const StackFrame *,
891/// unsigned int, bool, ...)' \n \param Trait The state trait to be printed.
892/// \param Printer A void function that prints Trait.
893/// \param Args An additional parameter pack that is passed to Print upon
894/// invocation.
895template <typename Trait, typename Printer, typename... Args>
897 raw_ostream &Out, ProgramStateRef State, const StackFrame *SF,
898 const char *NL, unsigned int Space, bool IsDot,
899 const char *jsonPropertyName, Printer printer, Args &&...args) {
900
901 using RequiredType =
902 void (*)(raw_ostream &, ProgramStateRef, const char *, const StackFrame *,
903 unsigned int, bool, Args &&...);
904
905 // Try to do as much compile time checking as possible.
906 // FIXME: check for invocable instead of function?
907 static_assert(std::is_function_v<std::remove_pointer_t<Printer>>,
908 "Printer is not a function!");
909 static_assert(std::is_convertible_v<Printer, RequiredType>,
910 "Printer doesn't have the required type!");
911
912 if (SF && !State->get<Trait>().isEmpty()) {
913 Indent(Out, Space, IsDot) << '\"' << jsonPropertyName << "\": ";
914 ++Space;
915 Out << '[' << NL;
916 SF->printJson(Out, NL, Space, IsDot, [&](const StackFrame *SF) {
917 printer(Out, State, NL, SF, Space, IsDot, std::forward<Args>(args)...);
918 });
919
920 --Space;
921 Indent(Out, Space, IsDot) << "]," << NL; // End of "jsonPropertyName".
922 }
923}
924
925void ExprEngine::printJson(raw_ostream &Out, ProgramStateRef State,
926 const StackFrame *SF, const char *NL,
927 unsigned int Space, bool IsDot) const {
928
930 Out, State, SF, NL, Space, IsDot, "constructing_objects",
933 Out, State, SF, NL, Space, IsDot, "index_of_element",
936 Out, State, SF, NL, Space, IsDot, "pending_init_loops",
939 Out, State, SF, NL, Space, IsDot, "pending_destructors",
941
942 getCheckerManager().runCheckersForPrintStateJson(Out, State, NL, Space,
943 IsDot);
944}
945
947 // This prints the name of the top-level function if we crash.
950}
951
953 unsigned StmtIdx) {
954 currStmtIdx = StmtIdx;
955
956 switch (E.getKind()) {
960 ProcessStmt(E.castAs<CFGStmt>().getStmt(), Pred);
961 return;
964 return;
967 Pred);
968 return;
975 return;
978 return;
981 E.castAs<CFGLifetimeEnds>().getVarDecl(), Pred);
982 return;
987 return;
988 }
989}
990
992 const ExplodedNode *Pred,
993 const StackFrame *SF) {
994 // Are we never purging state values?
995 if (AMgr.options.AnalysisPurgeOpt == PurgeNone)
996 return false;
997
998 // Is this the beginning of a basic block?
999 if (Pred->getLocation().getAs<BlockEntrance>())
1000 return true;
1001
1002 // Is this on a non-expression?
1003 if (!isa<Expr>(S))
1004 return true;
1005
1006 // Run before processing a call.
1007 if (CallEvent::isCallStmt(S))
1008 return true;
1009
1010 // Is this an expression that is consumed by another expression? If so,
1011 // postpone cleaning out the state.
1013 return !PM.isConsumedExpr(cast<Expr>(S));
1014}
1015
1017 const Stmt *ReferenceStmt, const StackFrame *SF,
1018 const Stmt *DiagnosticStmt, ProgramPoint::Kind K) {
1019 llvm::TimeTraceScope TimeScope("ExprEngine::removeDead");
1021 ReferenceStmt == nullptr || isa<ReturnStmt>(ReferenceStmt))
1022 && "PostStmt is not generally supported by the SymbolReaper yet");
1023 assert(SF && "Must pass the current (or expiring) StackFrame");
1024
1025 if (!DiagnosticStmt) {
1026 DiagnosticStmt = ReferenceStmt;
1027 assert(DiagnosticStmt && "Required for clearing a StackFrame");
1028 }
1029
1030 NumRemoveDeadBindings++;
1031 ProgramStateRef CleanedState = Pred->getState();
1032
1033 // SF is the stack frame being destroyed, but SymbolReaper wants a
1034 // stack frame that is still live. (If this is the top-level stack
1035 // frame, this will be null.)
1036 if (!ReferenceStmt) {
1038 "Use PostStmtPurgeDeadSymbolsKind for clearing a StackFrame");
1039 SF = SF->getParent();
1040 }
1041
1042 SymbolReaper SymReaper(SF, ReferenceStmt, SymMgr, getStoreManager());
1043
1044 for (auto I : CleanedState->get<ObjectsUnderConstruction>()) {
1045 if (SymbolRef Sym = I.second.getAsSymbol())
1046 SymReaper.markLive(Sym);
1047 if (const MemRegion *MR = I.second.getAsRegion())
1048 SymReaper.markLive(MR);
1049 }
1050
1051 getCheckerManager().runCheckersForLiveSymbols(CleanedState, SymReaper);
1052
1053 // Create a state in which dead bindings are removed from the environment
1054 // and the store. TODO: The function should just return new env and store,
1055 // not a new state.
1056 CleanedState = StateMgr.removeDeadBindingsFromEnvironmentAndStore(
1057 CleanedState, SF, SymReaper);
1058
1059 // Process any special transfer function for dead symbols.
1060 // Call checkers with the non-cleaned state so that they could query the
1061 // values of the soon to be dead symbols.
1062 ExplodedNodeSet CheckedSet;
1063 getCheckerManager().runCheckersForDeadSymbols(CheckedSet, Pred, SymReaper,
1064 DiagnosticStmt, *this, K);
1065
1066 // Extend lifetime of symbols used for dynamic extent while the parent region
1067 // is live. In this way size information about memory allocations is not lost
1068 // if the region remains live.
1069 markAllDynamicExtentLive(CleanedState, SymReaper);
1070
1071 // For each node in CheckedSet, generate CleanedNodes that have the
1072 // environment, the store, and the constraints cleaned up but have the
1073 // user-supplied states as the predecessors.
1074 for (const auto I : CheckedSet) {
1075 ProgramStateRef CheckerState = I->getState();
1076
1077 // The constraint manager has not been cleaned up yet, so clean up now.
1078 CheckerState =
1079 getConstraintManager().removeDeadBindings(CheckerState, SymReaper);
1080
1081 assert(StateMgr.haveEqualEnvironments(CheckerState, Pred->getState()) &&
1082 "Checkers are not allowed to modify the Environment as a part of "
1083 "checkDeadSymbols processing.");
1084 assert(StateMgr.haveEqualStores(CheckerState, Pred->getState()) &&
1085 "Checkers are not allowed to modify the Store as a part of "
1086 "checkDeadSymbols processing.");
1087
1088 // Create a state based on CleanedState with CheckerState GDM and
1089 // generate a transition to that state.
1090 ProgramStateRef CleanedCheckerSt =
1091 StateMgr.getPersistentStateWithGDM(CleanedState, CheckerState);
1093 DiagnosticStmt, K, I->getStackFrame(), cleanupNodeTag());
1094 Out.insert(Engine.makeNode(L, CleanedCheckerSt, I));
1095 }
1096}
1097
1099 static SimpleProgramPointTag cleanupTag(TagProviderName, "Clean Node");
1100 return &cleanupTag;
1101}
1102
1103void ExprEngine::ProcessStmt(const Stmt *currStmt, ExplodedNode *Pred) {
1104 // Reclaim any unnecessary nodes in the ExplodedGraph.
1105 G.reclaimRecentlyAllocatedNodes();
1106
1107 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
1108 currStmt->getBeginLoc(),
1109 "Error evaluating statement");
1110
1111 // Remove dead bindings and symbols.
1112 ExplodedNodeSet CleanedStates;
1113 if (shouldRemoveDeadBindings(AMgr, currStmt, Pred, Pred->getStackFrame())) {
1114 removeDead(Pred, CleanedStates, currStmt, Pred->getStackFrame());
1115 } else
1116 CleanedStates.insert(Pred);
1117
1118 // Visit the statement.
1119 ExplodedNodeSet Dst;
1120 for (const auto I : CleanedStates) {
1121 ExplodedNodeSet DstI;
1122 // Visit the statement.
1123 Visit(currStmt, I, DstI);
1124 Dst.insert(DstI);
1125 }
1126
1127 // Enqueue the new nodes onto the work list.
1128 Engine.enqueueStmtNodes(Dst, getCurrBlock(), currStmtIdx);
1129}
1130
1132 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
1133 S->getBeginLoc(),
1134 "Error evaluating end of the loop");
1135 ProgramStateRef NewState = Pred->getState();
1136
1137 if(AMgr.options.ShouldUnrollLoops)
1138 NewState = processLoopEnd(S, NewState);
1139
1140 LoopExit PP(S, Pred->getStackFrame());
1141 ExplodedNode *N = Engine.makeNode(PP, NewState, Pred);
1142 if (N && !N->isSink())
1143 Engine.enqueueStmtNode(N, getCurrBlock(), currStmtIdx);
1144}
1145
1147 ExplodedNode *Pred) {
1148 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
1149 S->getBeginLoc(),
1150 "Error evaluating end of a lifetime");
1151 ExplodedNodeSet Src;
1152 NodeBuilder Bldr(Pred, Src, *currBldrCtx);
1153 LifetimeEnd PP(S, D, Pred->getStackFrame());
1154 Bldr.generateNode(PP, Pred->getState(), Pred);
1155
1156 ExplodedNodeSet Dst;
1157 getCheckerManager().runCheckersForLifetimeEnd(Dst, Src, D, *this);
1158 Engine.enqueueStmtNodes(Dst, currBldrCtx->getBlock(), currStmtIdx);
1159}
1160
1162 ExplodedNode *Pred) {
1163 const CXXCtorInitializer *BMI = CFGInit.getInitializer();
1164 const Expr *Init = BMI->getInit()->IgnoreImplicit();
1165 const StackFrame *SF = Pred->getStackFrame();
1166
1167 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
1168 BMI->getSourceLocation(),
1169 "Error evaluating initializer");
1170
1171 // We don't clean up dead bindings here.
1172 const auto *decl = cast<CXXConstructorDecl>(SF->getDecl());
1173
1174 ProgramStateRef State = Pred->getState();
1175 SVal thisVal = State->getSVal(svalBuilder.getCXXThis(decl, SF));
1176
1177 ExplodedNodeSet Tmp;
1178 SVal FieldLoc;
1179
1180 // Evaluate the initializer, if necessary
1181 if (BMI->isAnyMemberInitializer()) {
1182 // Constructors build the object directly in the field,
1183 // but non-objects must be copied in from the initializer.
1184 if (getObjectUnderConstruction(State, BMI, SF)) {
1185 // The field was directly constructed, so there is no need to bind.
1186 // But we still need to stop tracking the object under construction.
1187 State = finishObjectConstruction(State, BMI, SF);
1188 PostStore PS(Init, SF, /*Loc*/ nullptr, /*tag*/ nullptr);
1189 Tmp.insert(Engine.makeNode(PS, State, Pred));
1190 } else {
1191 const ValueDecl *Field;
1192 if (BMI->isIndirectMemberInitializer()) {
1193 Field = BMI->getIndirectMember();
1194 FieldLoc = State->getLValue(BMI->getIndirectMember(), thisVal);
1195 } else {
1196 Field = BMI->getMember();
1197 FieldLoc = State->getLValue(BMI->getMember(), thisVal);
1198 }
1199
1200 SVal InitVal;
1201 if (Init->getType()->isArrayType()) {
1202 // Handle arrays of trivial type. We can represent this with a
1203 // primitive load/copy from the base array region.
1204 const ArraySubscriptExpr *ASE;
1205 while ((ASE = dyn_cast<ArraySubscriptExpr>(Init)))
1206 Init = ASE->getBase()->IgnoreImplicit();
1207
1208 InitVal = State->getSVal(Init, SF);
1209
1210 // If we fail to get the value for some reason, use a symbolic value.
1211 if (InitVal.isUnknownOrUndef()) {
1212 SValBuilder &SVB = getSValBuilder();
1213 InitVal = SVB.conjureSymbolVal(
1214 getCFGElementRef(), SF, Field->getType(), getNumVisitedCurrent());
1215 }
1216 } else {
1217 InitVal = State->getSVal(BMI->getInit(), SF);
1218 }
1219
1220 PostInitializer PP(BMI, FieldLoc.getAsRegion(), SF);
1221 evalBind(Tmp, Init, Pred, FieldLoc, InitVal, /*isInit=*/true, &PP);
1222 }
1223 } else if (BMI->isBaseInitializer() && isa<InitListExpr>(Init)) {
1224 // When the base class is initialized with an initialization list and the
1225 // base class does not have a ctor, there will not be a CXXConstructExpr to
1226 // initialize the base region. Hence, we need to make the bind for it.
1228 thisVal, QualType(BMI->getBaseClass(), 0), BMI->isBaseVirtual());
1229 SVal InitVal = State->getSVal(Init, SF);
1230 evalBind(Tmp, Init, Pred, BaseLoc, InitVal, /*isInit=*/true);
1231 } else {
1232 assert(BMI->isBaseInitializer() || BMI->isDelegatingInitializer());
1233 Tmp.insert(Pred);
1234 // We already did all the work when visiting the CXXConstructExpr.
1235 }
1236
1237 // Construct PostInitializer nodes whether the state changed or not,
1238 // so that the diagnostics don't get confused.
1239 PostInitializer PP(BMI, FieldLoc.getAsRegion(), SF);
1240
1241 ExplodedNodeSet Dst;
1242 for (ExplodedNode *Pred : Tmp)
1243 Dst.insert(Engine.makeNode(PP, Pred->getState(), Pred));
1244 // Enqueue the new nodes onto the work list.
1245 Engine.enqueueStmtNodes(Dst, getCurrBlock(), currStmtIdx);
1246}
1247
1248std::pair<ProgramStateRef, uint64_t>
1249ExprEngine::prepareStateForArrayDestruction(const ProgramStateRef State,
1250 const MemRegion *Region,
1251 const QualType &ElementTy,
1252 const StackFrame *SF,
1253 SVal *ElementCountVal) {
1254 assert(Region != nullptr && "Not-null region expected");
1255
1256 QualType Ty = ElementTy.getDesugaredType(getContext());
1257 while (const auto *NTy = dyn_cast<ArrayType>(Ty))
1258 Ty = NTy->getElementType().getDesugaredType(getContext());
1259
1260 auto ElementCount = getDynamicElementCount(State, Region, svalBuilder, Ty);
1261
1262 if (ElementCountVal)
1263 *ElementCountVal = ElementCount;
1264
1265 // Note: the destructors are called in reverse order.
1266 unsigned Idx = 0;
1267 if (auto OptionalIdx = getPendingArrayDestruction(State, SF)) {
1268 Idx = *OptionalIdx;
1269 } else {
1270 // The element count is either unknown, or an SVal that's not an integer.
1271 if (!ElementCount.isConstant())
1272 return {State, 0};
1273
1274 Idx = ElementCount.getAsInteger()->getLimitedValue();
1275 }
1276
1277 if (Idx == 0)
1278 return {State, 0};
1279
1280 --Idx;
1281
1282 return {setPendingArrayDestruction(State, SF, Idx), Idx};
1283}
1284
1286 ExplodedNode *Pred) {
1287 ExplodedNodeSet Dst;
1288 switch (D.getKind()) {
1291 break;
1293 ProcessBaseDtor(D.castAs<CFGBaseDtor>(), Pred, Dst);
1294 break;
1296 ProcessMemberDtor(D.castAs<CFGMemberDtor>(), Pred, Dst);
1297 break;
1300 break;
1302 ProcessDeleteDtor(D.castAs<CFGDeleteDtor>(), Pred, Dst);
1303 break;
1304 default:
1305 llvm_unreachable("Unexpected dtor kind.");
1306 }
1307
1308 // Enqueue the new nodes onto the work list.
1309 Engine.enqueueStmtNodes(Dst, getCurrBlock(), currStmtIdx);
1310}
1311
1313 ExplodedNode *Pred) {
1314 ExplodedNodeSet Dst;
1316 AnalyzerOptions &Opts = AMgr.options;
1317 // TODO: We're not evaluating allocators for all cases just yet as
1318 // we're not handling the return value correctly, which causes false
1319 // positives when the alpha.cplusplus.NewDeleteLeaks check is on.
1320 if (Opts.MayInlineCXXAllocator)
1321 VisitCXXNewAllocatorCall(NE, Pred, Dst);
1322 else {
1323 const StackFrame *SF = Pred->getStackFrame();
1324 PostImplicitCall PP(NE->getOperatorNew(), NE->getBeginLoc(), SF,
1326 Dst.insert(Engine.makeNode(PP, Pred->getState(), Pred));
1327 }
1328 Engine.enqueueStmtNodes(Dst, getCurrBlock(), currStmtIdx);
1329}
1330
1332 ExplodedNode *Pred,
1333 ExplodedNodeSet &Dst) {
1334 const auto *DtorDecl = Dtor.getDestructorDecl(getContext());
1335 const VarDecl *varDecl = Dtor.getVarDecl();
1336 QualType varType = varDecl->getType();
1337
1338 ProgramStateRef state = Pred->getState();
1339 const StackFrame *SF = Pred->getStackFrame();
1340
1341 SVal dest = state->getLValue(varDecl, SF);
1342 const MemRegion *Region = dest.castAs<loc::MemRegionVal>().getRegion();
1343
1344 if (varType->isReferenceType()) {
1345 const MemRegion *ValueRegion = state->getSVal(Region).getAsRegion();
1346 if (!ValueRegion) {
1347 // FIXME: This should not happen. The language guarantees a presence
1348 // of a valid initializer here, so the reference shall not be undefined.
1349 // It seems that we're calling destructors over variables that
1350 // were not initialized yet.
1351 return;
1352 }
1353 Region = ValueRegion->getBaseRegion();
1354 varType = cast<TypedValueRegion>(Region)->getValueType();
1355 }
1356
1357 unsigned Idx = 0;
1358 if (isa<ArrayType>(varType)) {
1359 SVal ElementCount;
1360 std::tie(state, Idx) = prepareStateForArrayDestruction(
1361 state, Region, varType, SF, &ElementCount);
1362
1363 if (ElementCount.isConstant()) {
1364 uint64_t ArrayLength = ElementCount.getAsInteger()->getLimitedValue();
1365 assert(ArrayLength &&
1366 "An automatic dtor for a 0 length array shouldn't be triggered!");
1367
1368 // Still handle this case if we don't have assertions enabled.
1369 if (!ArrayLength) {
1370 static SimpleProgramPointTag PT(
1371 "ExprEngine", "Skipping automatic 0 length array destruction, "
1372 "which shouldn't be in the CFG.");
1373 PostImplicitCall PP(DtorDecl, varDecl->getLocation(), SF,
1374 getCFGElementRef(), &PT);
1375 Engine.makeNode(PP, Pred->getState(), Pred, /*MarkAsSink=*/true);
1376 return;
1377 }
1378 }
1379 }
1380
1381 EvalCallOptions CallOpts;
1382 Region = makeElementRegion(state, loc::MemRegionVal(Region), varType,
1383 CallOpts.IsArrayCtorOrDtor, Idx)
1384 .getAsRegion();
1385
1386 static SimpleProgramPointTag PT("ExprEngine",
1387 "Prepare for object destruction");
1388 PreImplicitCall PP(DtorDecl, varDecl->getLocation(), SF, getCFGElementRef(),
1389 &PT);
1390 Pred = Engine.makeNode(PP, state, Pred);
1391
1392 if (!Pred)
1393 return;
1394
1395 VisitCXXDestructor(varType, Region, Dtor.getTriggerStmt(),
1396 /*IsBase=*/false, Pred, Dst, CallOpts);
1397}
1398
1400 ExplodedNode *Pred,
1401 ExplodedNodeSet &Dst) {
1402 ProgramStateRef State = Pred->getState();
1403 const StackFrame *SF = Pred->getStackFrame();
1404 const CXXDeleteExpr *DE = Dtor.getDeleteExpr();
1405 const Expr *Arg = DE->getArgument();
1406 QualType DTy = DE->getDestroyedType();
1407 SVal ArgVal = State->getSVal(Arg, SF);
1408
1409 // If the argument to delete is known to be a null value,
1410 // don't run destructor.
1411 if (State->isNull(ArgVal).isConstrainedTrue()) {
1413 const CXXRecordDecl *RD = BTy->getAsCXXRecordDecl();
1414 const CXXDestructorDecl *Dtor = RD->getDestructor();
1415
1416 PostImplicitCall PP(Dtor, DE->getBeginLoc(), SF, getCFGElementRef());
1417 Dst.insert(Engine.makeNode(PP, Pred->getState(), Pred));
1418 return;
1419 }
1420
1421 auto getDtorDecl = [](const QualType &DTy) {
1422 const CXXRecordDecl *RD = DTy->getAsCXXRecordDecl();
1423 return RD->getDestructor();
1424 };
1425
1426 unsigned Idx = 0;
1427 EvalCallOptions CallOpts;
1428 const MemRegion *ArgR = ArgVal.getAsRegion();
1429
1430 if (DE->isArrayForm()) {
1431 CallOpts.IsArrayCtorOrDtor = true;
1432 // Yes, it may even be a multi-dimensional array.
1433 while (const auto *AT = getContext().getAsArrayType(DTy))
1434 DTy = AT->getElementType();
1435
1436 if (ArgR) {
1437 SVal ElementCount;
1438 std::tie(State, Idx) =
1439 prepareStateForArrayDestruction(State, ArgR, DTy, SF, &ElementCount);
1440
1441 // If we're about to destruct a 0 length array, don't run any of the
1442 // destructors.
1443 if (ElementCount.isConstant() &&
1444 ElementCount.getAsInteger()->getLimitedValue() == 0) {
1445
1446 static SimpleProgramPointTag PT(
1447 "ExprEngine", "Skipping 0 length array delete destruction");
1448 PostImplicitCall PP(getDtorDecl(DTy), DE->getBeginLoc(), SF,
1449 getCFGElementRef(), &PT);
1450 Dst.insert(Engine.makeNode(PP, Pred->getState(), Pred));
1451 return;
1452 }
1453
1454 ArgR = State->getLValue(DTy, svalBuilder.makeArrayIndex(Idx), ArgVal)
1455 .getAsRegion();
1456 }
1457 }
1458
1459 static SimpleProgramPointTag PT("ExprEngine",
1460 "Prepare for object destruction");
1461 PreImplicitCall PP(getDtorDecl(DTy), DE->getBeginLoc(), SF,
1462 getCFGElementRef(), &PT);
1463 Pred = Engine.makeNode(PP, State, Pred);
1464
1465 if (!Pred)
1466 return;
1467
1468 VisitCXXDestructor(DTy, ArgR, DE, /*IsBase=*/false, Pred, Dst, CallOpts);
1469}
1470
1472 ExplodedNode *Pred, ExplodedNodeSet &Dst) {
1473 const StackFrame *SF = Pred->getStackFrame();
1474
1475 const auto *CurDtor = cast<CXXDestructorDecl>(SF->getDecl());
1476 Loc ThisPtr = getSValBuilder().getCXXThis(CurDtor, SF);
1477 SVal ThisVal = Pred->getState()->getSVal(ThisPtr);
1478
1479 // Create the base object region.
1481 QualType BaseTy = Base->getType();
1482 SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, BaseTy,
1483 Base->isVirtual());
1484
1485 EvalCallOptions CallOpts;
1486 VisitCXXDestructor(BaseTy, BaseVal.getAsRegion(), CurDtor->getBody(),
1487 /*IsBase=*/true, Pred, Dst, CallOpts);
1488}
1489
1491 ExplodedNode *Pred, ExplodedNodeSet &Dst) {
1492 const auto *DtorDecl = D.getDestructorDecl(getContext());
1493 const FieldDecl *Member = D.getFieldDecl();
1494 QualType T = Member->getType();
1495 ProgramStateRef State = Pred->getState();
1496 const StackFrame *SF = Pred->getStackFrame();
1497
1498 const auto *CurDtor = cast<CXXDestructorDecl>(SF->getDecl());
1499 Loc ThisStorageLoc = getSValBuilder().getCXXThis(CurDtor, SF);
1500 Loc ThisLoc = State->getSVal(ThisStorageLoc).castAs<Loc>();
1501 SVal FieldVal = State->getLValue(Member, ThisLoc);
1502
1503 unsigned Idx = 0;
1504 if (isa<ArrayType>(T)) {
1505 SVal ElementCount;
1506 std::tie(State, Idx) = prepareStateForArrayDestruction(
1507 State, FieldVal.getAsRegion(), T, SF, &ElementCount);
1508
1509 if (ElementCount.isConstant()) {
1510 uint64_t ArrayLength = ElementCount.getAsInteger()->getLimitedValue();
1511 assert(ArrayLength &&
1512 "A member dtor for a 0 length array shouldn't be triggered!");
1513
1514 // Still handle this case if we don't have assertions enabled.
1515 if (!ArrayLength) {
1516 static SimpleProgramPointTag PT(
1517 "ExprEngine", "Skipping member 0 length array destruction, which "
1518 "shouldn't be in the CFG.");
1519 PostImplicitCall PP(DtorDecl, Member->getLocation(), SF,
1520 getCFGElementRef(), &PT);
1521 Engine.makeNode(PP, Pred->getState(), Pred, /*MarkAsSink=*/true);
1522 return;
1523 }
1524 }
1525 }
1526
1527 EvalCallOptions CallOpts;
1528 FieldVal =
1529 makeElementRegion(State, FieldVal, T, CallOpts.IsArrayCtorOrDtor, Idx);
1530
1531 static SimpleProgramPointTag PT("ExprEngine",
1532 "Prepare for object destruction");
1533 PreImplicitCall PP(DtorDecl, Member->getLocation(), SF, getCFGElementRef(),
1534 &PT);
1535 Pred = Engine.makeNode(PP, State, Pred);
1536
1537 if (!Pred)
1538 return;
1539
1540 VisitCXXDestructor(T, FieldVal.getAsRegion(), CurDtor->getBody(),
1541 /*IsBase=*/false, Pred, Dst, CallOpts);
1542}
1543
1545 ExplodedNode *Pred,
1546 ExplodedNodeSet &Dst) {
1548 ProgramStateRef State = Pred->getState();
1549 const StackFrame *SF = Pred->getStackFrame();
1550 const MemRegion *MR = nullptr;
1551
1552 if (std::optional<SVal> V = getObjectUnderConstruction(State, BTE, SF)) {
1553 // FIXME: Currently we insert temporary destructors for default parameters,
1554 // but we don't insert the constructors, so the entry in
1555 // ObjectsUnderConstruction may be missing.
1556 State = finishObjectConstruction(State, BTE, SF);
1557 MR = V->getAsRegion();
1558 }
1559
1560 // If copy elision has occurred, and the constructor corresponding to the
1561 // destructor was elided, we need to skip the destructor as well.
1562 if (isDestructorElided(State, BTE, SF)) {
1563 State = cleanupElidedDestructor(State, BTE, SF);
1565 SF, getCFGElementRef());
1566 Dst.insert(Engine.makeNode(PP, State, Pred));
1567 return;
1568 }
1569
1570 ExplodedNode *CleanPred = Engine.makePostStmtNode(BTE, State, Pred);
1571 if (!CleanPred || CleanPred->isSink()) {
1572 // FIXME: We can get a null node here due to temporaries being
1573 // bound to default parameters.
1574 // Sink check is just PosteriorlyOverconstrained paranoia.
1575 CleanPred = Pred;
1576 }
1577
1578 QualType T = BTE->getSubExpr()->getType();
1579
1580 EvalCallOptions CallOpts;
1581 CallOpts.IsTemporaryCtorOrDtor = true;
1582 if (!MR) {
1583 // FIXME: If we have no MR, we still need to unwrap the array to avoid
1584 // destroying the whole array at once.
1585 //
1586 // For this case there is no universal solution as there is no way to
1587 // directly create an array of temporary objects. There are some expressions
1588 // however which can create temporary objects and have an array type.
1589 //
1590 // E.g.: std::initializer_list<S>{S(), S()};
1591 //
1592 // The expression above has a type of 'const struct S[2]' but it's a single
1593 // 'std::initializer_list<>'. The destructors of the 2 temporary 'S()'
1594 // objects will be called anyway, because they are 2 separate objects in 2
1595 // separate clusters, i.e.: not an array.
1596 //
1597 // Now the 'std::initializer_list<>' is not an array either even though it
1598 // has the type of an array. The point is, we only want to invoke the
1599 // destructor for the initializer list once not twice or so.
1600 while (const ArrayType *AT = getContext().getAsArrayType(T)) {
1601 T = AT->getElementType();
1602
1603 // FIXME: Enable this flag once we handle this case properly.
1604 // CallOpts.IsArrayCtorOrDtor = true;
1605 }
1606 } else {
1607 // FIXME: We'd eventually need to makeElementRegion() trick here,
1608 // but for now we don't have the respective construction contexts,
1609 // so MR would always be null in this case. Do nothing for now.
1610 }
1611 VisitCXXDestructor(T, MR, BTE,
1612 /*IsBase=*/false, CleanPred, Dst, CallOpts);
1613}
1614
1616 ExplodedNode *Pred,
1617 ExplodedNodeSet &Dst,
1618 const CFGBlock *DstT,
1619 const CFGBlock *DstF) {
1620 ProgramStateRef State = Pred->getState();
1621 const StackFrame *SF = Pred->getStackFrame();
1622
1623 std::optional<SVal> Obj = getObjectUnderConstruction(State, BTE, SF);
1624 if (const CFGBlock *DstBlock = Obj ? DstT : DstF) {
1625 BlockEdge BE(getCurrBlock(), DstBlock, SF);
1626 Dst.insert(Engine.makeNode(BE, State, Pred));
1627 }
1628}
1629
1631 ExplodedNodeSet &PreVisit,
1632 ExplodedNodeSet &Dst) {
1633 // This is a fallback solution in case we didn't have a construction
1634 // context when we were constructing the temporary. Otherwise the map should
1635 // have been populated there.
1636 if (!getAnalysisManager().options.ShouldIncludeTemporaryDtorsInCFG) {
1637 // In case we don't have temporary destructors in the CFG, do not mark
1638 // the initialization - we would otherwise never clean it up.
1639 Dst = PreVisit;
1640 return;
1641 }
1642 NodeBuilder Builder(PreVisit, Dst, *currBldrCtx);
1643 for (ExplodedNode *Node : PreVisit) {
1644 ProgramStateRef State = Node->getState();
1645 const StackFrame *SF = Node->getStackFrame();
1646 if (!getObjectUnderConstruction(State, BTE, SF)) {
1647 // FIXME: Currently the state might also already contain the marker due to
1648 // incorrect handling of temporaries bound to default parameters; for
1649 // those, we currently skip the CXXBindTemporaryExpr but rely on adding
1650 // temporary destructor nodes.
1651 State = addObjectUnderConstruction(State, BTE, SF, UnknownVal());
1652 }
1653 Builder.generateNode(BTE, Node, State);
1654 }
1655}
1656
1658 ArrayRef<SVal> Vs,
1660 const CallEvent *Call) const {
1661 class CollectReachableSymbolsCallback final : public SymbolVisitor {
1662 InvalidatedSymbols &Symbols;
1663
1664 public:
1665 explicit CollectReachableSymbolsCallback(InvalidatedSymbols &Symbols)
1666 : Symbols(Symbols) {}
1667
1668 const InvalidatedSymbols &getSymbols() const { return Symbols; }
1669
1670 bool VisitSymbol(SymbolRef Sym) override {
1671 Symbols.insert(Sym);
1672 return true;
1673 }
1674 };
1675 InvalidatedSymbols Symbols;
1676 CollectReachableSymbolsCallback CallBack(Symbols);
1677 for (SVal V : Vs)
1678 State->scanReachableSymbols(V, CallBack);
1679
1681 State, CallBack.getSymbols(), Call, K, nullptr);
1682}
1683
1685 ExplodedNodeSet &Dst) {
1686 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
1687 S->getBeginLoc(), "Error evaluating statement");
1688
1689 assert(!isa<Expr>(S) || S == cast<Expr>(S)->IgnoreParens());
1690
1691 switch (S->getStmtClass()) {
1692 // C++, OpenMP and ARC stuff we don't support yet.
1693 case Stmt::CXXDependentScopeMemberExprClass:
1694 case Stmt::CXXReflectExprClass:
1695 case Stmt::CXXTryStmtClass:
1696 case Stmt::CXXTypeidExprClass:
1697 case Stmt::CXXUuidofExprClass:
1698 case Stmt::CXXFoldExprClass:
1699 case Stmt::MSPropertyRefExprClass:
1700 case Stmt::MSPropertySubscriptExprClass:
1701 case Stmt::CXXUnresolvedConstructExprClass:
1702 case Stmt::DependentScopeDeclRefExprClass:
1703 case Stmt::ArrayTypeTraitExprClass:
1704 case Stmt::ExpressionTraitExprClass:
1705 case Stmt::UnresolvedLookupExprClass:
1706 case Stmt::UnresolvedMemberExprClass:
1707 case Stmt::RecoveryExprClass:
1708 case Stmt::CXXNoexceptExprClass:
1709 case Stmt::PackExpansionExprClass:
1710 case Stmt::PackIndexingExprClass:
1711 case Stmt::SubstNonTypeTemplateParmPackExprClass:
1712 case Stmt::FunctionParmPackExprClass:
1713 case Stmt::CoroutineBodyStmtClass:
1714 case Stmt::CoawaitExprClass:
1715 case Stmt::DependentCoawaitExprClass:
1716 case Stmt::CoreturnStmtClass:
1717 case Stmt::CoyieldExprClass:
1718 case Stmt::SEHTryStmtClass:
1719 case Stmt::SEHExceptStmtClass:
1720 case Stmt::SEHLeaveStmtClass:
1721 case Stmt::SEHFinallyStmtClass:
1722 case Stmt::OMPCanonicalLoopClass:
1723 case Stmt::OMPParallelDirectiveClass:
1724 case Stmt::OMPSimdDirectiveClass:
1725 case Stmt::OMPForDirectiveClass:
1726 case Stmt::OMPForSimdDirectiveClass:
1727 case Stmt::OMPSectionsDirectiveClass:
1728 case Stmt::OMPSectionDirectiveClass:
1729 case Stmt::OMPScopeDirectiveClass:
1730 case Stmt::OMPSingleDirectiveClass:
1731 case Stmt::OMPMasterDirectiveClass:
1732 case Stmt::OMPCriticalDirectiveClass:
1733 case Stmt::OMPParallelForDirectiveClass:
1734 case Stmt::OMPParallelForSimdDirectiveClass:
1735 case Stmt::OMPParallelSectionsDirectiveClass:
1736 case Stmt::OMPParallelMasterDirectiveClass:
1737 case Stmt::OMPParallelMaskedDirectiveClass:
1738 case Stmt::OMPTaskDirectiveClass:
1739 case Stmt::OMPTaskyieldDirectiveClass:
1740 case Stmt::OMPBarrierDirectiveClass:
1741 case Stmt::OMPTaskwaitDirectiveClass:
1742 case Stmt::OMPErrorDirectiveClass:
1743 case Stmt::OMPTaskgroupDirectiveClass:
1744 case Stmt::OMPFlushDirectiveClass:
1745 case Stmt::OMPDepobjDirectiveClass:
1746 case Stmt::OMPScanDirectiveClass:
1747 case Stmt::OMPOrderedDirectiveClass:
1748 case Stmt::OMPAtomicDirectiveClass:
1749 case Stmt::OMPAssumeDirectiveClass:
1750 case Stmt::OMPTargetDirectiveClass:
1751 case Stmt::OMPTargetDataDirectiveClass:
1752 case Stmt::OMPTargetEnterDataDirectiveClass:
1753 case Stmt::OMPTargetExitDataDirectiveClass:
1754 case Stmt::OMPTargetParallelDirectiveClass:
1755 case Stmt::OMPTargetParallelForDirectiveClass:
1756 case Stmt::OMPTargetUpdateDirectiveClass:
1757 case Stmt::OMPTeamsDirectiveClass:
1758 case Stmt::OMPCancellationPointDirectiveClass:
1759 case Stmt::OMPCancelDirectiveClass:
1760 case Stmt::OMPTaskLoopDirectiveClass:
1761 case Stmt::OMPTaskLoopSimdDirectiveClass:
1762 case Stmt::OMPMasterTaskLoopDirectiveClass:
1763 case Stmt::OMPMaskedTaskLoopDirectiveClass:
1764 case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
1765 case Stmt::OMPMaskedTaskLoopSimdDirectiveClass:
1766 case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
1767 case Stmt::OMPParallelMaskedTaskLoopDirectiveClass:
1768 case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
1769 case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass:
1770 case Stmt::OMPDistributeDirectiveClass:
1771 case Stmt::OMPDistributeParallelForDirectiveClass:
1772 case Stmt::OMPDistributeParallelForSimdDirectiveClass:
1773 case Stmt::OMPDistributeSimdDirectiveClass:
1774 case Stmt::OMPTargetParallelForSimdDirectiveClass:
1775 case Stmt::OMPTargetSimdDirectiveClass:
1776 case Stmt::OMPTeamsDistributeDirectiveClass:
1777 case Stmt::OMPTeamsDistributeSimdDirectiveClass:
1778 case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
1779 case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
1780 case Stmt::OMPTargetTeamsDirectiveClass:
1781 case Stmt::OMPTargetTeamsDistributeDirectiveClass:
1782 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
1783 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
1784 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
1785 case Stmt::OMPReverseDirectiveClass:
1786 case Stmt::OMPStripeDirectiveClass:
1787 case Stmt::OMPTileDirectiveClass:
1788 case Stmt::OMPInterchangeDirectiveClass:
1789 case Stmt::OMPSplitDirectiveClass:
1790 case Stmt::OMPFuseDirectiveClass:
1791 case Stmt::OMPInteropDirectiveClass:
1792 case Stmt::OMPDispatchDirectiveClass:
1793 case Stmt::OMPMaskedDirectiveClass:
1794 case Stmt::OMPGenericLoopDirectiveClass:
1795 case Stmt::OMPTeamsGenericLoopDirectiveClass:
1796 case Stmt::OMPTargetTeamsGenericLoopDirectiveClass:
1797 case Stmt::OMPParallelGenericLoopDirectiveClass:
1798 case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
1799 case Stmt::CapturedStmtClass:
1800 case Stmt::SYCLKernelCallStmtClass:
1801 case Stmt::UnresolvedSYCLKernelCallStmtClass:
1802 case Stmt::OpenACCComputeConstructClass:
1803 case Stmt::OpenACCLoopConstructClass:
1804 case Stmt::OpenACCCombinedConstructClass:
1805 case Stmt::OpenACCDataConstructClass:
1806 case Stmt::OpenACCEnterDataConstructClass:
1807 case Stmt::OpenACCExitDataConstructClass:
1808 case Stmt::OpenACCHostDataConstructClass:
1809 case Stmt::OpenACCWaitConstructClass:
1810 case Stmt::OpenACCCacheConstructClass:
1811 case Stmt::OpenACCInitConstructClass:
1812 case Stmt::OpenACCShutdownConstructClass:
1813 case Stmt::OpenACCSetConstructClass:
1814 case Stmt::OpenACCUpdateConstructClass:
1815 case Stmt::OpenACCAtomicConstructClass:
1816 case Stmt::OMPUnrollDirectiveClass:
1817 case Stmt::OMPMetaDirectiveClass:
1818 case Stmt::HLSLOutArgExprClass: {
1819 const ExplodedNode *Node = Engine.makePostStmtNode(
1820 S, Pred->getState(), Pred, /*MarkAsSink=*/true);
1821 Engine.addAbortedBlock(Node, getCurrBlock());
1822 break;
1823 }
1824
1825 case Stmt::ParenExprClass:
1826 llvm_unreachable("ParenExprs already handled.");
1827 case Stmt::GenericSelectionExprClass:
1828 llvm_unreachable("GenericSelectionExprs already handled.");
1829 // Cases that should never be evaluated simply because they shouldn't
1830 // appear in the CFG.
1831 case Stmt::BreakStmtClass:
1832 case Stmt::CaseStmtClass:
1833 case Stmt::CompoundStmtClass:
1834 case Stmt::ContinueStmtClass:
1835 case Stmt::CXXForRangeStmtClass:
1836 case Stmt::DefaultStmtClass:
1837 case Stmt::DoStmtClass:
1838 case Stmt::ForStmtClass:
1839 case Stmt::GotoStmtClass:
1840 case Stmt::IfStmtClass:
1841 case Stmt::IndirectGotoStmtClass:
1842 case Stmt::LabelStmtClass:
1843 case Stmt::NoStmtClass:
1844 case Stmt::NullStmtClass:
1845 case Stmt::SwitchStmtClass:
1846 case Stmt::WhileStmtClass:
1847 case Stmt::DeferStmtClass:
1848 case Expr::MSDependentExistsStmtClass:
1849 llvm_unreachable("Stmt should not be in analyzer evaluation loop");
1850 case Stmt::ImplicitValueInitExprClass:
1851 // These nodes are shared in the CFG and would case caching out.
1852 // Moreover, no additional evaluation required for them, the
1853 // analyzer can reconstruct these values from the AST.
1854 llvm_unreachable("Should be pruned from CFG");
1855
1856 case Stmt::ObjCSubscriptRefExprClass:
1857 case Stmt::ObjCPropertyRefExprClass:
1858 llvm_unreachable("These are handled by PseudoObjectExpr");
1859
1860 case Stmt::GNUNullExprClass: {
1861 // GNU __null is a pointer-width integer, not an actual pointer.
1862 SVal Val = svalBuilder.makeIntValWithWidth(getContext().VoidPtrTy, 0);
1863 Dst.insert(Engine.makeNodeWithBinding(Pred, cast<Expr>(S), Val));
1864 break;
1865 }
1866
1867 case Stmt::ObjCAtSynchronizedStmtClass:
1869 break;
1870
1871 case Expr::ConstantExprClass:
1872 case Stmt::ExprWithCleanupsClass:
1873 Dst.insert(Pred);
1874 // Handled due to fully linearised CFG.
1875 break;
1876
1877 case Stmt::CXXBindTemporaryExprClass: {
1878 ExplodedNodeSet PreVisit;
1879 getCheckerManager().runCheckersForPreStmt(PreVisit, Pred, S, *this);
1883 break;
1884 }
1885
1886 case Stmt::ArrayInitLoopExprClass:
1888 break;
1889 // Cases not handled yet; but will handle some day.
1890 case Stmt::DesignatedInitExprClass:
1891 case Stmt::DesignatedInitUpdateExprClass:
1892 case Stmt::ArrayInitIndexExprClass:
1893 case Stmt::ExtVectorElementExprClass:
1894 case Stmt::MatrixElementExprClass:
1895 case Stmt::ImaginaryLiteralClass:
1896 case Stmt::ObjCAtCatchStmtClass:
1897 case Stmt::ObjCAtFinallyStmtClass:
1898 case Stmt::ObjCAtTryStmtClass:
1899 case Stmt::ObjCAutoreleasePoolStmtClass:
1900 case Stmt::ObjCEncodeExprClass:
1901 case Stmt::ObjCIsaExprClass:
1902 case Stmt::ObjCProtocolExprClass:
1903 case Stmt::ObjCSelectorExprClass:
1904 case Stmt::ParenListExprClass:
1905 case Stmt::ShuffleVectorExprClass:
1906 case Stmt::ConvertVectorExprClass:
1907 case Stmt::VAArgExprClass:
1908 case Stmt::CUDAKernelCallExprClass:
1909 case Stmt::OpaqueValueExprClass:
1910 case Stmt::AsTypeExprClass:
1911 case Stmt::ConceptSpecializationExprClass:
1912 case Stmt::CXXRewrittenBinaryOperatorClass:
1913 case Stmt::RequiresExprClass:
1914 case Stmt::EmbedExprClass:
1915 // Fall through.
1916
1917 // Cases we intentionally don't evaluate, since they don't need
1918 // to be explicitly evaluated.
1919 case Stmt::PredefinedExprClass:
1920 case Stmt::AddrLabelExprClass:
1921 case Stmt::IntegerLiteralClass:
1922 case Stmt::FixedPointLiteralClass:
1923 case Stmt::CharacterLiteralClass:
1924 case Stmt::CXXScalarValueInitExprClass:
1925 case Stmt::CXXBoolLiteralExprClass:
1926 case Stmt::ObjCBoolLiteralExprClass:
1927 case Stmt::ObjCAvailabilityCheckExprClass:
1928 case Stmt::FloatingLiteralClass:
1929 case Stmt::NoInitExprClass:
1930 case Stmt::SizeOfPackExprClass:
1931 case Stmt::StringLiteralClass:
1932 case Stmt::SourceLocExprClass:
1933 case Stmt::ObjCStringLiteralClass:
1934 case Stmt::CXXPseudoDestructorExprClass:
1935 case Stmt::SubstNonTypeTemplateParmExprClass:
1936 case Stmt::CXXNullPtrLiteralExprClass:
1937 case Stmt::ArraySectionExprClass:
1938 case Stmt::OMPArrayShapingExprClass:
1939 case Stmt::OMPIteratorExprClass:
1940 case Stmt::SYCLUniqueStableNameExprClass:
1941 case Stmt::OpenACCAsteriskSizeExprClass:
1942 case Stmt::TypeTraitExprClass: {
1943 ExplodedNodeSet preVisit;
1944 getCheckerManager().runCheckersForPreStmt(preVisit, Pred, S, *this);
1945 getCheckerManager().runCheckersForPostStmt(Dst, preVisit, S, *this);
1946 break;
1947 }
1948
1949 case Stmt::AttributedStmtClass: {
1951 break;
1952 }
1953
1954 case Stmt::CXXDefaultArgExprClass:
1955 case Stmt::CXXDefaultInitExprClass: {
1956 ExplodedNodeSet PreVisit;
1957 getCheckerManager().runCheckersForPreStmt(PreVisit, Pred, S, *this);
1958
1959 ExplodedNodeSet Tmp;
1960
1961 const Expr *ArgE;
1962 if (const auto *DefE = dyn_cast<CXXDefaultArgExpr>(S))
1963 ArgE = DefE->getExpr();
1964 else if (const auto *DefE = dyn_cast<CXXDefaultInitExpr>(S))
1965 ArgE = DefE->getExpr();
1966 else
1967 llvm_unreachable("unknown constant wrapper kind");
1968
1969 bool IsTemporary = false;
1970 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(ArgE)) {
1971 ArgE = MTE->getSubExpr();
1972 IsTemporary = true;
1973 }
1974
1975 std::optional<SVal> ConstantVal = svalBuilder.getConstantVal(ArgE);
1976 if (!ConstantVal)
1977 ConstantVal = UnknownVal();
1978
1979 const StackFrame *SF = Pred->getStackFrame();
1980 for (const auto I : PreVisit) {
1981 ProgramStateRef State = I->getState();
1982 State = State->BindExpr(cast<Expr>(S), SF, *ConstantVal);
1983 if (IsTemporary)
1984 State = createTemporaryRegionIfNeeded(State, SF, cast<Expr>(S),
1985 cast<Expr>(S));
1986 Tmp.insert(Engine.makePostStmtNode(S, State, I));
1987 }
1988
1989 getCheckerManager().runCheckersForPostStmt(Dst, Tmp, S, *this);
1990 break;
1991 }
1992
1993 // Cases we evaluate as opaque expressions, conjuring a symbol.
1994 case Stmt::CXXStdInitializerListExprClass:
1995 case Expr::ObjCArrayLiteralClass:
1996 case Expr::ObjCDictionaryLiteralClass:
1997 case Expr::ObjCBoxedExprClass: {
1998 ExplodedNodeSet preVisit;
1999 getCheckerManager().runCheckersForPreStmt(preVisit, Pred, S, *this);
2000
2001 ExplodedNodeSet Tmp;
2002
2003 const auto *Ex = cast<Expr>(S);
2004 QualType resultType = Ex->getType();
2005
2006 for (const auto N : preVisit) {
2007 const StackFrame *SF = N->getStackFrame();
2008 SVal result = svalBuilder.conjureSymbolVal(
2009 /*symbolTag=*/nullptr, getCFGElementRef(), SF, resultType,
2011 ProgramStateRef State = N->getState()->BindExpr(Ex, SF, result);
2012
2013 // Escape pointers passed into the list, unless it's an ObjC boxed
2014 // expression which is not a boxable C structure.
2015 if (!(isa<ObjCBoxedExpr>(Ex) &&
2016 !cast<ObjCBoxedExpr>(Ex)->getSubExpr()
2017 ->getType()->isRecordType()))
2018 for (auto Child : Ex->children()) {
2019 assert(Child);
2020 const auto *ChildExpr = dyn_cast<Expr>(Child);
2021 SVal Val = ChildExpr ? State->getSVal(ChildExpr, SF) : UnknownVal();
2022 State = escapeValues(State, Val, PSK_EscapeOther);
2023 }
2024
2025 Tmp.insert(Engine.makePostStmtNode(S, State, N));
2026 }
2027
2028 getCheckerManager().runCheckersForPostStmt(Dst, Tmp, S, *this);
2029 break;
2030 }
2031
2032 case Stmt::ArraySubscriptExprClass:
2034 break;
2035
2036 case Stmt::MatrixSingleSubscriptExprClass:
2037 llvm_unreachable(
2038 "Support for MatrixSingleSubscriptExprClass is not implemented.");
2039 break;
2040
2041 case Stmt::MatrixSubscriptExprClass:
2042 llvm_unreachable("Support for MatrixSubscriptExpr is not implemented.");
2043 break;
2044
2045 case Stmt::GCCAsmStmtClass: {
2046 ExplodedNodeSet PreVisit;
2047 getCheckerManager().runCheckersForPreStmt(PreVisit, Pred, S, *this);
2048 ExplodedNodeSet PostVisit;
2049 for (ExplodedNode *const N : PreVisit)
2050 VisitGCCAsmStmt(cast<GCCAsmStmt>(S), N, PostVisit);
2051 getCheckerManager().runCheckersForPostStmt(Dst, PostVisit, S, *this);
2052 break;
2053 }
2054
2055 case Stmt::MSAsmStmtClass:
2056 VisitMSAsmStmt(cast<MSAsmStmt>(S), Pred, Dst);
2057 break;
2058
2059 case Stmt::BlockExprClass:
2060 VisitBlockExpr(cast<BlockExpr>(S), Pred, Dst);
2061 break;
2062
2063 case Stmt::LambdaExprClass:
2064 if (AMgr.options.ShouldInlineLambdas) {
2065 VisitLambdaExpr(cast<LambdaExpr>(S), Pred, Dst);
2066 } else {
2067 const ExplodedNode *Node = Engine.makePostStmtNode(
2068 S, Pred->getState(), Pred, /*MarkAsSink=*/true);
2069 Engine.addAbortedBlock(Node, getCurrBlock());
2070 }
2071 break;
2072
2073 case Stmt::BinaryOperatorClass: {
2074 const auto *B = cast<BinaryOperator>(S);
2075 if (B->isLogicalOp()) {
2076 VisitLogicalExpr(B, Pred, Dst);
2077 break;
2078 } else if (B->getOpcode() == BO_Comma) {
2079 SVal Val =
2080 Pred->getState()->getSVal(B->getRHS(), Pred->getStackFrame());
2081 Dst.insert(Engine.makeNodeWithBinding(Pred, B, Val));
2082 break;
2083 }
2084
2085 if (AMgr.options.ShouldEagerlyAssume &&
2086 (B->isRelationalOp() || B->isEqualityOp())) {
2087 ExplodedNodeSet Tmp;
2090 }
2091 else
2093
2094 break;
2095 }
2096
2097 case Stmt::CXXOperatorCallExprClass:
2098 case Stmt::CallExprClass:
2099 case Stmt::CXXMemberCallExprClass:
2100 case Stmt::UserDefinedLiteralClass:
2101 VisitCallExpr(cast<CallExpr>(S), Pred, Dst);
2102 break;
2103
2104 case Stmt::CXXCatchStmtClass:
2106 break;
2107
2108 case Stmt::CXXTemporaryObjectExprClass:
2109 case Stmt::CXXConstructExprClass:
2111 break;
2112
2113 case Stmt::CXXInheritedCtorInitExprClass:
2115 Dst);
2116 break;
2117
2118 case Stmt::CXXNewExprClass: {
2119
2120 ExplodedNodeSet PreVisit;
2121 getCheckerManager().runCheckersForPreStmt(PreVisit, Pred, S, *this);
2122
2123 ExplodedNodeSet PostVisit;
2124 for (const auto i : PreVisit)
2125 VisitCXXNewExpr(cast<CXXNewExpr>(S), i, PostVisit);
2126
2127 getCheckerManager().runCheckersForPostStmt(Dst, PostVisit, S, *this);
2128 break;
2129 }
2130
2131 case Stmt::CXXDeleteExprClass: {
2132 ExplodedNodeSet PreVisit;
2133 const auto *CDE = cast<CXXDeleteExpr>(S);
2134 getCheckerManager().runCheckersForPreStmt(PreVisit, Pred, S, *this);
2135 ExplodedNodeSet PostVisit;
2136 getCheckerManager().runCheckersForPostStmt(PostVisit, PreVisit, S, *this);
2137
2138 for (const auto i : PostVisit)
2139 VisitCXXDeleteExpr(CDE, i, Dst);
2140
2141 break;
2142 }
2143 // FIXME: ChooseExpr is really a constant. We need to fix
2144 // the CFG do not model them as explicit control-flow.
2145
2146 case Stmt::ChooseExprClass: { // __builtin_choose_expr
2147 const auto *C = cast<ChooseExpr>(S);
2148 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
2149 break;
2150 }
2151
2152 case Stmt::CompoundAssignOperatorClass:
2154 break;
2155
2156 case Stmt::CompoundLiteralExprClass:
2158 break;
2159
2160 case Stmt::BinaryConditionalOperatorClass:
2161 case Stmt::ConditionalOperatorClass: { // '?' operator
2162 const auto *C = cast<AbstractConditionalOperator>(S);
2163 VisitGuardedExpr(C, C->getTrueExpr(), C->getFalseExpr(), Pred, Dst);
2164 break;
2165 }
2166
2167 case Stmt::CXXThisExprClass:
2168 VisitCXXThisExpr(cast<CXXThisExpr>(S), Pred, Dst);
2169 break;
2170
2171 case Stmt::DeclRefExprClass: {
2172 const auto *DE = cast<DeclRefExpr>(S);
2173 VisitCommonDeclRefExpr(DE, DE->getDecl(), Pred, Dst);
2174 break;
2175 }
2176
2177 case Stmt::DeclStmtClass:
2178 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
2179 break;
2180
2181 case Stmt::ImplicitCastExprClass:
2182 case Stmt::CStyleCastExprClass:
2183 case Stmt::CXXStaticCastExprClass:
2184 case Stmt::CXXDynamicCastExprClass:
2185 case Stmt::CXXReinterpretCastExprClass:
2186 case Stmt::CXXConstCastExprClass:
2187 case Stmt::CXXFunctionalCastExprClass:
2188 case Stmt::BuiltinBitCastExprClass:
2189 case Stmt::ObjCBridgedCastExprClass:
2190 case Stmt::CXXAddrspaceCastExprClass: {
2191 const auto *C = cast<CastExpr>(S);
2192 ExplodedNodeSet dstExpr;
2193 VisitCast(C, C->getSubExpr(), Pred, dstExpr);
2194
2195 // Handle the postvisit checks.
2196 getCheckerManager().runCheckersForPostStmt(Dst, dstExpr, C, *this);
2197 break;
2198 }
2199
2200 case Expr::MaterializeTemporaryExprClass: {
2201 const auto *MTE = cast<MaterializeTemporaryExpr>(S);
2202 ExplodedNodeSet dstPrevisit;
2203 getCheckerManager().runCheckersForPreStmt(dstPrevisit, Pred, MTE, *this);
2204 ExplodedNodeSet dstExpr;
2205 for (const auto i : dstPrevisit)
2206 CreateCXXTemporaryObject(MTE, i, dstExpr);
2207 getCheckerManager().runCheckersForPostStmt(Dst, dstExpr, MTE, *this);
2208 break;
2209 }
2210
2211 case Stmt::InitListExprClass: {
2212 const InitListExpr *E = cast<InitListExpr>(S);
2213 ConstructInitList(E, E->inits(), E->isTransparent(), Pred, Dst);
2214 break;
2215 }
2216
2217 case Expr::CXXParenListInitExprClass: {
2219 ConstructInitList(E, E->getInitExprs(), /*IsTransparent*/ false, Pred,
2220 Dst);
2221 break;
2222 }
2223
2224 case Stmt::MemberExprClass:
2225 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst);
2226 break;
2227
2228 case Stmt::AtomicExprClass:
2229 VisitAtomicExpr(cast<AtomicExpr>(S), Pred, Dst);
2230 break;
2231
2232 case Stmt::ObjCIvarRefExprClass:
2234 break;
2235
2236 case Stmt::ObjCForCollectionStmtClass:
2238 break;
2239
2240 case Stmt::ObjCMessageExprClass:
2242 break;
2243
2244 case Stmt::ObjCAtThrowStmtClass:
2245 case Stmt::CXXThrowExprClass:
2246 // FIXME: This is not complete. We basically treat @throw as
2247 // an abort.
2248 Engine.makePostStmtNode(S, Pred->getState(), Pred, /*MarkAsSink=*/true);
2249 break;
2250
2251 case Stmt::ReturnStmtClass:
2252 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
2253 break;
2254
2255 case Stmt::OffsetOfExprClass: {
2256 ExplodedNodeSet PreVisit;
2257 getCheckerManager().runCheckersForPreStmt(PreVisit, Pred, S, *this);
2258
2259 ExplodedNodeSet PostVisit;
2260 for (const auto Node : PreVisit)
2261 VisitOffsetOfExpr(cast<OffsetOfExpr>(S), Node, PostVisit);
2262
2263 getCheckerManager().runCheckersForPostStmt(Dst, PostVisit, S, *this);
2264 break;
2265 }
2266
2267 case Stmt::UnaryExprOrTypeTraitExprClass:
2269 Dst);
2270 break;
2271
2272 case Stmt::StmtExprClass: {
2273 const auto *SE = cast<StmtExpr>(S);
2274
2275 if (SE->getSubStmt()->body_empty()) {
2276 // Empty statement expression.
2277 assert(SE->getType() == getContext().VoidTy
2278 && "Empty statement expression must have void type.");
2279 } else if (const auto *LastExpr =
2280 dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
2281 SVal Val = Pred->getState()->getSVal(LastExpr, Pred->getStackFrame());
2282 Pred = Engine.makeNodeWithBinding(Pred, SE, Val);
2283 }
2284 Dst.insert(Pred);
2285 break;
2286 }
2287
2288 case Stmt::UnaryOperatorClass: {
2289 const auto *U = cast<UnaryOperator>(S);
2290 if (AMgr.options.ShouldEagerlyAssume && (U->getOpcode() == UO_LNot)) {
2291 ExplodedNodeSet Tmp;
2292 VisitUnaryOperator(U, Pred, Tmp);
2294 }
2295 else
2296 VisitUnaryOperator(U, Pred, Dst);
2297 break;
2298 }
2299
2300 case Stmt::PseudoObjectExprClass: {
2301 const auto *PE = cast<PseudoObjectExpr>(S);
2302 SVal V = UnknownVal();
2303 if (const Expr *Result = PE->getResultExpr())
2304 V = Pred->getState()->getSVal(Result, Pred->getStackFrame());
2305 Dst.insert(Engine.makeNodeWithBinding(Pred, PE, V));
2306 break;
2307 }
2308
2309 case Expr::ObjCIndirectCopyRestoreExprClass: {
2310 // ObjCIndirectCopyRestoreExpr implies passing a temporary for
2311 // correctness of lifetime management. Due to limited analysis
2312 // of ARC, this is implemented as direct arg passing.
2313 const auto *OIE = cast<ObjCIndirectCopyRestoreExpr>(S);
2314 const Expr *E = OIE->getSubExpr();
2315 SVal V = Pred->getState()->getSVal(E, Pred->getStackFrame());
2316 Dst.insert(Engine.makeNodeWithBinding(Pred, OIE, V));
2317 break;
2318 }
2319 }
2320}
2321
2322bool ExprEngine::replayWithoutInlining(ExplodedNode *N,
2323 const StackFrame *CalleeSF) {
2324 const StackFrame *CallerSF = CalleeSF->getParent();
2325 assert(CalleeSF && CallerSF);
2326 ExplodedNode *BeforeProcessingCall = nullptr;
2327 const Expr *CE = CalleeSF->getCallSite();
2328
2329 // Find the first node before we started processing the call expression.
2330 while (N) {
2331 ProgramPoint L = N->getLocation();
2332 BeforeProcessingCall = N;
2333 N = N->pred_empty() ? nullptr : *(N->pred_begin());
2334
2335 // Skip the nodes corresponding to the inlined code.
2336 if (L.getStackFrame() != CallerSF)
2337 continue;
2338 // We reached the caller. Find the node right before we started
2339 // processing the call.
2340 if (L.isPurgeKind())
2341 continue;
2342 if (L.getAs<PreImplicitCall>())
2343 continue;
2344 if (L.getAs<CallEnter>())
2345 continue;
2346 if (std::optional<StmtPoint> SP = L.getAs<StmtPoint>())
2347 if (SP->getStmt() == CE)
2348 continue;
2349 break;
2350 }
2351
2352 if (!BeforeProcessingCall)
2353 return false;
2354
2355 // TODO: Clean up the unneeded nodes.
2356
2357 // Build an Epsilon node from which we will restart the analyzes.
2358 // Note that CE is permitted to be NULL!
2359 static SimpleProgramPointTag PT("ExprEngine", "Replay without inlining");
2360 ProgramPoint NewNodeLoc =
2361 EpsilonPoint(BeforeProcessingCall->getStackFrame(), CE, nullptr, &PT);
2362 // Add the special flag to GDM to signal retrying with no inlining.
2363 // Note, changing the state ensures that we are not going to cache out.
2364 // NOTE: This stores the call site (CE) in the state trait, but the the
2365 // actual pointer value is only checked by an assertion; for the analysis,
2366 // only the presence or absence of this trait matters.
2367 // TODO: If we are handling a destructor call, CE is nullpointer (because it
2368 // ultimately comes from the `Origin` of a `CXXDestructorCall`), which is
2369 // indistinguishable from the absence (default state) of this state trait.
2370 // I don't think that this bad logic causes actually observable problems, but
2371 // it would be nice to clean it up if somebody has time to do so.
2372 ProgramStateRef NewNodeState = BeforeProcessingCall->getState();
2373 NewNodeState = NewNodeState->set<ReplayWithoutInlining>(CE);
2374
2375 // Make the new node a successor of BeforeProcessingCall.
2376 bool IsNew = false;
2377 ExplodedNode *NewNode = G.getNode(NewNodeLoc, NewNodeState, false, &IsNew);
2378 // We cached out at this point. Caching out is common due to us backtracking
2379 // from the inlined function, which might spawn several paths.
2380 if (!IsNew)
2381 return true;
2382
2383 NewNode->addPredecessor(BeforeProcessingCall, G);
2384
2385 // Add the new node to the work list.
2386 Engine.enqueueStmtNode(NewNode, CalleeSF->getCallSiteBlock(),
2387 CalleeSF->getIndex());
2388 NumTimesRetriedWithoutInlining++;
2389 return true;
2390}
2391
2392/// Block entrance. (Update counters).
2393/// FIXME: `BlockEdge &L` is only used for debug statistics, consider removing
2394/// it and using `BlockEntrance &BE` (where `BlockEntrance` is a subtype of
2395/// `ProgramPoint`) for statistical purposes.
2397 const BlockEntrance &BE,
2398 NodeBuilder &Builder,
2399 ExplodedNode *Pred) {
2400 // If we reach a loop which has a known bound (and meets
2401 // other constraints) then consider completely unrolling it.
2402 if(AMgr.options.ShouldUnrollLoops) {
2403 unsigned maxBlockVisitOnPath = AMgr.options.maxBlockVisitOnPath;
2404 const Stmt *Term = getCurrBlock()->getTerminatorStmt();
2405 if (Term) {
2406 ProgramStateRef NewState = updateLoopStack(Term, AMgr.getASTContext(),
2407 Pred, maxBlockVisitOnPath);
2408 if (NewState != Pred->getState()) {
2409 ExplodedNode *UpdatedNode = Builder.generateNode(BE, NewState, Pred);
2410 if (!UpdatedNode)
2411 return;
2412 Pred = UpdatedNode;
2413 }
2414 }
2415 // Is we are inside an unrolled loop then no need the check the counters.
2416 if(isUnrolledState(Pred->getState()))
2417 return;
2418 }
2419
2420 // If this block is terminated by a loop and it has already been visited the
2421 // maximum number of times, widen the loop.
2422 unsigned int BlockCount = getNumVisitedCurrent();
2423 if (BlockCount == AMgr.options.maxBlockVisitOnPath - 1 &&
2424 AMgr.options.ShouldWidenLoops) {
2425 const Stmt *Term = getCurrBlock()->getTerminatorStmt();
2426 if (!isa_and_nonnull<ForStmt, WhileStmt, DoStmt, CXXForRangeStmt>(Term))
2427 return;
2428
2429 // Widen.
2430 const StackFrame *SF = Pred->getStackFrame();
2431
2432 // FIXME:
2433 // We cannot use the CFG element from the via `ExprEngine::getCFGElementRef`
2434 // since we are currently at the block entrance and the current reference
2435 // would be stale. Ideally, we should pass on the terminator of the CFG
2436 // block, but the terminator cannot be referred as a CFG element.
2437 // Here we just pass the the first CFG element in the block.
2438 ProgramStateRef WidenedState = getWidenedLoopState(
2439 Pred->getState(), SF, BlockCount, *getCurrBlock()->ref_begin());
2440 Builder.generateNode(BE, WidenedState, Pred);
2441 return;
2442 }
2443
2444 // FIXME: Refactor this into a checker.
2445 if (BlockCount >= AMgr.options.maxBlockVisitOnPath) {
2446 static SimpleProgramPointTag Tag(TagProviderName, "Block count exceeded");
2447 const ProgramPoint TaggedLoc = BE.withTag(&Tag);
2448 const ExplodedNode *Sink =
2449 Builder.generateSink(TaggedLoc, Pred->getState(), Pred);
2450
2451 const StackFrame *SF = Pred->getStackFrame();
2452 if (!SF->inTopFrame()) {
2453 // FIXME: This will unconditionally prevent inlining this function (even
2454 // from other entry points), which is not a reasonable heuristic: even if
2455 // we reached max block count on this particular execution path, there
2456 // may be other execution paths (especially with other parametrizations)
2457 // where the analyzer can reach the end of the function (so there is no
2458 // natural reason to avoid inlining it). However, disabling this would
2459 // significantly increase the analysis time (because more entry points
2460 // would exhaust their allocated budget), so it must be compensated by a
2461 // different (more reasonable) reduction of analysis scope.
2462 Engine.FunctionSummaries->markShouldNotInline(SF->getDecl());
2463
2464 // Re-run the call evaluation without inlining it, by storing the
2465 // no-inlining policy in the state and enqueuing the new work item on
2466 // the list. Replay should almost never fail. Use the stats to catch it
2467 // if it does.
2468 if ((!AMgr.options.NoRetryExhausted && replayWithoutInlining(Pred, SF)))
2469 return;
2470 NumMaxBlockCountReachedInInlined++;
2471 } else
2472 NumMaxBlockCountReached++;
2473
2474 // Make sink nodes as exhausted(for stats) only if retry failed.
2475 Engine.blocksExhausted.push_back(std::make_pair(L, Sink));
2476 }
2477}
2478
2480 ExplodedNode *Pred,
2481 ExplodedNodeSet &Dst) {
2482 llvm::PrettyStackTraceFormat CrashInfo(
2483 "Processing block entrance B%d -> B%d",
2484 Entrance.getPreviousBlock()->getBlockID(),
2485 Entrance.getBlock()->getBlockID());
2486 getCheckerManager().runCheckersForBlockEntrance(Dst, Pred, Entrance, *this);
2487}
2488
2489//===----------------------------------------------------------------------===//
2490// Branch processing.
2491//===----------------------------------------------------------------------===//
2492
2493/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
2494/// to try to recover some path-sensitivity for casts of symbolic
2495/// integers that promote their values (which are currently not tracked well).
2496/// This function returns the SVal bound to Condition->IgnoreCasts if all the
2497// cast(s) did was sign-extend the original value.
2499 const StackFrame *SF, ASTContext &Ctx) {
2500
2501 const auto *Ex = dyn_cast<Expr>(Condition);
2502 if (!Ex)
2503 return UnknownVal();
2504
2505 uint64_t bits = 0;
2506 bool bitsInit = false;
2507
2508 while (const auto *CE = dyn_cast<CastExpr>(Ex)) {
2509 QualType T = CE->getType();
2510
2511 if (!T->isIntegralOrEnumerationType())
2512 return UnknownVal();
2513
2514 uint64_t newBits = Ctx.getTypeSize(T);
2515 if (!bitsInit || newBits < bits) {
2516 bitsInit = true;
2517 bits = newBits;
2518 }
2519
2520 Ex = CE->getSubExpr();
2521 }
2522
2523 // We reached a non-cast. Is it a symbolic value?
2524 QualType T = Ex->getType();
2525
2526 if (!bitsInit || !T->isIntegralOrEnumerationType() ||
2527 Ctx.getTypeSize(T) > bits)
2528 return UnknownVal();
2529
2530 return state->getSVal(Ex, SF);
2531}
2532
2533#ifndef NDEBUG
2534static const Stmt *getRightmostLeaf(const Stmt *Condition) {
2535 while (Condition) {
2536 const auto *BO = dyn_cast<BinaryOperator>(Condition);
2537 if (!BO || !BO->isLogicalOp()) {
2538 return Condition;
2539 }
2540 Condition = BO->getRHS()->IgnoreParens();
2541 }
2542 return nullptr;
2543}
2544#endif
2545
2546// Returns the condition the branch at the end of 'B' depends on and whose value
2547// has been evaluated within 'B'.
2548// In most cases, the terminator condition of 'B' will be evaluated fully in
2549// the last statement of 'B'; in those cases, the resolved condition is the
2550// given 'Condition'.
2551// If the condition of the branch is a logical binary operator tree, the CFG is
2552// optimized: in that case, we know that the expression formed by all but the
2553// rightmost leaf of the logical binary operator tree must be true, and thus
2554// the branch condition is at this point equivalent to the truth value of that
2555// rightmost leaf; the CFG block thus only evaluates this rightmost leaf
2556// expression in its final statement. As the full condition in that case was
2557// not evaluated, and is thus not in the SVal cache, we need to use that leaf
2558// expression to evaluate the truth value of the condition in the current state
2559// space.
2561 const CFGBlock *B) {
2562 if (const auto *Ex = dyn_cast<Expr>(Condition))
2563 Condition = Ex->IgnoreParens();
2564
2565 const auto *BO = dyn_cast<BinaryOperator>(Condition);
2566 if (!BO || !BO->isLogicalOp())
2567 return Condition;
2568
2569 assert(B->getTerminator().isStmtBranch() &&
2570 "Other kinds of branches are handled separately!");
2571
2572 // For logical operations, we still have the case where some branches
2573 // use the traditional "merge" approach and others sink the branch
2574 // directly into the basic blocks representing the logical operation.
2575 // We need to distinguish between those two cases here.
2576
2577 // The invariants are still shifting, but it is possible that the
2578 // last element in a CFGBlock is not a CFGStmt. Look for the last
2579 // CFGStmt as the value of the condition.
2580 for (CFGElement Elem : llvm::reverse(*B)) {
2581 std::optional<CFGStmt> CS = Elem.getAs<CFGStmt>();
2582 if (!CS)
2583 continue;
2584 const Stmt *LastStmt = CS->getStmt();
2585 assert(LastStmt == Condition || LastStmt == getRightmostLeaf(Condition));
2586 return LastStmt;
2587 }
2588 llvm_unreachable("could not resolve condition");
2589}
2590
2592 std::pair<const ObjCForCollectionStmt *, const StackFrame *>;
2593
2594REGISTER_MAP_WITH_PROGRAMSTATE(ObjCForHasMoreIterations, ObjCForLctxPair, bool)
2595
2597 ProgramStateRef State, const ObjCForCollectionStmt *O, const StackFrame *SF,
2598 bool HasMoreIteraton) {
2599 assert(!State->contains<ObjCForHasMoreIterations>({O, SF}));
2600 return State->set<ObjCForHasMoreIterations>({O, SF}, HasMoreIteraton);
2601}
2602
2604 const ObjCForCollectionStmt *O,
2605 const StackFrame *SF) {
2606 assert(State->contains<ObjCForHasMoreIterations>({O, SF}));
2607 return State->remove<ObjCForHasMoreIterations>({O, SF});
2608}
2609
2611 const ObjCForCollectionStmt *O,
2612 const StackFrame *SF) {
2613 assert(State->contains<ObjCForHasMoreIterations>({O, SF}));
2614 return *State->get<ObjCForHasMoreIterations>({O, SF});
2615}
2616
2617/// Split the state on whether there are any more iterations left for this loop.
2618/// Returns a (HasMoreIteration, HasNoMoreIteration) pair, or std::nullopt when
2619/// the acquisition of the loop condition value failed.
2620static std::optional<std::pair<ProgramStateRef, ProgramStateRef>>
2621assumeCondition(const Stmt *ConditionStmt, ExplodedNode *N) {
2622 ProgramStateRef State = N->getState();
2623 if (const auto *ObjCFor = dyn_cast<ObjCForCollectionStmt>(ConditionStmt)) {
2624 bool HasMoreIteraton =
2625 ExprEngine::hasMoreIteration(State, ObjCFor, N->getStackFrame());
2626 // Checkers have already ran on branch conditions, so the current
2627 // information as to whether the loop has more iteration becomes outdated
2628 // after this point.
2629 State =
2630 ExprEngine::removeIterationState(State, ObjCFor, N->getStackFrame());
2631 if (HasMoreIteraton)
2632 return std::pair<ProgramStateRef, ProgramStateRef>{State, nullptr};
2633 else
2634 return std::pair<ProgramStateRef, ProgramStateRef>{nullptr, State};
2635 }
2636
2637 const auto *ConditionExpr = dyn_cast<Expr>(ConditionStmt);
2638 assert(ConditionExpr && "The condition must be an Expr from here!");
2639
2640 SVal X = State->getSVal(ConditionExpr, N->getStackFrame());
2641
2642 if (X.isUnknownOrUndef()) {
2643 // Give it a chance to recover from unknown.
2644 if (const auto *Ex = dyn_cast<Expr>(ConditionExpr)) {
2645 if (Ex->getType()->isIntegralOrEnumerationType()) {
2646 // Try to recover some path-sensitivity. Right now casts of symbolic
2647 // integers that promote their values are currently not tracked well.
2648 // If 'ConditionExpr' is such an expression, try and recover the
2649 // underlying value and use that instead.
2650 SVal recovered =
2651 RecoverCastedSymbol(State, ConditionExpr, N->getStackFrame(),
2652 N->getState()->getStateManager().getContext());
2653
2654 if (!recovered.isUnknown()) {
2655 X = recovered;
2656 }
2657 }
2658 }
2659 }
2660
2661 // If the condition is still unknown, give up.
2662 if (X.isUnknownOrUndef())
2663 return std::nullopt;
2664
2665 DefinedSVal V = X.castAs<DefinedSVal>();
2666
2667 ProgramStateRef StTrue, StFalse;
2668 return State->assume(V);
2669}
2670
2672 const Stmt *Condition, ExplodedNode *Pred, ExplodedNodeSet &Dst,
2673 const CFGBlock *DstT, const CFGBlock *DstF,
2674 std::optional<unsigned> IterationsCompletedInLoop) {
2676 "CXXBindTemporaryExprs are handled by processBindTemporary.");
2677
2678 const StackFrame *SF = Pred->getStackFrame();
2679
2680 // Check for NULL conditions; e.g. "for(;;)"
2681 if (!Condition) {
2682 if (!DstT) {
2683 // I _hope_ that this "null condition + null transition to loop body"
2684 // case is impossible, but I cannot prove this, so let's cover it.
2685 return;
2686 }
2687 BlockEdge BE(getCurrBlock(), DstT, SF);
2688 Dst.insert(Engine.makeNode(BE, Pred->getState(), Pred));
2689 return;
2690 }
2691
2692 if (const auto *Ex = dyn_cast<Expr>(Condition))
2693 Condition = Ex->IgnoreParens();
2694
2696 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
2697 Condition->getBeginLoc(),
2698 "Error evaluating branch");
2699
2700 ExplodedNodeSet CheckersOutSet;
2702 Pred, *this);
2703 // We generated only sinks.
2704 if (CheckersOutSet.empty())
2705 return;
2706
2707 for (ExplodedNode *PredN : CheckersOutSet) {
2708 ProgramStateRef PrevState = PredN->getState();
2709
2710 ProgramStateRef StTrue = PrevState, StFalse = PrevState;
2711 if (const auto KnownCondValueAssumption = assumeCondition(Condition, PredN))
2712 std::tie(StTrue, StFalse) = *KnownCondValueAssumption;
2713
2714 if (StTrue && StFalse)
2716
2717 // We want to ensure consistent behavior between `eagerly-assume=false`,
2718 // when the state split is always performed by the `assumeCondition()`
2719 // call within this function and `eagerly-assume=true` (the default), when
2720 // some conditions (comparison operators, unary negation) can trigger a
2721 // state split before this callback. There are some contrived corner cases
2722 // that behave differently with and without `eagerly-assume`, but I don't
2723 // know about an example that could plausibly appear in "real" code.
2724 bool BothFeasible =
2725 (StTrue && StFalse) ||
2726 didEagerlyAssumeBifurcateAt(PrevState, dyn_cast<Expr>(Condition));
2727
2728 if (StTrue) {
2729 // In a loop, if both branches are feasible (i.e. the analyzer doesn't
2730 // understand the loop condition) and two iterations have already been
2731 // completed, then don't assume a third iteration because it is a
2732 // redundant execution path (unlikely to be different from earlier loop
2733 // exits) and can cause false positives if e.g. the loop iterates over a
2734 // two-element structure with an opaque condition.
2735 //
2736 // The iteration count "2" is hardcoded because it's the natural limit:
2737 // * the fact that the programmer wrote a loop (and not just an `if`)
2738 // implies that they thought that the loop body might be executed twice;
2739 // * however, there are situations where the programmer knows that there
2740 // are at most two iterations but writes a loop that appears to be
2741 // generic, because there is no special syntax for "loop with at most
2742 // two iterations". (This pattern is common in FFMPEG and appears in
2743 // many other projects as well.)
2744 bool CompletedTwoIterations = IterationsCompletedInLoop.value_or(0) >= 2;
2745 bool SkipTrueBranch = BothFeasible && CompletedTwoIterations;
2746
2747 // FIXME: This "don't assume third iteration" heuristic partially
2748 // conflicts with the widen-loop analysis option (which is off by
2749 // default). If we intend to support and stabilize the loop widening,
2750 // we must ensure that it 'plays nicely' with this logic.
2751 if (!SkipTrueBranch || AMgr.options.ShouldWidenLoops) {
2752 if (DstT) {
2753 BlockEdge BE(getCurrBlock(), DstT, SF);
2754 Dst.insert(Engine.makeNode(BE, StTrue, PredN));
2755 }
2756 } else if (!AMgr.options.InlineFunctionsWithAmbiguousLoops) {
2757 // FIXME: There is an ancient and arbitrary heuristic in
2758 // `ExprEngine::processCFGBlockEntrance` which prevents all further
2759 // inlining of a function if it finds an execution path within that
2760 // function which reaches the `MaxBlockVisitOnPath` limit (a/k/a
2761 // `analyzer-max-loop`, by default four iterations in a loop). Adding
2762 // this "don't assume third iteration" logic significantly increased
2763 // the analysis runtime on some inputs because less functions were
2764 // arbitrarily excluded from being inlined, so more entry points used
2765 // up their full allocated budget. As a hacky compensation for this,
2766 // here we apply the "should not inline" mark in cases when the loop
2767 // could potentially reach the `MaxBlockVisitOnPath` limit without the
2768 // "don't assume third iteration" logic. This slightly overcompensates
2769 // (activates if the third iteration can be entered, and will not
2770 // recognize cases where the fourth iteration would't be completed), but
2771 // should be good enough for practical purposes.
2772 if (!SF->inTopFrame()) {
2773 Engine.FunctionSummaries->markShouldNotInline(SF->getDecl());
2774 }
2775 }
2776 }
2777
2778 if (StFalse) {
2779 // In a loop, if both branches are feasible (i.e. the analyzer doesn't
2780 // understand the loop condition), we are before the first iteration and
2781 // the analyzer option `assume-at-least-one-iteration` is set to `true`,
2782 // then avoid creating the execution path where the loop is skipped.
2783 //
2784 // In some situations this "loop is skipped" execution path is an
2785 // important corner case that may evade the notice of the developer and
2786 // hide significant bugs -- however, there are also many situations where
2787 // it's guaranteed that at least one iteration will happen (e.g. some
2788 // data structure is always nonempty), but the analyzer cannot realize
2789 // this and will produce false positives when it assumes that the loop is
2790 // skipped.
2791 bool BeforeFirstIteration = IterationsCompletedInLoop == std::optional{0};
2792 bool SkipFalseBranch = BothFeasible && BeforeFirstIteration &&
2793 AMgr.options.ShouldAssumeAtLeastOneIteration;
2794 if (!SkipFalseBranch && DstF) {
2795 BlockEdge BE(getCurrBlock(), DstF, SF);
2796 Dst.insert(Engine.makeNode(BE, StFalse, PredN));
2797 }
2798 }
2799 }
2800}
2801
2802/// The GDM component containing the set of global variables which have been
2803/// previously initialized with explicit initializers.
2805 llvm::ImmutableSet<const VarDecl *>)
2806
2808 ExplodedNode *Pred,
2809 ExplodedNodeSet &Dst,
2810 const CFGBlock *DstT,
2811 const CFGBlock *DstF) {
2812 const auto *VD = cast<VarDecl>(DS->getSingleDecl());
2813 ProgramStateRef State = Pred->getState();
2814 bool InitHasRun = State->contains<InitializedGlobalsSet>(VD);
2815 if (!InitHasRun)
2816 State = State->add<InitializedGlobalsSet>(VD);
2817
2818 if (const CFGBlock *DstBlock = InitHasRun ? DstT : DstF) {
2819 BlockEdge BE(getCurrBlock(), DstBlock, Pred->getStackFrame());
2820 Dst.insert(Engine.makeNode(BE, State, Pred));
2821 }
2822}
2823
2824/// processIndirectGoto - Called by CoreEngine. Used to generate successor
2825/// nodes by processing the 'effects' of a computed goto jump.
2827 const CFGBlock *Dispatch,
2828 ExplodedNode *Pred) {
2829 ProgramStateRef State = Pred->getState();
2830 SVal V = State->getSVal(Tgt, getCurrStackFrame());
2831
2832 // We cannot dispatch anywhere if the label is undefined, NULL or some other
2833 // concrete number.
2834 // FIXME: Emit a warning in this situation.
2836 return;
2837
2838 // If 'V' is the address of a concrete goto label (on this execution path),
2839 // then only transition along the edge to that label.
2840 // FIXME: Implement dispatch for symbolic pointers, utilizing information
2841 // that they are equal or not equal to pointers to a certain goto label.
2842 const LabelDecl *L = nullptr;
2843 if (auto LV = V.getAs<loc::GotoLabel>())
2844 L = LV->getLabel();
2845
2846 // Dispatch to the label 'L' or to all labels if 'L' is null.
2847 for (const CFGBlock *Succ : Dispatch->succs()) {
2848 if (!L || cast<LabelStmt>(Succ->getLabel())->getDecl() == L) {
2849 // FIXME: If 'V' was a symbolic value, then record that on this execution
2850 // path it is equal to the address of the label leading to 'Succ'.
2851 BlockEdge BE(getCurrBlock(), Succ, Pred->getStackFrame());
2852 Dst.insert(Engine.makeNode(BE, State, Pred));
2853 }
2854 }
2855}
2856
2858 ExplodedNodeSet &Dst,
2859 const BlockEdge &L) {
2860 getCheckerManager().runCheckersForBeginFunction(Dst, L, Pred, *this);
2861}
2862
2863/// ProcessEndPath - Called by CoreEngine. Used to generate end-of-path
2864/// nodes when the control reaches the end of a function.
2866 const ReturnStmt *RS) {
2867 ProgramStateRef State = Pred->getState();
2868
2869 if (!Pred->getStackFrame()->inTopFrame())
2870 State = finishArgumentConstruction(
2871 State, *getStateManager().getCallEventManager().getCaller(
2872 Pred->getStackFrame(), Pred->getState()));
2873
2874 // FIXME: We currently cannot assert that temporaries are clear, because
2875 // lifetime extended temporaries are not always modelled correctly. In some
2876 // cases when we materialize the temporary, we do
2877 // createTemporaryRegionIfNeeded(), and the region changes, and also the
2878 // respective destructor becomes automatic from temporary. So for now clean up
2879 // the state manually before asserting. Ideally, this braced block of code
2880 // should go away.
2881 {
2882 const StackFrame *FromSF = Pred->getStackFrame();
2883 const StackFrame *ToSF = FromSF->getParent();
2884 const StackFrame *SF = FromSF;
2885 while (SF != ToSF) {
2886 assert(SF && "ToSF must be a parent of FromSF!");
2887 for (auto I : State->get<ObjectsUnderConstruction>())
2888 if (I.first.getStackFrame() == SF) {
2889 // The comment above only pardons us for not cleaning up a
2890 // temporary destructor. If any other statements are found here,
2891 // it must be a separate problem.
2892 assert(I.first.getItem().getKind() ==
2894 I.first.getItem().getKind() ==
2896 State = State->remove<ObjectsUnderConstruction>(I.first);
2897 }
2898 SF = SF->getParent();
2899 }
2900 }
2901
2902 // Perform the transition with cleanups.
2903 if (State != Pred->getState()) {
2904 Pred = Engine.makeNode(Pred->getLocation(), State, Pred);
2905 if (!Pred) {
2906 // The node with clean temporaries already exists. We might have reached
2907 // it on a path on which we initialize different temporaries.
2908 return;
2909 }
2910 }
2911
2912 assert(areAllObjectsFullyConstructed(Pred->getState(), Pred->getStackFrame(),
2913 Pred->getStackFrame()->getParent()));
2914 ExplodedNodeSet Dst;
2915 if (Pred->getStackFrame()->inTopFrame()) {
2916 // Remove dead symbols.
2917 ExplodedNodeSet AfterRemovedDead;
2918 removeDeadOnEndOfFunction(Pred, AfterRemovedDead);
2919
2920 // Notify checkers.
2921 for (const auto I : AfterRemovedDead)
2922 getCheckerManager().runCheckersForEndFunction(Dst, I, *this, RS);
2923 } else {
2924 getCheckerManager().runCheckersForEndFunction(Dst, Pred, *this, RS);
2925 }
2926
2927 Engine.enqueueEndOfFunction(Dst, RS);
2928}
2929
2930/// ProcessSwitch - Called by CoreEngine. Used to generate successor
2931/// nodes by processing the 'effects' of a switch statement.
2933 ExplodedNodeSet &Dst) {
2934 const ASTContext &ACtx = getContext();
2935 const StackFrame *SF = Pred->getStackFrame();
2936 const Expr *Condition = Switch->getCond();
2937
2938 // The block that is terminated by the switch statement.
2939 const CFGBlock *SwitchBlock = getCurrBlock();
2940 // Note that successors may be null if they are pruned as unreachable.
2941 assert(SwitchBlock->succ_size() && "Switch must have at least one successor");
2942 // The reversed iteration order is present since the beginning, when in 2008
2943 // commit 80ebc1d1c95704b0ff0386b3a3cbc8b3ff960654 added support for handling
2944 // switch statements. I don't see any advantage over regular forward
2945 // iteration -- but switching the order would perturb the insertion order of
2946 // the work list and therefore the analysis results.
2947 llvm::iterator_range<CFGBlock::const_succ_reverse_iterator> CaseBlocks(
2948 SwitchBlock->succ_rbegin() + 1, SwitchBlock->succ_rend());
2949 const CFGBlock *DefaultBlock = *SwitchBlock->succ_rbegin();
2950
2951 ExplodedNodeSet CheckersOutSet;
2952
2954 Condition->IgnoreParens(), CheckersOutSet, Pred, *this);
2955
2956 for (ExplodedNode *Node : CheckersOutSet) {
2957 ProgramStateRef State = Node->getState();
2958
2959 SVal CondV = State->getSVal(Condition, SF);
2960 if (CondV.isUndef()) {
2961 // This can only happen if core.uninitialized.Branch is disabled.
2962 continue;
2963 }
2964 std::optional<NonLoc> CondNL = CondV.getAs<NonLoc>();
2965
2966 for (const CFGBlock *CaseBlock : CaseBlocks) {
2967 // Successor may be pruned out during CFG construction.
2968 if (!CaseBlock)
2969 continue;
2970
2971 const CaseStmt *Case = cast<CaseStmt>(CaseBlock->getLabel());
2972
2973 // Evaluate the LHS of the case value.
2974 llvm::APSInt V1 = Case->getLHS()->EvaluateKnownConstInt(ACtx);
2975 assert(V1.getBitWidth() ==
2976 getContext().getIntWidth(Condition->getType()));
2977
2978 // Get the RHS of the case, if it exists.
2979 llvm::APSInt V2;
2980 if (const Expr *E = Case->getRHS())
2981 V2 = E->EvaluateKnownConstInt(ACtx);
2982 else
2983 V2 = V1;
2984
2985 ProgramStateRef StateMatching;
2986 if (CondNL) {
2987 // Split the state: this "case:" matches / does not match.
2988 std::tie(StateMatching, State) =
2989 State->assumeInclusiveRange(*CondNL, V1, V2);
2990 } else {
2991 // The switch condition is UnknownVal, so we enter each "case:" without
2992 // any state update.
2993 StateMatching = State;
2994 }
2995
2996 if (StateMatching) {
2997 BlockEdge BE(SwitchBlock, CaseBlock, SF);
2998 Dst.insert(Engine.makeNode(BE, StateMatching, Node));
2999 }
3000
3001 // If _not_ entering the current case is infeasible, then we are done
3002 // with processing the paths through the current Node.
3003 if (!State)
3004 break;
3005 }
3006 if (!State)
3007 continue;
3008
3009 // The default block may be null if it is "optimized out" by CFG creation.
3010 if (!DefaultBlock)
3011 continue;
3012
3013 // If we have switch(enum value), the default branch is not
3014 // feasible if all of the enum constants not covered by 'case:' statements
3015 // are not feasible values for the switch condition.
3016 //
3017 // Note that this isn't as accurate as it could be. Even if there isn't
3018 // a case for a particular enum value as long as that enum value isn't
3019 // feasible then it shouldn't be considered for making 'default:' reachable.
3020 if (Condition->IgnoreParenImpCasts()->getType()->isEnumeralType()) {
3021 if (Switch->isAllEnumCasesCovered())
3022 continue;
3023 }
3024
3025 BlockEdge BE(SwitchBlock, DefaultBlock, SF);
3026 Dst.insert(Engine.makeNode(BE, State, Node));
3027 }
3028}
3029
3030//===----------------------------------------------------------------------===//
3031// Transfer functions: Loads and stores.
3032//===----------------------------------------------------------------------===//
3033
3035 ExplodedNode *Pred,
3036 ExplodedNodeSet &Dst) {
3037 ProgramStateRef state = Pred->getState();
3038 const StackFrame *SF = Pred->getStackFrame();
3039
3040 auto resolveAsLambdaCapturedVar =
3041 [&](const ValueDecl *VD) -> std::optional<std::pair<SVal, QualType>> {
3042 const auto *MD = dyn_cast<CXXMethodDecl>(SF->getDecl());
3043 const auto *DeclRefEx = dyn_cast<DeclRefExpr>(Ex);
3044 if (AMgr.options.ShouldInlineLambdas && DeclRefEx &&
3045 DeclRefEx->refersToEnclosingVariableOrCapture() && MD &&
3046 MD->getParent()->isLambda()) {
3047 // Lookup the field of the lambda.
3048 const CXXRecordDecl *CXXRec = MD->getParent();
3049 llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;
3050 FieldDecl *LambdaThisCaptureField;
3051 CXXRec->getCaptureFields(LambdaCaptureFields, LambdaThisCaptureField);
3052
3053 // Sema follows a sequence of complex rules to determine whether the
3054 // variable should be captured.
3055 if (const FieldDecl *FD = LambdaCaptureFields[VD]) {
3056 Loc CXXThis = svalBuilder.getCXXThis(MD, SF);
3057 SVal CXXThisVal = state->getSVal(CXXThis);
3058 return std::make_pair(state->getLValue(FD, CXXThisVal), FD->getType());
3059 }
3060 }
3061
3062 return std::nullopt;
3063 };
3064
3065 if (const auto *VD = dyn_cast<VarDecl>(D)) {
3066 // C permits "extern void v", and if you cast the address to a valid type,
3067 // you can even do things with it. We simply pretend
3068 assert(Ex->isGLValue() || VD->getType()->isVoidType());
3069 std::optional<std::pair<SVal, QualType>> VInfo =
3070 resolveAsLambdaCapturedVar(VD);
3071
3072 if (!VInfo)
3073 VInfo = std::make_pair(state->getLValue(VD, SF), VD->getType());
3074
3075 SVal V = VInfo->first;
3076 bool IsReference = VInfo->second->isReferenceType();
3077
3078 // For references, the 'lvalue' is the pointer address stored in the
3079 // reference region.
3080 if (IsReference) {
3081 if (const MemRegion *R = V.getAsRegion())
3082 V = state->getSVal(R);
3083 else
3084 V = UnknownVal();
3085 }
3086
3087 Dst.insert(
3088 Engine.makeNodeWithBinding(Pred, Ex, V, ProgramPoint::PostLValueKind));
3089 return;
3090 }
3091 if (const auto *ED = dyn_cast<EnumConstantDecl>(D)) {
3092 assert(!Ex->isGLValue());
3093 SVal V = svalBuilder.makeIntVal(ED->getInitVal());
3094 Dst.insert(Engine.makeNodeWithBinding(Pred, Ex, V));
3095 return;
3096 }
3097 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3098 SVal V = svalBuilder.getFunctionPointer(FD);
3099 Dst.insert(
3100 Engine.makeNodeWithBinding(Pred, Ex, V, ProgramPoint::PostLValueKind));
3101 return;
3102 }
3104 // Delegate all work related to pointer to members to the surrounding
3105 // operator&.
3106 Dst.insert(Pred);
3107 return;
3108 }
3109 if (const auto *BD = dyn_cast<BindingDecl>(D)) {
3110 // Handle structured bindings captured by lambda.
3111 if (std::optional<std::pair<SVal, QualType>> VInfo =
3112 resolveAsLambdaCapturedVar(BD)) {
3113 auto [V, T] = VInfo.value();
3114
3115 if (T->isReferenceType()) {
3116 if (const MemRegion *R = V.getAsRegion())
3117 V = state->getSVal(R);
3118 else
3119 V = UnknownVal();
3120 }
3121
3122 Dst.insert(Engine.makeNodeWithBinding(Pred, Ex, V,
3124 return;
3125 }
3126
3127 const auto *DD = cast<DecompositionDecl>(BD->getDecomposedDecl());
3128
3129 SVal Base = state->getLValue(DD, SF);
3130 if (DD->getType()->isReferenceType()) {
3131 if (const MemRegion *R = Base.getAsRegion())
3132 Base = state->getSVal(R);
3133 else
3134 Base = UnknownVal();
3135 }
3136
3137 SVal V = UnknownVal();
3138
3139 // Handle binding to data members
3140 if (const auto *ME = dyn_cast<MemberExpr>(BD->getBinding())) {
3141 const auto *Field = cast<FieldDecl>(ME->getMemberDecl());
3142 V = state->getLValue(Field, Base);
3143 }
3144 // Handle binding to arrays
3145 else if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BD->getBinding())) {
3146 SVal Idx = state->getSVal(ASE->getIdx(), SF);
3147
3148 // Note: the index of an element in a structured binding is automatically
3149 // created and it is a unique identifier of the specific element. Thus it
3150 // cannot be a value that varies at runtime.
3151 assert(Idx.isConstant() && "BindingDecl array index is not a constant!");
3152
3153 V = state->getLValue(BD->getType(), Idx, Base);
3154 }
3155 // Handle binding to tuple-like structures
3156 else if (const auto *HV = BD->getHoldingVar()) {
3157 V = state->getLValue(HV, SF);
3158
3159 if (HV->getType()->isReferenceType()) {
3160 if (const MemRegion *R = V.getAsRegion())
3161 V = state->getSVal(R);
3162 else
3163 V = UnknownVal();
3164 }
3165 } else
3166 llvm_unreachable("An unknown case of structured binding encountered!");
3167
3168 // In case of tuple-like types the references are already handled, so we
3169 // don't want to handle them again.
3170 if (BD->getType()->isReferenceType() && !BD->getHoldingVar()) {
3171 if (const MemRegion *R = V.getAsRegion())
3172 V = state->getSVal(R);
3173 else
3174 V = UnknownVal();
3175 }
3176
3177 Dst.insert(
3178 Engine.makeNodeWithBinding(Pred, Ex, V, ProgramPoint::PostLValueKind));
3179 return;
3180 }
3181
3182 if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(D)) {
3183 // FIXME: We should meaningfully implement this.
3184 (void)TPO;
3185 Dst.insert(Pred);
3186 return;
3187 }
3188
3189 llvm_unreachable("Support for this Decl not implemented.");
3190}
3191
3192/// VisitArrayInitLoopExpr - Transfer function for array init loop.
3194 ExplodedNode *Pred,
3195 ExplodedNodeSet &Dst) {
3196 ExplodedNodeSet CheckerPreStmt;
3197 getCheckerManager().runCheckersForPreStmt(CheckerPreStmt, Pred, Ex, *this);
3198
3199 ExplodedNodeSet EvalSet;
3200 NodeBuilder Bldr(CheckerPreStmt, EvalSet, *currBldrCtx);
3201
3202 const Expr *Arr = Ex->getCommonExpr()->getSourceExpr();
3203
3204 for (auto *Node : CheckerPreStmt) {
3205
3206 // The constructor visitior has already taken care of everything.
3208 break;
3209
3210 const StackFrame *SF = Node->getStackFrame();
3211 ProgramStateRef state = Node->getState();
3212
3213 SVal Base = UnknownVal();
3214
3215 // As in case of this expression the sub-expressions are not visited by any
3216 // other transfer functions, they are handled by matching their AST.
3217
3218 // Case of implicit copy or move ctor of object with array member
3219 //
3220 // Note: ExprEngine::VisitMemberExpr is not able to bind the array to the
3221 // environment.
3222 //
3223 // struct S {
3224 // int arr[2];
3225 // };
3226 //
3227 //
3228 // S a;
3229 // S b = a;
3230 //
3231 // The AST in case of a *copy constructor* looks like this:
3232 // ArrayInitLoopExpr
3233 // |-OpaqueValueExpr
3234 // | `-MemberExpr <-- match this
3235 // | `-DeclRefExpr
3236 // ` ...
3237 //
3238 //
3239 // S c;
3240 // S d = std::move(d);
3241 //
3242 // In case of a *move constructor* the resulting AST looks like:
3243 // ArrayInitLoopExpr
3244 // |-OpaqueValueExpr
3245 // | `-MemberExpr <-- match this first
3246 // | `-CXXStaticCastExpr <-- match this after
3247 // | `-DeclRefExpr
3248 // ` ...
3249 if (const auto *ME = dyn_cast<MemberExpr>(Arr)) {
3250 Expr *MEBase = ME->getBase();
3251
3252 // Move ctor
3253 if (auto CXXSCE = dyn_cast<CXXStaticCastExpr>(MEBase)) {
3254 MEBase = CXXSCE->getSubExpr();
3255 }
3256
3257 auto ObjDeclExpr = cast<DeclRefExpr>(MEBase);
3258 SVal Obj = state->getLValue(cast<VarDecl>(ObjDeclExpr->getDecl()), SF);
3259
3260 Base = state->getLValue(cast<FieldDecl>(ME->getMemberDecl()), Obj);
3261 }
3262
3263 // Case of lambda capture and decomposition declaration
3264 //
3265 // int arr[2];
3266 //
3267 // [arr]{ int a = arr[0]; }();
3268 // auto[a, b] = arr;
3269 //
3270 // In both of these cases the AST looks like the following:
3271 // ArrayInitLoopExpr
3272 // |-OpaqueValueExpr
3273 // | `-DeclRefExpr <-- match this
3274 // ` ...
3275 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arr))
3276 Base = state->getLValue(cast<VarDecl>(DRE->getDecl()), SF);
3277
3278 // Create a lazy compound value to the original array
3279 if (const MemRegion *R = Base.getAsRegion())
3280 Base = state->getSVal(R);
3281 else
3282 Base = UnknownVal();
3283
3284 Bldr.generateNode(Ex, Node, state->BindExpr(Ex, SF, Base));
3285 }
3286
3287 getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, Ex, *this);
3288}
3289
3290/// VisitArraySubscriptExpr - Transfer function for array accesses
3292 ExplodedNode *Pred,
3293 ExplodedNodeSet &Dst){
3294 const Expr *Base = A->getBase()->IgnoreParens();
3295 const Expr *Idx = A->getIdx()->IgnoreParens();
3296
3297 ExplodedNodeSet CheckerPreStmt;
3298 getCheckerManager().runCheckersForPreStmt(CheckerPreStmt, Pred, A, *this);
3299
3300 ExplodedNodeSet EvalSet;
3301
3302 bool IsVectorType = A->getBase()->getType()->isVectorType();
3303
3304 // The "like" case is for situations where C standard prohibits the type to
3305 // be an lvalue, e.g. taking the address of a subscript of an expression of
3306 // type "void *".
3307 bool IsGLValueLike = A->isGLValue() ||
3308 (A->getType().isCForbiddenLValueType() && !AMgr.getLangOpts().CPlusPlus);
3309
3310 for (auto *Node : CheckerPreStmt) {
3311 const StackFrame *SF = Node->getStackFrame();
3312 ProgramStateRef state = Node->getState();
3313
3314 if (IsGLValueLike) {
3315 QualType T = A->getType();
3316
3317 // One of the forbidden LValue types! We still need to have sensible
3318 // symbolic locations to represent this stuff. Note that arithmetic on
3319 // void pointers is a GCC extension.
3320 if (T->isVoidType())
3321 T = getContext().CharTy;
3322
3323 SVal V = state->getLValue(T, state->getSVal(Idx, SF),
3324 state->getSVal(Base, SF));
3325 EvalSet.insert(
3326 Engine.makeNodeWithBinding(Node, A, V, ProgramPoint::PostLValueKind));
3327 } else if (IsVectorType) {
3328 // FIXME: non-glvalue vector reads are not modelled.
3329 EvalSet.insert(Engine.makePostStmtNode(A, state, Node));
3330 } else {
3331 llvm_unreachable("Array subscript should be an lValue when not \
3332a vector and not a forbidden lvalue type");
3333 }
3334 }
3335
3336 getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, A, *this);
3337}
3338
3339/// VisitMemberExpr - Transfer function for member expressions.
3341 ExplodedNodeSet &Dst) {
3342 // FIXME: Prechecks eventually go in ::Visit().
3343 ExplodedNodeSet CheckedSet;
3344 getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, M, *this);
3345
3346 ExplodedNodeSet EvalSet;
3348
3349 // Handle static member variables and enum constants accessed via
3350 // member syntax.
3352 for (const auto I : CheckedSet)
3353 VisitCommonDeclRefExpr(M, Member, I, EvalSet);
3354 } else {
3355 NodeBuilder Bldr(CheckedSet, EvalSet, *currBldrCtx);
3356 ExplodedNodeSet Tmp;
3357
3358 for (const auto I : CheckedSet) {
3359 ProgramStateRef state = I->getState();
3360 const StackFrame *SF = I->getStackFrame();
3361 Expr *BaseExpr = M->getBase();
3362
3363 // Handle C++ method calls.
3364 if (const auto *MD = dyn_cast<CXXMethodDecl>(Member)) {
3365 if (MD->isImplicitObjectMemberFunction())
3366 state = createTemporaryRegionIfNeeded(state, SF, BaseExpr);
3367
3368 SVal MDVal = svalBuilder.getFunctionPointer(MD);
3369 state = state->BindExpr(M, SF, MDVal);
3370
3371 Bldr.generateNode(M, I, state);
3372 continue;
3373 }
3374
3375 // Handle regular struct fields / member variables.
3376 const SubRegion *MR = nullptr;
3377 state = createTemporaryRegionIfNeeded(state, SF, BaseExpr,
3378 /*Result=*/nullptr,
3379 /*OutRegionWithAdjustments=*/&MR);
3380 SVal baseExprVal =
3381 MR ? loc::MemRegionVal(MR) : state->getSVal(BaseExpr, SF);
3382
3383 // FIXME: Copied from RegionStoreManager::bind()
3384 if (const auto *SR =
3385 dyn_cast_or_null<SymbolicRegion>(baseExprVal.getAsRegion())) {
3386 QualType T = SR->getPointeeStaticType();
3387 baseExprVal =
3388 loc::MemRegionVal(getStoreManager().GetElementZeroRegion(SR, T));
3389 }
3390
3391 const auto *field = cast<FieldDecl>(Member);
3392 SVal L = state->getLValue(field, baseExprVal);
3393
3394 if (M->isGLValue() || M->getType()->isArrayType()) {
3395 // We special-case rvalues of array type because the analyzer cannot
3396 // reason about them, since we expect all regions to be wrapped in Locs.
3397 // We instead treat these as lvalues and assume that they will decay to
3398 // pointers as soon as they are used.
3399 if (!M->isGLValue()) {
3400 assert(M->getType()->isArrayType());
3401 const auto *PE =
3402 dyn_cast<ImplicitCastExpr>(I->getParentMap().getParentIgnoreParens(M));
3403 if (!PE || PE->getCastKind() != CK_ArrayToPointerDecay) {
3404 llvm_unreachable("should always be wrapped in ArrayToPointerDecay");
3405 }
3406 }
3407
3408 if (field->getType()->isReferenceType()) {
3409 if (const MemRegion *R = L.getAsRegion())
3410 L = state->getSVal(R);
3411 else
3412 L = UnknownVal();
3413 }
3414
3415 Bldr.generateNode(M, I, state->BindExpr(M, SF, L), nullptr,
3417 } else {
3418 Bldr.takeNodes(I);
3419 evalLoad(Tmp, M, M, I, state, L);
3420 Bldr.addNodes(Tmp);
3421 }
3422 }
3423 }
3424
3425 getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, M, *this);
3426}
3427
3429 ExplodedNodeSet &Dst) {
3430 ExplodedNodeSet AfterPreSet;
3431 getCheckerManager().runCheckersForPreStmt(AfterPreSet, Pred, AE, *this);
3432
3433 // For now, treat all the arguments to C11 atomics as escaping.
3434 // FIXME: Ideally we should model the behavior of the atomics precisely here.
3435
3436 ExplodedNodeSet AfterInvalidateSet;
3437 NodeBuilder Bldr(AfterPreSet, AfterInvalidateSet, *currBldrCtx);
3438
3439 for (const auto I : AfterPreSet) {
3440 ProgramStateRef State = I->getState();
3441 const StackFrame *SF = I->getStackFrame();
3442
3443 SmallVector<SVal, 8> ValuesToInvalidate;
3444 for (unsigned SI = 0, Count = AE->getNumSubExprs(); SI != Count; SI++) {
3445 const Expr *SubExpr = AE->getSubExprs()[SI];
3446 SVal SubExprVal = State->getSVal(SubExpr, SF);
3447 ValuesToInvalidate.push_back(SubExprVal);
3448 }
3449
3450 State = State->invalidateRegions(ValuesToInvalidate, getCFGElementRef(),
3452 /*CausedByPointerEscape*/ true,
3453 /*Symbols=*/nullptr);
3454
3455 SVal ResultVal = UnknownVal();
3456 State = State->BindExpr(AE, SF, ResultVal);
3457 Bldr.generateNode(AE, I, State, nullptr,
3459 }
3460
3461 getCheckerManager().runCheckersForPostStmt(Dst, AfterInvalidateSet, AE, *this);
3462}
3463
3464// A value escapes in four possible cases:
3465// (1) We are binding to something that is not a memory region.
3466// (2) We are binding to a MemRegion that does not have stack storage.
3467// (3) We are binding to a top-level parameter region with a non-trivial
3468// destructor. We won't see the destructor during analysis, but it's there.
3469// (4) We are binding to a MemRegion with stack storage that the store
3470// does not understand.
3472 ProgramStateRef State, ArrayRef<std::pair<SVal, SVal>> LocAndVals,
3473 const StackFrame *SF, PointerEscapeKind Kind, const CallEvent *Call) {
3474 SmallVector<SVal, 8> Escaped;
3475 for (const std::pair<SVal, SVal> &LocAndVal : LocAndVals) {
3476 // Cases (1) and (2).
3477 const MemRegion *MR = LocAndVal.first.getAsRegion();
3478 const MemSpaceRegion *Space = MR ? MR->getMemorySpace(State) : nullptr;
3480 Escaped.push_back(LocAndVal.second);
3481 continue;
3482 }
3483
3484 // Case (3).
3485 if (const auto *VR = dyn_cast<VarRegion>(MR->getBaseRegion()))
3486 if (isa<StackArgumentsSpaceRegion>(Space) &&
3487 VR->getStackFrame()->inTopFrame())
3488 if (const auto *RD = VR->getValueType()->getAsCXXRecordDecl())
3489 if (!RD->hasTrivialDestructor()) {
3490 Escaped.push_back(LocAndVal.second);
3491 continue;
3492 }
3493
3494 // Case (4): in order to test that, generate a new state with the binding
3495 // added. If it is the same state, then it escapes (since the store cannot
3496 // represent the binding).
3497 // Do this only if we know that the store is not supposed to generate the
3498 // same state.
3499 SVal StoredVal = State->getSVal(MR);
3500 if (StoredVal != LocAndVal.second)
3501 if (State ==
3502 (State->bindLoc(loc::MemRegionVal(MR), LocAndVal.second, SF)))
3503 Escaped.push_back(LocAndVal.second);
3504 }
3505
3506 if (Escaped.empty())
3507 return State;
3508
3509 return escapeValues(State, Escaped, Kind, Call);
3510}
3511
3513 SVal Loc, SVal Val,
3514 const StackFrame *SF) {
3515 std::pair<SVal, SVal> LocAndVal(Loc, Val);
3516 return processPointerEscapedOnBind(State, LocAndVal, SF, PSK_EscapeOnBind,
3517 nullptr);
3518}
3519
3522 const InvalidatedSymbols *Invalidated,
3523 ArrayRef<const MemRegion *> ExplicitRegions,
3524 const CallEvent *Call,
3526 if (!Invalidated || Invalidated->empty())
3527 return State;
3528
3529 if (!Call)
3531 *Invalidated,
3532 nullptr,
3534 &ITraits);
3535
3536 // If the symbols were invalidated by a call, we want to find out which ones
3537 // were invalidated directly due to being arguments to the call.
3538 InvalidatedSymbols SymbolsDirectlyInvalidated;
3539 for (const auto I : ExplicitRegions) {
3540 if (const SymbolicRegion *R = I->StripCasts()->getAs<SymbolicRegion>())
3541 SymbolsDirectlyInvalidated.insert(R->getSymbol());
3542 }
3543
3544 InvalidatedSymbols SymbolsIndirectlyInvalidated;
3545 for (const auto &sym : *Invalidated) {
3546 if (SymbolsDirectlyInvalidated.count(sym))
3547 continue;
3548 SymbolsIndirectlyInvalidated.insert(sym);
3549 }
3550
3551 if (!SymbolsDirectlyInvalidated.empty())
3553 SymbolsDirectlyInvalidated, Call, PSK_DirectEscapeOnCall, &ITraits);
3554
3555 // Notify about the symbols that get indirectly invalidated by the call.
3556 if (!SymbolsIndirectlyInvalidated.empty())
3558 SymbolsIndirectlyInvalidated, Call, PSK_IndirectEscapeOnCall, &ITraits);
3559
3560 return State;
3561}
3562
3563/// evalBind - Handle the semantics of binding a value to a specific location.
3564/// This method is used by evalStore, VisitDeclStmt, and others.
3565void ExprEngine::evalBind(ExplodedNodeSet &Dst, const Stmt *StoreE,
3566 ExplodedNode *Pred, SVal Location, SVal Val,
3567 bool AtDeclInit, const ProgramPoint *PP) {
3568
3569 // It may be a Loc, UnknownVal or perhaps UndefinedVal.
3570 assert(!isa<NonLoc>(Location) && "evalBind location should not be NonLoc!");
3571
3572 const StackFrame *SF = Pred->getStackFrame();
3573 PostStmt DefaultPP(StoreE, SF);
3574
3575 if (!PP)
3576 PP = &DefaultPP;
3577
3578 // Do a previsit of the bind.
3579 ExplodedNodeSet CheckedSet;
3580 getCheckerManager().runCheckersForBind(CheckedSet, Pred, Location, Val,
3581 StoreE, AtDeclInit, *this, *PP);
3582
3583 for (ExplodedNode *PredI : CheckedSet) {
3584 ProgramStateRef State = PredI->getState();
3585
3586 // Check and record that 'Val' may escape:
3587 State = processPointerEscapedOnBind(State, Location, Val, SF);
3588
3589 if (auto AsLoc = Location.getAs<Loc>()) {
3590 // When binding the value, pass on the hint that this is a
3591 // initialization. For initializations, we do not need to inform clients
3592 // of region changes.
3593 State = State->bindLoc(*AsLoc, Val, SF, /*notifyChanges=*/!AtDeclInit);
3594 }
3595
3596 PostStore PS(StoreE, SF, Location.getAsRegion(), /*tag=*/nullptr);
3597 Dst.insert(Engine.makeNode(PS, State, PredI));
3598 }
3599}
3600
3601/// evalStore - Handle the semantics of a store via an assignment.
3602/// @param Dst The node set to store generated state nodes
3603/// @param AssignE The assignment expression if the store happens in an
3604/// assignment.
3605/// @param LocationE The location expression that is stored to.
3606/// @param state The current simulation state
3607/// @param location The location to store the value
3608/// @param Val The value to be stored
3610 const Expr *LocationE,
3611 ExplodedNode *Pred,
3612 ProgramStateRef state, SVal location, SVal Val,
3613 const ProgramPointTag *tag) {
3614 // Proceed with the store. We use AssignE as the anchor for the PostStore
3615 // ProgramPoint if it is non-NULL, and LocationE otherwise.
3616 const Expr *StoreE = AssignE ? AssignE : LocationE;
3617
3618 // Evaluate the location (checks for bad dereferences).
3619 ExplodedNodeSet Tmp;
3620 evalLocation(Tmp, AssignE, LocationE, Pred, state, location, false);
3621
3622 if (Tmp.empty())
3623 return;
3624
3625 if (location.isUndef())
3626 return;
3627
3628 for (const auto I : Tmp)
3629 evalBind(Dst, StoreE, I, location, Val, false);
3630}
3631
3633 const Expr *NodeEx,
3634 const Expr *BoundEx,
3635 ExplodedNode *Pred,
3636 ProgramStateRef state,
3637 SVal location,
3638 const ProgramPointTag *tag,
3639 QualType LoadTy) {
3640 assert(!isa<NonLoc>(location) && "location cannot be a NonLoc.");
3641 assert(NodeEx);
3642 assert(BoundEx);
3643 // Evaluate the location (checks for bad dereferences).
3644 ExplodedNodeSet Tmp;
3645 evalLocation(Tmp, NodeEx, BoundEx, Pred, state, location, true);
3646 if (Tmp.empty())
3647 return;
3648
3649 NodeBuilder Bldr(Tmp, Dst, *currBldrCtx);
3650 if (location.isUndef())
3651 return;
3652
3653 // Proceed with the load.
3654 for (const auto I : Tmp) {
3655 state = I->getState();
3656
3657 SVal V = UnknownVal();
3658 if (location.isValid()) {
3659 if (LoadTy.isNull())
3660 LoadTy = BoundEx->getType();
3661 V = state->getSVal(location.castAs<Loc>(), LoadTy);
3662 }
3663
3664 Bldr.generateNode(NodeEx, I,
3665 state->BindExpr(BoundEx, I->getStackFrame(), V), tag,
3667 }
3668}
3669
3670void ExprEngine::evalLocation(ExplodedNodeSet &Dst,
3671 const Stmt *NodeEx,
3672 const Stmt *BoundEx,
3673 ExplodedNode *Pred,
3674 ProgramStateRef state,
3675 SVal location,
3676 bool isLoad) {
3677 NodeBuilder BldrTop(Pred, Dst, *currBldrCtx);
3678 // Early checks for performance reason.
3679 if (location.isUnknown()) {
3680 return;
3681 }
3682
3683 ExplodedNodeSet Src;
3684 BldrTop.takeNodes(Pred);
3685 NodeBuilder Bldr(Pred, Src, *currBldrCtx);
3686 if (Pred->getState() != state) {
3687 // Associate this new state with an ExplodedNode.
3688 // FIXME: If I pass null tag, the graph is incorrect, e.g for
3689 // int *p;
3690 // p = 0;
3691 // *p = 0xDEADBEEF;
3692 // "p = 0" is not noted as "Null pointer value stored to 'p'" but
3693 // instead "int *p" is noted as
3694 // "Variable 'p' initialized to a null pointer value"
3695
3696 static SimpleProgramPointTag tag(TagProviderName, "Location");
3697 Bldr.generateNode(NodeEx, Pred, state, &tag);
3698 }
3699 ExplodedNodeSet Tmp;
3700 getCheckerManager().runCheckersForLocation(Tmp, Src, location, isLoad,
3701 NodeEx, BoundEx, *this);
3702 BldrTop.addNodes(Tmp);
3703}
3704
3705std::pair<const ProgramPointTag *, const ProgramPointTag *>
3707 static SimpleProgramPointTag TrueTag(TagProviderName, "Eagerly Assume True"),
3708 FalseTag(TagProviderName, "Eagerly Assume False");
3709
3710 return std::make_pair(&TrueTag, &FalseTag);
3711}
3712
3713/// If the last EagerlyAssume attempt was successful (i.e. the true and false
3714/// cases were both feasible), this state trait stores the expression where it
3715/// happened; otherwise this holds nullptr.
3716REGISTER_TRAIT_WITH_PROGRAMSTATE(LastEagerlyAssumeExprIfSuccessful,
3717 const Expr *)
3718
3720 ExplodedNodeSet &Src,
3721 const Expr *Ex) {
3722 NodeBuilder Bldr(Src, Dst, *currBldrCtx);
3723
3724 for (ExplodedNode *Pred : Src) {
3725 // Test if the previous node was as the same expression. This can happen
3726 // when the expression fails to evaluate to anything meaningful and
3727 // (as an optimization) we don't generate a node.
3728 ProgramPoint P = Pred->getLocation();
3729 if (!P.getAs<PostStmt>() || P.castAs<PostStmt>().getStmt() != Ex) {
3730 continue;
3731 }
3732
3733 ProgramStateRef State = Pred->getState();
3734 State = State->set<LastEagerlyAssumeExprIfSuccessful>(nullptr);
3735 SVal V = State->getSVal(Ex, Pred->getStackFrame());
3736 std::optional<nonloc::SymbolVal> SEV = V.getAs<nonloc::SymbolVal>();
3737 if (SEV && SEV->isExpression()) {
3738 const auto &[TrueTag, FalseTag] = getEagerlyAssumeBifurcationTags();
3739
3740 auto [StateTrue, StateFalse] = State->assume(*SEV);
3741
3742 if (StateTrue && StateFalse) {
3743 StateTrue = StateTrue->set<LastEagerlyAssumeExprIfSuccessful>(Ex);
3744 StateFalse = StateFalse->set<LastEagerlyAssumeExprIfSuccessful>(Ex);
3745 }
3746
3747 // First assume that the condition is true.
3748 if (StateTrue) {
3749 SVal Val = svalBuilder.makeIntVal(1U, Ex->getType());
3750 StateTrue = StateTrue->BindExpr(Ex, Pred->getStackFrame(), Val);
3751 Bldr.generateNode(Ex, Pred, StateTrue, TrueTag);
3752 }
3753
3754 // Next, assume that the condition is false.
3755 if (StateFalse) {
3756 SVal Val = svalBuilder.makeIntVal(0U, Ex->getType());
3757 StateFalse = StateFalse->BindExpr(Ex, Pred->getStackFrame(), Val);
3758 Bldr.generateNode(Ex, Pred, StateFalse, FalseTag);
3759 }
3760 }
3761 }
3762}
3763
3765 const Expr *Ex) const {
3766 return Ex && State->get<LastEagerlyAssumeExprIfSuccessful>() == Ex;
3767}
3768
3770 ExplodedNodeSet &Dst) {
3771 NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
3772 // We have processed both the inputs and the outputs. All of the outputs
3773 // should evaluate to Locs. Nuke all of their values.
3774
3775 // FIXME: Some day in the future it would be nice to allow a "plug-in"
3776 // which interprets the inline asm and stores proper results in the
3777 // outputs.
3778
3779 ProgramStateRef state = Pred->getState();
3780
3781 for (const Expr *O : A->outputs()) {
3782 SVal X = state->getSVal(O, Pred->getStackFrame());
3783 assert(!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
3784
3785 if (std::optional<Loc> LV = X.getAs<Loc>())
3786 state = state->invalidateRegions(*LV, getCFGElementRef(),
3788 Pred->getStackFrame(),
3789 /*CausedByPointerEscape=*/true);
3790 }
3791
3792 // Do not reason about locations passed inside inline assembly.
3793 for (const Expr *I : A->inputs()) {
3794 SVal X = state->getSVal(I, Pred->getStackFrame());
3795
3796 if (std::optional<Loc> LV = X.getAs<Loc>())
3797 state = state->invalidateRegions(*LV, getCFGElementRef(),
3799 Pred->getStackFrame(),
3800 /*CausedByPointerEscape=*/true);
3801 }
3802
3803 Bldr.generateNode(A, Pred, state);
3804}
3805
3807 ExplodedNodeSet &Dst) {
3808 NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
3809 Bldr.generateNode(A, Pred, Pred->getState());
3810}
3811
3812//===----------------------------------------------------------------------===//
3813// Visualization.
3814//===----------------------------------------------------------------------===//
3815
3816namespace llvm {
3817
3818template<>
3820 DOTGraphTraits (bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
3821
3822 static bool nodeHasBugReport(const ExplodedNode *N) {
3823 BugReporter &BR = static_cast<ExprEngine &>(
3824 N->getState()->getStateManager().getOwningEngine()).getBugReporter();
3825
3826 for (const auto &Class : BR.equivalenceClasses()) {
3827 for (const auto &Report : Class.getReports()) {
3828 const auto *PR = dyn_cast<PathSensitiveBugReport>(Report.get());
3829 if (!PR)
3830 continue;
3831 const ExplodedNode *EN = PR->getErrorNode();
3832 if (EN->getState() == N->getState() &&
3833 EN->getLocation() == N->getLocation())
3834 return true;
3835 }
3836 }
3837 return false;
3838 }
3839
3840 /// \p PreCallback: callback before break.
3841 /// \p PostCallback: callback after break.
3842 /// \p Stop: stop iteration if returns @c true
3843 /// \return Whether @c Stop ever returned @c true.
3845 const ExplodedNode *N,
3846 llvm::function_ref<void(const ExplodedNode *)> PreCallback,
3847 llvm::function_ref<void(const ExplodedNode *)> PostCallback,
3848 llvm::function_ref<bool(const ExplodedNode *)> Stop) {
3849 while (true) {
3850 PreCallback(N);
3851 if (Stop(N))
3852 return true;
3853
3854 if (N->succ_size() != 1 || !isNodeHidden(N->getFirstSucc(), nullptr))
3855 break;
3856 PostCallback(N);
3857
3858 N = N->getFirstSucc();
3859 }
3860 return false;
3861 }
3862
3863 static bool isNodeHidden(const ExplodedNode *N, const ExplodedGraph *G) {
3864 return N->isTrivial();
3865 }
3866
3867 static std::string getNodeLabel(const ExplodedNode *N, ExplodedGraph *G){
3868 std::string Buf;
3869 llvm::raw_string_ostream Out(Buf);
3870
3871 const bool IsDot = true;
3872 const unsigned int Space = 1;
3873 ProgramStateRef State = N->getState();
3874
3875 Out << "{ \"state_id\": " << State->getID()
3876 << ",\\l";
3877
3878 Indent(Out, Space, IsDot) << "\"program_points\": [\\l";
3879
3880 // Dump program point for all the previously skipped nodes.
3882 N,
3883 [&](const ExplodedNode *OtherNode) {
3884 Indent(Out, Space + 1, IsDot) << "{ ";
3885 OtherNode->getLocation().printJson(Out, /*NL=*/"\\l");
3886 Out << ", \"tag\": ";
3887 if (const ProgramPointTag *Tag = OtherNode->getLocation().getTag())
3888 Out << '\"' << Tag->getDebugTag() << '\"';
3889 else
3890 Out << "null";
3891 Out << ", \"node_id\": " << OtherNode->getID() <<
3892 ", \"is_sink\": " << OtherNode->isSink() <<
3893 ", \"has_report\": " << nodeHasBugReport(OtherNode) << " }";
3894 },
3895 // Adds a comma and a new-line between each program point.
3896 [&](const ExplodedNode *) { Out << ",\\l"; },
3897 [&](const ExplodedNode *) { return false; });
3898
3899 Out << "\\l"; // Adds a new-line to the last program point.
3900 Indent(Out, Space, IsDot) << "],\\l";
3901
3902 State->printDOT(Out, N->getStackFrame(), Space);
3903
3904 Out << "\\l}\\l";
3905 return Buf;
3906 }
3907};
3908
3909} // namespace llvm
3910
3911void ExprEngine::ViewGraph(bool trim) {
3912 std::string Filename = DumpGraph(trim);
3913 llvm::DisplayGraph(Filename, false, llvm::GraphProgram::DOT);
3914}
3915
3917 std::string Filename = DumpGraph(Nodes);
3918 llvm::DisplayGraph(Filename, false, llvm::GraphProgram::DOT);
3919}
3920
3921std::string ExprEngine::DumpGraph(bool trim, StringRef Filename) {
3922 if (trim) {
3923 std::vector<const ExplodedNode *> Src;
3924
3925 // Iterate through the reports and get their nodes.
3926 for (const auto &Class : BR.equivalenceClasses()) {
3927 const auto *R =
3928 dyn_cast<PathSensitiveBugReport>(Class.getReports()[0].get());
3929 if (!R)
3930 continue;
3931 const auto *N = const_cast<ExplodedNode *>(R->getErrorNode());
3932 Src.push_back(N);
3933 }
3934 return DumpGraph(Src, Filename);
3935 }
3936
3937 // FIXME(sandboxing): Remove this by adopting `llvm::vfs::OutputBackend`.
3938 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
3939 return llvm::WriteGraph(&G, "ExprEngine", /*ShortNames=*/false,
3940 /*Title=*/"Exploded Graph",
3941 /*Filename=*/std::string(Filename));
3942}
3943
3945 StringRef Filename) {
3946 std::unique_ptr<ExplodedGraph> TrimmedG(G.trim(Nodes));
3947
3948 if (!TrimmedG) {
3949 llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n";
3950 return "";
3951 }
3952
3953 // FIXME(sandboxing): Remove this by adopting `llvm::vfs::OutputBackend`.
3954 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
3955 return llvm::WriteGraph(TrimmedG.get(), "TrimmedExprEngine",
3956 /*ShortNames=*/false,
3957 /*Title=*/"Trimmed Exploded Graph",
3958 /*Filename=*/std::string(Filename));
3959}
3960
3962 static int index = 0;
3963 return &index;
3964}
3965
3966void ExprEngine::anchor() { }
3967
3969 bool IsTransparent, ExplodedNode *Pred,
3970 ExplodedNodeSet &Dst) {
3972
3973 const StackFrame *SF = Pred->getStackFrame();
3974
3975 NodeBuilder B(Pred, Dst, *currBldrCtx);
3976 ProgramStateRef S = Pred->getState();
3978
3979 bool IsCompound = T->isArrayType() || T->isRecordType() ||
3980 T->isAnyComplexType() || T->isVectorType();
3981
3982 if (Args.size() > 1 || (E->isPRValue() && IsCompound && !IsTransparent)) {
3983 llvm::ImmutableList<SVal> ArgList = getBasicVals().getEmptySValList();
3984 for (Expr *E : llvm::reverse(Args))
3985 ArgList = getBasicVals().prependSVal(S->getSVal(E, SF), ArgList);
3986
3987 B.generateNode(E, Pred,
3988 S->BindExpr(E, SF, svalBuilder.makeCompoundVal(T, ArgList)));
3989 } else {
3990 B.generateNode(E, Pred,
3991 S->BindExpr(E, SF,
3992 Args.size() == 0
3993 ? getSValBuilder().makeZeroVal(T)
3994 : S->getSVal(Args.front(), SF)));
3995 }
3996}
Defines the clang::ASTContext interface.
#define V(N, I)
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
static const MemRegion * getRegion(const CallEvent &Call, const MutexDescriptor &Descriptor, bool IsLock)
static Decl::Kind getKind(const Decl *D)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
#define STAT_COUNTER(VARNAME, DESC)
Defines the clang::Expr interface and subclasses for C++ expressions.
static const Stmt * getRightmostLeaf(const Stmt *Condition)
static void printIndicesOfElementsToConstructJson(raw_ostream &Out, ProgramStateRef State, const char *NL, const StackFrame *SF, unsigned int Space=0, bool IsDot=false)
static const Stmt * ResolveCondition(const Stmt *Condition, const CFGBlock *B)
std::pair< const ObjCForCollectionStmt *, const StackFrame * > ObjCForLctxPair
static SVal RecoverCastedSymbol(ProgramStateRef state, const Stmt *Condition, const StackFrame *SF, ASTContext &Ctx)
RecoverCastedSymbol - A helper function for ProcessBranch that is used to try to recover some path-se...
static void printStateTraitWithStackFrameJson(raw_ostream &Out, ProgramStateRef State, const StackFrame *SF, const char *NL, unsigned int Space, bool IsDot, const char *jsonPropertyName, Printer printer, Args &&...args)
A helper function to generalize program state trait printing.
static void printPendingInitLoopJson(raw_ostream &Out, ProgramStateRef State, const char *NL, const StackFrame *SF, unsigned int Space=0, bool IsDot=false)
REGISTER_TRAIT_WITH_PROGRAMSTATE(ObjectsUnderConstruction, ObjectsUnderConstructionMap) typedef llvm REGISTER_TRAIT_WITH_PROGRAMSTATE(IndexOfElementToConstruct, IndexOfElementToConstructMap) typedef llvm typedef llvm::ImmutableMap< const StackFrame *, unsigned > PendingArrayDestructionMap
llvm::ImmutableMap< ConstructedObjectKey, SVal > ObjectsUnderConstructionMap
static bool shouldRemoveDeadBindings(AnalysisManager &AMgr, const Stmt *S, const ExplodedNode *Pred, const StackFrame *SF)
static void printObjectsUnderConstructionJson(raw_ostream &Out, ProgramStateRef State, const char *NL, const StackFrame *SF, unsigned int Space=0, bool IsDot=false)
static std::optional< std::pair< ProgramStateRef, ProgramStateRef > > assumeCondition(const Stmt *ConditionStmt, ExplodedNode *N)
Split the state on whether there are any more iterations left for this loop.
static void printPendingArrayDestructionsJson(raw_ostream &Out, ProgramStateRef State, const char *NL, const StackFrame *SF, unsigned int Space=0, bool IsDot=false)
TokenType getType() const
Returns the token's type, e.g.
FormatToken * Next
The next token in the unwrapped line.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Result
Implement __builtin_bit_cast and related operations.
#define X(type, name)
Definition Value.h:97
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
This header contains the declarations of functions which are used to decide which loops should be com...
This header contains the declarations of functions which are used to widen loops which do not otherwi...
Defines the PrettyStackTraceEntry class, which is used to make crashes give more contextual informati...
#define REGISTER_MAP_WITH_PROGRAMSTATE(Name, Key, Value)
Declares an immutable map of type NameTy, suitable for placement into the ProgramState.
#define REGISTER_TRAIT_WITH_PROGRAMSTATE(Name, Type)
Declares a program state trait for type Type called Name, and introduce a type named NameTy.
static bool isRecordType(QualType T)
Defines the clang::SourceLocation class and associated facilities.
Defines various enumerations that describe declaration and type specifiers.
Defines the Objective-C statement AST node classes.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
CanQualType CharTy
const clang::PrintingPolicy & getPrintingPolicy() const
Definition ASTContext.h:855
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
ASTContext & getASTContext() const
Stores options for the analyzer from the command line.
AnalysisPurgeMode AnalysisPurgeOpt
Represents a loop initializing the elements of an array.
Definition Expr.h:5971
OpaqueValueExpr * getCommonExpr() const
Get the common subexpression shared by all initializations (the source array).
Definition Expr.h:5986
Expr * getSubExpr() const
Get the initializer to use for each array element.
Definition Expr.h:5991
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition Expr.h:2727
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3786
outputs_range outputs()
Definition Stmt.h:3430
inputs_range inputs()
Definition Stmt.h:3401
AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, __atomic_load,...
Definition Expr.h:6931
Expr ** getSubExprs()
Definition Expr.h:7006
static unsigned getNumSubExprs(AtomicOp Op)
Determine the number of arguments the specified atomic builtin should have.
Definition Expr.cpp:5278
const CFGBlock * getPreviousBlock() const
const CFGBlock * getBlock() const
Represents C++ object destructor implicitly generated for automatic object or temporary bound to cons...
Definition CFG.h:465
const VarDecl * getVarDecl() const
Definition CFG.h:470
const Stmt * getTriggerStmt() const
Definition CFG.h:475
Represents C++ object destructor implicitly generated for base object in destructor.
Definition CFG.h:516
const CXXBaseSpecifier * getBaseSpecifier() const
Definition CFG.h:521
Represents a single basic block in a source-level CFG.
Definition CFG.h:652
succ_reverse_iterator succ_rend()
Definition CFG.h:1043
succ_reverse_iterator succ_rbegin()
Definition CFG.h:1042
succ_range succs()
Definition CFG.h:1047
CFGTerminator getTerminator() const
Definition CFG.h:1132
Stmt * getTerminatorStmt()
Definition CFG.h:1134
unsigned getBlockID() const
Definition CFG.h:1154
unsigned succ_size() const
Definition CFG.h:1055
Represents C++ object destructor generated from a call to delete.
Definition CFG.h:490
const CXXDeleteExpr * getDeleteExpr() const
Definition CFG.h:500
Represents a top-level expression in a basic block.
Definition CFG.h:55
@ CleanupFunction
Definition CFG.h:83
@ CXXRecordTypedCall
Definition CFG.h:72
@ FullExprCleanup
Definition CFG.h:62
@ AutomaticObjectDtor
Definition CFG.h:76
T castAs() const
Convert to the specified CFGElement type, asserting that this CFGElement is of the desired type.
Definition CFG.h:103
Kind getKind() const
Definition CFG.h:122
Represents C++ object destructor implicitly generated by compiler on various occasions.
Definition CFG.h:414
const CXXDestructorDecl * getDestructorDecl(ASTContext &astContext) const
Definition CFG.cpp:5511
Represents C++ base or member initializer from constructor's initialization list.
Definition CFG.h:232
const CXXCtorInitializer * getInitializer() const
Definition CFG.h:237
Represents the point where the lifetime of an automatic object ends.
Definition CFG.h:321
const VarDecl * getVarDecl() const
Definition CFG.h:326
Represents the point where a loop ends.
Definition CFG.h:278
const Stmt * getLoopStmt() const
Definition CFG.h:282
Represents C++ object destructor implicitly generated for member object in destructor.
Definition CFG.h:537
const FieldDecl * getFieldDecl() const
Definition CFG.h:542
Represents C++ allocator call.
Definition CFG.h:252
const CXXNewExpr * getAllocatorExpr() const
Definition CFG.h:258
LLVM_ATTRIBUTE_RETURNS_NONNULL const Stmt * getTriggerStmt() const
Definition CFG.h:300
const Stmt * getStmt() const
Definition CFG.h:143
Represents C++ object destructor implicitly generated at the end of full expression for temporary obj...
Definition CFG.h:558
const CXXBindTemporaryExpr * getBindTemporaryExpr() const
Definition CFG.h:563
bool isStmtBranch() const
Definition CFG.h:615
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents binding an expression to a temporary.
Definition ExprCXX.h:1497
const Expr * getSubExpr() const
Definition ExprCXX.h:1519
SourceLocation getBeginLoc() const LLVM_READONLY
Definition ExprCXX.h:1523
Represents a call to a C++ constructor.
Definition ExprCXX.h:1552
Represents a C++ base or member initializer.
Definition DeclCXX.h:2398
FieldDecl * getMember() const
If this is a member initializer, returns the declaration of the non-static data member being initiali...
Definition DeclCXX.h:2538
bool isDelegatingInitializer() const
Determine whether this initializer is creating a delegating constructor.
Definition DeclCXX.h:2498
Expr * getInit() const
Get the initializer.
Definition DeclCXX.h:2600
SourceLocation getSourceLocation() const
Determine the source location of the initializer.
Definition DeclCXX.cpp:2951
bool isAnyMemberInitializer() const
Definition DeclCXX.h:2478
bool isBaseInitializer() const
Determine whether this initializer is initializing a base class.
Definition DeclCXX.h:2470
bool isIndirectMemberInitializer() const
Definition DeclCXX.h:2482
int64_t getID(const ASTContext &Context) const
Definition DeclCXX.cpp:2932
const Type * getBaseClass() const
If this is a base class initializer, returns the type of the base class.
Definition DeclCXX.cpp:2944
FieldDecl * getAnyMember() const
Definition DeclCXX.h:2544
IndirectFieldDecl * getIndirectMember() const
Definition DeclCXX.h:2552
bool isBaseVirtual() const
Returns whether the base is virtual or not.
Definition DeclCXX.h:2524
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition ExprCXX.h:2630
bool isArrayForm() const
Definition ExprCXX.h:2656
SourceLocation getBeginLoc() const
Definition ExprCXX.h:2680
QualType getDestroyedType() const
Retrieve the type being destroyed.
Definition ExprCXX.cpp:343
Represents a C++ destructor within a class.
Definition DeclCXX.h:2895
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition ExprCXX.h:2359
Represents a list-initialization with parenthesis.
Definition ExprCXX.h:5141
MutableArrayRef< Expr * > getInitExprs()
Definition ExprCXX.h:5181
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
void getCaptureFields(llvm::DenseMap< const ValueDecl *, FieldDecl * > &Captures, FieldDecl *&ThisCapture) const
For a closure type, retrieve the mapping from captured variables and this to the non-static data memb...
Definition DeclCXX.cpp:1790
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition DeclCXX.cpp:2127
Represents a point when we begin processing an inlined call.
CaseStmt - Represent a case statement.
Definition Stmt.h:1930
Expr * getLHS()
Definition Stmt.h:2013
Expr * getRHS()
Definition Stmt.h:2025
Represents a single point (AST node) in the program that requires attention during construction of an...
unsigned getIndex() const
If a single trigger statement triggers multiple constructors, they are usually being enumerated.
const CXXCtorInitializer * getCXXCtorInitializer() const
The construction site is not necessarily a statement.
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2122
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1276
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1641
const Decl * getSingleDecl() const
Definition Stmt.h:1656
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
This is a meta program point, which should be skipped by all the diagnostic reasoning etc.
This represents one expression.
Definition Expr.h:112
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
Walk outwards from an expression we want to bind a reference to and find the expression whose lifetim...
Definition Expr.cpp:85
bool isGLValue() const
Definition Expr.h:287
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
Expr * IgnoreImplicit() LLVM_READONLY
Skip past any implicit AST nodes which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3087
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3095
bool isPRValue() const
Definition Expr.h:285
QualType getType() const
Definition Expr.h:144
Represents a member of a struct/union/class.
Definition Decl.h:3182
This represents a GCC inline-assembly statement extension.
Definition Stmt.h:3456
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
Describes an C or C++ initializer list.
Definition Expr.h:5305
bool isTransparent() const
Is this a transparent initializer list (that is, an InitListExpr that is purely syntactic,...
Definition Expr.cpp:2471
ArrayRef< Expr * > inits() const
Definition Expr.h:5358
Represents the declaration of a label.
Definition Decl.h:524
Represents a point when the lifetime of an automatic object ends.
Represents a point when we exit a loop.
This represents a Microsoft inline-assembly statement extension.
Definition Stmt.h:3675
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3370
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3453
Expr * getBase() const
Definition Expr.h:3447
This represents a decl that may have a name.
Definition Decl.h:274
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:340
Represents Objective-C's collection statement.
Definition StmtObjC.h:23
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition Expr.h:1234
bool isConsumedExpr(Expr *E) const
Represents a parameter to a function.
Definition Decl.h:1808
Represents a program point just after an implicit call event.
Represents a program point after a store evaluation.
Represents a program point just before an implicit call event.
If a crash happens while one of these objects are live, the message is printed out along with the spe...
ProgramPoints can be "tagged" as representing points specific to a given analysis entity.
const ProgramPointTag * getTag() const
bool isPurgeKind()
Is this a program point corresponding to purge/removal of dead symbols and bindings.
T castAs() const
Convert to the specified ProgramPoint type, asserting that this ProgramPoint is of the desired type.
static ProgramPoint getProgramPoint(const Stmt *S, ProgramPoint::Kind K, const StackFrame *SF, const ProgramPointTag *tag)
void printJson(llvm::raw_ostream &Out, const char *NL="\n") const
ProgramPoint withTag(const ProgramPointTag *tag) const
Create a new ProgramPoint object that is the same as the original except for using the specified tag ...
const StackFrame * getStackFrame() const
std::optional< T > getAs() const
Convert to the specified ProgramPoint type, returning std::nullopt if this ProgramPoint is not of the...
A (possibly-)qualified type.
Definition TypeBase.h:937
QualType getDesugaredType(const ASTContext &Context) const
Return the specified type with any "sugar" removed from the type.
Definition TypeBase.h:1311
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
QualType getCanonicalType() const
Definition TypeBase.h:8499
SplitQualType split() const
Divides a QualType into its unqualified type and a set of local qualifiers.
Definition TypeBase.h:8468
std::string getAsString() const
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition Stmt.h:3170
std::string printToString(const SourceManager &SM) const
It represents a stack frame of the call stack.
unsigned getIndex() const
LLVM_ATTRIBUTE_RETURNS_NONNULL AnalysisDeclContext * getAnalysisDeclContext() const
void printJson(raw_ostream &Out, const char *NL="\n", unsigned int Space=0, bool IsDot=false, std::function< void(const StackFrame *)> printMoreInfoPerStackFrame=[](const StackFrame *) {}) const
Prints out the call stack in json format.
const Expr * getCallSite() const
const Decl * getDecl() const
const StackFrame * getParent() const
It might return null.
const CFGBlock * getCallSiteBlock() const
const Stmt * getStmt() const
Stmt - This represents one statement.
Definition Stmt.h:86
@ NoStmtClass
Definition Stmt.h:89
void printJson(raw_ostream &Out, PrinterHelper *Helper, const PrintingPolicy &Policy, bool AddQuotes) const
Pretty-prints in JSON format.
StmtClass getStmtClass() const
Definition Stmt.h:1503
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
const char * getStmtClassName() const
Definition Stmt.cpp:86
int64_t getID(const ASTContext &Context) const
Definition Stmt.cpp:379
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
SwitchStmt - This represents a 'switch' stmt.
Definition Stmt.h:2519
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isArrayType() const
Definition TypeBase.h:8783
bool isReferenceType() const
Definition TypeBase.h:8708
bool isVectorType() const
Definition TypeBase.h:8823
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:924
This class is used for tools that requires cross translation unit capability.
llvm::ImmutableList< SVal > getEmptySValList()
llvm::ImmutableList< SVal > prependSVal(SVal X, llvm::ImmutableList< SVal > L)
BugReporter is a utility class for generating PathDiagnostics for analysis.
llvm::iterator_range< EQClasses_iterator > equivalenceClasses()
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:152
static bool isCallStmt(const Stmt *S)
Returns true if this is a statement is a function or method call of some kind.
ProgramStateRef runCheckersForRegionChanges(ProgramStateRef state, const InvalidatedSymbols *invalidated, ArrayRef< const MemRegion * > ExplicitRegions, ArrayRef< const MemRegion * > Regions, const StackFrame *SF, const CallEvent *Call)
Run checkers for region changes.
void runCheckersForLocation(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, SVal location, bool isLoad, const Stmt *NodeEx, const Stmt *BoundEx, ExprEngine &Eng)
Run checkers for load/store of a location.
void runCheckersForBind(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, SVal location, SVal val, const Stmt *S, bool AtDeclInit, ExprEngine &Eng, const ProgramPoint &PP)
Run checkers for binding of a value to a location.
void runCheckersForEndAnalysis(ExplodedGraph &G, BugReporter &BR, ExprEngine &Eng)
Run checkers for end of analysis.
void runCheckersForPrintStateJson(raw_ostream &Out, ProgramStateRef State, const char *NL="\n", unsigned int Space=0, bool IsDot=false) const
Run checkers for debug-printing a ProgramState.
void runCheckersForDeadSymbols(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, SymbolReaper &SymReaper, const Stmt *S, ExprEngine &Eng, ProgramPoint::Kind K)
Run checkers for dead symbols.
void runCheckersForEndFunction(ExplodedNodeSet &Dst, ExplodedNode *Pred, ExprEngine &Eng, const ReturnStmt *RS)
Run checkers on end of function.
void runCheckersForLiveSymbols(ProgramStateRef state, SymbolReaper &SymReaper)
Run checkers for live symbols.
void runCheckersForBeginFunction(ExplodedNodeSet &Dst, const BlockEdge &L, ExplodedNode *Pred, ExprEngine &Eng)
Run checkers on beginning of function.
void runCheckersForPostStmt(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const Stmt *S, ExprEngine &Eng, bool wasInlined=false)
Run checkers for post-visiting Stmts.
void runCheckersForPreStmt(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const Stmt *S, ExprEngine &Eng)
Run checkers for pre-visiting Stmts.
void runCheckersForBlockEntrance(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const BlockEntrance &Entrance, ExprEngine &Eng) const
Run checkers after taking a control flow edge.
void runCheckersForBranchCondition(const Stmt *condition, ExplodedNodeSet &Dst, ExplodedNode *Pred, ExprEngine &Eng)
Run checkers for branch condition.
ProgramStateRef runCheckersForPointerEscape(ProgramStateRef State, const InvalidatedSymbols &Escaped, const CallEvent *Call, PointerEscapeKind Kind, RegionAndSymbolInvalidationTraits *ITraits)
Run checkers when pointers escape.
void runCheckersForLifetimeEnd(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const VarDecl *Decl, ExprEngine &Eng)
Run checkers for the end of a variable's lifetime.
ProgramStateRef runCheckersForEvalAssume(ProgramStateRef state, SVal Cond, bool Assumption)
Run checkers for handling assumptions on symbolic values.
virtual ProgramStateRef removeDeadBindings(ProgramStateRef state, SymbolReaper &SymReaper)=0
Scan all symbols referenced by the constraints.
void enqueueStmtNode(ExplodedNode *N, const CFGBlock *Block, unsigned Idx)
Enqueue a single node created as a result of statement processing.
ExplodedNode * makeNode(const ProgramPoint &Loc, ProgramStateRef State, ExplodedNode *Pred, bool MarkAsSink=false) 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...
ExplodedNodeSet is a set of ExplodedNode * elements with the invariant that its elements cannot be nu...
void insert(ExplodedNode *N)
const ProgramStateRef & getState() const
bool isTrivial() const
The node is trivial if it has only one successor, only one predecessor, it's predecessor has only one...
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 ...
ExplodedNode * getFirstSucc()
unsigned succ_size() const
const StackFrame * getStackFrame() const
void VisitBinaryOperator(const BinaryOperator *B, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitBinaryOperator - Transfer function logic for binary operators.
const StackFrame * getRootStackFrame() const
Definition ExprEngine.h:268
ProgramStateManager & getStateManager()
Definition ExprEngine.h:476
void processCFGElement(const CFGElement E, ExplodedNode *Pred, unsigned StmtIdx)
processCFGElement - Called by CoreEngine.
void processBranch(const Stmt *Condition, ExplodedNode *Pred, ExplodedNodeSet &Dst, const CFGBlock *DstT, const CFGBlock *DstF, std::optional< unsigned > IterationsCompletedInLoop)
ProcessBranch - Called by CoreEngine.
void VisitArraySubscriptExpr(const ArraySubscriptExpr *Ex, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitArraySubscriptExpr - Transfer function for array accesses.
void VisitCommonDeclRefExpr(const Expr *DR, const NamedDecl *D, ExplodedNode *Pred, ExplodedNodeSet &Dst)
Transfer function logic for DeclRefExprs and BlockDeclRefExprs.
void ProcessInitializer(const CFGInitializer I, ExplodedNode *Pred)
void VisitObjCMessage(const ObjCMessageExpr *ME, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void ProcessTemporaryDtor(const CFGTemporaryDtor D, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void removeDead(ExplodedNode *Node, ExplodedNodeSet &Out, const Stmt *ReferenceStmt, const StackFrame *SF, const Stmt *DiagnosticStmt=nullptr, ProgramPoint::Kind K=ProgramPoint::PreStmtPurgeDeadSymbolsKind)
Run the analyzer's garbage collection - remove dead symbols and bindings from the state.
void VisitGuardedExpr(const Expr *Ex, const Expr *L, const Expr *R, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitGuardedExpr - Transfer function logic for ?, __builtin_choose.
void runCheckersForBlockEntrance(const BlockEntrance &Entrance, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitCast(const CastExpr *CastE, const Expr *Ex, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitCast - Transfer function logic for all casts (implicit and explicit).
BasicValueFactory & getBasicVals()
Definition ExprEngine.h:492
void VisitLogicalExpr(const BinaryOperator *B, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitLogicalExpr - Transfer function logic for '&&', '||'.
void processEndOfFunction(ExplodedNode *Pred, const ReturnStmt *RS=nullptr)
Called by CoreEngine.
void VisitCXXDestructor(QualType ObjectType, const MemRegion *Dest, const Stmt *S, bool IsBaseDtor, ExplodedNode *Pred, ExplodedNodeSet &Dst, EvalCallOptions &Options)
void removeDeadOnEndOfFunction(ExplodedNode *Pred, ExplodedNodeSet &Dst)
Remove dead bindings/symbols before exiting a function.
void evalEagerlyAssumeBifurcation(ExplodedNodeSet &Dst, ExplodedNodeSet &Src, const Expr *Ex)
evalEagerlyAssumeBifurcation - Given the nodes in 'Src', eagerly assume concrete boolean values for '...
void VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S, ExplodedNode *Pred, ExplodedNodeSet &Dst)
Transfer function logic for ObjCAtSynchronizedStmts.
void VisitReturnStmt(const ReturnStmt *R, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitReturnStmt - Transfer function logic for return statements.
SVal evalBinOp(ProgramStateRef ST, BinaryOperator::Opcode Op, SVal LHS, SVal RHS, QualType T)
Definition ExprEngine.h:680
void VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitLambdaExpr - Transfer function logic for LambdaExprs.
void ProcessImplicitDtor(const CFGImplicitDtor D, ExplodedNode *Pred)
void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitObjCForCollectionStmt - Transfer function logic for ObjCForCollectionStmt.
void VisitUnaryOperator(const UnaryOperator *B, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitUnaryOperator - Transfer function logic for unary operators.
void VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr *DR, ExplodedNode *Pred, ExplodedNodeSet &Dst)
Transfer function logic for computing the lvalue of an Objective-C ivar.
void VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitDeclStmt - Transfer function logic for DeclStmts.
void VisitMSAsmStmt(const MSAsmStmt *A, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitMSAsmStmt - Transfer function logic for MS inline asm.
void processStaticInitializer(const DeclStmt *DS, ExplodedNode *Pred, ExplodedNodeSet &Dst, const CFGBlock *DstT, const CFGBlock *DstF)
Called by CoreEngine.
static std::optional< unsigned > getIndexOfElementToConstruct(ProgramStateRef State, const CXXConstructExpr *E, const StackFrame *SF)
Retrieves which element is being constructed in a non-POD type array.
std::string DumpGraph(bool trim=false, StringRef Filename="")
Dump graph to the specified filename.
ProgramStateRef processRegionChanges(ProgramStateRef state, const InvalidatedSymbols *invalidated, ArrayRef< const MemRegion * > ExplicitRegions, ArrayRef< const MemRegion * > Regions, const StackFrame *SF, const CallEvent *Call)
processRegionChanges - Called by ProgramStateManager whenever a change is made to the store.
InliningModes
The modes of inlining, which override the default analysis-wide settings.
Definition ExprEngine.h:125
void printJson(raw_ostream &Out, ProgramStateRef State, const StackFrame *SF, const char *NL, unsigned int Space, bool IsDot) const
printJson - Called by ProgramStateManager to print checker-specific data.
void ProcessLifetimeEnd(const Stmt *S, const VarDecl *D, ExplodedNode *Pred)
static std::optional< unsigned > getPendingInitLoop(ProgramStateRef State, const CXXConstructExpr *E, const StackFrame *SF)
Retrieves the size of the array in the pending ArrayInitLoopExpr.
ProgramStateRef processAssume(ProgramStateRef state, SVal cond, bool assumption)
evalAssume - Callback function invoked by the ConstraintManager when making assumptions about state v...
AnalysisDeclContextManager & getAnalysisDeclContextManager()
Definition ExprEngine.h:219
static ProgramStateRef removeIterationState(ProgramStateRef State, const ObjCForCollectionStmt *O, const StackFrame *SF)
void VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitBlockExpr - Transfer function logic for BlockExprs.
void ProcessBaseDtor(const CFGBaseDtor D, ExplodedNode *Pred, ExplodedNodeSet &Dst)
static std::pair< const ProgramPointTag *, const ProgramPointTag * > getEagerlyAssumeBifurcationTags()
void VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitCall - Transfer function for function calls.
void processCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE, ExplodedNode *Pred, ExplodedNodeSet &Dst, const CFGBlock *DstT, const CFGBlock *DstF)
Called by CoreEngine.
ProgramStateRef processRegionChange(ProgramStateRef state, const MemRegion *MR, const StackFrame *SF)
Definition ExprEngine.h:466
ASTContext & getContext() const
getContext - Return the ASTContext associated with this analysis.
Definition ExprEngine.h:214
StoreManager & getStoreManager()
Definition ExprEngine.h:479
void VisitCXXNewAllocatorCall(const CXXNewExpr *CNE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME, ExplodedNode *Pred, ExplodedNodeSet &Dst)
Create a C++ temporary object for an rvalue.
void VisitGCCAsmStmt(const GCCAsmStmt *A, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitGCCAsmStmt - Transfer function logic for inline asm.
BugReporter & getBugReporter()
Definition ExprEngine.h:230
void ProcessStmt(const Stmt *S, ExplodedNode *Pred)
ConstCFGElementRef getCFGElementRef() const
Definition ExprEngine.h:290
ExprEngine(cross_tu::CrossTranslationUnitContext &CTU, AnalysisManager &mgr, SetOfConstDecls *VisitedCalleesIn, FunctionSummariesTy *FS, InliningModes HowToInlineIn)
void ViewGraph(bool trim=false)
Visualize the ExplodedGraph created by executing the simulation.
ProgramStateRef notifyCheckersOfPointerEscape(ProgramStateRef State, const InvalidatedSymbols *Invalidated, ArrayRef< const MemRegion * > ExplicitRegions, const CallEvent *Call, RegionAndSymbolInvalidationTraits &ITraits)
Call PointerEscape callback when a value escapes as a result of region invalidation.
static const ProgramPointTag * cleanupNodeTag()
A tag to track convenience transitions, which can be removed at cleanup.
static ProgramStateRef setWhetherHasMoreIteration(ProgramStateRef State, const ObjCForCollectionStmt *O, const StackFrame *SF, bool HasMoreIteraton)
Note whether this loop has any more iterations to model. These methods.
static std::optional< unsigned > getPendingArrayDestruction(ProgramStateRef State, const StackFrame *SF)
Retrieves which element is being destructed in a non-POD type array.
ProgramStateRef processPointerEscapedOnBind(ProgramStateRef State, ArrayRef< std::pair< SVal, SVal > > LocAndVals, const StackFrame *SF, PointerEscapeKind Kind, const CallEvent *Call)
Call PointerEscape callback when a value escapes as a result of bind.
void ConstructInitList(const Expr *Source, ArrayRef< Expr * > Args, bool IsTransparent, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitUnaryExprOrTypeTraitExpr - Transfer function for sizeof.
ProgramStateRef escapeValues(ProgramStateRef State, ArrayRef< SVal > Vs, PointerEscapeKind K, const CallEvent *Call=nullptr) const
A simple wrapper when you only need to notify checkers of pointer-escape of some values.
void ProcessLoopExit(const Stmt *S, ExplodedNode *Pred)
void processEndWorklist()
Called by CoreEngine when the analysis worklist has terminated.
CheckerManager & getCheckerManager() const
Definition ExprEngine.h:223
static std::optional< SVal > getObjectUnderConstruction(ProgramStateRef State, const ConstructionContextItem &Item, const StackFrame *SF)
By looking at a certain item that may be potentially part of an object's ConstructionContext,...
SymbolManager & getSymbolManager()
Definition ExprEngine.h:496
void processBeginOfFunction(ExplodedNode *Pred, ExplodedNodeSet &Dst, const BlockEdge &L)
Called by CoreEngine.
void VisitAtomicExpr(const AtomicExpr *E, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitAtomicExpr - Transfer function for builtin atomic expressions.
MemRegionManager & getRegionManager()
Definition ExprEngine.h:498
void ProcessMemberDtor(const CFGMemberDtor D, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitMemberExpr(const MemberExpr *M, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitMemberExpr - Transfer function for member expressions.
void processSwitch(const SwitchStmt *Switch, ExplodedNode *Pred, ExplodedNodeSet &Dst)
ProcessSwitch - Called by CoreEngine.
void VisitCXXConstructExpr(const CXXConstructExpr *E, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E, ExplodedNode *Pred, ExplodedNodeSet &Dst)
static bool hasMoreIteration(ProgramStateRef State, const ObjCForCollectionStmt *O, const StackFrame *SF)
bool didEagerlyAssumeBifurcateAt(ProgramStateRef State, const Expr *Ex) const
ConstraintManager & getConstraintManager()
Definition ExprEngine.h:484
ProgramStateRef getInitialState(const StackFrame *InitSF)
getInitialState - Return the initial state used for the root vertex in the ExplodedGraph.
void ProcessAutomaticObjDtor(const CFGAutomaticObjDtor D, ExplodedNode *Pred, ExplodedNodeSet &Dst)
unsigned getNumVisitedCurrent() const
Definition ExprEngine.h:299
void VisitOffsetOfExpr(const OffsetOfExpr *Ex, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitOffsetOfExpr - Transfer function for offsetof.
void evalLoad(ExplodedNodeSet &Dst, const Expr *NodeEx, const Expr *BoundExpr, ExplodedNode *Pred, ProgramStateRef St, SVal location, const ProgramPointTag *tag=nullptr, QualType LoadTy=QualType())
Simulate a read of the result of Ex.
void Visit(const Stmt *S, ExplodedNode *Pred, ExplodedNodeSet &Dst)
Visit - Transfer function logic for all statements.
AnalysisManager & getAnalysisManager()
Definition ExprEngine.h:216
ExplodedGraph & getGraph()
Definition ExprEngine.h:325
void ProcessDeleteDtor(const CFGDeleteDtor D, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitCXXCatchStmt(const CXXCatchStmt *CS, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitCompoundLiteralExpr - Transfer function logic for compound literals.
SValBuilder & getSValBuilder()
Definition ExprEngine.h:227
void VisitArrayInitLoopExpr(const ArrayInitLoopExpr *Ex, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitArrayInitLoopExpr - Transfer function for array init loop.
void evalStore(ExplodedNodeSet &Dst, const Expr *AssignE, const Expr *StoreE, ExplodedNode *Pred, ProgramStateRef St, SVal TargetLV, SVal Val, const ProgramPointTag *tag=nullptr)
evalStore - Handle the semantics of a store via an assignment.
void processCFGBlockEntrance(const BlockEdge &L, const BlockEntrance &BE, NodeBuilder &Builder, ExplodedNode *Pred)
Called by CoreEngine when processing the entrance of a CFGBlock.
void VisitAttributedStmt(const AttributedStmt *A, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitAttributedStmt - Transfer function logic for AttributedStmt.
void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *BTE, ExplodedNodeSet &PreVisit, ExplodedNodeSet &Dst)
const StackFrame * getCurrStackFrame() const
Get the 'current' stack frame corresponding to the current work item (elementary analysis step handle...
Definition ExprEngine.h:280
const CFGBlock * getCurrBlock() const
Get the 'current' CFGBlock corresponding to the current work item (elementary analysis step handled b...
Definition ExprEngine.h:286
void processIndirectGoto(ExplodedNodeSet &Dst, const Expr *Tgt, const CFGBlock *Dispatch, ExplodedNode *Pred)
processIndirectGoto - Called by CoreEngine.
void ProcessNewAllocator(const CXXNewExpr *NE, ExplodedNode *Pred)
static bool isLocType(QualType T)
Definition SVals.h:262
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion * getMemorySpace(ProgramStateRef State) const
Returns the most specific memory space for this memory region in the given ProgramStateRef.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
MemSpaceRegion - A memory region that represents a "memory space"; for example, the set of global var...
Definition MemRegion.h:235
This is the simplest builder which generates nodes in the ExplodedGraph.
Definition CoreEngine.h:265
void takeNodes(const ExplodedNodeSet &S)
Definition CoreEngine.h:330
ExplodedNode * generateNode(const ProgramPoint &PP, ProgramStateRef State, ExplodedNode *Pred, bool MarkAsSink=false)
Generates a node in the ExplodedGraph.
void addNodes(const ExplodedNodeSet &S)
Definition CoreEngine.h:336
While alive, includes the current analysis stack in a crash trace.
Information about invalidation for a particular region/symbol.
Definition MemRegion.h:1656
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
loc::MemRegionVal getCXXThis(const CXXMethodDecl *D, const StackFrame *SF)
Return a memory region for the 'this' object reference.
DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, ConstCFGElementRef elem, const StackFrame *SF, unsigned count)
Create a new symbol with a unique 'name'.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:56
bool isUndef() const
Definition SVals.h:107
bool isUnknownOrUndef() const
Definition SVals.h:109
bool isConstant() const
Definition SVals.cpp:245
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
Definition SVals.h:87
const MemRegion * getAsRegion() const
Definition SVals.cpp:119
bool isValid() const
Definition SVals.h:111
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition SVals.h:83
bool isUnknown() const
Definition SVals.h:105
SVal evalDerivedToBase(SVal Derived, const CastExpr *Cast)
Evaluates a chain of derived-to-base casts through the path specified in Cast.
Definition Store.cpp:254
virtual SVal getLValueField(const FieldDecl *D, SVal Base)
Definition Store.h:153
SubRegion - A region that subsets another larger region.
Definition MemRegion.h:473
A class responsible for cleaning up unused symbols.
void markLive(SymbolRef sym)
Unconditionally marks a symbol as live.
SymbolicRegion - A special, "non-concrete" region.
Definition MemRegion.h:806
Represents symbolic expression that isn't a location.
Definition SVals.h:279
Definition ARM.cpp:1102
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
PointerEscapeKind
Describes the different reasons a pointer escapes during analysis.
@ PSK_DirectEscapeOnCall
The pointer has been passed to a function call directly.
@ PSK_EscapeOnBind
A pointer escapes due to binding its value to a location that the analyzer cannot track.
@ PSK_IndirectEscapeOnCall
The pointer has been passed to a function indirectly.
@ PSK_EscapeOther
The reason for pointer escape is unknown.
DefinedOrUnknownSVal getDynamicElementCount(ProgramStateRef State, const MemRegion *MR, SValBuilder &SVB, QualType Ty)
llvm::DenseSet< const Decl * > SetOfConstDecls
llvm::DenseSet< SymbolRef > InvalidatedSymbols
Definition Store.h:50
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Definition SymExpr.h:133
ProgramStateRef processLoopEnd(const Stmt *LoopStmt, ProgramStateRef State)
Updates the given ProgramState.
bool isUnrolledState(ProgramStateRef State)
Returns if the given State indicates that is inside a completely unrolled loop.
ProgramStateRef getWidenedLoopState(ProgramStateRef PrevState, const StackFrame *SF, unsigned BlockCount, ConstCFGElementRef Elem)
Get the states that result from widening the loop.
void markAllDynamicExtentLive(ProgramStateRef State, SymbolReaper &SymReaper)
ProgramStateRef updateLoopStack(const Stmt *LoopStmt, ASTContext &ASTCtx, ExplodedNode *Pred, unsigned maxVisitOnPath)
Updates the stack of loops contained by the ProgramState.
bool LE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1513
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:207
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
Definition JsonSupport.h:21
StorageDuration
The storage duration for an object (per C++ [basic.stc]).
Definition Specifiers.h:340
@ SD_Thread
Thread storage duration.
Definition Specifiers.h:343
@ SD_Static
Static storage duration.
Definition Specifiers.h:344
@ SD_FullExpression
Full-expression storage duration (for temporaries).
Definition Specifiers.h:341
@ Result
The result type of a method or function.
Definition TypeBase.h:905
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5981
Expr * extractElementInitializerFromNestedAILE(const ArrayInitLoopExpr *AILE)
Definition CFG.cpp:1461
@ CXXThis
Parameter for C++ 'this' argument.
Definition Decl.h:1751
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
Describes how types, statements, expressions, and declarations should be printed.
Hints for figuring out if a call should be inlined during evalCall().
Definition ExprEngine.h:93
bool IsTemporaryCtorOrDtor
This call is a constructor or a destructor of a temporary value.
Definition ExprEngine.h:103
bool IsArrayCtorOrDtor
This call is a constructor or a destructor for a single element within an array, a part of array cons...
Definition ExprEngine.h:100
Traits for storing the call processing policy inside GDM.
static std::string getNodeLabel(const ExplodedNode *N, ExplodedGraph *G)
static bool nodeHasBugReport(const ExplodedNode *N)
static bool traverseHiddenNodes(const ExplodedNode *N, llvm::function_ref< void(const ExplodedNode *)> PreCallback, llvm::function_ref< void(const ExplodedNode *)> PostCallback, llvm::function_ref< bool(const ExplodedNode *)> Stop)
PreCallback: callback before break.
static bool isNodeHidden(const ExplodedNode *N, const ExplodedGraph *G)