blob: 9a1f3eca34468e8576419f3c2b27d077435c081a [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"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xubaf03a72008-11-24 09:44:56 +000029// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000030typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000031
32// RegionView GDM stuff.
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000033typedef llvm::ImmutableList<const MemRegion*> RegionViewTy;
34typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000035static int RegionViewMapTyIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000036namespace clang {
37template<> struct GRStateTrait<RegionViewMapTy>
38 : public GRStatePartialTrait<RegionViewMapTy> {
39 static void* GDMIndex() { return &RegionViewMapTyIndex; }
40};
41}
Zhongxing Xu17892752008-10-08 02:50:44 +000042
Zhongxing Xubaf03a72008-11-24 09:44:56 +000043// RegionExtents GDM stuff.
44// Currently RegionExtents are in bytes. We can change this representation when
45// there are real requirements.
46typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionExtentsTy;
47static int RegionExtentsTyIndex = 0;
48namespace clang {
49template<> struct GRStateTrait<RegionExtentsTy>
50 : public GRStatePartialTrait<RegionExtentsTy> {
51 static void* GDMIndex() { return &RegionExtentsTyIndex; }
52};
53}
54
Zhongxing Xu17892752008-10-08 02:50:44 +000055namespace {
56
57class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
58 RegionBindingsTy::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000059 RegionViewTy::Factory RVFactory;
60 RegionViewMapTy::Factory RVMFactory;
61
Zhongxing Xu17892752008-10-08 02:50:44 +000062 GRStateManager& StateMgr;
63 MemRegionManager MRMgr;
64
65public:
66 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000067 : RBFactory(mgr.getAllocator()),
68 RVFactory(mgr.getAllocator()),
69 RVMFactory(mgr.getAllocator()),
70 StateMgr(mgr),
71 MRMgr(StateMgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +000072
73 virtual ~RegionStoreManager() {}
74
Zhongxing Xu24194ef2008-10-24 01:38:55 +000075 MemRegionManager& getRegionManager() { return MRMgr; }
76
77 // FIXME: Is this function necessary?
78 SVal GetRegionSVal(Store St, const MemRegion* R) {
79 return Retrieve(St, loc::MemRegionVal(R));
80 }
Ted Kremenek4f090272008-10-27 21:54:31 +000081
Zhongxing Xuf22679e2008-11-07 10:38:33 +000082 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000083
Zhongxing Xu143bf822008-10-25 14:18:57 +000084 SVal getLValueString(const GRState* St, const StringLiteral* S);
85
Zhongxing Xuf22679e2008-11-07 10:38:33 +000086 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
87
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000088 SVal getLValueVar(const GRState* St, const VarDecl* VD);
89
90 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
91
92 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
93
Zhongxing Xub1d542a2008-10-24 01:09:32 +000094 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
95
Zhongxing Xue8a964b2008-11-22 13:21:46 +000096 SVal getSizeInElements(const GRState* St, const MemRegion* R);
97
Zhongxing Xub1d542a2008-10-24 01:09:32 +000098 SVal ArrayToPointer(SVal Array);
99
Zhongxing Xucb529b52008-11-16 07:06:26 +0000100 std::pair<const GRState*, SVal>
101 CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000102
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000103 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000104
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000105 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000106
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000107 Store Remove(Store store, Loc LV) {
108 // FIXME: Implement.
109 return store;
110 }
111
Zhongxing Xu17892752008-10-08 02:50:44 +0000112 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000113
114 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
115 /// 'this' object (C++). When used when analyzing a normal function this
116 /// method returns NULL.
117 const MemRegion* getSelfRegion(Store) {
118 assert (false && "Not implemented.");
119 return 0;
120 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000121
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000122 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
123 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000124 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000125
Ted Kremenek42577d12008-11-12 19:18:35 +0000126 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000127
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000128 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
129
Zhongxing Xu17892752008-10-08 02:50:44 +0000130 static inline RegionBindingsTy GetRegionBindings(Store store) {
131 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
132 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000133
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000134 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000135
136 void iterBindings(Store store, BindingsHandler& f) {
137 // FIXME: Implement.
138 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000139
140private:
141 Loc getVarLoc(const VarDecl* VD) {
142 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
143 }
144
Zhongxing Xud463d442008-11-02 12:13:30 +0000145 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
146 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000147 Store BindArrayToSymVal(Store store, const TypedRegion* BaseR);
148
Zhongxing Xud463d442008-11-02 12:13:30 +0000149 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
150 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000151 Store BindStructToSymVal(Store store, const TypedRegion* BaseR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000152
153 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000154 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000155
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000156 // Utility methods.
157 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
158 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000159 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000160
161 const GRState* AddRegionView(const GRState* St,
162 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000163};
164
165} // end anonymous namespace
166
Ted Kremenek95c7b002008-10-24 01:04:59 +0000167StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000168 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000169}
170
Zhongxing Xu143bf822008-10-25 14:18:57 +0000171SVal RegionStoreManager::getLValueString(const GRState* St,
172 const StringLiteral* S) {
173 return loc::MemRegionVal(MRMgr.getStringRegion(S));
174}
175
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000176SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
177 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
178}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000179
180SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
181 const CompoundLiteralExpr* CL) {
182 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
183}
184
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000185SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
186 SVal Base) {
187 return UnknownVal();
188}
189
190SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
191 const FieldDecl* D) {
192 if (Base.isUnknownOrUndef())
193 return Base;
194
195 Loc BaseL = cast<Loc>(Base);
196 const MemRegion* BaseR = 0;
197
198 switch (BaseL.getSubKind()) {
199 case loc::MemRegionKind:
200 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
201 break;
202
203 case loc::SymbolValKind:
204 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
205 break;
206
207 case loc::GotoLabelKind:
208 case loc::FuncValKind:
209 // These are anormal cases. Flag an undefined value.
210 return UndefinedVal();
211
212 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000213 // While these seem funny, this can happen through casts.
214 // FIXME: What we should return is the field offset. For example,
215 // add the field offset to the integer value. That way funny things
216 // like this work properly: &(((struct foo *) 0xa)->f)
217 return Base;
218
219 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000220 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000221 return Base;
222 }
223
224 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
225}
226
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000227SVal RegionStoreManager::getLValueElement(const GRState* St,
228 SVal Base, SVal Offset) {
229 if (Base.isUnknownOrUndef())
230 return Base;
231
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000232 if (isa<loc::SymbolVal>(Base))
233 return Base;
234
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000235 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
236
Zhongxing Xue4d13932008-11-13 09:48:44 +0000237 // Pointer of any type can be cast and used as array base. We do not support
238 // that case yet.
239 if (!isa<ElementRegion>(BaseL.getRegion())) {
240 // Record what we have seen in real code.
241 assert(isa<FieldRegion>(BaseL.getRegion()));
242 return UnknownVal();
243 }
244
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000245 // We expect BaseR is an ElementRegion, not a base VarRegion.
246
247 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
248
249 SVal Idx = ElemR->getIndex();
250
251 nonloc::ConcreteInt *CI1, *CI2;
252
253 // Only handle integer indices for now.
254 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
255 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000256
257 // Temporary SVal to hold a potential signed APSInt.
258 SVal SignedInt;
259
260 // Index might be unsigned. We have to convert it to signed.
261 if (CI2->getValue().isUnsigned()) {
262 llvm::APSInt SI = CI2->getValue();
263 SI.setIsSigned(true);
264 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
265 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
266 }
267
Zhongxing Xu63123d82008-11-23 04:30:35 +0000268 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000269 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
270 ElemR->getSuperRegion()));
271 }
272
273 return UnknownVal();
274}
275
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000276SVal RegionStoreManager::getSizeInElements(const GRState* St,
277 const MemRegion* R) {
278 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
279 // Get the type of the variable.
280 QualType T = VR->getType(getContext());
281
282 // It must be of array type.
283 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
284
285 // return the size as signed integer.
286 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
287 }
288
289 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000290 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000291 // We intentionally made the size value signed because it participates in
292 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000293 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000294 }
295
296 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000297 GRStateRef state(St, StateMgr);
298
299 // Get the size of the super region in bytes.
300 RegionExtentsTy::data_type* T
301 = state.get<RegionExtentsTy>(ATR->getSuperRegion());
302
303 assert(T && "region extent not exist");
304
305 // Assume it's ConcreteInt for now.
306 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*T).getValue();
307
308 // Get the size of the element in bits.
309 QualType ElemTy = cast<PointerType>(ATR->getType(getContext()).getTypePtr())
310 ->getPointeeType();
311
312 uint64_t X = getContext().getTypeSize(ElemTy);
313
314 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
315 false);
316
317 // Calculate the number of elements.
318
319 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
320 // signed?
321 if (SSize.isUnsigned())
322 SSize.setIsSigned(true);
323
324 // FIXME: move this operation into BasicVals.
325 const llvm::APSInt S =
326 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
327
328 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000329 }
330
331 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
332 // FIXME: Unsupported yet.
333 FR = 0;
334 return UnknownVal();
335 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000336
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000337 assert(0 && "Other regions are not supported yet.");
338}
339
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000340// Cast 'pointer to array' to 'pointer to the first element of array'.
341
342SVal RegionStoreManager::ArrayToPointer(SVal Array) {
343 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000344
Zhongxing Xu63123d82008-11-23 04:30:35 +0000345 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000346 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
347
348 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000349}
350
Zhongxing Xucb529b52008-11-16 07:06:26 +0000351std::pair<const GRState*, SVal>
352RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr,
353 QualType CastToTy, Stmt* CastE) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000354 if (const AllocaRegion* AR =
355 dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) {
356
357 // Create a new region to attach type information to it.
358 const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR);
359
360 // Get the pointer to the first element.
361 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
362 const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR);
363
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000364 // Add a RegionView to base region.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000365 return std::pair<const GRState*, SVal>(AddRegionView(St, TR, AR),
366 loc::MemRegionVal(ER));
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000367 }
368
369 // Default case.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000370 return std::pair<const GRState*, SVal>(St, UnknownVal());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000371}
372
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000373SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000374 assert(!isa<UnknownVal>(L) && "location unknown");
375 assert(!isa<UndefinedVal>(L) && "location undefined");
376
377 switch (L.getSubKind()) {
378 case loc::MemRegionKind: {
379 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
380 assert(R && "bad region");
381
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000382 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
383 if (TR->getType(getContext())->isStructureType())
384 return RetrieveStruct(S, TR);
385
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000386 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
387 RegionBindingsTy::data_type* V = B.lookup(R);
388 return V ? *V : UnknownVal();
389 }
390
391 case loc::SymbolValKind:
392 return UnknownVal();
393
394 case loc::ConcreteIntKind:
395 return UndefinedVal(); // As in BasicStoreManager.
396
397 case loc::FuncValKind:
398 return L;
399
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000400 default:
401 assert(false && "Invalid Location");
Ted Kremenekab7b32b2008-11-19 00:27:37 +0000402 return L;
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000403 }
404}
405
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000406SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
407 QualType T = R->getType(getContext());
408 assert(T->isStructureType());
409
410 const RecordType* RT = cast<RecordType>(T.getTypePtr());
411 RecordDecl* RD = RT->getDecl();
412 assert(RD->isDefinition());
413
414 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
415
416 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
417 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
418 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000419 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000420
421 SVal FieldValue = data ? *data : UnknownVal();
422
423 StructVal = getBasicVals().consVals(FieldValue, StructVal);
424 }
425
426 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
427}
428
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000429Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000430 if (LV.getSubKind() == loc::SymbolValKind)
431 return store;
432
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000433 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000434
Ted Kremenek993f1c72008-10-17 20:28:54 +0000435 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000436
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000437 assert(R);
438
439 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
440 if (TR->getType(getContext())->isStructureType())
441 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000442
443 RegionBindingsTy B = GetRegionBindings(store);
444 return V.isUnknown()
445 ? RBFactory.Remove(B, R).getRoot()
446 : RBFactory.Add(B, R, V).getRoot();
447}
448
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000449Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
450 QualType T = R->getType(getContext());
451 assert(T->isStructureType());
452
453 const RecordType* RT = cast<RecordType>(T.getTypePtr());
454 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000455
456 if (!RD->isDefinition()) {
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000457 // This can only occur when a pointer of incomplete struct type is used as a
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000458 // function argument.
459 assert(V.isUnknown());
460 return store;
461 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000462
463 RegionBindingsTy B = GetRegionBindings(store);
464
Zhongxing Xud463d442008-11-02 12:13:30 +0000465 if (isa<UnknownVal>(V))
466 return BindStructToVal(store, R, UnknownVal());
467
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000468 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
469
470 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
471 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
472
473 for (; FI != FE; ++FI, ++VI) {
474 assert(VI != VE);
475
476 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
477
478 B = RBFactory.Add(B, FR, *VI);
479 }
480
481 return B.getRoot();
482}
483
Zhongxing Xu17892752008-10-08 02:50:44 +0000484Store RegionStoreManager::getInitialStore() {
485 typedef LiveVariables::AnalysisDataTy LVDataTy;
486 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
487
488 Store St = RBFactory.GetEmptyMap().getRoot();
489
490 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000491 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000492
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000493 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000494 // Punt on static variables for now.
495 if (VD->getStorageClass() == VarDecl::Static)
496 continue;
497
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000498 VarRegion* VR = MRMgr.getVarRegion(VD);
499
Zhongxing Xu17892752008-10-08 02:50:44 +0000500 QualType T = VD->getType();
501 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000502 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000503 // Initialize globals and parameters to symbolic values.
504 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000505 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000506 isa<ImplicitParamDecl>(VD))
Zhongxing Xu63123d82008-11-23 04:30:35 +0000507 ? SVal::GetSymbolValue(getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000508 : UndefinedVal();
509
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000510 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000511 }
512 else if (T->isArrayType()) {
513 if (VD->hasGlobalStorage()) // Params cannot have array type.
514 St = BindArrayToSymVal(St, VR);
515 else
516 St = BindArrayToVal(St, VR, UndefinedVal());
517 }
518 else if (T->isStructureType()) {
519 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
520 isa<ImplicitParamDecl>(VD))
521 St = BindStructToSymVal(St, VR);
522 else
523 St = BindStructToVal(St, VR, UndefinedVal());
Zhongxing Xu17892752008-10-08 02:50:44 +0000524 }
525 }
526 }
527 return St;
528}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000529
Ted Kremenek42577d12008-11-12 19:18:35 +0000530Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
531 SVal* InitVal, unsigned Count) {
532
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000533 if (VD->hasGlobalStorage()) {
534 // Static global variables should not be visited here.
535 assert(!(VD->getStorageClass() == VarDecl::Static &&
536 VD->isFileVarDecl()));
537 // Process static variables.
538 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000539 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000540 // Only handle pointer and integer static variables.
541
542 QualType T = VD->getType();
543
544 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000545 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000546 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000547
548 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000549 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000550 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000551
552 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000553 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000554 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000555 }
556 }
557 } else {
558 // Process local variables.
559
560 QualType T = VD->getType();
561
Zhongxing Xua82512a2008-10-24 08:42:28 +0000562 VarRegion* VR = MRMgr.getVarRegion(VD);
563
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000564 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000565 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000566 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000567 }
568 else if (T->isArrayType()) {
569 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000570 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000571 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000572 store = InitializeArray(store, VR, *InitVal);
573 }
574 else if (T->isStructureType()) {
575 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000576 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000577 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000578 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000579 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000580
581 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000582 }
583 return store;
584}
585
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000586Store RegionStoreManager::BindCompoundLiteral(Store store,
587 const CompoundLiteralExpr* CL,
588 SVal V) {
589 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
590 store = Bind(store, loc::MemRegionVal(R), V);
591 return store;
592}
593
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000594const GRState* RegionStoreManager::setExtent(const GRState* St,
595 const MemRegion* R, SVal Extent) {
596 GRStateRef state(St, StateMgr);
597 return state.set<RegionExtentsTy>(R, Extent);
598}
599
600
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000601Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
602 const LiveVariables& Live,
603 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
604 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
605
606 RegionBindingsTy B = GetRegionBindings(store);
607 typedef SVal::symbol_iterator symbol_iterator;
608
609 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
610 // in SymbolicRegions.
611 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
612 SVal X = I.getData();
613 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
614 LSymbols.insert(*SI);
615 }
616
617 return store;
618}
619
Zhongxing Xua071eb02008-10-24 06:01:33 +0000620void RegionStoreManager::print(Store store, std::ostream& Out,
621 const char* nl, const char *sep) {
622 llvm::raw_os_ostream OS(Out);
623 RegionBindingsTy B = GetRegionBindings(store);
624 OS << "Store:" << nl;
625
626 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
627 OS << ' '; I.getKey()->print(OS); OS << " : ";
628 I.getData().print(OS); OS << nl;
629 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000630}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000631
Zhongxing Xud463d442008-11-02 12:13:30 +0000632Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000633 SVal Init) {
634 QualType T = R->getType(getContext());
635 assert(T->isArrayType());
636
637 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
638
639 llvm::APInt Size = CAT->getSize();
640
641 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
642
643 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
644
645 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
646
647 for (; i != Size; ++i) {
648 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
649
650 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
651
652 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
653 // The init list might be shorter than the array decl.
654 if (VI != VE) ++VI;
655 }
656
657 return store;
658}
659
Zhongxing Xud463d442008-11-02 12:13:30 +0000660// Bind all elements of the array to some value.
661Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
662 SVal V){
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000663 QualType T = BaseR->getType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000664 assert(T->isArrayType());
665
Zhongxing Xua82512a2008-10-24 08:42:28 +0000666 // Only handle constant size array for now.
667 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
668
669 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000670 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
671 for (; i != Size; ++i) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000672 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000673
674 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
675
Zhongxing Xu9b6ceb12008-11-18 13:11:04 +0000676 if (CAT->getElementType()->isStructureType())
677 store = BindStructToVal(store, ER, V);
678 else
679 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000680 }
681 }
682
683 return store;
684}
685
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000686Store RegionStoreManager::BindArrayToSymVal(Store store,
687 const TypedRegion* BaseR) {
688 QualType T = BaseR->getType(getContext());
689 assert(T->isArrayType());
690
691 if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) {
692 llvm::APInt Size = CAT->getSize();
693 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
694 for (; i != Size; ++i) {
695 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
696
697 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
698
699 if (CAT->getElementType()->isStructureType()) {
700 store = BindStructToSymVal(store, ER);
701 }
702 else {
703 SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR,
704 &Idx.getValue(), CAT->getElementType());
705 store = Bind(store, loc::MemRegionVal(ER), V);
706 }
707 }
708 }
709
710 return store;
711}
712
Zhongxing Xud463d442008-11-02 12:13:30 +0000713Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000714 SVal Init) {
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000715 QualType T = R->getType(getContext());
716 assert(T->isStructureType());
717
718 RecordType* RT = cast<RecordType>(T.getTypePtr());
719 RecordDecl* RD = RT->getDecl();
720 assert(RD->isDefinition());
721
722 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
723 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
724 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
725
726 for (; FI != FE; ++FI) {
727 QualType FTy = (*FI)->getType();
728 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
729
730 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
731 if (VI != VE) {
732 store = Bind(store, loc::MemRegionVal(FR), *VI);
733 ++VI;
734 } else
735 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
736 }
737 else if (FTy->isArrayType()) {
738 if (VI != VE) {
739 store = InitializeArray(store, FR, *VI);
740 ++VI;
741 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000742 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000743 }
744 else if (FTy->isStructureType()) {
745 if (VI != VE) {
746 store = InitializeStruct(store, FR, *VI);
747 ++VI;
748 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000749 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000750 }
751 }
752 return store;
753}
754
Zhongxing Xud463d442008-11-02 12:13:30 +0000755// Bind all fields of the struct to some value.
756Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
757 SVal V) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000758 QualType T = BaseR->getType(getContext());
759 assert(T->isStructureType());
760
761 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000762 RecordDecl* RD = RT->getDecl();
763 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000764
765 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
766
767 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000768
769 QualType FTy = (*I)->getType();
770 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
771
772 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000773 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000774
775 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000776 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000777
778 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000779 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000780 }
781 }
782
783 return store;
784}
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000785
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000786Store RegionStoreManager::BindStructToSymVal(Store store,
787 const TypedRegion* BaseR) {
788 QualType T = BaseR->getType(getContext());
789 assert(T->isStructureType());
790
791 const RecordType* RT = cast<RecordType>(T.getTypePtr());
792 RecordDecl* RD = RT->getDecl();
793 assert(RD->isDefinition());
794
795 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
796
797 for (; I != E; ++I) {
798 QualType FTy = (*I)->getType();
799 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
800
801 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
802 store = Bind(store, loc::MemRegionVal(FR),
803 SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy));
804 }
805 else if (FTy->isArrayType()) {
806 store = BindArrayToSymVal(store, FR);
807 }
808 else if (FTy->isStructureType()) {
809 store = BindStructToSymVal(store, FR);
810 }
811 }
812
813 return store;
814}
815
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000816const GRState* RegionStoreManager::AddRegionView(const GRState* St,
817 const MemRegion* View,
818 const MemRegion* Base) {
819 GRStateRef state(St, StateMgr);
820
821 // First, retrieve the region view of the base region.
822 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
823 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
824
825 // Now add View to the region view.
826 L = RVFactory.Add(View, L);
827
828 // Create a new state with the new region view.
829 return state.set<RegionViewMapTy>(Base, L);
830}