NonnullGlobalConstantsChecker.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. using namespace clang;
  29. using namespace ento;
  30. namespace {
  31. class NonnullGlobalConstantsChecker : public Checker<check::Location> {
  32. mutable IdentifierInfo *NSStringII = nullptr;
  33. mutable IdentifierInfo *CFStringRefII = nullptr;
  34. mutable IdentifierInfo *CFBooleanRefII = nullptr;
  35. mutable IdentifierInfo *CFNullRefII = nullptr;
  36. public:
  37. NonnullGlobalConstantsChecker() {}
  38. void checkLocation(SVal l, bool isLoad, const Stmt *S,
  39. CheckerContext &C) const;
  40. private:
  41. void initIdentifierInfo(ASTContext &Ctx) const;
  42. bool isGlobalConstString(SVal V) const;
  43. bool isNonnullType(QualType Ty) const;
  44. };
  45. } // namespace
  46. /// Lazily initialize cache for required identifier information.
  47. void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
  48. if (NSStringII)
  49. return;
  50. NSStringII = &Ctx.Idents.get("NSString");
  51. CFStringRefII = &Ctx.Idents.get("CFStringRef");
  52. CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
  53. CFNullRefII = &Ctx.Idents.get("CFNullRef");
  54. }
  55. /// Add an assumption that const string-like globals are non-null.
  56. void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad,
  57. const Stmt *S,
  58. CheckerContext &C) const {
  59. initIdentifierInfo(C.getASTContext());
  60. if (!isLoad || !location.isValid())
  61. return;
  62. ProgramStateRef State = C.getState();
  63. if (isGlobalConstString(location)) {
  64. SVal V = State->getSVal(location.castAs<Loc>());
  65. Optional<DefinedOrUnknownSVal> Constr = V.getAs<DefinedOrUnknownSVal>();
  66. if (Constr) {
  67. // Assume that the variable is non-null.
  68. ProgramStateRef OutputState = State->assume(*Constr, true);
  69. C.addTransition(OutputState);
  70. }
  71. }
  72. }
  73. /// \param V loaded lvalue.
  74. /// \return whether @c val is a string-like const global.
  75. bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const {
  76. Optional<loc::MemRegionVal> RegionVal = V.getAs<loc::MemRegionVal>();
  77. if (!RegionVal)
  78. return false;
  79. auto *Region = dyn_cast<VarRegion>(RegionVal->getAsRegion());
  80. if (!Region)
  81. return false;
  82. const VarDecl *Decl = Region->getDecl();
  83. if (!Decl->hasGlobalStorage())
  84. return false;
  85. QualType Ty = Decl->getType();
  86. bool HasConst = Ty.isConstQualified();
  87. if (isNonnullType(Ty) && HasConst)
  88. return true;
  89. // Look through the typedefs.
  90. while (const Type *T = Ty.getTypePtr()) {
  91. if (const auto *TT = dyn_cast<TypedefType>(T)) {
  92. Ty = TT->getDecl()->getUnderlyingType();
  93. // It is sufficient for any intermediate typedef
  94. // to be classified const.
  95. HasConst = HasConst || Ty.isConstQualified();
  96. if (isNonnullType(Ty) && HasConst)
  97. return true;
  98. } else if (const auto *AT = dyn_cast<AttributedType>(T)) {
  99. if (AT->getAttrKind() == attr::TypeNonNull)
  100. return true;
  101. Ty = AT->getModifiedType();
  102. } else {
  103. return false;
  104. }
  105. }
  106. return false;
  107. }
  108. /// \return whether @c type is extremely unlikely to be null
  109. bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
  110. if (Ty->isPointerType() && Ty->getPointeeType()->isCharType())
  111. return true;
  112. if (auto *T = dyn_cast<ObjCObjectPointerType>(Ty)) {
  113. return T->getInterfaceDecl() &&
  114. T->getInterfaceDecl()->getIdentifier() == NSStringII;
  115. } else if (auto *T = dyn_cast<TypedefType>(Ty)) {
  116. IdentifierInfo* II = T->getDecl()->getIdentifier();
  117. return II == CFStringRefII || II == CFBooleanRefII || II == CFNullRefII;
  118. }
  119. return false;
  120. }
  121. void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) {
  122. Mgr.registerChecker<NonnullGlobalConstantsChecker>();
  123. }
  124. bool ento::shouldRegisterNonnullGlobalConstantsChecker(const CheckerManager &mgr) {
  125. return true;
  126. }