2016-09-26 23:17:18 +08:00
|
|
|
//=== CastToStructChecker.cpp ----------------------------------*- C++ -*--===//
|
2009-11-09 16:07:38 +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
|
2009-11-09 16:07:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This files defines CastToStructChecker, a builtin checker that checks for
|
2016-09-26 23:17:18 +08:00
|
|
|
// cast from non-struct pointer to struct pointer and widening struct data cast.
|
2009-11-09 16:07:38 +08:00
|
|
|
// This check corresponds to CWE-588.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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"
|
2016-09-26 23:17:18 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-18 05:39:17 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2011-02-23 09:05:36 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2009-11-09 16:07:38 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-09 16:07:38 +08:00
|
|
|
|
|
|
|
namespace {
|
2016-09-26 23:17:18 +08:00
|
|
|
class CastToStructVisitor : public RecursiveASTVisitor<CastToStructVisitor> {
|
|
|
|
BugReporter &BR;
|
|
|
|
const CheckerBase *Checker;
|
|
|
|
AnalysisDeclContext *AC;
|
2011-02-23 09:05:36 +08:00
|
|
|
|
2009-11-09 16:07:38 +08:00
|
|
|
public:
|
2016-09-26 23:17:18 +08:00
|
|
|
explicit CastToStructVisitor(BugReporter &B, const CheckerBase *Checker,
|
|
|
|
AnalysisDeclContext *A)
|
|
|
|
: BR(B), Checker(Checker), AC(A) {}
|
|
|
|
bool VisitCastExpr(const CastExpr *CE);
|
2009-11-09 16:07:38 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-26 23:17:18 +08:00
|
|
|
bool CastToStructVisitor::VisitCastExpr(const CastExpr *CE) {
|
2009-11-09 16:07:38 +08:00
|
|
|
const Expr *E = CE->getSubExpr();
|
2016-09-26 23:17:18 +08:00
|
|
|
ASTContext &Ctx = AC->getASTContext();
|
2009-11-09 16:07:38 +08:00
|
|
|
QualType OrigTy = Ctx.getCanonicalType(E->getType());
|
|
|
|
QualType ToTy = Ctx.getCanonicalType(CE->getType());
|
|
|
|
|
2011-01-19 14:33:43 +08:00
|
|
|
const PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr());
|
|
|
|
const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr());
|
2009-11-09 16:07:38 +08:00
|
|
|
|
|
|
|
if (!ToPTy || !OrigPTy)
|
2016-09-26 23:17:18 +08:00
|
|
|
return true;
|
2009-11-09 16:07:38 +08:00
|
|
|
|
|
|
|
QualType OrigPointeeTy = OrigPTy->getPointeeType();
|
|
|
|
QualType ToPointeeTy = ToPTy->getPointeeType();
|
|
|
|
|
2010-04-27 05:31:17 +08:00
|
|
|
if (!ToPointeeTy->isStructureOrClassType())
|
2016-09-26 23:17:18 +08:00
|
|
|
return true;
|
2009-11-09 16:07:38 +08:00
|
|
|
|
|
|
|
// We allow cast from void*.
|
|
|
|
if (OrigPointeeTy->isVoidType())
|
2016-09-26 23:17:18 +08:00
|
|
|
return true;
|
2009-11-09 16:07:38 +08:00
|
|
|
|
|
|
|
// Now the cast-to-type is struct pointer, the original type is not void*.
|
|
|
|
if (!OrigPointeeTy->isRecordType()) {
|
2016-09-26 23:17:18 +08:00
|
|
|
SourceRange Sr[1] = {CE->getSourceRange()};
|
|
|
|
PathDiagnosticLocation Loc(CE, BR.getSourceManager(), AC);
|
|
|
|
BR.EmitBasicReport(
|
|
|
|
AC->getDecl(), Checker, "Cast from non-struct type to struct type",
|
|
|
|
categories::LogicError, "Casting a non-structure type to a structure "
|
|
|
|
"type and accessing a field can lead to memory "
|
|
|
|
"access errors or data corruption.",
|
|
|
|
Loc, Sr);
|
|
|
|
} else {
|
|
|
|
// Don't warn when size of data is unknown.
|
|
|
|
const auto *U = dyn_cast<UnaryOperator>(E);
|
|
|
|
if (!U || U->getOpcode() != UO_AddrOf)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Don't warn for references
|
|
|
|
const ValueDecl *VD = nullptr;
|
|
|
|
if (const auto *SE = dyn_cast<DeclRefExpr>(U->getSubExpr()))
|
2018-03-01 13:43:23 +08:00
|
|
|
VD = SE->getDecl();
|
2016-09-26 23:17:18 +08:00
|
|
|
else if (const auto *SE = dyn_cast<MemberExpr>(U->getSubExpr()))
|
|
|
|
VD = SE->getMemberDecl();
|
|
|
|
if (!VD || VD->getType()->isReferenceType())
|
|
|
|
return true;
|
|
|
|
|
2017-03-08 03:20:48 +08:00
|
|
|
if (ToPointeeTy->isIncompleteType() ||
|
|
|
|
OrigPointeeTy->isIncompleteType())
|
|
|
|
return true;
|
|
|
|
|
2016-09-26 23:17:18 +08:00
|
|
|
// Warn when there is widening cast.
|
|
|
|
unsigned ToWidth = Ctx.getTypeInfo(ToPointeeTy).Width;
|
|
|
|
unsigned OrigWidth = Ctx.getTypeInfo(OrigPointeeTy).Width;
|
|
|
|
if (ToWidth <= OrigWidth)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
PathDiagnosticLocation Loc(CE, BR.getSourceManager(), AC);
|
|
|
|
BR.EmitBasicReport(AC->getDecl(), Checker, "Widening cast to struct type",
|
|
|
|
categories::LogicError,
|
|
|
|
"Casting data to a larger structure type and accessing "
|
|
|
|
"a field can lead to memory access errors or data "
|
|
|
|
"corruption.",
|
|
|
|
Loc, CE->getSourceRange());
|
2009-11-09 16:07:38 +08:00
|
|
|
}
|
2016-09-26 23:17:18 +08:00
|
|
|
|
|
|
|
return true;
|
2009-11-09 16:07:38 +08:00
|
|
|
}
|
|
|
|
|
2016-09-26 23:17:18 +08:00
|
|
|
namespace {
|
|
|
|
class CastToStructChecker : public Checker<check::ASTCodeBody> {
|
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
CastToStructVisitor Visitor(BR, this, Mgr.getAnalysisDeclContext(D));
|
|
|
|
Visitor.TraverseDecl(const_cast<Decl *>(D));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-02-18 05:39:17 +08:00
|
|
|
void ento::registerCastToStructChecker(CheckerManager &mgr) {
|
2011-02-23 09:05:36 +08:00
|
|
|
mgr.registerChecker<CastToStructChecker>();
|
2011-02-18 05:39:17 +08:00
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterCastToStructChecker(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|