17330f729Sjoerg //==- NonnullGlobalConstantsChecker.cpp ---------------------------*- C++ -*--//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg //  This checker adds an assumption that constant globals of certain types* are
107330f729Sjoerg //  non-null, as otherwise they generally do not convey any useful information.
117330f729Sjoerg //  The assumption is useful, as many framework use e. g. global const strings,
127330f729Sjoerg //  and the analyzer might not be able to infer the global value if the
137330f729Sjoerg //  definition is in a separate translation unit.
147330f729Sjoerg //  The following types (and their typedef aliases) are considered to be
157330f729Sjoerg //  non-null:
167330f729Sjoerg //   - `char* const`
177330f729Sjoerg //   - `const CFStringRef` from CoreFoundation
187330f729Sjoerg //   - `NSString* const` from Foundation
197330f729Sjoerg //   - `CFBooleanRef` from Foundation
207330f729Sjoerg //
217330f729Sjoerg //===----------------------------------------------------------------------===//
227330f729Sjoerg 
237330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
247330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
257330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
267330f729Sjoerg #include "clang/StaticAnalyzer/Core/CheckerManager.h"
277330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
287330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
297330f729Sjoerg 
307330f729Sjoerg using namespace clang;
317330f729Sjoerg using namespace ento;
327330f729Sjoerg 
337330f729Sjoerg namespace {
347330f729Sjoerg 
357330f729Sjoerg class NonnullGlobalConstantsChecker : public Checker<check::Location> {
367330f729Sjoerg   mutable IdentifierInfo *NSStringII = nullptr;
377330f729Sjoerg   mutable IdentifierInfo *CFStringRefII = nullptr;
387330f729Sjoerg   mutable IdentifierInfo *CFBooleanRefII = nullptr;
39*e038c9c4Sjoerg   mutable IdentifierInfo *CFNullRefII = nullptr;
407330f729Sjoerg 
417330f729Sjoerg public:
NonnullGlobalConstantsChecker()427330f729Sjoerg   NonnullGlobalConstantsChecker() {}
437330f729Sjoerg 
447330f729Sjoerg   void checkLocation(SVal l, bool isLoad, const Stmt *S,
457330f729Sjoerg                      CheckerContext &C) const;
467330f729Sjoerg 
477330f729Sjoerg private:
487330f729Sjoerg   void initIdentifierInfo(ASTContext &Ctx) const;
497330f729Sjoerg 
507330f729Sjoerg   bool isGlobalConstString(SVal V) const;
517330f729Sjoerg 
527330f729Sjoerg   bool isNonnullType(QualType Ty) const;
537330f729Sjoerg };
547330f729Sjoerg 
557330f729Sjoerg } // namespace
567330f729Sjoerg 
577330f729Sjoerg /// Lazily initialize cache for required identifier information.
initIdentifierInfo(ASTContext & Ctx) const587330f729Sjoerg void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
597330f729Sjoerg   if (NSStringII)
607330f729Sjoerg     return;
617330f729Sjoerg 
627330f729Sjoerg   NSStringII = &Ctx.Idents.get("NSString");
637330f729Sjoerg   CFStringRefII = &Ctx.Idents.get("CFStringRef");
647330f729Sjoerg   CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
65*e038c9c4Sjoerg   CFNullRefII = &Ctx.Idents.get("CFNullRef");
667330f729Sjoerg }
677330f729Sjoerg 
687330f729Sjoerg /// Add an assumption that const string-like globals are non-null.
checkLocation(SVal location,bool isLoad,const Stmt * S,CheckerContext & C) const697330f729Sjoerg void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad,
707330f729Sjoerg                                                  const Stmt *S,
717330f729Sjoerg                                                  CheckerContext &C) const {
727330f729Sjoerg   initIdentifierInfo(C.getASTContext());
737330f729Sjoerg   if (!isLoad || !location.isValid())
747330f729Sjoerg     return;
757330f729Sjoerg 
767330f729Sjoerg   ProgramStateRef State = C.getState();
777330f729Sjoerg 
787330f729Sjoerg   if (isGlobalConstString(location)) {
797330f729Sjoerg     SVal V = State->getSVal(location.castAs<Loc>());
807330f729Sjoerg     Optional<DefinedOrUnknownSVal> Constr = V.getAs<DefinedOrUnknownSVal>();
817330f729Sjoerg 
827330f729Sjoerg     if (Constr) {
837330f729Sjoerg 
847330f729Sjoerg       // Assume that the variable is non-null.
857330f729Sjoerg       ProgramStateRef OutputState = State->assume(*Constr, true);
867330f729Sjoerg       C.addTransition(OutputState);
877330f729Sjoerg     }
887330f729Sjoerg   }
897330f729Sjoerg }
907330f729Sjoerg 
917330f729Sjoerg /// \param V loaded lvalue.
92*e038c9c4Sjoerg /// \return whether @c val is a string-like const global.
isGlobalConstString(SVal V) const937330f729Sjoerg bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const {
947330f729Sjoerg   Optional<loc::MemRegionVal> RegionVal = V.getAs<loc::MemRegionVal>();
957330f729Sjoerg   if (!RegionVal)
967330f729Sjoerg     return false;
977330f729Sjoerg   auto *Region = dyn_cast<VarRegion>(RegionVal->getAsRegion());
987330f729Sjoerg   if (!Region)
997330f729Sjoerg     return false;
1007330f729Sjoerg   const VarDecl *Decl = Region->getDecl();
1017330f729Sjoerg 
1027330f729Sjoerg   if (!Decl->hasGlobalStorage())
1037330f729Sjoerg     return false;
1047330f729Sjoerg 
1057330f729Sjoerg   QualType Ty = Decl->getType();
1067330f729Sjoerg   bool HasConst = Ty.isConstQualified();
1077330f729Sjoerg   if (isNonnullType(Ty) && HasConst)
1087330f729Sjoerg     return true;
1097330f729Sjoerg 
1107330f729Sjoerg   // Look through the typedefs.
1117330f729Sjoerg   while (const Type *T = Ty.getTypePtr()) {
1127330f729Sjoerg     if (const auto *TT = dyn_cast<TypedefType>(T)) {
1137330f729Sjoerg       Ty = TT->getDecl()->getUnderlyingType();
1147330f729Sjoerg       // It is sufficient for any intermediate typedef
1157330f729Sjoerg       // to be classified const.
1167330f729Sjoerg       HasConst = HasConst || Ty.isConstQualified();
1177330f729Sjoerg       if (isNonnullType(Ty) && HasConst)
1187330f729Sjoerg         return true;
1197330f729Sjoerg     } else if (const auto *AT = dyn_cast<AttributedType>(T)) {
1207330f729Sjoerg       if (AT->getAttrKind() == attr::TypeNonNull)
1217330f729Sjoerg         return true;
1227330f729Sjoerg       Ty = AT->getModifiedType();
1237330f729Sjoerg     } else {
1247330f729Sjoerg       return false;
1257330f729Sjoerg     }
1267330f729Sjoerg   }
1277330f729Sjoerg   return false;
1287330f729Sjoerg }
1297330f729Sjoerg 
130*e038c9c4Sjoerg /// \return whether @c type is extremely unlikely to be null
isNonnullType(QualType Ty) const1317330f729Sjoerg bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
1327330f729Sjoerg 
1337330f729Sjoerg   if (Ty->isPointerType() && Ty->getPointeeType()->isCharType())
1347330f729Sjoerg     return true;
1357330f729Sjoerg 
1367330f729Sjoerg   if (auto *T = dyn_cast<ObjCObjectPointerType>(Ty)) {
1377330f729Sjoerg     return T->getInterfaceDecl() &&
1387330f729Sjoerg       T->getInterfaceDecl()->getIdentifier() == NSStringII;
1397330f729Sjoerg   } else if (auto *T = dyn_cast<TypedefType>(Ty)) {
1407330f729Sjoerg     IdentifierInfo* II = T->getDecl()->getIdentifier();
141*e038c9c4Sjoerg     return II == CFStringRefII || II == CFBooleanRefII || II == CFNullRefII;
1427330f729Sjoerg   }
1437330f729Sjoerg   return false;
1447330f729Sjoerg }
1457330f729Sjoerg 
registerNonnullGlobalConstantsChecker(CheckerManager & Mgr)1467330f729Sjoerg void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) {
1477330f729Sjoerg   Mgr.registerChecker<NonnullGlobalConstantsChecker>();
1487330f729Sjoerg }
1497330f729Sjoerg 
shouldRegisterNonnullGlobalConstantsChecker(const CheckerManager & mgr)150*e038c9c4Sjoerg bool ento::shouldRegisterNonnullGlobalConstantsChecker(const CheckerManager &mgr) {
1517330f729Sjoerg   return true;
1527330f729Sjoerg }
153