blob: 54c0afbff33eea82d9639975aac042bc4dcbffa3 [file] [log] [blame]
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001//= GRState.cpp - Path-Sensitive "State" for tracking values -----*- C++ -*--=//
Ted Kremenek9153f732008-02-05 07:17:49 +00002//
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//
Ted Kremenek53ba0b62009-06-24 23:06:47 +000010// This file implements GRState and GRStateManager.
Ted Kremenek9153f732008-02-05 07:17:49 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremeneke7aa9a12008-08-17 02:59:30 +000014#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenek4adc81e2008-08-13 04:27:00 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek729a9a22008-07-17 23:15:45 +000016#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
Ted Kremenek05125f12008-08-27 23:13:01 +000017#include "llvm/ADT/SmallSet.h"
Chris Lattner405674c2008-08-23 22:23:37 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek05125f12008-08-27 23:13:01 +000019
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000020using namespace clang;
21
Ted Kremenek05125f12008-08-27 23:13:01 +000022// Give the vtable for ConstraintManager somewhere to live.
Ted Kremenek53ba0b62009-06-24 23:06:47 +000023// FIXME: Move this elsewhere.
Ted Kremenek05125f12008-08-27 23:13:01 +000024ConstraintManager::~ConstraintManager() {}
25
Ted Kremenek1c72ef02008-08-16 00:49:49 +000026GRStateManager::~GRStateManager() {
27 for (std::vector<GRState::Printer*>::iterator I=Printers.begin(),
28 E=Printers.end(); I!=E; ++I)
29 delete *I;
30
31 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
32 I!=E; ++I)
33 I->second.second(I->second.first);
34}
35
Ted Kremenek4adc81e2008-08-13 04:27:00 +000036const GRState*
Ted Kremenek2ed14be2008-12-05 00:47:52 +000037GRStateManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +000038 SymbolReaper& SymReaper) {
39
Ted Kremenekb87d9092008-02-08 19:17:19 +000040 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
41 // The roots are any Block-level exprs and Decls that our liveness algorithm
42 // tells us are live. We then see what Decls they may reference, and keep
43 // those around. This code more than likely can be made faster, and the
44 // frequency of which this method is called should be experimented with
Ted Kremenek9e240492008-10-04 05:50:14 +000045 // for optimum performance.
46 llvm::SmallVector<const MemRegion*, 10> RegionRoots;
Ted Kremenek2ed14be2008-12-05 00:47:52 +000047 GRState NewState = *state;
Ted Kremenekf59bf482008-07-17 18:38:48 +000048
Ted Kremenek5216ad72009-02-14 03:16:10 +000049 NewState.Env = EnvMgr.RemoveDeadBindings(NewState.Env, Loc, SymReaper, *this,
Ted Kremenek5dc27462009-03-03 02:51:43 +000050 state, RegionRoots);
Ted Kremenek016f52f2008-02-08 21:10:02 +000051
Ted Kremenekf59bf482008-07-17 18:38:48 +000052 // Clean up the store.
Ted Kremenek241677a2009-01-21 22:26:05 +000053 NewState.St = StoreMgr->RemoveDeadBindings(&NewState, Loc, SymReaper,
54 RegionRoots);
Ted Kremenekffdbefd2008-08-17 03:10:22 +000055
Ted Kremenek2ed14be2008-12-05 00:47:52 +000056 return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewState),
Ted Kremenek241677a2009-01-21 22:26:05 +000057 SymReaper);
Ted Kremenekb87d9092008-02-08 19:17:19 +000058}
Ted Kremenek862d5bb2008-02-06 00:54:14 +000059
Ted Kremenek76500d02009-06-23 20:38:51 +000060const GRState *GRState::unbindLoc(Loc LV) const {
61 Store OldStore = getStore();
62 Store NewStore = Mgr->StoreMgr->Remove(OldStore, LV);
Ted Kremenek4323a572008-07-10 22:03:41 +000063
64 if (NewStore == OldStore)
Ted Kremenek76500d02009-06-23 20:38:51 +000065 return this;
Ted Kremenek4323a572008-07-10 22:03:41 +000066
Ted Kremenek76500d02009-06-23 20:38:51 +000067 GRState NewSt = *this;
Ted Kremenek4323a572008-07-10 22:03:41 +000068 NewSt.St = NewStore;
Ted Kremenek76500d02009-06-23 20:38:51 +000069 return Mgr->getPersistentState(NewSt);
Ted Kremenek4323a572008-07-10 22:03:41 +000070}
71
Ted Kremenek233e9132009-06-24 22:15:30 +000072SVal GRState::getSValAsScalarOrLoc(const MemRegion *R) const {
73 // We only want to do fetches from regions that we can actually bind
74 // values. For example, SymbolicRegions of type 'id<...>' cannot
75 // have direct bindings (but their can be bindings on their subregions).
76 if (!R->isBoundable())
77 return UnknownVal();
78
79 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
80 QualType T = TR->getValueType(Mgr->getContext());
81 if (Loc::IsLocType(T) || T->isIntegerType())
82 return getSVal(R);
83 }
84
85 return UnknownVal();
86}
87
Ted Kremenek4f596c22009-06-27 00:24:54 +000088
89const GRState *GRState::bindExpr(const Stmt* Ex, SVal V, bool isBlkExpr,
90 bool Invalidate) const {
91
92 Environment NewEnv = Mgr->EnvMgr.BindExpr(Env, Ex, V, isBlkExpr, Invalidate);
93
94 if (NewEnv == Env)
95 return this;
96
97 GRState NewSt = *this;
98 NewSt.Env = NewEnv;
99 return Mgr->getPersistentState(NewSt);
100}
101
102const GRState *GRState::bindExpr(const Stmt* Ex, SVal V,
103 bool Invalidate) const {
104
105 bool isBlkExpr = false;
106
107 if (Ex == Mgr->CurrentStmt) {
108 // FIXME: Should this just be an assertion? When would we want to set
109 // the value of a block-level expression if it wasn't CurrentStmt?
110 isBlkExpr = Mgr->cfg.isBlkExpr(Ex);
111
112 if (!isBlkExpr)
113 return this;
114 }
115
116 return bindExpr(Ex, V, isBlkExpr, Invalidate);
117}
118
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000119const GRState* GRStateManager::getInitialState() {
Ted Kremenek67f28532009-06-17 22:02:04 +0000120 GRState StateImpl(this, EnvMgr.getInitialEnvironment(),
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000121 StoreMgr->getInitialStore(),
Ted Kremenekffdbefd2008-08-17 03:10:22 +0000122 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +0000123
Ted Kremenek9153f732008-02-05 07:17:49 +0000124 return getPersistentState(StateImpl);
125}
126
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000127const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000128
129 llvm::FoldingSetNodeID ID;
130 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000131 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +0000132
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000133 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000134 return I;
135
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000136 GRState* I = (GRState*) Alloc.Allocate<GRState>();
137 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000138 StateSet.InsertNode(I, InsertPos);
139 return I;
140}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000141
Ted Kremenek67f28532009-06-17 22:02:04 +0000142const GRState* GRState::makeWithStore(Store store) const {
143 GRState NewSt = *this;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000144 NewSt.St = store;
Ted Kremenek67f28532009-06-17 22:02:04 +0000145 return Mgr->getPersistentState(NewSt);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000146}
147
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000148//===----------------------------------------------------------------------===//
149// State pretty-printing.
150//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000151
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000152void GRState::print(llvm::raw_ostream& Out, const char* nl,
153 const char* sep) const {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000154 // Print the store.
Ted Kremenekb65be702009-06-18 01:23:53 +0000155 Mgr->getStoreManager().print(getStore(), Out, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000156
157 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000158 bool isFirst = true;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000159
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000160 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000161
162 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000163 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000164 isFirst = false;
165 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000166 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000167
168 Out << " (" << (void*) I.getKey() << ") ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000169 LangOptions LO; // FIXME.
170 I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremeneke7d22112008-02-11 19:21:59 +0000171 Out << " : ";
172 I.getData().print(Out);
173 }
174
175 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000176 isFirst = true;
177
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000178 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000179
180 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000181 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000182 isFirst = false;
183 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000184 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000185
186 Out << " (" << (void*) I.getKey() << ") ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000187 LangOptions LO; // FIXME.
188 I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremeneke7d22112008-02-11 19:21:59 +0000189 Out << " : ";
190 I.getData().print(Out);
191 }
192
Ted Kremenekb65be702009-06-18 01:23:53 +0000193 Mgr->getConstraintManager().print(this, Out, nl, sep);
Ted Kremenek461f9772008-03-11 18:57:24 +0000194
Ted Kremenekb65be702009-06-18 01:23:53 +0000195 // Print checker-specific data.
196 for (std::vector<Printer*>::iterator I = Mgr->Printers.begin(),
197 E = Mgr->Printers.end(); I != E; ++I) {
198 (*I)->Print(Out, this, nl, sep);
199 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000200}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000201
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000202void GRState::printDOT(llvm::raw_ostream& Out) const {
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000203 print(Out, "\\l", "\\|");
204}
205
Ted Kremenekb65be702009-06-18 01:23:53 +0000206void GRState::printStdErr() const {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000207 print(llvm::errs());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000208}
209
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000210//===----------------------------------------------------------------------===//
211// Generic Data Map.
212//===----------------------------------------------------------------------===//
213
214void* const* GRState::FindGDM(void* K) const {
215 return GDM.lookup(K);
216}
217
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000218void*
219GRStateManager::FindGDMContext(void* K,
220 void* (*CreateContext)(llvm::BumpPtrAllocator&),
221 void (*DeleteContext)(void*)) {
222
223 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
224 if (!p.first) {
225 p.first = CreateContext(Alloc);
226 p.second = DeleteContext;
227 }
228
229 return p.first;
230}
231
Zhongxing Xu4230da62008-11-03 05:18:34 +0000232const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000233 GRState::GenericDataMap M1 = St->getGDM();
234 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
235
236 if (M1 == M2)
237 return St;
238
239 GRState NewSt = *St;
240 NewSt.GDM = M2;
241 return getPersistentState(NewSt);
242}
Ted Kremenek584def72008-07-22 00:46:16 +0000243
244//===----------------------------------------------------------------------===//
Ted Kremenek5216ad72009-02-14 03:16:10 +0000245// Utility.
246//===----------------------------------------------------------------------===//
247
Ted Kremenek5dc27462009-03-03 02:51:43 +0000248namespace {
Zhongxing Xu63d1d602009-03-04 06:33:38 +0000249class VISIBILITY_HIDDEN ScanReachableSymbols : public SubRegionMap::Visitor {
Ted Kremenek5dc27462009-03-03 02:51:43 +0000250 typedef llvm::DenseSet<const MemRegion*> VisitedRegionsTy;
251
252 VisitedRegionsTy visited;
Ted Kremenek47fed902009-06-18 01:33:24 +0000253 const GRState *state;
Ted Kremenek5dc27462009-03-03 02:51:43 +0000254 SymbolVisitor &visitor;
255 llvm::OwningPtr<SubRegionMap> SRM;
256public:
257
Ted Kremenek47fed902009-06-18 01:33:24 +0000258 ScanReachableSymbols(const GRState *st, SymbolVisitor& v)
259 : state(st), visitor(v) {}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000260
261 bool scan(nonloc::CompoundVal val);
262 bool scan(SVal val);
263 bool scan(const MemRegion *R);
264
265 // From SubRegionMap::Visitor.
266 bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) {
267 return scan(SubRegion);
268 }
269};
270}
271
272bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
Ted Kremenek5216ad72009-02-14 03:16:10 +0000273 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
Ted Kremenek5dc27462009-03-03 02:51:43 +0000274 if (!scan(*I))
275 return false;
Ted Kremenek5216ad72009-02-14 03:16:10 +0000276
277 return true;
278}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000279
280bool ScanReachableSymbols::scan(SVal val) {
281 if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
282 return scan(X->getRegion());
Ted Kremenek380022d2009-03-30 18:45:36 +0000283
284 if (SymbolRef Sym = val.getAsSymbol())
285 return visitor.VisitSymbol(Sym);
Ted Kremenek5216ad72009-02-14 03:16:10 +0000286
287 if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000288 return scan(*X);
Ted Kremenek5216ad72009-02-14 03:16:10 +0000289
290 return true;
291}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000292
293bool ScanReachableSymbols::scan(const MemRegion *R) {
Ted Kremenek1cb151e2009-03-04 00:13:10 +0000294 if (isa<MemSpaceRegion>(R) || visited.count(R))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000295 return true;
296
297 visited.insert(R);
298
299 // If this is a symbolic region, visit the symbol for the region.
300 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
301 if (!visitor.VisitSymbol(SR->getSymbol()))
302 return false;
303
304 // If this is a subregion, also visit the parent regions.
305 if (const SubRegion *SR = dyn_cast<SubRegion>(R))
Ted Kremenek6076e0a2009-03-03 18:15:30 +0000306 if (!scan(SR->getSuperRegion()))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000307 return false;
308
309 // Now look at the binding to this region (if any).
Ted Kremenek47fed902009-06-18 01:33:24 +0000310 if (!scan(state->getSValAsScalarOrLoc(R)))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000311 return false;
312
313 // Now look at the subregions.
314 if (!SRM.get())
Ted Kremenek47fed902009-06-18 01:33:24 +0000315 SRM.reset(state->getStateManager().getStoreManager().getSubRegionMap(state));
Ted Kremenek5dc27462009-03-03 02:51:43 +0000316
317 return SRM->iterSubRegions(R, *this);
318}
319
Ted Kremenek47fed902009-06-18 01:33:24 +0000320bool GRState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
321 ScanReachableSymbols S(this, visitor);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000322 return S.scan(val);
323}
Ted Kremenek5216ad72009-02-14 03:16:10 +0000324
325//===----------------------------------------------------------------------===//
Ted Kremenek584def72008-07-22 00:46:16 +0000326// Queries.
327//===----------------------------------------------------------------------===//
328
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000329bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000330 const llvm::APSInt& Y) {
331
Ted Kremenekdbc2afc2009-06-23 17:55:07 +0000332 SVal V = state->getSVal(Ex);
Ted Kremenek584def72008-07-22 00:46:16 +0000333
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000334 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000335 return X->getValue() == Y;
336
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000337 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000338 return X->getValue() == Y;
339
Ted Kremenek380022d2009-03-30 18:45:36 +0000340 if (SymbolRef Sym = V.getAsSymbol())
341 return ConstraintMgr->isEqual(state, Sym, Y);
342
Ted Kremenek584def72008-07-22 00:46:16 +0000343 return false;
344}
345
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000346bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenek044b6f02009-04-09 16:13:17 +0000347 return isEqual(state, Ex, getBasicVals().getValue(x, Ex->getType()));
Ted Kremenek584def72008-07-22 00:46:16 +0000348}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000349
350//===----------------------------------------------------------------------===//
351// Persistent values for indexing into the Generic Data Map.
352
353int GRState::NullDerefTag::TagInt = 0;
354