blob: b2722141d9552568d8849a2ff2bf6db6fc3125df [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
Ted Kremenek45021952009-02-14 17:08:39 +000015#include "SimpleConstraintManager.h"
Zhongxing Xu30ad1672008-08-27 14:03:33 +000016#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xu39cfed32008-08-29 14:52:36 +000017#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenek2fb78a72008-12-17 21:50:35 +000018#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
Zhongxing Xu30ad1672008-08-27 14:03:33 +000019#include "llvm/Support/Compiler.h"
Zhongxing Xu39cfed32008-08-29 14:52:36 +000020#include "llvm/Support/raw_ostream.h"
Zhongxing Xu30ad1672008-08-27 14:03:33 +000021
22using namespace clang;
23
Ted Kremenek8ee74d52009-01-26 06:04:53 +000024
25namespace { class VISIBILITY_HIDDEN ConstNotEq {}; }
26namespace { class VISIBILITY_HIDDEN ConstEq {}; }
Zhongxing Xu30ad1672008-08-27 14:03:33 +000027
Ted Kremenek2dabd432008-12-05 02:27:51 +000028typedef llvm::ImmutableMap<SymbolRef,GRState::IntSetTy> ConstNotEqTy;
29typedef llvm::ImmutableMap<SymbolRef,const llvm::APSInt*> ConstEqTy;
Ted Kremenek8ee74d52009-01-26 06:04:53 +000030
31static int ConstEqIndex = 0;
32static int ConstNotEqIndex = 0;
Zhongxing Xu39cfed32008-08-29 14:52:36 +000033
Ted Kremenek8ee74d52009-01-26 06:04:53 +000034namespace clang {
35template<>
36struct GRStateTrait<ConstNotEq> : public GRStatePartialTrait<ConstNotEqTy> {
37 static inline void* GDMIndex() { return &ConstNotEqIndex; }
38};
39
40template<>
41struct GRStateTrait<ConstEq> : public GRStatePartialTrait<ConstEqTy> {
42 static inline void* GDMIndex() { return &ConstEqIndex; }
43};
44}
45
46namespace {
Zhongxing Xu30ad1672008-08-27 14:03:33 +000047// BasicConstraintManager only tracks equality and inequality constraints of
48// constants and integer variables.
Ted Kremenek45021952009-02-14 17:08:39 +000049class VISIBILITY_HIDDEN BasicConstraintManager
50 : public SimpleConstraintManager {
Zhongxing Xuf0bc50e2008-11-27 06:08:40 +000051 GRState::IntSetTy::Factory ISetFactory;
Zhongxing Xu30ad1672008-08-27 14:03:33 +000052public:
Zhongxing Xuf0bc50e2008-11-27 06:08:40 +000053 BasicConstraintManager(GRStateManager& statemgr)
Ted Kremenek45021952009-02-14 17:08:39 +000054 : SimpleConstraintManager(statemgr), ISetFactory(statemgr.getAllocator()) {}
Zhongxing Xu30ad1672008-08-27 14:03:33 +000055
Ted Kremenek2dabd432008-12-05 02:27:51 +000056 const GRState* AssumeSymNE(const GRState* St, SymbolRef sym,
Ted Kremenekb2bf7cd2009-01-28 22:27:59 +000057 const llvm::APSInt& V, bool& isFeasible);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000058
Ted Kremenek2dabd432008-12-05 02:27:51 +000059 const GRState* AssumeSymEQ(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000060 const llvm::APSInt& V, bool& isFeasible);
61
Ted Kremenek2dabd432008-12-05 02:27:51 +000062 const GRState* AssumeSymLT(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000063 const llvm::APSInt& V, bool& isFeasible);
64
Ted Kremenek2dabd432008-12-05 02:27:51 +000065 const GRState* AssumeSymGT(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000066 const llvm::APSInt& V, bool& isFeasible);
67
Ted Kremenek2dabd432008-12-05 02:27:51 +000068 const GRState* AssumeSymGE(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000069 const llvm::APSInt& V, bool& isFeasible);
70
Ted Kremenek2dabd432008-12-05 02:27:51 +000071 const GRState* AssumeSymLE(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000072 const llvm::APSInt& V, bool& isFeasible);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000073
Ted Kremenek2dabd432008-12-05 02:27:51 +000074 const GRState* AddEQ(const GRState* St, SymbolRef sym, const llvm::APSInt& V);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000075
Ted Kremenek2dabd432008-12-05 02:27:51 +000076 const GRState* AddNE(const GRState* St, SymbolRef sym, const llvm::APSInt& V);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000077
Ted Kremenek45021952009-02-14 17:08:39 +000078 const llvm::APSInt* getSymVal(const GRState* St, SymbolRef sym) const;
79 bool isNotEqual(const GRState* St, SymbolRef sym, const llvm::APSInt& V)
80 const;
81 bool isEqual(const GRState* St, SymbolRef sym, const llvm::APSInt& V)
82 const;
Zhongxing Xu39cfed32008-08-29 14:52:36 +000083
Ted Kremenek241677a2009-01-21 22:26:05 +000084 const GRState* RemoveDeadBindings(const GRState* St, SymbolReaper& SymReaper);
85
Zhongxing Xu39cfed32008-08-29 14:52:36 +000086 void print(const GRState* St, std::ostream& Out,
87 const char* nl, const char *sep);
88};
Zhongxing Xu30ad1672008-08-27 14:03:33 +000089
90} // end anonymous namespace
91
92ConstraintManager* clang::CreateBasicConstraintManager(GRStateManager& StateMgr)
93{
94 return new BasicConstraintManager(StateMgr);
95}
96
Zhongxing Xu30ad1672008-08-27 14:03:33 +000097const GRState*
Ted Kremenek2dabd432008-12-05 02:27:51 +000098BasicConstraintManager::AssumeSymNE(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000099 const llvm::APSInt& V, bool& isFeasible) {
100 // First, determine if sym == X, where X != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000101 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000102 isFeasible = (*X != V);
103 return St;
104 }
105
106 // Second, determine if sym != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000107 if (isNotEqual(St, sym, V)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000108 isFeasible = true;
109 return St;
110 }
111
112 // If we reach here, sym is not a constant and we don't know if it is != V.
113 // Make that assumption.
114 isFeasible = true;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000115 return AddNE(St, sym, V);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000116}
117
118const GRState*
Ted Kremenek2dabd432008-12-05 02:27:51 +0000119BasicConstraintManager::AssumeSymEQ(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000120 const llvm::APSInt& V, bool& isFeasible) {
121 // First, determine if sym == X, where X != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000122 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000123 isFeasible = *X == V;
124 return St;
125 }
126
127 // Second, determine if sym != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000128 if (isNotEqual(St, sym, V)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000129 isFeasible = false;
130 return St;
131 }
132
133 // If we reach here, sym is not a constant and we don't know if it is == V.
134 // Make that assumption.
135
136 isFeasible = true;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000137 return AddEQ(St, sym, V);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000138}
139
140// These logic will be handled in another ConstraintManager.
141const GRState*
Ted Kremenek2dabd432008-12-05 02:27:51 +0000142BasicConstraintManager::AssumeSymLT(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000143 const llvm::APSInt& V, bool& isFeasible) {
Ted Kremenek73abd132008-12-03 18:56:12 +0000144
145 // Is 'V' the smallest possible value?
Chris Lattner071e04e2009-01-30 01:58:33 +0000146 if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) {
Ted Kremenek73abd132008-12-03 18:56:12 +0000147 // sym cannot be any value less than 'V'. This path is infeasible.
148 isFeasible = false;
149 return St;
150 }
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000151
152 // FIXME: For now have assuming x < y be the same as assuming sym != V;
153 return AssumeSymNE(St, sym, V, isFeasible);
154}
155
156const GRState*
Ted Kremenek2dabd432008-12-05 02:27:51 +0000157BasicConstraintManager::AssumeSymGT(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000158 const llvm::APSInt& V, bool& isFeasible) {
159
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000160 // Is 'V' the largest possible value?
Chris Lattner071e04e2009-01-30 01:58:33 +0000161 if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) {
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000162 // sym cannot be any value greater than 'V'. This path is infeasible.
163 isFeasible = false;
164 return St;
165 }
166
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000167 // FIXME: For now have assuming x > y be the same as assuming sym != V;
168 return AssumeSymNE(St, sym, V, isFeasible);
169}
170
171const GRState*
Ted Kremenek2dabd432008-12-05 02:27:51 +0000172BasicConstraintManager::AssumeSymGE(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000173 const llvm::APSInt& V, bool& isFeasible) {
174
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000175 // Reject a path if the value of sym is a constant X and !(X >= V).
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000176 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000177 isFeasible = *X >= V;
178 return St;
179 }
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000180
181 // Sym is not a constant, but it is worth looking to see if V is the
182 // maximum integer value.
Chris Lattner071e04e2009-01-30 01:58:33 +0000183 if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) {
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000184 // If we know that sym != V, then this condition is infeasible since
185 // there is no other value greater than V.
186 isFeasible = !isNotEqual(St, sym, V);
187
188 // If the path is still feasible then as a consequence we know that
189 // 'sym == V' because we cannot have 'sym > V' (no larger values).
190 // Add this constraint.
191 if (isFeasible)
192 return AddEQ(St, sym, V);
193 }
194 else
195 isFeasible = true;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000196
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000197 return St;
198}
199
200const GRState*
Ted Kremenek2dabd432008-12-05 02:27:51 +0000201BasicConstraintManager::AssumeSymLE(const GRState* St, SymbolRef sym,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000202 const llvm::APSInt& V, bool& isFeasible) {
203
Ted Kremenek73abd132008-12-03 18:56:12 +0000204 // Reject a path if the value of sym is a constant X and !(X <= V).
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000205 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000206 isFeasible = *X <= V;
207 return St;
208 }
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000209
Ted Kremenek73abd132008-12-03 18:56:12 +0000210 // Sym is not a constant, but it is worth looking to see if V is the
211 // minimum integer value.
Chris Lattner071e04e2009-01-30 01:58:33 +0000212 if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) {
Ted Kremenek73abd132008-12-03 18:56:12 +0000213 // If we know that sym != V, then this condition is infeasible since
214 // there is no other value less than V.
215 isFeasible = !isNotEqual(St, sym, V);
216
217 // If the path is still feasible then as a consequence we know that
218 // 'sym == V' because we cannot have 'sym < V' (no smaller values).
219 // Add this constraint.
220 if (isFeasible)
221 return AddEQ(St, sym, V);
222 }
223 else
224 isFeasible = true;
225
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000226 return St;
227}
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000228
Ted Kremenek2dabd432008-12-05 02:27:51 +0000229const GRState* BasicConstraintManager::AddEQ(const GRState* St, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000230 const llvm::APSInt& V) {
231 // Create a new state with the old binding replaced.
232 GRStateRef state(St, StateMgr);
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000233 return state.set<ConstEq>(sym, &V);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000234}
235
Ted Kremenek2dabd432008-12-05 02:27:51 +0000236const GRState* BasicConstraintManager::AddNE(const GRState* St, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000237 const llvm::APSInt& V) {
Zhongxing Xuf0bc50e2008-11-27 06:08:40 +0000238
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000239 GRStateRef state(St, StateMgr);
240
241 // First, retrieve the NE-set associated with the given symbol.
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000242 ConstNotEqTy::data_type* T = state.get<ConstNotEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000243 GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet();
244
245
246 // Now add V to the NE set.
247 S = ISetFactory.Add(S, &V);
248
249 // Create a new state with the old binding replaced.
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000250 return state.set<ConstNotEq>(sym, S);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000251}
252
253const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* St,
Ted Kremenek45021952009-02-14 17:08:39 +0000254 SymbolRef sym) const {
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000255 const ConstEqTy::data_type* T = St->get<ConstEq>(sym);
Ted Kremenek45021952009-02-14 17:08:39 +0000256 return T ? *T : NULL;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000257}
258
Ted Kremenek2dabd432008-12-05 02:27:51 +0000259bool BasicConstraintManager::isNotEqual(const GRState* St, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000260 const llvm::APSInt& V) const {
261
262 // Retrieve the NE-set associated with the given symbol.
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000263 const ConstNotEqTy::data_type* T = St->get<ConstNotEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000264
265 // See if V is present in the NE-set.
266 return T ? T->contains(&V) : false;
267}
268
Ted Kremenek2dabd432008-12-05 02:27:51 +0000269bool BasicConstraintManager::isEqual(const GRState* St, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000270 const llvm::APSInt& V) const {
271 // Retrieve the EQ-set associated with the given symbol.
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000272 const ConstEqTy::data_type* T = St->get<ConstEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000273 // See if V is present in the EQ-set.
274 return T ? **T == V : false;
275}
276
Zhongxing Xu8fd9b352008-11-27 02:39:34 +0000277/// Scan all symbols referenced by the constraints. If the symbol is not alive
278/// as marked in LSymbols, mark it as dead in DSymbols.
Ted Kremenek241677a2009-01-21 22:26:05 +0000279const GRState*
280BasicConstraintManager::RemoveDeadBindings(const GRState* St,
281 SymbolReaper& SymReaper) {
282
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000283 GRStateRef state(St, StateMgr);
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000284 ConstEqTy CE = state.get<ConstEq>();
285 ConstEqTy::Factory& CEFactory = state.get_context<ConstEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000286
287 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000288 SymbolRef sym = I.getKey();
289 if (SymReaper.maybeDead(sym)) CE = CEFactory.Remove(CE, sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000290 }
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000291 state = state.set<ConstEq>(CE);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000292
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000293 ConstNotEqTy CNE = state.get<ConstNotEq>();
294 ConstNotEqTy::Factory& CNEFactory = state.get_context<ConstNotEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000295
296 for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000297 SymbolRef sym = I.getKey();
Ted Kremenek241677a2009-01-21 22:26:05 +0000298 if (SymReaper.maybeDead(sym)) CNE = CNEFactory.Remove(CNE, sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000299 }
300
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000301 return state.set<ConstNotEq>(CNE);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000302}
303
304void BasicConstraintManager::print(const GRState* St, std::ostream& Out,
305 const char* nl, const char *sep) {
306 // Print equality constraints.
307
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000308 ConstEqTy CE = St->get<ConstEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000309
310 if (!CE.isEmpty()) {
311 Out << nl << sep << "'==' constraints:";
312
313 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
314 Out << nl << " $" << I.getKey();
315 llvm::raw_os_ostream OS(Out);
316 OS << " : " << *I.getData();
317 }
318 }
319
320 // Print != constraints.
321
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000322 ConstNotEqTy CNE = St->get<ConstNotEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000323
324 if (!CNE.isEmpty()) {
325 Out << nl << sep << "'!=' constraints:";
326
327 for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) {
328 Out << nl << " $" << I.getKey() << " : ";
329 bool isFirst = true;
330
331 GRState::IntSetTy::iterator J = I.getData().begin(),
332 EJ = I.getData().end();
333
334 for ( ; J != EJ; ++J) {
335 if (isFirst) isFirst = false;
336 else Out << ", ";
337
Zhongxing Xu7d94e262008-11-10 05:00:06 +0000338 Out << (*J)->getSExtValue(); // Hack: should print to raw_ostream.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000339 }
340 }
341 }
Daniel Dunbar0e194dd2008-08-30 02:06:22 +0000342}