2009-11-04 12:24:16 +08:00
|
|
|
//===--- UndefinedAssignmentChecker.h ---------------------------*- 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
|
2009-11-04 12:24:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-02-28 09:27:37 +08:00
|
|
|
// This defines UndefinedAssignmentChecker, a builtin check in ExprEngine that
|
2009-11-04 12:24:16 +08:00
|
|
|
// checks for assigning undefined values.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:27:37 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2009-11-04 12:24:16 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-04 12:24:16 +08:00
|
|
|
|
2009-11-22 20:29:52 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class UndefinedAssignmentChecker
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker<check::Bind> {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT;
|
2011-02-28 09:27:37 +08:00
|
|
|
|
2009-11-22 20:29:52 +08:00
|
|
|
public:
|
2011-10-06 08:43:15 +08:00
|
|
|
void checkBind(SVal location, SVal val, const Stmt *S,
|
|
|
|
CheckerContext &C) const;
|
2009-11-22 20:29:52 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:27:37 +08:00
|
|
|
void UndefinedAssignmentChecker::checkBind(SVal location, SVal val,
|
2011-10-06 08:43:15 +08:00
|
|
|
const Stmt *StoreE,
|
2011-02-28 09:27:37 +08:00
|
|
|
CheckerContext &C) const {
|
2009-11-04 12:24:16 +08:00
|
|
|
if (!val.isUndef())
|
|
|
|
return;
|
|
|
|
|
2013-06-19 07:16:15 +08:00
|
|
|
// Do not report assignments of uninitialized values inside swap functions.
|
|
|
|
// This should allow to swap partially uninitialized structs
|
|
|
|
// (radar://14129997)
|
|
|
|
if (const FunctionDecl *EnclosingFunctionDecl =
|
|
|
|
dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
|
|
|
|
if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
|
|
|
|
return;
|
|
|
|
|
2015-09-17 06:03:05 +08:00
|
|
|
ExplodedNode *N = C.generateErrorNode();
|
2009-11-04 12:24:16 +08:00
|
|
|
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
2018-02-28 06:05:55 +08:00
|
|
|
static const char *const DefaultMsg =
|
|
|
|
"Assigned value is garbage or undefined";
|
2009-11-04 12:24:16 +08:00
|
|
|
if (!BT)
|
2018-02-28 06:05:55 +08:00
|
|
|
BT.reset(new BuiltinBug(this, DefaultMsg));
|
2009-11-04 12:24:16 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2018-02-28 06:05:55 +08:00
|
|
|
llvm::SmallString<128> Str;
|
|
|
|
llvm::raw_svector_ostream OS(Str);
|
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
const Expr *ex = nullptr;
|
2009-11-04 12:24:16 +08:00
|
|
|
|
2010-09-02 08:56:20 +08:00
|
|
|
while (StoreE) {
|
2017-11-30 17:18:35 +08:00
|
|
|
if (const UnaryOperator *U = dyn_cast<UnaryOperator>(StoreE)) {
|
2018-02-28 06:05:55 +08:00
|
|
|
OS << "The expression is an uninitialized value. "
|
2017-11-30 17:18:35 +08:00
|
|
|
"The computed value will also be garbage";
|
|
|
|
|
|
|
|
ex = U->getSubExpr();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-02 08:56:20 +08:00
|
|
|
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
|
2010-03-23 06:16:26 +08:00
|
|
|
if (B->isCompoundAssignmentOp()) {
|
2018-01-18 04:27:29 +08:00
|
|
|
if (C.getSVal(B->getLHS()).isUndef()) {
|
2018-02-28 06:05:55 +08:00
|
|
|
OS << "The left expression of the compound assignment is an "
|
2010-03-23 06:16:26 +08:00
|
|
|
"uninitialized value. The computed value will also be garbage";
|
|
|
|
ex = B->getLHS();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-05 08:42:23 +08:00
|
|
|
|
|
|
|
ex = B->getRHS();
|
2010-03-23 06:16:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-02 08:56:20 +08:00
|
|
|
if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
|
2011-08-13 07:37:29 +08:00
|
|
|
const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
|
2009-11-05 08:42:23 +08:00
|
|
|
ex = VD->getInit();
|
|
|
|
}
|
2010-03-23 06:16:26 +08:00
|
|
|
|
2018-02-28 06:05:55 +08:00
|
|
|
if (const auto *CD =
|
|
|
|
dyn_cast<CXXConstructorDecl>(C.getStackFrame()->getDecl())) {
|
|
|
|
if (CD->isImplicit()) {
|
|
|
|
for (auto I : CD->inits()) {
|
|
|
|
if (I->getInit()->IgnoreImpCasts() == StoreE) {
|
|
|
|
OS << "Value assigned to field '" << I->getMember()->getName()
|
|
|
|
<< "' in implicit constructor is garbage or undefined";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-23 06:16:26 +08:00
|
|
|
break;
|
2009-11-04 12:24:16 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 06:05:55 +08:00
|
|
|
if (OS.str().empty())
|
|
|
|
OS << DefaultMsg;
|
|
|
|
|
|
|
|
auto R = llvm::make_unique<BugReport>(*BT, OS.str(), N);
|
2010-03-23 06:16:26 +08:00
|
|
|
if (ex) {
|
|
|
|
R->addRange(ex->getSourceRange());
|
2018-10-24 02:24:53 +08:00
|
|
|
bugreporter::trackExpressionValue(N, ex, *R);
|
2010-03-23 06:16:26 +08:00
|
|
|
}
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(R));
|
2010-03-23 06:16:26 +08:00
|
|
|
}
|
2009-11-04 12:24:16 +08:00
|
|
|
|
2011-02-28 09:27:37 +08:00
|
|
|
void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UndefinedAssignmentChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterUndefinedAssignmentChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|