blob: 82801eb05d38369c69ddc26a10f834a50609517a [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 Kremeneka591bc02009-06-18 22:57:13 +000058const GRState *SimpleConstraintManager::Assume(const GRState *state,
59 SVal Cond, bool Assumption) {
Ted Kremenek45021952009-02-14 17:08:39 +000060 if (Cond.isUnknown()) {
Ted Kremeneka591bc02009-06-18 22:57:13 +000061 return state;
Ted Kremenek45021952009-02-14 17:08:39 +000062 }
63
64 if (isa<NonLoc>(Cond))
Ted Kremeneka591bc02009-06-18 22:57:13 +000065 return Assume(state, cast<NonLoc>(Cond), Assumption);
Ted Kremenek45021952009-02-14 17:08:39 +000066 else
Ted Kremeneka591bc02009-06-18 22:57:13 +000067 return Assume(state, cast<Loc>(Cond), Assumption);
Ted Kremenek45021952009-02-14 17:08:39 +000068}
69
Ted Kremeneka591bc02009-06-18 22:57:13 +000070const GRState *SimpleConstraintManager::Assume(const GRState *state, Loc Cond,
71 bool Assumption) {
72
73 state = AssumeAux(state, Cond, Assumption);
74
Ted Kremenek45021952009-02-14 17:08:39 +000075 // EvalAssume is used to call into the GRTransferFunction object to perform
76 // any checker-specific update of the state based on this assumption being
Ted Kremeneka591bc02009-06-18 22:57:13 +000077 // true or false.
78 return state ? state->getTransferFuncs().EvalAssume(state, Cond, Assumption)
79 : NULL;
Ted Kremenek45021952009-02-14 17:08:39 +000080}
81
Ted Kremeneka591bc02009-06-18 22:57:13 +000082const GRState *SimpleConstraintManager::AssumeAux(const GRState *state,
83 Loc Cond, bool Assumption) {
84
85 BasicValueFactory &BasicVals = state->getBasicVals();
Ted Kremenek45021952009-02-14 17:08:39 +000086
87 switch (Cond.getSubKind()) {
88 default:
89 assert (false && "'Assume' not implemented for this Loc.");
Ted Kremeneka591bc02009-06-18 22:57:13 +000090 return state;
Ted Kremenek45021952009-02-14 17:08:39 +000091
Ted Kremenek45021952009-02-14 17:08:39 +000092 case loc::MemRegionKind: {
93 // FIXME: Should this go into the storemanager?
94
Ted Kremeneka591bc02009-06-18 22:57:13 +000095 const MemRegion *R = cast<loc::MemRegionVal>(Cond).getRegion();
96 const SubRegion *SubR = dyn_cast<SubRegion>(R);
Ted Kremenek45021952009-02-14 17:08:39 +000097
98 while (SubR) {
99 // FIXME: now we only find the first symbolic region.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000100 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SubR)) {
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000101 if (Assumption)
Ted Kremeneka591bc02009-06-18 22:57:13 +0000102 return AssumeSymNE(state, SymR->getSymbol(),
103 BasicVals.getZeroWithPtrWidth());
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000104 else
Ted Kremeneka591bc02009-06-18 22:57:13 +0000105 return AssumeSymEQ(state, SymR->getSymbol(),
106 BasicVals.getZeroWithPtrWidth());
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000107 }
Ted Kremenek45021952009-02-14 17:08:39 +0000108 SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
109 }
110
111 // FALL-THROUGH.
112 }
113
Ted Kremenek45021952009-02-14 17:08:39 +0000114 case loc::GotoLabelKind:
Ted Kremeneka591bc02009-06-18 22:57:13 +0000115 return Assumption ? state : NULL;
Ted Kremenek45021952009-02-14 17:08:39 +0000116
117 case loc::ConcreteIntKind: {
Ted Kremeneka591bc02009-06-18 22:57:13 +0000118 bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0;
119 bool isFeasible = b ? Assumption : !Assumption;
120 return isFeasible ? state : NULL;
Ted Kremenek45021952009-02-14 17:08:39 +0000121 }
122 } // end switch
123}
124
Ted Kremeneka591bc02009-06-18 22:57:13 +0000125const GRState *SimpleConstraintManager::Assume(const GRState *state,
126 NonLoc Cond,
127 bool Assumption) {
128
129 state = AssumeAux(state, Cond, Assumption);
130
Ted Kremenek45021952009-02-14 17:08:39 +0000131 // EvalAssume is used to call into the GRTransferFunction object to perform
132 // any checker-specific update of the state based on this assumption being
Ted Kremeneka591bc02009-06-18 22:57:13 +0000133 // true or false.
134 return state ? state->getTransferFuncs().EvalAssume(state, Cond, Assumption)
135 : NULL;
Ted Kremenek45021952009-02-14 17:08:39 +0000136}
137
Ted Kremeneka591bc02009-06-18 22:57:13 +0000138const GRState *SimpleConstraintManager::AssumeAux(const GRState *state,
139 NonLoc Cond,
140 bool Assumption) {
141
Zhongxing Xua129eb92009-03-25 05:58:37 +0000142 // We cannot reason about SymIntExpr and SymSymExpr.
143 if (!canReasonAbout(Cond)) {
Ted Kremeneka591bc02009-06-18 22:57:13 +0000144 // Just return the current state indicating that the path is feasible.
145 // This may be an over-approximation of what is possible.
146 return state;
Zhongxing Xua129eb92009-03-25 05:58:37 +0000147 }
148
Ted Kremeneka591bc02009-06-18 22:57:13 +0000149 BasicValueFactory &BasicVals = state->getBasicVals();
150 SymbolManager &SymMgr = state->getSymbolManager();
Ted Kremenek45021952009-02-14 17:08:39 +0000151
152 switch (Cond.getSubKind()) {
153 default:
154 assert(false && "'Assume' not implemented for this NonLoc");
155
156 case nonloc::SymbolValKind: {
157 nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond);
158 SymbolRef sym = SV.getSymbol();
Ted Kremeneka591bc02009-06-18 22:57:13 +0000159 QualType T = SymMgr.getType(sym);
160 const llvm::APSInt &zero = BasicVals.getValue(0, T);
161
162 return Assumption ? AssumeSymNE(state, sym, zero)
163 : AssumeSymEQ(state, sym, zero);
Ted Kremenek45021952009-02-14 17:08:39 +0000164 }
165
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000166 case nonloc::SymExprValKind: {
167 nonloc::SymExprVal V = cast<nonloc::SymExprVal>(Cond);
168 if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(V.getSymbolicExpression()))
Ted Kremeneka591bc02009-06-18 22:57:13 +0000169 return AssumeSymInt(state, Assumption, SE);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000170
Ted Kremeneka591bc02009-06-18 22:57:13 +0000171 // For all other symbolic expressions, over-approximate and consider
172 // the constraint feasible.
173 return state;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000174 }
Ted Kremenek45021952009-02-14 17:08:39 +0000175
176 case nonloc::ConcreteIntKind: {
177 bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0;
Ted Kremeneka591bc02009-06-18 22:57:13 +0000178 bool isFeasible = b ? Assumption : !Assumption;
179 return isFeasible ? state : NULL;
Ted Kremenek45021952009-02-14 17:08:39 +0000180 }
181
182 case nonloc::LocAsIntegerKind:
Ted Kremeneka591bc02009-06-18 22:57:13 +0000183 return AssumeAux(state, cast<nonloc::LocAsInteger>(Cond).getLoc(),
184 Assumption);
Ted Kremenek45021952009-02-14 17:08:39 +0000185 } // end switch
186}
187
Ted Kremeneka591bc02009-06-18 22:57:13 +0000188const GRState *SimpleConstraintManager::AssumeSymInt(const GRState *state,
189 bool Assumption,
190 const SymIntExpr *SE) {
Ted Kremenek45021952009-02-14 17:08:39 +0000191
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000192
193 // Here we assume that LHS is a symbol. This is consistent with the
194 // rest of the constraint manager logic.
195 SymbolRef Sym = cast<SymbolData>(SE->getLHS());
196 const llvm::APSInt &Int = SE->getRHS();
197
198 switch (SE->getOpcode()) {
Ted Kremenek45021952009-02-14 17:08:39 +0000199 default:
Ted Kremeneka591bc02009-06-18 22:57:13 +0000200 // No logic yet for other operators. Assume the constraint is feasible.
201 return state;
Ted Kremenek45021952009-02-14 17:08:39 +0000202
203 case BinaryOperator::EQ:
Ted Kremeneka591bc02009-06-18 22:57:13 +0000204 return Assumption ? AssumeSymEQ(state, Sym, Int)
205 : AssumeSymNE(state, Sym, Int);
Ted Kremenek45021952009-02-14 17:08:39 +0000206
207 case BinaryOperator::NE:
Ted Kremeneka591bc02009-06-18 22:57:13 +0000208 return Assumption ? AssumeSymNE(state, Sym, Int)
209 : AssumeSymEQ(state, Sym, Int);
Ted Kremenek45021952009-02-14 17:08:39 +0000210 case BinaryOperator::GT:
Ted Kremeneka591bc02009-06-18 22:57:13 +0000211 return Assumption ? AssumeSymGT(state, Sym, Int)
212 : AssumeSymLE(state, Sym, Int);
Ted Kremenek45021952009-02-14 17:08:39 +0000213
214 case BinaryOperator::GE:
Ted Kremeneka591bc02009-06-18 22:57:13 +0000215 return Assumption ? AssumeSymGE(state, Sym, Int)
216 : AssumeSymLT(state, Sym, Int);
Ted Kremenek45021952009-02-14 17:08:39 +0000217
218 case BinaryOperator::LT:
Ted Kremeneka591bc02009-06-18 22:57:13 +0000219 return Assumption ? AssumeSymLT(state, Sym, Int)
220 : AssumeSymGE(state, Sym, Int);
Ted Kremenek45021952009-02-14 17:08:39 +0000221
222 case BinaryOperator::LE:
Ted Kremeneka591bc02009-06-18 22:57:13 +0000223 return Assumption ? AssumeSymLE(state, Sym, Int)
224 : AssumeSymGT(state, Sym, Int);
Ted Kremenek45021952009-02-14 17:08:39 +0000225 } // end switch
226}
227
Ted Kremeneka591bc02009-06-18 22:57:13 +0000228const GRState *SimpleConstraintManager::AssumeInBound(const GRState *state,
229 SVal Idx,
230 SVal UpperBound,
231 bool Assumption) {
232
Ted Kremenek45021952009-02-14 17:08:39 +0000233 // Only support ConcreteInt for now.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000234 if (!(isa<nonloc::ConcreteInt>(Idx) && isa<nonloc::ConcreteInt>(UpperBound)))
235 return state;
Ted Kremenek45021952009-02-14 17:08:39 +0000236
Ted Kremenekf1b82272009-06-18 23:20:05 +0000237 const llvm::APSInt& Zero = state->getBasicVals().getZeroWithPtrWidth(false);
Ted Kremenek45021952009-02-14 17:08:39 +0000238 llvm::APSInt IdxV = cast<nonloc::ConcreteInt>(Idx).getValue();
239 // IdxV might be too narrow.
240 if (IdxV.getBitWidth() < Zero.getBitWidth())
241 IdxV.extend(Zero.getBitWidth());
242 // UBV might be too narrow, too.
243 llvm::APSInt UBV = cast<nonloc::ConcreteInt>(UpperBound).getValue();
244 if (UBV.getBitWidth() < Zero.getBitWidth())
245 UBV.extend(Zero.getBitWidth());
246
247 bool InBound = (Zero <= IdxV) && (IdxV < UBV);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000248 bool isFeasible = Assumption ? InBound : !InBound;
249 return isFeasible ? state : NULL;
Ted Kremenek45021952009-02-14 17:08:39 +0000250}
251
252} // end of namespace clang