blob: 0576eaf2a14cfeb1ed4a10c9f8bebc5990858a88 [file] [log] [blame]
Zhongxing Xud19e21b2008-08-29 15:09:12 +00001//== BasicConstraintManager.cpp - Manage basic constraints.------*- 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 BasicConstraintManager, a class that tracks simple
11// equality and inequality constraints on symbolic values of GRState.
12//
13//===----------------------------------------------------------------------===//
14
Zhongxing Xu30ad1672008-08-27 14:03:33 +000015#include "clang/Analysis/PathSensitive/ConstraintManager.h"
16#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xu39cfed32008-08-29 14:52:36 +000017#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu30ad1672008-08-27 14:03:33 +000018#include "llvm/Support/Compiler.h"
Zhongxing Xu39cfed32008-08-29 14:52:36 +000019#include "llvm/Support/raw_ostream.h"
Zhongxing Xu30ad1672008-08-27 14:03:33 +000020
21using namespace clang;
22
23namespace {
24
Zhongxing Xu39cfed32008-08-29 14:52:36 +000025typedef llvm::ImmutableMap<SymbolID,GRState::IntSetTy> ConstNotEqTy;
26typedef llvm::ImmutableMap<SymbolID,const llvm::APSInt*> ConstEqTy;
27
Zhongxing Xu30ad1672008-08-27 14:03:33 +000028// BasicConstraintManager only tracks equality and inequality constraints of
29// constants and integer variables.
30class VISIBILITY_HIDDEN BasicConstraintManager : public ConstraintManager {
Zhongxing Xu30ad1672008-08-27 14:03:33 +000031 GRStateManager& StateMgr;
32
33public:
34 BasicConstraintManager(GRStateManager& statemgr) : StateMgr(statemgr) {}
35
36 virtual const GRState* Assume(const GRState* St, RVal Cond,
37 bool Assumption, bool& isFeasible);
38
39 const GRState* Assume(const GRState* St, LVal Cond, bool Assumption,
40 bool& isFeasible);
41
42 const GRState* AssumeAux(const GRState* St, LVal Cond,bool Assumption,
43 bool& isFeasible);
44
45 const GRState* Assume(const GRState* St, NonLVal Cond, bool Assumption,
46 bool& isFeasible);
47
48 const GRState* AssumeAux(const GRState* St, NonLVal Cond, bool Assumption,
49 bool& isFeasible);
50
51 const GRState* AssumeSymInt(const GRState* St, bool Assumption,
52 const SymIntConstraint& C, bool& isFeasible);
53
54 const GRState* AssumeSymNE(const GRState* St, SymbolID sym,
55 const llvm::APSInt& V, bool& isFeasible);
56
57 const GRState* AssumeSymEQ(const GRState* St, SymbolID sym,
58 const llvm::APSInt& V, bool& isFeasible);
59
60 const GRState* AssumeSymLT(const GRState* St, SymbolID sym,
61 const llvm::APSInt& V, bool& isFeasible);
62
63 const GRState* AssumeSymGT(const GRState* St, SymbolID sym,
64 const llvm::APSInt& V, bool& isFeasible);
65
66 const GRState* AssumeSymGE(const GRState* St, SymbolID sym,
67 const llvm::APSInt& V, bool& isFeasible);
68
69 const GRState* AssumeSymLE(const GRState* St, SymbolID sym,
70 const llvm::APSInt& V, bool& isFeasible);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000071
72 const GRState* AddEQ(const GRState* St, SymbolID sym, const llvm::APSInt& V);
73
74 const GRState* AddNE(const GRState* St, SymbolID sym, const llvm::APSInt& V);
75
76 const llvm::APSInt* getSymVal(const GRState* St, SymbolID sym);
77 bool isNotEqual(const GRState* St, SymbolID sym, const llvm::APSInt& V) const;
78 bool isEqual(const GRState* St, SymbolID sym, const llvm::APSInt& V) const;
79
80 const GRState* RemoveDeadBindings(const GRState* St,
81 StoreManager::LiveSymbolsTy& LSymbols,
82 StoreManager::DeadSymbolsTy& DSymbols);
83
84 void print(const GRState* St, std::ostream& Out,
85 const char* nl, const char *sep);
86};
Zhongxing Xu30ad1672008-08-27 14:03:33 +000087
88} // end anonymous namespace
89
90ConstraintManager* clang::CreateBasicConstraintManager(GRStateManager& StateMgr)
91{
92 return new BasicConstraintManager(StateMgr);
93}
94
95const GRState* BasicConstraintManager::Assume(const GRState* St, RVal Cond,
96 bool Assumption, bool& isFeasible) {
97 if (Cond.isUnknown()) {
98 isFeasible = true;
99 return St;
100 }
101
102 if (isa<NonLVal>(Cond))
103 return Assume(St, cast<NonLVal>(Cond), Assumption, isFeasible);
104 else
105 return Assume(St, cast<LVal>(Cond), Assumption, isFeasible);
106}
107
108const GRState* BasicConstraintManager::Assume(const GRState* St, LVal Cond,
109 bool Assumption, bool& isFeasible) {
110 St = AssumeAux(St, Cond, Assumption, isFeasible);
111 // TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
112 return St;
113}
114
115const GRState* BasicConstraintManager::AssumeAux(const GRState* St, LVal Cond,
116 bool Assumption, bool& isFeasible) {
117 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
118
119 switch (Cond.getSubKind()) {
120 default:
121 assert (false && "'Assume' not implemented for this LVal.");
122 return St;
123
124 case lval::SymbolValKind:
125 if (Assumption)
126 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
127 BasicVals.getZeroWithPtrWidth(), isFeasible);
128 else
129 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
130 BasicVals.getZeroWithPtrWidth(), isFeasible);
131
132 case lval::DeclValKind:
133 case lval::FuncValKind:
134 case lval::GotoLabelKind:
135 case lval::StringLiteralValKind:
136 isFeasible = Assumption;
137 return St;
138
139 case lval::FieldOffsetKind:
140 return AssumeAux(St, cast<lval::FieldOffset>(Cond).getBase(),
141 Assumption, isFeasible);
142
143 case lval::ArrayOffsetKind:
144 return AssumeAux(St, cast<lval::ArrayOffset>(Cond).getBase(),
145 Assumption, isFeasible);
146
147 case lval::ConcreteIntKind: {
148 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
149 isFeasible = b ? Assumption : !Assumption;
150 return St;
151 }
152 } // end switch
153}
154
155const GRState*
156BasicConstraintManager::Assume(const GRState* St, NonLVal Cond, bool Assumption,
157 bool& isFeasible) {
158 St = AssumeAux(St, Cond, Assumption, isFeasible);
159 // TF->EvalAssume() does nothing now.
160 return St;
161}
162
163const GRState*
164BasicConstraintManager::AssumeAux(const GRState* St,NonLVal Cond,
165 bool Assumption, bool& isFeasible) {
166 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
167 SymbolManager& SymMgr = StateMgr.getSymbolManager();
168
169 switch (Cond.getSubKind()) {
170 default:
171 assert(false && "'Assume' not implemented for this NonLVal");
172
173 case nonlval::SymbolValKind: {
174 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
175 SymbolID sym = SV.getSymbol();
176
177 if (Assumption)
178 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
179 isFeasible);
180 else
181 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
182 isFeasible);
183 }
184
185 case nonlval::SymIntConstraintValKind:
186 return
187 AssumeSymInt(St, Assumption,
188 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
189 isFeasible);
190
191 case nonlval::ConcreteIntKind: {
192 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
193 isFeasible = b ? Assumption : !Assumption;
194 return St;
195 }
196
197 case nonlval::LValAsIntegerKind:
198 return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(),
199 Assumption, isFeasible);
200 } // end switch
201}
202
203const GRState*
204BasicConstraintManager::AssumeSymInt(const GRState* St, bool Assumption,
205 const SymIntConstraint& C, bool& isFeasible) {
206
207 switch (C.getOpcode()) {
208 default:
209 // No logic yet for other operators.
210 isFeasible = true;
211 return St;
212
213 case BinaryOperator::EQ:
214 if (Assumption)
215 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
216 else
217 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
218
219 case BinaryOperator::NE:
220 if (Assumption)
221 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
222 else
223 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
224
Zhongxing Xu94b83122008-09-19 06:07:59 +0000225 case BinaryOperator::GT:
226 if (Assumption)
227 return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible);
228 else
229 return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible);
230
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000231 case BinaryOperator::GE:
232 if (Assumption)
233 return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible);
234 else
235 return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible);
236
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000237 case BinaryOperator::LT:
238 if (Assumption)
239 return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible);
240 else
241 return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible);
242
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000243 case BinaryOperator::LE:
244 if (Assumption)
245 return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible);
246 else
247 return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible);
248 } // end switch
249}
250
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000251
252
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000253const GRState*
254BasicConstraintManager::AssumeSymNE(const GRState* St, SymbolID sym,
255 const llvm::APSInt& V, bool& isFeasible) {
256 // First, determine if sym == X, where X != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000257 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000258 isFeasible = (*X != V);
259 return St;
260 }
261
262 // Second, determine if sym != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000263 if (isNotEqual(St, sym, V)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000264 isFeasible = true;
265 return St;
266 }
267
268 // If we reach here, sym is not a constant and we don't know if it is != V.
269 // Make that assumption.
270 isFeasible = true;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000271 return AddNE(St, sym, V);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000272}
273
274const GRState*
275BasicConstraintManager::AssumeSymEQ(const GRState* St, SymbolID sym,
276 const llvm::APSInt& V, bool& isFeasible) {
277 // First, determine if sym == X, where X != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000278 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000279 isFeasible = *X == V;
280 return St;
281 }
282
283 // Second, determine if sym != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000284 if (isNotEqual(St, sym, V)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000285 isFeasible = false;
286 return St;
287 }
288
289 // If we reach here, sym is not a constant and we don't know if it is == V.
290 // Make that assumption.
291
292 isFeasible = true;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000293 return AddEQ(St, sym, V);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000294}
295
296// These logic will be handled in another ConstraintManager.
297const GRState*
298BasicConstraintManager::AssumeSymLT(const GRState* St, SymbolID sym,
299 const llvm::APSInt& V, bool& isFeasible) {
300
301 // FIXME: For now have assuming x < y be the same as assuming sym != V;
302 return AssumeSymNE(St, sym, V, isFeasible);
303}
304
305const GRState*
306BasicConstraintManager::AssumeSymGT(const GRState* St, SymbolID sym,
307 const llvm::APSInt& V, bool& isFeasible) {
308
309 // FIXME: For now have assuming x > y be the same as assuming sym != V;
310 return AssumeSymNE(St, sym, V, isFeasible);
311}
312
313const GRState*
314BasicConstraintManager::AssumeSymGE(const GRState* St, SymbolID sym,
315 const llvm::APSInt& V, bool& isFeasible) {
316
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000317 // Reject a path if the value of sym is a constant X and !(X >= V).
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000318 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000319 isFeasible = *X >= V;
320 return St;
321 }
322
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000323 isFeasible = !isNotEqual(St, sym, V) ||
324 (V != llvm::APSInt::getMaxValue(V.getBitWidth(), V.isSigned()));
325
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000326 return St;
327}
328
329const GRState*
330BasicConstraintManager::AssumeSymLE(const GRState* St, SymbolID sym,
331 const llvm::APSInt& V, bool& isFeasible) {
332
333 // FIXME: Primitive logic for now. Only reject a path if the value of
334 // sym is a constant X and !(X <= V).
335
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000336 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000337 isFeasible = *X <= V;
338 return St;
339 }
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000340
341 isFeasible = !isNotEqual(St, sym, V) ||
342 (V != llvm::APSInt::getMinValue(V.getBitWidth(), V.isSigned()));
343
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000344 return St;
345}
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000346
347static int ConstEqTyIndex = 0;
348static int ConstNotEqTyIndex = 0;
349
350namespace clang {
351 template<>
352 struct GRStateTrait<ConstNotEqTy> : public GRStatePartialTrait<ConstNotEqTy> {
353 static inline void* GDMIndex() { return &ConstNotEqTyIndex; }
354 };
355
356 template<>
357 struct GRStateTrait<ConstEqTy> : public GRStatePartialTrait<ConstEqTy> {
358 static inline void* GDMIndex() { return &ConstEqTyIndex; }
359 };
360}
361
362const GRState* BasicConstraintManager::AddEQ(const GRState* St, SymbolID sym,
363 const llvm::APSInt& V) {
364 // Create a new state with the old binding replaced.
365 GRStateRef state(St, StateMgr);
366 return state.set<ConstEqTy>(sym, &V);
367}
368
369const GRState* BasicConstraintManager::AddNE(const GRState* St, SymbolID sym,
370 const llvm::APSInt& V) {
371 GRState::IntSetTy::Factory ISetFactory(StateMgr.getAllocator());
372 GRStateRef state(St, StateMgr);
373
374 // First, retrieve the NE-set associated with the given symbol.
375 ConstNotEqTy::data_type* T = state.get<ConstNotEqTy>(sym);
376 GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet();
377
378
379 // Now add V to the NE set.
380 S = ISetFactory.Add(S, &V);
381
382 // Create a new state with the old binding replaced.
383 return state.set<ConstNotEqTy>(sym, S);
384}
385
386const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* St,
387 SymbolID sym) {
388 const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym);
389 return T ? *T : NULL;
390}
391
392bool BasicConstraintManager::isNotEqual(const GRState* St, SymbolID sym,
393 const llvm::APSInt& V) const {
394
395 // Retrieve the NE-set associated with the given symbol.
396 const ConstNotEqTy::data_type* T = St->get<ConstNotEqTy>(sym);
397
398 // See if V is present in the NE-set.
399 return T ? T->contains(&V) : false;
400}
401
402bool BasicConstraintManager::isEqual(const GRState* St, SymbolID sym,
403 const llvm::APSInt& V) const {
404 // Retrieve the EQ-set associated with the given symbol.
405 const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym);
406 // See if V is present in the EQ-set.
407 return T ? **T == V : false;
408}
409
410const GRState* BasicConstraintManager::RemoveDeadBindings(const GRState* St,
411 StoreManager::LiveSymbolsTy& LSymbols,
412 StoreManager::DeadSymbolsTy& DSymbols) {
413 GRStateRef state(St, StateMgr);
414 ConstEqTy CE = state.get<ConstEqTy>();
415 ConstEqTy::Factory& CEFactory = state.get_context<ConstEqTy>();
416
417 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
418 SymbolID sym = I.getKey();
419 if (!LSymbols.count(sym)) {
420 DSymbols.insert(sym);
421 CE = CEFactory.Remove(CE, sym);
422 }
423 }
424 state = state.set<ConstEqTy>(CE);
425
426 ConstNotEqTy CNE = state.get<ConstNotEqTy>();
427 ConstNotEqTy::Factory& CNEFactory = state.get_context<ConstNotEqTy>();
428
429 for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) {
430 SymbolID sym = I.getKey();
431 if (!LSymbols.count(sym)) {
432 DSymbols.insert(sym);
433 CNE = CNEFactory.Remove(CNE, sym);
434 }
435 }
436
437 return state.set<ConstNotEqTy>(CNE);
438}
439
440void BasicConstraintManager::print(const GRState* St, std::ostream& Out,
441 const char* nl, const char *sep) {
442 // Print equality constraints.
443
444 ConstEqTy CE = St->get<ConstEqTy>();
445
446 if (!CE.isEmpty()) {
447 Out << nl << sep << "'==' constraints:";
448
449 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
450 Out << nl << " $" << I.getKey();
451 llvm::raw_os_ostream OS(Out);
452 OS << " : " << *I.getData();
453 }
454 }
455
456 // Print != constraints.
457
458 ConstNotEqTy CNE = St->get<ConstNotEqTy>();
459
460 if (!CNE.isEmpty()) {
461 Out << nl << sep << "'!=' constraints:";
462
463 for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) {
464 Out << nl << " $" << I.getKey() << " : ";
465 bool isFirst = true;
466
467 GRState::IntSetTy::iterator J = I.getData().begin(),
468 EJ = I.getData().end();
469
470 for ( ; J != EJ; ++J) {
471 if (isFirst) isFirst = false;
472 else Out << ", ";
473
474 Out << *J;
475 }
476 }
477 }
Daniel Dunbar0e194dd2008-08-30 02:06:22 +0000478}