2018-12-04 06:32:32 +08:00
|
|
|
// MoveChecker.cpp - Check use of moved-from objects. - C++ ---------------===//
|
2017-03-24 17:52:30 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines checker which checks for potential misuses of a moved-from
|
|
|
|
// object. That means method calls on the object or copying it in moved-from
|
|
|
|
// state.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ClangSACheckers.h"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct RegionState {
|
|
|
|
private:
|
|
|
|
enum Kind { Moved, Reported } K;
|
|
|
|
RegionState(Kind InK) : K(InK) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool isReported() const { return K == Reported; }
|
|
|
|
bool isMoved() const { return K == Moved; }
|
|
|
|
|
|
|
|
static RegionState getReported() { return RegionState(Reported); }
|
|
|
|
static RegionState getMoved() { return RegionState(Moved); }
|
|
|
|
|
|
|
|
bool operator==(const RegionState &X) const { return K == X.K; }
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(K); }
|
|
|
|
};
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
class MoveChecker
|
2018-12-04 06:44:16 +08:00
|
|
|
: public Checker<check::PreCall, check::PostCall,
|
2017-03-24 17:52:30 +08:00
|
|
|
check::DeadSymbols, check::RegionChanges> {
|
|
|
|
public:
|
2018-07-17 04:47:45 +08:00
|
|
|
void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
|
2017-03-24 17:52:30 +08:00
|
|
|
void checkPreCall(const CallEvent &MC, CheckerContext &C) const;
|
|
|
|
void checkPostCall(const CallEvent &MC, CheckerContext &C) const;
|
|
|
|
void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
|
|
|
|
ProgramStateRef
|
|
|
|
checkRegionChanges(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols *Invalidated,
|
|
|
|
ArrayRef<const MemRegion *> ExplicitRegions,
|
|
|
|
ArrayRef<const MemRegion *> Regions,
|
|
|
|
const LocationContext *LCtx, const CallEvent *Call) const;
|
2017-10-10 19:50:45 +08:00
|
|
|
void printState(raw_ostream &Out, ProgramStateRef State,
|
|
|
|
const char *NL, const char *Sep) const override;
|
2017-03-24 17:52:30 +08:00
|
|
|
|
|
|
|
private:
|
2018-12-04 07:06:07 +08:00
|
|
|
enum MisuseKind { MK_FunCall, MK_Copy, MK_Move };
|
|
|
|
|
|
|
|
struct ObjectKind {
|
|
|
|
bool Local : 1; // Is this a local variable or a local rvalue reference?
|
|
|
|
bool STL : 1; // Is this an object of a standard type?
|
|
|
|
};
|
|
|
|
|
|
|
|
static ObjectKind classifyObject(const MemRegion *MR,
|
|
|
|
const CXXRecordDecl *RD);
|
|
|
|
|
[analyzer] Do not run visitors until the fixpoint, run only once.
In the current implementation, we run visitors until the fixed point is
reached.
That is, if a visitor adds another visitor, the currently processed path
is destroyed, all diagnostics is discarded, and it is regenerated again,
until it's no longer modified.
This pattern has a few negative implications:
- This loop does not even guarantee to terminate.
E.g. just imagine two visitors bouncing a diagnostics around.
- Performance-wise, e.g. for sqlite3 all visitors are being re-run at
least 10 times for some bugs.
We have already seen a few reports where it leads to timeouts.
- If we want to add more computationally intense visitors, this will
become worse.
- From architectural standpoint, the current layout requires copying
visitors, which is conceptually wrong, and can be annoying (e.g. no
unique_ptr on visitors allowed).
The proposed change is a much simpler architecture: the outer loop
processes nodes upwards, and whenever the visitor is added it only
processes current nodes and above, thus guaranteeing termination.
Differential Revision: https://reviews.llvm.org/D47856
llvm-svn: 335666
2018-06-27 05:12:08 +08:00
|
|
|
class MovedBugVisitor : public BugReporterVisitor {
|
2017-03-24 17:52:30 +08:00
|
|
|
public:
|
|
|
|
MovedBugVisitor(const MemRegion *R) : Region(R), Found(false) {}
|
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const override {
|
|
|
|
static int X = 0;
|
|
|
|
ID.AddPointer(&X);
|
|
|
|
ID.AddPointer(Region);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
|
|
|
|
BugReporterContext &BRC,
|
|
|
|
BugReport &BR) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// The tracked region.
|
|
|
|
const MemRegion *Region;
|
|
|
|
bool Found;
|
|
|
|
};
|
|
|
|
|
2018-12-04 07:06:07 +08:00
|
|
|
bool IsAggressive = false;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setAggressiveness(bool Aggressive) { IsAggressive = Aggressive; }
|
|
|
|
|
|
|
|
private:
|
2017-03-24 17:52:30 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT;
|
2018-12-04 07:06:07 +08:00
|
|
|
ExplodedNode *reportBug(const MemRegion *Region, const CXXRecordDecl *RD,
|
2017-10-29 07:24:00 +08:00
|
|
|
CheckerContext &C, MisuseKind MK) const;
|
2017-03-24 17:52:30 +08:00
|
|
|
bool isInMoveSafeContext(const LocationContext *LC) const;
|
|
|
|
bool isStateResetMethod(const CXXMethodDecl *MethodDec) const;
|
|
|
|
bool isMoveSafeMethod(const CXXMethodDecl *MethodDec) const;
|
|
|
|
const ExplodedNode *getMoveLocation(const ExplodedNode *N,
|
|
|
|
const MemRegion *Region,
|
|
|
|
CheckerContext &C) const;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
REGISTER_MAP_WITH_PROGRAMSTATE(TrackedRegionMap, const MemRegion *, RegionState)
|
|
|
|
|
|
|
|
// If a region is removed all of the subregions needs to be removed too.
|
|
|
|
static ProgramStateRef removeFromState(ProgramStateRef State,
|
|
|
|
const MemRegion *Region) {
|
|
|
|
if (!Region)
|
|
|
|
return State;
|
|
|
|
for (auto &E : State->get<TrackedRegionMap>()) {
|
|
|
|
if (E.first->isSubRegionOf(Region))
|
|
|
|
State = State->remove<TrackedRegionMap>(E.first);
|
|
|
|
}
|
|
|
|
return State;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isAnyBaseRegionReported(ProgramStateRef State,
|
|
|
|
const MemRegion *Region) {
|
|
|
|
for (auto &E : State->get<TrackedRegionMap>()) {
|
|
|
|
if (Region->isSubRegionOf(E.first) && E.second.isReported())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-04 07:06:07 +08:00
|
|
|
static const MemRegion *unwrapRValueReferenceIndirection(const MemRegion *MR) {
|
|
|
|
if (const auto *SR = dyn_cast_or_null<SymbolicRegion>(MR)) {
|
|
|
|
SymbolRef Sym = SR->getSymbol();
|
|
|
|
if (Sym->getType()->isRValueReferenceType())
|
|
|
|
if (const MemRegion *OriginMR = Sym->getOriginRegion())
|
|
|
|
return OriginMR;
|
|
|
|
}
|
|
|
|
return MR;
|
|
|
|
}
|
|
|
|
|
2017-03-24 17:52:30 +08:00
|
|
|
std::shared_ptr<PathDiagnosticPiece>
|
2018-12-04 06:32:32 +08:00
|
|
|
MoveChecker::MovedBugVisitor::VisitNode(const ExplodedNode *N,
|
|
|
|
BugReporterContext &BRC, BugReport &) {
|
2017-03-24 17:52:30 +08:00
|
|
|
// We need only the last move of the reported object's region.
|
|
|
|
// The visitor walks the ExplodedGraph backwards.
|
|
|
|
if (Found)
|
|
|
|
return nullptr;
|
|
|
|
ProgramStateRef State = N->getState();
|
2018-09-29 02:49:41 +08:00
|
|
|
ProgramStateRef StatePrev = N->getFirstPred()->getState();
|
2017-03-24 17:52:30 +08:00
|
|
|
const RegionState *TrackedObject = State->get<TrackedRegionMap>(Region);
|
|
|
|
const RegionState *TrackedObjectPrev =
|
|
|
|
StatePrev->get<TrackedRegionMap>(Region);
|
|
|
|
if (!TrackedObject)
|
|
|
|
return nullptr;
|
|
|
|
if (TrackedObjectPrev && TrackedObject)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Retrieve the associated statement.
|
|
|
|
const Stmt *S = PathDiagnosticLocation::getStmt(N);
|
|
|
|
if (!S)
|
|
|
|
return nullptr;
|
|
|
|
Found = true;
|
|
|
|
|
|
|
|
std::string ObjectName;
|
2018-12-04 07:06:07 +08:00
|
|
|
if (const auto DecReg =
|
|
|
|
unwrapRValueReferenceIndirection(Region)->getAs<DeclRegion>()) {
|
2017-03-24 17:52:30 +08:00
|
|
|
const auto *RegionDecl = dyn_cast<NamedDecl>(DecReg->getDecl());
|
|
|
|
ObjectName = RegionDecl->getNameAsString();
|
|
|
|
}
|
|
|
|
std::string InfoText;
|
|
|
|
if (ObjectName != "")
|
|
|
|
InfoText = "'" + ObjectName + "' became 'moved-from' here";
|
|
|
|
else
|
|
|
|
InfoText = "Became 'moved-from' here";
|
|
|
|
|
|
|
|
// Generate the extra diagnostic.
|
|
|
|
PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
|
|
|
|
N->getLocationContext());
|
|
|
|
return std::make_shared<PathDiagnosticEventPiece>(Pos, InfoText, true);
|
|
|
|
}
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
const ExplodedNode *MoveChecker::getMoveLocation(const ExplodedNode *N,
|
|
|
|
const MemRegion *Region,
|
|
|
|
CheckerContext &C) const {
|
2017-03-24 17:52:30 +08:00
|
|
|
// Walk the ExplodedGraph backwards and find the first node that referred to
|
|
|
|
// the tracked region.
|
|
|
|
const ExplodedNode *MoveNode = N;
|
|
|
|
|
|
|
|
while (N) {
|
|
|
|
ProgramStateRef State = N->getState();
|
|
|
|
if (!State->get<TrackedRegionMap>(Region))
|
|
|
|
break;
|
|
|
|
MoveNode = N;
|
|
|
|
N = N->pred_empty() ? nullptr : *(N->pred_begin());
|
|
|
|
}
|
|
|
|
return MoveNode;
|
|
|
|
}
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
ExplodedNode *MoveChecker::reportBug(const MemRegion *Region,
|
2018-12-04 07:06:07 +08:00
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
CheckerContext &C,
|
2018-12-04 06:32:32 +08:00
|
|
|
MisuseKind MK) const {
|
2017-03-24 17:52:30 +08:00
|
|
|
if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
|
|
|
|
if (!BT)
|
|
|
|
BT.reset(new BugType(this, "Usage of a 'moved-from' object",
|
|
|
|
"C++ move semantics"));
|
|
|
|
|
|
|
|
// Uniqueing report to the same object.
|
|
|
|
PathDiagnosticLocation LocUsedForUniqueing;
|
|
|
|
const ExplodedNode *MoveNode = getMoveLocation(N, Region, C);
|
|
|
|
|
|
|
|
if (const Stmt *MoveStmt = PathDiagnosticLocation::getStmt(MoveNode))
|
|
|
|
LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
|
|
|
|
MoveStmt, C.getSourceManager(), MoveNode->getLocationContext());
|
|
|
|
|
|
|
|
// Creating the error message.
|
|
|
|
std::string ErrorMessage;
|
2017-10-29 07:24:00 +08:00
|
|
|
switch(MK) {
|
|
|
|
case MK_FunCall:
|
|
|
|
ErrorMessage = "Method call on a 'moved-from' object";
|
|
|
|
break;
|
|
|
|
case MK_Copy:
|
|
|
|
ErrorMessage = "Copying a 'moved-from' object";
|
|
|
|
break;
|
|
|
|
case MK_Move:
|
|
|
|
ErrorMessage = "Moving a 'moved-from' object";
|
|
|
|
break;
|
|
|
|
}
|
2017-03-24 17:52:30 +08:00
|
|
|
if (const auto DecReg = Region->getAs<DeclRegion>()) {
|
|
|
|
const auto *RegionDecl = dyn_cast<NamedDecl>(DecReg->getDecl());
|
|
|
|
ErrorMessage += " '" + RegionDecl->getNameAsString() + "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto R =
|
|
|
|
llvm::make_unique<BugReport>(*BT, ErrorMessage, N, LocUsedForUniqueing,
|
|
|
|
MoveNode->getLocationContext()->getDecl());
|
|
|
|
R->addVisitor(llvm::make_unique<MovedBugVisitor>(Region));
|
|
|
|
C.emitReport(std::move(R));
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
void MoveChecker::checkPostCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
2017-03-24 17:52:30 +08:00
|
|
|
const auto *AFC = dyn_cast<AnyFunctionCall>(&Call);
|
|
|
|
if (!AFC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
const auto MethodDecl = dyn_cast_or_null<CXXMethodDecl>(AFC->getDecl());
|
|
|
|
if (!MethodDecl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto *ConstructorDecl = dyn_cast<CXXConstructorDecl>(MethodDecl);
|
|
|
|
|
|
|
|
const auto *CC = dyn_cast_or_null<CXXConstructorCall>(&Call);
|
|
|
|
// Check if an object became moved-from.
|
|
|
|
// Object can become moved from after a call to move assignment operator or
|
|
|
|
// move constructor .
|
|
|
|
if (ConstructorDecl && !ConstructorDecl->isMoveConstructor())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!ConstructorDecl && !MethodDecl->isMoveAssignmentOperator())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto ArgRegion = AFC->getArgSVal(0).getAsRegion();
|
|
|
|
if (!ArgRegion)
|
|
|
|
return;
|
|
|
|
|
2018-12-04 07:06:07 +08:00
|
|
|
// In non-aggressive mode, only warn on use-after-move of local variables (or
|
|
|
|
// local rvalue references) and of STL objects. The former is possible because
|
|
|
|
// local variables (or local rvalue references) are not tempting their user to
|
|
|
|
// re-use the storage. The latter is possible because STL objects are known
|
|
|
|
// to end up in a valid but unspecified state after the move and their
|
|
|
|
// state-reset methods are also known, which allows us to predict
|
|
|
|
// precisely when use-after-move is invalid.
|
|
|
|
// In aggressive mode, warn on any use-after-move because the user
|
|
|
|
// has intentionally asked us to completely eliminate use-after-move
|
|
|
|
// in his code.
|
|
|
|
ObjectKind OK = classifyObject(ArgRegion, MethodDecl->getParent());
|
|
|
|
if (!IsAggressive && !OK.Local && !OK.STL)
|
|
|
|
return;
|
|
|
|
|
2017-03-24 17:52:30 +08:00
|
|
|
// Skip moving the object to itself.
|
|
|
|
if (CC && CC->getCXXThisVal().getAsRegion() == ArgRegion)
|
|
|
|
return;
|
|
|
|
if (const auto *IC = dyn_cast<CXXInstanceCall>(AFC))
|
|
|
|
if (IC->getCXXThisVal().getAsRegion() == ArgRegion)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const MemRegion *BaseRegion = ArgRegion->getBaseRegion();
|
|
|
|
// Skip temp objects because of their short lifetime.
|
|
|
|
if (BaseRegion->getAs<CXXTempObjectRegion>() ||
|
|
|
|
AFC->getArgExpr(0)->isRValue())
|
|
|
|
return;
|
|
|
|
// If it has already been reported do not need to modify the state.
|
|
|
|
|
|
|
|
if (State->get<TrackedRegionMap>(ArgRegion))
|
|
|
|
return;
|
|
|
|
// Mark object as moved-from.
|
|
|
|
State = State->set<TrackedRegionMap>(ArgRegion, RegionState::getMoved());
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
bool MoveChecker::isMoveSafeMethod(const CXXMethodDecl *MethodDec) const {
|
2017-03-24 17:52:30 +08:00
|
|
|
// We abandon the cases where bool/void/void* conversion happens.
|
|
|
|
if (const auto *ConversionDec =
|
|
|
|
dyn_cast_or_null<CXXConversionDecl>(MethodDec)) {
|
|
|
|
const Type *Tp = ConversionDec->getConversionType().getTypePtrOrNull();
|
|
|
|
if (!Tp)
|
|
|
|
return false;
|
|
|
|
if (Tp->isBooleanType() || Tp->isVoidType() || Tp->isVoidPointerType())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Function call `empty` can be skipped.
|
2018-10-09 15:28:57 +08:00
|
|
|
return (MethodDec && MethodDec->getDeclName().isIdentifier() &&
|
2017-03-24 17:52:30 +08:00
|
|
|
(MethodDec->getName().lower() == "empty" ||
|
2018-10-09 15:28:57 +08:00
|
|
|
MethodDec->getName().lower() == "isempty"));
|
2017-03-24 17:52:30 +08:00
|
|
|
}
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
bool MoveChecker::isStateResetMethod(const CXXMethodDecl *MethodDec) const {
|
2018-10-09 15:28:57 +08:00
|
|
|
if (!MethodDec)
|
|
|
|
return false;
|
|
|
|
if (MethodDec->hasAttr<ReinitializesAttr>())
|
|
|
|
return true;
|
|
|
|
if (MethodDec->getDeclName().isIdentifier()) {
|
2017-03-24 17:52:30 +08:00
|
|
|
std::string MethodName = MethodDec->getName().lower();
|
|
|
|
if (MethodName == "reset" || MethodName == "clear" ||
|
|
|
|
MethodName == "destroy")
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't report an error inside a move related operation.
|
|
|
|
// We assume that the programmer knows what she does.
|
2018-12-04 06:32:32 +08:00
|
|
|
bool MoveChecker::isInMoveSafeContext(const LocationContext *LC) const {
|
2017-03-24 17:52:30 +08:00
|
|
|
do {
|
|
|
|
const auto *CtxDec = LC->getDecl();
|
|
|
|
auto *CtorDec = dyn_cast_or_null<CXXConstructorDecl>(CtxDec);
|
|
|
|
auto *DtorDec = dyn_cast_or_null<CXXDestructorDecl>(CtxDec);
|
|
|
|
auto *MethodDec = dyn_cast_or_null<CXXMethodDecl>(CtxDec);
|
|
|
|
if (DtorDec || (CtorDec && CtorDec->isCopyOrMoveConstructor()) ||
|
|
|
|
(MethodDec && MethodDec->isOverloadedOperator() &&
|
|
|
|
MethodDec->getOverloadedOperator() == OO_Equal) ||
|
|
|
|
isStateResetMethod(MethodDec) || isMoveSafeMethod(MethodDec))
|
|
|
|
return true;
|
|
|
|
} while ((LC = LC->getParent()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-04 07:06:07 +08:00
|
|
|
MoveChecker::ObjectKind MoveChecker::classifyObject(const MemRegion *MR,
|
|
|
|
const CXXRecordDecl *RD) {
|
|
|
|
MR = unwrapRValueReferenceIndirection(MR);
|
|
|
|
return {
|
|
|
|
/*Local=*/
|
|
|
|
MR && isa<VarRegion>(MR) && isa<StackSpaceRegion>(MR->getMemorySpace()),
|
|
|
|
/*STL=*/
|
|
|
|
RD && RD->getDeclContext()->isStdNamespace()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
void MoveChecker::checkPreCall(const CallEvent &Call, CheckerContext &C) const {
|
2017-03-24 17:52:30 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
const LocationContext *LC = C.getLocationContext();
|
|
|
|
ExplodedNode *N = nullptr;
|
|
|
|
|
2017-10-29 07:09:37 +08:00
|
|
|
// Remove the MemRegions from the map on which a ctor/dtor call or assignment
|
2017-03-24 17:52:30 +08:00
|
|
|
// happened.
|
|
|
|
|
|
|
|
// Checking constructor calls.
|
|
|
|
if (const auto *CC = dyn_cast<CXXConstructorCall>(&Call)) {
|
|
|
|
State = removeFromState(State, CC->getCXXThisVal().getAsRegion());
|
|
|
|
auto CtorDec = CC->getDecl();
|
|
|
|
// Check for copying a moved-from object and report the bug.
|
|
|
|
if (CtorDec && CtorDec->isCopyOrMoveConstructor()) {
|
|
|
|
const MemRegion *ArgRegion = CC->getArgSVal(0).getAsRegion();
|
|
|
|
const RegionState *ArgState = State->get<TrackedRegionMap>(ArgRegion);
|
|
|
|
if (ArgState && ArgState->isMoved()) {
|
|
|
|
if (!isInMoveSafeContext(LC)) {
|
2018-12-04 07:06:07 +08:00
|
|
|
const CXXRecordDecl *RD = CtorDec->getParent();
|
2017-10-29 07:24:00 +08:00
|
|
|
if(CtorDec->isMoveConstructor())
|
2018-12-04 07:06:07 +08:00
|
|
|
N = reportBug(ArgRegion, RD, C, MK_Move);
|
2017-10-29 07:24:00 +08:00
|
|
|
else
|
2018-12-04 07:06:07 +08:00
|
|
|
N = reportBug(ArgRegion, RD, C, MK_Copy);
|
2017-03-24 17:52:30 +08:00
|
|
|
State = State->set<TrackedRegionMap>(ArgRegion,
|
|
|
|
RegionState::getReported());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.addTransition(State, N);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto IC = dyn_cast<CXXInstanceCall>(&Call);
|
|
|
|
if (!IC)
|
|
|
|
return;
|
2018-12-04 06:44:16 +08:00
|
|
|
|
|
|
|
// Calling a destructor on a moved object is fine.
|
|
|
|
if (isa<CXXDestructorCall>(IC))
|
2017-10-29 07:09:37 +08:00
|
|
|
return;
|
|
|
|
|
2018-12-04 06:44:16 +08:00
|
|
|
const MemRegion *ThisRegion = IC->getCXXThisVal().getAsRegion();
|
|
|
|
if (!ThisRegion)
|
2017-03-24 17:52:30 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
const auto MethodDecl = dyn_cast_or_null<CXXMethodDecl>(IC->getDecl());
|
|
|
|
if (!MethodDecl)
|
|
|
|
return;
|
2018-12-04 06:44:16 +08:00
|
|
|
|
2018-12-04 07:06:07 +08:00
|
|
|
// Store class declaration as well, for bug reporting purposes.
|
|
|
|
const CXXRecordDecl *RD = MethodDecl->getParent();
|
|
|
|
|
2017-03-24 17:52:30 +08:00
|
|
|
// Checking assignment operators.
|
|
|
|
bool OperatorEq = MethodDecl->isOverloadedOperator() &&
|
|
|
|
MethodDecl->getOverloadedOperator() == OO_Equal;
|
|
|
|
// Remove the tracked object for every assignment operator, but report bug
|
|
|
|
// only for move or copy assignment's argument.
|
|
|
|
if (OperatorEq) {
|
|
|
|
State = removeFromState(State, ThisRegion);
|
|
|
|
if (MethodDecl->isCopyAssignmentOperator() ||
|
|
|
|
MethodDecl->isMoveAssignmentOperator()) {
|
|
|
|
const RegionState *ArgState =
|
|
|
|
State->get<TrackedRegionMap>(IC->getArgSVal(0).getAsRegion());
|
|
|
|
if (ArgState && ArgState->isMoved() && !isInMoveSafeContext(LC)) {
|
|
|
|
const MemRegion *ArgRegion = IC->getArgSVal(0).getAsRegion();
|
2017-10-29 07:24:00 +08:00
|
|
|
if(MethodDecl->isMoveAssignmentOperator())
|
2018-12-04 07:06:07 +08:00
|
|
|
N = reportBug(ArgRegion, RD, C, MK_Move);
|
2017-10-29 07:24:00 +08:00
|
|
|
else
|
2018-12-04 07:06:07 +08:00
|
|
|
N = reportBug(ArgRegion, RD, C, MK_Copy);
|
2017-03-24 17:52:30 +08:00
|
|
|
State =
|
|
|
|
State->set<TrackedRegionMap>(ArgRegion, RegionState::getReported());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.addTransition(State, N);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The remaining part is check only for method call on a moved-from object.
|
2017-10-29 07:09:37 +08:00
|
|
|
|
|
|
|
// We want to investigate the whole object, not only sub-object of a parent
|
|
|
|
// class in which the encountered method defined.
|
2018-10-09 15:28:57 +08:00
|
|
|
while (const auto *BR = dyn_cast<CXXBaseObjectRegion>(ThisRegion))
|
2017-10-29 07:09:37 +08:00
|
|
|
ThisRegion = BR->getSuperRegion();
|
|
|
|
|
2017-03-24 17:52:30 +08:00
|
|
|
if (isMoveSafeMethod(MethodDecl))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isStateResetMethod(MethodDecl)) {
|
2017-10-29 07:09:37 +08:00
|
|
|
State = removeFromState(State, ThisRegion);
|
2017-03-24 17:52:30 +08:00
|
|
|
C.addTransition(State);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-29 07:09:37 +08:00
|
|
|
// If it is already reported then we don't report the bug again.
|
2017-03-24 17:52:30 +08:00
|
|
|
const RegionState *ThisState = State->get<TrackedRegionMap>(ThisRegion);
|
|
|
|
if (!(ThisState && ThisState->isMoved()))
|
|
|
|
return;
|
|
|
|
|
2017-10-29 07:09:37 +08:00
|
|
|
// Don't report it in case if any base region is already reported
|
2017-03-24 17:52:30 +08:00
|
|
|
if (isAnyBaseRegionReported(State, ThisRegion))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isInMoveSafeContext(LC))
|
|
|
|
return;
|
|
|
|
|
2018-12-04 07:06:07 +08:00
|
|
|
N = reportBug(ThisRegion, RD, C, MK_FunCall);
|
2017-03-24 17:52:30 +08:00
|
|
|
State = State->set<TrackedRegionMap>(ThisRegion, RegionState::getReported());
|
|
|
|
C.addTransition(State, N);
|
|
|
|
}
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
void MoveChecker::checkDeadSymbols(SymbolReaper &SymReaper,
|
|
|
|
CheckerContext &C) const {
|
2017-03-24 17:52:30 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
TrackedRegionMapTy TrackedRegions = State->get<TrackedRegionMap>();
|
|
|
|
for (TrackedRegionMapTy::value_type E : TrackedRegions) {
|
|
|
|
const MemRegion *Region = E.first;
|
|
|
|
bool IsRegDead = !SymReaper.isLiveRegion(Region);
|
|
|
|
|
|
|
|
// Remove the dead regions from the region map.
|
|
|
|
if (IsRegDead) {
|
|
|
|
State = State->remove<TrackedRegionMap>(Region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
ProgramStateRef MoveChecker::checkRegionChanges(
|
2017-03-24 17:52:30 +08:00
|
|
|
ProgramStateRef State, const InvalidatedSymbols *Invalidated,
|
|
|
|
ArrayRef<const MemRegion *> ExplicitRegions,
|
|
|
|
ArrayRef<const MemRegion *> Regions, const LocationContext *LCtx,
|
|
|
|
const CallEvent *Call) const {
|
|
|
|
// In case of an InstanceCall don't remove the ThisRegion from the GDM since
|
|
|
|
// it is handled in checkPreCall and checkPostCall.
|
|
|
|
const MemRegion *ThisRegion = nullptr;
|
|
|
|
if (const auto *IC = dyn_cast_or_null<CXXInstanceCall>(Call)) {
|
|
|
|
ThisRegion = IC->getCXXThisVal().getAsRegion();
|
|
|
|
}
|
|
|
|
|
2018-10-09 15:28:57 +08:00
|
|
|
for (const auto *Region : ExplicitRegions) {
|
|
|
|
if (ThisRegion != Region)
|
2017-03-24 17:52:30 +08:00
|
|
|
State = removeFromState(State, Region);
|
|
|
|
}
|
|
|
|
|
|
|
|
return State;
|
|
|
|
}
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
void MoveChecker::printState(raw_ostream &Out, ProgramStateRef State,
|
|
|
|
const char *NL, const char *Sep) const {
|
2017-10-10 19:50:45 +08:00
|
|
|
|
|
|
|
TrackedRegionMapTy RS = State->get<TrackedRegionMap>();
|
|
|
|
|
|
|
|
if (!RS.isEmpty()) {
|
|
|
|
Out << Sep << "Moved-from objects :" << NL;
|
|
|
|
for (auto I: RS) {
|
|
|
|
I.first->dumpToStream(Out);
|
|
|
|
if (I.second.isMoved())
|
|
|
|
Out << ": moved";
|
|
|
|
else
|
|
|
|
Out << ": moved and reported";
|
|
|
|
Out << NL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 06:32:32 +08:00
|
|
|
void ento::registerMoveChecker(CheckerManager &mgr) {
|
2018-12-04 07:06:07 +08:00
|
|
|
MoveChecker *chk = mgr.registerChecker<MoveChecker>();
|
|
|
|
chk->setAggressiveness(mgr.getAnalyzerOptions().getCheckerBooleanOption(
|
|
|
|
"Aggressive", false, chk));
|
2017-03-24 17:52:30 +08:00
|
|
|
}
|