NonnullGlobalConstantsChecker.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //==- NonnullGlobalConstantsChecker.cpp ---------------------------*- C++ -*--//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This checker adds an assumption that constant globals of certain types* are
  10. // non-null, as otherwise they generally do not convey any useful information.
  11. // The assumption is useful, as many framework use e. g. global const strings,
  12. // and the analyzer might not be able to infer the global value if the
  13. // definition is in a separate translation unit.
  14. // The following types (and their typedef aliases) are considered to be
  15. // non-null:
  16. // - `char* const`
  17. // - `const CFStringRef` from CoreFoundation
  18. // - `NSString* const` from Foundation
  19. // - `CFBooleanRef` from Foundation
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  23. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  24. #include "clang/StaticAnalyzer/Core/Checker.h"
  25. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  26. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  27. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  28. #include <optional>
  29. using namespace clang;
  30. using namespace ento;
  31. namespace {
  32. class NonnullGlobalConstantsChecker : public Checker<check::Location> {
  33. mutable IdentifierInfo *NSStringII = nullptr;
  34. mutable IdentifierInfo *CFStringRefII = nullptr;
  35. mutable IdentifierInfo *CFBooleanRefII = nullptr;
  36. mutable IdentifierInfo *CFNullRefII = nullptr;
  37. public:
  38. NonnullGlobalConstantsChecker() {}
  39. void checkLocation(SVal l, bool isLoad, const Stmt *S,
  40. CheckerContext &C) const;
  41. private:
  42. void initIdentifierInfo(ASTContext &Ctx) const;
  43. bool isGlobalConstString(SVal V) const;
  44. bool isNonnullType(QualType Ty) const;
  45. };
  46. } // namespace
  47. /// Lazily initialize cache for required identifier information.
  48. void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
  49. if (NSStringII)
  50. return;
  51. NSStringII = &Ctx.Idents.get("NSString");
  52. CFStringRefII = &Ctx.Idents.get("CFStringRef");
  53. CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
  54. CFNullRefII = &Ctx.Idents.get("CFNullRef");
  55. }
  56. /// Add an assumption that const string-like globals are non-null.
  57. void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad,
  58. const Stmt *S,
  59. CheckerContext &C) const {
  60. initIdentifierInfo(C.getASTContext());
  61. if (!isLoad || !location.isValid())
  62. return;
  63. ProgramStateRef State = C.getState();
  64. if (isGlobalConstString(location)) {
  65. SVal V = State->getSVal(location.castAs<Loc>());
  66. std::optional<DefinedOrUnknownSVal> Constr =
  67. V.getAs<DefinedOrUnknownSVal>();
  68. if (Constr) {
  69. // Assume that the variable is non-null.
  70. ProgramStateRef OutputState = State->assume(*Constr, true);
  71. C.addTransition(OutputState);
  72. }
  73. }
  74. }
  75. /// \param V loaded lvalue.
  76. /// \return whether @c val is a string-like const global.
  77. bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const {
  78. std::optional<loc::MemRegionVal> RegionVal = V.getAs<loc::MemRegionVal>();
  79. if (!RegionVal)
  80. return false;
  81. auto *Region = dyn_cast<VarRegion>(RegionVal->getAsRegion());
  82. if (!Region)
  83. return false;
  84. const VarDecl *Decl = Region->getDecl();
  85. if (!Decl->hasGlobalStorage())
  86. return false;
  87. QualType Ty = Decl->getType();
  88. bool HasConst = Ty.isConstQualified();
  89. if (isNonnullType(Ty) && HasConst)
  90. return true;
  91. // Look through the typedefs.
  92. while (const Type *T = Ty.getTypePtr()) {
  93. if (const auto *AT = dyn_cast<AttributedType>(T)) {
  94. if (AT->getAttrKind() == attr::TypeNonNull)
  95. return true;
  96. Ty = AT->getModifiedType();
  97. } else if (const auto *ET = dyn_cast<ElaboratedType>(T)) {
  98. const auto *TT = dyn_cast<TypedefType>(ET->getNamedType());
  99. if (!TT)
  100. return false;
  101. Ty = TT->getDecl()->getUnderlyingType();
  102. // It is sufficient for any intermediate typedef
  103. // to be classified const.
  104. HasConst = HasConst || Ty.isConstQualified();
  105. if (isNonnullType(Ty) && HasConst)
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. return false;
  112. }
  113. /// \return whether @c type is extremely unlikely to be null
  114. bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
  115. if (Ty->isPointerType() && Ty->getPointeeType()->isCharType())
  116. return true;
  117. if (auto *T = dyn_cast<ObjCObjectPointerType>(Ty)) {
  118. return T->getInterfaceDecl() &&
  119. T->getInterfaceDecl()->getIdentifier() == NSStringII;
  120. } else if (auto *T = Ty->getAs<TypedefType>()) {
  121. IdentifierInfo* II = T->getDecl()->getIdentifier();
  122. return II == CFStringRefII || II == CFBooleanRefII || II == CFNullRefII;
  123. }
  124. return false;
  125. }
  126. void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) {
  127. Mgr.registerChecker<NonnullGlobalConstantsChecker>();
  128. }
  129. bool ento::shouldRegisterNonnullGlobalConstantsChecker(const CheckerManager &mgr) {
  130. return true;
  131. }