blob: 7bef351006233d0df433035bc6dfa413f3199e02 [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 Kremenek0fb0bc42009-08-27 01:39:13 +000049 NewState.Env = EnvMgr.RemoveDeadBindings(NewState.Env, Loc, SymReaper,
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 Kremenek2f26bc32009-08-02 04:45:08 +000053 StoreMgr->RemoveDeadBindings(NewState, Loc, SymReaper, RegionRoots);
Ted Kremenekffdbefd2008-08-17 03:10:22 +000054
Ted Kremenek2ed14be2008-12-05 00:47:52 +000055 return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewState),
Ted Kremenek241677a2009-01-21 22:26:05 +000056 SymReaper);
Ted Kremenekb87d9092008-02-08 19:17:19 +000057}
Ted Kremenek862d5bb2008-02-06 00:54:14 +000058
Ted Kremenek76500d02009-06-23 20:38:51 +000059const GRState *GRState::unbindLoc(Loc LV) const {
60 Store OldStore = getStore();
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000061 Store NewStore = getStateManager().StoreMgr->Remove(OldStore, LV);
Ted Kremenek4323a572008-07-10 22:03:41 +000062
63 if (NewStore == OldStore)
Ted Kremenek76500d02009-06-23 20:38:51 +000064 return this;
Ted Kremenek4323a572008-07-10 22:03:41 +000065
Ted Kremenek76500d02009-06-23 20:38:51 +000066 GRState NewSt = *this;
Ted Kremenek4323a572008-07-10 22:03:41 +000067 NewSt.St = NewStore;
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000068 return getStateManager().getPersistentState(NewSt);
Ted Kremenek4323a572008-07-10 22:03:41 +000069}
70
Ted Kremenek233e9132009-06-24 22:15:30 +000071SVal GRState::getSValAsScalarOrLoc(const MemRegion *R) const {
72 // We only want to do fetches from regions that we can actually bind
73 // values. For example, SymbolicRegions of type 'id<...>' cannot
74 // have direct bindings (but their can be bindings on their subregions).
75 if (!R->isBoundable())
76 return UnknownVal();
77
78 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000079 QualType T = TR->getValueType(getStateManager().getContext());
Ted Kremenek233e9132009-06-24 22:15:30 +000080 if (Loc::IsLocType(T) || T->isIntegerType())
81 return getSVal(R);
82 }
83
84 return UnknownVal();
85}
86
Ted Kremenek4f596c22009-06-27 00:24:54 +000087
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000088const GRState *GRState::bindExpr(const Stmt* Ex, SVal V, bool Invalidate) const {
Ted Kremenek4f596c22009-06-27 00:24:54 +000089
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000090 Environment NewEnv = getStateManager().EnvMgr.BindExpr(Env, Ex, V,
91 Invalidate);
Ted Kremenek4f596c22009-06-27 00:24:54 +000092
93 if (NewEnv == Env)
94 return this;
95
96 GRState NewSt = *this;
97 NewSt.Env = NewEnv;
Ted Kremenek0fb0bc42009-08-27 01:39:13 +000098 return getStateManager().getPersistentState(NewSt);
Ted Kremenek4f596c22009-06-27 00:24:54 +000099}
100
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000101const GRState* GRStateManager::getInitialState(const LocationContext *InitLoc) {
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000102 GRState State(this, InitLoc->getAnalysisContext(),
103 EnvMgr.getInitialEnvironment(),
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000104 StoreMgr->getInitialStore(InitLoc),
105 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +0000106
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000107 return getPersistentState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000108}
109
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000110const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000111
112 llvm::FoldingSetNodeID ID;
113 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000114 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +0000115
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000116 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000117 return I;
118
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000119 GRState* I = (GRState*) Alloc.Allocate<GRState>();
120 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000121 StateSet.InsertNode(I, InsertPos);
122 return I;
123}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000124
Ted Kremenek67f28532009-06-17 22:02:04 +0000125const GRState* GRState::makeWithStore(Store store) const {
126 GRState NewSt = *this;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000127 NewSt.St = store;
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000128 return getStateManager().getPersistentState(NewSt);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000129}
130
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000131//===----------------------------------------------------------------------===//
132// State pretty-printing.
133//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000134
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000135void GRState::print(llvm::raw_ostream& Out, const char* nl,
136 const char* sep) const {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000137 // Print the store.
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000138 GRStateManager &Mgr = getStateManager();
139 Mgr.getStoreManager().print(getStore(), Out, nl, sep);
140
141 CFG &C = *getAnalysisContext().getCFG();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000142
143 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000144 bool isFirst = true;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000145
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000146 for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
147 if (C.isBlkExpr(I.getKey()))
148 continue;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000149
150 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000151 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000152 isFirst = false;
153 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000154 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000155
156 Out << " (" << (void*) I.getKey() << ") ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000157 LangOptions LO; // FIXME.
158 I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000159 Out << " : " << I.getData();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000160 }
161
162 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000163 isFirst = true;
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000164
165 for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
166 if (!C.isBlkExpr(I.getKey()))
167 continue;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000168
169 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000170 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000171 isFirst = false;
172 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000173 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000174
175 Out << " (" << (void*) I.getKey() << ") ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000176 LangOptions LO; // FIXME.
177 I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000178 Out << " : " << I.getData();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000179 }
180
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000181 Mgr.getConstraintManager().print(this, Out, nl, sep);
Ted Kremenek461f9772008-03-11 18:57:24 +0000182
Ted Kremenekb65be702009-06-18 01:23:53 +0000183 // Print checker-specific data.
Ted Kremenek0fb0bc42009-08-27 01:39:13 +0000184 for (std::vector<Printer*>::iterator I = Mgr.Printers.begin(),
185 E = Mgr.Printers.end(); I != E; ++I) {
Ted Kremenekb65be702009-06-18 01:23:53 +0000186 (*I)->Print(Out, this, nl, sep);
187 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000188}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000189
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000190void GRState::printDOT(llvm::raw_ostream& Out) const {
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000191 print(Out, "\\l", "\\|");
192}
193
Ted Kremenekb65be702009-06-18 01:23:53 +0000194void GRState::printStdErr() const {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000195 print(llvm::errs());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000196}
197
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000198//===----------------------------------------------------------------------===//
199// Generic Data Map.
200//===----------------------------------------------------------------------===//
201
202void* const* GRState::FindGDM(void* K) const {
203 return GDM.lookup(K);
204}
205
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000206void*
207GRStateManager::FindGDMContext(void* K,
208 void* (*CreateContext)(llvm::BumpPtrAllocator&),
209 void (*DeleteContext)(void*)) {
210
211 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
212 if (!p.first) {
213 p.first = CreateContext(Alloc);
214 p.second = DeleteContext;
215 }
216
217 return p.first;
218}
219
Zhongxing Xu4230da62008-11-03 05:18:34 +0000220const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000221 GRState::GenericDataMap M1 = St->getGDM();
222 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
223
224 if (M1 == M2)
225 return St;
226
227 GRState NewSt = *St;
228 NewSt.GDM = M2;
229 return getPersistentState(NewSt);
230}
Ted Kremenek584def72008-07-22 00:46:16 +0000231
232//===----------------------------------------------------------------------===//
Ted Kremenek5216ad72009-02-14 03:16:10 +0000233// Utility.
234//===----------------------------------------------------------------------===//
235
Ted Kremenek5dc27462009-03-03 02:51:43 +0000236namespace {
Zhongxing Xu63d1d602009-03-04 06:33:38 +0000237class VISIBILITY_HIDDEN ScanReachableSymbols : public SubRegionMap::Visitor {
Ted Kremenek5dc27462009-03-03 02:51:43 +0000238 typedef llvm::DenseSet<const MemRegion*> VisitedRegionsTy;
239
240 VisitedRegionsTy visited;
Ted Kremenek47fed902009-06-18 01:33:24 +0000241 const GRState *state;
Ted Kremenek5dc27462009-03-03 02:51:43 +0000242 SymbolVisitor &visitor;
243 llvm::OwningPtr<SubRegionMap> SRM;
244public:
245
Ted Kremenek47fed902009-06-18 01:33:24 +0000246 ScanReachableSymbols(const GRState *st, SymbolVisitor& v)
247 : state(st), visitor(v) {}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000248
249 bool scan(nonloc::CompoundVal val);
250 bool scan(SVal val);
251 bool scan(const MemRegion *R);
252
253 // From SubRegionMap::Visitor.
254 bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) {
255 return scan(SubRegion);
256 }
257};
258}
259
260bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
Ted Kremenek5216ad72009-02-14 03:16:10 +0000261 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
Ted Kremenek5dc27462009-03-03 02:51:43 +0000262 if (!scan(*I))
263 return false;
Ted Kremenek5216ad72009-02-14 03:16:10 +0000264
265 return true;
266}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000267
268bool ScanReachableSymbols::scan(SVal val) {
269 if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
270 return scan(X->getRegion());
Ted Kremenek380022d2009-03-30 18:45:36 +0000271
272 if (SymbolRef Sym = val.getAsSymbol())
273 return visitor.VisitSymbol(Sym);
Ted Kremenek5216ad72009-02-14 03:16:10 +0000274
275 if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000276 return scan(*X);
Ted Kremenek5216ad72009-02-14 03:16:10 +0000277
278 return true;
279}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000280
281bool ScanReachableSymbols::scan(const MemRegion *R) {
Ted Kremenek1cb151e2009-03-04 00:13:10 +0000282 if (isa<MemSpaceRegion>(R) || visited.count(R))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000283 return true;
284
285 visited.insert(R);
286
287 // If this is a symbolic region, visit the symbol for the region.
288 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
289 if (!visitor.VisitSymbol(SR->getSymbol()))
290 return false;
291
292 // If this is a subregion, also visit the parent regions.
293 if (const SubRegion *SR = dyn_cast<SubRegion>(R))
Ted Kremenek6076e0a2009-03-03 18:15:30 +0000294 if (!scan(SR->getSuperRegion()))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000295 return false;
296
297 // Now look at the binding to this region (if any).
Ted Kremenek47fed902009-06-18 01:33:24 +0000298 if (!scan(state->getSValAsScalarOrLoc(R)))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000299 return false;
300
301 // Now look at the subregions.
302 if (!SRM.get())
Ted Kremenek47fed902009-06-18 01:33:24 +0000303 SRM.reset(state->getStateManager().getStoreManager().getSubRegionMap(state));
Ted Kremenek5dc27462009-03-03 02:51:43 +0000304
305 return SRM->iterSubRegions(R, *this);
306}
307
Ted Kremenek47fed902009-06-18 01:33:24 +0000308bool GRState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
309 ScanReachableSymbols S(this, visitor);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000310 return S.scan(val);
311}
Ted Kremenek5216ad72009-02-14 03:16:10 +0000312
313//===----------------------------------------------------------------------===//
Ted Kremenek584def72008-07-22 00:46:16 +0000314// Queries.
315//===----------------------------------------------------------------------===//
316
Ted Kremenek5f85e172009-07-22 22:35:28 +0000317bool GRStateManager::isEqual(const GRState* state, const Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000318 const llvm::APSInt& Y) {
319
Ted Kremenekdbc2afc2009-06-23 17:55:07 +0000320 SVal V = state->getSVal(Ex);
Ted Kremenek584def72008-07-22 00:46:16 +0000321
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000322 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000323 return X->getValue() == Y;
324
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000325 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000326 return X->getValue() == Y;
327
Ted Kremenek380022d2009-03-30 18:45:36 +0000328 if (SymbolRef Sym = V.getAsSymbol())
329 return ConstraintMgr->isEqual(state, Sym, Y);
330
Ted Kremenek584def72008-07-22 00:46:16 +0000331 return false;
332}
333
Ted Kremenek5f85e172009-07-22 22:35:28 +0000334bool GRStateManager::isEqual(const GRState* state, const Expr* Ex, uint64_t x) {
Ted Kremenek044b6f02009-04-09 16:13:17 +0000335 return isEqual(state, Ex, getBasicVals().getValue(x, Ex->getType()));
Ted Kremenek584def72008-07-22 00:46:16 +0000336}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000337
338//===----------------------------------------------------------------------===//
339// Persistent values for indexing into the Generic Data Map.
340
341int GRState::NullDerefTag::TagInt = 0;
342