blob: dc54c8fa392b22a926730b88d62617541c3bda20 [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
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000282 SValuator::CastResult Retrieve(const GRState *state, Loc L,
283 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000284
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000285 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000286
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000287 SVal RetrieveField(const GRState *state, const FieldRegion *R);
288
289 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000290
Ted Kremenek9031dd72009-07-21 00:12:07 +0000291 SVal RetrieveVar(const GRState *state, const VarRegion *R);
292
Ted Kremenek25c54572009-07-20 22:58:02 +0000293 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
294
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000295 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
296 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000297
Ted Kremenek67f28532009-06-17 22:02:04 +0000298 /// Retrieve the values in a struct and return a CompoundVal, used when doing
299 /// struct copy:
300 /// struct s x, y;
301 /// x = y;
302 /// y's value is retrieved by this method.
303 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
304
305 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
306
307 //===------------------------------------------------------------------===//
308 // State pruning.
309 //===------------------------------------------------------------------===//
310
311 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
312 /// It returns a new Store with these values removed.
313 Store RemoveDeadBindings(const GRState *state, Stmt* Loc, SymbolReaper& SymReaper,
314 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
315
316 //===------------------------------------------------------------------===//
317 // Region "extents".
318 //===------------------------------------------------------------------===//
319
320 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
321 SVal getSizeInElements(const GRState *state, const MemRegion* R);
322
323 //===------------------------------------------------------------------===//
324 // Region "views".
325 //===------------------------------------------------------------------===//
326
327 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
328 const MemRegion* Base);
329
330 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
331 const MemRegion* Base);
332
333 //===------------------------------------------------------------------===//
334 // Utility methods.
335 //===------------------------------------------------------------------===//
336
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000337 const GRState *setCastType(const GRState *state, const MemRegion* R,
338 QualType T);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000339
Zhongxing Xu82037252009-07-14 01:12:46 +0000340 const QualType *getCastType(const GRState *state, const MemRegion *R) {
341 return state->get<RegionCasts>(R);
342 }
343
Zhongxing Xu17892752008-10-08 02:50:44 +0000344 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000345 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000346 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000347
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000348 void print(Store store, llvm::raw_ostream& Out, const char* nl,
349 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000350
351 void iterBindings(Store store, BindingsHandler& f) {
352 // FIXME: Implement.
353 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000354
Ted Kremenek67f28532009-06-17 22:02:04 +0000355 // FIXME: Remove.
356 BasicValueFactory& getBasicVals() {
357 return StateMgr.getBasicVals();
358 }
359
360 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000361 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000362};
363
364} // end anonymous namespace
365
Zhongxing Xu82037252009-07-14 01:12:46 +0000366static bool isGenericPtr(ASTContext &Ctx, QualType Ty) {
367 if (Ty->isObjCIdType() || Ty->isObjCQualifiedIdType())
368 return true;
369
370 while (true) {
371 Ty = Ctx.getCanonicalType(Ty);
372
373 if (Ty->isVoidType())
374 return true;
375
Ted Kremenek35366a62009-07-17 17:50:17 +0000376 if (const PointerType *PT = Ty->getAsPointerType()) {
Zhongxing Xu82037252009-07-14 01:12:46 +0000377 Ty = PT->getPointeeType();
378 continue;
379 }
380
381 break;
382 }
383
384 return false;
385}
386
Ted Kremenek9af46f52009-06-16 22:36:44 +0000387//===----------------------------------------------------------------------===//
388// RegionStore creation.
389//===----------------------------------------------------------------------===//
390
391StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
392 RegionStoreFeatures F = maximal_features_tag();
393 return new RegionStoreManager(StMgr, F);
394}
395
396StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
397 RegionStoreFeatures F = minimal_features_tag();
398 F.enableFields(true);
399 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000400}
401
Ted Kremenek14453bf2009-03-03 19:02:42 +0000402SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000403 RegionBindingsTy B = GetRegionBindings(state->getStore());
404 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
405
406 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
407 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
408 M->add(R->getSuperRegion(), R);
409 }
410
Ted Kremenek14453bf2009-03-03 19:02:42 +0000411 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000412}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000413
Ted Kremenek9af46f52009-06-16 22:36:44 +0000414//===----------------------------------------------------------------------===//
415// getLValueXXX methods.
416//===----------------------------------------------------------------------===//
417
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000418/// getLValueString - Returns an SVal representing the lvalue of a
419/// StringLiteral. Within RegionStore a StringLiteral has an
420/// associated StringRegion, and the lvalue of a StringLiteral is the
421/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000422SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000423 const StringLiteral* S) {
424 return loc::MemRegionVal(MRMgr.getStringRegion(S));
425}
426
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000427/// getLValueVar - Returns an SVal that represents the lvalue of a
428/// variable. Within RegionStore a variable has an associated
429/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000430SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000431 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
432}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000433
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000434/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
435/// of a compound literal. Within RegionStore a compound literal
436/// has an associated region, and the lvalue of the compound literal
437/// is the lvalue of that region.
438SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000439RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000440 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000441 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
442}
443
Ted Kremenek67f28532009-06-17 22:02:04 +0000444SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000445 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000446 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000447}
448
Ted Kremenek67f28532009-06-17 22:02:04 +0000449SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000450 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000451 return getLValueFieldOrIvar(St, Base, D);
452}
453
Ted Kremenek67f28532009-06-17 22:02:04 +0000454SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000455 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000456 if (Base.isUnknownOrUndef())
457 return Base;
458
459 Loc BaseL = cast<Loc>(Base);
460 const MemRegion* BaseR = 0;
461
462 switch (BaseL.getSubKind()) {
463 case loc::MemRegionKind:
464 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
465 break;
466
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000467 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000468 // These are anormal cases. Flag an undefined value.
469 return UndefinedVal();
470
471 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000472 // While these seem funny, this can happen through casts.
473 // FIXME: What we should return is the field offset. For example,
474 // add the field offset to the integer value. That way funny things
475 // like this work properly: &(((struct foo *) 0xa)->f)
476 return Base;
477
478 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000479 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000480 return Base;
481 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000482
483 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
484 // of FieldDecl.
485 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
486 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000487
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000488 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000489}
490
Ted Kremenek67f28532009-06-17 22:02:04 +0000491SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000492 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000493 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000494
Ted Kremenekde7ec632009-03-09 22:44:49 +0000495 // If the base is an unknown or undefined value, just return it back.
496 // FIXME: For absolute pointer addresses, we just return that value back as
497 // well, although in reality we should return the offset added to that
498 // value.
499 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000500 return Base;
501
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000502 // Only handle integer offsets... for now.
503 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000504 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000505
Zhongxing Xuce760782009-05-09 13:20:07 +0000506 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000507
508 // Pointer of any type can be cast and used as array base.
509 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
510
Ted Kremenek46537392009-07-16 01:33:37 +0000511 // Convert the offset to the appropriate size and signedness.
512 Offset = ValMgr.convertToArrayIndex(Offset);
513
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000514 if (!ElemR) {
515 //
516 // If the base region is not an ElementRegion, create one.
517 // This can happen in the following example:
518 //
519 // char *p = __builtin_alloc(10);
520 // p[1] = 8;
521 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000522 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000523 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000524 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000525 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000526 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000527
528 SVal BaseIdx = ElemR->getIndex();
529
530 if (!isa<nonloc::ConcreteInt>(BaseIdx))
531 return UnknownVal();
532
533 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
534 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
535 assert(BaseIdxI.isSigned());
536
Ted Kremenek46537392009-07-16 01:33:37 +0000537 // Compute the new index.
538 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000539
Ted Kremenek46537392009-07-16 01:33:37 +0000540 // Construct the new ElementRegion.
541 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000542 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
543 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000544}
545
Ted Kremenek9af46f52009-06-16 22:36:44 +0000546//===----------------------------------------------------------------------===//
547// Extents for regions.
548//===----------------------------------------------------------------------===//
549
Ted Kremenek67f28532009-06-17 22:02:04 +0000550SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000551 const MemRegion *R) {
552
553 switch (R->getKind()) {
554 case MemRegion::MemSpaceRegionKind:
555 assert(0 && "Cannot index into a MemSpace");
556 return UnknownVal();
557
558 case MemRegion::CodeTextRegionKind:
559 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000560 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000561
562 // Not yet handled.
563 case MemRegion::AllocaRegionKind:
564 case MemRegion::CompoundLiteralRegionKind:
565 case MemRegion::ElementRegionKind:
566 case MemRegion::FieldRegionKind:
567 case MemRegion::ObjCIvarRegionKind:
568 case MemRegion::ObjCObjectRegionKind:
569 case MemRegion::SymbolicRegionKind:
570 return UnknownVal();
571
572 case MemRegion::StringRegionKind: {
573 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
574 // We intentionally made the size value signed because it participates in
575 // operations with signed indices.
576 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000577 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000578
579 // TypedViewRegion will soon be removed.
580 case MemRegion::TypedViewRegionKind:
581 return UnknownVal();
Zhongxing Xu20794942009-05-06 08:33:50 +0000582
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000583 case MemRegion::VarRegionKind: {
584 const VarRegion* VR = cast<VarRegion>(R);
585 // Get the type of the variable.
586 QualType T = VR->getDesugaredValueType(getContext());
587
588 // FIXME: Handle variable-length arrays.
589 if (isa<VariableArrayType>(T))
590 return UnknownVal();
591
592 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
593 // return the size as signed integer.
594 return ValMgr.makeIntVal(CAT->getSize(), false);
595 }
596
597 const QualType* CastTy = state->get<RegionCasts>(VR);
598
599 // If the VarRegion is cast to other type, compute the size with respect to
600 // that type.
601 if (CastTy) {
602 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
603 QualType VarTy = VR->getValueType(getContext());
604 uint64_t EleSize = getContext().getTypeSize(EleTy);
605 uint64_t VarSize = getContext().getTypeSize(VarTy);
606 assert(VarSize != 0);
607 return ValMgr.makeIntVal(VarSize/EleSize, false);
608 }
609
610 // Clients can use ordinary variables as if they were arrays. These
611 // essentially are arrays of size 1.
612 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000613 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000614
615 case MemRegion::BEG_DECL_REGIONS:
616 case MemRegion::END_DECL_REGIONS:
617 case MemRegion::BEG_TYPED_REGIONS:
618 case MemRegion::END_TYPED_REGIONS:
619 assert(0 && "Infeasible region");
620 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000621 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000622
623 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000624 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000625}
626
Ted Kremenek67f28532009-06-17 22:02:04 +0000627const GRState *RegionStoreManager::setExtent(const GRState *state,
628 const MemRegion *region,
629 SVal extent) {
630 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000631}
632
633//===----------------------------------------------------------------------===//
634// Location and region casting.
635//===----------------------------------------------------------------------===//
636
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000637/// ArrayToPointer - Emulates the "decay" of an array to a pointer
638/// type. 'Array' represents the lvalue of the array being decayed
639/// to a pointer, and the returned SVal represents the decayed
640/// version of that lvalue (i.e., a pointer to the first element of
641/// the array). This is called by GRExprEngine when evaluating casts
642/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000643SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000644 if (!isa<loc::MemRegionVal>(Array))
645 return UnknownVal();
646
647 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
648 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
649
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000650 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000651 return UnknownVal();
652
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000653 // Strip off typedefs from the ArrayRegion's ValueType.
654 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000655 ArrayType *AT = cast<ArrayType>(T);
656 T = AT->getElementType();
657
Ted Kremenek75185b52009-07-16 00:00:11 +0000658 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
659 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000660
661 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000662}
663
Ted Kremenek9af46f52009-06-16 22:36:44 +0000664//===----------------------------------------------------------------------===//
665// Pointer arithmetic.
666//===----------------------------------------------------------------------===//
667
Zhongxing Xu262fd032009-05-20 09:00:16 +0000668SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000669 BinaryOperator::Opcode Op, Loc L, NonLoc R,
670 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000671 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000672 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000673 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000674
Zhongxing Xua1718c72009-04-03 07:33:13 +0000675 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000676 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000677
Ted Kremenek3bccf082009-07-11 00:58:27 +0000678 switch (MR->getKind()) {
679 case MemRegion::SymbolicRegionKind: {
680 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
681 QualType T;
682 // If the SymbolicRegion was cast to another type, use that type.
683 if (const QualType *t = state->get<RegionCasts>(SR))
684 T = *t;
685 else {
686 // Otherwise use the symbol's type.
687 SymbolRef Sym = SR->getSymbol();
688 T = Sym->getType(getContext());
689 }
690
Ted Kremenek35366a62009-07-17 17:50:17 +0000691 QualType EleTy = T->getAsPointerType()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000692 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
693 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
694 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000695 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000696 case MemRegion::AllocaRegionKind: {
697 // Get the alloca region's current cast type.
698 const AllocaRegion *AR = cast<AllocaRegion>(MR);
699 QualType T = *(state->get<RegionCasts>(AR));
Ted Kremenek35366a62009-07-17 17:50:17 +0000700 QualType EleTy = T->getAsPointerType()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000701 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
702 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
703 break;
704 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000705
Ted Kremenek3bccf082009-07-11 00:58:27 +0000706 case MemRegion::ElementRegionKind: {
707 ER = cast<ElementRegion>(MR);
708 break;
709 }
710
711 // Not yet handled.
712 case MemRegion::VarRegionKind:
713 case MemRegion::StringRegionKind:
714 case MemRegion::CompoundLiteralRegionKind:
715 case MemRegion::FieldRegionKind:
716 case MemRegion::ObjCObjectRegionKind:
717 case MemRegion::ObjCIvarRegionKind:
718 return UnknownVal();
719
720 // TypedViewRegion will soon be removed.
721 case MemRegion::TypedViewRegionKind:
722 return UnknownVal();
723
724 case MemRegion::CodeTextRegionKind:
725 // Technically this can happen if people do funny things with casts.
726 return UnknownVal();
727
728 case MemRegion::MemSpaceRegionKind:
729 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
730 return UnknownVal();
731
732 case MemRegion::BEG_DECL_REGIONS:
733 case MemRegion::END_DECL_REGIONS:
734 case MemRegion::BEG_TYPED_REGIONS:
735 case MemRegion::END_TYPED_REGIONS:
736 assert(0 && "Infeasible region");
737 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000738 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000739
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000740 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000741 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
742 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
743
744 // Only support concrete integer indexes for now.
745 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000746 // FIXME: Should use SValuator here.
747 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
748 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000749 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000750 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
751 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000752 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000753 }
754
755 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000756}
757
Ted Kremenek9af46f52009-06-16 22:36:44 +0000758//===----------------------------------------------------------------------===//
759// Loading values from regions.
760//===----------------------------------------------------------------------===//
761
Ted Kremeneka6275a52009-07-15 02:31:43 +0000762static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
763 RTy = Ctx.getCanonicalType(RTy);
764 UsedTy = Ctx.getCanonicalType(UsedTy);
765
766 if (RTy == UsedTy)
767 return false;
768
Ted Kremenek25c54572009-07-20 22:58:02 +0000769
770 // Recursively check the types. We basically want to see if a pointer value
771 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
772 // represents a reinterpretation.
773 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
774 const PointerType *PRTy = RTy->getAsPointerType();
775 const PointerType *PUsedTy = UsedTy->getAsPointerType();
776
777 return PUsedTy && PRTy &&
778 IsReinterpreted(PRTy->getPointeeType(),
779 PUsedTy->getPointeeType(), Ctx);
780 }
781
782 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000783}
784
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000785SValuator::CastResult
786RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000787
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000788 assert(!isa<UnknownVal>(L) && "location unknown");
789 assert(!isa<UndefinedVal>(L) && "location undefined");
790
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000791 // FIXME: Is this even possible? Shouldn't this be treated as a null
792 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000793 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000794 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000795
Ted Kremenek67f28532009-06-17 22:02:04 +0000796 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000797
Zhongxing Xu91844122009-05-20 09:18:48 +0000798 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000799 // Example:
800 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000801 // char* p = alloca();
802 // read(p);
803 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000804 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000805 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000806
807 if (isa<SymbolicRegion>(MR)) {
808 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000809 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000810 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000811 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
812 }
813
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000814 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
815 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000816 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000817 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000818
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000819 // FIXME: We should eventually handle funny addressing. e.g.:
820 //
821 // int x = ...;
822 // int *p = &x;
823 // char *q = (char*) p;
824 // char c = *q; // returns the first byte of 'x'.
825 //
826 // Such funny addressing will occur due to layering of regions.
827
Ted Kremeneka6275a52009-07-15 02:31:43 +0000828 ASTContext &Ctx = getContext();
829 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000830 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
831 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000832 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000833 assert(Ctx.getCanonicalType(RTy) ==
834 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000835 }
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000836
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000837 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000838 return SValuator::CastResult(state, RetrieveStruct(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000839
840 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000841 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000842
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000843 // FIXME: handle Vector types.
844 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000845 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000846
847 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000848 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000849
850 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000851 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000852
Ted Kremenek25c54572009-07-20 22:58:02 +0000853 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000854 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000855
856 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000857 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000858
Ted Kremenek67f28532009-06-17 22:02:04 +0000859 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000860 RegionBindingsTy::data_type* V = B.lookup(R);
861
862 // Check if the region has a binding.
863 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000864 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000865
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000866 // The location does not have a bound value. This means that it has
867 // the value it had upon its creation and/or entry to the analyzed
868 // function/method. These are either symbolic values or 'undefined'.
869
Ted Kremenek265a3052009-02-24 02:23:11 +0000870
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000871 if (R->hasHeapOrStackStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000872 // All stack variables are considered to have undefined values
873 // upon creation. All heap allocated blocks are considered to
874 // have undefined values as well unless they are explicitly bound
875 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000876 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000877 }
878
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000879 // If the region is already cast to another type, use that type to create the
880 // symbol value.
881 if (const QualType *p = state->get<RegionCasts>(R)) {
882 QualType T = *p;
Ted Kremenek35366a62009-07-17 17:50:17 +0000883 RTy = T->getAsPointerType()->getPointeeType();
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000884 }
885
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000886 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000887 return SValuator::CastResult(state,
888 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000889}
890
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000891SVal RegionStoreManager::RetrieveElement(const GRState* state,
892 const ElementRegion* R) {
893 // Check if the region has a binding.
894 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +0000895 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000896 return *V;
897
Ted Kremenek921109a2009-07-01 23:19:52 +0000898 const MemRegion* superR = R->getSuperRegion();
899
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000900 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +0000901 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000902 const StringLiteral *Str = StrR->getStringLiteral();
903 SVal Idx = R->getIndex();
904 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
905 int64_t i = CI->getValue().getSExtValue();
906 char c;
907 if (i == Str->getByteLength())
908 c = '\0';
909 else
910 c = Str->getStrData()[i];
911 return ValMgr.makeIntVal(c, getContext().CharTy);
912 }
913 }
914
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000915 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +0000916 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000917 if (D->hasConjuredSymbol())
918 return ValMgr.getRegionValueSymbolVal(R);
919 else
920 return *D;
921 }
922
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000923 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +0000924 if (const SVal *V = B.lookup(superR)) {
925 if (SymbolRef parentSym = V->getAsSymbol())
926 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
927
928 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +0000929 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000930 }
Ted Kremenek921109a2009-07-01 23:19:52 +0000931
932 if (R->hasHeapStorage()) {
933 // FIXME: If the region has heap storage and we know nothing special
934 // about its bindings, should we instead return UnknownVal? Seems like
935 // we should only return UndefinedVal in the cases where we know the value
936 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000937 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +0000938 }
939
Ted Kremenekdc147262009-07-02 22:02:15 +0000940 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +0000941 // Currently we don't reason specially about Clang-style vectors. Check
942 // if superR is a vector and if so return Unknown.
943 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
944 if (typedSuperR->getValueType(getContext())->isVectorType())
945 return UnknownVal();
946 }
947
948 return UndefinedVal();
949 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000950
951 QualType Ty = R->getValueType(getContext());
952
953 // If the region is already cast to another type, use that type to create the
954 // symbol value.
955 if (const QualType *p = state->get<RegionCasts>(R))
Ted Kremenek35366a62009-07-17 17:50:17 +0000956 Ty = (*p)->getAsPointerType()->getPointeeType();
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000957
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000958 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000959}
960
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000961SVal RegionStoreManager::RetrieveField(const GRState* state,
962 const FieldRegion* R) {
963 QualType Ty = R->getValueType(getContext());
964
965 // Check if the region has a binding.
966 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +0000967 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000968 return *V;
969
Ted Kremenek8b2ba312009-07-01 23:30:34 +0000970 const MemRegion* superR = R->getSuperRegion();
971 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000972 if (D->hasConjuredSymbol())
973 return ValMgr.getRegionValueSymbolVal(R);
974
975 if (D->isZeroConstant())
976 return ValMgr.makeZeroVal(Ty);
977
978 if (D->isUnknown())
979 return *D;
980
981 assert(0 && "Unknown default value");
982 }
983
Ted Kremenekdc147262009-07-02 22:02:15 +0000984 // FIXME: Is this correct? Should it be UnknownVal?
985 if (R->hasHeapStorage())
986 return UndefinedVal();
987
988 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000989 return UndefinedVal();
990
991 // If the region is already cast to another type, use that type to create the
992 // symbol value.
993 if (const QualType *p = state->get<RegionCasts>(R)) {
994 QualType tmp = *p;
Ted Kremenek35366a62009-07-17 17:50:17 +0000995 Ty = tmp->getAsPointerType()->getPointeeType();
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000996 }
997
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000998 // All other values are symbolic.
999 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001000}
1001
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001002SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1003 const ObjCIvarRegion* R) {
1004
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001005 // Check if the region has a binding.
1006 RegionBindingsTy B = GetRegionBindings(state->getStore());
1007
1008 if (const SVal* V = B.lookup(R))
1009 return *V;
1010
1011 const MemRegion *superR = R->getSuperRegion();
1012
1013 // Check if the super region has a binding.
1014 if (const SVal *V = B.lookup(superR)) {
1015 if (SymbolRef parentSym = V->getAsSymbol())
1016 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1017
1018 // Other cases: give up.
1019 return UnknownVal();
1020 }
1021
Ted Kremenek25c54572009-07-20 22:58:02 +00001022 return RetrieveLazySymbol(state, R);
1023}
1024
Ted Kremenek9031dd72009-07-21 00:12:07 +00001025SVal RegionStoreManager::RetrieveVar(const GRState *state,
1026 const VarRegion *R) {
1027
1028 // Check if the region has a binding.
1029 RegionBindingsTy B = GetRegionBindings(state->getStore());
1030
1031 if (const SVal* V = B.lookup(R))
1032 return *V;
1033
1034 // Lazily derive a value for the VarRegion.
1035 const VarDecl *VD = R->getDecl();
1036
1037 if (VD == SelfDecl)
1038 return loc::MemRegionVal(getSelfRegion(0));
1039
1040 if (R->hasGlobalsOrParametersStorage())
1041 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1042
1043 return UndefinedVal();
1044}
1045
Ted Kremenek25c54572009-07-20 22:58:02 +00001046SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1047 const TypedRegion *R) {
1048
1049 QualType valTy = R->getValueType(getContext());
1050
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001051 // If the region is already cast to another type, use that type to create the
1052 // symbol value.
Ted Kremenek25c54572009-07-20 22:58:02 +00001053 if (const QualType *ty = state->get<RegionCasts>(R)) {
1054 if (const PointerType *PT = (*ty)->getAsPointerType()) {
1055 QualType castTy = PT->getPointeeType();
1056
1057 if (!IsReinterpreted(valTy, castTy, getContext()))
1058 valTy = castTy;
1059 }
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001060 }
1061
1062 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001063 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001064}
1065
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001066SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1067 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001068 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001069 assert(T->isStructureType());
1070
Zhongxing Xub7507d12009-06-11 07:27:30 +00001071 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001072 RecordDecl* RD = RT->getDecl();
1073 assert(RD->isDefinition());
1074
1075 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1076
Ted Kremenek67f28532009-06-17 22:02:04 +00001077 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1078 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001079 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001080
Douglas Gregore267ff32008-12-11 20:41:00 +00001081 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1082 FieldEnd = Fields.rend();
1083 Field != FieldEnd; ++Field) {
1084 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001085 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001086 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001087 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1088 }
1089
Zhongxing Xud91ee272009-06-23 09:02:15 +00001090 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001091}
1092
Ted Kremenek67f28532009-06-17 22:02:04 +00001093SVal RegionStoreManager::RetrieveArray(const GRState *state,
1094 const TypedRegion * R) {
1095
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001096 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001097 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1098
1099 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001100 uint64_t size = CAT->getSize().getZExtValue();
1101 for (uint64_t i = 0; i < size; ++i) {
1102 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001103 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1104 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001105 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001106 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001107 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1108 }
1109
Zhongxing Xud91ee272009-06-23 09:02:15 +00001110 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001111}
1112
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001113SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1114 const GRState *state,
1115 const TypedRegion *R,
1116 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001117 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001118 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001119
1120 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001121 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001122}
1123
Ted Kremenek9af46f52009-06-16 22:36:44 +00001124//===----------------------------------------------------------------------===//
1125// Binding values to regions.
1126//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001127
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001128Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001129 const MemRegion* R = 0;
1130
1131 if (isa<loc::MemRegionVal>(L))
1132 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001133
1134 if (R) {
1135 RegionBindingsTy B = GetRegionBindings(store);
1136 return RBFactory.Remove(B, R).getRoot();
1137 }
1138
1139 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001140}
1141
Ted Kremenek67f28532009-06-17 22:02:04 +00001142const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001143 if (isa<loc::ConcreteInt>(L))
1144 return state;
1145
Ted Kremenek9af46f52009-06-16 22:36:44 +00001146 // If we get here, the location should be a region.
1147 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001148
1149 // Check if the region is a struct region.
1150 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1151 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001152 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001153
Ted Kremenek67f28532009-06-17 22:02:04 +00001154 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001155
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001156 B = RBFactory.Add(B, R, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001157
Ted Kremenek67f28532009-06-17 22:02:04 +00001158 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001159}
1160
Ted Kremenek67f28532009-06-17 22:02:04 +00001161const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001162 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001163
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001164 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001165 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001166
Ted Kremenek0964a062009-01-21 06:57:53 +00001167 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001168 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001169 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001170 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001171
Zhongxing Xud91ee272009-06-23 09:02:15 +00001172 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001173}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001174
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001175// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001176const GRState *
1177RegionStoreManager::BindCompoundLiteral(const GRState *state,
1178 const CompoundLiteralExpr* CL,
1179 SVal V) {
1180
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001181 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001182 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001183}
1184
Ted Kremenek67f28532009-06-17 22:02:04 +00001185const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001186 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001187 SVal Init) {
1188
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001189 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001190 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001191 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001192
Ted Kremenek46537392009-07-16 01:33:37 +00001193 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001194
1195 // Check if the init expr is a StringLiteral.
1196 if (isa<loc::MemRegionVal>(Init)) {
1197 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1198 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1199 const char* str = S->getStrData();
1200 unsigned len = S->getByteLength();
1201 unsigned j = 0;
1202
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001203 // Copy bytes from the string literal into the target array. Trailing bytes
1204 // in the array that are not covered by the string literal are initialized
1205 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001206 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001207 if (j >= len)
1208 break;
1209
Ted Kremenek46537392009-07-16 01:33:37 +00001210 SVal Idx = ValMgr.makeArrayIndex(i);
1211 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1212 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001213
Zhongxing Xud91ee272009-06-23 09:02:15 +00001214 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001215 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001216 }
1217
Ted Kremenek67f28532009-06-17 22:02:04 +00001218 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001219 }
1220
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001221 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001222 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001223 uint64_t i = 0;
1224
1225 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001226 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001227 if (VI == VE)
1228 break;
1229
Ted Kremenek46537392009-07-16 01:33:37 +00001230 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001231 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001232
1233 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001234 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001235 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001236 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001237 }
1238
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001239 // If the init list is shorter than the array length, set the array default
1240 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001241 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001242 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001243 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001244 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001245 }
1246 }
1247
Ted Kremenek67f28532009-06-17 22:02:04 +00001248 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001249}
1250
Ted Kremenek67f28532009-06-17 22:02:04 +00001251const GRState *
1252RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1253 SVal V) {
1254
1255 if (!Features.supportsFields())
1256 return state;
1257
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001258 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001259 assert(T->isStructureType());
1260
Ted Kremenek35366a62009-07-17 17:50:17 +00001261 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001262 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001263
1264 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001265 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001266
Ted Kremenek67f28532009-06-17 22:02:04 +00001267 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1268 // Ignore them and kill the field values.
1269 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1270 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001271
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001272 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001273 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001274
1275 RecordDecl::field_iterator FI, FE;
1276
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001277 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001278
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001279 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001280 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001281
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001282 QualType FTy = (*FI)->getType();
1283 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1284
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001285 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001286 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001287 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001288 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001289 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001290 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001291 }
1292
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001293 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001294 if (FI != FE)
1295 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001296
Ted Kremenek67f28532009-06-17 22:02:04 +00001297 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001298}
1299
Ted Kremenek67f28532009-06-17 22:02:04 +00001300const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001301 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001302
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001303 // Set the default value of the struct region to "unknown".
1304 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001305
1306 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001307 Store store = state->getStore();
1308 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001309 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001310 const MemRegion* R = I.getKey();
1311 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1312 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001313 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001314 }
1315
Ted Kremenek67f28532009-06-17 22:02:04 +00001316 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001317}
1318
Ted Kremenek9af46f52009-06-16 22:36:44 +00001319//===----------------------------------------------------------------------===//
1320// Region views.
1321//===----------------------------------------------------------------------===//
1322
Ted Kremenek67f28532009-06-17 22:02:04 +00001323const GRState *RegionStoreManager::AddRegionView(const GRState *state,
1324 const MemRegion* View,
1325 const MemRegion* Base) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001326
1327 // First, retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001328 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001329 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001330
1331 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001332 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001333
1334 // Create a new state with the new region view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001335 return state->set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001336}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001337
Ted Kremenek67f28532009-06-17 22:02:04 +00001338const GRState *RegionStoreManager::RemoveRegionView(const GRState *state,
1339 const MemRegion* View,
1340 const MemRegion* Base) {
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001341 // Retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001342 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001343
1344 // If the base region has no view, return.
1345 if (!d)
Ted Kremenek67f28532009-06-17 22:02:04 +00001346 return state;
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001347
1348 // Remove the view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001349 return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001350}
Zhongxing Xu20794942009-05-06 08:33:50 +00001351
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001352const GRState *RegionStoreManager::setCastType(const GRState *state,
1353 const MemRegion* R, QualType T) {
Zhongxing Xu82037252009-07-14 01:12:46 +00001354 // We do not record generic cast type, since we are using cast type to
1355 // invlidate regions, and generic type is meaningless for invalidating
1356 // regions.
1357 // If the region already has a cast type before, that type is preserved.
1358 // FIXME: is this the right thing to do?
1359 if (isGenericPtr(getContext(), T))
1360 return state;
Ted Kremenek67f28532009-06-17 22:02:04 +00001361 return state->set<RegionCasts>(R, T);
Zhongxing Xu20794942009-05-06 08:33:50 +00001362}
Zhongxing Xu264e9372009-05-12 10:10:00 +00001363
Ted Kremenek67f28532009-06-17 22:02:04 +00001364const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1365 const MemRegion* R, SVal V) {
1366 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001367}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001368
1369//===----------------------------------------------------------------------===//
1370// State pruning.
1371//===----------------------------------------------------------------------===//
1372
1373static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1374 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1375 const MemRegion *R = XR->getRegion();
1376
1377 while (R) {
1378 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1379 SymReaper.markLive(SR->getSymbol());
1380 return;
1381 }
1382
1383 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1384 R = SR->getSuperRegion();
1385 continue;
1386 }
1387
1388 break;
1389 }
1390
1391 return;
1392 }
1393
1394 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1395 SymReaper.markLive(*SI);
1396}
1397
Ted Kremenek67f28532009-06-17 22:02:04 +00001398Store RegionStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001399 SymbolReaper& SymReaper,
1400 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001401{
Ted Kremenek9af46f52009-06-16 22:36:44 +00001402 Store store = state->getStore();
1403 RegionBindingsTy B = GetRegionBindings(store);
1404
1405 // Lazily constructed backmap from MemRegions to SubRegions.
1406 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1407 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1408
1409 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
1410 // the ability to reuse memory. This way we can keep TmpAlloc around as
1411 // an instance variable of RegionStoreManager (avoiding repeated malloc
1412 // overhead).
1413 llvm::BumpPtrAllocator TmpAlloc;
1414
1415 // Factory objects.
1416 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1417 SubRegionsTy::Factory SubRegF(TmpAlloc);
1418
1419 // The backmap from regions to subregions.
1420 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1421
1422 // Do a pass over the regions in the store. For VarRegions we check if
1423 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001424 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001425 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1426
1427 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1428 IntermediateRoots.push_back(I.getKey());
1429 }
1430
1431 while (!IntermediateRoots.empty()) {
1432 const MemRegion* R = IntermediateRoots.back();
1433 IntermediateRoots.pop_back();
1434
1435 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001436 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001437 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001438 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001439 }
1440 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1441 if (SymReaper.isLive(SR->getSymbol()))
1442 RegionRoots.push_back(SR);
1443 }
1444 else {
1445 // Get the super region for R.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001446 const MemRegion* superR = cast<SubRegion>(R)->getSuperRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001447
1448 // Get the current set of subregions for SuperR.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001449 const SubRegionsTy* SRptr = SubRegMap.lookup(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001450 SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
1451
1452 // Add R to the subregions of SuperR.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001453 SubRegMap = SubRegMapF.Add(SubRegMap, superR, SubRegF.Add(SRs, R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001454
1455 // Super region may be VarRegion or subregion of another VarRegion. Add it
1456 // to the work list.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001457 if (isa<SubRegion>(superR))
1458 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001459 }
1460 }
1461
1462 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1463 // of the store. We want to find all live symbols and dead regions.
1464 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1465
1466 while (!RegionRoots.empty()) {
1467 // Dequeue the next region on the worklist.
1468 const MemRegion* R = RegionRoots.back();
1469 RegionRoots.pop_back();
1470
1471 // Check if we have already processed this region.
1472 if (Marked.count(R)) continue;
1473
1474 // Mark this region as processed. This is needed for termination in case
1475 // a region is referenced more than once.
1476 Marked.insert(R);
1477
1478 // Mark the symbol for any live SymbolicRegion as "live". This means we
1479 // should continue to track that symbol.
1480 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1481 SymReaper.markLive(SymR->getSymbol());
1482
1483 // Get the data binding for R (if any).
1484 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1485 if (Xptr) {
1486 SVal X = *Xptr;
1487 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1488
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001489 // If X is a region, then add it to the RegionRoots.
1490 if (const MemRegion *RX = X.getAsRegion()) {
1491 RegionRoots.push_back(RX);
1492
1493 // Mark the super region of the RX as live.
1494 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1495 // 'y' => element region. 'x' is its super region.
1496 // We only add one level super region for now.
Zhongxing Xu5ff8e8c2009-07-01 02:12:57 +00001497 // FIXME: maybe multiple level of super regions should be added.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001498 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1499 RegionRoots.push_back(SR->getSuperRegion());
1500 }
1501 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001502 }
1503
1504 // Get the subregions of R. These are RegionRoots as well since they
1505 // represent values that are also bound to R.
1506 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1507 if (!SRptr) continue;
1508 SubRegionsTy SR = *SRptr;
1509
1510 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1511 RegionRoots.push_back(*I);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001512
Ted Kremenek9af46f52009-06-16 22:36:44 +00001513 }
1514
1515 // We have now scanned the store, marking reachable regions and symbols
1516 // as live. We now remove all the regions that are dead from the store
1517 // as well as update DSymbols with the set symbols that are now dead.
1518 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1519 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001520 // If this region live? Is so, none of its symbols are dead.
1521 if (Marked.count(R))
1522 continue;
1523
1524 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001525 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001526
1527 // Mark all non-live symbols that this region references as dead.
1528 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1529 SymReaper.maybeDead(SymR->getSymbol());
1530
1531 SVal X = I.getData();
1532 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1533 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
1534 }
1535
1536 return store;
1537}
1538
1539//===----------------------------------------------------------------------===//
1540// Utility methods.
1541//===----------------------------------------------------------------------===//
1542
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001543void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001544 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001545 RegionBindingsTy B = GetRegionBindings(store);
1546 OS << "Store:" << nl;
1547
Ted Kremenek6f9b3a42009-07-13 23:53:06 +00001548 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
1549 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001550}