blob: eabe462cc7b36c335146feee63f9a517b5c64f21 [file] [log] [blame]
Zhongxing Xu79c57f82008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- 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 defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
19#include "clang/Analysis/Analyses/LiveVariables.h"
20
21#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xuca892b82008-10-24 06:01:33 +000022#include "llvm/Support/raw_ostream.h"
Zhongxing Xu79c57f82008-10-08 02:50:44 +000023#include "llvm/Support/Compiler.h"
24
25using namespace clang;
26
Zhongxing Xu097fc982008-10-17 05:57:07 +000027typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xu79c57f82008-10-08 02:50:44 +000028
29namespace {
30
31class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
32 RegionBindingsTy::Factory RBFactory;
33 GRStateManager& StateMgr;
34 MemRegionManager MRMgr;
35
36public:
37 RegionStoreManager(GRStateManager& mgr)
38 : StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {}
39
40 virtual ~RegionStoreManager() {}
41
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000042 MemRegionManager& getRegionManager() { return MRMgr; }
43
44 // FIXME: Is this function necessary?
45 SVal GetRegionSVal(Store St, const MemRegion* R) {
46 return Retrieve(St, loc::MemRegionVal(R));
47 }
48
Zhongxing Xu2abba442008-10-25 14:18:57 +000049 SVal getLValueString(const GRState* St, const StringLiteral* S);
50
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000051 SVal getLValueVar(const GRState* St, const VarDecl* VD);
52
53 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
54
55 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
56
Zhongxing Xu0972d0a2008-10-24 01:09:32 +000057 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
58
59 SVal ArrayToPointer(SVal Array);
60
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000061 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000062
Zhongxing Xu73249322008-10-21 06:27:32 +000063 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +000064
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000065 Store Remove(Store store, Loc LV) {
66 // FIXME: Implement.
67 return store;
68 }
69
Zhongxing Xu79c57f82008-10-08 02:50:44 +000070 Store getInitialStore();
Ted Kremenek73a36c92008-10-24 20:32:16 +000071
72 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
73 /// 'this' object (C++). When used when analyzing a normal function this
74 /// method returns NULL.
75 const MemRegion* getSelfRegion(Store) {
76 assert (false && "Not implemented.");
77 return 0;
78 }
Zhongxing Xu79c57f82008-10-08 02:50:44 +000079
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000080 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
81 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
82 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
83 // FIXME: Implement this.
84 return store;
85 }
86
Zhongxing Xue3954d12008-10-21 05:29:26 +000087 Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
88 unsigned Count);
89
Zhongxing Xu79c57f82008-10-08 02:50:44 +000090 static inline RegionBindingsTy GetRegionBindings(Store store) {
91 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
92 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000093
Zhongxing Xu6149e882008-10-24 04:33:15 +000094 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000095
96 void iterBindings(Store store, BindingsHandler& f) {
97 // FIXME: Implement.
98 }
Zhongxing Xu702d4702008-10-24 08:42:28 +000099
100private:
101 Loc getVarLoc(const VarDecl* VD) {
102 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
103 }
104
105 Store InitializeArrayToUndefined(Store store, QualType T, MemRegion* BaseR);
106 Store InitializeStructToUndefined(Store store, QualType T, MemRegion* BaseR);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000107};
108
109} // end anonymous namespace
110
Ted Kremenekc3803992008-10-24 01:04:59 +0000111StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000112 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000113}
114
Zhongxing Xu2abba442008-10-25 14:18:57 +0000115SVal RegionStoreManager::getLValueString(const GRState* St,
116 const StringLiteral* S) {
117 return loc::MemRegionVal(MRMgr.getStringRegion(S));
118}
119
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000120SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
121 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
122}
123
124SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
125 SVal Base) {
126 return UnknownVal();
127}
128
129SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
130 const FieldDecl* D) {
131 if (Base.isUnknownOrUndef())
132 return Base;
133
134 Loc BaseL = cast<Loc>(Base);
135 const MemRegion* BaseR = 0;
136
137 switch (BaseL.getSubKind()) {
138 case loc::MemRegionKind:
139 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
140 break;
141
142 case loc::SymbolValKind:
143 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
144 break;
145
146 case loc::GotoLabelKind:
147 case loc::FuncValKind:
148 // These are anormal cases. Flag an undefined value.
149 return UndefinedVal();
150
151 case loc::ConcreteIntKind:
152 case loc::StringLiteralValKind:
153 // While these seem funny, this can happen through casts.
154 // FIXME: What we should return is the field offset. For example,
155 // add the field offset to the integer value. That way funny things
156 // like this work properly: &(((struct foo *) 0xa)->f)
157 return Base;
158
159 default:
160 assert("Unhandled Base.");
161 return Base;
162 }
163
164 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
165}
166
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000167SVal RegionStoreManager::getLValueElement(const GRState* St,
168 SVal Base, SVal Offset) {
169 if (Base.isUnknownOrUndef())
170 return Base;
171
172 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
173
174 // We expect BaseR is an ElementRegion, not a base VarRegion.
175
176 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
177
178 SVal Idx = ElemR->getIndex();
179
180 nonloc::ConcreteInt *CI1, *CI2;
181
182 // Only handle integer indices for now.
183 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
184 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
185 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
186 *CI2);
187 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
188 ElemR->getSuperRegion()));
189 }
190
191 return UnknownVal();
192}
193
194// Cast 'pointer to array' to 'pointer to the first element of array'.
195
196SVal RegionStoreManager::ArrayToPointer(SVal Array) {
197 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu2abba442008-10-25 14:18:57 +0000198 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
199
200 if (const StringRegion* StringR = dyn_cast<StringRegion>(ArrayR)) {
201 // FIXME: Find a better way to get bit width.
202 nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false));
203 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
204
205 return loc::MemRegionVal(ER);
206 }
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000207
Zhongxing Xudd9fe5e2008-10-24 09:06:51 +0000208 const Decl* D = cast<DeclRegion>(ArrayR)->getDecl();
209
210 QualType ArrayTy;
211 if (const VarDecl* VD = dyn_cast<VarDecl>(D))
212 ArrayTy = VD->getType();
213 else if (const FieldDecl* FD = dyn_cast<FieldDecl>(D))
214 ArrayTy = FD->getType();
215 else
216 assert(0 && "unknown decl");
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000217
218 if (const ConstantArrayType* CAT =
Zhongxing Xudd9fe5e2008-10-24 09:06:51 +0000219 dyn_cast<ConstantArrayType>(ArrayTy.getTypePtr())) {
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000220
221 nonloc::ConcreteInt Idx(BasicVals.getValue(0, CAT->getSize().getBitWidth(),
222 false));
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000223 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
224
225 return loc::MemRegionVal(ER);
226 }
227
228 return Array;
229}
230
Zhongxing Xu73249322008-10-21 06:27:32 +0000231SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000232 assert(!isa<UnknownVal>(L) && "location unknown");
233 assert(!isa<UndefinedVal>(L) && "location undefined");
234
235 switch (L.getSubKind()) {
236 case loc::MemRegionKind: {
237 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
238 assert(R && "bad region");
239
240 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
241 RegionBindingsTy::data_type* V = B.lookup(R);
242 return V ? *V : UnknownVal();
243 }
244
245 case loc::SymbolValKind:
246 return UnknownVal();
247
248 case loc::ConcreteIntKind:
249 return UndefinedVal(); // As in BasicStoreManager.
250
251 case loc::FuncValKind:
252 return L;
253
254 case loc::StringLiteralValKind:
255 return UnknownVal();
256
257 default:
258 assert(false && "Invalid Location");
259 break;
260 }
261}
262
Zhongxing Xu73249322008-10-21 06:27:32 +0000263Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000264 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000265
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000266 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000267
268 if (!R)
269 return store;
270
271 RegionBindingsTy B = GetRegionBindings(store);
272 return V.isUnknown()
273 ? RBFactory.Remove(B, R).getRoot()
274 : RBFactory.Add(B, R, V).getRoot();
275}
276
277Store RegionStoreManager::getInitialStore() {
278 typedef LiveVariables::AnalysisDataTy LVDataTy;
279 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
280
281 Store St = RBFactory.GetEmptyMap().getRoot();
282
283 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000284 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000285
Douglas Gregord2baafd2008-10-21 16:13:35 +0000286 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000287 // Punt on static variables for now.
288 if (VD->getStorageClass() == VarDecl::Static)
289 continue;
290
291 QualType T = VD->getType();
292 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000293 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000294 // Initialize globals and parameters to symbolic values.
295 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000296 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000297 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000298 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000299 : UndefinedVal();
300
Zhongxing Xu73249322008-10-21 06:27:32 +0000301 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000302 }
303 }
304 }
305 return St;
306}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000307
308Store RegionStoreManager::AddDecl(Store store,
309 const VarDecl* VD, Expr* Ex,
310 SVal InitVal, unsigned Count) {
311 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
312 SymbolManager& SymMgr = StateMgr.getSymbolManager();
313
314 if (VD->hasGlobalStorage()) {
315 // Static global variables should not be visited here.
316 assert(!(VD->getStorageClass() == VarDecl::Static &&
317 VD->isFileVarDecl()));
318 // Process static variables.
319 if (VD->getStorageClass() == VarDecl::Static) {
320 if (!Ex) {
321 // Only handle pointer and integer static variables.
322
323 QualType T = VD->getType();
324
325 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000326 store = Bind(store, getVarLoc(VD),
327 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000328
329 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000330 store = Bind(store, getVarLoc(VD),
331 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000332 else
333 assert("ignore other types of variables");
334 } else {
Zhongxing Xu73249322008-10-21 06:27:32 +0000335 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000336 }
337 }
338 } else {
339 // Process local variables.
340
341 QualType T = VD->getType();
342
Zhongxing Xu702d4702008-10-24 08:42:28 +0000343 VarRegion* VR = MRMgr.getVarRegion(VD);
344
Zhongxing Xue3954d12008-10-21 05:29:26 +0000345 if (Loc::IsLocType(T) || T->isIntegerType()) {
346 SVal V = Ex ? InitVal : UndefinedVal();
347 if (Ex && InitVal.isUnknown()) {
348 // "Conjured" symbols.
349 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
350 V = Loc::IsLocType(Ex->getType())
351 ? cast<SVal>(loc::SymbolVal(Sym))
352 : cast<SVal>(nonloc::SymbolVal(Sym));
353 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000354 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000355
356 } else if (T->isArrayType()) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000357 store = InitializeArrayToUndefined(store, T, VR);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000358
Zhongxing Xue3954d12008-10-21 05:29:26 +0000359 } else if (T->isStructureType()) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000360 store = InitializeStructToUndefined(store, T, VR);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000361 }
362 }
363 return store;
364}
365
Zhongxing Xuca892b82008-10-24 06:01:33 +0000366void RegionStoreManager::print(Store store, std::ostream& Out,
367 const char* nl, const char *sep) {
368 llvm::raw_os_ostream OS(Out);
369 RegionBindingsTy B = GetRegionBindings(store);
370 OS << "Store:" << nl;
371
372 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
373 OS << ' '; I.getKey()->print(OS); OS << " : ";
374 I.getData().print(OS); OS << nl;
375 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000376}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000377
378Store RegionStoreManager::InitializeArrayToUndefined(Store store, QualType T,
379 MemRegion* BaseR) {
380 assert(T->isArrayType());
381
382 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
383
384 // Only handle constant size array for now.
385 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
386
387 llvm::APInt Size = CAT->getSize();
388
389 for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
390 i != Size; ++i) {
391 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
392
393 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
394
395 store = Bind(store, loc::MemRegionVal(ER), UndefinedVal());
396 }
397 }
398
399 return store;
400}
401
402Store RegionStoreManager::InitializeStructToUndefined(Store store, QualType T,
403 MemRegion* BaseR) {
404 const RecordType* RT = cast<RecordType>(T.getTypePtr());
405 RecordDecl* RD = RT->getDecl();
406 assert(RD->isDefinition());
407
408 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
409 I != E; ++I) {
410
411 QualType FTy = (*I)->getType();
412 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
413
414 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
415 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
416
417 } else if (FTy->isArrayType()) {
418 store = InitializeArrayToUndefined(store, FTy, FR);
419
420 } else if (FTy->isStructureType()) {
421 store = InitializeStructToUndefined(store, FTy, FR);
422 }
423 }
424
425 return store;
426}