blob: 65db6a29c83d03ea5855c0e84e4d6964c1f4ec29 [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
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 Kremenek4adc81e2008-08-13 04:27:00 +000087const GRState* GRStateManager::getInitialState() {
Ted Kremenek67f28532009-06-17 22:02:04 +000088 GRState StateImpl(this, EnvMgr.getInitialEnvironment(),
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000089 StoreMgr->getInitialStore(),
Ted Kremenekffdbefd2008-08-17 03:10:22 +000090 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +000091
Ted Kremenek9153f732008-02-05 07:17:49 +000092 return getPersistentState(StateImpl);
93}
94
Ted Kremenek4adc81e2008-08-13 04:27:00 +000095const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +000096
97 llvm::FoldingSetNodeID ID;
98 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +000099 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +0000100
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000101 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000102 return I;
103
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000104 GRState* I = (GRState*) Alloc.Allocate<GRState>();
105 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000106 StateSet.InsertNode(I, InsertPos);
107 return I;
108}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000109
Ted Kremenek67f28532009-06-17 22:02:04 +0000110const GRState* GRState::makeWithStore(Store store) const {
111 GRState NewSt = *this;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000112 NewSt.St = store;
Ted Kremenek67f28532009-06-17 22:02:04 +0000113 return Mgr->getPersistentState(NewSt);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000114}
115
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000116//===----------------------------------------------------------------------===//
117// State pretty-printing.
118//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000119
Ted Kremenekb65be702009-06-18 01:23:53 +0000120void GRState::print(std::ostream& Out, const char* nl, const char* sep) const {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000121 // Print the store.
Ted Kremenekb65be702009-06-18 01:23:53 +0000122 Mgr->getStoreManager().print(getStore(), Out, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000123
124 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000125 bool isFirst = true;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000126
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000127 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000128
129 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000130 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000131 isFirst = false;
132 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000133 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000134
135 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000136 llvm::raw_os_ostream OutS(Out);
137 I.getKey()->printPretty(OutS);
138 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000139 Out << " : ";
140 I.getData().print(Out);
141 }
142
143 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000144 isFirst = true;
145
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000146 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000147
148 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000149 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000150 isFirst = false;
151 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000152 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000153
154 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000155 llvm::raw_os_ostream OutS(Out);
156 I.getKey()->printPretty(OutS);
157 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000158 Out << " : ";
159 I.getData().print(Out);
160 }
161
Ted Kremenekb65be702009-06-18 01:23:53 +0000162 Mgr->getConstraintManager().print(this, Out, nl, sep);
Ted Kremenek461f9772008-03-11 18:57:24 +0000163
Ted Kremenekb65be702009-06-18 01:23:53 +0000164 // Print checker-specific data.
165 for (std::vector<Printer*>::iterator I = Mgr->Printers.begin(),
166 E = Mgr->Printers.end(); I != E; ++I) {
167 (*I)->Print(Out, this, nl, sep);
168 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000169}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000170
Ted Kremenekb65be702009-06-18 01:23:53 +0000171void GRState::printDOT(std::ostream& Out) const {
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000172 print(Out, "\\l", "\\|");
173}
174
Ted Kremenekb65be702009-06-18 01:23:53 +0000175void GRState::printStdErr() const {
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000176 print(*llvm::cerr);
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000177}
178
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000179//===----------------------------------------------------------------------===//
180// Generic Data Map.
181//===----------------------------------------------------------------------===//
182
183void* const* GRState::FindGDM(void* K) const {
184 return GDM.lookup(K);
185}
186
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000187void*
188GRStateManager::FindGDMContext(void* K,
189 void* (*CreateContext)(llvm::BumpPtrAllocator&),
190 void (*DeleteContext)(void*)) {
191
192 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
193 if (!p.first) {
194 p.first = CreateContext(Alloc);
195 p.second = DeleteContext;
196 }
197
198 return p.first;
199}
200
Zhongxing Xu4230da62008-11-03 05:18:34 +0000201const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000202 GRState::GenericDataMap M1 = St->getGDM();
203 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
204
205 if (M1 == M2)
206 return St;
207
208 GRState NewSt = *St;
209 NewSt.GDM = M2;
210 return getPersistentState(NewSt);
211}
Ted Kremenek584def72008-07-22 00:46:16 +0000212
213//===----------------------------------------------------------------------===//
Ted Kremenek5216ad72009-02-14 03:16:10 +0000214// Utility.
215//===----------------------------------------------------------------------===//
216
Ted Kremenek5dc27462009-03-03 02:51:43 +0000217namespace {
Zhongxing Xu63d1d602009-03-04 06:33:38 +0000218class VISIBILITY_HIDDEN ScanReachableSymbols : public SubRegionMap::Visitor {
Ted Kremenek5dc27462009-03-03 02:51:43 +0000219 typedef llvm::DenseSet<const MemRegion*> VisitedRegionsTy;
220
221 VisitedRegionsTy visited;
Ted Kremenek47fed902009-06-18 01:33:24 +0000222 const GRState *state;
Ted Kremenek5dc27462009-03-03 02:51:43 +0000223 SymbolVisitor &visitor;
224 llvm::OwningPtr<SubRegionMap> SRM;
225public:
226
Ted Kremenek47fed902009-06-18 01:33:24 +0000227 ScanReachableSymbols(const GRState *st, SymbolVisitor& v)
228 : state(st), visitor(v) {}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000229
230 bool scan(nonloc::CompoundVal val);
231 bool scan(SVal val);
232 bool scan(const MemRegion *R);
233
234 // From SubRegionMap::Visitor.
235 bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) {
236 return scan(SubRegion);
237 }
238};
239}
240
241bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
Ted Kremenek5216ad72009-02-14 03:16:10 +0000242 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
Ted Kremenek5dc27462009-03-03 02:51:43 +0000243 if (!scan(*I))
244 return false;
Ted Kremenek5216ad72009-02-14 03:16:10 +0000245
246 return true;
247}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000248
249bool ScanReachableSymbols::scan(SVal val) {
250 if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
251 return scan(X->getRegion());
Ted Kremenek380022d2009-03-30 18:45:36 +0000252
253 if (SymbolRef Sym = val.getAsSymbol())
254 return visitor.VisitSymbol(Sym);
Ted Kremenek5216ad72009-02-14 03:16:10 +0000255
256 if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000257 return scan(*X);
Ted Kremenek5216ad72009-02-14 03:16:10 +0000258
259 return true;
260}
Ted Kremenek5dc27462009-03-03 02:51:43 +0000261
262bool ScanReachableSymbols::scan(const MemRegion *R) {
Ted Kremenek1cb151e2009-03-04 00:13:10 +0000263 if (isa<MemSpaceRegion>(R) || visited.count(R))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000264 return true;
265
266 visited.insert(R);
267
268 // If this is a symbolic region, visit the symbol for the region.
269 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
270 if (!visitor.VisitSymbol(SR->getSymbol()))
271 return false;
272
273 // If this is a subregion, also visit the parent regions.
274 if (const SubRegion *SR = dyn_cast<SubRegion>(R))
Ted Kremenek6076e0a2009-03-03 18:15:30 +0000275 if (!scan(SR->getSuperRegion()))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000276 return false;
277
278 // Now look at the binding to this region (if any).
Ted Kremenek47fed902009-06-18 01:33:24 +0000279 if (!scan(state->getSValAsScalarOrLoc(R)))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000280 return false;
281
282 // Now look at the subregions.
283 if (!SRM.get())
Ted Kremenek47fed902009-06-18 01:33:24 +0000284 SRM.reset(state->getStateManager().getStoreManager().getSubRegionMap(state));
Ted Kremenek5dc27462009-03-03 02:51:43 +0000285
286 return SRM->iterSubRegions(R, *this);
287}
288
Ted Kremenek47fed902009-06-18 01:33:24 +0000289bool GRState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
290 ScanReachableSymbols S(this, visitor);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000291 return S.scan(val);
292}
Ted Kremenek5216ad72009-02-14 03:16:10 +0000293
294//===----------------------------------------------------------------------===//
Ted Kremenek584def72008-07-22 00:46:16 +0000295// Queries.
296//===----------------------------------------------------------------------===//
297
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000298bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000299 const llvm::APSInt& Y) {
300
Ted Kremenekdbc2afc2009-06-23 17:55:07 +0000301 SVal V = state->getSVal(Ex);
Ted Kremenek584def72008-07-22 00:46:16 +0000302
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000303 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000304 return X->getValue() == Y;
305
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000306 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000307 return X->getValue() == Y;
308
Ted Kremenek380022d2009-03-30 18:45:36 +0000309 if (SymbolRef Sym = V.getAsSymbol())
310 return ConstraintMgr->isEqual(state, Sym, Y);
311
Ted Kremenek584def72008-07-22 00:46:16 +0000312 return false;
313}
314
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000315bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenek044b6f02009-04-09 16:13:17 +0000316 return isEqual(state, Ex, getBasicVals().getValue(x, Ex->getType()));
Ted Kremenek584def72008-07-22 00:46:16 +0000317}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000318
319//===----------------------------------------------------------------------===//
320// Persistent values for indexing into the Generic Data Map.
321
322int GRState::NullDerefTag::TagInt = 0;
323