clang 18.0.0git
BoolAssignmentChecker.cpp
Go to the documentation of this file.
1//== BoolAssignmentChecker.cpp - Boolean assignment checker -----*- C++ -*--==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This defines BoolAssignmentChecker, a builtin check in ExprEngine that
10// performs checks for assignment of non-Boolean values to Boolean variables.
11//
12//===----------------------------------------------------------------------===//
13
20#include <optional>
21
22using namespace clang;
23using namespace ento;
24
25namespace {
26 class BoolAssignmentChecker : public Checker< check::Bind > {
27 mutable std::unique_ptr<BugType> BT;
28 void emitReport(ProgramStateRef state, CheckerContext &C,
29 bool IsTainted = false) const;
30
31 public:
32 void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
33 };
34} // end anonymous namespace
35
36void BoolAssignmentChecker::emitReport(ProgramStateRef state, CheckerContext &C,
37 bool IsTainted) const {
38 if (ExplodedNode *N = C.generateNonFatalErrorNode(state)) {
39 if (!BT)
40 BT.reset(new BugType(this, "Assignment of a non-Boolean value"));
41
42 StringRef Msg = IsTainted ? "Might assign a tainted non-Boolean value"
43 : "Assignment of a non-Boolean value";
44 C.emitReport(std::make_unique<PathSensitiveBugReport>(*BT, Msg, N));
45 }
46}
47
48static bool isBooleanType(QualType Ty) {
49 if (Ty->isBooleanType()) // C++ or C99
50 return true;
51
52 if (const TypedefType *TT = Ty->getAs<TypedefType>())
53 return TT->getDecl()->getName() == "BOOL" || // Objective-C
54 TT->getDecl()->getName() == "_Bool" || // stdbool.h < C99
55 TT->getDecl()->getName() == "Boolean"; // MacTypes.h
56
57 return false;
58}
59
60void BoolAssignmentChecker::checkBind(SVal loc, SVal val, const Stmt *S,
61 CheckerContext &C) const {
62
63 // We are only interested in stores into Booleans.
64 const TypedValueRegion *TR =
65 dyn_cast_or_null<TypedValueRegion>(loc.getAsRegion());
66
67 if (!TR)
68 return;
69
70 QualType valTy = TR->getValueType();
71
72 if (!isBooleanType(valTy))
73 return;
74
75 // Get the value of the right-hand side. We only care about values
76 // that are defined (UnknownVals and UndefinedVals are handled by other
77 // checkers).
78 std::optional<NonLoc> NV = val.getAs<NonLoc>();
79 if (!NV)
80 return;
81
82 // Check if the assigned value meets our criteria for correctness. It must
83 // be a value that is either 0 or 1. One way to check this is to see if
84 // the value is possibly < 0 (for a negative value) or greater than 1.
85 ProgramStateRef state = C.getState();
86 SValBuilder &svalBuilder = C.getSValBuilder();
87 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
88 ConstraintManager &CM = C.getConstraintManager();
89
90 llvm::APSInt Zero = BVF.getValue(0, valTy);
91 llvm::APSInt One = BVF.getValue(1, valTy);
92
93 ProgramStateRef StIn, StOut;
94 std::tie(StIn, StOut) = CM.assumeInclusiveRangeDual(state, *NV, Zero, One);
95
96 if (!StIn)
97 emitReport(StOut, C);
98 if (StIn && StOut && taint::isTainted(state, *NV))
99 emitReport(StOut, C, /*IsTainted=*/true);
100}
101
102void ento::registerBoolAssignmentChecker(CheckerManager &mgr) {
103 mgr.registerChecker<BoolAssignmentChecker>();
104}
105
106bool ento::shouldRegisterBoolAssignmentChecker(const CheckerManager &mgr) {
107 return true;
108}
static bool isBooleanType(QualType Ty)
A (possibly-)qualified type.
Definition: Type.h:736
Stmt - This represents one statement.
Definition: Stmt.h:84
bool isBooleanType() const
Definition: Type.h:7468
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7558
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
ProgramStatePair assumeInclusiveRangeDual(ProgramStateRef State, NonLoc Value, const llvm::APSInt &From, const llvm::APSInt &To)
Returns a pair of states (StInRange, StOutOfRange) where the given value is assumed to be in the rang...
BasicValueFactory & getBasicValueFactory()
Definition: SValBuilder.h:149
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:55
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
Definition: SVals.h:86
const MemRegion * getAsRegion() const
Definition: SVals.cpp:120
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:530
virtual QualType getValueType() const =0
bool isTainted(ProgramStateRef State, const Stmt *S, const LocationContext *LCtx, TaintTagType Kind=TaintTagGeneric)
Check if the statement has a tainted value in the given state.
Definition: Taint.cpp:147
bool Zero(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1686