clang  14.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  } else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(Sym)) {
45  BinaryOperator::Opcode Op = SSE->getOpcode();
47 
48  // We convert equality operations for pointers only.
49  if (Loc::isLocType(SSE->getLHS()->getType()) &&
50  Loc::isLocType(SSE->getRHS()->getType())) {
51  // Translate "a != b" to "(b - a) != 0".
52  // We invert the order of the operands as a heuristic for how loop
53  // conditions are usually written ("begin != end") as compared to length
54  // calculations ("end - begin"). The more correct thing to do would be to
55  // canonicalize "a - b" and "b - a", which would allow us to treat
56  // "a != b" and "b != a" the same.
57 
58  SymbolManager &SymMgr = getSymbolManager();
59  QualType DiffTy = SymMgr.getContext().getPointerDiffType();
60  SymbolRef Subtraction =
61  SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub, SSE->getLHS(), DiffTy);
62 
63  const llvm::APSInt &Zero = getBasicVals().getValue(0, DiffTy);
65  if (!Assumption)
67  return assumeSymRel(State, Subtraction, Op, Zero);
68  }
69 
71  SymbolManager &SymMgr = getSymbolManager();
72 
73  QualType ExprType = SSE->getType();
74  SymbolRef CanonicalEquality =
75  SymMgr.getSymSymExpr(SSE->getLHS(), BO_EQ, SSE->getRHS(), ExprType);
76 
77  bool WasEqual = SSE->getOpcode() == BO_EQ;
78  bool IsExpectedEqual = WasEqual == Assumption;
79 
80  const llvm::APSInt &Zero = getBasicVals().getValue(0, ExprType);
81 
82  if (IsExpectedEqual) {
83  return assumeSymNE(State, CanonicalEquality, Zero, Zero);
84  }
85 
86  return assumeSymEQ(State, CanonicalEquality, Zero, Zero);
87  }
88  }
89 
90  // If we get here, there's nothing else we can do but treat the symbol as
91  // opaque.
92  return assumeSymUnsupported(State, Sym, Assumption);
93 }
94 
96  ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
97  const llvm::APSInt &To, bool InRange) {
98 
99  Sym = simplify(State, Sym);
100 
101  // Get the type used for calculating wraparound.
103  APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
104 
105  llvm::APSInt Adjustment = WraparoundType.getZeroValue();
106  SymbolRef AdjustedSym = Sym;
107  computeAdjustment(AdjustedSym, Adjustment);
108 
109  // Convert the right-hand side integer as necessary.
110  APSIntType ComparisonType = std::max(WraparoundType, APSIntType(From));
111  llvm::APSInt ConvertedFrom = ComparisonType.convert(From);
112  llvm::APSInt ConvertedTo = ComparisonType.convert(To);
113 
114  // Prefer unsigned comparisons.
115  if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
116  ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
117  Adjustment.setIsSigned(false);
118 
119  if (InRange)
120  return assumeSymWithinInclusiveRange(State, AdjustedSym, ConvertedFrom,
121  ConvertedTo, Adjustment);
122  return assumeSymOutsideInclusiveRange(State, AdjustedSym, ConvertedFrom,
123  ConvertedTo, Adjustment);
124 }
125 
128  SymbolRef Sym, bool Assumption) {
129  Sym = simplify(State, Sym);
130 
132  QualType T = Sym->getType();
133 
134  // Non-integer types are not supported.
135  if (!T->isIntegralOrEnumerationType())
136  return State;
137 
138  // Reverse the operation and add directly to state.
139  const llvm::APSInt &Zero = BVF.getValue(0, T);
140  if (Assumption)
141  return assumeSymNE(State, Sym, Zero, Zero);
142  else
143  return assumeSymEQ(State, Sym, Zero, Zero);
144 }
145 
147  SymbolRef Sym,
149  const llvm::APSInt &Int) {
150  assert(BinaryOperator::isComparisonOp(Op) &&
151  "Non-comparison ops should be rewritten as comparisons to zero.");
152 
153  // Simplification: translate an assume of a constraint of the form
154  // "(exp comparison_op expr) != 0" to true into an assume of
155  // "exp comparison_op expr" to true. (And similarly, an assume of the form
156  // "(exp comparison_op expr) == 0" to true into an assume of
157  // "exp comparison_op expr" to false.)
158  if (Int == 0 && (Op == BO_EQ || Op == BO_NE)) {
159  if (const BinarySymExpr *SE = dyn_cast<BinarySymExpr>(Sym))
160  if (BinaryOperator::isComparisonOp(SE->getOpcode()))
161  return assumeSym(State, Sym, (Op == BO_NE ? true : false));
162  }
163 
164  // Get the type used for calculating wraparound.
166  APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
167 
168  // We only handle simple comparisons of the form "$sym == constant"
169  // or "($sym+constant1) == constant2".
170  // The adjustment is "constant1" in the above expression. It's used to
171  // "slide" the solution range around for modular arithmetic. For example,
172  // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which
173  // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to
174  // the subclasses of SimpleConstraintManager to handle the adjustment.
175  llvm::APSInt Adjustment = WraparoundType.getZeroValue();
176  computeAdjustment(Sym, Adjustment);
177 
178  // Convert the right-hand side integer as necessary.
179  APSIntType ComparisonType = std::max(WraparoundType, APSIntType(Int));
180  llvm::APSInt ConvertedInt = ComparisonType.convert(Int);
181 
182  // Prefer unsigned comparisons.
183  if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
184  ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
185  Adjustment.setIsSigned(false);
186 
187  switch (Op) {
188  default:
189  llvm_unreachable("invalid operation not caught by assertion above");
190 
191  case BO_EQ:
192  return assumeSymEQ(State, Sym, ConvertedInt, Adjustment);
193 
194  case BO_NE:
195  return assumeSymNE(State, Sym, ConvertedInt, Adjustment);
196 
197  case BO_GT:
198  return assumeSymGT(State, Sym, ConvertedInt, Adjustment);
199 
200  case BO_GE:
201  return assumeSymGE(State, Sym, ConvertedInt, Adjustment);
202 
203  case BO_LT:
204  return assumeSymLT(State, Sym, ConvertedInt, Adjustment);
205 
206  case BO_LE:
207  return assumeSymLE(State, Sym, ConvertedInt, Adjustment);
208  } // end switch
209 }
210 
211 void RangedConstraintManager::computeAdjustment(SymbolRef &Sym,
212  llvm::APSInt &Adjustment) {
213  // Is it a "($sym+constant1)" expression?
214  if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(Sym)) {
215  BinaryOperator::Opcode Op = SE->getOpcode();
216  if (Op == BO_Add || Op == BO_Sub) {
217  Sym = SE->getLHS();
218  Adjustment = APSIntType(Adjustment).convert(SE->getRHS());
219 
220  // Don't forget to negate the adjustment if it's being subtracted.
221  // This should happen /after/ promotion, in case the value being
222  // subtracted is, say, CHAR_MIN, and the promoted type is 'int'.
223  if (Op == BO_Sub)
224  Adjustment = -Adjustment;
225  }
226  }
227 }
228 
230  SValBuilder &SVB = State->getStateManager().getSValBuilder();
231  return SVB.simplifySVal(State, SVB.makeSymbolVal(Sym));
232 }
233 
235  SVal SimplifiedVal = simplifyToSVal(State, Sym);
236  if (SymbolRef SimplifiedSym = SimplifiedVal.getAsSymbol())
237  return SimplifiedSym;
238  return Sym;
239 }
240 
241 } // end of namespace ento
242 } // end of namespace clang
clang::ento::Loc::isLocType
static bool isLocType(QualType T)
Definition: SVals.h:336
max
__DEVICE__ int max(int __a, int __b)
Definition: __clang_cuda_math.h:196
clang::ento::BasicValueFactory
Definition: BasicValueFactory.h:108
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:294
clang::ento::SValBuilder::makeSymbolVal
SVal makeSymbolVal(SymbolRef Sym)
Make an SVal that represents the given symbol.
Definition: SValBuilder.h:399
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
clang::BinaryOperator::reverseComparisonOp
static Opcode reverseComparisonOp(Opcode Opc)
Definition: Expr.h:3921
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
APSInt
llvm::APSInt APSInt
Definition: ByteCodeEmitter.cpp:19
clang::BinaryOperator::isEqualityOp
bool isEqualityOp() const
Definition: Expr.h:3900
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:78
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:229
clang::BinaryOperator::negateComparisonOp
static Opcode negateComparisonOp(Opcode Opc)
Definition: Expr.h:3908
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:146
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:3903
State
LineState State
Definition: UnwrappedLineFormatter.cpp:986
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:127
clang::BinaryOperatorKind
BinaryOperatorKind
Definition: OperationKinds.h:25
clang::ento::SimpleConstraintManager::getSymbolManager
SymbolManager & getSymbolManager() const
Definition: SimpleConstraintManager.h:79
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:95
clang::ASTContext::getPointerDiffType
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
Definition: ASTContext.cpp:5756
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:75
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:143
llvm::IntrusiveRefCntPtr< const ProgramState >
clang::Type::isIntegralOrEnumerationType
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:7059
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:234