2018-11-24 20:24:27 +08:00
|
|
|
//===- EnumCastOutOfRangeChecker.cpp ---------------------------*- 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
|
2018-11-24 20:24:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The EnumCastOutOfRangeChecker is responsible for checking integer to
|
|
|
|
// enumeration casts that could result in undefined values. This could happen
|
|
|
|
// if the value that we cast from is out of the value range of the enumeration.
|
|
|
|
// Reference:
|
|
|
|
// [ISO/IEC 14882-2014] ISO/IEC 14882-2014.
|
|
|
|
// Programming Languages — C++, Fourth Edition. 2014.
|
|
|
|
// C++ Standard, [dcl.enum], in paragraph 8, which defines the range of an enum
|
|
|
|
// C++ Standard, [expr.static.cast], paragraph 10, which defines the behaviour
|
|
|
|
// of casting an integer value that is out of range
|
|
|
|
// SEI CERT C++ Coding Standard, INT50-CPP. Do not cast to an out-of-range
|
|
|
|
// enumeration value
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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"
|
2018-11-24 20:24:27 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// This evaluator checks two SVals for equality. The first SVal is provided via
|
|
|
|
// the constructor, the second is the parameter of the overloaded () operator.
|
|
|
|
// It uses the in-built ConstraintManager to resolve the equlity to possible or
|
|
|
|
// not possible ProgramStates.
|
|
|
|
class ConstraintBasedEQEvaluator {
|
|
|
|
const DefinedOrUnknownSVal CompareValue;
|
|
|
|
const ProgramStateRef PS;
|
|
|
|
SValBuilder &SVB;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ConstraintBasedEQEvaluator(CheckerContext &C,
|
|
|
|
const DefinedOrUnknownSVal CompareValue)
|
|
|
|
: CompareValue(CompareValue), PS(C.getState()), SVB(C.getSValBuilder()) {}
|
|
|
|
|
|
|
|
bool operator()(const llvm::APSInt &EnumDeclInitValue) {
|
|
|
|
DefinedOrUnknownSVal EnumDeclValue = SVB.makeIntVal(EnumDeclInitValue);
|
|
|
|
DefinedOrUnknownSVal ElemEqualsValueToCast =
|
|
|
|
SVB.evalEQ(PS, EnumDeclValue, CompareValue);
|
|
|
|
|
|
|
|
return static_cast<bool>(PS->assume(ElemEqualsValueToCast, true));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This checker checks CastExpr statements.
|
|
|
|
// If the value provided to the cast is one of the values the enumeration can
|
|
|
|
// represent, the said value matches the enumeration. If the checker can
|
|
|
|
// establish the impossibility of matching it gives a warning.
|
|
|
|
// Being conservative, it does not warn if there is slight possibility the
|
|
|
|
// value can be matching.
|
|
|
|
class EnumCastOutOfRangeChecker : public Checker<check::PreStmt<CastExpr>> {
|
|
|
|
mutable std::unique_ptr<BuiltinBug> EnumValueCastOutOfRange;
|
|
|
|
void reportWarning(CheckerContext &C) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void checkPreStmt(const CastExpr *CE, CheckerContext &C) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
using EnumValueVector = llvm::SmallVector<llvm::APSInt, 6>;
|
|
|
|
|
|
|
|
// Collects all of the values an enum can represent (as SVals).
|
|
|
|
EnumValueVector getDeclValuesForEnum(const EnumDecl *ED) {
|
|
|
|
EnumValueVector DeclValues(
|
|
|
|
std::distance(ED->enumerator_begin(), ED->enumerator_end()));
|
|
|
|
llvm::transform(ED->enumerators(), DeclValues.begin(),
|
|
|
|
[](const EnumConstantDecl *D) { return D->getInitVal(); });
|
|
|
|
return DeclValues;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void EnumCastOutOfRangeChecker::reportWarning(CheckerContext &C) const {
|
|
|
|
if (const ExplodedNode *N = C.generateNonFatalErrorNode()) {
|
|
|
|
if (!EnumValueCastOutOfRange)
|
|
|
|
EnumValueCastOutOfRange.reset(
|
|
|
|
new BuiltinBug(this, "Enum cast out of range",
|
|
|
|
"The value provided to the cast expression is not in "
|
|
|
|
"the valid range of values for the enum"));
|
2019-09-10 04:34:40 +08:00
|
|
|
C.emitReport(std::make_unique<PathSensitiveBugReport>(
|
2018-11-24 20:24:27 +08:00
|
|
|
*EnumValueCastOutOfRange, EnumValueCastOutOfRange->getDescription(),
|
|
|
|
N));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnumCastOutOfRangeChecker::checkPreStmt(const CastExpr *CE,
|
|
|
|
CheckerContext &C) const {
|
[analyzer] Avoid unnecessary enum range check on LValueToRValue casts
Summary: EnumCastOutOfRangeChecker should not perform enum range checks on LValueToRValue casts, since this type of cast does not actually change the underlying type. Performing the unnecessary check actually triggered an assertion failure deeper in EnumCastOutOfRange for certain input (which is captured in the accompanying test code).
Reviewers: #clang, Szelethus, gamesh411, NoQ
Reviewed By: Szelethus, gamesh411, NoQ
Subscribers: NoQ, gamesh411, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, dkrupp, Charusso, bjope, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66014
llvm-svn: 369760
2019-08-23 22:21:13 +08:00
|
|
|
|
|
|
|
// Only perform enum range check on casts where such checks are valid. For
|
|
|
|
// all other cast kinds (where enum range checks are unnecessary or invalid),
|
2021-10-30 02:32:03 +08:00
|
|
|
// just return immediately. TODO: The set of casts allowed for enum range
|
|
|
|
// checking may be incomplete. Better to add a missing cast kind to enable a
|
|
|
|
// missing check than to generate false negatives and have to remove those
|
|
|
|
// later.
|
[analyzer] Avoid unnecessary enum range check on LValueToRValue casts
Summary: EnumCastOutOfRangeChecker should not perform enum range checks on LValueToRValue casts, since this type of cast does not actually change the underlying type. Performing the unnecessary check actually triggered an assertion failure deeper in EnumCastOutOfRange for certain input (which is captured in the accompanying test code).
Reviewers: #clang, Szelethus, gamesh411, NoQ
Reviewed By: Szelethus, gamesh411, NoQ
Subscribers: NoQ, gamesh411, xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, donat.nagy, dkrupp, Charusso, bjope, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66014
llvm-svn: 369760
2019-08-23 22:21:13 +08:00
|
|
|
switch (CE->getCastKind()) {
|
|
|
|
case CK_IntegralCast:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-24 20:24:27 +08:00
|
|
|
// Get the value of the expression to cast.
|
|
|
|
const llvm::Optional<DefinedOrUnknownSVal> ValueToCast =
|
|
|
|
C.getSVal(CE->getSubExpr()).getAs<DefinedOrUnknownSVal>();
|
|
|
|
|
|
|
|
// If the value cannot be reasoned about (not even a DefinedOrUnknownSVal),
|
|
|
|
// don't analyze further.
|
|
|
|
if (!ValueToCast)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const QualType T = CE->getType();
|
|
|
|
// Check whether the cast type is an enum.
|
|
|
|
if (!T->isEnumeralType())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If the cast is an enum, get its declaration.
|
|
|
|
// If the isEnumeralType() returned true, then the declaration must exist
|
|
|
|
// even if it is a stub declaration. It is up to the getDeclValuesForEnum()
|
|
|
|
// function to handle this.
|
|
|
|
const EnumDecl *ED = T->castAs<EnumType>()->getDecl();
|
|
|
|
|
|
|
|
EnumValueVector DeclValues = getDeclValuesForEnum(ED);
|
|
|
|
// Check if any of the enum values possibly match.
|
|
|
|
bool PossibleValueMatch = llvm::any_of(
|
|
|
|
DeclValues, ConstraintBasedEQEvaluator(C, *ValueToCast));
|
|
|
|
|
|
|
|
// If there is no value that can possibly match any of the enum values, then
|
|
|
|
// warn.
|
|
|
|
if (!PossibleValueMatch)
|
|
|
|
reportWarning(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerEnumCastOutOfRangeChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<EnumCastOutOfRangeChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterEnumCastOutOfRangeChecker(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|