2012-01-30 14:42:48 +08:00
|
|
|
//== ObjCContainersChecker.cpp - Path sensitive checker for CFArray *- 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-30 14:42:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Performs path sensitive checks of Core Foundation static containers like
|
|
|
|
// CFArray.
|
|
|
|
// 1) Check for buffer overflows:
|
|
|
|
// In CFArrayGetArrayAtIndex( myArray, index), if the index is outside the
|
|
|
|
// index space of theArray (0 to N-1 inclusive (where N is the count of
|
|
|
|
// theArray), the behavior is undefined.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2012-01-30 14:42:48 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ObjCContainersChecker : public Checker< check::PreStmt<CallExpr>,
|
2015-06-15 09:00:42 +08:00
|
|
|
check::PostStmt<CallExpr>,
|
|
|
|
check::PointerEscape> {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT;
|
2012-01-30 14:42:48 +08:00
|
|
|
inline void initBugType() const {
|
|
|
|
if (!BT)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT.reset(new BugType(this, "CFArray API",
|
2012-04-06 04:43:28 +08:00
|
|
|
categories::CoreFoundationObjectiveC));
|
2012-01-30 14:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline SymbolRef getArraySym(const Expr *E, CheckerContext &C) const {
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal ArrayRef = C.getSVal(E);
|
2012-01-30 14:42:48 +08:00
|
|
|
SymbolRef ArraySym = ArrayRef.getAsSymbol();
|
|
|
|
return ArraySym;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addSizeInfo(const Expr *Array, const Expr *Size,
|
|
|
|
CheckerContext &C) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// A tag to id this checker.
|
|
|
|
static void *getTag() { static int Tag; return &Tag; }
|
|
|
|
|
|
|
|
void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
|
|
|
|
void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
|
2015-06-15 09:00:42 +08:00
|
|
|
ProgramStateRef checkPointerEscape(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols &Escaped,
|
|
|
|
const CallEvent *Call,
|
|
|
|
PointerEscapeKind Kind) const;
|
2018-12-15 10:06:13 +08:00
|
|
|
|
|
|
|
void printState(raw_ostream &OS, ProgramStateRef State,
|
|
|
|
const char *NL, const char *Sep) const;
|
2012-01-30 14:42:48 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2012-11-02 09:54:06 +08:00
|
|
|
// ProgramState trait - a map from array symbol to its state.
|
|
|
|
REGISTER_MAP_WITH_PROGRAMSTATE(ArraySizeMap, SymbolRef, DefinedSVal)
|
2012-01-30 14:42:48 +08:00
|
|
|
|
|
|
|
void ObjCContainersChecker::addSizeInfo(const Expr *Array, const Expr *Size,
|
|
|
|
CheckerContext &C) const {
|
2012-01-31 09:19:57 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal SizeV = C.getSVal(Size);
|
2012-01-30 14:42:48 +08:00
|
|
|
// Undefined is reported by another checker.
|
|
|
|
if (SizeV.isUnknownOrUndef())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the ArrayRef symbol.
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal ArrayRef = C.getSVal(Array);
|
2012-01-30 14:42:48 +08:00
|
|
|
SymbolRef ArraySym = ArrayRef.getAsSymbol();
|
|
|
|
if (!ArraySym)
|
|
|
|
return;
|
|
|
|
|
2013-02-20 13:52:05 +08:00
|
|
|
C.addTransition(
|
|
|
|
State->set<ArraySizeMap>(ArraySym, SizeV.castAs<DefinedSVal>()));
|
2012-01-30 14:42:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCContainersChecker::checkPostStmt(const CallExpr *CE,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
StringRef Name = C.getCalleeName(CE);
|
2012-02-04 14:40:52 +08:00
|
|
|
if (Name.empty() || CE->getNumArgs() < 1)
|
2012-01-30 14:42:48 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Add array size information to the state.
|
|
|
|
if (Name.equals("CFArrayCreate")) {
|
2012-02-04 14:40:52 +08:00
|
|
|
if (CE->getNumArgs() < 3)
|
|
|
|
return;
|
2012-01-30 14:42:48 +08:00
|
|
|
// Note, we can visit the Create method in the post-visit because
|
|
|
|
// the CFIndex parameter is passed in by value and will not be invalidated
|
|
|
|
// by the call.
|
|
|
|
addSizeInfo(CE, CE->getArg(2), C);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Name.equals("CFArrayGetCount")) {
|
|
|
|
addSizeInfo(CE->getArg(0), CE, C);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCContainersChecker::checkPreStmt(const CallExpr *CE,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
StringRef Name = C.getCalleeName(CE);
|
2012-02-04 14:40:52 +08:00
|
|
|
if (Name.empty() || CE->getNumArgs() < 2)
|
2012-01-30 14:42:48 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Check the array access.
|
|
|
|
if (Name.equals("CFArrayGetValueAtIndex")) {
|
2012-01-31 09:19:57 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
2012-01-30 14:42:48 +08:00
|
|
|
// Retrieve the size.
|
2015-06-04 08:18:10 +08:00
|
|
|
// Find out if we saw this array symbol before and have information about
|
|
|
|
// it.
|
2012-01-30 14:42:48 +08:00
|
|
|
const Expr *ArrayExpr = CE->getArg(0);
|
2012-04-05 13:18:05 +08:00
|
|
|
SymbolRef ArraySym = getArraySym(ArrayExpr, C);
|
|
|
|
if (!ArraySym)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const DefinedSVal *Size = State->get<ArraySizeMap>(ArraySym);
|
|
|
|
|
2012-02-04 14:40:52 +08:00
|
|
|
if (!Size)
|
2012-01-30 14:42:48 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the index.
|
|
|
|
const Expr *IdxExpr = CE->getArg(1);
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal IdxVal = C.getSVal(IdxExpr);
|
2012-02-04 14:40:52 +08:00
|
|
|
if (IdxVal.isUnknownOrUndef())
|
|
|
|
return;
|
2013-02-20 13:52:05 +08:00
|
|
|
DefinedSVal Idx = IdxVal.castAs<DefinedSVal>();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-01-30 14:42:48 +08:00
|
|
|
// Now, check if 'Idx in [0, Size-1]'.
|
|
|
|
const QualType T = IdxExpr->getType();
|
2012-02-04 14:40:52 +08:00
|
|
|
ProgramStateRef StInBound = State->assumeInBound(Idx, *Size, true, T);
|
|
|
|
ProgramStateRef StOutBound = State->assumeInBound(Idx, *Size, false, T);
|
2012-01-30 14:42:48 +08:00
|
|
|
if (StOutBound && !StInBound) {
|
2015-09-17 06:03:05 +08:00
|
|
|
ExplodedNode *N = C.generateErrorNode(StOutBound);
|
2012-01-30 14:42:48 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
initBugType();
|
2015-06-23 21:15:32 +08:00
|
|
|
auto R = llvm::make_unique<BugReport>(*BT, "Index is out of bounds", N);
|
2012-01-30 14:42:48 +08:00
|
|
|
R->addRange(IdxExpr->getSourceRange());
|
2018-12-15 10:06:13 +08:00
|
|
|
bugreporter::trackExpressionValue(N, IdxExpr, *R,
|
|
|
|
/*EnableNullFPSuppression=*/false);
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(R));
|
2012-01-30 14:42:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 09:00:42 +08:00
|
|
|
ProgramStateRef
|
|
|
|
ObjCContainersChecker::checkPointerEscape(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols &Escaped,
|
|
|
|
const CallEvent *Call,
|
|
|
|
PointerEscapeKind Kind) const {
|
2016-02-11 03:11:58 +08:00
|
|
|
for (const auto &Sym : Escaped) {
|
2015-06-15 09:00:42 +08:00
|
|
|
// When a symbol for a mutable array escapes, we can't reason precisely
|
|
|
|
// about its size any more -- so remove it from the map.
|
|
|
|
// Note that we aren't notified here when a CFMutableArrayRef escapes as a
|
|
|
|
// CFArrayRef. This is because CFArrayRef is typedef'd as a pointer to a
|
|
|
|
// const-qualified type.
|
|
|
|
State = State->remove<ArraySizeMap>(Sym);
|
|
|
|
}
|
|
|
|
return State;
|
|
|
|
}
|
2016-02-11 03:11:58 +08:00
|
|
|
|
2018-12-15 10:06:13 +08:00
|
|
|
void ObjCContainersChecker::printState(raw_ostream &OS, ProgramStateRef State,
|
|
|
|
const char *NL, const char *Sep) const {
|
|
|
|
ArraySizeMapTy Map = State->get<ArraySizeMap>();
|
|
|
|
if (Map.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
OS << Sep << "ObjC container sizes :" << NL;
|
|
|
|
for (auto I : Map) {
|
|
|
|
OS << I.first << " : " << I.second << NL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 14:42:48 +08:00
|
|
|
/// Register checker.
|
|
|
|
void ento::registerObjCContainersChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ObjCContainersChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterObjCContainersChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|