clang 23.0.0git
BugReporterVisitors.cpp
Go to the documentation of this file.
1//===- BugReporterVisitors.cpp - Helpers for reporting bugs ---------------===//
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 set of BugReporter "visitors" which can be used to
10// enhance the diagnostics reported for a bug.
11//
12//===----------------------------------------------------------------------===//
13
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclBase.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/ExprCXX.h"
21#include "clang/AST/ExprObjC.h"
22#include "clang/AST/Stmt.h"
23#include "clang/AST/Type.h"
27#include "clang/Analysis/CFG.h"
32#include "clang/Basic/LLVM.h"
35#include "clang/Lex/Lexer.h"
47#include "llvm/ADT/ArrayRef.h"
48#include "llvm/ADT/SmallPtrSet.h"
49#include "llvm/ADT/SmallString.h"
50#include "llvm/ADT/StringExtras.h"
51#include "llvm/ADT/StringRef.h"
52#include "llvm/Support/Casting.h"
53#include "llvm/Support/ErrorHandling.h"
54#include "llvm/Support/raw_ostream.h"
55#include <cassert>
56#include <memory>
57#include <optional>
58#include <stack>
59#include <string>
60#include <utility>
61
62using namespace clang;
63using namespace ento;
64using namespace bugreporter;
65
66//===----------------------------------------------------------------------===//
67// Utility functions.
68//===----------------------------------------------------------------------===//
69
71 if (B->isAdditiveOp() && B->getType()->isPointerType()) {
72 if (B->getLHS()->getType()->isPointerType()) {
73 return B->getLHS();
74 } else if (B->getRHS()->getType()->isPointerType()) {
75 return B->getRHS();
76 }
77 }
78 return nullptr;
79}
80
81/// \return A subexpression of @c Ex which represents the
82/// expression-of-interest.
83static const Expr *peelOffOuterExpr(const Expr *Ex, const ExplodedNode *N);
84
85/// Given that expression S represents a pointer that would be dereferenced,
86/// try to find a sub-expression from which the pointer came from.
87/// This is used for tracking down origins of a null or undefined value:
88/// "this is null because that is null because that is null" etc.
89/// We wipe away field and element offsets because they merely add offsets.
90/// We also wipe away all casts except lvalue-to-rvalue casts, because the
91/// latter represent an actual pointer dereference; however, we remove
92/// the final lvalue-to-rvalue cast before returning from this function
93/// because it demonstrates more clearly from where the pointer rvalue was
94/// loaded. Examples:
95/// x->y.z ==> x (lvalue)
96/// foo()->y.z ==> foo() (rvalue)
98 const auto *E = dyn_cast<Expr>(S);
99 if (!E)
100 return nullptr;
101
102 while (true) {
103 if (const auto *CE = dyn_cast<CastExpr>(E)) {
104 if (CE->getCastKind() == CK_LValueToRValue) {
105 // This cast represents the load we're looking for.
106 break;
107 }
108 E = CE->getSubExpr();
109 } else if (const auto *B = dyn_cast<BinaryOperator>(E)) {
110 // Pointer arithmetic: '*(x + 2)' -> 'x') etc.
111 if (const Expr *Inner = peelOffPointerArithmetic(B)) {
112 E = Inner;
113 } else if (B->isAssignmentOp()) {
114 // Follow LHS of assignments: '*p = 404' -> 'p'.
115 E = B->getLHS();
116 } else {
117 // Probably more arithmetic can be pattern-matched here,
118 // but for now give up.
119 break;
120 }
121 } else if (const auto *U = dyn_cast<UnaryOperator>(E)) {
122 if (U->getOpcode() == UO_Deref || U->getOpcode() == UO_AddrOf ||
123 (U->isIncrementDecrementOp() && U->getType()->isPointerType())) {
124 // Operators '*' and '&' don't actually mean anything.
125 // We look at casts instead.
126 E = U->getSubExpr();
127 } else {
128 // Probably more arithmetic can be pattern-matched here,
129 // but for now give up.
130 break;
131 }
132 }
133 // Pattern match for a few useful cases: a[0], p->f, *p etc.
134 else if (const auto *ME = dyn_cast<MemberExpr>(E)) {
135 // This handles the case when the dereferencing of a member reference
136 // happens. This is needed, because the AST for dereferencing a
137 // member reference looks like the following:
138 // |-MemberExpr
139 // `-DeclRefExpr
140 // Without this special case the notes would refer to the whole object
141 // (struct, class or union variable) instead of just the relevant member.
142
143 if (ME->getMemberDecl()->getType()->isReferenceType())
144 break;
145 E = ME->getBase();
146 } else if (const auto *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
147 E = IvarRef->getBase();
148 } else if (const auto *AE = dyn_cast<ArraySubscriptExpr>(E)) {
149 E = AE->getBase();
150 } else if (const auto *PE = dyn_cast<ParenExpr>(E)) {
151 E = PE->getSubExpr();
152 } else if (const auto *FE = dyn_cast<FullExpr>(E)) {
153 E = FE->getSubExpr();
154 } else {
155 // Other arbitrary stuff.
156 break;
157 }
158 }
159
160 // Special case: remove the final lvalue-to-rvalue cast, but do not recurse
161 // deeper into the sub-expression. This way we return the lvalue from which
162 // our pointer rvalue was loaded.
163 if (const auto *CE = dyn_cast<ImplicitCastExpr>(E))
164 if (CE->getCastKind() == CK_LValueToRValue)
165 E = CE->getSubExpr();
166
167 return E;
168}
169
170static const VarDecl *getVarDeclForExpression(const Expr *E) {
171 if (const auto *DR = dyn_cast<DeclRefExpr>(E))
172 return dyn_cast<VarDecl>(DR->getDecl());
173 return nullptr;
174}
175
176static const MemRegion *
178 bool LookingForReference = true) {
179 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
180 // This handles null references from FieldRegions, for example:
181 // struct Wrapper { int &ref; };
182 // Wrapper w = { *(int *)0 };
183 // w.ref = 1;
184 const Expr *Base = ME->getBase();
186 if (!VD)
187 return nullptr;
188
189 const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
190 if (!FD)
191 return nullptr;
192
193 if (FD->getType()->isReferenceType()) {
194 SVal StructSVal = N->getState()->getLValue(VD, N->getLocationContext());
195 return N->getState()->getLValue(FD, StructSVal).getAsRegion();
196 }
197 return nullptr;
198 }
199
200 const VarDecl *VD = getVarDeclForExpression(E);
201 if (!VD)
202 return nullptr;
203 if (LookingForReference && !VD->getType()->isReferenceType())
204 return nullptr;
205 return N->getState()->getLValue(VD, N->getLocationContext()).getAsRegion();
206}
207
208/// Comparing internal representations of symbolic values (via
209/// SVal::operator==()) is a valid way to check if the value was updated,
210/// unless it's a LazyCompoundVal that may have a different internal
211/// representation every time it is loaded from the state. In this function we
212/// do an approximate comparison for lazy compound values, checking that they
213/// are the immediate snapshots of the tracked region's bindings within the
214/// node's respective states but not really checking that these snapshots
215/// actually contain the same set of bindings.
216static bool hasVisibleUpdate(const ExplodedNode *LeftNode, SVal LeftVal,
217 const ExplodedNode *RightNode, SVal RightVal) {
218 if (LeftVal == RightVal)
219 return true;
220
221 const auto LLCV = LeftVal.getAs<nonloc::LazyCompoundVal>();
222 if (!LLCV)
223 return false;
224
225 const auto RLCV = RightVal.getAs<nonloc::LazyCompoundVal>();
226 if (!RLCV)
227 return false;
228
229 return LLCV->getRegion() == RLCV->getRegion() &&
230 LLCV->getStore() == LeftNode->getState()->getStore() &&
231 RLCV->getStore() == RightNode->getState()->getStore();
232}
233
234static std::optional<SVal> getSValForVar(const Expr *CondVarExpr,
235 const ExplodedNode *N) {
236 ProgramStateRef State = N->getState();
237 const LocationContext *LCtx = N->getLocationContext();
238
239 assert(CondVarExpr);
240 CondVarExpr = CondVarExpr->IgnoreImpCasts();
241
242 // The declaration of the value may rely on a pointer so take its l-value.
243 // FIXME: As seen in VisitCommonDeclRefExpr, sometimes DeclRefExpr may
244 // evaluate to a FieldRegion when it refers to a declaration of a lambda
245 // capture variable. We most likely need to duplicate that logic here.
246 if (const auto *DRE = dyn_cast<DeclRefExpr>(CondVarExpr))
247 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
248 return State->getSVal(State->getLValue(VD, LCtx));
249
250 if (const auto *ME = dyn_cast<MemberExpr>(CondVarExpr))
251 if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
252 if (auto FieldL = State->getSVal(ME, LCtx).getAs<Loc>())
253 return State->getRawSVal(*FieldL, FD->getType());
254
255 return std::nullopt;
256}
257
258static std::optional<const llvm::APSInt *>
259getConcreteIntegerValue(const Expr *CondVarExpr, const ExplodedNode *N) {
260
261 if (std::optional<SVal> V = getSValForVar(CondVarExpr, N))
262 if (auto CI = V->getAs<nonloc::ConcreteInt>())
263 return CI->getValue().get();
264 return std::nullopt;
265}
266
267static bool isVarAnInterestingCondition(const Expr *CondVarExpr,
268 const ExplodedNode *N,
269 const PathSensitiveBugReport *B) {
270 // Even if this condition is marked as interesting, it isn't *that*
271 // interesting if it didn't happen in a nested stackframe, the user could just
272 // follow the arrows.
274 return false;
275
276 if (std::optional<SVal> V = getSValForVar(CondVarExpr, N))
277 if (std::optional<bugreporter::TrackingKind> K =
279 return *K == bugreporter::TrackingKind::Condition;
280
281 return false;
282}
283
284static bool isInterestingExpr(const Expr *E, const ExplodedNode *N,
285 const PathSensitiveBugReport *B) {
286 if (std::optional<SVal> V = getSValForVar(E, N))
287 return B->getInterestingnessKind(*V).has_value();
288 return false;
289}
290
291/// \return name of the macro inside the location \p Loc.
293 BugReporterContext &BRC) {
295 Loc,
296 BRC.getSourceManager(),
297 BRC.getASTContext().getLangOpts());
298}
299
300/// \return Whether given spelling location corresponds to an expansion
301/// of a function-like macro.
303 const SourceManager &SM) {
304 if (!Loc.isMacroID())
305 return false;
306 while (SM.isMacroArgExpansion(Loc))
307 Loc = SM.getImmediateExpansionRange(Loc).getBegin();
308 FileIDAndOffset TLInfo = SM.getDecomposedLoc(Loc);
309 SrcMgr::SLocEntry SE = SM.getSLocEntry(TLInfo.first);
310 const SrcMgr::ExpansionInfo &EInfo = SE.getExpansion();
311 return EInfo.isFunctionMacroExpansion();
312}
313
314/// \return Whether \c RegionOfInterest was modified at \p N,
315/// where \p ValueAfter is \c RegionOfInterest's value at the end of the
316/// stack frame.
317static bool wasRegionOfInterestModifiedAt(const SubRegion *RegionOfInterest,
318 const ExplodedNode *N,
319 SVal ValueAfter) {
320 ProgramStateRef State = N->getState();
321 ProgramStateManager &Mgr = N->getState()->getStateManager();
322
324 !N->getLocationAs<PostStmt>())
325 return false;
326
327 // Writing into region of interest.
328 if (auto PS = N->getLocationAs<PostStmt>())
329 if (auto *BO = PS->getStmtAs<BinaryOperator>())
330 if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(
331 N->getSVal(BO->getLHS()).getAsRegion()))
332 return true;
333
334 // SVal after the state is possibly different.
335 SVal ValueAtN = N->getState()->getSVal(RegionOfInterest);
336 if (!Mgr.getSValBuilder()
337 .areEqual(State, ValueAtN, ValueAfter)
339 (!ValueAtN.isUndef() || !ValueAfter.isUndef()))
340 return true;
341
342 return false;
343}
344
345//===----------------------------------------------------------------------===//
346// Implementation of BugReporterVisitor.
347//===----------------------------------------------------------------------===//
348
354
358
361 const ExplodedNode *EndPathNode,
362 const PathSensitiveBugReport &BR) {
364 const auto &Ranges = BR.getRanges();
365
366 // Only add the statement itself as a range if we didn't specify any
367 // special ranges for this report.
368 auto P = std::make_shared<PathDiagnosticEventPiece>(
369 L, BR.getDescription(), Ranges.begin() == Ranges.end());
370 for (SourceRange Range : Ranges)
371 P->addRange(Range);
372
373 return P;
374}
375
376//===----------------------------------------------------------------------===//
377// Implementation of NoStateChangeFuncVisitor.
378//===----------------------------------------------------------------------===//
379
380bool NoStateChangeFuncVisitor::isModifiedInFrame(const ExplodedNode *N) {
381 const LocationContext *Ctx = N->getLocationContext();
382 const StackFrame *SF = Ctx->getStackFrame();
383 if (!FramesModifyingCalculated.count(SF))
384 findModifyingFrames(N);
385 return FramesModifying.count(SF);
386}
387
388void NoStateChangeFuncVisitor::markFrameAsModifying(const StackFrame *SF) {
389 while (!SF->inTopFrame()) {
390 auto p = FramesModifying.insert(SF);
391 if (!p.second)
392 break; // Frame and all its parents already inserted.
393
394 SF = SF->getParent()->getStackFrame();
395 }
396}
397
399 assert(N->getLocationAs<CallEnter>());
400 // The stackframe of the callee is only found in the nodes succeeding
401 // the CallEnter node. CallEnter's stack frame refers to the caller.
402 const StackFrame *OrigSF = N->getFirstSucc()->getStackFrame();
403
404 // Similarly, the nodes preceding CallExitEnd refer to the callee's stack
405 // frame.
406 auto IsMatchingCallExitEnd = [OrigSF](const ExplodedNode *N) {
407 return N->getLocationAs<CallExitEnd>() &&
408 OrigSF == N->getFirstPred()->getStackFrame();
409 };
410 while (N && !IsMatchingCallExitEnd(N)) {
411 assert(N->succ_size() <= 1 &&
412 "This function is to be used on the trimmed ExplodedGraph!");
413 N = N->getFirstSucc();
414 }
415 return N;
416}
417
418void NoStateChangeFuncVisitor::findModifyingFrames(
419 const ExplodedNode *const CallExitBeginN) {
420
421 assert(CallExitBeginN->getLocationAs<CallExitBegin>());
422
423 const StackFrame *const OriginalSF =
424 CallExitBeginN->getLocationContext()->getStackFrame();
425
426 const ExplodedNode *CurrCallExitBeginN = CallExitBeginN;
427 const StackFrame *CurrentSF = OriginalSF;
428
429 for (const ExplodedNode *CurrN = CallExitBeginN; CurrN;
430 CurrN = CurrN->getFirstPred()) {
431 // Found a new inlined call.
432 if (CurrN->getLocationAs<CallExitBegin>()) {
433 CurrCallExitBeginN = CurrN;
434 CurrentSF = CurrN->getStackFrame();
435 FramesModifyingCalculated.insert(CurrentSF);
436 // We won't see a change in between two identical exploded nodes: skip.
437 continue;
438 }
439
440 if (auto CE = CurrN->getLocationAs<CallEnter>()) {
441 if (const ExplodedNode *CallExitEndN = getMatchingCallExitEnd(CurrN))
442 if (wasModifiedInFunction(CurrN, CallExitEndN))
443 markFrameAsModifying(CurrentSF);
444
445 // We exited this inlined call, lets actualize the stack frame.
446 CurrentSF = CurrN->getStackFrame();
447
448 // Stop calculating at the current function, but always regard it as
449 // modifying, so we can avoid notes like this:
450 // void f(Foo &F) {
451 // F.field = 0; // note: 0 assigned to 'F.field'
452 // // note: returning without writing to 'F.field'
453 // }
454 if (CE->getCalleeContext() == OriginalSF) {
455 markFrameAsModifying(CurrentSF);
456 break;
457 }
458 }
459
460 if (wasModifiedBeforeCallExit(CurrN, CurrCallExitBeginN))
461 markFrameAsModifying(CurrentSF);
462 }
463}
464
467
468 const LocationContext *Ctx = N->getLocationContext();
469 const StackFrame *SF = Ctx->getStackFrame();
470 ProgramStateRef State = N->getState();
471 auto CallExitLoc = N->getLocationAs<CallExitBegin>();
472
473 // No diagnostic if region was modified inside the frame.
474 if (!CallExitLoc || isModifiedInFrame(N))
475 return nullptr;
476
479
480 // Optimistically suppress uninitialized value bugs that result
481 // from system headers having a chance to initialize the value
482 // but failing to do so. It's too unlikely a system header's fault.
483 // It's much more likely a situation in which the function has a failure
484 // mode that the user decided not to check. If we want to hunt such
485 // omitted checks, we should provide an explicit function-specific note
486 // describing the precondition under which the function isn't supposed to
487 // initialize its out-parameter, and additionally check that such
488 // precondition can actually be fulfilled on the current path.
489 if (Call->isInSystemHeader()) {
490 // We make an exception for system header functions that have no branches.
491 // Such functions unconditionally fail to initialize the variable.
492 // If they call other functions that have more paths within them,
493 // this suppression would still apply when we visit these inner functions.
494 // One common example of a standard function that doesn't ever initialize
495 // its out parameter is operator placement new; it's up to the follow-up
496 // constructor (if any) to initialize the memory.
497 if (!N->getStackFrame()->getCFG()->isLinear()) {
498 static int i = 0;
499 R.markInvalid(&i, nullptr);
500 }
501 return nullptr;
502 }
503
504 if (const auto *MC = dyn_cast<ObjCMethodCall>(Call)) {
505 // If we failed to construct a piece for self, we still want to check
506 // whether the entity of interest is in a parameter.
508 return Piece;
509 }
510
511 if (const auto *CCall = dyn_cast<CXXConstructorCall>(Call)) {
512 // Do not generate diagnostics for not modified parameters in
513 // constructors.
514 return maybeEmitNoteForCXXThis(R, *CCall, N);
515 }
516
517 return maybeEmitNoteForParameters(R, *Call, N);
518}
519
520/// \return Whether the method declaration \p Parent
521/// syntactically has a binary operation writing into the ivar \p Ivar.
522static bool potentiallyWritesIntoIvar(const Decl *Parent,
523 const ObjCIvarDecl *Ivar) {
524 using namespace ast_matchers;
525 const char *IvarBind = "Ivar";
526 if (!Parent || !Parent->hasBody())
527 return false;
528 StatementMatcher WriteIntoIvarM = binaryOperator(
529 hasOperatorName("="),
530 hasLHS(ignoringParenImpCasts(
531 objcIvarRefExpr(hasDeclaration(equalsNode(Ivar))).bind(IvarBind))));
532 StatementMatcher ParentM = stmt(hasDescendant(WriteIntoIvarM));
533 auto Matches = match(ParentM, *Parent->getBody(), Parent->getASTContext());
534 for (BoundNodes &Match : Matches) {
535 auto IvarRef = Match.getNodeAs<ObjCIvarRefExpr>(IvarBind);
536 if (IvarRef->isFreeIvar())
537 return true;
538
539 const Expr *Base = IvarRef->getBase();
540 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Base))
541 Base = ICE->getSubExpr();
542
543 if (const auto *DRE = dyn_cast<DeclRefExpr>(Base))
544 if (const auto *ID = dyn_cast<ImplicitParamDecl>(DRE->getDecl()))
545 if (ID->getParameterKind() == ImplicitParamKind::ObjCSelf)
546 return true;
547
548 return false;
549 }
550 return false;
551}
552
553/// Attempts to find the region of interest in a given CXX decl,
554/// by either following the base classes or fields.
555/// Dereferences fields up to a given recursion limit.
556/// Note that \p Vec is passed by value, leading to quadratic copying cost,
557/// but it's OK in practice since its length is limited to DEREFERENCE_LIMIT.
558/// \return A chain fields leading to the region of interest or std::nullopt.
559const std::optional<NoStoreFuncVisitor::RegionVector>
560NoStoreFuncVisitor::findRegionOfInterestInRecord(
561 const RecordDecl *RD, ProgramStateRef State, const MemRegion *R,
562 const NoStoreFuncVisitor::RegionVector &Vec /* = {} */,
563 int depth /* = 0 */) {
564
565 if (depth == DEREFERENCE_LIMIT) // Limit the recursion depth.
566 return std::nullopt;
567
568 if (const auto *RDX = dyn_cast<CXXRecordDecl>(RD))
569 if (!RDX->hasDefinition())
570 return std::nullopt;
571
572 // Recursively examine the base classes.
573 // Note that following base classes does not increase the recursion depth.
574 if (const auto *RDX = dyn_cast<CXXRecordDecl>(RD))
575 for (const auto &II : RDX->bases())
576 if (const RecordDecl *RRD = II.getType()->getAsRecordDecl())
577 if (std::optional<RegionVector> Out =
578 findRegionOfInterestInRecord(RRD, State, R, Vec, depth))
579 return Out;
580
581 for (const FieldDecl *I : RD->fields()) {
582 QualType FT = I->getType();
583 const FieldRegion *FR = MmrMgr.getFieldRegion(I, cast<SubRegion>(R));
584 const SVal V = State->getSVal(FR);
585 const MemRegion *VR = V.getAsRegion();
586
587 RegionVector VecF = Vec;
588 VecF.push_back(FR);
589
590 if (RegionOfInterest == VR)
591 return VecF;
592
593 if (const RecordDecl *RRD = FT->getAsRecordDecl())
594 if (auto Out =
595 findRegionOfInterestInRecord(RRD, State, FR, VecF, depth + 1))
596 return Out;
597
598 QualType PT = FT->getPointeeType();
599 if (PT.isNull() || PT->isVoidType() || !VR)
600 continue;
601
602 if (const RecordDecl *RRD = PT->getAsRecordDecl())
603 if (std::optional<RegionVector> Out =
604 findRegionOfInterestInRecord(RRD, State, VR, VecF, depth + 1))
605 return Out;
606 }
607
608 return std::nullopt;
609}
610
612NoStoreFuncVisitor::maybeEmitNoteForObjCSelf(PathSensitiveBugReport &R,
613 const ObjCMethodCall &Call,
614 const ExplodedNode *N) {
615 if (const auto *IvarR = dyn_cast<ObjCIvarRegion>(RegionOfInterest)) {
616 const MemRegion *SelfRegion = Call.getReceiverSVal().getAsRegion();
617 if (RegionOfInterest->isSubRegionOf(SelfRegion) &&
618 potentiallyWritesIntoIvar(Call.getRuntimeDefinition().getDecl(),
619 IvarR->getDecl()))
620 return maybeEmitNote(R, Call, N, {}, SelfRegion, "self",
621 /*FirstIsReferenceType=*/false, 1);
622 }
623 return nullptr;
624}
625
627NoStoreFuncVisitor::maybeEmitNoteForCXXThis(PathSensitiveBugReport &R,
629 const ExplodedNode *N) {
630 const MemRegion *ThisR = Call.getCXXThisVal().getAsRegion();
631 if (RegionOfInterest->isSubRegionOf(ThisR) && !Call.getDecl()->isImplicit())
632 return maybeEmitNote(R, Call, N, {}, ThisR, "this",
633 /*FirstIsReferenceType=*/false, 1);
634
635 // Do not generate diagnostics for not modified parameters in
636 // constructors.
637 return nullptr;
638}
639
640/// \return whether \p Ty points to a const type, or is a const reference.
641static bool isPointerToConst(QualType Ty) {
642 return !Ty->getPointeeType().isNull() &&
644}
645
646PathDiagnosticPieceRef NoStoreFuncVisitor::maybeEmitNoteForParameters(
647 PathSensitiveBugReport &R, const CallEvent &Call, const ExplodedNode *N) {
648 ArrayRef<ParmVarDecl *> Parameters = Call.parameters();
649 for (unsigned I = 0; I < Call.getNumArgs() && I < Parameters.size(); ++I) {
650 const ParmVarDecl *PVD = Parameters[I];
651 SVal V = Call.getArgSVal(I);
652 bool ParamIsReferenceType = PVD->getType()->isReferenceType();
653 std::string ParamName = PVD->getNameAsString();
654
655 unsigned IndirectionLevel = 1;
656 QualType T = PVD->getType();
657 while (const MemRegion *MR = V.getAsRegion()) {
658 if (RegionOfInterest->isSubRegionOf(MR) && !isPointerToConst(T))
659 return maybeEmitNote(R, Call, N, {}, MR, ParamName,
660 ParamIsReferenceType, IndirectionLevel);
661
662 QualType PT = T->getPointeeType();
663 if (PT.isNull() || PT->isVoidType())
664 break;
665
666 ProgramStateRef State = N->getState();
667
668 if (const RecordDecl *RD = PT->getAsRecordDecl())
669 if (std::optional<RegionVector> P =
670 findRegionOfInterestInRecord(RD, State, MR))
671 return maybeEmitNote(R, Call, N, *P, RegionOfInterest, ParamName,
672 ParamIsReferenceType, IndirectionLevel);
673
674 V = State->getSVal(MR, PT);
675 T = PT;
676 IndirectionLevel++;
677 }
678 }
679
680 return nullptr;
681}
682
683bool NoStoreFuncVisitor::wasModifiedBeforeCallExit(
684 const ExplodedNode *CurrN, const ExplodedNode *CallExitBeginN) {
685 return ::wasRegionOfInterestModifiedAt(
686 RegionOfInterest, CurrN,
687 CallExitBeginN->getState()->getSVal(RegionOfInterest));
688}
689
690static llvm::StringLiteral WillBeUsedForACondition =
691 ", which participates in a condition later";
692
693PathDiagnosticPieceRef NoStoreFuncVisitor::maybeEmitNote(
695 const RegionVector &FieldChain, const MemRegion *MatchedRegion,
696 StringRef FirstElement, bool FirstIsReferenceType,
697 unsigned IndirectionLevel) {
698
701
702 // For now this shouldn't trigger, but once it does (as we add more
703 // functions to the body farm), we'll need to decide if these reports
704 // are worth suppressing as well.
705 if (!L.hasValidLocation())
706 return nullptr;
707
708 SmallString<256> sbuf;
709 llvm::raw_svector_ostream os(sbuf);
710 os << "Returning without writing to '";
711
712 // Do not generate the note if failed to pretty-print.
713 if (!prettyPrintRegionName(FieldChain, MatchedRegion, FirstElement,
714 FirstIsReferenceType, IndirectionLevel, os))
715 return nullptr;
716
717 os << "'";
720 return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
721}
722
723bool NoStoreFuncVisitor::prettyPrintRegionName(const RegionVector &FieldChain,
724 const MemRegion *MatchedRegion,
725 StringRef FirstElement,
726 bool FirstIsReferenceType,
727 unsigned IndirectionLevel,
728 llvm::raw_svector_ostream &os) {
729
730 if (FirstIsReferenceType)
731 IndirectionLevel--;
732
733 RegionVector RegionSequence;
734
735 // Add the regions in the reverse order, then reverse the resulting array.
736 assert(RegionOfInterest->isSubRegionOf(MatchedRegion));
737 const MemRegion *R = RegionOfInterest;
738 while (R != MatchedRegion) {
739 RegionSequence.push_back(R);
740 R = cast<SubRegion>(R)->getSuperRegion();
741 }
742 std::reverse(RegionSequence.begin(), RegionSequence.end());
743 RegionSequence.append(FieldChain.begin(), FieldChain.end());
744
745 StringRef Sep;
746 for (const MemRegion *R : RegionSequence) {
747
748 // Just keep going up to the base region.
749 // Element regions may appear due to casts.
751 continue;
752
753 if (Sep.empty())
754 Sep = prettyPrintFirstElement(FirstElement,
755 /*MoreItemsExpected=*/true,
756 IndirectionLevel, os);
757
758 os << Sep;
759
760 // Can only reasonably pretty-print DeclRegions.
761 if (!isa<DeclRegion>(R))
762 return false;
763
764 const auto *DR = cast<DeclRegion>(R);
765 Sep = DR->getValueType()->isAnyPointerType() ? "->" : ".";
766 DR->getDecl()->getDeclName().print(os, PP);
767 }
768
769 if (Sep.empty())
770 prettyPrintFirstElement(FirstElement,
771 /*MoreItemsExpected=*/false, IndirectionLevel, os);
772 return true;
773}
774
775StringRef NoStoreFuncVisitor::prettyPrintFirstElement(
776 StringRef FirstElement, bool MoreItemsExpected, int IndirectionLevel,
777 llvm::raw_svector_ostream &os) {
778 StringRef Out = ".";
779
780 if (IndirectionLevel > 0 && MoreItemsExpected) {
781 IndirectionLevel--;
782 Out = "->";
783 }
784
785 if (IndirectionLevel > 0 && MoreItemsExpected)
786 os << "(";
787
788 for (int i = 0; i < IndirectionLevel; i++)
789 os << "*";
790 os << FirstElement;
791
792 if (IndirectionLevel > 0 && MoreItemsExpected)
793 os << ")";
794
795 return Out;
796}
797
798//===----------------------------------------------------------------------===//
799// Implementation of MacroNullReturnSuppressionVisitor.
800//===----------------------------------------------------------------------===//
801
802namespace {
803
804/// Suppress null-pointer-dereference bugs where dereferenced null was returned
805/// the macro.
806class MacroNullReturnSuppressionVisitor final : public BugReporterVisitor {
807 const SubRegion *RegionOfInterest;
808 const SVal ValueAtDereference;
809
810 // Do not invalidate the reports where the value was modified
811 // after it got assigned to from the macro.
812 bool WasModified = false;
813
814public:
815 MacroNullReturnSuppressionVisitor(const SubRegion *R, const SVal V)
816 : RegionOfInterest(R), ValueAtDereference(V) {}
817
818 PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
819 BugReporterContext &BRC,
820 PathSensitiveBugReport &BR) override {
821 if (WasModified)
822 return nullptr;
823
824 auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>();
825 if (!BugPoint)
826 return nullptr;
827
828 const SourceManager &SMgr = BRC.getSourceManager();
829 if (auto Loc = matchAssignment(N)) {
830 if (isFunctionMacroExpansion(*Loc, SMgr)) {
831 std::string MacroName = std::string(getMacroName(*Loc, BRC));
832 SourceLocation BugLoc = BugPoint->getStmt()->getBeginLoc();
833 if (!BugLoc.isMacroID() || getMacroName(BugLoc, BRC) != MacroName)
834 BR.markInvalid(getTag(), MacroName.c_str());
835 }
836 }
837
838 if (wasRegionOfInterestModifiedAt(RegionOfInterest, N, ValueAtDereference))
839 WasModified = true;
840
841 return nullptr;
842 }
843
844 static void addMacroVisitorIfNecessary(
845 const ExplodedNode *N, const MemRegion *R,
846 bool EnableNullFPSuppression, PathSensitiveBugReport &BR,
847 const SVal V) {
848 AnalyzerOptions &Options = N->getState()->getAnalysisManager().options;
849 if (EnableNullFPSuppression && Options.ShouldSuppressNullReturnPaths &&
850 isa<Loc>(V))
851 BR.addVisitor<MacroNullReturnSuppressionVisitor>(R->getAs<SubRegion>(),
852 V);
853 }
854
855 void* getTag() const {
856 static int Tag = 0;
857 return static_cast<void *>(&Tag);
858 }
859
860 void Profile(llvm::FoldingSetNodeID &ID) const override {
861 ID.AddPointer(getTag());
862 }
863
864private:
865 /// \return Source location of right hand side of an assignment
866 /// into \c RegionOfInterest, empty optional if none found.
867 std::optional<SourceLocation> matchAssignment(const ExplodedNode *N) {
868 const Stmt *S = N->getStmtForDiagnostics();
869 ProgramStateRef State = N->getState();
870 auto *LCtx = N->getLocationContext();
871 if (!S)
872 return std::nullopt;
873
874 if (const auto *DS = dyn_cast<DeclStmt>(S)) {
875 if (const auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl()))
876 if (const Expr *RHS = VD->getInit())
877 if (RegionOfInterest->isSubRegionOf(
878 State->getLValue(VD, LCtx).getAsRegion()))
879 return RHS->getBeginLoc();
880 } else if (const auto *BO = dyn_cast<BinaryOperator>(S)) {
881 const MemRegion *R = N->getSVal(BO->getLHS()).getAsRegion();
882 const Expr *RHS = BO->getRHS();
883 if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(R)) {
884 return RHS->getBeginLoc();
885 }
886 }
887 return std::nullopt;
888 }
889};
890
891} // end of anonymous namespace
892
893namespace {
894
895/// Emits an extra note at the return statement of an interesting stack frame.
896///
897/// The returned value is marked as an interesting value, and if it's null,
898/// adds a visitor to track where it became null.
899///
900/// This visitor is intended to be used when another visitor discovers that an
901/// interesting value comes from an inlined function call.
902class ReturnVisitor : public TrackingBugReporterVisitor {
903 const StackFrame *CalleeSF;
904 enum {
905 Initial,
906 MaybeUnsuppress,
907 Satisfied
908 } Mode = Initial;
909
910 bool EnableNullFPSuppression;
911 bool ShouldInvalidate = true;
912 AnalyzerOptions& Options;
914
915public:
916 ReturnVisitor(TrackerRef ParentTracker, const StackFrame *Frame,
917 bool Suppressed, AnalyzerOptions &Options,
919 : TrackingBugReporterVisitor(ParentTracker), CalleeSF(Frame),
920 EnableNullFPSuppression(Suppressed), Options(Options), TKind(TKind) {}
921
922 static void *getTag() {
923 static int Tag = 0;
924 return static_cast<void *>(&Tag);
925 }
926
927 void Profile(llvm::FoldingSetNodeID &ID) const override {
928 ID.AddPointer(ReturnVisitor::getTag());
929 ID.AddPointer(CalleeSF);
930 ID.AddBoolean(EnableNullFPSuppression);
931 }
932
933 PathDiagnosticPieceRef visitNodeInitial(const ExplodedNode *N,
934 BugReporterContext &BRC,
935 PathSensitiveBugReport &BR) {
936 // Only print a message at the interesting return statement.
937 if (N->getLocationContext() != CalleeSF)
938 return nullptr;
939
940 std::optional<StmtPoint> SP = N->getLocationAs<StmtPoint>();
941 if (!SP)
942 return nullptr;
943
944 const auto *Ret = dyn_cast<ReturnStmt>(SP->getStmt());
945 if (!Ret)
946 return nullptr;
947
948 // Okay, we're at the right return statement, but do we have the return
949 // value available?
950 ProgramStateRef State = N->getState();
951 const Expr *RV = Ret->getRetValue();
952 if (!RV)
953 return nullptr;
954 SVal V = State->getSVal(RV, CalleeSF);
955 if (V.isUnknownOrUndef())
956 return nullptr;
957
958 // Don't print any more notes after this one.
959 Mode = Satisfied;
960
961 const Expr *RetE = Ret->getRetValue();
962 assert(RetE && "Tracking a return value for a void function");
963
964 // Handle cases where a reference is returned and then immediately used.
965 std::optional<Loc> LValue;
966 if (RetE->isGLValue()) {
967 if ((LValue = V.getAs<Loc>())) {
968 SVal RValue = State->getRawSVal(*LValue, RetE->getType());
969 if (isa<DefinedSVal>(RValue))
970 V = RValue;
971 }
972 }
973
974 // Ignore aggregate rvalues.
976 return nullptr;
977
978 RetE = RetE->IgnoreParenCasts();
979
980 // Let's track the return value.
981 getParentTracker().track(RetE, N, {TKind, EnableNullFPSuppression});
982
983 // Build an appropriate message based on the return value.
984 SmallString<64> Msg;
985 llvm::raw_svector_ostream Out(Msg);
986
987 bool WouldEventBeMeaningless = false;
988
989 if (State->isNull(V).isConstrainedTrue()) {
990 if (isa<Loc>(V)) {
991
992 // If we have counter-suppression enabled, make sure we keep visiting
993 // future nodes. We want to emit a path note as well, in case
994 // the report is resurrected as valid later on.
995 if (EnableNullFPSuppression &&
996 Options.ShouldAvoidSuppressingNullArgumentPaths)
997 Mode = MaybeUnsuppress;
998
999 if (RetE->getType()->isObjCObjectPointerType()) {
1000 Out << "Returning nil";
1001 } else {
1002 Out << "Returning null pointer";
1003 }
1004 } else {
1005 Out << "Returning zero";
1006 }
1007
1008 } else {
1009 if (auto CI = V.getAs<nonloc::ConcreteInt>()) {
1010 Out << "Returning the value " << CI->getValue();
1011 } else {
1012 // There is nothing interesting about returning a value, when it is
1013 // plain value without any constraints, and the function is guaranteed
1014 // to return that every time. We could use CFG::isLinear() here, but
1015 // constexpr branches are obvious to the compiler, not necesserily to
1016 // the programmer.
1017 if (N->getCFG().size() == 3)
1018 WouldEventBeMeaningless = true;
1019
1020 Out << (isa<Loc>(V) ? "Returning pointer" : "Returning value");
1021 }
1022 }
1023
1024 if (LValue) {
1025 if (const MemRegion *MR = LValue->getAsRegion()) {
1026 if (MR->canPrintPretty()) {
1027 Out << " (reference to ";
1028 MR->printPretty(Out);
1029 Out << ")";
1030 }
1031 }
1032 } else {
1033 // FIXME: We should have a more generalized location printing mechanism.
1034 if (const auto *DR = dyn_cast<DeclRefExpr>(RetE))
1035 if (const auto *DD = dyn_cast<DeclaratorDecl>(DR->getDecl()))
1036 Out << " (loaded from '" << *DD << "')";
1037 }
1038
1039 PathDiagnosticLocation L(Ret, BRC.getSourceManager(), CalleeSF);
1040 if (!L.isValid() || !L.asLocation().isValid())
1041 return nullptr;
1042
1043 if (TKind == bugreporter::TrackingKind::Condition)
1045
1046 auto EventPiece = std::make_shared<PathDiagnosticEventPiece>(L, Out.str());
1047
1048 // If we determined that the note is meaningless, make it prunable, and
1049 // don't mark the stackframe interesting.
1050 if (WouldEventBeMeaningless)
1051 EventPiece->setPrunable(true);
1052 else
1053 BR.markInteresting(CalleeSF);
1054
1055 return EventPiece;
1056 }
1057
1058 PathDiagnosticPieceRef visitNodeMaybeUnsuppress(const ExplodedNode *N,
1059 BugReporterContext &BRC,
1060 PathSensitiveBugReport &BR) {
1061 assert(Options.ShouldAvoidSuppressingNullArgumentPaths);
1062
1063 // Are we at the entry node for this call?
1064 std::optional<CallEnter> CE = N->getLocationAs<CallEnter>();
1065 if (!CE)
1066 return nullptr;
1067
1068 if (CE->getCalleeContext() != CalleeSF)
1069 return nullptr;
1070
1071 Mode = Satisfied;
1072
1073 // Don't automatically suppress a report if one of the arguments is
1074 // known to be a null pointer. Instead, start tracking /that/ null
1075 // value back to its origin.
1076 ProgramStateManager &StateMgr = BRC.getStateManager();
1077 CallEventManager &CallMgr = StateMgr.getCallEventManager();
1078
1079 ProgramStateRef State = N->getState();
1080 CallEventRef<> Call = CallMgr.getCaller(CalleeSF, State);
1081 for (unsigned I = 0, E = Call->getNumArgs(); I != E; ++I) {
1082 std::optional<Loc> ArgV = Call->getArgSVal(I).getAs<Loc>();
1083 if (!ArgV)
1084 continue;
1085
1086 const Expr *ArgE = Call->getArgExpr(I);
1087 if (!ArgE)
1088 continue;
1089
1090 // Is it possible for this argument to be non-null?
1091 if (!State->isNull(*ArgV).isConstrainedTrue())
1092 continue;
1093
1094 if (getParentTracker()
1095 .track(ArgE, N, {TKind, EnableNullFPSuppression})
1096 .FoundSomethingToTrack)
1097 ShouldInvalidate = false;
1098
1099 // If we /can't/ track the null pointer, we should err on the side of
1100 // false negatives, and continue towards marking this report invalid.
1101 // (We will still look at the other arguments, though.)
1102 }
1103
1104 return nullptr;
1105 }
1106
1107 PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
1108 BugReporterContext &BRC,
1109 PathSensitiveBugReport &BR) override {
1110 switch (Mode) {
1111 case Initial:
1112 return visitNodeInitial(N, BRC, BR);
1113 case MaybeUnsuppress:
1114 return visitNodeMaybeUnsuppress(N, BRC, BR);
1115 case Satisfied:
1116 return nullptr;
1117 }
1118
1119 llvm_unreachable("Invalid visit mode!");
1120 }
1121
1122 void finalizeVisitor(BugReporterContext &, const ExplodedNode *,
1123 PathSensitiveBugReport &BR) override {
1124 if (EnableNullFPSuppression && ShouldInvalidate)
1125 BR.markInvalid(ReturnVisitor::getTag(), CalleeSF);
1126 }
1127};
1128
1129//===----------------------------------------------------------------------===//
1130// StoreSiteFinder
1131//===----------------------------------------------------------------------===//
1132
1133/// Finds last store into the given region,
1134/// which is different from a given symbolic value.
1135class StoreSiteFinder final : public TrackingBugReporterVisitor {
1136 const MemRegion *R;
1137 SVal V;
1138 bool Satisfied = false;
1139
1140 TrackingOptions Options;
1141 const StackFrame *OriginSF;
1142
1143public:
1144 /// \param V We're searching for the store where \c R received this value.
1145 /// \param R The region we're tracking.
1146 /// \param Options Tracking behavior options.
1147 /// \param OriginSF Only adds notes when the last store happened in a
1148 /// different stackframe to this one. Disregarded if the tracking kind
1149 /// is thorough.
1150 /// This is useful, because for non-tracked regions, notes about
1151 /// changes to its value in a nested stackframe could be pruned, and
1152 /// this visitor can prevent that without polluting the bugpath too
1153 /// much.
1154 StoreSiteFinder(bugreporter::TrackerRef ParentTracker, SVal V,
1155 const MemRegion *R, TrackingOptions Options,
1156 const StackFrame *OriginSF = nullptr)
1157 : TrackingBugReporterVisitor(ParentTracker), R(R), V(V), Options(Options),
1158 OriginSF(OriginSF) {
1159 assert(R);
1160 }
1161
1162 void Profile(llvm::FoldingSetNodeID &ID) const override;
1163
1164 PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
1165 BugReporterContext &BRC,
1166 PathSensitiveBugReport &BR) override;
1167};
1168} // namespace
1169
1170void StoreSiteFinder::Profile(llvm::FoldingSetNodeID &ID) const {
1171 static int tag = 0;
1172 ID.AddPointer(&tag);
1173 ID.AddPointer(R);
1174 ID.Add(V);
1175 ID.AddInteger(static_cast<int>(Options.Kind));
1176 ID.AddBoolean(Options.EnableNullFPSuppression);
1177}
1178
1179/// Returns true if \p N represents the DeclStmt declaring and initializing
1180/// \p VR.
1181static bool isInitializationOfVar(const ExplodedNode *N, const VarRegion *VR) {
1182 std::optional<PostStmt> P = N->getLocationAs<PostStmt>();
1183 if (!P)
1184 return false;
1185
1186 const DeclStmt *DS = P->getStmtAs<DeclStmt>();
1187 if (!DS)
1188 return false;
1189
1190 if (DS->getSingleDecl() != VR->getDecl())
1191 return false;
1192
1193 const auto *FrameSpace =
1195
1196 if (!FrameSpace) {
1197 // If we ever directly evaluate global DeclStmts, this assertion will be
1198 // invalid, but this still seems preferable to silently accepting an
1199 // initialization that may be for a path-sensitive variable.
1200 [[maybe_unused]] bool IsLocalStaticOrLocalExtern =
1201 VR->getDecl()->isStaticLocal() || VR->getDecl()->isLocalExternDecl();
1202 assert(IsLocalStaticOrLocalExtern &&
1203 "Declared a variable on the stack without Stack memspace?");
1204 return true;
1205 }
1206
1207 assert(VR->getDecl()->hasLocalStorage());
1208 const LocationContext *LCtx = N->getLocationContext();
1209 return FrameSpace->getStackFrame() == LCtx->getStackFrame();
1210}
1211
1212static bool isObjCPointer(const MemRegion *R) {
1213 if (R->isBoundable())
1214 if (const auto *TR = dyn_cast<TypedValueRegion>(R))
1215 return TR->getValueType()->isObjCObjectPointerType();
1216
1217 return false;
1218}
1219
1220static bool isObjCPointer(const ValueDecl *D) {
1221 return D->getType()->isObjCObjectPointerType();
1222}
1223
1224namespace {
1225using DestTypeValue = std::pair<const StoreInfo &, loc::ConcreteInt>;
1226
1227llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DestTypeValue &Val) {
1228 if (auto *TyR = Val.first.Dest->getAs<TypedRegion>()) {
1229 QualType LocTy = TyR->getLocationType();
1230 if (!LocTy.isNull()) {
1231 if (auto *PtrTy = LocTy->getAs<PointerType>()) {
1232 std::string PStr = PtrTy->getPointeeType().getAsString();
1233 if (!PStr.empty())
1234 OS << "(" << PStr << ")";
1235 }
1236 }
1237 }
1238 SmallString<16> ValStr;
1239 Val.second.getValue()->toString(ValStr, 10, true);
1240 OS << ValStr;
1241 return OS;
1242}
1243} // namespace
1244
1245/// Show diagnostics for initializing or declaring a region \p R with a bad value.
1246static void showBRDiagnostics(llvm::raw_svector_ostream &OS, StoreInfo SI) {
1247 const bool HasPrefix = SI.Dest->canPrintPretty();
1248
1249 if (HasPrefix) {
1250 SI.Dest->printPretty(OS);
1251 OS << " ";
1252 }
1253
1254 const char *Action = nullptr;
1255
1256 switch (SI.StoreKind) {
1258 Action = HasPrefix ? "initialized to " : "Initializing to ";
1259 break;
1261 Action = HasPrefix ? "captured by block as " : "Captured by block as ";
1262 break;
1263 default:
1264 llvm_unreachable("Unexpected store kind");
1265 }
1266
1267 if (auto CVal = SI.Value.getAs<loc::ConcreteInt>()) {
1268 if (!*CVal->getValue())
1269 OS << Action << (isObjCPointer(SI.Dest) ? "nil" : "a null pointer value");
1270 else
1271 OS << Action << DestTypeValue(SI, *CVal);
1272
1273 } else if (auto CVal = SI.Value.getAs<nonloc::ConcreteInt>()) {
1274 OS << Action << CVal->getValue();
1275
1276 } else if (SI.Origin && SI.Origin->canPrintPretty()) {
1277 OS << Action << "the value of ";
1278 SI.Origin->printPretty(OS);
1279
1280 } else if (SI.StoreKind == StoreInfo::Initialization) {
1281 // We don't need to check here, all these conditions were
1282 // checked by StoreSiteFinder, when it figured out that it is
1283 // initialization.
1284 const auto *DS =
1286
1287 if (SI.Value.isUndef()) {
1288 if (isa<VarRegion>(SI.Dest)) {
1289 const auto *VD = cast<VarDecl>(DS->getSingleDecl());
1290
1291 if (VD->getInit()) {
1292 OS << (HasPrefix ? "initialized" : "Initializing")
1293 << " to a garbage value";
1294 } else {
1295 OS << (HasPrefix ? "declared" : "Declaring")
1296 << " without an initial value";
1297 }
1298 }
1299 } else {
1300 OS << (HasPrefix ? "initialized" : "Initialized") << " here";
1301 }
1302 }
1303}
1304
1305/// Display diagnostics for passing bad region as a parameter.
1306static void showBRParamDiagnostics(llvm::raw_svector_ostream &OS,
1307 StoreInfo SI) {
1308 const auto *VR = cast<VarRegion>(SI.Dest);
1309 const auto *D = VR->getDecl();
1310
1311 OS << "Passing ";
1312
1313 if (auto CI = SI.Value.getAs<loc::ConcreteInt>()) {
1314 if (!*CI->getValue())
1315 OS << (isObjCPointer(D) ? "nil object reference" : "null pointer value");
1316 else
1317 OS << (isObjCPointer(D) ? "object reference of value " : "pointer value ")
1318 << DestTypeValue(SI, *CI);
1319
1320 } else if (SI.Value.isUndef()) {
1321 OS << "uninitialized value";
1322
1323 } else if (auto CI = SI.Value.getAs<nonloc::ConcreteInt>()) {
1324 OS << "the value " << CI->getValue();
1325
1326 } else if (SI.Origin && SI.Origin->canPrintPretty()) {
1327 SI.Origin->printPretty(OS);
1328
1329 } else {
1330 OS << "value";
1331 }
1332
1333 if (const auto *Param = dyn_cast<ParmVarDecl>(VR->getDecl())) {
1334 // Printed parameter indexes are 1-based, not 0-based.
1335 unsigned Idx = Param->getFunctionScopeIndex() + 1;
1336 OS << " via " << Idx << llvm::getOrdinalSuffix(Idx) << " parameter";
1337 if (VR->canPrintPretty()) {
1338 OS << " ";
1339 VR->printPretty(OS);
1340 }
1341 } else if (const auto *ImplParam = dyn_cast<ImplicitParamDecl>(D)) {
1342 if (ImplParam->getParameterKind() == ImplicitParamKind::ObjCSelf) {
1343 OS << " via implicit parameter 'self'";
1344 }
1345 }
1346}
1347
1348/// Show default diagnostics for storing bad region.
1349static void showBRDefaultDiagnostics(llvm::raw_svector_ostream &OS,
1350 StoreInfo SI) {
1351 const bool HasSuffix = SI.Dest->canPrintPretty();
1352
1353 if (auto CV = SI.Value.getAs<loc::ConcreteInt>()) {
1354 APSIntPtr V = CV->getValue();
1355 if (!*V)
1356 OS << (isObjCPointer(SI.Dest)
1357 ? "nil object reference stored"
1358 : (HasSuffix ? "Null pointer value stored"
1359 : "Storing null pointer value"));
1360 else {
1361 if (isObjCPointer(SI.Dest)) {
1362 OS << "object reference of value " << DestTypeValue(SI, *CV)
1363 << " stored";
1364 } else {
1365 if (HasSuffix)
1366 OS << "Pointer value of " << DestTypeValue(SI, *CV) << " stored";
1367 else
1368 OS << "Storing pointer value of " << DestTypeValue(SI, *CV);
1369 }
1370 }
1371 } else if (SI.Value.isUndef()) {
1372 OS << (HasSuffix ? "Uninitialized value stored"
1373 : "Storing uninitialized value");
1374
1375 } else if (auto CV = SI.Value.getAs<nonloc::ConcreteInt>()) {
1376 if (HasSuffix)
1377 OS << "The value " << CV->getValue() << " is assigned";
1378 else
1379 OS << "Assigning " << CV->getValue();
1380
1381 } else if (SI.Origin && SI.Origin->canPrintPretty()) {
1382 if (HasSuffix) {
1383 OS << "The value of ";
1384 SI.Origin->printPretty(OS);
1385 OS << " is assigned";
1386 } else {
1387 OS << "Assigning the value of ";
1388 SI.Origin->printPretty(OS);
1389 }
1390
1391 } else {
1392 OS << (HasSuffix ? "Value assigned" : "Assigning value");
1393 }
1394
1395 if (HasSuffix) {
1396 OS << " to ";
1397 SI.Dest->printPretty(OS);
1398 }
1399}
1400
1402 if (!CE)
1403 return false;
1404
1405 const auto *CtorDecl = CE->getConstructor();
1406
1407 return CtorDecl->isCopyOrMoveConstructor() && CtorDecl->isTrivial();
1408}
1409
1411 const MemRegion *R) {
1412
1413 const auto *TVR = dyn_cast_or_null<TypedValueRegion>(R);
1414
1415 if (!TVR)
1416 return nullptr;
1417
1418 const auto ITy = ILE->getType().getCanonicalType();
1419
1420 // Push each sub-region onto the stack.
1421 std::stack<const TypedValueRegion *> TVRStack;
1422 while (isa<FieldRegion>(TVR) || isa<ElementRegion>(TVR)) {
1423 // We found a region that matches the type of the init list,
1424 // so we assume this is the outer-most region. This can happen
1425 // if the initializer list is inside a class. If our assumption
1426 // is wrong, we return a nullptr in the end.
1427 if (ITy == TVR->getValueType().getCanonicalType())
1428 break;
1429
1430 TVRStack.push(TVR);
1431 TVR = cast<TypedValueRegion>(TVR->getSuperRegion());
1432 }
1433
1434 // If the type of the outer most region doesn't match the type
1435 // of the ILE, we can't match the ILE and the region.
1436 if (ITy != TVR->getValueType().getCanonicalType())
1437 return nullptr;
1438
1439 const Expr *Init = ILE;
1440 while (!TVRStack.empty()) {
1441 TVR = TVRStack.top();
1442 TVRStack.pop();
1443
1444 // We hit something that's not an init list before
1445 // running out of regions, so we most likely failed.
1446 if (!isa<InitListExpr>(Init))
1447 return nullptr;
1448
1449 ILE = cast<InitListExpr>(Init);
1450 auto NumInits = ILE->getNumInits();
1451
1452 if (const auto *FR = dyn_cast<FieldRegion>(TVR)) {
1453 const auto *FD = FR->getDecl();
1454
1455 if (FD->getFieldIndex() >= NumInits)
1456 return nullptr;
1457
1458 Init = ILE->getInit(FD->getFieldIndex());
1459 } else if (const auto *ER = dyn_cast<ElementRegion>(TVR)) {
1460 const auto Ind = ER->getIndex();
1461
1462 // If index is symbolic, we can't figure out which expression
1463 // belongs to the region.
1464 if (!Ind.isConstant())
1465 return nullptr;
1466
1467 const auto IndVal = Ind.getAsInteger()->getLimitedValue();
1468 if (IndVal >= NumInits)
1469 return nullptr;
1470
1471 Init = ILE->getInit(IndVal);
1472 }
1473 }
1474
1475 return Init;
1476}
1477
1478PathDiagnosticPieceRef StoreSiteFinder::VisitNode(const ExplodedNode *Succ,
1479 BugReporterContext &BRC,
1481 if (Satisfied)
1482 return nullptr;
1483
1484 const ExplodedNode *StoreSite = nullptr;
1485 const ExplodedNode *Pred = Succ->getFirstPred();
1486 const Expr *InitE = nullptr;
1487 bool IsParam = false;
1488
1489 // First see if we reached the declaration of the region.
1490 if (const auto *VR = dyn_cast<VarRegion>(R)) {
1491 if (isInitializationOfVar(Pred, VR)) {
1492 StoreSite = Pred;
1493 InitE = VR->getDecl()->getInit();
1494 }
1495 }
1496
1497 // If this is a post initializer expression, initializing the region, we
1498 // should track the initializer expression.
1499 if (std::optional<PostInitializer> PIP =
1500 Pred->getLocationAs<PostInitializer>()) {
1501 const MemRegion *FieldReg = (const MemRegion *)PIP->getLocationValue();
1502 if (FieldReg == R) {
1503 StoreSite = Pred;
1504 InitE = PIP->getInitializer()->getInit();
1505 }
1506 }
1507
1508 // Otherwise, see if this is the store site:
1509 // (1) Succ has this binding and Pred does not, i.e. this is
1510 // where the binding first occurred.
1511 // (2) Succ has this binding and is a PostStore node for this region, i.e.
1512 // the same binding was re-assigned here.
1513 if (!StoreSite) {
1514 if (Succ->getState()->getSVal(R) != V)
1515 return nullptr;
1516
1517 if (hasVisibleUpdate(Pred, Pred->getState()->getSVal(R), Succ, V)) {
1518 std::optional<PostStore> PS = Succ->getLocationAs<PostStore>();
1519 if (!PS || PS->getLocationValue() != R)
1520 return nullptr;
1521 }
1522
1523 StoreSite = Succ;
1524
1525 if (std::optional<PostStmt> P = Succ->getLocationAs<PostStmt>()) {
1526 // If this is an assignment expression, we can track the value
1527 // being assigned.
1528 if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>()) {
1529 if (BO->isAssignmentOp())
1530 InitE = BO->getRHS();
1531 }
1532 // If we have a declaration like 'S s{1,2}' that needs special
1533 // handling, we handle it here.
1534 else if (const auto *DS = P->getStmtAs<DeclStmt>()) {
1535 const auto *Decl = DS->getSingleDecl();
1536 if (isa<VarDecl>(Decl)) {
1537 const auto *VD = cast<VarDecl>(Decl);
1538
1539 // FIXME: Here we only track the inner most region, so we lose
1540 // information, but it's still better than a crash or no information
1541 // at all.
1542 //
1543 // E.g.: The region we have is 's.s2.s3.s4.y' and we only track 'y',
1544 // and throw away the rest.
1545 if (const auto *ILE = dyn_cast<InitListExpr>(VD->getInit()))
1546 InitE = tryExtractInitializerFromList(ILE, R);
1547 }
1548 } else if (const auto *CE = P->getStmtAs<CXXConstructExpr>()) {
1549
1550 const auto State = Succ->getState();
1551
1553 // Migrate the field regions from the current object to
1554 // the parent object. If we track 'a.y.e' and encounter
1555 // 'S a = b' then we need to track 'b.y.e'.
1556
1557 // Push the regions to a stack, from last to first, so
1558 // considering the example above the stack will look like
1559 // (bottom) 'e' -> 'y' (top).
1560
1561 std::stack<const SubRegion *> SRStack;
1562 const SubRegion *SR = cast<SubRegion>(R);
1563 while (isa<FieldRegion>(SR) || isa<ElementRegion>(SR)) {
1564 SRStack.push(SR);
1565 SR = cast<SubRegion>(SR->getSuperRegion());
1566 }
1567
1568 // Get the region for the object we copied/moved from.
1569 const auto *OriginEx = CE->getArg(0);
1570 const auto OriginVal =
1571 State->getSVal(OriginEx, Succ->getLocationContext());
1572
1573 // Pop the stored field regions and apply them to the origin
1574 // object in the same order we had them on the copy.
1575 // OriginField will evolve like 'b' -> 'b.y' -> 'b.y.e'.
1576 SVal OriginField = OriginVal;
1577 while (!SRStack.empty()) {
1578 const auto *TopR = SRStack.top();
1579 SRStack.pop();
1580
1581 if (const auto *FR = dyn_cast<FieldRegion>(TopR)) {
1582 OriginField = State->getLValue(FR->getDecl(), OriginField);
1583 } else if (const auto *ER = dyn_cast<ElementRegion>(TopR)) {
1584 OriginField = State->getLValue(ER->getElementType(),
1585 ER->getIndex(), OriginField);
1586 } else {
1587 // FIXME: handle other region type
1588 }
1589 }
1590
1591 // Track 'b.y.e'.
1592 getParentTracker().track(V, OriginField.getAsRegion(), Options);
1593 InitE = OriginEx;
1594 }
1595 }
1596 // This branch can occur in cases like `Ctor() : field{ x, y } {}'.
1597 else if (const auto *ILE = P->getStmtAs<InitListExpr>()) {
1598 // FIXME: Here we only track the top level region, so we lose
1599 // information, but it's still better than a crash or no information
1600 // at all.
1601 //
1602 // E.g.: The region we have is 's.s2.s3.s4.y' and we only track 'y', and
1603 // throw away the rest.
1604 InitE = tryExtractInitializerFromList(ILE, R);
1605 }
1606 }
1607
1608 // If this is a call entry, the variable should be a parameter.
1609 // FIXME: Handle CXXThisRegion as well. (This is not a priority because
1610 // 'this' should never be NULL, but this visitor isn't just for NULL and
1611 // UndefinedVal.)
1612 if (std::optional<CallEnter> CE = Succ->getLocationAs<CallEnter>()) {
1613 if (const auto *VR = dyn_cast<VarRegion>(R)) {
1614
1615 if (const auto *Param = dyn_cast<ParmVarDecl>(VR->getDecl())) {
1616 ProgramStateManager &StateMgr = BRC.getStateManager();
1617 CallEventManager &CallMgr = StateMgr.getCallEventManager();
1618
1619 CallEventRef<> Call = CallMgr.getCaller(CE->getCalleeContext(),
1620 Succ->getState());
1621 InitE = Call->getArgExpr(Param->getFunctionScopeIndex());
1622 } else {
1623 // Handle Objective-C 'self'.
1624 assert(isa<ImplicitParamDecl>(VR->getDecl()));
1625 InitE = cast<ObjCMessageExpr>(CE->getCalleeContext()->getCallSite())
1626 ->getInstanceReceiver()->IgnoreParenCasts();
1627 }
1628 IsParam = true;
1629 }
1630 }
1631
1632 // If this is a CXXTempObjectRegion, the Expr responsible for its creation
1633 // is wrapped inside of it.
1634 if (const auto *TmpR = dyn_cast<CXXTempObjectRegion>(R))
1635 InitE = TmpR->getExpr();
1636 }
1637
1638 if (!StoreSite)
1639 return nullptr;
1640
1641 Satisfied = true;
1642
1643 // If we have an expression that provided the value, try to track where it
1644 // came from.
1645 if (InitE) {
1646 if (!IsParam)
1647 InitE = InitE->IgnoreParenCasts();
1648
1649 getParentTracker().track(InitE, StoreSite, Options);
1650 }
1651
1652 // Let's try to find the region where the value came from.
1653 const MemRegion *OldRegion = nullptr;
1654
1655 // If we have init expression, it might be simply a reference
1656 // to a variable, so we can use it.
1657 if (InitE) {
1658 // That region might still be not exactly what we are looking for.
1659 // In situations like `int &ref = val;`, we can't say that
1660 // `ref` is initialized with `val`, rather refers to `val`.
1661 //
1662 // In order, to mitigate situations like this, we check if the last
1663 // stored value in that region is the value that we track.
1664 //
1665 // TODO: support other situations better.
1666 if (const MemRegion *Candidate =
1667 getLocationRegionIfReference(InitE, Succ, false)) {
1669
1670 // Here we traverse the graph up to find the last node where the
1671 // candidate region is still in the store.
1672 for (const ExplodedNode *N = StoreSite; N; N = N->getFirstPred()) {
1673 if (SM.includedInBindings(N->getState()->getStore(), Candidate)) {
1674 // And if it was bound to the target value, we can use it.
1675 if (N->getState()->getSVal(Candidate) == V) {
1676 OldRegion = Candidate;
1677 }
1678 break;
1679 }
1680 }
1681 }
1682 }
1683
1684 // Otherwise, if the current region does indeed contain the value
1685 // we are looking for, we can look for a region where this value
1686 // was before.
1687 //
1688 // It can be useful for situations like:
1689 // new = identity(old)
1690 // where the analyzer knows that 'identity' returns the value of its
1691 // first argument.
1692 //
1693 // NOTE: If the region R is not a simple var region, it can contain
1694 // V in one of its subregions.
1695 if (!OldRegion && StoreSite->getState()->getSVal(R) == V) {
1696 // Let's go up the graph to find the node where the region is
1697 // bound to V.
1698 const ExplodedNode *NodeWithoutBinding = StoreSite->getFirstPred();
1699 for (;
1700 NodeWithoutBinding && NodeWithoutBinding->getState()->getSVal(R) == V;
1701 NodeWithoutBinding = NodeWithoutBinding->getFirstPred()) {
1702 }
1703
1704 if (NodeWithoutBinding) {
1705 // Let's try to find a unique binding for the value in that node.
1706 // We want to use this to find unique bindings because of the following
1707 // situations:
1708 // b = a;
1709 // c = identity(b);
1710 //
1711 // Telling the user that the value of 'a' is assigned to 'c', while
1712 // correct, can be confusing.
1713 StoreManager::FindUniqueBinding FB(V.getAsLocSymbol());
1714 BRC.getStateManager().iterBindings(NodeWithoutBinding->getState(), FB);
1715 if (FB)
1716 OldRegion = FB.getRegion();
1717 }
1718 }
1719
1720 if (Options.Kind == TrackingKind::Condition && OriginSF &&
1721 !OriginSF->isParentOf(StoreSite->getStackFrame()))
1722 return nullptr;
1723
1724 // Okay, we've found the binding. Emit an appropriate message.
1725 SmallString<256> sbuf;
1726 llvm::raw_svector_ostream os(sbuf);
1727
1728 StoreInfo SI = {StoreInfo::Assignment, // default kind
1729 StoreSite,
1730 InitE,
1731 V,
1732 R,
1733 OldRegion};
1734
1735 if (std::optional<PostStmt> PS = StoreSite->getLocationAs<PostStmt>()) {
1736 const Stmt *S = PS->getStmt();
1737 const auto *DS = dyn_cast<DeclStmt>(S);
1738 const auto *VR = dyn_cast<VarRegion>(R);
1739
1740 if (DS) {
1742 } else if (const auto *BExpr = dyn_cast<BlockExpr>(S)) {
1744 if (VR) {
1745 // See if we can get the BlockVarRegion.
1746 ProgramStateRef State = StoreSite->getState();
1747 SVal V = StoreSite->getSVal(BExpr);
1748 if (const auto *BDR =
1749 dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) {
1750 if (const VarRegion *OriginalR = BDR->getOriginalRegion(VR)) {
1751 getParentTracker().track(State->getSVal(OriginalR), OriginalR,
1752 Options, OriginSF);
1753 }
1754 }
1755 }
1756 }
1757 } else if (SI.StoreSite->getLocation().getAs<CallEnter>() &&
1758 isa<VarRegion>(SI.Dest)) {
1760 }
1761
1762 return getParentTracker().handle(SI, BRC, Options);
1763}
1764
1765//===----------------------------------------------------------------------===//
1766// Implementation of TrackConstraintBRVisitor.
1767//===----------------------------------------------------------------------===//
1768
1769void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
1770 static int tag = 0;
1771 ID.AddPointer(&tag);
1772 ID.AddString(Message);
1773 ID.AddBoolean(Assumption);
1774 ID.Add(Constraint);
1775}
1776
1777/// Return the tag associated with this visitor. This tag will be used
1778/// to make all PathDiagnosticPieces created by this visitor.
1780 return "TrackConstraintBRVisitor";
1781}
1782
1783bool TrackConstraintBRVisitor::isZeroCheck() const {
1784 return !Assumption && Constraint.getAs<Loc>();
1785}
1786
1787bool TrackConstraintBRVisitor::isUnderconstrained(const ExplodedNode *N) const {
1788 if (isZeroCheck())
1789 return N->getState()->isNull(Constraint).isUnderconstrained();
1790 return (bool)N->getState()->assume(Constraint, !Assumption);
1791}
1792
1795 const ExplodedNode *PrevN = N->getFirstPred();
1796 if (IsSatisfied)
1797 return nullptr;
1798
1799 // Start tracking after we see the first state in which the value is
1800 // constrained.
1801 if (!IsTrackingTurnedOn)
1802 if (!isUnderconstrained(N))
1803 IsTrackingTurnedOn = true;
1804 if (!IsTrackingTurnedOn)
1805 return nullptr;
1806
1807 // Check if in the previous state it was feasible for this constraint
1808 // to *not* be true.
1809 if (isUnderconstrained(PrevN)) {
1810 IsSatisfied = true;
1811
1812 // At this point, the negation of the constraint should be infeasible. If it
1813 // is feasible, make sure that the negation of the constrainti was
1814 // infeasible in the current state. If it is feasible, we somehow missed
1815 // the transition point.
1816 assert(!isUnderconstrained(N));
1817
1818 // Construct a new PathDiagnosticPiece.
1819 ProgramPoint P = N->getLocation();
1820
1821 // If this node already have a specialized note, it's probably better
1822 // than our generic note.
1823 // FIXME: This only looks for note tags, not for other ways to add a note.
1824 if (isa_and_nonnull<NoteTag>(P.getTag()))
1825 return nullptr;
1826
1829 if (!L.isValid())
1830 return nullptr;
1831
1832 auto X = std::make_shared<PathDiagnosticEventPiece>(L, Message);
1833 X->setTag(getTag());
1834 return std::move(X);
1835 }
1836
1837 return nullptr;
1838}
1839
1840//===----------------------------------------------------------------------===//
1841// Implementation of SuppressInlineDefensiveChecksVisitor.
1842//===----------------------------------------------------------------------===//
1843
1846 : V(Value) {
1847 // Check if the visitor is disabled.
1848 AnalyzerOptions &Options = N->getState()->getAnalysisManager().options;
1849 if (!Options.ShouldSuppressInlinedDefensiveChecks)
1850 IsSatisfied = true;
1851}
1852
1854 llvm::FoldingSetNodeID &ID) const {
1855 static int id = 0;
1856 ID.AddPointer(&id);
1857 ID.Add(V);
1858}
1859
1861 return "IDCVisitor";
1862}
1863
1866 BugReporterContext &BRC,
1868 const ExplodedNode *Pred = Succ->getFirstPred();
1869 if (IsSatisfied)
1870 return nullptr;
1871
1872 // Start tracking after we see the first state in which the value is null.
1873 if (!IsTrackingTurnedOn)
1874 if (Succ->getState()->isNull(V).isConstrainedTrue())
1875 IsTrackingTurnedOn = true;
1876 if (!IsTrackingTurnedOn)
1877 return nullptr;
1878
1879 // Check if in the previous state it was feasible for this value
1880 // to *not* be null.
1881 if (!Pred->getState()->isNull(V).isConstrainedTrue() &&
1882 Succ->getState()->isNull(V).isConstrainedTrue()) {
1883 IsSatisfied = true;
1884
1885 // Check if this is inlined defensive checks.
1886 const LocationContext *CurLC = Succ->getLocationContext();
1887 const LocationContext *ReportLC = BR.getErrorNode()->getLocationContext();
1888 if (CurLC != ReportLC && !CurLC->isParentOf(ReportLC)) {
1889 BR.markInvalid("Suppress IDC", CurLC);
1890 return nullptr;
1891 }
1892
1893 // Treat defensive checks in function-like macros as if they were an inlined
1894 // defensive check. If the bug location is not in a macro and the
1895 // terminator for the current location is in a macro then suppress the
1896 // warning.
1897 auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>();
1898
1899 if (!BugPoint)
1900 return nullptr;
1901
1902 ProgramPoint CurPoint = Succ->getLocation();
1903 const Stmt *CurTerminatorStmt = nullptr;
1904 if (auto BE = CurPoint.getAs<BlockEdge>()) {
1905 CurTerminatorStmt = BE->getSrc()->getTerminator().getStmt();
1906 } else if (auto SP = CurPoint.getAs<StmtPoint>()) {
1907 const Stmt *CurStmt = SP->getStmt();
1908 if (!CurStmt->getBeginLoc().isMacroID())
1909 return nullptr;
1910
1911 const CFGStmtMap *Map = CurLC->getAnalysisDeclContext()->getCFGStmtMap();
1912 CurTerminatorStmt = Map->getBlock(CurStmt)->getTerminatorStmt();
1913 } else {
1914 return nullptr;
1915 }
1916
1917 if (!CurTerminatorStmt)
1918 return nullptr;
1919
1920 SourceLocation TerminatorLoc = CurTerminatorStmt->getBeginLoc();
1921 if (TerminatorLoc.isMacroID()) {
1922 SourceLocation BugLoc = BugPoint->getStmt()->getBeginLoc();
1923
1924 // Suppress reports unless we are in that same macro.
1925 if (!BugLoc.isMacroID() ||
1926 getMacroName(BugLoc, BRC) != getMacroName(TerminatorLoc, BRC)) {
1927 BR.markInvalid("Suppress Macro IDC", CurLC);
1928 }
1929 return nullptr;
1930 }
1931 }
1932 return nullptr;
1933}
1934
1935//===----------------------------------------------------------------------===//
1936// TrackControlDependencyCondBRVisitor.
1937//===----------------------------------------------------------------------===//
1938
1939namespace {
1940/// Tracks the expressions that are a control dependency of the node that was
1941/// supplied to the constructor.
1942/// For example:
1943///
1944/// cond = 1;
1945/// if (cond)
1946/// 10 / 0;
1947///
1948/// An error is emitted at line 3. This visitor realizes that the branch
1949/// on line 2 is a control dependency of line 3, and tracks it's condition via
1950/// trackExpressionValue().
1951class TrackControlDependencyCondBRVisitor final
1953 const ExplodedNode *Origin;
1954 ControlDependencyCalculator ControlDeps;
1956
1957public:
1958 TrackControlDependencyCondBRVisitor(TrackerRef ParentTracker,
1959 const ExplodedNode *O)
1960 : TrackingBugReporterVisitor(ParentTracker), Origin(O),
1961 ControlDeps(&O->getCFG()) {}
1962
1963 void Profile(llvm::FoldingSetNodeID &ID) const override {
1964 static int x = 0;
1965 ID.AddPointer(&x);
1966 }
1967
1968 PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
1969 BugReporterContext &BRC,
1970 PathSensitiveBugReport &BR) override;
1971};
1972} // end of anonymous namespace
1973
1974static std::shared_ptr<PathDiagnosticEventPiece>
1976 const ExplodedNode *N,
1977 BugReporterContext &BRC) {
1978
1980 !BRC.getAnalyzerOptions().ShouldTrackConditionsDebug)
1981 return nullptr;
1982
1983 std::string ConditionText = std::string(Lexer::getSourceText(
1984 CharSourceRange::getTokenRange(Cond->getSourceRange()),
1986
1987 return std::make_shared<PathDiagnosticEventPiece>(
1990 (Twine() + "Tracking condition '" + ConditionText + "'").str());
1991}
1992
1993static bool isAssertlikeBlock(const CFGBlock *B, ASTContext &Context) {
1994 if (B->succ_size() != 2)
1995 return false;
1996
1997 const CFGBlock *Then = B->succ_begin()->getReachableBlock();
1998 const CFGBlock *Else = (B->succ_begin() + 1)->getReachableBlock();
1999
2000 if (!Then || !Else)
2001 return false;
2002
2003 if (Then->isInevitablySinking() != Else->isInevitablySinking())
2004 return true;
2005
2006 // For the following condition the following CFG would be built:
2007 //
2008 // ------------->
2009 // / \
2010 // [B1] -> [B2] -> [B3] -> [sink]
2011 // assert(A && B || C); \ \
2012 // -----------> [go on with the execution]
2013 //
2014 // It so happens that CFGBlock::getTerminatorCondition returns 'A' for block
2015 // B1, 'A && B' for B2, and 'A && B || C' for B3. Let's check whether we
2016 // reached the end of the condition!
2017 if (const Stmt *ElseCond = Else->getTerminatorCondition())
2018 if (const auto *BinOp = dyn_cast<BinaryOperator>(ElseCond))
2019 if (BinOp->isLogicalOp())
2020 return isAssertlikeBlock(Else, Context);
2021
2022 return false;
2023}
2024
2026TrackControlDependencyCondBRVisitor::VisitNode(const ExplodedNode *N,
2027 BugReporterContext &BRC,
2029 // We can only reason about control dependencies within the same stack frame.
2030 if (Origin->getStackFrame() != N->getStackFrame())
2031 return nullptr;
2032
2033 CFGBlock *NB = const_cast<CFGBlock *>(N->getCFGBlock());
2034
2035 // Skip if we already inspected this block.
2036 if (!VisitedBlocks.insert(NB).second)
2037 return nullptr;
2038
2039 CFGBlock *OriginB = const_cast<CFGBlock *>(Origin->getCFGBlock());
2040
2041 // TODO: Cache CFGBlocks for each ExplodedNode.
2042 if (!OriginB || !NB)
2043 return nullptr;
2044
2045 if (isAssertlikeBlock(NB, BRC.getASTContext()))
2046 return nullptr;
2047
2048 if (ControlDeps.isControlDependent(OriginB, NB)) {
2049 // We don't really want to explain for range loops. Evidence suggests that
2050 // the only thing that leads to is the addition of calls to operator!=.
2051 if (llvm::isa_and_nonnull<CXXForRangeStmt>(NB->getTerminatorStmt()))
2052 return nullptr;
2053
2054 if (const Expr *Condition = NB->getLastCondition()) {
2055
2056 // If we can't retrieve a sensible condition, just bail out.
2057 const Expr *InnerExpr = peelOffOuterExpr(Condition, N);
2058 if (!InnerExpr)
2059 return nullptr;
2060
2061 // If the condition was a function call, we likely won't gain much from
2062 // tracking it either. Evidence suggests that it will mostly trigger in
2063 // scenarios like this:
2064 //
2065 // void f(int *x) {
2066 // x = nullptr;
2067 // if (alwaysTrue()) // We don't need a whole lot of explanation
2068 // // here, the function name is good enough.
2069 // *x = 5;
2070 // }
2071 //
2072 // Its easy to create a counterexample where this heuristic would make us
2073 // lose valuable information, but we've never really seen one in practice.
2074 if (isa<CallExpr>(InnerExpr))
2075 return nullptr;
2076
2077 // Keeping track of the already tracked conditions on a visitor level
2078 // isn't sufficient, because a new visitor is created for each tracked
2079 // expression, hence the BugReport level set.
2080 if (BR.addTrackedCondition(N)) {
2081 getParentTracker().track(InnerExpr, N,
2083 /*EnableNullFPSuppression=*/false});
2085 }
2086 }
2087 }
2088
2089 return nullptr;
2090}
2091
2092//===----------------------------------------------------------------------===//
2093// Implementation of trackExpressionValue.
2094//===----------------------------------------------------------------------===//
2095
2096static const Expr *peelOffOuterExpr(const Expr *Ex, const ExplodedNode *N) {
2097
2098 Ex = Ex->IgnoreParenCasts();
2099 if (const auto *FE = dyn_cast<FullExpr>(Ex))
2100 return peelOffOuterExpr(FE->getSubExpr(), N);
2101 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(Ex))
2102 return peelOffOuterExpr(OVE->getSourceExpr(), N);
2103 if (const auto *POE = dyn_cast<PseudoObjectExpr>(Ex)) {
2104 const auto *PropRef = dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
2105 if (PropRef && PropRef->isMessagingGetter()) {
2106 const Expr *GetterMessageSend =
2107 POE->getSemanticExpr(POE->getNumSemanticExprs() - 1);
2108 assert(isa<ObjCMessageExpr>(GetterMessageSend->IgnoreParenCasts()));
2109 return peelOffOuterExpr(GetterMessageSend, N);
2110 }
2111 }
2112
2113 // Peel off the ternary operator.
2114 if (const auto *CO = dyn_cast<ConditionalOperator>(Ex)) {
2115 // Find a node where the branching occurred and find out which branch
2116 // we took (true/false) by looking at the ExplodedGraph.
2117 const ExplodedNode *NI = N;
2118 do {
2119 ProgramPoint ProgPoint = NI->getLocation();
2120 if (std::optional<BlockEdge> BE = ProgPoint.getAs<BlockEdge>()) {
2121 const CFGBlock *srcBlk = BE->getSrc();
2122 if (const Stmt *term = srcBlk->getTerminatorStmt()) {
2123 if (term == CO) {
2124 bool TookTrueBranch = (*(srcBlk->succ_begin()) == BE->getDst());
2125 if (TookTrueBranch)
2126 return peelOffOuterExpr(CO->getTrueExpr(), N);
2127 else
2128 return peelOffOuterExpr(CO->getFalseExpr(), N);
2129 }
2130 }
2131 }
2132 NI = NI->getFirstPred();
2133 } while (NI);
2134 }
2135
2136 if (auto *BO = dyn_cast<BinaryOperator>(Ex))
2137 if (const Expr *SubEx = peelOffPointerArithmetic(BO))
2138 return peelOffOuterExpr(SubEx, N);
2139
2140 if (auto *UO = dyn_cast<UnaryOperator>(Ex)) {
2141 if (UO->getOpcode() == UO_LNot)
2142 return peelOffOuterExpr(UO->getSubExpr(), N);
2143
2144 // FIXME: There's a hack in our Store implementation that always computes
2145 // field offsets around null pointers as if they are always equal to 0.
2146 // The idea here is to report accesses to fields as null dereferences
2147 // even though the pointer value that's being dereferenced is actually
2148 // the offset of the field rather than exactly 0.
2149 // See the FIXME in StoreManager's getLValueFieldOrIvar() method.
2150 // This code interacts heavily with this hack; otherwise the value
2151 // would not be null at all for most fields, so we'd be unable to track it.
2152 if (UO->getOpcode() == UO_AddrOf && UO->getSubExpr()->isLValue())
2153 if (const Expr *DerefEx = bugreporter::getDerefExpr(UO->getSubExpr()))
2154 return peelOffOuterExpr(DerefEx, N);
2155 }
2156
2157 return Ex;
2158}
2159
2160/// Find the ExplodedNode where the lvalue (the value of 'Ex')
2161/// was computed.
2163 const Expr *Inner) {
2164 while (N) {
2165 if (N->getStmtForDiagnostics() == Inner)
2166 return N;
2167 N = N->getFirstPred();
2168 }
2169 return N;
2170}
2171
2172//===----------------------------------------------------------------------===//
2173// Tracker implementation
2174//===----------------------------------------------------------------------===//
2175
2177 BugReporterContext &BRC,
2178 StringRef NodeText) {
2179 // Construct a new PathDiagnosticPiece.
2182 if (P.getAs<CallEnter>() && SI.SourceOfTheValue)
2184 P.getLocationContext());
2185
2186 if (!L.isValid() || !L.asLocation().isValid())
2188
2189 if (!L.isValid() || !L.asLocation().isValid())
2190 return nullptr;
2191
2192 return std::make_shared<PathDiagnosticEventPiece>(L, NodeText);
2193}
2194
2195namespace {
2196class DefaultStoreHandler final : public StoreHandler {
2197public:
2199
2201 TrackingOptions Opts) override {
2202 // Okay, we've found the binding. Emit an appropriate message.
2203 SmallString<256> Buffer;
2204 llvm::raw_svector_ostream OS(Buffer);
2205
2206 switch (SI.StoreKind) {
2209 showBRDiagnostics(OS, SI);
2210 break;
2213 break;
2216 break;
2217 }
2218
2221
2222 return constructNote(SI, BRC, OS.str());
2223 }
2224};
2225
2226class ControlDependencyHandler final : public ExpressionHandler {
2227public:
2229
2230 Tracker::Result handle(const Expr *Inner, const ExplodedNode *InputNode,
2231 const ExplodedNode *LVNode,
2232 TrackingOptions Opts) override {
2233 PathSensitiveBugReport &Report = getParentTracker().getReport();
2234
2235 // We only track expressions if we believe that they are important. Chances
2236 // are good that control dependencies to the tracking point are also
2237 // important because of this, let's explain why we believe control reached
2238 // this point.
2239 // TODO: Shouldn't we track control dependencies of every bug location,
2240 // rather than only tracked expressions?
2241 if (LVNode->getState()
2242 ->getAnalysisManager()
2243 .getAnalyzerOptions()
2244 .ShouldTrackConditions) {
2245 Report.addVisitor<TrackControlDependencyCondBRVisitor>(
2246 &getParentTracker(), InputNode);
2247 return {/*FoundSomethingToTrack=*/true};
2248 }
2249
2250 return {};
2251 }
2252};
2253
2254class NilReceiverHandler final : public ExpressionHandler {
2255public:
2257
2258 Tracker::Result handle(const Expr *Inner, const ExplodedNode *InputNode,
2259 const ExplodedNode *LVNode,
2260 TrackingOptions Opts) override {
2261 // The message send could be nil due to the receiver being nil.
2262 // At this point in the path, the receiver should be live since we are at
2263 // the message send expr. If it is nil, start tracking it.
2264 if (const Expr *Receiver =
2266 return getParentTracker().track(Receiver, LVNode, Opts);
2267
2268 return {};
2269 }
2270};
2271
2272class ArrayIndexHandler final : public ExpressionHandler {
2273public:
2275
2276 Tracker::Result handle(const Expr *Inner, const ExplodedNode *InputNode,
2277 const ExplodedNode *LVNode,
2278 TrackingOptions Opts) override {
2279 // Track the index if this is an array subscript.
2280 if (const auto *Arr = dyn_cast<ArraySubscriptExpr>(Inner))
2281 return getParentTracker().track(
2282 Arr->getIdx(), LVNode,
2283 {Opts.Kind, /*EnableNullFPSuppression*/ false});
2284
2285 return {};
2286 }
2287};
2288
2289// TODO: extract it into more handlers
2290class InterestingLValueHandler final : public ExpressionHandler {
2291public:
2293
2294 Tracker::Result handle(const Expr *Inner, const ExplodedNode *InputNode,
2295 const ExplodedNode *LVNode,
2296 TrackingOptions Opts) override {
2297 ProgramStateRef LVState = LVNode->getState();
2298 const StackFrame *SF = LVNode->getStackFrame();
2299 PathSensitiveBugReport &Report = getParentTracker().getReport();
2300 Tracker::Result Result;
2301
2302 // See if the expression we're interested refers to a variable.
2303 // If so, we can track both its contents and constraints on its value.
2305 SVal LVal = LVNode->getSVal(Inner);
2306
2307 const MemRegion *RR = getLocationRegionIfReference(Inner, LVNode);
2308 bool LVIsNull = LVState->isNull(LVal).isConstrainedTrue();
2309
2310 // If this is a C++ reference to a null pointer, we are tracking the
2311 // pointer. In addition, we should find the store at which the reference
2312 // got initialized.
2313 if (RR && !LVIsNull)
2314 Result.combineWith(getParentTracker().track(LVal, RR, Opts, SF));
2315
2316 // In case of C++ references, we want to differentiate between a null
2317 // reference and reference to null pointer.
2318 // If the LVal is null, check if we are dealing with null reference.
2319 // For those, we want to track the location of the reference.
2320 const MemRegion *R =
2321 (RR && LVIsNull) ? RR : LVNode->getSVal(Inner).getAsRegion();
2322
2323 if (R) {
2324
2325 // Mark both the variable region and its contents as interesting.
2326 SVal V = LVState->getRawSVal(loc::MemRegionVal(R));
2327 Report.addVisitor<NoStoreFuncVisitor>(cast<SubRegion>(R), Opts.Kind);
2328
2329 // When we got here, we do have something to track, and we will
2330 // interrupt.
2331 Result.FoundSomethingToTrack = true;
2332 Result.WasInterrupted = true;
2333
2334 MacroNullReturnSuppressionVisitor::addMacroVisitorIfNecessary(
2335 LVNode, R, Opts.EnableNullFPSuppression, Report, V);
2336
2337 Report.markInteresting(V, Opts.Kind);
2338 Report.addVisitor<UndefOrNullArgVisitor>(R);
2339
2340 // If the contents are symbolic and null, find out when they became
2341 // null.
2342 if (V.getAsLocSymbol(/*IncludeBaseRegions=*/true))
2343 if (LVState->isNull(V).isConstrainedTrue())
2344 Report.addVisitor<TrackConstraintBRVisitor>(
2345 V.castAs<DefinedSVal>(),
2346 /*Assumption=*/false, "Assuming pointer value is null");
2347
2348 // Add visitor, which will suppress inline defensive checks.
2349 if (auto DV = V.getAs<DefinedSVal>())
2350 if (!DV->isZeroConstant() && Opts.EnableNullFPSuppression)
2351 // Note that LVNode may be too late (i.e., too far from the
2352 // InputNode) because the lvalue may have been computed before the
2353 // inlined call was evaluated. InputNode may as well be too early
2354 // here, because the symbol is already dead; this, however, is fine
2355 // because we can still find the node in which it collapsed to null
2356 // previously.
2357 Report.addVisitor<SuppressInlineDefensiveChecksVisitor>(*DV,
2358 InputNode);
2359 getParentTracker().track(V, R, Opts, SF);
2360 }
2361 }
2362
2363 return Result;
2364 }
2365};
2366
2367/// Adds a ReturnVisitor if the given statement represents a call that was
2368/// inlined.
2369///
2370/// This will search back through the ExplodedGraph, starting from the given
2371/// node, looking for when the given statement was processed. If it turns out
2372/// the statement is a call that was inlined, we add the visitor to the
2373/// bug report, so it can print a note later.
2374class InlinedFunctionCallHandler final : public ExpressionHandler {
2376
2377 Tracker::Result handle(const Expr *E, const ExplodedNode *InputNode,
2378 const ExplodedNode *ExprNode,
2379 TrackingOptions Opts) override {
2380 if (!CallEvent::isCallStmt(E))
2381 return {};
2382
2383 // First, find when we processed the statement.
2384 // If we work with a 'CXXNewExpr' that is going to be purged away before
2385 // its call take place. We would catch that purge in the last condition
2386 // as a 'StmtPoint' so we have to bypass it.
2387 const bool BypassCXXNewExprEval = isa<CXXNewExpr>(E);
2388
2389 // This is moving forward when we enter into another context.
2390 const StackFrame *CurrentSF = ExprNode->getStackFrame();
2391
2392 do {
2393 // If that is satisfied we found our statement as an inlined call.
2394 if (std::optional<CallExitEnd> CEE =
2395 ExprNode->getLocationAs<CallExitEnd>())
2396 if (CEE->getCalleeContext()->getCallSite() == E)
2397 break;
2398
2399 // Try to move forward to the end of the call-chain.
2400 ExprNode = ExprNode->getFirstPred();
2401 if (!ExprNode)
2402 break;
2403
2404 const StackFrame *PredSF = ExprNode->getStackFrame();
2405
2406 // If that is satisfied we found our statement.
2407 // FIXME: This code currently bypasses the call site for the
2408 // conservatively evaluated allocator.
2409 if (!BypassCXXNewExprEval)
2410 if (std::optional<StmtPoint> SP = ExprNode->getLocationAs<StmtPoint>())
2411 // See if we do not enter into another context.
2412 if (SP->getStmt() == E && CurrentSF == PredSF)
2413 break;
2414
2415 CurrentSF = PredSF;
2416 } while (ExprNode->getStackFrame() == CurrentSF);
2417
2418 // Next, step over any post-statement checks.
2419 while (ExprNode && ExprNode->getLocation().getAs<PostStmt>())
2420 ExprNode = ExprNode->getFirstPred();
2421 if (!ExprNode)
2422 return {};
2423
2424 // Finally, see if we inlined the call.
2425 std::optional<CallExitEnd> CEE = ExprNode->getLocationAs<CallExitEnd>();
2426 if (!CEE)
2427 return {};
2428
2429 const StackFrame *CalleeContext = CEE->getCalleeContext();
2430 if (CalleeContext->getCallSite() != E)
2431 return {};
2432
2433 // Check the return value.
2434 ProgramStateRef State = ExprNode->getState();
2435 SVal RetVal = ExprNode->getSVal(E);
2436
2437 // Handle cases where a reference is returned and then immediately used.
2438 if (cast<Expr>(E)->isGLValue())
2439 if (std::optional<Loc> LValue = RetVal.getAs<Loc>())
2440 RetVal = State->getSVal(*LValue);
2441
2442 // See if the return value is NULL. If so, suppress the report.
2443 AnalyzerOptions &Options = State->getAnalysisManager().options;
2444
2445 bool EnableNullFPSuppression = false;
2446 if (Opts.EnableNullFPSuppression && Options.ShouldSuppressNullReturnPaths)
2447 if (std::optional<Loc> RetLoc = RetVal.getAs<Loc>())
2448 EnableNullFPSuppression = State->isNull(*RetLoc).isConstrainedTrue();
2449
2450 PathSensitiveBugReport &Report = getParentTracker().getReport();
2451 Report.addVisitor<ReturnVisitor>(&getParentTracker(), CalleeContext,
2452 EnableNullFPSuppression, Options,
2453 Opts.Kind);
2454 return {true};
2455 }
2456};
2457
2458class DefaultExpressionHandler final : public ExpressionHandler {
2459public:
2461
2462 Tracker::Result handle(const Expr *Inner, const ExplodedNode *InputNode,
2463 const ExplodedNode *LVNode,
2464 TrackingOptions Opts) override {
2465 ProgramStateRef LVState = LVNode->getState();
2466 const StackFrame *SF = LVNode->getStackFrame();
2467 PathSensitiveBugReport &Report = getParentTracker().getReport();
2468 Tracker::Result Result;
2469
2470 // If the expression is not an "lvalue expression", we can still
2471 // track the constraints on its contents.
2472 SVal V = LVState->getSValAsScalarOrLoc(Inner, LVNode->getLocationContext());
2473
2474 // Is it a symbolic value?
2475 if (auto L = V.getAs<loc::MemRegionVal>()) {
2476 // FIXME: this is a hack for fixing a later crash when attempting to
2477 // dereference a void* pointer.
2478 // We should not try to dereference pointers at all when we don't care
2479 // what is written inside the pointer.
2480 bool CanDereference = true;
2481 if (const auto *SR = L->getRegionAs<SymbolicRegion>()) {
2482 if (SR->getPointeeStaticType()->isVoidType())
2483 CanDereference = false;
2484 } else if (L->getRegionAs<AllocaRegion>())
2485 CanDereference = false;
2486
2487 // At this point we are dealing with the region's LValue.
2488 // However, if the rvalue is a symbolic region, we should track it as
2489 // well. Try to use the correct type when looking up the value.
2490 SVal RVal;
2492 RVal = LVState->getRawSVal(*L, Inner->getType());
2493 else if (CanDereference)
2494 RVal = LVState->getSVal(L->getRegion());
2495
2496 if (CanDereference) {
2497 Report.addVisitor<UndefOrNullArgVisitor>(L->getRegion());
2498 Result.FoundSomethingToTrack = true;
2499
2500 if (!RVal.isUnknown())
2501 Result.combineWith(
2502 getParentTracker().track(RVal, L->getRegion(), Opts, SF));
2503 }
2504
2505 const MemRegion *RegionRVal = RVal.getAsRegion();
2506 if (isa_and_nonnull<SymbolicRegion>(RegionRVal)) {
2507 Report.markInteresting(RegionRVal, Opts.Kind);
2508 Report.addVisitor<TrackConstraintBRVisitor>(
2509 loc::MemRegionVal(RegionRVal),
2510 /*Assumption=*/false, "Assuming pointer value is null");
2511 Result.FoundSomethingToTrack = true;
2512 }
2513 }
2514
2515 return Result;
2516 }
2517};
2518
2519/// Attempts to add visitors to track an RValue expression back to its point of
2520/// origin.
2521class PRValueHandler final : public ExpressionHandler {
2522public:
2524
2525 Tracker::Result handle(const Expr *E, const ExplodedNode *InputNode,
2526 const ExplodedNode *ExprNode,
2527 TrackingOptions Opts) override {
2528 if (!E->isPRValue())
2529 return {};
2530
2531 const ExplodedNode *RVNode = findNodeForExpression(ExprNode, E);
2532 if (!RVNode)
2533 return {};
2534
2535 Tracker::Result CombinedResult;
2536 Tracker &Parent = getParentTracker();
2537
2538 const auto track = [&CombinedResult, &Parent, ExprNode,
2539 Opts](const Expr *Inner) {
2540 CombinedResult.combineWith(Parent.track(Inner, ExprNode, Opts));
2541 };
2542
2543 // FIXME: Initializer lists can appear in many different contexts
2544 // and most of them needs a special handling. For now let's handle
2545 // what we can. If the initializer list only has 1 element, we track
2546 // that.
2547 // This snippet even handles nesting, e.g.: int *x{{{{{y}}}}};
2548 if (const auto *ILE = dyn_cast<InitListExpr>(E)) {
2549 if (ILE->getNumInits() == 1) {
2550 track(ILE->getInit(0));
2551
2552 return CombinedResult;
2553 }
2554
2555 return {};
2556 }
2557
2558 ProgramStateRef RVState = RVNode->getState();
2559 SVal V = RVState->getSValAsScalarOrLoc(E, RVNode->getLocationContext());
2560 const auto *BO = dyn_cast<BinaryOperator>(E);
2561
2562 if (!BO || !BO->isMultiplicativeOp() || !V.isZeroConstant())
2563 return {};
2564
2565 SVal RHSV = RVState->getSVal(BO->getRHS(), RVNode->getLocationContext());
2566 SVal LHSV = RVState->getSVal(BO->getLHS(), RVNode->getLocationContext());
2567
2568 // Track both LHS and RHS of a multiplication.
2569 if (BO->getOpcode() == BO_Mul) {
2570 if (LHSV.isZeroConstant())
2571 track(BO->getLHS());
2572 if (RHSV.isZeroConstant())
2573 track(BO->getRHS());
2574 } else { // Track only the LHS of a division or a modulo.
2575 if (LHSV.isZeroConstant())
2576 track(BO->getLHS());
2577 }
2578
2579 return CombinedResult;
2580 }
2581};
2582} // namespace
2583
2596
2598 TrackingOptions Opts) {
2599 if (!E || !N)
2600 return {};
2601
2602 const Expr *Inner = peelOffOuterExpr(E, N);
2603 const ExplodedNode *LVNode = findNodeForExpression(N, Inner);
2604 if (!LVNode)
2605 return {};
2606
2607 Result CombinedResult;
2608 // Iterate through the handlers in the order according to their priorities.
2609 for (ExpressionHandlerPtr &Handler : ExpressionHandlers) {
2610 CombinedResult.combineWith(Handler->handle(Inner, N, LVNode, Opts));
2611 if (CombinedResult.WasInterrupted) {
2612 // There is no need to confuse our users here.
2613 // We got interrupted, but our users don't need to know about it.
2614 CombinedResult.WasInterrupted = false;
2615 break;
2616 }
2617 }
2618
2619 return CombinedResult;
2620}
2621
2623 const StackFrame *Origin) {
2624 if (!V.isUnknown()) {
2625 Report.addVisitor<StoreSiteFinder>(this, V, R, Opts, Origin);
2626 return {true};
2627 }
2628 return {};
2629}
2630
2632 TrackingOptions Opts) {
2633 // Iterate through the handlers in the order according to their priorities.
2634 for (StoreHandlerPtr &Handler : StoreHandlers) {
2635 if (PathDiagnosticPieceRef Result = Handler->handle(SI, BRC, Opts))
2636 // If the handler produced a non-null piece, return it.
2637 // There is no need in asking other handlers.
2638 return Result;
2639 }
2640 return {};
2641}
2642
2644 const Expr *E,
2645
2647 TrackingOptions Opts) {
2648 return Tracker::create(Report)
2649 ->track(E, InputNode, Opts)
2650 .FoundSomethingToTrack;
2651}
2652
2655 TrackingOptions Opts,
2656 const StackFrame *Origin) {
2657 Tracker::create(Report)->track(V, R, Opts, Origin);
2658}
2659
2660//===----------------------------------------------------------------------===//
2661// Implementation of NulReceiverBRVisitor.
2662//===----------------------------------------------------------------------===//
2663
2665 const ExplodedNode *N) {
2666 const auto *ME = dyn_cast<ObjCMessageExpr>(S);
2667 if (!ME)
2668 return nullptr;
2669 if (const Expr *Receiver = ME->getInstanceReceiver()) {
2670 ProgramStateRef state = N->getState();
2671 SVal V = N->getSVal(Receiver);
2672 if (state->isNull(V).isConstrainedTrue())
2673 return Receiver;
2674 }
2675 return nullptr;
2676}
2677
2681 std::optional<PreStmt> P = N->getLocationAs<PreStmt>();
2682 if (!P)
2683 return nullptr;
2684
2685 const Stmt *S = P->getStmt();
2686 const Expr *Receiver = getNilReceiver(S, N);
2687 if (!Receiver)
2688 return nullptr;
2689
2691 llvm::raw_svector_ostream OS(Buf);
2692
2693 if (const auto *ME = dyn_cast<ObjCMessageExpr>(S)) {
2694 OS << "'";
2695 ME->getSelector().print(OS);
2696 OS << "' not called";
2697 }
2698 else {
2699 OS << "No method is called";
2700 }
2701 OS << " because the receiver is nil";
2702
2703 // The receiver was nil, and hence the method was skipped.
2704 // Register a BugReporterVisitor to issue a message telling us how
2705 // the receiver was null.
2706 bugreporter::trackExpressionValue(N, Receiver, BR,
2708 /*EnableNullFPSuppression*/ false});
2709 // Issue a message saying that the method was skipped.
2710 PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
2711 N->getLocationContext());
2712 return std::make_shared<PathDiagnosticEventPiece>(L, OS.str());
2713}
2714
2715//===----------------------------------------------------------------------===//
2716// Visitor that tries to report interesting diagnostics from conditions.
2717//===----------------------------------------------------------------------===//
2718
2719/// Return the tag associated with this visitor. This tag will be used
2720/// to make all PathDiagnosticPieces created by this visitor.
2721const char *ConditionBRVisitor::getTag() { return "ConditionBRVisitor"; }
2722
2726 auto piece = VisitNodeImpl(N, BRC, BR);
2727 if (piece) {
2728 piece->setTag(getTag());
2729 if (auto *ev = dyn_cast<PathDiagnosticEventPiece>(piece.get()))
2730 ev->setPrunable(true, /* override */ false);
2731 }
2732 return piece;
2733}
2734
2737 BugReporterContext &BRC,
2739 ProgramPoint ProgPoint = N->getLocation();
2740 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &Tags =
2742
2743 // If an assumption was made on a branch, it should be caught
2744 // here by looking at the state transition.
2745 if (std::optional<BlockEdge> BE = ProgPoint.getAs<BlockEdge>()) {
2746 const CFGBlock *SrcBlock = BE->getSrc();
2747 if (const Stmt *Term = SrcBlock->getTerminatorStmt()) {
2748 // If the tag of the previous node is 'Eagerly Assume...' the current
2749 // 'BlockEdge' has the same constraint information. We do not want to
2750 // report the value as it is just an assumption on the predecessor node
2751 // which will be caught in the next VisitNode() iteration as a 'PostStmt'.
2752 const ProgramPointTag *PreviousNodeTag =
2754 if (PreviousNodeTag == Tags.first || PreviousNodeTag == Tags.second)
2755 return nullptr;
2756
2757 return VisitTerminator(Term, N, SrcBlock, BE->getDst(), BR, BRC);
2758 }
2759 return nullptr;
2760 }
2761
2762 if (std::optional<PostStmt> PS = ProgPoint.getAs<PostStmt>()) {
2763 const ProgramPointTag *CurrentNodeTag = PS->getTag();
2764 if (CurrentNodeTag != Tags.first && CurrentNodeTag != Tags.second)
2765 return nullptr;
2766
2767 bool TookTrue = CurrentNodeTag == Tags.first;
2768 return VisitTrueTest(cast<Expr>(PS->getStmt()), BRC, BR, N, TookTrue);
2769 }
2770
2771 return nullptr;
2772}
2773
2775 const Stmt *Term, const ExplodedNode *N, const CFGBlock *srcBlk,
2776 const CFGBlock *dstBlk, PathSensitiveBugReport &R,
2777 BugReporterContext &BRC) {
2778 const Expr *Cond = nullptr;
2779
2780 // In the code below, Term is a CFG terminator and Cond is a branch condition
2781 // expression upon which the decision is made on this terminator.
2782 //
2783 // For example, in "if (x == 0)", the "if (x == 0)" statement is a terminator,
2784 // and "x == 0" is the respective condition.
2785 //
2786 // Another example: in "if (x && y)", we've got two terminators and two
2787 // conditions due to short-circuit nature of operator "&&":
2788 // 1. The "if (x && y)" statement is a terminator,
2789 // and "y" is the respective condition.
2790 // 2. Also "x && ..." is another terminator,
2791 // and "x" is its condition.
2792
2793 switch (Term->getStmtClass()) {
2794 // FIXME: Stmt::SwitchStmtClass is worth handling, however it is a bit
2795 // more tricky because there are more than two branches to account for.
2796 default:
2797 return nullptr;
2798 case Stmt::IfStmtClass: {
2799 const auto *IfStatement = cast<IfStmt>(Term);
2800 // Handle if consteval which doesn't have a traditional condition.
2801 if (IfStatement->isConsteval())
2802 return nullptr;
2803 Cond = IfStatement->getCond();
2804 break;
2805 }
2806 case Stmt::ConditionalOperatorClass:
2807 Cond = cast<ConditionalOperator>(Term)->getCond();
2808 break;
2809 case Stmt::BinaryOperatorClass:
2810 // When we encounter a logical operator (&& or ||) as a CFG terminator,
2811 // then the condition is actually its LHS; otherwise, we'd encounter
2812 // the parent, such as if-statement, as a terminator.
2813 const auto *BO = cast<BinaryOperator>(Term);
2814 assert(BO->isLogicalOp() &&
2815 "CFG terminator is not a short-circuit operator!");
2816 Cond = BO->getLHS();
2817 break;
2818 }
2819
2820 Cond = Cond->IgnoreParens();
2821
2822 // However, when we encounter a logical operator as a branch condition,
2823 // then the condition is actually its RHS, because LHS would be
2824 // the condition for the logical operator terminator.
2825 while (const auto *InnerBO = dyn_cast<BinaryOperator>(Cond)) {
2826 if (!InnerBO->isLogicalOp())
2827 break;
2828 Cond = InnerBO->getRHS()->IgnoreParens();
2829 }
2830
2831 assert(Cond);
2832 assert(srcBlk->succ_size() == 2);
2833 const bool TookTrue = *(srcBlk->succ_begin()) == dstBlk;
2834 return VisitTrueTest(Cond, BRC, R, N, TookTrue);
2835}
2836
2840 const ExplodedNode *N, bool TookTrue) {
2841 ProgramStateRef CurrentState = N->getState();
2842 ProgramStateRef PrevState = N->getFirstPred()->getState();
2843 const LocationContext *LCtx = N->getLocationContext();
2844
2845 // If the constraint information is changed between the current and the
2846 // previous program state we assuming the newly seen constraint information.
2847 // If we cannot evaluate the condition (and the constraints are the same)
2848 // the analyzer has no information about the value and just assuming it.
2849 // FIXME: This logic is not entirely correct, because e.g. in code like
2850 // void f(unsigned arg) {
2851 // if (arg >= 0) {
2852 // // ...
2853 // }
2854 // }
2855 // it will say that the "arg >= 0" check is _assuming_ something new because
2856 // the constraint that "$arg >= 0" is 1 was added to the list of known
2857 // constraints. However, the unsigned value is always >= 0 so semantically
2858 // this is not a "real" assumption.
2859 bool IsAssuming =
2860 !BRC.getStateManager().haveEqualConstraints(CurrentState, PrevState) ||
2861 CurrentState->getSVal(Cond, LCtx).isUnknownOrUndef();
2862
2863 // These will be modified in code below, but we need to preserve the original
2864 // values in case we want to throw the generic message.
2865 const Expr *CondTmp = Cond;
2866 bool TookTrueTmp = TookTrue;
2867
2868 while (true) {
2869 CondTmp = CondTmp->IgnoreParenCasts();
2870 switch (CondTmp->getStmtClass()) {
2871 default:
2872 break;
2873 case Stmt::BinaryOperatorClass:
2874 if (auto P = VisitTrueTest(Cond, cast<BinaryOperator>(CondTmp),
2875 BRC, R, N, TookTrueTmp, IsAssuming))
2876 return P;
2877 break;
2878 case Stmt::DeclRefExprClass:
2879 if (auto P = VisitTrueTest(Cond, cast<DeclRefExpr>(CondTmp),
2880 BRC, R, N, TookTrueTmp, IsAssuming))
2881 return P;
2882 break;
2883 case Stmt::MemberExprClass:
2884 if (auto P = VisitTrueTest(Cond, cast<MemberExpr>(CondTmp),
2885 BRC, R, N, TookTrueTmp, IsAssuming))
2886 return P;
2887 break;
2888 case Stmt::UnaryOperatorClass: {
2889 const auto *UO = cast<UnaryOperator>(CondTmp);
2890 if (UO->getOpcode() == UO_LNot) {
2891 TookTrueTmp = !TookTrueTmp;
2892 CondTmp = UO->getSubExpr();
2893 continue;
2894 }
2895 break;
2896 }
2897 }
2898 break;
2899 }
2900
2901 // Condition too complex to explain? Just say something so that the user
2902 // knew we've made some path decision at this point.
2903 // If it is too complex and we know the evaluation of the condition do not
2904 // repeat the note from 'BugReporter.cpp'
2905 if (!IsAssuming)
2906 return nullptr;
2907
2909 if (!Loc.isValid() || !Loc.asLocation().isValid())
2910 return nullptr;
2911
2912 return std::make_shared<PathDiagnosticEventPiece>(
2913 Loc, TookTrue ? GenericTrueMessage : GenericFalseMessage);
2914}
2915
2916bool ConditionBRVisitor::patternMatch(const Expr *Ex, const Expr *ParentEx,
2917 raw_ostream &Out, BugReporterContext &BRC,
2918 PathSensitiveBugReport &report,
2919 const ExplodedNode *N,
2920 std::optional<bool> &prunable,
2921 bool IsSameFieldName) {
2922 const Expr *OriginalExpr = Ex;
2923 Ex = Ex->IgnoreParenCasts();
2924
2926 FloatingLiteral>(Ex)) {
2927 // Use heuristics to determine if the expression is a macro
2928 // expanding to a literal and if so, use the macro's name.
2929 SourceLocation BeginLoc = OriginalExpr->getBeginLoc();
2930 SourceLocation EndLoc = OriginalExpr->getEndLoc();
2931 if (BeginLoc.isMacroID() && EndLoc.isMacroID()) {
2932 const SourceManager &SM = BRC.getSourceManager();
2933 const LangOptions &LO = BRC.getASTContext().getLangOpts();
2934 if (Lexer::isAtStartOfMacroExpansion(BeginLoc, SM, LO) &&
2935 Lexer::isAtEndOfMacroExpansion(EndLoc, SM, LO)) {
2936 CharSourceRange R = Lexer::getAsCharRange({BeginLoc, EndLoc}, SM, LO);
2937 Out << Lexer::getSourceText(R, SM, LO);
2938 return false;
2939 }
2940 }
2941 }
2942
2943 if (const auto *DR = dyn_cast<DeclRefExpr>(Ex)) {
2944 const bool quotes = isa<VarDecl>(DR->getDecl());
2945 if (quotes) {
2946 Out << '\'';
2947 const LocationContext *LCtx = N->getLocationContext();
2948 const ProgramState *state = N->getState().get();
2949 if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
2950 LCtx).getAsRegion()) {
2951 if (report.isInteresting(R))
2952 prunable = false;
2953 else {
2954 const ProgramState *state = N->getState().get();
2955 SVal V = state->getSVal(R);
2956 if (report.isInteresting(V))
2957 prunable = false;
2958 }
2959 }
2960 }
2961 Out << DR->getDecl()->getDeclName().getAsString();
2962 if (quotes)
2963 Out << '\'';
2964 return quotes;
2965 }
2966
2967 if (const auto *IL = dyn_cast<IntegerLiteral>(Ex)) {
2968 QualType OriginalTy = OriginalExpr->getType();
2969 if (OriginalTy->isPointerType()) {
2970 if (IL->getValue() == 0) {
2971 Out << "null";
2972 return false;
2973 }
2974 }
2975 else if (OriginalTy->isObjCObjectPointerType()) {
2976 if (IL->getValue() == 0) {
2977 Out << "nil";
2978 return false;
2979 }
2980 }
2981
2982 Out << IL->getValue();
2983 return false;
2984 }
2985
2986 if (const auto *ME = dyn_cast<MemberExpr>(Ex)) {
2987 if (!IsSameFieldName)
2988 Out << "field '" << ME->getMemberDecl()->getName() << '\'';
2989 else
2990 Out << '\''
2994 nullptr)
2995 << '\'';
2996 }
2997
2998 return false;
2999}
3000
3002 const Expr *Cond, const BinaryOperator *BExpr, BugReporterContext &BRC,
3003 PathSensitiveBugReport &R, const ExplodedNode *N, bool TookTrue,
3004 bool IsAssuming) {
3005 bool shouldInvert = false;
3006 std::optional<bool> shouldPrune;
3007
3008 // Check if the field name of the MemberExprs is ambiguous. Example:
3009 // " 'a.d' is equal to 'h.d' " in 'test/Analysis/null-deref-path-notes.cpp'.
3010 bool IsSameFieldName = false;
3011 const auto *LhsME = dyn_cast<MemberExpr>(BExpr->getLHS()->IgnoreParenCasts());
3012 const auto *RhsME = dyn_cast<MemberExpr>(BExpr->getRHS()->IgnoreParenCasts());
3013
3014 if (LhsME && RhsME)
3015 IsSameFieldName =
3016 LhsME->getMemberDecl()->getName() == RhsME->getMemberDecl()->getName();
3017
3018 SmallString<128> LhsString, RhsString;
3019 {
3020 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
3021 const bool isVarLHS = patternMatch(BExpr->getLHS(), BExpr, OutLHS, BRC, R,
3022 N, shouldPrune, IsSameFieldName);
3023 const bool isVarRHS = patternMatch(BExpr->getRHS(), BExpr, OutRHS, BRC, R,
3024 N, shouldPrune, IsSameFieldName);
3025
3026 shouldInvert = !isVarLHS && isVarRHS;
3027 }
3028
3029 BinaryOperator::Opcode Op = BExpr->getOpcode();
3030
3032 // For assignment operators, all that we care about is that the LHS
3033 // evaluates to "true" or "false".
3034 return VisitConditionVariable(LhsString, BExpr->getLHS(), BRC, R, N,
3035 TookTrue);
3036 }
3037
3038 // For non-assignment operations, we require that we can understand
3039 // both the LHS and RHS.
3040 if (LhsString.empty() || RhsString.empty() ||
3041 !BinaryOperator::isComparisonOp(Op) || Op == BO_Cmp)
3042 return nullptr;
3043
3044 // Should we invert the strings if the LHS is not a variable name?
3045 SmallString<256> buf;
3046 llvm::raw_svector_ostream Out(buf);
3047 Out << (IsAssuming ? "Assuming " : "")
3048 << (shouldInvert ? RhsString : LhsString) << " is ";
3049
3050 // Do we need to invert the opcode?
3051 if (shouldInvert)
3052 switch (Op) {
3053 default: break;
3054 case BO_LT: Op = BO_GT; break;
3055 case BO_GT: Op = BO_LT; break;
3056 case BO_LE: Op = BO_GE; break;
3057 case BO_GE: Op = BO_LE; break;
3058 }
3059
3060 if (!TookTrue)
3061 switch (Op) {
3062 case BO_EQ: Op = BO_NE; break;
3063 case BO_NE: Op = BO_EQ; break;
3064 case BO_LT: Op = BO_GE; break;
3065 case BO_GT: Op = BO_LE; break;
3066 case BO_LE: Op = BO_GT; break;
3067 case BO_GE: Op = BO_LT; break;
3068 default:
3069 return nullptr;
3070 }
3071
3072 switch (Op) {
3073 case BO_EQ:
3074 Out << "equal to ";
3075 break;
3076 case BO_NE:
3077 Out << "not equal to ";
3078 break;
3079 default:
3080 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
3081 break;
3082 }
3083
3084 Out << (shouldInvert ? LhsString : RhsString);
3085 const LocationContext *LCtx = N->getLocationContext();
3086 const SourceManager &SM = BRC.getSourceManager();
3087
3088 if (isVarAnInterestingCondition(BExpr->getLHS(), N, &R) ||
3089 isVarAnInterestingCondition(BExpr->getRHS(), N, &R))
3091
3092 // Convert 'field ...' to 'Field ...' if it is a MemberExpr.
3093 std::string Message = std::string(Out.str());
3094 Message[0] = toupper(Message[0]);
3095
3096 // If we know the value create a pop-up note to the value part of 'BExpr'.
3097 if (!IsAssuming) {
3099 if (!shouldInvert) {
3100 if (LhsME && LhsME->getMemberLoc().isValid())
3101 Loc = PathDiagnosticLocation(LhsME->getMemberLoc(), SM);
3102 else
3103 Loc = PathDiagnosticLocation(BExpr->getLHS(), SM, LCtx);
3104 } else {
3105 if (RhsME && RhsME->getMemberLoc().isValid())
3106 Loc = PathDiagnosticLocation(RhsME->getMemberLoc(), SM);
3107 else
3108 Loc = PathDiagnosticLocation(BExpr->getRHS(), SM, LCtx);
3109 }
3110
3111 return std::make_shared<PathDiagnosticPopUpPiece>(Loc, Message);
3112 }
3113
3115 auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Message);
3116 if (shouldPrune)
3117 event->setPrunable(*shouldPrune);
3118 return event;
3119}
3120
3122 StringRef LhsString, const Expr *CondVarExpr, BugReporterContext &BRC,
3123 PathSensitiveBugReport &report, const ExplodedNode *N, bool TookTrue) {
3124 // FIXME: If there's already a constraint tracker for this variable,
3125 // we shouldn't emit anything here (c.f. the double note in
3126 // test/Analysis/inlining/path-notes.c)
3127 SmallString<256> buf;
3128 llvm::raw_svector_ostream Out(buf);
3129 Out << "Assuming " << LhsString << " is ";
3130
3131 if (!printValue(CondVarExpr, Out, N, TookTrue, /*IsAssuming=*/true))
3132 return nullptr;
3133
3134 const LocationContext *LCtx = N->getLocationContext();
3135 PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
3136
3137 if (isVarAnInterestingCondition(CondVarExpr, N, &report))
3139
3140 auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
3141
3142 if (isInterestingExpr(CondVarExpr, N, &report))
3143 event->setPrunable(false);
3144
3145 return event;
3146}
3147
3149 const Expr *Cond, const DeclRefExpr *DRE, BugReporterContext &BRC,
3150 PathSensitiveBugReport &report, const ExplodedNode *N, bool TookTrue,
3151 bool IsAssuming) {
3152 const auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
3153 if (!VD)
3154 return nullptr;
3155
3156 SmallString<256> Buf;
3157 llvm::raw_svector_ostream Out(Buf);
3158
3159 Out << (IsAssuming ? "Assuming '" : "'") << VD->getDeclName() << "' is ";
3160
3161 if (!printValue(DRE, Out, N, TookTrue, IsAssuming))
3162 return nullptr;
3163
3164 const LocationContext *LCtx = N->getLocationContext();
3165
3166 if (isVarAnInterestingCondition(DRE, N, &report))
3168
3169 // If we know the value create a pop-up note to the 'DRE'.
3170 if (!IsAssuming) {
3172 return std::make_shared<PathDiagnosticPopUpPiece>(Loc, Out.str());
3173 }
3174
3176 auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
3177
3178 if (isInterestingExpr(DRE, N, &report))
3179 event->setPrunable(false);
3180
3181 return std::move(event);
3182}
3183
3185 const Expr *Cond, const MemberExpr *ME, BugReporterContext &BRC,
3186 PathSensitiveBugReport &report, const ExplodedNode *N, bool TookTrue,
3187 bool IsAssuming) {
3188 SmallString<256> Buf;
3189 llvm::raw_svector_ostream Out(Buf);
3190
3191 Out << (IsAssuming ? "Assuming field '" : "Field '")
3192 << ME->getMemberDecl()->getName() << "' is ";
3193
3194 if (!printValue(ME, Out, N, TookTrue, IsAssuming))
3195 return nullptr;
3196
3197 const LocationContext *LCtx = N->getLocationContext();
3199
3200 // If we know the value create a pop-up note to the member of the MemberExpr.
3201 if (!IsAssuming && ME->getMemberLoc().isValid())
3203 else
3205
3206 if (!Loc.isValid() || !Loc.asLocation().isValid())
3207 return nullptr;
3208
3209 if (isVarAnInterestingCondition(ME, N, &report))
3211
3212 // If we know the value create a pop-up note.
3213 if (!IsAssuming)
3214 return std::make_shared<PathDiagnosticPopUpPiece>(Loc, Out.str());
3215
3216 auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
3217 if (isInterestingExpr(ME, N, &report))
3218 event->setPrunable(false);
3219 return event;
3220}
3221
3222bool ConditionBRVisitor::printValue(const Expr *CondVarExpr, raw_ostream &Out,
3223 const ExplodedNode *N, bool TookTrue,
3224 bool IsAssuming) {
3225 QualType Ty = CondVarExpr->getType();
3226
3227 if (Ty->isPointerType()) {
3228 Out << (TookTrue ? "non-null" : "null");
3229 return true;
3230 }
3231
3232 if (Ty->isObjCObjectPointerType()) {
3233 Out << (TookTrue ? "non-nil" : "nil");
3234 return true;
3235 }
3236
3237 if (!Ty->isIntegralOrEnumerationType())
3238 return false;
3239
3240 std::optional<const llvm::APSInt *> IntValue;
3241 if (!IsAssuming)
3242 IntValue = getConcreteIntegerValue(CondVarExpr, N);
3243
3244 if (IsAssuming || !IntValue) {
3245 if (Ty->isBooleanType())
3246 Out << (TookTrue ? "true" : "false");
3247 else
3248 Out << (TookTrue ? "not equal to 0" : "0");
3249 } else {
3250 if (Ty->isBooleanType())
3251 Out << ((*IntValue)->getBoolValue() ? "true" : "false");
3252 else
3253 Out << **IntValue;
3254 }
3255
3256 return true;
3257}
3258
3260 const PathDiagnosticPiece *Piece) {
3261 return Piece->getString() == GenericTrueMessage ||
3262 Piece->getString() == GenericFalseMessage;
3263}
3264
3265//===----------------------------------------------------------------------===//
3266// Implementation of LikelyFalsePositiveSuppressionBRVisitor.
3267//===----------------------------------------------------------------------===//
3268
3270 BugReporterContext &BRC, const ExplodedNode *N,
3272 // Here we suppress false positives coming from system headers. This list is
3273 // based on known issues.
3274 const AnalyzerOptions &Options = BRC.getAnalyzerOptions();
3275 const Decl *D = N->getLocationContext()->getDecl();
3276
3278 // Skip reports within the 'std' namespace. Although these can sometimes be
3279 // the user's fault, we currently don't report them very well, and
3280 // Note that this will not help for any other data structure libraries, like
3281 // TR1, Boost, or llvm/ADT.
3282 if (Options.ShouldSuppressFromCXXStandardLibrary) {
3283 BR.markInvalid(getTag(), nullptr);
3284 return;
3285 } else {
3286 // If the complete 'std' suppression is not enabled, suppress reports
3287 // from the 'std' namespace that are known to produce false positives.
3288
3289 // The analyzer issues a false use-after-free when std::list::pop_front
3290 // or std::list::pop_back are called multiple times because we cannot
3291 // reason about the internal invariants of the data structure.
3292 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
3293 const CXXRecordDecl *CD = MD->getParent();
3294 if (CD->getName() == "list") {
3295 BR.markInvalid(getTag(), nullptr);
3296 return;
3297 }
3298 }
3299
3300 // The analyzer issues a false positive when the constructor of
3301 // std::__independent_bits_engine from algorithms is used.
3302 if (const auto *MD = dyn_cast<CXXConstructorDecl>(D)) {
3303 const CXXRecordDecl *CD = MD->getParent();
3304 if (CD->getName() == "__independent_bits_engine") {
3305 BR.markInvalid(getTag(), nullptr);
3306 return;
3307 }
3308 }
3309
3310 for (const LocationContext *LCtx = N->getLocationContext(); LCtx;
3311 LCtx = LCtx->getParent()) {
3312 const auto *MD = dyn_cast<CXXMethodDecl>(LCtx->getDecl());
3313 if (!MD)
3314 continue;
3315
3316 const CXXRecordDecl *CD = MD->getParent();
3317 // The analyzer issues a false positive on
3318 // std::basic_string<uint8_t> v; v.push_back(1);
3319 // and
3320 // std::u16string s; s += u'a';
3321 // because we cannot reason about the internal invariants of the
3322 // data structure.
3323 if (CD->getName() == "basic_string") {
3324 BR.markInvalid(getTag(), nullptr);
3325 return;
3326 }
3327
3328 // The analyzer issues a false positive on
3329 // std::shared_ptr<int> p(new int(1)); p = nullptr;
3330 // because it does not reason properly about temporary destructors.
3331 if (CD->getName() == "shared_ptr") {
3332 BR.markInvalid(getTag(), nullptr);
3333 return;
3334 }
3335 }
3336 }
3337 }
3338
3339 // Skip reports within the sys/queue.h macros as we do not have the ability to
3340 // reason about data structure shapes.
3341 const SourceManager &SM = BRC.getSourceManager();
3343 while (Loc.isMacroID()) {
3344 Loc = Loc.getSpellingLoc();
3345 if (SM.getFilename(Loc).ends_with("sys/queue.h")) {
3346 BR.markInvalid(getTag(), nullptr);
3347 return;
3348 }
3349 }
3350}
3351
3352//===----------------------------------------------------------------------===//
3353// Implementation of UndefOrNullArgVisitor.
3354//===----------------------------------------------------------------------===//
3355
3359 ProgramStateRef State = N->getState();
3360 ProgramPoint ProgLoc = N->getLocation();
3361
3362 // We are only interested in visiting CallEnter nodes.
3363 std::optional<CallEnter> CEnter = ProgLoc.getAs<CallEnter>();
3364 if (!CEnter)
3365 return nullptr;
3366
3367 // Check if one of the arguments is the region the visitor is tracking.
3369 CallEventRef<> Call = CEMgr.getCaller(CEnter->getCalleeContext(), State);
3370 unsigned Idx = 0;
3371 ArrayRef<ParmVarDecl *> parms = Call->parameters();
3372
3373 for (const auto ParamDecl : parms) {
3374 const MemRegion *ArgReg = Call->getArgSVal(Idx).getAsRegion();
3375 ++Idx;
3376
3377 // Are we tracking the argument or its subregion?
3378 if ( !ArgReg || !R->isSubRegionOf(ArgReg->StripCasts()))
3379 continue;
3380
3381 // Check the function parameter type.
3382 assert(ParamDecl && "Formal parameter has no decl?");
3383 QualType T = ParamDecl->getType();
3384
3385 if (!(T->isAnyPointerType() || T->isReferenceType())) {
3386 // Function can only change the value passed in by address.
3387 continue;
3388 }
3389
3390 // If it is a const pointer value, the function does not intend to
3391 // change the value.
3392 if (T->getPointeeType().isConstQualified())
3393 continue;
3394
3395 // Mark the call site (LocationContext) as interesting if the value of the
3396 // argument is undefined or '0'/'NULL'.
3397 SVal BoundVal = State->getSVal(R);
3398 if (BoundVal.isUndef() || BoundVal.isZeroConstant()) {
3399 BR.markInteresting(CEnter->getCalleeContext());
3400 return nullptr;
3401 }
3402 }
3403 return nullptr;
3404}
3405
3406//===----------------------------------------------------------------------===//
3407// Implementation of TagVisitor.
3408//===----------------------------------------------------------------------===//
3409
3410int NoteTag::Kind = 0;
3411
3412void TagVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
3413 static int Tag = 0;
3414 ID.AddPointer(&Tag);
3415}
3416
3418 BugReporterContext &BRC,
3420 ProgramPoint PP = N->getLocation();
3421 const NoteTag *T = dyn_cast_or_null<NoteTag>(PP.getTag());
3422 if (!T)
3423 return nullptr;
3424
3425 if (std::optional<std::string> Msg = T->generateMessage(BRC, R)) {
3428 auto Piece = std::make_shared<PathDiagnosticEventPiece>(Loc, *Msg);
3429 Piece->setPrunable(T->isPrunable());
3430 return Piece;
3431 }
3432
3433 return nullptr;
3434}
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 bool isInterestingExpr(const Expr *E, const ExplodedNode *N, const PathSensitiveBugReport *B)
static const ExplodedNode * findNodeForExpression(const ExplodedNode *N, const Expr *Inner)
Find the ExplodedNode where the lvalue (the value of 'Ex') was computed.
static void showBRParamDiagnostics(llvm::raw_svector_ostream &OS, StoreInfo SI)
Display diagnostics for passing bad region as a parameter.
static const Expr * peelOffPointerArithmetic(const BinaryOperator *B)
static const Expr * tryExtractInitializerFromList(const InitListExpr *ILE, const MemRegion *R)
static bool wasRegionOfInterestModifiedAt(const SubRegion *RegionOfInterest, const ExplodedNode *N, SVal ValueAfter)
static llvm::StringLiteral WillBeUsedForACondition
static bool isFunctionMacroExpansion(SourceLocation Loc, const SourceManager &SM)
static std::shared_ptr< PathDiagnosticEventPiece > constructDebugPieceForTrackedCondition(const Expr *Cond, const ExplodedNode *N, BugReporterContext &BRC)
static const MemRegion * getLocationRegionIfReference(const Expr *E, const ExplodedNode *N, bool LookingForReference=true)
static bool hasVisibleUpdate(const ExplodedNode *LeftNode, SVal LeftVal, const ExplodedNode *RightNode, SVal RightVal)
Comparing internal representations of symbolic values (via SVal::operator==()) is a valid way to chec...
static bool potentiallyWritesIntoIvar(const Decl *Parent, const ObjCIvarDecl *Ivar)
static std::optional< const llvm::APSInt * > getConcreteIntegerValue(const Expr *CondVarExpr, const ExplodedNode *N)
static bool isVarAnInterestingCondition(const Expr *CondVarExpr, const ExplodedNode *N, const PathSensitiveBugReport *B)
static void showBRDefaultDiagnostics(llvm::raw_svector_ostream &OS, StoreInfo SI)
Show default diagnostics for storing bad region.
static std::optional< SVal > getSValForVar(const Expr *CondVarExpr, const ExplodedNode *N)
static const Expr * peelOffOuterExpr(const Expr *Ex, const ExplodedNode *N)
static const VarDecl * getVarDeclForExpression(const Expr *E)
static bool isTrivialCopyOrMoveCtor(const CXXConstructExpr *CE)
static StringRef getMacroName(SourceLocation Loc, BugReporterContext &BRC)
static bool isObjCPointer(const MemRegion *R)
static bool isAssertlikeBlock(const CFGBlock *B, ASTContext &Context)
static bool isInitializationOfVar(const ExplodedNode *N, const VarRegion *VR)
Returns true if N represents the DeclStmt declaring and initializing VR.
static const ExplodedNode * getMatchingCallExitEnd(const ExplodedNode *N)
static void showBRDiagnostics(llvm::raw_svector_ostream &OS, StoreInfo SI)
Show diagnostics for initializing or declaring a region R with a bad value.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
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.
#define SM(sm)
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
C Language Family Type Representation.
static bool isPointerToConst(const QualType &QT)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:227
const LangOptions & getLangOpts() const
Definition ASTContext.h:959
static bool isInStdNamespace(const Decl *D)
Stores options for the analyzer from the command line.
AnalysisDiagClients AnalysisDiagOpt
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4041
Expr * getLHS() const
Definition Expr.h:4091
static bool isComparisonOp(Opcode Opc)
Definition Expr.h:4141
StringRef getOpcodeStr() const
Definition Expr.h:4107
Expr * getRHS() const
Definition Expr.h:4093
static bool isAdditiveOp(Opcode Opc)
Definition Expr.h:4127
static bool isAssignmentOp(Opcode Opc)
Definition Expr.h:4177
Opcode getOpcode() const
Definition Expr.h:4086
BinaryOperatorKind Opcode
Definition Expr.h:4046
Represents a single basic block in a source-level CFG.
Definition CFG.h:632
bool isInevitablySinking() const
Returns true if the block would eventually end with a sink (a noreturn node).
Definition CFG.cpp:6450
succ_iterator succ_begin()
Definition CFG.h:1017
Stmt * getTerminatorStmt()
Definition CFG.h:1114
const Stmt * getTerminatorCondition(bool StripParens=true) const
Definition CFG.cpp:6515
const Expr * getLastCondition() const
Definition CFG.cpp:6487
unsigned succ_size() const
Definition CFG.h:1035
const CFGBlock * getBlock(const Stmt *S) const
Returns the CFGBlock the specified Stmt* appears in.
unsigned size() const
Return the total number of CFGBlocks within the CFG This is simply a renaming of the getNumBlockIDs()...
Definition CFG.h:1448
bool isLinear() const
Returns true if the CFG has no branches.
Definition CFG.cpp:5460
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition ExprCXX.h:727
Represents a call to a C++ constructor.
Definition ExprCXX.h:1552
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition ExprCXX.h:1615
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition DeclCXX.cpp:3071
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Represents a point when we begin processing an inlined call.
Represents a point when we start the call exit sequence (for inlined call).
Represents a point when we finish the call exit sequence (for inlined call).
Represents a byte-granular source range.
static CharSourceRange getTokenRange(SourceRange R)
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2122
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1273
ValueDecl * getDecl()
Definition Expr.h:1341
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1637
const Decl * getSingleDecl() const
Definition Stmt.h:1652
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:547
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition DeclBase.h:1100
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
Definition DeclBase.h:1106
bool isLocalExternDecl() const
Determine whether this is a block-scope declaration with linkage.
Definition DeclBase.h:1182
This represents one expression.
Definition Expr.h:112
bool isGLValue() const
Definition Expr.h:287
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition Expr.cpp:3095
bool isPRValue() const
Definition Expr.h:285
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3070
QualType getType() const
Definition Expr.h:144
A SourceLocation and its associated SourceManager.
GNUNullExpr - Implements the GNU __null extension, which is a name for a null pointer constant that h...
Definition Expr.h:4926
Describes an C or C++ initializer list.
Definition Expr.h:5302
unsigned getNumInits() const
Definition Expr.h:5332
const Expr * getInit(unsigned Init) const
Definition Expr.h:5354
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
static StringRef getSourceText(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts, bool *Invalid=nullptr)
Returns a string for the source that the range encompasses.
Definition Lexer.cpp:1052
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition Lexer.cpp:1088
static CharSourceRange getAsCharRange(SourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Given a token range, produce a corresponding CharSourceRange that is not a token range.
Definition Lexer.h:430
static bool isAtStartOfMacroExpansion(SourceLocation loc, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation *MacroBegin=nullptr)
Returns true if the given MacroID location points at the first token of the macro expansion.
Definition Lexer.cpp:889
static bool isAtEndOfMacroExpansion(SourceLocation loc, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation *MacroEnd=nullptr)
Returns true if the given MacroID location points at the last token of the macro expansion.
Definition Lexer.cpp:911
bool isParentOf(const LocationContext *LC) const
const Decl * getDecl() const
LLVM_ATTRIBUTE_RETURNS_NONNULL AnalysisDeclContext * getAnalysisDeclContext() const
const LocationContext * getParent() const
It might return null.
const StackFrame * getStackFrame() const
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3367
SourceLocation getMemberLoc() const
getMemberLoc - Return the location of the "member", in X->F, it is the location of 'F'.
Definition Expr.h:3556
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3450
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition Decl.h:317
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition ExprObjC.h:119
ObjCIvarDecl - Represents an ObjC instance variable.
Definition DeclObjC.h:1952
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition ExprObjC.h:580
Represents a program point after a store evaluation.
ProgramPoints can be "tagged" as representing points specific to a given analysis entity.
const ProgramPointTag * getTag() const
std::optional< T > getAs() const
Convert to the specified ProgramPoint type, returning std::nullopt if this ProgramPoint is not of the...
const LocationContext * getLocationContext() const
A (possibly-)qualified type.
Definition TypeBase.h:937
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:8488
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8509
field_range fields() const
Definition Decl.h:4546
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
Each ExpansionInfo encodes the expansion location - where the token was ultimately expanded,...
This is a discriminated union of FileInfo and ExpansionInfo.
const ExpansionInfo & getExpansion() const
It represents a stack frame of the call stack (based on CallEvent).
bool inTopFrame() const override
const Expr * getCallSite() const
const Stmt * getStmt() const
Stmt - This represents one statement.
Definition Stmt.h:86
SourceLocation getEndLoc() const LLVM_READONLY
Definition Stmt.cpp:367
StmtClass getStmtClass() const
Definition Stmt.h:1499
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
bool isVoidType() const
Definition TypeBase.h:9039
bool isBooleanType() const
Definition TypeBase.h:9176
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isPointerType() const
Definition TypeBase.h:8673
bool isReferenceType() const
Definition TypeBase.h:8697
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition TypeBase.h:9161
bool isObjCObjectPointerType() const
Definition TypeBase.h:8852
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9266
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
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1206
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition Decl.h:1182
Maps string IDs to AST nodes matched by parts of a matcher.
A safe wrapper around APSInt objects allocated and owned by BasicValueFactory.
Definition APSIntPtr.h:19
StringRef getDescription() const
A verbose warning message that is appropriate for displaying next to the source code that introduces ...
ASTContext & getASTContext() const
ProgramStateManager & getStateManager() const
const SourceManager & getSourceManager() const
const AnalyzerOptions & getAnalyzerOptions() const
BugReporterVisitors are used to add custom diagnostics along a path.
static PathDiagnosticPieceRef getDefaultEndPath(const BugReporterContext &BRC, const ExplodedNode *N, const PathSensitiveBugReport &BR)
Generates the default final diagnostic piece.
virtual PathDiagnosticPieceRef getEndPath(BugReporterContext &BRC, const ExplodedNode *N, PathSensitiveBugReport &BR)
Provide custom definition for the final diagnostic piece on the path - the piece, which is displayed ...
virtual void finalizeVisitor(BugReporterContext &BRC, const ExplodedNode *EndPathNode, PathSensitiveBugReport &BR)
Last function called on the visitor, no further calls to VisitNode would follow.
Represents a call to a C++ constructor.
Definition CallEvent.h:997
Manages the lifetime of CallEvent objects.
Definition CallEvent.h:1374
CallEventRef getCaller(const StackFrame *CalleeSF, ProgramStateRef State)
Gets an outside caller given a callee context.
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:153
static bool isCallStmt(const Stmt *S)
Returns true if this is a statement is a function or method call of some kind.
PathDiagnosticPieceRef VisitTerminator(const Stmt *Term, const ExplodedNode *N, const CFGBlock *SrcBlk, const CFGBlock *DstBlk, PathSensitiveBugReport &R, BugReporterContext &BRC)
bool printValue(const Expr *CondVarExpr, raw_ostream &Out, const ExplodedNode *N, bool TookTrue, bool IsAssuming)
Tries to print the value of the given expression.
PathDiagnosticPieceRef VisitNode(const ExplodedNode *N, BugReporterContext &BRC, PathSensitiveBugReport &BR) override
Return a diagnostic piece which should be associated with the given node.
bool patternMatch(const Expr *Ex, const Expr *ParentEx, raw_ostream &Out, BugReporterContext &BRC, PathSensitiveBugReport &R, const ExplodedNode *N, std::optional< bool > &prunable, bool IsSameFieldName)
static bool isPieceMessageGeneric(const PathDiagnosticPiece *Piece)
PathDiagnosticPieceRef VisitConditionVariable(StringRef LhsString, const Expr *CondVarExpr, BugReporterContext &BRC, PathSensitiveBugReport &R, const ExplodedNode *N, bool TookTrue)
PathDiagnosticPieceRef VisitTrueTest(const Expr *Cond, BugReporterContext &BRC, PathSensitiveBugReport &R, const ExplodedNode *N, bool TookTrue)
static const char * getTag()
Return the tag associated with this visitor.
PathDiagnosticPieceRef VisitNodeImpl(const ExplodedNode *N, BugReporterContext &BRC, PathSensitiveBugReport &BR)
bool isConstrainedTrue() const
Return true if the constraint is perfectly constrained to 'true'.
bool isValid() const =delete
static bool isInterestingLValueExpr(const Expr *Ex)
Returns true if nodes for the given expression kind are always kept around.
const CFGBlock * getCFGBlock() const
const ProgramStateRef & getState() const
SVal getSVal(const Expr *E) const
Get the value of an arbitrary expression at this node.
const Stmt * getStmtForDiagnostics() const
If the node's program point corresponds to a statement, retrieve that statement.
ProgramPoint getLocation() const
getLocation - Returns the edge associated with the given node.
ExplodedNode * getFirstSucc()
const LocationContext * getLocationContext() const
std::optional< T > getLocationAs() const &
ExplodedNode * getFirstPred()
unsigned succ_size() const
const StackFrame * getStackFrame() const
static std::pair< const ProgramPointTag *, const ProgramPointTag * > getEagerlyAssumeBifurcationTags()
LLVM_ATTRIBUTE_RETURNS_NONNULL const FieldDecl * getDecl() const override
Definition MemRegion.h:1158
void finalizeVisitor(BugReporterContext &BRC, const ExplodedNode *N, PathSensitiveBugReport &BR) override
Last function called on the visitor, no further calls to VisitNode would follow.
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:98
const MemSpace * getMemorySpaceAs(ProgramStateRef State) const
Definition MemRegion.h:143
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * StripCasts(bool StripBaseAndDerivedCasts=true) const
virtual bool isSubRegionOf(const MemRegion *R) const
Check if the region is a subregion of the given region.
virtual void printPretty(raw_ostream &os) const
Print the region for use in diagnostics.
virtual bool canPrintPretty() const
Returns true if this region can be printed in a user-friendly way.
PathDiagnosticPieceRef VisitNode(const ExplodedNode *N, BugReporterContext &BRC, PathSensitiveBugReport &BR) override
Return a diagnostic piece which should be associated with the given node.
static const Expr * getNilReceiver(const Stmt *S, const ExplodedNode *N)
If the statement is a message send expression with nil receiver, returns the receiver expression.
virtual bool wasModifiedBeforeCallExit(const ExplodedNode *CurrN, const ExplodedNode *CallExitBeginN)
virtual PathDiagnosticPieceRef maybeEmitNoteForObjCSelf(PathSensitiveBugReport &R, const ObjCMethodCall &Call, const ExplodedNode *N)=0
Consume the information on the non-modifying stack frame in order to either emit a note or not.
virtual PathDiagnosticPieceRef maybeEmitNoteForCXXThis(PathSensitiveBugReport &R, const CXXConstructorCall &Call, const ExplodedNode *N)=0
Consume the information on the non-modifying stack frame in order to either emit a note or not.
virtual bool wasModifiedInFunction(const ExplodedNode *CallEnterN, const ExplodedNode *CallExitEndN)
PathDiagnosticPieceRef VisitNode(const ExplodedNode *N, BugReporterContext &BR, PathSensitiveBugReport &R) final
Return a diagnostic piece which should be associated with the given node.
virtual PathDiagnosticPieceRef maybeEmitNoteForParameters(PathSensitiveBugReport &R, const CallEvent &Call, const ExplodedNode *N)=0
Consume the information on the non-modifying stack frame in order to either emit a note or not.
The tag upon which the TagVisitor reacts.
Represents any expression that calls an Objective-C method.
Definition CallEvent.h:1261
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
static PathDiagnosticLocation create(const Decl *D, const SourceManager &SM)
Create a location corresponding to the given declaration.
void markInteresting(SymbolRef sym, bugreporter::TrackingKind TKind=bugreporter::TrackingKind::Thorough)
Marks a symbol as interesting.
PathDiagnosticLocation getLocation() const override
The primary location of the bug report that points at the undesirable behavior in the code.
ArrayRef< SourceRange > getRanges() const override
Get the SourceRanges associated with the report.
const ExplodedNode * getErrorNode() const
bool addTrackedCondition(const ExplodedNode *Cond)
Notes that the condition of the CFGBlock associated with Cond is being tracked.
void markInvalid(const void *Tag, const void *Data)
Marks the current report as invalid, meaning that it is probably a false positive and should not be r...
void addVisitor(std::unique_ptr< BugReporterVisitor > visitor)
Add custom or predefined bug report visitors to this report.
std::optional< bugreporter::TrackingKind > getInterestingnessKind(SymbolRef sym) const
bool isInteresting(SymbolRef sym) const
CallEventManager & getCallEventManager()
bool haveEqualConstraints(ProgramStateRef S1, ProgramStateRef S2) const
void iterBindings(ProgramStateRef state, StoreManager::BindingsHandler &F)
ProgramState - This class encapsulates:
Loc getLValue(const CXXBaseSpecifier &BaseSpec, const SubRegion *Super) const
Get the lvalue for a base class object reference.
SVal getSVal(const Expr *E, const LocationContext *LCtx) const
Returns the SVal bound to the expression E in the state's environment.
A Range represents the closed range [from, to].
ConditionTruthVal areEqual(ProgramStateRef state, SVal lhs, SVal rhs)
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 isZeroConstant() const
Definition SVals.cpp:257
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 isUnknown() const
Definition SVals.h:105
SubRegion - A region that subsets another larger region.
Definition MemRegion.h:474
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getSuperRegion() const
Definition MemRegion.h:487
bool isSubRegionOf(const MemRegion *R) const override
Check if the region is a subregion of the given region.
PathDiagnosticPieceRef VisitNode(const ExplodedNode *Succ, BugReporterContext &BRC, PathSensitiveBugReport &BR) override
Return a diagnostic piece which should be associated with the given node.
SuppressInlineDefensiveChecksVisitor(DefinedSVal Val, const ExplodedNode *N)
static const char * getTag()
Return the tag associated with this visitor.
void Profile(llvm::FoldingSetNodeID &ID) const override
void Profile(llvm::FoldingSetNodeID &ID) const override
PathDiagnosticPieceRef VisitNode(const ExplodedNode *N, BugReporterContext &BRC, PathSensitiveBugReport &R) override
Return a diagnostic piece which should be associated with the given node.
void Profile(llvm::FoldingSetNodeID &ID) const override
static const char * getTag()
Return the tag associated with this visitor.
PathDiagnosticPieceRef VisitNode(const ExplodedNode *N, BugReporterContext &BRC, PathSensitiveBugReport &BR) override
Return a diagnostic piece which should be associated with the given node.
TypedRegion - An abstract class representing regions that are typed.
Definition MemRegion.h:539
PathDiagnosticPieceRef VisitNode(const ExplodedNode *N, BugReporterContext &BRC, PathSensitiveBugReport &BR) override
Return a diagnostic piece which should be associated with the given node.
const VarDecl * getDecl() const override=0
Handles expressions during the tracking.
Handles stores during the tracking.
PathDiagnosticPieceRef constructNote(StoreInfo SI, BugReporterContext &BRC, StringRef NodeText)
void addLowPriorityHandler(ExpressionHandlerPtr SH)
Add custom expression handler with the lowest priority.
static TrackerRef create(PathSensitiveBugReport &Report)
virtual PathDiagnosticPieceRef handle(StoreInfo SI, BugReporterContext &BRC, TrackingOptions Opts)
Handle the store operation and produce the note.
void addHighPriorityHandler(ExpressionHandlerPtr SH)
Add custom expression handler with the highest priority.
Tracker(PathSensitiveBugReport &Report)
virtual Result track(const Expr *E, const ExplodedNode *N, TrackingOptions Opts={})
Track expression value back to its point of origin.
Visitor that tracks expressions and values.
Value representing integer constant.
Definition SVals.h:300
While nonloc::CompoundVal covers a few simple use cases, nonloc::LazyCompoundVal is a more performant...
Definition SVals.h:389
LLVM_ATTRIBUTE_RETURNS_NONNULL const TypedValueRegion * getRegion() const
This function itself is immaterial.
Definition SVals.cpp:193
const internal::VariadicDynCastAllOfMatcher< Stmt, ObjCIvarRefExpr > objcIvarRefExpr
Matches a reference to an ObjCIvar.
const internal::ArgumentAdaptingMatcherFunc< internal::HasDescendantMatcher > hasDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
internal::Matcher< Stmt > StatementMatcher
const internal::VariadicDynCastAllOfMatcher< Stmt, BinaryOperator > binaryOperator
Matches binary operator expressions.
internal::PolymorphicMatcher< internal::HasDeclarationMatcher, void(internal::HasDeclarationSupportedTypes), internal::Matcher< Decl > > hasDeclaration(const internal::Matcher< Decl > &InnerMatcher)
Matches a node if the declaration associated with that node matches the given matcher.
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
llvm::IntrusiveRefCntPtr< Tracker > TrackerRef
const Expr * getDerefExpr(const Stmt *S)
Given that expression S represents a pointer that would be dereferenced, try to find a sub-expression...
void trackStoredValue(SVal V, const MemRegion *R, PathSensitiveBugReport &Report, TrackingOptions Opts={}, const StackFrame *Origin=nullptr)
Track how the value got stored into the given region and where it came from.
bool trackExpressionValue(const ExplodedNode *N, const Expr *E, PathSensitiveBugReport &R, TrackingOptions Opts={})
Attempts to add visitors to track expression value back to its point of origin.
TrackingKind
Specifies the type of tracking for an expression.
@ Thorough
Default tracking kind – specifies that as much information should be gathered about the tracked expre...
@ Condition
Specifies that a more moderate tracking should be used for the expression value.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
raw_ostream & operator<<(raw_ostream &os, const MemRegion *R)
Definition MemRegion.h:1701
std::shared_ptr< PathDiagnosticPiece > PathDiagnosticPieceRef
PRESERVE_NONE bool Ret(InterpState &S, CodePtr &PC)
Definition Interp.h:258
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
The JSON file list parser is used to communicate input to InstallAPI.
@ Match
This is not an overload because the signature exactly matches an existing declaration.
Definition Sema.h:830
bool isa(CodeGen::Address addr)
Definition Address.h:330
std::pair< FileID, unsigned > FileIDAndOffset
Expr * Cond
};
U cast(CodeGen::Address addr)
Definition Address.h:327
@ ObjCSelf
Parameter for Objective-C 'self' argument.
Definition Decl.h:1745
Describes an event when the value got stored into a memory region.
@ Assignment
The value got stored into the region during assignment: int x; x = 42;.
@ CallArgument
The value got stored into the parameter region as the result of a call.
@ BlockCapture
The value got stored into the region as block capture.
@ Initialization
The value got stored into the region during initialization: int x = 42;.
const Expr * SourceOfTheValue
The expression where the value comes from.
const ExplodedNode * StoreSite
The node where the store happened.
Kind StoreKind
The type of store operation.
SVal Value
Symbolic value that is being stored.
const MemRegion * Dest
Memory regions involved in the store operation.
Describes a tracking result with the most basic information of what was actually done (or not done).
void combineWith(const Result &Other)
Combines the current result with the given result.
bool WasInterrupted
Signifies that the tracking was interrupted at some point.
Defines a set of options altering tracking behavior.
bool EnableNullFPSuppression
Specifies whether we should employ false positive suppression (inlined defensive checks,...
TrackingKind Kind
Specifies the kind of tracking.