blob: 5c75ab369ece0ba251b16961e17edd116a708586 [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 Xuc4bf72c2008-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 Xub1d542a2008-10-24 01:09:32 +000055 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
56
57 SVal ArrayToPointer(SVal Array);
58
Zhongxing Xu24194ef2008-10-24 01:38:55 +000059 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000060
Zhongxing Xu8485ec62008-10-21 06:27:32 +000061 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +000062
Zhongxing Xu24194ef2008-10-24 01:38:55 +000063 Store Remove(Store store, Loc LV) {
64 // FIXME: Implement.
65 return store;
66 }
67
Zhongxing Xu17892752008-10-08 02:50:44 +000068 Store getInitialStore();
69
Zhongxing Xu24194ef2008-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 Xu53bcdd42008-10-21 05:29:26 +000077 Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal,
78 unsigned Count);
79
Zhongxing Xu17892752008-10-08 02:50:44 +000080 static inline RegionBindingsTy GetRegionBindings(Store store) {
81 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
82 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +000083
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +000084 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000085
86 void iterBindings(Store store, BindingsHandler& f) {
87 // FIXME: Implement.
88 }
Zhongxing Xua82512a2008-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 Xu17892752008-10-08 02:50:44 +000097};
98
99} // end anonymous namespace
100
Ted Kremenek95c7b002008-10-24 01:04:59 +0000101StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000102 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000103}
104
Zhongxing Xuc4bf72c2008-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 Xub1d542a2008-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
Zhongxing Xubfb65822008-10-24 09:06:51 +0000184 const Decl* D = cast<DeclRegion>(ArrayR)->getDecl();
185
186 QualType ArrayTy;
187 if (const VarDecl* VD = dyn_cast<VarDecl>(D))
188 ArrayTy = VD->getType();
189 else if (const FieldDecl* FD = dyn_cast<FieldDecl>(D))
190 ArrayTy = FD->getType();
191 else
192 assert(0 && "unknown decl");
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000193
194 if (const ConstantArrayType* CAT =
Zhongxing Xubfb65822008-10-24 09:06:51 +0000195 dyn_cast<ConstantArrayType>(ArrayTy.getTypePtr())) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000196
197 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
198
199 nonloc::ConcreteInt Idx(BasicVals.getValue(0, CAT->getSize().getBitWidth(),
200 false));
201
202 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
203
204 return loc::MemRegionVal(ER);
205 }
206
207 return Array;
208}
209
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000210SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000211 assert(!isa<UnknownVal>(L) && "location unknown");
212 assert(!isa<UndefinedVal>(L) && "location undefined");
213
214 switch (L.getSubKind()) {
215 case loc::MemRegionKind: {
216 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
217 assert(R && "bad region");
218
219 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
220 RegionBindingsTy::data_type* V = B.lookup(R);
221 return V ? *V : UnknownVal();
222 }
223
224 case loc::SymbolValKind:
225 return UnknownVal();
226
227 case loc::ConcreteIntKind:
228 return UndefinedVal(); // As in BasicStoreManager.
229
230 case loc::FuncValKind:
231 return L;
232
233 case loc::StringLiteralValKind:
234 return UnknownVal();
235
236 default:
237 assert(false && "Invalid Location");
238 break;
239 }
240}
241
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000242Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000243 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000244
Ted Kremenek993f1c72008-10-17 20:28:54 +0000245 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000246
247 if (!R)
248 return store;
249
250 RegionBindingsTy B = GetRegionBindings(store);
251 return V.isUnknown()
252 ? RBFactory.Remove(B, R).getRoot()
253 : RBFactory.Add(B, R, V).getRoot();
254}
255
256Store RegionStoreManager::getInitialStore() {
257 typedef LiveVariables::AnalysisDataTy LVDataTy;
258 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
259
260 Store St = RBFactory.GetEmptyMap().getRoot();
261
262 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000263 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000264
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000265 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000266 // Punt on static variables for now.
267 if (VD->getStorageClass() == VarDecl::Static)
268 continue;
269
270 QualType T = VD->getType();
271 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000272 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000273 // Initialize globals and parameters to symbolic values.
274 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000275 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000276 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000277 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000278 : UndefinedVal();
279
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000280 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu17892752008-10-08 02:50:44 +0000281 }
282 }
283 }
284 return St;
285}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000286
287Store RegionStoreManager::AddDecl(Store store,
288 const VarDecl* VD, Expr* Ex,
289 SVal InitVal, unsigned Count) {
290 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
291 SymbolManager& SymMgr = StateMgr.getSymbolManager();
292
293 if (VD->hasGlobalStorage()) {
294 // Static global variables should not be visited here.
295 assert(!(VD->getStorageClass() == VarDecl::Static &&
296 VD->isFileVarDecl()));
297 // Process static variables.
298 if (VD->getStorageClass() == VarDecl::Static) {
299 if (!Ex) {
300 // Only handle pointer and integer static variables.
301
302 QualType T = VD->getType();
303
304 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000305 store = Bind(store, getVarLoc(VD),
306 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000307
308 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000309 store = Bind(store, getVarLoc(VD),
310 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000311 else
312 assert("ignore other types of variables");
313 } else {
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000314 store = Bind(store, getVarLoc(VD), InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000315 }
316 }
317 } else {
318 // Process local variables.
319
320 QualType T = VD->getType();
321
Zhongxing Xua82512a2008-10-24 08:42:28 +0000322 VarRegion* VR = MRMgr.getVarRegion(VD);
323
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000324 if (Loc::IsLocType(T) || T->isIntegerType()) {
325 SVal V = Ex ? InitVal : UndefinedVal();
326 if (Ex && InitVal.isUnknown()) {
327 // "Conjured" symbols.
328 SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count);
329 V = Loc::IsLocType(Ex->getType())
330 ? cast<SVal>(loc::SymbolVal(Sym))
331 : cast<SVal>(nonloc::SymbolVal(Sym));
332 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000333 store = Bind(store, loc::MemRegionVal(VR), V);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000334
335 } else if (T->isArrayType()) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000336 store = InitializeArrayToUndefined(store, T, VR);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000337
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000338 } else if (T->isStructureType()) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000339 store = InitializeStructToUndefined(store, T, VR);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000340 }
341 }
342 return store;
343}
344
Zhongxing Xua071eb02008-10-24 06:01:33 +0000345void RegionStoreManager::print(Store store, std::ostream& Out,
346 const char* nl, const char *sep) {
347 llvm::raw_os_ostream OS(Out);
348 RegionBindingsTy B = GetRegionBindings(store);
349 OS << "Store:" << nl;
350
351 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
352 OS << ' '; I.getKey()->print(OS); OS << " : ";
353 I.getData().print(OS); OS << nl;
354 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000355}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000356
357Store RegionStoreManager::InitializeArrayToUndefined(Store store, QualType T,
358 MemRegion* BaseR) {
359 assert(T->isArrayType());
360
361 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
362
363 // Only handle constant size array for now.
364 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
365
366 llvm::APInt Size = CAT->getSize();
367
368 for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
369 i != Size; ++i) {
370 nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i)));
371
372 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
373
374 store = Bind(store, loc::MemRegionVal(ER), UndefinedVal());
375 }
376 }
377
378 return store;
379}
380
381Store RegionStoreManager::InitializeStructToUndefined(Store store, QualType T,
382 MemRegion* BaseR) {
383 const RecordType* RT = cast<RecordType>(T.getTypePtr());
384 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}