2016-08-22 19:21:30 +08:00
|
|
|
//== ValistChecker.cpp - stdarg.h macro usage checker -----------*- C++ -*--==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-08-22 19:21:30 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines checkers which detect usage of uninitialized va_list values
|
|
|
|
// and va_start calls with no matching va_end.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2016-08-22 19:21:30 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.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"
|
2016-08-22 19:21:30 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
REGISTER_SET_WITH_PROGRAMSTATE(InitializedVALists, const MemRegion *)
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
typedef SmallVector<const MemRegion *, 2> RegionVector;
|
|
|
|
|
|
|
|
class ValistChecker : public Checker<check::PreCall, check::PreStmt<VAArgExpr>,
|
|
|
|
check::DeadSymbols> {
|
|
|
|
mutable std::unique_ptr<BugType> BT_leakedvalist, BT_uninitaccess;
|
|
|
|
|
|
|
|
struct VAListAccepter {
|
|
|
|
CallDescription Func;
|
|
|
|
int VAListPos;
|
|
|
|
};
|
|
|
|
static const SmallVector<VAListAccepter, 15> VAListAccepters;
|
|
|
|
static const CallDescription VaStart, VaEnd, VaCopy;
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum CheckKind {
|
|
|
|
CK_Uninitialized,
|
|
|
|
CK_Unterminated,
|
|
|
|
CK_CopyToSelf,
|
|
|
|
CK_NumCheckKinds
|
|
|
|
};
|
|
|
|
|
2022-04-10 20:44:11 +08:00
|
|
|
bool ChecksEnabled[CK_NumCheckKinds] = {false};
|
2019-09-13 03:09:24 +08:00
|
|
|
CheckerNameRef CheckNames[CK_NumCheckKinds];
|
2016-08-22 19:21:30 +08:00
|
|
|
|
|
|
|
void checkPreStmt(const VAArgExpr *VAA, CheckerContext &C) const;
|
|
|
|
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
|
|
|
|
void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
|
|
|
|
|
|
|
|
private:
|
2017-03-08 00:04:23 +08:00
|
|
|
const MemRegion *getVAListAsRegion(SVal SV, const Expr *VAExpr,
|
|
|
|
bool &IsSymbolic, CheckerContext &C) const;
|
2016-08-22 19:21:30 +08:00
|
|
|
const ExplodedNode *getStartCallSite(const ExplodedNode *N,
|
2017-03-08 00:04:23 +08:00
|
|
|
const MemRegion *Reg) const;
|
2016-08-22 19:21:30 +08:00
|
|
|
|
|
|
|
void reportUninitializedAccess(const MemRegion *VAList, StringRef Msg,
|
|
|
|
CheckerContext &C) const;
|
|
|
|
void reportLeakedVALists(const RegionVector &LeakedVALists, StringRef Msg1,
|
|
|
|
StringRef Msg2, CheckerContext &C, ExplodedNode *N,
|
2018-01-06 18:51:00 +08:00
|
|
|
bool ReportUninit = false) const;
|
2016-08-22 19:21:30 +08:00
|
|
|
|
|
|
|
void checkVAListStartCall(const CallEvent &Call, CheckerContext &C,
|
|
|
|
bool IsCopy) const;
|
|
|
|
void checkVAListEndCall(const CallEvent &Call, CheckerContext &C) const;
|
|
|
|
|
[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 ValistBugVisitor : public BugReporterVisitor {
|
2016-08-22 19:21:30 +08:00
|
|
|
public:
|
|
|
|
ValistBugVisitor(const MemRegion *Reg, bool IsLeak = false)
|
|
|
|
: Reg(Reg), IsLeak(IsLeak) {}
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const override {
|
|
|
|
static int X = 0;
|
|
|
|
ID.AddPointer(&X);
|
|
|
|
ID.AddPointer(Reg);
|
|
|
|
}
|
2019-08-14 00:45:48 +08:00
|
|
|
PathDiagnosticPieceRef getEndPath(BugReporterContext &BRC,
|
|
|
|
const ExplodedNode *EndPathNode,
|
2019-09-10 04:34:40 +08:00
|
|
|
PathSensitiveBugReport &BR) override {
|
2016-08-22 19:21:30 +08:00
|
|
|
if (!IsLeak)
|
|
|
|
return nullptr;
|
|
|
|
|
2019-09-12 04:54:21 +08:00
|
|
|
PathDiagnosticLocation L = BR.getLocation();
|
2016-08-22 19:21:30 +08:00
|
|
|
// Do not add the statement itself as a range in case of leak.
|
2019-08-14 00:45:48 +08:00
|
|
|
return std::make_shared<PathDiagnosticEventPiece>(L, BR.getDescription(),
|
|
|
|
false);
|
2016-08-22 19:21:30 +08:00
|
|
|
}
|
2019-08-14 00:45:48 +08:00
|
|
|
PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
|
|
|
|
BugReporterContext &BRC,
|
2019-09-10 04:34:40 +08:00
|
|
|
PathSensitiveBugReport &BR) override;
|
2016-08-22 19:21:30 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const MemRegion *Reg;
|
|
|
|
bool IsLeak;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const SmallVector<ValistChecker::VAListAccepter, 15>
|
|
|
|
ValistChecker::VAListAccepters = {
|
|
|
|
{{"vfprintf", 3}, 2},
|
|
|
|
{{"vfscanf", 3}, 2},
|
|
|
|
{{"vprintf", 2}, 1},
|
|
|
|
{{"vscanf", 2}, 1},
|
|
|
|
{{"vsnprintf", 4}, 3},
|
|
|
|
{{"vsprintf", 3}, 2},
|
|
|
|
{{"vsscanf", 3}, 2},
|
|
|
|
{{"vfwprintf", 3}, 2},
|
|
|
|
{{"vfwscanf", 3}, 2},
|
|
|
|
{{"vwprintf", 2}, 1},
|
|
|
|
{{"vwscanf", 2}, 1},
|
|
|
|
{{"vswprintf", 4}, 3},
|
|
|
|
// vswprintf is the wide version of vsnprintf,
|
|
|
|
// vsprintf has no wide version
|
|
|
|
{{"vswscanf", 3}, 2}};
|
2019-09-07 04:55:24 +08:00
|
|
|
|
|
|
|
const CallDescription
|
|
|
|
ValistChecker::VaStart("__builtin_va_start", /*Args=*/2, /*Params=*/1),
|
2016-08-22 19:21:30 +08:00
|
|
|
ValistChecker::VaCopy("__builtin_va_copy", 2),
|
|
|
|
ValistChecker::VaEnd("__builtin_va_end", 1);
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void ValistChecker::checkPreCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
if (!Call.isGlobalCFunction())
|
|
|
|
return;
|
2021-11-20 01:32:13 +08:00
|
|
|
if (VaStart.matches(Call))
|
2016-08-22 19:21:30 +08:00
|
|
|
checkVAListStartCall(Call, C, false);
|
2021-11-20 01:32:13 +08:00
|
|
|
else if (VaCopy.matches(Call))
|
2016-08-22 19:21:30 +08:00
|
|
|
checkVAListStartCall(Call, C, true);
|
2021-11-20 01:32:13 +08:00
|
|
|
else if (VaEnd.matches(Call))
|
2016-08-22 19:21:30 +08:00
|
|
|
checkVAListEndCall(Call, C);
|
|
|
|
else {
|
|
|
|
for (auto FuncInfo : VAListAccepters) {
|
2021-11-20 01:32:13 +08:00
|
|
|
if (!FuncInfo.Func.matches(Call))
|
2016-08-22 19:21:30 +08:00
|
|
|
continue;
|
2017-03-08 00:04:23 +08:00
|
|
|
bool Symbolic;
|
2016-08-22 19:21:30 +08:00
|
|
|
const MemRegion *VAList =
|
2017-03-08 00:04:23 +08:00
|
|
|
getVAListAsRegion(Call.getArgSVal(FuncInfo.VAListPos),
|
|
|
|
Call.getArgExpr(FuncInfo.VAListPos), Symbolic, C);
|
2016-08-22 19:21:30 +08:00
|
|
|
if (!VAList)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (C.getState()->contains<InitializedVALists>(VAList))
|
|
|
|
return;
|
|
|
|
|
2017-03-08 00:04:23 +08:00
|
|
|
// We did not see va_start call, but the source of the region is unknown.
|
|
|
|
// Be conservative and assume the best.
|
|
|
|
if (Symbolic)
|
|
|
|
return;
|
|
|
|
|
2016-08-22 19:21:30 +08:00
|
|
|
SmallString<80> Errmsg("Function '");
|
|
|
|
Errmsg += FuncInfo.Func.getFunctionName();
|
|
|
|
Errmsg += "' is called with an uninitialized va_list argument";
|
|
|
|
reportUninitializedAccess(VAList, Errmsg.c_str(), C);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:04:23 +08:00
|
|
|
const MemRegion *ValistChecker::getVAListAsRegion(SVal SV, const Expr *E,
|
|
|
|
bool &IsSymbolic,
|
|
|
|
CheckerContext &C) const {
|
2017-03-13 20:48:26 +08:00
|
|
|
const MemRegion *Reg = SV.getAsRegion();
|
|
|
|
if (!Reg)
|
2017-03-08 00:04:23 +08:00
|
|
|
return nullptr;
|
|
|
|
// TODO: In the future this should be abstracted away by the analyzer.
|
|
|
|
bool VaListModelledAsArray = false;
|
|
|
|
if (const auto *Cast = dyn_cast<CastExpr>(E)) {
|
|
|
|
QualType Ty = Cast->getType();
|
|
|
|
VaListModelledAsArray =
|
|
|
|
Ty->isPointerType() && Ty->getPointeeType()->isRecordType();
|
|
|
|
}
|
|
|
|
if (const auto *DeclReg = Reg->getAs<DeclRegion>()) {
|
|
|
|
if (isa<ParmVarDecl>(DeclReg->getDecl()))
|
|
|
|
Reg = C.getState()->getSVal(SV.castAs<Loc>()).getAsRegion();
|
|
|
|
}
|
2022-04-26 14:49:05 +08:00
|
|
|
IsSymbolic = Reg && Reg->getBaseRegion()->getAs<SymbolicRegion>();
|
2017-03-08 00:04:23 +08:00
|
|
|
// Some VarRegion based VA lists reach here as ElementRegions.
|
|
|
|
const auto *EReg = dyn_cast_or_null<ElementRegion>(Reg);
|
|
|
|
return (EReg && VaListModelledAsArray) ? EReg->getSuperRegion() : Reg;
|
|
|
|
}
|
|
|
|
|
2016-08-22 19:21:30 +08:00
|
|
|
void ValistChecker::checkPreStmt(const VAArgExpr *VAA,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
ProgramStateRef State = C.getState();
|
2017-03-08 00:04:23 +08:00
|
|
|
const Expr *VASubExpr = VAA->getSubExpr();
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal VAListSVal = C.getSVal(VASubExpr);
|
2017-03-08 00:04:23 +08:00
|
|
|
bool Symbolic;
|
|
|
|
const MemRegion *VAList =
|
|
|
|
getVAListAsRegion(VAListSVal, VASubExpr, Symbolic, C);
|
2016-08-22 19:21:30 +08:00
|
|
|
if (!VAList)
|
|
|
|
return;
|
2017-03-08 00:04:23 +08:00
|
|
|
if (Symbolic)
|
|
|
|
return;
|
2016-08-22 19:21:30 +08:00
|
|
|
if (!State->contains<InitializedVALists>(VAList))
|
|
|
|
reportUninitializedAccess(
|
|
|
|
VAList, "va_arg() is called on an uninitialized va_list", C);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValistChecker::checkDeadSymbols(SymbolReaper &SR,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
InitializedVAListsTy TrackedVALists = State->get<InitializedVALists>();
|
|
|
|
RegionVector LeakedVALists;
|
|
|
|
for (auto Reg : TrackedVALists) {
|
|
|
|
if (SR.isLiveRegion(Reg))
|
|
|
|
continue;
|
|
|
|
LeakedVALists.push_back(Reg);
|
|
|
|
State = State->remove<InitializedVALists>(Reg);
|
|
|
|
}
|
|
|
|
if (ExplodedNode *N = C.addTransition(State))
|
|
|
|
reportLeakedVALists(LeakedVALists, "Initialized va_list", " is leaked", C,
|
|
|
|
N);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function traverses the exploded graph backwards and finds the node where
|
|
|
|
// the va_list is initialized. That node is used for uniquing the bug paths.
|
|
|
|
// It is not likely that there are several different va_lists that belongs to
|
|
|
|
// different stack frames, so that case is not yet handled.
|
2017-03-08 00:04:23 +08:00
|
|
|
const ExplodedNode *
|
|
|
|
ValistChecker::getStartCallSite(const ExplodedNode *N,
|
|
|
|
const MemRegion *Reg) const {
|
2016-08-22 19:21:30 +08:00
|
|
|
const LocationContext *LeakContext = N->getLocationContext();
|
|
|
|
const ExplodedNode *StartCallNode = N;
|
|
|
|
|
|
|
|
bool FoundInitializedState = false;
|
|
|
|
|
|
|
|
while (N) {
|
|
|
|
ProgramStateRef State = N->getState();
|
|
|
|
if (!State->contains<InitializedVALists>(Reg)) {
|
|
|
|
if (FoundInitializedState)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
FoundInitializedState = true;
|
|
|
|
}
|
|
|
|
const LocationContext *NContext = N->getLocationContext();
|
|
|
|
if (NContext == LeakContext || NContext->isParentOf(LeakContext))
|
|
|
|
StartCallNode = N;
|
|
|
|
N = N->pred_empty() ? nullptr : *(N->pred_begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
return StartCallNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValistChecker::reportUninitializedAccess(const MemRegion *VAList,
|
|
|
|
StringRef Msg,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
if (!ChecksEnabled[CK_Uninitialized])
|
|
|
|
return;
|
|
|
|
if (ExplodedNode *N = C.generateErrorNode()) {
|
|
|
|
if (!BT_uninitaccess)
|
|
|
|
BT_uninitaccess.reset(new BugType(CheckNames[CK_Uninitialized],
|
|
|
|
"Uninitialized va_list",
|
2017-05-03 19:47:13 +08:00
|
|
|
categories::MemoryError));
|
2019-09-10 04:34:40 +08:00
|
|
|
auto R = std::make_unique<PathSensitiveBugReport>(*BT_uninitaccess, Msg, N);
|
2016-08-22 19:21:30 +08:00
|
|
|
R->markInteresting(VAList);
|
2019-08-15 07:04:18 +08:00
|
|
|
R->addVisitor(std::make_unique<ValistBugVisitor>(VAList));
|
2016-08-22 19:21:30 +08:00
|
|
|
C.emitReport(std::move(R));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValistChecker::reportLeakedVALists(const RegionVector &LeakedVALists,
|
|
|
|
StringRef Msg1, StringRef Msg2,
|
|
|
|
CheckerContext &C, ExplodedNode *N,
|
2018-01-06 18:51:00 +08:00
|
|
|
bool ReportUninit) const {
|
2016-08-22 19:21:30 +08:00
|
|
|
if (!(ChecksEnabled[CK_Unterminated] ||
|
2018-01-06 18:51:00 +08:00
|
|
|
(ChecksEnabled[CK_Uninitialized] && ReportUninit)))
|
2016-08-22 19:21:30 +08:00
|
|
|
return;
|
|
|
|
for (auto Reg : LeakedVALists) {
|
|
|
|
if (!BT_leakedvalist) {
|
2018-01-06 18:51:00 +08:00
|
|
|
// FIXME: maybe creating a new check name for this type of bug is a better
|
|
|
|
// solution.
|
|
|
|
BT_leakedvalist.reset(
|
|
|
|
new BugType(CheckNames[CK_Unterminated].getName().empty()
|
|
|
|
? CheckNames[CK_Uninitialized]
|
|
|
|
: CheckNames[CK_Unterminated],
|
2019-01-19 03:24:55 +08:00
|
|
|
"Leaked va_list", categories::MemoryError,
|
|
|
|
/*SuppressOnSink=*/true));
|
2016-08-22 19:21:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-08 00:04:23 +08:00
|
|
|
const ExplodedNode *StartNode = getStartCallSite(N, Reg);
|
2016-08-22 19:21:30 +08:00
|
|
|
PathDiagnosticLocation LocUsedForUniqueing;
|
|
|
|
|
2019-09-12 04:54:21 +08:00
|
|
|
if (const Stmt *StartCallStmt = StartNode->getStmtForDiagnostics())
|
2016-08-22 19:21:30 +08:00
|
|
|
LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
|
|
|
|
StartCallStmt, C.getSourceManager(), StartNode->getLocationContext());
|
|
|
|
|
|
|
|
SmallString<100> Buf;
|
|
|
|
llvm::raw_svector_ostream OS(Buf);
|
|
|
|
OS << Msg1;
|
|
|
|
std::string VariableName = Reg->getDescriptiveName();
|
|
|
|
if (!VariableName.empty())
|
|
|
|
OS << " " << VariableName;
|
|
|
|
OS << Msg2;
|
|
|
|
|
2019-09-10 04:34:40 +08:00
|
|
|
auto R = std::make_unique<PathSensitiveBugReport>(
|
2016-08-22 19:21:30 +08:00
|
|
|
*BT_leakedvalist, OS.str(), N, LocUsedForUniqueing,
|
|
|
|
StartNode->getLocationContext()->getDecl());
|
|
|
|
R->markInteresting(Reg);
|
2019-08-15 07:04:18 +08:00
|
|
|
R->addVisitor(std::make_unique<ValistBugVisitor>(Reg, true));
|
2016-08-22 19:21:30 +08:00
|
|
|
C.emitReport(std::move(R));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValistChecker::checkVAListStartCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C, bool IsCopy) const {
|
2017-03-08 00:04:23 +08:00
|
|
|
bool Symbolic;
|
|
|
|
const MemRegion *VAList =
|
|
|
|
getVAListAsRegion(Call.getArgSVal(0), Call.getArgExpr(0), Symbolic, C);
|
2016-08-22 19:21:30 +08:00
|
|
|
if (!VAList)
|
|
|
|
return;
|
|
|
|
|
2017-03-08 00:04:23 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
|
2016-08-22 19:21:30 +08:00
|
|
|
if (IsCopy) {
|
2017-03-08 00:04:23 +08:00
|
|
|
const MemRegion *Arg2 =
|
|
|
|
getVAListAsRegion(Call.getArgSVal(1), Call.getArgExpr(1), Symbolic, C);
|
2016-08-22 19:21:30 +08:00
|
|
|
if (Arg2) {
|
|
|
|
if (ChecksEnabled[CK_CopyToSelf] && VAList == Arg2) {
|
|
|
|
RegionVector LeakedVALists{VAList};
|
|
|
|
if (ExplodedNode *N = C.addTransition(State))
|
|
|
|
reportLeakedVALists(LeakedVALists, "va_list",
|
|
|
|
" is copied onto itself", C, N, true);
|
|
|
|
return;
|
2017-03-08 00:04:23 +08:00
|
|
|
} else if (!State->contains<InitializedVALists>(Arg2) && !Symbolic) {
|
2016-08-22 19:21:30 +08:00
|
|
|
if (State->contains<InitializedVALists>(VAList)) {
|
|
|
|
State = State->remove<InitializedVALists>(VAList);
|
|
|
|
RegionVector LeakedVALists{VAList};
|
|
|
|
if (ExplodedNode *N = C.addTransition(State))
|
|
|
|
reportLeakedVALists(LeakedVALists, "Initialized va_list",
|
|
|
|
" is overwritten by an uninitialized one", C, N,
|
|
|
|
true);
|
|
|
|
} else {
|
|
|
|
reportUninitializedAccess(Arg2, "Uninitialized va_list is copied", C);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (State->contains<InitializedVALists>(VAList)) {
|
|
|
|
RegionVector LeakedVALists{VAList};
|
|
|
|
if (ExplodedNode *N = C.addTransition(State))
|
|
|
|
reportLeakedVALists(LeakedVALists, "Initialized va_list",
|
|
|
|
" is initialized again", C, N);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
State = State->add<InitializedVALists>(VAList);
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValistChecker::checkVAListEndCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
2017-03-08 00:04:23 +08:00
|
|
|
bool Symbolic;
|
|
|
|
const MemRegion *VAList =
|
|
|
|
getVAListAsRegion(Call.getArgSVal(0), Call.getArgExpr(0), Symbolic, C);
|
2016-08-22 19:21:30 +08:00
|
|
|
if (!VAList)
|
|
|
|
return;
|
|
|
|
|
2017-03-08 00:04:23 +08:00
|
|
|
// We did not see va_start call, but the source of the region is unknown.
|
|
|
|
// Be conservative and assume the best.
|
|
|
|
if (Symbolic)
|
|
|
|
return;
|
|
|
|
|
2016-08-22 19:21:30 +08:00
|
|
|
if (!C.getState()->contains<InitializedVALists>(VAList)) {
|
|
|
|
reportUninitializedAccess(
|
|
|
|
VAList, "va_end() is called on an uninitialized va_list", C);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
State = State->remove<InitializedVALists>(VAList);
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
2019-08-14 00:45:48 +08:00
|
|
|
PathDiagnosticPieceRef ValistChecker::ValistBugVisitor::VisitNode(
|
2019-09-10 04:34:40 +08:00
|
|
|
const ExplodedNode *N, BugReporterContext &BRC, PathSensitiveBugReport &) {
|
2016-08-22 19:21:30 +08:00
|
|
|
ProgramStateRef State = N->getState();
|
2018-09-29 02:49:41 +08:00
|
|
|
ProgramStateRef StatePrev = N->getFirstPred()->getState();
|
2016-08-22 19:21:30 +08:00
|
|
|
|
2019-09-12 04:54:21 +08:00
|
|
|
const Stmt *S = N->getStmtForDiagnostics();
|
2016-08-22 19:21:30 +08:00
|
|
|
if (!S)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
StringRef Msg;
|
|
|
|
if (State->contains<InitializedVALists>(Reg) &&
|
|
|
|
!StatePrev->contains<InitializedVALists>(Reg))
|
|
|
|
Msg = "Initialized va_list";
|
|
|
|
else if (!State->contains<InitializedVALists>(Reg) &&
|
|
|
|
StatePrev->contains<InitializedVALists>(Reg))
|
|
|
|
Msg = "Ended va_list";
|
|
|
|
|
|
|
|
if (Msg.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
|
|
|
|
N->getLocationContext());
|
2017-01-06 01:26:53 +08:00
|
|
|
return std::make_shared<PathDiagnosticEventPiece>(Pos, Msg, true);
|
2016-08-22 19:21:30 +08:00
|
|
|
}
|
|
|
|
|
[analyzer] Reimplement dependencies between checkers
Unfortunately, up until now, the fact that certain checkers depended on one
another was known, but how these actually unfolded was hidden deep within the
implementation. For example, many checkers (like RetainCount, Malloc or CString)
modelled a certain functionality, and exposed certain reportable bug types to
the user. For example, while MallocChecker models many many different types of
memory handling, the actual "unix.MallocChecker" checker the user was exposed to
was merely and option to this modeling part.
Other than this being an ugly mess, this issue made resolving the checker naming
issue almost impossible. (The checker naming issue being that if a checker
registered more than one checker within its registry function, both checker
object recieved the same name) Also, if the user explicitly disabled a checker
that was a dependency of another that _was_ explicitly enabled, it implicitly,
without "telling" the user, reenabled it.
Clearly, changing this to a well structured, declarative form, where the
handling of dependencies are done on a higher level is very much preferred.
This patch, among the detailed things later, makes checkers declare their
dependencies within the TableGen file Checkers.td, and exposes the same
functionality to plugins and statically linked non-generated checkers through
CheckerRegistry::addDependency. CheckerRegistry now resolves these dependencies,
makes sure that checkers are added to CheckerManager in the correct order,
and makes sure that if a dependency is disabled, so will be every checker that
depends on it.
In detail:
* Add a new field to the Checker class in CheckerBase.td called Dependencies,
which is a list of Checkers.
* Move unix checkers before cplusplus, as there is no forward declaration in
tblgen :/
* Add the following new checkers:
- StackAddrEscapeBase
- StackAddrEscapeBase
- CStringModeling
- DynamicMemoryModeling (base of the MallocChecker family)
- IteratorModeling (base of the IteratorChecker family)
- ValistBase
- SecuritySyntaxChecker (base of bcmp, bcopy, etc...)
- NSOrCFErrorDerefChecker (base of NSErrorChecker and CFErrorChecker)
- IvarInvalidationModeling (base of IvarInvalidation checker family)
- RetainCountBase (base of RetainCount and OSObjectRetainCount)
* Clear up and registry functions in MallocChecker, happily remove old FIXMEs.
* Add a new addDependency function to CheckerRegistry.
* Neatly format RUN lines in files I looked at while debugging.
Big thanks to Artem Degrachev for all the guidance through this project!
Differential Revision: https://reviews.llvm.org/D54438
llvm-svn: 352287
2019-01-27 04:06:54 +08:00
|
|
|
void ento::registerValistBase(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ValistChecker>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterValistBase(const CheckerManager &mgr) {
|
[analyzer] Reimplement dependencies between checkers
Unfortunately, up until now, the fact that certain checkers depended on one
another was known, but how these actually unfolded was hidden deep within the
implementation. For example, many checkers (like RetainCount, Malloc or CString)
modelled a certain functionality, and exposed certain reportable bug types to
the user. For example, while MallocChecker models many many different types of
memory handling, the actual "unix.MallocChecker" checker the user was exposed to
was merely and option to this modeling part.
Other than this being an ugly mess, this issue made resolving the checker naming
issue almost impossible. (The checker naming issue being that if a checker
registered more than one checker within its registry function, both checker
object recieved the same name) Also, if the user explicitly disabled a checker
that was a dependency of another that _was_ explicitly enabled, it implicitly,
without "telling" the user, reenabled it.
Clearly, changing this to a well structured, declarative form, where the
handling of dependencies are done on a higher level is very much preferred.
This patch, among the detailed things later, makes checkers declare their
dependencies within the TableGen file Checkers.td, and exposes the same
functionality to plugins and statically linked non-generated checkers through
CheckerRegistry::addDependency. CheckerRegistry now resolves these dependencies,
makes sure that checkers are added to CheckerManager in the correct order,
and makes sure that if a dependency is disabled, so will be every checker that
depends on it.
In detail:
* Add a new field to the Checker class in CheckerBase.td called Dependencies,
which is a list of Checkers.
* Move unix checkers before cplusplus, as there is no forward declaration in
tblgen :/
* Add the following new checkers:
- StackAddrEscapeBase
- StackAddrEscapeBase
- CStringModeling
- DynamicMemoryModeling (base of the MallocChecker family)
- IteratorModeling (base of the IteratorChecker family)
- ValistBase
- SecuritySyntaxChecker (base of bcmp, bcopy, etc...)
- NSOrCFErrorDerefChecker (base of NSErrorChecker and CFErrorChecker)
- IvarInvalidationModeling (base of IvarInvalidation checker family)
- RetainCountBase (base of RetainCount and OSObjectRetainCount)
* Clear up and registry functions in MallocChecker, happily remove old FIXMEs.
* Add a new addDependency function to CheckerRegistry.
* Neatly format RUN lines in files I looked at while debugging.
Big thanks to Artem Degrachev for all the guidance through this project!
Differential Revision: https://reviews.llvm.org/D54438
llvm-svn: 352287
2019-01-27 04:06:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-22 19:21:30 +08:00
|
|
|
#define REGISTER_CHECKER(name) \
|
|
|
|
void ento::register##name##Checker(CheckerManager &mgr) { \
|
2019-01-27 05:41:50 +08:00
|
|
|
ValistChecker *checker = mgr.getChecker<ValistChecker>(); \
|
2016-08-22 19:21:30 +08:00
|
|
|
checker->ChecksEnabled[ValistChecker::CK_##name] = true; \
|
2019-09-13 03:09:24 +08:00
|
|
|
checker->CheckNames[ValistChecker::CK_##name] = \
|
|
|
|
mgr.getCurrentCheckerName(); \
|
2019-01-26 22:23:08 +08:00
|
|
|
} \
|
|
|
|
\
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegister##name##Checker(const CheckerManager &mgr) { \
|
2019-01-26 22:23:08 +08:00
|
|
|
return true; \
|
2016-08-22 19:21:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_CHECKER(Uninitialized)
|
|
|
|
REGISTER_CHECKER(Unterminated)
|
|
|
|
REGISTER_CHECKER(CopyToSelf)
|