2012-01-26 09:05:43 +08:00
|
|
|
//== ObjCContainersASTChecker.cpp - CoreFoundation containers API *- C++ -*-==//
|
|
|
|
//
|
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
|
2012-01-26 09:05:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// An AST checker that looks for common pitfalls when using 'CFArray',
|
|
|
|
// 'CFDictionary', 'CFSet' APIs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
[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"
|
2012-01-26 09:05:43 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2017-09-07 05:45:03 +08:00
|
|
|
#include "clang/Analysis/AnalysisDeclContext.h"
|
2012-01-26 09:05:43 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2012-01-26 09:05:43 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-01-26 09:05:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class WalkAST : public StmtVisitor<WalkAST> {
|
|
|
|
BugReporter &BR;
|
2014-02-12 05:49:21 +08:00
|
|
|
const CheckerBase *Checker;
|
2012-01-26 09:05:43 +08:00
|
|
|
AnalysisDeclContext* AC;
|
|
|
|
ASTContext &ASTC;
|
|
|
|
uint64_t PtrWidth;
|
|
|
|
|
|
|
|
/// Check if the type has pointer size (very conservative).
|
|
|
|
inline bool isPointerSize(const Type *T) {
|
|
|
|
if (!T)
|
|
|
|
return true;
|
|
|
|
if (T->isIncompleteType())
|
|
|
|
return true;
|
|
|
|
return (ASTC.getTypeSize(T) == PtrWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the type is a pointer/array to pointer sized values.
|
|
|
|
inline bool hasPointerToPointerSizedType(const Expr *E) {
|
|
|
|
QualType T = E->getType();
|
|
|
|
|
|
|
|
// The type could be either a pointer or array.
|
|
|
|
const Type *TP = T.getTypePtr();
|
|
|
|
QualType PointeeT = TP->getPointeeType();
|
2012-02-02 09:30:08 +08:00
|
|
|
if (!PointeeT.isNull()) {
|
|
|
|
// If the type is a pointer to an array, check the size of the array
|
|
|
|
// elements. To avoid false positives coming from assumption that the
|
|
|
|
// values x and &x are equal when x is an array.
|
|
|
|
if (const Type *TElem = PointeeT->getArrayElementTypeNoTypeQual())
|
|
|
|
if (isPointerSize(TElem))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Else, check the pointee size.
|
2012-01-26 09:05:43 +08:00
|
|
|
return isPointerSize(PointeeT.getTypePtr());
|
2012-02-02 09:30:08 +08:00
|
|
|
}
|
2012-01-26 09:05:43 +08:00
|
|
|
|
|
|
|
if (const Type *TElem = TP->getArrayElementTypeNoTypeQual())
|
|
|
|
return isPointerSize(TElem);
|
|
|
|
|
|
|
|
// The type must be an array/pointer type.
|
|
|
|
|
|
|
|
// This could be a null constant, which is allowed.
|
2015-12-28 21:06:58 +08:00
|
|
|
return static_cast<bool>(
|
|
|
|
E->isNullPointerConstant(ASTC, Expr::NPC_ValueDependentIsNull));
|
2012-01-26 09:05:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2014-02-12 05:49:21 +08:00
|
|
|
WalkAST(BugReporter &br, const CheckerBase *checker, AnalysisDeclContext *ac)
|
|
|
|
: BR(br), Checker(checker), AC(ac), ASTC(AC->getASTContext()),
|
|
|
|
PtrWidth(ASTC.getTargetInfo().getPointerWidth(0)) {}
|
2012-01-26 09:05:43 +08:00
|
|
|
|
|
|
|
// Statement visitor methods.
|
|
|
|
void VisitChildren(Stmt *S);
|
|
|
|
void VisitStmt(Stmt *S) { VisitChildren(S); }
|
|
|
|
void VisitCallExpr(CallExpr *CE);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static StringRef getCalleeName(CallExpr *CE) {
|
|
|
|
const FunctionDecl *FD = CE->getDirectCallee();
|
|
|
|
if (!FD)
|
|
|
|
return StringRef();
|
|
|
|
|
|
|
|
IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
if (!II) // if no identifier, not a simple C function
|
|
|
|
return StringRef();
|
|
|
|
|
|
|
|
return II->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WalkAST::VisitCallExpr(CallExpr *CE) {
|
|
|
|
StringRef Name = getCalleeName(CE);
|
|
|
|
if (Name.empty())
|
|
|
|
return;
|
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
const Expr *Arg = nullptr;
|
2012-10-16 08:47:25 +08:00
|
|
|
unsigned ArgNum;
|
2012-01-26 09:05:43 +08:00
|
|
|
|
|
|
|
if (Name.equals("CFArrayCreate") || Name.equals("CFSetCreate")) {
|
2012-10-13 06:56:36 +08:00
|
|
|
if (CE->getNumArgs() != 4)
|
|
|
|
return;
|
2012-01-26 09:05:43 +08:00
|
|
|
ArgNum = 1;
|
|
|
|
Arg = CE->getArg(ArgNum)->IgnoreParenCasts();
|
|
|
|
if (hasPointerToPointerSizedType(Arg))
|
|
|
|
return;
|
2012-10-16 08:47:25 +08:00
|
|
|
} else if (Name.equals("CFDictionaryCreate")) {
|
2012-10-13 06:56:36 +08:00
|
|
|
if (CE->getNumArgs() != 6)
|
|
|
|
return;
|
2012-01-26 09:05:43 +08:00
|
|
|
// Check first argument.
|
|
|
|
ArgNum = 1;
|
|
|
|
Arg = CE->getArg(ArgNum)->IgnoreParenCasts();
|
|
|
|
if (hasPointerToPointerSizedType(Arg)) {
|
|
|
|
// Check second argument.
|
|
|
|
ArgNum = 2;
|
|
|
|
Arg = CE->getArg(ArgNum)->IgnoreParenCasts();
|
|
|
|
if (hasPointerToPointerSizedType(Arg))
|
|
|
|
// Both are good, return.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-16 08:47:25 +08:00
|
|
|
if (Arg) {
|
2012-01-26 09:05:43 +08:00
|
|
|
assert(ArgNum == 1 || ArgNum == 2);
|
|
|
|
|
2012-10-16 08:47:25 +08:00
|
|
|
SmallString<64> BufName;
|
2012-01-26 09:05:43 +08:00
|
|
|
llvm::raw_svector_ostream OsName(BufName);
|
|
|
|
OsName << " Invalid use of '" << Name << "'" ;
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> Buf;
|
2012-01-26 09:05:43 +08:00
|
|
|
llvm::raw_svector_ostream Os(Buf);
|
2012-09-07 15:13:08 +08:00
|
|
|
// Use "second" and "third" since users will expect 1-based indexing
|
|
|
|
// for parameter names when mentioned in prose.
|
|
|
|
Os << " The "<< ((ArgNum == 1) ? "second" : "third") << " argument to '"
|
2012-01-26 09:05:43 +08:00
|
|
|
<< Name << "' must be a C array of pointer-sized values, not '"
|
|
|
|
<< Arg->getType().getAsString() << "'";
|
|
|
|
|
|
|
|
PathDiagnosticLocation CELoc =
|
|
|
|
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
|
2014-02-12 05:49:21 +08:00
|
|
|
BR.EmitBasicReport(AC->getDecl(), Checker, OsName.str(),
|
|
|
|
categories::CoreFoundationObjectiveC, Os.str(), CELoc,
|
|
|
|
Arg->getSourceRange());
|
2012-01-26 09:05:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse and check children.
|
|
|
|
VisitChildren(CE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WalkAST::VisitChildren(Stmt *S) {
|
2015-07-03 23:12:24 +08:00
|
|
|
for (Stmt *Child : S->children())
|
|
|
|
if (Child)
|
|
|
|
Visit(Child);
|
2012-01-26 09:05:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ObjCContainersASTChecker : public Checker<check::ASTCodeBody> {
|
|
|
|
public:
|
|
|
|
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager& Mgr,
|
|
|
|
BugReporter &BR) const {
|
2014-02-12 05:49:21 +08:00
|
|
|
WalkAST walker(BR, this, Mgr.getAnalysisDeclContext(D));
|
2012-01-26 09:05:43 +08:00
|
|
|
walker.Visit(D->getBody());
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-01-26 09:05:43 +08:00
|
|
|
|
|
|
|
void ento::registerObjCContainersASTChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ObjCContainersASTChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterObjCContainersASTChecker(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|