blob: e0e478c30717a7a84923b575ffd8a993e56448c9 [file] [log] [blame]
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001//= GRState*cpp - Path-Sens. "State" for tracking valuues -----*- 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 Kremenek2dabd432008-12-05 02:27:51 +000010// This file defines SymbolRef, ExprBindKey, and GRState*
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.
23ConstraintManager::~ConstraintManager() {}
24
Ted Kremenek1c72ef02008-08-16 00:49:49 +000025GRStateManager::~GRStateManager() {
26 for (std::vector<GRState::Printer*>::iterator I=Printers.begin(),
27 E=Printers.end(); I!=E; ++I)
28 delete *I;
29
30 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
31 I!=E; ++I)
32 I->second.second(I->second.first);
33}
34
Ted Kremenek4adc81e2008-08-13 04:27:00 +000035const GRState*
Ted Kremenek2ed14be2008-12-05 00:47:52 +000036GRStateManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +000037 SymbolReaper& SymReaper) {
38
Ted Kremenekb87d9092008-02-08 19:17:19 +000039 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
40 // The roots are any Block-level exprs and Decls that our liveness algorithm
41 // tells us are live. We then see what Decls they may reference, and keep
42 // those around. This code more than likely can be made faster, and the
43 // frequency of which this method is called should be experimented with
Ted Kremenek9e240492008-10-04 05:50:14 +000044 // for optimum performance.
45 llvm::SmallVector<const MemRegion*, 10> RegionRoots;
Ted Kremenek2ed14be2008-12-05 00:47:52 +000046 GRState NewState = *state;
Ted Kremenekf59bf482008-07-17 18:38:48 +000047
Ted Kremenek5216ad72009-02-14 03:16:10 +000048 NewState.Env = EnvMgr.RemoveDeadBindings(NewState.Env, Loc, SymReaper, *this,
Ted Kremenek5dc27462009-03-03 02:51:43 +000049 state, RegionRoots);
Ted Kremenek016f52f2008-02-08 21:10:02 +000050
Ted Kremenekf59bf482008-07-17 18:38:48 +000051 // Clean up the store.
Ted Kremenek241677a2009-01-21 22:26:05 +000052 NewState.St = StoreMgr->RemoveDeadBindings(&NewState, Loc, SymReaper,
53 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
Zhongxing Xu1c96b242008-10-17 05:57:07 +000059const GRState* GRStateManager::Unbind(const GRState* St, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +000060 Store OldStore = St->getStore();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000061 Store NewStore = StoreMgr->Remove(OldStore, LV);
Ted Kremenek4323a572008-07-10 22:03:41 +000062
63 if (NewStore == OldStore)
64 return St;
65
Ted Kremenek4adc81e2008-08-13 04:27:00 +000066 GRState NewSt = *St;
Ted Kremenek4323a572008-07-10 22:03:41 +000067 NewSt.St = NewStore;
68 return getPersistentState(NewSt);
69}
70
Ted Kremenek4adc81e2008-08-13 04:27:00 +000071const GRState* GRStateManager::getInitialState() {
Ted Kremenek5a7b3822008-02-26 23:37:01 +000072
Ted Kremenekcaa37242008-08-19 16:51:45 +000073 GRState StateImpl(EnvMgr.getInitialEnvironment(),
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000074 StoreMgr->getInitialStore(),
Ted Kremenekffdbefd2008-08-17 03:10:22 +000075 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +000076
Ted Kremenek9153f732008-02-05 07:17:49 +000077 return getPersistentState(StateImpl);
78}
79
Ted Kremenek4adc81e2008-08-13 04:27:00 +000080const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +000081
82 llvm::FoldingSetNodeID ID;
83 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +000084 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +000085
Ted Kremenek4adc81e2008-08-13 04:27:00 +000086 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +000087 return I;
88
Ted Kremenek4adc81e2008-08-13 04:27:00 +000089 GRState* I = (GRState*) Alloc.Allocate<GRState>();
90 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +000091 StateSet.InsertNode(I, InsertPos);
92 return I;
93}
Ted Kremeneke7d22112008-02-11 19:21:59 +000094
Zhongxing Xu4193eca2008-12-20 06:32:12 +000095const GRState* GRStateManager::MakeStateWithStore(const GRState* St,
96 Store store) {
97 GRState NewSt = *St;
98 NewSt.St = store;
99 return getPersistentState(NewSt);
100}
101
Ted Kremenek59894f92008-03-04 18:30:35 +0000102
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000103//===----------------------------------------------------------------------===//
104// State pretty-printing.
105//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000106
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000107void GRState::print(std::ostream& Out, StoreManager& StoreMgr,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000108 ConstraintManager& ConstraintMgr,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000109 Printer** Beg, Printer** End,
Ted Kremenekae6814e2008-08-13 21:24:49 +0000110 const char* nl, const char* sep) const {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000111
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000112 // Print the store.
113 StoreMgr.print(getStore(), Out, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000114
115 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000116 bool isFirst = true;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000117
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000118 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000119
120 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000121 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000122 isFirst = false;
123 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000124 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000125
126 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000127 llvm::raw_os_ostream OutS(Out);
128 I.getKey()->printPretty(OutS);
129 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000130 Out << " : ";
131 I.getData().print(Out);
132 }
133
134 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000135 isFirst = true;
136
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000137 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000138
139 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000140 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000141 isFirst = false;
142 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000143 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000144
145 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000146 llvm::raw_os_ostream OutS(Out);
147 I.getKey()->printPretty(OutS);
148 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000149 Out << " : ";
150 I.getData().print(Out);
151 }
152
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000153 ConstraintMgr.print(this, Out, nl, sep);
Ted Kremenek461f9772008-03-11 18:57:24 +0000154
Ted Kremenekae6814e2008-08-13 21:24:49 +0000155 // Print checker-specific data.
156 for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000157}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000158
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000159void GRStateRef::printDOT(std::ostream& Out) const {
160 print(Out, "\\l", "\\|");
161}
162
163void GRStateRef::printStdErr() const {
164 print(*llvm::cerr);
165}
166
167void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{
168 GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0];
169 GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000170 St->print(Out, *Mgr->StoreMgr, *Mgr->ConstraintMgr, beg, end, nl, sep);
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000171}
172
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000173//===----------------------------------------------------------------------===//
174// Generic Data Map.
175//===----------------------------------------------------------------------===//
176
177void* const* GRState::FindGDM(void* K) const {
178 return GDM.lookup(K);
179}
180
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000181void*
182GRStateManager::FindGDMContext(void* K,
183 void* (*CreateContext)(llvm::BumpPtrAllocator&),
184 void (*DeleteContext)(void*)) {
185
186 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
187 if (!p.first) {
188 p.first = CreateContext(Alloc);
189 p.second = DeleteContext;
190 }
191
192 return p.first;
193}
194
Zhongxing Xu4230da62008-11-03 05:18:34 +0000195const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000196 GRState::GenericDataMap M1 = St->getGDM();
197 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
198
199 if (M1 == M2)
200 return St;
201
202 GRState NewSt = *St;
203 NewSt.GDM = M2;
204 return getPersistentState(NewSt);
205}
Ted Kremenek584def72008-07-22 00:46:16 +0000206
207//===----------------------------------------------------------------------===//
Ted Kremenek5216ad72009-02-14 03:16:10 +0000208// Utility.
209//===----------------------------------------------------------------------===//
210
Ted Kremenek5dc27462009-03-03 02:51:43 +0000211namespace {
Zhongxing Xu63d1d602009-03-04 06:33:38 +0000212class VISIBILITY_HIDDEN ScanReachableSymbols : public SubRegionMap::Visitor {
Ted Kremenek5dc27462009-03-03 02:51:43 +0000213 typedef llvm::DenseSet<const MemRegion*> VisitedRegionsTy;
214
215 VisitedRegionsTy visited;
216 GRStateRef state;
217 SymbolVisitor &visitor;
218 llvm::OwningPtr<SubRegionMap> SRM;
219public:
220
221 ScanReachableSymbols(GRStateManager* sm, const GRState *st, SymbolVisitor& v)
222 : state(st, *sm), visitor(v) {}
223
224 bool scan(nonloc::CompoundVal val);
225 bool scan(SVal val);
226 bool scan(const MemRegion *R);
227
228 // From SubRegionMap::Visitor.
229 bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) {
230 return scan(SubRegion);
231 }
232};
233}
234
235bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
Ted Kremenek5216ad72009-02-14 03:16:10 +0000236 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
Ted Kremenek5dc27462009-03-03 02:51:43 +0000237 if (!scan(*I))
238 return false;
Ted Kremenek5216ad72009-02-14 03:16:10 +0000239
240 return true;
241}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000242
243bool ScanReachableSymbols::scan(SVal val) {
244 if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
245 return scan(X->getRegion());
Ted Kremenek380022d2009-03-30 18:45:36 +0000246
247 if (SymbolRef Sym = val.getAsSymbol())
248 return visitor.VisitSymbol(Sym);
Ted Kremenek5216ad72009-02-14 03:16:10 +0000249
250 if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000251 return scan(*X);
Ted Kremenek5216ad72009-02-14 03:16:10 +0000252
253 return true;
254}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000255
256bool ScanReachableSymbols::scan(const MemRegion *R) {
Ted Kremenek1cb151e2009-03-04 00:13:10 +0000257 if (isa<MemSpaceRegion>(R) || visited.count(R))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000258 return true;
259
260 visited.insert(R);
261
262 // If this is a symbolic region, visit the symbol for the region.
263 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
264 if (!visitor.VisitSymbol(SR->getSymbol()))
265 return false;
266
267 // If this is a subregion, also visit the parent regions.
268 if (const SubRegion *SR = dyn_cast<SubRegion>(R))
Ted Kremenek6076e0a2009-03-03 18:15:30 +0000269 if (!scan(SR->getSuperRegion()))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000270 return false;
271
272 // Now look at the binding to this region (if any).
Ted Kremenek1cb151e2009-03-04 00:13:10 +0000273 if (!scan(state.GetSValAsScalarOrLoc(R)))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000274 return false;
275
276 // Now look at the subregions.
277 if (!SRM.get())
Ted Kremenek14453bf2009-03-03 19:02:42 +0000278 SRM.reset(state.getManager().getStoreManager().getSubRegionMap(state));
Ted Kremenek5dc27462009-03-03 02:51:43 +0000279
280 return SRM->iterSubRegions(R, *this);
281}
282
283bool GRStateManager::scanReachableSymbols(SVal val, const GRState* state,
284 SymbolVisitor& visitor) {
285 ScanReachableSymbols S(this, state, visitor);
286 return S.scan(val);
287}
Ted Kremenek5216ad72009-02-14 03:16:10 +0000288
289//===----------------------------------------------------------------------===//
Ted Kremenek584def72008-07-22 00:46:16 +0000290// Queries.
291//===----------------------------------------------------------------------===//
292
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000293bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000294 const llvm::APSInt& Y) {
295
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000296 SVal V = GetSVal(state, Ex);
Ted Kremenek584def72008-07-22 00:46:16 +0000297
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000298 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000299 return X->getValue() == Y;
300
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000301 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000302 return X->getValue() == Y;
303
Ted Kremenek380022d2009-03-30 18:45:36 +0000304 if (SymbolRef Sym = V.getAsSymbol())
305 return ConstraintMgr->isEqual(state, Sym, Y);
306
Ted Kremenek584def72008-07-22 00:46:16 +0000307 return false;
308}
309
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000310bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenek044b6f02009-04-09 16:13:17 +0000311 return isEqual(state, Ex, getBasicVals().getValue(x, Ex->getType()));
Ted Kremenek584def72008-07-22 00:46:16 +0000312}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000313
314//===----------------------------------------------------------------------===//
315// Persistent values for indexing into the Generic Data Map.
316
317int GRState::NullDerefTag::TagInt = 0;
318