2012-02-12 00:32:09 +08:00
|
|
|
//== BoolAssignmentChecker.cpp - Boolean assignment 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
|
2012-02-12 00:32:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines BoolAssignmentChecker, a builtin check in ExprEngine that
|
|
|
|
// performs checks for assignment of non-Boolean values to Boolean variables.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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"
|
2012-02-12 00:32:09 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class BoolAssignmentChecker : public Checker< check::Bind > {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BuiltinBug> BT;
|
2012-02-12 00:32:09 +08:00
|
|
|
void emitReport(ProgramStateRef state, CheckerContext &C) const;
|
|
|
|
public:
|
|
|
|
void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void BoolAssignmentChecker::emitReport(ProgramStateRef state,
|
|
|
|
CheckerContext &C) const {
|
2015-09-17 06:03:05 +08:00
|
|
|
if (ExplodedNode *N = C.generateNonFatalErrorNode(state)) {
|
2012-02-12 00:32:09 +08:00
|
|
|
if (!BT)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT.reset(new BuiltinBug(this, "Assignment of a non-Boolean value"));
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(llvm::make_unique<BugReport>(*BT, BT->getDescription(), N));
|
2012-02-12 00:32:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isBooleanType(QualType Ty) {
|
|
|
|
if (Ty->isBooleanType()) // C++ or C99
|
|
|
|
return true;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
if (const TypedefType *TT = Ty->getAs<TypedefType>())
|
|
|
|
return TT->getDecl()->getName() == "BOOL" || // Objective-C
|
|
|
|
TT->getDecl()->getName() == "_Bool" || // stdbool.h < C99
|
|
|
|
TT->getDecl()->getName() == "Boolean"; // MacTypes.h
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoolAssignmentChecker::checkBind(SVal loc, SVal val, const Stmt *S,
|
|
|
|
CheckerContext &C) const {
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
// We are only interested in stores into Booleans.
|
|
|
|
const TypedValueRegion *TR =
|
|
|
|
dyn_cast_or_null<TypedValueRegion>(loc.getAsRegion());
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
if (!TR)
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
QualType valTy = TR->getValueType();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
if (!isBooleanType(valTy))
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
// Get the value of the right-hand side. We only care about values
|
|
|
|
// that are defined (UnknownVals and UndefinedVals are handled by other
|
|
|
|
// checkers).
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<DefinedSVal> DV = val.getAs<DefinedSVal>();
|
2012-02-12 00:32:09 +08:00
|
|
|
if (!DV)
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
// Check if the assigned value meets our criteria for correctness. It must
|
|
|
|
// be a value that is either 0 or 1. One way to check this is to see if
|
|
|
|
// the value is possibly < 0 (for a negative value) or greater than 1.
|
2015-09-08 11:50:52 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-02-12 00:32:09 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
ConstraintManager &CM = C.getConstraintManager();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
|
|
|
// First, ensure that the value is >= 0.
|
2012-02-12 00:32:09 +08:00
|
|
|
DefinedSVal zeroVal = svalBuilder.makeIntVal(0, valTy);
|
|
|
|
SVal greaterThanOrEqualToZeroVal =
|
|
|
|
svalBuilder.evalBinOp(state, BO_GE, *DV, zeroVal,
|
|
|
|
svalBuilder.getConditionType());
|
2013-02-20 13:52:05 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<DefinedSVal> greaterThanEqualToZero =
|
2013-02-20 13:52:05 +08:00
|
|
|
greaterThanOrEqualToZeroVal.getAs<DefinedSVal>();
|
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
if (!greaterThanEqualToZero) {
|
|
|
|
// The SValBuilder cannot construct a valid SVal for this condition.
|
2015-09-08 11:50:52 +08:00
|
|
|
// This means we cannot properly reason about it.
|
2012-02-12 00:32:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
ProgramStateRef stateLT, stateGE;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateGE, stateLT) = CM.assumeDual(state, *greaterThanEqualToZero);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
// Is it possible for the value to be less than zero?
|
|
|
|
if (stateLT) {
|
|
|
|
// It is possible for the value to be less than zero. We only
|
|
|
|
// want to emit a warning, however, if that value is fully constrained.
|
|
|
|
// If it it possible for the value to be >= 0, then essentially the
|
|
|
|
// value is underconstrained and there is nothing left to be done.
|
|
|
|
if (!stateGE)
|
|
|
|
emitReport(stateLT, C);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
// In either case, we are done.
|
|
|
|
return;
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
// If we reach here, it must be the case that the value is constrained
|
|
|
|
// to only be >= 0.
|
|
|
|
assert(stateGE == state);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
// At this point we know that the value is >= 0.
|
|
|
|
// Now check to ensure that the value is <= 1.
|
|
|
|
DefinedSVal OneVal = svalBuilder.makeIntVal(1, valTy);
|
|
|
|
SVal lessThanEqToOneVal =
|
|
|
|
svalBuilder.evalBinOp(state, BO_LE, *DV, OneVal,
|
|
|
|
svalBuilder.getConditionType());
|
2013-02-20 13:52:05 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<DefinedSVal> lessThanEqToOne =
|
2013-02-20 13:52:05 +08:00
|
|
|
lessThanEqToOneVal.getAs<DefinedSVal>();
|
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
if (!lessThanEqToOne) {
|
|
|
|
// The SValBuilder cannot construct a valid SVal for this condition.
|
2015-09-08 11:50:52 +08:00
|
|
|
// This means we cannot properly reason about it.
|
2012-02-12 00:32:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
ProgramStateRef stateGT, stateLE;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateLE, stateGT) = CM.assumeDual(state, *lessThanEqToOne);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
// Is it possible for the value to be greater than one?
|
|
|
|
if (stateGT) {
|
|
|
|
// It is possible for the value to be greater than one. We only
|
|
|
|
// want to emit a warning, however, if that value is fully constrained.
|
|
|
|
// If it is possible for the value to be <= 1, then essentially the
|
|
|
|
// value is underconstrained and there is nothing left to be done.
|
|
|
|
if (!stateLE)
|
|
|
|
emitReport(stateGT, C);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
// In either case, we are done.
|
|
|
|
return;
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-12 00:32:09 +08:00
|
|
|
// If we reach here, it must be the case that the value is constrained
|
|
|
|
// to only be <= 1.
|
|
|
|
assert(stateLE == state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerBoolAssignmentChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<BoolAssignmentChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterBoolAssignmentChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|