clang 20.0.0git
RangedConstraintManager.cpp
Go to the documentation of this file.
1//== RangedConstraintManager.cpp --------------------------------*- 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 file defines RangedConstraintManager, a class that provides a
10// range-based constraint manager interface.
11//
12//===----------------------------------------------------------------------===//
13
16
17namespace clang {
18
19namespace ento {
20
22
24 SymbolRef Sym,
25 bool Assumption) {
26 Sym = simplify(State, Sym);
27
28 // Handle SymbolData.
29 if (isa<SymbolData>(Sym))
30 return assumeSymUnsupported(State, Sym, Assumption);
31
32 // Handle symbolic expression.
33 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(Sym)) {
34 // We can only simplify expressions whose RHS is an integer.
35
36 BinaryOperator::Opcode op = SIE->getOpcode();
37 if (BinaryOperator::isComparisonOp(op) && op != BO_Cmp) {
38 if (!Assumption)
40
41 return assumeSymRel(State, SIE->getLHS(), op, SIE->getRHS());
42 }
43
44 // Handle adjustment with non-comparison ops.
45 const llvm::APSInt &Zero = getBasicVals().getValue(0, SIE->getType());
46 return assumeSymRel(State, SIE, (Assumption ? BO_NE : BO_EQ), Zero);
47 }
48
49 if (const auto *SSE = dyn_cast<SymSymExpr>(Sym)) {
50 BinaryOperator::Opcode Op = SSE->getOpcode();
52
53 // We convert equality operations for pointers only.
54 if (Loc::isLocType(SSE->getLHS()->getType()) &&
55 Loc::isLocType(SSE->getRHS()->getType())) {
56 // Translate "a != b" to "(b - a) != 0".
57 // We invert the order of the operands as a heuristic for how loop
58 // conditions are usually written ("begin != end") as compared to length
59 // calculations ("end - begin"). The more correct thing to do would be
60 // to canonicalize "a - b" and "b - a", which would allow us to treat
61 // "a != b" and "b != a" the same.
62
64 QualType DiffTy = SymMgr.getContext().getPointerDiffType();
65 SymbolRef Subtraction =
66 SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub, SSE->getLHS(), DiffTy);
67
68 const llvm::APSInt &Zero = getBasicVals().getValue(0, DiffTy);
70 if (!Assumption)
72 return assumeSymRel(State, Subtraction, Op, Zero);
73 }
74
77
78 QualType ExprType = SSE->getType();
79 SymbolRef CanonicalEquality =
80 SymMgr.getSymSymExpr(SSE->getLHS(), BO_EQ, SSE->getRHS(), ExprType);
81
82 bool WasEqual = SSE->getOpcode() == BO_EQ;
83 bool IsExpectedEqual = WasEqual == Assumption;
84
85 const llvm::APSInt &Zero = getBasicVals().getValue(0, ExprType);
86
87 if (IsExpectedEqual) {
88 return assumeSymNE(State, CanonicalEquality, Zero, Zero);
89 }
90
91 return assumeSymEQ(State, CanonicalEquality, Zero, Zero);
92 }
93 }
94 }
95
96 // If we get here, there's nothing else we can do but treat the symbol as
97 // opaque.
98 return assumeSymUnsupported(State, Sym, Assumption);
99}
100
102 ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
103 const llvm::APSInt &To, bool InRange) {
104
105 Sym = simplify(State, Sym);
106
107 // Get the type used for calculating wraparound.
109 APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
110
111 llvm::APSInt Adjustment = WraparoundType.getZeroValue();
112 SymbolRef AdjustedSym = Sym;
113 computeAdjustment(AdjustedSym, Adjustment);
114
115 // Convert the right-hand side integer as necessary.
116 APSIntType ComparisonType = std::max(WraparoundType, APSIntType(From));
117 llvm::APSInt ConvertedFrom = ComparisonType.convert(From);
118 llvm::APSInt ConvertedTo = ComparisonType.convert(To);
119
120 // Prefer unsigned comparisons.
121 if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
122 ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
123 Adjustment.setIsSigned(false);
124
125 if (InRange)
126 return assumeSymWithinInclusiveRange(State, AdjustedSym, ConvertedFrom,
127 ConvertedTo, Adjustment);
128 return assumeSymOutsideInclusiveRange(State, AdjustedSym, ConvertedFrom,
129 ConvertedTo, Adjustment);
130}
131
134 SymbolRef Sym, bool Assumption) {
135 Sym = simplify(State, Sym);
136
138 QualType T = Sym->getType();
139
140 // Non-integer types are not supported.
142 return State;
143
144 // Reverse the operation and add directly to state.
145 const llvm::APSInt &Zero = BVF.getValue(0, T);
146 if (Assumption)
147 return assumeSymNE(State, Sym, Zero, Zero);
148 else
149 return assumeSymEQ(State, Sym, Zero, Zero);
150}
151
153 SymbolRef Sym,
155 const llvm::APSInt &Int) {
157 "Non-comparison ops should be rewritten as comparisons to zero.");
158
159 // Simplification: translate an assume of a constraint of the form
160 // "(exp comparison_op expr) != 0" to true into an assume of
161 // "exp comparison_op expr" to true. (And similarly, an assume of the form
162 // "(exp comparison_op expr) == 0" to true into an assume of
163 // "exp comparison_op expr" to false.)
164 if (Int == 0 && (Op == BO_EQ || Op == BO_NE)) {
165 if (const BinarySymExpr *SE = dyn_cast<BinarySymExpr>(Sym))
166 if (BinaryOperator::isComparisonOp(SE->getOpcode()))
167 return assumeSym(State, Sym, (Op == BO_NE ? true : false));
168 }
169
170 // Get the type used for calculating wraparound.
172 APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
173
174 // We only handle simple comparisons of the form "$sym == constant"
175 // or "($sym+constant1) == constant2".
176 // The adjustment is "constant1" in the above expression. It's used to
177 // "slide" the solution range around for modular arithmetic. For example,
178 // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which
179 // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to
180 // the subclasses of SimpleConstraintManager to handle the adjustment.
181 llvm::APSInt Adjustment = WraparoundType.getZeroValue();
182 computeAdjustment(Sym, Adjustment);
183
184 // Convert the right-hand side integer as necessary.
185 APSIntType ComparisonType = std::max(WraparoundType, APSIntType(Int));
186 llvm::APSInt ConvertedInt = ComparisonType.convert(Int);
187
188 // Prefer unsigned comparisons.
189 if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
190 ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
191 Adjustment.setIsSigned(false);
192
193 switch (Op) {
194 default:
195 llvm_unreachable("invalid operation not caught by assertion above");
196
197 case BO_EQ:
198 return assumeSymEQ(State, Sym, ConvertedInt, Adjustment);
199
200 case BO_NE:
201 return assumeSymNE(State, Sym, ConvertedInt, Adjustment);
202
203 case BO_GT:
204 return assumeSymGT(State, Sym, ConvertedInt, Adjustment);
205
206 case BO_GE:
207 return assumeSymGE(State, Sym, ConvertedInt, Adjustment);
208
209 case BO_LT:
210 return assumeSymLT(State, Sym, ConvertedInt, Adjustment);
211
212 case BO_LE:
213 return assumeSymLE(State, Sym, ConvertedInt, Adjustment);
214 } // end switch
215}
216
217void RangedConstraintManager::computeAdjustment(SymbolRef &Sym,
218 llvm::APSInt &Adjustment) {
219 // Is it a "($sym+constant1)" expression?
220 if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(Sym)) {
221 BinaryOperator::Opcode Op = SE->getOpcode();
222 if (Op == BO_Add || Op == BO_Sub) {
223 Sym = SE->getLHS();
224 Adjustment = APSIntType(Adjustment).convert(SE->getRHS());
225
226 // Don't forget to negate the adjustment if it's being subtracted.
227 // This should happen /after/ promotion, in case the value being
228 // subtracted is, say, CHAR_MIN, and the promoted type is 'int'.
229 if (Op == BO_Sub)
230 Adjustment = -Adjustment;
231 }
232 }
233}
234
236 SValBuilder &SVB = State->getStateManager().getSValBuilder();
237 return SVB.simplifySVal(State, SVB.makeSymbolVal(Sym));
238}
239
241 SVal SimplifiedVal = simplifyToSVal(State, Sym);
242 if (SymbolRef SimplifiedSym = SimplifiedVal.getAsSymbol())
243 return SimplifiedSym;
244 return Sym;
245}
246
247} // end of namespace ento
248} // end of namespace clang
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
bool isComparisonOp() const
Definition: Expr.h:3961
static Opcode negateComparisonOp(Opcode Opc)
Definition: Expr.h:3966
static Opcode reverseComparisonOp(Opcode Opc)
Definition: Expr.h:3979
bool isEqualityOp() const
Definition: Expr.h:3958
A (possibly-)qualified type.
Definition: Type.h:941
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:8434
A record of the "type" of an APSInt, used for conversions.
Definition: APSIntType.h:19
bool isUnsigned() const
Definition: APSIntType.h:31
llvm::APSInt getZeroValue() const LLVM_READONLY
Returns an all-zero value for this type.
Definition: APSIntType.h:55
uint32_t getBitWidth() const
Definition: APSIntType.h:30
llvm::APSInt convert(const llvm::APSInt &Value) const LLVM_READONLY
Convert and return a new APSInt with the given value, but this type's bit width and signedness.
Definition: APSIntType.h:48
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
Template implementation for all binary symbolic expressions.
Represents a symbolic expression involving a binary operator.
BinaryOperator::Opcode getOpcode() const
static bool isLocType(QualType T)
Definition: SVals.h:259
virtual ProgramStateRef assumeSymNE(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
virtual ProgramStateRef assumeSymLE(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
virtual ProgramStateRef assumeSymWithinInclusiveRange(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, const llvm::APSInt &To, const llvm::APSInt &Adjustment)=0
ProgramStateRef assumeSym(ProgramStateRef State, SymbolRef Sym, bool Assumption) override
Given a symbolic expression that can be reasoned about, assume that it is true/false and generate the...
virtual ProgramStateRef assumeSymRel(ProgramStateRef State, SymbolRef Sym, BinaryOperator::Opcode op, const llvm::APSInt &Int)
Assume a constraint between a symbolic expression and a concrete integer.
ProgramStateRef assumeSymUnsupported(ProgramStateRef State, SymbolRef Sym, bool Assumption) override
Given a symbolic expression that cannot be reasoned about, assume that it is zero/nonzero and add it ...
virtual ProgramStateRef assumeSymOutsideInclusiveRange(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, const llvm::APSInt &To, const llvm::APSInt &Adjustment)=0
virtual ProgramStateRef assumeSymGE(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
virtual ProgramStateRef assumeSymGT(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
ProgramStateRef assumeSymInclusiveRange(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, const llvm::APSInt &To, bool InRange) override
Given a symbolic expression within the range [From, To], assume that it is true/false and generate th...
virtual ProgramStateRef assumeSymEQ(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
virtual ProgramStateRef assumeSymLT(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
DefinedSVal makeSymbolVal(SymbolRef Sym)
Make an SVal that represents the given symbol.
Definition: SValBuilder.h:400
virtual SVal simplifySVal(ProgramStateRef State, SVal Val)=0
Simplify symbolic expressions within a given SVal.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:55
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
Definition: SVals.cpp:104
BasicValueFactory & getBasicVals() const
Symbolic value.
Definition: SymExpr.h:30
virtual QualType getType() const =0
const SymSymExpr * getSymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op, const SymExpr *rhs, QualType t)
SVal simplifyToSVal(ProgramStateRef State, SymbolRef Sym)
Try to simplify a given symbolic expression's associated SVal based on the constraints in State.
SymbolRef simplify(ProgramStateRef State, SymbolRef Sym)
Try to simplify a given symbolic expression based on the constraints in State.
The JSON file list parser is used to communicate input to InstallAPI.
BinaryOperatorKind
const FunctionProtoType * T