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 C.getLocationContext()->inTopFrame()) { 567 static CheckerProgramPointTag Tag(this, "NullReturnedFromNonnull"); 568 ExplodedNode *N = C.generateErrorNode(State, &Tag); 569 if (!N) 570 return; 571 572 SmallString<256> SBuf; 573 llvm::raw_svector_ostream OS(SBuf); 574 OS << "Null is returned from a " << C.getDeclDescription(D) << 575 " that is expected to return a non-null value"; 576 577 reportBugIfInvariantHolds(OS.str(), 578 ErrorKind::NilReturnedToNonnull, N, nullptr, C, 579 RetExpr); 580 return; 581 } 582 583 // If null was returned from a non-null function, mark the nullability 584 // invariant as violated even if the diagnostic was suppressed. 585 if (NullReturnedFromNonNull) { 586 State = State->set<InvariantViolated>(true); 587 C.addTransition(State); 588 return; 589 } 590 591 const MemRegion *Region = getTrackRegion(*RetSVal); 592 if (!Region) 593 return; 594 595 const NullabilityState *TrackedNullability = 596 State->get<NullabilityMap>(Region); 597 if (TrackedNullability) { 598 Nullability TrackedNullabValue = TrackedNullability->getValue(); 599 if (Filter.CheckNullableReturnedFromNonnull && 600 Nullness != NullConstraint::IsNotNull && 601 TrackedNullabValue == Nullability::Nullable && 602 RequiredNullability == Nullability::Nonnull) { 603 static CheckerProgramPointTag Tag(this, "NullableReturnedFromNonnull"); 604 ExplodedNode *N = C.addTransition(State, C.getPredecessor(), &Tag); 605 606 SmallString<256> SBuf; 607 llvm::raw_svector_ostream OS(SBuf); 608 OS << "Nullable pointer is returned from a " << C.getDeclDescription(D) << 609 " that is expected to return a non-null value"; 610 611 reportBugIfInvariantHolds(OS.str(), 612 ErrorKind::NullableReturnedToNonnull, N, 613 Region, C); 614 } 615 return; 616 } 617 if (RequiredNullability == Nullability::Nullable) { 618 State = State->set<NullabilityMap>(Region, 619 NullabilityState(RequiredNullability, 620 S)); 621 C.addTransition(State); 622 } 623 } 624 625 /// This callback warns when a nullable pointer or a null value is passed to a 626 /// function that expects its argument to be nonnull. 627 void NullabilityChecker::checkPreCall(const CallEvent &Call, 628 CheckerContext &C) const { 629 if (!Call.getDecl()) 630 return; 631 632 ProgramStateRef State = C.getState(); 633 if (State->get<InvariantViolated>()) 634 return; 635 636 ProgramStateRef OrigState = State; 637 638 unsigned Idx = 0; 639 for (const ParmVarDecl *Param : Call.parameters()) { 640 if (Param->isParameterPack()) 641 break; 642 643 const Expr *ArgExpr = nullptr; 644 if (Idx < Call.getNumArgs()) 645 ArgExpr = Call.getArgExpr(Idx); 646 auto ArgSVal = Call.getArgSVal(Idx++).getAs<DefinedOrUnknownSVal>(); 647 if (!ArgSVal) 648 continue; 649 650 if (!Param->getType()->isAnyPointerType() && 651 !Param->getType()->isReferenceType()) 652 continue; 653 654 NullConstraint Nullness = getNullConstraint(*ArgSVal, State); 655 656 Nullability RequiredNullability = 657 getNullabilityAnnotation(Param->getType()); 658 Nullability ArgExprTypeLevelNullability = 659 getNullabilityAnnotation(ArgExpr->getType()); 660 661 unsigned ParamIdx = Param->getFunctionScopeIndex() + 1; 662 663 if (Filter.CheckNullPassedToNonnull && Nullness == NullConstraint::IsNull && 664 ArgExprTypeLevelNullability != Nullability::Nonnull && 665 RequiredNullability == Nullability::Nonnull && 666 isDiagnosableCall(Call)) { 667 ExplodedNode *N = C.generateErrorNode(State); 668 if (!N) 669 return; 670 SmallString<256> SBuf; 671 llvm::raw_svector_ostream OS(SBuf); 672 OS << "Null passed to a callee that requires a non-null " << ParamIdx 673 << llvm::getOrdinalSuffix(ParamIdx) << " parameter"; 674 reportBugIfInvariantHolds(OS.str(), ErrorKind::NilPassedToNonnull, N, 675 nullptr, C, 676 ArgExpr, /*SuppressPath=*/false); 677 return; 678 } 679 680 const MemRegion *Region = getTrackRegion(*ArgSVal); 681 if (!Region) 682 continue; 683 684 const NullabilityState *TrackedNullability = 685 State->get<NullabilityMap>(Region); 686 687 if (TrackedNullability) { 688 if (Nullness == NullConstraint::IsNotNull || 689 TrackedNullability->getValue() != Nullability::Nullable) 690 continue; 691 692 if (Filter.CheckNullablePassedToNonnull && 693 RequiredNullability == Nullability::Nonnull && 694 isDiagnosableCall(Call)) { 695 ExplodedNode *N = C.addTransition(State); 696 SmallString<256> SBuf; 697 llvm::raw_svector_ostream OS(SBuf); 698 OS << "Nullable pointer is passed to a callee that requires a non-null " 699 << ParamIdx << llvm::getOrdinalSuffix(ParamIdx) << " parameter"; 700 reportBugIfInvariantHolds(OS.str(), 701 ErrorKind::NullablePassedToNonnull, N, 702 Region, C, ArgExpr, /*SuppressPath=*/true); 703 return; 704 } 705 if (Filter.CheckNullableDereferenced && 706 Param->getType()->isReferenceType()) { 707 ExplodedNode *N = C.addTransition(State); 708 reportBugIfInvariantHolds("Nullable pointer is dereferenced", 709 ErrorKind::NullableDereferenced, N, Region, 710 C, ArgExpr, /*SuppressPath=*/true); 711 return; 712 } 713 continue; 714 } 715 // No tracked nullability yet. 716 if (ArgExprTypeLevelNullability != Nullability::Nullable) 717 continue; 718 State = State->set<NullabilityMap>( 719 Region, NullabilityState(ArgExprTypeLevelNullability, ArgExpr)); 720 } 721 if (State != OrigState) 722 C.addTransition(State); 723 } 724 725 /// Suppress the nullability warnings for some functions. 726 void NullabilityChecker::checkPostCall(const CallEvent &Call, 727 CheckerContext &C) const { 728 auto Decl = Call.getDecl(); 729 if (!Decl) 730 return; 731 // ObjC Messages handles in a different callback. 732 if (Call.getKind() == CE_ObjCMessage) 733 return; 734 const FunctionType *FuncType = Decl->getFunctionType(); 735 if (!FuncType) 736 return; 737 QualType ReturnType = FuncType->getReturnType(); 738 if (!ReturnType->isAnyPointerType()) 739 return; 740 ProgramStateRef State = C.getState(); 741 if (State->get<InvariantViolated>()) 742 return; 743 744 const MemRegion *Region = getTrackRegion(Call.getReturnValue()); 745 if (!Region) 746 return; 747 748 // CG headers are misannotated. Do not warn for symbols that are the results 749 // of CG calls. 750 const SourceManager &SM = C.getSourceManager(); 751 StringRef FilePath = SM.getFilename(SM.getSpellingLoc(Decl->getLocStart())); 752 if (llvm::sys::path::filename(FilePath).startswith("CG")) { 753 State = State->set<NullabilityMap>(Region, Nullability::Contradicted); 754 C.addTransition(State); 755 return; 756 } 757 758 const NullabilityState *TrackedNullability = 759 State->get<NullabilityMap>(Region); 760 761 if (!TrackedNullability && 762 getNullabilityAnnotation(ReturnType) == Nullability::Nullable) { 763 State = State->set<NullabilityMap>(Region, Nullability::Nullable); 764 C.addTransition(State); 765 } 766 } 767 768 static Nullability getReceiverNullability(const ObjCMethodCall &M, 769 ProgramStateRef State) { 770 if (M.isReceiverSelfOrSuper()) { 771 // For super and super class receivers we assume that the receiver is 772 // nonnull. 773 return Nullability::Nonnull; 774 } 775 // Otherwise look up nullability in the state. 776 SVal Receiver = M.getReceiverSVal(); 777 if (auto DefOrUnknown = Receiver.getAs<DefinedOrUnknownSVal>()) { 778 // If the receiver is constrained to be nonnull, assume that it is nonnull 779 // regardless of its type. 780 NullConstraint Nullness = getNullConstraint(*DefOrUnknown, State); 781 if (Nullness == NullConstraint::IsNotNull) 782 return Nullability::Nonnull; 783 } 784 auto ValueRegionSVal = Receiver.getAs<loc::MemRegionVal>(); 785 if (ValueRegionSVal) { 786 const MemRegion *SelfRegion = ValueRegionSVal->getRegion(); 787 assert(SelfRegion); 788 789 const NullabilityState *TrackedSelfNullability = 790 State->get<NullabilityMap>(SelfRegion); 791 if (TrackedSelfNullability) 792 return TrackedSelfNullability->getValue(); 793 } 794 return Nullability::Unspecified; 795 } 796 797 /// Calculate the nullability of the result of a message expr based on the 798 /// nullability of the receiver, the nullability of the return value, and the 799 /// constraints. 800 void NullabilityChecker::checkPostObjCMessage(const ObjCMethodCall &M, 801 CheckerContext &C) const { 802 auto Decl = M.getDecl(); 803 if (!Decl) 804 return; 805 QualType RetType = Decl->getReturnType(); 806 if (!RetType->isAnyPointerType()) 807 return; 808 809 ProgramStateRef State = C.getState(); 810 if (State->get<InvariantViolated>()) 811 return; 812 813 const MemRegion *ReturnRegion = getTrackRegion(M.getReturnValue()); 814 if (!ReturnRegion) 815 return; 816 817 auto Interface = Decl->getClassInterface(); 818 auto Name = Interface ? Interface->getName() : ""; 819 // In order to reduce the noise in the diagnostics generated by this checker, 820 // some framework and programming style based heuristics are used. These 821 // heuristics are for Cocoa APIs which have NS prefix. 822 if (Name.startswith("NS")) { 823 // Developers rely on dynamic invariants such as an item should be available 824 // in a collection, or a collection is not empty often. Those invariants can 825 // not be inferred by any static analysis tool. To not to bother the users 826 // with too many false positives, every item retrieval function should be 827 // ignored for collections. The instance methods of dictionaries in Cocoa 828 // are either item retrieval related or not interesting nullability wise. 829 // Using this fact, to keep the code easier to read just ignore the return 830 // value of every instance method of dictionaries. 831 if (M.isInstanceMessage() && Name.find("Dictionary") != StringRef::npos) { 832 State = 833 State->set<NullabilityMap>(ReturnRegion, Nullability::Contradicted); 834 C.addTransition(State); 835 return; 836 } 837 // For similar reasons ignore some methods of Cocoa arrays. 838 StringRef FirstSelectorSlot = M.getSelector().getNameForSlot(0); 839 if (Name.find("Array") != StringRef::npos && 840 (FirstSelectorSlot == "firstObject" || 841 FirstSelectorSlot == "lastObject")) { 842 State = 843 State->set<NullabilityMap>(ReturnRegion, Nullability::Contradicted); 844 C.addTransition(State); 845 return; 846 } 847 848 // Encoding related methods of string should not fail when lossless 849 // encodings are used. Using lossless encodings is so frequent that ignoring 850 // this class of methods reduced the emitted diagnostics by about 30% on 851 // some projects (and all of that was false positives). 852 if (Name.find("String") != StringRef::npos) { 853 for (auto Param : M.parameters()) { 854 if (Param->getName() == "encoding") { 855 State = State->set<NullabilityMap>(ReturnRegion, 856 Nullability::Contradicted); 857 C.addTransition(State); 858 return; 859 } 860 } 861 } 862 } 863 864 const ObjCMessageExpr *Message = M.getOriginExpr(); 865 Nullability SelfNullability = getReceiverNullability(M, State); 866 867 const NullabilityState *NullabilityOfReturn = 868 State->get<NullabilityMap>(ReturnRegion); 869 870 if (NullabilityOfReturn) { 871 // When we have a nullability tracked for the return value, the nullability 872 // of the expression will be the most nullable of the receiver and the 873 // return value. 874 Nullability RetValTracked = NullabilityOfReturn->getValue(); 875 Nullability ComputedNullab = 876 getMostNullable(RetValTracked, SelfNullability); 877 if (ComputedNullab != RetValTracked && 878 ComputedNullab != Nullability::Unspecified) { 879 const Stmt *NullabilitySource = 880 ComputedNullab == RetValTracked 881 ? NullabilityOfReturn->getNullabilitySource() 882 : Message->getInstanceReceiver(); 883 State = State->set<NullabilityMap>( 884 ReturnRegion, NullabilityState(ComputedNullab, NullabilitySource)); 885 C.addTransition(State); 886 } 887 return; 888 } 889 890 // No tracked information. Use static type information for return value. 891 Nullability RetNullability = getNullabilityAnnotation(RetType); 892 893 // Properties might be computed. For this reason the static analyzer creates a 894 // new symbol each time an unknown property is read. To avoid false pozitives 895 // do not treat unknown properties as nullable, even when they explicitly 896 // marked nullable. 897 if (M.getMessageKind() == OCM_PropertyAccess && !C.wasInlined) 898 RetNullability = Nullability::Nonnull; 899 900 Nullability ComputedNullab = getMostNullable(RetNullability, SelfNullability); 901 if (ComputedNullab == Nullability::Nullable) { 902 const Stmt *NullabilitySource = ComputedNullab == RetNullability 903 ? Message 904 : Message->getInstanceReceiver(); 905 State = State->set<NullabilityMap>( 906 ReturnRegion, NullabilityState(ComputedNullab, NullabilitySource)); 907 C.addTransition(State); 908 } 909 } 910 911 /// Explicit casts are trusted. If there is a disagreement in the nullability 912 /// annotations in the destination and the source or '0' is casted to nonnull 913 /// track the value as having contraditory nullability. This will allow users to 914 /// suppress warnings. 915 void NullabilityChecker::checkPostStmt(const ExplicitCastExpr *CE, 916 CheckerContext &C) const { 917 QualType OriginType = CE->getSubExpr()->getType(); 918 QualType DestType = CE->getType(); 919 if (!OriginType->isAnyPointerType()) 920 return; 921 if (!DestType->isAnyPointerType()) 922 return; 923 924 ProgramStateRef State = C.getState(); 925 if (State->get<InvariantViolated>()) 926 return; 927 928 Nullability DestNullability = getNullabilityAnnotation(DestType); 929 930 // No explicit nullability in the destination type, so this cast does not 931 // change the nullability. 932 if (DestNullability == Nullability::Unspecified) 933 return; 934 935 auto RegionSVal = 936 State->getSVal(CE, C.getLocationContext()).getAs<DefinedOrUnknownSVal>(); 937 const MemRegion *Region = getTrackRegion(*RegionSVal); 938 if (!Region) 939 return; 940 941 // When 0 is converted to nonnull mark it as contradicted. 942 if (DestNullability == Nullability::Nonnull) { 943 NullConstraint Nullness = getNullConstraint(*RegionSVal, State); 944 if (Nullness == NullConstraint::IsNull) { 945 State = State->set<NullabilityMap>(Region, Nullability::Contradicted); 946 C.addTransition(State); 947 return; 948 } 949 } 950 951 const NullabilityState *TrackedNullability = 952 State->get<NullabilityMap>(Region); 953 954 if (!TrackedNullability) { 955 if (DestNullability != Nullability::Nullable) 956 return; 957 State = State->set<NullabilityMap>(Region, 958 NullabilityState(DestNullability, CE)); 959 C.addTransition(State); 960 return; 961 } 962 963 if (TrackedNullability->getValue() != DestNullability && 964 TrackedNullability->getValue() != Nullability::Contradicted) { 965 State = State->set<NullabilityMap>(Region, Nullability::Contradicted); 966 C.addTransition(State); 967 } 968 } 969 970 /// For a given statement performing a bind, attempt to syntactically 971 /// match the expression resulting in the bound value. 972 static const Expr * matchValueExprForBind(const Stmt *S) { 973 // For `x = e` the value expression is the right-hand side. 974 if (auto *BinOp = dyn_cast<BinaryOperator>(S)) { 975 if (BinOp->getOpcode() == BO_Assign) 976 return BinOp->getRHS(); 977 } 978 979 // For `int x = e` the value expression is the initializer. 980 if (auto *DS = dyn_cast<DeclStmt>(S)) { 981 if (DS->isSingleDecl()) { 982 auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); 983 if (!VD) 984 return nullptr; 985 986 if (const Expr *Init = VD->getInit()) 987 return Init; 988 } 989 } 990 991 return nullptr; 992 } 993 994 /// Returns true if \param S is a DeclStmt for a local variable that 995 /// ObjC automated reference counting initialized with zero. 996 static bool isARCNilInitializedLocal(CheckerContext &C, const Stmt *S) { 997 // We suppress diagnostics for ARC zero-initialized _Nonnull locals. This 998 // prevents false positives when a _Nonnull local variable cannot be 999 // initialized with an initialization expression: 1000 // NSString * _Nonnull s; // no-warning 1001 // @autoreleasepool { 1002 // s = ... 1003 // } 1004 // 1005 // FIXME: We should treat implicitly zero-initialized _Nonnull locals as 1006 // uninitialized in Sema's UninitializedValues analysis to warn when a use of 1007 // the zero-initialized definition will unexpectedly yield nil. 1008 1009 // Locals are only zero-initialized when automated reference counting 1010 // is turned on. 1011 if (!C.getASTContext().getLangOpts().ObjCAutoRefCount) 1012 return false; 1013 1014 auto *DS = dyn_cast<DeclStmt>(S); 1015 if (!DS || !DS->isSingleDecl()) 1016 return false; 1017 1018 auto *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); 1019 if (!VD) 1020 return false; 1021 1022 // Sema only zero-initializes locals with ObjCLifetimes. 1023 if(!VD->getType().getQualifiers().hasObjCLifetime()) 1024 return false; 1025 1026 const Expr *Init = VD->getInit(); 1027 assert(Init && "ObjC local under ARC without initializer"); 1028 1029 // Return false if the local is explicitly initialized (e.g., with '= nil'). 1030 if (!isa<ImplicitValueInitExpr>(Init)) 1031 return false; 1032 1033 return true; 1034 } 1035 1036 /// Propagate the nullability information through binds and warn when nullable 1037 /// pointer or null symbol is assigned to a pointer with a nonnull type. 1038 void NullabilityChecker::checkBind(SVal L, SVal V, const Stmt *S, 1039 CheckerContext &C) const { 1040 const TypedValueRegion *TVR = 1041 dyn_cast_or_null<TypedValueRegion>(L.getAsRegion()); 1042 if (!TVR) 1043 return; 1044 1045 QualType LocType = TVR->getValueType(); 1046 if (!LocType->isAnyPointerType()) 1047 return; 1048 1049 ProgramStateRef State = C.getState(); 1050 if (State->get<InvariantViolated>()) 1051 return; 1052 1053 auto ValDefOrUnknown = V.getAs<DefinedOrUnknownSVal>(); 1054 if (!ValDefOrUnknown) 1055 return; 1056 1057 NullConstraint RhsNullness = getNullConstraint(*ValDefOrUnknown, State); 1058 1059 Nullability ValNullability = Nullability::Unspecified; 1060 if (SymbolRef Sym = ValDefOrUnknown->getAsSymbol()) 1061 ValNullability = getNullabilityAnnotation(Sym->getType()); 1062 1063 Nullability LocNullability = getNullabilityAnnotation(LocType); 1064 if (Filter.CheckNullPassedToNonnull && 1065 RhsNullness == NullConstraint::IsNull && 1066 ValNullability != Nullability::Nonnull && 1067 LocNullability == Nullability::Nonnull && 1068 !isARCNilInitializedLocal(C, S)) { 1069 static CheckerProgramPointTag Tag(this, "NullPassedToNonnull"); 1070 ExplodedNode *N = C.generateErrorNode(State, &Tag); 1071 if (!N) 1072 return; 1073 1074 const Stmt *ValueExpr = matchValueExprForBind(S); 1075 if (!ValueExpr) 1076 ValueExpr = S; 1077 1078 reportBugIfInvariantHolds("Null is assigned to a pointer which is " 1079 "expected to have non-null value", 1080 ErrorKind::NilAssignedToNonnull, N, nullptr, C, 1081 ValueExpr); 1082 return; 1083 } 1084 // Intentionally missing case: '0' is bound to a reference. It is handled by 1085 // the DereferenceChecker. 1086 1087 const MemRegion *ValueRegion = getTrackRegion(*ValDefOrUnknown); 1088 if (!ValueRegion) 1089 return; 1090 1091 const NullabilityState *TrackedNullability = 1092 State->get<NullabilityMap>(ValueRegion); 1093 1094 if (TrackedNullability) { 1095 if (RhsNullness == NullConstraint::IsNotNull || 1096 TrackedNullability->getValue() != Nullability::Nullable) 1097 return; 1098 if (Filter.CheckNullablePassedToNonnull && 1099 LocNullability == Nullability::Nonnull) { 1100 static CheckerProgramPointTag Tag(this, "NullablePassedToNonnull"); 1101 ExplodedNode *N = C.addTransition(State, C.getPredecessor(), &Tag); 1102 reportBugIfInvariantHolds("Nullable pointer is assigned to a pointer " 1103 "which is expected to have non-null value", 1104 ErrorKind::NullableAssignedToNonnull, N, 1105 ValueRegion, C); 1106 } 1107 return; 1108 } 1109 1110 const auto *BinOp = dyn_cast<BinaryOperator>(S); 1111 1112 if (ValNullability == Nullability::Nullable) { 1113 // Trust the static information of the value more than the static 1114 // information on the location. 1115 const Stmt *NullabilitySource = BinOp ? BinOp->getRHS() : S; 1116 State = State->set<NullabilityMap>( 1117 ValueRegion, NullabilityState(ValNullability, NullabilitySource)); 1118 C.addTransition(State); 1119 return; 1120 } 1121 1122 if (LocNullability == Nullability::Nullable) { 1123 const Stmt *NullabilitySource = BinOp ? BinOp->getLHS() : S; 1124 State = State->set<NullabilityMap>( 1125 ValueRegion, NullabilityState(LocNullability, NullabilitySource)); 1126 C.addTransition(State); 1127 } 1128 } 1129 1130 void NullabilityChecker::printState(raw_ostream &Out, ProgramStateRef State, 1131 const char *NL, const char *Sep) const { 1132 1133 NullabilityMapTy B = State->get<NullabilityMap>(); 1134 1135 if (B.isEmpty()) 1136 return; 1137 1138 Out << Sep << NL; 1139 1140 for (NullabilityMapTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { 1141 Out << I->first << " : "; 1142 I->second.print(Out); 1143 Out << NL; 1144 } 1145 } 1146 1147 #define REGISTER_CHECKER(name, trackingRequired) \ 1148 void ento::register##name##Checker(CheckerManager &mgr) { \ 1149 NullabilityChecker *checker = mgr.registerChecker<NullabilityChecker>(); \ 1150 checker->Filter.Check##name = true; \ 1151 checker->Filter.CheckName##name = mgr.getCurrentCheckName(); \ 1152 checker->NeedTracking = checker->NeedTracking || trackingRequired; \ 1153 checker->NoDiagnoseCallsToSystemHeaders = \ 1154 checker->NoDiagnoseCallsToSystemHeaders || \ 1155 mgr.getAnalyzerOptions().getBooleanOption( \ 1156 "NoDiagnoseCallsToSystemHeaders", false, checker, true); \ 1157 } 1158 1159 // The checks are likely to be turned on by default and it is possible to do 1160 // them without tracking any nullability related information. As an optimization 1161 // no nullability information will be tracked when only these two checks are 1162 // enables. 1163 REGISTER_CHECKER(NullPassedToNonnull, false) 1164 REGISTER_CHECKER(NullReturnedFromNonnull, false) 1165 1166 REGISTER_CHECKER(NullableDereferenced, true) 1167 REGISTER_CHECKER(NullablePassedToNonnull, true) 1168 REGISTER_CHECKER(NullableReturnedFromNonnull, true) 1169