blob: 6f0ec1d0b73ba180b421c65ac239ea093ae09e0b [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,
Ted Kremenek241677a2009-01-21 22:26:05 +0000198 SymbolReaper& SymReaper,
199 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000200
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000201 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
202
203 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
204 return St;
205 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000206
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000207 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
208
Zhongxing Xu17892752008-10-08 02:50:44 +0000209 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000210 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000211 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000212
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000213 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000214
215 void iterBindings(Store store, BindingsHandler& f) {
216 // FIXME: Implement.
217 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000218
219private:
220 Loc getVarLoc(const VarDecl* VD) {
221 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
222 }
223
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000224 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000225
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000226 /// Retrieve the values in a struct and return a CompoundVal, used when doing
227 /// struct copy:
228 /// struct s x, y;
229 /// x = y;
230 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000231 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000232
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000233 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000234
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000235 /// KillStruct - Set the entire struct to unknown.
236 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
237
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000238 // Utility methods.
239 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
240 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000241 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000242
243 const GRState* AddRegionView(const GRState* St,
244 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000245 const GRState* RemoveRegionView(const GRState* St,
246 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000247};
248
249} // end anonymous namespace
250
Ted Kremenek95c7b002008-10-24 01:04:59 +0000251StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000252 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000253}
254
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000255
256/// getLValueString - Returns an SVal representing the lvalue of a
257/// StringLiteral. Within RegionStore a StringLiteral has an
258/// associated StringRegion, and the lvalue of a StringLiteral is the
259/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000260SVal RegionStoreManager::getLValueString(const GRState* St,
261 const StringLiteral* S) {
262 return loc::MemRegionVal(MRMgr.getStringRegion(S));
263}
264
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000265/// getLValueVar - Returns an SVal that represents the lvalue of a
266/// variable. Within RegionStore a variable has an associated
267/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000268SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
269 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
270}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000271
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000272/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
273/// of a compound literal. Within RegionStore a compound literal
274/// has an associated region, and the lvalue of the compound literal
275/// is the lvalue of that region.
276SVal
277RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
278 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000279 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
280}
281
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000282SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
283 SVal Base) {
284 return UnknownVal();
285}
286
287SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
288 const FieldDecl* D) {
289 if (Base.isUnknownOrUndef())
290 return Base;
291
292 Loc BaseL = cast<Loc>(Base);
293 const MemRegion* BaseR = 0;
294
295 switch (BaseL.getSubKind()) {
296 case loc::MemRegionKind:
297 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
298 break;
299
300 case loc::SymbolValKind:
301 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
302 break;
303
304 case loc::GotoLabelKind:
305 case loc::FuncValKind:
306 // These are anormal cases. Flag an undefined value.
307 return UndefinedVal();
308
309 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000310 // While these seem funny, this can happen through casts.
311 // FIXME: What we should return is the field offset. For example,
312 // add the field offset to the integer value. That way funny things
313 // like this work properly: &(((struct foo *) 0xa)->f)
314 return Base;
315
316 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000317 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000318 return Base;
319 }
320
321 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
322}
323
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000324SVal RegionStoreManager::getLValueElement(const GRState* St,
325 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000326
Ted Kremenekc979e802009-01-21 22:58:50 +0000327 if (Base.isUnknownOrUndef() || isa<loc::SymbolVal>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000328 return Base;
329
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000330 // Only handle integer offsets... for now.
331 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000332 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000333
334 const TypedRegion *BaseRegion =
335 cast<TypedRegion>(cast<loc::MemRegionVal>(Base).getRegion());
336
337 // Pointer of any type can be cast and used as array base.
338 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
339
340 if (!ElemR) {
341 //
342 // If the base region is not an ElementRegion, create one.
343 // This can happen in the following example:
344 //
345 // char *p = __builtin_alloc(10);
346 // p[1] = 8;
347 //
348 // Observe that 'p' binds to an AnonTypedRegion<AllocaRegion>.
349 //
350 return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000351 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000352
353 SVal BaseIdx = ElemR->getIndex();
354
355 if (!isa<nonloc::ConcreteInt>(BaseIdx))
356 return UnknownVal();
357
358 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
359 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
360 assert(BaseIdxI.isSigned());
361
362 // FIXME: This appears to be the assumption of this code. We should review
363 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
364 // can't we need to put a comment here. If it can, we should handle it.
365 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000366
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000367 const TypedRegion *ArrayR = ElemR->getArrayRegion();
368 SVal NewIdx;
369
370 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
371 // 'Offset' might be unsigned. We have to convert it to signed and
372 // possibly extend it.
373 llvm::APSInt Tmp = OffI;
374
375 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
376 Tmp.extend(BaseIdxI.getBitWidth());
377
378 Tmp.setIsSigned(true);
379 Tmp += BaseIdxI; // Compute the new offset.
380 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(Tmp));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000381 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000382 else
383 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000384
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000385 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000386}
387
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000388SVal RegionStoreManager::getSizeInElements(const GRState* St,
389 const MemRegion* R) {
390 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
391 // Get the type of the variable.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000392 QualType T = VR->getRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000393
394 // It must be of array type.
395 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
396
397 // return the size as signed integer.
398 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
399 }
400
401 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000402 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000403 // We intentionally made the size value signed because it participates in
404 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000405 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000406 }
407
408 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000409 GRStateRef state(St, StateMgr);
410
411 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000412 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
413 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000414
415 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000416 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000417
418 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000419 QualType LvT = ATR->getLValueType(getContext());
420 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000421
422 uint64_t X = getContext().getTypeSize(ElemTy);
423
424 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
425 false);
426
427 // Calculate the number of elements.
428
429 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
430 // signed?
431 if (SSize.isUnsigned())
432 SSize.setIsSigned(true);
433
434 // FIXME: move this operation into BasicVals.
435 const llvm::APSInt S =
436 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
437
438 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000439 }
440
441 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
442 // FIXME: Unsupported yet.
443 FR = 0;
444 return UnknownVal();
445 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000446
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000447 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000448 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000449}
450
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000451/// ArrayToPointer - Emulates the "decay" of an array to a pointer
452/// type. 'Array' represents the lvalue of the array being decayed
453/// to a pointer, and the returned SVal represents the decayed
454/// version of that lvalue (i.e., a pointer to the first element of
455/// the array). This is called by GRExprEngine when evaluating casts
456/// from arrays to pointers.
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000457SVal RegionStoreManager::ArrayToPointer(SVal Array) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000458 // FIXME: This should be factored into GRExprEngine. This allows
459 // us to pass a "loc" instead of an "SVal" for "Array".
Ted Kremenekabb042f2008-12-13 19:24:37 +0000460 if (Array.isUnknownOrUndef())
461 return Array;
462
463 if (!isa<loc::MemRegionVal>(Array))
464 return UnknownVal();
465
466 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
467 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
468
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000469 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000470 return UnknownVal();
471
Zhongxing Xu63123d82008-11-23 04:30:35 +0000472 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000473 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
474
475 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000476}
477
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000478StoreManager::CastResult
479RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
480 QualType CastToTy) {
481
482 // Return the same region if the region types are compatible.
483 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
484 ASTContext& Ctx = StateMgr.getContext();
485 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
486 QualType Tb = Ctx.getCanonicalType(CastToTy);
487
488 if (Ta == Tb)
489 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000490 }
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000491
492 // FIXME: We should handle the case when we are casting *back* to a
493 // previous type. For example:
494 //
495 // void* x = ...;
496 // char* y = (char*) x;
497 // void* z = (void*) y; // <-- we should get the same region that is
498 // bound to 'x'
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000499 const MemRegion* ViewR = MRMgr.getAnonTypedRegion(CastToTy, R);
500 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000501}
502
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000503SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000504 assert(!isa<UnknownVal>(L) && "location unknown");
505 assert(!isa<UndefinedVal>(L) && "location undefined");
506
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000507 // FIXME: What does loc::SymbolVal represent? It represents the value
508 // of a location but that value is not known. In the future we should
509 // handle potential aliasing relationships; e.g. a loc::SymbolVal could
510 // be an alias for a particular region.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000511 if (isa<loc::SymbolVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000512 return UnknownVal();
513
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000514 // FIXME: Is this even possible? Shouldn't this be treated as a null
515 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000516 if (isa<loc::ConcreteInt>(L))
517 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000518
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000519 // FIXME: Should this be refactored into GRExprEngine or GRStateManager?
520 // It seems that all StoreManagers would do the same thing here.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000521 if (isa<loc::FuncVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000522 return L;
523
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000524 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
525 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000526 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
527 assert(R && "bad region");
528
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000529 // FIXME: We should eventually handle funny addressing. e.g.:
530 //
531 // int x = ...;
532 // int *p = &x;
533 // char *q = (char*) p;
534 // char c = *q; // returns the first byte of 'x'.
535 //
536 // Such funny addressing will occur due to layering of regions.
537
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000538 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
539 if (TR->getRValueType(getContext())->isStructureType())
540 return RetrieveStruct(St, TR);
541
542 RegionBindingsTy B = GetRegionBindings(St->getStore());
543 RegionBindingsTy::data_type* V = B.lookup(R);
544
545 // Check if the region has a binding.
546 if (V)
547 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000548
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000549 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000550
551 // FIXME: Do we even need a killset? If 'Unknown' is explicitly
552 // bound to to a region won't this be enough? (that's basically
553 // what a killset is). RemoveDeadBindings should only remove
554 // bindings that are no longer accessible, which means that won't
555 // ever be read.
556
557 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000558 if (state.contains<RegionKills>(R))
559 return UnknownVal();
560
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000561 // The location does not have a bound value. This means that it has
562 // the value it had upon its creation and/or entry to the analyzed
563 // function/method. These are either symbolic values or 'undefined'.
564
565 // We treat function parameters as symbolic values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000566 if (const VarRegion* VR = dyn_cast<VarRegion>(R))
567 if (isa<ParmVarDecl>(VR->getDecl()))
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000568 return SVal::GetRValueSymbolVal(getSymbolManager(), VR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000569
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000570 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
571 // All stack variables are considered to have undefined values
572 // upon creation. All heap allocated blocks are considered to
573 // have undefined values as well unless they are explicitly bound
574 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000575 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000576 }
577
578 // All other values are symbolic.
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000579 return SVal::GetRValueSymbolVal(getSymbolManager(), R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000580
581 // FIXME: consider default values for elements and fields.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000582}
583
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000584SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
585
586 Store store = St->getStore();
587 GRStateRef state(St, StateMgr);
588
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000589 // FIXME: Verify we want getRValueType instead of getLValueType.
590 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000591 assert(T->isStructureType());
592
593 const RecordType* RT = cast<RecordType>(T.getTypePtr());
594 RecordDecl* RD = RT->getDecl();
595 assert(RD->isDefinition());
596
597 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
598
Douglas Gregore267ff32008-12-11 20:41:00 +0000599 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000600
Douglas Gregore267ff32008-12-11 20:41:00 +0000601 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
602 FieldEnd = Fields.rend();
603 Field != FieldEnd; ++Field) {
604 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000605 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000606 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000607
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000608 SVal FieldValue;
609 if (data)
610 FieldValue = *data;
611 else if (state.contains<RegionKills>(FR))
612 FieldValue = UnknownVal();
613 else {
614 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
615 FieldValue = UndefinedVal();
616 else
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000617 FieldValue = SVal::GetRValueSymbolVal(getSymbolManager(), FR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000618 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000619
620 StructVal = getBasicVals().consVals(FieldValue, StructVal);
621 }
622
623 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
624}
625
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000626const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
627 // Currently we don't bind value to symbolic location. But if the logic is
628 // made clear, we might change this decision.
629 if (isa<loc::SymbolVal>(L))
630 return St;
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000631
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000632 // If we get here, the location should be a region.
633 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000634 assert(R);
635
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000636 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000637 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000638 // FIXME: Verify we want getRValueType().
639 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000640 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000641
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000642 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000643 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000644
645 if (V.isUnknown()) {
646 // Remove the binding.
647 store = RBFactory.Remove(B, R).getRoot();
648
649 // Add the region to the killset.
650 GRStateRef state(St, StateMgr);
651 St = state.add<RegionKills>(R);
652 }
653 else
654 store = RBFactory.Add(B, R, V).getRoot();
655
656 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000657}
658
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000659Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000660 const MemRegion* R = 0;
661
662 if (isa<loc::MemRegionVal>(L))
663 R = cast<loc::MemRegionVal>(L).getRegion();
664 else if (isa<loc::SymbolVal>(L))
665 R = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(L).getSymbol());
666
667 if (R) {
668 RegionBindingsTy B = GetRegionBindings(store);
669 return RBFactory.Remove(B, R).getRoot();
670 }
671
672 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000673}
674
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000675const GRState* RegionStoreManager::BindDecl(const GRState* St,
676 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000677
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000678 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000679 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000680
Ted Kremenek0964a062009-01-21 06:57:53 +0000681 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000682 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000683 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000684 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000685
Ted Kremenek0964a062009-01-21 06:57:53 +0000686 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000687}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000688
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000689// FIXME: this method should be merged into Bind().
690const GRState*
691RegionStoreManager::BindCompoundLiteral(const GRState* St,
692 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000693 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000694 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000695}
696
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000697const GRState* RegionStoreManager::setExtent(const GRState* St,
698 const MemRegion* R, SVal Extent) {
699 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000700 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000701}
702
703
Ted Kremenek241677a2009-01-21 22:26:05 +0000704static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
705 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
706 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000707}
708
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000709Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000710 SymbolReaper& SymReaper,
711 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
712{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000713
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000714 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000715 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000716
717 // Lazily constructed backmap from MemRegions to SubRegions.
718 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
719 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
720
721 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
722 // the ability to reuse memory. This way we can keep TmpAlloc around as
723 // an instance variable of RegionStoreManager (avoiding repeated malloc
724 // overhead).
725 llvm::BumpPtrAllocator TmpAlloc;
726
727 // Factory objects.
728 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
729 SubRegionsTy::Factory SubRegF(TmpAlloc);
730
731 // The backmap from regions to subregions.
732 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
733
734 // Do a pass over the regions in the store. For VarRegions we check if
735 // the variable is still live and if so add it to the list of live roots.
736 // For other regions we populate our region backmap.
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000737 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000738 const MemRegion* R = I.getKey();
739 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000740 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000741 RegionRoots.push_back(VR); // This is a live "root".
742 }
743 else {
744 // Get the super region for R.
745 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
746 // Get the current set of subregions for SuperR.
747 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
748 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
749 // Add R to the subregions of SuperR.
750 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
751
752 // Finally, check if SuperR is a VarRegion. We need to do this
753 // to also mark SuperR as a root (as it may not have a value directly
754 // bound to it in the store).
755 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000756 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000757 RegionRoots.push_back(VR); // This is a live "root".
758 }
759 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000760 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000761
762 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
763 // of the store. We want to find all live symbols and dead regions.
764 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
765
766 while (!RegionRoots.empty()) {
767 // Dequeue the next region on the worklist.
768 const MemRegion* R = RegionRoots.back();
769 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000770
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000771 // Check if we have already processed this region.
772 if (Marked.count(R)) continue;
773
774 // Mark this region as processed. This is needed for termination in case
775 // a region is referenced more than once.
776 Marked.insert(R);
777
778 // Mark the symbol for any live SymbolicRegion as "live". This means we
779 // should continue to track that symbol.
780 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +0000781 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000782
783 // Get the data binding for R (if any).
784 RegionBindingsTy::data_type* Xptr = B.lookup(R);
785 if (Xptr) {
786 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +0000787 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000788
789 // If X is a region, then add it the RegionRoots.
790 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
791 RegionRoots.push_back(RegionX->getRegion());
792 }
793
794 // Get the subregions of R. These are RegionRoots as well since they
795 // represent values that are also bound to R.
796 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
797 if (!SRptr) continue;
798 SubRegionsTy SR = *SRptr;
799
800 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
801 RegionRoots.push_back(*I);
802 }
803
804 // We have now scanned the store, marking reachable regions and symbols
805 // as live. We now remove all the regions that are dead from the store
806 // as well as update DSymbols with the set symbols that are now dead.
807
808 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
809 const MemRegion* R = I.getKey();
810
811 // If this region live? Is so, none of its symbols are dead.
812 if (Marked.count(R))
813 continue;
814
815 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000816 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000817
818 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +0000819 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
820 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000821
822 SVal X = I.getData();
823 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +0000824 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000825 }
826
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000827 return store;
828}
829
Zhongxing Xua071eb02008-10-24 06:01:33 +0000830void RegionStoreManager::print(Store store, std::ostream& Out,
831 const char* nl, const char *sep) {
832 llvm::raw_os_ostream OS(Out);
833 RegionBindingsTy B = GetRegionBindings(store);
834 OS << "Store:" << nl;
835
836 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
837 OS << ' '; I.getKey()->print(OS); OS << " : ";
838 I.getData().print(OS); OS << nl;
839 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000840}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000841
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000842const GRState* RegionStoreManager::BindArray(const GRState* St,
843 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000844
845 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +0000846 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000847 assert(T->isArrayType());
848
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000849 // When we are binding the whole array, it always has default value 0.
850 GRStateRef state(St, StateMgr);
Zhongxing Xu13020162008-12-24 07:29:24 +0000851 St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0,
852 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000853
854 Store store = St->getStore();
855
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000856 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
857
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000858 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000859 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000860
861 // Check if the init expr is a StringLiteral.
862 if (isa<loc::MemRegionVal>(Init)) {
863 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
864 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
865 const char* str = S->getStrData();
866 unsigned len = S->getByteLength();
867 unsigned j = 0;
868
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000869 // Copy bytes from the string literal into the target array. Trailing bytes
870 // in the array that are not covered by the string literal are initialized
871 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000872 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000873 if (j >= len)
874 break;
875
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000876 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
877 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
878
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000879 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
880 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000881 }
882
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000883 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000884 }
885
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000886
887 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
888
889 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
890
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000891 for (; i < Size; ++i, ++VI) {
892 // The init list might be shorter than the array decl.
893 if (VI == VE)
894 break;
895
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000896 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000897 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000898
899 if (CAT->getElementType()->isStructureType())
900 St = BindStruct(St, ER, *VI);
901 else
902 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000903 }
904
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000905 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000906}
907
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000908const GRState*
909RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000910 // FIXME: Verify that we should use getRValueType or getLValueType.
911 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000912 assert(T->isStructureType());
913
914 RecordType* RT = cast<RecordType>(T.getTypePtr());
915 RecordDecl* RD = RT->getDecl();
916 assert(RD->isDefinition());
917
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000918 if (V.isUnknown())
919 return KillStruct(St, R);
920
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000921 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000922 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
923 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
924
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000925 for (; FI != FE; ++FI, ++VI) {
926
927 // There may be fewer values than fields only when we are initializing a
928 // struct decl. In this case, mark the region as having default value.
929 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +0000930 GRStateRef state(St, StateMgr);
931 St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0,
932 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000933 break;
934 }
935
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000936 QualType FTy = (*FI)->getType();
937 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
938
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000939 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
940 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000941
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000942 else if (FTy->isArrayType())
943 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000944
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000945 else if (FTy->isStructureType())
946 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000947 }
948
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000949 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000950}
951
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000952const GRState* RegionStoreManager::KillStruct(const GRState* St,
953 const TypedRegion* R){
954 GRStateRef state(St, StateMgr);
955
956 // Kill the struct region because it is assigned "unknown".
957 St = state.add<RegionKills>(R);
958
959 // Set the default value of the struct region to "unknown".
960 St = state.set<RegionDefaultValue>(R, UnknownVal());
961
962 Store store = St->getStore();
963 RegionBindingsTy B = GetRegionBindings(store);
964
965 // Remove all bindings for the subregions of the struct.
966 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
967 const MemRegion* r = I.getKey();
968 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
969 if (sr->isSubRegionOf(R))
970 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +0000971 // FIXME: Maybe we should also remove the bindings for the "views" of the
972 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000973 }
974
975 return StateMgr.MakeStateWithStore(St, store);
976}
977
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000978const GRState* RegionStoreManager::AddRegionView(const GRState* St,
979 const MemRegion* View,
980 const MemRegion* Base) {
981 GRStateRef state(St, StateMgr);
982
983 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000984 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000985 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000986
987 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000988 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000989
990 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000991 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000992}
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000993
994const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
995 const MemRegion* View,
996 const MemRegion* Base) {
997 GRStateRef state(St, StateMgr);
998
999 // Retrieve the region view of the base region.
1000 const RegionViews* d = state.get<RegionViewMap>(Base);
1001
1002 // If the base region has no view, return.
1003 if (!d)
1004 return St;
1005
1006 // Remove the view.
1007 RegionViews V = *d;
1008 V = RVFactory.Remove(V, View);
1009
1010 return state.set<RegionViewMap>(Base, V);
1011}