blob: ae0383d06eb316e4f83903a4283fb45a8e89b2ae [file] [log] [blame]
Zhongxing Xu17892752008-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 Xua071eb02008-10-24 06:01:33 +000022#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000023#include "llvm/Support/Compiler.h"
24
25using namespace clang;
26
Zhongxing Xu1c96b242008-10-17 05:57:07 +000027typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xu17892752008-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 Xu24194ef2008-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 Xu143bf822008-10-25 14:18:57 +000049 SVal getLValueString(const GRState* St, const StringLiteral* S);
50
Zhongxing Xuc4bf72c2008-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 Xub1d542a2008-10-24 01:09:32 +000057 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
58
59 SVal ArrayToPointer(SVal Array);
60
Zhongxing Xu24194ef2008-10-24 01:38:55 +000061 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000062
Zhongxing Xu8485ec62008-10-21 06:27:32 +000063 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +000064
Zhongxing Xu24194ef2008-10-24 01:38:55 +000065 Store Remove(Store store, Loc LV) {
66 // FIXME: Implement.
67 return store;
68 }
69
Zhongxing Xu17892752008-10-08 02:50:44 +000070 Store getInitialStore();
Ted Kremenek9deb0e32008-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 Xu17892752008-10-08 02:50:44 +000079
Zhongxing Xu24194ef2008-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 Xu53bcdd42008-10-21 05:29:26 +000087 Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
88 unsigned Count);
89
Zhongxing Xu17892752008-10-08 02:50:44 +000090 static inline RegionBindingsTy GetRegionBindings(Store store) {
91 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
92 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +000093
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +000094 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000095
96 void iterBindings(Store store, BindingsHandler& f) {
97 // FIXME: Implement.
98 }
Zhongxing Xua82512a2008-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 Xu17892752008-10-08 02:50:44 +0000107};
108
109} // end anonymous namespace
110
Ted Kremenek95c7b002008-10-24 01:04:59 +0000111StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000112 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000113}
114
Zhongxing Xu143bf822008-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 Xuc4bf72c2008-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:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000152 // While these seem funny, this can happen through casts.
153 // FIXME: What we should return is the field offset. For example,
154 // add the field offset to the integer value. That way funny things
155 // like this work properly: &(((struct foo *) 0xa)->f)
156 return Base;
157
158 default:
159 assert("Unhandled Base.");
160 return Base;
161 }
162
163 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
164}
165
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000166SVal RegionStoreManager::getLValueElement(const GRState* St,
167 SVal Base, SVal Offset) {
168 if (Base.isUnknownOrUndef())
169 return Base;
170
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000171 if (isa<loc::SymbolVal>(Base))
172 return Base;
173
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000174 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
175
176 // We expect BaseR is an ElementRegion, not a base VarRegion.
177
178 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
179
180 SVal Idx = ElemR->getIndex();
181
182 nonloc::ConcreteInt *CI1, *CI2;
183
184 // Only handle integer indices for now.
185 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
186 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
187 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
188 *CI2);
189 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
190 ElemR->getSuperRegion()));
191 }
192
193 return UnknownVal();
194}
195
196// Cast 'pointer to array' to 'pointer to the first element of array'.
197
198SVal RegionStoreManager::ArrayToPointer(SVal Array) {
199 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000200 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
201
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000202 // FIXME: Find a better way to get bit width.
203 nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false));
204 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
205
206 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000207}
208
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000209SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000210 assert(!isa<UnknownVal>(L) && "location unknown");
211 assert(!isa<UndefinedVal>(L) && "location undefined");
212
213 switch (L.getSubKind()) {
214 case loc::MemRegionKind: {
215 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
216 assert(R && "bad region");
217
218 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
219 RegionBindingsTy::data_type* V = B.lookup(R);
220 return V ? *V : UnknownVal();
221 }
222
223 case loc::SymbolValKind:
224 return UnknownVal();
225
226 case loc::ConcreteIntKind:
227 return UndefinedVal(); // As in BasicStoreManager.
228
229 case loc::FuncValKind:
230 return L;
231
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000232 default:
233 assert(false && "Invalid Location");
234 break;
235 }
236}
237
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000238Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000239 if (LV.getSubKind() == loc::SymbolValKind)
240 return store;
241
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000242 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000243
Ted Kremenek993f1c72008-10-17 20:28:54 +0000244 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000245
246 if (!R)
247 return store;
248
249 RegionBindingsTy B = GetRegionBindings(store);
250 return V.isUnknown()
251 ? RBFactory.Remove(B, R).getRoot()
252 : RBFactory.Add(B, R, V).getRoot();
253}
254
255Store RegionStoreManager::getInitialStore() {
256 typedef LiveVariables::AnalysisDataTy LVDataTy;
257 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
258
259 Store St = RBFactory.GetEmptyMap().getRoot();
260
261 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000262 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000263
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000264 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000265 // Punt on static variables for now.
266 if (VD->getStorageClass() == VarDecl::Static)
267 continue;
268
269 QualType T = VD->getType();
270 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000271 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000272 // Initialize globals and parameters to symbolic values.
273 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000274 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000275 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000276 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000277 : UndefinedVal();
278
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000279 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu17892752008-10-08 02:50:44 +0000280 }
281 }
282 }
283 return St;
284}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000285
286Store RegionStoreManager::AddDecl(Store store,
287 const VarDecl* VD, Expr* Ex,
288 SVal InitVal, unsigned Count) {
289 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
290 SymbolManager& SymMgr = StateMgr.getSymbolManager();
291
292 if (VD->hasGlobalStorage()) {
293 // Static global variables should not be visited here.
294 assert(!(VD->getStorageClass() == VarDecl::Static &&
295 VD->isFileVarDecl()));
296 // Process static variables.
297 if (VD->getStorageClass() == VarDecl::Static) {
298 if (!Ex) {
299 // Only handle pointer and integer static variables.
300
301 QualType T = VD->getType();
302
303 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000304 store = Bind(store, getVarLoc(VD),
305 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000306
307 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000308 store = Bind(store, getVarLoc(VD),
309 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000310 else
311 assert("ignore other types of variables");
312 } else {
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000313 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000314 }
315 }
316 } else {
317 // Process local variables.
318
319 QualType T = VD->getType();
320
Zhongxing Xua82512a2008-10-24 08:42:28 +0000321 VarRegion* VR = MRMgr.getVarRegion(VD);
322
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000323 if (Loc::IsLocType(T) || T->isIntegerType()) {
324 SVal V = Ex ? InitVal : UndefinedVal();
325 if (Ex && InitVal.isUnknown()) {
326 // "Conjured" symbols.
327 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
328 V = Loc::IsLocType(Ex->getType())
329 ? cast<SVal>(loc::SymbolVal(Sym))
330 : cast<SVal>(nonloc::SymbolVal(Sym));
331 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000332 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000333
334 } else if (T->isArrayType()) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000335 store = InitializeArrayToUndefined(store, T, VR);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000336
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000337 } else if (T->isStructureType()) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000338 store = InitializeStructToUndefined(store, T, VR);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000339 }
340 }
341 return store;
342}
343
Zhongxing Xua071eb02008-10-24 06:01:33 +0000344void RegionStoreManager::print(Store store, std::ostream& Out,
345 const char* nl, const char *sep) {
346 llvm::raw_os_ostream OS(Out);
347 RegionBindingsTy B = GetRegionBindings(store);
348 OS << "Store:" << nl;
349
350 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
351 OS << ' '; I.getKey()->print(OS); OS << " : ";
352 I.getData().print(OS); OS << nl;
353 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000354}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000355
356Store RegionStoreManager::InitializeArrayToUndefined(Store store, QualType T,
357 MemRegion* BaseR) {
358 assert(T->isArrayType());
359
360 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
361
362 // Only handle constant size array for now.
363 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
364
365 llvm::APInt Size = CAT->getSize();
366
367 for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
368 i != Size; ++i) {
369 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
370
371 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
372
373 store = Bind(store, loc::MemRegionVal(ER), UndefinedVal());
374 }
375 }
376
377 return store;
378}
379
380Store RegionStoreManager::InitializeStructToUndefined(Store store, QualType T,
381 MemRegion* BaseR) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000382 QualType CT = T->getCanonicalTypeInternal();
383 const RecordType* RT = cast<RecordType>(CT.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000384 RecordDecl* RD = RT->getDecl();
385 assert(RD->isDefinition());
386
387 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
388 I != E; ++I) {
389
390 QualType FTy = (*I)->getType();
391 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
392
393 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
394 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
395
396 } else if (FTy->isArrayType()) {
397 store = InitializeArrayToUndefined(store, FTy, FR);
398
399 } else if (FTy->isStructureType()) {
400 store = InitializeStructToUndefined(store, FTy, FR);
401 }
402 }
403
404 return store;
405}