blob: 90baf2df92e673bda6be2a448a1fb1d28a76354e [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;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000060
Zhongxing Xu17892752008-10-08 02:50:44 +000061 GRStateManager& StateMgr;
62 MemRegionManager MRMgr;
63
64public:
65 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000066 : RBFactory(mgr.getAllocator()),
67 RVFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000068 StateMgr(mgr),
69 MRMgr(StateMgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +000070
71 virtual ~RegionStoreManager() {}
72
Zhongxing Xu24194ef2008-10-24 01:38:55 +000073 MemRegionManager& getRegionManager() { return MRMgr; }
74
75 // FIXME: Is this function necessary?
76 SVal GetRegionSVal(Store St, const MemRegion* R) {
77 return Retrieve(St, loc::MemRegionVal(R));
78 }
Ted Kremenek4f090272008-10-27 21:54:31 +000079
Zhongxing Xuf22679e2008-11-07 10:38:33 +000080 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000081
Zhongxing Xu143bf822008-10-25 14:18:57 +000082 SVal getLValueString(const GRState* St, const StringLiteral* S);
83
Zhongxing Xuf22679e2008-11-07 10:38:33 +000084 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
85
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000086 SVal getLValueVar(const GRState* St, const VarDecl* VD);
87
88 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
89
90 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
91
Zhongxing Xub1d542a2008-10-24 01:09:32 +000092 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
93
Zhongxing Xue8a964b2008-11-22 13:21:46 +000094 SVal getSizeInElements(const GRState* St, const MemRegion* R);
95
Zhongxing Xub1d542a2008-10-24 01:09:32 +000096 SVal ArrayToPointer(SVal Array);
97
Zhongxing Xucb529b52008-11-16 07:06:26 +000098 std::pair<const GRState*, SVal>
99 CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000100
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000101 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000102
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000103 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000104
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000105 Store Remove(Store store, Loc LV) {
106 // FIXME: Implement.
107 return store;
108 }
109
Zhongxing Xu17892752008-10-08 02:50:44 +0000110 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000111
112 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
113 /// 'this' object (C++). When used when analyzing a normal function this
114 /// method returns NULL.
115 const MemRegion* getSelfRegion(Store) {
116 assert (false && "Not implemented.");
117 return 0;
118 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000119
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000120 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
121 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000122 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000123
Ted Kremenek42577d12008-11-12 19:18:35 +0000124 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000125
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000126 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
127
Zhongxing Xu17892752008-10-08 02:50:44 +0000128 static inline RegionBindingsTy GetRegionBindings(Store store) {
129 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
130 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000131
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000132 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000133
134 void iterBindings(Store store, BindingsHandler& f) {
135 // FIXME: Implement.
136 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000137
138private:
139 Loc getVarLoc(const VarDecl* VD) {
140 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
141 }
142
Zhongxing Xud463d442008-11-02 12:13:30 +0000143 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
144 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000145 Store BindArrayToSymVal(Store store, const TypedRegion* BaseR);
146
Zhongxing Xud463d442008-11-02 12:13:30 +0000147 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
148 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000149 Store BindStructToSymVal(Store store, const TypedRegion* BaseR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000150
151 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000152 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000153
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000154 // Utility methods.
155 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
156 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000157 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000158
159 const GRState* AddRegionView(const GRState* St,
160 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000161};
162
163} // end anonymous namespace
164
Ted Kremenek95c7b002008-10-24 01:04:59 +0000165StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000166 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000167}
168
Zhongxing Xu143bf822008-10-25 14:18:57 +0000169SVal RegionStoreManager::getLValueString(const GRState* St,
170 const StringLiteral* S) {
171 return loc::MemRegionVal(MRMgr.getStringRegion(S));
172}
173
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000174SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
175 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
176}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000177
178SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
179 const CompoundLiteralExpr* CL) {
180 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
181}
182
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000183SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
184 SVal Base) {
185 return UnknownVal();
186}
187
188SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
189 const FieldDecl* D) {
190 if (Base.isUnknownOrUndef())
191 return Base;
192
193 Loc BaseL = cast<Loc>(Base);
194 const MemRegion* BaseR = 0;
195
196 switch (BaseL.getSubKind()) {
197 case loc::MemRegionKind:
198 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
199 break;
200
201 case loc::SymbolValKind:
202 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
203 break;
204
205 case loc::GotoLabelKind:
206 case loc::FuncValKind:
207 // These are anormal cases. Flag an undefined value.
208 return UndefinedVal();
209
210 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000211 // While these seem funny, this can happen through casts.
212 // FIXME: What we should return is the field offset. For example,
213 // add the field offset to the integer value. That way funny things
214 // like this work properly: &(((struct foo *) 0xa)->f)
215 return Base;
216
217 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000218 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000219 return Base;
220 }
221
222 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
223}
224
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000225SVal RegionStoreManager::getLValueElement(const GRState* St,
226 SVal Base, SVal Offset) {
227 if (Base.isUnknownOrUndef())
228 return Base;
229
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000230 if (isa<loc::SymbolVal>(Base))
231 return Base;
232
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000233 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
234
Zhongxing Xue4d13932008-11-13 09:48:44 +0000235 // Pointer of any type can be cast and used as array base. We do not support
236 // that case yet.
237 if (!isa<ElementRegion>(BaseL.getRegion())) {
238 // Record what we have seen in real code.
239 assert(isa<FieldRegion>(BaseL.getRegion()));
240 return UnknownVal();
241 }
242
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000243 // We expect BaseR is an ElementRegion, not a base VarRegion.
244
245 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
246
247 SVal Idx = ElemR->getIndex();
248
249 nonloc::ConcreteInt *CI1, *CI2;
250
251 // Only handle integer indices for now.
252 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
253 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000254
Sebastian Redle95db4f2008-11-24 19:35:33 +0000255 // Temporary SVal to hold a potential signed and extended APSInt.
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000256 SVal SignedInt;
257
Sebastian Redle95db4f2008-11-24 19:35:33 +0000258 // Index might be unsigned. We have to convert it to signed. It might also
259 // be less wide than the size. We have to extend it.
260 if (CI2->getValue().isUnsigned() ||
261 CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000262 llvm::APSInt SI = CI2->getValue();
Sebastian Redlddee68b2008-11-24 19:39:40 +0000263 if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth())
264 SI.extend(CI1->getValue().getBitWidth());
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000265 SI.setIsSigned(true);
266 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
267 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
268 }
269
Zhongxing Xu63123d82008-11-23 04:30:35 +0000270 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000271 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
272 ElemR->getSuperRegion()));
273 }
274
275 return UnknownVal();
276}
277
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000278SVal RegionStoreManager::getSizeInElements(const GRState* St,
279 const MemRegion* R) {
280 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
281 // Get the type of the variable.
282 QualType T = VR->getType(getContext());
283
284 // It must be of array type.
285 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
286
287 // return the size as signed integer.
288 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
289 }
290
291 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000292 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000293 // We intentionally made the size value signed because it participates in
294 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000295 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000296 }
297
298 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000299 GRStateRef state(St, StateMgr);
300
301 // Get the size of the super region in bytes.
302 RegionExtentsTy::data_type* T
303 = state.get<RegionExtentsTy>(ATR->getSuperRegion());
304
305 assert(T && "region extent not exist");
306
307 // Assume it's ConcreteInt for now.
308 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*T).getValue();
309
310 // Get the size of the element in bits.
311 QualType ElemTy = cast<PointerType>(ATR->getType(getContext()).getTypePtr())
312 ->getPointeeType();
313
314 uint64_t X = getContext().getTypeSize(ElemTy);
315
316 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
317 false);
318
319 // Calculate the number of elements.
320
321 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
322 // signed?
323 if (SSize.isUnsigned())
324 SSize.setIsSigned(true);
325
326 // FIXME: move this operation into BasicVals.
327 const llvm::APSInt S =
328 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
329
330 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000331 }
332
333 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
334 // FIXME: Unsupported yet.
335 FR = 0;
336 return UnknownVal();
337 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000338
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000339 assert(0 && "Other regions are not supported yet.");
340}
341
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000342// Cast 'pointer to array' to 'pointer to the first element of array'.
343
344SVal RegionStoreManager::ArrayToPointer(SVal Array) {
345 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000346
Zhongxing Xu63123d82008-11-23 04:30:35 +0000347 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000348 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
349
350 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000351}
352
Zhongxing Xucb529b52008-11-16 07:06:26 +0000353std::pair<const GRState*, SVal>
354RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr,
355 QualType CastToTy, Stmt* CastE) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000356 if (const AllocaRegion* AR =
357 dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) {
358
359 // Create a new region to attach type information to it.
360 const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR);
361
362 // Get the pointer to the first element.
363 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
364 const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR);
365
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000366 // Add a RegionView to base region.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000367 return std::pair<const GRState*, SVal>(AddRegionView(St, TR, AR),
368 loc::MemRegionVal(ER));
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000369 }
370
371 // Default case.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000372 return std::pair<const GRState*, SVal>(St, UnknownVal());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000373}
374
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000375SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000376 assert(!isa<UnknownVal>(L) && "location unknown");
377 assert(!isa<UndefinedVal>(L) && "location undefined");
378
379 switch (L.getSubKind()) {
380 case loc::MemRegionKind: {
381 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
382 assert(R && "bad region");
383
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000384 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
385 if (TR->getType(getContext())->isStructureType())
386 return RetrieveStruct(S, TR);
387
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000388 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
389 RegionBindingsTy::data_type* V = B.lookup(R);
390 return V ? *V : UnknownVal();
391 }
392
393 case loc::SymbolValKind:
394 return UnknownVal();
395
396 case loc::ConcreteIntKind:
397 return UndefinedVal(); // As in BasicStoreManager.
398
399 case loc::FuncValKind:
400 return L;
401
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000402 default:
403 assert(false && "Invalid Location");
Ted Kremenekab7b32b2008-11-19 00:27:37 +0000404 return L;
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000405 }
406}
407
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000408SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
409 QualType T = R->getType(getContext());
410 assert(T->isStructureType());
411
412 const RecordType* RT = cast<RecordType>(T.getTypePtr());
413 RecordDecl* RD = RT->getDecl();
414 assert(RD->isDefinition());
415
416 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
417
418 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
419 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
420 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000421 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000422
423 SVal FieldValue = data ? *data : UnknownVal();
424
425 StructVal = getBasicVals().consVals(FieldValue, StructVal);
426 }
427
428 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
429}
430
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000431Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000432 if (LV.getSubKind() == loc::SymbolValKind)
433 return store;
434
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000435 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000436
Ted Kremenek993f1c72008-10-17 20:28:54 +0000437 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000438
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000439 assert(R);
440
441 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
442 if (TR->getType(getContext())->isStructureType())
443 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000444
445 RegionBindingsTy B = GetRegionBindings(store);
446 return V.isUnknown()
447 ? RBFactory.Remove(B, R).getRoot()
448 : RBFactory.Add(B, R, V).getRoot();
449}
450
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000451Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
452 QualType T = R->getType(getContext());
453 assert(T->isStructureType());
454
455 const RecordType* RT = cast<RecordType>(T.getTypePtr());
456 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000457
458 if (!RD->isDefinition()) {
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000459 // This can only occur when a pointer of incomplete struct type is used as a
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000460 // function argument.
461 assert(V.isUnknown());
462 return store;
463 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000464
465 RegionBindingsTy B = GetRegionBindings(store);
466
Zhongxing Xud463d442008-11-02 12:13:30 +0000467 if (isa<UnknownVal>(V))
468 return BindStructToVal(store, R, UnknownVal());
469
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000470 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
471
472 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
473 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
474
475 for (; FI != FE; ++FI, ++VI) {
476 assert(VI != VE);
477
478 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
479
480 B = RBFactory.Add(B, FR, *VI);
481 }
482
483 return B.getRoot();
484}
485
Zhongxing Xu17892752008-10-08 02:50:44 +0000486Store RegionStoreManager::getInitialStore() {
487 typedef LiveVariables::AnalysisDataTy LVDataTy;
488 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
489
490 Store St = RBFactory.GetEmptyMap().getRoot();
491
492 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000493 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000494
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000495 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000496 // Punt on static variables for now.
497 if (VD->getStorageClass() == VarDecl::Static)
498 continue;
499
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000500 VarRegion* VR = MRMgr.getVarRegion(VD);
501
Zhongxing Xu17892752008-10-08 02:50:44 +0000502 QualType T = VD->getType();
503 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000504 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000505 // Initialize globals and parameters to symbolic values.
506 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000507 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000508 isa<ImplicitParamDecl>(VD))
Zhongxing Xu63123d82008-11-23 04:30:35 +0000509 ? SVal::GetSymbolValue(getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000510 : UndefinedVal();
511
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000512 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000513 }
514 else if (T->isArrayType()) {
515 if (VD->hasGlobalStorage()) // Params cannot have array type.
516 St = BindArrayToSymVal(St, VR);
517 else
518 St = BindArrayToVal(St, VR, UndefinedVal());
519 }
520 else if (T->isStructureType()) {
521 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
522 isa<ImplicitParamDecl>(VD))
523 St = BindStructToSymVal(St, VR);
524 else
525 St = BindStructToVal(St, VR, UndefinedVal());
Zhongxing Xu17892752008-10-08 02:50:44 +0000526 }
527 }
528 }
529 return St;
530}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000531
Ted Kremenek42577d12008-11-12 19:18:35 +0000532Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
533 SVal* InitVal, unsigned Count) {
534
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000535 if (VD->hasGlobalStorage()) {
536 // Static global variables should not be visited here.
537 assert(!(VD->getStorageClass() == VarDecl::Static &&
538 VD->isFileVarDecl()));
539 // Process static variables.
540 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000541 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000542 // Only handle pointer and integer static variables.
543
544 QualType T = VD->getType();
545
546 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000547 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000548 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000549
550 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000551 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000552 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000553
554 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000555 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000556 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000557 }
558 }
559 } else {
560 // Process local variables.
561
562 QualType T = VD->getType();
563
Zhongxing Xua82512a2008-10-24 08:42:28 +0000564 VarRegion* VR = MRMgr.getVarRegion(VD);
565
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000566 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000567 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000568 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000569 }
570 else if (T->isArrayType()) {
571 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000572 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000573 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000574 store = InitializeArray(store, VR, *InitVal);
575 }
576 else if (T->isStructureType()) {
577 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000578 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000579 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000580 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000581 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000582
583 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000584 }
585 return store;
586}
587
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000588Store RegionStoreManager::BindCompoundLiteral(Store store,
589 const CompoundLiteralExpr* CL,
590 SVal V) {
591 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
592 store = Bind(store, loc::MemRegionVal(R), V);
593 return store;
594}
595
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000596const GRState* RegionStoreManager::setExtent(const GRState* St,
597 const MemRegion* R, SVal Extent) {
598 GRStateRef state(St, StateMgr);
599 return state.set<RegionExtentsTy>(R, Extent);
600}
601
602
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000603Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
604 const LiveVariables& Live,
605 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
606 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
607
608 RegionBindingsTy B = GetRegionBindings(store);
609 typedef SVal::symbol_iterator symbol_iterator;
610
611 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
612 // in SymbolicRegions.
613 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
614 SVal X = I.getData();
615 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
616 LSymbols.insert(*SI);
617 }
618
619 return store;
620}
621
Zhongxing Xua071eb02008-10-24 06:01:33 +0000622void RegionStoreManager::print(Store store, std::ostream& Out,
623 const char* nl, const char *sep) {
624 llvm::raw_os_ostream OS(Out);
625 RegionBindingsTy B = GetRegionBindings(store);
626 OS << "Store:" << nl;
627
628 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
629 OS << ' '; I.getKey()->print(OS); OS << " : ";
630 I.getData().print(OS); OS << nl;
631 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000632}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000633
Zhongxing Xud463d442008-11-02 12:13:30 +0000634Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000635 SVal Init) {
636 QualType T = R->getType(getContext());
637 assert(T->isArrayType());
638
639 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
640
641 llvm::APInt Size = CAT->getSize();
642
643 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
644
645 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
646
647 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
648
649 for (; i != Size; ++i) {
650 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
651
652 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
653
654 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
655 // The init list might be shorter than the array decl.
656 if (VI != VE) ++VI;
657 }
658
659 return store;
660}
661
Zhongxing Xud463d442008-11-02 12:13:30 +0000662// Bind all elements of the array to some value.
663Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
664 SVal V){
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000665 QualType T = BaseR->getType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000666 assert(T->isArrayType());
667
Zhongxing Xua82512a2008-10-24 08:42:28 +0000668 // Only handle constant size array for now.
669 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
670
671 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000672 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
673 for (; i != Size; ++i) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000674 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000675
676 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
677
Zhongxing Xu9b6ceb12008-11-18 13:11:04 +0000678 if (CAT->getElementType()->isStructureType())
679 store = BindStructToVal(store, ER, V);
680 else
681 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000682 }
683 }
684
685 return store;
686}
687
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000688Store RegionStoreManager::BindArrayToSymVal(Store store,
689 const TypedRegion* BaseR) {
690 QualType T = BaseR->getType(getContext());
691 assert(T->isArrayType());
692
693 if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) {
694 llvm::APInt Size = CAT->getSize();
695 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
696 for (; i != Size; ++i) {
697 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
698
699 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
700
701 if (CAT->getElementType()->isStructureType()) {
702 store = BindStructToSymVal(store, ER);
703 }
704 else {
705 SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR,
706 &Idx.getValue(), CAT->getElementType());
707 store = Bind(store, loc::MemRegionVal(ER), V);
708 }
709 }
710 }
711
712 return store;
713}
714
Zhongxing Xud463d442008-11-02 12:13:30 +0000715Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000716 SVal Init) {
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000717 QualType T = R->getType(getContext());
718 assert(T->isStructureType());
719
720 RecordType* RT = cast<RecordType>(T.getTypePtr());
721 RecordDecl* RD = RT->getDecl();
722 assert(RD->isDefinition());
723
724 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
725 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
726 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
727
728 for (; FI != FE; ++FI) {
729 QualType FTy = (*FI)->getType();
730 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
731
732 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
733 if (VI != VE) {
734 store = Bind(store, loc::MemRegionVal(FR), *VI);
735 ++VI;
736 } else
737 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
738 }
739 else if (FTy->isArrayType()) {
740 if (VI != VE) {
741 store = InitializeArray(store, FR, *VI);
742 ++VI;
743 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000744 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000745 }
746 else if (FTy->isStructureType()) {
747 if (VI != VE) {
748 store = InitializeStruct(store, FR, *VI);
749 ++VI;
750 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000751 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000752 }
753 }
754 return store;
755}
756
Zhongxing Xud463d442008-11-02 12:13:30 +0000757// Bind all fields of the struct to some value.
758Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
759 SVal V) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000760 QualType T = BaseR->getType(getContext());
761 assert(T->isStructureType());
762
763 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000764 RecordDecl* RD = RT->getDecl();
765 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000766
767 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
768
769 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000770
771 QualType FTy = (*I)->getType();
772 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
773
774 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000775 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000776
777 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000778 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000779
780 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000781 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000782 }
783 }
784
785 return store;
786}
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000787
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000788Store RegionStoreManager::BindStructToSymVal(Store store,
789 const TypedRegion* BaseR) {
790 QualType T = BaseR->getType(getContext());
791 assert(T->isStructureType());
792
793 const RecordType* RT = cast<RecordType>(T.getTypePtr());
794 RecordDecl* RD = RT->getDecl();
795 assert(RD->isDefinition());
796
797 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
798
799 for (; I != E; ++I) {
800 QualType FTy = (*I)->getType();
801 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
802
803 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
804 store = Bind(store, loc::MemRegionVal(FR),
805 SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy));
806 }
807 else if (FTy->isArrayType()) {
808 store = BindArrayToSymVal(store, FR);
809 }
810 else if (FTy->isStructureType()) {
811 store = BindStructToSymVal(store, FR);
812 }
813 }
814
815 return store;
816}
817
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000818const GRState* RegionStoreManager::AddRegionView(const GRState* St,
819 const MemRegion* View,
820 const MemRegion* Base) {
821 GRStateRef state(St, StateMgr);
822
823 // First, retrieve the region view of the base region.
824 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
825 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
826
827 // Now add View to the region view.
828 L = RVFactory.Add(View, L);
829
830 // Create a new state with the new region view.
831 return state.set<RegionViewMapTy>(Base, L);
832}