1 //===- BugReporterVisitors.cpp - Helpers for reporting bugs ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a set of BugReporter "visitors" which can be used to
11 // enhance the diagnostics reported for a bug.
13 //===----------------------------------------------------------------------===//
15 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/ExprObjC.h"
23 #include "clang/AST/Stmt.h"
24 #include "clang/AST/Type.h"
25 #include "clang/ASTMatchers/ASTMatchFinder.h"
26 #include "clang/Analysis/AnalysisDeclContext.h"
27 #include "clang/Analysis/CFG.h"
28 #include "clang/Analysis/CFGStmtMap.h"
29 #include "clang/Analysis/ProgramPoint.h"
30 #include "clang/Basic/IdentifierTable.h"
31 #include "clang/Basic/LLVM.h"
32 #include "clang/Basic/SourceLocation.h"
33 #include "clang/Basic/SourceManager.h"
34 #include "clang/Lex/Lexer.h"
35 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
36 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
37 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
38 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
39 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
40 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
41 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
42 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
43 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
44 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
45 #include "clang/StaticAnalyzer/Core/PathSensitive/SMTConv.h"
46 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
47 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
48 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
49 #include "llvm/ADT/ArrayRef.h"
50 #include "llvm/ADT/None.h"
51 #include "llvm/ADT/Optional.h"
52 #include "llvm/ADT/STLExtras.h"
53 #include "llvm/ADT/SmallPtrSet.h"
54 #include "llvm/ADT/SmallString.h"
55 #include "llvm/ADT/SmallVector.h"
56 #include "llvm/ADT/StringExtras.h"
57 #include "llvm/ADT/StringRef.h"
58 #include "llvm/Support/Casting.h"
59 #include "llvm/Support/ErrorHandling.h"
60 #include "llvm/Support/raw_ostream.h"
67 using namespace clang;
70 //===----------------------------------------------------------------------===//
72 //===----------------------------------------------------------------------===//
74 static const Expr *peelOffPointerArithmetic(const BinaryOperator *B) {
75 if (B->isAdditiveOp() && B->getType()->isPointerType()) {
76 if (B->getLHS()->getType()->isPointerType()) {
78 } else if (B->getRHS()->getType()->isPointerType()) {
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
95 /// x->y.z ==> x (lvalue)
96 /// foo()->y.z ==> foo() (rvalue)
97 const Expr *bugreporter::getDerefExpr(const Stmt *S) {
98 const auto *E = dyn_cast<Expr>(S);
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.
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)) {
114 // Probably more arithmetic can be pattern-matched here,
115 // but for now give up.
118 } else if (const auto *U = dyn_cast<UnaryOperator>(E)) {
119 if (U->getOpcode() == UO_Deref || U->getOpcode() == UO_AddrOf ||
120 (U->isIncrementDecrementOp() && U->getType()->isPointerType())) {
121 // Operators '*' and '&' don't actually mean anything.
122 // We look at casts instead.
125 // Probably more arithmetic can be pattern-matched here,
126 // but for now give up.
130 // Pattern match for a few useful cases: a[0], p->f, *p etc.
131 else if (const auto *ME = dyn_cast<MemberExpr>(E)) {
133 } else if (const auto *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
134 E = IvarRef->getBase();
135 } else if (const auto *AE = dyn_cast<ArraySubscriptExpr>(E)) {
137 } else if (const auto *PE = dyn_cast<ParenExpr>(E)) {
138 E = PE->getSubExpr();
139 } else if (const auto *FE = dyn_cast<FullExpr>(E)) {
140 E = FE->getSubExpr();
142 // Other arbitrary stuff.
147 // Special case: remove the final lvalue-to-rvalue cast, but do not recurse
148 // deeper into the sub-expression. This way we return the lvalue from which
149 // our pointer rvalue was loaded.
150 if (const auto *CE = dyn_cast<ImplicitCastExpr>(E))
151 if (CE->getCastKind() == CK_LValueToRValue)
152 E = CE->getSubExpr();
157 //===----------------------------------------------------------------------===//
158 // Definitions for bug reporter visitors.
159 //===----------------------------------------------------------------------===//
161 std::shared_ptr<PathDiagnosticPiece>
162 BugReporterVisitor::getEndPath(BugReporterContext &,
163 const ExplodedNode *, BugReport &) {
168 BugReporterVisitor::finalizeVisitor(BugReporterContext &,
169 const ExplodedNode *, BugReport &) {}
171 std::shared_ptr<PathDiagnosticPiece> BugReporterVisitor::getDefaultEndPath(
172 BugReporterContext &BRC, const ExplodedNode *EndPathNode, BugReport &BR) {
173 PathDiagnosticLocation L =
174 PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager());
176 const auto &Ranges = BR.getRanges();
178 // Only add the statement itself as a range if we didn't specify any
179 // special ranges for this report.
180 auto P = std::make_shared<PathDiagnosticEventPiece>(
181 L, BR.getDescription(), Ranges.begin() == Ranges.end());
182 for (SourceRange Range : Ranges)
188 /// \return name of the macro inside the location \p Loc.
189 static StringRef getMacroName(SourceLocation Loc,
190 BugReporterContext &BRC) {
191 return Lexer::getImmediateMacroName(
193 BRC.getSourceManager(),
194 BRC.getASTContext().getLangOpts());
197 /// \return Whether given spelling location corresponds to an expansion
198 /// of a function-like macro.
199 static bool isFunctionMacroExpansion(SourceLocation Loc,
200 const SourceManager &SM) {
201 if (!Loc.isMacroID())
203 while (SM.isMacroArgExpansion(Loc))
204 Loc = SM.getImmediateExpansionRange(Loc).getBegin();
205 std::pair<FileID, unsigned> TLInfo = SM.getDecomposedLoc(Loc);
206 SrcMgr::SLocEntry SE = SM.getSLocEntry(TLInfo.first);
207 const SrcMgr::ExpansionInfo &EInfo = SE.getExpansion();
208 return EInfo.isFunctionMacroExpansion();
211 /// \return Whether \c RegionOfInterest was modified at \p N,
212 /// where \p ReturnState is a state associated with the return
213 /// from the current frame.
214 static bool wasRegionOfInterestModifiedAt(
215 const SubRegion *RegionOfInterest,
216 const ExplodedNode *N,
218 ProgramStateRef State = N->getState();
219 ProgramStateManager &Mgr = N->getState()->getStateManager();
221 if (!N->getLocationAs<PostStore>()
222 && !N->getLocationAs<PostInitializer>()
223 && !N->getLocationAs<PostStmt>())
226 // Writing into region of interest.
227 if (auto PS = N->getLocationAs<PostStmt>())
228 if (auto *BO = PS->getStmtAs<BinaryOperator>())
229 if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(
230 N->getSVal(BO->getLHS()).getAsRegion()))
233 // SVal after the state is possibly different.
234 SVal ValueAtN = N->getState()->getSVal(RegionOfInterest);
235 if (!Mgr.getSValBuilder().areEqual(State, ValueAtN, ValueAfter).isConstrainedTrue() &&
236 (!ValueAtN.isUndef() || !ValueAfter.isUndef()))
245 /// Put a diagnostic on return statement of all inlined functions
246 /// for which the region of interest \p RegionOfInterest was passed into,
247 /// but not written inside, and it has caused an undefined read or a null
248 /// pointer dereference outside.
249 class NoStoreFuncVisitor final : public BugReporterVisitor {
250 const SubRegion *RegionOfInterest;
251 MemRegionManager &MmrMgr;
252 const SourceManager &SM;
253 const PrintingPolicy &PP;
255 /// Recursion limit for dereferencing fields when looking for the
256 /// region of interest.
257 /// The limit of two indicates that we will dereference fields only once.
258 static const unsigned DEREFERENCE_LIMIT = 2;
260 /// Frames writing into \c RegionOfInterest.
261 /// This visitor generates a note only if a function does not write into
262 /// a region of interest. This information is not immediately available
263 /// by looking at the node associated with the exit from the function
264 /// (usually the return statement). To avoid recomputing the same information
265 /// many times (going up the path for each node and checking whether the
266 /// region was written into) we instead lazily compute the
267 /// stack frames along the path which write into the region of interest.
268 llvm::SmallPtrSet<const StackFrameContext *, 32> FramesModifyingRegion;
269 llvm::SmallPtrSet<const StackFrameContext *, 32> FramesModifyingCalculated;
271 using RegionVector = SmallVector<const MemRegion *, 5>;
273 NoStoreFuncVisitor(const SubRegion *R)
274 : RegionOfInterest(R), MmrMgr(*R->getMemRegionManager()),
275 SM(MmrMgr.getContext().getSourceManager()),
276 PP(MmrMgr.getContext().getPrintingPolicy()) {}
278 void Profile(llvm::FoldingSetNodeID &ID) const override {
281 ID.AddPointer(RegionOfInterest);
284 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
285 BugReporterContext &BR,
286 BugReport &) override {
288 const LocationContext *Ctx = N->getLocationContext();
289 const StackFrameContext *SCtx = Ctx->getStackFrame();
290 ProgramStateRef State = N->getState();
291 auto CallExitLoc = N->getLocationAs<CallExitBegin>();
293 // No diagnostic if region was modified inside the frame.
294 if (!CallExitLoc || isRegionOfInterestModifiedInFrame(N))
297 CallEventRef<> Call =
298 BR.getStateManager().getCallEventManager().getCaller(SCtx, State);
300 if (SM.isInSystemHeader(Call->getDecl()->getSourceRange().getBegin()))
303 // Region of interest corresponds to an IVar, exiting a method
304 // which could have written into that IVar, but did not.
305 if (const auto *MC = dyn_cast<ObjCMethodCall>(Call)) {
306 if (const auto *IvarR = dyn_cast<ObjCIvarRegion>(RegionOfInterest)) {
307 const MemRegion *SelfRegion = MC->getReceiverSVal().getAsRegion();
308 if (RegionOfInterest->isSubRegionOf(SelfRegion) &&
309 potentiallyWritesIntoIvar(Call->getRuntimeDefinition().getDecl(),
311 return notModifiedDiagnostics(Ctx, *CallExitLoc, Call, {}, SelfRegion,
312 "self", /*FirstIsReferenceType=*/false,
317 if (const auto *CCall = dyn_cast<CXXConstructorCall>(Call)) {
318 const MemRegion *ThisR = CCall->getCXXThisVal().getAsRegion();
319 if (RegionOfInterest->isSubRegionOf(ThisR)
320 && !CCall->getDecl()->isImplicit())
321 return notModifiedDiagnostics(Ctx, *CallExitLoc, Call, {}, ThisR,
323 /*FirstIsReferenceType=*/false, 1);
325 // Do not generate diagnostics for not modified parameters in
330 ArrayRef<ParmVarDecl *> parameters = getCallParameters(Call);
331 for (unsigned I = 0; I < Call->getNumArgs() && I < parameters.size(); ++I) {
332 const ParmVarDecl *PVD = parameters[I];
333 SVal S = Call->getArgSVal(I);
334 bool ParamIsReferenceType = PVD->getType()->isReferenceType();
335 std::string ParamName = PVD->getNameAsString();
337 int IndirectionLevel = 1;
338 QualType T = PVD->getType();
339 while (const MemRegion *R = S.getAsRegion()) {
340 if (RegionOfInterest->isSubRegionOf(R) && !isPointerToConst(T))
341 return notModifiedDiagnostics(Ctx, *CallExitLoc, Call, {}, R,
342 ParamName, ParamIsReferenceType,
345 QualType PT = T->getPointeeType();
346 if (PT.isNull() || PT->isVoidType()) break;
348 if (const RecordDecl *RD = PT->getAsRecordDecl())
349 if (auto P = findRegionOfInterestInRecord(RD, State, R))
350 return notModifiedDiagnostics(
351 Ctx, *CallExitLoc, Call, *P, RegionOfInterest, ParamName,
352 ParamIsReferenceType, IndirectionLevel);
354 S = State->getSVal(R, PT);
364 /// Attempts to find the region of interest in a given CXX decl,
365 /// by either following the base classes or fields.
366 /// Dereferences fields up to a given recursion limit.
367 /// Note that \p Vec is passed by value, leading to quadratic copying cost,
368 /// but it's OK in practice since its length is limited to DEREFERENCE_LIMIT.
369 /// \return A chain fields leading to the region of interest or None.
370 const Optional<RegionVector>
371 findRegionOfInterestInRecord(const RecordDecl *RD, ProgramStateRef State,
373 const RegionVector &Vec = {},
376 if (depth == DEREFERENCE_LIMIT) // Limit the recursion depth.
379 if (const auto *RDX = dyn_cast<CXXRecordDecl>(RD))
380 if (!RDX->hasDefinition())
383 // Recursively examine the base classes.
384 // Note that following base classes does not increase the recursion depth.
385 if (const auto *RDX = dyn_cast<CXXRecordDecl>(RD))
386 for (const auto II : RDX->bases())
387 if (const RecordDecl *RRD = II.getType()->getAsRecordDecl())
388 if (auto Out = findRegionOfInterestInRecord(RRD, State, R, Vec, depth))
391 for (const FieldDecl *I : RD->fields()) {
392 QualType FT = I->getType();
393 const FieldRegion *FR = MmrMgr.getFieldRegion(I, cast<SubRegion>(R));
394 const SVal V = State->getSVal(FR);
395 const MemRegion *VR = V.getAsRegion();
397 RegionVector VecF = Vec;
400 if (RegionOfInterest == VR)
403 if (const RecordDecl *RRD = FT->getAsRecordDecl())
405 findRegionOfInterestInRecord(RRD, State, FR, VecF, depth + 1))
408 QualType PT = FT->getPointeeType();
409 if (PT.isNull() || PT->isVoidType() || !VR) continue;
411 if (const RecordDecl *RRD = PT->getAsRecordDecl())
413 findRegionOfInterestInRecord(RRD, State, VR, VecF, depth + 1))
421 /// \return Whether the method declaration \p Parent
422 /// syntactically has a binary operation writing into the ivar \p Ivar.
423 bool potentiallyWritesIntoIvar(const Decl *Parent,
424 const ObjCIvarDecl *Ivar) {
425 using namespace ast_matchers;
426 const char * IvarBind = "Ivar";
427 if (!Parent || !Parent->hasBody())
429 StatementMatcher WriteIntoIvarM = binaryOperator(
430 hasOperatorName("="),
431 hasLHS(ignoringParenImpCasts(
432 objcIvarRefExpr(hasDeclaration(equalsNode(Ivar))).bind(IvarBind))));
433 StatementMatcher ParentM = stmt(hasDescendant(WriteIntoIvarM));
434 auto Matches = match(ParentM, *Parent->getBody(), Parent->getASTContext());
435 for (BoundNodes &Match : Matches) {
436 auto IvarRef = Match.getNodeAs<ObjCIvarRefExpr>(IvarBind);
437 if (IvarRef->isFreeIvar())
440 const Expr *Base = IvarRef->getBase();
441 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Base))
442 Base = ICE->getSubExpr();
444 if (const auto *DRE = dyn_cast<DeclRefExpr>(Base))
445 if (const auto *ID = dyn_cast<ImplicitParamDecl>(DRE->getDecl()))
446 if (ID->getParameterKind() == ImplicitParamDecl::ObjCSelf)
454 /// Check and lazily calculate whether the region of interest is
455 /// modified in the stack frame to which \p N belongs.
456 /// The calculation is cached in FramesModifyingRegion.
457 bool isRegionOfInterestModifiedInFrame(const ExplodedNode *N) {
458 const LocationContext *Ctx = N->getLocationContext();
459 const StackFrameContext *SCtx = Ctx->getStackFrame();
460 if (!FramesModifyingCalculated.count(SCtx))
461 findModifyingFrames(N);
462 return FramesModifyingRegion.count(SCtx);
466 /// Write to \c FramesModifyingRegion all stack frames along
467 /// the path in the current stack frame which modify \c RegionOfInterest.
468 void findModifyingFrames(const ExplodedNode *N) {
469 assert(N->getLocationAs<CallExitBegin>());
470 ProgramStateRef LastReturnState = N->getState();
471 SVal ValueAtReturn = LastReturnState->getSVal(RegionOfInterest);
472 const LocationContext *Ctx = N->getLocationContext();
473 const StackFrameContext *OriginalSCtx = Ctx->getStackFrame();
476 ProgramStateRef State = N->getState();
477 auto CallExitLoc = N->getLocationAs<CallExitBegin>();
479 LastReturnState = State;
480 ValueAtReturn = LastReturnState->getSVal(RegionOfInterest);
483 FramesModifyingCalculated.insert(
484 N->getLocationContext()->getStackFrame());
486 if (wasRegionOfInterestModifiedAt(RegionOfInterest, N, ValueAtReturn)) {
487 const StackFrameContext *SCtx = N->getStackFrame();
488 while (!SCtx->inTopFrame()) {
489 auto p = FramesModifyingRegion.insert(SCtx);
491 break; // Frame and all its parents already inserted.
492 SCtx = SCtx->getParent()->getStackFrame();
496 // Stop calculation at the call to the current function.
497 if (auto CE = N->getLocationAs<CallEnter>())
498 if (CE->getCalleeContext() == OriginalSCtx)
501 N = N->getFirstPred();
505 /// Get parameters associated with runtime definition in order
506 /// to get the correct parameter name.
507 ArrayRef<ParmVarDecl *> getCallParameters(CallEventRef<> Call) {
508 // Use runtime definition, if available.
509 RuntimeDefinition RD = Call->getRuntimeDefinition();
510 if (const auto *FD = dyn_cast_or_null<FunctionDecl>(RD.getDecl()))
511 return FD->parameters();
512 if (const auto *MD = dyn_cast_or_null<ObjCMethodDecl>(RD.getDecl()))
513 return MD->parameters();
515 return Call->parameters();
518 /// \return whether \p Ty points to a const type, or is a const reference.
519 bool isPointerToConst(QualType Ty) {
520 return !Ty->getPointeeType().isNull() &&
521 Ty->getPointeeType().getCanonicalType().isConstQualified();
524 /// \return Diagnostics piece for region not modified in the current function.
525 std::shared_ptr<PathDiagnosticPiece>
526 notModifiedDiagnostics(const LocationContext *Ctx, CallExitBegin &CallExitLoc,
527 CallEventRef<> Call, const RegionVector &FieldChain,
528 const MemRegion *MatchedRegion, StringRef FirstElement,
529 bool FirstIsReferenceType, unsigned IndirectionLevel) {
531 PathDiagnosticLocation L;
532 if (const ReturnStmt *RS = CallExitLoc.getReturnStmt()) {
533 L = PathDiagnosticLocation::createBegin(RS, SM, Ctx);
535 L = PathDiagnosticLocation(
536 Call->getRuntimeDefinition().getDecl()->getSourceRange().getEnd(),
540 SmallString<256> sbuf;
541 llvm::raw_svector_ostream os(sbuf);
542 os << "Returning without writing to '";
544 // Do not generate the note if failed to pretty-print.
545 if (!prettyPrintRegionName(FirstElement, FirstIsReferenceType,
546 MatchedRegion, FieldChain, IndirectionLevel, os))
550 return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
553 /// Pretty-print region \p MatchedRegion to \p os.
554 /// \return Whether printing succeeded.
555 bool prettyPrintRegionName(StringRef FirstElement, bool FirstIsReferenceType,
556 const MemRegion *MatchedRegion,
557 const RegionVector &FieldChain,
558 int IndirectionLevel,
559 llvm::raw_svector_ostream &os) {
561 if (FirstIsReferenceType)
564 RegionVector RegionSequence;
566 // Add the regions in the reverse order, then reverse the resulting array.
567 assert(RegionOfInterest->isSubRegionOf(MatchedRegion));
568 const MemRegion *R = RegionOfInterest;
569 while (R != MatchedRegion) {
570 RegionSequence.push_back(R);
571 R = cast<SubRegion>(R)->getSuperRegion();
573 std::reverse(RegionSequence.begin(), RegionSequence.end());
574 RegionSequence.append(FieldChain.begin(), FieldChain.end());
577 for (const MemRegion *R : RegionSequence) {
579 // Just keep going up to the base region.
580 // Element regions may appear due to casts.
581 if (isa<CXXBaseObjectRegion>(R) || isa<CXXTempObjectRegion>(R))
585 Sep = prettyPrintFirstElement(FirstElement,
586 /*MoreItemsExpected=*/true,
587 IndirectionLevel, os);
591 // Can only reasonably pretty-print DeclRegions.
592 if (!isa<DeclRegion>(R))
595 const auto *DR = cast<DeclRegion>(R);
596 Sep = DR->getValueType()->isAnyPointerType() ? "->" : ".";
597 DR->getDecl()->getDeclName().print(os, PP);
601 prettyPrintFirstElement(FirstElement,
602 /*MoreItemsExpected=*/false, IndirectionLevel,
607 /// Print first item in the chain, return new separator.
608 StringRef prettyPrintFirstElement(StringRef FirstElement,
609 bool MoreItemsExpected,
610 int IndirectionLevel,
611 llvm::raw_svector_ostream &os) {
614 if (IndirectionLevel > 0 && MoreItemsExpected) {
619 if (IndirectionLevel > 0 && MoreItemsExpected)
622 for (int i=0; i<IndirectionLevel; i++)
626 if (IndirectionLevel > 0 && MoreItemsExpected)
633 /// Suppress null-pointer-dereference bugs where dereferenced null was returned
635 class MacroNullReturnSuppressionVisitor final : public BugReporterVisitor {
636 const SubRegion *RegionOfInterest;
637 const SVal ValueAtDereference;
639 // Do not invalidate the reports where the value was modified
640 // after it got assigned to from the macro.
641 bool WasModified = false;
644 MacroNullReturnSuppressionVisitor(const SubRegion *R,
645 const SVal V) : RegionOfInterest(R),
646 ValueAtDereference(V) {}
648 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
649 BugReporterContext &BRC,
650 BugReport &BR) override {
654 auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>();
658 const SourceManager &SMgr = BRC.getSourceManager();
659 if (auto Loc = matchAssignment(N)) {
660 if (isFunctionMacroExpansion(*Loc, SMgr)) {
661 std::string MacroName = getMacroName(*Loc, BRC);
662 SourceLocation BugLoc = BugPoint->getStmt()->getBeginLoc();
663 if (!BugLoc.isMacroID() || getMacroName(BugLoc, BRC) != MacroName)
664 BR.markInvalid(getTag(), MacroName.c_str());
668 if (wasRegionOfInterestModifiedAt(RegionOfInterest, N, ValueAtDereference))
674 static void addMacroVisitorIfNecessary(
675 const ExplodedNode *N, const MemRegion *R,
676 bool EnableNullFPSuppression, BugReport &BR,
678 AnalyzerOptions &Options = N->getState()->getAnalysisManager().options;
679 if (EnableNullFPSuppression &&
680 Options.ShouldSuppressNullReturnPaths && V.getAs<Loc>())
681 BR.addVisitor(llvm::make_unique<MacroNullReturnSuppressionVisitor>(
682 R->getAs<SubRegion>(), V));
685 void* getTag() const {
687 return static_cast<void *>(&Tag);
690 void Profile(llvm::FoldingSetNodeID &ID) const override {
691 ID.AddPointer(getTag());
695 /// \return Source location of right hand side of an assignment
696 /// into \c RegionOfInterest, empty optional if none found.
697 Optional<SourceLocation> matchAssignment(const ExplodedNode *N) {
698 const Stmt *S = PathDiagnosticLocation::getStmt(N);
699 ProgramStateRef State = N->getState();
700 auto *LCtx = N->getLocationContext();
704 if (const auto *DS = dyn_cast<DeclStmt>(S)) {
705 if (const auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl()))
706 if (const Expr *RHS = VD->getInit())
707 if (RegionOfInterest->isSubRegionOf(
708 State->getLValue(VD, LCtx).getAsRegion()))
709 return RHS->getBeginLoc();
710 } else if (const auto *BO = dyn_cast<BinaryOperator>(S)) {
711 const MemRegion *R = N->getSVal(BO->getLHS()).getAsRegion();
712 const Expr *RHS = BO->getRHS();
713 if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(R)) {
714 return RHS->getBeginLoc();
721 /// Emits an extra note at the return statement of an interesting stack frame.
723 /// The returned value is marked as an interesting value, and if it's null,
724 /// adds a visitor to track where it became null.
726 /// This visitor is intended to be used when another visitor discovers that an
727 /// interesting value comes from an inlined function call.
728 class ReturnVisitor : public BugReporterVisitor {
729 const StackFrameContext *StackFrame;
736 bool EnableNullFPSuppression;
737 bool ShouldInvalidate = true;
738 AnalyzerOptions& Options;
741 ReturnVisitor(const StackFrameContext *Frame,
743 AnalyzerOptions &Options)
744 : StackFrame(Frame), EnableNullFPSuppression(Suppressed),
747 static void *getTag() {
749 return static_cast<void *>(&Tag);
752 void Profile(llvm::FoldingSetNodeID &ID) const override {
753 ID.AddPointer(ReturnVisitor::getTag());
754 ID.AddPointer(StackFrame);
755 ID.AddBoolean(EnableNullFPSuppression);
758 /// Adds a ReturnVisitor if the given statement represents a call that was
761 /// This will search back through the ExplodedGraph, starting from the given
762 /// node, looking for when the given statement was processed. If it turns out
763 /// the statement is a call that was inlined, we add the visitor to the
764 /// bug report, so it can print a note later.
765 static void addVisitorIfNecessary(const ExplodedNode *Node, const Stmt *S,
767 bool InEnableNullFPSuppression) {
768 if (!CallEvent::isCallStmt(S))
771 // First, find when we processed the statement.
773 if (auto CEE = Node->getLocationAs<CallExitEnd>())
774 if (CEE->getCalleeContext()->getCallSite() == S)
776 if (auto SP = Node->getLocationAs<StmtPoint>())
777 if (SP->getStmt() == S)
780 Node = Node->getFirstPred();
783 // Next, step over any post-statement checks.
784 while (Node && Node->getLocation().getAs<PostStmt>())
785 Node = Node->getFirstPred();
789 // Finally, see if we inlined the call.
790 Optional<CallExitEnd> CEE = Node->getLocationAs<CallExitEnd>();
794 const StackFrameContext *CalleeContext = CEE->getCalleeContext();
795 if (CalleeContext->getCallSite() != S)
798 // Check the return value.
799 ProgramStateRef State = Node->getState();
800 SVal RetVal = Node->getSVal(S);
802 // Handle cases where a reference is returned and then immediately used.
803 if (cast<Expr>(S)->isGLValue())
804 if (Optional<Loc> LValue = RetVal.getAs<Loc>())
805 RetVal = State->getSVal(*LValue);
807 // See if the return value is NULL. If so, suppress the report.
808 AnalyzerOptions &Options = State->getAnalysisManager().options;
810 bool EnableNullFPSuppression = false;
811 if (InEnableNullFPSuppression &&
812 Options.ShouldSuppressNullReturnPaths)
813 if (Optional<Loc> RetLoc = RetVal.getAs<Loc>())
814 EnableNullFPSuppression = State->isNull(*RetLoc).isConstrainedTrue();
816 BR.markInteresting(CalleeContext);
817 BR.addVisitor(llvm::make_unique<ReturnVisitor>(CalleeContext,
818 EnableNullFPSuppression,
822 std::shared_ptr<PathDiagnosticPiece>
823 visitNodeInitial(const ExplodedNode *N,
824 BugReporterContext &BRC, BugReport &BR) {
825 // Only print a message at the interesting return statement.
826 if (N->getLocationContext() != StackFrame)
829 Optional<StmtPoint> SP = N->getLocationAs<StmtPoint>();
833 const auto *Ret = dyn_cast<ReturnStmt>(SP->getStmt());
837 // Okay, we're at the right return statement, but do we have the return
839 ProgramStateRef State = N->getState();
840 SVal V = State->getSVal(Ret, StackFrame);
841 if (V.isUnknownOrUndef())
844 // Don't print any more notes after this one.
847 const Expr *RetE = Ret->getRetValue();
848 assert(RetE && "Tracking a return value for a void function");
850 // Handle cases where a reference is returned and then immediately used.
851 Optional<Loc> LValue;
852 if (RetE->isGLValue()) {
853 if ((LValue = V.getAs<Loc>())) {
854 SVal RValue = State->getRawSVal(*LValue, RetE->getType());
855 if (RValue.getAs<DefinedSVal>())
860 // Ignore aggregate rvalues.
861 if (V.getAs<nonloc::LazyCompoundVal>() ||
862 V.getAs<nonloc::CompoundVal>())
865 RetE = RetE->IgnoreParenCasts();
867 // If we're returning 0, we should track where that 0 came from.
868 bugreporter::trackExpressionValue(N, RetE, BR, EnableNullFPSuppression);
870 // Build an appropriate message based on the return value.
872 llvm::raw_svector_ostream Out(Msg);
874 if (State->isNull(V).isConstrainedTrue()) {
875 if (V.getAs<Loc>()) {
877 // If we have counter-suppression enabled, make sure we keep visiting
878 // future nodes. We want to emit a path note as well, in case
879 // the report is resurrected as valid later on.
880 if (EnableNullFPSuppression &&
881 Options.ShouldAvoidSuppressingNullArgumentPaths)
882 Mode = MaybeUnsuppress;
884 if (RetE->getType()->isObjCObjectPointerType()) {
885 Out << "Returning nil";
887 Out << "Returning null pointer";
890 Out << "Returning zero";
894 if (auto CI = V.getAs<nonloc::ConcreteInt>()) {
895 Out << "Returning the value " << CI->getValue();
896 } else if (V.getAs<Loc>()) {
897 Out << "Returning pointer";
899 Out << "Returning value";
904 if (const MemRegion *MR = LValue->getAsRegion()) {
905 if (MR->canPrintPretty()) {
906 Out << " (reference to ";
907 MR->printPretty(Out);
912 // FIXME: We should have a more generalized location printing mechanism.
913 if (const auto *DR = dyn_cast<DeclRefExpr>(RetE))
914 if (const auto *DD = dyn_cast<DeclaratorDecl>(DR->getDecl()))
915 Out << " (loaded from '" << *DD << "')";
918 PathDiagnosticLocation L(Ret, BRC.getSourceManager(), StackFrame);
919 if (!L.isValid() || !L.asLocation().isValid())
922 return std::make_shared<PathDiagnosticEventPiece>(L, Out.str());
925 std::shared_ptr<PathDiagnosticPiece>
926 visitNodeMaybeUnsuppress(const ExplodedNode *N,
927 BugReporterContext &BRC, BugReport &BR) {
929 assert(Options.ShouldAvoidSuppressingNullArgumentPaths);
932 // Are we at the entry node for this call?
933 Optional<CallEnter> CE = N->getLocationAs<CallEnter>();
937 if (CE->getCalleeContext() != StackFrame)
942 // Don't automatically suppress a report if one of the arguments is
943 // known to be a null pointer. Instead, start tracking /that/ null
944 // value back to its origin.
945 ProgramStateManager &StateMgr = BRC.getStateManager();
946 CallEventManager &CallMgr = StateMgr.getCallEventManager();
948 ProgramStateRef State = N->getState();
949 CallEventRef<> Call = CallMgr.getCaller(StackFrame, State);
950 for (unsigned I = 0, E = Call->getNumArgs(); I != E; ++I) {
951 Optional<Loc> ArgV = Call->getArgSVal(I).getAs<Loc>();
955 const Expr *ArgE = Call->getArgExpr(I);
959 // Is it possible for this argument to be non-null?
960 if (!State->isNull(*ArgV).isConstrainedTrue())
963 if (bugreporter::trackExpressionValue(N, ArgE, BR, EnableNullFPSuppression))
964 ShouldInvalidate = false;
966 // If we /can't/ track the null pointer, we should err on the side of
967 // false negatives, and continue towards marking this report invalid.
968 // (We will still look at the other arguments, though.)
974 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
975 BugReporterContext &BRC,
976 BugReport &BR) override {
979 return visitNodeInitial(N, BRC, BR);
980 case MaybeUnsuppress:
981 return visitNodeMaybeUnsuppress(N, BRC, BR);
986 llvm_unreachable("Invalid visit mode!");
989 void finalizeVisitor(BugReporterContext &, const ExplodedNode *,
990 BugReport &BR) override {
991 if (EnableNullFPSuppression && ShouldInvalidate)
992 BR.markInvalid(ReturnVisitor::getTag(), StackFrame);
998 void FindLastStoreBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
1000 ID.AddPointer(&tag);
1003 ID.AddBoolean(EnableNullFPSuppression);
1006 /// Returns true if \p N represents the DeclStmt declaring and initializing
1008 static bool isInitializationOfVar(const ExplodedNode *N, const VarRegion *VR) {
1009 Optional<PostStmt> P = N->getLocationAs<PostStmt>();
1013 const DeclStmt *DS = P->getStmtAs<DeclStmt>();
1017 if (DS->getSingleDecl() != VR->getDecl())
1020 const MemSpaceRegion *VarSpace = VR->getMemorySpace();
1021 const auto *FrameSpace = dyn_cast<StackSpaceRegion>(VarSpace);
1023 // If we ever directly evaluate global DeclStmts, this assertion will be
1024 // invalid, but this still seems preferable to silently accepting an
1025 // initialization that may be for a path-sensitive variable.
1026 assert(VR->getDecl()->isStaticLocal() && "non-static stackless VarRegion");
1030 assert(VR->getDecl()->hasLocalStorage());
1031 const LocationContext *LCtx = N->getLocationContext();
1032 return FrameSpace->getStackFrame() == LCtx->getStackFrame();
1035 /// Show diagnostics for initializing or declaring a region \p R with a bad value.
1036 static void showBRDiagnostics(const char *action, llvm::raw_svector_ostream &os,
1037 const MemRegion *R, SVal V, const DeclStmt *DS) {
1038 if (R->canPrintPretty()) {
1043 if (V.getAs<loc::ConcreteInt>()) {
1045 if (R->isBoundable()) {
1046 if (const auto *TR = dyn_cast<TypedValueRegion>(R)) {
1047 if (TR->getValueType()->isObjCObjectPointerType()) {
1048 os << action << "nil";
1054 os << action << "a null pointer value";
1056 } else if (auto CVal = V.getAs<nonloc::ConcreteInt>()) {
1057 os << action << CVal->getValue();
1060 if (isa<VarRegion>(R)) {
1061 const auto *VD = cast<VarDecl>(DS->getSingleDecl());
1062 if (VD->getInit()) {
1063 os << (R->canPrintPretty() ? "initialized" : "Initializing")
1064 << " to a garbage value";
1066 os << (R->canPrintPretty() ? "declared" : "Declaring")
1067 << " without an initial value";
1071 os << (R->canPrintPretty() ? "initialized" : "Initialized")
1077 /// Display diagnostics for passing bad region as a parameter.
1078 static void showBRParamDiagnostics(llvm::raw_svector_ostream& os,
1079 const VarRegion *VR,
1081 const auto *Param = cast<ParmVarDecl>(VR->getDecl());
1085 if (V.getAs<loc::ConcreteInt>()) {
1086 if (Param->getType()->isObjCObjectPointerType())
1087 os << "nil object reference";
1089 os << "null pointer value";
1090 } else if (V.isUndef()) {
1091 os << "uninitialized value";
1092 } else if (auto CI = V.getAs<nonloc::ConcreteInt>()) {
1093 os << "the value " << CI->getValue();
1098 // Printed parameter indexes are 1-based, not 0-based.
1099 unsigned Idx = Param->getFunctionScopeIndex() + 1;
1100 os << " via " << Idx << llvm::getOrdinalSuffix(Idx) << " parameter";
1101 if (VR->canPrintPretty()) {
1103 VR->printPretty(os);
1107 /// Show default diagnostics for storing bad region.
1108 static void showBRDefaultDiagnostics(llvm::raw_svector_ostream& os,
1111 if (V.getAs<loc::ConcreteInt>()) {
1113 if (R->isBoundable()) {
1114 if (const auto *TR = dyn_cast<TypedValueRegion>(R)) {
1115 if (TR->getValueType()->isObjCObjectPointerType()) {
1116 os << "nil object reference stored";
1122 if (R->canPrintPretty())
1123 os << "Null pointer value stored";
1125 os << "Storing null pointer value";
1128 } else if (V.isUndef()) {
1129 if (R->canPrintPretty())
1130 os << "Uninitialized value stored";
1132 os << "Storing uninitialized value";
1134 } else if (auto CV = V.getAs<nonloc::ConcreteInt>()) {
1135 if (R->canPrintPretty())
1136 os << "The value " << CV->getValue() << " is assigned";
1138 os << "Assigning " << CV->getValue();
1141 if (R->canPrintPretty())
1142 os << "Value assigned";
1144 os << "Assigning value";
1147 if (R->canPrintPretty()) {
1153 std::shared_ptr<PathDiagnosticPiece>
1154 FindLastStoreBRVisitor::VisitNode(const ExplodedNode *Succ,
1155 BugReporterContext &BRC, BugReport &BR) {
1159 const ExplodedNode *StoreSite = nullptr;
1160 const ExplodedNode *Pred = Succ->getFirstPred();
1161 const Expr *InitE = nullptr;
1162 bool IsParam = false;
1164 // First see if we reached the declaration of the region.
1165 if (const auto *VR = dyn_cast<VarRegion>(R)) {
1166 if (isInitializationOfVar(Pred, VR)) {
1168 InitE = VR->getDecl()->getInit();
1172 // If this is a post initializer expression, initializing the region, we
1173 // should track the initializer expression.
1174 if (Optional<PostInitializer> PIP = Pred->getLocationAs<PostInitializer>()) {
1175 const MemRegion *FieldReg = (const MemRegion *)PIP->getLocationValue();
1176 if (FieldReg && FieldReg == R) {
1178 InitE = PIP->getInitializer()->getInit();
1182 // Otherwise, see if this is the store site:
1183 // (1) Succ has this binding and Pred does not, i.e. this is
1184 // where the binding first occurred.
1185 // (2) Succ has this binding and is a PostStore node for this region, i.e.
1186 // the same binding was re-assigned here.
1188 if (Succ->getState()->getSVal(R) != V)
1191 if (Pred->getState()->getSVal(R) == V) {
1192 Optional<PostStore> PS = Succ->getLocationAs<PostStore>();
1193 if (!PS || PS->getLocationValue() != R)
1199 // If this is an assignment expression, we can track the value
1201 if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
1202 if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>())
1203 if (BO->isAssignmentOp())
1204 InitE = BO->getRHS();
1206 // If this is a call entry, the variable should be a parameter.
1207 // FIXME: Handle CXXThisRegion as well. (This is not a priority because
1208 // 'this' should never be NULL, but this visitor isn't just for NULL and
1210 if (Optional<CallEnter> CE = Succ->getLocationAs<CallEnter>()) {
1211 if (const auto *VR = dyn_cast<VarRegion>(R)) {
1212 const auto *Param = cast<ParmVarDecl>(VR->getDecl());
1214 ProgramStateManager &StateMgr = BRC.getStateManager();
1215 CallEventManager &CallMgr = StateMgr.getCallEventManager();
1217 CallEventRef<> Call = CallMgr.getCaller(CE->getCalleeContext(),
1219 InitE = Call->getArgExpr(Param->getFunctionScopeIndex());
1224 // If this is a CXXTempObjectRegion, the Expr responsible for its creation
1225 // is wrapped inside of it.
1226 if (const auto *TmpR = dyn_cast<CXXTempObjectRegion>(R))
1227 InitE = TmpR->getExpr();
1234 // If we have an expression that provided the value, try to track where it
1238 V.getAs<loc::ConcreteInt>() || V.getAs<nonloc::ConcreteInt>()) {
1240 InitE = InitE->IgnoreParenCasts();
1241 bugreporter::trackExpressionValue(StoreSite, InitE, BR,
1242 EnableNullFPSuppression);
1244 ReturnVisitor::addVisitorIfNecessary(StoreSite, InitE->IgnoreParenCasts(),
1245 BR, EnableNullFPSuppression);
1248 // Okay, we've found the binding. Emit an appropriate message.
1249 SmallString<256> sbuf;
1250 llvm::raw_svector_ostream os(sbuf);
1252 if (Optional<PostStmt> PS = StoreSite->getLocationAs<PostStmt>()) {
1253 const Stmt *S = PS->getStmt();
1254 const char *action = nullptr;
1255 const auto *DS = dyn_cast<DeclStmt>(S);
1256 const auto *VR = dyn_cast<VarRegion>(R);
1259 action = R->canPrintPretty() ? "initialized to " :
1261 } else if (isa<BlockExpr>(S)) {
1262 action = R->canPrintPretty() ? "captured by block as " :
1263 "Captured by block as ";
1265 // See if we can get the BlockVarRegion.
1266 ProgramStateRef State = StoreSite->getState();
1267 SVal V = StoreSite->getSVal(S);
1268 if (const auto *BDR =
1269 dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) {
1270 if (const VarRegion *OriginalR = BDR->getOriginalRegion(VR)) {
1271 if (auto KV = State->getSVal(OriginalR).getAs<KnownSVal>())
1272 BR.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1273 *KV, OriginalR, EnableNullFPSuppression));
1279 showBRDiagnostics(action, os, R, V, DS);
1281 } else if (StoreSite->getLocation().getAs<CallEnter>()) {
1282 if (const auto *VR = dyn_cast<VarRegion>(R))
1283 showBRParamDiagnostics(os, VR, V);
1286 if (os.str().empty())
1287 showBRDefaultDiagnostics(os, R, V);
1289 // Construct a new PathDiagnosticPiece.
1290 ProgramPoint P = StoreSite->getLocation();
1291 PathDiagnosticLocation L;
1292 if (P.getAs<CallEnter>() && InitE)
1293 L = PathDiagnosticLocation(InitE, BRC.getSourceManager(),
1294 P.getLocationContext());
1296 if (!L.isValid() || !L.asLocation().isValid())
1297 L = PathDiagnosticLocation::create(P, BRC.getSourceManager());
1299 if (!L.isValid() || !L.asLocation().isValid())
1302 return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
1305 void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
1307 ID.AddPointer(&tag);
1308 ID.AddBoolean(Assumption);
1312 /// Return the tag associated with this visitor. This tag will be used
1313 /// to make all PathDiagnosticPieces created by this visitor.
1314 const char *TrackConstraintBRVisitor::getTag() {
1315 return "TrackConstraintBRVisitor";
1318 bool TrackConstraintBRVisitor::isUnderconstrained(const ExplodedNode *N) const {
1320 return N->getState()->isNull(Constraint).isUnderconstrained();
1321 return (bool)N->getState()->assume(Constraint, !Assumption);
1324 std::shared_ptr<PathDiagnosticPiece>
1325 TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
1326 BugReporterContext &BRC, BugReport &) {
1327 const ExplodedNode *PrevN = N->getFirstPred();
1331 // Start tracking after we see the first state in which the value is
1333 if (!IsTrackingTurnedOn)
1334 if (!isUnderconstrained(N))
1335 IsTrackingTurnedOn = true;
1336 if (!IsTrackingTurnedOn)
1339 // Check if in the previous state it was feasible for this constraint
1340 // to *not* be true.
1341 if (isUnderconstrained(PrevN)) {
1344 // As a sanity check, make sure that the negation of the constraint
1345 // was infeasible in the current state. If it is feasible, we somehow
1346 // missed the transition point.
1347 assert(!isUnderconstrained(N));
1349 // We found the transition point for the constraint. We now need to
1350 // pretty-print the constraint. (work-in-progress)
1351 SmallString<64> sbuf;
1352 llvm::raw_svector_ostream os(sbuf);
1354 if (Constraint.getAs<Loc>()) {
1355 os << "Assuming pointer value is ";
1356 os << (Assumption ? "non-null" : "null");
1359 if (os.str().empty())
1362 // Construct a new PathDiagnosticPiece.
1363 ProgramPoint P = N->getLocation();
1364 PathDiagnosticLocation L =
1365 PathDiagnosticLocation::create(P, BRC.getSourceManager());
1369 auto X = std::make_shared<PathDiagnosticEventPiece>(L, os.str());
1370 X->setTag(getTag());
1371 return std::move(X);
1377 SuppressInlineDefensiveChecksVisitor::
1378 SuppressInlineDefensiveChecksVisitor(DefinedSVal Value, const ExplodedNode *N)
1380 // Check if the visitor is disabled.
1381 AnalyzerOptions &Options = N->getState()->getAnalysisManager().options;
1382 if (!Options.ShouldSuppressInlinedDefensiveChecks)
1385 assert(N->getState()->isNull(V).isConstrainedTrue() &&
1386 "The visitor only tracks the cases where V is constrained to 0");
1389 void SuppressInlineDefensiveChecksVisitor::Profile(
1390 llvm::FoldingSetNodeID &ID) const {
1396 const char *SuppressInlineDefensiveChecksVisitor::getTag() {
1397 return "IDCVisitor";
1400 std::shared_ptr<PathDiagnosticPiece>
1401 SuppressInlineDefensiveChecksVisitor::VisitNode(const ExplodedNode *Succ,
1402 BugReporterContext &BRC,
1404 const ExplodedNode *Pred = Succ->getFirstPred();
1408 // Start tracking after we see the first state in which the value is null.
1409 if (!IsTrackingTurnedOn)
1410 if (Succ->getState()->isNull(V).isConstrainedTrue())
1411 IsTrackingTurnedOn = true;
1412 if (!IsTrackingTurnedOn)
1415 // Check if in the previous state it was feasible for this value
1416 // to *not* be null.
1417 if (!Pred->getState()->isNull(V).isConstrainedTrue()) {
1420 assert(Succ->getState()->isNull(V).isConstrainedTrue());
1422 // Check if this is inlined defensive checks.
1423 const LocationContext *CurLC =Succ->getLocationContext();
1424 const LocationContext *ReportLC = BR.getErrorNode()->getLocationContext();
1425 if (CurLC != ReportLC && !CurLC->isParentOf(ReportLC)) {
1426 BR.markInvalid("Suppress IDC", CurLC);
1430 // Treat defensive checks in function-like macros as if they were an inlined
1431 // defensive check. If the bug location is not in a macro and the
1432 // terminator for the current location is in a macro then suppress the
1434 auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>();
1439 ProgramPoint CurPoint = Succ->getLocation();
1440 const Stmt *CurTerminatorStmt = nullptr;
1441 if (auto BE = CurPoint.getAs<BlockEdge>()) {
1442 CurTerminatorStmt = BE->getSrc()->getTerminator().getStmt();
1443 } else if (auto SP = CurPoint.getAs<StmtPoint>()) {
1444 const Stmt *CurStmt = SP->getStmt();
1445 if (!CurStmt->getBeginLoc().isMacroID())
1448 CFGStmtMap *Map = CurLC->getAnalysisDeclContext()->getCFGStmtMap();
1449 CurTerminatorStmt = Map->getBlock(CurStmt)->getTerminator();
1454 if (!CurTerminatorStmt)
1457 SourceLocation TerminatorLoc = CurTerminatorStmt->getBeginLoc();
1458 if (TerminatorLoc.isMacroID()) {
1459 SourceLocation BugLoc = BugPoint->getStmt()->getBeginLoc();
1461 // Suppress reports unless we are in that same macro.
1462 if (!BugLoc.isMacroID() ||
1463 getMacroName(BugLoc, BRC) != getMacroName(TerminatorLoc, BRC)) {
1464 BR.markInvalid("Suppress Macro IDC", CurLC);
1472 static const MemRegion *getLocationRegionIfReference(const Expr *E,
1473 const ExplodedNode *N) {
1474 if (const auto *DR = dyn_cast<DeclRefExpr>(E)) {
1475 if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1476 if (!VD->getType()->isReferenceType())
1478 ProgramStateManager &StateMgr = N->getState()->getStateManager();
1479 MemRegionManager &MRMgr = StateMgr.getRegionManager();
1480 return MRMgr.getVarRegion(VD, N->getLocationContext());
1484 // FIXME: This does not handle other kinds of null references,
1485 // for example, references from FieldRegions:
1486 // struct Wrapper { int &ref; };
1487 // Wrapper w = { *(int *)0 };
1493 /// \return A subexpression of {@code Ex} which represents the
1494 /// expression-of-interest.
1495 static const Expr *peelOffOuterExpr(const Expr *Ex,
1496 const ExplodedNode *N) {
1497 Ex = Ex->IgnoreParenCasts();
1498 if (const auto *FE = dyn_cast<FullExpr>(Ex))
1499 return peelOffOuterExpr(FE->getSubExpr(), N);
1500 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(Ex))
1501 return peelOffOuterExpr(OVE->getSourceExpr(), N);
1502 if (const auto *POE = dyn_cast<PseudoObjectExpr>(Ex)) {
1503 const auto *PropRef = dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
1504 if (PropRef && PropRef->isMessagingGetter()) {
1505 const Expr *GetterMessageSend =
1506 POE->getSemanticExpr(POE->getNumSemanticExprs() - 1);
1507 assert(isa<ObjCMessageExpr>(GetterMessageSend->IgnoreParenCasts()));
1508 return peelOffOuterExpr(GetterMessageSend, N);
1512 // Peel off the ternary operator.
1513 if (const auto *CO = dyn_cast<ConditionalOperator>(Ex)) {
1514 // Find a node where the branching occurred and find out which branch
1515 // we took (true/false) by looking at the ExplodedGraph.
1516 const ExplodedNode *NI = N;
1518 ProgramPoint ProgPoint = NI->getLocation();
1519 if (Optional<BlockEdge> BE = ProgPoint.getAs<BlockEdge>()) {
1520 const CFGBlock *srcBlk = BE->getSrc();
1521 if (const Stmt *term = srcBlk->getTerminator()) {
1523 bool TookTrueBranch = (*(srcBlk->succ_begin()) == BE->getDst());
1525 return peelOffOuterExpr(CO->getTrueExpr(), N);
1527 return peelOffOuterExpr(CO->getFalseExpr(), N);
1531 NI = NI->getFirstPred();
1535 if (auto *BO = dyn_cast<BinaryOperator>(Ex))
1536 if (const Expr *SubEx = peelOffPointerArithmetic(BO))
1537 return peelOffOuterExpr(SubEx, N);
1539 if (auto *UO = dyn_cast<UnaryOperator>(Ex)) {
1540 if (UO->getOpcode() == UO_LNot)
1541 return peelOffOuterExpr(UO->getSubExpr(), N);
1543 // FIXME: There's a hack in our Store implementation that always computes
1544 // field offsets around null pointers as if they are always equal to 0.
1545 // The idea here is to report accesses to fields as null dereferences
1546 // even though the pointer value that's being dereferenced is actually
1547 // the offset of the field rather than exactly 0.
1548 // See the FIXME in StoreManager's getLValueFieldOrIvar() method.
1549 // This code interacts heavily with this hack; otherwise the value
1550 // would not be null at all for most fields, so we'd be unable to track it.
1551 if (UO->getOpcode() == UO_AddrOf && UO->getSubExpr()->isLValue())
1552 if (const Expr *DerefEx = bugreporter::getDerefExpr(UO->getSubExpr()))
1553 return peelOffOuterExpr(DerefEx, N);
1559 /// Find the ExplodedNode where the lvalue (the value of 'Ex')
1561 static const ExplodedNode* findNodeForExpression(const ExplodedNode *N,
1562 const Expr *Inner) {
1564 if (PathDiagnosticLocation::getStmt(N) == Inner)
1566 N = N->getFirstPred();
1571 bool bugreporter::trackExpressionValue(const ExplodedNode *InputNode,
1572 const Expr *E, BugReport &report,
1573 bool EnableNullFPSuppression) {
1574 if (!E || !InputNode)
1577 const Expr *Inner = peelOffOuterExpr(E, InputNode);
1578 const ExplodedNode *LVNode = findNodeForExpression(InputNode, Inner);
1582 ProgramStateRef LVState = LVNode->getState();
1584 // The message send could be nil due to the receiver being nil.
1585 // At this point in the path, the receiver should be live since we are at the
1586 // message send expr. If it is nil, start tracking it.
1587 if (const Expr *Receiver = NilReceiverBRVisitor::getNilReceiver(Inner, LVNode))
1588 trackExpressionValue(LVNode, Receiver, report, EnableNullFPSuppression);
1590 // See if the expression we're interested refers to a variable.
1591 // If so, we can track both its contents and constraints on its value.
1592 if (ExplodedGraph::isInterestingLValueExpr(Inner)) {
1593 SVal LVal = LVNode->getSVal(Inner);
1595 const MemRegion *RR = getLocationRegionIfReference(Inner, LVNode);
1596 bool LVIsNull = LVState->isNull(LVal).isConstrainedTrue();
1598 // If this is a C++ reference to a null pointer, we are tracking the
1599 // pointer. In addition, we should find the store at which the reference
1601 if (RR && !LVIsNull)
1602 if (auto KV = LVal.getAs<KnownSVal>())
1603 report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1604 *KV, RR, EnableNullFPSuppression));
1606 // In case of C++ references, we want to differentiate between a null
1607 // reference and reference to null pointer.
1608 // If the LVal is null, check if we are dealing with null reference.
1609 // For those, we want to track the location of the reference.
1610 const MemRegion *R = (RR && LVIsNull) ? RR :
1611 LVNode->getSVal(Inner).getAsRegion();
1615 // Mark both the variable region and its contents as interesting.
1616 SVal V = LVState->getRawSVal(loc::MemRegionVal(R));
1618 llvm::make_unique<NoStoreFuncVisitor>(cast<SubRegion>(R)));
1620 MacroNullReturnSuppressionVisitor::addMacroVisitorIfNecessary(
1621 LVNode, R, EnableNullFPSuppression, report, V);
1623 report.markInteresting(V);
1624 report.addVisitor(llvm::make_unique<UndefOrNullArgVisitor>(R));
1626 // If the contents are symbolic, find out when they became null.
1627 if (V.getAsLocSymbol(/*IncludeBaseRegions*/ true))
1628 report.addVisitor(llvm::make_unique<TrackConstraintBRVisitor>(
1629 V.castAs<DefinedSVal>(), false));
1631 // Add visitor, which will suppress inline defensive checks.
1632 if (auto DV = V.getAs<DefinedSVal>())
1633 if (!DV->isZeroConstant() && LVState->isNull(*DV).isConstrainedTrue() &&
1634 EnableNullFPSuppression)
1636 llvm::make_unique<SuppressInlineDefensiveChecksVisitor>(*DV,
1639 if (auto KV = V.getAs<KnownSVal>())
1640 report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1641 *KV, R, EnableNullFPSuppression));
1646 // If the expression is not an "lvalue expression", we can still
1647 // track the constraints on its contents.
1648 SVal V = LVState->getSValAsScalarOrLoc(Inner, LVNode->getLocationContext());
1650 ReturnVisitor::addVisitorIfNecessary(
1651 LVNode, Inner, report, EnableNullFPSuppression);
1653 // Is it a symbolic value?
1654 if (auto L = V.getAs<loc::MemRegionVal>()) {
1655 report.addVisitor(llvm::make_unique<UndefOrNullArgVisitor>(L->getRegion()));
1657 // FIXME: this is a hack for fixing a later crash when attempting to
1658 // dereference a void* pointer.
1659 // We should not try to dereference pointers at all when we don't care
1660 // what is written inside the pointer.
1661 bool CanDereference = true;
1662 if (const auto *SR = dyn_cast<SymbolicRegion>(L->getRegion()))
1663 if (SR->getSymbol()->getType()->getPointeeType()->isVoidType())
1664 CanDereference = false;
1666 // At this point we are dealing with the region's LValue.
1667 // However, if the rvalue is a symbolic region, we should track it as well.
1668 // Try to use the correct type when looking up the value.
1670 if (ExplodedGraph::isInterestingLValueExpr(Inner)) {
1671 RVal = LVState->getRawSVal(L.getValue(), Inner->getType());
1672 } else if (CanDereference) {
1673 RVal = LVState->getSVal(L->getRegion());
1677 if (auto KV = RVal.getAs<KnownSVal>())
1678 report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1679 *KV, L->getRegion(), EnableNullFPSuppression));
1681 const MemRegion *RegionRVal = RVal.getAsRegion();
1682 if (RegionRVal && isa<SymbolicRegion>(RegionRVal)) {
1683 report.markInteresting(RegionRVal);
1684 report.addVisitor(llvm::make_unique<TrackConstraintBRVisitor>(
1685 loc::MemRegionVal(RegionRVal), /*assumption=*/false));
1691 const Expr *NilReceiverBRVisitor::getNilReceiver(const Stmt *S,
1692 const ExplodedNode *N) {
1693 const auto *ME = dyn_cast<ObjCMessageExpr>(S);
1696 if (const Expr *Receiver = ME->getInstanceReceiver()) {
1697 ProgramStateRef state = N->getState();
1698 SVal V = N->getSVal(Receiver);
1699 if (state->isNull(V).isConstrainedTrue())
1705 std::shared_ptr<PathDiagnosticPiece>
1706 NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
1707 BugReporterContext &BRC, BugReport &BR) {
1708 Optional<PreStmt> P = N->getLocationAs<PreStmt>();
1712 const Stmt *S = P->getStmt();
1713 const Expr *Receiver = getNilReceiver(S, N);
1717 llvm::SmallString<256> Buf;
1718 llvm::raw_svector_ostream OS(Buf);
1720 if (const auto *ME = dyn_cast<ObjCMessageExpr>(S)) {
1722 ME->getSelector().print(OS);
1723 OS << "' not called";
1726 OS << "No method is called";
1728 OS << " because the receiver is nil";
1730 // The receiver was nil, and hence the method was skipped.
1731 // Register a BugReporterVisitor to issue a message telling us how
1732 // the receiver was null.
1733 bugreporter::trackExpressionValue(N, Receiver, BR,
1734 /*EnableNullFPSuppression*/ false);
1735 // Issue a message saying that the method was skipped.
1736 PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
1737 N->getLocationContext());
1738 return std::make_shared<PathDiagnosticEventPiece>(L, OS.str());
1741 // Registers every VarDecl inside a Stmt with a last store visitor.
1742 void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
1744 bool EnableNullFPSuppression) {
1745 const ExplodedNode *N = BR.getErrorNode();
1746 std::deque<const Stmt *> WorkList;
1747 WorkList.push_back(S);
1749 while (!WorkList.empty()) {
1750 const Stmt *Head = WorkList.front();
1751 WorkList.pop_front();
1753 ProgramStateManager &StateMgr = N->getState()->getStateManager();
1755 if (const auto *DR = dyn_cast<DeclRefExpr>(Head)) {
1756 if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1757 const VarRegion *R =
1758 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
1760 // What did we load?
1761 SVal V = N->getSVal(S);
1763 if (V.getAs<loc::ConcreteInt>() || V.getAs<nonloc::ConcreteInt>()) {
1764 // Register a new visitor with the BugReport.
1765 BR.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1766 V.castAs<KnownSVal>(), R, EnableNullFPSuppression));
1771 for (const Stmt *SubStmt : Head->children())
1772 WorkList.push_back(SubStmt);
1776 //===----------------------------------------------------------------------===//
1777 // Visitor that tries to report interesting diagnostics from conditions.
1778 //===----------------------------------------------------------------------===//
1780 /// Return the tag associated with this visitor. This tag will be used
1781 /// to make all PathDiagnosticPieces created by this visitor.
1782 const char *ConditionBRVisitor::getTag() {
1783 return "ConditionBRVisitor";
1786 std::shared_ptr<PathDiagnosticPiece>
1787 ConditionBRVisitor::VisitNode(const ExplodedNode *N,
1788 BugReporterContext &BRC, BugReport &BR) {
1789 auto piece = VisitNodeImpl(N, BRC, BR);
1791 piece->setTag(getTag());
1792 if (auto *ev = dyn_cast<PathDiagnosticEventPiece>(piece.get()))
1793 ev->setPrunable(true, /* override */ false);
1798 std::shared_ptr<PathDiagnosticPiece>
1799 ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N,
1800 BugReporterContext &BRC, BugReport &BR) {
1801 ProgramPoint progPoint = N->getLocation();
1802 ProgramStateRef CurrentState = N->getState();
1803 ProgramStateRef PrevState = N->getFirstPred()->getState();
1805 // Compare the GDMs of the state, because that is where constraints
1806 // are managed. Note that ensure that we only look at nodes that
1807 // were generated by the analyzer engine proper, not checkers.
1808 if (CurrentState->getGDM().getRoot() ==
1809 PrevState->getGDM().getRoot())
1812 // If an assumption was made on a branch, it should be caught
1813 // here by looking at the state transition.
1814 if (Optional<BlockEdge> BE = progPoint.getAs<BlockEdge>()) {
1815 const CFGBlock *srcBlk = BE->getSrc();
1816 if (const Stmt *term = srcBlk->getTerminator())
1817 return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC);
1821 if (Optional<PostStmt> PS = progPoint.getAs<PostStmt>()) {
1822 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
1823 ExprEngine::geteagerlyAssumeBinOpBifurcationTags();
1825 const ProgramPointTag *tag = PS->getTag();
1826 if (tag == tags.first)
1827 return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
1829 if (tag == tags.second)
1830 return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
1839 std::shared_ptr<PathDiagnosticPiece> ConditionBRVisitor::VisitTerminator(
1840 const Stmt *Term, const ExplodedNode *N, const CFGBlock *srcBlk,
1841 const CFGBlock *dstBlk, BugReport &R, BugReporterContext &BRC) {
1842 const Expr *Cond = nullptr;
1844 // In the code below, Term is a CFG terminator and Cond is a branch condition
1845 // expression upon which the decision is made on this terminator.
1847 // For example, in "if (x == 0)", the "if (x == 0)" statement is a terminator,
1848 // and "x == 0" is the respective condition.
1850 // Another example: in "if (x && y)", we've got two terminators and two
1851 // conditions due to short-circuit nature of operator "&&":
1852 // 1. The "if (x && y)" statement is a terminator,
1853 // and "y" is the respective condition.
1854 // 2. Also "x && ..." is another terminator,
1855 // and "x" is its condition.
1857 switch (Term->getStmtClass()) {
1858 // FIXME: Stmt::SwitchStmtClass is worth handling, however it is a bit
1859 // more tricky because there are more than two branches to account for.
1862 case Stmt::IfStmtClass:
1863 Cond = cast<IfStmt>(Term)->getCond();
1865 case Stmt::ConditionalOperatorClass:
1866 Cond = cast<ConditionalOperator>(Term)->getCond();
1868 case Stmt::BinaryOperatorClass:
1869 // When we encounter a logical operator (&& or ||) as a CFG terminator,
1870 // then the condition is actually its LHS; otherwise, we'd encounter
1871 // the parent, such as if-statement, as a terminator.
1872 const auto *BO = cast<BinaryOperator>(Term);
1873 assert(BO->isLogicalOp() &&
1874 "CFG terminator is not a short-circuit operator!");
1875 Cond = BO->getLHS();
1879 // However, when we encounter a logical operator as a branch condition,
1880 // then the condition is actually its RHS, because LHS would be
1881 // the condition for the logical operator terminator.
1882 while (const auto *InnerBO = dyn_cast<BinaryOperator>(Cond)) {
1883 if (!InnerBO->isLogicalOp())
1885 Cond = InnerBO->getRHS()->IgnoreParens();
1889 assert(srcBlk->succ_size() == 2);
1890 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
1891 return VisitTrueTest(Cond, tookTrue, BRC, R, N);
1894 std::shared_ptr<PathDiagnosticPiece>
1895 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, bool tookTrue,
1896 BugReporterContext &BRC, BugReport &R,
1897 const ExplodedNode *N) {
1898 // These will be modified in code below, but we need to preserve the original
1899 // values in case we want to throw the generic message.
1900 const Expr *CondTmp = Cond;
1901 bool tookTrueTmp = tookTrue;
1904 CondTmp = CondTmp->IgnoreParenCasts();
1905 switch (CondTmp->getStmtClass()) {
1908 case Stmt::BinaryOperatorClass:
1909 if (auto P = VisitTrueTest(Cond, cast<BinaryOperator>(CondTmp),
1910 tookTrueTmp, BRC, R, N))
1913 case Stmt::DeclRefExprClass:
1914 if (auto P = VisitTrueTest(Cond, cast<DeclRefExpr>(CondTmp),
1915 tookTrueTmp, BRC, R, N))
1918 case Stmt::UnaryOperatorClass: {
1919 const auto *UO = cast<UnaryOperator>(CondTmp);
1920 if (UO->getOpcode() == UO_LNot) {
1921 tookTrueTmp = !tookTrueTmp;
1922 CondTmp = UO->getSubExpr();
1931 // Condition too complex to explain? Just say something so that the user
1932 // knew we've made some path decision at this point.
1933 const LocationContext *LCtx = N->getLocationContext();
1934 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
1935 if (!Loc.isValid() || !Loc.asLocation().isValid())
1938 return std::make_shared<PathDiagnosticEventPiece>(
1939 Loc, tookTrue ? GenericTrueMessage : GenericFalseMessage);
1942 bool ConditionBRVisitor::patternMatch(const Expr *Ex,
1943 const Expr *ParentEx,
1945 BugReporterContext &BRC,
1947 const ExplodedNode *N,
1948 Optional<bool> &prunable) {
1949 const Expr *OriginalExpr = Ex;
1950 Ex = Ex->IgnoreParenCasts();
1952 // Use heuristics to determine if Ex is a macro expending to a literal and
1953 // if so, use the macro's name.
1954 SourceLocation LocStart = Ex->getBeginLoc();
1955 SourceLocation LocEnd = Ex->getEndLoc();
1956 if (LocStart.isMacroID() && LocEnd.isMacroID() &&
1957 (isa<GNUNullExpr>(Ex) ||
1958 isa<ObjCBoolLiteralExpr>(Ex) ||
1959 isa<CXXBoolLiteralExpr>(Ex) ||
1960 isa<IntegerLiteral>(Ex) ||
1961 isa<FloatingLiteral>(Ex))) {
1962 StringRef StartName = Lexer::getImmediateMacroNameForDiagnostics(LocStart,
1963 BRC.getSourceManager(), BRC.getASTContext().getLangOpts());
1964 StringRef EndName = Lexer::getImmediateMacroNameForDiagnostics(LocEnd,
1965 BRC.getSourceManager(), BRC.getASTContext().getLangOpts());
1966 bool beginAndEndAreTheSameMacro = StartName.equals(EndName);
1968 bool partOfParentMacro = false;
1969 if (ParentEx->getBeginLoc().isMacroID()) {
1970 StringRef PName = Lexer::getImmediateMacroNameForDiagnostics(
1971 ParentEx->getBeginLoc(), BRC.getSourceManager(),
1972 BRC.getASTContext().getLangOpts());
1973 partOfParentMacro = PName.equals(StartName);
1976 if (beginAndEndAreTheSameMacro && !partOfParentMacro ) {
1977 // Get the location of the macro name as written by the caller.
1978 SourceLocation Loc = LocStart;
1979 while (LocStart.isMacroID()) {
1981 LocStart = BRC.getSourceManager().getImmediateMacroCallerLoc(LocStart);
1983 StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics(
1984 Loc, BRC.getSourceManager(), BRC.getASTContext().getLangOpts());
1986 // Return the macro name.
1992 if (const auto *DR = dyn_cast<DeclRefExpr>(Ex)) {
1993 const bool quotes = isa<VarDecl>(DR->getDecl());
1996 const LocationContext *LCtx = N->getLocationContext();
1997 const ProgramState *state = N->getState().get();
1998 if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
1999 LCtx).getAsRegion()) {
2000 if (report.isInteresting(R))
2003 const ProgramState *state = N->getState().get();
2004 SVal V = state->getSVal(R);
2005 if (report.isInteresting(V))
2010 Out << DR->getDecl()->getDeclName().getAsString();
2016 if (const auto *IL = dyn_cast<IntegerLiteral>(Ex)) {
2017 QualType OriginalTy = OriginalExpr->getType();
2018 if (OriginalTy->isPointerType()) {
2019 if (IL->getValue() == 0) {
2024 else if (OriginalTy->isObjCObjectPointerType()) {
2025 if (IL->getValue() == 0) {
2031 Out << IL->getValue();
2038 std::shared_ptr<PathDiagnosticPiece>
2039 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, const BinaryOperator *BExpr,
2040 const bool tookTrue, BugReporterContext &BRC,
2041 BugReport &R, const ExplodedNode *N) {
2042 bool shouldInvert = false;
2043 Optional<bool> shouldPrune;
2045 SmallString<128> LhsString, RhsString;
2047 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
2048 const bool isVarLHS = patternMatch(BExpr->getLHS(), BExpr, OutLHS,
2049 BRC, R, N, shouldPrune);
2050 const bool isVarRHS = patternMatch(BExpr->getRHS(), BExpr, OutRHS,
2051 BRC, R, N, shouldPrune);
2053 shouldInvert = !isVarLHS && isVarRHS;
2056 BinaryOperator::Opcode Op = BExpr->getOpcode();
2058 if (BinaryOperator::isAssignmentOp(Op)) {
2059 // For assignment operators, all that we care about is that the LHS
2060 // evaluates to "true" or "false".
2061 return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
2065 // For non-assignment operations, we require that we can understand
2066 // both the LHS and RHS.
2067 if (LhsString.empty() || RhsString.empty() ||
2068 !BinaryOperator::isComparisonOp(Op) || Op == BO_Cmp)
2071 // Should we invert the strings if the LHS is not a variable name?
2072 SmallString<256> buf;
2073 llvm::raw_svector_ostream Out(buf);
2074 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
2076 // Do we need to invert the opcode?
2080 case BO_LT: Op = BO_GT; break;
2081 case BO_GT: Op = BO_LT; break;
2082 case BO_LE: Op = BO_GE; break;
2083 case BO_GE: Op = BO_LE; break;
2088 case BO_EQ: Op = BO_NE; break;
2089 case BO_NE: Op = BO_EQ; break;
2090 case BO_LT: Op = BO_GE; break;
2091 case BO_GT: Op = BO_LE; break;
2092 case BO_LE: Op = BO_GT; break;
2093 case BO_GE: Op = BO_LT; break;
2103 Out << "not equal to ";
2106 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
2110 Out << (shouldInvert ? LhsString : RhsString);
2111 const LocationContext *LCtx = N->getLocationContext();
2112 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
2113 auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2114 if (shouldPrune.hasValue())
2115 event->setPrunable(shouldPrune.getValue());
2119 std::shared_ptr<PathDiagnosticPiece> ConditionBRVisitor::VisitConditionVariable(
2120 StringRef LhsString, const Expr *CondVarExpr, const bool tookTrue,
2121 BugReporterContext &BRC, BugReport &report, const ExplodedNode *N) {
2122 // FIXME: If there's already a constraint tracker for this variable,
2123 // we shouldn't emit anything here (c.f. the double note in
2124 // test/Analysis/inlining/path-notes.c)
2125 SmallString<256> buf;
2126 llvm::raw_svector_ostream Out(buf);
2127 Out << "Assuming " << LhsString << " is ";
2129 QualType Ty = CondVarExpr->getType();
2131 if (Ty->isPointerType())
2132 Out << (tookTrue ? "not null" : "null");
2133 else if (Ty->isObjCObjectPointerType())
2134 Out << (tookTrue ? "not nil" : "nil");
2135 else if (Ty->isBooleanType())
2136 Out << (tookTrue ? "true" : "false");
2137 else if (Ty->isIntegralOrEnumerationType())
2138 Out << (tookTrue ? "non-zero" : "zero");
2142 const LocationContext *LCtx = N->getLocationContext();
2143 PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
2144 auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2146 if (const auto *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) {
2147 if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
2148 const ProgramState *state = N->getState().get();
2149 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
2150 if (report.isInteresting(R))
2151 event->setPrunable(false);
2159 std::shared_ptr<PathDiagnosticPiece>
2160 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, const DeclRefExpr *DR,
2161 const bool tookTrue, BugReporterContext &BRC,
2162 BugReport &report, const ExplodedNode *N) {
2163 const auto *VD = dyn_cast<VarDecl>(DR->getDecl());
2167 SmallString<256> Buf;
2168 llvm::raw_svector_ostream Out(Buf);
2170 Out << "Assuming '" << VD->getDeclName() << "' is ";
2172 QualType VDTy = VD->getType();
2174 if (VDTy->isPointerType())
2175 Out << (tookTrue ? "non-null" : "null");
2176 else if (VDTy->isObjCObjectPointerType())
2177 Out << (tookTrue ? "non-nil" : "nil");
2178 else if (VDTy->isScalarType())
2179 Out << (tookTrue ? "not equal to 0" : "0");
2183 const LocationContext *LCtx = N->getLocationContext();
2184 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
2185 auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2187 const ProgramState *state = N->getState().get();
2188 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
2189 if (report.isInteresting(R))
2190 event->setPrunable(false);
2192 SVal V = state->getSVal(R);
2193 if (report.isInteresting(V))
2194 event->setPrunable(false);
2197 return std::move(event);
2200 const char *const ConditionBRVisitor::GenericTrueMessage =
2201 "Assuming the condition is true";
2202 const char *const ConditionBRVisitor::GenericFalseMessage =
2203 "Assuming the condition is false";
2205 bool ConditionBRVisitor::isPieceMessageGeneric(
2206 const PathDiagnosticPiece *Piece) {
2207 return Piece->getString() == GenericTrueMessage ||
2208 Piece->getString() == GenericFalseMessage;
2211 void LikelyFalsePositiveSuppressionBRVisitor::finalizeVisitor(
2212 BugReporterContext &BRC, const ExplodedNode *N, BugReport &BR) {
2213 // Here we suppress false positives coming from system headers. This list is
2214 // based on known issues.
2215 AnalyzerOptions &Options = BRC.getAnalyzerOptions();
2216 const Decl *D = N->getLocationContext()->getDecl();
2218 if (AnalysisDeclContext::isInStdNamespace(D)) {
2219 // Skip reports within the 'std' namespace. Although these can sometimes be
2220 // the user's fault, we currently don't report them very well, and
2221 // Note that this will not help for any other data structure libraries, like
2222 // TR1, Boost, or llvm/ADT.
2223 if (Options.ShouldSuppressFromCXXStandardLibrary) {
2224 BR.markInvalid(getTag(), nullptr);
2227 // If the complete 'std' suppression is not enabled, suppress reports
2228 // from the 'std' namespace that are known to produce false positives.
2230 // The analyzer issues a false use-after-free when std::list::pop_front
2231 // or std::list::pop_back are called multiple times because we cannot
2232 // reason about the internal invariants of the data structure.
2233 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
2234 const CXXRecordDecl *CD = MD->getParent();
2235 if (CD->getName() == "list") {
2236 BR.markInvalid(getTag(), nullptr);
2241 // The analyzer issues a false positive when the constructor of
2242 // std::__independent_bits_engine from algorithms is used.
2243 if (const auto *MD = dyn_cast<CXXConstructorDecl>(D)) {
2244 const CXXRecordDecl *CD = MD->getParent();
2245 if (CD->getName() == "__independent_bits_engine") {
2246 BR.markInvalid(getTag(), nullptr);
2251 for (const LocationContext *LCtx = N->getLocationContext(); LCtx;
2252 LCtx = LCtx->getParent()) {
2253 const auto *MD = dyn_cast<CXXMethodDecl>(LCtx->getDecl());
2257 const CXXRecordDecl *CD = MD->getParent();
2258 // The analyzer issues a false positive on
2259 // std::basic_string<uint8_t> v; v.push_back(1);
2261 // std::u16string s; s += u'a';
2262 // because we cannot reason about the internal invariants of the
2264 if (CD->getName() == "basic_string") {
2265 BR.markInvalid(getTag(), nullptr);
2269 // The analyzer issues a false positive on
2270 // std::shared_ptr<int> p(new int(1)); p = nullptr;
2271 // because it does not reason properly about temporary destructors.
2272 if (CD->getName() == "shared_ptr") {
2273 BR.markInvalid(getTag(), nullptr);
2280 // Skip reports within the sys/queue.h macros as we do not have the ability to
2281 // reason about data structure shapes.
2282 SourceManager &SM = BRC.getSourceManager();
2283 FullSourceLoc Loc = BR.getLocation(SM).asLocation();
2284 while (Loc.isMacroID()) {
2285 Loc = Loc.getSpellingLoc();
2286 if (SM.getFilename(Loc).endswith("sys/queue.h")) {
2287 BR.markInvalid(getTag(), nullptr);
2293 std::shared_ptr<PathDiagnosticPiece>
2294 UndefOrNullArgVisitor::VisitNode(const ExplodedNode *N,
2295 BugReporterContext &BRC, BugReport &BR) {
2296 ProgramStateRef State = N->getState();
2297 ProgramPoint ProgLoc = N->getLocation();
2299 // We are only interested in visiting CallEnter nodes.
2300 Optional<CallEnter> CEnter = ProgLoc.getAs<CallEnter>();
2304 // Check if one of the arguments is the region the visitor is tracking.
2305 CallEventManager &CEMgr = BRC.getStateManager().getCallEventManager();
2306 CallEventRef<> Call = CEMgr.getCaller(CEnter->getCalleeContext(), State);
2308 ArrayRef<ParmVarDecl *> parms = Call->parameters();
2310 for (const auto ParamDecl : parms) {
2311 const MemRegion *ArgReg = Call->getArgSVal(Idx).getAsRegion();
2314 // Are we tracking the argument or its subregion?
2315 if ( !ArgReg || !R->isSubRegionOf(ArgReg->StripCasts()))
2318 // Check the function parameter type.
2319 assert(ParamDecl && "Formal parameter has no decl?");
2320 QualType T = ParamDecl->getType();
2322 if (!(T->isAnyPointerType() || T->isReferenceType())) {
2323 // Function can only change the value passed in by address.
2327 // If it is a const pointer value, the function does not intend to
2328 // change the value.
2329 if (T->getPointeeType().isConstQualified())
2332 // Mark the call site (LocationContext) as interesting if the value of the
2333 // argument is undefined or '0'/'NULL'.
2334 SVal BoundVal = State->getSVal(R);
2335 if (BoundVal.isUndef() || BoundVal.isZeroConstant()) {
2336 BR.markInteresting(CEnter->getCalleeContext());
2343 std::shared_ptr<PathDiagnosticPiece>
2344 CXXSelfAssignmentBRVisitor::VisitNode(const ExplodedNode *Succ,
2345 BugReporterContext &BRC, BugReport &) {
2349 const auto Edge = Succ->getLocation().getAs<BlockEdge>();
2350 if (!Edge.hasValue())
2353 auto Tag = Edge->getTag();
2357 if (Tag->getTagDescription() != "cplusplus.SelfAssignment")
2363 dyn_cast<CXXMethodDecl>(Succ->getCodeDecl().getAsFunction());
2364 assert(Met && "Not a C++ method.");
2365 assert((Met->isCopyAssignmentOperator() || Met->isMoveAssignmentOperator()) &&
2366 "Not a copy/move assignment operator.");
2368 const auto *LCtx = Edge->getLocationContext();
2370 const auto &State = Succ->getState();
2371 auto &SVB = State->getStateManager().getSValBuilder();
2374 State->getSVal(State->getRegion(Met->getParamDecl(0), LCtx));
2376 State->getSVal(SVB.getCXXThis(Met, LCtx->getStackFrame()));
2378 auto L = PathDiagnosticLocation::create(Met, BRC.getSourceManager());
2380 if (!L.isValid() || !L.asLocation().isValid())
2383 SmallString<256> Buf;
2384 llvm::raw_svector_ostream Out(Buf);
2386 Out << "Assuming " << Met->getParamDecl(0)->getName() <<
2387 ((Param == This) ? " == " : " != ") << "*this";
2389 auto Piece = std::make_shared<PathDiagnosticEventPiece>(L, Out.str());
2390 Piece->addRange(Met->getSourceRange());
2392 return std::move(Piece);
2395 std::shared_ptr<PathDiagnosticPiece>
2396 TaintBugVisitor::VisitNode(const ExplodedNode *N,
2397 BugReporterContext &BRC, BugReport &) {
2399 // Find the ExplodedNode where the taint was first introduced
2400 if (!N->getState()->isTainted(V) || N->getFirstPred()->getState()->isTainted(V))
2403 const Stmt *S = PathDiagnosticLocation::getStmt(N);
2407 const LocationContext *NCtx = N->getLocationContext();
2408 PathDiagnosticLocation L =
2409 PathDiagnosticLocation::createBegin(S, BRC.getSourceManager(), NCtx);
2410 if (!L.isValid() || !L.asLocation().isValid())
2413 return std::make_shared<PathDiagnosticEventPiece>(L, "Taint originated here");
2416 FalsePositiveRefutationBRVisitor::FalsePositiveRefutationBRVisitor()
2417 : Constraints(ConstraintRangeTy::Factory().getEmptyMap()) {}
2419 void FalsePositiveRefutationBRVisitor::finalizeVisitor(
2420 BugReporterContext &BRC, const ExplodedNode *EndPathNode, BugReport &BR) {
2421 // Collect new constraints
2422 VisitNode(EndPathNode, BRC, BR);
2424 // Create a refutation manager
2425 SMTSolverRef RefutationSolver = CreateZ3Solver();
2426 ASTContext &Ctx = BRC.getASTContext();
2428 // Add constraints to the solver
2429 for (const auto &I : Constraints) {
2430 const SymbolRef Sym = I.first;
2431 auto RangeIt = I.second.begin();
2433 SMTExprRef Constraints = SMTConv::getRangeExpr(
2434 RefutationSolver, Ctx, Sym, RangeIt->From(), RangeIt->To(),
2436 while ((++RangeIt) != I.second.end()) {
2437 Constraints = RefutationSolver->mkOr(
2438 Constraints, SMTConv::getRangeExpr(RefutationSolver, Ctx, Sym,
2439 RangeIt->From(), RangeIt->To(),
2443 RefutationSolver->addConstraint(Constraints);
2446 // And check for satisfiability
2447 Optional<bool> isSat = RefutationSolver->check();
2448 if (!isSat.hasValue())
2451 if (!isSat.getValue())
2452 BR.markInvalid("Infeasible constraints", EndPathNode->getLocationContext());
2455 std::shared_ptr<PathDiagnosticPiece>
2456 FalsePositiveRefutationBRVisitor::VisitNode(const ExplodedNode *N,
2457 BugReporterContext &,
2459 // Collect new constraints
2460 const ConstraintRangeTy &NewCs = N->getState()->get<ConstraintRange>();
2461 ConstraintRangeTy::Factory &CF =
2462 N->getState()->get_context<ConstraintRange>();
2464 // Add constraints if we don't have them yet
2465 for (auto const &C : NewCs) {
2466 const SymbolRef &Sym = C.first;
2467 if (!Constraints.contains(Sym)) {
2468 Constraints = CF.add(Constraints, Sym, C.second);
2475 void FalsePositiveRefutationBRVisitor::Profile(
2476 llvm::FoldingSetNodeID &ID) const {
2478 ID.AddPointer(&Tag);