blob: f79dba0cc5eee7be4aa08ce143ab82e1df7f53e1 [file] [log] [blame]
Ted Kremenek45021952009-02-14 17:08:39 +00001//== SimpleConstraintManager.cpp --------------------------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines SimpleConstraintManager, a class that holds code shared
11// between BasicConstraintManager and RangeConstraintManager.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SimpleConstraintManager.h"
16#include "clang/Analysis/PathSensitive/GRExprEngine.h"
17#include "clang/Analysis/PathSensitive/GRState.h"
18
19namespace clang {
20
21SimpleConstraintManager::~SimpleConstraintManager() {}
22
Ted Kremenek66b52712009-03-11 02:22:59 +000023bool SimpleConstraintManager::canReasonAbout(SVal X) const {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000024 if (nonloc::SymExprVal *SymVal = dyn_cast<nonloc::SymExprVal>(&X)) {
25 const SymExpr *SE = SymVal->getSymbolicExpression();
26
27 if (isa<SymbolData>(SE))
28 return true;
29
30 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
31 switch (SIE->getOpcode()) {
32 // We don't reason yet about bitwise-constraints on symbolic values.
33 case BinaryOperator::And:
34 case BinaryOperator::Or:
35 case BinaryOperator::Xor:
36 return false;
37 // We don't reason yet about arithmetic constraints on symbolic values.
38 case BinaryOperator::Mul:
39 case BinaryOperator::Div:
40 case BinaryOperator::Rem:
41 case BinaryOperator::Add:
42 case BinaryOperator::Sub:
43 case BinaryOperator::Shl:
44 case BinaryOperator::Shr:
45 return false;
46 // All other cases.
47 default:
48 return true;
49 }
50 }
51
52 return false;
Ted Kremenek7de20fe2009-03-11 02:29:48 +000053 }
54
Ted Kremenek66b52712009-03-11 02:22:59 +000055 return true;
56}
57
Ted Kremenek45021952009-02-14 17:08:39 +000058const GRState*
59SimpleConstraintManager::Assume(const GRState* St, SVal Cond, bool Assumption,
60 bool& isFeasible) {
61 if (Cond.isUnknown()) {
62 isFeasible = true;
63 return St;
64 }
65
66 if (isa<NonLoc>(Cond))
67 return Assume(St, cast<NonLoc>(Cond), Assumption, isFeasible);
68 else
69 return Assume(St, cast<Loc>(Cond), Assumption, isFeasible);
70}
71
72const GRState*
73SimpleConstraintManager::Assume(const GRState* St, Loc Cond, bool Assumption,
74 bool& isFeasible) {
75 St = AssumeAux(St, Cond, Assumption, isFeasible);
76
77 if (!isFeasible)
78 return St;
79
80 // EvalAssume is used to call into the GRTransferFunction object to perform
81 // any checker-specific update of the state based on this assumption being
82 // true or false.
83 return StateMgr.getTransferFuncs().EvalAssume(StateMgr, St, Cond, Assumption,
84 isFeasible);
85}
86
87const GRState*
88SimpleConstraintManager::AssumeAux(const GRState* St, Loc Cond, bool Assumption,
89 bool& isFeasible) {
90 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
91
92 switch (Cond.getSubKind()) {
93 default:
94 assert (false && "'Assume' not implemented for this Loc.");
95 return St;
96
Ted Kremenek45021952009-02-14 17:08:39 +000097 case loc::MemRegionKind: {
98 // FIXME: Should this go into the storemanager?
99
100 const MemRegion* R = cast<loc::MemRegionVal>(Cond).getRegion();
101 const SubRegion* SubR = dyn_cast<SubRegion>(R);
102
103 while (SubR) {
104 // FIXME: now we only find the first symbolic region.
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000105 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(SubR)) {
106 if (Assumption)
107 return AssumeSymNE(St, SymR->getSymbol(),
108 BasicVals.getZeroWithPtrWidth(), isFeasible);
109 else
110 return AssumeSymEQ(St, SymR->getSymbol(),
111 BasicVals.getZeroWithPtrWidth(), isFeasible);
112 }
Ted Kremenek45021952009-02-14 17:08:39 +0000113 SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
114 }
115
116 // FALL-THROUGH.
117 }
118
Ted Kremenek45021952009-02-14 17:08:39 +0000119 case loc::GotoLabelKind:
120 isFeasible = Assumption;
121 return St;
122
123 case loc::ConcreteIntKind: {
124 bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0;
125 isFeasible = b ? Assumption : !Assumption;
126 return St;
127 }
128 } // end switch
129}
130
131const GRState*
132SimpleConstraintManager::Assume(const GRState* St, NonLoc Cond, bool Assumption,
133 bool& isFeasible) {
134 St = AssumeAux(St, Cond, Assumption, isFeasible);
135
136 if (!isFeasible)
137 return St;
138
139 // EvalAssume is used to call into the GRTransferFunction object to perform
140 // any checker-specific update of the state based on this assumption being
141 // true or false.
142 return StateMgr.getTransferFuncs().EvalAssume(StateMgr, St, Cond, Assumption,
143 isFeasible);
144}
145
146const GRState*
147SimpleConstraintManager::AssumeAux(const GRState* St,NonLoc Cond,
148 bool Assumption, bool& isFeasible) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000149 // We cannot reason about SymIntExpr and SymSymExpr.
150 if (!canReasonAbout(Cond)) {
151 isFeasible = true;
152 return St;
153 }
154
Ted Kremenek45021952009-02-14 17:08:39 +0000155 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
156 SymbolManager& SymMgr = StateMgr.getSymbolManager();
157
158 switch (Cond.getSubKind()) {
159 default:
160 assert(false && "'Assume' not implemented for this NonLoc");
161
162 case nonloc::SymbolValKind: {
163 nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond);
164 SymbolRef sym = SV.getSymbol();
165 QualType T = SymMgr.getType(sym);
166
167 if (Assumption)
168 return AssumeSymNE(St, sym, BasicVals.getValue(0, T), isFeasible);
169 else
170 return AssumeSymEQ(St, sym, BasicVals.getValue(0, T), isFeasible);
171 }
172
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000173 case nonloc::SymExprValKind: {
174 nonloc::SymExprVal V = cast<nonloc::SymExprVal>(Cond);
175 if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(V.getSymbolicExpression()))
176 return AssumeSymInt(St, Assumption, SE, isFeasible);
177
178 isFeasible = true;
179 return St;
180 }
Ted Kremenek45021952009-02-14 17:08:39 +0000181
182 case nonloc::ConcreteIntKind: {
183 bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0;
184 isFeasible = b ? Assumption : !Assumption;
185 return St;
186 }
187
188 case nonloc::LocAsIntegerKind:
189 return AssumeAux(St, cast<nonloc::LocAsInteger>(Cond).getLoc(),
190 Assumption, isFeasible);
191 } // end switch
192}
193
194const GRState*
195SimpleConstraintManager::AssumeSymInt(const GRState* St, bool Assumption,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000196 const SymIntExpr *SE, bool& isFeasible) {
Ted Kremenek45021952009-02-14 17:08:39 +0000197
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000198
199 // Here we assume that LHS is a symbol. This is consistent with the
200 // rest of the constraint manager logic.
201 SymbolRef Sym = cast<SymbolData>(SE->getLHS());
202 const llvm::APSInt &Int = SE->getRHS();
203
204 switch (SE->getOpcode()) {
Ted Kremenek45021952009-02-14 17:08:39 +0000205 default:
206 // No logic yet for other operators.
207 isFeasible = true;
208 return St;
209
210 case BinaryOperator::EQ:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000211 return Assumption ? AssumeSymEQ(St, Sym, Int, isFeasible)
212 : AssumeSymNE(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000213
214 case BinaryOperator::NE:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000215 return Assumption ? AssumeSymNE(St, Sym, Int, isFeasible)
216 : AssumeSymEQ(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000217
218 case BinaryOperator::GT:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000219 return Assumption ? AssumeSymGT(St, Sym, Int, isFeasible)
220 : AssumeSymLE(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000221
222 case BinaryOperator::GE:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000223 return Assumption ? AssumeSymGE(St, Sym, Int, isFeasible)
224 : AssumeSymLT(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000225
226 case BinaryOperator::LT:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000227 return Assumption ? AssumeSymLT(St, Sym, Int, isFeasible)
228 : AssumeSymGE(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000229
230 case BinaryOperator::LE:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000231 return Assumption ? AssumeSymLE(St, Sym, Int, isFeasible)
232 : AssumeSymGT(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000233 } // end switch
234}
235
236const GRState*
237SimpleConstraintManager::AssumeInBound(const GRState* St, SVal Idx,
238 SVal UpperBound, bool Assumption,
239 bool& isFeasible) {
240 // Only support ConcreteInt for now.
241 if (!(isa<nonloc::ConcreteInt>(Idx) && isa<nonloc::ConcreteInt>(UpperBound))){
242 isFeasible = true;
243 return St;
244 }
245
246 const llvm::APSInt& Zero = getBasicVals().getZeroWithPtrWidth(false);
247 llvm::APSInt IdxV = cast<nonloc::ConcreteInt>(Idx).getValue();
248 // IdxV might be too narrow.
249 if (IdxV.getBitWidth() < Zero.getBitWidth())
250 IdxV.extend(Zero.getBitWidth());
251 // UBV might be too narrow, too.
252 llvm::APSInt UBV = cast<nonloc::ConcreteInt>(UpperBound).getValue();
253 if (UBV.getBitWidth() < Zero.getBitWidth())
254 UBV.extend(Zero.getBitWidth());
255
256 bool InBound = (Zero <= IdxV) && (IdxV < UBV);
257
258 isFeasible = Assumption ? InBound : !InBound;
259
260 return St;
261}
262
263} // end of namespace clang