blob: 8d36d10a8a515304d0407f8c5b5929b5018ccb88 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xubaf03a72008-11-24 09:44:56 +000029// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000030typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000031
Ted Kremenek50dc1b32008-12-24 01:05:03 +000032//===----------------------------------------------------------------------===//
33// Region "Views"
34//===----------------------------------------------------------------------===//
35//
36// MemRegions can be layered on top of each other. This GDM entry tracks
37// what are the MemRegions that layer a given MemRegion.
38//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000039typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000040namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
41static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000042namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000043 template<> struct GRStateTrait<RegionViewMap>
44 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
45 RegionViews> > {
46
47 static void* GDMIndex() { return &RegionViewMapIndex; }
48 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000049}
Zhongxing Xu17892752008-10-08 02:50:44 +000050
Ted Kremenek50dc1b32008-12-24 01:05:03 +000051//===----------------------------------------------------------------------===//
52// Region "Extents"
53//===----------------------------------------------------------------------===//
54//
55// MemRegions represent chunks of memory with a size (their "extent"). This
56// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000057//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000058namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
59static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000060namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000061 template<> struct GRStateTrait<RegionExtents>
62 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
63 static void* GDMIndex() { return &RegionExtentsIndex; }
64 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000065}
66
Ted Kremenek50dc1b32008-12-24 01:05:03 +000067//===----------------------------------------------------------------------===//
68// Region "killsets".
69//===----------------------------------------------------------------------===//
70//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000071// RegionStore lazily adds value bindings to regions when the analyzer handles
72// assignment statements. Killsets track which default values have been
73// killed, thus distinguishing between "unknown" values and default
74// values. Regions are added to killset only when they are assigned "unknown"
75// directly, otherwise we should have their value in the region bindings.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000076//
77namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek2ed14be2008-12-05 00:47:52 +000078static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000079namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000080 template<> struct GRStateTrait<RegionKills>
Ted Kremenek50dc1b32008-12-24 01:05:03 +000081 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000082 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000083 };
84}
85
Ted Kremenek50dc1b32008-12-24 01:05:03 +000086//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000087// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000088//===----------------------------------------------------------------------===//
89//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000090// This GDM entry tracks what regions have a default value if they have no bound
91// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000092//
93namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
94static int RegionDefaultValueIndex = 0;
95namespace clang {
96 template<> struct GRStateTrait<RegionDefaultValue>
97 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
98 static void* GDMIndex() { return &RegionDefaultValueIndex; }
99 };
100}
101
102//===----------------------------------------------------------------------===//
103// Main RegionStore logic.
104//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000105
Zhongxing Xu17892752008-10-08 02:50:44 +0000106namespace {
107
108class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
109 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000110 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000111
Zhongxing Xu17892752008-10-08 02:50:44 +0000112 GRStateManager& StateMgr;
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000113 const MemRegion* SelfRegion;
114 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000115
116public:
117 RegionStoreManager(GRStateManager& mgr)
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000118 : StoreManager(mgr.getAllocator()),
119 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000120 RVFactory(mgr.getAllocator()),
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000121 StateMgr(mgr), SelfRegion(0), SelfDecl(0) {
122 if (const ObjCMethodDecl* MD =
123 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
124 SelfDecl = MD->getSelfDecl();
125 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000126
127 virtual ~RegionStoreManager() {}
128
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000129 MemRegionManager& getRegionManager() { return MRMgr; }
Ted Kremenek4f090272008-10-27 21:54:31 +0000130
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000131 const GRState* BindCompoundLiteral(const GRState* St,
132 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000133
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000134 /// getLValueString - Returns an SVal representing the lvalue of a
135 /// StringLiteral. Within RegionStore a StringLiteral has an
136 /// associated StringRegion, and the lvalue of a StringLiteral is
137 /// the lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000138 SVal getLValueString(const GRState* St, const StringLiteral* S);
139
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000140 /// getLValueCompoundLiteral - Returns an SVal representing the
141 /// lvalue of a compound literal. Within RegionStore a compound
142 /// literal has an associated region, and the lvalue of the
143 /// compound literal is the lvalue of that region.
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000144 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
145
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000146 /// getLValueVar - Returns an SVal that represents the lvalue of a
147 /// variable. Within RegionStore a variable has an associated
148 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000149 SVal getLValueVar(const GRState* St, const VarDecl* VD);
150
151 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
152
153 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
154
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000155 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
156
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000157 SVal getSizeInElements(const GRState* St, const MemRegion* R);
158
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000159 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
160 /// type. 'Array' represents the lvalue of the array being decayed
161 /// to a pointer, and the returned SVal represents the decayed
162 /// version of that lvalue (i.e., a pointer to the first element of
163 /// the array). This is called by GRExprEngine when evaluating
164 /// casts from arrays to pointers.
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000165 SVal ArrayToPointer(SVal Array);
166
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000167 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
168 /// a MemRegion* to a specific location type. 'R' is the region being
169 /// casted and 'CastToTy' the result type of the cast.
170 CastResult CastRegion(const GRState* state, const MemRegion* R,
171 QualType CastToTy);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000172
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000173 /// The high level logic for this method is this:
174 /// Retrieve (L)
175 /// if L has binding
176 /// return L's binding
177 /// else if L is in killset
178 /// return unknown
179 /// else
180 /// if L is on stack or heap
181 /// return undefined
182 /// else
183 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000184 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000185
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000186 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000187
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000188 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000189
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000190 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000191
192 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
193 /// 'this' object (C++). When used when analyzing a normal function this
194 /// method returns NULL.
195 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000196 if (!SelfDecl)
197 return 0;
198
199 if (!SelfRegion) {
200 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
201 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
202 MRMgr.getHeapRegion());
203 }
204
205 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000206 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000207
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000208 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
209 /// It returns a new Store with these values removed, and populates LSymbols
210 // and DSymbols with the known set of live and dead symbols respectively.
211 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000212 SymbolReaper& SymReaper,
213 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000214
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000215 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
216
217 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
218 return St;
219 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000220
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000221 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
222
Zhongxing Xu17892752008-10-08 02:50:44 +0000223 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000224 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000225 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000226
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000227 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000228
229 void iterBindings(Store store, BindingsHandler& f) {
230 // FIXME: Implement.
231 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000232
233private:
234 Loc getVarLoc(const VarDecl* VD) {
235 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
236 }
237
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000238 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000239
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000240 /// Retrieve the values in a struct and return a CompoundVal, used when doing
241 /// struct copy:
242 /// struct s x, y;
243 /// x = y;
244 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000245 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000246
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000247 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000248
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000249 /// KillStruct - Set the entire struct to unknown.
250 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
251
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000252 // Utility methods.
253 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
254 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000255 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000256
257 const GRState* AddRegionView(const GRState* St,
258 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000259 const GRState* RemoveRegionView(const GRState* St,
260 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000261};
262
263} // end anonymous namespace
264
Ted Kremenek95c7b002008-10-24 01:04:59 +0000265StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000266 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000267}
268
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000269
270/// getLValueString - Returns an SVal representing the lvalue of a
271/// StringLiteral. Within RegionStore a StringLiteral has an
272/// associated StringRegion, and the lvalue of a StringLiteral is the
273/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000274SVal RegionStoreManager::getLValueString(const GRState* St,
275 const StringLiteral* S) {
276 return loc::MemRegionVal(MRMgr.getStringRegion(S));
277}
278
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000279/// getLValueVar - Returns an SVal that represents the lvalue of a
280/// variable. Within RegionStore a variable has an associated
281/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000282SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
283 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
284}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000285
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000286/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
287/// of a compound literal. Within RegionStore a compound literal
288/// has an associated region, and the lvalue of the compound literal
289/// is the lvalue of that region.
290SVal
291RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
292 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000293 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
294}
295
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000296SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
297 SVal Base) {
298 return UnknownVal();
299}
300
301SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
302 const FieldDecl* D) {
303 if (Base.isUnknownOrUndef())
304 return Base;
305
306 Loc BaseL = cast<Loc>(Base);
307 const MemRegion* BaseR = 0;
308
309 switch (BaseL.getSubKind()) {
310 case loc::MemRegionKind:
311 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
312 break;
313
314 case loc::SymbolValKind:
315 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
316 break;
317
318 case loc::GotoLabelKind:
319 case loc::FuncValKind:
320 // These are anormal cases. Flag an undefined value.
321 return UndefinedVal();
322
323 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000324 // While these seem funny, this can happen through casts.
325 // FIXME: What we should return is the field offset. For example,
326 // add the field offset to the integer value. That way funny things
327 // like this work properly: &(((struct foo *) 0xa)->f)
328 return Base;
329
330 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000331 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000332 return Base;
333 }
334
335 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
336}
337
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000338SVal RegionStoreManager::getLValueElement(const GRState* St,
339 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000340
Ted Kremenekc979e802009-01-21 22:58:50 +0000341 if (Base.isUnknownOrUndef() || isa<loc::SymbolVal>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000342 return Base;
343
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000344 // Only handle integer offsets... for now.
345 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000346 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000347
348 const TypedRegion *BaseRegion =
349 cast<TypedRegion>(cast<loc::MemRegionVal>(Base).getRegion());
350
351 // Pointer of any type can be cast and used as array base.
352 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
353
354 if (!ElemR) {
355 //
356 // If the base region is not an ElementRegion, create one.
357 // This can happen in the following example:
358 //
359 // char *p = __builtin_alloc(10);
360 // p[1] = 8;
361 //
362 // Observe that 'p' binds to an AnonTypedRegion<AllocaRegion>.
363 //
364 return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000365 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000366
367 SVal BaseIdx = ElemR->getIndex();
368
369 if (!isa<nonloc::ConcreteInt>(BaseIdx))
370 return UnknownVal();
371
372 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
373 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
374 assert(BaseIdxI.isSigned());
375
376 // FIXME: This appears to be the assumption of this code. We should review
377 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
378 // can't we need to put a comment here. If it can, we should handle it.
379 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000380
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000381 const TypedRegion *ArrayR = ElemR->getArrayRegion();
382 SVal NewIdx;
383
384 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
385 // 'Offset' might be unsigned. We have to convert it to signed and
386 // possibly extend it.
387 llvm::APSInt Tmp = OffI;
388
389 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
390 Tmp.extend(BaseIdxI.getBitWidth());
391
392 Tmp.setIsSigned(true);
393 Tmp += BaseIdxI; // Compute the new offset.
394 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(Tmp));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000395 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000396 else
397 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000398
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000399 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000400}
401
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000402SVal RegionStoreManager::getSizeInElements(const GRState* St,
403 const MemRegion* R) {
404 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
405 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000406 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000407
Ted Kremenek14553ab2009-01-30 00:08:43 +0000408 // FIXME: Handle variable-length arrays.
409 if (isa<VariableArrayType>(T))
410 return UnknownVal();
411
412 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
413 // return the size as signed integer.
414 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
415 }
416
417 // Clients can use ordinary variables as if they were arrays. These
418 // essentially are arrays of size 1.
419 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000420 }
421
422 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000423 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000424 // We intentionally made the size value signed because it participates in
425 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000426 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000427 }
428
429 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000430#if 0
431 // FIXME: This logic doesn't really work, as we can have all sorts of
432 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
433 // The weird cases come in when arbitrary casting comes into play, violating
434 // any type-safe programming.
435
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000436 GRStateRef state(St, StateMgr);
437
438 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000439 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
440 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000441
442 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000443 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000444
445 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000446 QualType LvT = ATR->getLValueType(getContext());
447 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000448
449 uint64_t X = getContext().getTypeSize(ElemTy);
450
451 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
452 false);
453
454 // Calculate the number of elements.
455
456 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
457 // signed?
458 if (SSize.isUnsigned())
459 SSize.setIsSigned(true);
460
461 // FIXME: move this operation into BasicVals.
462 const llvm::APSInt S =
463 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
464
465 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000466#else
467 ATR = ATR;
468 return UnknownVal();
469#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000470 }
471
472 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
473 // FIXME: Unsupported yet.
474 FR = 0;
475 return UnknownVal();
476 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000477
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000478 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000479 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000480}
481
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000482/// ArrayToPointer - Emulates the "decay" of an array to a pointer
483/// type. 'Array' represents the lvalue of the array being decayed
484/// to a pointer, and the returned SVal represents the decayed
485/// version of that lvalue (i.e., a pointer to the first element of
486/// the array). This is called by GRExprEngine when evaluating casts
487/// from arrays to pointers.
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000488SVal RegionStoreManager::ArrayToPointer(SVal Array) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000489 // FIXME: This should be factored into GRExprEngine. This allows
490 // us to pass a "loc" instead of an "SVal" for "Array".
Ted Kremenekabb042f2008-12-13 19:24:37 +0000491 if (Array.isUnknownOrUndef())
492 return Array;
493
494 if (!isa<loc::MemRegionVal>(Array))
495 return UnknownVal();
496
497 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
498 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
499
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000500 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000501 return UnknownVal();
502
Zhongxing Xu63123d82008-11-23 04:30:35 +0000503 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000504 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
505
506 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000507}
508
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000509StoreManager::CastResult
510RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
511 QualType CastToTy) {
512
513 // Return the same region if the region types are compatible.
514 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
515 ASTContext& Ctx = StateMgr.getContext();
516 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
517 QualType Tb = Ctx.getCanonicalType(CastToTy);
518
519 if (Ta == Tb)
520 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000521 }
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000522
523 // FIXME: We should handle the case when we are casting *back* to a
524 // previous type. For example:
525 //
526 // void* x = ...;
527 // char* y = (char*) x;
528 // void* z = (void*) y; // <-- we should get the same region that is
529 // bound to 'x'
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000530 const MemRegion* ViewR = MRMgr.getAnonTypedRegion(CastToTy, R);
531 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000532}
533
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000534SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000535 assert(!isa<UnknownVal>(L) && "location unknown");
536 assert(!isa<UndefinedVal>(L) && "location undefined");
537
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000538 // FIXME: What does loc::SymbolVal represent? It represents the value
539 // of a location but that value is not known. In the future we should
540 // handle potential aliasing relationships; e.g. a loc::SymbolVal could
541 // be an alias for a particular region.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000542 if (isa<loc::SymbolVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000543 return UnknownVal();
544
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000545 // FIXME: Is this even possible? Shouldn't this be treated as a null
546 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000547 if (isa<loc::ConcreteInt>(L))
548 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000549
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000550 // FIXME: Should this be refactored into GRExprEngine or GRStateManager?
551 // It seems that all StoreManagers would do the same thing here.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000552 if (isa<loc::FuncVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000553 return L;
554
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000555 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
556 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000557 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
558 assert(R && "bad region");
559
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000560 // FIXME: We should eventually handle funny addressing. e.g.:
561 //
562 // int x = ...;
563 // int *p = &x;
564 // char *q = (char*) p;
565 // char c = *q; // returns the first byte of 'x'.
566 //
567 // Such funny addressing will occur due to layering of regions.
568
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000569 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
570 if (TR->getRValueType(getContext())->isStructureType())
571 return RetrieveStruct(St, TR);
572
573 RegionBindingsTy B = GetRegionBindings(St->getStore());
574 RegionBindingsTy::data_type* V = B.lookup(R);
575
576 // Check if the region has a binding.
577 if (V)
578 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000579
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000580 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000581
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000582 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000583 if (state.contains<RegionKills>(R))
584 return UnknownVal();
585
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000586 // If the region is an element of field, it may have a default value.
587 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
588 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
589 GRStateTrait<RegionDefaultValue>::lookup_type D =
590 state.get<RegionDefaultValue>(SuperR);
591 if (D)
592 return *D;
593 }
594
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000595 // The location does not have a bound value. This means that it has
596 // the value it had upon its creation and/or entry to the analyzed
597 // function/method. These are either symbolic values or 'undefined'.
598
599 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000600 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
601 const VarDecl *VD = VR->getDecl();
602
603 if (isa<ParmVarDecl>(VD))
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000604 return SVal::GetRValueSymbolVal(getSymbolManager(), VR);
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000605 else if (VD == SelfDecl)
606 return loc::MemRegionVal(getSelfRegion(0));
607 }
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000608
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000609 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
610 // All stack variables are considered to have undefined values
611 // upon creation. All heap allocated blocks are considered to
612 // have undefined values as well unless they are explicitly bound
613 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000614 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000615 }
616
617 // All other values are symbolic.
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000618 return SVal::GetRValueSymbolVal(getSymbolManager(), R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000619
620 // FIXME: consider default values for elements and fields.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000621}
622
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000623SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
624
625 Store store = St->getStore();
626 GRStateRef state(St, StateMgr);
627
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000628 // FIXME: Verify we want getRValueType instead of getLValueType.
629 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000630 assert(T->isStructureType());
631
632 const RecordType* RT = cast<RecordType>(T.getTypePtr());
633 RecordDecl* RD = RT->getDecl();
634 assert(RD->isDefinition());
635
636 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
637
Douglas Gregore267ff32008-12-11 20:41:00 +0000638 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000639
Douglas Gregore267ff32008-12-11 20:41:00 +0000640 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
641 FieldEnd = Fields.rend();
642 Field != FieldEnd; ++Field) {
643 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000644 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000645 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000646
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000647 SVal FieldValue;
648 if (data)
649 FieldValue = *data;
650 else if (state.contains<RegionKills>(FR))
651 FieldValue = UnknownVal();
652 else {
653 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
654 FieldValue = UndefinedVal();
655 else
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000656 FieldValue = SVal::GetRValueSymbolVal(getSymbolManager(), FR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000657 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000658
659 StructVal = getBasicVals().consVals(FieldValue, StructVal);
660 }
661
662 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
663}
664
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000665const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
666 // Currently we don't bind value to symbolic location. But if the logic is
667 // made clear, we might change this decision.
668 if (isa<loc::SymbolVal>(L))
669 return St;
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000670
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000671 // If we get here, the location should be a region.
672 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000673 assert(R);
674
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000675 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000676 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000677 // FIXME: Verify we want getRValueType().
678 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000679 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000680
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000681 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000682 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000683
684 if (V.isUnknown()) {
685 // Remove the binding.
686 store = RBFactory.Remove(B, R).getRoot();
687
688 // Add the region to the killset.
689 GRStateRef state(St, StateMgr);
690 St = state.add<RegionKills>(R);
691 }
692 else
693 store = RBFactory.Add(B, R, V).getRoot();
694
695 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000696}
697
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000698Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000699 const MemRegion* R = 0;
700
701 if (isa<loc::MemRegionVal>(L))
702 R = cast<loc::MemRegionVal>(L).getRegion();
703 else if (isa<loc::SymbolVal>(L))
704 R = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(L).getSymbol());
705
706 if (R) {
707 RegionBindingsTy B = GetRegionBindings(store);
708 return RBFactory.Remove(B, R).getRoot();
709 }
710
711 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000712}
713
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000714const GRState* RegionStoreManager::BindDecl(const GRState* St,
715 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000716
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000717 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000718 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000719
Ted Kremenek0964a062009-01-21 06:57:53 +0000720 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000721 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000722 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000723 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000724
Ted Kremenek0964a062009-01-21 06:57:53 +0000725 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000726}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000727
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000728// FIXME: this method should be merged into Bind().
729const GRState*
730RegionStoreManager::BindCompoundLiteral(const GRState* St,
731 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000732 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000733 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000734}
735
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000736const GRState* RegionStoreManager::setExtent(const GRState* St,
737 const MemRegion* R, SVal Extent) {
738 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000739 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000740}
741
742
Ted Kremenek241677a2009-01-21 22:26:05 +0000743static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
744 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
745 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000746}
747
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000748Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000749 SymbolReaper& SymReaper,
750 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
751{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000752
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000753 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000754 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000755
756 // Lazily constructed backmap from MemRegions to SubRegions.
757 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
758 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
759
760 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
761 // the ability to reuse memory. This way we can keep TmpAlloc around as
762 // an instance variable of RegionStoreManager (avoiding repeated malloc
763 // overhead).
764 llvm::BumpPtrAllocator TmpAlloc;
765
766 // Factory objects.
767 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
768 SubRegionsTy::Factory SubRegF(TmpAlloc);
769
770 // The backmap from regions to subregions.
771 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
772
773 // Do a pass over the regions in the store. For VarRegions we check if
774 // the variable is still live and if so add it to the list of live roots.
775 // For other regions we populate our region backmap.
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000776 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000777 const MemRegion* R = I.getKey();
778 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000779 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000780 RegionRoots.push_back(VR); // This is a live "root".
781 }
782 else {
783 // Get the super region for R.
784 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
785 // Get the current set of subregions for SuperR.
786 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
787 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
788 // Add R to the subregions of SuperR.
789 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
790
791 // Finally, check if SuperR is a VarRegion. We need to do this
792 // to also mark SuperR as a root (as it may not have a value directly
793 // bound to it in the store).
794 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000795 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000796 RegionRoots.push_back(VR); // This is a live "root".
797 }
798 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000799 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000800
801 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
802 // of the store. We want to find all live symbols and dead regions.
803 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
804
805 while (!RegionRoots.empty()) {
806 // Dequeue the next region on the worklist.
807 const MemRegion* R = RegionRoots.back();
808 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000809
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000810 // Check if we have already processed this region.
811 if (Marked.count(R)) continue;
812
813 // Mark this region as processed. This is needed for termination in case
814 // a region is referenced more than once.
815 Marked.insert(R);
816
817 // Mark the symbol for any live SymbolicRegion as "live". This means we
818 // should continue to track that symbol.
819 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +0000820 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000821
822 // Get the data binding for R (if any).
823 RegionBindingsTy::data_type* Xptr = B.lookup(R);
824 if (Xptr) {
825 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +0000826 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000827
828 // If X is a region, then add it the RegionRoots.
829 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
830 RegionRoots.push_back(RegionX->getRegion());
831 }
832
833 // Get the subregions of R. These are RegionRoots as well since they
834 // represent values that are also bound to R.
835 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
836 if (!SRptr) continue;
837 SubRegionsTy SR = *SRptr;
838
839 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
840 RegionRoots.push_back(*I);
841 }
842
843 // We have now scanned the store, marking reachable regions and symbols
844 // as live. We now remove all the regions that are dead from the store
845 // as well as update DSymbols with the set symbols that are now dead.
846
847 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
848 const MemRegion* R = I.getKey();
849
850 // If this region live? Is so, none of its symbols are dead.
851 if (Marked.count(R))
852 continue;
853
854 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000855 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000856
857 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +0000858 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
859 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000860
861 SVal X = I.getData();
862 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +0000863 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000864 }
865
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000866 return store;
867}
868
Zhongxing Xua071eb02008-10-24 06:01:33 +0000869void RegionStoreManager::print(Store store, std::ostream& Out,
870 const char* nl, const char *sep) {
871 llvm::raw_os_ostream OS(Out);
872 RegionBindingsTy B = GetRegionBindings(store);
873 OS << "Store:" << nl;
874
875 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
876 OS << ' '; I.getKey()->print(OS); OS << " : ";
877 I.getData().print(OS); OS << nl;
878 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000879}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000880
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000881const GRState* RegionStoreManager::BindArray(const GRState* St,
882 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000883
884 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +0000885 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000886 assert(T->isArrayType());
887
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000888 // When we are binding the whole array, it always has default value 0.
889 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000890 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
891 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000892
893 Store store = St->getStore();
894
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000895 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
896
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000897 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +0000898 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
899 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000900
901 // Check if the init expr is a StringLiteral.
902 if (isa<loc::MemRegionVal>(Init)) {
903 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
904 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
905 const char* str = S->getStrData();
906 unsigned len = S->getByteLength();
907 unsigned j = 0;
908
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000909 // Copy bytes from the string literal into the target array. Trailing bytes
910 // in the array that are not covered by the string literal are initialized
911 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000912 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000913 if (j >= len)
914 break;
915
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000916 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
917 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
918
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000919 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
920 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000921 }
922
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000923 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000924 }
925
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000926
927 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
928
929 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
930
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000931 for (; i < Size; ++i, ++VI) {
932 // The init list might be shorter than the array decl.
933 if (VI == VE)
934 break;
935
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000936 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000937 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000938
939 if (CAT->getElementType()->isStructureType())
940 St = BindStruct(St, ER, *VI);
941 else
942 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000943 }
944
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000945 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000946}
947
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000948const GRState*
949RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000950 // FIXME: Verify that we should use getRValueType or getLValueType.
951 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000952 assert(T->isStructureType());
953
954 RecordType* RT = cast<RecordType>(T.getTypePtr());
955 RecordDecl* RD = RT->getDecl();
956 assert(RD->isDefinition());
957
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000958 if (V.isUnknown())
959 return KillStruct(St, R);
960
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000961 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000962 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
963 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
964
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000965 for (; FI != FE; ++FI, ++VI) {
966
967 // There may be fewer values than fields only when we are initializing a
968 // struct decl. In this case, mark the region as having default value.
969 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +0000970 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000971 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
972 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000973 break;
974 }
975
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000976 QualType FTy = (*FI)->getType();
977 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
978
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000979 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
980 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000981
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000982 else if (FTy->isArrayType())
983 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000984
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000985 else if (FTy->isStructureType())
986 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000987 }
988
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000989 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000990}
991
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000992const GRState* RegionStoreManager::KillStruct(const GRState* St,
993 const TypedRegion* R){
994 GRStateRef state(St, StateMgr);
995
996 // Kill the struct region because it is assigned "unknown".
997 St = state.add<RegionKills>(R);
998
999 // Set the default value of the struct region to "unknown".
1000 St = state.set<RegionDefaultValue>(R, UnknownVal());
1001
1002 Store store = St->getStore();
1003 RegionBindingsTy B = GetRegionBindings(store);
1004
1005 // Remove all bindings for the subregions of the struct.
1006 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1007 const MemRegion* r = I.getKey();
1008 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1009 if (sr->isSubRegionOf(R))
1010 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001011 // FIXME: Maybe we should also remove the bindings for the "views" of the
1012 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001013 }
1014
1015 return StateMgr.MakeStateWithStore(St, store);
1016}
1017
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001018const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1019 const MemRegion* View,
1020 const MemRegion* Base) {
1021 GRStateRef state(St, StateMgr);
1022
1023 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001024 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001025 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001026
1027 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001028 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001029
1030 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001031 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001032}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001033
1034const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1035 const MemRegion* View,
1036 const MemRegion* Base) {
1037 GRStateRef state(St, StateMgr);
1038
1039 // Retrieve the region view of the base region.
1040 const RegionViews* d = state.get<RegionViewMap>(Base);
1041
1042 // If the base region has no view, return.
1043 if (!d)
1044 return St;
1045
1046 // Remove the view.
1047 RegionViews V = *d;
1048 V = RVFactory.Remove(V, View);
1049
1050 return state.set<RegionViewMap>(Base, V);
1051}