blob: 602d6bde1d3553b2bf8857c94df1d24ec2f43a5f [file] [log] [blame]
Zhongxing Xu79c57f82008-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 Xu8fbe7ae2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu79c57f82008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xuca892b82008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu79c57f82008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +000029// Actual Store type.
Zhongxing Xu097fc982008-10-17 05:57:07 +000030typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +000031
Ted Kremenek721c6f22008-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 Xu8fbe7ae2008-11-16 04:07:26 +000042namespace clang {
Ted Kremenek721c6f22008-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 Xu8fbe7ae2008-11-16 04:07:26 +000049}
Zhongxing Xu79c57f82008-10-08 02:50:44 +000050
Ted Kremenek721c6f22008-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.
57namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
58static int RegionExtentsIndex = 0;
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +000059namespace clang {
Ted Kremenek721c6f22008-12-24 01:05:03 +000060 template<> struct GRStateTrait<RegionExtents>
61 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
62 static void* GDMIndex() { return &RegionExtentsIndex; }
63 };
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +000064}
65
Ted Kremenek721c6f22008-12-24 01:05:03 +000066//===----------------------------------------------------------------------===//
67// Region "killsets".
68//===----------------------------------------------------------------------===//
69//
70// RegionStore lazily adds value bindings to regions when the analyzer
71// handles assignment statements. Killsets track which default values have
72// been killed, thus distinguishing between "unknown" values and default
73// values.
74//
75namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek0de2e0f2008-12-05 00:47:52 +000076static int RegionKillsIndex = 0;
Ted Kremenek479553b2008-12-04 02:08:27 +000077namespace clang {
Ted Kremenek0de2e0f2008-12-05 00:47:52 +000078 template<> struct GRStateTrait<RegionKills>
Ted Kremenek721c6f22008-12-24 01:05:03 +000079 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek0de2e0f2008-12-05 00:47:52 +000080 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenek479553b2008-12-04 02:08:27 +000081 };
82}
83
Ted Kremenek721c6f22008-12-24 01:05:03 +000084//===----------------------------------------------------------------------===//
85// Regions with default values of '0'.
86//===----------------------------------------------------------------------===//
87//
88// This GDM entry tracks what regions have a default value of 0 if they
89// have no bound value and have not been killed.
90//
91namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
92static int RegionDefaultValueIndex = 0;
93namespace clang {
94 template<> struct GRStateTrait<RegionDefaultValue>
95 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
96 static void* GDMIndex() { return &RegionDefaultValueIndex; }
97 };
98}
99
100//===----------------------------------------------------------------------===//
101// Main RegionStore logic.
102//===----------------------------------------------------------------------===//
Ted Kremenek479553b2008-12-04 02:08:27 +0000103
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000104namespace {
105
106class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
107 RegionBindingsTy::Factory RBFactory;
Ted Kremenek721c6f22008-12-24 01:05:03 +0000108 RegionViews::Factory RVFactory;
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000109
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000110 GRStateManager& StateMgr;
111 MemRegionManager MRMgr;
112
113public:
114 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000115 : RBFactory(mgr.getAllocator()),
116 RVFactory(mgr.getAllocator()),
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000117 StateMgr(mgr),
118 MRMgr(StateMgr.getAllocator()) {}
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000119
120 virtual ~RegionStoreManager() {}
121
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000122 MemRegionManager& getRegionManager() { return MRMgr; }
Ted Kremenekd83daa52008-10-27 21:54:31 +0000123
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000124 const GRState* BindCompoundLiteral(const GRState* St,
125 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000126
Ted Kremeneka793e1d2008-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 Xu2abba442008-10-25 14:18:57 +0000131 SVal getLValueString(const GRState* St, const StringLiteral* S);
132
Ted Kremeneka793e1d2008-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 Xuc88ca9d2008-11-07 10:38:33 +0000137 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
138
Ted Kremeneka793e1d2008-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 Xu6f1b5152008-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 Xu0972d0a2008-10-24 01:09:32 +0000148 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
149
Zhongxing Xud52b8cf2008-11-22 13:21:46 +0000150 SVal getSizeInElements(const GRState* St, const MemRegion* R);
151
Ted Kremeneka793e1d2008-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 Xu0972d0a2008-10-24 01:09:32 +0000158 SVal ArrayToPointer(SVal Array);
159
Ted Kremenekf5da3252008-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 Xu8fbe7ae2008-11-16 04:07:26 +0000165
Zhongxing Xu5ea4ad02008-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 Kremenek0de2e0f2008-12-05 00:47:52 +0000177 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000178
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000179 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000180
Zhongxing Xud980b142008-12-16 02:36:30 +0000181 Store Remove(Store store, Loc LV);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000182
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000183 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek73a36c92008-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 Kremenek479553b2008-12-04 02:08:27 +0000192
Ted Kremenek0de2e0f2008-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 Xue4b6fc22008-10-24 01:38:55 +0000198 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xuab42da32008-11-10 09:39:04 +0000199 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000200
Ted Kremenek479553b2008-12-04 02:08:27 +0000201 void UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000202
Zhongxing Xu5ea4ad02008-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 Xue3954d12008-10-21 05:29:26 +0000208
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +0000209 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
210
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000211 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xud980b142008-12-16 02:36:30 +0000212 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000213 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000214
Zhongxing Xu6149e882008-10-24 04:33:15 +0000215 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000216
217 void iterBindings(Store store, BindingsHandler& f) {
218 // FIXME: Implement.
219 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000220
221private:
222 Loc getVarLoc(const VarDecl* VD) {
223 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
224 }
225
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000226 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000227
Zhongxing Xuc3e0c8b2008-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 Xu5ea4ad02008-12-20 06:32:12 +0000233 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xuc3e0c8b2008-12-04 01:12:41 +0000234
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000235 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xuf4205df2008-11-23 04:30:35 +0000236
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000237 // Utility methods.
238 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
239 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xuf4205df2008-11-23 04:30:35 +0000240 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000241
242 const GRState* AddRegionView(const GRState* St,
243 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000244};
245
246} // end anonymous namespace
247
Ted Kremenekc3803992008-10-24 01:04:59 +0000248StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000249 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000250}
251
Ted Kremeneka793e1d2008-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 Xu2abba442008-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 Kremeneka793e1d2008-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 Xu6f1b5152008-10-22 13:44:38 +0000265SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
266 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
267}
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000268
Ted Kremeneka793e1d2008-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 Xuc88ca9d2008-11-07 10:38:33 +0000276 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
277}
278
Zhongxing Xu6f1b5152008-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 Xu6f1b5152008-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 Xuedfbcd92008-11-07 08:57:30 +0000314 assert(0 && "Unhandled Base.");
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000315 return Base;
316 }
317
318 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
319}
320
Zhongxing Xu0972d0a2008-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 Xu13a05fa2008-10-27 12:23:17 +0000326 if (isa<loc::SymbolVal>(Base))
327 return Base;
328
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000329 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
330
Zhongxing Xu853c6f62008-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 Xu0972d0a2008-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 Xuec12b812008-11-13 09:15:14 +0000350
Sebastian Redl23b7aa82008-11-24 19:35:33 +0000351 // Temporary SVal to hold a potential signed and extended APSInt.
Zhongxing Xuec12b812008-11-13 09:15:14 +0000352 SVal SignedInt;
353
Sebastian Redl23b7aa82008-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 Xuec12b812008-11-13 09:15:14 +0000358 llvm::APSInt SI = CI2->getValue();
Sebastian Redld5ea6d92008-11-24 19:39:40 +0000359 if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth())
360 SI.extend(CI1->getValue().getBitWidth());
Zhongxing Xuec12b812008-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 Xuf4205df2008-11-23 04:30:35 +0000366 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000367 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
Ted Kremenek2c0de352008-12-13 19:24:37 +0000368 ElemR->getArrayRegion()));
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000369 }
370
371 return UnknownVal();
372}
373
Zhongxing Xud52b8cf2008-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 Kremenekf5da3252008-12-13 21:49:13 +0000378 QualType T = VR->getRValueType(getContext());
Zhongxing Xud52b8cf2008-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 Xu83cae2c2008-11-24 02:18:56 +0000388 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xu08360be2008-11-24 02:30:48 +0000389 // We intentionally made the size value signed because it participates in
390 // operations with signed indices.
Zhongxing Xu910eee62008-11-24 05:16:01 +0000391 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xud52b8cf2008-11-22 13:21:46 +0000392 }
393
394 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +0000395 GRStateRef state(St, StateMgr);
396
397 // Get the size of the super region in bytes.
Ted Kremenek721c6f22008-12-24 01:05:03 +0000398 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
399 assert(Extent && "region extent not exist");
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +0000400
401 // Assume it's ConcreteInt for now.
Ted Kremenek721c6f22008-12-24 01:05:03 +0000402 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +0000403
404 // Get the size of the element in bits.
Ted Kremenekf5da3252008-12-13 21:49:13 +0000405 QualType LvT = ATR->getLValueType(getContext());
406 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xu2ca0d6e2008-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 Xud52b8cf2008-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 Xu8e676d22008-11-22 13:23:00 +0000432
Zhongxing Xud52b8cf2008-11-22 13:21:46 +0000433 assert(0 && "Other regions are not supported yet.");
434}
435
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000436/// ArrayToPointer - Emulates the "decay" of an array to a pointer
437/// type. 'Array' represents the lvalue of the array being decayed
438/// to a pointer, and the returned SVal represents the decayed
439/// version of that lvalue (i.e., a pointer to the first element of
440/// the array). This is called by GRExprEngine when evaluating casts
441/// from arrays to pointers.
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000442SVal RegionStoreManager::ArrayToPointer(SVal Array) {
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000443 // FIXME: This should be factored into GRExprEngine. This allows
444 // us to pass a "loc" instead of an "SVal" for "Array".
Ted Kremenek2c0de352008-12-13 19:24:37 +0000445 if (Array.isUnknownOrUndef())
446 return Array;
447
448 if (!isa<loc::MemRegionVal>(Array))
449 return UnknownVal();
450
451 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
452 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
453
454 if (ArrayR)
455 return UnknownVal();
456
Zhongxing Xuf4205df2008-11-23 04:30:35 +0000457 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu5ac8bf12008-10-26 02:23:57 +0000458 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
459
460 return loc::MemRegionVal(ER);
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000461}
462
Ted Kremenekf5da3252008-12-13 21:49:13 +0000463StoreManager::CastResult
464RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
465 QualType CastToTy) {
466
467 // Return the same region if the region types are compatible.
468 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
469 ASTContext& Ctx = StateMgr.getContext();
470 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
471 QualType Tb = Ctx.getCanonicalType(CastToTy);
472
473 if (Ta == Tb)
474 return CastResult(state, R);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000475 }
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000476
477 // FIXME: We should handle the case when we are casting *back* to a
478 // previous type. For example:
479 //
480 // void* x = ...;
481 // char* y = (char*) x;
482 // void* z = (void*) y; // <-- we should get the same region that is
483 // bound to 'x'
Ted Kremenekf5da3252008-12-13 21:49:13 +0000484 const MemRegion* ViewR = MRMgr.getAnonTypedRegion(CastToTy, R);
485 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000486}
487
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000488SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000489 assert(!isa<UnknownVal>(L) && "location unknown");
490 assert(!isa<UndefinedVal>(L) && "location undefined");
491
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000492 // FIXME: What does loc::SymbolVal represent? It represents the value
493 // of a location but that value is not known. In the future we should
494 // handle potential aliasing relationships; e.g. a loc::SymbolVal could
495 // be an alias for a particular region.
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000496 if (isa<loc::SymbolVal>(L))
Zhongxing Xue3954d12008-10-21 05:29:26 +0000497 return UnknownVal();
498
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000499 // FIXME: Is this even possible? Shouldn't this be treated as a null
500 // dereference at a higher level?
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000501 if (isa<loc::ConcreteInt>(L))
502 return UndefinedVal();
Zhongxing Xue3954d12008-10-21 05:29:26 +0000503
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000504 // FIXME: Should this be refactored into GRExprEngine or GRStateManager?
505 // It seems that all StoreManagers would do the same thing here.
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000506 if (isa<loc::FuncVal>(L))
Zhongxing Xue3954d12008-10-21 05:29:26 +0000507 return L;
508
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000509 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
510 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000511 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
512 assert(R && "bad region");
513
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000514 // FIXME: We should eventually handle funny addressing. e.g.:
515 //
516 // int x = ...;
517 // int *p = &x;
518 // char *q = (char*) p;
519 // char c = *q; // returns the first byte of 'x'.
520 //
521 // Such funny addressing will occur due to layering of regions.
522
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000523 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
524 if (TR->getRValueType(getContext())->isStructureType())
525 return RetrieveStruct(St, TR);
526
527 RegionBindingsTy B = GetRegionBindings(St->getStore());
528 RegionBindingsTy::data_type* V = B.lookup(R);
529
530 // Check if the region has a binding.
531 if (V)
532 return *V;
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000533
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000534 GRStateRef state(St, StateMgr);
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000535
536 // FIXME: Do we even need a killset? If 'Unknown' is explicitly
537 // bound to to a region won't this be enough? (that's basically
538 // what a killset is). RemoveDeadBindings should only remove
539 // bindings that are no longer accessible, which means that won't
540 // ever be read.
541
542 // Check if the region is in killset.
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000543 if (state.contains<RegionKills>(R))
544 return UnknownVal();
545
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000546 // The location does not have a bound value. This means that it has
547 // the value it had upon its creation and/or entry to the analyzed
548 // function/method. These are either symbolic values or 'undefined'.
549
550 // We treat function parameters as symbolic values.
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000551 if (const VarRegion* VR = dyn_cast<VarRegion>(R))
552 if (isa<ParmVarDecl>(VR->getDecl()))
553 return SVal::MakeSymbolValue(getSymbolManager(), VR,
554 VR->getRValueType(getContext()));
555
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000556 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
557 // All stack variables are considered to have undefined values
558 // upon creation. All heap allocated blocks are considered to
559 // have undefined values as well unless they are explicitly bound
560 // to specific values.
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000561 return UndefinedVal();
Ted Kremeneka793e1d2008-12-24 07:46:32 +0000562 }
563
564 // All other values are symbolic.
565 return SVal::MakeSymbolValue(getSymbolManager(), R,
566 cast<TypedRegion>(R)->getRValueType(getContext()));
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000567
568 // FIXME: consider default values for elements and fields.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000569}
570
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000571SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
572
573 Store store = St->getStore();
574 GRStateRef state(St, StateMgr);
575
Ted Kremenekf5da3252008-12-13 21:49:13 +0000576 // FIXME: Verify we want getRValueType instead of getLValueType.
577 QualType T = R->getRValueType(getContext());
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000578 assert(T->isStructureType());
579
580 const RecordType* RT = cast<RecordType>(T.getTypePtr());
581 RecordDecl* RD = RT->getDecl();
582 assert(RD->isDefinition());
583
584 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
585
Douglas Gregor39677622008-12-11 20:41:00 +0000586 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000587
Douglas Gregor39677622008-12-11 20:41:00 +0000588 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
589 FieldEnd = Fields.rend();
590 Field != FieldEnd; ++Field) {
591 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000592 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu29454f42008-10-31 08:10:01 +0000593 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000594
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000595 SVal FieldValue;
596 if (data)
597 FieldValue = *data;
598 else if (state.contains<RegionKills>(FR))
599 FieldValue = UnknownVal();
600 else {
601 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
602 FieldValue = UndefinedVal();
603 else
604 FieldValue = SVal::MakeSymbolValue(getSymbolManager(), FR,
605 FR->getRValueType(getContext()));
606 }
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000607
608 StructVal = getBasicVals().consVals(FieldValue, StructVal);
609 }
610
611 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
612}
613
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000614const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
615 // Currently we don't bind value to symbolic location. But if the logic is
616 // made clear, we might change this decision.
617 if (isa<loc::SymbolVal>(L))
618 return St;
Zhongxing Xue7c8a132008-10-27 09:24:07 +0000619
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000620 // If we get here, the location should be a region.
621 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xu29454f42008-10-31 08:10:01 +0000622 assert(R);
623
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000624 // Check if the region is a struct region.
Zhongxing Xu29454f42008-10-31 08:10:01 +0000625 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenekf5da3252008-12-13 21:49:13 +0000626 // FIXME: Verify we want getRValueType().
627 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000628 return BindStruct(St, TR, V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000629
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000630 Store store = St->getStore();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000631 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000632
633 if (V.isUnknown()) {
634 // Remove the binding.
635 store = RBFactory.Remove(B, R).getRoot();
636
637 // Add the region to the killset.
638 GRStateRef state(St, StateMgr);
639 St = state.add<RegionKills>(R);
640 }
641 else
642 store = RBFactory.Add(B, R, V).getRoot();
643
644 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000645}
646
Zhongxing Xud980b142008-12-16 02:36:30 +0000647Store RegionStoreManager::Remove(Store store, Loc L) {
648 RegionBindingsTy B = GetRegionBindings(store);
649
650 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
651 assert(R);
652
653 return RBFactory.Remove(B, R).getRoot();
654}
655
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000656const GRState* RegionStoreManager::BindDecl(const GRState* St,
657 const VarDecl* VD, SVal InitVal) {
658 // All static variables are treated as symbolic values.
659 if (VD->hasGlobalStorage())
660 return St;
Zhongxing Xu29454f42008-10-31 08:10:01 +0000661
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000662 // Process local variables.
Zhongxing Xuca1c2522008-11-13 08:41:36 +0000663
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000664 QualType T = VD->getType();
665
666 VarRegion* VR = MRMgr.getVarRegion(VD);
667
668 if (Loc::IsLocType(T) || T->isIntegerType())
669 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu29454f42008-10-31 08:10:01 +0000670
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000671 else if (T->isArrayType())
672 return BindArray(St, VR, InitVal);
Zhongxing Xu29454f42008-10-31 08:10:01 +0000673
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000674 else if (T->isStructureType())
675 return BindStruct(St, VR, InitVal);
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000676
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000677 // Other types of variable are not supported yet.
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000678 return St;
679}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000680
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000681// FIXME: this method should be merged into Bind().
682const GRState*
683RegionStoreManager::BindCompoundLiteral(const GRState* St,
684 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000685 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000686 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000687}
688
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +0000689const GRState* RegionStoreManager::setExtent(const GRState* St,
690 const MemRegion* R, SVal Extent) {
691 GRStateRef state(St, StateMgr);
Ted Kremenek721c6f22008-12-24 01:05:03 +0000692 return state.set<RegionExtents>(R, Extent);
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +0000693}
694
695
Ted Kremenek479553b2008-12-04 02:08:27 +0000696void RegionStoreManager::UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols) {
697 for (SVal::symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
698 LSymbols.insert(*SI);
699}
700
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000701Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Zhongxing Xuab42da32008-11-10 09:39:04 +0000702 const LiveVariables& Live,
703 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
704 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
705
Ted Kremenek0de2e0f2008-12-05 00:47:52 +0000706 Store store = state->getStore();
Zhongxing Xuab42da32008-11-10 09:39:04 +0000707 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenek479553b2008-12-04 02:08:27 +0000708
709 // Lazily constructed backmap from MemRegions to SubRegions.
710 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
711 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
712
713 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
714 // the ability to reuse memory. This way we can keep TmpAlloc around as
715 // an instance variable of RegionStoreManager (avoiding repeated malloc
716 // overhead).
717 llvm::BumpPtrAllocator TmpAlloc;
718
719 // Factory objects.
720 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
721 SubRegionsTy::Factory SubRegF(TmpAlloc);
722
723 // The backmap from regions to subregions.
724 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
725
726 // Do a pass over the regions in the store. For VarRegions we check if
727 // the variable is still live and if so add it to the list of live roots.
728 // For other regions we populate our region backmap.
Zhongxing Xuab42da32008-11-10 09:39:04 +0000729 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek479553b2008-12-04 02:08:27 +0000730 const MemRegion* R = I.getKey();
731 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
732 if (Live.isLive(Loc, VR->getDecl()))
733 RegionRoots.push_back(VR); // This is a live "root".
734 }
735 else {
736 // Get the super region for R.
737 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
738 // Get the current set of subregions for SuperR.
739 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
740 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
741 // Add R to the subregions of SuperR.
742 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
743
744 // Finally, check if SuperR is a VarRegion. We need to do this
745 // to also mark SuperR as a root (as it may not have a value directly
746 // bound to it in the store).
747 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
748 if (Live.isLive(Loc, VR->getDecl()))
749 RegionRoots.push_back(VR); // This is a live "root".
750 }
751 }
Zhongxing Xuab42da32008-11-10 09:39:04 +0000752 }
Ted Kremenek479553b2008-12-04 02:08:27 +0000753
754 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
755 // of the store. We want to find all live symbols and dead regions.
756 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
757
758 while (!RegionRoots.empty()) {
759 // Dequeue the next region on the worklist.
760 const MemRegion* R = RegionRoots.back();
761 RegionRoots.pop_back();
Zhongxing Xuab42da32008-11-10 09:39:04 +0000762
Ted Kremenek479553b2008-12-04 02:08:27 +0000763 // Check if we have already processed this region.
764 if (Marked.count(R)) continue;
765
766 // Mark this region as processed. This is needed for termination in case
767 // a region is referenced more than once.
768 Marked.insert(R);
769
770 // Mark the symbol for any live SymbolicRegion as "live". This means we
771 // should continue to track that symbol.
772 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
773 LSymbols.insert(SymR->getSymbol());
774
775 // Get the data binding for R (if any).
776 RegionBindingsTy::data_type* Xptr = B.lookup(R);
777 if (Xptr) {
778 SVal X = *Xptr;
779 UpdateLiveSymbols(X, LSymbols); // Update the set of live symbols.
780
781 // If X is a region, then add it the RegionRoots.
782 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
783 RegionRoots.push_back(RegionX->getRegion());
784 }
785
786 // Get the subregions of R. These are RegionRoots as well since they
787 // represent values that are also bound to R.
788 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
789 if (!SRptr) continue;
790 SubRegionsTy SR = *SRptr;
791
792 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
793 RegionRoots.push_back(*I);
794 }
795
796 // We have now scanned the store, marking reachable regions and symbols
797 // as live. We now remove all the regions that are dead from the store
798 // as well as update DSymbols with the set symbols that are now dead.
799
800 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
801 const MemRegion* R = I.getKey();
802
803 // If this region live? Is so, none of its symbols are dead.
804 if (Marked.count(R))
805 continue;
806
807 // Remove this dead region from the store.
Zhongxing Xud980b142008-12-16 02:36:30 +0000808 store = Remove(store, Loc::MakeVal(R));
Ted Kremenek479553b2008-12-04 02:08:27 +0000809
810 // Mark all non-live symbols that this region references as dead.
811 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenekb9cd9a72008-12-05 02:27:51 +0000812 SymbolRef Sym = SymR->getSymbol();
Ted Kremenek479553b2008-12-04 02:08:27 +0000813 if (!LSymbols.count(Sym)) DSymbols.insert(Sym);
814 }
815
816 SVal X = I.getData();
817 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
818 for (; SI != SE; ++SI) { if (!LSymbols.count(*SI)) DSymbols.insert(*SI); }
819 }
820
Zhongxing Xuab42da32008-11-10 09:39:04 +0000821 return store;
822}
823
Zhongxing Xuca892b82008-10-24 06:01:33 +0000824void RegionStoreManager::print(Store store, std::ostream& Out,
825 const char* nl, const char *sep) {
826 llvm::raw_os_ostream OS(Out);
827 RegionBindingsTy B = GetRegionBindings(store);
828 OS << "Store:" << nl;
829
830 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
831 OS << ' '; I.getKey()->print(OS); OS << " : ";
832 I.getData().print(OS); OS << nl;
833 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000834}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000835
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000836const GRState* RegionStoreManager::BindArray(const GRState* St,
837 const TypedRegion* R, SVal Init) {
Ted Kremenekf5da3252008-12-13 21:49:13 +0000838
839 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu9d9fd882008-12-14 03:14:52 +0000840 QualType T = R->getRValueType(getContext());
Zhongxing Xub30a0732008-10-31 10:24:47 +0000841 assert(T->isArrayType());
842
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000843 // When we are binding the whole array, it always has default value 0.
844 GRStateRef state(St, StateMgr);
Zhongxing Xu82e4c672008-12-24 07:29:24 +0000845 St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0,
846 false));
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000847
848 Store store = St->getStore();
849
Zhongxing Xub30a0732008-10-31 10:24:47 +0000850 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
851
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000852 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000853 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000854
855 // Check if the init expr is a StringLiteral.
856 if (isa<loc::MemRegionVal>(Init)) {
857 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
858 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
859 const char* str = S->getStrData();
860 unsigned len = S->getByteLength();
861 unsigned j = 0;
862
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000863 // Copy bytes from the string literal into the target array. Trailing bytes
864 // in the array that are not covered by the string literal are initialized
865 // to zero.
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000866 for (; i < Size; ++i, ++j) {
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000867 if (j >= len)
868 break;
869
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000870 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
871 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
872
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000873 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
874 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000875 }
876
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000877 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000878 }
879
Zhongxing Xub30a0732008-10-31 10:24:47 +0000880
881 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
882
883 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
884
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000885 for (; i < Size; ++i, ++VI) {
886 // The init list might be shorter than the array decl.
887 if (VI == VE)
888 break;
889
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000890 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xub30a0732008-10-31 10:24:47 +0000891 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000892
893 if (CAT->getElementType()->isStructureType())
894 St = BindStruct(St, ER, *VI);
895 else
896 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xub30a0732008-10-31 10:24:47 +0000897 }
898
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000899 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xub30a0732008-10-31 10:24:47 +0000900}
901
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000902const GRState*
903RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenekf5da3252008-12-13 21:49:13 +0000904 // FIXME: Verify that we should use getRValueType or getLValueType.
905 QualType T = R->getRValueType(getContext());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000906 assert(T->isStructureType());
907
908 RecordType* RT = cast<RecordType>(T.getTypePtr());
909 RecordDecl* RD = RT->getDecl();
910 assert(RD->isDefinition());
911
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000912 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000913 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
914 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
915
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000916 for (; FI != FE; ++FI, ++VI) {
917
918 // There may be fewer values than fields only when we are initializing a
919 // struct decl. In this case, mark the region as having default value.
920 if (VI == VE) {
Zhongxing Xu82e4c672008-12-24 07:29:24 +0000921 GRStateRef state(St, StateMgr);
922 St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0,
923 false));
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000924 break;
925 }
926
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000927 QualType FTy = (*FI)->getType();
928 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
929
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000930 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
931 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000932
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000933 else if (FTy->isArrayType())
934 St = BindArray(St, FR, *VI);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000935
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000936 else if (FTy->isStructureType())
937 St = BindStruct(St, FR, *VI);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000938 }
939
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000940 return St;
Zhongxing Xu60cfd122008-11-19 11:06:24 +0000941}
942
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000943const GRState* RegionStoreManager::AddRegionView(const GRState* St,
944 const MemRegion* View,
945 const MemRegion* Base) {
946 GRStateRef state(St, StateMgr);
947
948 // First, retrieve the region view of the base region.
Ted Kremenek721c6f22008-12-24 01:05:03 +0000949 const RegionViews* d = state.get<RegionViewMap>(Base);
950 RegionViews L = d ? *d : RVFactory.GetEmptyList();
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000951
952 // Now add View to the region view.
953 L = RVFactory.Add(View, L);
954
955 // Create a new state with the new region view.
Ted Kremenek721c6f22008-12-24 01:05:03 +0000956 return state.set<RegionViewMap>(Base, L);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000957}