blob: 1f907baa24997b8792cfc44bdd3e5cf899fdd51e [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 Kremenekc8781382009-06-17 22:28:13 +0000229const GRState* BasicConstraintManager::AddEQ(const GRState* state, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000230 const llvm::APSInt& V) {
231 // Create a new state with the old binding replaced.
Ted Kremenekc8781382009-06-17 22:28:13 +0000232 return state->set<ConstEq>(sym, &V);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000233}
234
Ted Kremenekc8781382009-06-17 22:28:13 +0000235const GRState* BasicConstraintManager::AddNE(const GRState* state, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000236 const llvm::APSInt& V) {
Zhongxing Xuf0bc50e2008-11-27 06:08:40 +0000237
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000238 // First, retrieve the NE-set associated with the given symbol.
Ted Kremenekc8781382009-06-17 22:28:13 +0000239 ConstNotEqTy::data_type* T = state->get<ConstNotEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000240 GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000241
242 // Now add V to the NE set.
243 S = ISetFactory.Add(S, &V);
244
245 // Create a new state with the old binding replaced.
Ted Kremenekc8781382009-06-17 22:28:13 +0000246 return state->set<ConstNotEq>(sym, S);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000247}
248
249const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* St,
Ted Kremenek45021952009-02-14 17:08:39 +0000250 SymbolRef sym) const {
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000251 const ConstEqTy::data_type* T = St->get<ConstEq>(sym);
Ted Kremenek45021952009-02-14 17:08:39 +0000252 return T ? *T : NULL;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000253}
254
Ted Kremenek2dabd432008-12-05 02:27:51 +0000255bool BasicConstraintManager::isNotEqual(const GRState* St, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000256 const llvm::APSInt& V) const {
257
258 // Retrieve the NE-set associated with the given symbol.
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000259 const ConstNotEqTy::data_type* T = St->get<ConstNotEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000260
261 // See if V is present in the NE-set.
262 return T ? T->contains(&V) : false;
263}
264
Ted Kremenek2dabd432008-12-05 02:27:51 +0000265bool BasicConstraintManager::isEqual(const GRState* St, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000266 const llvm::APSInt& V) const {
267 // Retrieve the EQ-set associated with the given symbol.
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000268 const ConstEqTy::data_type* T = St->get<ConstEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000269 // See if V is present in the EQ-set.
270 return T ? **T == V : false;
271}
272
Zhongxing Xu8fd9b352008-11-27 02:39:34 +0000273/// Scan all symbols referenced by the constraints. If the symbol is not alive
274/// as marked in LSymbols, mark it as dead in DSymbols.
Ted Kremenek241677a2009-01-21 22:26:05 +0000275const GRState*
Ted Kremenekc8781382009-06-17 22:28:13 +0000276BasicConstraintManager::RemoveDeadBindings(const GRState* state,
Ted Kremenek241677a2009-01-21 22:26:05 +0000277 SymbolReaper& SymReaper) {
278
Ted Kremenekc8781382009-06-17 22:28:13 +0000279 ConstEqTy CE = state->get<ConstEq>();
280 ConstEqTy::Factory& CEFactory = state->get_context<ConstEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000281
282 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000283 SymbolRef sym = I.getKey();
284 if (SymReaper.maybeDead(sym)) CE = CEFactory.Remove(CE, sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000285 }
Ted Kremenekc8781382009-06-17 22:28:13 +0000286 state = state->set<ConstEq>(CE);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000287
Ted Kremenekc8781382009-06-17 22:28:13 +0000288 ConstNotEqTy CNE = state->get<ConstNotEq>();
289 ConstNotEqTy::Factory& CNEFactory = state->get_context<ConstNotEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000290
291 for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000292 SymbolRef sym = I.getKey();
Ted Kremenek241677a2009-01-21 22:26:05 +0000293 if (SymReaper.maybeDead(sym)) CNE = CNEFactory.Remove(CNE, sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000294 }
295
Ted Kremenekc8781382009-06-17 22:28:13 +0000296 return state->set<ConstNotEq>(CNE);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000297}
298
299void BasicConstraintManager::print(const GRState* St, std::ostream& Out,
300 const char* nl, const char *sep) {
301 // Print equality constraints.
302
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000303 ConstEqTy CE = St->get<ConstEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000304
305 if (!CE.isEmpty()) {
306 Out << nl << sep << "'==' constraints:";
307
308 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
309 Out << nl << " $" << I.getKey();
310 llvm::raw_os_ostream OS(Out);
311 OS << " : " << *I.getData();
312 }
313 }
314
315 // Print != constraints.
316
Ted Kremenek8ee74d52009-01-26 06:04:53 +0000317 ConstNotEqTy CNE = St->get<ConstNotEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000318
319 if (!CNE.isEmpty()) {
320 Out << nl << sep << "'!=' constraints:";
321
322 for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) {
323 Out << nl << " $" << I.getKey() << " : ";
324 bool isFirst = true;
325
326 GRState::IntSetTy::iterator J = I.getData().begin(),
327 EJ = I.getData().end();
328
329 for ( ; J != EJ; ++J) {
330 if (isFirst) isFirst = false;
331 else Out << ", ";
332
Zhongxing Xu7d94e262008-11-10 05:00:06 +0000333 Out << (*J)->getSExtValue(); // Hack: should print to raw_ostream.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000334 }
335 }
336 }
Daniel Dunbar0e194dd2008-08-30 02:06:22 +0000337}