10b57cec5SDimitry Andric //== BoolAssignmentChecker.cpp - Boolean assignment checker -----*- C++ -*--==// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This defines BoolAssignmentChecker, a builtin check in ExprEngine that 100b57cec5SDimitry Andric // performs checks for assignment of non-Boolean values to Boolean variables. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 150b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 160b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h" 170b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h" 180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric using namespace clang; 210b57cec5SDimitry Andric using namespace ento; 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric namespace { 240b57cec5SDimitry Andric class BoolAssignmentChecker : public Checker< check::Bind > { 250b57cec5SDimitry Andric mutable std::unique_ptr<BuiltinBug> BT; 260b57cec5SDimitry Andric void emitReport(ProgramStateRef state, CheckerContext &C) const; 270b57cec5SDimitry Andric public: 280b57cec5SDimitry Andric void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const; 290b57cec5SDimitry Andric }; 300b57cec5SDimitry Andric } // end anonymous namespace 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric void BoolAssignmentChecker::emitReport(ProgramStateRef state, 330b57cec5SDimitry Andric CheckerContext &C) const { 340b57cec5SDimitry Andric if (ExplodedNode *N = C.generateNonFatalErrorNode(state)) { 350b57cec5SDimitry Andric if (!BT) 360b57cec5SDimitry Andric BT.reset(new BuiltinBug(this, "Assignment of a non-Boolean value")); 37*a7dea167SDimitry Andric 38*a7dea167SDimitry Andric C.emitReport( 39*a7dea167SDimitry Andric std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N)); 400b57cec5SDimitry Andric } 410b57cec5SDimitry Andric } 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric static bool isBooleanType(QualType Ty) { 440b57cec5SDimitry Andric if (Ty->isBooleanType()) // C++ or C99 450b57cec5SDimitry Andric return true; 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric if (const TypedefType *TT = Ty->getAs<TypedefType>()) 480b57cec5SDimitry Andric return TT->getDecl()->getName() == "BOOL" || // Objective-C 490b57cec5SDimitry Andric TT->getDecl()->getName() == "_Bool" || // stdbool.h < C99 500b57cec5SDimitry Andric TT->getDecl()->getName() == "Boolean"; // MacTypes.h 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric return false; 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric void BoolAssignmentChecker::checkBind(SVal loc, SVal val, const Stmt *S, 560b57cec5SDimitry Andric CheckerContext &C) const { 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric // We are only interested in stores into Booleans. 590b57cec5SDimitry Andric const TypedValueRegion *TR = 600b57cec5SDimitry Andric dyn_cast_or_null<TypedValueRegion>(loc.getAsRegion()); 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric if (!TR) 630b57cec5SDimitry Andric return; 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric QualType valTy = TR->getValueType(); 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric if (!isBooleanType(valTy)) 680b57cec5SDimitry Andric return; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric // Get the value of the right-hand side. We only care about values 710b57cec5SDimitry Andric // that are defined (UnknownVals and UndefinedVals are handled by other 720b57cec5SDimitry Andric // checkers). 730b57cec5SDimitry Andric Optional<DefinedSVal> DV = val.getAs<DefinedSVal>(); 740b57cec5SDimitry Andric if (!DV) 750b57cec5SDimitry Andric return; 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric // Check if the assigned value meets our criteria for correctness. It must 780b57cec5SDimitry Andric // be a value that is either 0 or 1. One way to check this is to see if 790b57cec5SDimitry Andric // the value is possibly < 0 (for a negative value) or greater than 1. 800b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 810b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 820b57cec5SDimitry Andric ConstraintManager &CM = C.getConstraintManager(); 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric // First, ensure that the value is >= 0. 850b57cec5SDimitry Andric DefinedSVal zeroVal = svalBuilder.makeIntVal(0, valTy); 860b57cec5SDimitry Andric SVal greaterThanOrEqualToZeroVal = 870b57cec5SDimitry Andric svalBuilder.evalBinOp(state, BO_GE, *DV, zeroVal, 880b57cec5SDimitry Andric svalBuilder.getConditionType()); 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric Optional<DefinedSVal> greaterThanEqualToZero = 910b57cec5SDimitry Andric greaterThanOrEqualToZeroVal.getAs<DefinedSVal>(); 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric if (!greaterThanEqualToZero) { 940b57cec5SDimitry Andric // The SValBuilder cannot construct a valid SVal for this condition. 950b57cec5SDimitry Andric // This means we cannot properly reason about it. 960b57cec5SDimitry Andric return; 970b57cec5SDimitry Andric } 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric ProgramStateRef stateLT, stateGE; 1000b57cec5SDimitry Andric std::tie(stateGE, stateLT) = CM.assumeDual(state, *greaterThanEqualToZero); 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric // Is it possible for the value to be less than zero? 1030b57cec5SDimitry Andric if (stateLT) { 1040b57cec5SDimitry Andric // It is possible for the value to be less than zero. We only 1050b57cec5SDimitry Andric // want to emit a warning, however, if that value is fully constrained. 1060b57cec5SDimitry Andric // If it it possible for the value to be >= 0, then essentially the 1070b57cec5SDimitry Andric // value is underconstrained and there is nothing left to be done. 1080b57cec5SDimitry Andric if (!stateGE) 1090b57cec5SDimitry Andric emitReport(stateLT, C); 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric // In either case, we are done. 1120b57cec5SDimitry Andric return; 1130b57cec5SDimitry Andric } 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric // If we reach here, it must be the case that the value is constrained 1160b57cec5SDimitry Andric // to only be >= 0. 1170b57cec5SDimitry Andric assert(stateGE == state); 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric // At this point we know that the value is >= 0. 1200b57cec5SDimitry Andric // Now check to ensure that the value is <= 1. 1210b57cec5SDimitry Andric DefinedSVal OneVal = svalBuilder.makeIntVal(1, valTy); 1220b57cec5SDimitry Andric SVal lessThanEqToOneVal = 1230b57cec5SDimitry Andric svalBuilder.evalBinOp(state, BO_LE, *DV, OneVal, 1240b57cec5SDimitry Andric svalBuilder.getConditionType()); 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric Optional<DefinedSVal> lessThanEqToOne = 1270b57cec5SDimitry Andric lessThanEqToOneVal.getAs<DefinedSVal>(); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric if (!lessThanEqToOne) { 1300b57cec5SDimitry Andric // The SValBuilder cannot construct a valid SVal for this condition. 1310b57cec5SDimitry Andric // This means we cannot properly reason about it. 1320b57cec5SDimitry Andric return; 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric ProgramStateRef stateGT, stateLE; 1360b57cec5SDimitry Andric std::tie(stateLE, stateGT) = CM.assumeDual(state, *lessThanEqToOne); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric // Is it possible for the value to be greater than one? 1390b57cec5SDimitry Andric if (stateGT) { 1400b57cec5SDimitry Andric // It is possible for the value to be greater than one. We only 1410b57cec5SDimitry Andric // want to emit a warning, however, if that value is fully constrained. 1420b57cec5SDimitry Andric // If it is possible for the value to be <= 1, then essentially the 1430b57cec5SDimitry Andric // value is underconstrained and there is nothing left to be done. 1440b57cec5SDimitry Andric if (!stateLE) 1450b57cec5SDimitry Andric emitReport(stateGT, C); 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric // In either case, we are done. 1480b57cec5SDimitry Andric return; 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric // If we reach here, it must be the case that the value is constrained 1520b57cec5SDimitry Andric // to only be <= 1. 1530b57cec5SDimitry Andric assert(stateLE == state); 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric void ento::registerBoolAssignmentChecker(CheckerManager &mgr) { 1570b57cec5SDimitry Andric mgr.registerChecker<BoolAssignmentChecker>(); 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric bool ento::shouldRegisterBoolAssignmentChecker(const LangOptions &LO) { 1610b57cec5SDimitry Andric return true; 1620b57cec5SDimitry Andric } 163