blob: ca97f930ddb0b8227e6b2859fdaa448e1526a9dc [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
225 case BinaryOperator::GE:
226 if (Assumption)
227 return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible);
228 else
229 return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible);
230
231 case BinaryOperator::LE:
232 if (Assumption)
233 return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible);
234 else
235 return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible);
236 } // end switch
237}
238
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000239
240
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000241const GRState*
242BasicConstraintManager::AssumeSymNE(const GRState* St, SymbolID sym,
243 const llvm::APSInt& V, bool& isFeasible) {
244 // First, determine if sym == X, where X != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000245 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000246 isFeasible = (*X != V);
247 return St;
248 }
249
250 // Second, determine if sym != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000251 if (isNotEqual(St, sym, V)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000252 isFeasible = true;
253 return St;
254 }
255
256 // If we reach here, sym is not a constant and we don't know if it is != V.
257 // Make that assumption.
258 isFeasible = true;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000259 return AddNE(St, sym, V);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000260}
261
262const GRState*
263BasicConstraintManager::AssumeSymEQ(const GRState* St, SymbolID sym,
264 const llvm::APSInt& V, bool& isFeasible) {
265 // First, determine if sym == X, where X != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000266 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000267 isFeasible = *X == V;
268 return St;
269 }
270
271 // Second, determine if sym != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000272 if (isNotEqual(St, sym, V)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000273 isFeasible = false;
274 return St;
275 }
276
277 // If we reach here, sym is not a constant and we don't know if it is == V.
278 // Make that assumption.
279
280 isFeasible = true;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000281 return AddEQ(St, sym, V);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000282}
283
284// These logic will be handled in another ConstraintManager.
285const GRState*
286BasicConstraintManager::AssumeSymLT(const GRState* St, SymbolID sym,
287 const llvm::APSInt& V, bool& isFeasible) {
288
289 // FIXME: For now have assuming x < y be the same as assuming sym != V;
290 return AssumeSymNE(St, sym, V, isFeasible);
291}
292
293const GRState*
294BasicConstraintManager::AssumeSymGT(const GRState* St, SymbolID sym,
295 const llvm::APSInt& V, bool& isFeasible) {
296
297 // FIXME: For now have assuming x > y be the same as assuming sym != V;
298 return AssumeSymNE(St, sym, V, isFeasible);
299}
300
301const GRState*
302BasicConstraintManager::AssumeSymGE(const GRState* St, SymbolID sym,
303 const llvm::APSInt& V, bool& isFeasible) {
304
305 // FIXME: Primitive logic for now. Only reject a path if the value of
306 // sym is a constant X and !(X >= V).
307
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000308 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000309 isFeasible = *X >= V;
310 return St;
311 }
312
313 isFeasible = true;
314 return St;
315}
316
317const GRState*
318BasicConstraintManager::AssumeSymLE(const GRState* St, SymbolID sym,
319 const llvm::APSInt& V, bool& isFeasible) {
320
321 // FIXME: Primitive logic for now. Only reject a path if the value of
322 // sym is a constant X and !(X <= V).
323
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000324 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000325 isFeasible = *X <= V;
326 return St;
327 }
328
329 isFeasible = true;
330 return St;
331}
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000332
333static int ConstEqTyIndex = 0;
334static int ConstNotEqTyIndex = 0;
335
336namespace clang {
337 template<>
338 struct GRStateTrait<ConstNotEqTy> : public GRStatePartialTrait<ConstNotEqTy> {
339 static inline void* GDMIndex() { return &ConstNotEqTyIndex; }
340 };
341
342 template<>
343 struct GRStateTrait<ConstEqTy> : public GRStatePartialTrait<ConstEqTy> {
344 static inline void* GDMIndex() { return &ConstEqTyIndex; }
345 };
346}
347
348const GRState* BasicConstraintManager::AddEQ(const GRState* St, SymbolID sym,
349 const llvm::APSInt& V) {
350 // Create a new state with the old binding replaced.
351 GRStateRef state(St, StateMgr);
352 return state.set<ConstEqTy>(sym, &V);
353}
354
355const GRState* BasicConstraintManager::AddNE(const GRState* St, SymbolID sym,
356 const llvm::APSInt& V) {
357 GRState::IntSetTy::Factory ISetFactory(StateMgr.getAllocator());
358 GRStateRef state(St, StateMgr);
359
360 // First, retrieve the NE-set associated with the given symbol.
361 ConstNotEqTy::data_type* T = state.get<ConstNotEqTy>(sym);
362 GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet();
363
364
365 // Now add V to the NE set.
366 S = ISetFactory.Add(S, &V);
367
368 // Create a new state with the old binding replaced.
369 return state.set<ConstNotEqTy>(sym, S);
370}
371
372const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* St,
373 SymbolID sym) {
374 const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym);
375 return T ? *T : NULL;
376}
377
378bool BasicConstraintManager::isNotEqual(const GRState* St, SymbolID sym,
379 const llvm::APSInt& V) const {
380
381 // Retrieve the NE-set associated with the given symbol.
382 const ConstNotEqTy::data_type* T = St->get<ConstNotEqTy>(sym);
383
384 // See if V is present in the NE-set.
385 return T ? T->contains(&V) : false;
386}
387
388bool BasicConstraintManager::isEqual(const GRState* St, SymbolID sym,
389 const llvm::APSInt& V) const {
390 // Retrieve the EQ-set associated with the given symbol.
391 const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym);
392 // See if V is present in the EQ-set.
393 return T ? **T == V : false;
394}
395
396const GRState* BasicConstraintManager::RemoveDeadBindings(const GRState* St,
397 StoreManager::LiveSymbolsTy& LSymbols,
398 StoreManager::DeadSymbolsTy& DSymbols) {
399 GRStateRef state(St, StateMgr);
400 ConstEqTy CE = state.get<ConstEqTy>();
401 ConstEqTy::Factory& CEFactory = state.get_context<ConstEqTy>();
402
403 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
404 SymbolID sym = I.getKey();
405 if (!LSymbols.count(sym)) {
406 DSymbols.insert(sym);
407 CE = CEFactory.Remove(CE, sym);
408 }
409 }
410 state = state.set<ConstEqTy>(CE);
411
412 ConstNotEqTy CNE = state.get<ConstNotEqTy>();
413 ConstNotEqTy::Factory& CNEFactory = state.get_context<ConstNotEqTy>();
414
415 for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) {
416 SymbolID sym = I.getKey();
417 if (!LSymbols.count(sym)) {
418 DSymbols.insert(sym);
419 CNE = CNEFactory.Remove(CNE, sym);
420 }
421 }
422
423 return state.set<ConstNotEqTy>(CNE);
424}
425
426void BasicConstraintManager::print(const GRState* St, std::ostream& Out,
427 const char* nl, const char *sep) {
428 // Print equality constraints.
429
430 ConstEqTy CE = St->get<ConstEqTy>();
431
432 if (!CE.isEmpty()) {
433 Out << nl << sep << "'==' constraints:";
434
435 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
436 Out << nl << " $" << I.getKey();
437 llvm::raw_os_ostream OS(Out);
438 OS << " : " << *I.getData();
439 }
440 }
441
442 // Print != constraints.
443
444 ConstNotEqTy CNE = St->get<ConstNotEqTy>();
445
446 if (!CNE.isEmpty()) {
447 Out << nl << sep << "'!=' constraints:";
448
449 for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) {
450 Out << nl << " $" << I.getKey() << " : ";
451 bool isFirst = true;
452
453 GRState::IntSetTy::iterator J = I.getData().begin(),
454 EJ = I.getData().end();
455
456 for ( ; J != EJ; ++J) {
457 if (isFirst) isFirst = false;
458 else Out << ", ";
459
460 Out << *J;
461 }
462 }
463 }
Daniel Dunbar0e194dd2008-08-30 02:06:22 +0000464}