2019-07-10 08:20:03 +08:00
|
|
|
//===- CastValueChecker - Model implementation of custom RTTIs --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2019-08-22 08:20:36 +08:00
|
|
|
// This defines CastValueChecker which models casts of custom RTTIs.
|
|
|
|
//
|
|
|
|
// TODO list:
|
|
|
|
// - It only allows one succesful cast between two types however in the wild
|
|
|
|
// the object could be casted to multiple types.
|
|
|
|
// - It needs to check the most likely type information from the dynamic type
|
|
|
|
// map to increase precision of dynamic casting.
|
2019-07-10 08:20:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-08-22 10:57:59 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2019-07-10 08:20:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2021-11-16 02:10:46 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
|
2019-07-10 08:20:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2019-08-22 08:20:36 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h"
|
2019-07-10 08:20:03 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2019-08-09 10:24:42 +08:00
|
|
|
#include <utility>
|
2019-07-10 08:20:03 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
2020-04-22 23:15:03 +08:00
|
|
|
class CastValueChecker : public Checker<check::DeadSymbols, eval::Call> {
|
2019-08-22 10:57:59 +08:00
|
|
|
enum class CallKind { Function, Method, InstanceOf };
|
2019-08-09 10:24:42 +08:00
|
|
|
|
2019-07-10 08:20:03 +08:00
|
|
|
using CastCheck =
|
2019-08-22 08:20:36 +08:00
|
|
|
std::function<void(const CastValueChecker *, const CallEvent &Call,
|
2019-07-10 08:20:03 +08:00
|
|
|
DefinedOrUnknownSVal, CheckerContext &)>;
|
|
|
|
|
|
|
|
public:
|
2019-08-09 10:24:42 +08:00
|
|
|
// We have five cases to evaluate a cast:
|
2019-08-22 08:20:36 +08:00
|
|
|
// 1) The parameter is non-null, the return value is non-null.
|
|
|
|
// 2) The parameter is non-null, the return value is null.
|
|
|
|
// 3) The parameter is null, the return value is null.
|
2019-07-10 08:20:03 +08:00
|
|
|
// cast: 1; dyn_cast: 1, 2; cast_or_null: 1, 3; dyn_cast_or_null: 1, 2, 3.
|
2019-08-09 10:24:42 +08:00
|
|
|
//
|
2019-08-22 08:20:36 +08:00
|
|
|
// 4) castAs: Has no parameter, the return value is non-null.
|
|
|
|
// 5) getAs: Has no parameter, the return value is null or non-null.
|
2019-08-22 10:57:59 +08:00
|
|
|
//
|
|
|
|
// We have two cases to check the parameter is an instance of the given type.
|
|
|
|
// 1) isa: The parameter is non-null, returns boolean.
|
|
|
|
// 2) isa_and_nonnull: The parameter is null or non-null, returns boolean.
|
2019-07-10 08:20:03 +08:00
|
|
|
bool evalCall(const CallEvent &Call, CheckerContext &C) const;
|
2020-04-22 23:15:03 +08:00
|
|
|
void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
|
2019-07-10 08:20:03 +08:00
|
|
|
|
|
|
|
private:
|
2019-08-09 10:24:42 +08:00
|
|
|
// These are known in the LLVM project. The pairs are in the following form:
|
|
|
|
// {{{namespace, call}, argument-count}, {callback, kind}}
|
2019-08-22 08:20:36 +08:00
|
|
|
const CallDescriptionMap<std::pair<CastCheck, CallKind>> CDM = {
|
2019-08-09 10:24:42 +08:00
|
|
|
{{{"llvm", "cast"}, 1},
|
2019-08-22 08:20:36 +08:00
|
|
|
{&CastValueChecker::evalCast, CallKind::Function}},
|
2019-08-09 10:24:42 +08:00
|
|
|
{{{"llvm", "dyn_cast"}, 1},
|
2019-08-22 08:20:36 +08:00
|
|
|
{&CastValueChecker::evalDynCast, CallKind::Function}},
|
2019-08-09 10:24:42 +08:00
|
|
|
{{{"llvm", "cast_or_null"}, 1},
|
2019-08-22 08:20:36 +08:00
|
|
|
{&CastValueChecker::evalCastOrNull, CallKind::Function}},
|
2019-07-10 08:20:03 +08:00
|
|
|
{{{"llvm", "dyn_cast_or_null"}, 1},
|
2019-08-22 08:20:36 +08:00
|
|
|
{&CastValueChecker::evalDynCastOrNull, CallKind::Function}},
|
2019-08-09 10:24:42 +08:00
|
|
|
{{{"clang", "castAs"}, 0},
|
2019-08-22 08:20:36 +08:00
|
|
|
{&CastValueChecker::evalCastAs, CallKind::Method}},
|
2019-08-09 10:24:42 +08:00
|
|
|
{{{"clang", "getAs"}, 0},
|
2019-08-22 10:57:59 +08:00
|
|
|
{&CastValueChecker::evalGetAs, CallKind::Method}},
|
|
|
|
{{{"llvm", "isa"}, 1},
|
|
|
|
{&CastValueChecker::evalIsa, CallKind::InstanceOf}},
|
|
|
|
{{{"llvm", "isa_and_nonnull"}, 1},
|
|
|
|
{&CastValueChecker::evalIsaAndNonNull, CallKind::InstanceOf}}};
|
2019-07-10 08:20:03 +08:00
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
void evalCast(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
2019-07-10 08:20:03 +08:00
|
|
|
CheckerContext &C) const;
|
2019-08-22 08:20:36 +08:00
|
|
|
void evalDynCast(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
2019-07-10 08:20:03 +08:00
|
|
|
CheckerContext &C) const;
|
2019-08-22 08:20:36 +08:00
|
|
|
void evalCastOrNull(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
2019-07-10 08:20:03 +08:00
|
|
|
CheckerContext &C) const;
|
2019-08-22 08:20:36 +08:00
|
|
|
void evalDynCastOrNull(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
2019-07-10 08:20:03 +08:00
|
|
|
CheckerContext &C) const;
|
2019-08-22 08:20:36 +08:00
|
|
|
void evalCastAs(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
2019-08-09 10:24:42 +08:00
|
|
|
CheckerContext &C) const;
|
2019-08-22 08:20:36 +08:00
|
|
|
void evalGetAs(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
2019-08-09 10:24:42 +08:00
|
|
|
CheckerContext &C) const;
|
2019-08-22 10:57:59 +08:00
|
|
|
void evalIsa(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
|
|
|
CheckerContext &C) const;
|
|
|
|
void evalIsaAndNonNull(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
|
|
|
CheckerContext &C) const;
|
2019-07-10 08:20:03 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
static bool isInfeasibleCast(const DynamicCastInfo *CastInfo,
|
|
|
|
bool CastSucceeds) {
|
|
|
|
if (!CastInfo)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return CastSucceeds ? CastInfo->fails() : CastInfo->succeeds();
|
|
|
|
}
|
|
|
|
|
|
|
|
static const NoteTag *getNoteTag(CheckerContext &C,
|
|
|
|
const DynamicCastInfo *CastInfo,
|
|
|
|
QualType CastToTy, const Expr *Object,
|
|
|
|
bool CastSucceeds, bool IsKnownCast) {
|
|
|
|
std::string CastToName =
|
2020-08-27 23:01:43 +08:00
|
|
|
CastInfo ? CastInfo->to()->getAsCXXRecordDecl()->getNameAsString()
|
2019-08-23 11:24:01 +08:00
|
|
|
: CastToTy->getPointeeCXXRecordDecl()->getNameAsString();
|
2019-08-22 08:20:36 +08:00
|
|
|
Object = Object->IgnoreParenImpCasts();
|
2019-07-10 08:20:03 +08:00
|
|
|
|
2019-08-09 10:24:42 +08:00
|
|
|
return C.getNoteTag(
|
2019-08-22 09:41:06 +08:00
|
|
|
[=]() -> std::string {
|
2019-07-10 08:20:03 +08:00
|
|
|
SmallString<128> Msg;
|
|
|
|
llvm::raw_svector_ostream Out(Msg);
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
if (!IsKnownCast)
|
|
|
|
Out << "Assuming ";
|
|
|
|
|
|
|
|
if (const auto *DRE = dyn_cast<DeclRefExpr>(Object)) {
|
2020-08-05 18:48:09 +08:00
|
|
|
Out << '\'' << DRE->getDecl()->getDeclName() << '\'';
|
2019-08-22 08:20:36 +08:00
|
|
|
} else if (const auto *ME = dyn_cast<MemberExpr>(Object)) {
|
|
|
|
Out << (IsKnownCast ? "Field '" : "field '")
|
2020-08-05 18:48:09 +08:00
|
|
|
<< ME->getMemberDecl()->getDeclName() << '\'';
|
2019-08-22 08:20:36 +08:00
|
|
|
} else {
|
|
|
|
Out << (IsKnownCast ? "The object" : "the object");
|
|
|
|
}
|
2019-07-10 08:20:03 +08:00
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
Out << ' ' << (CastSucceeds ? "is a" : "is not a") << " '" << CastToName
|
|
|
|
<< '\'';
|
2019-07-10 08:20:03 +08:00
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(Out.str());
|
2019-07-10 08:20:03 +08:00
|
|
|
},
|
|
|
|
/*IsPrunable=*/true);
|
2019-08-09 10:24:42 +08:00
|
|
|
}
|
2019-07-10 08:20:03 +08:00
|
|
|
|
2020-08-27 23:06:10 +08:00
|
|
|
static const NoteTag *getNoteTag(CheckerContext &C,
|
|
|
|
SmallVector<QualType, 4> CastToTyVec,
|
|
|
|
const Expr *Object,
|
|
|
|
bool IsKnownCast) {
|
|
|
|
Object = Object->IgnoreParenImpCasts();
|
|
|
|
|
|
|
|
return C.getNoteTag(
|
|
|
|
[=]() -> std::string {
|
|
|
|
SmallString<128> Msg;
|
|
|
|
llvm::raw_svector_ostream Out(Msg);
|
|
|
|
|
|
|
|
if (!IsKnownCast)
|
|
|
|
Out << "Assuming ";
|
|
|
|
|
|
|
|
if (const auto *DRE = dyn_cast<DeclRefExpr>(Object)) {
|
|
|
|
Out << '\'' << DRE->getDecl()->getNameAsString() << '\'';
|
|
|
|
} else if (const auto *ME = dyn_cast<MemberExpr>(Object)) {
|
|
|
|
Out << (IsKnownCast ? "Field '" : "field '")
|
|
|
|
<< ME->getMemberDecl()->getNameAsString() << '\'';
|
|
|
|
} else {
|
|
|
|
Out << (IsKnownCast ? "The object" : "the object");
|
|
|
|
}
|
|
|
|
Out << " is";
|
|
|
|
|
|
|
|
bool First = true;
|
|
|
|
for (QualType CastToTy: CastToTyVec) {
|
|
|
|
std::string CastToName =
|
|
|
|
CastToTy->getAsCXXRecordDecl() ?
|
|
|
|
CastToTy->getAsCXXRecordDecl()->getNameAsString() :
|
|
|
|
CastToTy->getPointeeCXXRecordDecl()->getNameAsString();
|
|
|
|
Out << ' ' << ((CastToTyVec.size() == 1) ? "not" :
|
|
|
|
(First ? "neither" : "nor")) << " a '" << CastToName
|
|
|
|
<< '\'';
|
|
|
|
First = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string(Out.str());
|
|
|
|
},
|
|
|
|
/*IsPrunable=*/true);
|
|
|
|
}
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Main logic to evaluate a cast.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-08-23 11:24:04 +08:00
|
|
|
static QualType alignReferenceTypes(QualType toAlign, QualType alignTowards,
|
|
|
|
ASTContext &ACtx) {
|
|
|
|
if (alignTowards->isLValueReferenceType() &&
|
|
|
|
alignTowards.isConstQualified()) {
|
|
|
|
toAlign.addConst();
|
|
|
|
return ACtx.getLValueReferenceType(toAlign);
|
|
|
|
} else if (alignTowards->isLValueReferenceType())
|
|
|
|
return ACtx.getLValueReferenceType(toAlign);
|
|
|
|
else if (alignTowards->isRValueReferenceType())
|
|
|
|
return ACtx.getRValueReferenceType(toAlign);
|
|
|
|
|
|
|
|
llvm_unreachable("Must align towards a reference type!");
|
|
|
|
}
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
static void addCastTransition(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
|
|
|
CheckerContext &C, bool IsNonNullParam,
|
|
|
|
bool IsNonNullReturn,
|
|
|
|
bool IsCheckedCast = false) {
|
|
|
|
ProgramStateRef State = C.getState()->assume(DV, IsNonNullParam);
|
|
|
|
if (!State)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Expr *Object;
|
|
|
|
QualType CastFromTy;
|
2019-08-23 11:24:01 +08:00
|
|
|
QualType CastToTy = Call.getResultType();
|
2019-08-22 08:20:36 +08:00
|
|
|
|
|
|
|
if (Call.getNumArgs() > 0) {
|
|
|
|
Object = Call.getArgExpr(0);
|
2019-08-23 11:24:01 +08:00
|
|
|
CastFromTy = Call.parameters()[0]->getType();
|
2019-08-22 08:20:36 +08:00
|
|
|
} else {
|
|
|
|
Object = cast<CXXInstanceCall>(&Call)->getCXXThisExpr();
|
2019-08-23 11:24:01 +08:00
|
|
|
CastFromTy = Object->getType();
|
2019-08-23 11:24:04 +08:00
|
|
|
if (CastToTy->isPointerType()) {
|
|
|
|
if (!CastFromTy->isPointerType())
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (!CastFromTy->isReferenceType())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CastFromTy = alignReferenceTypes(CastFromTy, CastToTy, C.getASTContext());
|
|
|
|
}
|
2019-08-22 08:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const MemRegion *MR = DV.getAsRegion();
|
|
|
|
const DynamicCastInfo *CastInfo =
|
|
|
|
getDynamicCastInfo(State, MR, CastFromTy, CastToTy);
|
|
|
|
|
|
|
|
// We assume that every checked cast succeeds.
|
|
|
|
bool CastSucceeds = IsCheckedCast || CastFromTy == CastToTy;
|
|
|
|
if (!CastSucceeds) {
|
|
|
|
if (CastInfo)
|
|
|
|
CastSucceeds = IsNonNullReturn && CastInfo->succeeds();
|
|
|
|
else
|
|
|
|
CastSucceeds = IsNonNullReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for infeasible casts.
|
|
|
|
if (isInfeasibleCast(CastInfo, CastSucceeds)) {
|
|
|
|
C.generateSink(State, C.getPredecessor());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the type and the cast information.
|
|
|
|
bool IsKnownCast = CastInfo || IsCheckedCast || CastFromTy == CastToTy;
|
|
|
|
if (!IsKnownCast || IsCheckedCast)
|
|
|
|
State = setDynamicTypeAndCastInfo(State, MR, CastFromTy, CastToTy,
|
2019-08-23 11:24:01 +08:00
|
|
|
CastSucceeds);
|
2019-08-22 08:20:36 +08:00
|
|
|
|
2019-08-23 11:24:04 +08:00
|
|
|
SVal V = CastSucceeds ? C.getSValBuilder().evalCast(DV, CastToTy, CastFromTy)
|
|
|
|
: C.getSValBuilder().makeNull();
|
2019-08-22 08:20:36 +08:00
|
|
|
C.addTransition(
|
|
|
|
State->BindExpr(Call.getOriginExpr(), C.getLocationContext(), V, false),
|
|
|
|
getNoteTag(C, CastInfo, CastToTy, Object, CastSucceeds, IsKnownCast));
|
2019-07-10 08:20:03 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 10:57:59 +08:00
|
|
|
static void addInstanceOfTransition(const CallEvent &Call,
|
|
|
|
DefinedOrUnknownSVal DV,
|
|
|
|
ProgramStateRef State, CheckerContext &C,
|
|
|
|
bool IsInstanceOf) {
|
|
|
|
const FunctionDecl *FD = Call.getDecl()->getAsFunction();
|
2019-08-23 11:24:01 +08:00
|
|
|
QualType CastFromTy = Call.parameters()[0]->getType();
|
2020-08-27 23:06:10 +08:00
|
|
|
SmallVector<QualType, 4> CastToTyVec;
|
|
|
|
for (unsigned idx = 0; idx < FD->getTemplateSpecializationArgs()->size() - 1;
|
|
|
|
++idx) {
|
|
|
|
TemplateArgument CastToTempArg =
|
|
|
|
FD->getTemplateSpecializationArgs()->get(idx);
|
|
|
|
switch (CastToTempArg.getKind()) {
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
case TemplateArgument::Type:
|
|
|
|
CastToTyVec.push_back(CastToTempArg.getAsType());
|
|
|
|
break;
|
|
|
|
case TemplateArgument::Pack:
|
|
|
|
for (TemplateArgument ArgInPack: CastToTempArg.pack_elements())
|
|
|
|
CastToTyVec.push_back(ArgInPack.getAsType());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-22 10:57:59 +08:00
|
|
|
|
|
|
|
const MemRegion *MR = DV.getAsRegion();
|
2020-08-27 23:06:10 +08:00
|
|
|
if (MR && CastFromTy->isReferenceType())
|
|
|
|
MR = State->getSVal(DV.castAs<Loc>()).getAsRegion();
|
|
|
|
|
|
|
|
bool Success = false;
|
|
|
|
bool IsAnyKnown = false;
|
|
|
|
for (QualType CastToTy: CastToTyVec) {
|
|
|
|
if (CastFromTy->isPointerType())
|
|
|
|
CastToTy = C.getASTContext().getPointerType(CastToTy);
|
|
|
|
else if (CastFromTy->isReferenceType())
|
|
|
|
CastToTy = alignReferenceTypes(CastToTy, CastFromTy, C.getASTContext());
|
|
|
|
else
|
|
|
|
return;
|
2019-08-22 10:57:59 +08:00
|
|
|
|
2020-08-27 23:06:10 +08:00
|
|
|
const DynamicCastInfo *CastInfo =
|
|
|
|
getDynamicCastInfo(State, MR, CastFromTy, CastToTy);
|
2019-08-22 10:57:59 +08:00
|
|
|
|
2020-08-27 23:06:10 +08:00
|
|
|
bool CastSucceeds;
|
|
|
|
if (CastInfo)
|
|
|
|
CastSucceeds = IsInstanceOf && CastInfo->succeeds();
|
|
|
|
else
|
|
|
|
CastSucceeds = IsInstanceOf || CastFromTy == CastToTy;
|
|
|
|
|
|
|
|
// Store the type and the cast information.
|
|
|
|
bool IsKnownCast = CastInfo || CastFromTy == CastToTy;
|
|
|
|
IsAnyKnown = IsAnyKnown || IsKnownCast;
|
|
|
|
ProgramStateRef NewState = State;
|
|
|
|
if (!IsKnownCast)
|
|
|
|
NewState = setDynamicTypeAndCastInfo(State, MR, CastFromTy, CastToTy,
|
|
|
|
IsInstanceOf);
|
|
|
|
|
|
|
|
if (CastSucceeds) {
|
|
|
|
Success = true;
|
|
|
|
C.addTransition(
|
|
|
|
NewState->BindExpr(Call.getOriginExpr(), C.getLocationContext(),
|
|
|
|
C.getSValBuilder().makeTruthVal(true)),
|
|
|
|
getNoteTag(C, CastInfo, CastToTy, Call.getArgExpr(0), true,
|
|
|
|
IsKnownCast));
|
|
|
|
if (IsKnownCast)
|
|
|
|
return;
|
|
|
|
} else if (CastInfo && CastInfo->succeeds()) {
|
|
|
|
C.generateSink(NewState, C.getPredecessor());
|
|
|
|
return;
|
|
|
|
}
|
2019-08-22 10:57:59 +08:00
|
|
|
}
|
|
|
|
|
2020-08-27 23:06:10 +08:00
|
|
|
if (!Success) {
|
|
|
|
C.addTransition(
|
|
|
|
State->BindExpr(Call.getOriginExpr(), C.getLocationContext(),
|
|
|
|
C.getSValBuilder().makeTruthVal(false)),
|
|
|
|
getNoteTag(C, CastToTyVec, Call.getArgExpr(0), IsAnyKnown));
|
|
|
|
}
|
2019-08-22 10:57:59 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 10:24:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Evaluating cast, dyn_cast, cast_or_null, dyn_cast_or_null.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-07-10 08:20:03 +08:00
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
static void evalNonNullParamNonNullReturn(const CallEvent &Call,
|
2019-08-09 10:24:42 +08:00
|
|
|
DefinedOrUnknownSVal DV,
|
|
|
|
CheckerContext &C,
|
|
|
|
bool IsCheckedCast = false) {
|
2019-08-22 08:20:36 +08:00
|
|
|
addCastTransition(Call, DV, C, /*IsNonNullParam=*/true,
|
|
|
|
/*IsNonNullReturn=*/true, IsCheckedCast);
|
2019-08-09 10:24:42 +08:00
|
|
|
}
|
2019-07-10 08:20:03 +08:00
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
static void evalNonNullParamNullReturn(const CallEvent &Call,
|
2019-08-09 10:24:42 +08:00
|
|
|
DefinedOrUnknownSVal DV,
|
|
|
|
CheckerContext &C) {
|
2019-08-22 08:20:36 +08:00
|
|
|
addCastTransition(Call, DV, C, /*IsNonNullParam=*/true,
|
|
|
|
/*IsNonNullReturn=*/false);
|
2019-08-09 10:24:42 +08:00
|
|
|
}
|
2019-07-10 08:20:03 +08:00
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
static void evalNullParamNullReturn(const CallEvent &Call,
|
|
|
|
DefinedOrUnknownSVal DV,
|
2019-08-09 10:24:42 +08:00
|
|
|
CheckerContext &C) {
|
|
|
|
if (ProgramStateRef State = C.getState()->assume(DV, false))
|
2019-08-22 08:20:36 +08:00
|
|
|
C.addTransition(State->BindExpr(Call.getOriginExpr(),
|
|
|
|
C.getLocationContext(),
|
|
|
|
C.getSValBuilder().makeNull(), false),
|
2019-08-09 10:24:42 +08:00
|
|
|
C.getNoteTag("Assuming null pointer is passed into cast",
|
|
|
|
/*IsPrunable=*/true));
|
2019-07-10 08:20:03 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
void CastValueChecker::evalCast(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
2019-07-10 08:20:03 +08:00
|
|
|
CheckerContext &C) const {
|
2019-08-22 08:20:36 +08:00
|
|
|
evalNonNullParamNonNullReturn(Call, DV, C, /*IsCheckedCast=*/true);
|
2019-07-10 08:20:03 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
void CastValueChecker::evalDynCast(const CallEvent &Call,
|
|
|
|
DefinedOrUnknownSVal DV,
|
2019-07-10 08:20:03 +08:00
|
|
|
CheckerContext &C) const {
|
2019-08-22 08:20:36 +08:00
|
|
|
evalNonNullParamNonNullReturn(Call, DV, C);
|
|
|
|
evalNonNullParamNullReturn(Call, DV, C);
|
2019-07-10 08:20:03 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
void CastValueChecker::evalCastOrNull(const CallEvent &Call,
|
2019-08-09 10:24:42 +08:00
|
|
|
DefinedOrUnknownSVal DV,
|
2019-07-10 08:20:03 +08:00
|
|
|
CheckerContext &C) const {
|
2019-08-22 08:20:36 +08:00
|
|
|
evalNonNullParamNonNullReturn(Call, DV, C);
|
|
|
|
evalNullParamNullReturn(Call, DV, C);
|
2019-07-10 08:20:03 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
void CastValueChecker::evalDynCastOrNull(const CallEvent &Call,
|
2019-08-09 10:24:42 +08:00
|
|
|
DefinedOrUnknownSVal DV,
|
2019-07-10 08:20:03 +08:00
|
|
|
CheckerContext &C) const {
|
2019-08-22 08:20:36 +08:00
|
|
|
evalNonNullParamNonNullReturn(Call, DV, C);
|
|
|
|
evalNonNullParamNullReturn(Call, DV, C);
|
|
|
|
evalNullParamNullReturn(Call, DV, C);
|
2019-08-09 10:24:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Evaluating castAs, getAs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
static void evalZeroParamNonNullReturn(const CallEvent &Call,
|
2019-08-09 10:24:42 +08:00
|
|
|
DefinedOrUnknownSVal DV,
|
|
|
|
CheckerContext &C,
|
|
|
|
bool IsCheckedCast = false) {
|
2019-08-22 08:20:36 +08:00
|
|
|
addCastTransition(Call, DV, C, /*IsNonNullParam=*/true,
|
|
|
|
/*IsNonNullReturn=*/true, IsCheckedCast);
|
2019-08-09 10:24:42 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
static void evalZeroParamNullReturn(const CallEvent &Call,
|
|
|
|
DefinedOrUnknownSVal DV,
|
2019-08-09 10:24:42 +08:00
|
|
|
CheckerContext &C) {
|
2019-08-22 08:20:36 +08:00
|
|
|
addCastTransition(Call, DV, C, /*IsNonNullParam=*/true,
|
|
|
|
/*IsNonNullReturn=*/false);
|
2019-08-09 10:24:42 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
void CastValueChecker::evalCastAs(const CallEvent &Call,
|
|
|
|
DefinedOrUnknownSVal DV,
|
2019-08-09 10:24:42 +08:00
|
|
|
CheckerContext &C) const {
|
2019-08-22 08:20:36 +08:00
|
|
|
evalZeroParamNonNullReturn(Call, DV, C, /*IsCheckedCast=*/true);
|
2019-08-09 10:24:42 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
void CastValueChecker::evalGetAs(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
2019-08-09 10:24:42 +08:00
|
|
|
CheckerContext &C) const {
|
2019-08-22 08:20:36 +08:00
|
|
|
evalZeroParamNonNullReturn(Call, DV, C);
|
|
|
|
evalZeroParamNullReturn(Call, DV, C);
|
2019-07-10 08:20:03 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 10:57:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Evaluating isa, isa_and_nonnull.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void CastValueChecker::evalIsa(const CallEvent &Call, DefinedOrUnknownSVal DV,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
ProgramStateRef NonNullState, NullState;
|
|
|
|
std::tie(NonNullState, NullState) = C.getState()->assume(DV);
|
|
|
|
|
|
|
|
if (NonNullState) {
|
|
|
|
addInstanceOfTransition(Call, DV, NonNullState, C, /*IsInstanceOf=*/true);
|
|
|
|
addInstanceOfTransition(Call, DV, NonNullState, C, /*IsInstanceOf=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NullState) {
|
|
|
|
C.generateSink(NullState, C.getPredecessor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CastValueChecker::evalIsaAndNonNull(const CallEvent &Call,
|
|
|
|
DefinedOrUnknownSVal DV,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
ProgramStateRef NonNullState, NullState;
|
|
|
|
std::tie(NonNullState, NullState) = C.getState()->assume(DV);
|
|
|
|
|
|
|
|
if (NonNullState) {
|
|
|
|
addInstanceOfTransition(Call, DV, NonNullState, C, /*IsInstanceOf=*/true);
|
|
|
|
addInstanceOfTransition(Call, DV, NonNullState, C, /*IsInstanceOf=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NullState) {
|
|
|
|
addInstanceOfTransition(Call, DV, NullState, C, /*IsInstanceOf=*/false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Main logic to evaluate a call.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-07-10 08:20:03 +08:00
|
|
|
bool CastValueChecker::evalCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
2019-08-09 10:24:42 +08:00
|
|
|
const auto *Lookup = CDM.lookup(Call);
|
|
|
|
if (!Lookup)
|
2019-07-10 08:20:03 +08:00
|
|
|
return false;
|
|
|
|
|
2019-08-22 10:57:59 +08:00
|
|
|
const CastCheck &Check = Lookup->first;
|
|
|
|
CallKind Kind = Lookup->second;
|
|
|
|
|
2019-08-09 10:24:42 +08:00
|
|
|
Optional<DefinedOrUnknownSVal> DV;
|
|
|
|
|
|
|
|
switch (Kind) {
|
2019-08-22 08:20:36 +08:00
|
|
|
case CallKind::Function: {
|
2019-08-23 11:23:58 +08:00
|
|
|
// We only model casts from pointers to pointers or from references
|
|
|
|
// to references. Other casts are most likely specialized and we
|
|
|
|
// cannot model them.
|
|
|
|
QualType ParamT = Call.parameters()[0]->getType();
|
|
|
|
QualType ResultT = Call.getResultType();
|
|
|
|
if (!(ParamT->isPointerType() && ResultT->isPointerType()) &&
|
2020-08-27 23:06:10 +08:00
|
|
|
!(ParamT->isReferenceType() && ResultT->isReferenceType())) {
|
2019-08-09 10:24:42 +08:00
|
|
|
return false;
|
2020-08-27 23:06:10 +08:00
|
|
|
}
|
2019-08-09 10:24:42 +08:00
|
|
|
|
|
|
|
DV = Call.getArgSVal(0).getAs<DefinedOrUnknownSVal>();
|
|
|
|
break;
|
|
|
|
}
|
2019-08-22 10:57:59 +08:00
|
|
|
case CallKind::InstanceOf: {
|
|
|
|
// We need to obtain the only template argument to determinte the type.
|
|
|
|
const FunctionDecl *FD = Call.getDecl()->getAsFunction();
|
|
|
|
if (!FD || !FD->getTemplateSpecializationArgs())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DV = Call.getArgSVal(0).getAs<DefinedOrUnknownSVal>();
|
|
|
|
break;
|
|
|
|
}
|
2019-08-22 08:20:36 +08:00
|
|
|
case CallKind::Method:
|
2019-08-09 10:24:42 +08:00
|
|
|
const auto *InstanceCall = dyn_cast<CXXInstanceCall>(&Call);
|
|
|
|
if (!InstanceCall)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DV = InstanceCall->getCXXThisVal().getAs<DefinedOrUnknownSVal>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DV)
|
2019-07-10 08:20:03 +08:00
|
|
|
return false;
|
|
|
|
|
2019-08-22 08:20:36 +08:00
|
|
|
Check(this, Call, *DV, C);
|
2019-07-10 08:20:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-22 23:15:03 +08:00
|
|
|
void CastValueChecker::checkDeadSymbols(SymbolReaper &SR,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
C.addTransition(removeDeadCasts(C.getState(), SR));
|
|
|
|
}
|
|
|
|
|
2019-07-10 08:20:03 +08:00
|
|
|
void ento::registerCastValueChecker(CheckerManager &Mgr) {
|
|
|
|
Mgr.registerChecker<CastValueChecker>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterCastValueChecker(const CheckerManager &mgr) {
|
2019-07-10 08:20:03 +08:00
|
|
|
return true;
|
|
|
|
}
|