blob: bd3dd0911dd4922806c4af646a0ec0f3a2b7a2bb [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();
69
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000070 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
71 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
72 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
73 // FIXME: Implement this.
74 return store;
75 }
76
Zhongxing Xue3954d12008-10-21 05:29:26 +000077 Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
78 unsigned Count);
79
Zhongxing Xu79c57f82008-10-08 02:50:44 +000080 static inline RegionBindingsTy GetRegionBindings(Store store) {
81 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
82 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000083
Zhongxing Xu6149e882008-10-24 04:33:15 +000084 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000085
86 void iterBindings(Store store, BindingsHandler& f) {
87 // FIXME: Implement.
88 }
Zhongxing Xu702d4702008-10-24 08:42:28 +000089
90private:
91 Loc getVarLoc(const VarDecl* VD) {
92 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
93 }
94
95 Store InitializeArrayToUndefined(Store store, QualType T, MemRegion* BaseR);
96 Store InitializeStructToUndefined(Store store, QualType T, MemRegion* BaseR);
Zhongxing Xu79c57f82008-10-08 02:50:44 +000097};
98
99} // end anonymous namespace
100
Ted Kremenekc3803992008-10-24 01:04:59 +0000101StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000102 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000103}
104
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000105SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
106 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
107}
108
109SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
110 SVal Base) {
111 return UnknownVal();
112}
113
114SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
115 const FieldDecl* D) {
116 if (Base.isUnknownOrUndef())
117 return Base;
118
119 Loc BaseL = cast<Loc>(Base);
120 const MemRegion* BaseR = 0;
121
122 switch (BaseL.getSubKind()) {
123 case loc::MemRegionKind:
124 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
125 break;
126
127 case loc::SymbolValKind:
128 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
129 break;
130
131 case loc::GotoLabelKind:
132 case loc::FuncValKind:
133 // These are anormal cases. Flag an undefined value.
134 return UndefinedVal();
135
136 case loc::ConcreteIntKind:
137 case loc::StringLiteralValKind:
138 // While these seem funny, this can happen through casts.
139 // FIXME: What we should return is the field offset. For example,
140 // add the field offset to the integer value. That way funny things
141 // like this work properly: &(((struct foo *) 0xa)->f)
142 return Base;
143
144 default:
145 assert("Unhandled Base.");
146 return Base;
147 }
148
149 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
150}
151
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000152SVal RegionStoreManager::getLValueElement(const GRState* St,
153 SVal Base, SVal Offset) {
154 if (Base.isUnknownOrUndef())
155 return Base;
156
157 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
158
159 // We expect BaseR is an ElementRegion, not a base VarRegion.
160
161 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
162
163 SVal Idx = ElemR->getIndex();
164
165 nonloc::ConcreteInt *CI1, *CI2;
166
167 // Only handle integer indices for now.
168 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
169 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
170 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
171 *CI2);
172 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
173 ElemR->getSuperRegion()));
174 }
175
176 return UnknownVal();
177}
178
179// Cast 'pointer to array' to 'pointer to the first element of array'.
180
181SVal RegionStoreManager::ArrayToPointer(SVal Array) {
182 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
183
184 const VarDecl* D = cast<VarRegion>(ArrayR)->getDecl();
185
186 if (const ConstantArrayType* CAT =
187 dyn_cast<ConstantArrayType>(D->getType().getTypePtr())) {
188
189 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
190
191 nonloc::ConcreteInt Idx(BasicVals.getValue(0, CAT->getSize().getBitWidth(),
192 false));
193
194 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
195
196 return loc::MemRegionVal(ER);
197 }
198
199 return Array;
200}
201
Zhongxing Xu73249322008-10-21 06:27:32 +0000202SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000203 assert(!isa<UnknownVal>(L) && "location unknown");
204 assert(!isa<UndefinedVal>(L) && "location undefined");
205
206 switch (L.getSubKind()) {
207 case loc::MemRegionKind: {
208 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
209 assert(R && "bad region");
210
211 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
212 RegionBindingsTy::data_type* V = B.lookup(R);
213 return V ? *V : UnknownVal();
214 }
215
216 case loc::SymbolValKind:
217 return UnknownVal();
218
219 case loc::ConcreteIntKind:
220 return UndefinedVal(); // As in BasicStoreManager.
221
222 case loc::FuncValKind:
223 return L;
224
225 case loc::StringLiteralValKind:
226 return UnknownVal();
227
228 default:
229 assert(false && "Invalid Location");
230 break;
231 }
232}
233
Zhongxing Xu73249322008-10-21 06:27:32 +0000234Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu097fc982008-10-17 05:57:07 +0000235 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000236
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000237 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000238
239 if (!R)
240 return store;
241
242 RegionBindingsTy B = GetRegionBindings(store);
243 return V.isUnknown()
244 ? RBFactory.Remove(B, R).getRoot()
245 : RBFactory.Add(B, R, V).getRoot();
246}
247
248Store RegionStoreManager::getInitialStore() {
249 typedef LiveVariables::AnalysisDataTy LVDataTy;
250 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
251
252 Store St = RBFactory.GetEmptyMap().getRoot();
253
254 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000255 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000256
Douglas Gregord2baafd2008-10-21 16:13:35 +0000257 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000258 // Punt on static variables for now.
259 if (VD->getStorageClass() == VarDecl::Static)
260 continue;
261
262 QualType T = VD->getType();
263 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000264 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000265 // Initialize globals and parameters to symbolic values.
266 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000267 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000268 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000269 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000270 : UndefinedVal();
271
Zhongxing Xu73249322008-10-21 06:27:32 +0000272 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000273 }
274 }
275 }
276 return St;
277}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000278
279Store RegionStoreManager::AddDecl(Store store,
280 const VarDecl* VD, Expr* Ex,
281 SVal InitVal, unsigned Count) {
282 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
283 SymbolManager& SymMgr = StateMgr.getSymbolManager();
284
285 if (VD->hasGlobalStorage()) {
286 // Static global variables should not be visited here.
287 assert(!(VD->getStorageClass() == VarDecl::Static &&
288 VD->isFileVarDecl()));
289 // Process static variables.
290 if (VD->getStorageClass() == VarDecl::Static) {
291 if (!Ex) {
292 // Only handle pointer and integer static variables.
293
294 QualType T = VD->getType();
295
296 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000297 store = Bind(store, getVarLoc(VD),
298 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000299
300 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000301 store = Bind(store, getVarLoc(VD),
302 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000303 else
304 assert("ignore other types of variables");
305 } else {
Zhongxing Xu73249322008-10-21 06:27:32 +0000306 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000307 }
308 }
309 } else {
310 // Process local variables.
311
312 QualType T = VD->getType();
313
Zhongxing Xu702d4702008-10-24 08:42:28 +0000314 VarRegion* VR = MRMgr.getVarRegion(VD);
315
Zhongxing Xue3954d12008-10-21 05:29:26 +0000316 if (Loc::IsLocType(T) || T->isIntegerType()) {
317 SVal V = Ex ? InitVal : UndefinedVal();
318 if (Ex && InitVal.isUnknown()) {
319 // "Conjured" symbols.
320 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
321 V = Loc::IsLocType(Ex->getType())
322 ? cast<SVal>(loc::SymbolVal(Sym))
323 : cast<SVal>(nonloc::SymbolVal(Sym));
324 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000325 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000326
327 } else if (T->isArrayType()) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000328 store = InitializeArrayToUndefined(store, T, VR);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000329
Zhongxing Xue3954d12008-10-21 05:29:26 +0000330 } else if (T->isStructureType()) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000331 store = InitializeStructToUndefined(store, T, VR);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000332 }
333 }
334 return store;
335}
336
Zhongxing Xuca892b82008-10-24 06:01:33 +0000337void RegionStoreManager::print(Store store, std::ostream& Out,
338 const char* nl, const char *sep) {
339 llvm::raw_os_ostream OS(Out);
340 RegionBindingsTy B = GetRegionBindings(store);
341 OS << "Store:" << nl;
342
343 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
344 OS << ' '; I.getKey()->print(OS); OS << " : ";
345 I.getData().print(OS); OS << nl;
346 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000347}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000348
349Store RegionStoreManager::InitializeArrayToUndefined(Store store, QualType T,
350 MemRegion* BaseR) {
351 assert(T->isArrayType());
352
353 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
354
355 // Only handle constant size array for now.
356 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
357
358 llvm::APInt Size = CAT->getSize();
359
360 for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
361 i != Size; ++i) {
362 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
363
364 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
365
366 store = Bind(store, loc::MemRegionVal(ER), UndefinedVal());
367 }
368 }
369
370 return store;
371}
372
373Store RegionStoreManager::InitializeStructToUndefined(Store store, QualType T,
374 MemRegion* BaseR) {
375 const RecordType* RT = cast<RecordType>(T.getTypePtr());
376 RecordDecl* RD = RT->getDecl();
377 assert(RD->isDefinition());
378
379 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
380 I != E; ++I) {
381
382 QualType FTy = (*I)->getType();
383 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
384
385 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
386 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
387
388 } else if (FTy->isArrayType()) {
389 store = InitializeArrayToUndefined(store, FTy, FR);
390
391 } else if (FTy->isStructureType()) {
392 store = InitializeStructToUndefined(store, FTy, FR);
393 }
394 }
395
396 return store;
397}