blob: a1267928063889b43c2bf43e60cfb2151e71ad32 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xubaf03a72008-11-24 09:44:56 +000029// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000030typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000031
Ted Kremenek50dc1b32008-12-24 01:05:03 +000032//===----------------------------------------------------------------------===//
33// Region "Views"
34//===----------------------------------------------------------------------===//
35//
36// MemRegions can be layered on top of each other. This GDM entry tracks
37// what are the MemRegions that layer a given MemRegion.
38//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000039typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000040namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
41static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000042namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000043 template<> struct GRStateTrait<RegionViewMap>
44 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
45 RegionViews> > {
46
47 static void* GDMIndex() { return &RegionViewMapIndex; }
48 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000049}
Zhongxing Xu17892752008-10-08 02:50:44 +000050
Ted Kremenek50dc1b32008-12-24 01:05:03 +000051//===----------------------------------------------------------------------===//
52// Region "Extents"
53//===----------------------------------------------------------------------===//
54//
55// MemRegions represent chunks of memory with a size (their "extent"). This
56// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000057//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000058namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
59static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000060namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000061 template<> struct GRStateTrait<RegionExtents>
62 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
63 static void* GDMIndex() { return &RegionExtentsIndex; }
64 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000065}
66
Ted Kremenek50dc1b32008-12-24 01:05:03 +000067//===----------------------------------------------------------------------===//
68// Region "killsets".
69//===----------------------------------------------------------------------===//
70//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000071// RegionStore lazily adds value bindings to regions when the analyzer handles
72// assignment statements. Killsets track which default values have been
73// killed, thus distinguishing between "unknown" values and default
74// values. Regions are added to killset only when they are assigned "unknown"
75// directly, otherwise we should have their value in the region bindings.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000076//
77namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek2ed14be2008-12-05 00:47:52 +000078static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000079namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000080 template<> struct GRStateTrait<RegionKills>
Ted Kremenek50dc1b32008-12-24 01:05:03 +000081 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000082 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000083 };
84}
85
Ted Kremenek50dc1b32008-12-24 01:05:03 +000086//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000087// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000088//===----------------------------------------------------------------------===//
89//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000090// This GDM entry tracks what regions have a default value if they have no bound
91// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000092//
93namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
94static int RegionDefaultValueIndex = 0;
95namespace clang {
96 template<> struct GRStateTrait<RegionDefaultValue>
97 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
98 static void* GDMIndex() { return &RegionDefaultValueIndex; }
99 };
100}
101
102//===----------------------------------------------------------------------===//
103// Main RegionStore logic.
104//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000105
Zhongxing Xu17892752008-10-08 02:50:44 +0000106namespace {
107
Ted Kremenek59e8f112009-03-03 01:35:36 +0000108class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
109 typedef llvm::DenseMap<const MemRegion*,
110 llvm::ImmutableSet<const MemRegion*> > Map;
111
112 llvm::ImmutableSet<const MemRegion*>::Factory F;
113 Map M;
114
115public:
116 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
117 Map::iterator I = M.find(Parent);
118 M.insert(std::make_pair(Parent,
119 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
120 }
121
122 ~RegionStoreSubRegionMap() {}
123
Ted Kremenek5dc27462009-03-03 02:51:43 +0000124 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000125 Map::iterator I = M.find(Parent);
126
127 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000128 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000129
130 llvm::ImmutableSet<const MemRegion*> S = I->second;
131 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
132 SI != SE; ++SI) {
133 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000134 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000135 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000136
137 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000138 }
139};
140
Zhongxing Xu17892752008-10-08 02:50:44 +0000141class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
142 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000143 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000144
Zhongxing Xu17892752008-10-08 02:50:44 +0000145 GRStateManager& StateMgr;
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000146 const MemRegion* SelfRegion;
147 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000148
149public:
150 RegionStoreManager(GRStateManager& mgr)
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000151 : StoreManager(mgr.getAllocator()),
152 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000153 RVFactory(mgr.getAllocator()),
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000154 StateMgr(mgr), SelfRegion(0), SelfDecl(0) {
155 if (const ObjCMethodDecl* MD =
156 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
157 SelfDecl = MD->getSelfDecl();
158 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000159
160 virtual ~RegionStoreManager() {}
161
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000162 MemRegionManager& getRegionManager() { return MRMgr; }
Ted Kremenek4f090272008-10-27 21:54:31 +0000163
Ted Kremenek14453bf2009-03-03 19:02:42 +0000164 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000165
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000166 const GRState* BindCompoundLiteral(const GRState* St,
167 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000168
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000169 /// getLValueString - Returns an SVal representing the lvalue of a
170 /// StringLiteral. Within RegionStore a StringLiteral has an
171 /// associated StringRegion, and the lvalue of a StringLiteral is
172 /// the lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000173 SVal getLValueString(const GRState* St, const StringLiteral* S);
174
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000175 /// getLValueCompoundLiteral - Returns an SVal representing the
176 /// lvalue of a compound literal. Within RegionStore a compound
177 /// literal has an associated region, and the lvalue of the
178 /// compound literal is the lvalue of that region.
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000179 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
180
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000181 /// getLValueVar - Returns an SVal that represents the lvalue of a
182 /// variable. Within RegionStore a variable has an associated
183 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000184 SVal getLValueVar(const GRState* St, const VarDecl* VD);
185
186 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
187
188 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000189
190 SVal getLValueFieldOrIvar(const GRState* St, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000191
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000192 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
193
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000194 SVal getSizeInElements(const GRState* St, const MemRegion* R);
195
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000196 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
197 /// type. 'Array' represents the lvalue of the array being decayed
198 /// to a pointer, and the returned SVal represents the decayed
199 /// version of that lvalue (i.e., a pointer to the first element of
200 /// the array). This is called by GRExprEngine when evaluating
201 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000202 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000203
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000204 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
205 /// a MemRegion* to a specific location type. 'R' is the region being
206 /// casted and 'CastToTy' the result type of the cast.
207 CastResult CastRegion(const GRState* state, const MemRegion* R,
208 QualType CastToTy);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000209
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000210 SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R);
211
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000212 /// The high level logic for this method is this:
213 /// Retrieve (L)
214 /// if L has binding
215 /// return L's binding
216 /// else if L is in killset
217 /// return unknown
218 /// else
219 /// if L is on stack or heap
220 /// return undefined
221 /// else
222 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000223 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000224
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000225 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000226
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000227 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000228
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000229 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000230
231 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
232 /// 'this' object (C++). When used when analyzing a normal function this
233 /// method returns NULL.
234 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000235 if (!SelfDecl)
236 return 0;
237
238 if (!SelfRegion) {
239 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
240 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
241 MRMgr.getHeapRegion());
242 }
243
244 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000245 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000246
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000247 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
248 /// It returns a new Store with these values removed, and populates LSymbols
249 // and DSymbols with the known set of live and dead symbols respectively.
250 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000251 SymbolReaper& SymReaper,
252 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000253
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000254 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
255
256 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
257 return St;
258 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000259
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000260 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
261
Zhongxing Xu17892752008-10-08 02:50:44 +0000262 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000263 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000264 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000265
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000266 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000267
268 void iterBindings(Store store, BindingsHandler& f) {
269 // FIXME: Implement.
270 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000271
272private:
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000273 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000274
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000275 /// Retrieve the values in a struct and return a CompoundVal, used when doing
276 /// struct copy:
277 /// struct s x, y;
278 /// x = y;
279 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000280 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000281
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000282 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000283
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000284 /// KillStruct - Set the entire struct to unknown.
285 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
286
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000287 // Utility methods.
288 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
289 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000290 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000291
292 const GRState* AddRegionView(const GRState* St,
293 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000294 const GRState* RemoveRegionView(const GRState* St,
295 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000296};
297
298} // end anonymous namespace
299
Ted Kremenek95c7b002008-10-24 01:04:59 +0000300StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000301 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000302}
303
Ted Kremenek14453bf2009-03-03 19:02:42 +0000304SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000305 RegionBindingsTy B = GetRegionBindings(state->getStore());
306 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
307
308 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
309 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
310 M->add(R->getSuperRegion(), R);
311 }
312
Ted Kremenek14453bf2009-03-03 19:02:42 +0000313 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000314}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000315
316/// getLValueString - Returns an SVal representing the lvalue of a
317/// StringLiteral. Within RegionStore a StringLiteral has an
318/// associated StringRegion, and the lvalue of a StringLiteral is the
319/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000320SVal RegionStoreManager::getLValueString(const GRState* St,
321 const StringLiteral* S) {
322 return loc::MemRegionVal(MRMgr.getStringRegion(S));
323}
324
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000325/// getLValueVar - Returns an SVal that represents the lvalue of a
326/// variable. Within RegionStore a variable has an associated
327/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000328SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
329 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
330}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000331
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000332/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
333/// of a compound literal. Within RegionStore a compound literal
334/// has an associated region, and the lvalue of the compound literal
335/// is the lvalue of that region.
336SVal
337RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
338 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000339 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
340}
341
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000342SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
343 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000344 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000345}
346
347SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
348 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000349 return getLValueFieldOrIvar(St, Base, D);
350}
351
352SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
353 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000354 if (Base.isUnknownOrUndef())
355 return Base;
356
357 Loc BaseL = cast<Loc>(Base);
358 const MemRegion* BaseR = 0;
359
360 switch (BaseL.getSubKind()) {
361 case loc::MemRegionKind:
362 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000363 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(BaseR)) {
364 SymbolRef Sym = SR->getSymbol();
365 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
366 }
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000367 break;
368
Ted Kremeneke8e86482009-03-30 22:20:54 +0000369 case loc::SymbolValKind: {
370 SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol();
371 const SymbolicRegion* SR = MRMgr.getSymbolicRegion(Sym);
372 // Layer the type information.
373 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000374 break;
Ted Kremeneke8e86482009-03-30 22:20:54 +0000375 }
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000376
377 case loc::GotoLabelKind:
378 case loc::FuncValKind:
379 // These are anormal cases. Flag an undefined value.
380 return UndefinedVal();
381
382 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000383 // While these seem funny, this can happen through casts.
384 // FIXME: What we should return is the field offset. For example,
385 // add the field offset to the integer value. That way funny things
386 // like this work properly: &(((struct foo *) 0xa)->f)
387 return Base;
388
389 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000390 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000391 return Base;
392 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000393
394 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
395 // of FieldDecl.
396 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
397 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000398
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000399 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000400}
401
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000402SVal RegionStoreManager::getLValueElement(const GRState* St,
403 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000404
Ted Kremenekde7ec632009-03-09 22:44:49 +0000405 // If the base is an unknown or undefined value, just return it back.
406 // FIXME: For absolute pointer addresses, we just return that value back as
407 // well, although in reality we should return the offset added to that
408 // value.
409 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000410 return Base;
411
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000412 // Only handle integer offsets... for now.
413 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000414 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000415
Zhongxing Xua48f7372009-02-06 08:44:27 +0000416 const TypedRegion* BaseRegion = 0;
417
Ted Kremeneke8e86482009-03-30 22:20:54 +0000418 if (isa<loc::SymbolVal>(Base)) {
Zhongxing Xua1718c72009-04-03 07:33:13 +0000419 // FIXME: This case will be removed.
Ted Kremeneke8e86482009-03-30 22:20:54 +0000420 SymbolRef Sym = cast<loc::SymbolVal>(Base).getSymbol();
421 SymbolicRegion* SR = MRMgr.getSymbolicRegion(Sym);
422 // Layer the type information.
423 BaseRegion = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000424 } else {
425 const MemRegion* R = cast<loc::MemRegionVal>(Base).getRegion();
426 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
427 SymbolRef Sym = SR->getSymbol();
428 BaseRegion = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
429 }
430 else
431 BaseRegion = cast<TypedRegion>(R);
432 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000433
434 // Pointer of any type can be cast and used as array base.
435 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
436
437 if (!ElemR) {
438 //
439 // If the base region is not an ElementRegion, create one.
440 // This can happen in the following example:
441 //
442 // char *p = __builtin_alloc(10);
443 // p[1] = 8;
444 //
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000445 // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000446 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000447
448 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
449 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
450 const llvm::APSInt& OffI = CI->getValue();
451 if (OffI.isUnsigned()) {
452 llvm::APSInt Tmp = OffI;
453 Tmp.setIsSigned(true);
454 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
455 }
456 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000457 return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000458 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000459
460 SVal BaseIdx = ElemR->getIndex();
461
462 if (!isa<nonloc::ConcreteInt>(BaseIdx))
463 return UnknownVal();
464
465 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
466 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
467 assert(BaseIdxI.isSigned());
468
469 // FIXME: This appears to be the assumption of this code. We should review
470 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
471 // can't we need to put a comment here. If it can, we should handle it.
472 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000473
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000474 const TypedRegion *ArrayR = ElemR->getArrayRegion();
475 SVal NewIdx;
476
477 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
478 // 'Offset' might be unsigned. We have to convert it to signed and
479 // possibly extend it.
480 llvm::APSInt Tmp = OffI;
481
482 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
483 Tmp.extend(BaseIdxI.getBitWidth());
484
485 Tmp.setIsSigned(true);
486 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000487 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000488 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000489 else
490 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000491
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000492 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000493}
494
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000495SVal RegionStoreManager::getSizeInElements(const GRState* St,
496 const MemRegion* R) {
497 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
498 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000499 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000500
Ted Kremenek14553ab2009-01-30 00:08:43 +0000501 // FIXME: Handle variable-length arrays.
502 if (isa<VariableArrayType>(T))
503 return UnknownVal();
504
505 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
506 // return the size as signed integer.
507 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
508 }
509
510 // Clients can use ordinary variables as if they were arrays. These
511 // essentially are arrays of size 1.
512 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000513 }
514
515 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000516 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000517 // We intentionally made the size value signed because it participates in
518 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000519 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000520 }
521
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000522 if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000523#if 0
524 // FIXME: This logic doesn't really work, as we can have all sorts of
525 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
526 // The weird cases come in when arbitrary casting comes into play, violating
527 // any type-safe programming.
528
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000529 GRStateRef state(St, StateMgr);
530
531 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000532 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
533 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000534
535 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000536 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000537
538 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000539 QualType LvT = ATR->getLValueType(getContext());
540 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000541
542 uint64_t X = getContext().getTypeSize(ElemTy);
543
544 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
545 false);
546
547 // Calculate the number of elements.
548
549 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
550 // signed?
551 if (SSize.isUnsigned())
552 SSize.setIsSigned(true);
553
554 // FIXME: move this operation into BasicVals.
555 const llvm::APSInt S =
556 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
557
558 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000559#else
560 ATR = ATR;
561 return UnknownVal();
562#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000563 }
564
565 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
566 // FIXME: Unsupported yet.
567 FR = 0;
568 return UnknownVal();
569 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000570
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000571 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000572 return UnknownVal();
573 }
574
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000575 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000576 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000577}
578
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000579/// ArrayToPointer - Emulates the "decay" of an array to a pointer
580/// type. 'Array' represents the lvalue of the array being decayed
581/// to a pointer, and the returned SVal represents the decayed
582/// version of that lvalue (i.e., a pointer to the first element of
583/// the array). This is called by GRExprEngine when evaluating casts
584/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000585SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000586 if (!isa<loc::MemRegionVal>(Array))
587 return UnknownVal();
588
589 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
590 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
591
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000592 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000593 return UnknownVal();
594
Zhongxing Xu63123d82008-11-23 04:30:35 +0000595 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000596 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
597
598 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000599}
600
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000601StoreManager::CastResult
602RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
603 QualType CastToTy) {
604
605 // Return the same region if the region types are compatible.
606 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
607 ASTContext& Ctx = StateMgr.getContext();
608 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
609 QualType Tb = Ctx.getCanonicalType(CastToTy);
610
611 if (Ta == Tb)
612 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000613 }
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000614
615 // FIXME: We should handle the case when we are casting *back* to a
616 // previous type. For example:
617 //
618 // void* x = ...;
619 // char* y = (char*) x;
620 // void* z = (void*) y; // <-- we should get the same region that is
621 // bound to 'x'
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000622 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000623 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000624}
625
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000626SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
627 // Assume the base location is MemRegionVal(ElementRegion).
Ted Kremenek5dc27462009-03-03 02:51:43 +0000628 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000629 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000630
Zhongxing Xua1718c72009-04-03 07:33:13 +0000631 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
632 if (isa<SymbolicRegion>(MR))
633 return UnknownVal();
634
635 const TypedRegion* TR = cast<TypedRegion>(MR);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000636
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000637 const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
638
639 if (!ER) {
640 // If the region is not element region, create one with index 0. This can
641 // happen in the following example:
642 // char *p = foo();
643 // p += 3;
644 // Note that p binds to a TypedViewRegion(SymbolicRegion).
645 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
646 ER = MRMgr.getElementRegion(Idx, TR);
647 }
648
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000649 SVal Idx = ER->getIndex();
650
651 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
652 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
653
654 // Only support concrete integer indexes for now.
655 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000656 // FIXME: For now, convert the signedness and bitwidth of offset in case
657 // they don't match. This can result from pointer arithmetic. In reality,
658 // we should figure out what are the proper semantics and implement them.
659 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000660 // This addresses the test case test/Analysis/ptr-arith.c
661 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000662 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
663 Offset->getValue()));
664 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000665 const MemRegion* NewER = MRMgr.getElementRegion(NewIdx,
666 ER->getArrayRegion());
667 return Loc::MakeVal(NewER);
668
Ted Kremenek5dc27462009-03-03 02:51:43 +0000669 }
670
671 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000672}
673
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000674SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000675 assert(!isa<UnknownVal>(L) && "location unknown");
676 assert(!isa<UndefinedVal>(L) && "location undefined");
677
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000678 // FIXME: What does loc::SymbolVal represent? It represents the value
679 // of a location but that value is not known. In the future we should
680 // handle potential aliasing relationships; e.g. a loc::SymbolVal could
681 // be an alias for a particular region.
Zhongxing Xu779747c2009-02-20 05:19:30 +0000682 // Example:
683 // void foo(char* buf) {
684 // char c = *buf;
685 // }
686 if (isa<loc::SymbolVal>(L)) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000687 return UnknownVal();
Zhongxing Xu779747c2009-02-20 05:19:30 +0000688 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000689
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000690 // FIXME: Is this even possible? Shouldn't this be treated as a null
691 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000692 if (isa<loc::ConcreteInt>(L))
693 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000694
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000695 // FIXME: Should this be refactored into GRExprEngine or GRStateManager?
696 // It seems that all StoreManagers would do the same thing here.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000697 if (isa<loc::FuncVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000698 return L;
699
Zhongxing Xua1718c72009-04-03 07:33:13 +0000700 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
701
702 // We return unknown for symbolic region for now. This might be improved.
703 // Example:
704 // void f(int* p) { int x = *p; }
705 if (isa<SymbolicRegion>(MR))
706 return UnknownVal();
707
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000708 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
709 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000710 const TypedRegion* R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000711 assert(R && "bad region");
712
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000713 // FIXME: We should eventually handle funny addressing. e.g.:
714 //
715 // int x = ...;
716 // int *p = &x;
717 // char *q = (char*) p;
718 // char c = *q; // returns the first byte of 'x'.
719 //
720 // Such funny addressing will occur due to layering of regions.
721
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000722 QualType RTy = R->getRValueType(getContext());
723 if (RTy->isStructureType())
724 return RetrieveStruct(St, R);
725 // FIXME: handle Vector types.
726 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000727 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000728
729 RegionBindingsTy B = GetRegionBindings(St->getStore());
730 RegionBindingsTy::data_type* V = B.lookup(R);
731
732 // Check if the region has a binding.
733 if (V)
734 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000735
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000736 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000737
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000738 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000739 if (state.contains<RegionKills>(R))
740 return UnknownVal();
741
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000742 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000743 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
744 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
745 GRStateTrait<RegionDefaultValue>::lookup_type D =
746 state.get<RegionDefaultValue>(SuperR);
747 if (D)
748 return *D;
749 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000750
751 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
752 const MemRegion *SR = IVR->getSuperRegion();
753
754 // If the super region is 'self' then return the symbol representing
755 // the value of the ivar upon entry to the method.
756 if (SR == SelfRegion) {
757 // FIXME: Do we need to handle the case where the super region
758 // has a view? We want to canonicalize the bindings.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000759 return SVal::GetRValueSymbolVal(getSymbolManager(), MRMgr, R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000760 }
761
762 // Otherwise, we need a new symbol. For now return Unknown.
763 return UnknownVal();
764 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000765
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000766 // The location does not have a bound value. This means that it has
767 // the value it had upon its creation and/or entry to the analyzed
768 // function/method. These are either symbolic values or 'undefined'.
769
770 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000771 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
772 const VarDecl *VD = VR->getDecl();
773
Ted Kremenekcec35252009-03-04 00:23:05 +0000774 if (VD == SelfDecl)
775 return loc::MemRegionVal(getSelfRegion(0));
776
777 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
778 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000779 QualType VTy = VD->getType();
780 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Zhongxing Xua1718c72009-04-03 07:33:13 +0000781 return SVal::GetRValueSymbolVal(getSymbolManager(), MRMgr, VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000782 else
783 return UnknownVal();
784 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000785 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000786
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000787 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
788 // All stack variables are considered to have undefined values
789 // upon creation. All heap allocated blocks are considered to
790 // have undefined values as well unless they are explicitly bound
791 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000792 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000793 }
794
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000795 // All other integer values are symbolic.
796 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Zhongxing Xua1718c72009-04-03 07:33:13 +0000797 return SVal::GetRValueSymbolVal(getSymbolManager(), MRMgr, R);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000798 else
799 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000800}
801
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000802SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
803
804 Store store = St->getStore();
805 GRStateRef state(St, StateMgr);
806
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000807 // FIXME: Verify we want getRValueType instead of getLValueType.
808 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000809 assert(T->isStructureType());
810
811 const RecordType* RT = cast<RecordType>(T.getTypePtr());
812 RecordDecl* RD = RT->getDecl();
813 assert(RD->isDefinition());
814
815 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
816
Douglas Gregore267ff32008-12-11 20:41:00 +0000817 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000818
Douglas Gregore267ff32008-12-11 20:41:00 +0000819 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
820 FieldEnd = Fields.rend();
821 Field != FieldEnd; ++Field) {
822 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000823 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000824 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000825
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000826 SVal FieldValue;
827 if (data)
828 FieldValue = *data;
829 else if (state.contains<RegionKills>(FR))
830 FieldValue = UnknownVal();
831 else {
832 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
833 FieldValue = UndefinedVal();
834 else
Zhongxing Xua1718c72009-04-03 07:33:13 +0000835 FieldValue = SVal::GetRValueSymbolVal(getSymbolManager(), MRMgr, FR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000836 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000837
838 StructVal = getBasicVals().consVals(FieldValue, StructVal);
839 }
840
841 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
842}
843
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000844const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
845 // Currently we don't bind value to symbolic location. But if the logic is
846 // made clear, we might change this decision.
847 if (isa<loc::SymbolVal>(L))
848 return St;
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000849
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000850 // If we get here, the location should be a region.
851 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000852 assert(R);
853
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000854 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000855 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000856 // FIXME: Verify we want getRValueType().
857 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000858 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000859
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000860 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000861 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000862
863 if (V.isUnknown()) {
864 // Remove the binding.
865 store = RBFactory.Remove(B, R).getRoot();
866
867 // Add the region to the killset.
868 GRStateRef state(St, StateMgr);
869 St = state.add<RegionKills>(R);
870 }
871 else
872 store = RBFactory.Add(B, R, V).getRoot();
873
874 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000875}
876
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000877Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000878 const MemRegion* R = 0;
879
880 if (isa<loc::MemRegionVal>(L))
881 R = cast<loc::MemRegionVal>(L).getRegion();
882 else if (isa<loc::SymbolVal>(L))
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000883 R = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(L).getSymbol());
Ted Kremenek0964a062009-01-21 06:57:53 +0000884
885 if (R) {
886 RegionBindingsTy B = GetRegionBindings(store);
887 return RBFactory.Remove(B, R).getRoot();
888 }
889
890 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000891}
892
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000893const GRState* RegionStoreManager::BindDecl(const GRState* St,
894 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000895
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000896 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000897 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000898
Ted Kremenek0964a062009-01-21 06:57:53 +0000899 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000900 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000901 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000902 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000903
Ted Kremenek0964a062009-01-21 06:57:53 +0000904 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000905}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000906
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000907// FIXME: this method should be merged into Bind().
908const GRState*
909RegionStoreManager::BindCompoundLiteral(const GRState* St,
910 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000911 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000912 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000913}
914
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000915const GRState* RegionStoreManager::setExtent(const GRState* St,
916 const MemRegion* R, SVal Extent) {
917 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000918 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000919}
920
921
Ted Kremenek241677a2009-01-21 22:26:05 +0000922static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +0000923 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
924 const MemRegion *R = XR->getRegion();
925
926 while (R) {
927 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
928 SymReaper.markLive(SR->getSymbol());
929 return;
930 }
931
932 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
933 R = SR->getSuperRegion();
934 continue;
935 }
936
937 break;
938 }
939
940 return;
941 }
942
Ted Kremenek241677a2009-01-21 22:26:05 +0000943 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
944 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000945}
946
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000947Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000948 SymbolReaper& SymReaper,
949 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
950{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000951
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000952 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000953 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000954
955 // Lazily constructed backmap from MemRegions to SubRegions.
956 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
957 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
958
959 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
960 // the ability to reuse memory. This way we can keep TmpAlloc around as
961 // an instance variable of RegionStoreManager (avoiding repeated malloc
962 // overhead).
963 llvm::BumpPtrAllocator TmpAlloc;
964
965 // Factory objects.
966 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
967 SubRegionsTy::Factory SubRegF(TmpAlloc);
968
969 // The backmap from regions to subregions.
970 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
971
972 // Do a pass over the regions in the store. For VarRegions we check if
973 // the variable is still live and if so add it to the list of live roots.
974 // For other regions we populate our region backmap.
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000975
976 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
977
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000978 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000979 IntermediateRoots.push_back(I.getKey());
980 }
981
982 while (!IntermediateRoots.empty()) {
983 const MemRegion* R = IntermediateRoots.back();
984 IntermediateRoots.pop_back();
985
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000986 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000987 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000988 RegionRoots.push_back(VR); // This is a live "root".
989 }
990 else {
991 // Get the super region for R.
992 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000993
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000994 // Get the current set of subregions for SuperR.
995 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
996 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000997
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000998 // Add R to the subregions of SuperR.
999 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001000
1001 // Super region may be VarRegion or subregion of another VarRegion. Add it
1002 // to the work list.
1003 if (isa<SubRegion>(SuperR))
1004 IntermediateRoots.push_back(SuperR);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001005 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001006 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001007
1008 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1009 // of the store. We want to find all live symbols and dead regions.
1010 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1011
1012 while (!RegionRoots.empty()) {
1013 // Dequeue the next region on the worklist.
1014 const MemRegion* R = RegionRoots.back();
1015 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001016
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001017 // Check if we have already processed this region.
1018 if (Marked.count(R)) continue;
1019
1020 // Mark this region as processed. This is needed for termination in case
1021 // a region is referenced more than once.
1022 Marked.insert(R);
1023
1024 // Mark the symbol for any live SymbolicRegion as "live". This means we
1025 // should continue to track that symbol.
1026 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +00001027 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001028
1029 // Get the data binding for R (if any).
1030 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1031 if (Xptr) {
1032 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +00001033 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001034
1035 // If X is a region, then add it the RegionRoots.
1036 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1037 RegionRoots.push_back(RegionX->getRegion());
1038 }
1039
1040 // Get the subregions of R. These are RegionRoots as well since they
1041 // represent values that are also bound to R.
1042 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1043 if (!SRptr) continue;
1044 SubRegionsTy SR = *SRptr;
1045
1046 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1047 RegionRoots.push_back(*I);
1048 }
1049
1050 // We have now scanned the store, marking reachable regions and symbols
1051 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001052 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001053 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1054 const MemRegion* R = I.getKey();
1055
1056 // If this region live? Is so, none of its symbols are dead.
1057 if (Marked.count(R))
1058 continue;
1059
1060 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001061 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001062
1063 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +00001064 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1065 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001066
1067 SVal X = I.getData();
1068 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +00001069 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001070 }
1071
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001072 return store;
1073}
1074
Zhongxing Xua071eb02008-10-24 06:01:33 +00001075void RegionStoreManager::print(Store store, std::ostream& Out,
1076 const char* nl, const char *sep) {
1077 llvm::raw_os_ostream OS(Out);
1078 RegionBindingsTy B = GetRegionBindings(store);
1079 OS << "Store:" << nl;
1080
1081 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1082 OS << ' '; I.getKey()->print(OS); OS << " : ";
1083 I.getData().print(OS); OS << nl;
1084 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001085}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001086
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001087const GRState* RegionStoreManager::BindArray(const GRState* St,
1088 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001089
1090 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +00001091 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001092 assert(T->isArrayType());
1093
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001094 // When we are binding the whole array, it always has default value 0.
1095 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001096 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1097 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001098
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001099 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1100
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001101 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001102 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1103 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001104
1105 // Check if the init expr is a StringLiteral.
1106 if (isa<loc::MemRegionVal>(Init)) {
1107 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1108 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1109 const char* str = S->getStrData();
1110 unsigned len = S->getByteLength();
1111 unsigned j = 0;
1112
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001113 // Copy bytes from the string literal into the target array. Trailing bytes
1114 // in the array that are not covered by the string literal are initialized
1115 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001116 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001117 if (j >= len)
1118 break;
1119
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001120 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
1121 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
1122
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001123 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1124 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001125 }
1126
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001127 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001128 }
1129
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001130
1131 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
1132
1133 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1134
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001135 for (; i < Size; ++i, ++VI) {
1136 // The init list might be shorter than the array decl.
1137 if (VI == VE)
1138 break;
1139
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001140 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001141 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001142
1143 if (CAT->getElementType()->isStructureType())
1144 St = BindStruct(St, ER, *VI);
1145 else
1146 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001147 }
1148
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001149 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001150}
1151
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001152const GRState*
1153RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001154 // FIXME: Verify that we should use getRValueType or getLValueType.
1155 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001156 assert(T->isStructureType());
1157
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001158 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001159 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001160
1161 if (!RD->isDefinition())
1162 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001163
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001164 if (V.isUnknown())
1165 return KillStruct(St, R);
1166
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001167 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001168 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1169 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
1170
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001171 for (; FI != FE; ++FI, ++VI) {
1172
1173 // There may be fewer values than fields only when we are initializing a
1174 // struct decl. In this case, mark the region as having default value.
1175 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001176 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001177 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1178 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001179 break;
1180 }
1181
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001182 QualType FTy = (*FI)->getType();
1183 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1184
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001185 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1186 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001187
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001188 else if (FTy->isArrayType())
1189 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001190
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001191 else if (FTy->isStructureType())
1192 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001193 }
1194
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001195 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001196}
1197
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001198const GRState* RegionStoreManager::KillStruct(const GRState* St,
1199 const TypedRegion* R){
1200 GRStateRef state(St, StateMgr);
1201
1202 // Kill the struct region because it is assigned "unknown".
1203 St = state.add<RegionKills>(R);
1204
1205 // Set the default value of the struct region to "unknown".
1206 St = state.set<RegionDefaultValue>(R, UnknownVal());
1207
1208 Store store = St->getStore();
1209 RegionBindingsTy B = GetRegionBindings(store);
1210
1211 // Remove all bindings for the subregions of the struct.
1212 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1213 const MemRegion* r = I.getKey();
1214 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1215 if (sr->isSubRegionOf(R))
1216 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001217 // FIXME: Maybe we should also remove the bindings for the "views" of the
1218 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001219 }
1220
1221 return StateMgr.MakeStateWithStore(St, store);
1222}
1223
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001224const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1225 const MemRegion* View,
1226 const MemRegion* Base) {
1227 GRStateRef state(St, StateMgr);
1228
1229 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001230 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001231 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001232
1233 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001234 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001235
1236 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001237 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001238}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001239
1240const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1241 const MemRegion* View,
1242 const MemRegion* Base) {
1243 GRStateRef state(St, StateMgr);
1244
1245 // Retrieve the region view of the base region.
1246 const RegionViews* d = state.get<RegionViewMap>(Base);
1247
1248 // If the base region has no view, return.
1249 if (!d)
1250 return St;
1251
1252 // Remove the view.
1253 RegionViews V = *d;
1254 V = RVFactory.Remove(V, View);
1255
1256 return state.set<RegionViewMap>(Base, V);
1257}