xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/NonnullGlobalConstantsChecker.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //==- NonnullGlobalConstantsChecker.cpp ---------------------------*- C++ -*--//
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 adds an assumption that constant globals of certain types* are
100b57cec5SDimitry Andric //  non-null, as otherwise they generally do not convey any useful information.
110b57cec5SDimitry Andric //  The assumption is useful, as many framework use e. g. global const strings,
120b57cec5SDimitry Andric //  and the analyzer might not be able to infer the global value if the
130b57cec5SDimitry Andric //  definition is in a separate translation unit.
140b57cec5SDimitry Andric //  The following types (and their typedef aliases) are considered to be
150b57cec5SDimitry Andric //  non-null:
160b57cec5SDimitry Andric //   - `char* const`
170b57cec5SDimitry Andric //   - `const CFStringRef` from CoreFoundation
180b57cec5SDimitry Andric //   - `NSString* const` from Foundation
190b57cec5SDimitry Andric //   - `CFBooleanRef` from Foundation
200b57cec5SDimitry Andric //
210b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
240b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
250b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
260b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
270b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
280b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
29*bdd1243dSDimitry Andric #include <optional>
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric using namespace clang;
320b57cec5SDimitry Andric using namespace ento;
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric namespace {
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric class NonnullGlobalConstantsChecker : public Checker<check::Location> {
370b57cec5SDimitry Andric   mutable IdentifierInfo *NSStringII = nullptr;
380b57cec5SDimitry Andric   mutable IdentifierInfo *CFStringRefII = nullptr;
390b57cec5SDimitry Andric   mutable IdentifierInfo *CFBooleanRefII = nullptr;
40480093f4SDimitry Andric   mutable IdentifierInfo *CFNullRefII = nullptr;
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric public:
NonnullGlobalConstantsChecker()430b57cec5SDimitry Andric   NonnullGlobalConstantsChecker() {}
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   void checkLocation(SVal l, bool isLoad, const Stmt *S,
460b57cec5SDimitry Andric                      CheckerContext &C) const;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric private:
490b57cec5SDimitry Andric   void initIdentifierInfo(ASTContext &Ctx) const;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   bool isGlobalConstString(SVal V) const;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   bool isNonnullType(QualType Ty) const;
540b57cec5SDimitry Andric };
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric } // namespace
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric /// Lazily initialize cache for required identifier information.
initIdentifierInfo(ASTContext & Ctx) const590b57cec5SDimitry Andric void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
600b57cec5SDimitry Andric   if (NSStringII)
610b57cec5SDimitry Andric     return;
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   NSStringII = &Ctx.Idents.get("NSString");
640b57cec5SDimitry Andric   CFStringRefII = &Ctx.Idents.get("CFStringRef");
650b57cec5SDimitry Andric   CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
66480093f4SDimitry Andric   CFNullRefII = &Ctx.Idents.get("CFNullRef");
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric /// Add an assumption that const string-like globals are non-null.
checkLocation(SVal location,bool isLoad,const Stmt * S,CheckerContext & C) const700b57cec5SDimitry Andric void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad,
710b57cec5SDimitry Andric                                                  const Stmt *S,
720b57cec5SDimitry Andric                                                  CheckerContext &C) const {
730b57cec5SDimitry Andric   initIdentifierInfo(C.getASTContext());
740b57cec5SDimitry Andric   if (!isLoad || !location.isValid())
750b57cec5SDimitry Andric     return;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   if (isGlobalConstString(location)) {
800b57cec5SDimitry Andric     SVal V = State->getSVal(location.castAs<Loc>());
81*bdd1243dSDimitry Andric     std::optional<DefinedOrUnknownSVal> Constr =
82*bdd1243dSDimitry Andric         V.getAs<DefinedOrUnknownSVal>();
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric     if (Constr) {
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric       // Assume that the variable is non-null.
870b57cec5SDimitry Andric       ProgramStateRef OutputState = State->assume(*Constr, true);
880b57cec5SDimitry Andric       C.addTransition(OutputState);
890b57cec5SDimitry Andric     }
900b57cec5SDimitry Andric   }
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric /// \param V loaded lvalue.
94fe6060f1SDimitry Andric /// \return whether @c val is a string-like const global.
isGlobalConstString(SVal V) const950b57cec5SDimitry Andric bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const {
96*bdd1243dSDimitry Andric   std::optional<loc::MemRegionVal> RegionVal = V.getAs<loc::MemRegionVal>();
970b57cec5SDimitry Andric   if (!RegionVal)
980b57cec5SDimitry Andric     return false;
990b57cec5SDimitry Andric   auto *Region = dyn_cast<VarRegion>(RegionVal->getAsRegion());
1000b57cec5SDimitry Andric   if (!Region)
1010b57cec5SDimitry Andric     return false;
1020b57cec5SDimitry Andric   const VarDecl *Decl = Region->getDecl();
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   if (!Decl->hasGlobalStorage())
1050b57cec5SDimitry Andric     return false;
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   QualType Ty = Decl->getType();
1080b57cec5SDimitry Andric   bool HasConst = Ty.isConstQualified();
1090b57cec5SDimitry Andric   if (isNonnullType(Ty) && HasConst)
1100b57cec5SDimitry Andric     return true;
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   // Look through the typedefs.
1130b57cec5SDimitry Andric   while (const Type *T = Ty.getTypePtr()) {
114*bdd1243dSDimitry Andric     if (const auto *AT = dyn_cast<AttributedType>(T)) {
115*bdd1243dSDimitry Andric       if (AT->getAttrKind() == attr::TypeNonNull)
116*bdd1243dSDimitry Andric         return true;
117*bdd1243dSDimitry Andric       Ty = AT->getModifiedType();
118*bdd1243dSDimitry Andric     } else if (const auto *ET = dyn_cast<ElaboratedType>(T)) {
119*bdd1243dSDimitry Andric       const auto *TT = dyn_cast<TypedefType>(ET->getNamedType());
120*bdd1243dSDimitry Andric       if (!TT)
121*bdd1243dSDimitry Andric         return false;
1220b57cec5SDimitry Andric       Ty = TT->getDecl()->getUnderlyingType();
1230b57cec5SDimitry Andric       // It is sufficient for any intermediate typedef
1240b57cec5SDimitry Andric       // to be classified const.
1250b57cec5SDimitry Andric       HasConst = HasConst || Ty.isConstQualified();
1260b57cec5SDimitry Andric       if (isNonnullType(Ty) && HasConst)
1270b57cec5SDimitry Andric         return true;
1280b57cec5SDimitry Andric     } else {
1290b57cec5SDimitry Andric       return false;
1300b57cec5SDimitry Andric     }
1310b57cec5SDimitry Andric   }
1320b57cec5SDimitry Andric   return false;
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
135fe6060f1SDimitry Andric /// \return whether @c type is extremely unlikely to be null
isNonnullType(QualType Ty) const1360b57cec5SDimitry Andric bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   if (Ty->isPointerType() && Ty->getPointeeType()->isCharType())
1390b57cec5SDimitry Andric     return true;
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   if (auto *T = dyn_cast<ObjCObjectPointerType>(Ty)) {
1420b57cec5SDimitry Andric     return T->getInterfaceDecl() &&
1430b57cec5SDimitry Andric       T->getInterfaceDecl()->getIdentifier() == NSStringII;
144*bdd1243dSDimitry Andric   } else if (auto *T = Ty->getAs<TypedefType>()) {
1450b57cec5SDimitry Andric     IdentifierInfo* II = T->getDecl()->getIdentifier();
146480093f4SDimitry Andric     return II == CFStringRefII || II == CFBooleanRefII || II == CFNullRefII;
1470b57cec5SDimitry Andric   }
1480b57cec5SDimitry Andric   return false;
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric 
registerNonnullGlobalConstantsChecker(CheckerManager & Mgr)1510b57cec5SDimitry Andric void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) {
1520b57cec5SDimitry Andric   Mgr.registerChecker<NonnullGlobalConstantsChecker>();
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric 
shouldRegisterNonnullGlobalConstantsChecker(const CheckerManager & mgr)1555ffd83dbSDimitry Andric bool ento::shouldRegisterNonnullGlobalConstantsChecker(const CheckerManager &mgr) {
1560b57cec5SDimitry Andric   return true;
1570b57cec5SDimitry Andric }
158