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