1 //===- BugReporterVisitors.cpp - Helpers for reporting bugs ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines a set of BugReporter "visitors" which can be used to
11 //  enhance the diagnostics reported for a bug.
12 //
13 //===----------------------------------------------------------------------===//
14 
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/Analysis/AnalysisDeclContext.h"
26 #include "clang/Analysis/CFG.h"
27 #include "clang/Analysis/CFGStmtMap.h"
28 #include "clang/Analysis/ProgramPoint.h"
29 #include "clang/Basic/IdentifierTable.h"
30 #include "clang/Basic/LLVM.h"
31 #include "clang/Basic/SourceLocation.h"
32 #include "clang/Basic/SourceManager.h"
33 #include "clang/Lex/Lexer.h"
34 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
35 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
36 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
37 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
38 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
39 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
40 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
41 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
42 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
43 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
44 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
45 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
46 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
47 #include "llvm/ADT/ArrayRef.h"
48 #include "llvm/ADT/None.h"
49 #include "llvm/ADT/Optional.h"
50 #include "llvm/ADT/STLExtras.h"
51 #include "llvm/ADT/SmallPtrSet.h"
52 #include "llvm/ADT/SmallString.h"
53 #include "llvm/ADT/SmallVector.h"
54 #include "llvm/ADT/StringExtras.h"
55 #include "llvm/ADT/StringRef.h"
56 #include "llvm/Support/Casting.h"
57 #include "llvm/Support/ErrorHandling.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include <cassert>
60 #include <deque>
61 #include <memory>
62 #include <string>
63 #include <utility>
64 
65 using namespace clang;
66 using namespace ento;
67 
68 //===----------------------------------------------------------------------===//
69 // Utility functions.
70 //===----------------------------------------------------------------------===//
71 
72 bool bugreporter::isDeclRefExprToReference(const Expr *E) {
73   if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
74     return DRE->getDecl()->getType()->isReferenceType();
75   return false;
76 }
77 
78 /// Given that expression S represents a pointer that would be dereferenced,
79 /// try to find a sub-expression from which the pointer came from.
80 /// This is used for tracking down origins of a null or undefined value:
81 /// "this is null because that is null because that is null" etc.
82 /// We wipe away field and element offsets because they merely add offsets.
83 /// We also wipe away all casts except lvalue-to-rvalue casts, because the
84 /// latter represent an actual pointer dereference; however, we remove
85 /// the final lvalue-to-rvalue cast before returning from this function
86 /// because it demonstrates more clearly from where the pointer rvalue was
87 /// loaded. Examples:
88 ///   x->y.z      ==>  x (lvalue)
89 ///   foo()->y.z  ==>  foo() (rvalue)
90 const Expr *bugreporter::getDerefExpr(const Stmt *S) {
91   const auto *E = dyn_cast<Expr>(S);
92   if (!E)
93     return nullptr;
94 
95   while (true) {
96     if (const auto *CE = dyn_cast<CastExpr>(E)) {
97       if (CE->getCastKind() == CK_LValueToRValue) {
98         // This cast represents the load we're looking for.
99         break;
100       }
101       E = CE->getSubExpr();
102     } else if (const auto *B = dyn_cast<BinaryOperator>(E)) {
103       // Pointer arithmetic: '*(x + 2)' -> 'x') etc.
104       if (B->getType()->isPointerType()) {
105         if (B->getLHS()->getType()->isPointerType()) {
106           E = B->getLHS();
107         } else if (B->getRHS()->getType()->isPointerType()) {
108           E = B->getRHS();
109         } else {
110           break;
111         }
112       } else {
113         // Probably more arithmetic can be pattern-matched here,
114         // but for now give up.
115         break;
116       }
117     } else if (const auto *U = dyn_cast<UnaryOperator>(E)) {
118       if (U->getOpcode() == UO_Deref || U->getOpcode() == UO_AddrOf ||
119           (U->isIncrementDecrementOp() && U->getType()->isPointerType())) {
120         // Operators '*' and '&' don't actually mean anything.
121         // We look at casts instead.
122         E = U->getSubExpr();
123       } else {
124         // Probably more arithmetic can be pattern-matched here,
125         // but for now give up.
126         break;
127       }
128     }
129     // Pattern match for a few useful cases: a[0], p->f, *p etc.
130     else if (const auto *ME = dyn_cast<MemberExpr>(E)) {
131       E = ME->getBase();
132     } else if (const auto *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
133       E = IvarRef->getBase();
134     } else if (const auto *AE = dyn_cast<ArraySubscriptExpr>(E)) {
135       E = AE->getBase();
136     } else if (const auto *PE = dyn_cast<ParenExpr>(E)) {
137       E = PE->getSubExpr();
138     } else {
139       // Other arbitrary stuff.
140       break;
141     }
142   }
143 
144   // Special case: remove the final lvalue-to-rvalue cast, but do not recurse
145   // deeper into the sub-expression. This way we return the lvalue from which
146   // our pointer rvalue was loaded.
147   if (const auto *CE = dyn_cast<ImplicitCastExpr>(E))
148     if (CE->getCastKind() == CK_LValueToRValue)
149       E = CE->getSubExpr();
150 
151   return E;
152 }
153 
154 const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) {
155   const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
156   if (const auto *BE = dyn_cast<BinaryOperator>(S))
157     return BE->getRHS();
158   return nullptr;
159 }
160 
161 const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) {
162   const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
163   if (const auto *RS = dyn_cast<ReturnStmt>(S))
164     return RS->getRetValue();
165   return nullptr;
166 }
167 
168 //===----------------------------------------------------------------------===//
169 // Definitions for bug reporter visitors.
170 //===----------------------------------------------------------------------===//
171 
172 std::unique_ptr<PathDiagnosticPiece>
173 BugReporterVisitor::getEndPath(BugReporterContext &BRC,
174                                const ExplodedNode *EndPathNode, BugReport &BR) {
175   return nullptr;
176 }
177 
178 std::unique_ptr<PathDiagnosticPiece> BugReporterVisitor::getDefaultEndPath(
179     BugReporterContext &BRC, const ExplodedNode *EndPathNode, BugReport &BR) {
180   PathDiagnosticLocation L =
181     PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager());
182 
183   const auto &Ranges = BR.getRanges();
184 
185   // Only add the statement itself as a range if we didn't specify any
186   // special ranges for this report.
187   auto P = llvm::make_unique<PathDiagnosticEventPiece>(
188       L, BR.getDescription(), Ranges.begin() == Ranges.end());
189   for (SourceRange Range : Ranges)
190     P->addRange(Range);
191 
192   return std::move(P);
193 }
194 
195 /// \return name of the macro inside the location \p Loc.
196 static StringRef getMacroName(SourceLocation Loc,
197     BugReporterContext &BRC) {
198   return Lexer::getImmediateMacroName(
199       Loc,
200       BRC.getSourceManager(),
201       BRC.getASTContext().getLangOpts());
202 }
203 
204 /// \return Whether given spelling location corresponds to an expansion
205 /// of a function-like macro.
206 static bool isFunctionMacroExpansion(SourceLocation Loc,
207                                 const SourceManager &SM) {
208   if (!Loc.isMacroID())
209     return false;
210   while (SM.isMacroArgExpansion(Loc))
211     Loc = SM.getImmediateExpansionRange(Loc).first;
212   std::pair<FileID, unsigned> TLInfo = SM.getDecomposedLoc(Loc);
213   SrcMgr::SLocEntry SE = SM.getSLocEntry(TLInfo.first);
214   const SrcMgr::ExpansionInfo &EInfo = SE.getExpansion();
215   return EInfo.isFunctionMacroExpansion();
216 }
217 
218 namespace {
219 
220 /// Put a diagnostic on return statement of all inlined functions
221 /// for which  the region of interest \p RegionOfInterest was passed into,
222 /// but not written inside, and it has caused an undefined read or a null
223 /// pointer dereference outside.
224 class NoStoreFuncVisitor final
225     : public BugReporterVisitorImpl<NoStoreFuncVisitor> {
226   const SubRegion *RegionOfInterest;
227   static constexpr const char *DiagnosticsMsg =
228       "Returning without writing to '";
229   bool Initialized = false;
230 
231   /// Frames writing into \c RegionOfInterest.
232   /// This visitor generates a note only if a function does not write into
233   /// a region of interest. This information is not immediately available
234   /// by looking at the node associated with the exit from the function
235   /// (usually the return statement). To avoid recomputing the same information
236   /// many times (going up the path for each node and checking whether the
237   /// region was written into) we instead pre-compute and store all
238   /// stack frames along the path which write into the region of interest
239   /// on the first \c VisitNode invocation.
240   llvm::SmallPtrSet<const StackFrameContext *, 32> FramesModifyingRegion;
241 
242 public:
243   NoStoreFuncVisitor(const SubRegion *R) : RegionOfInterest(R) {}
244 
245   void Profile(llvm::FoldingSetNodeID &ID) const override {
246     static int Tag = 0;
247     ID.AddPointer(&Tag);
248   }
249 
250   std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
251                                                  const ExplodedNode *PrevN,
252                                                  BugReporterContext &BRC,
253                                                  BugReport &BR) override {
254     if (!Initialized) {
255       findModifyingFrames(N);
256       Initialized = true;
257     }
258 
259     const LocationContext *Ctx = N->getLocationContext();
260     const StackFrameContext *SCtx = Ctx->getCurrentStackFrame();
261     ProgramStateRef State = N->getState();
262     auto CallExitLoc = N->getLocationAs<CallExitBegin>();
263 
264     // No diagnostic if region was modified inside the frame.
265     if (!CallExitLoc || FramesModifyingRegion.count(SCtx))
266       return nullptr;
267 
268     CallEventRef<> Call =
269         BRC.getStateManager().getCallEventManager().getCaller(SCtx, State);
270 
271     const PrintingPolicy &PP = BRC.getASTContext().getPrintingPolicy();
272     const SourceManager &SM = BRC.getSourceManager();
273     if (const auto *CCall = dyn_cast<CXXConstructorCall>(Call)) {
274       const MemRegion *ThisRegion = CCall->getCXXThisVal().getAsRegion();
275       if (RegionOfInterest->isSubRegionOf(ThisRegion) &&
276           !CCall->getDecl()->isImplicit())
277         return notModifiedInConstructorDiagnostics(Ctx, SM, PP, *CallExitLoc,
278                                                    CCall, ThisRegion);
279     }
280 
281     ArrayRef<ParmVarDecl *> parameters = getCallParameters(Call);
282     for (unsigned I = 0, E = Call->getNumArgs(); I != E; ++I) {
283       const ParmVarDecl *PVD = parameters[I];
284       SVal S = Call->getArgSVal(I);
285       unsigned IndirectionLevel = 1;
286       QualType T = PVD->getType();
287       while (const MemRegion *R = S.getAsRegion()) {
288         if (RegionOfInterest->isSubRegionOf(R) &&
289             !isPointerToConst(PVD->getType()))
290           return notModifiedDiagnostics(
291               Ctx, SM, PP, *CallExitLoc, Call, PVD, R, IndirectionLevel);
292         QualType PT = T->getPointeeType();
293         if (PT.isNull() || PT->isVoidType()) break;
294         S = State->getSVal(R, PT);
295         T = PT;
296         IndirectionLevel++;
297       }
298     }
299 
300     return nullptr;
301   }
302 
303 private:
304   /// Write to \c FramesModifyingRegion all stack frames along
305   /// the path which modify \c RegionOfInterest.
306   void findModifyingFrames(const ExplodedNode *N) {
307     ProgramStateRef LastReturnState;
308     do {
309       ProgramStateRef State = N->getState();
310       auto CallExitLoc = N->getLocationAs<CallExitBegin>();
311       if (CallExitLoc) {
312         LastReturnState = State;
313       }
314       if (LastReturnState &&
315           wasRegionOfInterestModifiedAt(N, LastReturnState)) {
316         const StackFrameContext *SCtx =
317             N->getLocationContext()->getCurrentStackFrame();
318         while (!SCtx->inTopFrame()) {
319           auto p = FramesModifyingRegion.insert(SCtx);
320           if (!p.second)
321             break; // Frame and all its parents already inserted.
322           SCtx = SCtx->getParent()->getCurrentStackFrame();
323         }
324       }
325 
326       N = N->getFirstPred();
327     } while (N);
328   }
329 
330   /// \return Whether \c RegionOfInterest was modified at \p N,
331   /// where \p ReturnState is a state associated with the return
332   /// from the current frame.
333   bool wasRegionOfInterestModifiedAt(const ExplodedNode *N,
334                                      ProgramStateRef ReturnState) {
335     SVal ValueAtReturn = ReturnState->getSVal(RegionOfInterest);
336 
337     // Writing into region of interest.
338     if (auto PS = N->getLocationAs<PostStmt>())
339       if (auto *BO = PS->getStmtAs<BinaryOperator>())
340         if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(
341                                         N->getSVal(BO->getLHS()).getAsRegion()))
342           return true;
343 
344     // SVal after the state is possibly different.
345     SVal ValueAtN = N->getState()->getSVal(RegionOfInterest);
346     if (!ReturnState->areEqual(ValueAtN, ValueAtReturn).isConstrainedTrue() &&
347         (!ValueAtN.isUndef() || !ValueAtReturn.isUndef()))
348       return true;
349 
350     return false;
351   }
352 
353   /// Get parameters associated with runtime definition in order
354   /// to get the correct parameter name.
355   ArrayRef<ParmVarDecl *> getCallParameters(CallEventRef<> Call) {
356     // Use runtime definition, if available.
357     RuntimeDefinition RD = Call->getRuntimeDefinition();
358     if (const auto *FD = dyn_cast_or_null<FunctionDecl>(RD.getDecl()))
359       return FD->parameters();
360 
361     return Call->parameters();
362   }
363 
364   /// \return whether \p Ty points to a const type, or is a const reference.
365   bool isPointerToConst(QualType Ty) {
366     return !Ty->getPointeeType().isNull() &&
367            Ty->getPointeeType().getCanonicalType().isConstQualified();
368   }
369 
370   std::shared_ptr<PathDiagnosticPiece> notModifiedInConstructorDiagnostics(
371       const LocationContext *Ctx,
372       const SourceManager &SM,
373       const PrintingPolicy &PP,
374       CallExitBegin &CallExitLoc,
375       const CXXConstructorCall *Call,
376       const MemRegion *ArgRegion) {
377     SmallString<256> sbuf;
378     llvm::raw_svector_ostream os(sbuf);
379     os << DiagnosticsMsg;
380     bool out = prettyPrintRegionName(
381         "this", "->", /*IsReference=*/true,
382         /*IndirectionLevel=*/1, ArgRegion, os, PP);
383 
384     // Return nothing if we have failed to pretty-print.
385     if (!out)
386       return nullptr;
387 
388     os << "'";
389     PathDiagnosticLocation L =
390         getPathDiagnosticLocation(nullptr, SM, Ctx, Call);
391     return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
392   }
393 
394   /// \p IndirectionLevel How many times \c ArgRegion has to be dereferenced
395   /// before we get to the super region of \c RegionOfInterest
396   std::shared_ptr<PathDiagnosticPiece>
397   notModifiedDiagnostics(const LocationContext *Ctx,
398                          const SourceManager &SM,
399                          const PrintingPolicy &PP,
400                          CallExitBegin &CallExitLoc,
401                          CallEventRef<> Call,
402                          const ParmVarDecl *PVD,
403                          const MemRegion *ArgRegion,
404                          unsigned IndirectionLevel) {
405     PathDiagnosticLocation L = getPathDiagnosticLocation(
406         CallExitLoc.getReturnStmt(), SM, Ctx, Call);
407     SmallString<256> sbuf;
408     llvm::raw_svector_ostream os(sbuf);
409     os << DiagnosticsMsg;
410     bool IsReference = PVD->getType()->isReferenceType();
411     const char *Sep = IsReference && IndirectionLevel == 1 ? "." : "->";
412     bool Success = prettyPrintRegionName(
413         PVD->getQualifiedNameAsString().c_str(),
414         Sep, IsReference, IndirectionLevel, ArgRegion, os, PP);
415 
416     // Print the parameter name if the pretty-printing has failed.
417     if (!Success)
418       PVD->printQualifiedName(os);
419     os << "'";
420     return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
421   }
422 
423   /// \return a path diagnostic location for the optionally
424   /// present return statement \p RS.
425   PathDiagnosticLocation getPathDiagnosticLocation(const ReturnStmt *RS,
426                                                    const SourceManager &SM,
427                                                    const LocationContext *Ctx,
428                                                    CallEventRef<> Call) {
429     if (RS)
430       return PathDiagnosticLocation::createBegin(RS, SM, Ctx);
431     return PathDiagnosticLocation(
432         Call->getRuntimeDefinition().getDecl()->getSourceRange().getEnd(), SM);
433   }
434 
435   /// Pretty-print region \p ArgRegion starting from parent to \p os.
436   /// \return whether printing has succeeded
437   bool prettyPrintRegionName(const char *TopRegionName,
438                              const char *Sep,
439                              bool IsReference,
440                              int IndirectionLevel,
441                              const MemRegion *ArgRegion,
442                              llvm::raw_svector_ostream &os,
443                              const PrintingPolicy &PP) {
444     SmallVector<const MemRegion *, 5> Subregions;
445     const MemRegion *R = RegionOfInterest;
446     while (R != ArgRegion) {
447       if (!(isa<FieldRegion>(R) || isa<CXXBaseObjectRegion>(R)))
448         return false; // Pattern-matching failed.
449       Subregions.push_back(R);
450       R = dyn_cast<SubRegion>(R)->getSuperRegion();
451     }
452     bool IndirectReference = !Subregions.empty();
453 
454     if (IndirectReference)
455       IndirectionLevel--; // Due to "->" symbol.
456 
457     if (IsReference)
458       IndirectionLevel--; // Due to reference semantics.
459 
460     bool ShouldSurround = IndirectReference && IndirectionLevel > 0;
461 
462     if (ShouldSurround)
463       os << "(";
464     for (int i = 0; i < IndirectionLevel; i++)
465       os << "*";
466     os << TopRegionName;
467     if (ShouldSurround)
468       os << ")";
469 
470     for (auto I = Subregions.rbegin(), E = Subregions.rend(); I != E; ++I) {
471       if (const auto *FR = dyn_cast<FieldRegion>(*I)) {
472         os << Sep;
473         FR->getDecl()->getDeclName().print(os, PP);
474         Sep = ".";
475       } else if (isa<CXXBaseObjectRegion>(*I)) {
476         continue; // Just keep going up to the base region.
477       } else {
478         llvm_unreachable("Previous check has missed an unexpected region");
479       }
480     }
481     return true;
482   }
483 };
484 
485 class MacroNullReturnSuppressionVisitor final
486     : public BugReporterVisitorImpl<MacroNullReturnSuppressionVisitor> {
487   const SubRegion *RegionOfInterest;
488 
489 public:
490   MacroNullReturnSuppressionVisitor(const SubRegion *R) : RegionOfInterest(R) {}
491 
492   static void *getTag() {
493     static int Tag = 0;
494     return static_cast<void *>(&Tag);
495   }
496 
497   void Profile(llvm::FoldingSetNodeID &ID) const override {
498     ID.AddPointer(getTag());
499   }
500 
501   std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
502                                                  const ExplodedNode *PrevN,
503                                                  BugReporterContext &BRC,
504                                                  BugReport &BR) override {
505     auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>();
506     if (!BugPoint)
507       return nullptr;
508 
509     const SourceManager &SMgr = BRC.getSourceManager();
510     if (auto Loc = matchAssignment(N, BRC)) {
511       if (isFunctionMacroExpansion(*Loc, SMgr)) {
512         std::string MacroName = getMacroName(*Loc, BRC);
513         SourceLocation BugLoc = BugPoint->getStmt()->getLocStart();
514         if (!BugLoc.isMacroID() || getMacroName(BugLoc, BRC) != MacroName)
515           BR.markInvalid(getTag(), MacroName.c_str());
516       }
517     }
518     return nullptr;
519   }
520 
521   static void addMacroVisitorIfNecessary(
522         const ExplodedNode *N, const MemRegion *R,
523         bool EnableNullFPSuppression, BugReport &BR,
524         const SVal V) {
525     AnalyzerOptions &Options = N->getState()->getStateManager()
526         .getOwningEngine()->getAnalysisManager().options;
527     if (EnableNullFPSuppression && Options.shouldSuppressNullReturnPaths()
528           && V.getAs<Loc>())
529       BR.addVisitor(llvm::make_unique<MacroNullReturnSuppressionVisitor>(
530               R->getAs<SubRegion>()));
531   }
532 
533 private:
534   /// \return Source location of right hand side of an assignment
535   /// into \c RegionOfInterest, empty optional if none found.
536   Optional<SourceLocation> matchAssignment(const ExplodedNode *N,
537                                            BugReporterContext &BRC) {
538     const Stmt *S = PathDiagnosticLocation::getStmt(N);
539     ProgramStateRef State = N->getState();
540     auto *LCtx = N->getLocationContext();
541     if (!S)
542       return None;
543 
544     if (const auto *DS = dyn_cast<DeclStmt>(S)) {
545       if (const auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl()))
546         if (const Expr *RHS = VD->getInit())
547           if (RegionOfInterest->isSubRegionOf(
548                   State->getLValue(VD, LCtx).getAsRegion()))
549             return RHS->getLocStart();
550     } else if (const auto *BO = dyn_cast<BinaryOperator>(S)) {
551       const MemRegion *R = N->getSVal(BO->getLHS()).getAsRegion();
552       const Expr *RHS = BO->getRHS();
553       if (BO->isAssignmentOp() && RegionOfInterest->isSubRegionOf(R)) {
554         return RHS->getLocStart();
555       }
556     }
557     return None;
558   }
559 };
560 
561 /// Emits an extra note at the return statement of an interesting stack frame.
562 ///
563 /// The returned value is marked as an interesting value, and if it's null,
564 /// adds a visitor to track where it became null.
565 ///
566 /// This visitor is intended to be used when another visitor discovers that an
567 /// interesting value comes from an inlined function call.
568 class ReturnVisitor : public BugReporterVisitorImpl<ReturnVisitor> {
569   const StackFrameContext *StackFrame;
570   enum {
571     Initial,
572     MaybeUnsuppress,
573     Satisfied
574   } Mode = Initial;
575 
576   bool EnableNullFPSuppression;
577 
578 public:
579   ReturnVisitor(const StackFrameContext *Frame, bool Suppressed)
580       : StackFrame(Frame), EnableNullFPSuppression(Suppressed) {}
581 
582   static void *getTag() {
583     static int Tag = 0;
584     return static_cast<void *>(&Tag);
585   }
586 
587   void Profile(llvm::FoldingSetNodeID &ID) const override {
588     ID.AddPointer(ReturnVisitor::getTag());
589     ID.AddPointer(StackFrame);
590     ID.AddBoolean(EnableNullFPSuppression);
591   }
592 
593   /// Adds a ReturnVisitor if the given statement represents a call that was
594   /// inlined.
595   ///
596   /// This will search back through the ExplodedGraph, starting from the given
597   /// node, looking for when the given statement was processed. If it turns out
598   /// the statement is a call that was inlined, we add the visitor to the
599   /// bug report, so it can print a note later.
600   static void addVisitorIfNecessary(const ExplodedNode *Node, const Stmt *S,
601                                     BugReport &BR,
602                                     bool InEnableNullFPSuppression) {
603     if (!CallEvent::isCallStmt(S))
604       return;
605 
606     // First, find when we processed the statement.
607     do {
608       if (Optional<CallExitEnd> CEE = Node->getLocationAs<CallExitEnd>())
609         if (CEE->getCalleeContext()->getCallSite() == S)
610           break;
611       if (Optional<StmtPoint> SP = Node->getLocationAs<StmtPoint>())
612         if (SP->getStmt() == S)
613           break;
614 
615       Node = Node->getFirstPred();
616     } while (Node);
617 
618     // Next, step over any post-statement checks.
619     while (Node && Node->getLocation().getAs<PostStmt>())
620       Node = Node->getFirstPred();
621     if (!Node)
622       return;
623 
624     // Finally, see if we inlined the call.
625     Optional<CallExitEnd> CEE = Node->getLocationAs<CallExitEnd>();
626     if (!CEE)
627       return;
628 
629     const StackFrameContext *CalleeContext = CEE->getCalleeContext();
630     if (CalleeContext->getCallSite() != S)
631       return;
632 
633     // Check the return value.
634     ProgramStateRef State = Node->getState();
635     SVal RetVal = Node->getSVal(S);
636 
637     // Handle cases where a reference is returned and then immediately used.
638     if (cast<Expr>(S)->isGLValue())
639       if (Optional<Loc> LValue = RetVal.getAs<Loc>())
640         RetVal = State->getSVal(*LValue);
641 
642     // See if the return value is NULL. If so, suppress the report.
643     SubEngine *Eng = State->getStateManager().getOwningEngine();
644     assert(Eng && "Cannot file a bug report without an owning engine");
645     AnalyzerOptions &Options = Eng->getAnalysisManager().options;
646 
647     bool EnableNullFPSuppression = false;
648     if (InEnableNullFPSuppression && Options.shouldSuppressNullReturnPaths())
649       if (Optional<Loc> RetLoc = RetVal.getAs<Loc>())
650         EnableNullFPSuppression = State->isNull(*RetLoc).isConstrainedTrue();
651 
652     BR.markInteresting(CalleeContext);
653     BR.addVisitor(llvm::make_unique<ReturnVisitor>(CalleeContext,
654                                                    EnableNullFPSuppression));
655   }
656 
657   /// Returns true if any counter-suppression heuristics are enabled for
658   /// ReturnVisitor.
659   static bool hasCounterSuppression(AnalyzerOptions &Options) {
660     return Options.shouldAvoidSuppressingNullArgumentPaths();
661   }
662 
663   std::shared_ptr<PathDiagnosticPiece>
664   visitNodeInitial(const ExplodedNode *N, const ExplodedNode *PrevN,
665                    BugReporterContext &BRC, BugReport &BR) {
666     // Only print a message at the interesting return statement.
667     if (N->getLocationContext() != StackFrame)
668       return nullptr;
669 
670     Optional<StmtPoint> SP = N->getLocationAs<StmtPoint>();
671     if (!SP)
672       return nullptr;
673 
674     const auto *Ret = dyn_cast<ReturnStmt>(SP->getStmt());
675     if (!Ret)
676       return nullptr;
677 
678     // Okay, we're at the right return statement, but do we have the return
679     // value available?
680     ProgramStateRef State = N->getState();
681     SVal V = State->getSVal(Ret, StackFrame);
682     if (V.isUnknownOrUndef())
683       return nullptr;
684 
685     // Don't print any more notes after this one.
686     Mode = Satisfied;
687 
688     const Expr *RetE = Ret->getRetValue();
689     assert(RetE && "Tracking a return value for a void function");
690 
691     // Handle cases where a reference is returned and then immediately used.
692     Optional<Loc> LValue;
693     if (RetE->isGLValue()) {
694       if ((LValue = V.getAs<Loc>())) {
695         SVal RValue = State->getRawSVal(*LValue, RetE->getType());
696         if (RValue.getAs<DefinedSVal>())
697           V = RValue;
698       }
699     }
700 
701     // Ignore aggregate rvalues.
702     if (V.getAs<nonloc::LazyCompoundVal>() ||
703         V.getAs<nonloc::CompoundVal>())
704       return nullptr;
705 
706     RetE = RetE->IgnoreParenCasts();
707 
708     // If we can't prove the return value is 0, just mark it interesting, and
709     // make sure to track it into any further inner functions.
710     if (!State->isNull(V).isConstrainedTrue()) {
711       BR.markInteresting(V);
712       ReturnVisitor::addVisitorIfNecessary(N, RetE, BR,
713                                            EnableNullFPSuppression);
714       return nullptr;
715     }
716 
717     // If we're returning 0, we should track where that 0 came from.
718     bugreporter::trackNullOrUndefValue(N, RetE, BR, /*IsArg*/ false,
719                                        EnableNullFPSuppression);
720 
721     // Build an appropriate message based on the return value.
722     SmallString<64> Msg;
723     llvm::raw_svector_ostream Out(Msg);
724 
725     if (V.getAs<Loc>()) {
726       // If we have counter-suppression enabled, make sure we keep visiting
727       // future nodes. We want to emit a path note as well, in case
728       // the report is resurrected as valid later on.
729       ExprEngine &Eng = BRC.getBugReporter().getEngine();
730       AnalyzerOptions &Options = Eng.getAnalysisManager().options;
731       if (EnableNullFPSuppression && hasCounterSuppression(Options))
732         Mode = MaybeUnsuppress;
733 
734       if (RetE->getType()->isObjCObjectPointerType())
735         Out << "Returning nil";
736       else
737         Out << "Returning null pointer";
738     } else {
739       Out << "Returning zero";
740     }
741 
742     if (LValue) {
743       if (const MemRegion *MR = LValue->getAsRegion()) {
744         if (MR->canPrintPretty()) {
745           Out << " (reference to ";
746           MR->printPretty(Out);
747           Out << ")";
748         }
749       }
750     } else {
751       // FIXME: We should have a more generalized location printing mechanism.
752       if (const auto *DR = dyn_cast<DeclRefExpr>(RetE))
753         if (const auto *DD = dyn_cast<DeclaratorDecl>(DR->getDecl()))
754           Out << " (loaded from '" << *DD << "')";
755     }
756 
757     PathDiagnosticLocation L(Ret, BRC.getSourceManager(), StackFrame);
758     if (!L.isValid() || !L.asLocation().isValid())
759       return nullptr;
760 
761     return std::make_shared<PathDiagnosticEventPiece>(L, Out.str());
762   }
763 
764   std::shared_ptr<PathDiagnosticPiece>
765   visitNodeMaybeUnsuppress(const ExplodedNode *N, const ExplodedNode *PrevN,
766                            BugReporterContext &BRC, BugReport &BR) {
767 #ifndef NDEBUG
768     ExprEngine &Eng = BRC.getBugReporter().getEngine();
769     AnalyzerOptions &Options = Eng.getAnalysisManager().options;
770     assert(hasCounterSuppression(Options));
771 #endif
772 
773     // Are we at the entry node for this call?
774     Optional<CallEnter> CE = N->getLocationAs<CallEnter>();
775     if (!CE)
776       return nullptr;
777 
778     if (CE->getCalleeContext() != StackFrame)
779       return nullptr;
780 
781     Mode = Satisfied;
782 
783     // Don't automatically suppress a report if one of the arguments is
784     // known to be a null pointer. Instead, start tracking /that/ null
785     // value back to its origin.
786     ProgramStateManager &StateMgr = BRC.getStateManager();
787     CallEventManager &CallMgr = StateMgr.getCallEventManager();
788 
789     ProgramStateRef State = N->getState();
790     CallEventRef<> Call = CallMgr.getCaller(StackFrame, State);
791     for (unsigned I = 0, E = Call->getNumArgs(); I != E; ++I) {
792       Optional<Loc> ArgV = Call->getArgSVal(I).getAs<Loc>();
793       if (!ArgV)
794         continue;
795 
796       const Expr *ArgE = Call->getArgExpr(I);
797       if (!ArgE)
798         continue;
799 
800       // Is it possible for this argument to be non-null?
801       if (!State->isNull(*ArgV).isConstrainedTrue())
802         continue;
803 
804       if (bugreporter::trackNullOrUndefValue(N, ArgE, BR, /*IsArg=*/true,
805                                              EnableNullFPSuppression))
806         BR.removeInvalidation(ReturnVisitor::getTag(), StackFrame);
807 
808       // If we /can't/ track the null pointer, we should err on the side of
809       // false negatives, and continue towards marking this report invalid.
810       // (We will still look at the other arguments, though.)
811     }
812 
813     return nullptr;
814   }
815 
816   std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
817                                                  const ExplodedNode *PrevN,
818                                                  BugReporterContext &BRC,
819                                                  BugReport &BR) override {
820     switch (Mode) {
821     case Initial:
822       return visitNodeInitial(N, PrevN, BRC, BR);
823     case MaybeUnsuppress:
824       return visitNodeMaybeUnsuppress(N, PrevN, BRC, BR);
825     case Satisfied:
826       return nullptr;
827     }
828 
829     llvm_unreachable("Invalid visit mode!");
830   }
831 
832   std::unique_ptr<PathDiagnosticPiece> getEndPath(BugReporterContext &BRC,
833                                                   const ExplodedNode *N,
834                                                   BugReport &BR) override {
835     if (EnableNullFPSuppression)
836       BR.markInvalid(ReturnVisitor::getTag(), StackFrame);
837     return nullptr;
838   }
839 };
840 
841 } // namespace
842 
843 void FindLastStoreBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
844   static int tag = 0;
845   ID.AddPointer(&tag);
846   ID.AddPointer(R);
847   ID.Add(V);
848   ID.AddBoolean(EnableNullFPSuppression);
849 }
850 
851 /// Returns true if \p N represents the DeclStmt declaring and initializing
852 /// \p VR.
853 static bool isInitializationOfVar(const ExplodedNode *N, const VarRegion *VR) {
854   Optional<PostStmt> P = N->getLocationAs<PostStmt>();
855   if (!P)
856     return false;
857 
858   const DeclStmt *DS = P->getStmtAs<DeclStmt>();
859   if (!DS)
860     return false;
861 
862   if (DS->getSingleDecl() != VR->getDecl())
863     return false;
864 
865   const MemSpaceRegion *VarSpace = VR->getMemorySpace();
866   const auto *FrameSpace = dyn_cast<StackSpaceRegion>(VarSpace);
867   if (!FrameSpace) {
868     // If we ever directly evaluate global DeclStmts, this assertion will be
869     // invalid, but this still seems preferable to silently accepting an
870     // initialization that may be for a path-sensitive variable.
871     assert(VR->getDecl()->isStaticLocal() && "non-static stackless VarRegion");
872     return true;
873   }
874 
875   assert(VR->getDecl()->hasLocalStorage());
876   const LocationContext *LCtx = N->getLocationContext();
877   return FrameSpace->getStackFrame() == LCtx->getCurrentStackFrame();
878 }
879 
880 /// Show diagnostics for initializing or declaring a region \p R with a bad value.
881 void showBRDiagnostics(const char *action,
882     llvm::raw_svector_ostream& os,
883     const MemRegion *R,
884     SVal V,
885     const DeclStmt *DS) {
886   if (R->canPrintPretty()) {
887     R->printPretty(os);
888     os << " ";
889   }
890 
891   if (V.getAs<loc::ConcreteInt>()) {
892     bool b = false;
893     if (R->isBoundable()) {
894       if (const auto *TR = dyn_cast<TypedValueRegion>(R)) {
895         if (TR->getValueType()->isObjCObjectPointerType()) {
896           os << action << "nil";
897           b = true;
898         }
899       }
900     }
901     if (!b)
902       os << action << "a null pointer value";
903 
904   } else if (auto CVal = V.getAs<nonloc::ConcreteInt>()) {
905     os << action << CVal->getValue();
906   } else if (DS) {
907     if (V.isUndef()) {
908       if (isa<VarRegion>(R)) {
909         const auto *VD = cast<VarDecl>(DS->getSingleDecl());
910         if (VD->getInit()) {
911           os << (R->canPrintPretty() ? "initialized" : "Initializing")
912             << " to a garbage value";
913         } else {
914           os << (R->canPrintPretty() ? "declared" : "Declaring")
915             << " without an initial value";
916         }
917       }
918     } else {
919       os << (R->canPrintPretty() ? "initialized" : "Initialized")
920         << " here";
921     }
922   }
923 }
924 
925 /// Display diagnostics for passing bad region as a parameter.
926 static void showBRParamDiagnostics(llvm::raw_svector_ostream& os,
927     const VarRegion *VR,
928     SVal V) {
929   const auto *Param = cast<ParmVarDecl>(VR->getDecl());
930 
931   os << "Passing ";
932 
933   if (V.getAs<loc::ConcreteInt>()) {
934     if (Param->getType()->isObjCObjectPointerType())
935       os << "nil object reference";
936     else
937       os << "null pointer value";
938   } else if (V.isUndef()) {
939     os << "uninitialized value";
940   } else if (auto CI = V.getAs<nonloc::ConcreteInt>()) {
941     os << "the value " << CI->getValue();
942   } else {
943     os << "value";
944   }
945 
946   // Printed parameter indexes are 1-based, not 0-based.
947   unsigned Idx = Param->getFunctionScopeIndex() + 1;
948   os << " via " << Idx << llvm::getOrdinalSuffix(Idx) << " parameter";
949   if (VR->canPrintPretty()) {
950     os << " ";
951     VR->printPretty(os);
952   }
953 }
954 
955 /// Show default diagnostics for storing bad region.
956 static void showBRDefaultDiagnostics(llvm::raw_svector_ostream& os,
957     const MemRegion *R,
958     SVal V) {
959   if (V.getAs<loc::ConcreteInt>()) {
960     bool b = false;
961     if (R->isBoundable()) {
962       if (const auto *TR = dyn_cast<TypedValueRegion>(R)) {
963         if (TR->getValueType()->isObjCObjectPointerType()) {
964           os << "nil object reference stored";
965           b = true;
966         }
967       }
968     }
969     if (!b) {
970       if (R->canPrintPretty())
971         os << "Null pointer value stored";
972       else
973         os << "Storing null pointer value";
974     }
975 
976   } else if (V.isUndef()) {
977     if (R->canPrintPretty())
978       os << "Uninitialized value stored";
979     else
980       os << "Storing uninitialized value";
981 
982   } else if (auto CV = V.getAs<nonloc::ConcreteInt>()) {
983     if (R->canPrintPretty())
984       os << "The value " << CV->getValue() << " is assigned";
985     else
986       os << "Assigning " << CV->getValue();
987 
988   } else {
989     if (R->canPrintPretty())
990       os << "Value assigned";
991     else
992       os << "Assigning value";
993   }
994 
995   if (R->canPrintPretty()) {
996     os << " to ";
997     R->printPretty(os);
998   }
999 }
1000 
1001 std::shared_ptr<PathDiagnosticPiece>
1002 FindLastStoreBRVisitor::VisitNode(const ExplodedNode *Succ,
1003                                   const ExplodedNode *Pred,
1004                                   BugReporterContext &BRC, BugReport &BR) {
1005   if (Satisfied)
1006     return nullptr;
1007 
1008   const ExplodedNode *StoreSite = nullptr;
1009   const Expr *InitE = nullptr;
1010   bool IsParam = false;
1011 
1012   // First see if we reached the declaration of the region.
1013   if (const auto *VR = dyn_cast<VarRegion>(R)) {
1014     if (isInitializationOfVar(Pred, VR)) {
1015       StoreSite = Pred;
1016       InitE = VR->getDecl()->getInit();
1017     }
1018   }
1019 
1020   // If this is a post initializer expression, initializing the region, we
1021   // should track the initializer expression.
1022   if (Optional<PostInitializer> PIP = Pred->getLocationAs<PostInitializer>()) {
1023     const MemRegion *FieldReg = (const MemRegion *)PIP->getLocationValue();
1024     if (FieldReg && FieldReg == R) {
1025       StoreSite = Pred;
1026       InitE = PIP->getInitializer()->getInit();
1027     }
1028   }
1029 
1030   // Otherwise, see if this is the store site:
1031   // (1) Succ has this binding and Pred does not, i.e. this is
1032   //     where the binding first occurred.
1033   // (2) Succ has this binding and is a PostStore node for this region, i.e.
1034   //     the same binding was re-assigned here.
1035   if (!StoreSite) {
1036     if (Succ->getState()->getSVal(R) != V)
1037       return nullptr;
1038 
1039     if (Pred->getState()->getSVal(R) == V) {
1040       Optional<PostStore> PS = Succ->getLocationAs<PostStore>();
1041       if (!PS || PS->getLocationValue() != R)
1042         return nullptr;
1043     }
1044 
1045     StoreSite = Succ;
1046 
1047     // If this is an assignment expression, we can track the value
1048     // being assigned.
1049     if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
1050       if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>())
1051         if (BO->isAssignmentOp())
1052           InitE = BO->getRHS();
1053 
1054     // If this is a call entry, the variable should be a parameter.
1055     // FIXME: Handle CXXThisRegion as well. (This is not a priority because
1056     // 'this' should never be NULL, but this visitor isn't just for NULL and
1057     // UndefinedVal.)
1058     if (Optional<CallEnter> CE = Succ->getLocationAs<CallEnter>()) {
1059       if (const auto *VR = dyn_cast<VarRegion>(R)) {
1060         const auto *Param = cast<ParmVarDecl>(VR->getDecl());
1061 
1062         ProgramStateManager &StateMgr = BRC.getStateManager();
1063         CallEventManager &CallMgr = StateMgr.getCallEventManager();
1064 
1065         CallEventRef<> Call = CallMgr.getCaller(CE->getCalleeContext(),
1066                                                 Succ->getState());
1067         InitE = Call->getArgExpr(Param->getFunctionScopeIndex());
1068         IsParam = true;
1069       }
1070     }
1071 
1072     // If this is a CXXTempObjectRegion, the Expr responsible for its creation
1073     // is wrapped inside of it.
1074     if (const auto *TmpR = dyn_cast<CXXTempObjectRegion>(R))
1075       InitE = TmpR->getExpr();
1076   }
1077 
1078   if (!StoreSite)
1079     return nullptr;
1080   Satisfied = true;
1081 
1082   // If we have an expression that provided the value, try to track where it
1083   // came from.
1084   if (InitE) {
1085     if (V.isUndef() ||
1086         V.getAs<loc::ConcreteInt>() || V.getAs<nonloc::ConcreteInt>()) {
1087       if (!IsParam)
1088         InitE = InitE->IgnoreParenCasts();
1089       bugreporter::trackNullOrUndefValue(StoreSite, InitE, BR, IsParam,
1090                                          EnableNullFPSuppression);
1091     } else {
1092       ReturnVisitor::addVisitorIfNecessary(StoreSite, InitE->IgnoreParenCasts(),
1093                                            BR, EnableNullFPSuppression);
1094     }
1095   }
1096 
1097   // Okay, we've found the binding. Emit an appropriate message.
1098   SmallString<256> sbuf;
1099   llvm::raw_svector_ostream os(sbuf);
1100 
1101   if (Optional<PostStmt> PS = StoreSite->getLocationAs<PostStmt>()) {
1102     const Stmt *S = PS->getStmt();
1103     const char *action = nullptr;
1104     const auto *DS = dyn_cast<DeclStmt>(S);
1105     const auto *VR = dyn_cast<VarRegion>(R);
1106 
1107     if (DS) {
1108       action = R->canPrintPretty() ? "initialized to " :
1109                                      "Initializing to ";
1110     } else if (isa<BlockExpr>(S)) {
1111       action = R->canPrintPretty() ? "captured by block as " :
1112                                      "Captured by block as ";
1113       if (VR) {
1114         // See if we can get the BlockVarRegion.
1115         ProgramStateRef State = StoreSite->getState();
1116         SVal V = StoreSite->getSVal(S);
1117         if (const auto *BDR =
1118               dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) {
1119           if (const VarRegion *OriginalR = BDR->getOriginalRegion(VR)) {
1120             if (Optional<KnownSVal> KV =
1121                 State->getSVal(OriginalR).getAs<KnownSVal>())
1122               BR.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1123                   *KV, OriginalR, EnableNullFPSuppression));
1124           }
1125         }
1126       }
1127     }
1128     if (action)
1129       showBRDiagnostics(action, os, R, V, DS);
1130 
1131   } else if (StoreSite->getLocation().getAs<CallEnter>()) {
1132     if (const auto *VR = dyn_cast<VarRegion>(R))
1133       showBRParamDiagnostics(os, VR, V);
1134   }
1135 
1136   if (os.str().empty())
1137     showBRDefaultDiagnostics(os, R, V);
1138 
1139   // Construct a new PathDiagnosticPiece.
1140   ProgramPoint P = StoreSite->getLocation();
1141   PathDiagnosticLocation L;
1142   if (P.getAs<CallEnter>() && InitE)
1143     L = PathDiagnosticLocation(InitE, BRC.getSourceManager(),
1144                                P.getLocationContext());
1145 
1146   if (!L.isValid() || !L.asLocation().isValid())
1147     L = PathDiagnosticLocation::create(P, BRC.getSourceManager());
1148 
1149   if (!L.isValid() || !L.asLocation().isValid())
1150     return nullptr;
1151 
1152   return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
1153 }
1154 
1155 void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
1156   static int tag = 0;
1157   ID.AddPointer(&tag);
1158   ID.AddBoolean(Assumption);
1159   ID.Add(Constraint);
1160 }
1161 
1162 /// Return the tag associated with this visitor.  This tag will be used
1163 /// to make all PathDiagnosticPieces created by this visitor.
1164 const char *TrackConstraintBRVisitor::getTag() {
1165   return "TrackConstraintBRVisitor";
1166 }
1167 
1168 bool TrackConstraintBRVisitor::isUnderconstrained(const ExplodedNode *N) const {
1169   if (IsZeroCheck)
1170     return N->getState()->isNull(Constraint).isUnderconstrained();
1171   return (bool)N->getState()->assume(Constraint, !Assumption);
1172 }
1173 
1174 std::shared_ptr<PathDiagnosticPiece>
1175 TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
1176                                     const ExplodedNode *PrevN,
1177                                     BugReporterContext &BRC, BugReport &BR) {
1178   if (IsSatisfied)
1179     return nullptr;
1180 
1181   // Start tracking after we see the first state in which the value is
1182   // constrained.
1183   if (!IsTrackingTurnedOn)
1184     if (!isUnderconstrained(N))
1185       IsTrackingTurnedOn = true;
1186   if (!IsTrackingTurnedOn)
1187     return nullptr;
1188 
1189   // Check if in the previous state it was feasible for this constraint
1190   // to *not* be true.
1191   if (isUnderconstrained(PrevN)) {
1192     IsSatisfied = true;
1193 
1194     // As a sanity check, make sure that the negation of the constraint
1195     // was infeasible in the current state.  If it is feasible, we somehow
1196     // missed the transition point.
1197     assert(!isUnderconstrained(N));
1198 
1199     // We found the transition point for the constraint.  We now need to
1200     // pretty-print the constraint. (work-in-progress)
1201     SmallString<64> sbuf;
1202     llvm::raw_svector_ostream os(sbuf);
1203 
1204     if (Constraint.getAs<Loc>()) {
1205       os << "Assuming pointer value is ";
1206       os << (Assumption ? "non-null" : "null");
1207     }
1208 
1209     if (os.str().empty())
1210       return nullptr;
1211 
1212     // Construct a new PathDiagnosticPiece.
1213     ProgramPoint P = N->getLocation();
1214     PathDiagnosticLocation L =
1215       PathDiagnosticLocation::create(P, BRC.getSourceManager());
1216     if (!L.isValid())
1217       return nullptr;
1218 
1219     auto X = std::make_shared<PathDiagnosticEventPiece>(L, os.str());
1220     X->setTag(getTag());
1221     return std::move(X);
1222   }
1223 
1224   return nullptr;
1225 }
1226 
1227 SuppressInlineDefensiveChecksVisitor::
1228 SuppressInlineDefensiveChecksVisitor(DefinedSVal Value, const ExplodedNode *N)
1229     : V(Value) {
1230   // Check if the visitor is disabled.
1231   SubEngine *Eng = N->getState()->getStateManager().getOwningEngine();
1232   assert(Eng && "Cannot file a bug report without an owning engine");
1233   AnalyzerOptions &Options = Eng->getAnalysisManager().options;
1234   if (!Options.shouldSuppressInlinedDefensiveChecks())
1235     IsSatisfied = true;
1236 
1237   assert(N->getState()->isNull(V).isConstrainedTrue() &&
1238          "The visitor only tracks the cases where V is constrained to 0");
1239 }
1240 
1241 void SuppressInlineDefensiveChecksVisitor::Profile(
1242     llvm::FoldingSetNodeID &ID) const {
1243   static int id = 0;
1244   ID.AddPointer(&id);
1245   ID.Add(V);
1246 }
1247 
1248 const char *SuppressInlineDefensiveChecksVisitor::getTag() {
1249   return "IDCVisitor";
1250 }
1251 
1252 std::shared_ptr<PathDiagnosticPiece>
1253 SuppressInlineDefensiveChecksVisitor::VisitNode(const ExplodedNode *Succ,
1254                                                 const ExplodedNode *Pred,
1255                                                 BugReporterContext &BRC,
1256                                                 BugReport &BR) {
1257   if (IsSatisfied)
1258     return nullptr;
1259 
1260   // Start tracking after we see the first state in which the value is null.
1261   if (!IsTrackingTurnedOn)
1262     if (Succ->getState()->isNull(V).isConstrainedTrue())
1263       IsTrackingTurnedOn = true;
1264   if (!IsTrackingTurnedOn)
1265     return nullptr;
1266 
1267   // Check if in the previous state it was feasible for this value
1268   // to *not* be null.
1269   if (!Pred->getState()->isNull(V).isConstrainedTrue()) {
1270     IsSatisfied = true;
1271 
1272     assert(Succ->getState()->isNull(V).isConstrainedTrue());
1273 
1274     // Check if this is inlined defensive checks.
1275     const LocationContext *CurLC =Succ->getLocationContext();
1276     const LocationContext *ReportLC = BR.getErrorNode()->getLocationContext();
1277     if (CurLC != ReportLC && !CurLC->isParentOf(ReportLC)) {
1278       BR.markInvalid("Suppress IDC", CurLC);
1279       return nullptr;
1280     }
1281 
1282     // Treat defensive checks in function-like macros as if they were an inlined
1283     // defensive check. If the bug location is not in a macro and the
1284     // terminator for the current location is in a macro then suppress the
1285     // warning.
1286     auto BugPoint = BR.getErrorNode()->getLocation().getAs<StmtPoint>();
1287 
1288     if (!BugPoint)
1289       return nullptr;
1290 
1291     ProgramPoint CurPoint = Succ->getLocation();
1292     const Stmt *CurTerminatorStmt = nullptr;
1293     if (auto BE = CurPoint.getAs<BlockEdge>()) {
1294       CurTerminatorStmt = BE->getSrc()->getTerminator().getStmt();
1295     } else if (auto SP = CurPoint.getAs<StmtPoint>()) {
1296       const Stmt *CurStmt = SP->getStmt();
1297       if (!CurStmt->getLocStart().isMacroID())
1298         return nullptr;
1299 
1300       CFGStmtMap *Map = CurLC->getAnalysisDeclContext()->getCFGStmtMap();
1301       CurTerminatorStmt = Map->getBlock(CurStmt)->getTerminator();
1302     } else {
1303       return nullptr;
1304     }
1305 
1306     if (!CurTerminatorStmt)
1307       return nullptr;
1308 
1309     SourceLocation TerminatorLoc = CurTerminatorStmt->getLocStart();
1310     if (TerminatorLoc.isMacroID()) {
1311       SourceLocation BugLoc = BugPoint->getStmt()->getLocStart();
1312 
1313       // Suppress reports unless we are in that same macro.
1314       if (!BugLoc.isMacroID() ||
1315           getMacroName(BugLoc, BRC) != getMacroName(TerminatorLoc, BRC)) {
1316         BR.markInvalid("Suppress Macro IDC", CurLC);
1317       }
1318       return nullptr;
1319     }
1320   }
1321   return nullptr;
1322 }
1323 
1324 static const MemRegion *getLocationRegionIfReference(const Expr *E,
1325                                                      const ExplodedNode *N) {
1326   if (const auto *DR = dyn_cast<DeclRefExpr>(E)) {
1327     if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1328       if (!VD->getType()->isReferenceType())
1329         return nullptr;
1330       ProgramStateManager &StateMgr = N->getState()->getStateManager();
1331       MemRegionManager &MRMgr = StateMgr.getRegionManager();
1332       return MRMgr.getVarRegion(VD, N->getLocationContext());
1333     }
1334   }
1335 
1336   // FIXME: This does not handle other kinds of null references,
1337   // for example, references from FieldRegions:
1338   //   struct Wrapper { int &ref; };
1339   //   Wrapper w = { *(int *)0 };
1340   //   w.ref = 1;
1341 
1342   return nullptr;
1343 }
1344 
1345 static const Expr *peelOffOuterExpr(const Expr *Ex,
1346                                     const ExplodedNode *N) {
1347   Ex = Ex->IgnoreParenCasts();
1348   if (const auto *EWC = dyn_cast<ExprWithCleanups>(Ex))
1349     return peelOffOuterExpr(EWC->getSubExpr(), N);
1350   if (const auto *OVE = dyn_cast<OpaqueValueExpr>(Ex))
1351     return peelOffOuterExpr(OVE->getSourceExpr(), N);
1352   if (const auto *POE = dyn_cast<PseudoObjectExpr>(Ex)) {
1353     const auto *PropRef = dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
1354     if (PropRef && PropRef->isMessagingGetter()) {
1355       const Expr *GetterMessageSend =
1356           POE->getSemanticExpr(POE->getNumSemanticExprs() - 1);
1357       assert(isa<ObjCMessageExpr>(GetterMessageSend->IgnoreParenCasts()));
1358       return peelOffOuterExpr(GetterMessageSend, N);
1359     }
1360   }
1361 
1362   // Peel off the ternary operator.
1363   if (const auto *CO = dyn_cast<ConditionalOperator>(Ex)) {
1364     // Find a node where the branching occurred and find out which branch
1365     // we took (true/false) by looking at the ExplodedGraph.
1366     const ExplodedNode *NI = N;
1367     do {
1368       ProgramPoint ProgPoint = NI->getLocation();
1369       if (Optional<BlockEdge> BE = ProgPoint.getAs<BlockEdge>()) {
1370         const CFGBlock *srcBlk = BE->getSrc();
1371         if (const Stmt *term = srcBlk->getTerminator()) {
1372           if (term == CO) {
1373             bool TookTrueBranch = (*(srcBlk->succ_begin()) == BE->getDst());
1374             if (TookTrueBranch)
1375               return peelOffOuterExpr(CO->getTrueExpr(), N);
1376             else
1377               return peelOffOuterExpr(CO->getFalseExpr(), N);
1378           }
1379         }
1380       }
1381       NI = NI->getFirstPred();
1382     } while (NI);
1383   }
1384   return Ex;
1385 }
1386 
1387 /// Walk through nodes until we get one that matches the statement exactly.
1388 /// Alternately, if we hit a known lvalue for the statement, we know we've
1389 /// gone too far (though we can likely track the lvalue better anyway).
1390 static const ExplodedNode* findNodeForStatement(const ExplodedNode *N,
1391                                                 const Stmt *S,
1392                                                 const Expr *Inner) {
1393   do {
1394     const ProgramPoint &pp = N->getLocation();
1395     if (auto ps = pp.getAs<StmtPoint>()) {
1396       if (ps->getStmt() == S || ps->getStmt() == Inner)
1397         break;
1398     } else if (auto CEE = pp.getAs<CallExitEnd>()) {
1399       if (CEE->getCalleeContext()->getCallSite() == S ||
1400           CEE->getCalleeContext()->getCallSite() == Inner)
1401         break;
1402     }
1403     N = N->getFirstPred();
1404   } while (N);
1405   return N;
1406 }
1407 
1408 /// Find the ExplodedNode where the lvalue (the value of 'Ex')
1409 /// was computed.
1410 static const ExplodedNode* findNodeForExpression(const ExplodedNode *N,
1411     const Expr *Inner) {
1412   while (N) {
1413     if (auto P = N->getLocation().getAs<PostStmt>()) {
1414       if (P->getStmt() == Inner)
1415         break;
1416     }
1417     N = N->getFirstPred();
1418   }
1419   assert(N && "Unable to find the lvalue node.");
1420   return N;
1421 }
1422 
1423 /// Performing operator `&' on an lvalue expression is essentially a no-op.
1424 /// Then, if we are taking addresses of fields or elements, these are also
1425 /// unlikely to matter.
1426 static const Expr* peelOfOuterAddrOf(const Expr* Ex) {
1427   Ex = Ex->IgnoreParenCasts();
1428 
1429   // FIXME: There's a hack in our Store implementation that always computes
1430   // field offsets around null pointers as if they are always equal to 0.
1431   // The idea here is to report accesses to fields as null dereferences
1432   // even though the pointer value that's being dereferenced is actually
1433   // the offset of the field rather than exactly 0.
1434   // See the FIXME in StoreManager's getLValueFieldOrIvar() method.
1435   // This code interacts heavily with this hack; otherwise the value
1436   // would not be null at all for most fields, so we'd be unable to track it.
1437   if (const auto *Op = dyn_cast<UnaryOperator>(Ex))
1438     if (Op->getOpcode() == UO_AddrOf && Op->getSubExpr()->isLValue())
1439       if (const Expr *DerefEx = bugreporter::getDerefExpr(Op->getSubExpr()))
1440         return DerefEx;
1441   return Ex;
1442 }
1443 
1444 bool bugreporter::trackNullOrUndefValue(const ExplodedNode *N,
1445                                         const Stmt *S,
1446                                         BugReport &report, bool IsArg,
1447                                         bool EnableNullFPSuppression) {
1448   if (!S || !N)
1449     return false;
1450 
1451   if (const auto *Ex = dyn_cast<Expr>(S))
1452     S = peelOffOuterExpr(Ex, N);
1453 
1454   const Expr *Inner = nullptr;
1455   if (const auto *Ex = dyn_cast<Expr>(S)) {
1456     Ex = peelOfOuterAddrOf(Ex);
1457     Ex = Ex->IgnoreParenCasts();
1458 
1459     if (Ex && (ExplodedGraph::isInterestingLValueExpr(Ex)
1460           || CallEvent::isCallStmt(Ex)))
1461       Inner = Ex;
1462   }
1463 
1464   if (IsArg && !Inner) {
1465     assert(N->getLocation().getAs<CallEnter>() && "Tracking arg but not at call");
1466   } else {
1467     N = findNodeForStatement(N, S, Inner);
1468     if (!N)
1469       return false;
1470   }
1471 
1472   ProgramStateRef state = N->getState();
1473 
1474   // The message send could be nil due to the receiver being nil.
1475   // At this point in the path, the receiver should be live since we are at the
1476   // message send expr. If it is nil, start tracking it.
1477   if (const Expr *Receiver = NilReceiverBRVisitor::getNilReceiver(S, N))
1478     trackNullOrUndefValue(N, Receiver, report, /* IsArg=*/ false,
1479         EnableNullFPSuppression);
1480 
1481   // See if the expression we're interested refers to a variable.
1482   // If so, we can track both its contents and constraints on its value.
1483   if (Inner && ExplodedGraph::isInterestingLValueExpr(Inner)) {
1484     const ExplodedNode *LVNode = findNodeForExpression(N, Inner);
1485     ProgramStateRef LVState = LVNode->getState();
1486     SVal LVal = LVNode->getSVal(Inner);
1487 
1488     const MemRegion *RR = getLocationRegionIfReference(Inner, N);
1489     bool LVIsNull = LVState->isNull(LVal).isConstrainedTrue();
1490 
1491     // If this is a C++ reference to a null pointer, we are tracking the
1492     // pointer. In addition, we should find the store at which the reference
1493     // got initialized.
1494     if (RR && !LVIsNull) {
1495       if (auto KV = LVal.getAs<KnownSVal>())
1496         report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1497               *KV, RR, EnableNullFPSuppression));
1498     }
1499 
1500     // In case of C++ references, we want to differentiate between a null
1501     // reference and reference to null pointer.
1502     // If the LVal is null, check if we are dealing with null reference.
1503     // For those, we want to track the location of the reference.
1504     const MemRegion *R = (RR && LVIsNull) ? RR :
1505         LVNode->getSVal(Inner).getAsRegion();
1506 
1507     if (R) {
1508       // Mark both the variable region and its contents as interesting.
1509       SVal V = LVState->getRawSVal(loc::MemRegionVal(R));
1510       report.addVisitor(
1511           llvm::make_unique<NoStoreFuncVisitor>(cast<SubRegion>(R)));
1512 
1513       MacroNullReturnSuppressionVisitor::addMacroVisitorIfNecessary(
1514           N, R, EnableNullFPSuppression, report, V);
1515 
1516       report.markInteresting(R);
1517       report.markInteresting(V);
1518       report.addVisitor(llvm::make_unique<UndefOrNullArgVisitor>(R));
1519 
1520       // If the contents are symbolic, find out when they became null.
1521       if (V.getAsLocSymbol(/*IncludeBaseRegions*/ true))
1522         report.addVisitor(llvm::make_unique<TrackConstraintBRVisitor>(
1523               V.castAs<DefinedSVal>(), false));
1524 
1525       // Add visitor, which will suppress inline defensive checks.
1526       if (auto DV = V.getAs<DefinedSVal>()) {
1527         if (!DV->isZeroConstant() && LVState->isNull(*DV).isConstrainedTrue() &&
1528             EnableNullFPSuppression) {
1529           report.addVisitor(
1530               llvm::make_unique<SuppressInlineDefensiveChecksVisitor>(*DV,
1531                 LVNode));
1532         }
1533       }
1534 
1535       if (auto KV = V.getAs<KnownSVal>())
1536         report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1537               *KV, R, EnableNullFPSuppression));
1538       return true;
1539     }
1540   }
1541 
1542   // If the expression is not an "lvalue expression", we can still
1543   // track the constraints on its contents.
1544   SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext());
1545 
1546   // If the value came from an inlined function call, we should at least make
1547   // sure that function isn't pruned in our output.
1548   if (const auto *E = dyn_cast<Expr>(S))
1549     S = E->IgnoreParenCasts();
1550 
1551   ReturnVisitor::addVisitorIfNecessary(N, S, report, EnableNullFPSuppression);
1552 
1553   // Uncomment this to find cases where we aren't properly getting the
1554   // base value that was dereferenced.
1555   // assert(!V.isUnknownOrUndef());
1556   // Is it a symbolic value?
1557   if (auto L = V.getAs<loc::MemRegionVal>()) {
1558     report.addVisitor(llvm::make_unique<UndefOrNullArgVisitor>(L->getRegion()));
1559 
1560     // At this point we are dealing with the region's LValue.
1561     // However, if the rvalue is a symbolic region, we should track it as well.
1562     // Try to use the correct type when looking up the value.
1563     SVal RVal;
1564     if (const auto *E = dyn_cast<Expr>(S))
1565       RVal = state->getRawSVal(L.getValue(), E->getType());
1566     else
1567       RVal = state->getSVal(L->getRegion());
1568 
1569     if (auto KV = RVal.getAs<KnownSVal>())
1570       report.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1571             *KV, L->getRegion(), EnableNullFPSuppression));
1572 
1573     const MemRegion *RegionRVal = RVal.getAsRegion();
1574     if (RegionRVal && isa<SymbolicRegion>(RegionRVal)) {
1575       report.markInteresting(RegionRVal);
1576       report.addVisitor(llvm::make_unique<TrackConstraintBRVisitor>(
1577             loc::MemRegionVal(RegionRVal), false));
1578     }
1579   }
1580   return true;
1581 }
1582 
1583 const Expr *NilReceiverBRVisitor::getNilReceiver(const Stmt *S,
1584                                                  const ExplodedNode *N) {
1585   const auto *ME = dyn_cast<ObjCMessageExpr>(S);
1586   if (!ME)
1587     return nullptr;
1588   if (const Expr *Receiver = ME->getInstanceReceiver()) {
1589     ProgramStateRef state = N->getState();
1590     SVal V = N->getSVal(Receiver);
1591     if (state->isNull(V).isConstrainedTrue())
1592       return Receiver;
1593   }
1594   return nullptr;
1595 }
1596 
1597 std::shared_ptr<PathDiagnosticPiece>
1598 NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
1599                                 const ExplodedNode *PrevN,
1600                                 BugReporterContext &BRC, BugReport &BR) {
1601   Optional<PreStmt> P = N->getLocationAs<PreStmt>();
1602   if (!P)
1603     return nullptr;
1604 
1605   const Stmt *S = P->getStmt();
1606   const Expr *Receiver = getNilReceiver(S, N);
1607   if (!Receiver)
1608     return nullptr;
1609 
1610   llvm::SmallString<256> Buf;
1611   llvm::raw_svector_ostream OS(Buf);
1612 
1613   if (const auto *ME = dyn_cast<ObjCMessageExpr>(S)) {
1614     OS << "'";
1615     ME->getSelector().print(OS);
1616     OS << "' not called";
1617   }
1618   else {
1619     OS << "No method is called";
1620   }
1621   OS << " because the receiver is nil";
1622 
1623   // The receiver was nil, and hence the method was skipped.
1624   // Register a BugReporterVisitor to issue a message telling us how
1625   // the receiver was null.
1626   bugreporter::trackNullOrUndefValue(N, Receiver, BR, /*IsArg*/ false,
1627                                      /*EnableNullFPSuppression*/ false);
1628   // Issue a message saying that the method was skipped.
1629   PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
1630                                      N->getLocationContext());
1631   return std::make_shared<PathDiagnosticEventPiece>(L, OS.str());
1632 }
1633 
1634 // Registers every VarDecl inside a Stmt with a last store visitor.
1635 void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
1636                                                 const Stmt *S,
1637                                                 bool EnableNullFPSuppression) {
1638   const ExplodedNode *N = BR.getErrorNode();
1639   std::deque<const Stmt *> WorkList;
1640   WorkList.push_back(S);
1641 
1642   while (!WorkList.empty()) {
1643     const Stmt *Head = WorkList.front();
1644     WorkList.pop_front();
1645 
1646     ProgramStateManager &StateMgr = N->getState()->getStateManager();
1647 
1648     if (const auto *DR = dyn_cast<DeclRefExpr>(Head)) {
1649       if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1650         const VarRegion *R =
1651         StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
1652 
1653         // What did we load?
1654         SVal V = N->getSVal(S);
1655 
1656         if (V.getAs<loc::ConcreteInt>() || V.getAs<nonloc::ConcreteInt>()) {
1657           // Register a new visitor with the BugReport.
1658           BR.addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
1659               V.castAs<KnownSVal>(), R, EnableNullFPSuppression));
1660         }
1661       }
1662     }
1663 
1664     for (const Stmt *SubStmt : Head->children())
1665       WorkList.push_back(SubStmt);
1666   }
1667 }
1668 
1669 //===----------------------------------------------------------------------===//
1670 // Visitor that tries to report interesting diagnostics from conditions.
1671 //===----------------------------------------------------------------------===//
1672 
1673 /// Return the tag associated with this visitor.  This tag will be used
1674 /// to make all PathDiagnosticPieces created by this visitor.
1675 const char *ConditionBRVisitor::getTag() {
1676   return "ConditionBRVisitor";
1677 }
1678 
1679 std::shared_ptr<PathDiagnosticPiece>
1680 ConditionBRVisitor::VisitNode(const ExplodedNode *N, const ExplodedNode *Prev,
1681                               BugReporterContext &BRC, BugReport &BR) {
1682   auto piece = VisitNodeImpl(N, Prev, BRC, BR);
1683   if (piece) {
1684     piece->setTag(getTag());
1685     if (auto *ev = dyn_cast<PathDiagnosticEventPiece>(piece.get()))
1686       ev->setPrunable(true, /* override */ false);
1687   }
1688   return piece;
1689 }
1690 
1691 std::shared_ptr<PathDiagnosticPiece>
1692 ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N,
1693                                   const ExplodedNode *Prev,
1694                                   BugReporterContext &BRC, BugReport &BR) {
1695   ProgramPoint progPoint = N->getLocation();
1696   ProgramStateRef CurrentState = N->getState();
1697   ProgramStateRef PrevState = Prev->getState();
1698 
1699   // Compare the GDMs of the state, because that is where constraints
1700   // are managed.  Note that ensure that we only look at nodes that
1701   // were generated by the analyzer engine proper, not checkers.
1702   if (CurrentState->getGDM().getRoot() ==
1703       PrevState->getGDM().getRoot())
1704     return nullptr;
1705 
1706   // If an assumption was made on a branch, it should be caught
1707   // here by looking at the state transition.
1708   if (Optional<BlockEdge> BE = progPoint.getAs<BlockEdge>()) {
1709     const CFGBlock *srcBlk = BE->getSrc();
1710     if (const Stmt *term = srcBlk->getTerminator())
1711       return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC);
1712     return nullptr;
1713   }
1714 
1715   if (Optional<PostStmt> PS = progPoint.getAs<PostStmt>()) {
1716     // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
1717     // violation.
1718     const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
1719       cast<GRBugReporter>(BRC.getBugReporter()).
1720         getEngine().geteagerlyAssumeBinOpBifurcationTags();
1721 
1722     const ProgramPointTag *tag = PS->getTag();
1723     if (tag == tags.first)
1724       return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
1725                            BRC, BR, N);
1726     if (tag == tags.second)
1727       return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
1728                            BRC, BR, N);
1729 
1730     return nullptr;
1731   }
1732 
1733   return nullptr;
1734 }
1735 
1736 std::shared_ptr<PathDiagnosticPiece> ConditionBRVisitor::VisitTerminator(
1737     const Stmt *Term, const ExplodedNode *N, const CFGBlock *srcBlk,
1738     const CFGBlock *dstBlk, BugReport &R, BugReporterContext &BRC) {
1739   const Expr *Cond = nullptr;
1740 
1741   // In the code below, Term is a CFG terminator and Cond is a branch condition
1742   // expression upon which the decision is made on this terminator.
1743   //
1744   // For example, in "if (x == 0)", the "if (x == 0)" statement is a terminator,
1745   // and "x == 0" is the respective condition.
1746   //
1747   // Another example: in "if (x && y)", we've got two terminators and two
1748   // conditions due to short-circuit nature of operator "&&":
1749   // 1. The "if (x && y)" statement is a terminator,
1750   //    and "y" is the respective condition.
1751   // 2. Also "x && ..." is another terminator,
1752   //    and "x" is its condition.
1753 
1754   switch (Term->getStmtClass()) {
1755   // FIXME: Stmt::SwitchStmtClass is worth handling, however it is a bit
1756   // more tricky because there are more than two branches to account for.
1757   default:
1758     return nullptr;
1759   case Stmt::IfStmtClass:
1760     Cond = cast<IfStmt>(Term)->getCond();
1761     break;
1762   case Stmt::ConditionalOperatorClass:
1763     Cond = cast<ConditionalOperator>(Term)->getCond();
1764     break;
1765   case Stmt::BinaryOperatorClass:
1766     // When we encounter a logical operator (&& or ||) as a CFG terminator,
1767     // then the condition is actually its LHS; otherwise, we'd encounter
1768     // the parent, such as if-statement, as a terminator.
1769     const auto *BO = cast<BinaryOperator>(Term);
1770     assert(BO->isLogicalOp() &&
1771            "CFG terminator is not a short-circuit operator!");
1772     Cond = BO->getLHS();
1773     break;
1774   }
1775 
1776   // However, when we encounter a logical operator as a branch condition,
1777   // then the condition is actually its RHS, because LHS would be
1778   // the condition for the logical operator terminator.
1779   while (const auto *InnerBO = dyn_cast<BinaryOperator>(Cond)) {
1780     if (!InnerBO->isLogicalOp())
1781       break;
1782     Cond = InnerBO->getRHS()->IgnoreParens();
1783   }
1784 
1785   assert(Cond);
1786   assert(srcBlk->succ_size() == 2);
1787   const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
1788   return VisitTrueTest(Cond, tookTrue, BRC, R, N);
1789 }
1790 
1791 std::shared_ptr<PathDiagnosticPiece>
1792 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, bool tookTrue,
1793                                   BugReporterContext &BRC, BugReport &R,
1794                                   const ExplodedNode *N) {
1795   // These will be modified in code below, but we need to preserve the original
1796   //  values in case we want to throw the generic message.
1797   const Expr *CondTmp = Cond;
1798   bool tookTrueTmp = tookTrue;
1799 
1800   while (true) {
1801     CondTmp = CondTmp->IgnoreParenCasts();
1802     switch (CondTmp->getStmtClass()) {
1803       default:
1804         break;
1805       case Stmt::BinaryOperatorClass:
1806         if (auto P = VisitTrueTest(Cond, cast<BinaryOperator>(CondTmp),
1807                                    tookTrueTmp, BRC, R, N))
1808           return P;
1809         break;
1810       case Stmt::DeclRefExprClass:
1811         if (auto P = VisitTrueTest(Cond, cast<DeclRefExpr>(CondTmp),
1812                                    tookTrueTmp, BRC, R, N))
1813           return P;
1814         break;
1815       case Stmt::UnaryOperatorClass: {
1816         const auto *UO = cast<UnaryOperator>(CondTmp);
1817         if (UO->getOpcode() == UO_LNot) {
1818           tookTrueTmp = !tookTrueTmp;
1819           CondTmp = UO->getSubExpr();
1820           continue;
1821         }
1822         break;
1823       }
1824     }
1825     break;
1826   }
1827 
1828   // Condition too complex to explain? Just say something so that the user
1829   // knew we've made some path decision at this point.
1830   const LocationContext *LCtx = N->getLocationContext();
1831   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
1832   if (!Loc.isValid() || !Loc.asLocation().isValid())
1833     return nullptr;
1834 
1835   return std::make_shared<PathDiagnosticEventPiece>(
1836       Loc, tookTrue ? GenericTrueMessage : GenericFalseMessage);
1837 }
1838 
1839 bool ConditionBRVisitor::patternMatch(const Expr *Ex,
1840                                       const Expr *ParentEx,
1841                                       raw_ostream &Out,
1842                                       BugReporterContext &BRC,
1843                                       BugReport &report,
1844                                       const ExplodedNode *N,
1845                                       Optional<bool> &prunable) {
1846   const Expr *OriginalExpr = Ex;
1847   Ex = Ex->IgnoreParenCasts();
1848 
1849   // Use heuristics to determine if Ex is a macro expending to a literal and
1850   // if so, use the macro's name.
1851   SourceLocation LocStart = Ex->getLocStart();
1852   SourceLocation LocEnd = Ex->getLocEnd();
1853   if (LocStart.isMacroID() && LocEnd.isMacroID() &&
1854       (isa<GNUNullExpr>(Ex) ||
1855        isa<ObjCBoolLiteralExpr>(Ex) ||
1856        isa<CXXBoolLiteralExpr>(Ex) ||
1857        isa<IntegerLiteral>(Ex) ||
1858        isa<FloatingLiteral>(Ex))) {
1859     StringRef StartName = Lexer::getImmediateMacroNameForDiagnostics(LocStart,
1860       BRC.getSourceManager(), BRC.getASTContext().getLangOpts());
1861     StringRef EndName = Lexer::getImmediateMacroNameForDiagnostics(LocEnd,
1862       BRC.getSourceManager(), BRC.getASTContext().getLangOpts());
1863     bool beginAndEndAreTheSameMacro = StartName.equals(EndName);
1864 
1865     bool partOfParentMacro = false;
1866     if (ParentEx->getLocStart().isMacroID()) {
1867       StringRef PName = Lexer::getImmediateMacroNameForDiagnostics(
1868         ParentEx->getLocStart(), BRC.getSourceManager(),
1869         BRC.getASTContext().getLangOpts());
1870       partOfParentMacro = PName.equals(StartName);
1871     }
1872 
1873     if (beginAndEndAreTheSameMacro && !partOfParentMacro ) {
1874       // Get the location of the macro name as written by the caller.
1875       SourceLocation Loc = LocStart;
1876       while (LocStart.isMacroID()) {
1877         Loc = LocStart;
1878         LocStart = BRC.getSourceManager().getImmediateMacroCallerLoc(LocStart);
1879       }
1880       StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics(
1881         Loc, BRC.getSourceManager(), BRC.getASTContext().getLangOpts());
1882 
1883       // Return the macro name.
1884       Out << MacroName;
1885       return false;
1886     }
1887   }
1888 
1889   if (const auto *DR = dyn_cast<DeclRefExpr>(Ex)) {
1890     const bool quotes = isa<VarDecl>(DR->getDecl());
1891     if (quotes) {
1892       Out << '\'';
1893       const LocationContext *LCtx = N->getLocationContext();
1894       const ProgramState *state = N->getState().get();
1895       if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
1896                                                 LCtx).getAsRegion()) {
1897         if (report.isInteresting(R))
1898           prunable = false;
1899         else {
1900           const ProgramState *state = N->getState().get();
1901           SVal V = state->getSVal(R);
1902           if (report.isInteresting(V))
1903             prunable = false;
1904         }
1905       }
1906     }
1907     Out << DR->getDecl()->getDeclName().getAsString();
1908     if (quotes)
1909       Out << '\'';
1910     return quotes;
1911   }
1912 
1913   if (const auto *IL = dyn_cast<IntegerLiteral>(Ex)) {
1914     QualType OriginalTy = OriginalExpr->getType();
1915     if (OriginalTy->isPointerType()) {
1916       if (IL->getValue() == 0) {
1917         Out << "null";
1918         return false;
1919       }
1920     }
1921     else if (OriginalTy->isObjCObjectPointerType()) {
1922       if (IL->getValue() == 0) {
1923         Out << "nil";
1924         return false;
1925       }
1926     }
1927 
1928     Out << IL->getValue();
1929     return false;
1930   }
1931 
1932   return false;
1933 }
1934 
1935 std::shared_ptr<PathDiagnosticPiece>
1936 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, const BinaryOperator *BExpr,
1937                                   const bool tookTrue, BugReporterContext &BRC,
1938                                   BugReport &R, const ExplodedNode *N) {
1939   bool shouldInvert = false;
1940   Optional<bool> shouldPrune;
1941 
1942   SmallString<128> LhsString, RhsString;
1943   {
1944     llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
1945     const bool isVarLHS = patternMatch(BExpr->getLHS(), BExpr, OutLHS,
1946                                        BRC, R, N, shouldPrune);
1947     const bool isVarRHS = patternMatch(BExpr->getRHS(), BExpr, OutRHS,
1948                                        BRC, R, N, shouldPrune);
1949 
1950     shouldInvert = !isVarLHS && isVarRHS;
1951   }
1952 
1953   BinaryOperator::Opcode Op = BExpr->getOpcode();
1954 
1955   if (BinaryOperator::isAssignmentOp(Op)) {
1956     // For assignment operators, all that we care about is that the LHS
1957     // evaluates to "true" or "false".
1958     return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
1959                                   BRC, R, N);
1960   }
1961 
1962   // For non-assignment operations, we require that we can understand
1963   // both the LHS and RHS.
1964   if (LhsString.empty() || RhsString.empty() ||
1965       !BinaryOperator::isComparisonOp(Op) || Op == BO_Cmp)
1966     return nullptr;
1967 
1968   // Should we invert the strings if the LHS is not a variable name?
1969   SmallString<256> buf;
1970   llvm::raw_svector_ostream Out(buf);
1971   Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
1972 
1973   // Do we need to invert the opcode?
1974   if (shouldInvert)
1975     switch (Op) {
1976       default: break;
1977       case BO_LT: Op = BO_GT; break;
1978       case BO_GT: Op = BO_LT; break;
1979       case BO_LE: Op = BO_GE; break;
1980       case BO_GE: Op = BO_LE; break;
1981     }
1982 
1983   if (!tookTrue)
1984     switch (Op) {
1985       case BO_EQ: Op = BO_NE; break;
1986       case BO_NE: Op = BO_EQ; break;
1987       case BO_LT: Op = BO_GE; break;
1988       case BO_GT: Op = BO_LE; break;
1989       case BO_LE: Op = BO_GT; break;
1990       case BO_GE: Op = BO_LT; break;
1991       default:
1992         return nullptr;
1993     }
1994 
1995   switch (Op) {
1996     case BO_EQ:
1997       Out << "equal to ";
1998       break;
1999     case BO_NE:
2000       Out << "not equal to ";
2001       break;
2002     default:
2003       Out << BinaryOperator::getOpcodeStr(Op) << ' ';
2004       break;
2005   }
2006 
2007   Out << (shouldInvert ? LhsString : RhsString);
2008   const LocationContext *LCtx = N->getLocationContext();
2009   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
2010   auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2011   if (shouldPrune.hasValue())
2012     event->setPrunable(shouldPrune.getValue());
2013   return event;
2014 }
2015 
2016 std::shared_ptr<PathDiagnosticPiece> ConditionBRVisitor::VisitConditionVariable(
2017     StringRef LhsString, const Expr *CondVarExpr, const bool tookTrue,
2018     BugReporterContext &BRC, BugReport &report, const ExplodedNode *N) {
2019   // FIXME: If there's already a constraint tracker for this variable,
2020   // we shouldn't emit anything here (c.f. the double note in
2021   // test/Analysis/inlining/path-notes.c)
2022   SmallString<256> buf;
2023   llvm::raw_svector_ostream Out(buf);
2024   Out << "Assuming " << LhsString << " is ";
2025 
2026   QualType Ty = CondVarExpr->getType();
2027 
2028   if (Ty->isPointerType())
2029     Out << (tookTrue ? "not null" : "null");
2030   else if (Ty->isObjCObjectPointerType())
2031     Out << (tookTrue ? "not nil" : "nil");
2032   else if (Ty->isBooleanType())
2033     Out << (tookTrue ? "true" : "false");
2034   else if (Ty->isIntegralOrEnumerationType())
2035     Out << (tookTrue ? "non-zero" : "zero");
2036   else
2037     return nullptr;
2038 
2039   const LocationContext *LCtx = N->getLocationContext();
2040   PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
2041   auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2042 
2043   if (const auto *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) {
2044     if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
2045       const ProgramState *state = N->getState().get();
2046       if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
2047         if (report.isInteresting(R))
2048           event->setPrunable(false);
2049       }
2050     }
2051   }
2052 
2053   return event;
2054 }
2055 
2056 std::shared_ptr<PathDiagnosticPiece>
2057 ConditionBRVisitor::VisitTrueTest(const Expr *Cond, const DeclRefExpr *DR,
2058                                   const bool tookTrue, BugReporterContext &BRC,
2059                                   BugReport &report, const ExplodedNode *N) {
2060   const auto *VD = dyn_cast<VarDecl>(DR->getDecl());
2061   if (!VD)
2062     return nullptr;
2063 
2064   SmallString<256> Buf;
2065   llvm::raw_svector_ostream Out(Buf);
2066 
2067   Out << "Assuming '" << VD->getDeclName() << "' is ";
2068 
2069   QualType VDTy = VD->getType();
2070 
2071   if (VDTy->isPointerType())
2072     Out << (tookTrue ? "non-null" : "null");
2073   else if (VDTy->isObjCObjectPointerType())
2074     Out << (tookTrue ? "non-nil" : "nil");
2075   else if (VDTy->isScalarType())
2076     Out << (tookTrue ? "not equal to 0" : "0");
2077   else
2078     return nullptr;
2079 
2080   const LocationContext *LCtx = N->getLocationContext();
2081   PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
2082   auto event = std::make_shared<PathDiagnosticEventPiece>(Loc, Out.str());
2083 
2084   const ProgramState *state = N->getState().get();
2085   if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
2086     if (report.isInteresting(R))
2087       event->setPrunable(false);
2088     else {
2089       SVal V = state->getSVal(R);
2090       if (report.isInteresting(V))
2091         event->setPrunable(false);
2092     }
2093   }
2094   return std::move(event);
2095 }
2096 
2097 const char *const ConditionBRVisitor::GenericTrueMessage =
2098     "Assuming the condition is true";
2099 const char *const ConditionBRVisitor::GenericFalseMessage =
2100     "Assuming the condition is false";
2101 
2102 bool ConditionBRVisitor::isPieceMessageGeneric(
2103     const PathDiagnosticPiece *Piece) {
2104   return Piece->getString() == GenericTrueMessage ||
2105          Piece->getString() == GenericFalseMessage;
2106 }
2107 
2108 std::unique_ptr<PathDiagnosticPiece>
2109 LikelyFalsePositiveSuppressionBRVisitor::getEndPath(BugReporterContext &BRC,
2110                                                     const ExplodedNode *N,
2111                                                     BugReport &BR) {
2112   // Here we suppress false positives coming from system headers. This list is
2113   // based on known issues.
2114   ExprEngine &Eng = BRC.getBugReporter().getEngine();
2115   AnalyzerOptions &Options = Eng.getAnalysisManager().options;
2116   const Decl *D = N->getLocationContext()->getDecl();
2117 
2118   if (AnalysisDeclContext::isInStdNamespace(D)) {
2119     // Skip reports within the 'std' namespace. Although these can sometimes be
2120     // the user's fault, we currently don't report them very well, and
2121     // Note that this will not help for any other data structure libraries, like
2122     // TR1, Boost, or llvm/ADT.
2123     if (Options.shouldSuppressFromCXXStandardLibrary()) {
2124       BR.markInvalid(getTag(), nullptr);
2125       return nullptr;
2126     } else {
2127       // If the complete 'std' suppression is not enabled, suppress reports
2128       // from the 'std' namespace that are known to produce false positives.
2129 
2130       // The analyzer issues a false use-after-free when std::list::pop_front
2131       // or std::list::pop_back are called multiple times because we cannot
2132       // reason about the internal invariants of the data structure.
2133       if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
2134         const CXXRecordDecl *CD = MD->getParent();
2135         if (CD->getName() == "list") {
2136           BR.markInvalid(getTag(), nullptr);
2137           return nullptr;
2138         }
2139       }
2140 
2141       // The analyzer issues a false positive when the constructor of
2142       // std::__independent_bits_engine from algorithms is used.
2143       if (const auto *MD = dyn_cast<CXXConstructorDecl>(D)) {
2144         const CXXRecordDecl *CD = MD->getParent();
2145         if (CD->getName() == "__independent_bits_engine") {
2146           BR.markInvalid(getTag(), nullptr);
2147           return nullptr;
2148         }
2149       }
2150 
2151       for (const LocationContext *LCtx = N->getLocationContext(); LCtx;
2152            LCtx = LCtx->getParent()) {
2153         const auto *MD = dyn_cast<CXXMethodDecl>(LCtx->getDecl());
2154         if (!MD)
2155           continue;
2156 
2157         const CXXRecordDecl *CD = MD->getParent();
2158         // The analyzer issues a false positive on
2159         //   std::basic_string<uint8_t> v; v.push_back(1);
2160         // and
2161         //   std::u16string s; s += u'a';
2162         // because we cannot reason about the internal invariants of the
2163         // data structure.
2164         if (CD->getName() == "basic_string") {
2165           BR.markInvalid(getTag(), nullptr);
2166           return nullptr;
2167         }
2168 
2169         // The analyzer issues a false positive on
2170         //    std::shared_ptr<int> p(new int(1)); p = nullptr;
2171         // because it does not reason properly about temporary destructors.
2172         if (CD->getName() == "shared_ptr") {
2173           BR.markInvalid(getTag(), nullptr);
2174           return nullptr;
2175         }
2176       }
2177     }
2178   }
2179 
2180   // Skip reports within the sys/queue.h macros as we do not have the ability to
2181   // reason about data structure shapes.
2182   SourceManager &SM = BRC.getSourceManager();
2183   FullSourceLoc Loc = BR.getLocation(SM).asLocation();
2184   while (Loc.isMacroID()) {
2185     Loc = Loc.getSpellingLoc();
2186     if (SM.getFilename(Loc).endswith("sys/queue.h")) {
2187       BR.markInvalid(getTag(), nullptr);
2188       return nullptr;
2189     }
2190   }
2191 
2192   return nullptr;
2193 }
2194 
2195 std::shared_ptr<PathDiagnosticPiece>
2196 UndefOrNullArgVisitor::VisitNode(const ExplodedNode *N,
2197                                  const ExplodedNode *PrevN,
2198                                  BugReporterContext &BRC, BugReport &BR) {
2199   ProgramStateRef State = N->getState();
2200   ProgramPoint ProgLoc = N->getLocation();
2201 
2202   // We are only interested in visiting CallEnter nodes.
2203   Optional<CallEnter> CEnter = ProgLoc.getAs<CallEnter>();
2204   if (!CEnter)
2205     return nullptr;
2206 
2207   // Check if one of the arguments is the region the visitor is tracking.
2208   CallEventManager &CEMgr = BRC.getStateManager().getCallEventManager();
2209   CallEventRef<> Call = CEMgr.getCaller(CEnter->getCalleeContext(), State);
2210   unsigned Idx = 0;
2211   ArrayRef<ParmVarDecl *> parms = Call->parameters();
2212 
2213   for (const auto ParamDecl : parms) {
2214     const MemRegion *ArgReg = Call->getArgSVal(Idx).getAsRegion();
2215     ++Idx;
2216 
2217     // Are we tracking the argument or its subregion?
2218     if ( !ArgReg || !R->isSubRegionOf(ArgReg->StripCasts()))
2219       continue;
2220 
2221     // Check the function parameter type.
2222     assert(ParamDecl && "Formal parameter has no decl?");
2223     QualType T = ParamDecl->getType();
2224 
2225     if (!(T->isAnyPointerType() || T->isReferenceType())) {
2226       // Function can only change the value passed in by address.
2227       continue;
2228     }
2229 
2230     // If it is a const pointer value, the function does not intend to
2231     // change the value.
2232     if (T->getPointeeType().isConstQualified())
2233       continue;
2234 
2235     // Mark the call site (LocationContext) as interesting if the value of the
2236     // argument is undefined or '0'/'NULL'.
2237     SVal BoundVal = State->getSVal(R);
2238     if (BoundVal.isUndef() || BoundVal.isZeroConstant()) {
2239       BR.markInteresting(CEnter->getCalleeContext());
2240       return nullptr;
2241     }
2242   }
2243   return nullptr;
2244 }
2245 
2246 std::shared_ptr<PathDiagnosticPiece>
2247 CXXSelfAssignmentBRVisitor::VisitNode(const ExplodedNode *Succ,
2248                                       const ExplodedNode *Pred,
2249                                       BugReporterContext &BRC, BugReport &BR) {
2250   if (Satisfied)
2251     return nullptr;
2252 
2253   const auto Edge = Succ->getLocation().getAs<BlockEdge>();
2254   if (!Edge.hasValue())
2255     return nullptr;
2256 
2257   auto Tag = Edge->getTag();
2258   if (!Tag)
2259     return nullptr;
2260 
2261   if (Tag->getTagDescription() != "cplusplus.SelfAssignment")
2262     return nullptr;
2263 
2264   Satisfied = true;
2265 
2266   const auto *Met =
2267       dyn_cast<CXXMethodDecl>(Succ->getCodeDecl().getAsFunction());
2268   assert(Met && "Not a C++ method.");
2269   assert((Met->isCopyAssignmentOperator() || Met->isMoveAssignmentOperator()) &&
2270          "Not a copy/move assignment operator.");
2271 
2272   const auto *LCtx = Edge->getLocationContext();
2273 
2274   const auto &State = Succ->getState();
2275   auto &SVB = State->getStateManager().getSValBuilder();
2276 
2277   const auto Param =
2278       State->getSVal(State->getRegion(Met->getParamDecl(0), LCtx));
2279   const auto This =
2280       State->getSVal(SVB.getCXXThis(Met, LCtx->getCurrentStackFrame()));
2281 
2282   auto L = PathDiagnosticLocation::create(Met, BRC.getSourceManager());
2283 
2284   if (!L.isValid() || !L.asLocation().isValid())
2285     return nullptr;
2286 
2287   SmallString<256> Buf;
2288   llvm::raw_svector_ostream Out(Buf);
2289 
2290   Out << "Assuming " << Met->getParamDecl(0)->getName() <<
2291     ((Param == This) ? " == " : " != ") << "*this";
2292 
2293   auto Piece = std::make_shared<PathDiagnosticEventPiece>(L, Out.str());
2294   Piece->addRange(Met->getSourceRange());
2295 
2296   return std::move(Piece);
2297 }
2298