blob: c533a7ec074ddced70c9cea8f8166e9cb1b62e0d [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"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000021#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000022
23#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000024#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000025#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000026#include "llvm/Support/Compiler.h"
27
28using namespace clang;
29
Ted Kremenek356e9d62009-07-22 04:35:42 +000030#define USE_REGION_CASTS 0
31#define HEAP_UNDEFINED 0
32
Zhongxing Xubaf03a72008-11-24 09:44:56 +000033// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000034typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000035
Ted Kremenek50dc1b32008-12-24 01:05:03 +000036//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000037// Fine-grained control of RegionStoreManager.
38//===----------------------------------------------------------------------===//
39
40namespace {
41struct VISIBILITY_HIDDEN minimal_features_tag {};
42struct VISIBILITY_HIDDEN maximal_features_tag {};
43
44class VISIBILITY_HIDDEN RegionStoreFeatures {
45 bool SupportsFields;
46 bool SupportsRemaining;
47
48public:
49 RegionStoreFeatures(minimal_features_tag) :
50 SupportsFields(false), SupportsRemaining(false) {}
51
52 RegionStoreFeatures(maximal_features_tag) :
53 SupportsFields(true), SupportsRemaining(false) {}
54
55 void enableFields(bool t) { SupportsFields = t; }
56
57 bool supportsFields() const { return SupportsFields; }
58 bool supportsRemaining() const { return SupportsRemaining; }
59};
60}
61
62//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000063// Region "Views"
64//===----------------------------------------------------------------------===//
65//
66// MemRegions can be layered on top of each other. This GDM entry tracks
67// what are the MemRegions that layer a given MemRegion.
68//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000069typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000070namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
71static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000072namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000073 template<> struct GRStateTrait<RegionViewMap>
74 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
75 RegionViews> > {
76
77 static void* GDMIndex() { return &RegionViewMapIndex; }
78 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000079}
Zhongxing Xu17892752008-10-08 02:50:44 +000080
Zhongxing Xu20794942009-05-06 08:33:50 +000081// RegionCasts records the current cast type of a region.
82namespace { class VISIBILITY_HIDDEN RegionCasts {}; }
83static int RegionCastsIndex = 0;
84namespace clang {
85 template<> struct GRStateTrait<RegionCasts>
86 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
87 QualType> > {
88 static void* GDMIndex() { return &RegionCastsIndex; }
89 };
90}
91
Ted Kremenek50dc1b32008-12-24 01:05:03 +000092//===----------------------------------------------------------------------===//
93// Region "Extents"
94//===----------------------------------------------------------------------===//
95//
96// MemRegions represent chunks of memory with a size (their "extent"). This
97// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000098//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000099namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
100static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000101namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000102 template<> struct GRStateTrait<RegionExtents>
103 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
104 static void* GDMIndex() { return &RegionExtentsIndex; }
105 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000106}
107
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000108//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000109// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000110//===----------------------------------------------------------------------===//
111//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000112// This GDM entry tracks what regions have a default value if they have no bound
113// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000114//
115namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
116static int RegionDefaultValueIndex = 0;
117namespace clang {
118 template<> struct GRStateTrait<RegionDefaultValue>
119 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
120 static void* GDMIndex() { return &RegionDefaultValueIndex; }
121 };
122}
123
124//===----------------------------------------------------------------------===//
125// Main RegionStore logic.
126//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000127
Zhongxing Xu17892752008-10-08 02:50:44 +0000128namespace {
129
Ted Kremenek59e8f112009-03-03 01:35:36 +0000130class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
131 typedef llvm::DenseMap<const MemRegion*,
132 llvm::ImmutableSet<const MemRegion*> > Map;
133
134 llvm::ImmutableSet<const MemRegion*>::Factory F;
135 Map M;
136
137public:
138 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
139 Map::iterator I = M.find(Parent);
140 M.insert(std::make_pair(Parent,
141 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
142 }
143
144 ~RegionStoreSubRegionMap() {}
145
Ted Kremenek5dc27462009-03-03 02:51:43 +0000146 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000147 Map::iterator I = M.find(Parent);
148
149 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000150 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000151
152 llvm::ImmutableSet<const MemRegion*> S = I->second;
153 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
154 SI != SE; ++SI) {
155 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000156 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000157 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000158
159 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000160 }
161};
162
Zhongxing Xu17892752008-10-08 02:50:44 +0000163class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000164 const RegionStoreFeatures Features;
Zhongxing Xu17892752008-10-08 02:50:44 +0000165 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000166 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000167
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000168 const MemRegion* SelfRegion;
169 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000170
171public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000172 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000173 : StoreManager(mgr, true),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000174 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000175 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000176 RVFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000177 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000178 if (const ObjCMethodDecl* MD =
179 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
180 SelfDecl = MD->getSelfDecl();
181 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000182
183 virtual ~RegionStoreManager() {}
184
Ted Kremenek14453bf2009-03-03 19:02:42 +0000185 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000186
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000187 /// getLValueString - Returns an SVal representing the lvalue of a
188 /// StringLiteral. Within RegionStore a StringLiteral has an
189 /// associated StringRegion, and the lvalue of a StringLiteral is
190 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000191 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000192
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000193 /// getLValueCompoundLiteral - Returns an SVal representing the
194 /// lvalue of a compound literal. Within RegionStore a compound
195 /// literal has an associated region, and the lvalue of the
196 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000197 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000198
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000199 /// getLValueVar - Returns an SVal that represents the lvalue of a
200 /// variable. Within RegionStore a variable has an associated
201 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000202 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000203
Ted Kremenek67f28532009-06-17 22:02:04 +0000204 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000205
Ted Kremenek67f28532009-06-17 22:02:04 +0000206 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000207
Ted Kremenek67f28532009-06-17 22:02:04 +0000208 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000209
Ted Kremenek67f28532009-06-17 22:02:04 +0000210 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000211 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000212
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000213
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000214 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
215 /// type. 'Array' represents the lvalue of the array being decayed
216 /// to a pointer, and the returned SVal represents the decayed
217 /// version of that lvalue (i.e., a pointer to the first element of
218 /// the array). This is called by GRExprEngine when evaluating
219 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000220 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000221
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000222 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000223 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000224
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000225 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000226
227 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
228 /// 'this' object (C++). When used when analyzing a normal function this
229 /// method returns NULL.
230 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000231 if (!SelfDecl)
232 return 0;
233
234 if (!SelfRegion) {
235 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
236 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
237 MRMgr.getHeapRegion());
238 }
239
240 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000241 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000242
243 //===-------------------------------------------------------------------===//
244 // Binding values to regions.
245 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000246
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000247 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
248 const Expr *E, unsigned Count);
249
Ted Kremenek67f28532009-06-17 22:02:04 +0000250 const GRState *Bind(const GRState *state, Loc LV, SVal V);
251
252 const GRState *BindCompoundLiteral(const GRState *state,
253 const CompoundLiteralExpr* CL, SVal V);
254
255 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
256
257 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
258 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000259 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000260
Ted Kremenek67f28532009-06-17 22:02:04 +0000261 /// BindStruct - Bind a compound value to a structure.
262 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
263
264 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
265
266 /// KillStruct - Set the entire struct to unknown.
267 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
268
269 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
270
271 Store Remove(Store store, Loc LV);
272
273 //===------------------------------------------------------------------===//
274 // Loading values from regions.
275 //===------------------------------------------------------------------===//
276
277 /// The high level logic for this method is this:
278 /// Retrieve (L)
279 /// if L has binding
280 /// return L's binding
281 /// else if L is in killset
282 /// return unknown
283 /// else
284 /// if L is on stack or heap
285 /// return undefined
286 /// else
287 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000288 SValuator::CastResult Retrieve(const GRState *state, Loc L,
289 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000290
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000291 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000292
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000293 SVal RetrieveField(const GRState *state, const FieldRegion *R);
294
295 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000296
Ted Kremenek9031dd72009-07-21 00:12:07 +0000297 SVal RetrieveVar(const GRState *state, const VarRegion *R);
298
Ted Kremenek25c54572009-07-20 22:58:02 +0000299 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
300
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000301 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
302 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000303
Ted Kremenek67f28532009-06-17 22:02:04 +0000304 /// Retrieve the values in a struct and return a CompoundVal, used when doing
305 /// struct copy:
306 /// struct s x, y;
307 /// x = y;
308 /// y's value is retrieved by this method.
309 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
310
311 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
312
313 //===------------------------------------------------------------------===//
314 // State pruning.
315 //===------------------------------------------------------------------===//
316
317 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
318 /// It returns a new Store with these values removed.
319 Store RemoveDeadBindings(const GRState *state, Stmt* Loc, SymbolReaper& SymReaper,
320 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
321
322 //===------------------------------------------------------------------===//
323 // Region "extents".
324 //===------------------------------------------------------------------===//
325
326 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
327 SVal getSizeInElements(const GRState *state, const MemRegion* R);
328
329 //===------------------------------------------------------------------===//
330 // Region "views".
331 //===------------------------------------------------------------------===//
332
333 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
334 const MemRegion* Base);
335
336 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
337 const MemRegion* Base);
338
339 //===------------------------------------------------------------------===//
340 // Utility methods.
341 //===------------------------------------------------------------------===//
342
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000343 const GRState *setCastType(const GRState *state, const MemRegion* R,
344 QualType T);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000345
Zhongxing Xu82037252009-07-14 01:12:46 +0000346 const QualType *getCastType(const GRState *state, const MemRegion *R) {
347 return state->get<RegionCasts>(R);
348 }
349
Zhongxing Xu17892752008-10-08 02:50:44 +0000350 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000351 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000352 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000353
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000354 void print(Store store, llvm::raw_ostream& Out, const char* nl,
355 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000356
357 void iterBindings(Store store, BindingsHandler& f) {
358 // FIXME: Implement.
359 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000360
Ted Kremenek67f28532009-06-17 22:02:04 +0000361 // FIXME: Remove.
362 BasicValueFactory& getBasicVals() {
363 return StateMgr.getBasicVals();
364 }
365
366 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000367 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000368};
369
370} // end anonymous namespace
371
Zhongxing Xu82037252009-07-14 01:12:46 +0000372static bool isGenericPtr(ASTContext &Ctx, QualType Ty) {
373 if (Ty->isObjCIdType() || Ty->isObjCQualifiedIdType())
374 return true;
375
376 while (true) {
377 Ty = Ctx.getCanonicalType(Ty);
378
379 if (Ty->isVoidType())
380 return true;
381
Ted Kremenek35366a62009-07-17 17:50:17 +0000382 if (const PointerType *PT = Ty->getAsPointerType()) {
Zhongxing Xu82037252009-07-14 01:12:46 +0000383 Ty = PT->getPointeeType();
384 continue;
385 }
386
387 break;
388 }
389
390 return false;
391}
392
Ted Kremenek9af46f52009-06-16 22:36:44 +0000393//===----------------------------------------------------------------------===//
394// RegionStore creation.
395//===----------------------------------------------------------------------===//
396
397StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
398 RegionStoreFeatures F = maximal_features_tag();
399 return new RegionStoreManager(StMgr, F);
400}
401
402StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
403 RegionStoreFeatures F = minimal_features_tag();
404 F.enableFields(true);
405 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000406}
407
Ted Kremenek14453bf2009-03-03 19:02:42 +0000408SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000409 RegionBindingsTy B = GetRegionBindings(state->getStore());
410 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
411
412 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
413 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
414 M->add(R->getSuperRegion(), R);
415 }
416
Ted Kremenek14453bf2009-03-03 19:02:42 +0000417 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000418}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000419
Ted Kremenek9af46f52009-06-16 22:36:44 +0000420//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000421// Binding invalidation.
422//===----------------------------------------------------------------------===//
423
424const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
425 const MemRegion *R,
426 const Expr *E,
427 unsigned Count) {
428 ASTContext& Ctx = StateMgr.getContext();
429
430 if (!R->isBoundable())
431 return state;
432
433 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)
434 || isa<ObjCObjectRegion>(R)) {
435 // Invalidate the alloca region by setting its default value to
436 // conjured symbol. The type of the symbol is irrelavant.
437 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
438 state = setDefaultValue(state, R, V);
439
440 // FIXME: This form of invalidation is a little bogus; we actually need
441 // to invalidate all subregions as well.
442 return state;
443 }
444
445 const TypedRegion *TR = cast<TypedRegion>(R);
446 QualType T = TR->getValueType(Ctx);
447
448 // FIXME: The code causes a crash when using RegionStore on the test case
449 // 'test_invalidate_cast_int' (misc-ps.m). Consider removing it
450 // permanently. Region casts are probably not too strict to handle
451 // the transient interpretation of memory. Instead we can use the QualType
452 // passed to 'Retrieve' and friends to determine the most current
453 // interpretation of memory when it is actually used.
454#if 0
455 // If the region is cast to another type, use that type.
456 if (const QualType *CastTy = getCastType(state, R)) {
457 assert(!(*CastTy)->isObjCObjectPointerType());
458 QualType NewT = (*CastTy)->getAsPointerType()->getPointeeType();
459
460 // The only exception is if the original region had a location type as its
461 // value type we always want to treat the region as binding to a location.
462 // This issue can arise when pointers are casted to integers and back.
463
464 if (!(Loc::IsLocType(T) && !Loc::IsLocType(NewT)))
465 T = NewT;
466 }
467#endif
468
469 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
470 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
471 return Bind(state, ValMgr.makeLoc(TR), V);
472 }
473 else if (const RecordType *RT = T->getAsStructureType()) {
474 // FIXME: handle structs with default region value.
475 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
476
477 // No record definition. There is nothing we can do.
478 if (!RD)
479 return state;
480
481 // Iterate through the fields and construct new symbols.
482 for (RecordDecl::field_iterator FI=RD->field_begin(),
483 FE=RD->field_end(); FI!=FE; ++FI) {
484
485 // For now just handle scalar fields.
486 FieldDecl *FD = *FI;
487 QualType FT = FD->getType();
488 const FieldRegion* FR = MRMgr.getFieldRegion(FD, TR);
489
490 if (Loc::IsLocType(FT) ||
491 (FT->isIntegerType() && FT->isScalarType())) {
492 SVal V = ValMgr.getConjuredSymbolVal(E, FT, Count);
493 state = state->bindLoc(ValMgr.makeLoc(FR), V);
494 }
495 else if (FT->isStructureType()) {
496 // set the default value of the struct field to conjured
497 // symbol. Note that the type of the symbol is irrelavant.
498 // We cannot use the type of the struct otherwise ValMgr won't
499 // give us the conjured symbol.
500 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
501 state = setDefaultValue(state, FR, V);
502 }
503 }
504 } else if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
505 // Set the default value of the array to conjured symbol.
506 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
507 Count);
508 state = setDefaultValue(state, TR, V);
509 } else {
510 // Just blast away other values.
511 state = Bind(state, ValMgr.makeLoc(TR), UnknownVal());
512 }
513
514 return state;
515}
516
517//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000518// getLValueXXX methods.
519//===----------------------------------------------------------------------===//
520
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000521/// getLValueString - Returns an SVal representing the lvalue of a
522/// StringLiteral. Within RegionStore a StringLiteral has an
523/// associated StringRegion, and the lvalue of a StringLiteral is the
524/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000525SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000526 const StringLiteral* S) {
527 return loc::MemRegionVal(MRMgr.getStringRegion(S));
528}
529
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000530/// getLValueVar - Returns an SVal that represents the lvalue of a
531/// variable. Within RegionStore a variable has an associated
532/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000533SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000534 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
535}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000536
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000537/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
538/// of a compound literal. Within RegionStore a compound literal
539/// has an associated region, and the lvalue of the compound literal
540/// is the lvalue of that region.
541SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000542RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000543 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000544 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
545}
546
Ted Kremenek67f28532009-06-17 22:02:04 +0000547SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000548 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000549 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000550}
551
Ted Kremenek67f28532009-06-17 22:02:04 +0000552SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000553 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000554 return getLValueFieldOrIvar(St, Base, D);
555}
556
Ted Kremenek67f28532009-06-17 22:02:04 +0000557SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000558 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000559 if (Base.isUnknownOrUndef())
560 return Base;
561
562 Loc BaseL = cast<Loc>(Base);
563 const MemRegion* BaseR = 0;
564
565 switch (BaseL.getSubKind()) {
566 case loc::MemRegionKind:
567 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
568 break;
569
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000570 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000571 // These are anormal cases. Flag an undefined value.
572 return UndefinedVal();
573
574 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000575 // While these seem funny, this can happen through casts.
576 // FIXME: What we should return is the field offset. For example,
577 // add the field offset to the integer value. That way funny things
578 // like this work properly: &(((struct foo *) 0xa)->f)
579 return Base;
580
581 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000582 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000583 return Base;
584 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000585
586 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
587 // of FieldDecl.
588 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
589 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000590
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000591 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000592}
593
Ted Kremenek67f28532009-06-17 22:02:04 +0000594SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000595 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000596 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000597
Ted Kremenekde7ec632009-03-09 22:44:49 +0000598 // If the base is an unknown or undefined value, just return it back.
599 // FIXME: For absolute pointer addresses, we just return that value back as
600 // well, although in reality we should return the offset added to that
601 // value.
602 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000603 return Base;
604
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000605 // Only handle integer offsets... for now.
606 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000607 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000608
Zhongxing Xuce760782009-05-09 13:20:07 +0000609 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000610
611 // Pointer of any type can be cast and used as array base.
612 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
613
Ted Kremenek46537392009-07-16 01:33:37 +0000614 // Convert the offset to the appropriate size and signedness.
615 Offset = ValMgr.convertToArrayIndex(Offset);
616
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000617 if (!ElemR) {
618 //
619 // If the base region is not an ElementRegion, create one.
620 // This can happen in the following example:
621 //
622 // char *p = __builtin_alloc(10);
623 // p[1] = 8;
624 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000625 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000626 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000627 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000628 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000629 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000630
631 SVal BaseIdx = ElemR->getIndex();
632
633 if (!isa<nonloc::ConcreteInt>(BaseIdx))
634 return UnknownVal();
635
636 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
637 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
638 assert(BaseIdxI.isSigned());
639
Ted Kremenek46537392009-07-16 01:33:37 +0000640 // Compute the new index.
641 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000642
Ted Kremenek46537392009-07-16 01:33:37 +0000643 // Construct the new ElementRegion.
644 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000645 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
646 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000647}
648
Ted Kremenek9af46f52009-06-16 22:36:44 +0000649//===----------------------------------------------------------------------===//
650// Extents for regions.
651//===----------------------------------------------------------------------===//
652
Ted Kremenek67f28532009-06-17 22:02:04 +0000653SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000654 const MemRegion *R) {
655
656 switch (R->getKind()) {
657 case MemRegion::MemSpaceRegionKind:
658 assert(0 && "Cannot index into a MemSpace");
659 return UnknownVal();
660
661 case MemRegion::CodeTextRegionKind:
662 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000663 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000664
665 // Not yet handled.
666 case MemRegion::AllocaRegionKind:
667 case MemRegion::CompoundLiteralRegionKind:
668 case MemRegion::ElementRegionKind:
669 case MemRegion::FieldRegionKind:
670 case MemRegion::ObjCIvarRegionKind:
671 case MemRegion::ObjCObjectRegionKind:
672 case MemRegion::SymbolicRegionKind:
673 return UnknownVal();
674
675 case MemRegion::StringRegionKind: {
676 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
677 // We intentionally made the size value signed because it participates in
678 // operations with signed indices.
679 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000680 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000681
682 // TypedViewRegion will soon be removed.
683 case MemRegion::TypedViewRegionKind:
684 return UnknownVal();
Zhongxing Xu20794942009-05-06 08:33:50 +0000685
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000686 case MemRegion::VarRegionKind: {
687 const VarRegion* VR = cast<VarRegion>(R);
688 // Get the type of the variable.
689 QualType T = VR->getDesugaredValueType(getContext());
690
691 // FIXME: Handle variable-length arrays.
692 if (isa<VariableArrayType>(T))
693 return UnknownVal();
694
695 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
696 // return the size as signed integer.
697 return ValMgr.makeIntVal(CAT->getSize(), false);
698 }
699
700 const QualType* CastTy = state->get<RegionCasts>(VR);
701
702 // If the VarRegion is cast to other type, compute the size with respect to
703 // that type.
704 if (CastTy) {
705 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
706 QualType VarTy = VR->getValueType(getContext());
707 uint64_t EleSize = getContext().getTypeSize(EleTy);
708 uint64_t VarSize = getContext().getTypeSize(VarTy);
709 assert(VarSize != 0);
710 return ValMgr.makeIntVal(VarSize/EleSize, false);
711 }
712
713 // Clients can use ordinary variables as if they were arrays. These
714 // essentially are arrays of size 1.
715 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000716 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000717
718 case MemRegion::BEG_DECL_REGIONS:
719 case MemRegion::END_DECL_REGIONS:
720 case MemRegion::BEG_TYPED_REGIONS:
721 case MemRegion::END_TYPED_REGIONS:
722 assert(0 && "Infeasible region");
723 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000724 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000725
726 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000727 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000728}
729
Ted Kremenek67f28532009-06-17 22:02:04 +0000730const GRState *RegionStoreManager::setExtent(const GRState *state,
731 const MemRegion *region,
732 SVal extent) {
733 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000734}
735
736//===----------------------------------------------------------------------===//
737// Location and region casting.
738//===----------------------------------------------------------------------===//
739
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000740/// ArrayToPointer - Emulates the "decay" of an array to a pointer
741/// type. 'Array' represents the lvalue of the array being decayed
742/// to a pointer, and the returned SVal represents the decayed
743/// version of that lvalue (i.e., a pointer to the first element of
744/// the array). This is called by GRExprEngine when evaluating casts
745/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000746SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000747 if (!isa<loc::MemRegionVal>(Array))
748 return UnknownVal();
749
750 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
751 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
752
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000753 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000754 return UnknownVal();
755
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000756 // Strip off typedefs from the ArrayRegion's ValueType.
757 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000758 ArrayType *AT = cast<ArrayType>(T);
759 T = AT->getElementType();
760
Ted Kremenek75185b52009-07-16 00:00:11 +0000761 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
762 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000763
764 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000765}
766
Ted Kremenek9af46f52009-06-16 22:36:44 +0000767//===----------------------------------------------------------------------===//
768// Pointer arithmetic.
769//===----------------------------------------------------------------------===//
770
Zhongxing Xu262fd032009-05-20 09:00:16 +0000771SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000772 BinaryOperator::Opcode Op, Loc L, NonLoc R,
773 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000774 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000775 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000776 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000777
Zhongxing Xua1718c72009-04-03 07:33:13 +0000778 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000779 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000780
Ted Kremenek3bccf082009-07-11 00:58:27 +0000781 switch (MR->getKind()) {
782 case MemRegion::SymbolicRegionKind: {
783 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
784 QualType T;
Ted Kremenek356e9d62009-07-22 04:35:42 +0000785#if USE_REGION_CASTS
Ted Kremenek3bccf082009-07-11 00:58:27 +0000786 // If the SymbolicRegion was cast to another type, use that type.
787 if (const QualType *t = state->get<RegionCasts>(SR))
788 T = *t;
Ted Kremenek356e9d62009-07-22 04:35:42 +0000789 else
790#endif
791 {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000792 // Otherwise use the symbol's type.
793 SymbolRef Sym = SR->getSymbol();
794 T = Sym->getType(getContext());
795 }
796
Ted Kremenek35366a62009-07-17 17:50:17 +0000797 QualType EleTy = T->getAsPointerType()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000798 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
799 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
800 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000801 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000802 case MemRegion::AllocaRegionKind: {
803 // Get the alloca region's current cast type.
804 const AllocaRegion *AR = cast<AllocaRegion>(MR);
805 QualType T = *(state->get<RegionCasts>(AR));
Ted Kremenek35366a62009-07-17 17:50:17 +0000806 QualType EleTy = T->getAsPointerType()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000807 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
808 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
809 break;
810 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000811
Ted Kremenek3bccf082009-07-11 00:58:27 +0000812 case MemRegion::ElementRegionKind: {
813 ER = cast<ElementRegion>(MR);
814 break;
815 }
816
817 // Not yet handled.
818 case MemRegion::VarRegionKind:
819 case MemRegion::StringRegionKind:
820 case MemRegion::CompoundLiteralRegionKind:
821 case MemRegion::FieldRegionKind:
822 case MemRegion::ObjCObjectRegionKind:
823 case MemRegion::ObjCIvarRegionKind:
824 return UnknownVal();
825
826 // TypedViewRegion will soon be removed.
827 case MemRegion::TypedViewRegionKind:
828 return UnknownVal();
829
830 case MemRegion::CodeTextRegionKind:
831 // Technically this can happen if people do funny things with casts.
832 return UnknownVal();
833
834 case MemRegion::MemSpaceRegionKind:
835 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
836 return UnknownVal();
837
838 case MemRegion::BEG_DECL_REGIONS:
839 case MemRegion::END_DECL_REGIONS:
840 case MemRegion::BEG_TYPED_REGIONS:
841 case MemRegion::END_TYPED_REGIONS:
842 assert(0 && "Infeasible region");
843 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000844 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000845
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000846 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000847 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
848 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
849
850 // Only support concrete integer indexes for now.
851 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000852 // FIXME: Should use SValuator here.
853 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
854 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000855 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000856 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
857 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000858 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000859 }
860
861 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000862}
863
Ted Kremenek9af46f52009-06-16 22:36:44 +0000864//===----------------------------------------------------------------------===//
865// Loading values from regions.
866//===----------------------------------------------------------------------===//
867
Ted Kremeneka6275a52009-07-15 02:31:43 +0000868static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
869 RTy = Ctx.getCanonicalType(RTy);
870 UsedTy = Ctx.getCanonicalType(UsedTy);
871
872 if (RTy == UsedTy)
873 return false;
874
Ted Kremenek25c54572009-07-20 22:58:02 +0000875
876 // Recursively check the types. We basically want to see if a pointer value
877 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
878 // represents a reinterpretation.
879 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
880 const PointerType *PRTy = RTy->getAsPointerType();
881 const PointerType *PUsedTy = UsedTy->getAsPointerType();
882
883 return PUsedTy && PRTy &&
884 IsReinterpreted(PRTy->getPointeeType(),
885 PUsedTy->getPointeeType(), Ctx);
886 }
887
888 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000889}
890
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000891SValuator::CastResult
892RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000893
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000894 assert(!isa<UnknownVal>(L) && "location unknown");
895 assert(!isa<UndefinedVal>(L) && "location undefined");
896
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000897 // FIXME: Is this even possible? Shouldn't this be treated as a null
898 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000899 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000900 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000901
Ted Kremenek67f28532009-06-17 22:02:04 +0000902 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000903
Zhongxing Xu91844122009-05-20 09:18:48 +0000904 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000905 // Example:
906 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000907 // char* p = alloca();
908 // read(p);
909 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000910 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000911 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000912
913 if (isa<SymbolicRegion>(MR)) {
914 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000915 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000916 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000917 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
918 }
919
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000920 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
921 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000922 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000923 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000924
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000925 // FIXME: We should eventually handle funny addressing. e.g.:
926 //
927 // int x = ...;
928 // int *p = &x;
929 // char *q = (char*) p;
930 // char c = *q; // returns the first byte of 'x'.
931 //
932 // Such funny addressing will occur due to layering of regions.
933
Ted Kremeneka6275a52009-07-15 02:31:43 +0000934 ASTContext &Ctx = getContext();
935 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000936 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
937 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000938 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000939 assert(Ctx.getCanonicalType(RTy) ==
940 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000941 }
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000942
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000943 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000944 return SValuator::CastResult(state, RetrieveStruct(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000945
946 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000947 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000948
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000949 // FIXME: handle Vector types.
950 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000951 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000952
953 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000954 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000955
956 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000957 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000958
Ted Kremenek25c54572009-07-20 22:58:02 +0000959 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000960 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000961
962 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000963 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000964
Ted Kremenek67f28532009-06-17 22:02:04 +0000965 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000966 RegionBindingsTy::data_type* V = B.lookup(R);
967
968 // Check if the region has a binding.
969 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000970 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000971
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000972 // The location does not have a bound value. This means that it has
973 // the value it had upon its creation and/or entry to the analyzed
974 // function/method. These are either symbolic values or 'undefined'.
975
Ted Kremenek356e9d62009-07-22 04:35:42 +0000976#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000977 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000978#else
979 if (R->hasStackStorage()) {
980#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000981 // All stack variables are considered to have undefined values
982 // upon creation. All heap allocated blocks are considered to
983 // have undefined values as well unless they are explicitly bound
984 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000985 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000986 }
987
Ted Kremenek356e9d62009-07-22 04:35:42 +0000988#if USE_REGION_CASTS
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000989 // If the region is already cast to another type, use that type to create the
990 // symbol value.
991 if (const QualType *p = state->get<RegionCasts>(R)) {
992 QualType T = *p;
Ted Kremenek35366a62009-07-17 17:50:17 +0000993 RTy = T->getAsPointerType()->getPointeeType();
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000994 }
Ted Kremenek356e9d62009-07-22 04:35:42 +0000995#endif
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000996
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000997 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000998 return SValuator::CastResult(state,
999 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001000}
1001
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001002SVal RegionStoreManager::RetrieveElement(const GRState* state,
1003 const ElementRegion* R) {
1004 // Check if the region has a binding.
1005 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001006 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001007 return *V;
1008
Ted Kremenek921109a2009-07-01 23:19:52 +00001009 const MemRegion* superR = R->getSuperRegion();
1010
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001011 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001012 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001013 const StringLiteral *Str = StrR->getStringLiteral();
1014 SVal Idx = R->getIndex();
1015 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1016 int64_t i = CI->getValue().getSExtValue();
1017 char c;
1018 if (i == Str->getByteLength())
1019 c = '\0';
1020 else
1021 c = Str->getStrData()[i];
1022 return ValMgr.makeIntVal(c, getContext().CharTy);
1023 }
1024 }
1025
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001026 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +00001027 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001028 if (D->hasConjuredSymbol())
1029 return ValMgr.getRegionValueSymbolVal(R);
1030 else
1031 return *D;
1032 }
1033
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001034 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001035 if (const SVal *V = B.lookup(superR)) {
1036 if (SymbolRef parentSym = V->getAsSymbol())
1037 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001038
1039 if (V->isUnknownOrUndef())
1040 return *V;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001041
1042 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001043 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001044 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001045
Ted Kremenek356e9d62009-07-22 04:35:42 +00001046#if 0
Ted Kremenek921109a2009-07-01 23:19:52 +00001047 if (R->hasHeapStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001048 // FIXME: If the region has heap storage and we know nothing special
1049 // about its bindings, should we instead return UnknownVal? Seems like
1050 // we should only return UndefinedVal in the cases where we know the value
1051 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001052 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +00001053 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001054#endif
1055
Ted Kremenekdc147262009-07-02 22:02:15 +00001056 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +00001057 // Currently we don't reason specially about Clang-style vectors. Check
1058 // if superR is a vector and if so return Unknown.
1059 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1060 if (typedSuperR->getValueType(getContext())->isVectorType())
1061 return UnknownVal();
1062 }
1063
1064 return UndefinedVal();
1065 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001066
1067 QualType Ty = R->getValueType(getContext());
1068
Ted Kremenek356e9d62009-07-22 04:35:42 +00001069#if USE_REGION_CASTS
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001070 // If the region is already cast to another type, use that type to create the
1071 // symbol value.
1072 if (const QualType *p = state->get<RegionCasts>(R))
Ted Kremenek35366a62009-07-17 17:50:17 +00001073 Ty = (*p)->getAsPointerType()->getPointeeType();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001074#endif
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001075
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001076 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001077}
1078
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001079SVal RegionStoreManager::RetrieveField(const GRState* state,
1080 const FieldRegion* R) {
1081 QualType Ty = R->getValueType(getContext());
1082
1083 // Check if the region has a binding.
1084 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001085 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001086 return *V;
1087
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001088 const MemRegion* superR = R->getSuperRegion();
1089 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001090 if (D->hasConjuredSymbol())
1091 return ValMgr.getRegionValueSymbolVal(R);
1092
1093 if (D->isZeroConstant())
1094 return ValMgr.makeZeroVal(Ty);
1095
1096 if (D->isUnknown())
1097 return *D;
1098
1099 assert(0 && "Unknown default value");
1100 }
1101
Ted Kremenek356e9d62009-07-22 04:35:42 +00001102#if HEAP_UNDEFINED
Ted Kremenekdc147262009-07-02 22:02:15 +00001103 // FIXME: Is this correct? Should it be UnknownVal?
1104 if (R->hasHeapStorage())
1105 return UndefinedVal();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001106#endif
Ted Kremenekdc147262009-07-02 22:02:15 +00001107
1108 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001109 return UndefinedVal();
1110
Ted Kremenek356e9d62009-07-22 04:35:42 +00001111#if USE_REGION_CASTS
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001112 // If the region is already cast to another type, use that type to create the
1113 // symbol value.
1114 if (const QualType *p = state->get<RegionCasts>(R)) {
1115 QualType tmp = *p;
Ted Kremenek35366a62009-07-17 17:50:17 +00001116 Ty = tmp->getAsPointerType()->getPointeeType();
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001117 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001118#endif
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001119
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001120 // All other values are symbolic.
1121 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001122}
1123
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001124SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1125 const ObjCIvarRegion* R) {
1126
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001127 // Check if the region has a binding.
1128 RegionBindingsTy B = GetRegionBindings(state->getStore());
1129
1130 if (const SVal* V = B.lookup(R))
1131 return *V;
1132
1133 const MemRegion *superR = R->getSuperRegion();
1134
1135 // Check if the super region has a binding.
1136 if (const SVal *V = B.lookup(superR)) {
1137 if (SymbolRef parentSym = V->getAsSymbol())
1138 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1139
1140 // Other cases: give up.
1141 return UnknownVal();
1142 }
1143
Ted Kremenek25c54572009-07-20 22:58:02 +00001144 return RetrieveLazySymbol(state, R);
1145}
1146
Ted Kremenek9031dd72009-07-21 00:12:07 +00001147SVal RegionStoreManager::RetrieveVar(const GRState *state,
1148 const VarRegion *R) {
1149
1150 // Check if the region has a binding.
1151 RegionBindingsTy B = GetRegionBindings(state->getStore());
1152
1153 if (const SVal* V = B.lookup(R))
1154 return *V;
1155
1156 // Lazily derive a value for the VarRegion.
1157 const VarDecl *VD = R->getDecl();
1158
1159 if (VD == SelfDecl)
1160 return loc::MemRegionVal(getSelfRegion(0));
1161
1162 if (R->hasGlobalsOrParametersStorage())
1163 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1164
1165 return UndefinedVal();
1166}
1167
Ted Kremenek25c54572009-07-20 22:58:02 +00001168SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1169 const TypedRegion *R) {
1170
1171 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001172
1173#if USE_REGION_CASTS
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001174 // If the region is already cast to another type, use that type to create the
1175 // symbol value.
Ted Kremenek25c54572009-07-20 22:58:02 +00001176 if (const QualType *ty = state->get<RegionCasts>(R)) {
1177 if (const PointerType *PT = (*ty)->getAsPointerType()) {
1178 QualType castTy = PT->getPointeeType();
1179
1180 if (!IsReinterpreted(valTy, castTy, getContext()))
1181 valTy = castTy;
1182 }
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001183 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001184#endif
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001185
1186 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001187 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001188}
1189
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001190SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1191 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001192 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001193 assert(T->isStructureType());
1194
Zhongxing Xub7507d12009-06-11 07:27:30 +00001195 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001196 RecordDecl* RD = RT->getDecl();
1197 assert(RD->isDefinition());
1198
1199 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1200
Ted Kremenek67f28532009-06-17 22:02:04 +00001201 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1202 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001203 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001204
Douglas Gregore267ff32008-12-11 20:41:00 +00001205 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1206 FieldEnd = Fields.rend();
1207 Field != FieldEnd; ++Field) {
1208 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001209 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001210 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001211 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1212 }
1213
Zhongxing Xud91ee272009-06-23 09:02:15 +00001214 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001215}
1216
Ted Kremenek67f28532009-06-17 22:02:04 +00001217SVal RegionStoreManager::RetrieveArray(const GRState *state,
1218 const TypedRegion * R) {
1219
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001220 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001221 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1222
1223 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001224 uint64_t size = CAT->getSize().getZExtValue();
1225 for (uint64_t i = 0; i < size; ++i) {
1226 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001227 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1228 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001229 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001230 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001231 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1232 }
1233
Zhongxing Xud91ee272009-06-23 09:02:15 +00001234 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001235}
1236
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001237SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1238 const GRState *state,
1239 const TypedRegion *R,
1240 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001241 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001242 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001243
1244 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001245 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001246}
1247
Ted Kremenek9af46f52009-06-16 22:36:44 +00001248//===----------------------------------------------------------------------===//
1249// Binding values to regions.
1250//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001251
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001252Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001253 const MemRegion* R = 0;
1254
1255 if (isa<loc::MemRegionVal>(L))
1256 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001257
1258 if (R) {
1259 RegionBindingsTy B = GetRegionBindings(store);
1260 return RBFactory.Remove(B, R).getRoot();
1261 }
1262
1263 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001264}
1265
Ted Kremenek67f28532009-06-17 22:02:04 +00001266const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001267 if (isa<loc::ConcreteInt>(L))
1268 return state;
1269
Ted Kremenek9af46f52009-06-16 22:36:44 +00001270 // If we get here, the location should be a region.
1271 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001272
1273 // Check if the region is a struct region.
1274 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1275 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001276 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001277
Ted Kremenek67f28532009-06-17 22:02:04 +00001278 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001279
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001280 B = RBFactory.Add(B, R, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001281
Ted Kremenek67f28532009-06-17 22:02:04 +00001282 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001283}
1284
Ted Kremenek67f28532009-06-17 22:02:04 +00001285const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001286 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001287
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001288 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001289 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001290
Ted Kremenek0964a062009-01-21 06:57:53 +00001291 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001292 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001293 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001294 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001295
Zhongxing Xud91ee272009-06-23 09:02:15 +00001296 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001297}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001298
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001299// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001300const GRState *
1301RegionStoreManager::BindCompoundLiteral(const GRState *state,
1302 const CompoundLiteralExpr* CL,
1303 SVal V) {
1304
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001305 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001306 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001307}
1308
Ted Kremenek67f28532009-06-17 22:02:04 +00001309const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001310 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001311 SVal Init) {
1312
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001313 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001314 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001315 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001316
Ted Kremenek46537392009-07-16 01:33:37 +00001317 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001318
1319 // Check if the init expr is a StringLiteral.
1320 if (isa<loc::MemRegionVal>(Init)) {
1321 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1322 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1323 const char* str = S->getStrData();
1324 unsigned len = S->getByteLength();
1325 unsigned j = 0;
1326
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001327 // Copy bytes from the string literal into the target array. Trailing bytes
1328 // in the array that are not covered by the string literal are initialized
1329 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001330 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001331 if (j >= len)
1332 break;
1333
Ted Kremenek46537392009-07-16 01:33:37 +00001334 SVal Idx = ValMgr.makeArrayIndex(i);
1335 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1336 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001337
Zhongxing Xud91ee272009-06-23 09:02:15 +00001338 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001339 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001340 }
1341
Ted Kremenek67f28532009-06-17 22:02:04 +00001342 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001343 }
1344
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001345 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001346 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001347 uint64_t i = 0;
1348
1349 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001350 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001351 if (VI == VE)
1352 break;
1353
Ted Kremenek46537392009-07-16 01:33:37 +00001354 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001355 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001356
1357 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001358 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001359 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001360 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001361 }
1362
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001363 // If the init list is shorter than the array length, set the array default
1364 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001365 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001366 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001367 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001368 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001369 }
1370 }
1371
Ted Kremenek67f28532009-06-17 22:02:04 +00001372 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001373}
1374
Ted Kremenek67f28532009-06-17 22:02:04 +00001375const GRState *
1376RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1377 SVal V) {
1378
1379 if (!Features.supportsFields())
1380 return state;
1381
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001382 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001383 assert(T->isStructureType());
1384
Ted Kremenek35366a62009-07-17 17:50:17 +00001385 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001386 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001387
1388 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001389 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001390
Ted Kremenek67f28532009-06-17 22:02:04 +00001391 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1392 // Ignore them and kill the field values.
1393 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1394 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001395
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001396 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001397 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001398
1399 RecordDecl::field_iterator FI, FE;
1400
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001401 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001402
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001403 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001404 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001405
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001406 QualType FTy = (*FI)->getType();
1407 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1408
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001409 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001410 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001411 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001412 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001413 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001414 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001415 }
1416
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001417 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001418 if (FI != FE)
1419 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001420
Ted Kremenek67f28532009-06-17 22:02:04 +00001421 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001422}
1423
Ted Kremenek67f28532009-06-17 22:02:04 +00001424const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001425 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001426
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001427 // Set the default value of the struct region to "unknown".
1428 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001429
1430 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001431 Store store = state->getStore();
1432 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001433 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001434 const MemRegion* R = I.getKey();
1435 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1436 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001437 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001438 }
1439
Ted Kremenek67f28532009-06-17 22:02:04 +00001440 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001441}
1442
Ted Kremenek9af46f52009-06-16 22:36:44 +00001443//===----------------------------------------------------------------------===//
1444// Region views.
1445//===----------------------------------------------------------------------===//
1446
Ted Kremenek67f28532009-06-17 22:02:04 +00001447const GRState *RegionStoreManager::AddRegionView(const GRState *state,
1448 const MemRegion* View,
1449 const MemRegion* Base) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001450
1451 // First, retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001452 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001453 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001454
1455 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001456 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001457
1458 // Create a new state with the new region view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001459 return state->set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001460}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001461
Ted Kremenek67f28532009-06-17 22:02:04 +00001462const GRState *RegionStoreManager::RemoveRegionView(const GRState *state,
1463 const MemRegion* View,
1464 const MemRegion* Base) {
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001465 // Retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001466 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001467
1468 // If the base region has no view, return.
1469 if (!d)
Ted Kremenek67f28532009-06-17 22:02:04 +00001470 return state;
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001471
1472 // Remove the view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001473 return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001474}
Zhongxing Xu20794942009-05-06 08:33:50 +00001475
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001476const GRState *RegionStoreManager::setCastType(const GRState *state,
1477 const MemRegion* R, QualType T) {
Zhongxing Xu82037252009-07-14 01:12:46 +00001478 // We do not record generic cast type, since we are using cast type to
1479 // invlidate regions, and generic type is meaningless for invalidating
1480 // regions.
1481 // If the region already has a cast type before, that type is preserved.
1482 // FIXME: is this the right thing to do?
1483 if (isGenericPtr(getContext(), T))
1484 return state;
Ted Kremenek67f28532009-06-17 22:02:04 +00001485 return state->set<RegionCasts>(R, T);
Zhongxing Xu20794942009-05-06 08:33:50 +00001486}
Zhongxing Xu264e9372009-05-12 10:10:00 +00001487
Ted Kremenek67f28532009-06-17 22:02:04 +00001488const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1489 const MemRegion* R, SVal V) {
1490 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001491}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001492
1493//===----------------------------------------------------------------------===//
1494// State pruning.
1495//===----------------------------------------------------------------------===//
1496
1497static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1498 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1499 const MemRegion *R = XR->getRegion();
1500
1501 while (R) {
1502 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1503 SymReaper.markLive(SR->getSymbol());
1504 return;
1505 }
1506
1507 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1508 R = SR->getSuperRegion();
1509 continue;
1510 }
1511
1512 break;
1513 }
1514
1515 return;
1516 }
1517
1518 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1519 SymReaper.markLive(*SI);
1520}
1521
Ted Kremenek67f28532009-06-17 22:02:04 +00001522Store RegionStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001523 SymbolReaper& SymReaper,
1524 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001525{
Ted Kremenek9af46f52009-06-16 22:36:44 +00001526 Store store = state->getStore();
1527 RegionBindingsTy B = GetRegionBindings(store);
1528
1529 // Lazily constructed backmap from MemRegions to SubRegions.
1530 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1531 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1532
1533 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
1534 // the ability to reuse memory. This way we can keep TmpAlloc around as
1535 // an instance variable of RegionStoreManager (avoiding repeated malloc
1536 // overhead).
1537 llvm::BumpPtrAllocator TmpAlloc;
1538
1539 // Factory objects.
1540 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1541 SubRegionsTy::Factory SubRegF(TmpAlloc);
1542
1543 // The backmap from regions to subregions.
1544 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1545
1546 // Do a pass over the regions in the store. For VarRegions we check if
1547 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001548 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001549 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1550
1551 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1552 IntermediateRoots.push_back(I.getKey());
1553 }
1554
1555 while (!IntermediateRoots.empty()) {
1556 const MemRegion* R = IntermediateRoots.back();
1557 IntermediateRoots.pop_back();
1558
1559 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001560 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001561 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001562 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001563 }
1564 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1565 if (SymReaper.isLive(SR->getSymbol()))
1566 RegionRoots.push_back(SR);
1567 }
1568 else {
1569 // Get the super region for R.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001570 const MemRegion* superR = cast<SubRegion>(R)->getSuperRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001571
1572 // Get the current set of subregions for SuperR.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001573 const SubRegionsTy* SRptr = SubRegMap.lookup(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001574 SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
1575
1576 // Add R to the subregions of SuperR.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001577 SubRegMap = SubRegMapF.Add(SubRegMap, superR, SubRegF.Add(SRs, R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001578
1579 // Super region may be VarRegion or subregion of another VarRegion. Add it
1580 // to the work list.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001581 if (isa<SubRegion>(superR))
1582 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001583 }
1584 }
1585
1586 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1587 // of the store. We want to find all live symbols and dead regions.
1588 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1589
1590 while (!RegionRoots.empty()) {
1591 // Dequeue the next region on the worklist.
1592 const MemRegion* R = RegionRoots.back();
1593 RegionRoots.pop_back();
1594
1595 // Check if we have already processed this region.
1596 if (Marked.count(R)) continue;
1597
1598 // Mark this region as processed. This is needed for termination in case
1599 // a region is referenced more than once.
1600 Marked.insert(R);
1601
1602 // Mark the symbol for any live SymbolicRegion as "live". This means we
1603 // should continue to track that symbol.
1604 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1605 SymReaper.markLive(SymR->getSymbol());
1606
1607 // Get the data binding for R (if any).
1608 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1609 if (Xptr) {
1610 SVal X = *Xptr;
1611 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1612
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001613 // If X is a region, then add it to the RegionRoots.
1614 if (const MemRegion *RX = X.getAsRegion()) {
1615 RegionRoots.push_back(RX);
1616
1617 // Mark the super region of the RX as live.
1618 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1619 // 'y' => element region. 'x' is its super region.
1620 // We only add one level super region for now.
Zhongxing Xu5ff8e8c2009-07-01 02:12:57 +00001621 // FIXME: maybe multiple level of super regions should be added.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001622 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1623 RegionRoots.push_back(SR->getSuperRegion());
1624 }
1625 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001626 }
1627
1628 // Get the subregions of R. These are RegionRoots as well since they
1629 // represent values that are also bound to R.
1630 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1631 if (!SRptr) continue;
1632 SubRegionsTy SR = *SRptr;
1633
1634 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1635 RegionRoots.push_back(*I);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001636
Ted Kremenek9af46f52009-06-16 22:36:44 +00001637 }
1638
1639 // We have now scanned the store, marking reachable regions and symbols
1640 // as live. We now remove all the regions that are dead from the store
1641 // as well as update DSymbols with the set symbols that are now dead.
1642 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1643 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001644 // If this region live? Is so, none of its symbols are dead.
1645 if (Marked.count(R))
1646 continue;
1647
1648 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001649 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001650
1651 // Mark all non-live symbols that this region references as dead.
1652 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1653 SymReaper.maybeDead(SymR->getSymbol());
1654
1655 SVal X = I.getData();
1656 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1657 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
1658 }
1659
1660 return store;
1661}
1662
1663//===----------------------------------------------------------------------===//
1664// Utility methods.
1665//===----------------------------------------------------------------------===//
1666
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001667void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001668 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001669 RegionBindingsTy B = GetRegionBindings(store);
1670 OS << "Store:" << nl;
1671
Ted Kremenek6f9b3a42009-07-13 23:53:06 +00001672 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
1673 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001674}