blob: c2a5da728926f3cfceb77b433f034d9b07c1234b [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 Xu8b2e05d2008-10-29 02:34:02 +000096 Store BindDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
97 unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +000098
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 Xu6e3f01c2008-10-31 07:16:08 +0000116
117 SVal RetrieveStruct(Store store, const TypedRegion* R);
118
119 // Utility methods.
120 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
121 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000122};
123
124} // end anonymous namespace
125
Ted Kremenek95c7b002008-10-24 01:04:59 +0000126StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000127 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000128}
129
Zhongxing Xu143bf822008-10-25 14:18:57 +0000130SVal RegionStoreManager::getLValueString(const GRState* St,
131 const StringLiteral* S) {
132 return loc::MemRegionVal(MRMgr.getStringRegion(S));
133}
134
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000135SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
136 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
137}
138
139SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
140 SVal Base) {
141 return UnknownVal();
142}
143
144SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
145 const FieldDecl* D) {
146 if (Base.isUnknownOrUndef())
147 return Base;
148
149 Loc BaseL = cast<Loc>(Base);
150 const MemRegion* BaseR = 0;
151
152 switch (BaseL.getSubKind()) {
153 case loc::MemRegionKind:
154 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
155 break;
156
157 case loc::SymbolValKind:
158 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
159 break;
160
161 case loc::GotoLabelKind:
162 case loc::FuncValKind:
163 // These are anormal cases. Flag an undefined value.
164 return UndefinedVal();
165
166 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000167 // While these seem funny, this can happen through casts.
168 // FIXME: What we should return is the field offset. For example,
169 // add the field offset to the integer value. That way funny things
170 // like this work properly: &(((struct foo *) 0xa)->f)
171 return Base;
172
173 default:
174 assert("Unhandled Base.");
175 return Base;
176 }
177
178 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
179}
180
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000181SVal RegionStoreManager::getLValueElement(const GRState* St,
182 SVal Base, SVal Offset) {
183 if (Base.isUnknownOrUndef())
184 return Base;
185
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000186 if (isa<loc::SymbolVal>(Base))
187 return Base;
188
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000189 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
190
191 // We expect BaseR is an ElementRegion, not a base VarRegion.
192
193 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
194
195 SVal Idx = ElemR->getIndex();
196
197 nonloc::ConcreteInt *CI1, *CI2;
198
199 // Only handle integer indices for now.
200 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
201 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
202 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
203 *CI2);
204 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
205 ElemR->getSuperRegion()));
206 }
207
208 return UnknownVal();
209}
210
211// Cast 'pointer to array' to 'pointer to the first element of array'.
212
213SVal RegionStoreManager::ArrayToPointer(SVal Array) {
214 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000215 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
216
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000217 // FIXME: Find a better way to get bit width.
218 nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false));
219 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
220
221 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000222}
223
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000224SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000225 assert(!isa<UnknownVal>(L) && "location unknown");
226 assert(!isa<UndefinedVal>(L) && "location undefined");
227
228 switch (L.getSubKind()) {
229 case loc::MemRegionKind: {
230 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
231 assert(R && "bad region");
232
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000233 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
234 if (TR->getType(getContext())->isStructureType())
235 return RetrieveStruct(S, TR);
236
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000237 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
238 RegionBindingsTy::data_type* V = B.lookup(R);
239 return V ? *V : UnknownVal();
240 }
241
242 case loc::SymbolValKind:
243 return UnknownVal();
244
245 case loc::ConcreteIntKind:
246 return UndefinedVal(); // As in BasicStoreManager.
247
248 case loc::FuncValKind:
249 return L;
250
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000251 default:
252 assert(false && "Invalid Location");
253 break;
254 }
255}
256
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000257SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
258 QualType T = R->getType(getContext());
259 assert(T->isStructureType());
260
261 const RecordType* RT = cast<RecordType>(T.getTypePtr());
262 RecordDecl* RD = RT->getDecl();
263 assert(RD->isDefinition());
264
265 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
266
267 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
268 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
269 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
270 RegionBindingsTy::data_type* data = B.lookup(R);
271
272 SVal FieldValue = data ? *data : UnknownVal();
273
274 StructVal = getBasicVals().consVals(FieldValue, StructVal);
275 }
276
277 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
278}
279
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000280Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000281 if (LV.getSubKind() == loc::SymbolValKind)
282 return store;
283
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000284 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000285
Ted Kremenek993f1c72008-10-17 20:28:54 +0000286 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000287
288 if (!R)
289 return store;
290
291 RegionBindingsTy B = GetRegionBindings(store);
292 return V.isUnknown()
293 ? RBFactory.Remove(B, R).getRoot()
294 : RBFactory.Add(B, R, V).getRoot();
295}
296
297Store RegionStoreManager::getInitialStore() {
298 typedef LiveVariables::AnalysisDataTy LVDataTy;
299 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
300
301 Store St = RBFactory.GetEmptyMap().getRoot();
302
303 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000304 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000305
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000306 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000307 // Punt on static variables for now.
308 if (VD->getStorageClass() == VarDecl::Static)
309 continue;
310
311 QualType T = VD->getType();
312 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000313 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000314 // Initialize globals and parameters to symbolic values.
315 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000316 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000317 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000318 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000319 : UndefinedVal();
320
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000321 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu17892752008-10-08 02:50:44 +0000322 }
323 }
324 }
325 return St;
326}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000327
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +0000328Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, Expr* Ex,
329 SVal InitVal, unsigned Count) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000330 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
331 SymbolManager& SymMgr = StateMgr.getSymbolManager();
332
333 if (VD->hasGlobalStorage()) {
334 // Static global variables should not be visited here.
335 assert(!(VD->getStorageClass() == VarDecl::Static &&
336 VD->isFileVarDecl()));
337 // Process static variables.
338 if (VD->getStorageClass() == VarDecl::Static) {
339 if (!Ex) {
340 // Only handle pointer and integer static variables.
341
342 QualType T = VD->getType();
343
344 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000345 store = Bind(store, getVarLoc(VD),
346 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000347
348 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000349 store = Bind(store, getVarLoc(VD),
350 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000351 else
352 assert("ignore other types of variables");
353 } else {
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000354 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000355 }
356 }
357 } else {
358 // Process local variables.
359
360 QualType T = VD->getType();
361
Zhongxing Xua82512a2008-10-24 08:42:28 +0000362 VarRegion* VR = MRMgr.getVarRegion(VD);
363
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000364 if (Loc::IsLocType(T) || T->isIntegerType()) {
365 SVal V = Ex ? InitVal : UndefinedVal();
366 if (Ex && InitVal.isUnknown()) {
367 // "Conjured" symbols.
368 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
369 V = Loc::IsLocType(Ex->getType())
370 ? cast<SVal>(loc::SymbolVal(Sym))
371 : cast<SVal>(nonloc::SymbolVal(Sym));
372 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000373 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000374
375 } else if (T->isArrayType()) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000376 store = InitializeArrayToUndefined(store, T, VR);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000377
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000378 } else if (T->isStructureType()) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000379 store = InitializeStructToUndefined(store, T, VR);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000380 }
381 }
382 return store;
383}
384
Zhongxing Xua071eb02008-10-24 06:01:33 +0000385void RegionStoreManager::print(Store store, std::ostream& Out,
386 const char* nl, const char *sep) {
387 llvm::raw_os_ostream OS(Out);
388 RegionBindingsTy B = GetRegionBindings(store);
389 OS << "Store:" << nl;
390
391 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
392 OS << ' '; I.getKey()->print(OS); OS << " : ";
393 I.getData().print(OS); OS << nl;
394 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000395}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000396
397Store RegionStoreManager::InitializeArrayToUndefined(Store store, QualType T,
398 MemRegion* BaseR) {
399 assert(T->isArrayType());
400
401 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
402
403 // Only handle constant size array for now.
404 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
405
406 llvm::APInt Size = CAT->getSize();
407
408 for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
409 i != Size; ++i) {
410 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
411
412 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
413
414 store = Bind(store, loc::MemRegionVal(ER), UndefinedVal());
415 }
416 }
417
418 return store;
419}
420
421Store RegionStoreManager::InitializeStructToUndefined(Store store, QualType T,
422 MemRegion* BaseR) {
Zhongxing Xue70559f2008-10-27 13:35:03 +0000423 QualType CT = StateMgr.getContext().getCanonicalType(T);
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000424 const RecordType* RT = cast<RecordType>(CT.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000425 RecordDecl* RD = RT->getDecl();
426 assert(RD->isDefinition());
427
428 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
429 I != E; ++I) {
430
431 QualType FTy = (*I)->getType();
432 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
433
434 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
435 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
436
437 } else if (FTy->isArrayType()) {
438 store = InitializeArrayToUndefined(store, FTy, FR);
439
440 } else if (FTy->isStructureType()) {
441 store = InitializeStructToUndefined(store, FTy, FR);
442 }
443 }
444
445 return store;
446}