1*0b57cec5SDimitry Andric //== BoolAssignmentChecker.cpp - Boolean assignment checker -----*- C++ -*--==// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This defines BoolAssignmentChecker, a builtin check in ExprEngine that 10*0b57cec5SDimitry Andric // performs checks for assignment of non-Boolean values to Boolean variables. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 15*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 16*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h" 17*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h" 18*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 19*0b57cec5SDimitry Andric 20*0b57cec5SDimitry Andric using namespace clang; 21*0b57cec5SDimitry Andric using namespace ento; 22*0b57cec5SDimitry Andric 23*0b57cec5SDimitry Andric namespace { 24*0b57cec5SDimitry Andric class BoolAssignmentChecker : public Checker< check::Bind > { 25*0b57cec5SDimitry Andric mutable std::unique_ptr<BuiltinBug> BT; 26*0b57cec5SDimitry Andric void emitReport(ProgramStateRef state, CheckerContext &C) const; 27*0b57cec5SDimitry Andric public: 28*0b57cec5SDimitry Andric void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const; 29*0b57cec5SDimitry Andric }; 30*0b57cec5SDimitry Andric } // end anonymous namespace 31*0b57cec5SDimitry Andric 32*0b57cec5SDimitry Andric void BoolAssignmentChecker::emitReport(ProgramStateRef state, 33*0b57cec5SDimitry Andric CheckerContext &C) const { 34*0b57cec5SDimitry Andric if (ExplodedNode *N = C.generateNonFatalErrorNode(state)) { 35*0b57cec5SDimitry Andric if (!BT) 36*0b57cec5SDimitry Andric BT.reset(new BuiltinBug(this, "Assignment of a non-Boolean value")); 37*0b57cec5SDimitry Andric C.emitReport(llvm::make_unique<BugReport>(*BT, BT->getDescription(), N)); 38*0b57cec5SDimitry Andric } 39*0b57cec5SDimitry Andric } 40*0b57cec5SDimitry Andric 41*0b57cec5SDimitry Andric static bool isBooleanType(QualType Ty) { 42*0b57cec5SDimitry Andric if (Ty->isBooleanType()) // C++ or C99 43*0b57cec5SDimitry Andric return true; 44*0b57cec5SDimitry Andric 45*0b57cec5SDimitry Andric if (const TypedefType *TT = Ty->getAs<TypedefType>()) 46*0b57cec5SDimitry Andric return TT->getDecl()->getName() == "BOOL" || // Objective-C 47*0b57cec5SDimitry Andric TT->getDecl()->getName() == "_Bool" || // stdbool.h < C99 48*0b57cec5SDimitry Andric TT->getDecl()->getName() == "Boolean"; // MacTypes.h 49*0b57cec5SDimitry Andric 50*0b57cec5SDimitry Andric return false; 51*0b57cec5SDimitry Andric } 52*0b57cec5SDimitry Andric 53*0b57cec5SDimitry Andric void BoolAssignmentChecker::checkBind(SVal loc, SVal val, const Stmt *S, 54*0b57cec5SDimitry Andric CheckerContext &C) const { 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric // We are only interested in stores into Booleans. 57*0b57cec5SDimitry Andric const TypedValueRegion *TR = 58*0b57cec5SDimitry Andric dyn_cast_or_null<TypedValueRegion>(loc.getAsRegion()); 59*0b57cec5SDimitry Andric 60*0b57cec5SDimitry Andric if (!TR) 61*0b57cec5SDimitry Andric return; 62*0b57cec5SDimitry Andric 63*0b57cec5SDimitry Andric QualType valTy = TR->getValueType(); 64*0b57cec5SDimitry Andric 65*0b57cec5SDimitry Andric if (!isBooleanType(valTy)) 66*0b57cec5SDimitry Andric return; 67*0b57cec5SDimitry Andric 68*0b57cec5SDimitry Andric // Get the value of the right-hand side. We only care about values 69*0b57cec5SDimitry Andric // that are defined (UnknownVals and UndefinedVals are handled by other 70*0b57cec5SDimitry Andric // checkers). 71*0b57cec5SDimitry Andric Optional<DefinedSVal> DV = val.getAs<DefinedSVal>(); 72*0b57cec5SDimitry Andric if (!DV) 73*0b57cec5SDimitry Andric return; 74*0b57cec5SDimitry Andric 75*0b57cec5SDimitry Andric // Check if the assigned value meets our criteria for correctness. It must 76*0b57cec5SDimitry Andric // be a value that is either 0 or 1. One way to check this is to see if 77*0b57cec5SDimitry Andric // the value is possibly < 0 (for a negative value) or greater than 1. 78*0b57cec5SDimitry Andric ProgramStateRef state = C.getState(); 79*0b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder(); 80*0b57cec5SDimitry Andric ConstraintManager &CM = C.getConstraintManager(); 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric // First, ensure that the value is >= 0. 83*0b57cec5SDimitry Andric DefinedSVal zeroVal = svalBuilder.makeIntVal(0, valTy); 84*0b57cec5SDimitry Andric SVal greaterThanOrEqualToZeroVal = 85*0b57cec5SDimitry Andric svalBuilder.evalBinOp(state, BO_GE, *DV, zeroVal, 86*0b57cec5SDimitry Andric svalBuilder.getConditionType()); 87*0b57cec5SDimitry Andric 88*0b57cec5SDimitry Andric Optional<DefinedSVal> greaterThanEqualToZero = 89*0b57cec5SDimitry Andric greaterThanOrEqualToZeroVal.getAs<DefinedSVal>(); 90*0b57cec5SDimitry Andric 91*0b57cec5SDimitry Andric if (!greaterThanEqualToZero) { 92*0b57cec5SDimitry Andric // The SValBuilder cannot construct a valid SVal for this condition. 93*0b57cec5SDimitry Andric // This means we cannot properly reason about it. 94*0b57cec5SDimitry Andric return; 95*0b57cec5SDimitry Andric } 96*0b57cec5SDimitry Andric 97*0b57cec5SDimitry Andric ProgramStateRef stateLT, stateGE; 98*0b57cec5SDimitry Andric std::tie(stateGE, stateLT) = CM.assumeDual(state, *greaterThanEqualToZero); 99*0b57cec5SDimitry Andric 100*0b57cec5SDimitry Andric // Is it possible for the value to be less than zero? 101*0b57cec5SDimitry Andric if (stateLT) { 102*0b57cec5SDimitry Andric // It is possible for the value to be less than zero. We only 103*0b57cec5SDimitry Andric // want to emit a warning, however, if that value is fully constrained. 104*0b57cec5SDimitry Andric // If it it possible for the value to be >= 0, then essentially the 105*0b57cec5SDimitry Andric // value is underconstrained and there is nothing left to be done. 106*0b57cec5SDimitry Andric if (!stateGE) 107*0b57cec5SDimitry Andric emitReport(stateLT, C); 108*0b57cec5SDimitry Andric 109*0b57cec5SDimitry Andric // In either case, we are done. 110*0b57cec5SDimitry Andric return; 111*0b57cec5SDimitry Andric } 112*0b57cec5SDimitry Andric 113*0b57cec5SDimitry Andric // If we reach here, it must be the case that the value is constrained 114*0b57cec5SDimitry Andric // to only be >= 0. 115*0b57cec5SDimitry Andric assert(stateGE == state); 116*0b57cec5SDimitry Andric 117*0b57cec5SDimitry Andric // At this point we know that the value is >= 0. 118*0b57cec5SDimitry Andric // Now check to ensure that the value is <= 1. 119*0b57cec5SDimitry Andric DefinedSVal OneVal = svalBuilder.makeIntVal(1, valTy); 120*0b57cec5SDimitry Andric SVal lessThanEqToOneVal = 121*0b57cec5SDimitry Andric svalBuilder.evalBinOp(state, BO_LE, *DV, OneVal, 122*0b57cec5SDimitry Andric svalBuilder.getConditionType()); 123*0b57cec5SDimitry Andric 124*0b57cec5SDimitry Andric Optional<DefinedSVal> lessThanEqToOne = 125*0b57cec5SDimitry Andric lessThanEqToOneVal.getAs<DefinedSVal>(); 126*0b57cec5SDimitry Andric 127*0b57cec5SDimitry Andric if (!lessThanEqToOne) { 128*0b57cec5SDimitry Andric // The SValBuilder cannot construct a valid SVal for this condition. 129*0b57cec5SDimitry Andric // This means we cannot properly reason about it. 130*0b57cec5SDimitry Andric return; 131*0b57cec5SDimitry Andric } 132*0b57cec5SDimitry Andric 133*0b57cec5SDimitry Andric ProgramStateRef stateGT, stateLE; 134*0b57cec5SDimitry Andric std::tie(stateLE, stateGT) = CM.assumeDual(state, *lessThanEqToOne); 135*0b57cec5SDimitry Andric 136*0b57cec5SDimitry Andric // Is it possible for the value to be greater than one? 137*0b57cec5SDimitry Andric if (stateGT) { 138*0b57cec5SDimitry Andric // It is possible for the value to be greater than one. We only 139*0b57cec5SDimitry Andric // want to emit a warning, however, if that value is fully constrained. 140*0b57cec5SDimitry Andric // If it is possible for the value to be <= 1, then essentially the 141*0b57cec5SDimitry Andric // value is underconstrained and there is nothing left to be done. 142*0b57cec5SDimitry Andric if (!stateLE) 143*0b57cec5SDimitry Andric emitReport(stateGT, C); 144*0b57cec5SDimitry Andric 145*0b57cec5SDimitry Andric // In either case, we are done. 146*0b57cec5SDimitry Andric return; 147*0b57cec5SDimitry Andric } 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andric // If we reach here, it must be the case that the value is constrained 150*0b57cec5SDimitry Andric // to only be <= 1. 151*0b57cec5SDimitry Andric assert(stateLE == state); 152*0b57cec5SDimitry Andric } 153*0b57cec5SDimitry Andric 154*0b57cec5SDimitry Andric void ento::registerBoolAssignmentChecker(CheckerManager &mgr) { 155*0b57cec5SDimitry Andric mgr.registerChecker<BoolAssignmentChecker>(); 156*0b57cec5SDimitry Andric } 157*0b57cec5SDimitry Andric 158*0b57cec5SDimitry Andric bool ento::shouldRegisterBoolAssignmentChecker(const LangOptions &LO) { 159*0b57cec5SDimitry Andric return true; 160*0b57cec5SDimitry Andric } 161