blob: e2b6b13e64de412d7c66fb031a557e50d6b363fb [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 Xu6f1b5152008-10-22 13:44:38 +000049 SVal getLValueVar(const GRState* St, const VarDecl* VD);
50
51 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
52
53 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
54
Zhongxing Xu0972d0a2008-10-24 01:09:32 +000055 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
56
57 SVal ArrayToPointer(SVal Array);
58
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000059 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000060
Zhongxing Xu73249322008-10-21 06:27:32 +000061 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +000062
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000063 Store Remove(Store store, Loc LV) {
64 // FIXME: Implement.
65 return store;
66 }
67
Zhongxing Xu79c57f82008-10-08 02:50:44 +000068 Store getInitialStore();
Ted Kremenek73a36c92008-10-24 20:32:16 +000069
70 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
71 /// 'this' object (C++). When used when analyzing a normal function this
72 /// method returns NULL.
73 const MemRegion* getSelfRegion(Store) {
74 assert (false && "Not implemented.");
75 return 0;
76 }
Zhongxing Xu79c57f82008-10-08 02:50:44 +000077
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000078 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
79 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
80 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
81 // FIXME: Implement this.
82 return store;
83 }
84
Zhongxing Xue3954d12008-10-21 05:29:26 +000085 Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
86 unsigned Count);
87
Zhongxing Xu79c57f82008-10-08 02:50:44 +000088 static inline RegionBindingsTy GetRegionBindings(Store store) {
89 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
90 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000091
Zhongxing Xu6149e882008-10-24 04:33:15 +000092 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000093
94 void iterBindings(Store store, BindingsHandler& f) {
95 // FIXME: Implement.
96 }
Zhongxing Xu702d4702008-10-24 08:42:28 +000097
98private:
99 Loc getVarLoc(const VarDecl* VD) {
100 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
101 }
102
103 Store InitializeArrayToUndefined(Store store, QualType T, MemRegion* BaseR);
104 Store InitializeStructToUndefined(Store store, QualType T, MemRegion* BaseR);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000105};
106
107} // end anonymous namespace
108
Ted Kremenekc3803992008-10-24 01:04:59 +0000109StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000110 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000111}
112
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000113SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
114 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
115}
116
117SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
118 SVal Base) {
119 return UnknownVal();
120}
121
122SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
123 const FieldDecl* D) {
124 if (Base.isUnknownOrUndef())
125 return Base;
126
127 Loc BaseL = cast<Loc>(Base);
128 const MemRegion* BaseR = 0;
129
130 switch (BaseL.getSubKind()) {
131 case loc::MemRegionKind:
132 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
133 break;
134
135 case loc::SymbolValKind:
136 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
137 break;
138
139 case loc::GotoLabelKind:
140 case loc::FuncValKind:
141 // These are anormal cases. Flag an undefined value.
142 return UndefinedVal();
143
144 case loc::ConcreteIntKind:
145 case loc::StringLiteralValKind:
146 // While these seem funny, this can happen through casts.
147 // FIXME: What we should return is the field offset. For example,
148 // add the field offset to the integer value. That way funny things
149 // like this work properly: &(((struct foo *) 0xa)->f)
150 return Base;
151
152 default:
153 assert("Unhandled Base.");
154 return Base;
155 }
156
157 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
158}
159
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000160SVal RegionStoreManager::getLValueElement(const GRState* St,
161 SVal Base, SVal Offset) {
162 if (Base.isUnknownOrUndef())
163 return Base;
164
165 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
166
167 // We expect BaseR is an ElementRegion, not a base VarRegion.
168
169 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
170
171 SVal Idx = ElemR->getIndex();
172
173 nonloc::ConcreteInt *CI1, *CI2;
174
175 // Only handle integer indices for now.
176 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
177 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
178 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
179 *CI2);
180 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
181 ElemR->getSuperRegion()));
182 }
183
184 return UnknownVal();
185}
186
187// Cast 'pointer to array' to 'pointer to the first element of array'.
188
189SVal RegionStoreManager::ArrayToPointer(SVal Array) {
190 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
191
Zhongxing Xudd9fe5e2008-10-24 09:06:51 +0000192 const Decl* D = cast<DeclRegion>(ArrayR)->getDecl();
193
194 QualType ArrayTy;
195 if (const VarDecl* VD = dyn_cast<VarDecl>(D))
196 ArrayTy = VD->getType();
197 else if (const FieldDecl* FD = dyn_cast<FieldDecl>(D))
198 ArrayTy = FD->getType();
199 else
200 assert(0 && "unknown decl");
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000201
202 if (const ConstantArrayType* CAT =
Zhongxing Xudd9fe5e2008-10-24 09:06:51 +0000203 dyn_cast<ConstantArrayType>(ArrayTy.getTypePtr())) {
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000204
205 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
206
207 nonloc::ConcreteInt Idx(BasicVals.getValue(0, CAT->getSize().getBitWidth(),
208 false));
209
210 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
211
212 return loc::MemRegionVal(ER);
213 }
214
215 return Array;
216}
217
Zhongxing Xu73249322008-10-21 06:27:32 +0000218SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000219 assert(!isa<UnknownVal>(L) && "location unknown");
220 assert(!isa<UndefinedVal>(L) && "location undefined");
221
222 switch (L.getSubKind()) {
223 case loc::MemRegionKind: {
224 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
225 assert(R && "bad region");
226
227 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
228 RegionBindingsTy::data_type* V = B.lookup(R);
229 return V ? *V : UnknownVal();
230 }
231
232 case loc::SymbolValKind:
233 return UnknownVal();
234
235 case loc::ConcreteIntKind:
236 return UndefinedVal(); // As in BasicStoreManager.
237
238 case loc::FuncValKind:
239 return L;
240
241 case loc::StringLiteralValKind:
242 return UnknownVal();
243
244 default:
245 assert(false && "Invalid Location");
246 break;
247 }
248}
249
Zhongxing Xu73249322008-10-21 06:27:32 +0000250Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000251 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000252
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000253 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000254
255 if (!R)
256 return store;
257
258 RegionBindingsTy B = GetRegionBindings(store);
259 return V.isUnknown()
260 ? RBFactory.Remove(B, R).getRoot()
261 : RBFactory.Add(B, R, V).getRoot();
262}
263
264Store RegionStoreManager::getInitialStore() {
265 typedef LiveVariables::AnalysisDataTy LVDataTy;
266 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
267
268 Store St = RBFactory.GetEmptyMap().getRoot();
269
270 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000271 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000272
Douglas Gregord2baafd2008-10-21 16:13:35 +0000273 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000274 // Punt on static variables for now.
275 if (VD->getStorageClass() == VarDecl::Static)
276 continue;
277
278 QualType T = VD->getType();
279 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000280 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000281 // Initialize globals and parameters to symbolic values.
282 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000283 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000284 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000285 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000286 : UndefinedVal();
287
Zhongxing Xu73249322008-10-21 06:27:32 +0000288 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000289 }
290 }
291 }
292 return St;
293}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000294
295Store RegionStoreManager::AddDecl(Store store,
296 const VarDecl* VD, Expr* Ex,
297 SVal InitVal, unsigned Count) {
298 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
299 SymbolManager& SymMgr = StateMgr.getSymbolManager();
300
301 if (VD->hasGlobalStorage()) {
302 // Static global variables should not be visited here.
303 assert(!(VD->getStorageClass() == VarDecl::Static &&
304 VD->isFileVarDecl()));
305 // Process static variables.
306 if (VD->getStorageClass() == VarDecl::Static) {
307 if (!Ex) {
308 // Only handle pointer and integer static variables.
309
310 QualType T = VD->getType();
311
312 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000313 store = Bind(store, getVarLoc(VD),
314 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000315
316 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000317 store = Bind(store, getVarLoc(VD),
318 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000319 else
320 assert("ignore other types of variables");
321 } else {
Zhongxing Xu73249322008-10-21 06:27:32 +0000322 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000323 }
324 }
325 } else {
326 // Process local variables.
327
328 QualType T = VD->getType();
329
Zhongxing Xu702d4702008-10-24 08:42:28 +0000330 VarRegion* VR = MRMgr.getVarRegion(VD);
331
Zhongxing Xue3954d12008-10-21 05:29:26 +0000332 if (Loc::IsLocType(T) || T->isIntegerType()) {
333 SVal V = Ex ? InitVal : UndefinedVal();
334 if (Ex && InitVal.isUnknown()) {
335 // "Conjured" symbols.
336 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
337 V = Loc::IsLocType(Ex->getType())
338 ? cast<SVal>(loc::SymbolVal(Sym))
339 : cast<SVal>(nonloc::SymbolVal(Sym));
340 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000341 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000342
343 } else if (T->isArrayType()) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000344 store = InitializeArrayToUndefined(store, T, VR);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000345
Zhongxing Xue3954d12008-10-21 05:29:26 +0000346 } else if (T->isStructureType()) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000347 store = InitializeStructToUndefined(store, T, VR);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000348 }
349 }
350 return store;
351}
352
Zhongxing Xuca892b82008-10-24 06:01:33 +0000353void RegionStoreManager::print(Store store, std::ostream& Out,
354 const char* nl, const char *sep) {
355 llvm::raw_os_ostream OS(Out);
356 RegionBindingsTy B = GetRegionBindings(store);
357 OS << "Store:" << nl;
358
359 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
360 OS << ' '; I.getKey()->print(OS); OS << " : ";
361 I.getData().print(OS); OS << nl;
362 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000363}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000364
365Store RegionStoreManager::InitializeArrayToUndefined(Store store, QualType T,
366 MemRegion* BaseR) {
367 assert(T->isArrayType());
368
369 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
370
371 // Only handle constant size array for now.
372 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
373
374 llvm::APInt Size = CAT->getSize();
375
376 for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
377 i != Size; ++i) {
378 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
379
380 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
381
382 store = Bind(store, loc::MemRegionVal(ER), UndefinedVal());
383 }
384 }
385
386 return store;
387}
388
389Store RegionStoreManager::InitializeStructToUndefined(Store store, QualType T,
390 MemRegion* BaseR) {
391 const RecordType* RT = cast<RecordType>(T.getTypePtr());
392 RecordDecl* RD = RT->getDecl();
393 assert(RD->isDefinition());
394
395 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
396 I != E; ++I) {
397
398 QualType FTy = (*I)->getType();
399 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
400
401 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
402 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
403
404 } else if (FTy->isArrayType()) {
405 store = InitializeArrayToUndefined(store, FTy, FR);
406
407 } else if (FTy->isStructureType()) {
408 store = InitializeStructToUndefined(store, FTy, FR);
409 }
410 }
411
412 return store;
413}