clang 10.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
14 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h"
16
17 namespace clang {
18
19 namespace ento {
20
21 RangedConstraintManager::~RangedConstraintManager() {}
22
23 ProgramStateRef RangedConstraintManager::assumeSym(ProgramStateRef State,
24 SymbolRef Sym,
25 bool Assumption) {
26 // Handle SymbolData.
27 if (isa<SymbolData>(Sym)) {
28 return assumeSymUnsupported(State, Sym, Assumption);
29
30 // Handle symbolic expression.
31 } else if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(Sym)) {
32 // We can only simplify expressions whose RHS is an integer.
33
34 BinaryOperator::Opcode op = SIE->getOpcode();
35 if (BinaryOperator::isComparisonOp(op) && op != BO_Cmp) {
36 if (!Assumption)
37 op = BinaryOperator::negateComparisonOp(op);
38
39 return assumeSymRel(State, SIE->getLHS(), op, SIE->getRHS());
40 }
41
42 } else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(Sym)) {
43 // Translate "a != b" to "(b - a) != 0".
44 // We invert the order of the operands as a heuristic for how loop
45 // conditions are usually written ("begin != end") as compared to length
46 // calculations ("end - begin"). The more correct thing to do would be to
47 // canonicalize "a - b" and "b - a", which would allow us to treat
48 // "a != b" and "b != a" the same.
49 SymbolManager &SymMgr = getSymbolManager();
50 BinaryOperator::Opcode Op = SSE->getOpcode();
51 assert(BinaryOperator::isComparisonOp(Op));
52
53 // For now, we only support comparing pointers.
54 if (Loc::isLocType(SSE->getLHS()->getType()) &&
55 Loc::isLocType(SSE->getRHS()->getType())) {
56 QualType DiffTy = SymMgr.getContext().getPointerDiffType();
57 SymbolRef Subtraction =
58 SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub, SSE->getLHS(), DiffTy);
59
60 const llvm::APSInt &Zero = getBasicVals().getValue(0, DiffTy);
61 Op = BinaryOperator::reverseComparisonOp(Op);
62 if (!Assumption)
63 Op = BinaryOperator::negateComparisonOp(Op);
64 return assumeSymRel(State, Subtraction, Op, Zero);
65 }
66 }
67
68 // If we get here, there's nothing else we can do but treat the symbol as
69 // opaque.
70 return assumeSymUnsupported(State, Sym, Assumption);
71 }
72
73 ProgramStateRef RangedConstraintManager::assumeSymInclusiveRange(
74 ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
75 const llvm::APSInt &To, bool InRange) {
76 // Get the type used for calculating wraparound.
77 BasicValueFactory &BVF = getBasicVals();
78 APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
79
80 llvm::APSInt Adjustment = WraparoundType.getZeroValue();
81 SymbolRef AdjustedSym = Sym;
82 computeAdjustment(AdjustedSym, Adjustment);
83
84 // Convert the right-hand side integer as necessary.
85 APSIntType ComparisonType = std::max(WraparoundType, APSIntType(From));
86 llvm::APSInt ConvertedFrom = ComparisonType.convert(From);
87 llvm::APSInt ConvertedTo = ComparisonType.convert(To);
88
89 // Prefer unsigned comparisons.
90 if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
91 ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
92 Adjustment.setIsSigned(false);
93
94 if (InRange)
95 return assumeSymWithinInclusiveRange(State, AdjustedSym, ConvertedFrom,
96 ConvertedTo, Adjustment);
97 return assumeSymOutsideInclusiveRange(State, AdjustedSym, ConvertedFrom,
98 ConvertedTo, Adjustment);
99 }
100
101 ProgramStateRef
102 RangedConstraintManager::assumeSymUnsupported(ProgramStateRef State,
103 SymbolRef Sym, bool Assumption) {
104 BasicValueFactory &BVF = getBasicVals();
105 QualType T = Sym->getType();
106
107 // Non-integer types are not supported.
108 if (!T->isIntegralOrEnumerationType())
109 return State;
110
111 // Reverse the operation and add directly to state.
112 const llvm::APSInt &Zero = BVF.getValue(0, T);
113 if (Assumption)
114 return assumeSymNE(State, Sym, Zero, Zero);
115 else
116 return assumeSymEQ(State, Sym, Zero, Zero);
117 }
118
119 ProgramStateRef RangedConstraintManager::assumeSymRel(ProgramStateRef State,
120 SymbolRef Sym,
121 BinaryOperator::Opcode Op,
122 const llvm::APSInt &Int) {
123 assert(BinaryOperator::isComparisonOp(Op) &&
124 "Non-comparison ops should be rewritten as comparisons to zero.");
125
126 // Simplification: translate an assume of a constraint of the form
127 // "(exp comparison_op expr) != 0" to true into an assume of
128 // "exp comparison_op expr" to true. (And similarly, an assume of the form
129 // "(exp comparison_op expr) == 0" to true into an assume of
130 // "exp comparison_op expr" to false.)
131 if (Int == 0 && (Op == BO_EQ || Op == BO_NE)) {
132 if (const BinarySymExpr *SE = dyn_cast<BinarySymExpr>(Sym))
133 if (BinaryOperator::isComparisonOp(SE->getOpcode()))
134 return assumeSym(State, Sym, (Op == BO_NE ? true : false));
135 }
136
137 // Get the type used for calculating wraparound.
138 BasicValueFactory &BVF = getBasicVals();
139 APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
140
141 // We only handle simple comparisons of the form "$sym == constant"
142 // or "($sym+constant1) == constant2".
143 // The adjustment is "constant1" in the above expression. It's used to
144 // "slide" the solution range around for modular arithmetic. For example,
145 // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which
146 // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to
147 // the subclasses of SimpleConstraintManager to handle the adjustment.
148 llvm::APSInt Adjustment = WraparoundType.getZeroValue();
149 computeAdjustment(Sym, Adjustment);
150
151 // Convert the right-hand side integer as necessary.
152 APSIntType ComparisonType = std::max(WraparoundType, APSIntType(Int));
153 llvm::APSInt ConvertedInt = ComparisonType.convert(Int);
154
155 // Prefer unsigned comparisons.
156 if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
157 ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
158 Adjustment.setIsSigned(false);
159
160 switch (Op) {
161 default:
162 llvm_unreachable("invalid operation not caught by assertion above");
163
164 case BO_EQ:
165 return assumeSymEQ(State, Sym, ConvertedInt, Adjustment);
166
167 case BO_NE:
168 return assumeSymNE(State, Sym, ConvertedInt, Adjustment);
169
170 case BO_GT:
171 return assumeSymGT(State, Sym, ConvertedInt, Adjustment);
172
173 case BO_GE:
174 return assumeSymGE(State, Sym, ConvertedInt, Adjustment);
175
176 case BO_LT:
177 return assumeSymLT(State, Sym, ConvertedInt, Adjustment);
178
179 case BO_LE:
180 return assumeSymLE(State, Sym, ConvertedInt, Adjustment);
181 } // end switch
182 }
183
184 void RangedConstraintManager::computeAdjustment(SymbolRef &Sym,
185 llvm::APSInt &Adjustment) {
186 // Is it a "($sym+constant1)" expression?
187 if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(Sym)) {
188 BinaryOperator::Opcode Op = SE->getOpcode();
189 if (Op == BO_Add || Op == BO_Sub) {
190 Sym = SE->getLHS();
191 Adjustment = APSIntType(Adjustment).convert(SE->getRHS());
192
193 // Don't forget to negate the adjustment if it's being subtracted.
194 // This should happen /after/ promotion, in case the value being
195 // subtracted is, say, CHAR_MIN, and the promoted type is 'int'.
196 if (Op == BO_Sub)
197 Adjustment = -Adjustment;
198 }
199 }
200 }
201
202 void *ProgramStateTrait<ConstraintRange>::GDMIndex() {
203 static int Index;
204 return &Index;
205 }
206
207 } // end of namespace ento
208
209 } // end of namespace clang
ento
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:654
RangedConstraintManager.h
clang::ASTContext::getPointerDiffType
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
Definition: ASTContext.cpp:5298
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::RangedConstraintManager::assumeSymEQ
virtual ProgramStateRef assumeSymEQ(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::BasicValueFactory
Definition: BasicValueFactory.h:106
clang::ento::SymExpr
Symbolic value.
Definition: SymExpr.h:29
clang::ento::SymbolManager::getContext
ASTContext & getContext()
Definition: SymbolManager.h:543
clang::ento::RangedConstraintManager::assumeSymLT
virtual ProgramStateRef assumeSymLT(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
clang::BinaryOperator::reverseComparisonOp
static Opcode reverseComparisonOp(Opcode Opc)
Definition: Expr.h:3543
State
LineState State
Definition: UnwrappedLineFormatter.cpp:932
clang::interp::Zero
bool Zero(InterpState &S, CodePtr OpPC)
Definition: Interp.h:812
max
__DEVICE__ int max(int __a, int __b)
Definition: __clang_cuda_device_functions.h:1628
clang::Type::isIntegralOrEnumerationType
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:6881
clang::ento::SymbolManager
Definition: SymbolManager.h:460
clang::ento::Loc::isLocType
static bool isLocType(QualType T)
Definition: SVals.h:329
clang::ento::ProgramStateTrait
Definition: ProgramState.h:52
clang::BinaryOperatorKind
BinaryOperatorKind
Definition: OperationKinds.h:25
clang::ento::APSIntType
A record of the "type" of an APSInt, used for conversions.
Definition: APSIntType.h:19
clang::ento::SymIntExpr
Represents a symbolic expression like &#39;x&#39; + 3.
Definition: SymbolManager.h:332
clang::ento::APSIntType::getZeroValue
llvm::APSInt getZeroValue() const LLVM_READONLY
Returns an all-zero value for this type.
Definition: APSIntType.h:55
clang::ento::SymExpr::getType
virtual QualType getType() const =0
clang::BinaryOperator::negateComparisonOp
static Opcode negateComparisonOp(Opcode Opc)
Definition: Expr.h:3530
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:102
clang::ento::SimpleConstraintManager::getBasicVals
BasicValueFactory & getBasicVals() const
Definition: SimpleConstraintManager.h:78
clang::ento::APSIntType::getBitWidth
uint32_t getBitWidth() const
Definition: APSIntType.h:30
ProgramState.h
clang::BinaryOperator::isComparisonOp
bool isComparisonOp() const
Definition: Expr.h:3525
clang::ento::SymbolManager::getSymSymExpr
const SymSymExpr * getSymSymExpr(const SymExpr *lhs, BinaryOperator::Opcode op, const SymExpr *rhs, QualType t)
Definition: SymbolManager.cpp:305
llvm::IntrusiveRefCntPtr< const ProgramState >
APSInt
llvm::APSInt APSInt
Definition: ByteCodeEmitter.cpp:18
clang::interp::InRange
bool InRange(InterpState &S, CodePtr OpPC)
Definition: Interp.h:264
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:119
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::convert
llvm::APSInt convert(const llvm::APSInt &Value) const LLVM_READONLY
Convert and return a new APSInt with the given value, but this type&#39;s bit width and signedness...
Definition: APSIntType.h:48
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::ento::SimpleConstraintManager::getSymbolManager
SymbolManager & getSymbolManager() const
Definition: SimpleConstraintManager.h:79
clang::ento::RangedConstraintManager::assumeSymGT
virtual ProgramStateRef assumeSymGT(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0
clang::ento::BinarySymExpr
Represents a symbolic expression involving a binary operator.
Definition: SymbolManager.h:303
clang::ento::RangedConstraintManager::~RangedConstraintManager
~RangedConstraintManager() override
Definition: RangedConstraintManager.cpp:21
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:141
clang::ento::APSIntType::isUnsigned
bool isUnsigned() const
Definition: APSIntType.h:31
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:73
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::ento::SymSymExpr
Represents a symbolic expression like &#39;x&#39; + &#39;y&#39;.
Definition: SymbolManager.h:418
clang::ento::RangedConstraintManager::assumeSymGE
virtual ProgramStateRef assumeSymGE(ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &V, const llvm::APSInt &Adjustment)=0