1 //== Nullabilityhecker.cpp - Nullability checker ----------------*- C++ -*--==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This checker tries to find nullability violations. There are several kinds of
11 // possible violations:
12 // * Null pointer is passed to a pointer which has a _Nonnull type.
13 // * Null pointer is returned from a function which has a _Nonnull return type.
14 // * Nullable pointer is passed to a pointer which has a _Nonnull type.
15 // * Nullable pointer is returned from a function which has a _Nonnull return
16 //   type.
17 // * Nullable pointer is dereferenced.
18 //
19 // This checker propagates the nullability information of the pointers and looks
20 // for the patterns that are described above. Explicit casts are trusted and are
21 // considered a way to suppress false positives for this checker. The other way
22 // to suppress warnings would be to add asserts or guarding if statements to the
23 // code. In addition to the nullability propagation this checker also uses some
24 // heuristics to suppress potential false positives.
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #include "ClangSACheckers.h"
29 
30 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
31 #include "clang/StaticAnalyzer/Core/Checker.h"
32 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
33 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
34 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
35 
36 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/Support/Path.h"
38 
39 using namespace clang;
40 using namespace ento;
41 
42 namespace {
43 // Do not reorder! The getMostNullable method relies on the order.
44 // Optimization: Most pointers expected to be unspecified. When a symbol has an
45 // unspecified or nonnull type non of the rules would indicate any problem for
46 // that symbol. For this reason only nullable and contradicted nullability are
47 // stored for a symbol. When a symbol is already contradicted, it can not be
48 // casted back to nullable.
49 enum class Nullability : char {
50   Contradicted, // Tracked nullability is contradicted by an explicit cast. Do
51                 // not report any nullability related issue for this symbol.
52                 // This nullability is propagated agressively to avoid false
53                 // positive results. See the comment on getMostNullable method.
54   Nullable,
55   Unspecified,
56   Nonnull
57 };
58 
59 /// Returns the most nullable nullability. This is used for message expressions
60 /// like [reciever method], where the nullability of this expression is either
61 /// the nullability of the receiver or the nullability of the return type of the
62 /// method, depending on which is more nullable. Contradicted is considered to
63 /// be the most nullable, to avoid false positive results.
64 Nullability getMostNullable(Nullability Lhs, Nullability Rhs) {
65   return static_cast<Nullability>(
66       std::min(static_cast<char>(Lhs), static_cast<char>(Rhs)));
67 }
68 
69 const char *getNullabilityString(Nullability Nullab) {
70   switch (Nullab) {
71   case Nullability::Contradicted:
72     return "contradicted";
73   case Nullability::Nullable:
74     return "nullable";
75   case Nullability::Unspecified:
76     return "unspecified";
77   case Nullability::Nonnull:
78     return "nonnull";
79   }
80   llvm_unreachable("Unexpected enumeration.");
81   return "";
82 }
83 
84 // These enums are used as an index to ErrorMessages array.
85 enum class ErrorKind : int {
86   NilAssignedToNonnull,
87   NilPassedToNonnull,
88   NilReturnedToNonnull,
89   NullableAssignedToNonnull,
90   NullableReturnedToNonnull,
91   NullableDereferenced,
92   NullablePassedToNonnull
93 };
94 
95 class NullabilityChecker
96     : public Checker<check::Bind, check::PreCall, check::PreStmt<ReturnStmt>,
97                      check::PostCall, check::PostStmt<ExplicitCastExpr>,
98                      check::PostObjCMessage, check::DeadSymbols,
99                      check::Event<ImplicitNullDerefEvent>> {
100   mutable std::unique_ptr<BugType> BT;
101 
102 public:
103   // If true, the checker will not diagnose nullabilility issues for calls
104   // to system headers. This option is motivated by the observation that large
105   // projects may have many nullability warnings. These projects may
106   // find warnings about nullability annotations that they have explicitly
107   // added themselves higher priority to fix than warnings on calls to system
108   // libraries.
109   DefaultBool NoDiagnoseCallsToSystemHeaders;
110 
111   void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const;
112   void checkPostStmt(const ExplicitCastExpr *CE, CheckerContext &C) const;
113   void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
114   void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;
115   void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
116   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
117   void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
118   void checkEvent(ImplicitNullDerefEvent Event) const;
119 
120   void printState(raw_ostream &Out, ProgramStateRef State, const char *NL,
121                   const char *Sep) const override;
122 
123   struct NullabilityChecksFilter {
124     DefaultBool CheckNullPassedToNonnull;
125     DefaultBool CheckNullReturnedFromNonnull;
126     DefaultBool CheckNullableDereferenced;
127     DefaultBool CheckNullablePassedToNonnull;
128     DefaultBool CheckNullableReturnedFromNonnull;
129 
130     CheckName CheckNameNullPassedToNonnull;
131     CheckName CheckNameNullReturnedFromNonnull;
132     CheckName CheckNameNullableDereferenced;
133     CheckName CheckNameNullablePassedToNonnull;
134     CheckName CheckNameNullableReturnedFromNonnull;
135   };
136 
137   NullabilityChecksFilter Filter;
138   // When set to false no nullability information will be tracked in
139   // NullabilityMap. It is possible to catch errors like passing a null pointer
140   // to a callee that expects nonnull argument without the information that is
141   // stroed in the NullabilityMap. This is an optimization.
142   DefaultBool NeedTracking;
143 
144 private:
145   class NullabilityBugVisitor
146       : public BugReporterVisitorImpl<NullabilityBugVisitor> {
147   public:
148     NullabilityBugVisitor(const MemRegion *M) : Region(M) {}
149 
150     void Profile(llvm::FoldingSetNodeID &ID) const override {
151       static int X = 0;
152       ID.AddPointer(&X);
153       ID.AddPointer(Region);
154     }
155 
156     PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
157                                    const ExplodedNode *PrevN,
158                                    BugReporterContext &BRC,
159                                    BugReport &BR) override;
160 
161   private:
162     // The tracked region.
163     const MemRegion *Region;
164   };
165 
166   /// When any of the nonnull arguments of the analyzed function is null, do not
167   /// report anything and turn off the check.
168   ///
169   /// When \p SuppressPath is set to true, no more bugs will be reported on this
170   /// path by this checker.
171   void reportBugIfInvariantHolds(StringRef Msg, ErrorKind Error,
172                                  ExplodedNode *N, const MemRegion *Region,
173                                  CheckerContext &C,
174                                  const Stmt *ValueExpr = nullptr,
175                                   bool SuppressPath = false) const;
176 
177   void reportBug(StringRef Msg, ErrorKind Error, ExplodedNode *N,
178                  const MemRegion *Region, BugReporter &BR,
179                  const Stmt *ValueExpr = nullptr) const {
180     if (!BT)
181       BT.reset(new BugType(this, "Nullability", "Memory error"));
182 
183     auto R = llvm::make_unique<BugReport>(*BT, Msg, N);
184     if (Region) {
185       R->markInteresting(Region);
186       R->addVisitor(llvm::make_unique<NullabilityBugVisitor>(Region));
187     }
188     if (ValueExpr) {
189       R->addRange(ValueExpr->getSourceRange());
190       if (Error == ErrorKind::NilAssignedToNonnull ||
191           Error == ErrorKind::NilPassedToNonnull ||
192           Error == ErrorKind::NilReturnedToNonnull)
193         bugreporter::trackNullOrUndefValue(N, ValueExpr, *R);
194     }
195     BR.emitReport(std::move(R));
196   }
197 
198   /// If an SVal wraps a region that should be tracked, it will return a pointer
199   /// to the wrapped region. Otherwise it will return a nullptr.
200   const SymbolicRegion *getTrackRegion(SVal Val,
201                                        bool CheckSuperRegion = false) const;
202 
203   /// Returns true if the call is diagnosable in the currrent analyzer
204   /// configuration.
205   bool isDiagnosableCall(const CallEvent &Call) const {
206     if (NoDiagnoseCallsToSystemHeaders && Call.isInSystemHeader())
207       return false;
208 
209     return true;
210   }
211 };
212 
213 class NullabilityState {
214 public:
215   NullabilityState(Nullability Nullab, const Stmt *Source = nullptr)
216       : Nullab(Nullab), Source(Source) {}
217 
218   const Stmt *getNullabilitySource() const { return Source; }
219 
220   Nullability getValue() const { return Nullab; }
221 
222   void Profile(llvm::FoldingSetNodeID &ID) const {
223     ID.AddInteger(static_cast<char>(Nullab));
224     ID.AddPointer(Source);
225   }
226 
227   void print(raw_ostream &Out) const {
228     Out << getNullabilityString(Nullab) << "\n";
229   }
230 
231 private:
232   Nullability Nullab;
233   // Source is the expression which determined the nullability. For example in a
234   // message like [nullable nonnull_returning] has nullable nullability, because
235   // the receiver is nullable. Here the receiver will be the source of the
236   // nullability. This is useful information when the diagnostics are generated.
237   const Stmt *Source;
238 };
239 
240 bool operator==(NullabilityState Lhs, NullabilityState Rhs) {
241   return Lhs.getValue() == Rhs.getValue() &&
242          Lhs.getNullabilitySource() == Rhs.getNullabilitySource();
243 }
244 
245 } // end anonymous namespace
246 
247 REGISTER_MAP_WITH_PROGRAMSTATE(NullabilityMap, const MemRegion *,
248                                NullabilityState)
249 
250 // We say "the nullability type invariant is violated" when a location with a
251 // non-null type contains NULL or a function with a non-null return type returns
252 // NULL. Violations of the nullability type invariant can be detected either
253 // directly (for example, when NULL is passed as an argument to a nonnull
254 // parameter) or indirectly (for example, when, inside a function, the
255 // programmer defensively checks whether a nonnull parameter contains NULL and
256 // finds that it does).
257 //
258 // As a matter of policy, the nullability checker typically warns on direct
259 // violations of the nullability invariant (although it uses various
260 // heuristics to suppress warnings in some cases) but will not warn if the
261 // invariant has already been violated along the path (either directly or
262 // indirectly). As a practical matter, this prevents the analyzer from
263 // (1) warning on defensive code paths where a nullability precondition is
264 // determined to have been violated, (2) warning additional times after an
265 // initial direct violation has been discovered, and (3) warning after a direct
266 // violation that has been implicitly or explicitly suppressed (for
267 // example, with a cast of NULL to _Nonnull). In essence, once an invariant
268 // violation is detected on a path, this checker will be esentially turned off
269 // for the rest of the analysis
270 //
271 // The analyzer takes this approach (rather than generating a sink node) to
272 // ensure coverage of defensive paths, which may be important for backwards
273 // compatibility in codebases that were developed without nullability in mind.
274 REGISTER_TRAIT_WITH_PROGRAMSTATE(InvariantViolated, bool)
275 
276 enum class NullConstraint { IsNull, IsNotNull, Unknown };
277 
278 static NullConstraint getNullConstraint(DefinedOrUnknownSVal Val,
279                                         ProgramStateRef State) {
280   ConditionTruthVal Nullness = State->isNull(Val);
281   if (Nullness.isConstrainedFalse())
282     return NullConstraint::IsNotNull;
283   if (Nullness.isConstrainedTrue())
284     return NullConstraint::IsNull;
285   return NullConstraint::Unknown;
286 }
287 
288 const SymbolicRegion *
289 NullabilityChecker::getTrackRegion(SVal Val, bool CheckSuperRegion) const {
290   if (!NeedTracking)
291     return nullptr;
292 
293   auto RegionSVal = Val.getAs<loc::MemRegionVal>();
294   if (!RegionSVal)
295     return nullptr;
296 
297   const MemRegion *Region = RegionSVal->getRegion();
298 
299   if (CheckSuperRegion) {
300     if (auto FieldReg = Region->getAs<FieldRegion>())
301       return dyn_cast<SymbolicRegion>(FieldReg->getSuperRegion());
302     if (auto ElementReg = Region->getAs<ElementRegion>())
303       return dyn_cast<SymbolicRegion>(ElementReg->getSuperRegion());
304   }
305 
306   return dyn_cast<SymbolicRegion>(Region);
307 }
308 
309 PathDiagnosticPiece *NullabilityChecker::NullabilityBugVisitor::VisitNode(
310     const ExplodedNode *N, const ExplodedNode *PrevN, BugReporterContext &BRC,
311     BugReport &BR) {
312   ProgramStateRef State = N->getState();
313   ProgramStateRef StatePrev = PrevN->getState();
314 
315   const NullabilityState *TrackedNullab = State->get<NullabilityMap>(Region);
316   const NullabilityState *TrackedNullabPrev =
317       StatePrev->get<NullabilityMap>(Region);
318   if (!TrackedNullab)
319     return nullptr;
320 
321   if (TrackedNullabPrev &&
322       TrackedNullabPrev->getValue() == TrackedNullab->getValue())
323     return nullptr;
324 
325   // Retrieve the associated statement.
326   const Stmt *S = TrackedNullab->getNullabilitySource();
327   if (!S) {
328     ProgramPoint ProgLoc = N->getLocation();
329     if (Optional<StmtPoint> SP = ProgLoc.getAs<StmtPoint>()) {
330       S = SP->getStmt();
331     }
332   }
333 
334   if (!S)
335     return nullptr;
336 
337   std::string InfoText =
338       (llvm::Twine("Nullability '") +
339        getNullabilityString(TrackedNullab->getValue()) + "' is infered")
340           .str();
341 
342   // Generate the extra diagnostic.
343   PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
344                              N->getLocationContext());
345   return new PathDiagnosticEventPiece(Pos, InfoText, true, nullptr);
346 }
347 
348 static Nullability getNullabilityAnnotation(QualType Type) {
349   const auto *AttrType = Type->getAs<AttributedType>();
350   if (!AttrType)
351     return Nullability::Unspecified;
352   if (AttrType->getAttrKind() == AttributedType::attr_nullable)
353     return Nullability::Nullable;
354   else if (AttrType->getAttrKind() == AttributedType::attr_nonnull)
355     return Nullability::Nonnull;
356   return Nullability::Unspecified;
357 }
358 
359 template <typename ParamVarDeclRange>
360 static bool
361 checkParamsForPreconditionViolation(const ParamVarDeclRange &Params,
362                                     ProgramStateRef State,
363                                     const LocationContext *LocCtxt) {
364   for (const auto *ParamDecl : Params) {
365     if (ParamDecl->isParameterPack())
366       break;
367 
368     if (getNullabilityAnnotation(ParamDecl->getType()) != Nullability::Nonnull)
369       continue;
370 
371     auto RegVal = State->getLValue(ParamDecl, LocCtxt)
372                       .template getAs<loc::MemRegionVal>();
373     if (!RegVal)
374       continue;
375 
376     auto ParamValue = State->getSVal(RegVal->getRegion())
377                           .template getAs<DefinedOrUnknownSVal>();
378     if (!ParamValue)
379       continue;
380 
381     if (getNullConstraint(*ParamValue, State) == NullConstraint::IsNull) {
382       return true;
383     }
384   }
385   return false;
386 }
387 
388 static bool checkInvariantViolation(ProgramStateRef State, ExplodedNode *N,
389                                     CheckerContext &C) {
390   if (State->get<InvariantViolated>())
391     return true;
392 
393   const LocationContext *LocCtxt = C.getLocationContext();
394   const Decl *D = LocCtxt->getDecl();
395   if (!D)
396     return false;
397 
398   ArrayRef<ParmVarDecl*> Params;
399   if (const auto *BD = dyn_cast<BlockDecl>(D))
400     Params = BD->parameters();
401   else if (const auto *FD = dyn_cast<FunctionDecl>(D))
402     Params = FD->parameters();
403   else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
404     Params = MD->parameters();
405   else
406     return false;
407 
408   if (checkParamsForPreconditionViolation(Params, State, LocCtxt)) {
409     if (!N->isSink())
410       C.addTransition(State->set<InvariantViolated>(true), N);
411     return true;
412   }
413   return false;
414 }
415 
416 void NullabilityChecker::reportBugIfInvariantHolds(StringRef Msg,
417     ErrorKind Error, ExplodedNode *N, const MemRegion *Region,
418     CheckerContext &C, const Stmt *ValueExpr, bool SuppressPath) const {
419   ProgramStateRef OriginalState = N->getState();
420 
421   if (checkInvariantViolation(OriginalState, N, C))
422     return;
423   if (SuppressPath) {
424     OriginalState = OriginalState->set<InvariantViolated>(true);
425     N = C.addTransition(OriginalState, N);
426   }
427 
428   reportBug(Msg, Error, N, Region, C.getBugReporter(), ValueExpr);
429 }
430 
431 /// Cleaning up the program state.
432 void NullabilityChecker::checkDeadSymbols(SymbolReaper &SR,
433                                           CheckerContext &C) const {
434   if (!SR.hasDeadSymbols())
435     return;
436 
437   ProgramStateRef State = C.getState();
438   NullabilityMapTy Nullabilities = State->get<NullabilityMap>();
439   for (NullabilityMapTy::iterator I = Nullabilities.begin(),
440                                   E = Nullabilities.end();
441        I != E; ++I) {
442     const auto *Region = I->first->getAs<SymbolicRegion>();
443     assert(Region && "Non-symbolic region is tracked.");
444     if (SR.isDead(Region->getSymbol())) {
445       State = State->remove<NullabilityMap>(I->first);
446     }
447   }
448   // When one of the nonnull arguments are constrained to be null, nullability
449   // preconditions are violated. It is not enough to check this only when we
450   // actually report an error, because at that time interesting symbols might be
451   // reaped.
452   if (checkInvariantViolation(State, C.getPredecessor(), C))
453     return;
454   C.addTransition(State);
455 }
456 
457 /// This callback triggers when a pointer is dereferenced and the analyzer does
458 /// not know anything about the value of that pointer. When that pointer is
459 /// nullable, this code emits a warning.
460 void NullabilityChecker::checkEvent(ImplicitNullDerefEvent Event) const {
461   if (Event.SinkNode->getState()->get<InvariantViolated>())
462     return;
463 
464   const MemRegion *Region =
465       getTrackRegion(Event.Location, /*CheckSuperregion=*/true);
466   if (!Region)
467     return;
468 
469   ProgramStateRef State = Event.SinkNode->getState();
470   const NullabilityState *TrackedNullability =
471       State->get<NullabilityMap>(Region);
472 
473   if (!TrackedNullability)
474     return;
475 
476   if (Filter.CheckNullableDereferenced &&
477       TrackedNullability->getValue() == Nullability::Nullable) {
478     BugReporter &BR = *Event.BR;
479     // Do not suppress errors on defensive code paths, because dereferencing
480     // a nullable pointer is always an error.
481     if (Event.IsDirectDereference)
482       reportBug("Nullable pointer is dereferenced",
483                 ErrorKind::NullableDereferenced, Event.SinkNode, Region, BR);
484     else {
485       reportBug("Nullable pointer is passed to a callee that requires a "
486                 "non-null", ErrorKind::NullablePassedToNonnull,
487                 Event.SinkNode, Region, BR);
488     }
489   }
490 }
491 
492 /// Find the outermost subexpression of E that is not an implicit cast.
493 /// This looks through the implicit casts to _Nonnull that ARC adds to
494 /// return expressions of ObjC types when the return type of the function or
495 /// method is non-null but the express is not.
496 static const Expr *lookThroughImplicitCasts(const Expr *E) {
497   assert(E);
498 
499   while (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) {
500     E = ICE->getSubExpr();
501   }
502 
503   return E;
504 }
505 
506 /// This method check when nullable pointer or null value is returned from a
507 /// function that has nonnull return type.
508 void NullabilityChecker::checkPreStmt(const ReturnStmt *S,
509                                       CheckerContext &C) const {
510   auto RetExpr = S->getRetValue();
511   if (!RetExpr)
512     return;
513 
514   if (!RetExpr->getType()->isAnyPointerType())
515     return;
516 
517   ProgramStateRef State = C.getState();
518   if (State->get<InvariantViolated>())
519     return;
520 
521   auto RetSVal =
522       State->getSVal(S, C.getLocationContext()).getAs<DefinedOrUnknownSVal>();
523   if (!RetSVal)
524     return;
525 
526   bool InSuppressedMethodFamily = false;
527 
528   QualType RequiredRetType;
529   AnalysisDeclContext *DeclCtxt =
530       C.getLocationContext()->getAnalysisDeclContext();
531   const Decl *D = DeclCtxt->getDecl();
532   if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
533     // HACK: This is a big hammer to avoid warning when there are defensive
534     // nil checks in -init and -copy methods. We should add more sophisticated
535     // logic here to suppress on common defensive idioms but still
536     // warn when there is a likely problem.
537     ObjCMethodFamily Family = MD->getMethodFamily();
538     if (OMF_init == Family || OMF_copy == Family || OMF_mutableCopy == Family)
539       InSuppressedMethodFamily = true;
540 
541     RequiredRetType = MD->getReturnType();
542   } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
543     RequiredRetType = FD->getReturnType();
544   } else {
545     return;
546   }
547 
548   NullConstraint Nullness = getNullConstraint(*RetSVal, State);
549 
550   Nullability RequiredNullability = getNullabilityAnnotation(RequiredRetType);
551 
552   // If the returned value is null but the type of the expression
553   // generating it is nonnull then we will suppress the diagnostic.
554   // This enables explicit suppression when returning a nil literal in a
555   // function with a _Nonnull return type:
556   //    return (NSString * _Nonnull)0;
557   Nullability RetExprTypeLevelNullability =
558         getNullabilityAnnotation(lookThroughImplicitCasts(RetExpr)->getType());
559 
560   bool NullReturnedFromNonNull = (RequiredNullability == Nullability::Nonnull &&
561                                   Nullness == NullConstraint::IsNull);
562   if (Filter.CheckNullReturnedFromNonnull &&
563       NullReturnedFromNonNull &&
564       RetExprTypeLevelNullability != Nullability::Nonnull &&
565       !InSuppressedMethodFamily) {
566     static CheckerProgramPointTag Tag(this, "NullReturnedFromNonnull");
567     ExplodedNode *N = C.generateErrorNode(State, &Tag);
568     if (!N)
569       return;
570 
571     SmallString<256> SBuf;
572     llvm::raw_svector_ostream OS(SBuf);
573     OS << "Null is returned from a " << C.getDeclDescription(D) <<
574           " that is expected to return a non-null value";
575 
576     reportBugIfInvariantHolds(OS.str(),
577                               ErrorKind::NilReturnedToNonnull, N, nullptr, C,
578                               RetExpr);
579     return;
580   }
581 
582   // If null was returned from a non-null function, mark the nullability
583   // invariant as violated even if the diagnostic was suppressed.
584   if (NullReturnedFromNonNull) {
585     State = State->set<InvariantViolated>(true);
586     C.addTransition(State);
587     return;
588   }
589 
590   const MemRegion *Region = getTrackRegion(*RetSVal);
591   if (!Region)
592     return;
593 
594   const NullabilityState *TrackedNullability =
595       State->get<NullabilityMap>(Region);
596   if (TrackedNullability) {
597     Nullability TrackedNullabValue = TrackedNullability->getValue();
598     if (Filter.CheckNullableReturnedFromNonnull &&
599         Nullness != NullConstraint::IsNotNull &&
600         TrackedNullabValue == Nullability::Nullable &&
601         RequiredNullability == Nullability::Nonnull) {
602       static CheckerProgramPointTag Tag(this, "NullableReturnedFromNonnull");
603       ExplodedNode *N = C.addTransition(State, C.getPredecessor(), &Tag);
604 
605       SmallString<256> SBuf;
606       llvm::raw_svector_ostream OS(SBuf);
607       OS << "Nullable pointer is returned from a " << C.getDeclDescription(D) <<
608             " that is expected to return a non-null value";
609 
610       reportBugIfInvariantHolds(OS.str(),
611                                 ErrorKind::NullableReturnedToNonnull, N,
612                                 Region, C);
613     }
614     return;
615   }
616   if (RequiredNullability == Nullability::Nullable) {
617     State = State->set<NullabilityMap>(Region,
618                                        NullabilityState(RequiredNullability,
619                                                         S));
620     C.addTransition(State);
621   }
622 }
623 
624 /// This callback warns when a nullable pointer or a null value is passed to a
625 /// function that expects its argument to be nonnull.
626 void NullabilityChecker::checkPreCall(const CallEvent &Call,
627                                       CheckerContext &C) const {
628   if (!Call.getDecl())
629     return;
630 
631   ProgramStateRef State = C.getState();
632   if (State->get<InvariantViolated>())
633     return;
634 
635   ProgramStateRef OrigState = State;
636 
637   unsigned Idx = 0;
638   for (const ParmVarDecl *Param : Call.parameters()) {
639     if (Param->isParameterPack())
640       break;
641 
642     const Expr *ArgExpr = nullptr;
643     if (Idx < Call.getNumArgs())
644       ArgExpr = Call.getArgExpr(Idx);
645     auto ArgSVal = Call.getArgSVal(Idx++).getAs<DefinedOrUnknownSVal>();
646     if (!ArgSVal)
647       continue;
648 
649     if (!Param->getType()->isAnyPointerType() &&
650         !Param->getType()->isReferenceType())
651       continue;
652 
653     NullConstraint Nullness = getNullConstraint(*ArgSVal, State);
654 
655     Nullability RequiredNullability =
656         getNullabilityAnnotation(Param->getType());
657     Nullability ArgExprTypeLevelNullability =
658         getNullabilityAnnotation(ArgExpr->getType());
659 
660     unsigned ParamIdx = Param->getFunctionScopeIndex() + 1;
661 
662     if (Filter.CheckNullPassedToNonnull && Nullness == NullConstraint::IsNull &&
663         ArgExprTypeLevelNullability != Nullability::Nonnull &&
664         RequiredNullability == Nullability::Nonnull &&
665         isDiagnosableCall(Call)) {
666       ExplodedNode *N = C.generateErrorNode(State);
667       if (!N)
668         return;
669       SmallString<256> SBuf;
670       llvm::raw_svector_ostream OS(SBuf);
671       OS << "Null passed to a callee that requires a non-null " << ParamIdx
672          << llvm::getOrdinalSuffix(ParamIdx) << " parameter";
673       reportBugIfInvariantHolds(OS.str(), ErrorKind::NilPassedToNonnull, N,
674                                 nullptr, C,
675                                 ArgExpr, /*SuppressPath=*/false);
676       return;
677     }
678 
679     const MemRegion *Region = getTrackRegion(*ArgSVal);
680     if (!Region)
681       continue;
682 
683     const NullabilityState *TrackedNullability =
684         State->get<NullabilityMap>(Region);
685 
686     if (TrackedNullability) {
687       if (Nullness == NullConstraint::IsNotNull ||
688           TrackedNullability->getValue() != Nullability::Nullable)
689         continue;
690 
691       if (Filter.CheckNullablePassedToNonnull &&
692           RequiredNullability == Nullability::Nonnull &&
693           isDiagnosableCall(Call)) {
694         ExplodedNode *N = C.addTransition(State);
695         SmallString<256> SBuf;
696         llvm::raw_svector_ostream OS(SBuf);
697         OS << "Nullable pointer is passed to a callee that requires a non-null "
698            << ParamIdx << llvm::getOrdinalSuffix(ParamIdx) << " parameter";
699         reportBugIfInvariantHolds(OS.str(),
700                                   ErrorKind::NullablePassedToNonnull, N,
701                                   Region, C, ArgExpr, /*SuppressPath=*/true);
702         return;
703       }
704       if (Filter.CheckNullableDereferenced &&
705           Param->getType()->isReferenceType()) {
706         ExplodedNode *N = C.addTransition(State);
707         reportBugIfInvariantHolds("Nullable pointer is dereferenced",
708                                   ErrorKind::NullableDereferenced, N, Region,
709                                   C, ArgExpr, /*SuppressPath=*/true);
710         return;
711       }
712       continue;
713     }
714     // No tracked nullability yet.
715     if (ArgExprTypeLevelNullability != Nullability::Nullable)
716       continue;
717     State = State->set<NullabilityMap>(
718         Region, NullabilityState(ArgExprTypeLevelNullability, ArgExpr));
719   }
720   if (State != OrigState)
721     C.addTransition(State);
722 }
723 
724 /// Suppress the nullability warnings for some functions.
725 void NullabilityChecker::checkPostCall(const CallEvent &Call,
726                                        CheckerContext &C) const {
727   auto Decl = Call.getDecl();
728   if (!Decl)
729     return;
730   // ObjC Messages handles in a different callback.
731   if (Call.getKind() == CE_ObjCMessage)
732     return;
733   const FunctionType *FuncType = Decl->getFunctionType();
734   if (!FuncType)
735     return;
736   QualType ReturnType = FuncType->getReturnType();
737   if (!ReturnType->isAnyPointerType())
738     return;
739   ProgramStateRef State = C.getState();
740   if (State->get<InvariantViolated>())
741     return;
742 
743   const MemRegion *Region = getTrackRegion(Call.getReturnValue());
744   if (!Region)
745     return;
746 
747   // CG headers are misannotated. Do not warn for symbols that are the results
748   // of CG calls.
749   const SourceManager &SM = C.getSourceManager();
750   StringRef FilePath = SM.getFilename(SM.getSpellingLoc(Decl->getLocStart()));
751   if (llvm::sys::path::filename(FilePath).startswith("CG")) {
752     State = State->set<NullabilityMap>(Region, Nullability::Contradicted);
753     C.addTransition(State);
754     return;
755   }
756 
757   const NullabilityState *TrackedNullability =
758       State->get<NullabilityMap>(Region);
759 
760   if (!TrackedNullability &&
761       getNullabilityAnnotation(ReturnType) == Nullability::Nullable) {
762     State = State->set<NullabilityMap>(Region, Nullability::Nullable);
763     C.addTransition(State);
764   }
765 }
766 
767 static Nullability getReceiverNullability(const ObjCMethodCall &M,
768                                           ProgramStateRef State) {
769   if (M.isReceiverSelfOrSuper()) {
770     // For super and super class receivers we assume that the receiver is
771     // nonnull.
772     return Nullability::Nonnull;
773   }
774   // Otherwise look up nullability in the state.
775   SVal Receiver = M.getReceiverSVal();
776   if (auto DefOrUnknown = Receiver.getAs<DefinedOrUnknownSVal>()) {
777     // If the receiver is constrained to be nonnull, assume that it is nonnull
778     // regardless of its type.
779     NullConstraint Nullness = getNullConstraint(*DefOrUnknown, State);
780     if (Nullness == NullConstraint::IsNotNull)
781       return Nullability::Nonnull;
782   }
783   auto ValueRegionSVal = Receiver.getAs<loc::MemRegionVal>();
784   if (ValueRegionSVal) {
785     const MemRegion *SelfRegion = ValueRegionSVal->getRegion();
786     assert(SelfRegion);
787 
788     const NullabilityState *TrackedSelfNullability =
789         State->get<NullabilityMap>(SelfRegion);
790     if (TrackedSelfNullability)
791       return TrackedSelfNullability->getValue();
792   }
793   return Nullability::Unspecified;
794 }
795 
796 /// Calculate the nullability of the result of a message expr based on the
797 /// nullability of the receiver, the nullability of the return value, and the
798 /// constraints.
799 void NullabilityChecker::checkPostObjCMessage(const ObjCMethodCall &M,
800                                               CheckerContext &C) const {
801   auto Decl = M.getDecl();
802   if (!Decl)
803     return;
804   QualType RetType = Decl->getReturnType();
805   if (!RetType->isAnyPointerType())
806     return;
807 
808   ProgramStateRef State = C.getState();
809   if (State->get<InvariantViolated>())
810     return;
811 
812   const MemRegion *ReturnRegion = getTrackRegion(M.getReturnValue());
813   if (!ReturnRegion)
814     return;
815 
816   auto Interface = Decl->getClassInterface();
817   auto Name = Interface ? Interface->getName() : "";
818   // In order to reduce the noise in the diagnostics generated by this checker,
819   // some framework and programming style based heuristics are used. These
820   // heuristics are for Cocoa APIs which have NS prefix.
821   if (Name.startswith("NS")) {
822     // Developers rely on dynamic invariants such as an item should be available
823     // in a collection, or a collection is not empty often. Those invariants can
824     // not be inferred by any static analysis tool. To not to bother the users
825     // with too many false positives, every item retrieval function should be
826     // ignored for collections. The instance methods of dictionaries in Cocoa
827     // are either item retrieval related or not interesting nullability wise.
828     // Using this fact, to keep the code easier to read just ignore the return
829     // value of every instance method of dictionaries.
830     if (M.isInstanceMessage() && Name.find("Dictionary") != StringRef::npos) {
831       State =
832           State->set<NullabilityMap>(ReturnRegion, Nullability::Contradicted);
833       C.addTransition(State);
834       return;
835     }
836     // For similar reasons ignore some methods of Cocoa arrays.
837     StringRef FirstSelectorSlot = M.getSelector().getNameForSlot(0);
838     if (Name.find("Array") != StringRef::npos &&
839         (FirstSelectorSlot == "firstObject" ||
840          FirstSelectorSlot == "lastObject")) {
841       State =
842           State->set<NullabilityMap>(ReturnRegion, Nullability::Contradicted);
843       C.addTransition(State);
844       return;
845     }
846 
847     // Encoding related methods of string should not fail when lossless
848     // encodings are used. Using lossless encodings is so frequent that ignoring
849     // this class of methods reduced the emitted diagnostics by about 30% on
850     // some projects (and all of that was false positives).
851     if (Name.find("String") != StringRef::npos) {
852       for (auto Param : M.parameters()) {
853         if (Param->getName() == "encoding") {
854           State = State->set<NullabilityMap>(ReturnRegion,
855                                              Nullability::Contradicted);
856           C.addTransition(State);
857           return;
858         }
859       }
860     }
861   }
862 
863   const ObjCMessageExpr *Message = M.getOriginExpr();
864   Nullability SelfNullability = getReceiverNullability(M, State);
865 
866   const NullabilityState *NullabilityOfReturn =
867       State->get<NullabilityMap>(ReturnRegion);
868 
869   if (NullabilityOfReturn) {
870     // When we have a nullability tracked for the return value, the nullability
871     // of the expression will be the most nullable of the receiver and the
872     // return value.
873     Nullability RetValTracked = NullabilityOfReturn->getValue();
874     Nullability ComputedNullab =
875         getMostNullable(RetValTracked, SelfNullability);
876     if (ComputedNullab != RetValTracked &&
877         ComputedNullab != Nullability::Unspecified) {
878       const Stmt *NullabilitySource =
879           ComputedNullab == RetValTracked
880               ? NullabilityOfReturn->getNullabilitySource()
881               : Message->getInstanceReceiver();
882       State = State->set<NullabilityMap>(
883           ReturnRegion, NullabilityState(ComputedNullab, NullabilitySource));
884       C.addTransition(State);
885     }
886     return;
887   }
888 
889   // No tracked information. Use static type information for return value.
890   Nullability RetNullability = getNullabilityAnnotation(RetType);
891 
892   // Properties might be computed. For this reason the static analyzer creates a
893   // new symbol each time an unknown property  is read. To avoid false pozitives
894   // do not treat unknown properties as nullable, even when they explicitly
895   // marked nullable.
896   if (M.getMessageKind() == OCM_PropertyAccess && !C.wasInlined)
897     RetNullability = Nullability::Nonnull;
898 
899   Nullability ComputedNullab = getMostNullable(RetNullability, SelfNullability);
900   if (ComputedNullab == Nullability::Nullable) {
901     const Stmt *NullabilitySource = ComputedNullab == RetNullability
902                                         ? Message
903                                         : Message->getInstanceReceiver();
904     State = State->set<NullabilityMap>(
905         ReturnRegion, NullabilityState(ComputedNullab, NullabilitySource));
906     C.addTransition(State);
907   }
908 }
909 
910 /// Explicit casts are trusted. If there is a disagreement in the nullability
911 /// annotations in the destination and the source or '0' is casted to nonnull
912 /// track the value as having contraditory nullability. This will allow users to
913 /// suppress warnings.
914 void NullabilityChecker::checkPostStmt(const ExplicitCastExpr *CE,
915                                        CheckerContext &C) const {
916   QualType OriginType = CE->getSubExpr()->getType();
917   QualType DestType = CE->getType();
918   if (!OriginType->isAnyPointerType())
919     return;
920   if (!DestType->isAnyPointerType())
921     return;
922 
923   ProgramStateRef State = C.getState();
924   if (State->get<InvariantViolated>())
925     return;
926 
927   Nullability DestNullability = getNullabilityAnnotation(DestType);
928 
929   // No explicit nullability in the destination type, so this cast does not
930   // change the nullability.
931   if (DestNullability == Nullability::Unspecified)
932     return;
933 
934   auto RegionSVal =
935       State->getSVal(CE, C.getLocationContext()).getAs<DefinedOrUnknownSVal>();
936   const MemRegion *Region = getTrackRegion(*RegionSVal);
937   if (!Region)
938     return;
939 
940   // When 0 is converted to nonnull mark it as contradicted.
941   if (DestNullability == Nullability::Nonnull) {
942     NullConstraint Nullness = getNullConstraint(*RegionSVal, State);
943     if (Nullness == NullConstraint::IsNull) {
944       State = State->set<NullabilityMap>(Region, Nullability::Contradicted);
945       C.addTransition(State);
946       return;
947     }
948   }
949 
950   const NullabilityState *TrackedNullability =
951       State->get<NullabilityMap>(Region);
952 
953   if (!TrackedNullability) {
954     if (DestNullability != Nullability::Nullable)
955       return;
956     State = State->set<NullabilityMap>(Region,
957                                        NullabilityState(DestNullability, CE));
958     C.addTransition(State);
959     return;
960   }
961 
962   if (TrackedNullability->getValue() != DestNullability &&
963       TrackedNullability->getValue() != Nullability::Contradicted) {
964     State = State->set<NullabilityMap>(Region, Nullability::Contradicted);
965     C.addTransition(State);
966   }
967 }
968 
969 /// For a given statement performing a bind, attempt to syntactically
970 /// match the expression resulting in the bound value.
971 static const Expr * matchValueExprForBind(const Stmt *S) {
972   // For `x = e` the value expression is the right-hand side.
973   if (auto *BinOp = dyn_cast<BinaryOperator>(S)) {
974     if (BinOp->getOpcode() == BO_Assign)
975       return BinOp->getRHS();
976   }
977 
978   // For `int x = e` the value expression is the initializer.
979   if (auto *DS = dyn_cast<DeclStmt>(S))  {
980     if (DS->isSingleDecl()) {
981       auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
982       if (!VD)
983         return nullptr;
984 
985       if (const Expr *Init = VD->getInit())
986         return Init;
987     }
988   }
989 
990   return nullptr;
991 }
992 
993 /// Returns true if \param S is a DeclStmt for a local variable that
994 /// ObjC automated reference counting initialized with zero.
995 static bool isARCNilInitializedLocal(CheckerContext &C, const Stmt *S) {
996   // We suppress diagnostics for ARC zero-initialized _Nonnull locals. This
997   // prevents false positives when a _Nonnull local variable cannot be
998   // initialized with an initialization expression:
999   //    NSString * _Nonnull s; // no-warning
1000   //    @autoreleasepool {
1001   //      s = ...
1002   //    }
1003   //
1004   // FIXME: We should treat implicitly zero-initialized _Nonnull locals as
1005   // uninitialized in Sema's UninitializedValues analysis to warn when a use of
1006   // the zero-initialized definition will unexpectedly yield nil.
1007 
1008   // Locals are only zero-initialized when automated reference counting
1009   // is turned on.
1010   if (!C.getASTContext().getLangOpts().ObjCAutoRefCount)
1011     return false;
1012 
1013   auto *DS = dyn_cast<DeclStmt>(S);
1014   if (!DS || !DS->isSingleDecl())
1015     return false;
1016 
1017   auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
1018   if (!VD)
1019     return false;
1020 
1021   // Sema only zero-initializes locals with ObjCLifetimes.
1022   if(!VD->getType().getQualifiers().hasObjCLifetime())
1023     return false;
1024 
1025   const Expr *Init = VD->getInit();
1026   assert(Init && "ObjC local under ARC without initializer");
1027 
1028   // Return false if the local is explicitly initialized (e.g., with '= nil').
1029   if (!isa<ImplicitValueInitExpr>(Init))
1030     return false;
1031 
1032   return true;
1033 }
1034 
1035 /// Propagate the nullability information through binds and warn when nullable
1036 /// pointer or null symbol is assigned to a pointer with a nonnull type.
1037 void NullabilityChecker::checkBind(SVal L, SVal V, const Stmt *S,
1038                                    CheckerContext &C) const {
1039   const TypedValueRegion *TVR =
1040       dyn_cast_or_null<TypedValueRegion>(L.getAsRegion());
1041   if (!TVR)
1042     return;
1043 
1044   QualType LocType = TVR->getValueType();
1045   if (!LocType->isAnyPointerType())
1046     return;
1047 
1048   ProgramStateRef State = C.getState();
1049   if (State->get<InvariantViolated>())
1050     return;
1051 
1052   auto ValDefOrUnknown = V.getAs<DefinedOrUnknownSVal>();
1053   if (!ValDefOrUnknown)
1054     return;
1055 
1056   NullConstraint RhsNullness = getNullConstraint(*ValDefOrUnknown, State);
1057 
1058   Nullability ValNullability = Nullability::Unspecified;
1059   if (SymbolRef Sym = ValDefOrUnknown->getAsSymbol())
1060     ValNullability = getNullabilityAnnotation(Sym->getType());
1061 
1062   Nullability LocNullability = getNullabilityAnnotation(LocType);
1063   if (Filter.CheckNullPassedToNonnull &&
1064       RhsNullness == NullConstraint::IsNull &&
1065       ValNullability != Nullability::Nonnull &&
1066       LocNullability == Nullability::Nonnull &&
1067       !isARCNilInitializedLocal(C, S)) {
1068     static CheckerProgramPointTag Tag(this, "NullPassedToNonnull");
1069     ExplodedNode *N = C.generateErrorNode(State, &Tag);
1070     if (!N)
1071       return;
1072 
1073     const Stmt *ValueExpr = matchValueExprForBind(S);
1074     if (!ValueExpr)
1075       ValueExpr = S;
1076 
1077     reportBugIfInvariantHolds("Null is assigned to a pointer which is "
1078                               "expected to have non-null value",
1079                               ErrorKind::NilAssignedToNonnull, N, nullptr, C,
1080                               ValueExpr);
1081     return;
1082   }
1083   // Intentionally missing case: '0' is bound to a reference. It is handled by
1084   // the DereferenceChecker.
1085 
1086   const MemRegion *ValueRegion = getTrackRegion(*ValDefOrUnknown);
1087   if (!ValueRegion)
1088     return;
1089 
1090   const NullabilityState *TrackedNullability =
1091       State->get<NullabilityMap>(ValueRegion);
1092 
1093   if (TrackedNullability) {
1094     if (RhsNullness == NullConstraint::IsNotNull ||
1095         TrackedNullability->getValue() != Nullability::Nullable)
1096       return;
1097     if (Filter.CheckNullablePassedToNonnull &&
1098         LocNullability == Nullability::Nonnull) {
1099       static CheckerProgramPointTag Tag(this, "NullablePassedToNonnull");
1100       ExplodedNode *N = C.addTransition(State, C.getPredecessor(), &Tag);
1101       reportBugIfInvariantHolds("Nullable pointer is assigned to a pointer "
1102                                 "which is expected to have non-null value",
1103                                 ErrorKind::NullableAssignedToNonnull, N,
1104                                 ValueRegion, C);
1105     }
1106     return;
1107   }
1108 
1109   const auto *BinOp = dyn_cast<BinaryOperator>(S);
1110 
1111   if (ValNullability == Nullability::Nullable) {
1112     // Trust the static information of the value more than the static
1113     // information on the location.
1114     const Stmt *NullabilitySource = BinOp ? BinOp->getRHS() : S;
1115     State = State->set<NullabilityMap>(
1116         ValueRegion, NullabilityState(ValNullability, NullabilitySource));
1117     C.addTransition(State);
1118     return;
1119   }
1120 
1121   if (LocNullability == Nullability::Nullable) {
1122     const Stmt *NullabilitySource = BinOp ? BinOp->getLHS() : S;
1123     State = State->set<NullabilityMap>(
1124         ValueRegion, NullabilityState(LocNullability, NullabilitySource));
1125     C.addTransition(State);
1126   }
1127 }
1128 
1129 void NullabilityChecker::printState(raw_ostream &Out, ProgramStateRef State,
1130                                     const char *NL, const char *Sep) const {
1131 
1132   NullabilityMapTy B = State->get<NullabilityMap>();
1133 
1134   if (B.isEmpty())
1135     return;
1136 
1137   Out << Sep << NL;
1138 
1139   for (NullabilityMapTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1140     Out << I->first << " : ";
1141     I->second.print(Out);
1142     Out << NL;
1143   }
1144 }
1145 
1146 #define REGISTER_CHECKER(name, trackingRequired)                               \
1147   void ento::register##name##Checker(CheckerManager &mgr) {                    \
1148     NullabilityChecker *checker = mgr.registerChecker<NullabilityChecker>();   \
1149     checker->Filter.Check##name = true;                                        \
1150     checker->Filter.CheckName##name = mgr.getCurrentCheckName();               \
1151     checker->NeedTracking = checker->NeedTracking || trackingRequired;         \
1152     checker->NoDiagnoseCallsToSystemHeaders =                                  \
1153         checker->NoDiagnoseCallsToSystemHeaders ||                             \
1154         mgr.getAnalyzerOptions().getBooleanOption(                             \
1155                       "NoDiagnoseCallsToSystemHeaders", false, checker, true); \
1156   }
1157 
1158 // The checks are likely to be turned on by default and it is possible to do
1159 // them without tracking any nullability related information. As an optimization
1160 // no nullability information will be tracked when only these two checks are
1161 // enables.
1162 REGISTER_CHECKER(NullPassedToNonnull, false)
1163 REGISTER_CHECKER(NullReturnedFromNonnull, false)
1164 
1165 REGISTER_CHECKER(NullableDereferenced, true)
1166 REGISTER_CHECKER(NullablePassedToNonnull, true)
1167 REGISTER_CHECKER(NullableReturnedFromNonnull, true)
1168