blob: 1dc5118d9e896ee175c54bed6d17679f606536ff [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
Zhongxing Xubaf03a72008-11-24 09:44:56 +000030// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000031typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000032
Ted Kremenek50dc1b32008-12-24 01:05:03 +000033//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000034// Fine-grained control of RegionStoreManager.
35//===----------------------------------------------------------------------===//
36
37namespace {
38struct VISIBILITY_HIDDEN minimal_features_tag {};
39struct VISIBILITY_HIDDEN maximal_features_tag {};
40
41class VISIBILITY_HIDDEN RegionStoreFeatures {
42 bool SupportsFields;
43 bool SupportsRemaining;
44
45public:
46 RegionStoreFeatures(minimal_features_tag) :
47 SupportsFields(false), SupportsRemaining(false) {}
48
49 RegionStoreFeatures(maximal_features_tag) :
50 SupportsFields(true), SupportsRemaining(false) {}
51
52 void enableFields(bool t) { SupportsFields = t; }
53
54 bool supportsFields() const { return SupportsFields; }
55 bool supportsRemaining() const { return SupportsRemaining; }
56};
57}
58
59//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000060// Region "Views"
61//===----------------------------------------------------------------------===//
62//
63// MemRegions can be layered on top of each other. This GDM entry tracks
64// what are the MemRegions that layer a given MemRegion.
65//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000066typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000067namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
68static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000069namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000070 template<> struct GRStateTrait<RegionViewMap>
71 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
72 RegionViews> > {
73
74 static void* GDMIndex() { return &RegionViewMapIndex; }
75 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000076}
Zhongxing Xu17892752008-10-08 02:50:44 +000077
Zhongxing Xu20794942009-05-06 08:33:50 +000078// RegionCasts records the current cast type of a region.
79namespace { class VISIBILITY_HIDDEN RegionCasts {}; }
80static int RegionCastsIndex = 0;
81namespace clang {
82 template<> struct GRStateTrait<RegionCasts>
83 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
84 QualType> > {
85 static void* GDMIndex() { return &RegionCastsIndex; }
86 };
87}
88
Ted Kremenek50dc1b32008-12-24 01:05:03 +000089//===----------------------------------------------------------------------===//
90// Region "Extents"
91//===----------------------------------------------------------------------===//
92//
93// MemRegions represent chunks of memory with a size (their "extent"). This
94// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000095//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000096namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
97static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000098namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000099 template<> struct GRStateTrait<RegionExtents>
100 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
101 static void* GDMIndex() { return &RegionExtentsIndex; }
102 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000103}
104
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000105//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000106// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000107//===----------------------------------------------------------------------===//
108//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000109// This GDM entry tracks what regions have a default value if they have no bound
110// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000111//
112namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
113static int RegionDefaultValueIndex = 0;
114namespace clang {
115 template<> struct GRStateTrait<RegionDefaultValue>
116 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
117 static void* GDMIndex() { return &RegionDefaultValueIndex; }
118 };
119}
120
121//===----------------------------------------------------------------------===//
122// Main RegionStore logic.
123//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000124
Zhongxing Xu17892752008-10-08 02:50:44 +0000125namespace {
126
Ted Kremenek59e8f112009-03-03 01:35:36 +0000127class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
128 typedef llvm::DenseMap<const MemRegion*,
129 llvm::ImmutableSet<const MemRegion*> > Map;
130
131 llvm::ImmutableSet<const MemRegion*>::Factory F;
132 Map M;
133
134public:
135 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
136 Map::iterator I = M.find(Parent);
137 M.insert(std::make_pair(Parent,
138 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
139 }
140
141 ~RegionStoreSubRegionMap() {}
142
Ted Kremenek5dc27462009-03-03 02:51:43 +0000143 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000144 Map::iterator I = M.find(Parent);
145
146 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000147 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148
149 llvm::ImmutableSet<const MemRegion*> S = I->second;
150 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
151 SI != SE; ++SI) {
152 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000153 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000154 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000155
156 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000157 }
158};
159
Zhongxing Xu17892752008-10-08 02:50:44 +0000160class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000161 const RegionStoreFeatures Features;
Zhongxing Xu17892752008-10-08 02:50:44 +0000162 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000163 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000164
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000165 const MemRegion* SelfRegion;
166 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000167
168public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000169 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000170 : StoreManager(mgr, true),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000171 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000172 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000173 RVFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000174 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000175 if (const ObjCMethodDecl* MD =
176 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
177 SelfDecl = MD->getSelfDecl();
178 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000179
180 virtual ~RegionStoreManager() {}
181
Ted Kremenek14453bf2009-03-03 19:02:42 +0000182 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000183
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000184 /// getLValueString - Returns an SVal representing the lvalue of a
185 /// StringLiteral. Within RegionStore a StringLiteral has an
186 /// associated StringRegion, and the lvalue of a StringLiteral is
187 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000188 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000189
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000190 /// getLValueCompoundLiteral - Returns an SVal representing the
191 /// lvalue of a compound literal. Within RegionStore a compound
192 /// literal has an associated region, and the lvalue of the
193 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000194 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000195
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000196 /// getLValueVar - Returns an SVal that represents the lvalue of a
197 /// variable. Within RegionStore a variable has an associated
198 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000199 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000200
Ted Kremenek67f28532009-06-17 22:02:04 +0000201 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000202
Ted Kremenek67f28532009-06-17 22:02:04 +0000203 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000204
Ted Kremenek67f28532009-06-17 22:02:04 +0000205 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000206
Ted Kremenek67f28532009-06-17 22:02:04 +0000207 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000208 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000209
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000210
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000211 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
212 /// type. 'Array' represents the lvalue of the array being decayed
213 /// to a pointer, and the returned SVal represents the decayed
214 /// version of that lvalue (i.e., a pointer to the first element of
215 /// the array). This is called by GRExprEngine when evaluating
216 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000217 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000218
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000219 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000220 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000221
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000222 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000223
224 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
225 /// 'this' object (C++). When used when analyzing a normal function this
226 /// method returns NULL.
227 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000228 if (!SelfDecl)
229 return 0;
230
231 if (!SelfRegion) {
232 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
233 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
234 MRMgr.getHeapRegion());
235 }
236
237 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000238 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000239
240 //===-------------------------------------------------------------------===//
241 // Binding values to regions.
242 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000243
Ted Kremenek67f28532009-06-17 22:02:04 +0000244 const GRState *Bind(const GRState *state, Loc LV, SVal V);
245
246 const GRState *BindCompoundLiteral(const GRState *state,
247 const CompoundLiteralExpr* CL, SVal V);
248
249 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
250
251 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
252 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000253 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000254
Ted Kremenek67f28532009-06-17 22:02:04 +0000255 /// BindStruct - Bind a compound value to a structure.
256 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
257
258 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
259
260 /// KillStruct - Set the entire struct to unknown.
261 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
262
263 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
264
265 Store Remove(Store store, Loc LV);
266
267 //===------------------------------------------------------------------===//
268 // Loading values from regions.
269 //===------------------------------------------------------------------===//
270
271 /// The high level logic for this method is this:
272 /// Retrieve (L)
273 /// if L has binding
274 /// return L's binding
275 /// else if L is in killset
276 /// return unknown
277 /// else
278 /// if L is on stack or heap
279 /// return undefined
280 /// else
281 /// return symbolic
282 SVal Retrieve(const GRState *state, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000283
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000284 SVal RetrieveElement(const GRState* state, const ElementRegion* R);
285
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000286 SVal RetrieveField(const GRState* state, const FieldRegion* R);
287
Ted Kremenek67f28532009-06-17 22:02:04 +0000288 /// Retrieve the values in a struct and return a CompoundVal, used when doing
289 /// struct copy:
290 /// struct s x, y;
291 /// x = y;
292 /// y's value is retrieved by this method.
293 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
294
295 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
296
297 //===------------------------------------------------------------------===//
298 // State pruning.
299 //===------------------------------------------------------------------===//
300
301 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
302 /// It returns a new Store with these values removed.
303 Store RemoveDeadBindings(const GRState *state, Stmt* Loc, SymbolReaper& SymReaper,
304 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
305
306 //===------------------------------------------------------------------===//
307 // Region "extents".
308 //===------------------------------------------------------------------===//
309
310 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
311 SVal getSizeInElements(const GRState *state, const MemRegion* R);
312
313 //===------------------------------------------------------------------===//
314 // Region "views".
315 //===------------------------------------------------------------------===//
316
317 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
318 const MemRegion* Base);
319
320 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
321 const MemRegion* Base);
322
323 //===------------------------------------------------------------------===//
324 // Utility methods.
325 //===------------------------------------------------------------------===//
326
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000327 const GRState *setCastType(const GRState *state, const MemRegion* R,
328 QualType T);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000329
Zhongxing Xu82037252009-07-14 01:12:46 +0000330 const QualType *getCastType(const GRState *state, const MemRegion *R) {
331 return state->get<RegionCasts>(R);
332 }
333
Zhongxing Xu17892752008-10-08 02:50:44 +0000334 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000335 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000336 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000337
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000338 void print(Store store, llvm::raw_ostream& Out, const char* nl,
339 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000340
341 void iterBindings(Store store, BindingsHandler& f) {
342 // FIXME: Implement.
343 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000344
Ted Kremenek67f28532009-06-17 22:02:04 +0000345 // FIXME: Remove.
346 BasicValueFactory& getBasicVals() {
347 return StateMgr.getBasicVals();
348 }
349
350 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000351 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000352};
353
354} // end anonymous namespace
355
Zhongxing Xu82037252009-07-14 01:12:46 +0000356static bool isGenericPtr(ASTContext &Ctx, QualType Ty) {
357 if (Ty->isObjCIdType() || Ty->isObjCQualifiedIdType())
358 return true;
359
360 while (true) {
361 Ty = Ctx.getCanonicalType(Ty);
362
363 if (Ty->isVoidType())
364 return true;
365
366 if (const PointerType *PT = Ty->getAsPointerType()) {
367 Ty = PT->getPointeeType();
368 continue;
369 }
370
371 break;
372 }
373
374 return false;
375}
376
Ted Kremenek9af46f52009-06-16 22:36:44 +0000377//===----------------------------------------------------------------------===//
378// RegionStore creation.
379//===----------------------------------------------------------------------===//
380
381StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
382 RegionStoreFeatures F = maximal_features_tag();
383 return new RegionStoreManager(StMgr, F);
384}
385
386StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
387 RegionStoreFeatures F = minimal_features_tag();
388 F.enableFields(true);
389 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000390}
391
Ted Kremenek14453bf2009-03-03 19:02:42 +0000392SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000393 RegionBindingsTy B = GetRegionBindings(state->getStore());
394 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
395
396 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
397 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
398 M->add(R->getSuperRegion(), R);
399 }
400
Ted Kremenek14453bf2009-03-03 19:02:42 +0000401 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000402}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000403
Ted Kremenek9af46f52009-06-16 22:36:44 +0000404//===----------------------------------------------------------------------===//
405// getLValueXXX methods.
406//===----------------------------------------------------------------------===//
407
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000408/// getLValueString - Returns an SVal representing the lvalue of a
409/// StringLiteral. Within RegionStore a StringLiteral has an
410/// associated StringRegion, and the lvalue of a StringLiteral is the
411/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000412SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000413 const StringLiteral* S) {
414 return loc::MemRegionVal(MRMgr.getStringRegion(S));
415}
416
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000417/// getLValueVar - Returns an SVal that represents the lvalue of a
418/// variable. Within RegionStore a variable has an associated
419/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000420SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000421 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
422}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000423
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000424/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
425/// of a compound literal. Within RegionStore a compound literal
426/// has an associated region, and the lvalue of the compound literal
427/// is the lvalue of that region.
428SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000429RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000430 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000431 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
432}
433
Ted Kremenek67f28532009-06-17 22:02:04 +0000434SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000435 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000436 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000437}
438
Ted Kremenek67f28532009-06-17 22:02:04 +0000439SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000440 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000441 return getLValueFieldOrIvar(St, Base, D);
442}
443
Ted Kremenek67f28532009-06-17 22:02:04 +0000444SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000445 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000446 if (Base.isUnknownOrUndef())
447 return Base;
448
449 Loc BaseL = cast<Loc>(Base);
450 const MemRegion* BaseR = 0;
451
452 switch (BaseL.getSubKind()) {
453 case loc::MemRegionKind:
454 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
455 break;
456
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000457 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000458 // These are anormal cases. Flag an undefined value.
459 return UndefinedVal();
460
461 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000462 // While these seem funny, this can happen through casts.
463 // FIXME: What we should return is the field offset. For example,
464 // add the field offset to the integer value. That way funny things
465 // like this work properly: &(((struct foo *) 0xa)->f)
466 return Base;
467
468 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000469 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000470 return Base;
471 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000472
473 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
474 // of FieldDecl.
475 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
476 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000477
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000478 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000479}
480
Ted Kremenek67f28532009-06-17 22:02:04 +0000481SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000482 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000483 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000484
Ted Kremenekde7ec632009-03-09 22:44:49 +0000485 // If the base is an unknown or undefined value, just return it back.
486 // FIXME: For absolute pointer addresses, we just return that value back as
487 // well, although in reality we should return the offset added to that
488 // value.
489 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000490 return Base;
491
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000492 // Only handle integer offsets... for now.
493 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000494 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000495
Zhongxing Xuce760782009-05-09 13:20:07 +0000496 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000497
498 // Pointer of any type can be cast and used as array base.
499 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
500
501 if (!ElemR) {
502 //
503 // If the base region is not an ElementRegion, create one.
504 // This can happen in the following example:
505 //
506 // char *p = __builtin_alloc(10);
507 // p[1] = 8;
508 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000509 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000510 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000511
512 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
513 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
514 const llvm::APSInt& OffI = CI->getValue();
515 if (OffI.isUnsigned()) {
516 llvm::APSInt Tmp = OffI;
517 Tmp.setIsSigned(true);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000518 Offset = ValMgr.makeIntVal(Tmp);
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000519 }
520 }
Ted Kremenekf936f452009-05-04 06:18:28 +0000521 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000522 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000523 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000524
525 SVal BaseIdx = ElemR->getIndex();
526
527 if (!isa<nonloc::ConcreteInt>(BaseIdx))
528 return UnknownVal();
529
530 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
531 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
532 assert(BaseIdxI.isSigned());
533
534 // FIXME: This appears to be the assumption of this code. We should review
535 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
536 // can't we need to put a comment here. If it can, we should handle it.
537 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000538
Zhongxing Xuce760782009-05-09 13:20:07 +0000539 const MemRegion *ArrayR = ElemR->getSuperRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000540 SVal NewIdx;
541
542 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
543 // 'Offset' might be unsigned. We have to convert it to signed and
544 // possibly extend it.
545 llvm::APSInt Tmp = OffI;
546
547 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
548 Tmp.extend(BaseIdxI.getBitWidth());
549
550 Tmp.setIsSigned(true);
551 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000552 NewIdx = ValMgr.makeIntVal(Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000553 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000554 else
555 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000556
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000557 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
558 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000559}
560
Ted Kremenek9af46f52009-06-16 22:36:44 +0000561//===----------------------------------------------------------------------===//
562// Extents for regions.
563//===----------------------------------------------------------------------===//
564
Ted Kremenek67f28532009-06-17 22:02:04 +0000565SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000566 const MemRegion *R) {
567
568 switch (R->getKind()) {
569 case MemRegion::MemSpaceRegionKind:
570 assert(0 && "Cannot index into a MemSpace");
571 return UnknownVal();
572
573 case MemRegion::CodeTextRegionKind:
574 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000575 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000576
577 // Not yet handled.
578 case MemRegion::AllocaRegionKind:
579 case MemRegion::CompoundLiteralRegionKind:
580 case MemRegion::ElementRegionKind:
581 case MemRegion::FieldRegionKind:
582 case MemRegion::ObjCIvarRegionKind:
583 case MemRegion::ObjCObjectRegionKind:
584 case MemRegion::SymbolicRegionKind:
585 return UnknownVal();
586
587 case MemRegion::StringRegionKind: {
588 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
589 // We intentionally made the size value signed because it participates in
590 // operations with signed indices.
591 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000592 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000593
594 // TypedViewRegion will soon be removed.
595 case MemRegion::TypedViewRegionKind:
596 return UnknownVal();
Zhongxing Xu20794942009-05-06 08:33:50 +0000597
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000598 case MemRegion::VarRegionKind: {
599 const VarRegion* VR = cast<VarRegion>(R);
600 // Get the type of the variable.
601 QualType T = VR->getDesugaredValueType(getContext());
602
603 // FIXME: Handle variable-length arrays.
604 if (isa<VariableArrayType>(T))
605 return UnknownVal();
606
607 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
608 // return the size as signed integer.
609 return ValMgr.makeIntVal(CAT->getSize(), false);
610 }
611
612 const QualType* CastTy = state->get<RegionCasts>(VR);
613
614 // If the VarRegion is cast to other type, compute the size with respect to
615 // that type.
616 if (CastTy) {
617 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
618 QualType VarTy = VR->getValueType(getContext());
619 uint64_t EleSize = getContext().getTypeSize(EleTy);
620 uint64_t VarSize = getContext().getTypeSize(VarTy);
621 assert(VarSize != 0);
622 return ValMgr.makeIntVal(VarSize/EleSize, false);
623 }
624
625 // Clients can use ordinary variables as if they were arrays. These
626 // essentially are arrays of size 1.
627 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000628 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000629
630 case MemRegion::BEG_DECL_REGIONS:
631 case MemRegion::END_DECL_REGIONS:
632 case MemRegion::BEG_TYPED_REGIONS:
633 case MemRegion::END_TYPED_REGIONS:
634 assert(0 && "Infeasible region");
635 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000636 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000637
638 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000639 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000640}
641
Ted Kremenek67f28532009-06-17 22:02:04 +0000642const GRState *RegionStoreManager::setExtent(const GRState *state,
643 const MemRegion *region,
644 SVal extent) {
645 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000646}
647
648//===----------------------------------------------------------------------===//
649// Location and region casting.
650//===----------------------------------------------------------------------===//
651
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000652/// ArrayToPointer - Emulates the "decay" of an array to a pointer
653/// type. 'Array' represents the lvalue of the array being decayed
654/// to a pointer, and the returned SVal represents the decayed
655/// version of that lvalue (i.e., a pointer to the first element of
656/// the array). This is called by GRExprEngine when evaluating casts
657/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000658SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000659 if (!isa<loc::MemRegionVal>(Array))
660 return UnknownVal();
661
662 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
663 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
664
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000665 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000666 return UnknownVal();
667
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000668 // Strip off typedefs from the ArrayRegion's ValueType.
669 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000670 ArrayType *AT = cast<ArrayType>(T);
671 T = AT->getElementType();
672
Zhongxing Xu63123d82008-11-23 04:30:35 +0000673 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000674 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000675
676 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000677}
678
Ted Kremenek9af46f52009-06-16 22:36:44 +0000679//===----------------------------------------------------------------------===//
680// Pointer arithmetic.
681//===----------------------------------------------------------------------===//
682
Zhongxing Xu262fd032009-05-20 09:00:16 +0000683SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000684 BinaryOperator::Opcode Op, Loc L, NonLoc R,
685 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000686 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000687 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000688 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000689
Zhongxing Xua1718c72009-04-03 07:33:13 +0000690 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000691 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000692
Ted Kremenek3bccf082009-07-11 00:58:27 +0000693 switch (MR->getKind()) {
694 case MemRegion::SymbolicRegionKind: {
695 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
696 QualType T;
697 // If the SymbolicRegion was cast to another type, use that type.
698 if (const QualType *t = state->get<RegionCasts>(SR))
699 T = *t;
700 else {
701 // Otherwise use the symbol's type.
702 SymbolRef Sym = SR->getSymbol();
703 T = Sym->getType(getContext());
704 }
705
706 QualType EleTy = T->getAsPointerType()->getPointeeType();
707 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
708 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
709 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000710 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000711 case MemRegion::AllocaRegionKind: {
712 // Get the alloca region's current cast type.
713 const AllocaRegion *AR = cast<AllocaRegion>(MR);
714 QualType T = *(state->get<RegionCasts>(AR));
715 QualType EleTy = T->getAsPointerType()->getPointeeType();
716 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
717 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
718 break;
719 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000720
Ted Kremenek3bccf082009-07-11 00:58:27 +0000721 case MemRegion::ElementRegionKind: {
722 ER = cast<ElementRegion>(MR);
723 break;
724 }
725
726 // Not yet handled.
727 case MemRegion::VarRegionKind:
728 case MemRegion::StringRegionKind:
729 case MemRegion::CompoundLiteralRegionKind:
730 case MemRegion::FieldRegionKind:
731 case MemRegion::ObjCObjectRegionKind:
732 case MemRegion::ObjCIvarRegionKind:
733 return UnknownVal();
734
735 // TypedViewRegion will soon be removed.
736 case MemRegion::TypedViewRegionKind:
737 return UnknownVal();
738
739 case MemRegion::CodeTextRegionKind:
740 // Technically this can happen if people do funny things with casts.
741 return UnknownVal();
742
743 case MemRegion::MemSpaceRegionKind:
744 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
745 return UnknownVal();
746
747 case MemRegion::BEG_DECL_REGIONS:
748 case MemRegion::END_DECL_REGIONS:
749 case MemRegion::BEG_TYPED_REGIONS:
750 case MemRegion::END_TYPED_REGIONS:
751 assert(0 && "Infeasible region");
752 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000753 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000754
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000755 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000756 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
757 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
758
759 // Only support concrete integer indexes for now.
760 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000761 // FIXME: For now, convert the signedness and bitwidth of offset in case
762 // they don't match. This can result from pointer arithmetic. In reality,
763 // we should figure out what are the proper semantics and implement them.
764 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000765 // This addresses the test case test/Analysis/ptr-arith.c
766 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000767 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
768 Offset->getValue()));
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000769 SVal NewIdx = Base->evalBinOp(ValMgr, Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000770 const MemRegion* NewER =
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000771 MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion(),
772 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000773 return ValMgr.makeLoc(NewER);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000774
Ted Kremenek5dc27462009-03-03 02:51:43 +0000775 }
776
777 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000778}
779
Ted Kremenek9af46f52009-06-16 22:36:44 +0000780//===----------------------------------------------------------------------===//
781// Loading values from regions.
782//===----------------------------------------------------------------------===//
783
Ted Kremeneka6275a52009-07-15 02:31:43 +0000784static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
785 RTy = Ctx.getCanonicalType(RTy);
786 UsedTy = Ctx.getCanonicalType(UsedTy);
787
788 if (RTy == UsedTy)
789 return false;
790
791 return !(Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy));
792}
793
Ted Kremenek67f28532009-06-17 22:02:04 +0000794SVal RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
795
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000796 assert(!isa<UnknownVal>(L) && "location unknown");
797 assert(!isa<UndefinedVal>(L) && "location undefined");
798
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000799 // FIXME: Is this even possible? Shouldn't this be treated as a null
800 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000801 if (isa<loc::ConcreteInt>(L))
802 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000803
Ted Kremenek67f28532009-06-17 22:02:04 +0000804 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000805
Zhongxing Xu91844122009-05-20 09:18:48 +0000806 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000807 // Example:
808 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000809 // char* p = alloca();
810 // read(p);
811 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000812 if (isa<AllocaRegion>(MR))
Zhongxing Xua1718c72009-04-03 07:33:13 +0000813 return UnknownVal();
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000814
815 if (isa<SymbolicRegion>(MR)) {
816 ASTContext &Ctx = getContext();
817 SVal idx = ValMgr.makeIntVal(0, Ctx.IntTy);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000818 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000819 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
820 }
821
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000822 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
823 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000824 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000825 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000826
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000827 // FIXME: We should eventually handle funny addressing. e.g.:
828 //
829 // int x = ...;
830 // int *p = &x;
831 // char *q = (char*) p;
832 // char c = *q; // returns the first byte of 'x'.
833 //
834 // Such funny addressing will occur due to layering of regions.
835
Ted Kremeneka6275a52009-07-15 02:31:43 +0000836 ASTContext &Ctx = getContext();
837 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
838 SVal idx = ValMgr.makeIntVal(0, Ctx.IntTy);
839 R = MRMgr.getElementRegion(T, idx, R, Ctx);
840 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000841 assert(Ctx.getCanonicalType(RTy) ==
842 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000843 }
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000844
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000845 if (RTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000846 return RetrieveStruct(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000847
848 if (RTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000849 return RetrieveArray(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000850
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000851 // FIXME: handle Vector types.
852 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000853 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +0000854
855 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
856 return RetrieveField(state, FR);
857
858 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
859 return RetrieveElement(state, ER);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000860
Ted Kremenek67f28532009-06-17 22:02:04 +0000861 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000862 RegionBindingsTy::data_type* V = B.lookup(R);
863
864 // Check if the region has a binding.
865 if (V)
866 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000867
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000868 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
869 const MemRegion *SR = IVR->getSuperRegion();
870
871 // If the super region is 'self' then return the symbol representing
872 // the value of the ivar upon entry to the method.
873 if (SR == SelfRegion) {
874 // FIXME: Do we need to handle the case where the super region
875 // has a view? We want to canonicalize the bindings.
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000876 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000877 }
878
879 // Otherwise, we need a new symbol. For now return Unknown.
880 return UnknownVal();
881 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000882
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000883 // The location does not have a bound value. This means that it has
884 // the value it had upon its creation and/or entry to the analyzed
885 // function/method. These are either symbolic values or 'undefined'.
886
887 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000888 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
889 const VarDecl *VD = VR->getDecl();
890
Ted Kremenekcec35252009-03-04 00:23:05 +0000891 if (VD == SelfDecl)
892 return loc::MemRegionVal(getSelfRegion(0));
893
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000894 if (VR->hasGlobalsOrParametersStorage())
895 return ValMgr.getRegionValueSymbolValOrUnknown(VR, VD->getType());
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000896 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000897
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000898 if (R->hasHeapOrStackStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000899 // All stack variables are considered to have undefined values
900 // upon creation. All heap allocated blocks are considered to
901 // have undefined values as well unless they are explicitly bound
902 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000903 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000904 }
905
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000906 // If the region is already cast to another type, use that type to create the
907 // symbol value.
908 if (const QualType *p = state->get<RegionCasts>(R)) {
909 QualType T = *p;
910 RTy = T->getAsPointerType()->getPointeeType();
911 }
912
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000913 // All other values are symbolic.
914 return ValMgr.getRegionValueSymbolValOrUnknown(R, RTy);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000915}
916
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000917SVal RegionStoreManager::RetrieveElement(const GRState* state,
918 const ElementRegion* R) {
919 // Check if the region has a binding.
920 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +0000921 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000922 return *V;
923
Ted Kremenek921109a2009-07-01 23:19:52 +0000924 const MemRegion* superR = R->getSuperRegion();
925
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000926 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +0000927 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000928 const StringLiteral *Str = StrR->getStringLiteral();
929 SVal Idx = R->getIndex();
930 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
931 int64_t i = CI->getValue().getSExtValue();
932 char c;
933 if (i == Str->getByteLength())
934 c = '\0';
935 else
936 c = Str->getStrData()[i];
937 return ValMgr.makeIntVal(c, getContext().CharTy);
938 }
939 }
940
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000941 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +0000942 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000943 if (D->hasConjuredSymbol())
944 return ValMgr.getRegionValueSymbolVal(R);
945 else
946 return *D;
947 }
948
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000949 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +0000950 if (const SVal *V = B.lookup(superR)) {
951 if (SymbolRef parentSym = V->getAsSymbol())
952 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
953
954 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +0000955 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000956 }
Ted Kremenek921109a2009-07-01 23:19:52 +0000957
958 if (R->hasHeapStorage()) {
959 // FIXME: If the region has heap storage and we know nothing special
960 // about its bindings, should we instead return UnknownVal? Seems like
961 // we should only return UndefinedVal in the cases where we know the value
962 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000963 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +0000964 }
965
Ted Kremenekdc147262009-07-02 22:02:15 +0000966 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +0000967 // Currently we don't reason specially about Clang-style vectors. Check
968 // if superR is a vector and if so return Unknown.
969 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
970 if (typedSuperR->getValueType(getContext())->isVectorType())
971 return UnknownVal();
972 }
973
974 return UndefinedVal();
975 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000976
977 QualType Ty = R->getValueType(getContext());
978
979 // If the region is already cast to another type, use that type to create the
980 // symbol value.
981 if (const QualType *p = state->get<RegionCasts>(R))
982 Ty = (*p)->getAsPointerType()->getPointeeType();
983
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000984 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000985}
986
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000987SVal RegionStoreManager::RetrieveField(const GRState* state,
988 const FieldRegion* R) {
989 QualType Ty = R->getValueType(getContext());
990
991 // Check if the region has a binding.
992 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +0000993 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000994 return *V;
995
Ted Kremenek8b2ba312009-07-01 23:30:34 +0000996 const MemRegion* superR = R->getSuperRegion();
997 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000998 if (D->hasConjuredSymbol())
999 return ValMgr.getRegionValueSymbolVal(R);
1000
1001 if (D->isZeroConstant())
1002 return ValMgr.makeZeroVal(Ty);
1003
1004 if (D->isUnknown())
1005 return *D;
1006
1007 assert(0 && "Unknown default value");
1008 }
1009
Ted Kremenekdc147262009-07-02 22:02:15 +00001010 // FIXME: Is this correct? Should it be UnknownVal?
1011 if (R->hasHeapStorage())
1012 return UndefinedVal();
1013
1014 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001015 return UndefinedVal();
1016
1017 // If the region is already cast to another type, use that type to create the
1018 // symbol value.
1019 if (const QualType *p = state->get<RegionCasts>(R)) {
1020 QualType tmp = *p;
1021 Ty = tmp->getAsPointerType()->getPointeeType();
1022 }
1023
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001024 // All other values are symbolic.
1025 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001026}
1027
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001028SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1029 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001030 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001031 assert(T->isStructureType());
1032
Zhongxing Xub7507d12009-06-11 07:27:30 +00001033 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001034 RecordDecl* RD = RT->getDecl();
1035 assert(RD->isDefinition());
1036
1037 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1038
Ted Kremenek67f28532009-06-17 22:02:04 +00001039 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1040 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001041 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001042
Douglas Gregore267ff32008-12-11 20:41:00 +00001043 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1044 FieldEnd = Fields.rend();
1045 Field != FieldEnd; ++Field) {
1046 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001047 QualType FTy = (*Field)->getType();
Ted Kremenek67f28532009-06-17 22:02:04 +00001048 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001049 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1050 }
1051
Zhongxing Xud91ee272009-06-23 09:02:15 +00001052 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001053}
1054
Ted Kremenek67f28532009-06-17 22:02:04 +00001055SVal RegionStoreManager::RetrieveArray(const GRState *state,
1056 const TypedRegion * R) {
1057
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001058 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001059 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1060
1061 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001062 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu2ee52142009-05-11 12:48:56 +00001063 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001064
1065 for (; i < Size; ++i) {
Zhongxing Xud91ee272009-06-23 09:02:15 +00001066 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001067 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1068 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001069 QualType ETy = ER->getElementType();
Ted Kremenek67f28532009-06-17 22:02:04 +00001070 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001071 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1072 }
1073
Zhongxing Xud91ee272009-06-23 09:02:15 +00001074 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001075}
1076
Ted Kremenek9af46f52009-06-16 22:36:44 +00001077//===----------------------------------------------------------------------===//
1078// Binding values to regions.
1079//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001080
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001081Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001082 const MemRegion* R = 0;
1083
1084 if (isa<loc::MemRegionVal>(L))
1085 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001086
1087 if (R) {
1088 RegionBindingsTy B = GetRegionBindings(store);
1089 return RBFactory.Remove(B, R).getRoot();
1090 }
1091
1092 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001093}
1094
Ted Kremenek67f28532009-06-17 22:02:04 +00001095const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001096 if (isa<loc::ConcreteInt>(L))
1097 return state;
1098
Ted Kremenek9af46f52009-06-16 22:36:44 +00001099 // If we get here, the location should be a region.
1100 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001101
1102 // Check if the region is a struct region.
1103 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1104 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001105 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001106
Ted Kremenek67f28532009-06-17 22:02:04 +00001107 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001108
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001109 B = RBFactory.Add(B, R, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001110
Ted Kremenek67f28532009-06-17 22:02:04 +00001111 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001112}
1113
Ted Kremenek67f28532009-06-17 22:02:04 +00001114const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001115 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001116
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001117 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001118 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001119
Ted Kremenek0964a062009-01-21 06:57:53 +00001120 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001121 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001122 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001123 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001124
Zhongxing Xud91ee272009-06-23 09:02:15 +00001125 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001126}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001127
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001128// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001129const GRState *
1130RegionStoreManager::BindCompoundLiteral(const GRState *state,
1131 const CompoundLiteralExpr* CL,
1132 SVal V) {
1133
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001134 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001135 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001136}
1137
Ted Kremenek67f28532009-06-17 22:02:04 +00001138const GRState *RegionStoreManager::BindArray(const GRState *state,
1139 const TypedRegion* R,
1140 SVal Init) {
1141
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001142 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001143 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001144 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001145
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001146 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001147 llvm::APSInt i(llvm::APInt::getNullValue(Size.getBitWidth()), false);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001148
1149 // Check if the init expr is a StringLiteral.
1150 if (isa<loc::MemRegionVal>(Init)) {
1151 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1152 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1153 const char* str = S->getStrData();
1154 unsigned len = S->getByteLength();
1155 unsigned j = 0;
1156
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001157 // Copy bytes from the string literal into the target array. Trailing bytes
1158 // in the array that are not covered by the string literal are initialized
1159 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001160 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001161 if (j >= len)
1162 break;
1163
Zhongxing Xu3038c5a2009-06-23 06:13:19 +00001164 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001165 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx,R,getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001166
Zhongxing Xud91ee272009-06-23 09:02:15 +00001167 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001168 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001169 }
1170
Ted Kremenek67f28532009-06-17 22:02:04 +00001171 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001172 }
1173
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001174 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001175 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1176
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001177 for (; i < Size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001178 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001179 if (VI == VE)
1180 break;
1181
Zhongxing Xu3038c5a2009-06-23 06:13:19 +00001182 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001183 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001184
1185 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001186 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001187 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001188 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001189 }
1190
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001191 // If the init list is shorter than the array length, set the array default
1192 // value.
1193 if (i < Size) {
1194 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001195 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001196 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001197 }
1198 }
1199
Ted Kremenek67f28532009-06-17 22:02:04 +00001200 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001201}
1202
Ted Kremenek67f28532009-06-17 22:02:04 +00001203const GRState *
1204RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1205 SVal V) {
1206
1207 if (!Features.supportsFields())
1208 return state;
1209
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001210 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001211 assert(T->isStructureType());
1212
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001213 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001214 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001215
1216 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001217 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001218
Ted Kremenek67f28532009-06-17 22:02:04 +00001219 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1220 // Ignore them and kill the field values.
1221 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1222 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001223
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001224 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001225 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001226
1227 RecordDecl::field_iterator FI, FE;
1228
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001229 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001230
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001231 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001232 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001233
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001234 QualType FTy = (*FI)->getType();
1235 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1236
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001237 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001238 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001239 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001240 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001241 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001242 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001243 }
1244
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001245 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001246 if (FI != FE)
1247 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001248
Ted Kremenek67f28532009-06-17 22:02:04 +00001249 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001250}
1251
Ted Kremenek67f28532009-06-17 22:02:04 +00001252const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001253 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001254
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001255 // Set the default value of the struct region to "unknown".
1256 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001257
1258 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001259 Store store = state->getStore();
1260 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001261 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001262 const MemRegion* R = I.getKey();
1263 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1264 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001265 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001266 }
1267
Ted Kremenek67f28532009-06-17 22:02:04 +00001268 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001269}
1270
Ted Kremenek9af46f52009-06-16 22:36:44 +00001271//===----------------------------------------------------------------------===//
1272// Region views.
1273//===----------------------------------------------------------------------===//
1274
Ted Kremenek67f28532009-06-17 22:02:04 +00001275const GRState *RegionStoreManager::AddRegionView(const GRState *state,
1276 const MemRegion* View,
1277 const MemRegion* Base) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001278
1279 // First, retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001280 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001281 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001282
1283 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001284 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001285
1286 // Create a new state with the new region view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001287 return state->set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001288}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001289
Ted Kremenek67f28532009-06-17 22:02:04 +00001290const GRState *RegionStoreManager::RemoveRegionView(const GRState *state,
1291 const MemRegion* View,
1292 const MemRegion* Base) {
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001293 // Retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001294 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001295
1296 // If the base region has no view, return.
1297 if (!d)
Ted Kremenek67f28532009-06-17 22:02:04 +00001298 return state;
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001299
1300 // Remove the view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001301 return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001302}
Zhongxing Xu20794942009-05-06 08:33:50 +00001303
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001304const GRState *RegionStoreManager::setCastType(const GRState *state,
1305 const MemRegion* R, QualType T) {
Zhongxing Xu82037252009-07-14 01:12:46 +00001306 // We do not record generic cast type, since we are using cast type to
1307 // invlidate regions, and generic type is meaningless for invalidating
1308 // regions.
1309 // If the region already has a cast type before, that type is preserved.
1310 // FIXME: is this the right thing to do?
1311 if (isGenericPtr(getContext(), T))
1312 return state;
Ted Kremenek67f28532009-06-17 22:02:04 +00001313 return state->set<RegionCasts>(R, T);
Zhongxing Xu20794942009-05-06 08:33:50 +00001314}
Zhongxing Xu264e9372009-05-12 10:10:00 +00001315
Ted Kremenek67f28532009-06-17 22:02:04 +00001316const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1317 const MemRegion* R, SVal V) {
1318 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001319}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001320
1321//===----------------------------------------------------------------------===//
1322// State pruning.
1323//===----------------------------------------------------------------------===//
1324
1325static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1326 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1327 const MemRegion *R = XR->getRegion();
1328
1329 while (R) {
1330 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1331 SymReaper.markLive(SR->getSymbol());
1332 return;
1333 }
1334
1335 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1336 R = SR->getSuperRegion();
1337 continue;
1338 }
1339
1340 break;
1341 }
1342
1343 return;
1344 }
1345
1346 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1347 SymReaper.markLive(*SI);
1348}
1349
Ted Kremenek67f28532009-06-17 22:02:04 +00001350Store RegionStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001351 SymbolReaper& SymReaper,
1352 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001353{
Ted Kremenek9af46f52009-06-16 22:36:44 +00001354 Store store = state->getStore();
1355 RegionBindingsTy B = GetRegionBindings(store);
1356
1357 // Lazily constructed backmap from MemRegions to SubRegions.
1358 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1359 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1360
1361 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
1362 // the ability to reuse memory. This way we can keep TmpAlloc around as
1363 // an instance variable of RegionStoreManager (avoiding repeated malloc
1364 // overhead).
1365 llvm::BumpPtrAllocator TmpAlloc;
1366
1367 // Factory objects.
1368 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1369 SubRegionsTy::Factory SubRegF(TmpAlloc);
1370
1371 // The backmap from regions to subregions.
1372 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1373
1374 // Do a pass over the regions in the store. For VarRegions we check if
1375 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001376 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001377 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1378
1379 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1380 IntermediateRoots.push_back(I.getKey());
1381 }
1382
1383 while (!IntermediateRoots.empty()) {
1384 const MemRegion* R = IntermediateRoots.back();
1385 IntermediateRoots.pop_back();
1386
1387 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001388 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001389 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001390 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001391 }
1392 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1393 if (SymReaper.isLive(SR->getSymbol()))
1394 RegionRoots.push_back(SR);
1395 }
1396 else {
1397 // Get the super region for R.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001398 const MemRegion* superR = cast<SubRegion>(R)->getSuperRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001399
1400 // Get the current set of subregions for SuperR.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001401 const SubRegionsTy* SRptr = SubRegMap.lookup(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001402 SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
1403
1404 // Add R to the subregions of SuperR.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001405 SubRegMap = SubRegMapF.Add(SubRegMap, superR, SubRegF.Add(SRs, R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001406
1407 // Super region may be VarRegion or subregion of another VarRegion. Add it
1408 // to the work list.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001409 if (isa<SubRegion>(superR))
1410 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001411 }
1412 }
1413
1414 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1415 // of the store. We want to find all live symbols and dead regions.
1416 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1417
1418 while (!RegionRoots.empty()) {
1419 // Dequeue the next region on the worklist.
1420 const MemRegion* R = RegionRoots.back();
1421 RegionRoots.pop_back();
1422
1423 // Check if we have already processed this region.
1424 if (Marked.count(R)) continue;
1425
1426 // Mark this region as processed. This is needed for termination in case
1427 // a region is referenced more than once.
1428 Marked.insert(R);
1429
1430 // Mark the symbol for any live SymbolicRegion as "live". This means we
1431 // should continue to track that symbol.
1432 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1433 SymReaper.markLive(SymR->getSymbol());
1434
1435 // Get the data binding for R (if any).
1436 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1437 if (Xptr) {
1438 SVal X = *Xptr;
1439 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1440
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001441 // If X is a region, then add it to the RegionRoots.
1442 if (const MemRegion *RX = X.getAsRegion()) {
1443 RegionRoots.push_back(RX);
1444
1445 // Mark the super region of the RX as live.
1446 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1447 // 'y' => element region. 'x' is its super region.
1448 // We only add one level super region for now.
Zhongxing Xu5ff8e8c2009-07-01 02:12:57 +00001449 // FIXME: maybe multiple level of super regions should be added.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001450 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1451 RegionRoots.push_back(SR->getSuperRegion());
1452 }
1453 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001454 }
1455
1456 // Get the subregions of R. These are RegionRoots as well since they
1457 // represent values that are also bound to R.
1458 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1459 if (!SRptr) continue;
1460 SubRegionsTy SR = *SRptr;
1461
1462 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1463 RegionRoots.push_back(*I);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001464
Ted Kremenek9af46f52009-06-16 22:36:44 +00001465 }
1466
1467 // We have now scanned the store, marking reachable regions and symbols
1468 // as live. We now remove all the regions that are dead from the store
1469 // as well as update DSymbols with the set symbols that are now dead.
1470 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1471 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001472 // If this region live? Is so, none of its symbols are dead.
1473 if (Marked.count(R))
1474 continue;
1475
1476 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001477 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001478
1479 // Mark all non-live symbols that this region references as dead.
1480 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1481 SymReaper.maybeDead(SymR->getSymbol());
1482
1483 SVal X = I.getData();
1484 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1485 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
1486 }
1487
1488 return store;
1489}
1490
1491//===----------------------------------------------------------------------===//
1492// Utility methods.
1493//===----------------------------------------------------------------------===//
1494
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001495void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001496 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001497 RegionBindingsTy B = GetRegionBindings(store);
1498 OS << "Store:" << nl;
1499
Ted Kremenek6f9b3a42009-07-13 23:53:06 +00001500 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
1501 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001502}