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
emitReport(ProgramStateRef state,CheckerContext & C) const32*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
38*0b57cec5SDimitry Andric C.emitReport(
39*0b57cec5SDimitry Andric std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N));
40*0b57cec5SDimitry Andric }
41*0b57cec5SDimitry Andric }
42*0b57cec5SDimitry Andric
isBooleanType(QualType Ty)43*0b57cec5SDimitry Andric static bool isBooleanType(QualType Ty) {
44*0b57cec5SDimitry Andric if (Ty->isBooleanType()) // C++ or C99
45*0b57cec5SDimitry Andric return true;
46*0b57cec5SDimitry Andric
47*0b57cec5SDimitry Andric if (const TypedefType *TT = Ty->getAs<TypedefType>())
48*0b57cec5SDimitry Andric return TT->getDecl()->getName() == "BOOL" || // Objective-C
49*0b57cec5SDimitry Andric TT->getDecl()->getName() == "_Bool" || // stdbool.h < C99
50*0b57cec5SDimitry Andric TT->getDecl()->getName() == "Boolean"; // MacTypes.h
51*0b57cec5SDimitry Andric
52*0b57cec5SDimitry Andric return false;
53*0b57cec5SDimitry Andric }
54*0b57cec5SDimitry Andric
checkBind(SVal loc,SVal val,const Stmt * S,CheckerContext & C) const55*0b57cec5SDimitry Andric void BoolAssignmentChecker::checkBind(SVal loc, SVal val, const Stmt *S,
56*0b57cec5SDimitry Andric CheckerContext &C) const {
57*0b57cec5SDimitry Andric
58*0b57cec5SDimitry Andric // We are only interested in stores into Booleans.
59*0b57cec5SDimitry Andric const TypedValueRegion *TR =
60*0b57cec5SDimitry Andric dyn_cast_or_null<TypedValueRegion>(loc.getAsRegion());
61*0b57cec5SDimitry Andric
62*0b57cec5SDimitry Andric if (!TR)
63*0b57cec5SDimitry Andric return;
64*0b57cec5SDimitry Andric
65*0b57cec5SDimitry Andric QualType valTy = TR->getValueType();
66*0b57cec5SDimitry Andric
67*0b57cec5SDimitry Andric if (!isBooleanType(valTy))
68*0b57cec5SDimitry Andric return;
69*0b57cec5SDimitry Andric
70*0b57cec5SDimitry Andric // Get the value of the right-hand side. We only care about values
71*0b57cec5SDimitry Andric // that are defined (UnknownVals and UndefinedVals are handled by other
72*0b57cec5SDimitry Andric // checkers).
73*0b57cec5SDimitry Andric Optional<NonLoc> NV = val.getAs<NonLoc>();
74*0b57cec5SDimitry Andric if (!NV)
75*0b57cec5SDimitry Andric return;
76*0b57cec5SDimitry Andric
77*0b57cec5SDimitry Andric // Check if the assigned value meets our criteria for correctness. It must
78*0b57cec5SDimitry Andric // be a value that is either 0 or 1. One way to check this is to see if
79*0b57cec5SDimitry Andric // the value is possibly < 0 (for a negative value) or greater than 1.
80*0b57cec5SDimitry Andric ProgramStateRef state = C.getState();
81*0b57cec5SDimitry Andric SValBuilder &svalBuilder = C.getSValBuilder();
82*0b57cec5SDimitry Andric BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
83*0b57cec5SDimitry Andric ConstraintManager &CM = C.getConstraintManager();
84*0b57cec5SDimitry Andric
85*0b57cec5SDimitry Andric llvm::APSInt Zero = BVF.getValue(0, valTy);
86*0b57cec5SDimitry Andric llvm::APSInt One = BVF.getValue(1, valTy);
87*0b57cec5SDimitry Andric
88*0b57cec5SDimitry Andric ProgramStateRef StIn, StOut;
89*0b57cec5SDimitry Andric std::tie(StIn, StOut) = CM.assumeInclusiveRangeDual(state, *NV, Zero, One);
90*0b57cec5SDimitry Andric
91*0b57cec5SDimitry Andric if (!StIn)
92*0b57cec5SDimitry Andric emitReport(StOut, C);
93*0b57cec5SDimitry Andric }
94*0b57cec5SDimitry Andric
registerBoolAssignmentChecker(CheckerManager & mgr)95*0b57cec5SDimitry Andric void ento::registerBoolAssignmentChecker(CheckerManager &mgr) {
96*0b57cec5SDimitry Andric mgr.registerChecker<BoolAssignmentChecker>();
97*0b57cec5SDimitry Andric }
98*0b57cec5SDimitry Andric
shouldRegisterBoolAssignmentChecker(const CheckerManager & mgr)99*0b57cec5SDimitry Andric bool ento::shouldRegisterBoolAssignmentChecker(const CheckerManager &mgr) {
100*0b57cec5SDimitry Andric return true;
101*0b57cec5SDimitry Andric }
102*0b57cec5SDimitry Andric