2009-11-23 11:20:54 +08:00
|
|
|
//=== UndefBranchChecker.cpp -----------------------------------*- 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-23 11:20:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines UndefBranchChecker, which checks for undefined branch
|
|
|
|
// condition.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2016-05-27 22:27:13 +08:00
|
|
|
#include <utility>
|
2009-11-23 11:20:54 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-23 11:20:54 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2011-03-01 09:16:21 +08:00
|
|
|
class UndefBranchChecker : public Checker<check::BranchCondition> {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BuiltinBug> BT;
|
2009-11-23 11:29:59 +08:00
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
struct FindUndefExpr {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef St;
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx;
|
2009-11-23 11:29:59 +08:00
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
FindUndefExpr(ProgramStateRef S, const LocationContext *L)
|
2016-05-27 22:27:13 +08:00
|
|
|
: St(std::move(S)), LCtx(L) {}
|
2009-11-23 11:29:59 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
const Expr *FindExpr(const Expr *Ex) {
|
2009-11-23 11:29:59 +08:00
|
|
|
if (!MatchesCriteria(Ex))
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2009-11-23 11:29:59 +08:00
|
|
|
|
2015-07-03 23:12:24 +08:00
|
|
|
for (const Stmt *SubStmt : Ex->children())
|
|
|
|
if (const Expr *ExI = dyn_cast_or_null<Expr>(SubStmt))
|
|
|
|
if (const Expr *E2 = FindExpr(ExI))
|
|
|
|
return E2;
|
2009-11-23 11:29:59 +08:00
|
|
|
|
|
|
|
return Ex;
|
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
bool MatchesCriteria(const Expr *Ex) {
|
2012-01-07 06:09:28 +08:00
|
|
|
return St->getSVal(Ex, LCtx).isUndef();
|
|
|
|
}
|
2009-11-23 11:29:59 +08:00
|
|
|
};
|
|
|
|
|
2009-11-23 11:20:54 +08:00
|
|
|
public:
|
2011-10-26 03:56:54 +08:00
|
|
|
void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const;
|
2009-11-23 11:20:54 +08:00
|
|
|
};
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2009-11-23 11:20:54 +08:00
|
|
|
|
2011-02-28 09:27:33 +08:00
|
|
|
void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
|
2011-10-26 03:56:54 +08:00
|
|
|
CheckerContext &Ctx) const {
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal X = Ctx.getSVal(Condition);
|
2009-11-23 11:20:54 +08:00
|
|
|
if (X.isUndef()) {
|
2011-10-19 07:06:21 +08:00
|
|
|
// Generate a sink node, which implicitly marks both outgoing branches as
|
|
|
|
// infeasible.
|
2015-09-17 06:03:05 +08:00
|
|
|
ExplodedNode *N = Ctx.generateErrorNode();
|
2009-11-23 11:20:54 +08:00
|
|
|
if (N) {
|
|
|
|
if (!BT)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT.reset(new BuiltinBug(
|
|
|
|
this, "Branch condition evaluates to a garbage value"));
|
2009-11-23 11:29:59 +08:00
|
|
|
|
|
|
|
// What's going on here: we want to highlight the subexpression of the
|
|
|
|
// condition that is the most likely source of the "uninitialized
|
|
|
|
// branch condition." We do a recursive walk of the condition's
|
|
|
|
// subexpressions and roughly look for the most nested subexpression
|
|
|
|
// that binds to Undefined. We then highlight that expression's range.
|
|
|
|
|
|
|
|
// Get the predecessor node and check if is a PostStmt with the Stmt
|
|
|
|
// being the terminator condition. We want to inspect the state
|
|
|
|
// of that node instead because it will contain main information about
|
|
|
|
// the subexpressions.
|
|
|
|
|
|
|
|
// Note: any predecessor will do. They should have identical state,
|
|
|
|
// since all the BlockEdge did was act as an error sink since the value
|
|
|
|
// had to already be undefined.
|
2011-10-04 05:16:32 +08:00
|
|
|
assert (!N->pred_empty());
|
|
|
|
const Expr *Ex = cast<Expr>(Condition);
|
2009-11-23 11:29:59 +08:00
|
|
|
ExplodedNode *PrevN = *N->pred_begin();
|
|
|
|
ProgramPoint P = PrevN->getLocation();
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef St = N->getState();
|
2009-11-23 11:29:59 +08:00
|
|
|
|
2013-02-22 06:23:56 +08:00
|
|
|
if (Optional<PostStmt> PS = P.getAs<PostStmt>())
|
2009-11-23 11:29:59 +08:00
|
|
|
if (PS->getStmt() == Ex)
|
|
|
|
St = PrevN->getState();
|
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
FindUndefExpr FindIt(St, Ctx.getLocationContext());
|
2009-11-23 11:29:59 +08:00
|
|
|
Ex = FindIt.FindExpr(Ex);
|
2009-11-24 02:12:03 +08:00
|
|
|
|
|
|
|
// Emit the bug report.
|
2015-06-23 21:15:32 +08:00
|
|
|
auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
|
2018-10-24 02:24:53 +08:00
|
|
|
bugreporter::trackExpressionValue(N, Ex, *R);
|
2009-11-23 11:29:59 +08:00
|
|
|
R->addRange(Ex->getSourceRange());
|
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
Ctx.emitReport(std::move(R));
|
2009-11-23 11:20:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-28 09:27:33 +08:00
|
|
|
|
|
|
|
void ento::registerUndefBranchChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UndefBranchChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterUndefBranchChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|