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