blob: 598107c1ea53c08d3c4fcdeb091c660480c19c33 [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//
39typedef llvm::ImmutableList<const MemRegion*> RegionViews;
40namespace { 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//
71// RegionStore lazily adds value bindings to regions when the analyzer
72// handles assignment statements. Killsets track which default values have
73// been killed, thus distinguishing between "unknown" values and default
74// values.
75//
76namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek2ed14be2008-12-05 00:47:52 +000077static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000078namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000079 template<> struct GRStateTrait<RegionKills>
Ted Kremenek50dc1b32008-12-24 01:05:03 +000080 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000081 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000082 };
83}
84
Ted Kremenek50dc1b32008-12-24 01:05:03 +000085//===----------------------------------------------------------------------===//
86// Regions with default values of '0'.
87//===----------------------------------------------------------------------===//
88//
89// This GDM entry tracks what regions have a default value of 0 if they
90// have no bound value and have not been killed.
91//
92namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
93static int RegionDefaultValueIndex = 0;
94namespace clang {
95 template<> struct GRStateTrait<RegionDefaultValue>
96 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
97 static void* GDMIndex() { return &RegionDefaultValueIndex; }
98 };
99}
100
101//===----------------------------------------------------------------------===//
102// Main RegionStore logic.
103//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000104
Zhongxing Xu17892752008-10-08 02:50:44 +0000105namespace {
106
107class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
108 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000109 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000110
Zhongxing Xu17892752008-10-08 02:50:44 +0000111 GRStateManager& StateMgr;
Zhongxing Xu17892752008-10-08 02:50:44 +0000112
113public:
114 RegionStoreManager(GRStateManager& mgr)
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000115 : StoreManager(mgr.getAllocator()),
116 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000117 RVFactory(mgr.getAllocator()),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000118 StateMgr(mgr) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000119
120 virtual ~RegionStoreManager() {}
121
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000122 MemRegionManager& getRegionManager() { return MRMgr; }
Ted Kremenek4f090272008-10-27 21:54:31 +0000123
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000124 const GRState* BindCompoundLiteral(const GRState* St,
125 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000126
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000127 /// getLValueString - Returns an SVal representing the lvalue of a
128 /// StringLiteral. Within RegionStore a StringLiteral has an
129 /// associated StringRegion, and the lvalue of a StringLiteral is
130 /// the lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000131 SVal getLValueString(const GRState* St, const StringLiteral* S);
132
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000133 /// getLValueCompoundLiteral - Returns an SVal representing the
134 /// lvalue of a compound literal. Within RegionStore a compound
135 /// literal has an associated region, and the lvalue of the
136 /// compound literal is the lvalue of that region.
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000137 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
138
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000139 /// getLValueVar - Returns an SVal that represents the lvalue of a
140 /// variable. Within RegionStore a variable has an associated
141 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000142 SVal getLValueVar(const GRState* St, const VarDecl* VD);
143
144 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
145
146 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
147
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000148 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
149
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000150 SVal getSizeInElements(const GRState* St, const MemRegion* R);
151
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000152 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
153 /// type. 'Array' represents the lvalue of the array being decayed
154 /// to a pointer, and the returned SVal represents the decayed
155 /// version of that lvalue (i.e., a pointer to the first element of
156 /// the array). This is called by GRExprEngine when evaluating
157 /// casts from arrays to pointers.
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000158 SVal ArrayToPointer(SVal Array);
159
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000160 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
161 /// a MemRegion* to a specific location type. 'R' is the region being
162 /// casted and 'CastToTy' the result type of the cast.
163 CastResult CastRegion(const GRState* state, const MemRegion* R,
164 QualType CastToTy);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000165
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000166 /// The high level logic for this method is this:
167 /// Retrieve (L)
168 /// if L has binding
169 /// return L's binding
170 /// else if L is in killset
171 /// return unknown
172 /// else
173 /// if L is on stack or heap
174 /// return undefined
175 /// else
176 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000177 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000178
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000179 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000180
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000181 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000182
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000183 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000184
185 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
186 /// 'this' object (C++). When used when analyzing a normal function this
187 /// method returns NULL.
188 const MemRegion* getSelfRegion(Store) {
189 assert (false && "Not implemented.");
190 return 0;
191 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000192
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000193 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
194 /// It returns a new Store with these values removed, and populates LSymbols
195 // and DSymbols with the known set of live and dead symbols respectively.
196 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
197 const LiveVariables& Live,
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000198 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000199 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000200
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000201 void UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000202
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000203 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
204
205 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
206 return St;
207 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000208
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000209 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
210
Zhongxing Xu17892752008-10-08 02:50:44 +0000211 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000212 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000213 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000214
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000215 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000216
217 void iterBindings(Store store, BindingsHandler& f) {
218 // FIXME: Implement.
219 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000220
221private:
222 Loc getVarLoc(const VarDecl* VD) {
223 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
224 }
225
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000226 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000227
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000228 /// Retrieve the values in a struct and return a CompoundVal, used when doing
229 /// struct copy:
230 /// struct s x, y;
231 /// x = y;
232 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000233 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000234
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000235 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000236
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000237 // Utility methods.
238 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
239 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000240 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000241
242 const GRState* AddRegionView(const GRState* St,
243 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000244};
245
246} // end anonymous namespace
247
Ted Kremenek95c7b002008-10-24 01:04:59 +0000248StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000249 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000250}
251
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000252
253/// getLValueString - Returns an SVal representing the lvalue of a
254/// StringLiteral. Within RegionStore a StringLiteral has an
255/// associated StringRegion, and the lvalue of a StringLiteral is the
256/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000257SVal RegionStoreManager::getLValueString(const GRState* St,
258 const StringLiteral* S) {
259 return loc::MemRegionVal(MRMgr.getStringRegion(S));
260}
261
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000262/// getLValueVar - Returns an SVal that represents the lvalue of a
263/// variable. Within RegionStore a variable has an associated
264/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000265SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
266 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
267}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000268
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000269/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
270/// of a compound literal. Within RegionStore a compound literal
271/// has an associated region, and the lvalue of the compound literal
272/// is the lvalue of that region.
273SVal
274RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
275 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000276 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
277}
278
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000279SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
280 SVal Base) {
281 return UnknownVal();
282}
283
284SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
285 const FieldDecl* D) {
286 if (Base.isUnknownOrUndef())
287 return Base;
288
289 Loc BaseL = cast<Loc>(Base);
290 const MemRegion* BaseR = 0;
291
292 switch (BaseL.getSubKind()) {
293 case loc::MemRegionKind:
294 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
295 break;
296
297 case loc::SymbolValKind:
298 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
299 break;
300
301 case loc::GotoLabelKind:
302 case loc::FuncValKind:
303 // These are anormal cases. Flag an undefined value.
304 return UndefinedVal();
305
306 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000307 // While these seem funny, this can happen through casts.
308 // FIXME: What we should return is the field offset. For example,
309 // add the field offset to the integer value. That way funny things
310 // like this work properly: &(((struct foo *) 0xa)->f)
311 return Base;
312
313 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000314 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000315 return Base;
316 }
317
318 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
319}
320
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000321SVal RegionStoreManager::getLValueElement(const GRState* St,
322 SVal Base, SVal Offset) {
323 if (Base.isUnknownOrUndef())
324 return Base;
325
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000326 if (isa<loc::SymbolVal>(Base))
327 return Base;
328
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000329 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
330
Zhongxing Xue4d13932008-11-13 09:48:44 +0000331 // Pointer of any type can be cast and used as array base. We do not support
332 // that case yet.
333 if (!isa<ElementRegion>(BaseL.getRegion())) {
334 // Record what we have seen in real code.
335 assert(isa<FieldRegion>(BaseL.getRegion()));
336 return UnknownVal();
337 }
338
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000339 // We expect BaseR is an ElementRegion, not a base VarRegion.
340
341 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
342
343 SVal Idx = ElemR->getIndex();
344
345 nonloc::ConcreteInt *CI1, *CI2;
346
347 // Only handle integer indices for now.
348 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
349 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000350
Sebastian Redle95db4f2008-11-24 19:35:33 +0000351 // Temporary SVal to hold a potential signed and extended APSInt.
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000352 SVal SignedInt;
353
Sebastian Redle95db4f2008-11-24 19:35:33 +0000354 // Index might be unsigned. We have to convert it to signed. It might also
355 // be less wide than the size. We have to extend it.
356 if (CI2->getValue().isUnsigned() ||
357 CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000358 llvm::APSInt SI = CI2->getValue();
Sebastian Redlddee68b2008-11-24 19:39:40 +0000359 if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth())
360 SI.extend(CI1->getValue().getBitWidth());
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000361 SI.setIsSigned(true);
362 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
363 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
364 }
365
Zhongxing Xu63123d82008-11-23 04:30:35 +0000366 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000367 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
Ted Kremenekabb042f2008-12-13 19:24:37 +0000368 ElemR->getArrayRegion()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000369 }
370
371 return UnknownVal();
372}
373
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000374SVal RegionStoreManager::getSizeInElements(const GRState* St,
375 const MemRegion* R) {
376 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
377 // Get the type of the variable.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000378 QualType T = VR->getRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000379
380 // It must be of array type.
381 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
382
383 // return the size as signed integer.
384 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
385 }
386
387 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000388 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000389 // We intentionally made the size value signed because it participates in
390 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000391 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000392 }
393
394 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000395 GRStateRef state(St, StateMgr);
396
397 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000398 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
399 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000400
401 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000402 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000403
404 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000405 QualType LvT = ATR->getLValueType(getContext());
406 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000407
408 uint64_t X = getContext().getTypeSize(ElemTy);
409
410 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
411 false);
412
413 // Calculate the number of elements.
414
415 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
416 // signed?
417 if (SSize.isUnsigned())
418 SSize.setIsSigned(true);
419
420 // FIXME: move this operation into BasicVals.
421 const llvm::APSInt S =
422 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
423
424 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000425 }
426
427 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
428 // FIXME: Unsupported yet.
429 FR = 0;
430 return UnknownVal();
431 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000432
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000433 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000434 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000435}
436
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000437/// ArrayToPointer - Emulates the "decay" of an array to a pointer
438/// type. 'Array' represents the lvalue of the array being decayed
439/// to a pointer, and the returned SVal represents the decayed
440/// version of that lvalue (i.e., a pointer to the first element of
441/// the array). This is called by GRExprEngine when evaluating casts
442/// from arrays to pointers.
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000443SVal RegionStoreManager::ArrayToPointer(SVal Array) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000444 // FIXME: This should be factored into GRExprEngine. This allows
445 // us to pass a "loc" instead of an "SVal" for "Array".
Ted Kremenekabb042f2008-12-13 19:24:37 +0000446 if (Array.isUnknownOrUndef())
447 return Array;
448
449 if (!isa<loc::MemRegionVal>(Array))
450 return UnknownVal();
451
452 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
453 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
454
455 if (ArrayR)
456 return UnknownVal();
457
Zhongxing Xu63123d82008-11-23 04:30:35 +0000458 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000459 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
460
461 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000462}
463
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000464StoreManager::CastResult
465RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
466 QualType CastToTy) {
467
468 // Return the same region if the region types are compatible.
469 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
470 ASTContext& Ctx = StateMgr.getContext();
471 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
472 QualType Tb = Ctx.getCanonicalType(CastToTy);
473
474 if (Ta == Tb)
475 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000476 }
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000477
478 // FIXME: We should handle the case when we are casting *back* to a
479 // previous type. For example:
480 //
481 // void* x = ...;
482 // char* y = (char*) x;
483 // void* z = (void*) y; // <-- we should get the same region that is
484 // bound to 'x'
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000485 const MemRegion* ViewR = MRMgr.getAnonTypedRegion(CastToTy, R);
486 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000487}
488
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000489SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000490 assert(!isa<UnknownVal>(L) && "location unknown");
491 assert(!isa<UndefinedVal>(L) && "location undefined");
492
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000493 // FIXME: What does loc::SymbolVal represent? It represents the value
494 // of a location but that value is not known. In the future we should
495 // handle potential aliasing relationships; e.g. a loc::SymbolVal could
496 // be an alias for a particular region.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000497 if (isa<loc::SymbolVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000498 return UnknownVal();
499
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000500 // FIXME: Is this even possible? Shouldn't this be treated as a null
501 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000502 if (isa<loc::ConcreteInt>(L))
503 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000504
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000505 // FIXME: Should this be refactored into GRExprEngine or GRStateManager?
506 // It seems that all StoreManagers would do the same thing here.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000507 if (isa<loc::FuncVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000508 return L;
509
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000510 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
511 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000512 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
513 assert(R && "bad region");
514
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000515 // FIXME: We should eventually handle funny addressing. e.g.:
516 //
517 // int x = ...;
518 // int *p = &x;
519 // char *q = (char*) p;
520 // char c = *q; // returns the first byte of 'x'.
521 //
522 // Such funny addressing will occur due to layering of regions.
523
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000524 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
525 if (TR->getRValueType(getContext())->isStructureType())
526 return RetrieveStruct(St, TR);
527
528 RegionBindingsTy B = GetRegionBindings(St->getStore());
529 RegionBindingsTy::data_type* V = B.lookup(R);
530
531 // Check if the region has a binding.
532 if (V)
533 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000534
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000535 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000536
537 // FIXME: Do we even need a killset? If 'Unknown' is explicitly
538 // bound to to a region won't this be enough? (that's basically
539 // what a killset is). RemoveDeadBindings should only remove
540 // bindings that are no longer accessible, which means that won't
541 // ever be read.
542
543 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000544 if (state.contains<RegionKills>(R))
545 return UnknownVal();
546
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000547 // The location does not have a bound value. This means that it has
548 // the value it had upon its creation and/or entry to the analyzed
549 // function/method. These are either symbolic values or 'undefined'.
550
551 // We treat function parameters as symbolic values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000552 if (const VarRegion* VR = dyn_cast<VarRegion>(R))
553 if (isa<ParmVarDecl>(VR->getDecl()))
554 return SVal::MakeSymbolValue(getSymbolManager(), VR,
555 VR->getRValueType(getContext()));
556
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000557 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
558 // All stack variables are considered to have undefined values
559 // upon creation. All heap allocated blocks are considered to
560 // have undefined values as well unless they are explicitly bound
561 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000562 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000563 }
564
565 // All other values are symbolic.
566 return SVal::MakeSymbolValue(getSymbolManager(), R,
567 cast<TypedRegion>(R)->getRValueType(getContext()));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000568
569 // FIXME: consider default values for elements and fields.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000570}
571
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000572SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
573
574 Store store = St->getStore();
575 GRStateRef state(St, StateMgr);
576
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000577 // FIXME: Verify we want getRValueType instead of getLValueType.
578 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000579 assert(T->isStructureType());
580
581 const RecordType* RT = cast<RecordType>(T.getTypePtr());
582 RecordDecl* RD = RT->getDecl();
583 assert(RD->isDefinition());
584
585 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
586
Douglas Gregore267ff32008-12-11 20:41:00 +0000587 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000588
Douglas Gregore267ff32008-12-11 20:41:00 +0000589 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
590 FieldEnd = Fields.rend();
591 Field != FieldEnd; ++Field) {
592 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000593 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000594 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000595
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000596 SVal FieldValue;
597 if (data)
598 FieldValue = *data;
599 else if (state.contains<RegionKills>(FR))
600 FieldValue = UnknownVal();
601 else {
602 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
603 FieldValue = UndefinedVal();
604 else
605 FieldValue = SVal::MakeSymbolValue(getSymbolManager(), FR,
606 FR->getRValueType(getContext()));
607 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000608
609 StructVal = getBasicVals().consVals(FieldValue, StructVal);
610 }
611
612 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
613}
614
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000615const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
616 // Currently we don't bind value to symbolic location. But if the logic is
617 // made clear, we might change this decision.
618 if (isa<loc::SymbolVal>(L))
619 return St;
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000620
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000621 // If we get here, the location should be a region.
622 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000623 assert(R);
624
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000625 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000626 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000627 // FIXME: Verify we want getRValueType().
628 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000629 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000630
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000631 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000632 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000633
634 if (V.isUnknown()) {
635 // Remove the binding.
636 store = RBFactory.Remove(B, R).getRoot();
637
638 // Add the region to the killset.
639 GRStateRef state(St, StateMgr);
640 St = state.add<RegionKills>(R);
641 }
642 else
643 store = RBFactory.Add(B, R, V).getRoot();
644
645 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000646}
647
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000648Store RegionStoreManager::Remove(Store store, Loc L) {
649 RegionBindingsTy B = GetRegionBindings(store);
650
651 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
652 assert(R);
653
654 return RBFactory.Remove(B, R).getRoot();
655}
656
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000657const GRState* RegionStoreManager::BindDecl(const GRState* St,
658 const VarDecl* VD, SVal InitVal) {
659 // All static variables are treated as symbolic values.
660 if (VD->hasGlobalStorage())
661 return St;
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000662
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000663 // Process local variables.
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000664
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000665 QualType T = VD->getType();
666
667 VarRegion* VR = MRMgr.getVarRegion(VD);
668
669 if (Loc::IsLocType(T) || T->isIntegerType())
670 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000671
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000672 else if (T->isArrayType())
673 return BindArray(St, VR, InitVal);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000674
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000675 else if (T->isStructureType())
676 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000677
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000678 // Other types of variable are not supported yet.
Zhongxing Xu17892752008-10-08 02:50:44 +0000679 return St;
680}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000681
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000682// FIXME: this method should be merged into Bind().
683const GRState*
684RegionStoreManager::BindCompoundLiteral(const GRState* St,
685 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000686 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000687 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000688}
689
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000690const GRState* RegionStoreManager::setExtent(const GRState* St,
691 const MemRegion* R, SVal Extent) {
692 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000693 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000694}
695
696
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000697void RegionStoreManager::UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols) {
698 for (SVal::symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
699 LSymbols.insert(*SI);
700}
701
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000702Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000703 const LiveVariables& Live,
704 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
705 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
706
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000707 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000708 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000709
710 // Lazily constructed backmap from MemRegions to SubRegions.
711 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
712 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
713
714 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
715 // the ability to reuse memory. This way we can keep TmpAlloc around as
716 // an instance variable of RegionStoreManager (avoiding repeated malloc
717 // overhead).
718 llvm::BumpPtrAllocator TmpAlloc;
719
720 // Factory objects.
721 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
722 SubRegionsTy::Factory SubRegF(TmpAlloc);
723
724 // The backmap from regions to subregions.
725 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
726
727 // Do a pass over the regions in the store. For VarRegions we check if
728 // the variable is still live and if so add it to the list of live roots.
729 // For other regions we populate our region backmap.
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000730 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000731 const MemRegion* R = I.getKey();
732 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
733 if (Live.isLive(Loc, VR->getDecl()))
734 RegionRoots.push_back(VR); // This is a live "root".
735 }
736 else {
737 // Get the super region for R.
738 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
739 // Get the current set of subregions for SuperR.
740 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
741 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
742 // Add R to the subregions of SuperR.
743 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
744
745 // Finally, check if SuperR is a VarRegion. We need to do this
746 // to also mark SuperR as a root (as it may not have a value directly
747 // bound to it in the store).
748 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
749 if (Live.isLive(Loc, VR->getDecl()))
750 RegionRoots.push_back(VR); // This is a live "root".
751 }
752 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000753 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000754
755 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
756 // of the store. We want to find all live symbols and dead regions.
757 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
758
759 while (!RegionRoots.empty()) {
760 // Dequeue the next region on the worklist.
761 const MemRegion* R = RegionRoots.back();
762 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000763
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000764 // Check if we have already processed this region.
765 if (Marked.count(R)) continue;
766
767 // Mark this region as processed. This is needed for termination in case
768 // a region is referenced more than once.
769 Marked.insert(R);
770
771 // Mark the symbol for any live SymbolicRegion as "live". This means we
772 // should continue to track that symbol.
773 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
774 LSymbols.insert(SymR->getSymbol());
775
776 // Get the data binding for R (if any).
777 RegionBindingsTy::data_type* Xptr = B.lookup(R);
778 if (Xptr) {
779 SVal X = *Xptr;
780 UpdateLiveSymbols(X, LSymbols); // Update the set of live symbols.
781
782 // If X is a region, then add it the RegionRoots.
783 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
784 RegionRoots.push_back(RegionX->getRegion());
785 }
786
787 // Get the subregions of R. These are RegionRoots as well since they
788 // represent values that are also bound to R.
789 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
790 if (!SRptr) continue;
791 SubRegionsTy SR = *SRptr;
792
793 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
794 RegionRoots.push_back(*I);
795 }
796
797 // We have now scanned the store, marking reachable regions and symbols
798 // as live. We now remove all the regions that are dead from the store
799 // as well as update DSymbols with the set symbols that are now dead.
800
801 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
802 const MemRegion* R = I.getKey();
803
804 // If this region live? Is so, none of its symbols are dead.
805 if (Marked.count(R))
806 continue;
807
808 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000809 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000810
811 // Mark all non-live symbols that this region references as dead.
812 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000813 SymbolRef Sym = SymR->getSymbol();
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000814 if (!LSymbols.count(Sym)) DSymbols.insert(Sym);
815 }
816
817 SVal X = I.getData();
818 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
819 for (; SI != SE; ++SI) { if (!LSymbols.count(*SI)) DSymbols.insert(*SI); }
820 }
821
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000822 return store;
823}
824
Zhongxing Xua071eb02008-10-24 06:01:33 +0000825void RegionStoreManager::print(Store store, std::ostream& Out,
826 const char* nl, const char *sep) {
827 llvm::raw_os_ostream OS(Out);
828 RegionBindingsTy B = GetRegionBindings(store);
829 OS << "Store:" << nl;
830
831 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
832 OS << ' '; I.getKey()->print(OS); OS << " : ";
833 I.getData().print(OS); OS << nl;
834 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000835}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000836
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000837const GRState* RegionStoreManager::BindArray(const GRState* St,
838 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000839
840 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +0000841 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000842 assert(T->isArrayType());
843
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000844 // When we are binding the whole array, it always has default value 0.
845 GRStateRef state(St, StateMgr);
Zhongxing Xu13020162008-12-24 07:29:24 +0000846 St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0,
847 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000848
849 Store store = St->getStore();
850
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000851 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
852
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000853 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000854 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000855
856 // Check if the init expr is a StringLiteral.
857 if (isa<loc::MemRegionVal>(Init)) {
858 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
859 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
860 const char* str = S->getStrData();
861 unsigned len = S->getByteLength();
862 unsigned j = 0;
863
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000864 // Copy bytes from the string literal into the target array. Trailing bytes
865 // in the array that are not covered by the string literal are initialized
866 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000867 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000868 if (j >= len)
869 break;
870
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000871 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
872 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
873
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000874 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
875 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000876 }
877
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000878 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000879 }
880
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000881
882 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
883
884 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
885
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000886 for (; i < Size; ++i, ++VI) {
887 // The init list might be shorter than the array decl.
888 if (VI == VE)
889 break;
890
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000891 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000892 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000893
894 if (CAT->getElementType()->isStructureType())
895 St = BindStruct(St, ER, *VI);
896 else
897 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000898 }
899
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000900 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000901}
902
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000903const GRState*
904RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000905 // FIXME: Verify that we should use getRValueType or getLValueType.
906 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000907 assert(T->isStructureType());
908
909 RecordType* RT = cast<RecordType>(T.getTypePtr());
910 RecordDecl* RD = RT->getDecl();
911 assert(RD->isDefinition());
912
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000913 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000914 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
915 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
916
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000917 for (; FI != FE; ++FI, ++VI) {
918
919 // There may be fewer values than fields only when we are initializing a
920 // struct decl. In this case, mark the region as having default value.
921 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +0000922 GRStateRef state(St, StateMgr);
923 St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0,
924 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000925 break;
926 }
927
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000928 QualType FTy = (*FI)->getType();
929 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
930
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000931 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
932 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000933
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000934 else if (FTy->isArrayType())
935 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000936
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000937 else if (FTy->isStructureType())
938 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000939 }
940
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000941 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000942}
943
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000944const GRState* RegionStoreManager::AddRegionView(const GRState* St,
945 const MemRegion* View,
946 const MemRegion* Base) {
947 GRStateRef state(St, StateMgr);
948
949 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000950 const RegionViews* d = state.get<RegionViewMap>(Base);
951 RegionViews L = d ? *d : RVFactory.GetEmptyList();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000952
953 // Now add View to the region view.
954 L = RVFactory.Add(View, L);
955
956 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000957 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000958}