blob: a76a870eb445ec0092019986c491c7856b325c28 [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 Xu94aa6c12009-03-02 07:52:23 +0000173 SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R);
174
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000175 /// The high level logic for this method is this:
176 /// Retrieve (L)
177 /// if L has binding
178 /// return L's binding
179 /// else if L is in killset
180 /// return unknown
181 /// else
182 /// if L is on stack or heap
183 /// return undefined
184 /// else
185 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000186 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000187
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000188 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000189
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000190 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000191
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000192 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000193
194 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
195 /// 'this' object (C++). When used when analyzing a normal function this
196 /// method returns NULL.
197 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000198 if (!SelfDecl)
199 return 0;
200
201 if (!SelfRegion) {
202 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
203 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
204 MRMgr.getHeapRegion());
205 }
206
207 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000208 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000209
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000210 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
211 /// It returns a new Store with these values removed, and populates LSymbols
212 // and DSymbols with the known set of live and dead symbols respectively.
213 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000214 SymbolReaper& SymReaper,
215 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000216
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000217 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
218
219 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
220 return St;
221 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000222
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000223 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
224
Zhongxing Xu17892752008-10-08 02:50:44 +0000225 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000226 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000227 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000228
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000229 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000230
231 void iterBindings(Store store, BindingsHandler& f) {
232 // FIXME: Implement.
233 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000234
235private:
236 Loc getVarLoc(const VarDecl* VD) {
237 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
238 }
239
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000240 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000241
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000242 /// Retrieve the values in a struct and return a CompoundVal, used when doing
243 /// struct copy:
244 /// struct s x, y;
245 /// x = y;
246 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000247 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000248
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000249 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000250
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000251 /// KillStruct - Set the entire struct to unknown.
252 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
253
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000254 // Utility methods.
255 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
256 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000257 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000258
259 const GRState* AddRegionView(const GRState* St,
260 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000261 const GRState* RemoveRegionView(const GRState* St,
262 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000263};
264
265} // end anonymous namespace
266
Ted Kremenek95c7b002008-10-24 01:04:59 +0000267StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000268 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000269}
270
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000271
272/// getLValueString - Returns an SVal representing the lvalue of a
273/// StringLiteral. Within RegionStore a StringLiteral has an
274/// associated StringRegion, and the lvalue of a StringLiteral is the
275/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000276SVal RegionStoreManager::getLValueString(const GRState* St,
277 const StringLiteral* S) {
278 return loc::MemRegionVal(MRMgr.getStringRegion(S));
279}
280
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000281/// getLValueVar - Returns an SVal that represents the lvalue of a
282/// variable. Within RegionStore a variable has an associated
283/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000284SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
285 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
286}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000287
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000288/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
289/// of a compound literal. Within RegionStore a compound literal
290/// has an associated region, and the lvalue of the compound literal
291/// is the lvalue of that region.
292SVal
293RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
294 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000295 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
296}
297
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000298SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
299 SVal Base) {
300 return UnknownVal();
301}
302
303SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
304 const FieldDecl* D) {
305 if (Base.isUnknownOrUndef())
306 return Base;
307
308 Loc BaseL = cast<Loc>(Base);
309 const MemRegion* BaseR = 0;
310
311 switch (BaseL.getSubKind()) {
312 case loc::MemRegionKind:
313 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
314 break;
315
316 case loc::SymbolValKind:
Zhongxing Xu026c6632009-02-05 06:57:29 +0000317 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol(),
318 StateMgr.getSymbolManager());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000319 break;
320
321 case loc::GotoLabelKind:
322 case loc::FuncValKind:
323 // These are anormal cases. Flag an undefined value.
324 return UndefinedVal();
325
326 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000327 // While these seem funny, this can happen through casts.
328 // FIXME: What we should return is the field offset. For example,
329 // add the field offset to the integer value. That way funny things
330 // like this work properly: &(((struct foo *) 0xa)->f)
331 return Base;
332
333 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000334 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000335 return Base;
336 }
337
338 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
339}
340
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000341SVal RegionStoreManager::getLValueElement(const GRState* St,
342 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000343
Zhongxing Xua48f7372009-02-06 08:44:27 +0000344 if (Base.isUnknownOrUndef())
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000345 return Base;
346
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000347 // Only handle integer offsets... for now.
348 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000349 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000350
Zhongxing Xua48f7372009-02-06 08:44:27 +0000351 const TypedRegion* BaseRegion = 0;
352
353 if (isa<loc::SymbolVal>(Base))
354 BaseRegion = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(Base).getSymbol(),
355 StateMgr.getSymbolManager());
356 else
357 BaseRegion = cast<TypedRegion>(cast<loc::MemRegionVal>(Base).getRegion());
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000358
359 // Pointer of any type can be cast and used as array base.
360 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
361
362 if (!ElemR) {
363 //
364 // If the base region is not an ElementRegion, create one.
365 // This can happen in the following example:
366 //
367 // char *p = __builtin_alloc(10);
368 // p[1] = 8;
369 //
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000370 // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000371 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000372
373 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
374 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
375 const llvm::APSInt& OffI = CI->getValue();
376 if (OffI.isUnsigned()) {
377 llvm::APSInt Tmp = OffI;
378 Tmp.setIsSigned(true);
379 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
380 }
381 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000382 return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000383 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000384
385 SVal BaseIdx = ElemR->getIndex();
386
387 if (!isa<nonloc::ConcreteInt>(BaseIdx))
388 return UnknownVal();
389
390 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
391 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
392 assert(BaseIdxI.isSigned());
393
394 // FIXME: This appears to be the assumption of this code. We should review
395 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
396 // can't we need to put a comment here. If it can, we should handle it.
397 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000398
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000399 const TypedRegion *ArrayR = ElemR->getArrayRegion();
400 SVal NewIdx;
401
402 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
403 // 'Offset' might be unsigned. We have to convert it to signed and
404 // possibly extend it.
405 llvm::APSInt Tmp = OffI;
406
407 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
408 Tmp.extend(BaseIdxI.getBitWidth());
409
410 Tmp.setIsSigned(true);
411 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000412 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000413 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000414 else
415 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000416
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000417 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000418}
419
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000420SVal RegionStoreManager::getSizeInElements(const GRState* St,
421 const MemRegion* R) {
422 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
423 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000424 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000425
Ted Kremenek14553ab2009-01-30 00:08:43 +0000426 // FIXME: Handle variable-length arrays.
427 if (isa<VariableArrayType>(T))
428 return UnknownVal();
429
430 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
431 // return the size as signed integer.
432 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
433 }
434
435 // Clients can use ordinary variables as if they were arrays. These
436 // essentially are arrays of size 1.
437 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000438 }
439
440 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000441 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000442 // We intentionally made the size value signed because it participates in
443 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000444 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000445 }
446
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000447 if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000448#if 0
449 // FIXME: This logic doesn't really work, as we can have all sorts of
450 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
451 // The weird cases come in when arbitrary casting comes into play, violating
452 // any type-safe programming.
453
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000454 GRStateRef state(St, StateMgr);
455
456 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000457 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
458 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000459
460 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000461 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000462
463 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000464 QualType LvT = ATR->getLValueType(getContext());
465 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000466
467 uint64_t X = getContext().getTypeSize(ElemTy);
468
469 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
470 false);
471
472 // Calculate the number of elements.
473
474 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
475 // signed?
476 if (SSize.isUnsigned())
477 SSize.setIsSigned(true);
478
479 // FIXME: move this operation into BasicVals.
480 const llvm::APSInt S =
481 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
482
483 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000484#else
485 ATR = ATR;
486 return UnknownVal();
487#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000488 }
489
490 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
491 // FIXME: Unsupported yet.
492 FR = 0;
493 return UnknownVal();
494 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000495
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000496 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000497 return UnknownVal();
498 }
499
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000500 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000501 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000502}
503
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000504/// ArrayToPointer - Emulates the "decay" of an array to a pointer
505/// type. 'Array' represents the lvalue of the array being decayed
506/// to a pointer, and the returned SVal represents the decayed
507/// version of that lvalue (i.e., a pointer to the first element of
508/// the array). This is called by GRExprEngine when evaluating casts
509/// from arrays to pointers.
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000510SVal RegionStoreManager::ArrayToPointer(SVal Array) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000511 // FIXME: This should be factored into GRExprEngine. This allows
512 // us to pass a "loc" instead of an "SVal" for "Array".
Ted Kremenekabb042f2008-12-13 19:24:37 +0000513 if (Array.isUnknownOrUndef())
514 return Array;
515
516 if (!isa<loc::MemRegionVal>(Array))
517 return UnknownVal();
518
519 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
520 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
521
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000522 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000523 return UnknownVal();
524
Zhongxing Xu63123d82008-11-23 04:30:35 +0000525 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000526 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
527
528 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000529}
530
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000531StoreManager::CastResult
532RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
533 QualType CastToTy) {
534
535 // Return the same region if the region types are compatible.
536 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
537 ASTContext& Ctx = StateMgr.getContext();
538 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
539 QualType Tb = Ctx.getCanonicalType(CastToTy);
540
541 if (Ta == Tb)
542 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000543 }
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000544
545 // FIXME: We should handle the case when we are casting *back* to a
546 // previous type. For example:
547 //
548 // void* x = ...;
549 // char* y = (char*) x;
550 // void* z = (void*) y; // <-- we should get the same region that is
551 // bound to 'x'
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000552 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000553 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000554}
555
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000556SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
557 // Assume the base location is MemRegionVal(ElementRegion).
558
559 if (!isa<loc::MemRegionVal>(L)) {
560 return UnknownVal();
561 }
562
563 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
564
565 const ElementRegion* ER = cast<ElementRegion>(MR);
566 SVal Idx = ER->getIndex();
567
568 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
569 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
570
571 // Only support concrete integer indexes for now.
572 if (Base && Offset) {
573 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, *Offset);
574
575 const MemRegion* NewER = MRMgr.getElementRegion(NewIdx,
576 ER->getArrayRegion());
577 return Loc::MakeVal(NewER);
578
579 } else
580 return UnknownVal();
581}
582
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000583SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000584 assert(!isa<UnknownVal>(L) && "location unknown");
585 assert(!isa<UndefinedVal>(L) && "location undefined");
586
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000587 // FIXME: What does loc::SymbolVal represent? It represents the value
588 // of a location but that value is not known. In the future we should
589 // handle potential aliasing relationships; e.g. a loc::SymbolVal could
590 // be an alias for a particular region.
Zhongxing Xu779747c2009-02-20 05:19:30 +0000591 // Example:
592 // void foo(char* buf) {
593 // char c = *buf;
594 // }
595 if (isa<loc::SymbolVal>(L)) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000596 return UnknownVal();
Zhongxing Xu779747c2009-02-20 05:19:30 +0000597 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000598
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000599 // FIXME: Is this even possible? Shouldn't this be treated as a null
600 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000601 if (isa<loc::ConcreteInt>(L))
602 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000603
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000604 // FIXME: Should this be refactored into GRExprEngine or GRStateManager?
605 // It seems that all StoreManagers would do the same thing here.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000606 if (isa<loc::FuncVal>(L))
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000607 return L;
608
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000609 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
610 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000611 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
612 assert(R && "bad region");
613
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000614 // FIXME: We should eventually handle funny addressing. e.g.:
615 //
616 // int x = ...;
617 // int *p = &x;
618 // char *q = (char*) p;
619 // char c = *q; // returns the first byte of 'x'.
620 //
621 // Such funny addressing will occur due to layering of regions.
622
Ted Kremenek265a3052009-02-24 02:23:11 +0000623 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
624 QualType T =TR->getRValueType(getContext());
625 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000626 return RetrieveStruct(St, TR);
Ted Kremenek265a3052009-02-24 02:23:11 +0000627 // FIXME: handle Vector types.
628 if (T->isVectorType())
629 return UnknownVal();
630 }
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000631
632 RegionBindingsTy B = GetRegionBindings(St->getStore());
633 RegionBindingsTy::data_type* V = B.lookup(R);
634
635 // Check if the region has a binding.
636 if (V)
637 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000638
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000639 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000640
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000641 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000642 if (state.contains<RegionKills>(R))
643 return UnknownVal();
644
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000645 // If the region is an element of field, it may have a default value.
646 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
647 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
648 GRStateTrait<RegionDefaultValue>::lookup_type D =
649 state.get<RegionDefaultValue>(SuperR);
650 if (D)
651 return *D;
652 }
653
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000654 // The location does not have a bound value. This means that it has
655 // the value it had upon its creation and/or entry to the analyzed
656 // function/method. These are either symbolic values or 'undefined'.
657
658 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000659 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
660 const VarDecl *VD = VR->getDecl();
661
Zhongxing Xud8895f62009-02-19 09:56:08 +0000662 if (isa<ParmVarDecl>(VD) || VD->hasGlobalStorage()) {
663 QualType VTy = VD->getType();
664 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
665 return SVal::GetRValueSymbolVal(getSymbolManager(), VR);
666 else
667 return UnknownVal();
668 }
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000669 else if (VD == SelfDecl)
670 return loc::MemRegionVal(getSelfRegion(0));
671 }
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000672
Ted Kremenek265a3052009-02-24 02:23:11 +0000673
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000674 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
675 // All stack variables are considered to have undefined values
676 // upon creation. All heap allocated blocks are considered to
677 // have undefined values as well unless they are explicitly bound
678 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000679 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000680 }
681
682 // All other values are symbolic.
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000683 return SVal::GetRValueSymbolVal(getSymbolManager(), R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000684}
685
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000686SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
687
688 Store store = St->getStore();
689 GRStateRef state(St, StateMgr);
690
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000691 // FIXME: Verify we want getRValueType instead of getLValueType.
692 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000693 assert(T->isStructureType());
694
695 const RecordType* RT = cast<RecordType>(T.getTypePtr());
696 RecordDecl* RD = RT->getDecl();
697 assert(RD->isDefinition());
698
699 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
700
Douglas Gregore267ff32008-12-11 20:41:00 +0000701 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000702
Douglas Gregore267ff32008-12-11 20:41:00 +0000703 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
704 FieldEnd = Fields.rend();
705 Field != FieldEnd; ++Field) {
706 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000707 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000708 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000709
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000710 SVal FieldValue;
711 if (data)
712 FieldValue = *data;
713 else if (state.contains<RegionKills>(FR))
714 FieldValue = UnknownVal();
715 else {
716 if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
717 FieldValue = UndefinedVal();
718 else
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000719 FieldValue = SVal::GetRValueSymbolVal(getSymbolManager(), FR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000720 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000721
722 StructVal = getBasicVals().consVals(FieldValue, StructVal);
723 }
724
725 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
726}
727
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000728const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
729 // Currently we don't bind value to symbolic location. But if the logic is
730 // made clear, we might change this decision.
731 if (isa<loc::SymbolVal>(L))
732 return St;
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000733
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000734 // If we get here, the location should be a region.
735 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000736 assert(R);
737
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000738 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000739 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000740 // FIXME: Verify we want getRValueType().
741 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000742 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000743
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000744 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000745 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000746
747 if (V.isUnknown()) {
748 // Remove the binding.
749 store = RBFactory.Remove(B, R).getRoot();
750
751 // Add the region to the killset.
752 GRStateRef state(St, StateMgr);
753 St = state.add<RegionKills>(R);
754 }
755 else
756 store = RBFactory.Add(B, R, V).getRoot();
757
758 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000759}
760
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000761Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000762 const MemRegion* R = 0;
763
764 if (isa<loc::MemRegionVal>(L))
765 R = cast<loc::MemRegionVal>(L).getRegion();
766 else if (isa<loc::SymbolVal>(L))
Zhongxing Xu026c6632009-02-05 06:57:29 +0000767 R = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(L).getSymbol(),
768 StateMgr.getSymbolManager());
Ted Kremenek0964a062009-01-21 06:57:53 +0000769
770 if (R) {
771 RegionBindingsTy B = GetRegionBindings(store);
772 return RBFactory.Remove(B, R).getRoot();
773 }
774
775 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000776}
777
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000778const GRState* RegionStoreManager::BindDecl(const GRState* St,
779 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000780
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000781 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000782 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000783
Ted Kremenek0964a062009-01-21 06:57:53 +0000784 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000785 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000786 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000787 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000788
Ted Kremenek0964a062009-01-21 06:57:53 +0000789 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000790}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000791
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000792// FIXME: this method should be merged into Bind().
793const GRState*
794RegionStoreManager::BindCompoundLiteral(const GRState* St,
795 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000796 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000797 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000798}
799
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000800const GRState* RegionStoreManager::setExtent(const GRState* St,
801 const MemRegion* R, SVal Extent) {
802 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000803 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000804}
805
806
Ted Kremenek241677a2009-01-21 22:26:05 +0000807static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
808 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
809 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000810}
811
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000812Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000813 SymbolReaper& SymReaper,
814 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
815{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000816
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000817 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000818 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000819
820 // Lazily constructed backmap from MemRegions to SubRegions.
821 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
822 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
823
824 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
825 // the ability to reuse memory. This way we can keep TmpAlloc around as
826 // an instance variable of RegionStoreManager (avoiding repeated malloc
827 // overhead).
828 llvm::BumpPtrAllocator TmpAlloc;
829
830 // Factory objects.
831 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
832 SubRegionsTy::Factory SubRegF(TmpAlloc);
833
834 // The backmap from regions to subregions.
835 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
836
837 // Do a pass over the regions in the store. For VarRegions we check if
838 // the variable is still live and if so add it to the list of live roots.
839 // For other regions we populate our region backmap.
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000840 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000841 const MemRegion* R = I.getKey();
842 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000843 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000844 RegionRoots.push_back(VR); // This is a live "root".
845 }
846 else {
847 // Get the super region for R.
848 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
849 // Get the current set of subregions for SuperR.
850 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
851 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
852 // Add R to the subregions of SuperR.
853 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
854
855 // Finally, check if SuperR is a VarRegion. We need to do this
856 // to also mark SuperR as a root (as it may not have a value directly
857 // bound to it in the store).
858 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000859 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000860 RegionRoots.push_back(VR); // This is a live "root".
861 }
862 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000863 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000864
865 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
866 // of the store. We want to find all live symbols and dead regions.
867 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
868
869 while (!RegionRoots.empty()) {
870 // Dequeue the next region on the worklist.
871 const MemRegion* R = RegionRoots.back();
872 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000873
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000874 // Check if we have already processed this region.
875 if (Marked.count(R)) continue;
876
877 // Mark this region as processed. This is needed for termination in case
878 // a region is referenced more than once.
879 Marked.insert(R);
880
881 // Mark the symbol for any live SymbolicRegion as "live". This means we
882 // should continue to track that symbol.
883 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +0000884 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000885
886 // Get the data binding for R (if any).
887 RegionBindingsTy::data_type* Xptr = B.lookup(R);
888 if (Xptr) {
889 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +0000890 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000891
892 // If X is a region, then add it the RegionRoots.
893 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
894 RegionRoots.push_back(RegionX->getRegion());
895 }
896
897 // Get the subregions of R. These are RegionRoots as well since they
898 // represent values that are also bound to R.
899 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
900 if (!SRptr) continue;
901 SubRegionsTy SR = *SRptr;
902
903 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
904 RegionRoots.push_back(*I);
905 }
906
907 // We have now scanned the store, marking reachable regions and symbols
908 // as live. We now remove all the regions that are dead from the store
909 // as well as update DSymbols with the set symbols that are now dead.
910
911 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
912 const MemRegion* R = I.getKey();
913
914 // If this region live? Is so, none of its symbols are dead.
915 if (Marked.count(R))
916 continue;
917
918 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000919 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000920
921 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +0000922 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
923 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000924
925 SVal X = I.getData();
926 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +0000927 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000928 }
929
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000930 return store;
931}
932
Zhongxing Xua071eb02008-10-24 06:01:33 +0000933void RegionStoreManager::print(Store store, std::ostream& Out,
934 const char* nl, const char *sep) {
935 llvm::raw_os_ostream OS(Out);
936 RegionBindingsTy B = GetRegionBindings(store);
937 OS << "Store:" << nl;
938
939 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
940 OS << ' '; I.getKey()->print(OS); OS << " : ";
941 I.getData().print(OS); OS << nl;
942 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000943}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000944
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000945const GRState* RegionStoreManager::BindArray(const GRState* St,
946 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000947
948 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +0000949 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000950 assert(T->isArrayType());
951
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000952 // When we are binding the whole array, it always has default value 0.
953 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000954 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
955 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000956
957 Store store = St->getStore();
958
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000959 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
960
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000961 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +0000962 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
963 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000964
965 // Check if the init expr is a StringLiteral.
966 if (isa<loc::MemRegionVal>(Init)) {
967 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
968 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
969 const char* str = S->getStrData();
970 unsigned len = S->getByteLength();
971 unsigned j = 0;
972
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000973 // Copy bytes from the string literal into the target array. Trailing bytes
974 // in the array that are not covered by the string literal are initialized
975 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000976 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000977 if (j >= len)
978 break;
979
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000980 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
981 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
982
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000983 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
984 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000985 }
986
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000987 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000988 }
989
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000990
991 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
992
993 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
994
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000995 for (; i < Size; ++i, ++VI) {
996 // The init list might be shorter than the array decl.
997 if (VI == VE)
998 break;
999
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001000 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001001 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001002
1003 if (CAT->getElementType()->isStructureType())
1004 St = BindStruct(St, ER, *VI);
1005 else
1006 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001007 }
1008
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001009 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001010}
1011
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001012const GRState*
1013RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001014 // FIXME: Verify that we should use getRValueType or getLValueType.
1015 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001016 assert(T->isStructureType());
1017
1018 RecordType* RT = cast<RecordType>(T.getTypePtr());
1019 RecordDecl* RD = RT->getDecl();
1020 assert(RD->isDefinition());
1021
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001022 if (V.isUnknown())
1023 return KillStruct(St, R);
1024
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001025 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001026 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1027 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
1028
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001029 for (; FI != FE; ++FI, ++VI) {
1030
1031 // There may be fewer values than fields only when we are initializing a
1032 // struct decl. In this case, mark the region as having default value.
1033 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001034 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001035 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1036 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001037 break;
1038 }
1039
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001040 QualType FTy = (*FI)->getType();
1041 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1042
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001043 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1044 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001045
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001046 else if (FTy->isArrayType())
1047 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001048
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001049 else if (FTy->isStructureType())
1050 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001051 }
1052
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001053 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001054}
1055
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001056const GRState* RegionStoreManager::KillStruct(const GRState* St,
1057 const TypedRegion* R){
1058 GRStateRef state(St, StateMgr);
1059
1060 // Kill the struct region because it is assigned "unknown".
1061 St = state.add<RegionKills>(R);
1062
1063 // Set the default value of the struct region to "unknown".
1064 St = state.set<RegionDefaultValue>(R, UnknownVal());
1065
1066 Store store = St->getStore();
1067 RegionBindingsTy B = GetRegionBindings(store);
1068
1069 // Remove all bindings for the subregions of the struct.
1070 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1071 const MemRegion* r = I.getKey();
1072 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1073 if (sr->isSubRegionOf(R))
1074 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001075 // FIXME: Maybe we should also remove the bindings for the "views" of the
1076 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001077 }
1078
1079 return StateMgr.MakeStateWithStore(St, store);
1080}
1081
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001082const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1083 const MemRegion* View,
1084 const MemRegion* Base) {
1085 GRStateRef state(St, StateMgr);
1086
1087 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001088 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001089 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001090
1091 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001092 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001093
1094 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001095 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001096}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001097
1098const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1099 const MemRegion* View,
1100 const MemRegion* Base) {
1101 GRStateRef state(St, StateMgr);
1102
1103 // Retrieve the region view of the base region.
1104 const RegionViews* d = state.get<RegionViewMap>(Base);
1105
1106 // If the base region has no view, return.
1107 if (!d)
1108 return St;
1109
1110 // Remove the view.
1111 RegionViews V = *d;
1112 V = RVFactory.Remove(V, View);
1113
1114 return state.set<RegionViewMap>(Base, V);
1115}