155011c01SRyan Govostes //== BoolAssignmentChecker.cpp - Boolean assignment checker -----*- C++ -*--==//
255011c01SRyan Govostes //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
655011c01SRyan Govostes //
755011c01SRyan Govostes //===----------------------------------------------------------------------===//
855011c01SRyan Govostes //
955011c01SRyan Govostes // This defines BoolAssignmentChecker, a builtin check in ExprEngine that
1055011c01SRyan Govostes // performs checks for assignment of non-Boolean values to Boolean variables.
1155011c01SRyan Govostes //
1255011c01SRyan Govostes //===----------------------------------------------------------------------===//
1355011c01SRyan Govostes
1476a21502SKristof Umann #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15*094fb13bSEndre Fülöp #include "clang/StaticAnalyzer/Checkers/Taint.h"
163a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
1755011c01SRyan Govostes #include "clang/StaticAnalyzer/Core/Checker.h"
1855011c01SRyan Govostes #include "clang/StaticAnalyzer/Core/CheckerManager.h"
1955011c01SRyan Govostes #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
2055011c01SRyan Govostes
2155011c01SRyan Govostes using namespace clang;
2255011c01SRyan Govostes using namespace ento;
2355011c01SRyan Govostes
2455011c01SRyan Govostes namespace {
2555011c01SRyan Govostes class BoolAssignmentChecker : public Checker< check::Bind > {
26b8984329SAhmed Charles mutable std::unique_ptr<BuiltinBug> BT;
27*094fb13bSEndre Fülöp void emitReport(ProgramStateRef state, CheckerContext &C,
28*094fb13bSEndre Fülöp bool IsTainted = false) const;
29*094fb13bSEndre Fülöp
3055011c01SRyan Govostes public:
3155011c01SRyan Govostes void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
3255011c01SRyan Govostes };
3355011c01SRyan Govostes } // end anonymous namespace
3455011c01SRyan Govostes
emitReport(ProgramStateRef state,CheckerContext & C,bool IsTainted) const35*094fb13bSEndre Fülöp void BoolAssignmentChecker::emitReport(ProgramStateRef state, CheckerContext &C,
36*094fb13bSEndre Fülöp bool IsTainted) const {
37e39bd407SDevin Coughlin if (ExplodedNode *N = C.generateNonFatalErrorNode(state)) {
3855011c01SRyan Govostes if (!BT)
394aca9b1cSAlexander Kornienko BT.reset(new BuiltinBug(this, "Assignment of a non-Boolean value"));
402f169e7cSArtem Dergachev
41*094fb13bSEndre Fülöp StringRef Msg = IsTainted ? "Might assign a tainted non-Boolean value"
42*094fb13bSEndre Fülöp : "Assignment of a non-Boolean value";
43*094fb13bSEndre Fülöp C.emitReport(std::make_unique<PathSensitiveBugReport>(*BT, Msg, N));
4455011c01SRyan Govostes }
4555011c01SRyan Govostes }
4655011c01SRyan Govostes
isBooleanType(QualType Ty)4755011c01SRyan Govostes static bool isBooleanType(QualType Ty) {
4855011c01SRyan Govostes if (Ty->isBooleanType()) // C++ or C99
4955011c01SRyan Govostes return true;
5055011c01SRyan Govostes
5155011c01SRyan Govostes if (const TypedefType *TT = Ty->getAs<TypedefType>())
5255011c01SRyan Govostes return TT->getDecl()->getName() == "BOOL" || // Objective-C
5355011c01SRyan Govostes TT->getDecl()->getName() == "_Bool" || // stdbool.h < C99
5455011c01SRyan Govostes TT->getDecl()->getName() == "Boolean"; // MacTypes.h
5555011c01SRyan Govostes
5655011c01SRyan Govostes return false;
5755011c01SRyan Govostes }
5855011c01SRyan Govostes
checkBind(SVal loc,SVal val,const Stmt * S,CheckerContext & C) const5955011c01SRyan Govostes void BoolAssignmentChecker::checkBind(SVal loc, SVal val, const Stmt *S,
6055011c01SRyan Govostes CheckerContext &C) const {
6155011c01SRyan Govostes
6255011c01SRyan Govostes // We are only interested in stores into Booleans.
6355011c01SRyan Govostes const TypedValueRegion *TR =
6455011c01SRyan Govostes dyn_cast_or_null<TypedValueRegion>(loc.getAsRegion());
6555011c01SRyan Govostes
6655011c01SRyan Govostes if (!TR)
6755011c01SRyan Govostes return;
6855011c01SRyan Govostes
6955011c01SRyan Govostes QualType valTy = TR->getValueType();
7055011c01SRyan Govostes
7155011c01SRyan Govostes if (!isBooleanType(valTy))
7255011c01SRyan Govostes return;
7355011c01SRyan Govostes
7455011c01SRyan Govostes // Get the value of the right-hand side. We only care about values
7555011c01SRyan Govostes // that are defined (UnknownVals and UndefinedVals are handled by other
7655011c01SRyan Govostes // checkers).
7788c7b164SMikhail Gadelha Optional<NonLoc> NV = val.getAs<NonLoc>();
7888c7b164SMikhail Gadelha if (!NV)
7955011c01SRyan Govostes return;
8055011c01SRyan Govostes
8155011c01SRyan Govostes // Check if the assigned value meets our criteria for correctness. It must
8255011c01SRyan Govostes // be a value that is either 0 or 1. One way to check this is to see if
8355011c01SRyan Govostes // the value is possibly < 0 (for a negative value) or greater than 1.
8455011c01SRyan Govostes ProgramStateRef state = C.getState();
8555011c01SRyan Govostes SValBuilder &svalBuilder = C.getSValBuilder();
8688c7b164SMikhail Gadelha BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
8755011c01SRyan Govostes ConstraintManager &CM = C.getConstraintManager();
8855011c01SRyan Govostes
8988c7b164SMikhail Gadelha llvm::APSInt Zero = BVF.getValue(0, valTy);
9088c7b164SMikhail Gadelha llvm::APSInt One = BVF.getValue(1, valTy);
9155011c01SRyan Govostes
9288c7b164SMikhail Gadelha ProgramStateRef StIn, StOut;
9388c7b164SMikhail Gadelha std::tie(StIn, StOut) = CM.assumeInclusiveRangeDual(state, *NV, Zero, One);
9455011c01SRyan Govostes
9588c7b164SMikhail Gadelha if (!StIn)
9688c7b164SMikhail Gadelha emitReport(StOut, C);
97*094fb13bSEndre Fülöp if (StIn && StOut && taint::isTainted(state, *NV))
98*094fb13bSEndre Fülöp emitReport(StOut, C, /*IsTainted=*/true);
9955011c01SRyan Govostes }
10055011c01SRyan Govostes
registerBoolAssignmentChecker(CheckerManager & mgr)10155011c01SRyan Govostes void ento::registerBoolAssignmentChecker(CheckerManager &mgr) {
10255011c01SRyan Govostes mgr.registerChecker<BoolAssignmentChecker>();
10355011c01SRyan Govostes }
104058a7a45SKristof Umann
shouldRegisterBoolAssignmentChecker(const CheckerManager & mgr)105bda3dd0dSKirstóf Umann bool ento::shouldRegisterBoolAssignmentChecker(const CheckerManager &mgr) {
106058a7a45SKristof Umann return true;
107058a7a45SKristof Umann }
108