10b57cec5SDimitry Andric //===-- NullabilityChecker.cpp - Nullability checker ----------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This checker tries to find nullability violations. There are several kinds of 100b57cec5SDimitry Andric // possible violations: 110b57cec5SDimitry Andric // * Null pointer is passed to a pointer which has a _Nonnull type. 120b57cec5SDimitry Andric // * Null pointer is returned from a function which has a _Nonnull return type. 130b57cec5SDimitry Andric // * Nullable pointer is passed to a pointer which has a _Nonnull type. 140b57cec5SDimitry Andric // * Nullable pointer is returned from a function which has a _Nonnull return 150b57cec5SDimitry Andric // type. 160b57cec5SDimitry Andric // * Nullable pointer is dereferenced. 170b57cec5SDimitry Andric // 180b57cec5SDimitry Andric // This checker propagates the nullability information of the pointers and looks 190b57cec5SDimitry Andric // for the patterns that are described above. Explicit casts are trusted and are 200b57cec5SDimitry Andric // considered a way to suppress false positives for this checker. The other way 210b57cec5SDimitry Andric // to suppress warnings would be to add asserts or guarding if statements to the 220b57cec5SDimitry Andric // code. In addition to the nullability propagation this checker also uses some 230b57cec5SDimitry Andric // heuristics to suppress potential false positives. 240b57cec5SDimitry Andric // 250b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 300b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h" 310b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h" 320b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h" 330b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 340b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 370b57cec5SDimitry Andric #include "llvm/Support/Path.h" 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric using namespace clang; 400b57cec5SDimitry Andric using namespace ento; 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric namespace { 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric /// Returns the most nullable nullability. This is used for message expressions 450b57cec5SDimitry Andric /// like [receiver method], where the nullability of this expression is either 460b57cec5SDimitry Andric /// the nullability of the receiver or the nullability of the return type of the 470b57cec5SDimitry Andric /// method, depending on which is more nullable. Contradicted is considered to 480b57cec5SDimitry Andric /// be the most nullable, to avoid false positive results. 490b57cec5SDimitry Andric Nullability getMostNullable(Nullability Lhs, Nullability Rhs) { 500b57cec5SDimitry Andric return static_cast<Nullability>( 510b57cec5SDimitry Andric std::min(static_cast<char>(Lhs), static_cast<char>(Rhs))); 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric const char *getNullabilityString(Nullability Nullab) { 550b57cec5SDimitry Andric switch (Nullab) { 560b57cec5SDimitry Andric case Nullability::Contradicted: 570b57cec5SDimitry Andric return "contradicted"; 580b57cec5SDimitry Andric case Nullability::Nullable: 590b57cec5SDimitry Andric return "nullable"; 600b57cec5SDimitry Andric case Nullability::Unspecified: 610b57cec5SDimitry Andric return "unspecified"; 620b57cec5SDimitry Andric case Nullability::Nonnull: 630b57cec5SDimitry Andric return "nonnull"; 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric llvm_unreachable("Unexpected enumeration."); 660b57cec5SDimitry Andric return ""; 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric // These enums are used as an index to ErrorMessages array. 700b57cec5SDimitry Andric enum class ErrorKind : int { 710b57cec5SDimitry Andric NilAssignedToNonnull, 720b57cec5SDimitry Andric NilPassedToNonnull, 730b57cec5SDimitry Andric NilReturnedToNonnull, 740b57cec5SDimitry Andric NullableAssignedToNonnull, 750b57cec5SDimitry Andric NullableReturnedToNonnull, 760b57cec5SDimitry Andric NullableDereferenced, 770b57cec5SDimitry Andric NullablePassedToNonnull 780b57cec5SDimitry Andric }; 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric class NullabilityChecker 810b57cec5SDimitry Andric : public Checker<check::Bind, check::PreCall, check::PreStmt<ReturnStmt>, 820b57cec5SDimitry Andric check::PostCall, check::PostStmt<ExplicitCastExpr>, 83*bdd1243dSDimitry Andric check::PostObjCMessage, check::DeadSymbols, eval::Assume, 845ffd83dbSDimitry Andric check::Location, check::Event<ImplicitNullDerefEvent>> { 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric public: 870b57cec5SDimitry Andric // If true, the checker will not diagnose nullabilility issues for calls 880b57cec5SDimitry Andric // to system headers. This option is motivated by the observation that large 890b57cec5SDimitry Andric // projects may have many nullability warnings. These projects may 900b57cec5SDimitry Andric // find warnings about nullability annotations that they have explicitly 910b57cec5SDimitry Andric // added themselves higher priority to fix than warnings on calls to system 920b57cec5SDimitry Andric // libraries. 9381ad6265SDimitry Andric bool NoDiagnoseCallsToSystemHeaders = false; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const; 960b57cec5SDimitry Andric void checkPostStmt(const ExplicitCastExpr *CE, CheckerContext &C) const; 970b57cec5SDimitry Andric void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const; 980b57cec5SDimitry Andric void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const; 990b57cec5SDimitry Andric void checkPostCall(const CallEvent &Call, CheckerContext &C) const; 1000b57cec5SDimitry Andric void checkPreCall(const CallEvent &Call, CheckerContext &C) const; 1010b57cec5SDimitry Andric void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; 1020b57cec5SDimitry Andric void checkEvent(ImplicitNullDerefEvent Event) const; 1035ffd83dbSDimitry Andric void checkLocation(SVal Location, bool IsLoad, const Stmt *S, 1045ffd83dbSDimitry Andric CheckerContext &C) const; 105*bdd1243dSDimitry Andric ProgramStateRef evalAssume(ProgramStateRef State, SVal Cond, 106*bdd1243dSDimitry Andric bool Assumption) const; 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric void printState(raw_ostream &Out, ProgramStateRef State, const char *NL, 1090b57cec5SDimitry Andric const char *Sep) const override; 1100b57cec5SDimitry Andric 1115ffd83dbSDimitry Andric enum CheckKind { 1125ffd83dbSDimitry Andric CK_NullPassedToNonnull, 1135ffd83dbSDimitry Andric CK_NullReturnedFromNonnull, 1145ffd83dbSDimitry Andric CK_NullableDereferenced, 1155ffd83dbSDimitry Andric CK_NullablePassedToNonnull, 1165ffd83dbSDimitry Andric CK_NullableReturnedFromNonnull, 1175ffd83dbSDimitry Andric CK_NumCheckKinds 1180b57cec5SDimitry Andric }; 1190b57cec5SDimitry Andric 12081ad6265SDimitry Andric bool ChecksEnabled[CK_NumCheckKinds] = {false}; 1215ffd83dbSDimitry Andric CheckerNameRef CheckNames[CK_NumCheckKinds]; 1225ffd83dbSDimitry Andric mutable std::unique_ptr<BugType> BTs[CK_NumCheckKinds]; 1235ffd83dbSDimitry Andric 1245ffd83dbSDimitry Andric const std::unique_ptr<BugType> &getBugType(CheckKind Kind) const { 1255ffd83dbSDimitry Andric if (!BTs[Kind]) 1265ffd83dbSDimitry Andric BTs[Kind].reset(new BugType(CheckNames[Kind], "Nullability", 1275ffd83dbSDimitry Andric categories::MemoryError)); 1285ffd83dbSDimitry Andric return BTs[Kind]; 1295ffd83dbSDimitry Andric } 1305ffd83dbSDimitry Andric 1310b57cec5SDimitry Andric // When set to false no nullability information will be tracked in 1320b57cec5SDimitry Andric // NullabilityMap. It is possible to catch errors like passing a null pointer 1330b57cec5SDimitry Andric // to a callee that expects nonnull argument without the information that is 134*bdd1243dSDimitry Andric // stored in the NullabilityMap. This is an optimization. 13581ad6265SDimitry Andric bool NeedTracking = false; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric private: 1380b57cec5SDimitry Andric class NullabilityBugVisitor : public BugReporterVisitor { 1390b57cec5SDimitry Andric public: 1400b57cec5SDimitry Andric NullabilityBugVisitor(const MemRegion *M) : Region(M) {} 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric void Profile(llvm::FoldingSetNodeID &ID) const override { 1430b57cec5SDimitry Andric static int X = 0; 1440b57cec5SDimitry Andric ID.AddPointer(&X); 1450b57cec5SDimitry Andric ID.AddPointer(Region); 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 148a7dea167SDimitry Andric PathDiagnosticPieceRef VisitNode(const ExplodedNode *N, 1490b57cec5SDimitry Andric BugReporterContext &BRC, 150a7dea167SDimitry Andric PathSensitiveBugReport &BR) override; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric private: 1530b57cec5SDimitry Andric // The tracked region. 1540b57cec5SDimitry Andric const MemRegion *Region; 1550b57cec5SDimitry Andric }; 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric /// When any of the nonnull arguments of the analyzed function is null, do not 1580b57cec5SDimitry Andric /// report anything and turn off the check. 1590b57cec5SDimitry Andric /// 1600b57cec5SDimitry Andric /// When \p SuppressPath is set to true, no more bugs will be reported on this 1610b57cec5SDimitry Andric /// path by this checker. 1625ffd83dbSDimitry Andric void reportBugIfInvariantHolds(StringRef Msg, ErrorKind Error, CheckKind CK, 1630b57cec5SDimitry Andric ExplodedNode *N, const MemRegion *Region, 1640b57cec5SDimitry Andric CheckerContext &C, 1650b57cec5SDimitry Andric const Stmt *ValueExpr = nullptr, 1660b57cec5SDimitry Andric bool SuppressPath = false) const; 1670b57cec5SDimitry Andric 1685ffd83dbSDimitry Andric void reportBug(StringRef Msg, ErrorKind Error, CheckKind CK, ExplodedNode *N, 1690b57cec5SDimitry Andric const MemRegion *Region, BugReporter &BR, 1700b57cec5SDimitry Andric const Stmt *ValueExpr = nullptr) const { 1715ffd83dbSDimitry Andric const std::unique_ptr<BugType> &BT = getBugType(CK); 172a7dea167SDimitry Andric auto R = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N); 1730b57cec5SDimitry Andric if (Region) { 1740b57cec5SDimitry Andric R->markInteresting(Region); 175fe6060f1SDimitry Andric R->addVisitor<NullabilityBugVisitor>(Region); 1760b57cec5SDimitry Andric } 1770b57cec5SDimitry Andric if (ValueExpr) { 1780b57cec5SDimitry Andric R->addRange(ValueExpr->getSourceRange()); 1790b57cec5SDimitry Andric if (Error == ErrorKind::NilAssignedToNonnull || 1800b57cec5SDimitry Andric Error == ErrorKind::NilPassedToNonnull || 1810b57cec5SDimitry Andric Error == ErrorKind::NilReturnedToNonnull) 1820b57cec5SDimitry Andric if (const auto *Ex = dyn_cast<Expr>(ValueExpr)) 1830b57cec5SDimitry Andric bugreporter::trackExpressionValue(N, Ex, *R); 1840b57cec5SDimitry Andric } 1850b57cec5SDimitry Andric BR.emitReport(std::move(R)); 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric /// If an SVal wraps a region that should be tracked, it will return a pointer 1890b57cec5SDimitry Andric /// to the wrapped region. Otherwise it will return a nullptr. 1900b57cec5SDimitry Andric const SymbolicRegion *getTrackRegion(SVal Val, 1910b57cec5SDimitry Andric bool CheckSuperRegion = false) const; 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric /// Returns true if the call is diagnosable in the current analyzer 1940b57cec5SDimitry Andric /// configuration. 1950b57cec5SDimitry Andric bool isDiagnosableCall(const CallEvent &Call) const { 1960b57cec5SDimitry Andric if (NoDiagnoseCallsToSystemHeaders && Call.isInSystemHeader()) 1970b57cec5SDimitry Andric return false; 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric return true; 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric }; 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric class NullabilityState { 2040b57cec5SDimitry Andric public: 2050b57cec5SDimitry Andric NullabilityState(Nullability Nullab, const Stmt *Source = nullptr) 2060b57cec5SDimitry Andric : Nullab(Nullab), Source(Source) {} 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric const Stmt *getNullabilitySource() const { return Source; } 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric Nullability getValue() const { return Nullab; } 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric void Profile(llvm::FoldingSetNodeID &ID) const { 2130b57cec5SDimitry Andric ID.AddInteger(static_cast<char>(Nullab)); 2140b57cec5SDimitry Andric ID.AddPointer(Source); 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric void print(raw_ostream &Out) const { 2180b57cec5SDimitry Andric Out << getNullabilityString(Nullab) << "\n"; 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric private: 2220b57cec5SDimitry Andric Nullability Nullab; 2230b57cec5SDimitry Andric // Source is the expression which determined the nullability. For example in a 2240b57cec5SDimitry Andric // message like [nullable nonnull_returning] has nullable nullability, because 2250b57cec5SDimitry Andric // the receiver is nullable. Here the receiver will be the source of the 2260b57cec5SDimitry Andric // nullability. This is useful information when the diagnostics are generated. 2270b57cec5SDimitry Andric const Stmt *Source; 2280b57cec5SDimitry Andric }; 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric bool operator==(NullabilityState Lhs, NullabilityState Rhs) { 2310b57cec5SDimitry Andric return Lhs.getValue() == Rhs.getValue() && 2320b57cec5SDimitry Andric Lhs.getNullabilitySource() == Rhs.getNullabilitySource(); 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 235*bdd1243dSDimitry Andric // For the purpose of tracking historical property accesses, the key for lookup 236*bdd1243dSDimitry Andric // is an object pointer (could be an instance or a class) paired with the unique 237*bdd1243dSDimitry Andric // identifier for the property being invoked on that object. 238*bdd1243dSDimitry Andric using ObjectPropPair = std::pair<const MemRegion *, const IdentifierInfo *>; 239*bdd1243dSDimitry Andric 240*bdd1243dSDimitry Andric // Metadata associated with the return value from a recorded property access. 241*bdd1243dSDimitry Andric struct ConstrainedPropertyVal { 242*bdd1243dSDimitry Andric // This will reference the conjured return SVal for some call 243*bdd1243dSDimitry Andric // of the form [object property] 244*bdd1243dSDimitry Andric DefinedOrUnknownSVal Value; 245*bdd1243dSDimitry Andric 246*bdd1243dSDimitry Andric // If the SVal has been determined to be nonnull, that is recorded here 247*bdd1243dSDimitry Andric bool isConstrainedNonnull; 248*bdd1243dSDimitry Andric 249*bdd1243dSDimitry Andric ConstrainedPropertyVal(DefinedOrUnknownSVal SV) 250*bdd1243dSDimitry Andric : Value(SV), isConstrainedNonnull(false) {} 251*bdd1243dSDimitry Andric 252*bdd1243dSDimitry Andric void Profile(llvm::FoldingSetNodeID &ID) const { 253*bdd1243dSDimitry Andric Value.Profile(ID); 254*bdd1243dSDimitry Andric ID.AddInteger(isConstrainedNonnull ? 1 : 0); 255*bdd1243dSDimitry Andric } 256*bdd1243dSDimitry Andric }; 257*bdd1243dSDimitry Andric 258*bdd1243dSDimitry Andric bool operator==(const ConstrainedPropertyVal &Lhs, 259*bdd1243dSDimitry Andric const ConstrainedPropertyVal &Rhs) { 260*bdd1243dSDimitry Andric return Lhs.Value == Rhs.Value && 261*bdd1243dSDimitry Andric Lhs.isConstrainedNonnull == Rhs.isConstrainedNonnull; 262*bdd1243dSDimitry Andric } 263*bdd1243dSDimitry Andric 2640b57cec5SDimitry Andric } // end anonymous namespace 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric REGISTER_MAP_WITH_PROGRAMSTATE(NullabilityMap, const MemRegion *, 2670b57cec5SDimitry Andric NullabilityState) 268*bdd1243dSDimitry Andric REGISTER_MAP_WITH_PROGRAMSTATE(PropertyAccessesMap, ObjectPropPair, 269*bdd1243dSDimitry Andric ConstrainedPropertyVal) 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric // We say "the nullability type invariant is violated" when a location with a 2720b57cec5SDimitry Andric // non-null type contains NULL or a function with a non-null return type returns 2730b57cec5SDimitry Andric // NULL. Violations of the nullability type invariant can be detected either 2740b57cec5SDimitry Andric // directly (for example, when NULL is passed as an argument to a nonnull 2750b57cec5SDimitry Andric // parameter) or indirectly (for example, when, inside a function, the 2760b57cec5SDimitry Andric // programmer defensively checks whether a nonnull parameter contains NULL and 2770b57cec5SDimitry Andric // finds that it does). 2780b57cec5SDimitry Andric // 2790b57cec5SDimitry Andric // As a matter of policy, the nullability checker typically warns on direct 2800b57cec5SDimitry Andric // violations of the nullability invariant (although it uses various 2810b57cec5SDimitry Andric // heuristics to suppress warnings in some cases) but will not warn if the 2820b57cec5SDimitry Andric // invariant has already been violated along the path (either directly or 2830b57cec5SDimitry Andric // indirectly). As a practical matter, this prevents the analyzer from 2840b57cec5SDimitry Andric // (1) warning on defensive code paths where a nullability precondition is 2850b57cec5SDimitry Andric // determined to have been violated, (2) warning additional times after an 2860b57cec5SDimitry Andric // initial direct violation has been discovered, and (3) warning after a direct 2870b57cec5SDimitry Andric // violation that has been implicitly or explicitly suppressed (for 2880b57cec5SDimitry Andric // example, with a cast of NULL to _Nonnull). In essence, once an invariant 2890b57cec5SDimitry Andric // violation is detected on a path, this checker will be essentially turned off 2900b57cec5SDimitry Andric // for the rest of the analysis 2910b57cec5SDimitry Andric // 2920b57cec5SDimitry Andric // The analyzer takes this approach (rather than generating a sink node) to 2930b57cec5SDimitry Andric // ensure coverage of defensive paths, which may be important for backwards 2940b57cec5SDimitry Andric // compatibility in codebases that were developed without nullability in mind. 2950b57cec5SDimitry Andric REGISTER_TRAIT_WITH_PROGRAMSTATE(InvariantViolated, bool) 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric enum class NullConstraint { IsNull, IsNotNull, Unknown }; 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric static NullConstraint getNullConstraint(DefinedOrUnknownSVal Val, 3000b57cec5SDimitry Andric ProgramStateRef State) { 3010b57cec5SDimitry Andric ConditionTruthVal Nullness = State->isNull(Val); 3020b57cec5SDimitry Andric if (Nullness.isConstrainedFalse()) 3030b57cec5SDimitry Andric return NullConstraint::IsNotNull; 3040b57cec5SDimitry Andric if (Nullness.isConstrainedTrue()) 3050b57cec5SDimitry Andric return NullConstraint::IsNull; 3060b57cec5SDimitry Andric return NullConstraint::Unknown; 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric const SymbolicRegion * 3100b57cec5SDimitry Andric NullabilityChecker::getTrackRegion(SVal Val, bool CheckSuperRegion) const { 3110b57cec5SDimitry Andric if (!NeedTracking) 3120b57cec5SDimitry Andric return nullptr; 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric auto RegionSVal = Val.getAs<loc::MemRegionVal>(); 3150b57cec5SDimitry Andric if (!RegionSVal) 3160b57cec5SDimitry Andric return nullptr; 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric const MemRegion *Region = RegionSVal->getRegion(); 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric if (CheckSuperRegion) { 321*bdd1243dSDimitry Andric if (const SubRegion *FieldReg = Region->getAs<FieldRegion>()) { 322*bdd1243dSDimitry Andric if (const auto *ER = dyn_cast<ElementRegion>(FieldReg->getSuperRegion())) 323*bdd1243dSDimitry Andric FieldReg = ER; 3240b57cec5SDimitry Andric return dyn_cast<SymbolicRegion>(FieldReg->getSuperRegion()); 325*bdd1243dSDimitry Andric } 3260b57cec5SDimitry Andric if (auto ElementReg = Region->getAs<ElementRegion>()) 3270b57cec5SDimitry Andric return dyn_cast<SymbolicRegion>(ElementReg->getSuperRegion()); 3280b57cec5SDimitry Andric } 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric return dyn_cast<SymbolicRegion>(Region); 3310b57cec5SDimitry Andric } 3320b57cec5SDimitry Andric 333a7dea167SDimitry Andric PathDiagnosticPieceRef NullabilityChecker::NullabilityBugVisitor::VisitNode( 334a7dea167SDimitry Andric const ExplodedNode *N, BugReporterContext &BRC, 335a7dea167SDimitry Andric PathSensitiveBugReport &BR) { 3360b57cec5SDimitry Andric ProgramStateRef State = N->getState(); 3370b57cec5SDimitry Andric ProgramStateRef StatePrev = N->getFirstPred()->getState(); 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric const NullabilityState *TrackedNullab = State->get<NullabilityMap>(Region); 3400b57cec5SDimitry Andric const NullabilityState *TrackedNullabPrev = 3410b57cec5SDimitry Andric StatePrev->get<NullabilityMap>(Region); 3420b57cec5SDimitry Andric if (!TrackedNullab) 3430b57cec5SDimitry Andric return nullptr; 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric if (TrackedNullabPrev && 3460b57cec5SDimitry Andric TrackedNullabPrev->getValue() == TrackedNullab->getValue()) 3470b57cec5SDimitry Andric return nullptr; 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric // Retrieve the associated statement. 3500b57cec5SDimitry Andric const Stmt *S = TrackedNullab->getNullabilitySource(); 3510b57cec5SDimitry Andric if (!S || S->getBeginLoc().isInvalid()) { 352a7dea167SDimitry Andric S = N->getStmtForDiagnostics(); 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric if (!S) 3560b57cec5SDimitry Andric return nullptr; 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric std::string InfoText = 3590b57cec5SDimitry Andric (llvm::Twine("Nullability '") + 3600b57cec5SDimitry Andric getNullabilityString(TrackedNullab->getValue()) + "' is inferred") 3610b57cec5SDimitry Andric .str(); 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric // Generate the extra diagnostic. 3640b57cec5SDimitry Andric PathDiagnosticLocation Pos(S, BRC.getSourceManager(), 3650b57cec5SDimitry Andric N->getLocationContext()); 366a7dea167SDimitry Andric return std::make_shared<PathDiagnosticEventPiece>(Pos, InfoText, true); 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric /// Returns true when the value stored at the given location has been 3700b57cec5SDimitry Andric /// constrained to null after being passed through an object of nonnnull type. 3710b57cec5SDimitry Andric static bool checkValueAtLValForInvariantViolation(ProgramStateRef State, 3720b57cec5SDimitry Andric SVal LV, QualType T) { 3730b57cec5SDimitry Andric if (getNullabilityAnnotation(T) != Nullability::Nonnull) 3740b57cec5SDimitry Andric return false; 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric auto RegionVal = LV.getAs<loc::MemRegionVal>(); 3770b57cec5SDimitry Andric if (!RegionVal) 3780b57cec5SDimitry Andric return false; 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric // If the value was constrained to null *after* it was passed through that 3810b57cec5SDimitry Andric // location, it could not have been a concrete pointer *when* it was passed. 3820b57cec5SDimitry Andric // In that case we would have handled the situation when the value was 3830b57cec5SDimitry Andric // bound to that location, by emitting (or not emitting) a report. 3840b57cec5SDimitry Andric // Therefore we are only interested in symbolic regions that can be either 3850b57cec5SDimitry Andric // null or non-null depending on the value of their respective symbol. 3860b57cec5SDimitry Andric auto StoredVal = State->getSVal(*RegionVal).getAs<loc::MemRegionVal>(); 3870b57cec5SDimitry Andric if (!StoredVal || !isa<SymbolicRegion>(StoredVal->getRegion())) 3880b57cec5SDimitry Andric return false; 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric if (getNullConstraint(*StoredVal, State) == NullConstraint::IsNull) 3910b57cec5SDimitry Andric return true; 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric return false; 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric static bool 3970b57cec5SDimitry Andric checkParamsForPreconditionViolation(ArrayRef<ParmVarDecl *> Params, 3980b57cec5SDimitry Andric ProgramStateRef State, 3990b57cec5SDimitry Andric const LocationContext *LocCtxt) { 4000b57cec5SDimitry Andric for (const auto *ParamDecl : Params) { 4010b57cec5SDimitry Andric if (ParamDecl->isParameterPack()) 4020b57cec5SDimitry Andric break; 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric SVal LV = State->getLValue(ParamDecl, LocCtxt); 4050b57cec5SDimitry Andric if (checkValueAtLValForInvariantViolation(State, LV, 4060b57cec5SDimitry Andric ParamDecl->getType())) { 4070b57cec5SDimitry Andric return true; 4080b57cec5SDimitry Andric } 4090b57cec5SDimitry Andric } 4100b57cec5SDimitry Andric return false; 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric static bool 4140b57cec5SDimitry Andric checkSelfIvarsForInvariantViolation(ProgramStateRef State, 4150b57cec5SDimitry Andric const LocationContext *LocCtxt) { 4160b57cec5SDimitry Andric auto *MD = dyn_cast<ObjCMethodDecl>(LocCtxt->getDecl()); 4170b57cec5SDimitry Andric if (!MD || !MD->isInstanceMethod()) 4180b57cec5SDimitry Andric return false; 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric const ImplicitParamDecl *SelfDecl = LocCtxt->getSelfDecl(); 4210b57cec5SDimitry Andric if (!SelfDecl) 4220b57cec5SDimitry Andric return false; 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric SVal SelfVal = State->getSVal(State->getRegion(SelfDecl, LocCtxt)); 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric const ObjCObjectPointerType *SelfType = 4270b57cec5SDimitry Andric dyn_cast<ObjCObjectPointerType>(SelfDecl->getType()); 4280b57cec5SDimitry Andric if (!SelfType) 4290b57cec5SDimitry Andric return false; 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric const ObjCInterfaceDecl *ID = SelfType->getInterfaceDecl(); 4320b57cec5SDimitry Andric if (!ID) 4330b57cec5SDimitry Andric return false; 4340b57cec5SDimitry Andric 4350b57cec5SDimitry Andric for (const auto *IvarDecl : ID->ivars()) { 4360b57cec5SDimitry Andric SVal LV = State->getLValue(IvarDecl, SelfVal); 4370b57cec5SDimitry Andric if (checkValueAtLValForInvariantViolation(State, LV, IvarDecl->getType())) { 4380b57cec5SDimitry Andric return true; 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric return false; 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric static bool checkInvariantViolation(ProgramStateRef State, ExplodedNode *N, 4450b57cec5SDimitry Andric CheckerContext &C) { 4460b57cec5SDimitry Andric if (State->get<InvariantViolated>()) 4470b57cec5SDimitry Andric return true; 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric const LocationContext *LocCtxt = C.getLocationContext(); 4500b57cec5SDimitry Andric const Decl *D = LocCtxt->getDecl(); 4510b57cec5SDimitry Andric if (!D) 4520b57cec5SDimitry Andric return false; 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric ArrayRef<ParmVarDecl*> Params; 4550b57cec5SDimitry Andric if (const auto *BD = dyn_cast<BlockDecl>(D)) 4560b57cec5SDimitry Andric Params = BD->parameters(); 4570b57cec5SDimitry Andric else if (const auto *FD = dyn_cast<FunctionDecl>(D)) 4580b57cec5SDimitry Andric Params = FD->parameters(); 4590b57cec5SDimitry Andric else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) 4600b57cec5SDimitry Andric Params = MD->parameters(); 4610b57cec5SDimitry Andric else 4620b57cec5SDimitry Andric return false; 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric if (checkParamsForPreconditionViolation(Params, State, LocCtxt) || 4650b57cec5SDimitry Andric checkSelfIvarsForInvariantViolation(State, LocCtxt)) { 4660b57cec5SDimitry Andric if (!N->isSink()) 4670b57cec5SDimitry Andric C.addTransition(State->set<InvariantViolated>(true), N); 4680b57cec5SDimitry Andric return true; 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric return false; 4710b57cec5SDimitry Andric } 4720b57cec5SDimitry Andric 4735ffd83dbSDimitry Andric void NullabilityChecker::reportBugIfInvariantHolds( 4745ffd83dbSDimitry Andric StringRef Msg, ErrorKind Error, CheckKind CK, ExplodedNode *N, 4755ffd83dbSDimitry Andric const MemRegion *Region, CheckerContext &C, const Stmt *ValueExpr, 4765ffd83dbSDimitry Andric bool SuppressPath) const { 4770b57cec5SDimitry Andric ProgramStateRef OriginalState = N->getState(); 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric if (checkInvariantViolation(OriginalState, N, C)) 4800b57cec5SDimitry Andric return; 4810b57cec5SDimitry Andric if (SuppressPath) { 4820b57cec5SDimitry Andric OriginalState = OriginalState->set<InvariantViolated>(true); 4830b57cec5SDimitry Andric N = C.addTransition(OriginalState, N); 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric 4865ffd83dbSDimitry Andric reportBug(Msg, Error, CK, N, Region, C.getBugReporter(), ValueExpr); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric /// Cleaning up the program state. 4900b57cec5SDimitry Andric void NullabilityChecker::checkDeadSymbols(SymbolReaper &SR, 4910b57cec5SDimitry Andric CheckerContext &C) const { 4920b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 4930b57cec5SDimitry Andric NullabilityMapTy Nullabilities = State->get<NullabilityMap>(); 4940b57cec5SDimitry Andric for (NullabilityMapTy::iterator I = Nullabilities.begin(), 4950b57cec5SDimitry Andric E = Nullabilities.end(); 4960b57cec5SDimitry Andric I != E; ++I) { 4970b57cec5SDimitry Andric const auto *Region = I->first->getAs<SymbolicRegion>(); 4980b57cec5SDimitry Andric assert(Region && "Non-symbolic region is tracked."); 4990b57cec5SDimitry Andric if (SR.isDead(Region->getSymbol())) { 5000b57cec5SDimitry Andric State = State->remove<NullabilityMap>(I->first); 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric } 503*bdd1243dSDimitry Andric 504*bdd1243dSDimitry Andric // When an object goes out of scope, we can free the history associated 505*bdd1243dSDimitry Andric // with any property accesses on that object 506*bdd1243dSDimitry Andric PropertyAccessesMapTy PropertyAccesses = State->get<PropertyAccessesMap>(); 507*bdd1243dSDimitry Andric for (PropertyAccessesMapTy::iterator I = PropertyAccesses.begin(), 508*bdd1243dSDimitry Andric E = PropertyAccesses.end(); 509*bdd1243dSDimitry Andric I != E; ++I) { 510*bdd1243dSDimitry Andric const MemRegion *ReceiverRegion = I->first.first; 511*bdd1243dSDimitry Andric if (!SR.isLiveRegion(ReceiverRegion)) { 512*bdd1243dSDimitry Andric State = State->remove<PropertyAccessesMap>(I->first); 513*bdd1243dSDimitry Andric } 514*bdd1243dSDimitry Andric } 515*bdd1243dSDimitry Andric 5160b57cec5SDimitry Andric // When one of the nonnull arguments are constrained to be null, nullability 5170b57cec5SDimitry Andric // preconditions are violated. It is not enough to check this only when we 5180b57cec5SDimitry Andric // actually report an error, because at that time interesting symbols might be 5190b57cec5SDimitry Andric // reaped. 5200b57cec5SDimitry Andric if (checkInvariantViolation(State, C.getPredecessor(), C)) 5210b57cec5SDimitry Andric return; 5220b57cec5SDimitry Andric C.addTransition(State); 5230b57cec5SDimitry Andric } 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric /// This callback triggers when a pointer is dereferenced and the analyzer does 5260b57cec5SDimitry Andric /// not know anything about the value of that pointer. When that pointer is 5270b57cec5SDimitry Andric /// nullable, this code emits a warning. 5280b57cec5SDimitry Andric void NullabilityChecker::checkEvent(ImplicitNullDerefEvent Event) const { 5290b57cec5SDimitry Andric if (Event.SinkNode->getState()->get<InvariantViolated>()) 5300b57cec5SDimitry Andric return; 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric const MemRegion *Region = 5330b57cec5SDimitry Andric getTrackRegion(Event.Location, /*CheckSuperRegion=*/true); 5340b57cec5SDimitry Andric if (!Region) 5350b57cec5SDimitry Andric return; 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric ProgramStateRef State = Event.SinkNode->getState(); 5380b57cec5SDimitry Andric const NullabilityState *TrackedNullability = 5390b57cec5SDimitry Andric State->get<NullabilityMap>(Region); 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric if (!TrackedNullability) 5420b57cec5SDimitry Andric return; 5430b57cec5SDimitry Andric 5445ffd83dbSDimitry Andric if (ChecksEnabled[CK_NullableDereferenced] && 5450b57cec5SDimitry Andric TrackedNullability->getValue() == Nullability::Nullable) { 5460b57cec5SDimitry Andric BugReporter &BR = *Event.BR; 5470b57cec5SDimitry Andric // Do not suppress errors on defensive code paths, because dereferencing 5480b57cec5SDimitry Andric // a nullable pointer is always an error. 5490b57cec5SDimitry Andric if (Event.IsDirectDereference) 5500b57cec5SDimitry Andric reportBug("Nullable pointer is dereferenced", 5515ffd83dbSDimitry Andric ErrorKind::NullableDereferenced, CK_NullableDereferenced, 5525ffd83dbSDimitry Andric Event.SinkNode, Region, BR); 5530b57cec5SDimitry Andric else { 5540b57cec5SDimitry Andric reportBug("Nullable pointer is passed to a callee that requires a " 5555ffd83dbSDimitry Andric "non-null", 5565ffd83dbSDimitry Andric ErrorKind::NullablePassedToNonnull, CK_NullableDereferenced, 5570b57cec5SDimitry Andric Event.SinkNode, Region, BR); 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric } 5600b57cec5SDimitry Andric } 5610b57cec5SDimitry Andric 5625ffd83dbSDimitry Andric // Whenever we see a load from a typed memory region that's been annotated as 5635ffd83dbSDimitry Andric // 'nonnull', we want to trust the user on that and assume that it is is indeed 5645ffd83dbSDimitry Andric // non-null. 5655ffd83dbSDimitry Andric // 5665ffd83dbSDimitry Andric // We do so even if the value is known to have been assigned to null. 5675ffd83dbSDimitry Andric // The user should be warned on assigning the null value to a non-null pointer 5685ffd83dbSDimitry Andric // as opposed to warning on the later dereference of this pointer. 5695ffd83dbSDimitry Andric // 5705ffd83dbSDimitry Andric // \code 5715ffd83dbSDimitry Andric // int * _Nonnull var = 0; // we want to warn the user here... 5725ffd83dbSDimitry Andric // // . . . 5735ffd83dbSDimitry Andric // *var = 42; // ...and not here 5745ffd83dbSDimitry Andric // \endcode 5755ffd83dbSDimitry Andric void NullabilityChecker::checkLocation(SVal Location, bool IsLoad, 5765ffd83dbSDimitry Andric const Stmt *S, 5775ffd83dbSDimitry Andric CheckerContext &Context) const { 5785ffd83dbSDimitry Andric // We should care only about loads. 5795ffd83dbSDimitry Andric // The main idea is to add a constraint whenever we're loading a value from 5805ffd83dbSDimitry Andric // an annotated pointer type. 5815ffd83dbSDimitry Andric if (!IsLoad) 5825ffd83dbSDimitry Andric return; 5835ffd83dbSDimitry Andric 5845ffd83dbSDimitry Andric // Annotations that we want to consider make sense only for types. 5855ffd83dbSDimitry Andric const auto *Region = 5865ffd83dbSDimitry Andric dyn_cast_or_null<TypedValueRegion>(Location.getAsRegion()); 5875ffd83dbSDimitry Andric if (!Region) 5885ffd83dbSDimitry Andric return; 5895ffd83dbSDimitry Andric 5905ffd83dbSDimitry Andric ProgramStateRef State = Context.getState(); 5915ffd83dbSDimitry Andric 5925ffd83dbSDimitry Andric auto StoredVal = State->getSVal(Region).getAs<loc::MemRegionVal>(); 5935ffd83dbSDimitry Andric if (!StoredVal) 5945ffd83dbSDimitry Andric return; 5955ffd83dbSDimitry Andric 5965ffd83dbSDimitry Andric Nullability NullabilityOfTheLoadedValue = 5975ffd83dbSDimitry Andric getNullabilityAnnotation(Region->getValueType()); 5985ffd83dbSDimitry Andric 5995ffd83dbSDimitry Andric if (NullabilityOfTheLoadedValue == Nullability::Nonnull) { 6005ffd83dbSDimitry Andric // It doesn't matter what we think about this particular pointer, it should 6015ffd83dbSDimitry Andric // be considered non-null as annotated by the developer. 6025ffd83dbSDimitry Andric if (ProgramStateRef NewState = State->assume(*StoredVal, true)) { 6035ffd83dbSDimitry Andric Context.addTransition(NewState); 6045ffd83dbSDimitry Andric } 6055ffd83dbSDimitry Andric } 6065ffd83dbSDimitry Andric } 6075ffd83dbSDimitry Andric 6080b57cec5SDimitry Andric /// Find the outermost subexpression of E that is not an implicit cast. 6090b57cec5SDimitry Andric /// This looks through the implicit casts to _Nonnull that ARC adds to 6100b57cec5SDimitry Andric /// return expressions of ObjC types when the return type of the function or 6110b57cec5SDimitry Andric /// method is non-null but the express is not. 6120b57cec5SDimitry Andric static const Expr *lookThroughImplicitCasts(const Expr *E) { 6135ffd83dbSDimitry Andric return E->IgnoreImpCasts(); 6140b57cec5SDimitry Andric } 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric /// This method check when nullable pointer or null value is returned from a 6170b57cec5SDimitry Andric /// function that has nonnull return type. 6180b57cec5SDimitry Andric void NullabilityChecker::checkPreStmt(const ReturnStmt *S, 6190b57cec5SDimitry Andric CheckerContext &C) const { 6200b57cec5SDimitry Andric auto RetExpr = S->getRetValue(); 6210b57cec5SDimitry Andric if (!RetExpr) 6220b57cec5SDimitry Andric return; 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric if (!RetExpr->getType()->isAnyPointerType()) 6250b57cec5SDimitry Andric return; 6260b57cec5SDimitry Andric 6270b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 6280b57cec5SDimitry Andric if (State->get<InvariantViolated>()) 6290b57cec5SDimitry Andric return; 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric auto RetSVal = C.getSVal(S).getAs<DefinedOrUnknownSVal>(); 6320b57cec5SDimitry Andric if (!RetSVal) 6330b57cec5SDimitry Andric return; 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric bool InSuppressedMethodFamily = false; 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric QualType RequiredRetType; 6380b57cec5SDimitry Andric AnalysisDeclContext *DeclCtxt = 6390b57cec5SDimitry Andric C.getLocationContext()->getAnalysisDeclContext(); 6400b57cec5SDimitry Andric const Decl *D = DeclCtxt->getDecl(); 6410b57cec5SDimitry Andric if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) { 6420b57cec5SDimitry Andric // HACK: This is a big hammer to avoid warning when there are defensive 6430b57cec5SDimitry Andric // nil checks in -init and -copy methods. We should add more sophisticated 6440b57cec5SDimitry Andric // logic here to suppress on common defensive idioms but still 6450b57cec5SDimitry Andric // warn when there is a likely problem. 6460b57cec5SDimitry Andric ObjCMethodFamily Family = MD->getMethodFamily(); 6470b57cec5SDimitry Andric if (OMF_init == Family || OMF_copy == Family || OMF_mutableCopy == Family) 6480b57cec5SDimitry Andric InSuppressedMethodFamily = true; 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric RequiredRetType = MD->getReturnType(); 6510b57cec5SDimitry Andric } else if (auto *FD = dyn_cast<FunctionDecl>(D)) { 6520b57cec5SDimitry Andric RequiredRetType = FD->getReturnType(); 6530b57cec5SDimitry Andric } else { 6540b57cec5SDimitry Andric return; 6550b57cec5SDimitry Andric } 6560b57cec5SDimitry Andric 6570b57cec5SDimitry Andric NullConstraint Nullness = getNullConstraint(*RetSVal, State); 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric Nullability RequiredNullability = getNullabilityAnnotation(RequiredRetType); 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric // If the returned value is null but the type of the expression 6620b57cec5SDimitry Andric // generating it is nonnull then we will suppress the diagnostic. 6630b57cec5SDimitry Andric // This enables explicit suppression when returning a nil literal in a 6640b57cec5SDimitry Andric // function with a _Nonnull return type: 6650b57cec5SDimitry Andric // return (NSString * _Nonnull)0; 6660b57cec5SDimitry Andric Nullability RetExprTypeLevelNullability = 6670b57cec5SDimitry Andric getNullabilityAnnotation(lookThroughImplicitCasts(RetExpr)->getType()); 6680b57cec5SDimitry Andric 6690b57cec5SDimitry Andric bool NullReturnedFromNonNull = (RequiredNullability == Nullability::Nonnull && 6700b57cec5SDimitry Andric Nullness == NullConstraint::IsNull); 6715ffd83dbSDimitry Andric if (ChecksEnabled[CK_NullReturnedFromNonnull] && NullReturnedFromNonNull && 6720b57cec5SDimitry Andric RetExprTypeLevelNullability != Nullability::Nonnull && 6735ffd83dbSDimitry Andric !InSuppressedMethodFamily && C.getLocationContext()->inTopFrame()) { 6740b57cec5SDimitry Andric static CheckerProgramPointTag Tag(this, "NullReturnedFromNonnull"); 6750b57cec5SDimitry Andric ExplodedNode *N = C.generateErrorNode(State, &Tag); 6760b57cec5SDimitry Andric if (!N) 6770b57cec5SDimitry Andric return; 6780b57cec5SDimitry Andric 6790b57cec5SDimitry Andric SmallString<256> SBuf; 6800b57cec5SDimitry Andric llvm::raw_svector_ostream OS(SBuf); 6810b57cec5SDimitry Andric OS << (RetExpr->getType()->isObjCObjectPointerType() ? "nil" : "Null"); 6820b57cec5SDimitry Andric OS << " returned from a " << C.getDeclDescription(D) << 6830b57cec5SDimitry Andric " that is expected to return a non-null value"; 6845ffd83dbSDimitry Andric reportBugIfInvariantHolds(OS.str(), ErrorKind::NilReturnedToNonnull, 6855ffd83dbSDimitry Andric CK_NullReturnedFromNonnull, N, nullptr, C, 6860b57cec5SDimitry Andric RetExpr); 6870b57cec5SDimitry Andric return; 6880b57cec5SDimitry Andric } 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric // If null was returned from a non-null function, mark the nullability 6910b57cec5SDimitry Andric // invariant as violated even if the diagnostic was suppressed. 6920b57cec5SDimitry Andric if (NullReturnedFromNonNull) { 6930b57cec5SDimitry Andric State = State->set<InvariantViolated>(true); 6940b57cec5SDimitry Andric C.addTransition(State); 6950b57cec5SDimitry Andric return; 6960b57cec5SDimitry Andric } 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric const MemRegion *Region = getTrackRegion(*RetSVal); 6990b57cec5SDimitry Andric if (!Region) 7000b57cec5SDimitry Andric return; 7010b57cec5SDimitry Andric 7020b57cec5SDimitry Andric const NullabilityState *TrackedNullability = 7030b57cec5SDimitry Andric State->get<NullabilityMap>(Region); 7040b57cec5SDimitry Andric if (TrackedNullability) { 7050b57cec5SDimitry Andric Nullability TrackedNullabValue = TrackedNullability->getValue(); 7065ffd83dbSDimitry Andric if (ChecksEnabled[CK_NullableReturnedFromNonnull] && 7070b57cec5SDimitry Andric Nullness != NullConstraint::IsNotNull && 7080b57cec5SDimitry Andric TrackedNullabValue == Nullability::Nullable && 7090b57cec5SDimitry Andric RequiredNullability == Nullability::Nonnull) { 7100b57cec5SDimitry Andric static CheckerProgramPointTag Tag(this, "NullableReturnedFromNonnull"); 7110b57cec5SDimitry Andric ExplodedNode *N = C.addTransition(State, C.getPredecessor(), &Tag); 7120b57cec5SDimitry Andric 7130b57cec5SDimitry Andric SmallString<256> SBuf; 7140b57cec5SDimitry Andric llvm::raw_svector_ostream OS(SBuf); 7150b57cec5SDimitry Andric OS << "Nullable pointer is returned from a " << C.getDeclDescription(D) << 7160b57cec5SDimitry Andric " that is expected to return a non-null value"; 7170b57cec5SDimitry Andric 7185ffd83dbSDimitry Andric reportBugIfInvariantHolds(OS.str(), ErrorKind::NullableReturnedToNonnull, 7195ffd83dbSDimitry Andric CK_NullableReturnedFromNonnull, N, Region, C); 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric return; 7220b57cec5SDimitry Andric } 7230b57cec5SDimitry Andric if (RequiredNullability == Nullability::Nullable) { 7240b57cec5SDimitry Andric State = State->set<NullabilityMap>(Region, 7250b57cec5SDimitry Andric NullabilityState(RequiredNullability, 7260b57cec5SDimitry Andric S)); 7270b57cec5SDimitry Andric C.addTransition(State); 7280b57cec5SDimitry Andric } 7290b57cec5SDimitry Andric } 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric /// This callback warns when a nullable pointer or a null value is passed to a 7320b57cec5SDimitry Andric /// function that expects its argument to be nonnull. 7330b57cec5SDimitry Andric void NullabilityChecker::checkPreCall(const CallEvent &Call, 7340b57cec5SDimitry Andric CheckerContext &C) const { 7350b57cec5SDimitry Andric if (!Call.getDecl()) 7360b57cec5SDimitry Andric return; 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 7390b57cec5SDimitry Andric if (State->get<InvariantViolated>()) 7400b57cec5SDimitry Andric return; 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric ProgramStateRef OrigState = State; 7430b57cec5SDimitry Andric 7440b57cec5SDimitry Andric unsigned Idx = 0; 7450b57cec5SDimitry Andric for (const ParmVarDecl *Param : Call.parameters()) { 7460b57cec5SDimitry Andric if (Param->isParameterPack()) 7470b57cec5SDimitry Andric break; 7480b57cec5SDimitry Andric 7490b57cec5SDimitry Andric if (Idx >= Call.getNumArgs()) 7500b57cec5SDimitry Andric break; 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric const Expr *ArgExpr = Call.getArgExpr(Idx); 7530b57cec5SDimitry Andric auto ArgSVal = Call.getArgSVal(Idx++).getAs<DefinedOrUnknownSVal>(); 7540b57cec5SDimitry Andric if (!ArgSVal) 7550b57cec5SDimitry Andric continue; 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric if (!Param->getType()->isAnyPointerType() && 7580b57cec5SDimitry Andric !Param->getType()->isReferenceType()) 7590b57cec5SDimitry Andric continue; 7600b57cec5SDimitry Andric 7610b57cec5SDimitry Andric NullConstraint Nullness = getNullConstraint(*ArgSVal, State); 7620b57cec5SDimitry Andric 7630b57cec5SDimitry Andric Nullability RequiredNullability = 7640b57cec5SDimitry Andric getNullabilityAnnotation(Param->getType()); 7650b57cec5SDimitry Andric Nullability ArgExprTypeLevelNullability = 7660b57cec5SDimitry Andric getNullabilityAnnotation(ArgExpr->getType()); 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric unsigned ParamIdx = Param->getFunctionScopeIndex() + 1; 7690b57cec5SDimitry Andric 7705ffd83dbSDimitry Andric if (ChecksEnabled[CK_NullPassedToNonnull] && 7715ffd83dbSDimitry Andric Nullness == NullConstraint::IsNull && 7720b57cec5SDimitry Andric ArgExprTypeLevelNullability != Nullability::Nonnull && 7730b57cec5SDimitry Andric RequiredNullability == Nullability::Nonnull && 7740b57cec5SDimitry Andric isDiagnosableCall(Call)) { 7750b57cec5SDimitry Andric ExplodedNode *N = C.generateErrorNode(State); 7760b57cec5SDimitry Andric if (!N) 7770b57cec5SDimitry Andric return; 7780b57cec5SDimitry Andric 7790b57cec5SDimitry Andric SmallString<256> SBuf; 7800b57cec5SDimitry Andric llvm::raw_svector_ostream OS(SBuf); 7810b57cec5SDimitry Andric OS << (Param->getType()->isObjCObjectPointerType() ? "nil" : "Null"); 7820b57cec5SDimitry Andric OS << " passed to a callee that requires a non-null " << ParamIdx 7830b57cec5SDimitry Andric << llvm::getOrdinalSuffix(ParamIdx) << " parameter"; 7845ffd83dbSDimitry Andric reportBugIfInvariantHolds(OS.str(), ErrorKind::NilPassedToNonnull, 7855ffd83dbSDimitry Andric CK_NullPassedToNonnull, N, nullptr, C, ArgExpr, 7865ffd83dbSDimitry Andric /*SuppressPath=*/false); 7870b57cec5SDimitry Andric return; 7880b57cec5SDimitry Andric } 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric const MemRegion *Region = getTrackRegion(*ArgSVal); 7910b57cec5SDimitry Andric if (!Region) 7920b57cec5SDimitry Andric continue; 7930b57cec5SDimitry Andric 7940b57cec5SDimitry Andric const NullabilityState *TrackedNullability = 7950b57cec5SDimitry Andric State->get<NullabilityMap>(Region); 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric if (TrackedNullability) { 7980b57cec5SDimitry Andric if (Nullness == NullConstraint::IsNotNull || 7990b57cec5SDimitry Andric TrackedNullability->getValue() != Nullability::Nullable) 8000b57cec5SDimitry Andric continue; 8010b57cec5SDimitry Andric 8025ffd83dbSDimitry Andric if (ChecksEnabled[CK_NullablePassedToNonnull] && 8030b57cec5SDimitry Andric RequiredNullability == Nullability::Nonnull && 8040b57cec5SDimitry Andric isDiagnosableCall(Call)) { 8050b57cec5SDimitry Andric ExplodedNode *N = C.addTransition(State); 8060b57cec5SDimitry Andric SmallString<256> SBuf; 8070b57cec5SDimitry Andric llvm::raw_svector_ostream OS(SBuf); 8080b57cec5SDimitry Andric OS << "Nullable pointer is passed to a callee that requires a non-null " 8090b57cec5SDimitry Andric << ParamIdx << llvm::getOrdinalSuffix(ParamIdx) << " parameter"; 8105ffd83dbSDimitry Andric reportBugIfInvariantHolds(OS.str(), ErrorKind::NullablePassedToNonnull, 8115ffd83dbSDimitry Andric CK_NullablePassedToNonnull, N, Region, C, 8125ffd83dbSDimitry Andric ArgExpr, /*SuppressPath=*/true); 8130b57cec5SDimitry Andric return; 8140b57cec5SDimitry Andric } 8155ffd83dbSDimitry Andric if (ChecksEnabled[CK_NullableDereferenced] && 8160b57cec5SDimitry Andric Param->getType()->isReferenceType()) { 8170b57cec5SDimitry Andric ExplodedNode *N = C.addTransition(State); 8180b57cec5SDimitry Andric reportBugIfInvariantHolds("Nullable pointer is dereferenced", 8195ffd83dbSDimitry Andric ErrorKind::NullableDereferenced, 8205ffd83dbSDimitry Andric CK_NullableDereferenced, N, Region, C, 8215ffd83dbSDimitry Andric ArgExpr, /*SuppressPath=*/true); 8220b57cec5SDimitry Andric return; 8230b57cec5SDimitry Andric } 8240b57cec5SDimitry Andric continue; 8250b57cec5SDimitry Andric } 8260b57cec5SDimitry Andric } 8270b57cec5SDimitry Andric if (State != OrigState) 8280b57cec5SDimitry Andric C.addTransition(State); 8290b57cec5SDimitry Andric } 8300b57cec5SDimitry Andric 8310b57cec5SDimitry Andric /// Suppress the nullability warnings for some functions. 8320b57cec5SDimitry Andric void NullabilityChecker::checkPostCall(const CallEvent &Call, 8330b57cec5SDimitry Andric CheckerContext &C) const { 8340b57cec5SDimitry Andric auto Decl = Call.getDecl(); 8350b57cec5SDimitry Andric if (!Decl) 8360b57cec5SDimitry Andric return; 8370b57cec5SDimitry Andric // ObjC Messages handles in a different callback. 8380b57cec5SDimitry Andric if (Call.getKind() == CE_ObjCMessage) 8390b57cec5SDimitry Andric return; 8400b57cec5SDimitry Andric const FunctionType *FuncType = Decl->getFunctionType(); 8410b57cec5SDimitry Andric if (!FuncType) 8420b57cec5SDimitry Andric return; 8430b57cec5SDimitry Andric QualType ReturnType = FuncType->getReturnType(); 8440b57cec5SDimitry Andric if (!ReturnType->isAnyPointerType()) 8450b57cec5SDimitry Andric return; 8460b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 8470b57cec5SDimitry Andric if (State->get<InvariantViolated>()) 8480b57cec5SDimitry Andric return; 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric const MemRegion *Region = getTrackRegion(Call.getReturnValue()); 8510b57cec5SDimitry Andric if (!Region) 8520b57cec5SDimitry Andric return; 8530b57cec5SDimitry Andric 8540b57cec5SDimitry Andric // CG headers are misannotated. Do not warn for symbols that are the results 8550b57cec5SDimitry Andric // of CG calls. 8560b57cec5SDimitry Andric const SourceManager &SM = C.getSourceManager(); 8570b57cec5SDimitry Andric StringRef FilePath = SM.getFilename(SM.getSpellingLoc(Decl->getBeginLoc())); 8580b57cec5SDimitry Andric if (llvm::sys::path::filename(FilePath).startswith("CG")) { 8590b57cec5SDimitry Andric State = State->set<NullabilityMap>(Region, Nullability::Contradicted); 8600b57cec5SDimitry Andric C.addTransition(State); 8610b57cec5SDimitry Andric return; 8620b57cec5SDimitry Andric } 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric const NullabilityState *TrackedNullability = 8650b57cec5SDimitry Andric State->get<NullabilityMap>(Region); 8660b57cec5SDimitry Andric 8670b57cec5SDimitry Andric if (!TrackedNullability && 8680b57cec5SDimitry Andric getNullabilityAnnotation(ReturnType) == Nullability::Nullable) { 8690b57cec5SDimitry Andric State = State->set<NullabilityMap>(Region, Nullability::Nullable); 8700b57cec5SDimitry Andric C.addTransition(State); 8710b57cec5SDimitry Andric } 8720b57cec5SDimitry Andric } 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andric static Nullability getReceiverNullability(const ObjCMethodCall &M, 8750b57cec5SDimitry Andric ProgramStateRef State) { 8760b57cec5SDimitry Andric if (M.isReceiverSelfOrSuper()) { 8770b57cec5SDimitry Andric // For super and super class receivers we assume that the receiver is 8780b57cec5SDimitry Andric // nonnull. 8790b57cec5SDimitry Andric return Nullability::Nonnull; 8800b57cec5SDimitry Andric } 8810b57cec5SDimitry Andric // Otherwise look up nullability in the state. 8820b57cec5SDimitry Andric SVal Receiver = M.getReceiverSVal(); 8830b57cec5SDimitry Andric if (auto DefOrUnknown = Receiver.getAs<DefinedOrUnknownSVal>()) { 8840b57cec5SDimitry Andric // If the receiver is constrained to be nonnull, assume that it is nonnull 8850b57cec5SDimitry Andric // regardless of its type. 8860b57cec5SDimitry Andric NullConstraint Nullness = getNullConstraint(*DefOrUnknown, State); 8870b57cec5SDimitry Andric if (Nullness == NullConstraint::IsNotNull) 8880b57cec5SDimitry Andric return Nullability::Nonnull; 8890b57cec5SDimitry Andric } 8900b57cec5SDimitry Andric auto ValueRegionSVal = Receiver.getAs<loc::MemRegionVal>(); 8910b57cec5SDimitry Andric if (ValueRegionSVal) { 8920b57cec5SDimitry Andric const MemRegion *SelfRegion = ValueRegionSVal->getRegion(); 8930b57cec5SDimitry Andric assert(SelfRegion); 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric const NullabilityState *TrackedSelfNullability = 8960b57cec5SDimitry Andric State->get<NullabilityMap>(SelfRegion); 8970b57cec5SDimitry Andric if (TrackedSelfNullability) 8980b57cec5SDimitry Andric return TrackedSelfNullability->getValue(); 8990b57cec5SDimitry Andric } 9000b57cec5SDimitry Andric return Nullability::Unspecified; 9010b57cec5SDimitry Andric } 9020b57cec5SDimitry Andric 903*bdd1243dSDimitry Andric // The return value of a property access is typically a temporary value which 904*bdd1243dSDimitry Andric // will not be tracked in a persistent manner by the analyzer. We use 905*bdd1243dSDimitry Andric // evalAssume() in order to immediately record constraints on those temporaries 906*bdd1243dSDimitry Andric // at the time they are imposed (e.g. by a nil-check conditional). 907*bdd1243dSDimitry Andric ProgramStateRef NullabilityChecker::evalAssume(ProgramStateRef State, SVal Cond, 908*bdd1243dSDimitry Andric bool Assumption) const { 909*bdd1243dSDimitry Andric PropertyAccessesMapTy PropertyAccesses = State->get<PropertyAccessesMap>(); 910*bdd1243dSDimitry Andric for (PropertyAccessesMapTy::iterator I = PropertyAccesses.begin(), 911*bdd1243dSDimitry Andric E = PropertyAccesses.end(); 912*bdd1243dSDimitry Andric I != E; ++I) { 913*bdd1243dSDimitry Andric if (!I->second.isConstrainedNonnull) { 914*bdd1243dSDimitry Andric ConditionTruthVal IsNonNull = State->isNonNull(I->second.Value); 915*bdd1243dSDimitry Andric if (IsNonNull.isConstrainedTrue()) { 916*bdd1243dSDimitry Andric ConstrainedPropertyVal Replacement = I->second; 917*bdd1243dSDimitry Andric Replacement.isConstrainedNonnull = true; 918*bdd1243dSDimitry Andric State = State->set<PropertyAccessesMap>(I->first, Replacement); 919*bdd1243dSDimitry Andric } else if (IsNonNull.isConstrainedFalse()) { 920*bdd1243dSDimitry Andric // Space optimization: no point in tracking constrained-null cases 921*bdd1243dSDimitry Andric State = State->remove<PropertyAccessesMap>(I->first); 922*bdd1243dSDimitry Andric } 923*bdd1243dSDimitry Andric } 924*bdd1243dSDimitry Andric } 925*bdd1243dSDimitry Andric 926*bdd1243dSDimitry Andric return State; 927*bdd1243dSDimitry Andric } 928*bdd1243dSDimitry Andric 9290b57cec5SDimitry Andric /// Calculate the nullability of the result of a message expr based on the 9300b57cec5SDimitry Andric /// nullability of the receiver, the nullability of the return value, and the 9310b57cec5SDimitry Andric /// constraints. 9320b57cec5SDimitry Andric void NullabilityChecker::checkPostObjCMessage(const ObjCMethodCall &M, 9330b57cec5SDimitry Andric CheckerContext &C) const { 9340b57cec5SDimitry Andric auto Decl = M.getDecl(); 9350b57cec5SDimitry Andric if (!Decl) 9360b57cec5SDimitry Andric return; 9370b57cec5SDimitry Andric QualType RetType = Decl->getReturnType(); 9380b57cec5SDimitry Andric if (!RetType->isAnyPointerType()) 9390b57cec5SDimitry Andric return; 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 9420b57cec5SDimitry Andric if (State->get<InvariantViolated>()) 9430b57cec5SDimitry Andric return; 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric const MemRegion *ReturnRegion = getTrackRegion(M.getReturnValue()); 9460b57cec5SDimitry Andric if (!ReturnRegion) 9470b57cec5SDimitry Andric return; 9480b57cec5SDimitry Andric 9490b57cec5SDimitry Andric auto Interface = Decl->getClassInterface(); 9500b57cec5SDimitry Andric auto Name = Interface ? Interface->getName() : ""; 9510b57cec5SDimitry Andric // In order to reduce the noise in the diagnostics generated by this checker, 9520b57cec5SDimitry Andric // some framework and programming style based heuristics are used. These 9530b57cec5SDimitry Andric // heuristics are for Cocoa APIs which have NS prefix. 9540b57cec5SDimitry Andric if (Name.startswith("NS")) { 9550b57cec5SDimitry Andric // Developers rely on dynamic invariants such as an item should be available 9560b57cec5SDimitry Andric // in a collection, or a collection is not empty often. Those invariants can 9570b57cec5SDimitry Andric // not be inferred by any static analysis tool. To not to bother the users 9580b57cec5SDimitry Andric // with too many false positives, every item retrieval function should be 9590b57cec5SDimitry Andric // ignored for collections. The instance methods of dictionaries in Cocoa 9600b57cec5SDimitry Andric // are either item retrieval related or not interesting nullability wise. 9610b57cec5SDimitry Andric // Using this fact, to keep the code easier to read just ignore the return 9620b57cec5SDimitry Andric // value of every instance method of dictionaries. 9630b57cec5SDimitry Andric if (M.isInstanceMessage() && Name.contains("Dictionary")) { 9640b57cec5SDimitry Andric State = 9650b57cec5SDimitry Andric State->set<NullabilityMap>(ReturnRegion, Nullability::Contradicted); 9660b57cec5SDimitry Andric C.addTransition(State); 9670b57cec5SDimitry Andric return; 9680b57cec5SDimitry Andric } 9690b57cec5SDimitry Andric // For similar reasons ignore some methods of Cocoa arrays. 9700b57cec5SDimitry Andric StringRef FirstSelectorSlot = M.getSelector().getNameForSlot(0); 9710b57cec5SDimitry Andric if (Name.contains("Array") && 9720b57cec5SDimitry Andric (FirstSelectorSlot == "firstObject" || 9730b57cec5SDimitry Andric FirstSelectorSlot == "lastObject")) { 9740b57cec5SDimitry Andric State = 9750b57cec5SDimitry Andric State->set<NullabilityMap>(ReturnRegion, Nullability::Contradicted); 9760b57cec5SDimitry Andric C.addTransition(State); 9770b57cec5SDimitry Andric return; 9780b57cec5SDimitry Andric } 9790b57cec5SDimitry Andric 9800b57cec5SDimitry Andric // Encoding related methods of string should not fail when lossless 9810b57cec5SDimitry Andric // encodings are used. Using lossless encodings is so frequent that ignoring 9820b57cec5SDimitry Andric // this class of methods reduced the emitted diagnostics by about 30% on 9830b57cec5SDimitry Andric // some projects (and all of that was false positives). 9840b57cec5SDimitry Andric if (Name.contains("String")) { 985*bdd1243dSDimitry Andric for (auto *Param : M.parameters()) { 9860b57cec5SDimitry Andric if (Param->getName() == "encoding") { 9870b57cec5SDimitry Andric State = State->set<NullabilityMap>(ReturnRegion, 9880b57cec5SDimitry Andric Nullability::Contradicted); 9890b57cec5SDimitry Andric C.addTransition(State); 9900b57cec5SDimitry Andric return; 9910b57cec5SDimitry Andric } 9920b57cec5SDimitry Andric } 9930b57cec5SDimitry Andric } 9940b57cec5SDimitry Andric } 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric const ObjCMessageExpr *Message = M.getOriginExpr(); 9970b57cec5SDimitry Andric Nullability SelfNullability = getReceiverNullability(M, State); 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric const NullabilityState *NullabilityOfReturn = 10000b57cec5SDimitry Andric State->get<NullabilityMap>(ReturnRegion); 10010b57cec5SDimitry Andric 10020b57cec5SDimitry Andric if (NullabilityOfReturn) { 10030b57cec5SDimitry Andric // When we have a nullability tracked for the return value, the nullability 10040b57cec5SDimitry Andric // of the expression will be the most nullable of the receiver and the 10050b57cec5SDimitry Andric // return value. 10060b57cec5SDimitry Andric Nullability RetValTracked = NullabilityOfReturn->getValue(); 10070b57cec5SDimitry Andric Nullability ComputedNullab = 10080b57cec5SDimitry Andric getMostNullable(RetValTracked, SelfNullability); 10090b57cec5SDimitry Andric if (ComputedNullab != RetValTracked && 10100b57cec5SDimitry Andric ComputedNullab != Nullability::Unspecified) { 10110b57cec5SDimitry Andric const Stmt *NullabilitySource = 10120b57cec5SDimitry Andric ComputedNullab == RetValTracked 10130b57cec5SDimitry Andric ? NullabilityOfReturn->getNullabilitySource() 10140b57cec5SDimitry Andric : Message->getInstanceReceiver(); 10150b57cec5SDimitry Andric State = State->set<NullabilityMap>( 10160b57cec5SDimitry Andric ReturnRegion, NullabilityState(ComputedNullab, NullabilitySource)); 10170b57cec5SDimitry Andric C.addTransition(State); 10180b57cec5SDimitry Andric } 10190b57cec5SDimitry Andric return; 10200b57cec5SDimitry Andric } 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric // No tracked information. Use static type information for return value. 10230b57cec5SDimitry Andric Nullability RetNullability = getNullabilityAnnotation(RetType); 10240b57cec5SDimitry Andric 1025*bdd1243dSDimitry Andric // Properties might be computed, which means the property value could 1026*bdd1243dSDimitry Andric // theoretically change between calls even in commonly-observed cases like 1027*bdd1243dSDimitry Andric // this: 1028*bdd1243dSDimitry Andric // 1029*bdd1243dSDimitry Andric // if (foo.prop) { // ok, it's nonnull here... 1030*bdd1243dSDimitry Andric // [bar doStuffWithNonnullVal:foo.prop]; // ...but what about 1031*bdd1243dSDimitry Andric // here? 1032*bdd1243dSDimitry Andric // } 1033*bdd1243dSDimitry Andric // 1034*bdd1243dSDimitry Andric // If the property is nullable-annotated, a naive analysis would lead to many 1035*bdd1243dSDimitry Andric // false positives despite the presence of probably-correct nil-checks. To 1036*bdd1243dSDimitry Andric // reduce the false positive rate, we maintain a history of the most recently 1037*bdd1243dSDimitry Andric // observed property value. For each property access, if the prior value has 1038*bdd1243dSDimitry Andric // been constrained to be not nil then we will conservatively assume that the 1039*bdd1243dSDimitry Andric // next access can be inferred as nonnull. 1040*bdd1243dSDimitry Andric if (RetNullability != Nullability::Nonnull && 1041*bdd1243dSDimitry Andric M.getMessageKind() == OCM_PropertyAccess && !C.wasInlined) { 1042*bdd1243dSDimitry Andric bool LookupResolved = false; 1043*bdd1243dSDimitry Andric if (const MemRegion *ReceiverRegion = getTrackRegion(M.getReceiverSVal())) { 1044*bdd1243dSDimitry Andric if (IdentifierInfo *Ident = M.getSelector().getIdentifierInfoForSlot(0)) { 1045*bdd1243dSDimitry Andric LookupResolved = true; 1046*bdd1243dSDimitry Andric ObjectPropPair Key = std::make_pair(ReceiverRegion, Ident); 1047*bdd1243dSDimitry Andric const ConstrainedPropertyVal *PrevPropVal = 1048*bdd1243dSDimitry Andric State->get<PropertyAccessesMap>(Key); 1049*bdd1243dSDimitry Andric if (PrevPropVal && PrevPropVal->isConstrainedNonnull) { 10500b57cec5SDimitry Andric RetNullability = Nullability::Nonnull; 1051*bdd1243dSDimitry Andric } else { 1052*bdd1243dSDimitry Andric // If a previous property access was constrained as nonnull, we hold 1053*bdd1243dSDimitry Andric // on to that constraint (effectively inferring that all subsequent 1054*bdd1243dSDimitry Andric // accesses on that code path can be inferred as nonnull). If the 1055*bdd1243dSDimitry Andric // previous property access was *not* constrained as nonnull, then 1056*bdd1243dSDimitry Andric // let's throw it away in favor of keeping the SVal associated with 1057*bdd1243dSDimitry Andric // this more recent access. 1058*bdd1243dSDimitry Andric if (auto ReturnSVal = 1059*bdd1243dSDimitry Andric M.getReturnValue().getAs<DefinedOrUnknownSVal>()) { 1060*bdd1243dSDimitry Andric State = State->set<PropertyAccessesMap>( 1061*bdd1243dSDimitry Andric Key, ConstrainedPropertyVal(*ReturnSVal)); 1062*bdd1243dSDimitry Andric } 1063*bdd1243dSDimitry Andric } 1064*bdd1243dSDimitry Andric } 1065*bdd1243dSDimitry Andric } 1066*bdd1243dSDimitry Andric 1067*bdd1243dSDimitry Andric if (!LookupResolved) { 1068*bdd1243dSDimitry Andric // Fallback: err on the side of suppressing the false positive. 1069*bdd1243dSDimitry Andric RetNullability = Nullability::Nonnull; 1070*bdd1243dSDimitry Andric } 1071*bdd1243dSDimitry Andric } 10720b57cec5SDimitry Andric 10730b57cec5SDimitry Andric Nullability ComputedNullab = getMostNullable(RetNullability, SelfNullability); 10740b57cec5SDimitry Andric if (ComputedNullab == Nullability::Nullable) { 10750b57cec5SDimitry Andric const Stmt *NullabilitySource = ComputedNullab == RetNullability 10760b57cec5SDimitry Andric ? Message 10770b57cec5SDimitry Andric : Message->getInstanceReceiver(); 10780b57cec5SDimitry Andric State = State->set<NullabilityMap>( 10790b57cec5SDimitry Andric ReturnRegion, NullabilityState(ComputedNullab, NullabilitySource)); 10800b57cec5SDimitry Andric C.addTransition(State); 10810b57cec5SDimitry Andric } 10820b57cec5SDimitry Andric } 10830b57cec5SDimitry Andric 10840b57cec5SDimitry Andric /// Explicit casts are trusted. If there is a disagreement in the nullability 10850b57cec5SDimitry Andric /// annotations in the destination and the source or '0' is casted to nonnull 10860b57cec5SDimitry Andric /// track the value as having contraditory nullability. This will allow users to 10870b57cec5SDimitry Andric /// suppress warnings. 10880b57cec5SDimitry Andric void NullabilityChecker::checkPostStmt(const ExplicitCastExpr *CE, 10890b57cec5SDimitry Andric CheckerContext &C) const { 10900b57cec5SDimitry Andric QualType OriginType = CE->getSubExpr()->getType(); 10910b57cec5SDimitry Andric QualType DestType = CE->getType(); 10920b57cec5SDimitry Andric if (!OriginType->isAnyPointerType()) 10930b57cec5SDimitry Andric return; 10940b57cec5SDimitry Andric if (!DestType->isAnyPointerType()) 10950b57cec5SDimitry Andric return; 10960b57cec5SDimitry Andric 10970b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 10980b57cec5SDimitry Andric if (State->get<InvariantViolated>()) 10990b57cec5SDimitry Andric return; 11000b57cec5SDimitry Andric 11010b57cec5SDimitry Andric Nullability DestNullability = getNullabilityAnnotation(DestType); 11020b57cec5SDimitry Andric 11030b57cec5SDimitry Andric // No explicit nullability in the destination type, so this cast does not 11040b57cec5SDimitry Andric // change the nullability. 11050b57cec5SDimitry Andric if (DestNullability == Nullability::Unspecified) 11060b57cec5SDimitry Andric return; 11070b57cec5SDimitry Andric 11080b57cec5SDimitry Andric auto RegionSVal = C.getSVal(CE).getAs<DefinedOrUnknownSVal>(); 11090b57cec5SDimitry Andric const MemRegion *Region = getTrackRegion(*RegionSVal); 11100b57cec5SDimitry Andric if (!Region) 11110b57cec5SDimitry Andric return; 11120b57cec5SDimitry Andric 11130b57cec5SDimitry Andric // When 0 is converted to nonnull mark it as contradicted. 11140b57cec5SDimitry Andric if (DestNullability == Nullability::Nonnull) { 11150b57cec5SDimitry Andric NullConstraint Nullness = getNullConstraint(*RegionSVal, State); 11160b57cec5SDimitry Andric if (Nullness == NullConstraint::IsNull) { 11170b57cec5SDimitry Andric State = State->set<NullabilityMap>(Region, Nullability::Contradicted); 11180b57cec5SDimitry Andric C.addTransition(State); 11190b57cec5SDimitry Andric return; 11200b57cec5SDimitry Andric } 11210b57cec5SDimitry Andric } 11220b57cec5SDimitry Andric 11230b57cec5SDimitry Andric const NullabilityState *TrackedNullability = 11240b57cec5SDimitry Andric State->get<NullabilityMap>(Region); 11250b57cec5SDimitry Andric 11260b57cec5SDimitry Andric if (!TrackedNullability) { 11270b57cec5SDimitry Andric if (DestNullability != Nullability::Nullable) 11280b57cec5SDimitry Andric return; 11290b57cec5SDimitry Andric State = State->set<NullabilityMap>(Region, 11300b57cec5SDimitry Andric NullabilityState(DestNullability, CE)); 11310b57cec5SDimitry Andric C.addTransition(State); 11320b57cec5SDimitry Andric return; 11330b57cec5SDimitry Andric } 11340b57cec5SDimitry Andric 11350b57cec5SDimitry Andric if (TrackedNullability->getValue() != DestNullability && 11360b57cec5SDimitry Andric TrackedNullability->getValue() != Nullability::Contradicted) { 11370b57cec5SDimitry Andric State = State->set<NullabilityMap>(Region, Nullability::Contradicted); 11380b57cec5SDimitry Andric C.addTransition(State); 11390b57cec5SDimitry Andric } 11400b57cec5SDimitry Andric } 11410b57cec5SDimitry Andric 11420b57cec5SDimitry Andric /// For a given statement performing a bind, attempt to syntactically 11430b57cec5SDimitry Andric /// match the expression resulting in the bound value. 11440b57cec5SDimitry Andric static const Expr * matchValueExprForBind(const Stmt *S) { 11450b57cec5SDimitry Andric // For `x = e` the value expression is the right-hand side. 11460b57cec5SDimitry Andric if (auto *BinOp = dyn_cast<BinaryOperator>(S)) { 11470b57cec5SDimitry Andric if (BinOp->getOpcode() == BO_Assign) 11480b57cec5SDimitry Andric return BinOp->getRHS(); 11490b57cec5SDimitry Andric } 11500b57cec5SDimitry Andric 11510b57cec5SDimitry Andric // For `int x = e` the value expression is the initializer. 11520b57cec5SDimitry Andric if (auto *DS = dyn_cast<DeclStmt>(S)) { 11530b57cec5SDimitry Andric if (DS->isSingleDecl()) { 11540b57cec5SDimitry Andric auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); 11550b57cec5SDimitry Andric if (!VD) 11560b57cec5SDimitry Andric return nullptr; 11570b57cec5SDimitry Andric 11580b57cec5SDimitry Andric if (const Expr *Init = VD->getInit()) 11590b57cec5SDimitry Andric return Init; 11600b57cec5SDimitry Andric } 11610b57cec5SDimitry Andric } 11620b57cec5SDimitry Andric 11630b57cec5SDimitry Andric return nullptr; 11640b57cec5SDimitry Andric } 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andric /// Returns true if \param S is a DeclStmt for a local variable that 11670b57cec5SDimitry Andric /// ObjC automated reference counting initialized with zero. 11680b57cec5SDimitry Andric static bool isARCNilInitializedLocal(CheckerContext &C, const Stmt *S) { 11690b57cec5SDimitry Andric // We suppress diagnostics for ARC zero-initialized _Nonnull locals. This 11700b57cec5SDimitry Andric // prevents false positives when a _Nonnull local variable cannot be 11710b57cec5SDimitry Andric // initialized with an initialization expression: 11720b57cec5SDimitry Andric // NSString * _Nonnull s; // no-warning 11730b57cec5SDimitry Andric // @autoreleasepool { 11740b57cec5SDimitry Andric // s = ... 11750b57cec5SDimitry Andric // } 11760b57cec5SDimitry Andric // 11770b57cec5SDimitry Andric // FIXME: We should treat implicitly zero-initialized _Nonnull locals as 11780b57cec5SDimitry Andric // uninitialized in Sema's UninitializedValues analysis to warn when a use of 11790b57cec5SDimitry Andric // the zero-initialized definition will unexpectedly yield nil. 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric // Locals are only zero-initialized when automated reference counting 11820b57cec5SDimitry Andric // is turned on. 11830b57cec5SDimitry Andric if (!C.getASTContext().getLangOpts().ObjCAutoRefCount) 11840b57cec5SDimitry Andric return false; 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric auto *DS = dyn_cast<DeclStmt>(S); 11870b57cec5SDimitry Andric if (!DS || !DS->isSingleDecl()) 11880b57cec5SDimitry Andric return false; 11890b57cec5SDimitry Andric 11900b57cec5SDimitry Andric auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); 11910b57cec5SDimitry Andric if (!VD) 11920b57cec5SDimitry Andric return false; 11930b57cec5SDimitry Andric 11940b57cec5SDimitry Andric // Sema only zero-initializes locals with ObjCLifetimes. 11950b57cec5SDimitry Andric if(!VD->getType().getQualifiers().hasObjCLifetime()) 11960b57cec5SDimitry Andric return false; 11970b57cec5SDimitry Andric 11980b57cec5SDimitry Andric const Expr *Init = VD->getInit(); 11990b57cec5SDimitry Andric assert(Init && "ObjC local under ARC without initializer"); 12000b57cec5SDimitry Andric 12010b57cec5SDimitry Andric // Return false if the local is explicitly initialized (e.g., with '= nil'). 12020b57cec5SDimitry Andric if (!isa<ImplicitValueInitExpr>(Init)) 12030b57cec5SDimitry Andric return false; 12040b57cec5SDimitry Andric 12050b57cec5SDimitry Andric return true; 12060b57cec5SDimitry Andric } 12070b57cec5SDimitry Andric 12080b57cec5SDimitry Andric /// Propagate the nullability information through binds and warn when nullable 12090b57cec5SDimitry Andric /// pointer or null symbol is assigned to a pointer with a nonnull type. 12100b57cec5SDimitry Andric void NullabilityChecker::checkBind(SVal L, SVal V, const Stmt *S, 12110b57cec5SDimitry Andric CheckerContext &C) const { 12120b57cec5SDimitry Andric const TypedValueRegion *TVR = 12130b57cec5SDimitry Andric dyn_cast_or_null<TypedValueRegion>(L.getAsRegion()); 12140b57cec5SDimitry Andric if (!TVR) 12150b57cec5SDimitry Andric return; 12160b57cec5SDimitry Andric 12170b57cec5SDimitry Andric QualType LocType = TVR->getValueType(); 12180b57cec5SDimitry Andric if (!LocType->isAnyPointerType()) 12190b57cec5SDimitry Andric return; 12200b57cec5SDimitry Andric 12210b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 12220b57cec5SDimitry Andric if (State->get<InvariantViolated>()) 12230b57cec5SDimitry Andric return; 12240b57cec5SDimitry Andric 12250b57cec5SDimitry Andric auto ValDefOrUnknown = V.getAs<DefinedOrUnknownSVal>(); 12260b57cec5SDimitry Andric if (!ValDefOrUnknown) 12270b57cec5SDimitry Andric return; 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric NullConstraint RhsNullness = getNullConstraint(*ValDefOrUnknown, State); 12300b57cec5SDimitry Andric 12310b57cec5SDimitry Andric Nullability ValNullability = Nullability::Unspecified; 12320b57cec5SDimitry Andric if (SymbolRef Sym = ValDefOrUnknown->getAsSymbol()) 12330b57cec5SDimitry Andric ValNullability = getNullabilityAnnotation(Sym->getType()); 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andric Nullability LocNullability = getNullabilityAnnotation(LocType); 12360b57cec5SDimitry Andric 12370b57cec5SDimitry Andric // If the type of the RHS expression is nonnull, don't warn. This 12380b57cec5SDimitry Andric // enables explicit suppression with a cast to nonnull. 12390b57cec5SDimitry Andric Nullability ValueExprTypeLevelNullability = Nullability::Unspecified; 12400b57cec5SDimitry Andric const Expr *ValueExpr = matchValueExprForBind(S); 12410b57cec5SDimitry Andric if (ValueExpr) { 12420b57cec5SDimitry Andric ValueExprTypeLevelNullability = 12430b57cec5SDimitry Andric getNullabilityAnnotation(lookThroughImplicitCasts(ValueExpr)->getType()); 12440b57cec5SDimitry Andric } 12450b57cec5SDimitry Andric 12460b57cec5SDimitry Andric bool NullAssignedToNonNull = (LocNullability == Nullability::Nonnull && 12470b57cec5SDimitry Andric RhsNullness == NullConstraint::IsNull); 12485ffd83dbSDimitry Andric if (ChecksEnabled[CK_NullPassedToNonnull] && NullAssignedToNonNull && 12490b57cec5SDimitry Andric ValNullability != Nullability::Nonnull && 12500b57cec5SDimitry Andric ValueExprTypeLevelNullability != Nullability::Nonnull && 12510b57cec5SDimitry Andric !isARCNilInitializedLocal(C, S)) { 12520b57cec5SDimitry Andric static CheckerProgramPointTag Tag(this, "NullPassedToNonnull"); 12530b57cec5SDimitry Andric ExplodedNode *N = C.generateErrorNode(State, &Tag); 12540b57cec5SDimitry Andric if (!N) 12550b57cec5SDimitry Andric return; 12560b57cec5SDimitry Andric 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andric const Stmt *ValueStmt = S; 12590b57cec5SDimitry Andric if (ValueExpr) 12600b57cec5SDimitry Andric ValueStmt = ValueExpr; 12610b57cec5SDimitry Andric 12620b57cec5SDimitry Andric SmallString<256> SBuf; 12630b57cec5SDimitry Andric llvm::raw_svector_ostream OS(SBuf); 12640b57cec5SDimitry Andric OS << (LocType->isObjCObjectPointerType() ? "nil" : "Null"); 12650b57cec5SDimitry Andric OS << " assigned to a pointer which is expected to have non-null value"; 12665ffd83dbSDimitry Andric reportBugIfInvariantHolds(OS.str(), ErrorKind::NilAssignedToNonnull, 12675ffd83dbSDimitry Andric CK_NullPassedToNonnull, N, nullptr, C, ValueStmt); 12680b57cec5SDimitry Andric return; 12690b57cec5SDimitry Andric } 12700b57cec5SDimitry Andric 12710b57cec5SDimitry Andric // If null was returned from a non-null function, mark the nullability 12720b57cec5SDimitry Andric // invariant as violated even if the diagnostic was suppressed. 12730b57cec5SDimitry Andric if (NullAssignedToNonNull) { 12740b57cec5SDimitry Andric State = State->set<InvariantViolated>(true); 12750b57cec5SDimitry Andric C.addTransition(State); 12760b57cec5SDimitry Andric return; 12770b57cec5SDimitry Andric } 12780b57cec5SDimitry Andric 12790b57cec5SDimitry Andric // Intentionally missing case: '0' is bound to a reference. It is handled by 12800b57cec5SDimitry Andric // the DereferenceChecker. 12810b57cec5SDimitry Andric 12820b57cec5SDimitry Andric const MemRegion *ValueRegion = getTrackRegion(*ValDefOrUnknown); 12830b57cec5SDimitry Andric if (!ValueRegion) 12840b57cec5SDimitry Andric return; 12850b57cec5SDimitry Andric 12860b57cec5SDimitry Andric const NullabilityState *TrackedNullability = 12870b57cec5SDimitry Andric State->get<NullabilityMap>(ValueRegion); 12880b57cec5SDimitry Andric 12890b57cec5SDimitry Andric if (TrackedNullability) { 12900b57cec5SDimitry Andric if (RhsNullness == NullConstraint::IsNotNull || 12910b57cec5SDimitry Andric TrackedNullability->getValue() != Nullability::Nullable) 12920b57cec5SDimitry Andric return; 12935ffd83dbSDimitry Andric if (ChecksEnabled[CK_NullablePassedToNonnull] && 12940b57cec5SDimitry Andric LocNullability == Nullability::Nonnull) { 12950b57cec5SDimitry Andric static CheckerProgramPointTag Tag(this, "NullablePassedToNonnull"); 12960b57cec5SDimitry Andric ExplodedNode *N = C.addTransition(State, C.getPredecessor(), &Tag); 12970b57cec5SDimitry Andric reportBugIfInvariantHolds("Nullable pointer is assigned to a pointer " 12980b57cec5SDimitry Andric "which is expected to have non-null value", 12995ffd83dbSDimitry Andric ErrorKind::NullableAssignedToNonnull, 13005ffd83dbSDimitry Andric CK_NullablePassedToNonnull, N, ValueRegion, C); 13010b57cec5SDimitry Andric } 13020b57cec5SDimitry Andric return; 13030b57cec5SDimitry Andric } 13040b57cec5SDimitry Andric 13050b57cec5SDimitry Andric const auto *BinOp = dyn_cast<BinaryOperator>(S); 13060b57cec5SDimitry Andric 13070b57cec5SDimitry Andric if (ValNullability == Nullability::Nullable) { 13080b57cec5SDimitry Andric // Trust the static information of the value more than the static 13090b57cec5SDimitry Andric // information on the location. 13100b57cec5SDimitry Andric const Stmt *NullabilitySource = BinOp ? BinOp->getRHS() : S; 13110b57cec5SDimitry Andric State = State->set<NullabilityMap>( 13120b57cec5SDimitry Andric ValueRegion, NullabilityState(ValNullability, NullabilitySource)); 13130b57cec5SDimitry Andric C.addTransition(State); 13140b57cec5SDimitry Andric return; 13150b57cec5SDimitry Andric } 13160b57cec5SDimitry Andric 13170b57cec5SDimitry Andric if (LocNullability == Nullability::Nullable) { 13180b57cec5SDimitry Andric const Stmt *NullabilitySource = BinOp ? BinOp->getLHS() : S; 13190b57cec5SDimitry Andric State = State->set<NullabilityMap>( 13200b57cec5SDimitry Andric ValueRegion, NullabilityState(LocNullability, NullabilitySource)); 13210b57cec5SDimitry Andric C.addTransition(State); 13220b57cec5SDimitry Andric } 13230b57cec5SDimitry Andric } 13240b57cec5SDimitry Andric 13250b57cec5SDimitry Andric void NullabilityChecker::printState(raw_ostream &Out, ProgramStateRef State, 13260b57cec5SDimitry Andric const char *NL, const char *Sep) const { 13270b57cec5SDimitry Andric 13280b57cec5SDimitry Andric NullabilityMapTy B = State->get<NullabilityMap>(); 13290b57cec5SDimitry Andric 13300b57cec5SDimitry Andric if (State->get<InvariantViolated>()) 13310b57cec5SDimitry Andric Out << Sep << NL 13320b57cec5SDimitry Andric << "Nullability invariant was violated, warnings suppressed." << NL; 13330b57cec5SDimitry Andric 13340b57cec5SDimitry Andric if (B.isEmpty()) 13350b57cec5SDimitry Andric return; 13360b57cec5SDimitry Andric 13370b57cec5SDimitry Andric if (!State->get<InvariantViolated>()) 13380b57cec5SDimitry Andric Out << Sep << NL; 13390b57cec5SDimitry Andric 13400b57cec5SDimitry Andric for (NullabilityMapTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { 13410b57cec5SDimitry Andric Out << I->first << " : "; 13420b57cec5SDimitry Andric I->second.print(Out); 13430b57cec5SDimitry Andric Out << NL; 13440b57cec5SDimitry Andric } 13450b57cec5SDimitry Andric } 13460b57cec5SDimitry Andric 13470b57cec5SDimitry Andric void ento::registerNullabilityBase(CheckerManager &mgr) { 13480b57cec5SDimitry Andric mgr.registerChecker<NullabilityChecker>(); 13490b57cec5SDimitry Andric } 13500b57cec5SDimitry Andric 13515ffd83dbSDimitry Andric bool ento::shouldRegisterNullabilityBase(const CheckerManager &mgr) { 13520b57cec5SDimitry Andric return true; 13530b57cec5SDimitry Andric } 13540b57cec5SDimitry Andric 13550b57cec5SDimitry Andric #define REGISTER_CHECKER(name, trackingRequired) \ 13560b57cec5SDimitry Andric void ento::register##name##Checker(CheckerManager &mgr) { \ 13570b57cec5SDimitry Andric NullabilityChecker *checker = mgr.getChecker<NullabilityChecker>(); \ 13585ffd83dbSDimitry Andric checker->ChecksEnabled[NullabilityChecker::CK_##name] = true; \ 13595ffd83dbSDimitry Andric checker->CheckNames[NullabilityChecker::CK_##name] = \ 13605ffd83dbSDimitry Andric mgr.getCurrentCheckerName(); \ 13610b57cec5SDimitry Andric checker->NeedTracking = checker->NeedTracking || trackingRequired; \ 13620b57cec5SDimitry Andric checker->NoDiagnoseCallsToSystemHeaders = \ 13630b57cec5SDimitry Andric checker->NoDiagnoseCallsToSystemHeaders || \ 13640b57cec5SDimitry Andric mgr.getAnalyzerOptions().getCheckerBooleanOption( \ 13650b57cec5SDimitry Andric checker, "NoDiagnoseCallsToSystemHeaders", true); \ 13660b57cec5SDimitry Andric } \ 13670b57cec5SDimitry Andric \ 13685ffd83dbSDimitry Andric bool ento::shouldRegister##name##Checker(const CheckerManager &mgr) { \ 13690b57cec5SDimitry Andric return true; \ 13700b57cec5SDimitry Andric } 13710b57cec5SDimitry Andric 13720b57cec5SDimitry Andric // The checks are likely to be turned on by default and it is possible to do 13730b57cec5SDimitry Andric // them without tracking any nullability related information. As an optimization 13740b57cec5SDimitry Andric // no nullability information will be tracked when only these two checks are 13750b57cec5SDimitry Andric // enables. 13760b57cec5SDimitry Andric REGISTER_CHECKER(NullPassedToNonnull, false) 13770b57cec5SDimitry Andric REGISTER_CHECKER(NullReturnedFromNonnull, false) 13780b57cec5SDimitry Andric 13790b57cec5SDimitry Andric REGISTER_CHECKER(NullableDereferenced, true) 13800b57cec5SDimitry Andric REGISTER_CHECKER(NullablePassedToNonnull, true) 13810b57cec5SDimitry Andric REGISTER_CHECKER(NullableReturnedFromNonnull, true) 1382