blob: 1c73ceae202cc66648280f8655ae36daca59c360 [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 }
Ted Kremenek4f090272008-10-27 21:54:31 +000048
49 Store BindCompoundLiteral(Store store, const CompoundLiteralRegion* R,
50 const SVal* BegInit, const SVal* EndInit) {
51
52 // FIXME: Let's discuss how we want to do the mapping in RegionStore
53 // from CompoundLiteralRegion to values.
54 assert (false && "Not yet implemented.");
55 return store;
56 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +000057
Zhongxing Xu143bf822008-10-25 14:18:57 +000058 SVal getLValueString(const GRState* St, const StringLiteral* S);
59
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000060 SVal getLValueVar(const GRState* St, const VarDecl* VD);
61
62 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
63
64 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
65
Zhongxing Xub1d542a2008-10-24 01:09:32 +000066 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
67
68 SVal ArrayToPointer(SVal Array);
69
Zhongxing Xu24194ef2008-10-24 01:38:55 +000070 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000071
Zhongxing Xu8485ec62008-10-21 06:27:32 +000072 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +000073
Zhongxing Xu24194ef2008-10-24 01:38:55 +000074 Store Remove(Store store, Loc LV) {
75 // FIXME: Implement.
76 return store;
77 }
78
Zhongxing Xu17892752008-10-08 02:50:44 +000079 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +000080
81 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
82 /// 'this' object (C++). When used when analyzing a normal function this
83 /// method returns NULL.
84 const MemRegion* getSelfRegion(Store) {
85 assert (false && "Not implemented.");
86 return 0;
87 }
Zhongxing Xu17892752008-10-08 02:50:44 +000088
Zhongxing Xu24194ef2008-10-24 01:38:55 +000089 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
90 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
91 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
92 // FIXME: Implement this.
93 return store;
94 }
95
Zhongxing Xu53bcdd42008-10-21 05:29:26 +000096 Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
97 unsigned Count);
98
Zhongxing Xu17892752008-10-08 02:50:44 +000099 static inline RegionBindingsTy GetRegionBindings(Store store) {
100 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
101 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000102
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000103 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000104
105 void iterBindings(Store store, BindingsHandler& f) {
106 // FIXME: Implement.
107 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000108
109private:
110 Loc getVarLoc(const VarDecl* VD) {
111 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
112 }
113
114 Store InitializeArrayToUndefined(Store store, QualType T, MemRegion* BaseR);
115 Store InitializeStructToUndefined(Store store, QualType T, MemRegion* BaseR);
Zhongxing Xu17892752008-10-08 02:50:44 +0000116};
117
118} // end anonymous namespace
119
Ted Kremenek95c7b002008-10-24 01:04:59 +0000120StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000121 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000122}
123
Zhongxing Xu143bf822008-10-25 14:18:57 +0000124SVal RegionStoreManager::getLValueString(const GRState* St,
125 const StringLiteral* S) {
126 return loc::MemRegionVal(MRMgr.getStringRegion(S));
127}
128
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000129SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
130 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
131}
132
133SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
134 SVal Base) {
135 return UnknownVal();
136}
137
138SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
139 const FieldDecl* D) {
140 if (Base.isUnknownOrUndef())
141 return Base;
142
143 Loc BaseL = cast<Loc>(Base);
144 const MemRegion* BaseR = 0;
145
146 switch (BaseL.getSubKind()) {
147 case loc::MemRegionKind:
148 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
149 break;
150
151 case loc::SymbolValKind:
152 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
153 break;
154
155 case loc::GotoLabelKind:
156 case loc::FuncValKind:
157 // These are anormal cases. Flag an undefined value.
158 return UndefinedVal();
159
160 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000161 // While these seem funny, this can happen through casts.
162 // FIXME: What we should return is the field offset. For example,
163 // add the field offset to the integer value. That way funny things
164 // like this work properly: &(((struct foo *) 0xa)->f)
165 return Base;
166
167 default:
168 assert("Unhandled Base.");
169 return Base;
170 }
171
172 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
173}
174
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000175SVal RegionStoreManager::getLValueElement(const GRState* St,
176 SVal Base, SVal Offset) {
177 if (Base.isUnknownOrUndef())
178 return Base;
179
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000180 if (isa<loc::SymbolVal>(Base))
181 return Base;
182
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000183 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
184
185 // We expect BaseR is an ElementRegion, not a base VarRegion.
186
187 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
188
189 SVal Idx = ElemR->getIndex();
190
191 nonloc::ConcreteInt *CI1, *CI2;
192
193 // Only handle integer indices for now.
194 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
195 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
196 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
197 *CI2);
198 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
199 ElemR->getSuperRegion()));
200 }
201
202 return UnknownVal();
203}
204
205// Cast 'pointer to array' to 'pointer to the first element of array'.
206
207SVal RegionStoreManager::ArrayToPointer(SVal Array) {
208 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000209 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
210
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000211 // FIXME: Find a better way to get bit width.
212 nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false));
213 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
214
215 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000216}
217
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000218SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-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
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000241 default:
242 assert(false && "Invalid Location");
243 break;
244 }
245}
246
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000247Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000248 if (LV.getSubKind() == loc::SymbolValKind)
249 return store;
250
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000251 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000252
Ted Kremenek993f1c72008-10-17 20:28:54 +0000253 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-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 Gregor8e9bebd2008-10-21 16:13:35 +0000271 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000272
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000273 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-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 Xu1c96b242008-10-17 05:57:07 +0000280 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000281 // Initialize globals and parameters to symbolic values.
282 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000283 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000284 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000285 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000286 : UndefinedVal();
287
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000288 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu17892752008-10-08 02:50:44 +0000289 }
290 }
291 }
292 return St;
293}
Zhongxing Xu53bcdd42008-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 Xu8485ec62008-10-21 06:27:32 +0000313 store = Bind(store, getVarLoc(VD),
314 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000315
316 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000317 store = Bind(store, getVarLoc(VD),
318 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000319 else
320 assert("ignore other types of variables");
321 } else {
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000322 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000323 }
324 }
325 } else {
326 // Process local variables.
327
328 QualType T = VD->getType();
329
Zhongxing Xua82512a2008-10-24 08:42:28 +0000330 VarRegion* VR = MRMgr.getVarRegion(VD);
331
Zhongxing Xu53bcdd42008-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 Xua82512a2008-10-24 08:42:28 +0000341 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000342
343 } else if (T->isArrayType()) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000344 store = InitializeArrayToUndefined(store, T, VR);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000345
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000346 } else if (T->isStructureType()) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000347 store = InitializeStructToUndefined(store, T, VR);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000348 }
349 }
350 return store;
351}
352
Zhongxing Xua071eb02008-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 Xu5b8b6f22008-10-24 04:33:15 +0000363}
Zhongxing Xua82512a2008-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) {
Zhongxing Xue70559f2008-10-27 13:35:03 +0000391 QualType CT = StateMgr.getContext().getCanonicalType(T);
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000392 const RecordType* RT = cast<RecordType>(CT.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000393 RecordDecl* RD = RT->getDecl();
394 assert(RD->isDefinition());
395
396 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
397 I != E; ++I) {
398
399 QualType FTy = (*I)->getType();
400 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
401
402 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
403 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
404
405 } else if (FTy->isArrayType()) {
406 store = InitializeArrayToUndefined(store, FTy, FR);
407
408 } else if (FTy->isStructureType()) {
409 store = InitializeStructToUndefined(store, FTy, FR);
410 }
411 }
412
413 return store;
414}