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
|
|
|
//
|
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
|
2017-03-24 17:52:30 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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 "clang/AST/ExprCXX.h"
|
2019-03-09 00:00:42 +08:00
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
[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"
|
2017-03-24 17:52:30 +08:00
|
|
|
#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"
|
2018-12-15 04:52:57 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2017-03-24 17:52:30 +08:00
|
|
|
|
|
|
|
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-17 14:19:32 +08:00
|
|
|
} // end of anonymous namespace
|
2017-03-24 17:52:30 +08:00
|
|
|
|
2018-12-17 14:19:32 +08:00
|
|
|
namespace {
|
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,
|
2018-12-15 04:47:58 +08:00
|
|
|
ArrayRef<const MemRegion *> RequestedRegions,
|
|
|
|
ArrayRef<const MemRegion *> InvalidatedRegions,
|
2017-03-24 17:52:30 +08:00
|
|
|
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-17 13:25:23 +08:00
|
|
|
enum MisuseKind { MK_FunCall, MK_Copy, MK_Move, MK_Dereference };
|
2018-12-18 05:07:38 +08:00
|
|
|
enum StdObjectKind { SK_NonStd, SK_Unsafe, SK_Safe, SK_SmartPtr };
|
2018-12-17 13:25:23 +08:00
|
|
|
|
2018-12-17 14:19:32 +08:00
|
|
|
enum AggressivenessKind { // In any case, don't warn after a reset.
|
|
|
|
AK_Invalid = -1,
|
|
|
|
AK_KnownsOnly = 0, // Warn only about known move-unsafe classes.
|
|
|
|
AK_KnownsAndLocals = 1, // Also warn about all local objects.
|
|
|
|
AK_All = 2, // Warn on any use-after-move.
|
|
|
|
AK_NumKinds = AK_All
|
|
|
|
};
|
|
|
|
|
2018-12-17 13:25:23 +08:00
|
|
|
static bool misuseCausesCrash(MisuseKind MK) {
|
|
|
|
return MK == MK_Dereference;
|
|
|
|
}
|
2018-12-04 07:06:07 +08:00
|
|
|
|
|
|
|
struct ObjectKind {
|
2018-12-17 13:25:23 +08:00
|
|
|
// Is this a local variable or a local rvalue reference?
|
2018-12-18 05:07:38 +08:00
|
|
|
bool IsLocal;
|
2018-12-17 13:25:23 +08:00
|
|
|
// Is this an STL object? If so, of what kind?
|
2018-12-18 05:07:38 +08:00
|
|
|
StdObjectKind StdKind;
|
2018-12-17 13:25:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// STL smart pointers are automatically re-initialized to null when moved
|
|
|
|
// from. So we can't warn on many methods, but we can warn when it is
|
|
|
|
// dereferenced, which is UB even if the resulting lvalue never gets read.
|
|
|
|
const llvm::StringSet<> StdSmartPtrClasses = {
|
|
|
|
"shared_ptr",
|
|
|
|
"unique_ptr",
|
|
|
|
"weak_ptr",
|
2018-12-04 07:06:07 +08:00
|
|
|
};
|
|
|
|
|
2018-12-15 04:52:57 +08:00
|
|
|
// Not all of these are entirely move-safe, but they do provide *some*
|
|
|
|
// guarantees, and it means that somebody is using them after move
|
|
|
|
// in a valid manner.
|
2018-12-17 13:25:23 +08:00
|
|
|
// TODO: We can still try to identify *unsafe* use after move,
|
|
|
|
// like we did with smart pointers.
|
|
|
|
const llvm::StringSet<> StdSafeClasses = {
|
2018-12-15 04:52:57 +08:00
|
|
|
"basic_filebuf",
|
|
|
|
"basic_ios",
|
|
|
|
"future",
|
|
|
|
"optional",
|
|
|
|
"packaged_task"
|
|
|
|
"promise",
|
|
|
|
"shared_future",
|
|
|
|
"shared_lock",
|
|
|
|
"thread",
|
|
|
|
"unique_lock",
|
|
|
|
};
|
|
|
|
|
2018-12-15 09:50:58 +08:00
|
|
|
// Should we bother tracking the state of the object?
|
|
|
|
bool shouldBeTracked(ObjectKind OK) const {
|
|
|
|
// 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
|
2018-12-17 13:25:23 +08:00
|
|
|
// predict precisely when use-after-move is invalid.
|
|
|
|
// Some STL objects are known to conform to additional contracts after move,
|
|
|
|
// so they are not tracked. However, smart pointers specifically are tracked
|
|
|
|
// because we can perform extra checking over them.
|
|
|
|
// 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.
|
2018-12-17 14:19:32 +08:00
|
|
|
return (Aggressiveness == AK_All) ||
|
|
|
|
(Aggressiveness >= AK_KnownsAndLocals && OK.IsLocal) ||
|
|
|
|
OK.StdKind == SK_Unsafe || OK.StdKind == SK_SmartPtr;
|
2018-12-17 13:25:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some objects only suffer from some kinds of misuses, but we need to track
|
|
|
|
// them anyway because we cannot know in advance what misuse will we find.
|
|
|
|
bool shouldWarnAbout(ObjectKind OK, MisuseKind MK) const {
|
|
|
|
// Additionally, only warn on smart pointers when they are dereferenced (or
|
|
|
|
// local or we are aggressive).
|
|
|
|
return shouldBeTracked(OK) &&
|
2018-12-17 14:19:32 +08:00
|
|
|
((Aggressiveness == AK_All) ||
|
|
|
|
(Aggressiveness >= AK_KnownsAndLocals && OK.IsLocal) ||
|
|
|
|
OK.StdKind != SK_SmartPtr || MK == MK_Dereference);
|
2018-12-15 09:50:58 +08:00
|
|
|
}
|
|
|
|
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
// Obtains ObjectKind of an object. Because class declaration cannot always
|
|
|
|
// be easily obtained from the memory region, it is supplied separately.
|
2018-12-15 04:52:57 +08:00
|
|
|
ObjectKind classifyObject(const MemRegion *MR, const CXXRecordDecl *RD) const;
|
2018-12-04 07:06:07 +08:00
|
|
|
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
// Classifies the object and dumps a user-friendly description string to
|
2018-12-17 13:25:23 +08:00
|
|
|
// the stream.
|
|
|
|
void explainObject(llvm::raw_ostream &OS, const MemRegion *MR,
|
|
|
|
const CXXRecordDecl *RD, MisuseKind MK) const;
|
2018-12-15 04:52:57 +08:00
|
|
|
|
2018-12-17 13:25:23 +08:00
|
|
|
bool belongsTo(const CXXRecordDecl *RD, const llvm::StringSet<> &Set) const;
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
|
[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:
|
2018-12-17 13:25:23 +08:00
|
|
|
MovedBugVisitor(const MoveChecker &Chk, const MemRegion *R,
|
|
|
|
const CXXRecordDecl *RD, MisuseKind MK)
|
|
|
|
: Chk(Chk), Region(R), RD(RD), MK(MK), Found(false) {}
|
2017-03-24 17:52:30 +08:00
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const override {
|
|
|
|
static int X = 0;
|
|
|
|
ID.AddPointer(&X);
|
|
|
|
ID.AddPointer(Region);
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
// Don't add RD because it's, in theory, uniquely determined by
|
|
|
|
// the region. In practice though, it's not always possible to obtain
|
|
|
|
// the declaration directly from the region, that's why we store it
|
|
|
|
// in the first place.
|
2017-03-24 17:52:30 +08:00
|
|
|
}
|
|
|
|
|
2019-08-14 00:45:48 +08:00
|
|
|
PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
|
|
|
|
BugReporterContext &BRC,
|
|
|
|
BugReport &BR) override;
|
2017-03-24 17:52:30 +08:00
|
|
|
|
|
|
|
private:
|
2018-12-15 04:52:57 +08:00
|
|
|
const MoveChecker &Chk;
|
2017-03-24 17:52:30 +08:00
|
|
|
// The tracked region.
|
|
|
|
const MemRegion *Region;
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
// The class of the tracked object.
|
|
|
|
const CXXRecordDecl *RD;
|
2018-12-17 13:25:23 +08:00
|
|
|
// How exactly the object was misused.
|
|
|
|
const MisuseKind MK;
|
2017-03-24 17:52:30 +08:00
|
|
|
bool Found;
|
|
|
|
};
|
|
|
|
|
2018-12-17 14:19:32 +08:00
|
|
|
AggressivenessKind Aggressiveness;
|
2018-12-04 07:06:07 +08:00
|
|
|
|
|
|
|
public:
|
2019-03-09 00:00:42 +08:00
|
|
|
void setAggressiveness(StringRef Str, CheckerManager &Mgr) {
|
2018-12-17 14:19:32 +08:00
|
|
|
Aggressiveness =
|
|
|
|
llvm::StringSwitch<AggressivenessKind>(Str)
|
|
|
|
.Case("KnownsOnly", AK_KnownsOnly)
|
|
|
|
.Case("KnownsAndLocals", AK_KnownsAndLocals)
|
|
|
|
.Case("All", AK_All)
|
2019-03-09 00:00:42 +08:00
|
|
|
.Default(AK_Invalid);
|
|
|
|
|
|
|
|
if (Aggressiveness == AK_Invalid)
|
|
|
|
Mgr.reportInvalidCheckerOptionValue(this, "WarnOn",
|
|
|
|
"either \"KnownsOnly\", \"KnownsAndLocals\" or \"All\" string value");
|
2018-12-17 14:19:32 +08:00
|
|
|
};
|
2018-12-04 07:06:07 +08:00
|
|
|
|
|
|
|
private:
|
2017-03-24 17:52:30 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT;
|
2018-12-15 09:50:58 +08:00
|
|
|
|
|
|
|
// Check if the given form of potential misuse of a given object
|
|
|
|
// should be reported. If so, get it reported. The callback from which
|
|
|
|
// this function was called should immediately return after the call
|
|
|
|
// because this function adds one or two transitions.
|
|
|
|
void modelUse(ProgramStateRef State, const MemRegion *Region,
|
|
|
|
const CXXRecordDecl *RD, MisuseKind MK,
|
|
|
|
CheckerContext &C) const;
|
|
|
|
|
|
|
|
// Returns the exploded node against which the report was emitted.
|
|
|
|
// The caller *must* add any further transitions against this node.
|
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;
|
2018-12-15 09:50:58 +08:00
|
|
|
|
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)
|
|
|
|
|
2019-04-23 10:45:42 +08:00
|
|
|
// Define the inter-checker API.
|
|
|
|
namespace clang {
|
|
|
|
namespace ento {
|
|
|
|
namespace move {
|
|
|
|
bool isMovedFrom(ProgramStateRef State, const MemRegion *Region) {
|
|
|
|
const RegionState *RS = State->get<TrackedRegionMap>(Region);
|
|
|
|
return RS && (RS->isMoved() || RS->isReported());
|
|
|
|
}
|
|
|
|
} // namespace move
|
|
|
|
} // namespace ento
|
|
|
|
} // namespace clang
|
|
|
|
|
2017-03-24 17:52:30 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2019-08-14 00:45:48 +08:00
|
|
|
PathDiagnosticPieceRef MoveChecker::MovedBugVisitor::VisitNode(
|
|
|
|
const ExplodedNode *N, BugReporterContext &BRC, BugReport &BR) {
|
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;
|
|
|
|
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
SmallString<128> Str;
|
|
|
|
llvm::raw_svector_ostream OS(Str);
|
|
|
|
|
2018-12-17 13:25:23 +08:00
|
|
|
ObjectKind OK = Chk.classifyObject(Region, RD);
|
|
|
|
switch (OK.StdKind) {
|
|
|
|
case SK_SmartPtr:
|
|
|
|
if (MK == MK_Dereference) {
|
|
|
|
OS << "Smart pointer";
|
|
|
|
Chk.explainObject(OS, Region, RD, MK);
|
|
|
|
OS << " is reset to null when moved from";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's not a dereference, we don't care if it was reset to null
|
|
|
|
// or that it is even a smart pointer.
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case SK_NonStd:
|
|
|
|
case SK_Safe:
|
|
|
|
OS << "Object";
|
|
|
|
Chk.explainObject(OS, Region, RD, MK);
|
|
|
|
OS << " is moved";
|
|
|
|
break;
|
|
|
|
case SK_Unsafe:
|
|
|
|
OS << "Object";
|
|
|
|
Chk.explainObject(OS, Region, RD, MK);
|
|
|
|
OS << " is left in a valid but unspecified state after move";
|
|
|
|
break;
|
|
|
|
}
|
2017-03-24 17:52:30 +08:00
|
|
|
|
|
|
|
// Generate the extra diagnostic.
|
|
|
|
PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
|
|
|
|
N->getLocationContext());
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true);
|
2018-12-17 13:25:23 +08:00
|
|
|
}
|
2017-03-24 17:52:30 +08:00
|
|
|
|
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-15 09:50:58 +08:00
|
|
|
void MoveChecker::modelUse(ProgramStateRef State, const MemRegion *Region,
|
|
|
|
const CXXRecordDecl *RD, MisuseKind MK,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
assert(!C.isDifferent() && "No transitions should have been made by now");
|
|
|
|
const RegionState *RS = State->get<TrackedRegionMap>(Region);
|
2018-12-17 13:25:23 +08:00
|
|
|
ObjectKind OK = classifyObject(Region, RD);
|
|
|
|
|
|
|
|
// Just in case: if it's not a smart pointer but it does have operator *,
|
|
|
|
// we shouldn't call the bug a dereference.
|
|
|
|
if (MK == MK_Dereference && OK.StdKind != SK_SmartPtr)
|
|
|
|
MK = MK_FunCall;
|
2018-12-15 09:50:58 +08:00
|
|
|
|
2018-12-17 13:25:23 +08:00
|
|
|
if (!RS || !shouldWarnAbout(OK, MK)
|
2018-12-15 09:50:58 +08:00
|
|
|
|| isInMoveSafeContext(C.getLocationContext())) {
|
|
|
|
// Finalize changes made by the caller.
|
|
|
|
C.addTransition(State);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-17 13:25:23 +08:00
|
|
|
// Don't report it in case if any base region is already reported.
|
|
|
|
// But still generate a sink in case of UB.
|
|
|
|
// And still finalize changes made by the caller.
|
|
|
|
if (isAnyBaseRegionReported(State, Region)) {
|
|
|
|
if (misuseCausesCrash(MK)) {
|
|
|
|
C.generateSink(State, C.getPredecessor());
|
|
|
|
} else {
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-15 09:50:58 +08:00
|
|
|
ExplodedNode *N = reportBug(Region, RD, C, MK);
|
|
|
|
|
2018-12-17 13:25:23 +08:00
|
|
|
// If the program has already crashed on this path, don't bother.
|
|
|
|
if (N->isSink())
|
|
|
|
return;
|
|
|
|
|
2018-12-15 09:50:58 +08:00
|
|
|
State = State->set<TrackedRegionMap>(Region, RegionState::getReported());
|
|
|
|
C.addTransition(State, N);
|
|
|
|
}
|
|
|
|
|
2018-12-04 06:32:32 +08:00
|
|
|
ExplodedNode *MoveChecker::reportBug(const MemRegion *Region,
|
2018-12-17 13:25:23 +08:00
|
|
|
const CXXRecordDecl *RD, CheckerContext &C,
|
2018-12-04 06:32:32 +08:00
|
|
|
MisuseKind MK) const {
|
2018-12-17 13:25:23 +08:00
|
|
|
if (ExplodedNode *N = misuseCausesCrash(MK) ? C.generateErrorNode()
|
|
|
|
: C.generateNonFatalErrorNode()) {
|
|
|
|
|
2017-03-24 17:52:30 +08:00
|
|
|
if (!BT)
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
BT.reset(new BugType(this, "Use-after-move",
|
2017-03-24 17:52:30 +08:00
|
|
|
"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.
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
llvm::SmallString<128> Str;
|
|
|
|
llvm::raw_svector_ostream OS(Str);
|
2017-10-29 07:24:00 +08:00
|
|
|
switch(MK) {
|
|
|
|
case MK_FunCall:
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
OS << "Method called on moved-from object";
|
2018-12-17 13:25:23 +08:00
|
|
|
explainObject(OS, Region, RD, MK);
|
2017-10-29 07:24:00 +08:00
|
|
|
break;
|
|
|
|
case MK_Copy:
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
OS << "Moved-from object";
|
2018-12-17 13:25:23 +08:00
|
|
|
explainObject(OS, Region, RD, MK);
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
OS << " is copied";
|
2017-10-29 07:24:00 +08:00
|
|
|
break;
|
|
|
|
case MK_Move:
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
OS << "Moved-from object";
|
2018-12-17 13:25:23 +08:00
|
|
|
explainObject(OS, Region, RD, MK);
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
OS << " is moved";
|
2017-10-29 07:24:00 +08:00
|
|
|
break;
|
2018-12-17 13:25:23 +08:00
|
|
|
case MK_Dereference:
|
|
|
|
OS << "Dereference of null smart pointer";
|
|
|
|
explainObject(OS, Region, RD, MK);
|
|
|
|
break;
|
2017-10-29 07:24:00 +08:00
|
|
|
}
|
2017-03-24 17:52:30 +08:00
|
|
|
|
|
|
|
auto R =
|
2019-08-15 07:04:18 +08:00
|
|
|
std::make_unique<BugReport>(*BT, OS.str(), N, LocUsedForUniqueing,
|
2017-03-24 17:52:30 +08:00
|
|
|
MoveNode->getLocationContext()->getDecl());
|
2019-08-15 07:04:18 +08:00
|
|
|
R->addVisitor(std::make_unique<MovedBugVisitor>(*this, Region, RD, MK));
|
2017-03-24 17:52:30 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
// Check if an object became moved-from.
|
|
|
|
// Object can become moved from after a call to move assignment operator or
|
|
|
|
// move constructor .
|
2018-12-15 09:50:58 +08:00
|
|
|
const auto *ConstructorDecl = dyn_cast<CXXConstructorDecl>(MethodDecl);
|
2017-03-24 17:52:30 +08:00
|
|
|
if (ConstructorDecl && !ConstructorDecl->isMoveConstructor())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!ConstructorDecl && !MethodDecl->isMoveAssignmentOperator())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto ArgRegion = AFC->getArgSVal(0).getAsRegion();
|
|
|
|
if (!ArgRegion)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Skip moving the object to itself.
|
2018-12-15 09:50:58 +08:00
|
|
|
const auto *CC = dyn_cast_or_null<CXXConstructorCall>(&Call);
|
2017-03-24 17:52:30 +08:00
|
|
|
if (CC && CC->getCXXThisVal().getAsRegion() == ArgRegion)
|
|
|
|
return;
|
2018-12-15 09:50:58 +08:00
|
|
|
|
2017-03-24 17:52:30 +08:00
|
|
|
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;
|
2018-12-15 09:50:58 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *RD = MethodDecl->getParent();
|
|
|
|
ObjectKind OK = classifyObject(ArgRegion, RD);
|
|
|
|
if (shouldBeTracked(OK)) {
|
|
|
|
// Mark object as moved-from.
|
|
|
|
State = State->set<TrackedRegionMap>(ArgRegion, RegionState::getMoved());
|
|
|
|
C.addTransition(State);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(!C.isDifferent() && "Should not have made transitions on this path!");
|
2017-03-24 17:52:30 +08:00
|
|
|
}
|
|
|
|
|
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();
|
2018-12-04 11:38:08 +08:00
|
|
|
// TODO: Some of these methods (eg., resize) are not always resetting
|
|
|
|
// the state, so we should consider looking at the arguments.
|
2019-01-18 08:16:25 +08:00
|
|
|
if (MethodName == "assign" || MethodName == "clear" ||
|
|
|
|
MethodName == "destroy" || MethodName == "reset" ||
|
|
|
|
MethodName == "resize" || MethodName == "shrink")
|
2017-03-24 17:52:30 +08:00
|
|
|
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-17 13:25:23 +08:00
|
|
|
bool MoveChecker::belongsTo(const CXXRecordDecl *RD,
|
|
|
|
const llvm::StringSet<> &Set) const {
|
2018-12-15 04:52:57 +08:00
|
|
|
const IdentifierInfo *II = RD->getIdentifier();
|
2018-12-17 13:25:23 +08:00
|
|
|
return II && Set.count(II->getName());
|
2018-12-15 04:52:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MoveChecker::ObjectKind
|
|
|
|
MoveChecker::classifyObject(const MemRegion *MR,
|
|
|
|
const CXXRecordDecl *RD) const {
|
|
|
|
// Local variables and local rvalue references are classified as "Local".
|
|
|
|
// For the purposes of this checker, we classify move-safe STL types
|
|
|
|
// as not-"STL" types, because that's how the checker treats them.
|
2018-12-04 07:06:07 +08:00
|
|
|
MR = unwrapRValueReferenceIndirection(MR);
|
2018-12-17 13:25:23 +08:00
|
|
|
bool IsLocal =
|
|
|
|
MR && isa<VarRegion>(MR) && isa<StackSpaceRegion>(MR->getMemorySpace());
|
|
|
|
|
|
|
|
if (!RD || !RD->getDeclContext()->isStdNamespace())
|
|
|
|
return { IsLocal, SK_NonStd };
|
|
|
|
|
|
|
|
if (belongsTo(RD, StdSmartPtrClasses))
|
|
|
|
return { IsLocal, SK_SmartPtr };
|
|
|
|
|
|
|
|
if (belongsTo(RD, StdSafeClasses))
|
|
|
|
return { IsLocal, SK_Safe };
|
|
|
|
|
|
|
|
return { IsLocal, SK_Unsafe };
|
2018-12-04 07:06:07 +08:00
|
|
|
}
|
|
|
|
|
2018-12-17 13:25:23 +08:00
|
|
|
void MoveChecker::explainObject(llvm::raw_ostream &OS, const MemRegion *MR,
|
|
|
|
const CXXRecordDecl *RD, MisuseKind MK) const {
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
// We may need a leading space every time we actually explain anything,
|
|
|
|
// and we never know if we are to explain anything until we try.
|
|
|
|
if (const auto DR =
|
|
|
|
dyn_cast_or_null<DeclRegion>(unwrapRValueReferenceIndirection(MR))) {
|
|
|
|
const auto *RegionDecl = cast<NamedDecl>(DR->getDecl());
|
|
|
|
OS << " '" << RegionDecl->getNameAsString() << "'";
|
|
|
|
}
|
2018-12-17 13:25:23 +08:00
|
|
|
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
ObjectKind OK = classifyObject(MR, RD);
|
2018-12-17 13:25:23 +08:00
|
|
|
switch (OK.StdKind) {
|
|
|
|
case SK_NonStd:
|
|
|
|
case SK_Safe:
|
|
|
|
break;
|
|
|
|
case SK_SmartPtr:
|
|
|
|
if (MK != MK_Dereference)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// We only care about the type if it's a dereference.
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case SK_Unsafe:
|
|
|
|
OS << " of type '" << RD->getQualifiedNameAsString() << "'";
|
|
|
|
break;
|
|
|
|
};
|
[analyzer] MoveChecker: Improve warning and note messages.
The warning piece traditionally describes the bug itself, i.e.
"The bug is a _____", eg. "Attempt to delete released memory",
"Resource leak", "Method call on a moved-from object".
Event pieces produced by the visitor are usually in a present tense, i.e.
"At this moment _____": "Memory is released", "File is closed",
"Object is moved".
Additionally, type information is added into the event pieces for STL objects
(in order to highlight that it is in fact an STL object), and the respective
event piece now mentions that the object is left in an unspecified state
after it was moved, which is a vital piece of information to understand the bug.
Differential Revision: https://reviews.llvm.org/D54560
llvm-svn: 348229
2018-12-04 10:00:29 +08:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
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();
|
2018-12-15 09:50:58 +08:00
|
|
|
const CXXRecordDecl *RD = CtorDec->getParent();
|
|
|
|
MisuseKind MK = CtorDec->isMoveConstructor() ? MK_Move : MK_Copy;
|
|
|
|
modelUse(State, ArgRegion, RD, MK, C);
|
|
|
|
return;
|
2017-03-24 17:52:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-12-15 09:50:58 +08:00
|
|
|
// The remaining part is check only for method call on a moved-from object.
|
2017-03-24 17:52:30 +08:00
|
|
|
const auto MethodDecl = dyn_cast_or_null<CXXMethodDecl>(IC->getDecl());
|
|
|
|
if (!MethodDecl)
|
|
|
|
return;
|
2018-12-04 06:44:16 +08:00
|
|
|
|
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-12-15 09:50:58 +08:00
|
|
|
ThisRegion = ThisRegion->getMostDerivedObjectRegion();
|
2017-03-24 17:52:30 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-12-15 09:50:58 +08:00
|
|
|
if (isMoveSafeMethod(MethodDecl))
|
2017-03-24 17:52:30 +08:00
|
|
|
return;
|
|
|
|
|
2018-12-15 09:50:58 +08:00
|
|
|
// Store class declaration as well, for bug reporting purposes.
|
|
|
|
const CXXRecordDecl *RD = MethodDecl->getParent();
|
2017-03-24 17:52:30 +08:00
|
|
|
|
2018-12-15 09:50:58 +08:00
|
|
|
if (MethodDecl->isOverloadedOperator()) {
|
|
|
|
OverloadedOperatorKind OOK = MethodDecl->getOverloadedOperator();
|
2017-03-24 17:52:30 +08:00
|
|
|
|
2018-12-15 09:50:58 +08:00
|
|
|
if (OOK == OO_Equal) {
|
|
|
|
// Remove the tracked object for every assignment operator, but report bug
|
|
|
|
// only for move or copy assignment's argument.
|
|
|
|
State = removeFromState(State, ThisRegion);
|
|
|
|
|
|
|
|
if (MethodDecl->isCopyAssignmentOperator() ||
|
|
|
|
MethodDecl->isMoveAssignmentOperator()) {
|
|
|
|
const MemRegion *ArgRegion = IC->getArgSVal(0).getAsRegion();
|
|
|
|
MisuseKind MK =
|
|
|
|
MethodDecl->isMoveAssignmentOperator() ? MK_Move : MK_Copy;
|
|
|
|
modelUse(State, ArgRegion, RD, MK, C);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
C.addTransition(State);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-17 13:25:23 +08:00
|
|
|
|
|
|
|
if (OOK == OO_Star || OOK == OO_Arrow) {
|
|
|
|
modelUse(State, ThisRegion, RD, MK_Dereference, C);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-15 09:50:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
modelUse(State, ThisRegion, RD, MK_FunCall, C);
|
2017-03-24 17:52:30 +08:00
|
|
|
}
|
|
|
|
|
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,
|
2018-12-15 04:47:58 +08:00
|
|
|
ArrayRef<const MemRegion *> RequestedRegions,
|
|
|
|
ArrayRef<const MemRegion *> InvalidatedRegions,
|
|
|
|
const LocationContext *LCtx, const CallEvent *Call) const {
|
|
|
|
if (Call) {
|
|
|
|
// Relax invalidation upon function calls: only invalidate parameters
|
|
|
|
// that are passed directly via non-const pointers or non-const references
|
|
|
|
// or rvalue references.
|
|
|
|
// In case of an InstanceCall don't invalidate the this-region since
|
|
|
|
// it is fully handled in checkPreCall and checkPostCall.
|
|
|
|
const MemRegion *ThisRegion = nullptr;
|
|
|
|
if (const auto *IC = dyn_cast<CXXInstanceCall>(Call))
|
|
|
|
ThisRegion = IC->getCXXThisVal().getAsRegion();
|
|
|
|
|
|
|
|
// Requested ("explicit") regions are the regions passed into the call
|
|
|
|
// directly, but not all of them end up being invalidated.
|
|
|
|
// But when they do, they appear in the InvalidatedRegions array as well.
|
|
|
|
for (const auto *Region : RequestedRegions) {
|
|
|
|
if (ThisRegion != Region) {
|
|
|
|
if (llvm::find(InvalidatedRegions, Region) !=
|
|
|
|
std::end(InvalidatedRegions)) {
|
|
|
|
State = removeFromState(State, Region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For invalidations that aren't caused by calls, assume nothing. In
|
|
|
|
// particular, direct write into an object's field invalidates the status.
|
|
|
|
for (const auto *Region : InvalidatedRegions)
|
|
|
|
State = removeFromState(State, Region->getBaseRegion());
|
2017-03-24 17:52:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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>();
|
2018-12-17 14:19:32 +08:00
|
|
|
chk->setAggressiveness(
|
[analyzer] Remove the default value arg from getChecker*Option
Since D57922, the config table contains every checker option, and it's default
value, so having it as an argument for getChecker*Option is redundant.
By the time any of the getChecker*Option function is called, we verified the
value in CheckerRegistry (after D57860), so we can confidently assert here, as
any irregularities detected at this point must be a programmer error. However,
in compatibility mode, verification won't happen, so the default value must be
restored.
This implies something else, other than adding removing one more potential point
of failure -- debug.ConfigDumper will always contain valid values for
checker/package options!
Differential Revision: https://reviews.llvm.org/D59195
llvm-svn: 361042
2019-05-17 23:52:13 +08:00
|
|
|
mgr.getAnalyzerOptions().getCheckerStringOption(chk, "WarnOn"), mgr);
|
2017-03-24 17:52:30 +08:00
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterMoveChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|