blob: 9ff44e01125aa7e298166350638ecd561cf63bad [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 Kremenek8c3e7fb2008-09-16 23:24:45 +0000323 // sym is not a constant, but it might be not-equal to a constant.
324 // Observe: V >= sym is the same as sym <= V.
325 // check: is sym != V?
326 // check: is sym > V?
327 // if both are true, the path is infeasible.
328
329 if (isNotEqual(St, sym, V)) {
330 // Is sym > V?
331 //
332 // We're not doing heavy range analysis yet, so all we can accurately
333 // reason about are the edge cases.
334 //
335 // If V == 0, since we know that sym != V, we also know that sym > V.
336 isFeasible = V != 0;
337 }
338 else
339 isFeasible = true;
340
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000341 return St;
342}
343
344const GRState*
345BasicConstraintManager::AssumeSymLE(const GRState* St, SymbolID sym,
346 const llvm::APSInt& V, bool& isFeasible) {
347
348 // FIXME: Primitive logic for now. Only reject a path if the value of
349 // sym is a constant X and !(X <= V).
350
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000351 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000352 isFeasible = *X <= V;
353 return St;
354 }
355
356 isFeasible = true;
357 return St;
358}
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000359
360static int ConstEqTyIndex = 0;
361static int ConstNotEqTyIndex = 0;
362
363namespace clang {
364 template<>
365 struct GRStateTrait<ConstNotEqTy> : public GRStatePartialTrait<ConstNotEqTy> {
366 static inline void* GDMIndex() { return &ConstNotEqTyIndex; }
367 };
368
369 template<>
370 struct GRStateTrait<ConstEqTy> : public GRStatePartialTrait<ConstEqTy> {
371 static inline void* GDMIndex() { return &ConstEqTyIndex; }
372 };
373}
374
375const GRState* BasicConstraintManager::AddEQ(const GRState* St, SymbolID sym,
376 const llvm::APSInt& V) {
377 // Create a new state with the old binding replaced.
378 GRStateRef state(St, StateMgr);
379 return state.set<ConstEqTy>(sym, &V);
380}
381
382const GRState* BasicConstraintManager::AddNE(const GRState* St, SymbolID sym,
383 const llvm::APSInt& V) {
384 GRState::IntSetTy::Factory ISetFactory(StateMgr.getAllocator());
385 GRStateRef state(St, StateMgr);
386
387 // First, retrieve the NE-set associated with the given symbol.
388 ConstNotEqTy::data_type* T = state.get<ConstNotEqTy>(sym);
389 GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet();
390
391
392 // Now add V to the NE set.
393 S = ISetFactory.Add(S, &V);
394
395 // Create a new state with the old binding replaced.
396 return state.set<ConstNotEqTy>(sym, S);
397}
398
399const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* St,
400 SymbolID sym) {
401 const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym);
402 return T ? *T : NULL;
403}
404
405bool BasicConstraintManager::isNotEqual(const GRState* St, SymbolID sym,
406 const llvm::APSInt& V) const {
407
408 // Retrieve the NE-set associated with the given symbol.
409 const ConstNotEqTy::data_type* T = St->get<ConstNotEqTy>(sym);
410
411 // See if V is present in the NE-set.
412 return T ? T->contains(&V) : false;
413}
414
415bool BasicConstraintManager::isEqual(const GRState* St, SymbolID sym,
416 const llvm::APSInt& V) const {
417 // Retrieve the EQ-set associated with the given symbol.
418 const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym);
419 // See if V is present in the EQ-set.
420 return T ? **T == V : false;
421}
422
423const GRState* BasicConstraintManager::RemoveDeadBindings(const GRState* St,
424 StoreManager::LiveSymbolsTy& LSymbols,
425 StoreManager::DeadSymbolsTy& DSymbols) {
426 GRStateRef state(St, StateMgr);
427 ConstEqTy CE = state.get<ConstEqTy>();
428 ConstEqTy::Factory& CEFactory = state.get_context<ConstEqTy>();
429
430 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
431 SymbolID sym = I.getKey();
432 if (!LSymbols.count(sym)) {
433 DSymbols.insert(sym);
434 CE = CEFactory.Remove(CE, sym);
435 }
436 }
437 state = state.set<ConstEqTy>(CE);
438
439 ConstNotEqTy CNE = state.get<ConstNotEqTy>();
440 ConstNotEqTy::Factory& CNEFactory = state.get_context<ConstNotEqTy>();
441
442 for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) {
443 SymbolID sym = I.getKey();
444 if (!LSymbols.count(sym)) {
445 DSymbols.insert(sym);
446 CNE = CNEFactory.Remove(CNE, sym);
447 }
448 }
449
450 return state.set<ConstNotEqTy>(CNE);
451}
452
453void BasicConstraintManager::print(const GRState* St, std::ostream& Out,
454 const char* nl, const char *sep) {
455 // Print equality constraints.
456
457 ConstEqTy CE = St->get<ConstEqTy>();
458
459 if (!CE.isEmpty()) {
460 Out << nl << sep << "'==' constraints:";
461
462 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
463 Out << nl << " $" << I.getKey();
464 llvm::raw_os_ostream OS(Out);
465 OS << " : " << *I.getData();
466 }
467 }
468
469 // Print != constraints.
470
471 ConstNotEqTy CNE = St->get<ConstNotEqTy>();
472
473 if (!CNE.isEmpty()) {
474 Out << nl << sep << "'!=' constraints:";
475
476 for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) {
477 Out << nl << " $" << I.getKey() << " : ";
478 bool isFirst = true;
479
480 GRState::IntSetTy::iterator J = I.getData().begin(),
481 EJ = I.getData().end();
482
483 for ( ; J != EJ; ++J) {
484 if (isFirst) isFirst = false;
485 else Out << ", ";
486
487 Out << *J;
488 }
489 }
490 }
Daniel Dunbar0e194dd2008-08-30 02:06:22 +0000491}