blob: aab1f5e7eb1510211aed52dedfedc141701c0d3a [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
Zhongxing Xu1c96b242008-10-17 05:57:07 +000036 virtual const GRState* Assume(const GRState* St, SVal Cond,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000037 bool Assumption, bool& isFeasible);
38
Zhongxing Xu1c96b242008-10-17 05:57:07 +000039 const GRState* Assume(const GRState* St, Loc Cond, bool Assumption,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000040 bool& isFeasible);
41
Zhongxing Xu1c96b242008-10-17 05:57:07 +000042 const GRState* AssumeAux(const GRState* St, Loc Cond,bool Assumption,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000043 bool& isFeasible);
44
Zhongxing Xu1c96b242008-10-17 05:57:07 +000045 const GRState* Assume(const GRState* St, NonLoc Cond, bool Assumption,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000046 bool& isFeasible);
47
Zhongxing Xu1c96b242008-10-17 05:57:07 +000048 const GRState* AssumeAux(const GRState* St, NonLoc Cond, bool Assumption,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000049 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
Zhongxing Xu1c96b242008-10-17 05:57:07 +000095const GRState* BasicConstraintManager::Assume(const GRState* St, SVal Cond,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000096 bool Assumption, bool& isFeasible) {
97 if (Cond.isUnknown()) {
98 isFeasible = true;
99 return St;
100 }
101
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000102 if (isa<NonLoc>(Cond))
103 return Assume(St, cast<NonLoc>(Cond), Assumption, isFeasible);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000104 else
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000105 return Assume(St, cast<Loc>(Cond), Assumption, isFeasible);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000106}
107
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000108const GRState* BasicConstraintManager::Assume(const GRState* St, Loc Cond,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000109 bool Assumption, bool& isFeasible) {
110 St = AssumeAux(St, Cond, Assumption, isFeasible);
111 // TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
112 return St;
113}
114
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000115const GRState* BasicConstraintManager::AssumeAux(const GRState* St, Loc Cond,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000116 bool Assumption, bool& isFeasible) {
117 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
118
119 switch (Cond.getSubKind()) {
120 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000121 assert (false && "'Assume' not implemented for this Loc.");
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000122 return St;
123
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000124 case loc::SymbolValKind:
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000125 if (Assumption)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000126 return AssumeSymNE(St, cast<loc::SymbolVal>(Cond).getSymbol(),
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000127 BasicVals.getZeroWithPtrWidth(), isFeasible);
128 else
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000129 return AssumeSymEQ(St, cast<loc::SymbolVal>(Cond).getSymbol(),
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000130 BasicVals.getZeroWithPtrWidth(), isFeasible);
131
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000132 case loc::MemRegionKind:
133 case loc::FuncValKind:
134 case loc::GotoLabelKind:
135 case loc::StringLiteralValKind:
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000136 isFeasible = Assumption;
137 return St;
138
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000139 case loc::ConcreteIntKind: {
140 bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000141 isFeasible = b ? Assumption : !Assumption;
142 return St;
143 }
144 } // end switch
145}
146
147const GRState*
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000148BasicConstraintManager::Assume(const GRState* St, NonLoc Cond, bool Assumption,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000149 bool& isFeasible) {
150 St = AssumeAux(St, Cond, Assumption, isFeasible);
151 // TF->EvalAssume() does nothing now.
152 return St;
153}
154
155const GRState*
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000156BasicConstraintManager::AssumeAux(const GRState* St,NonLoc Cond,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000157 bool Assumption, bool& isFeasible) {
158 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
159 SymbolManager& SymMgr = StateMgr.getSymbolManager();
160
161 switch (Cond.getSubKind()) {
162 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000163 assert(false && "'Assume' not implemented for this NonLoc");
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000164
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000165 case nonloc::SymbolValKind: {
166 nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000167 SymbolID sym = SV.getSymbol();
168
169 if (Assumption)
170 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
171 isFeasible);
172 else
173 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
174 isFeasible);
175 }
176
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000177 case nonloc::SymIntConstraintValKind:
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000178 return
179 AssumeSymInt(St, Assumption,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000180 cast<nonloc::SymIntConstraintVal>(Cond).getConstraint(),
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000181 isFeasible);
182
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000183 case nonloc::ConcreteIntKind: {
184 bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000185 isFeasible = b ? Assumption : !Assumption;
186 return St;
187 }
188
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000189 case nonloc::LocAsIntegerKind:
190 return AssumeAux(St, cast<nonloc::LocAsInteger>(Cond).getLoc(),
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000191 Assumption, isFeasible);
192 } // end switch
193}
194
195const GRState*
196BasicConstraintManager::AssumeSymInt(const GRState* St, bool Assumption,
197 const SymIntConstraint& C, bool& isFeasible) {
198
199 switch (C.getOpcode()) {
200 default:
201 // No logic yet for other operators.
202 isFeasible = true;
203 return St;
204
205 case BinaryOperator::EQ:
206 if (Assumption)
207 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
208 else
209 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
210
211 case BinaryOperator::NE:
212 if (Assumption)
213 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
214 else
215 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
216
Zhongxing Xu94b83122008-09-19 06:07:59 +0000217 case BinaryOperator::GT:
218 if (Assumption)
219 return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible);
220 else
221 return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible);
222
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000223 case BinaryOperator::GE:
224 if (Assumption)
225 return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible);
226 else
227 return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible);
228
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000229 case BinaryOperator::LT:
230 if (Assumption)
231 return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible);
232 else
233 return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible);
234
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000235 case BinaryOperator::LE:
236 if (Assumption)
237 return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible);
238 else
239 return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible);
240 } // end switch
241}
242
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000243
244
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000245const GRState*
246BasicConstraintManager::AssumeSymNE(const GRState* St, SymbolID sym,
247 const llvm::APSInt& V, bool& isFeasible) {
248 // First, determine if sym == X, where X != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000249 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000250 isFeasible = (*X != V);
251 return St;
252 }
253
254 // Second, determine if sym != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000255 if (isNotEqual(St, sym, V)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000256 isFeasible = true;
257 return St;
258 }
259
260 // If we reach here, sym is not a constant and we don't know if it is != V.
261 // Make that assumption.
262 isFeasible = true;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000263 return AddNE(St, sym, V);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000264}
265
266const GRState*
267BasicConstraintManager::AssumeSymEQ(const GRState* St, SymbolID sym,
268 const llvm::APSInt& V, bool& isFeasible) {
269 // First, determine if sym == X, where X != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000270 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000271 isFeasible = *X == V;
272 return St;
273 }
274
275 // Second, determine if sym != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000276 if (isNotEqual(St, sym, V)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000277 isFeasible = false;
278 return St;
279 }
280
281 // If we reach here, sym is not a constant and we don't know if it is == V.
282 // Make that assumption.
283
284 isFeasible = true;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000285 return AddEQ(St, sym, V);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000286}
287
288// These logic will be handled in another ConstraintManager.
289const GRState*
290BasicConstraintManager::AssumeSymLT(const GRState* St, SymbolID sym,
291 const llvm::APSInt& V, bool& isFeasible) {
292
293 // FIXME: For now have assuming x < y be the same as assuming sym != V;
294 return AssumeSymNE(St, sym, V, isFeasible);
295}
296
297const GRState*
298BasicConstraintManager::AssumeSymGT(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::AssumeSymGE(const GRState* St, SymbolID sym,
307 const llvm::APSInt& V, bool& isFeasible) {
308
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000309 // Reject a path if the value of sym is a constant X and !(X >= V).
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000310 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000311 isFeasible = *X >= V;
312 return St;
313 }
314
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000315 isFeasible = !isNotEqual(St, sym, V) ||
316 (V != llvm::APSInt::getMaxValue(V.getBitWidth(), V.isSigned()));
317
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000318 return St;
319}
320
321const GRState*
322BasicConstraintManager::AssumeSymLE(const GRState* St, SymbolID sym,
323 const llvm::APSInt& V, bool& isFeasible) {
324
325 // FIXME: Primitive logic for now. Only reject a path if the value of
326 // sym is a constant X and !(X <= V).
327
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000328 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000329 isFeasible = *X <= V;
330 return St;
331 }
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000332
333 isFeasible = !isNotEqual(St, sym, V) ||
334 (V != llvm::APSInt::getMinValue(V.getBitWidth(), V.isSigned()));
335
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000336 return St;
337}
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000338
339static int ConstEqTyIndex = 0;
340static int ConstNotEqTyIndex = 0;
341
342namespace clang {
343 template<>
344 struct GRStateTrait<ConstNotEqTy> : public GRStatePartialTrait<ConstNotEqTy> {
345 static inline void* GDMIndex() { return &ConstNotEqTyIndex; }
346 };
347
348 template<>
349 struct GRStateTrait<ConstEqTy> : public GRStatePartialTrait<ConstEqTy> {
350 static inline void* GDMIndex() { return &ConstEqTyIndex; }
351 };
352}
353
354const GRState* BasicConstraintManager::AddEQ(const GRState* St, SymbolID sym,
355 const llvm::APSInt& V) {
356 // Create a new state with the old binding replaced.
357 GRStateRef state(St, StateMgr);
358 return state.set<ConstEqTy>(sym, &V);
359}
360
361const GRState* BasicConstraintManager::AddNE(const GRState* St, SymbolID sym,
362 const llvm::APSInt& V) {
363 GRState::IntSetTy::Factory ISetFactory(StateMgr.getAllocator());
364 GRStateRef state(St, StateMgr);
365
366 // First, retrieve the NE-set associated with the given symbol.
367 ConstNotEqTy::data_type* T = state.get<ConstNotEqTy>(sym);
368 GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet();
369
370
371 // Now add V to the NE set.
372 S = ISetFactory.Add(S, &V);
373
374 // Create a new state with the old binding replaced.
375 return state.set<ConstNotEqTy>(sym, S);
376}
377
378const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* St,
379 SymbolID sym) {
380 const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym);
381 return T ? *T : NULL;
382}
383
384bool BasicConstraintManager::isNotEqual(const GRState* St, SymbolID sym,
385 const llvm::APSInt& V) const {
386
387 // Retrieve the NE-set associated with the given symbol.
388 const ConstNotEqTy::data_type* T = St->get<ConstNotEqTy>(sym);
389
390 // See if V is present in the NE-set.
391 return T ? T->contains(&V) : false;
392}
393
394bool BasicConstraintManager::isEqual(const GRState* St, SymbolID sym,
395 const llvm::APSInt& V) const {
396 // Retrieve the EQ-set associated with the given symbol.
397 const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym);
398 // See if V is present in the EQ-set.
399 return T ? **T == V : false;
400}
401
402const GRState* BasicConstraintManager::RemoveDeadBindings(const GRState* St,
403 StoreManager::LiveSymbolsTy& LSymbols,
404 StoreManager::DeadSymbolsTy& DSymbols) {
405 GRStateRef state(St, StateMgr);
406 ConstEqTy CE = state.get<ConstEqTy>();
407 ConstEqTy::Factory& CEFactory = state.get_context<ConstEqTy>();
408
409 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
410 SymbolID sym = I.getKey();
411 if (!LSymbols.count(sym)) {
412 DSymbols.insert(sym);
413 CE = CEFactory.Remove(CE, sym);
414 }
415 }
416 state = state.set<ConstEqTy>(CE);
417
418 ConstNotEqTy CNE = state.get<ConstNotEqTy>();
419 ConstNotEqTy::Factory& CNEFactory = state.get_context<ConstNotEqTy>();
420
421 for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) {
422 SymbolID sym = I.getKey();
423 if (!LSymbols.count(sym)) {
424 DSymbols.insert(sym);
425 CNE = CNEFactory.Remove(CNE, sym);
426 }
427 }
428
429 return state.set<ConstNotEqTy>(CNE);
430}
431
432void BasicConstraintManager::print(const GRState* St, std::ostream& Out,
433 const char* nl, const char *sep) {
434 // Print equality constraints.
435
436 ConstEqTy CE = St->get<ConstEqTy>();
437
438 if (!CE.isEmpty()) {
439 Out << nl << sep << "'==' constraints:";
440
441 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
442 Out << nl << " $" << I.getKey();
443 llvm::raw_os_ostream OS(Out);
444 OS << " : " << *I.getData();
445 }
446 }
447
448 // Print != constraints.
449
450 ConstNotEqTy CNE = St->get<ConstNotEqTy>();
451
452 if (!CNE.isEmpty()) {
453 Out << nl << sep << "'!=' constraints:";
454
455 for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) {
456 Out << nl << " $" << I.getKey() << " : ";
457 bool isFirst = true;
458
459 GRState::IntSetTy::iterator J = I.getData().begin(),
460 EJ = I.getData().end();
461
462 for ( ; J != EJ; ++J) {
463 if (isFirst) isFirst = false;
464 else Out << ", ";
465
466 Out << *J;
467 }
468 }
469 }
Daniel Dunbar0e194dd2008-08-30 02:06:22 +0000470}