blob: 9d8a9acda5099965affe98390b5d678657b33d85 [file] [log] [blame]
Ted Kremenek4323a572008-07-10 22:03:41 +00001//== BasicStore.cpp - Basic map from Locations to Values --------*- C++ -*--==//
2//
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//
10// This file defined the BasicStore and BasicStoreManager classes.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek5f81c442008-08-28 23:31:31 +000014#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekcaa37242008-08-19 16:51:45 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000016#include "llvm/ADT/ImmutableMap.h"
17#include "llvm/Support/Compiler.h"
Ted Kremeneka622d8c2008-08-19 22:24:03 +000018#include "llvm/Support/Streams.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000019
20using namespace clang;
21
Ted Kremenek9e240492008-10-04 05:50:14 +000022typedef llvm::ImmutableMap<const VarDecl*,RVal> VarBindingsTy;
Ted Kremenek60dbad82008-09-03 03:06:11 +000023
Ted Kremenek4323a572008-07-10 22:03:41 +000024namespace {
25
26class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek4323a572008-07-10 22:03:41 +000027 VarBindingsTy::Factory VBFactory;
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +000028 GRStateManager& StateMgr;
Zhongxing Xubc678fd2008-10-07 01:31:04 +000029 MemRegionManager MRMgr;
Ted Kremenek4323a572008-07-10 22:03:41 +000030
31public:
Zhongxing Xubc678fd2008-10-07 01:31:04 +000032 BasicStoreManager(GRStateManager& mgr)
33 : StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000034
Ted Kremenek4323a572008-07-10 22:03:41 +000035 virtual ~BasicStoreManager() {}
36
37 virtual RVal GetRVal(Store St, LVal LV, QualType T);
38 virtual Store SetRVal(Store St, LVal LV, RVal V);
39 virtual Store Remove(Store St, LVal LV);
40
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +000041 virtual Store getInitialStore();
Zhongxing Xubc678fd2008-10-07 01:31:04 +000042
43 virtual MemRegionManager& getRegionManager() { return MRMgr; }
44
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000045 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xubc678fd2008-10-07 01:31:04 +000046 virtual LVal getLVal(const VarDecl* VD) {
47 return lval::MemRegionVal(MRMgr.getVarRegion(VD));
48 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000049
50 RVal getLValueVar(const GRState* St, const VarDecl* VD);
51 RVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, RVal Base);
52 RVal getLValueField(const GRState* St, const FieldDecl* D, RVal Base);
53 RVal getLValueElement(const GRState* St, RVal Base, RVal Offset);
Ted Kremenekf59bf482008-07-17 18:38:48 +000054
Ted Kremenek9e240492008-10-04 05:50:14 +000055 virtual Store
56 RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
57 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
58 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000059
Ted Kremenek60dbad82008-09-03 03:06:11 +000060 virtual void iterBindings(Store store, BindingsHandler& f);
61
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +000062 virtual Store AddDecl(Store store,
Ted Kremeneke53c0692008-08-23 00:50:55 +000063 const VarDecl* VD, Expr* Ex,
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000064 RVal InitVal = UndefinedVal(), unsigned Count = 0);
65
Ted Kremenekf59bf482008-07-17 18:38:48 +000066 static inline VarBindingsTy GetVarBindings(Store store) {
67 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +000068 }
69
70 virtual void print(Store store, std::ostream& Out,
71 const char* nl, const char *sep);
Zhongxing Xubc678fd2008-10-07 01:31:04 +000072
Ted Kremenek60dbad82008-09-03 03:06:11 +000073};
Ted Kremenek9e240492008-10-04 05:50:14 +000074
Ted Kremenek4323a572008-07-10 22:03:41 +000075} // end anonymous namespace
76
77
Ted Kremenek5f81c442008-08-28 23:31:31 +000078StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
79 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +000080}
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000081RVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000082 return lval::MemRegionVal(MRMgr.getVarRegion(VD));
83}
84
85RVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
86 RVal Base) {
87 return UnknownVal();
88}
89
90
91RVal BasicStoreManager::getLValueField(const GRState* St, const FieldDecl* D,
92 RVal Base) {
93 return UnknownVal();
94}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000095
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000096RVal BasicStoreManager::getLValueElement(const GRState* St, RVal Base,
97 RVal Offset) {
98 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000099}
100
Ted Kremenek4323a572008-07-10 22:03:41 +0000101RVal BasicStoreManager::GetRVal(Store St, LVal LV, QualType T) {
102
103 if (isa<UnknownVal>(LV))
104 return UnknownVal();
105
106 assert (!isa<UndefinedVal>(LV));
107
108 switch (LV.getSubKind()) {
109
Ted Kremenek9e240492008-10-04 05:50:14 +0000110 case lval::MemRegionKind: {
111 VarRegion* R =
112 dyn_cast<VarRegion>(cast<lval::MemRegionVal>(LV).getRegion());
113
114 if (!R)
115 return UnknownVal();
116
Ted Kremenek4323a572008-07-10 22:03:41 +0000117 VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St));
Ted Kremenek9e240492008-10-04 05:50:14 +0000118 VarBindingsTy::data_type* T = B.lookup(R->getDecl());
Ted Kremenek4323a572008-07-10 22:03:41 +0000119 return T ? *T : UnknownVal();
120 }
121
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000122 case lval::SymbolValKind:
Ted Kremenek4323a572008-07-10 22:03:41 +0000123 return UnknownVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000124
125 case lval::ConcreteIntKind:
126 // Some clients may call GetRVal with such an option simply because
127 // they are doing a quick scan through their LVals (potentially to
128 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000129 return UndefinedVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000130 case lval::FuncValKind:
131 return LV;
132
133 case lval::StringLiteralValKind:
134 // FIXME: Implement better support for fetching characters from strings.
135 return UnknownVal();
136
137 default:
138 assert (false && "Invalid LVal.");
139 break;
140 }
141
142 return UnknownVal();
143}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000144
Ted Kremenekf59bf482008-07-17 18:38:48 +0000145Store BasicStoreManager::SetRVal(Store store, LVal LV, RVal V) {
146 switch (LV.getSubKind()) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000147 case lval::MemRegionKind: {
148 VarRegion* R =
149 dyn_cast<VarRegion>(cast<lval::MemRegionVal>(LV).getRegion());
150
151 if (!R)
152 return store;
153
Ted Kremenekf59bf482008-07-17 18:38:48 +0000154 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000155 return V.isUnknown()
Ted Kremenek9e240492008-10-04 05:50:14 +0000156 ? VBFactory.Remove(B, R->getDecl()).getRoot()
157 : VBFactory.Add(B, R->getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000158 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000159 default:
160 assert ("SetRVal for given LVal type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000161 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000162 }
163}
164
Ted Kremenekf59bf482008-07-17 18:38:48 +0000165Store BasicStoreManager::Remove(Store store, LVal LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000166 switch (LV.getSubKind()) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000167 case lval::MemRegionKind: {
168 VarRegion* R =
169 dyn_cast<VarRegion>(cast<lval::MemRegionVal>(LV).getRegion());
170
171 if (!R)
172 return store;
173
Ted Kremenekf59bf482008-07-17 18:38:48 +0000174 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek9e240492008-10-04 05:50:14 +0000175 return VBFactory.Remove(B,R->getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000176 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000177 default:
178 assert ("Remove for given LVal type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000179 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000180 }
181}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000182
Ted Kremenek9e240492008-10-04 05:50:14 +0000183Store
184BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
185 const LiveVariables& Liveness,
186 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
187 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000188
189 VarBindingsTy B = GetVarBindings(store);
190 typedef RVal::symbol_iterator symbol_iterator;
191
192 // Iterate over the variable bindings.
193 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
194 if (Liveness.isLive(Loc, I.getKey())) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000195 RegionRoots.push_back(MRMgr.getVarRegion(I.getKey()));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000196 RVal X = I.getData();
197
198 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
199 LSymbols.insert(*SI);
200 }
201
202 // Scan for live variables and live symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +0000203 llvm::SmallPtrSet<const VarRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000204
Ted Kremenek9e240492008-10-04 05:50:14 +0000205 while (!RegionRoots.empty()) {
206 const VarRegion* R = cast<VarRegion>(RegionRoots.back());
207 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000208
Ted Kremenek9e240492008-10-04 05:50:14 +0000209 if (Marked.count(R))
Ted Kremenekf59bf482008-07-17 18:38:48 +0000210 continue;
211
Ted Kremenek9e240492008-10-04 05:50:14 +0000212 Marked.insert(R);
213 // FIXME: Do we need the QualType here, since regions are partially
214 // typed?
215 RVal X = GetRVal(store, lval::MemRegionVal(R), QualType());
Ted Kremenekf59bf482008-07-17 18:38:48 +0000216
217 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
218 LSymbols.insert(*SI);
219
Ted Kremenek9e240492008-10-04 05:50:14 +0000220 if (!isa<lval::MemRegionVal>(X))
Ted Kremenekf59bf482008-07-17 18:38:48 +0000221 continue;
222
Ted Kremenek9e240492008-10-04 05:50:14 +0000223 const lval::MemRegionVal& LVD = cast<lval::MemRegionVal>(X);
224 RegionRoots.push_back(cast<VarRegion>(LVD.getRegion()));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000225 }
226
227 // Remove dead variable bindings.
Ted Kremenek9e240492008-10-04 05:50:14 +0000228 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000229 const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey()));
Ted Kremenek9e240492008-10-04 05:50:14 +0000230
231 if (!Marked.count(R)) {
232 store = Remove(store, lval::MemRegionVal(R));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000233 RVal X = I.getData();
234
235 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
236 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
237 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000238 }
239
Ted Kremenekf59bf482008-07-17 18:38:48 +0000240 return store;
241}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000242
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000243Store BasicStoreManager::getInitialStore() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000244 // The LiveVariables information already has a compilation of all VarDecls
245 // used in the function. Iterate through this set, and "symbolicate"
246 // any VarDecl whose value originally comes from outside the function.
247
248 typedef LiveVariables::AnalysisDataTy LVDataTy;
249 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
250
251 Store St = VBFactory.GetEmptyMap().getRoot();
252
253 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
254 ScopedDecl* SD = const_cast<ScopedDecl*>(I->first);
255
256 if (VarDecl* VD = dyn_cast<VarDecl>(SD)) {
257 // Punt on static variables for now.
258 if (VD->getStorageClass() == VarDecl::Static)
259 continue;
260
261 // Only handle pointers and integers for now.
262 QualType T = VD->getType();
263 if (LVal::IsLValType(T) || T->isIntegerType()) {
264 // Initialize globals and parameters to symbolic values.
265 // Initialize local variables to undefined.
266 RVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
267 isa<ImplicitParamDecl>(VD))
268 ? RVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
269 : UndefinedVal();
270
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000271 St = SetRVal(St, lval::MemRegionVal(MRMgr.getVarRegion(VD)), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000272 }
273 }
274 }
275 return St;
276}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000277
Zhongxing Xuc1d1bbf2008-10-05 12:12:48 +0000278Store BasicStoreManager::AddDecl(Store store,
Ted Kremeneke53c0692008-08-23 00:50:55 +0000279 const VarDecl* VD, Expr* Ex,
280 RVal InitVal, unsigned Count) {
281
282 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
283 SymbolManager& SymMgr = StateMgr.getSymbolManager();
284
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000285 // BasicStore does not model arrays and structs.
286 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
287 return store;
288
289 if (VD->hasGlobalStorage()) {
290 // Handle variables with global storage: extern, static, PrivateExtern.
291
292 // FIXME:: static variables may have an initializer, but the second time a
293 // function is called those values may not be current. Currently, a function
294 // will not be called more than once.
295
296 // Static global variables should not be visited here.
297 assert(!(VD->getStorageClass() == VarDecl::Static &&
298 VD->isFileVarDecl()));
299
300 // Process static variables.
301 if (VD->getStorageClass() == VarDecl::Static) {
302 // C99: 6.7.8 Initialization
303 // If an object that has static storage duration is not initialized
304 // explicitly, then:
305 // —if it has pointer type, it is initialized to a null pointer;
306 // —if it has arithmetic type, it is initialized to (positive or
307 // unsigned) zero;
308 if (!Ex) {
309 QualType T = VD->getType();
310 if (LVal::IsLValType(T))
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000311 store = SetRVal(store, getLVal(VD),
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000312 lval::ConcreteInt(BasicVals.getValue(0, T)));
313 else if (T->isIntegerType())
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000314 store = SetRVal(store, getLVal(VD),
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000315 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
316 else {
317 // assert(0 && "ignore other types of variables");
318 }
319 } else {
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000320 store = SetRVal(store, getLVal(VD), InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000321 }
322 }
323 } else {
324 // Process local scalar variables.
325 QualType T = VD->getType();
326 if (LVal::IsLValType(T) || T->isIntegerType()) {
327 RVal V = Ex ? InitVal : UndefinedVal();
328
329 if (Ex && InitVal.isUnknown()) {
330 // EXPERIMENTAL: "Conjured" symbols.
331 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
332
333 V = LVal::IsLValType(Ex->getType())
334 ? cast<RVal>(lval::SymbolVal(Sym))
335 : cast<RVal>(nonlval::SymbolVal(Sym));
336 }
337
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000338 store = SetRVal(store, getLVal(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000339 }
340 }
341
342 return store;
343}
344
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000345void BasicStoreManager::print(Store store, std::ostream& Out,
346 const char* nl, const char *sep) {
347
348 VarBindingsTy B = GetVarBindings(store);
349 Out << "Variables:" << nl;
350
351 bool isFirst = true;
352
353 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
354 if (isFirst) isFirst = false;
355 else Out << nl;
356
357 Out << ' ' << I.getKey()->getName() << " : ";
358 I.getData().print(Out);
359 }
360}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000361
Ted Kremenek60dbad82008-09-03 03:06:11 +0000362
363void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
364 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000365
Ted Kremenek60dbad82008-09-03 03:06:11 +0000366 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000367
Zhongxing Xubc678fd2008-10-07 01:31:04 +0000368 f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData());
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000369 }
370}
371
Ted Kremenek60dbad82008-09-03 03:06:11 +0000372StoreManager::BindingsHandler::~BindingsHandler() {}