blob: 20cf38f0bbada77c883bbe9537125e9ab0d2b1a5 [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
Ted Kremenek50dc1b32008-12-24 01:05:03 +000032//===----------------------------------------------------------------------===//
33// Region "Views"
34//===----------------------------------------------------------------------===//
35//
36// MemRegions can be layered on top of each other. This GDM entry tracks
37// what are the MemRegions that layer a given MemRegion.
38//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000039typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000040namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
41static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000042namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000043 template<> struct GRStateTrait<RegionViewMap>
44 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
45 RegionViews> > {
46
47 static void* GDMIndex() { return &RegionViewMapIndex; }
48 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000049}
Zhongxing Xu17892752008-10-08 02:50:44 +000050
Ted Kremenek50dc1b32008-12-24 01:05:03 +000051//===----------------------------------------------------------------------===//
52// Region "Extents"
53//===----------------------------------------------------------------------===//
54//
55// MemRegions represent chunks of memory with a size (their "extent"). This
56// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000057//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000058namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
59static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000060namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000061 template<> struct GRStateTrait<RegionExtents>
62 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
63 static void* GDMIndex() { return &RegionExtentsIndex; }
64 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000065}
66
Ted Kremenek50dc1b32008-12-24 01:05:03 +000067//===----------------------------------------------------------------------===//
68// Region "killsets".
69//===----------------------------------------------------------------------===//
70//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000071// RegionStore lazily adds value bindings to regions when the analyzer handles
72// assignment statements. Killsets track which default values have been
73// killed, thus distinguishing between "unknown" values and default
74// values. Regions are added to killset only when they are assigned "unknown"
75// directly, otherwise we should have their value in the region bindings.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000076//
77namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek2ed14be2008-12-05 00:47:52 +000078static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000079namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000080 template<> struct GRStateTrait<RegionKills>
Ted Kremenek50dc1b32008-12-24 01:05:03 +000081 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000082 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000083 };
84}
85
Ted Kremenek50dc1b32008-12-24 01:05:03 +000086//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000087// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000088//===----------------------------------------------------------------------===//
89//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000090// This GDM entry tracks what regions have a default value if they have no bound
91// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000092//
93namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
94static int RegionDefaultValueIndex = 0;
95namespace clang {
96 template<> struct GRStateTrait<RegionDefaultValue>
97 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
98 static void* GDMIndex() { return &RegionDefaultValueIndex; }
99 };
100}
101
102//===----------------------------------------------------------------------===//
103// Main RegionStore logic.
104//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000105
Zhongxing Xu17892752008-10-08 02:50:44 +0000106namespace {
107
108class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
109 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000110 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000111
Zhongxing Xu17892752008-10-08 02:50:44 +0000112 GRStateManager& StateMgr;
Zhongxing Xu17892752008-10-08 02:50:44 +0000113
114public:
115 RegionStoreManager(GRStateManager& mgr)
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000116 : StoreManager(mgr.getAllocator()),
117 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000118 RVFactory(mgr.getAllocator()),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000119 StateMgr(mgr) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000120
121 virtual ~RegionStoreManager() {}
122
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000123 MemRegionManager& getRegionManager() { return MRMgr; }
Ted Kremenek4f090272008-10-27 21:54:31 +0000124
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000125 const GRState* BindCompoundLiteral(const GRState* St,
126 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000127
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000128 /// getLValueString - Returns an SVal representing the lvalue of a
129 /// StringLiteral. Within RegionStore a StringLiteral has an
130 /// associated StringRegion, and the lvalue of a StringLiteral is
131 /// the lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000132 SVal getLValueString(const GRState* St, const StringLiteral* S);
133
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000134 /// getLValueCompoundLiteral - Returns an SVal representing the
135 /// lvalue of a compound literal. Within RegionStore a compound
136 /// literal has an associated region, and the lvalue of the
137 /// compound literal is the lvalue of that region.
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000138 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
139
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000140 /// getLValueVar - Returns an SVal that represents the lvalue of a
141 /// variable. Within RegionStore a variable has an associated
142 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000143 SVal getLValueVar(const GRState* St, const VarDecl* VD);
144
145 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
146
147 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
148
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000149 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
150
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000151 SVal getSizeInElements(const GRState* St, const MemRegion* R);
152
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000153 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
154 /// type. 'Array' represents the lvalue of the array being decayed
155 /// to a pointer, and the returned SVal represents the decayed
156 /// version of that lvalue (i.e., a pointer to the first element of
157 /// the array). This is called by GRExprEngine when evaluating
158 /// casts from arrays to pointers.
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000159 SVal ArrayToPointer(SVal Array);
160
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000161 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
162 /// a MemRegion* to a specific location type. 'R' is the region being
163 /// casted and 'CastToTy' the result type of the cast.
164 CastResult CastRegion(const GRState* state, const MemRegion* R,
165 QualType CastToTy);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000166
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000167 /// The high level logic for this method is this:
168 /// Retrieve (L)
169 /// if L has binding
170 /// return L's binding
171 /// else if L is in killset
172 /// return unknown
173 /// else
174 /// if L is on stack or heap
175 /// return undefined
176 /// else
177 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000178 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000179
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000180 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000181
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000182 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000183
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000184 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000185
186 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
187 /// 'this' object (C++). When used when analyzing a normal function this
188 /// method returns NULL.
189 const MemRegion* getSelfRegion(Store) {
190 assert (false && "Not implemented.");
191 return 0;
192 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000193
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000194 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
195 /// It returns a new Store with these values removed, and populates LSymbols
196 // and DSymbols with the known set of live and dead symbols respectively.
197 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
198 const LiveVariables& Live,
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000199 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000200 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000201
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000202 void UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000203
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000204 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
205
206 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
207 return St;
208 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000209
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000210 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
211
Zhongxing Xu17892752008-10-08 02:50:44 +0000212 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000213 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000214 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000215
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000216 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000217
218 void iterBindings(Store store, BindingsHandler& f) {
219 // FIXME: Implement.
220 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000221
222private:
223 Loc getVarLoc(const VarDecl* VD) {
224 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
225 }
226
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000227 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000228
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000229 /// Retrieve the values in a struct and return a CompoundVal, used when doing
230 /// struct copy:
231 /// struct s x, y;
232 /// x = y;
233 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000234 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000235
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000236 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000237
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000238 /// KillStruct - Set the entire struct to unknown.
239 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
240
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000241 // Utility methods.
242 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
243 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000244 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000245
246 const GRState* AddRegionView(const GRState* St,
247 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000248 const GRState* RemoveRegionView(const GRState* St,
249 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000250};
251
252} // end anonymous namespace
253
Ted Kremenek95c7b002008-10-24 01:04:59 +0000254StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000255 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000256}
257
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000258
259/// getLValueString - Returns an SVal representing the lvalue of a
260/// StringLiteral. Within RegionStore a StringLiteral has an
261/// associated StringRegion, and the lvalue of a StringLiteral is the
262/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000263SVal RegionStoreManager::getLValueString(const GRState* St,
264 const StringLiteral* S) {
265 return loc::MemRegionVal(MRMgr.getStringRegion(S));
266}
267
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000268/// getLValueVar - Returns an SVal that represents the lvalue of a
269/// variable. Within RegionStore a variable has an associated
270/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000271SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
272 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
273}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000274
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000275/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
276/// of a compound literal. Within RegionStore a compound literal
277/// has an associated region, and the lvalue of the compound literal
278/// is the lvalue of that region.
279SVal
280RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
281 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000282 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
283}
284
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000285SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
286 SVal Base) {
287 return UnknownVal();
288}
289
290SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
291 const FieldDecl* D) {
292 if (Base.isUnknownOrUndef())
293 return Base;
294
295 Loc BaseL = cast<Loc>(Base);
296 const MemRegion* BaseR = 0;
297
298 switch (BaseL.getSubKind()) {
299 case loc::MemRegionKind:
300 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
301 break;
302
303 case loc::SymbolValKind:
304 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
305 break;
306
307 case loc::GotoLabelKind:
308 case loc::FuncValKind:
309 // These are anormal cases. Flag an undefined value.
310 return UndefinedVal();
311
312 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000313 // While these seem funny, this can happen through casts.
314 // FIXME: What we should return is the field offset. For example,
315 // add the field offset to the integer value. That way funny things
316 // like this work properly: &(((struct foo *) 0xa)->f)
317 return Base;
318
319 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000320 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000321 return Base;
322 }
323
324 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
325}
326
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000327SVal RegionStoreManager::getLValueElement(const GRState* St,
328 SVal Base, SVal Offset) {
329 if (Base.isUnknownOrUndef())
330 return Base;
331
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000332 if (isa<loc::SymbolVal>(Base))
333 return Base;
334
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000335 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
336
Zhongxing Xue4d13932008-11-13 09:48:44 +0000337 // Pointer of any type can be cast and used as array base. We do not support
338 // that case yet.
339 if (!isa<ElementRegion>(BaseL.getRegion())) {
340 // Record what we have seen in real code.
341 assert(isa<FieldRegion>(BaseL.getRegion()));
342 return UnknownVal();
343 }
344
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000345 // We expect BaseR is an ElementRegion, not a base VarRegion.
346
347 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
348
349 SVal Idx = ElemR->getIndex();
350
351 nonloc::ConcreteInt *CI1, *CI2;
352
353 // Only handle integer indices for now.
354 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
355 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000356
Sebastian Redle95db4f2008-11-24 19:35:33 +0000357 // Temporary SVal to hold a potential signed and extended APSInt.
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000358 SVal SignedInt;
359
Sebastian Redle95db4f2008-11-24 19:35:33 +0000360 // Index might be unsigned. We have to convert it to signed. It might also
361 // be less wide than the size. We have to extend it.
362 if (CI2->getValue().isUnsigned() ||
363 CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000364 llvm::APSInt SI = CI2->getValue();
Sebastian Redlddee68b2008-11-24 19:39:40 +0000365 if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth())
366 SI.extend(CI1->getValue().getBitWidth());
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000367 SI.setIsSigned(true);
368 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
369 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
370 }
371
Zhongxing Xu63123d82008-11-23 04:30:35 +0000372 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000373 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
Ted Kremenekabb042f2008-12-13 19:24:37 +0000374 ElemR->getArrayRegion()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000375 }
376
377 return UnknownVal();
378}
379
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000380SVal RegionStoreManager::getSizeInElements(const GRState* St,
381 const MemRegion* R) {
382 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
383 // Get the type of the variable.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000384 QualType T = VR->getRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000385
386 // It must be of array type.
387 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
388
389 // return the size as signed integer.
390 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
391 }
392
393 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000394 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000395 // We intentionally made the size value signed because it participates in
396 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000397 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000398 }
399
400 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000401 GRStateRef state(St, StateMgr);
402
403 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000404 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
405 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000406
407 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000408 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000409
410 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000411 QualType LvT = ATR->getLValueType(getContext());
412 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000413
414 uint64_t X = getContext().getTypeSize(ElemTy);
415
416 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
417 false);
418
419 // Calculate the number of elements.
420
421 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
422 // signed?
423 if (SSize.isUnsigned())
424 SSize.setIsSigned(true);
425
426 // FIXME: move this operation into BasicVals.
427 const llvm::APSInt S =
428 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
429
430 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000431 }
432
433 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
434 // FIXME: Unsupported yet.
435 FR = 0;
436 return UnknownVal();
437 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000438
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000439 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000440 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000441}
442
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000443/// ArrayToPointer - Emulates the "decay" of an array to a pointer
444/// type. 'Array' represents the lvalue of the array being decayed
445/// to a pointer, and the returned SVal represents the decayed
446/// version of that lvalue (i.e., a pointer to the first element of
447/// the array). This is called by GRExprEngine when evaluating casts
448/// from arrays to pointers.
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000449SVal RegionStoreManager::ArrayToPointer(SVal Array) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000450 // FIXME: This should be factored into GRExprEngine. This allows
451 // us to pass a "loc" instead of an "SVal" for "Array".
Ted Kremenekabb042f2008-12-13 19:24:37 +0000452 if (Array.isUnknownOrUndef())
453 return Array;
454
455 if (!isa<loc::MemRegionVal>(Array))
456 return UnknownVal();
457
458 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
459 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
460
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000461 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000462 return UnknownVal();
463
Zhongxing Xu63123d82008-11-23 04:30:35 +0000464 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000465 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
466
467 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000468}
469
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000470StoreManager::CastResult
471RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
472 QualType CastToTy) {
473
474 // Return the same region if the region types are compatible.
475 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
476 ASTContext& Ctx = StateMgr.getContext();
477 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
478 QualType Tb = Ctx.getCanonicalType(CastToTy);
479
480 if (Ta == Tb)
481 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000482 }
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000483
484 // FIXME: We should handle the case when we are casting *back* to a
485 // previous type. For example:
486 //
487 // void* x = ...;
488 // char* y = (char*) x;
489 // void* z = (void*) y; // <-- we should get the same region that is
490 // bound to 'x'
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000491 const MemRegion* ViewR = MRMgr.getAnonTypedRegion(CastToTy, R);
492 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000493}
494
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000495SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000496 assert(!isa<UnknownVal>(L) && "location unknown");
497 assert(!isa<UndefinedVal>(L) && "location undefined");
498
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000499 // FIXME: What does loc::SymbolVal represent? It represents the value
500 // of a location but that value is not known. In the future we should
501 // handle potential aliasing relationships; e.g. a loc::SymbolVal could
502 // be an alias for a particular region.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000503 if (isa<loc::SymbolVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000504 return UnknownVal();
505
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000506 // FIXME: Is this even possible? Shouldn't this be treated as a null
507 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000508 if (isa<loc::ConcreteInt>(L))
509 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000510
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000511 // FIXME: Should this be refactored into GRExprEngine or GRStateManager?
512 // It seems that all StoreManagers would do the same thing here.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000513 if (isa<loc::FuncVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000514 return L;
515
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000516 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
517 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000518 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
519 assert(R && "bad region");
520
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000521 // FIXME: We should eventually handle funny addressing. e.g.:
522 //
523 // int x = ...;
524 // int *p = &x;
525 // char *q = (char*) p;
526 // char c = *q; // returns the first byte of 'x'.
527 //
528 // Such funny addressing will occur due to layering of regions.
529
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000530 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
531 if (TR->getRValueType(getContext())->isStructureType())
532 return RetrieveStruct(St, TR);
533
534 RegionBindingsTy B = GetRegionBindings(St->getStore());
535 RegionBindingsTy::data_type* V = B.lookup(R);
536
537 // Check if the region has a binding.
538 if (V)
539 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000540
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000541 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000542
543 // FIXME: Do we even need a killset? If 'Unknown' is explicitly
544 // bound to to a region won't this be enough? (that's basically
545 // what a killset is). RemoveDeadBindings should only remove
546 // bindings that are no longer accessible, which means that won't
547 // ever be read.
548
549 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000550 if (state.contains<RegionKills>(R))
551 return UnknownVal();
552
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000553 // The location does not have a bound value. This means that it has
554 // the value it had upon its creation and/or entry to the analyzed
555 // function/method. These are either symbolic values or 'undefined'.
556
557 // We treat function parameters as symbolic values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000558 if (const VarRegion* VR = dyn_cast<VarRegion>(R))
559 if (isa<ParmVarDecl>(VR->getDecl()))
560 return SVal::MakeSymbolValue(getSymbolManager(), VR,
561 VR->getRValueType(getContext()));
562
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000563 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
564 // All stack variables are considered to have undefined values
565 // upon creation. All heap allocated blocks are considered to
566 // have undefined values as well unless they are explicitly bound
567 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000568 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000569 }
570
571 // All other values are symbolic.
572 return SVal::MakeSymbolValue(getSymbolManager(), R,
573 cast<TypedRegion>(R)->getRValueType(getContext()));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000574
575 // FIXME: consider default values for elements and fields.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000576}
577
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000578SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
579
580 Store store = St->getStore();
581 GRStateRef state(St, StateMgr);
582
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000583 // FIXME: Verify we want getRValueType instead of getLValueType.
584 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000585 assert(T->isStructureType());
586
587 const RecordType* RT = cast<RecordType>(T.getTypePtr());
588 RecordDecl* RD = RT->getDecl();
589 assert(RD->isDefinition());
590
591 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
592
Douglas Gregore267ff32008-12-11 20:41:00 +0000593 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000594
Douglas Gregore267ff32008-12-11 20:41:00 +0000595 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
596 FieldEnd = Fields.rend();
597 Field != FieldEnd; ++Field) {
598 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000599 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000600 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000601
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000602 SVal FieldValue;
603 if (data)
604 FieldValue = *data;
605 else if (state.contains<RegionKills>(FR))
606 FieldValue = UnknownVal();
607 else {
608 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
609 FieldValue = UndefinedVal();
610 else
611 FieldValue = SVal::MakeSymbolValue(getSymbolManager(), FR,
612 FR->getRValueType(getContext()));
613 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000614
615 StructVal = getBasicVals().consVals(FieldValue, StructVal);
616 }
617
618 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
619}
620
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000621const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
622 // Currently we don't bind value to symbolic location. But if the logic is
623 // made clear, we might change this decision.
624 if (isa<loc::SymbolVal>(L))
625 return St;
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000626
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000627 // If we get here, the location should be a region.
628 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000629 assert(R);
630
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000631 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000632 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000633 // FIXME: Verify we want getRValueType().
634 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000635 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000636
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000637 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000638 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000639
640 if (V.isUnknown()) {
641 // Remove the binding.
642 store = RBFactory.Remove(B, R).getRoot();
643
644 // Add the region to the killset.
645 GRStateRef state(St, StateMgr);
646 St = state.add<RegionKills>(R);
647 }
648 else
649 store = RBFactory.Add(B, R, V).getRoot();
650
651 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000652}
653
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000654Store RegionStoreManager::Remove(Store store, Loc L) {
655 RegionBindingsTy B = GetRegionBindings(store);
656
657 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
658 assert(R);
659
660 return RBFactory.Remove(B, R).getRoot();
661}
662
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000663const GRState* RegionStoreManager::BindDecl(const GRState* St,
664 const VarDecl* VD, SVal InitVal) {
665 // All static variables are treated as symbolic values.
666 if (VD->hasGlobalStorage())
667 return St;
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000668
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000669 // Process local variables.
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000670
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000671 QualType T = VD->getType();
672
673 VarRegion* VR = MRMgr.getVarRegion(VD);
674
675 if (Loc::IsLocType(T) || T->isIntegerType())
676 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000677
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000678 else if (T->isArrayType())
679 return BindArray(St, VR, InitVal);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000680
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000681 else if (T->isStructureType())
682 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000683
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000684 // Other types of variable are not supported yet.
Zhongxing Xu17892752008-10-08 02:50:44 +0000685 return St;
686}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000687
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000688// FIXME: this method should be merged into Bind().
689const GRState*
690RegionStoreManager::BindCompoundLiteral(const GRState* St,
691 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000692 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000693 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000694}
695
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000696const GRState* RegionStoreManager::setExtent(const GRState* St,
697 const MemRegion* R, SVal Extent) {
698 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000699 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000700}
701
702
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000703void RegionStoreManager::UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols) {
704 for (SVal::symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
705 LSymbols.insert(*SI);
706}
707
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000708Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000709 const LiveVariables& Live,
710 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
711 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
712
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000713 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000714 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000715
716 // Lazily constructed backmap from MemRegions to SubRegions.
717 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
718 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
719
720 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
721 // the ability to reuse memory. This way we can keep TmpAlloc around as
722 // an instance variable of RegionStoreManager (avoiding repeated malloc
723 // overhead).
724 llvm::BumpPtrAllocator TmpAlloc;
725
726 // Factory objects.
727 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
728 SubRegionsTy::Factory SubRegF(TmpAlloc);
729
730 // The backmap from regions to subregions.
731 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
732
733 // Do a pass over the regions in the store. For VarRegions we check if
734 // the variable is still live and if so add it to the list of live roots.
735 // For other regions we populate our region backmap.
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000736 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000737 const MemRegion* R = I.getKey();
738 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
739 if (Live.isLive(Loc, VR->getDecl()))
740 RegionRoots.push_back(VR); // This is a live "root".
741 }
742 else {
743 // Get the super region for R.
744 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
745 // Get the current set of subregions for SuperR.
746 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
747 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
748 // Add R to the subregions of SuperR.
749 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
750
751 // Finally, check if SuperR is a VarRegion. We need to do this
752 // to also mark SuperR as a root (as it may not have a value directly
753 // bound to it in the store).
754 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
755 if (Live.isLive(Loc, VR->getDecl()))
756 RegionRoots.push_back(VR); // This is a live "root".
757 }
758 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000759 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000760
761 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
762 // of the store. We want to find all live symbols and dead regions.
763 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
764
765 while (!RegionRoots.empty()) {
766 // Dequeue the next region on the worklist.
767 const MemRegion* R = RegionRoots.back();
768 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000769
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000770 // Check if we have already processed this region.
771 if (Marked.count(R)) continue;
772
773 // Mark this region as processed. This is needed for termination in case
774 // a region is referenced more than once.
775 Marked.insert(R);
776
777 // Mark the symbol for any live SymbolicRegion as "live". This means we
778 // should continue to track that symbol.
779 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
780 LSymbols.insert(SymR->getSymbol());
781
782 // Get the data binding for R (if any).
783 RegionBindingsTy::data_type* Xptr = B.lookup(R);
784 if (Xptr) {
785 SVal X = *Xptr;
786 UpdateLiveSymbols(X, LSymbols); // Update the set of live symbols.
787
788 // If X is a region, then add it the RegionRoots.
789 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
790 RegionRoots.push_back(RegionX->getRegion());
791 }
792
793 // Get the subregions of R. These are RegionRoots as well since they
794 // represent values that are also bound to R.
795 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
796 if (!SRptr) continue;
797 SubRegionsTy SR = *SRptr;
798
799 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
800 RegionRoots.push_back(*I);
801 }
802
803 // We have now scanned the store, marking reachable regions and symbols
804 // as live. We now remove all the regions that are dead from the store
805 // as well as update DSymbols with the set symbols that are now dead.
806
807 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
808 const MemRegion* R = I.getKey();
809
810 // If this region live? Is so, none of its symbols are dead.
811 if (Marked.count(R))
812 continue;
813
814 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000815 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000816
817 // Mark all non-live symbols that this region references as dead.
818 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000819 SymbolRef Sym = SymR->getSymbol();
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000820 if (!LSymbols.count(Sym)) DSymbols.insert(Sym);
821 }
822
823 SVal X = I.getData();
824 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
825 for (; SI != SE; ++SI) { if (!LSymbols.count(*SI)) DSymbols.insert(*SI); }
826 }
827
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000828 return store;
829}
830
Zhongxing Xua071eb02008-10-24 06:01:33 +0000831void RegionStoreManager::print(Store store, std::ostream& Out,
832 const char* nl, const char *sep) {
833 llvm::raw_os_ostream OS(Out);
834 RegionBindingsTy B = GetRegionBindings(store);
835 OS << "Store:" << nl;
836
837 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
838 OS << ' '; I.getKey()->print(OS); OS << " : ";
839 I.getData().print(OS); OS << nl;
840 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000841}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000842
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000843const GRState* RegionStoreManager::BindArray(const GRState* St,
844 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000845
846 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +0000847 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000848 assert(T->isArrayType());
849
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000850 // When we are binding the whole array, it always has default value 0.
851 GRStateRef state(St, StateMgr);
Zhongxing Xu13020162008-12-24 07:29:24 +0000852 St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0,
853 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000854
855 Store store = St->getStore();
856
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000857 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
858
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000859 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000860 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000861
862 // Check if the init expr is a StringLiteral.
863 if (isa<loc::MemRegionVal>(Init)) {
864 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
865 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
866 const char* str = S->getStrData();
867 unsigned len = S->getByteLength();
868 unsigned j = 0;
869
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000870 // Copy bytes from the string literal into the target array. Trailing bytes
871 // in the array that are not covered by the string literal are initialized
872 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000873 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000874 if (j >= len)
875 break;
876
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000877 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
878 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
879
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000880 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
881 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000882 }
883
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000884 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000885 }
886
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000887
888 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
889
890 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
891
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000892 for (; i < Size; ++i, ++VI) {
893 // The init list might be shorter than the array decl.
894 if (VI == VE)
895 break;
896
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000897 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000898 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000899
900 if (CAT->getElementType()->isStructureType())
901 St = BindStruct(St, ER, *VI);
902 else
903 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000904 }
905
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000906 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000907}
908
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000909const GRState*
910RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000911 // FIXME: Verify that we should use getRValueType or getLValueType.
912 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000913 assert(T->isStructureType());
914
915 RecordType* RT = cast<RecordType>(T.getTypePtr());
916 RecordDecl* RD = RT->getDecl();
917 assert(RD->isDefinition());
918
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000919 if (V.isUnknown())
920 return KillStruct(St, R);
921
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000922 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000923 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
924 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
925
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000926 for (; FI != FE; ++FI, ++VI) {
927
928 // There may be fewer values than fields only when we are initializing a
929 // struct decl. In this case, mark the region as having default value.
930 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +0000931 GRStateRef state(St, StateMgr);
932 St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0,
933 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000934 break;
935 }
936
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000937 QualType FTy = (*FI)->getType();
938 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
939
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000940 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
941 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000942
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000943 else if (FTy->isArrayType())
944 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000945
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000946 else if (FTy->isStructureType())
947 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000948 }
949
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000950 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000951}
952
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000953const GRState* RegionStoreManager::KillStruct(const GRState* St,
954 const TypedRegion* R){
955 GRStateRef state(St, StateMgr);
956
957 // Kill the struct region because it is assigned "unknown".
958 St = state.add<RegionKills>(R);
959
960 // Set the default value of the struct region to "unknown".
961 St = state.set<RegionDefaultValue>(R, UnknownVal());
962
963 Store store = St->getStore();
964 RegionBindingsTy B = GetRegionBindings(store);
965
966 // Remove all bindings for the subregions of the struct.
967 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
968 const MemRegion* r = I.getKey();
969 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
970 if (sr->isSubRegionOf(R))
971 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +0000972 // FIXME: Maybe we should also remove the bindings for the "views" of the
973 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000974 }
975
976 return StateMgr.MakeStateWithStore(St, store);
977}
978
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000979const GRState* RegionStoreManager::AddRegionView(const GRState* St,
980 const MemRegion* View,
981 const MemRegion* Base) {
982 GRStateRef state(St, StateMgr);
983
984 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000985 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000986 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000987
988 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000989 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000990
991 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000992 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000993}
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000994
995const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
996 const MemRegion* View,
997 const MemRegion* Base) {
998 GRStateRef state(St, StateMgr);
999
1000 // Retrieve the region view of the base region.
1001 const RegionViews* d = state.get<RegionViewMap>(Base);
1002
1003 // If the base region has no view, return.
1004 if (!d)
1005 return St;
1006
1007 // Remove the view.
1008 RegionViews V = *d;
1009 V = RVFactory.Remove(V, View);
1010
1011 return state.set<RegionViewMap>(Base, V);
1012}