1 //==- NonnullGlobalConstantsChecker.cpp ---------------------------*- C++ -*--//
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
7 //===----------------------------------------------------------------------===//
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
17 // - `const CFStringRef` from CoreFoundation
18 // - `NSString* const` from Foundation
19 // - `CFBooleanRef` from Foundation
21 //===----------------------------------------------------------------------===//
23 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
24 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
25 #include "clang/StaticAnalyzer/Core/Checker.h"
26 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
28 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
30 using namespace clang;
35 class NonnullGlobalConstantsChecker : public Checker<check::Location> {
36 mutable IdentifierInfo *NSStringII = nullptr;
37 mutable IdentifierInfo *CFStringRefII = nullptr;
38 mutable IdentifierInfo *CFBooleanRefII = nullptr;
39 mutable IdentifierInfo *CFNullRefII = nullptr;
42 NonnullGlobalConstantsChecker() {}
44 void checkLocation(SVal l, bool isLoad, const Stmt *S,
45 CheckerContext &C) const;
48 void initIdentifierInfo(ASTContext &Ctx) const;
50 bool isGlobalConstString(SVal V) const;
52 bool isNonnullType(QualType Ty) const;
57 /// Lazily initialize cache for required identifier information.
58 void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
62 NSStringII = &Ctx.Idents.get("NSString");
63 CFStringRefII = &Ctx.Idents.get("CFStringRef");
64 CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
65 CFNullRefII = &Ctx.Idents.get("CFNullRef");
68 /// Add an assumption that const string-like globals are non-null.
69 void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad,
71 CheckerContext &C) const {
72 initIdentifierInfo(C.getASTContext());
73 if (!isLoad || !location.isValid())
76 ProgramStateRef State = C.getState();
78 if (isGlobalConstString(location)) {
79 SVal V = State->getSVal(location.castAs<Loc>());
80 Optional<DefinedOrUnknownSVal> Constr = V.getAs<DefinedOrUnknownSVal>();
84 // Assume that the variable is non-null.
85 ProgramStateRef OutputState = State->assume(*Constr, true);
86 C.addTransition(OutputState);
91 /// \param V loaded lvalue.
92 /// \return whether {@code val} is a string-like const global.
93 bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const {
94 Optional<loc::MemRegionVal> RegionVal = V.getAs<loc::MemRegionVal>();
97 auto *Region = dyn_cast<VarRegion>(RegionVal->getAsRegion());
100 const VarDecl *Decl = Region->getDecl();
102 if (!Decl->hasGlobalStorage())
105 QualType Ty = Decl->getType();
106 bool HasConst = Ty.isConstQualified();
107 if (isNonnullType(Ty) && HasConst)
110 // Look through the typedefs.
111 while (const Type *T = Ty.getTypePtr()) {
112 if (const auto *TT = dyn_cast<TypedefType>(T)) {
113 Ty = TT->getDecl()->getUnderlyingType();
114 // It is sufficient for any intermediate typedef
115 // to be classified const.
116 HasConst = HasConst || Ty.isConstQualified();
117 if (isNonnullType(Ty) && HasConst)
119 } else if (const auto *AT = dyn_cast<AttributedType>(T)) {
120 if (AT->getAttrKind() == attr::TypeNonNull)
122 Ty = AT->getModifiedType();
130 /// \return whether {@code type} is extremely unlikely to be null
131 bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
133 if (Ty->isPointerType() && Ty->getPointeeType()->isCharType())
136 if (auto *T = dyn_cast<ObjCObjectPointerType>(Ty)) {
137 return T->getInterfaceDecl() &&
138 T->getInterfaceDecl()->getIdentifier() == NSStringII;
139 } else if (auto *T = dyn_cast<TypedefType>(Ty)) {
140 IdentifierInfo* II = T->getDecl()->getIdentifier();
141 return II == CFStringRefII || II == CFBooleanRefII || II == CFNullRefII;
146 void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) {
147 Mgr.registerChecker<NonnullGlobalConstantsChecker>();
150 bool ento::shouldRegisterNonnullGlobalConstantsChecker(const CheckerManager &mgr) {