You've already forked linux-packaging-mono
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
INPUTS
bindings
cmake
docs
examples
include
lib
ARCMigrate
AST
ASTMatchers
Analysis
Basic
CodeGen
CrossTU
Driver
Edit
Format
Frontend
FrontendTool
Headers
Index
Lex
Parse
Rewrite
Sema
Serialization
StaticAnalyzer
Checkers
MPI-Checker
AllocationDiagnostics.cpp
AllocationDiagnostics.h
AnalysisOrderChecker.cpp
AnalyzerStatsChecker.cpp
ArrayBoundChecker.cpp
ArrayBoundCheckerV2.cpp
BasicObjCFoundationChecks.cpp
BlockInCriticalSectionChecker.cpp
BoolAssignmentChecker.cpp
BuiltinFunctionChecker.cpp
CMakeLists.txt
CStringChecker.cpp
CStringSyntaxChecker.cpp
CXXSelfAssignmentChecker.cpp
CallAndMessageChecker.cpp
CastSizeChecker.cpp
CastToStructChecker.cpp
CheckObjCDealloc.cpp
CheckObjCInstMethSignature.cpp
CheckSecuritySyntaxOnly.cpp
CheckSizeofPointer.cpp
CheckerDocumentation.cpp
ChrootChecker.cpp
ClangCheckers.cpp
ClangSACheckers.h
CloneChecker.cpp
ConversionChecker.cpp
DeadStoresChecker.cpp
DebugCheckers.cpp
DeleteWithNonVirtualDtorChecker.cpp
DereferenceChecker.cpp
DirectIvarAssignment.cpp
DivZeroChecker.cpp
DynamicTypeChecker.cpp
DynamicTypePropagation.cpp
ExprInspectionChecker.cpp
FixedAddressChecker.cpp
GTestChecker.cpp
GenericTaintChecker.cpp
IdenticalExprChecker.cpp
InterCheckerAPI.h
IteratorChecker.cpp
IvarInvalidationChecker.cpp
LLVMConventionsChecker.cpp
LocalizationChecker.cpp
MacOSKeychainAPIChecker.cpp
MacOSXAPIChecker.cpp
MallocChecker.cpp.REMOVED.git-id
MallocOverflowSecurityChecker.cpp
MallocSizeofChecker.cpp
MisusedMovedObjectChecker.cpp
NSAutoreleasePoolChecker.cpp
NSErrorChecker.cpp
NoReturnFunctionChecker.cpp
NonNullParamChecker.cpp
NonnullGlobalConstantsChecker.cpp
NullabilityChecker.cpp
NumberObjectConversionChecker.cpp
ObjCAtSyncChecker.cpp
ObjCContainersASTChecker.cpp
ObjCContainersChecker.cpp
ObjCMissingSuperCallChecker.cpp
ObjCPropertyChecker.cpp
ObjCSelfInitChecker.cpp
ObjCSuperDeallocChecker.cpp
ObjCUnusedIVarsChecker.cpp
PaddingChecker.cpp
PointerArithChecker.cpp
PointerSubChecker.cpp
PthreadLockChecker.cpp
RetainCountChecker.cpp.REMOVED.git-id
ReturnPointerRangeChecker.cpp
ReturnUndefChecker.cpp
SelectorExtras.h
SimpleStreamChecker.cpp
StackAddrEscapeChecker.cpp
StdLibraryFunctionsChecker.cpp
StreamChecker.cpp
TaintTesterChecker.cpp
TestAfterDivZeroChecker.cpp
TraversalChecker.cpp
UndefBranchChecker.cpp
UndefCapturedBlockVarChecker.cpp
UndefResultChecker.cpp
UndefinedArraySubscriptChecker.cpp
UndefinedAssignmentChecker.cpp
UnixAPIChecker.cpp
UnreachableCodeChecker.cpp
VLASizeChecker.cpp
ValistChecker.cpp
VforkChecker.cpp
VirtualCallChecker.cpp
Core
Frontend
CMakeLists.txt
README.txt
Tooling
CMakeLists.txt
runtime
tools
unittests
utils
www
.arcconfig
.clang-format
.clang-tidy
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
INSTALL.txt
LICENSE.TXT
ModuleInfo.txt
NOTES.txt
README.txt
clang-tools-extra
compiler-rt
eng
libcxx
libcxxabi
libunwind
lld
lldb
llvm
nuget
openmp
polly
Directory.Build.props
Directory.Build.targets
NuGet.config
azure-pipelines.yml
build.cmd
build.sh
dir.common.props
global.json
llvm.proj
mxe-Win64.cmake.in
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
141 lines
4.6 KiB
C++
141 lines
4.6 KiB
C++
![]() |
//==- NonnullGlobalConstantsChecker.cpp ---------------------------*- C++ -*--//
|
||
|
//
|
||
|
// The LLVM Compiler Infrastructure
|
||
|
//
|
||
|
// This file is distributed under the University of Illinois Open Source
|
||
|
// License. See LICENSE.TXT for details.
|
||
|
//
|
||
|
//===----------------------------------------------------------------------===//
|
||
|
//
|
||
|
// This checker adds an assumption that constant globals of certain types* are
|
||
|
// non-null, as otherwise they generally do not convey any useful information.
|
||
|
// The assumption is useful, as many framework use e. g. global const strings,
|
||
|
// and the analyzer might not be able to infer the global value if the
|
||
|
// definition is in a separate translation unit.
|
||
|
// The following types (and their typedef aliases) are considered to be
|
||
|
// non-null:
|
||
|
// - `char* const`
|
||
|
// - `const CFStringRef` from CoreFoundation
|
||
|
// - `NSString* const` from Foundation
|
||
|
// - `CFBooleanRef` from Foundation
|
||
|
//
|
||
|
//===----------------------------------------------------------------------===//
|
||
|
|
||
|
#include "ClangSACheckers.h"
|
||
|
#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 {
|
||
|
|
||
|
class NonnullGlobalConstantsChecker : public Checker<check::Location> {
|
||
|
mutable IdentifierInfo *NSStringII = nullptr;
|
||
|
mutable IdentifierInfo *CFStringRefII = nullptr;
|
||
|
mutable IdentifierInfo *CFBooleanRefII = nullptr;
|
||
|
|
||
|
public:
|
||
|
NonnullGlobalConstantsChecker() {}
|
||
|
|
||
|
void checkLocation(SVal l, bool isLoad, const Stmt *S,
|
||
|
CheckerContext &C) const;
|
||
|
|
||
|
private:
|
||
|
void initIdentifierInfo(ASTContext &Ctx) const;
|
||
|
|
||
|
bool isGlobalConstString(SVal V) const;
|
||
|
|
||
|
bool isNonnullType(QualType Ty) const;
|
||
|
};
|
||
|
|
||
|
} // namespace
|
||
|
|
||
|
/// Lazily initialize cache for required identifier informations.
|
||
|
void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
|
||
|
if (NSStringII)
|
||
|
return;
|
||
|
|
||
|
NSStringII = &Ctx.Idents.get("NSString");
|
||
|
CFStringRefII = &Ctx.Idents.get("CFStringRef");
|
||
|
CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
|
||
|
}
|
||
|
|
||
|
/// Add an assumption that const string-like globals are non-null.
|
||
|
void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad,
|
||
|
const Stmt *S,
|
||
|
CheckerContext &C) const {
|
||
|
initIdentifierInfo(C.getASTContext());
|
||
|
if (!isLoad || !location.isValid())
|
||
|
return;
|
||
|
|
||
|
ProgramStateRef State = C.getState();
|
||
|
SVal V = State->getSVal(location.castAs<Loc>());
|
||
|
|
||
|
if (isGlobalConstString(location)) {
|
||
|
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.
|
||
|
/// \return whether {@code val} is a string-like const global.
|
||
|
bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const {
|
||
|
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();
|
||
|
if (isNonnullType(Ty) && HasConst)
|
||
|
return true;
|
||
|
|
||
|
// Look through the typedefs.
|
||
|
while (auto *T = dyn_cast<TypedefType>(Ty)) {
|
||
|
Ty = T->getDecl()->getUnderlyingType();
|
||
|
|
||
|
// It is sufficient for any intermediate typedef
|
||
|
// to be classified const.
|
||
|
HasConst = HasConst || Ty.isConstQualified();
|
||
|
if (isNonnullType(Ty) && HasConst)
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/// \return whether {@code type} is extremely unlikely to be null
|
||
|
bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
|
||
|
|
||
|
if (Ty->isPointerType() && Ty->getPointeeType()->isCharType())
|
||
|
return true;
|
||
|
|
||
|
if (auto *T = dyn_cast<ObjCObjectPointerType>(Ty)) {
|
||
|
return T->getInterfaceDecl() &&
|
||
|
T->getInterfaceDecl()->getIdentifier() == NSStringII;
|
||
|
} else if (auto *T = dyn_cast<TypedefType>(Ty)) {
|
||
|
IdentifierInfo* II = T->getDecl()->getIdentifier();
|
||
|
return II == CFStringRefII || II == CFBooleanRefII;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) {
|
||
|
Mgr.registerChecker<NonnullGlobalConstantsChecker>();
|
||
|
}
|