blob: 2ed89c6f7d4c84df72b97417cfbc88b1620f507f [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 Kremenekf59bf482008-07-17 18:38:48 +000014#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000015#include "clang/Analysis/PathSensitive/BasicStore.h"
Ted Kremenekcaa37242008-08-19 16:51:45 +000016#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000017#include "llvm/ADT/ImmutableMap.h"
18#include "llvm/Support/Compiler.h"
Ted Kremeneka622d8c2008-08-19 22:24:03 +000019#include "llvm/Support/Streams.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000020
21using namespace clang;
22
23namespace {
24
25class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
26 typedef llvm::ImmutableMap<VarDecl*,RVal> VarBindingsTy;
27 VarBindingsTy::Factory VBFactory;
28
29public:
30 BasicStoreManager(llvm::BumpPtrAllocator& A) : VBFactory(A) {}
31 virtual ~BasicStoreManager() {}
32
33 virtual RVal GetRVal(Store St, LVal LV, QualType T);
34 virtual Store SetRVal(Store St, LVal LV, RVal V);
35 virtual Store Remove(Store St, LVal LV);
36
Ted Kremenekcaa37242008-08-19 16:51:45 +000037 virtual Store getInitialStore(GRStateManager& StateMgr);
Ted Kremenekf59bf482008-07-17 18:38:48 +000038
39 virtual Store RemoveDeadBindings(Store store, Stmt* Loc,
40 const LiveVariables& Live,
41 DeclRootsTy& DRoots, LiveSymbolsTy& LSymbols,
42 DeadSymbolsTy& DSymbols);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000043
Ted Kremeneke53c0692008-08-23 00:50:55 +000044 virtual Store AddDecl(Store store, GRStateManager& StateMgr,
45 const VarDecl* VD, Expr* Ex,
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000046 RVal InitVal = UndefinedVal(), unsigned Count = 0);
47
Ted Kremenekf59bf482008-07-17 18:38:48 +000048 static inline VarBindingsTy GetVarBindings(Store store) {
49 return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +000050 }
51
52 virtual void print(Store store, std::ostream& Out,
53 const char* nl, const char *sep);
Ted Kremenek4323a572008-07-10 22:03:41 +000054};
55
56} // end anonymous namespace
57
58
59StoreManager* clang::CreateBasicStoreManager(llvm::BumpPtrAllocator& A) {
60 return new BasicStoreManager(A);
61}
62
63RVal BasicStoreManager::GetRVal(Store St, LVal LV, QualType T) {
64
65 if (isa<UnknownVal>(LV))
66 return UnknownVal();
67
68 assert (!isa<UndefinedVal>(LV));
69
70 switch (LV.getSubKind()) {
71
72 case lval::DeclValKind: {
73 VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St));
74 VarBindingsTy::data_type* T = B.lookup(cast<lval::DeclVal>(LV).getDecl());
75 return T ? *T : UnknownVal();
76 }
77
78 case lval::SymbolValKind: {
79
80 // FIXME: This is a broken representation of memory, and is prone
81 // to crashing the analyzer when addresses to symbolic values are
82 // passed through casts. We need a better representation of symbolic
83 // memory (or just memory in general); probably we should do this
84 // as a plugin class (similar to GRTransferFuncs).
85
86#if 0
87 const lval::SymbolVal& SV = cast<lval::SymbolVal>(LV);
88 assert (T.getTypePtr());
89
90 // Punt on "symbolic" function pointers.
91 if (T->isFunctionType())
92 return UnknownVal();
93
94 if (T->isPointerType())
95 return lval::SymbolVal(SymMgr.getContentsOfSymbol(SV.getSymbol()));
96 else
97 return nonlval::SymbolVal(SymMgr.getContentsOfSymbol(SV.getSymbol()));
98#endif
99
100 return UnknownVal();
101 }
102
103 case lval::ConcreteIntKind:
104 // Some clients may call GetRVal with such an option simply because
105 // they are doing a quick scan through their LVals (potentially to
106 // invalidate their bindings). Just return Undefined.
107 return UndefinedVal();
108
109 case lval::ArrayOffsetKind:
110 case lval::FieldOffsetKind:
111 return UnknownVal();
112
113 case lval::FuncValKind:
114 return LV;
115
116 case lval::StringLiteralValKind:
117 // FIXME: Implement better support for fetching characters from strings.
118 return UnknownVal();
119
120 default:
121 assert (false && "Invalid LVal.");
122 break;
123 }
124
125 return UnknownVal();
126}
127
Ted Kremenekf59bf482008-07-17 18:38:48 +0000128Store BasicStoreManager::SetRVal(Store store, LVal LV, RVal V) {
129 switch (LV.getSubKind()) {
130 case lval::DeclValKind: {
131 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000132 return V.isUnknown()
133 ? VBFactory.Remove(B,cast<lval::DeclVal>(LV).getDecl()).getRoot()
134 : VBFactory.Add(B, cast<lval::DeclVal>(LV).getDecl(), V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000135 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000136 default:
137 assert ("SetRVal for given LVal type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000138 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000139 }
140}
141
Ted Kremenekf59bf482008-07-17 18:38:48 +0000142Store BasicStoreManager::Remove(Store store, LVal LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000143 switch (LV.getSubKind()) {
Ted Kremenekf59bf482008-07-17 18:38:48 +0000144 case lval::DeclValKind: {
145 VarBindingsTy B = GetVarBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000146 return VBFactory.Remove(B,cast<lval::DeclVal>(LV).getDecl()).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000147 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000148 default:
149 assert ("Remove for given LVal type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000150 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000151 }
152}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000153
154Store BasicStoreManager::RemoveDeadBindings(Store store,
155 Stmt* Loc,
156 const LiveVariables& Liveness,
157 DeclRootsTy& DRoots,
158 LiveSymbolsTy& LSymbols,
159 DeadSymbolsTy& DSymbols) {
160
161 VarBindingsTy B = GetVarBindings(store);
162 typedef RVal::symbol_iterator symbol_iterator;
163
164 // Iterate over the variable bindings.
165 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
166 if (Liveness.isLive(Loc, I.getKey())) {
167 DRoots.push_back(I.getKey());
168 RVal X = I.getData();
169
170 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
171 LSymbols.insert(*SI);
172 }
173
174 // Scan for live variables and live symbols.
175 llvm::SmallPtrSet<ValueDecl*, 10> Marked;
176
177 while (!DRoots.empty()) {
178 ValueDecl* V = DRoots.back();
179 DRoots.pop_back();
180
181 if (Marked.count(V))
182 continue;
183
184 Marked.insert(V);
185
186 RVal X = GetRVal(store, lval::DeclVal(cast<VarDecl>(V)), QualType());
187
188 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
189 LSymbols.insert(*SI);
190
191 if (!isa<lval::DeclVal>(X))
192 continue;
193
194 const lval::DeclVal& LVD = cast<lval::DeclVal>(X);
195 DRoots.push_back(LVD.getDecl());
196 }
197
198 // Remove dead variable bindings.
199 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I)
200 if (!Marked.count(I.getKey())) {
201 store = Remove(store, lval::DeclVal(I.getKey()));
202 RVal X = I.getData();
203
204 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
205 if (!LSymbols.count(*SI)) DSymbols.insert(*SI);
206 }
207
208 return store;
209}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000210
211Store BasicStoreManager::getInitialStore(GRStateManager& StateMgr) {
212 // The LiveVariables information already has a compilation of all VarDecls
213 // used in the function. Iterate through this set, and "symbolicate"
214 // any VarDecl whose value originally comes from outside the function.
215
216 typedef LiveVariables::AnalysisDataTy LVDataTy;
217 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
218
219 Store St = VBFactory.GetEmptyMap().getRoot();
220
221 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
222 ScopedDecl* SD = const_cast<ScopedDecl*>(I->first);
223
224 if (VarDecl* VD = dyn_cast<VarDecl>(SD)) {
225 // Punt on static variables for now.
226 if (VD->getStorageClass() == VarDecl::Static)
227 continue;
228
229 // Only handle pointers and integers for now.
230 QualType T = VD->getType();
231 if (LVal::IsLValType(T) || T->isIntegerType()) {
232 // Initialize globals and parameters to symbolic values.
233 // Initialize local variables to undefined.
234 RVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
235 isa<ImplicitParamDecl>(VD))
236 ? RVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
237 : UndefinedVal();
238
239 St = SetRVal(St, lval::DeclVal(VD), X);
240 }
241 }
242 }
243 return St;
244}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000245
Ted Kremeneke53c0692008-08-23 00:50:55 +0000246Store BasicStoreManager::AddDecl(Store store, GRStateManager& StateMgr,
247 const VarDecl* VD, Expr* Ex,
248 RVal InitVal, unsigned Count) {
249
250 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
251 SymbolManager& SymMgr = StateMgr.getSymbolManager();
252
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000253 // BasicStore does not model arrays and structs.
254 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
255 return store;
256
257 if (VD->hasGlobalStorage()) {
258 // Handle variables with global storage: extern, static, PrivateExtern.
259
260 // FIXME:: static variables may have an initializer, but the second time a
261 // function is called those values may not be current. Currently, a function
262 // will not be called more than once.
263
264 // Static global variables should not be visited here.
265 assert(!(VD->getStorageClass() == VarDecl::Static &&
266 VD->isFileVarDecl()));
267
268 // Process static variables.
269 if (VD->getStorageClass() == VarDecl::Static) {
270 // C99: 6.7.8 Initialization
271 // If an object that has static storage duration is not initialized
272 // explicitly, then:
273 // —if it has pointer type, it is initialized to a null pointer;
274 // —if it has arithmetic type, it is initialized to (positive or
275 // unsigned) zero;
276 if (!Ex) {
277 QualType T = VD->getType();
278 if (LVal::IsLValType(T))
279 store = SetRVal(store, lval::DeclVal(VD),
280 lval::ConcreteInt(BasicVals.getValue(0, T)));
281 else if (T->isIntegerType())
282 store = SetRVal(store, lval::DeclVal(VD),
283 nonlval::ConcreteInt(BasicVals.getValue(0, T)));
284 else {
285 // assert(0 && "ignore other types of variables");
286 }
287 } else {
288 store = SetRVal(store, lval::DeclVal(VD), InitVal);
289 }
290 }
291 } else {
292 // Process local scalar variables.
293 QualType T = VD->getType();
294 if (LVal::IsLValType(T) || T->isIntegerType()) {
295 RVal V = Ex ? InitVal : UndefinedVal();
296
297 if (Ex && InitVal.isUnknown()) {
298 // EXPERIMENTAL: "Conjured" symbols.
299 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
300
301 V = LVal::IsLValType(Ex->getType())
302 ? cast<RVal>(lval::SymbolVal(Sym))
303 : cast<RVal>(nonlval::SymbolVal(Sym));
304 }
305
306 store = SetRVal(store, lval::DeclVal(VD), V);
307 }
308 }
309
310 return store;
311}
312
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000313void BasicStoreManager::print(Store store, std::ostream& Out,
314 const char* nl, const char *sep) {
315
316 VarBindingsTy B = GetVarBindings(store);
317 Out << "Variables:" << nl;
318
319 bool isFirst = true;
320
321 for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
322 if (isFirst) isFirst = false;
323 else Out << nl;
324
325 Out << ' ' << I.getKey()->getName() << " : ";
326 I.getData().print(Out);
327 }
328}