blob: dc7c799288ccf440dc3c1128fd9154c64d1e7e2a [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 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();
61 Store NewStore = Mgr->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 Kremenek76500d02009-06-23 20:38:51 +000068 return Mgr->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)) {
79 QualType T = TR->getValueType(Mgr->getContext());
80 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
88const GRState *GRState::bindExpr(const Stmt* Ex, SVal V, bool isBlkExpr,
89 bool Invalidate) const {
90
91 Environment NewEnv = Mgr->EnvMgr.BindExpr(Env, Ex, V, isBlkExpr, Invalidate);
92
93 if (NewEnv == Env)
94 return this;
95
96 GRState NewSt = *this;
97 NewSt.Env = NewEnv;
98 return Mgr->getPersistentState(NewSt);
99}
100
101const GRState *GRState::bindExpr(const Stmt* Ex, SVal V,
102 bool Invalidate) const {
103
104 bool isBlkExpr = false;
105
106 if (Ex == Mgr->CurrentStmt) {
107 // FIXME: Should this just be an assertion? When would we want to set
108 // the value of a block-level expression if it wasn't CurrentStmt?
109 isBlkExpr = Mgr->cfg.isBlkExpr(Ex);
110
111 if (!isBlkExpr)
112 return this;
113 }
114
115 return bindExpr(Ex, V, isBlkExpr, Invalidate);
116}
117
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000118const GRState* GRStateManager::getInitialState() {
Ted Kremenek67f28532009-06-17 22:02:04 +0000119 GRState StateImpl(this, EnvMgr.getInitialEnvironment(),
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000120 StoreMgr->getInitialStore(),
Ted Kremenekffdbefd2008-08-17 03:10:22 +0000121 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +0000122
Ted Kremenek9153f732008-02-05 07:17:49 +0000123 return getPersistentState(StateImpl);
124}
125
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000126const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000127
128 llvm::FoldingSetNodeID ID;
129 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000130 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +0000131
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000132 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000133 return I;
134
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000135 GRState* I = (GRState*) Alloc.Allocate<GRState>();
136 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000137 StateSet.InsertNode(I, InsertPos);
138 return I;
139}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000140
Ted Kremenek67f28532009-06-17 22:02:04 +0000141const GRState* GRState::makeWithStore(Store store) const {
142 GRState NewSt = *this;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000143 NewSt.St = store;
Ted Kremenek67f28532009-06-17 22:02:04 +0000144 return Mgr->getPersistentState(NewSt);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000145}
146
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000147//===----------------------------------------------------------------------===//
148// State pretty-printing.
149//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000150
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000151void GRState::print(llvm::raw_ostream& Out, const char* nl,
152 const char* sep) const {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000153 // Print the store.
Ted Kremenekb65be702009-06-18 01:23:53 +0000154 Mgr->getStoreManager().print(getStore(), Out, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000155
156 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000157 bool isFirst = true;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000158
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000159 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000160
161 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000162 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000163 isFirst = false;
164 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000165 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000166
167 Out << " (" << (void*) I.getKey() << ") ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000168 LangOptions LO; // FIXME.
169 I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000170 Out << " : " << I.getData();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000171 }
172
173 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000174 isFirst = true;
175
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000176 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000177
178 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000179 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000180 isFirst = false;
181 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000182 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000183
184 Out << " (" << (void*) I.getKey() << ") ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000185 LangOptions LO; // FIXME.
186 I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000187 Out << " : " << I.getData();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000188 }
189
Ted Kremenekb65be702009-06-18 01:23:53 +0000190 Mgr->getConstraintManager().print(this, Out, nl, sep);
Ted Kremenek461f9772008-03-11 18:57:24 +0000191
Ted Kremenekb65be702009-06-18 01:23:53 +0000192 // Print checker-specific data.
193 for (std::vector<Printer*>::iterator I = Mgr->Printers.begin(),
194 E = Mgr->Printers.end(); I != E; ++I) {
195 (*I)->Print(Out, this, nl, sep);
196 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000197}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000198
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000199void GRState::printDOT(llvm::raw_ostream& Out) const {
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000200 print(Out, "\\l", "\\|");
201}
202
Ted Kremenekb65be702009-06-18 01:23:53 +0000203void GRState::printStdErr() const {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000204 print(llvm::errs());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000205}
206
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000207//===----------------------------------------------------------------------===//
208// Generic Data Map.
209//===----------------------------------------------------------------------===//
210
211void* const* GRState::FindGDM(void* K) const {
212 return GDM.lookup(K);
213}
214
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000215void*
216GRStateManager::FindGDMContext(void* K,
217 void* (*CreateContext)(llvm::BumpPtrAllocator&),
218 void (*DeleteContext)(void*)) {
219
220 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
221 if (!p.first) {
222 p.first = CreateContext(Alloc);
223 p.second = DeleteContext;
224 }
225
226 return p.first;
227}
228
Zhongxing Xu4230da62008-11-03 05:18:34 +0000229const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000230 GRState::GenericDataMap M1 = St->getGDM();
231 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
232
233 if (M1 == M2)
234 return St;
235
236 GRState NewSt = *St;
237 NewSt.GDM = M2;
238 return getPersistentState(NewSt);
239}
Ted Kremenek584def72008-07-22 00:46:16 +0000240
241//===----------------------------------------------------------------------===//
Ted Kremenek5216ad72009-02-14 03:16:10 +0000242// Utility.
243//===----------------------------------------------------------------------===//
244
Ted Kremenek5dc27462009-03-03 02:51:43 +0000245namespace {
Zhongxing Xu63d1d602009-03-04 06:33:38 +0000246class VISIBILITY_HIDDEN ScanReachableSymbols : public SubRegionMap::Visitor {
Ted Kremenek5dc27462009-03-03 02:51:43 +0000247 typedef llvm::DenseSet<const MemRegion*> VisitedRegionsTy;
248
249 VisitedRegionsTy visited;
Ted Kremenek47fed902009-06-18 01:33:24 +0000250 const GRState *state;
Ted Kremenek5dc27462009-03-03 02:51:43 +0000251 SymbolVisitor &visitor;
252 llvm::OwningPtr<SubRegionMap> SRM;
253public:
254
Ted Kremenek47fed902009-06-18 01:33:24 +0000255 ScanReachableSymbols(const GRState *st, SymbolVisitor& v)
256 : state(st), visitor(v) {}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000257
258 bool scan(nonloc::CompoundVal val);
259 bool scan(SVal val);
260 bool scan(const MemRegion *R);
261
262 // From SubRegionMap::Visitor.
263 bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) {
264 return scan(SubRegion);
265 }
266};
267}
268
269bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
Ted Kremenek5216ad72009-02-14 03:16:10 +0000270 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
Ted Kremenek5dc27462009-03-03 02:51:43 +0000271 if (!scan(*I))
272 return false;
Ted Kremenek5216ad72009-02-14 03:16:10 +0000273
274 return true;
275}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000276
277bool ScanReachableSymbols::scan(SVal val) {
278 if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
279 return scan(X->getRegion());
Ted Kremenek380022d2009-03-30 18:45:36 +0000280
281 if (SymbolRef Sym = val.getAsSymbol())
282 return visitor.VisitSymbol(Sym);
Ted Kremenek5216ad72009-02-14 03:16:10 +0000283
284 if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000285 return scan(*X);
Ted Kremenek5216ad72009-02-14 03:16:10 +0000286
287 return true;
288}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000289
290bool ScanReachableSymbols::scan(const MemRegion *R) {
Ted Kremenek1cb151e2009-03-04 00:13:10 +0000291 if (isa<MemSpaceRegion>(R) || visited.count(R))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000292 return true;
293
294 visited.insert(R);
295
296 // If this is a symbolic region, visit the symbol for the region.
297 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
298 if (!visitor.VisitSymbol(SR->getSymbol()))
299 return false;
300
301 // If this is a subregion, also visit the parent regions.
302 if (const SubRegion *SR = dyn_cast<SubRegion>(R))
Ted Kremenek6076e0a2009-03-03 18:15:30 +0000303 if (!scan(SR->getSuperRegion()))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000304 return false;
305
306 // Now look at the binding to this region (if any).
Ted Kremenek47fed902009-06-18 01:33:24 +0000307 if (!scan(state->getSValAsScalarOrLoc(R)))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000308 return false;
309
310 // Now look at the subregions.
311 if (!SRM.get())
Ted Kremenek47fed902009-06-18 01:33:24 +0000312 SRM.reset(state->getStateManager().getStoreManager().getSubRegionMap(state));
Ted Kremenek5dc27462009-03-03 02:51:43 +0000313
314 return SRM->iterSubRegions(R, *this);
315}
316
Ted Kremenek47fed902009-06-18 01:33:24 +0000317bool GRState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
318 ScanReachableSymbols S(this, visitor);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000319 return S.scan(val);
320}
Ted Kremenek5216ad72009-02-14 03:16:10 +0000321
322//===----------------------------------------------------------------------===//
Ted Kremenek584def72008-07-22 00:46:16 +0000323// Queries.
324//===----------------------------------------------------------------------===//
325
Ted Kremenek5f85e172009-07-22 22:35:28 +0000326bool GRStateManager::isEqual(const GRState* state, const Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000327 const llvm::APSInt& Y) {
328
Ted Kremenekdbc2afc2009-06-23 17:55:07 +0000329 SVal V = state->getSVal(Ex);
Ted Kremenek584def72008-07-22 00:46:16 +0000330
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000331 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000332 return X->getValue() == Y;
333
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000334 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000335 return X->getValue() == Y;
336
Ted Kremenek380022d2009-03-30 18:45:36 +0000337 if (SymbolRef Sym = V.getAsSymbol())
338 return ConstraintMgr->isEqual(state, Sym, Y);
339
Ted Kremenek584def72008-07-22 00:46:16 +0000340 return false;
341}
342
Ted Kremenek5f85e172009-07-22 22:35:28 +0000343bool GRStateManager::isEqual(const GRState* state, const Expr* Ex, uint64_t x) {
Ted Kremenek044b6f02009-04-09 16:13:17 +0000344 return isEqual(state, Ex, getBasicVals().getValue(x, Ex->getType()));
Ted Kremenek584def72008-07-22 00:46:16 +0000345}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000346
347//===----------------------------------------------------------------------===//
348// Persistent values for indexing into the Generic Data Map.
349
350int GRState::NullDerefTag::TagInt = 0;
351