blob: 5f8e6c3f92a715aee88fa31d729b95ff8ea30e53 [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
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000145 const MemRegion* SelfRegion;
146 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000147
148public:
149 RegionStoreManager(GRStateManager& mgr)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000150 : StoreManager(mgr),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000151 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000152 RVFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000153 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000154 if (const ObjCMethodDecl* MD =
155 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
156 SelfDecl = MD->getSelfDecl();
157 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000158
159 virtual ~RegionStoreManager() {}
160
Ted Kremenek14453bf2009-03-03 19:02:42 +0000161 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000162
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000163 const GRState* BindCompoundLiteral(const GRState* St,
164 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000165
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000166 /// getLValueString - Returns an SVal representing the lvalue of a
167 /// StringLiteral. Within RegionStore a StringLiteral has an
168 /// associated StringRegion, and the lvalue of a StringLiteral is
169 /// the lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000170 SVal getLValueString(const GRState* St, const StringLiteral* S);
171
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000172 /// getLValueCompoundLiteral - Returns an SVal representing the
173 /// lvalue of a compound literal. Within RegionStore a compound
174 /// literal has an associated region, and the lvalue of the
175 /// compound literal is the lvalue of that region.
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000176 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
177
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000178 /// getLValueVar - Returns an SVal that represents the lvalue of a
179 /// variable. Within RegionStore a variable has an associated
180 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000181 SVal getLValueVar(const GRState* St, const VarDecl* VD);
182
183 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
184
185 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000186
187 SVal getLValueFieldOrIvar(const GRState* St, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000188
Ted Kremenekf936f452009-05-04 06:18:28 +0000189 SVal getLValueElement(const GRState* St, QualType elementType,
190 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000191
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000192 SVal getSizeInElements(const GRState* St, const MemRegion* R);
193
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000194 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
195 /// type. 'Array' represents the lvalue of the array being decayed
196 /// to a pointer, and the returned SVal represents the decayed
197 /// version of that lvalue (i.e., a pointer to the first element of
198 /// the array). This is called by GRExprEngine when evaluating
199 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000200 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000201
Zhongxing Xu88980592009-05-06 02:42:32 +0000202 CastResult CastRegion(const GRState* state, const MemRegion* R,
203 QualType CastToTy);
204
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000205 SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R);
206
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000207 /// The high level logic for this method is this:
208 /// Retrieve (L)
209 /// if L has binding
210 /// return L's binding
211 /// else if L is in killset
212 /// return unknown
213 /// else
214 /// if L is on stack or heap
215 /// return undefined
216 /// else
217 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000218 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000219
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000220 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000221
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000222 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000223
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000224 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000225
226 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
227 /// 'this' object (C++). When used when analyzing a normal function this
228 /// method returns NULL.
229 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000230 if (!SelfDecl)
231 return 0;
232
233 if (!SelfRegion) {
234 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
235 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
236 MRMgr.getHeapRegion());
237 }
238
239 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000240 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000241
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000242 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
243 /// It returns a new Store with these values removed, and populates LSymbols
244 // and DSymbols with the known set of live and dead symbols respectively.
245 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000246 SymbolReaper& SymReaper,
247 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000248
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000249 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
250
251 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
252 return St;
253 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000254
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000255 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
256
Zhongxing Xu17892752008-10-08 02:50:44 +0000257 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000258 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000259 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000260
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000261 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000262
263 void iterBindings(Store store, BindingsHandler& f) {
264 // FIXME: Implement.
265 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000266
267private:
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000268 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000269
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000270 /// Retrieve the values in a struct and return a CompoundVal, used when doing
271 /// struct copy:
272 /// struct s x, y;
273 /// x = y;
274 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000275 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000276
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000277 SVal RetrieveArray(const GRState* St, const TypedRegion* R);
278
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000279 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000280
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000281 /// KillStruct - Set the entire struct to unknown.
282 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
283
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000284 // Utility methods.
285 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
286 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000287 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000288
289 const GRState* AddRegionView(const GRState* St,
290 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000291 const GRState* RemoveRegionView(const GRState* St,
292 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000293};
294
295} // end anonymous namespace
296
Ted Kremenek95c7b002008-10-24 01:04:59 +0000297StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000298 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000299}
300
Ted Kremenek14453bf2009-03-03 19:02:42 +0000301SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000302 RegionBindingsTy B = GetRegionBindings(state->getStore());
303 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
304
305 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
306 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
307 M->add(R->getSuperRegion(), R);
308 }
309
Ted Kremenek14453bf2009-03-03 19:02:42 +0000310 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000311}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000312
313/// getLValueString - Returns an SVal representing the lvalue of a
314/// StringLiteral. Within RegionStore a StringLiteral has an
315/// associated StringRegion, and the lvalue of a StringLiteral is the
316/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000317SVal RegionStoreManager::getLValueString(const GRState* St,
318 const StringLiteral* S) {
319 return loc::MemRegionVal(MRMgr.getStringRegion(S));
320}
321
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000322/// getLValueVar - Returns an SVal that represents the lvalue of a
323/// variable. Within RegionStore a variable has an associated
324/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000325SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
326 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
327}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000328
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000329/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
330/// of a compound literal. Within RegionStore a compound literal
331/// has an associated region, and the lvalue of the compound literal
332/// is the lvalue of that region.
333SVal
334RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
335 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000336 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
337}
338
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000339SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
340 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000341 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000342}
343
344SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
345 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000346 return getLValueFieldOrIvar(St, Base, D);
347}
348
349SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
350 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000351 if (Base.isUnknownOrUndef())
352 return Base;
353
354 Loc BaseL = cast<Loc>(Base);
355 const MemRegion* BaseR = 0;
356
357 switch (BaseL.getSubKind()) {
358 case loc::MemRegionKind:
359 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000360 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(BaseR)) {
361 SymbolRef Sym = SR->getSymbol();
362 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
363 }
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000364 break;
365
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000366 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000367 // These are anormal cases. Flag an undefined value.
368 return UndefinedVal();
369
370 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000371 // While these seem funny, this can happen through casts.
372 // FIXME: What we should return is the field offset. For example,
373 // add the field offset to the integer value. That way funny things
374 // like this work properly: &(((struct foo *) 0xa)->f)
375 return Base;
376
377 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000378 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000379 return Base;
380 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000381
382 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
383 // of FieldDecl.
384 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
385 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000386
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000387 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000388}
389
Ted Kremenekf936f452009-05-04 06:18:28 +0000390SVal RegionStoreManager::getLValueElement(const GRState* St,
391 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000392 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000393
Ted Kremenekde7ec632009-03-09 22:44:49 +0000394 // If the base is an unknown or undefined value, just return it back.
395 // FIXME: For absolute pointer addresses, we just return that value back as
396 // well, although in reality we should return the offset added to that
397 // value.
398 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000399 return Base;
400
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000401 // Only handle integer offsets... for now.
402 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000403 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000404
Zhongxing Xua48f7372009-02-06 08:44:27 +0000405 const TypedRegion* BaseRegion = 0;
406
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000407 const MemRegion* R = cast<loc::MemRegionVal>(Base).getRegion();
408 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
409 SymbolRef Sym = SR->getSymbol();
Ted Kremeneke8e86482009-03-30 22:20:54 +0000410 BaseRegion = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000411 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000412 else
413 BaseRegion = cast<TypedRegion>(R);
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000414
415 // Pointer of any type can be cast and used as array base.
416 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
417
418 if (!ElemR) {
419 //
420 // If the base region is not an ElementRegion, create one.
421 // This can happen in the following example:
422 //
423 // char *p = __builtin_alloc(10);
424 // p[1] = 8;
425 //
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000426 // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000427 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000428
429 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
430 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
431 const llvm::APSInt& OffI = CI->getValue();
432 if (OffI.isUnsigned()) {
433 llvm::APSInt Tmp = OffI;
434 Tmp.setIsSigned(true);
435 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
436 }
437 }
Ted Kremenekf936f452009-05-04 06:18:28 +0000438 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
439 BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000440 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000441
442 SVal BaseIdx = ElemR->getIndex();
443
444 if (!isa<nonloc::ConcreteInt>(BaseIdx))
445 return UnknownVal();
446
447 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
448 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
449 assert(BaseIdxI.isSigned());
450
451 // FIXME: This appears to be the assumption of this code. We should review
452 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
453 // can't we need to put a comment here. If it can, we should handle it.
454 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000455
Ted Kremenekf936f452009-05-04 06:18:28 +0000456 const TypedRegion *ArrayR = cast<TypedRegion>(ElemR->getSuperRegion());
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000457 SVal NewIdx;
458
459 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
460 // 'Offset' might be unsigned. We have to convert it to signed and
461 // possibly extend it.
462 llvm::APSInt Tmp = OffI;
463
464 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
465 Tmp.extend(BaseIdxI.getBitWidth());
466
467 Tmp.setIsSigned(true);
468 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000469 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000470 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000471 else
472 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000473
Ted Kremenekf936f452009-05-04 06:18:28 +0000474 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000475}
476
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000477SVal RegionStoreManager::getSizeInElements(const GRState* St,
478 const MemRegion* R) {
479 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
480 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000481 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000482
Ted Kremenek14553ab2009-01-30 00:08:43 +0000483 // FIXME: Handle variable-length arrays.
484 if (isa<VariableArrayType>(T))
485 return UnknownVal();
486
487 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
488 // return the size as signed integer.
489 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
490 }
491
492 // Clients can use ordinary variables as if they were arrays. These
493 // essentially are arrays of size 1.
494 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000495 }
496
497 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000498 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000499 // We intentionally made the size value signed because it participates in
500 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000501 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000502 }
503
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000504 if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000505#if 0
506 // FIXME: This logic doesn't really work, as we can have all sorts of
507 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
508 // The weird cases come in when arbitrary casting comes into play, violating
509 // any type-safe programming.
510
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000511 GRStateRef state(St, StateMgr);
512
513 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000514 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
515 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000516
517 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000518 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000519
520 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000521 QualType LvT = ATR->getLValueType(getContext());
522 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000523
524 uint64_t X = getContext().getTypeSize(ElemTy);
525
526 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
527 false);
528
529 // Calculate the number of elements.
530
531 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
532 // signed?
533 if (SSize.isUnsigned())
534 SSize.setIsSigned(true);
535
536 // FIXME: move this operation into BasicVals.
537 const llvm::APSInt S =
538 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
539
540 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000541#else
542 ATR = ATR;
543 return UnknownVal();
544#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000545 }
546
547 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
548 // FIXME: Unsupported yet.
549 FR = 0;
550 return UnknownVal();
551 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000552
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000553 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000554 return UnknownVal();
555 }
556
Zhongxing Xuccb16162009-05-06 08:08:27 +0000557 if (isa<ElementRegion>(R)) {
558 return UnknownVal();
559 }
560
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000561 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000562 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000563}
564
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000565/// ArrayToPointer - Emulates the "decay" of an array to a pointer
566/// type. 'Array' represents the lvalue of the array being decayed
567/// to a pointer, and the returned SVal represents the decayed
568/// version of that lvalue (i.e., a pointer to the first element of
569/// the array). This is called by GRExprEngine when evaluating casts
570/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000571SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000572 if (!isa<loc::MemRegionVal>(Array))
573 return UnknownVal();
574
575 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
576 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
577
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000578 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000579 return UnknownVal();
580
Ted Kremenekf936f452009-05-04 06:18:28 +0000581 // Strip off typedefs from the ArrayRegion's RvalueType.
582 QualType T = ArrayR->getRValueType(getContext())->getDesugaredType();
583 ArrayType *AT = cast<ArrayType>(T);
584 T = AT->getElementType();
585
Zhongxing Xu63123d82008-11-23 04:30:35 +0000586 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Ted Kremenekf936f452009-05-04 06:18:28 +0000587 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR);
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000588
589 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000590}
591
Zhongxing Xuccb16162009-05-06 08:08:27 +0000592static bool isSmallerThan(QualType T1, QualType T2) {
593 if (T1->isCharType())
594 return true;
595 else
596 return false;
597}
598
Zhongxing Xu88980592009-05-06 02:42:32 +0000599RegionStoreManager::CastResult
600RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
601 QualType CastToTy) {
602
603 ASTContext& Ctx = StateMgr.getContext();
604
605 // We need to know the real type of CastToTy.
606 QualType ToTy = Ctx.getCanonicalType(CastToTy);
607
608 // Check cast to ObjCQualifiedID type.
609 if (isa<ObjCQualifiedIdType>(ToTy)) {
610 // FIXME: Record the type information aside.
611 return CastResult(state, R);
612 }
613
614 // CodeTextRegion should be cast to only function pointer type.
615 if (isa<CodeTextRegion>(R)) {
616 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType());
617 return CastResult(state, R);
618 }
619
620 // Assume we are casting from pointer to pointer. Other cases are handled
621 // elsewhere.
622 QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType();
623
624 // Return the same region if the region types are compatible.
625 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
626 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
627
628 if (Ta == ToTy)
629 return CastResult(state, R);
630 }
631
632 // Process region cast according to the kind of the region being cast.
633
634
635 // FIXME: Need to handle arbitrary downcasts.
636 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
637 // or an AllocaRegion is cast to another view, thus causing the memory
638 // to be re-used for a different purpose.
639
640 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
641 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
642 return CastResult(AddRegionView(state, ViewR, R), ViewR);
643 }
644
645 // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally
646 // they should not be cast. We only layer an ElementRegion when the cast-to
647 // pointee type is of smaller size. In other cases, we return the original
648 // VarRegion.
649 if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R)
650 || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) {
Zhongxing Xuccb16162009-05-06 08:08:27 +0000651 if (isSmallerThan(PointeeTy,
652 cast<TypedRegion>(R)->getRValueType(getContext()))) {
653 SVal Idx = ValMgr.makeZeroArrayIndex();
654 ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx,
655 cast<TypedRegion>(R));
656 return CastResult(state, ER);
657 } else
658 return CastResult(state, R);
Zhongxing Xu88980592009-05-06 02:42:32 +0000659 }
660
661 if (isa<TypedViewRegion>(R)) {
662 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
663 return CastResult(state, ViewR);
664 }
665
666 if (isa<ObjCObjectRegion>(R)) {
667 return CastResult(state, R);
668 }
669
670 assert(0 && "Unprocessed region.");
671}
672
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000673SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
674 // Assume the base location is MemRegionVal(ElementRegion).
Ted Kremenek5dc27462009-03-03 02:51:43 +0000675 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000676 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000677
Zhongxing Xua1718c72009-04-03 07:33:13 +0000678 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
679 if (isa<SymbolicRegion>(MR))
680 return UnknownVal();
681
682 const TypedRegion* TR = cast<TypedRegion>(MR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000683 const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
684
685 if (!ER) {
686 // If the region is not element region, create one with index 0. This can
687 // happen in the following example:
688 // char *p = foo();
689 // p += 3;
690 // Note that p binds to a TypedViewRegion(SymbolicRegion).
691 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Ted Kremenekf936f452009-05-04 06:18:28 +0000692 ER = MRMgr.getElementRegion(TR->getRValueType(getContext()), Idx, TR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000693 }
694
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000695 SVal Idx = ER->getIndex();
696
697 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
698 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
699
700 // Only support concrete integer indexes for now.
701 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000702 // FIXME: For now, convert the signedness and bitwidth of offset in case
703 // they don't match. This can result from pointer arithmetic. In reality,
704 // we should figure out what are the proper semantics and implement them.
705 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000706 // This addresses the test case test/Analysis/ptr-arith.c
707 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000708 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
709 Offset->getValue()));
710 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000711 const MemRegion* NewER =
712 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
713 cast<TypedRegion>(ER->getSuperRegion()));
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000714 return Loc::MakeVal(NewER);
715
Ted Kremenek5dc27462009-03-03 02:51:43 +0000716 }
717
718 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000719}
720
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000721SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000722 assert(!isa<UnknownVal>(L) && "location unknown");
723 assert(!isa<UndefinedVal>(L) && "location undefined");
724
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000725 // FIXME: Is this even possible? Shouldn't this be treated as a null
726 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000727 if (isa<loc::ConcreteInt>(L))
728 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000729
Zhongxing Xua1718c72009-04-03 07:33:13 +0000730 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
731
732 // We return unknown for symbolic region for now. This might be improved.
733 // Example:
734 // void f(int* p) { int x = *p; }
735 if (isa<SymbolicRegion>(MR))
736 return UnknownVal();
737
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000738 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
739 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000740 const TypedRegion* R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000741 assert(R && "bad region");
742
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000743 // FIXME: We should eventually handle funny addressing. e.g.:
744 //
745 // int x = ...;
746 // int *p = &x;
747 // char *q = (char*) p;
748 // char c = *q; // returns the first byte of 'x'.
749 //
750 // Such funny addressing will occur due to layering of regions.
751
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000752 QualType RTy = R->getRValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000753
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000754 if (RTy->isStructureType())
755 return RetrieveStruct(St, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000756
757 if (RTy->isArrayType())
758 return RetrieveArray(St, R);
759
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000760 // FIXME: handle Vector types.
761 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000762 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000763
764 RegionBindingsTy B = GetRegionBindings(St->getStore());
765 RegionBindingsTy::data_type* V = B.lookup(R);
766
767 // Check if the region has a binding.
768 if (V)
769 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000770
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000771 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000772
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000773 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000774 if (state.contains<RegionKills>(R))
775 return UnknownVal();
776
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000777 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000778 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
779 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
780 GRStateTrait<RegionDefaultValue>::lookup_type D =
781 state.get<RegionDefaultValue>(SuperR);
782 if (D)
783 return *D;
784 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000785
786 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
787 const MemRegion *SR = IVR->getSuperRegion();
788
789 // If the super region is 'self' then return the symbol representing
790 // the value of the ivar upon entry to the method.
791 if (SR == SelfRegion) {
792 // FIXME: Do we need to handle the case where the super region
793 // has a view? We want to canonicalize the bindings.
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000794 return ValMgr.getRValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000795 }
796
797 // Otherwise, we need a new symbol. For now return Unknown.
798 return UnknownVal();
799 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000800
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000801 // The location does not have a bound value. This means that it has
802 // the value it had upon its creation and/or entry to the analyzed
803 // function/method. These are either symbolic values or 'undefined'.
804
805 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000806 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
807 const VarDecl *VD = VR->getDecl();
808
Ted Kremenekcec35252009-03-04 00:23:05 +0000809 if (VD == SelfDecl)
810 return loc::MemRegionVal(getSelfRegion(0));
811
812 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
813 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000814 QualType VTy = VD->getType();
815 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000816 return ValMgr.getRValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000817 else
818 return UnknownVal();
819 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000820 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000821
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000822 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
823 // All stack variables are considered to have undefined values
824 // upon creation. All heap allocated blocks are considered to
825 // have undefined values as well unless they are explicitly bound
826 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000827 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000828 }
829
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000830 // All other integer values are symbolic.
831 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000832 return ValMgr.getRValueSymbolVal(R);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000833 else
834 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000835}
836
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000837SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000838 // FIXME: Verify we want getRValueType instead of getLValueType.
839 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000840 assert(T->isStructureType());
841
842 const RecordType* RT = cast<RecordType>(T.getTypePtr());
843 RecordDecl* RD = RT->getDecl();
844 assert(RD->isDefinition());
845
846 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
847
Douglas Gregor6ab35242009-04-09 21:40:53 +0000848 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
849 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +0000850
Douglas Gregore267ff32008-12-11 20:41:00 +0000851 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
852 FieldEnd = Fields.rend();
853 Field != FieldEnd; ++Field) {
854 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000855 QualType FTy = (*Field)->getType();
856 SVal FieldValue = Retrieve(St, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000857 StructVal = getBasicVals().consVals(FieldValue, StructVal);
858 }
859
860 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
861}
862
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000863SVal RegionStoreManager::RetrieveArray(const GRState* St, const TypedRegion* R){
864 QualType T = R->getRValueType(getContext());
865 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
866
867 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000868 llvm::APSInt Size(CAT->getSize(), false);
869 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
870 Size.isUnsigned());
871
872 for (; i < Size; ++i) {
873 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +0000874 ElementRegion* ER = MRMgr.getElementRegion(R->getRValueType(getContext()),
875 Idx, R);
876 QualType ETy = ER->getElementType();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000877 SVal ElementVal = Retrieve(St, loc::MemRegionVal(ER), ETy);
878 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
879 }
880
881 return NonLoc::MakeCompoundVal(T, ArrayVal, getBasicVals());
882}
883
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000884const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000885 // If we get here, the location should be a region.
886 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000887 assert(R);
888
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000889 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000890 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000891 // FIXME: Verify we want getRValueType().
892 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000893 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000894
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000895 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000896 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000897
898 if (V.isUnknown()) {
899 // Remove the binding.
900 store = RBFactory.Remove(B, R).getRoot();
901
902 // Add the region to the killset.
903 GRStateRef state(St, StateMgr);
904 St = state.add<RegionKills>(R);
905 }
906 else
907 store = RBFactory.Add(B, R, V).getRoot();
908
909 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000910}
911
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000912Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000913 const MemRegion* R = 0;
914
915 if (isa<loc::MemRegionVal>(L))
916 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +0000917
918 if (R) {
919 RegionBindingsTy B = GetRegionBindings(store);
920 return RBFactory.Remove(B, R).getRoot();
921 }
922
923 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000924}
925
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000926const GRState* RegionStoreManager::BindDecl(const GRState* St,
927 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000928
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000929 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000930 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000931
Ted Kremenek0964a062009-01-21 06:57:53 +0000932 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000933 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000934 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000935 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000936
Ted Kremenek0964a062009-01-21 06:57:53 +0000937 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000938}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000939
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000940// FIXME: this method should be merged into Bind().
941const GRState*
942RegionStoreManager::BindCompoundLiteral(const GRState* St,
943 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000944 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000945 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000946}
947
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000948const GRState* RegionStoreManager::setExtent(const GRState* St,
949 const MemRegion* R, SVal Extent) {
950 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000951 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000952}
953
954
Ted Kremenek241677a2009-01-21 22:26:05 +0000955static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +0000956 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
957 const MemRegion *R = XR->getRegion();
958
959 while (R) {
960 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
961 SymReaper.markLive(SR->getSymbol());
962 return;
963 }
964
965 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
966 R = SR->getSuperRegion();
967 continue;
968 }
969
970 break;
971 }
972
973 return;
974 }
975
Ted Kremenek241677a2009-01-21 22:26:05 +0000976 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
977 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000978}
979
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000980Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000981 SymbolReaper& SymReaper,
982 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
983{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000984
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000985 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000986 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000987
988 // Lazily constructed backmap from MemRegions to SubRegions.
989 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
990 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
991
992 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
993 // the ability to reuse memory. This way we can keep TmpAlloc around as
994 // an instance variable of RegionStoreManager (avoiding repeated malloc
995 // overhead).
996 llvm::BumpPtrAllocator TmpAlloc;
997
998 // Factory objects.
999 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1000 SubRegionsTy::Factory SubRegF(TmpAlloc);
1001
1002 // The backmap from regions to subregions.
1003 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1004
1005 // Do a pass over the regions in the store. For VarRegions we check if
1006 // the variable is still live and if so add it to the list of live roots.
1007 // For other regions we populate our region backmap.
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001008
1009 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1010
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001011 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001012 IntermediateRoots.push_back(I.getKey());
1013 }
1014
1015 while (!IntermediateRoots.empty()) {
1016 const MemRegion* R = IntermediateRoots.back();
1017 IntermediateRoots.pop_back();
1018
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001019 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +00001020 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001021 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu5c86b192009-04-29 09:24:35 +00001022 }
1023 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1024 if (SymReaper.isLive(SR->getSymbol()))
1025 RegionRoots.push_back(SR);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001026 }
1027 else {
1028 // Get the super region for R.
1029 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001030
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001031 // Get the current set of subregions for SuperR.
1032 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
1033 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001034
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001035 // Add R to the subregions of SuperR.
1036 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001037
1038 // Super region may be VarRegion or subregion of another VarRegion. Add it
1039 // to the work list.
1040 if (isa<SubRegion>(SuperR))
1041 IntermediateRoots.push_back(SuperR);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001042 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001043 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001044
1045 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1046 // of the store. We want to find all live symbols and dead regions.
1047 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1048
1049 while (!RegionRoots.empty()) {
1050 // Dequeue the next region on the worklist.
1051 const MemRegion* R = RegionRoots.back();
1052 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001053
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001054 // Check if we have already processed this region.
1055 if (Marked.count(R)) continue;
1056
1057 // Mark this region as processed. This is needed for termination in case
1058 // a region is referenced more than once.
1059 Marked.insert(R);
1060
1061 // Mark the symbol for any live SymbolicRegion as "live". This means we
1062 // should continue to track that symbol.
1063 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +00001064 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001065
1066 // Get the data binding for R (if any).
1067 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1068 if (Xptr) {
1069 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +00001070 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001071
1072 // If X is a region, then add it the RegionRoots.
1073 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1074 RegionRoots.push_back(RegionX->getRegion());
1075 }
1076
1077 // Get the subregions of R. These are RegionRoots as well since they
1078 // represent values that are also bound to R.
1079 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1080 if (!SRptr) continue;
1081 SubRegionsTy SR = *SRptr;
1082
1083 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1084 RegionRoots.push_back(*I);
1085 }
1086
1087 // We have now scanned the store, marking reachable regions and symbols
1088 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001089 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001090 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1091 const MemRegion* R = I.getKey();
1092
1093 // If this region live? Is so, none of its symbols are dead.
1094 if (Marked.count(R))
1095 continue;
1096
1097 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001098 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001099
1100 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +00001101 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1102 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001103
1104 SVal X = I.getData();
1105 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +00001106 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001107 }
1108
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001109 return store;
1110}
1111
Zhongxing Xua071eb02008-10-24 06:01:33 +00001112void RegionStoreManager::print(Store store, std::ostream& Out,
1113 const char* nl, const char *sep) {
1114 llvm::raw_os_ostream OS(Out);
1115 RegionBindingsTy B = GetRegionBindings(store);
1116 OS << "Store:" << nl;
1117
1118 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1119 OS << ' '; I.getKey()->print(OS); OS << " : ";
1120 I.getData().print(OS); OS << nl;
1121 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001122}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001123
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001124const GRState* RegionStoreManager::BindArray(const GRState* St,
1125 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001126
1127 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +00001128 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001129 assert(T->isArrayType());
1130
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001131 // When we are binding the whole array, it always has default value 0.
1132 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001133 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1134 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001135
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001136 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1137
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001138 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001139 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1140 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001141
1142 // Check if the init expr is a StringLiteral.
1143 if (isa<loc::MemRegionVal>(Init)) {
1144 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1145 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1146 const char* str = S->getStrData();
1147 unsigned len = S->getByteLength();
1148 unsigned j = 0;
1149
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001150 // Copy bytes from the string literal into the target array. Trailing bytes
1151 // in the array that are not covered by the string literal are initialized
1152 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001153 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001154 if (j >= len)
1155 break;
1156
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001157 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001158 ElementRegion* ER =
1159 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
1160 Idx, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001161
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001162 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1163 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001164 }
1165
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001166 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001167 }
1168
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001169 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001170 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1171
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001172 for (; i < Size; ++i, ++VI) {
1173 // The init list might be shorter than the array decl.
1174 if (VI == VE)
1175 break;
1176
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001177 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001178 ElementRegion* ER =
1179 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
1180 Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001181
1182 if (CAT->getElementType()->isStructureType())
1183 St = BindStruct(St, ER, *VI);
1184 else
1185 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001186 }
1187
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001188 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001189}
1190
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001191const GRState*
1192RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001193 // FIXME: Verify that we should use getRValueType or getLValueType.
1194 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001195 assert(T->isStructureType());
1196
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001197 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001198 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001199
1200 if (!RD->isDefinition())
1201 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001202
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001203 if (V.isUnknown())
1204 return KillStruct(St, R);
1205
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001206 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001207 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001208 RecordDecl::field_iterator FI = RD->field_begin(getContext()),
1209 FE = RD->field_end(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001210
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001211 for (; FI != FE; ++FI, ++VI) {
1212
1213 // There may be fewer values than fields only when we are initializing a
1214 // struct decl. In this case, mark the region as having default value.
1215 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001216 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001217 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1218 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001219 break;
1220 }
1221
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001222 QualType FTy = (*FI)->getType();
1223 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1224
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001225 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1226 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001227
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001228 else if (FTy->isArrayType())
1229 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001230
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001231 else if (FTy->isStructureType())
1232 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001233 }
1234
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001235 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001236}
1237
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001238const GRState* RegionStoreManager::KillStruct(const GRState* St,
1239 const TypedRegion* R){
1240 GRStateRef state(St, StateMgr);
1241
1242 // Kill the struct region because it is assigned "unknown".
1243 St = state.add<RegionKills>(R);
1244
1245 // Set the default value of the struct region to "unknown".
1246 St = state.set<RegionDefaultValue>(R, UnknownVal());
1247
1248 Store store = St->getStore();
1249 RegionBindingsTy B = GetRegionBindings(store);
1250
1251 // Remove all bindings for the subregions of the struct.
1252 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1253 const MemRegion* r = I.getKey();
1254 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1255 if (sr->isSubRegionOf(R))
1256 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001257 // FIXME: Maybe we should also remove the bindings for the "views" of the
1258 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001259 }
1260
1261 return StateMgr.MakeStateWithStore(St, store);
1262}
1263
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001264const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1265 const MemRegion* View,
1266 const MemRegion* Base) {
1267 GRStateRef state(St, StateMgr);
1268
1269 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001270 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001271 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001272
1273 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001274 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001275
1276 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001277 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001278}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001279
1280const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1281 const MemRegion* View,
1282 const MemRegion* Base) {
1283 GRStateRef state(St, StateMgr);
1284
1285 // Retrieve the region view of the base region.
1286 const RegionViews* d = state.get<RegionViewMap>(Base);
1287
1288 // If the base region has no view, return.
1289 if (!d)
1290 return St;
1291
1292 // Remove the view.
1293 RegionViews V = *d;
1294 V = RVFactory.Remove(V, View);
1295
1296 return state.set<RegionViewMap>(Base, V);
1297}