clang  15.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 
17 namespace clang {
18 
19 namespace 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 
63  SymbolManager &SymMgr = getSymbolManager();
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 
76  SymbolManager &SymMgr = getSymbolManager();
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.
141  if (!T->isIntegralOrEnumerationType())
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) {
156  assert(BinaryOperator::isComparisonOp(Op) &&
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 
217 void 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
clang::ento::Loc::isLocType
static bool isLocType(QualType T)
Definition: SVals.h:335
max
__DEVICE__ int max(int __a, int __b)
Definition: __clang_cuda_math.h:196
clang::ento::BasicValueFactory
Definition: BasicValueFactory.h:107
clang::ento::RangedConstraintManager::assumeSymGE
virtual ProgramStateRef assumeSymGE(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
clang::ento::SymbolManager::getSymSymExpr
const SymSymExpr * getSymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op, const SymExpr *rhs, QualType t)
Definition: SymbolManager.cpp:291
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:675
clang::BinaryOperator::reverseComparisonOp
static Opcode reverseComparisonOp(Opcode Opc)
Definition: Expr.h:3925
clang::ento::BinarySymExpr
Represents a symbolic expression involving a binary operator.
Definition: SymbolManager.h:313
clang::ento::APSIntType::isUnsigned
bool isUnsigned() const
Definition: APSIntType.h:31
clang::ento::SValBuilder::makeSymbolVal
DefinedSVal makeSymbolVal(SymbolRef Sym)
Make an SVal that represents the given symbol.
Definition: SValBuilder.h:407
APSInt
llvm::APSInt APSInt
Definition: ByteCodeEmitter.cpp:19
clang::BinaryOperator::isEqualityOp
bool isEqualityOp() const
Definition: Expr.h:3904
clang::ento::SymbolManager::getContext
ASTContext & getContext()
Definition: SymbolManager.h:500
clang::ento::SymExpr::getType
virtual QualType getType() const =0
clang::ento::RangedConstraintManager::assumeSym
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...
Definition: RangedConstraintManager.cpp:23
clang::ento::SymExpr
Symbolic value.
Definition: SymExpr.h:29
clang::ento::SimpleConstraintManager::getBasicVals
BasicValueFactory & getBasicVals() const
Definition: SimpleConstraintManager.h:79
clang::ento::BinarySymExpr::getOpcode
BinaryOperator::Opcode getOpcode() const
Definition: SymbolManager.h:332
clang::interp::Zero
bool Zero(InterpState &S, CodePtr OpPC)
Definition: Interp.h:814
clang::ento::RangedConstraintManager::assumeSymLT
virtual ProgramStateRef assumeSymLT(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
clang::ento::RangedConstraintManager::assumeSymLE
virtual ProgramStateRef assumeSymLE(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
clang::ento::APSIntType::getBitWidth
uint32_t getBitWidth() const
Definition: APSIntType.h:30
clang::ento::SValBuilder::simplifySVal
virtual SVal simplifySVal(ProgramStateRef State, SVal Val)=0
Simplify symbolic expressions within a given SVal.
clang::ento::simplifyToSVal
SVal simplifyToSVal(ProgramStateRef State, SymbolRef Sym)
Try to simplify a given symbolic expression's associated SVal based on the constraints in State.
Definition: RangedConstraintManager.cpp:235
clang::BinaryOperator::negateComparisonOp
static Opcode negateComparisonOp(Opcode Opc)
Definition: Expr.h:3912
clang::ento::APSIntType
A record of the "type" of an APSInt, used for conversions.
Definition: APSIntType.h:19
clang::interp::InRange
bool InRange(InterpState &S, CodePtr OpPC)
Definition: Interp.h:266
clang::ento::SValBuilder
Definition: SValBuilder.h:53
clang::ento::RangedConstraintManager::assumeSymGT
virtual ProgramStateRef assumeSymGT(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
clang::ento::RangedConstraintManager::assumeSymRel
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.
Definition: RangedConstraintManager.cpp:152
clang::ento::BinarySymExprImpl
Template implementation for all binary symbolic expressions.
Definition: SymbolManager.h:360
clang::ento::RangedConstraintManager::assumeSymOutsideInclusiveRange
virtual ProgramStateRef assumeSymOutsideInclusiveRange(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, const llvm::APSInt &To, const llvm::APSInt &Adjustment)=0
clang::ento::APSIntType::getZeroValue
llvm::APSInt getZeroValue() const LLVM_READONLY
Returns an all-zero value for this type.
Definition: APSIntType.h:55
clang::BinaryOperator::isComparisonOp
bool isComparisonOp() const
Definition: Expr.h:3907
State
LineState State
Definition: UnwrappedLineFormatter.cpp:1090
clang::ento::RangedConstraintManager::assumeSymUnsupported
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 ...
Definition: RangedConstraintManager.cpp:133
clang::BinaryOperatorKind
BinaryOperatorKind
Definition: OperationKinds.h:25
clang::ento::SimpleConstraintManager::getSymbolManager
SymbolManager & getSymbolManager() const
Definition: SimpleConstraintManager.h:80
ProgramState.h
clang::ento::RangedConstraintManager::assumeSymWithinInclusiveRange
virtual ProgramStateRef assumeSymWithinInclusiveRange(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, const llvm::APSInt &To, const llvm::APSInt &Adjustment)=0
clang
Definition: CalledOnceCheck.h:17
clang::ento::APSIntType::convert
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
clang::ento::RangedConstraintManager::~RangedConstraintManager
~RangedConstraintManager() override
Definition: RangedConstraintManager.cpp:21
clang::ento::RangedConstraintManager::assumeSymInclusiveRange
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...
Definition: RangedConstraintManager.cpp:101
clang::ASTContext::getPointerDiffType
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
Definition: ASTContext.cpp:5867
clang::ento::RangedConstraintManager::assumeSymEQ
virtual ProgramStateRef assumeSymEQ(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
clang::ento::SVal
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:74
RangedConstraintManager.h
clang::ento::SVal::getAsSymbol
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
Definition: SVals.cpp:123
clang::ento::SymbolManager
Definition: SymbolManager.h:417
clang::ento::RangedConstraintManager::assumeSymNE
virtual ProgramStateRef assumeSymNE(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
clang::ento::BasicValueFactory::getAPSIntType
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
Definition: BasicValueFactory.h:142
llvm::IntrusiveRefCntPtr< const ProgramState >
clang::Type::isIntegralOrEnumerationType
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:7140
clang::ento::simplify
SymbolRef simplify(ProgramStateRef State, SymbolRef Sym)
Try to simplify a given symbolic expression based on the constraints in State.
Definition: RangedConstraintManager.cpp:240