2017-10-13 08:51:41 +08:00
|
|
|
//==- NonnullGlobalConstantsChecker.cpp ---------------------------*- C++ -*--//
|
2017-10-12 02:39:40 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-10-12 02:39:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-10-13 08:51:41 +08:00
|
|
|
// This checker adds an assumption that constant globals of certain types* are
|
2017-10-12 02:39:40 +08:00
|
|
|
// non-null, as otherwise they generally do not convey any useful information.
|
2017-10-13 08:51:41 +08:00
|
|
|
// The assumption is useful, as many framework use e. g. global const strings,
|
2017-10-12 02:39:40 +08:00
|
|
|
// and the analyzer might not be able to infer the global value if the
|
|
|
|
// definition is in a separate translation unit.
|
2017-10-13 08:51:41 +08:00
|
|
|
// The following types (and their typedef aliases) are considered to be
|
|
|
|
// non-null:
|
2017-10-12 02:39:40 +08:00
|
|
|
// - `char* const`
|
|
|
|
// - `const CFStringRef` from CoreFoundation
|
|
|
|
// - `NSString* const` from Foundation
|
2017-10-13 08:51:41 +08:00
|
|
|
// - `CFBooleanRef` from Foundation
|
2017-10-12 02:39:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2017-10-12 02:39:40 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-10-13 08:51:41 +08:00
|
|
|
class NonnullGlobalConstantsChecker : public Checker<check::Location> {
|
2017-10-12 02:39:40 +08:00
|
|
|
mutable IdentifierInfo *NSStringII = nullptr;
|
|
|
|
mutable IdentifierInfo *CFStringRefII = nullptr;
|
2017-10-13 08:51:41 +08:00
|
|
|
mutable IdentifierInfo *CFBooleanRefII = nullptr;
|
2019-12-19 04:04:18 +08:00
|
|
|
mutable IdentifierInfo *CFNullRefII = nullptr;
|
2017-10-12 02:39:40 +08:00
|
|
|
|
|
|
|
public:
|
2017-10-13 08:51:41 +08:00
|
|
|
NonnullGlobalConstantsChecker() {}
|
2017-10-12 02:39:40 +08:00
|
|
|
|
|
|
|
void checkLocation(SVal l, bool isLoad, const Stmt *S,
|
|
|
|
CheckerContext &C) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void initIdentifierInfo(ASTContext &Ctx) const;
|
|
|
|
|
|
|
|
bool isGlobalConstString(SVal V) const;
|
|
|
|
|
2017-10-13 08:51:41 +08:00
|
|
|
bool isNonnullType(QualType Ty) const;
|
2017-10-12 02:39:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-04-06 23:14:32 +08:00
|
|
|
/// Lazily initialize cache for required identifier information.
|
2017-10-13 08:51:41 +08:00
|
|
|
void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
|
2017-10-12 02:39:40 +08:00
|
|
|
if (NSStringII)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NSStringII = &Ctx.Idents.get("NSString");
|
|
|
|
CFStringRefII = &Ctx.Idents.get("CFStringRef");
|
2017-10-13 08:51:41 +08:00
|
|
|
CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
|
2019-12-19 04:04:18 +08:00
|
|
|
CFNullRefII = &Ctx.Idents.get("CFNullRef");
|
2017-10-12 02:39:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add an assumption that const string-like globals are non-null.
|
2017-10-13 08:51:41 +08:00
|
|
|
void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad,
|
2017-10-12 02:39:40 +08:00
|
|
|
const Stmt *S,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
initIdentifierInfo(C.getASTContext());
|
|
|
|
if (!isLoad || !location.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
|
|
|
|
if (isGlobalConstString(location)) {
|
2018-02-28 03:28:52 +08:00
|
|
|
SVal V = State->getSVal(location.castAs<Loc>());
|
2017-10-12 02:39:40 +08:00
|
|
|
Optional<DefinedOrUnknownSVal> Constr = V.getAs<DefinedOrUnknownSVal>();
|
|
|
|
|
|
|
|
if (Constr) {
|
|
|
|
|
|
|
|
// Assume that the variable is non-null.
|
|
|
|
ProgramStateRef OutputState = State->assume(*Constr, true);
|
|
|
|
C.addTransition(OutputState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \param V loaded lvalue.
|
2021-03-16 22:17:43 +08:00
|
|
|
/// \return whether @c val is a string-like const global.
|
2017-10-13 08:51:41 +08:00
|
|
|
bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const {
|
2017-10-12 02:39:40 +08:00
|
|
|
Optional<loc::MemRegionVal> RegionVal = V.getAs<loc::MemRegionVal>();
|
|
|
|
if (!RegionVal)
|
|
|
|
return false;
|
|
|
|
auto *Region = dyn_cast<VarRegion>(RegionVal->getAsRegion());
|
|
|
|
if (!Region)
|
|
|
|
return false;
|
|
|
|
const VarDecl *Decl = Region->getDecl();
|
|
|
|
|
|
|
|
if (!Decl->hasGlobalStorage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QualType Ty = Decl->getType();
|
|
|
|
bool HasConst = Ty.isConstQualified();
|
2017-10-13 08:51:41 +08:00
|
|
|
if (isNonnullType(Ty) && HasConst)
|
2017-10-12 02:39:40 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Look through the typedefs.
|
2019-07-02 07:02:14 +08:00
|
|
|
while (const Type *T = Ty.getTypePtr()) {
|
|
|
|
if (const auto *TT = dyn_cast<TypedefType>(T)) {
|
|
|
|
Ty = TT->getDecl()->getUnderlyingType();
|
|
|
|
// It is sufficient for any intermediate typedef
|
|
|
|
// to be classified const.
|
|
|
|
HasConst = HasConst || Ty.isConstQualified();
|
|
|
|
if (isNonnullType(Ty) && HasConst)
|
|
|
|
return true;
|
|
|
|
} else if (const auto *AT = dyn_cast<AttributedType>(T)) {
|
|
|
|
if (AT->getAttrKind() == attr::TypeNonNull)
|
|
|
|
return true;
|
|
|
|
Ty = AT->getModifiedType();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-12 02:39:40 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-16 22:17:43 +08:00
|
|
|
/// \return whether @c type is extremely unlikely to be null
|
2017-10-13 08:51:41 +08:00
|
|
|
bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
|
2017-10-12 02:39:40 +08:00
|
|
|
|
|
|
|
if (Ty->isPointerType() && Ty->getPointeeType()->isCharType())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (auto *T = dyn_cast<ObjCObjectPointerType>(Ty)) {
|
2017-10-12 03:13:15 +08:00
|
|
|
return T->getInterfaceDecl() &&
|
|
|
|
T->getInterfaceDecl()->getIdentifier() == NSStringII;
|
2017-10-12 02:39:40 +08:00
|
|
|
} else if (auto *T = dyn_cast<TypedefType>(Ty)) {
|
2017-10-13 08:51:41 +08:00
|
|
|
IdentifierInfo* II = T->getDecl()->getIdentifier();
|
2019-12-19 04:04:18 +08:00
|
|
|
return II == CFStringRefII || II == CFBooleanRefII || II == CFNullRefII;
|
2017-10-12 02:39:40 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-13 08:51:41 +08:00
|
|
|
void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) {
|
|
|
|
Mgr.registerChecker<NonnullGlobalConstantsChecker>();
|
2017-10-12 02:39:40 +08:00
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterNonnullGlobalConstantsChecker(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|