blob: 1e95f174b5ac76d658543cef49dd7e4d7d29bc5c [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 Xu17892752008-10-08 02:50:44 +0000330 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000331 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000332 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000333
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000334 void print(Store store, llvm::raw_ostream& Out, const char* nl,
335 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000336
337 void iterBindings(Store store, BindingsHandler& f) {
338 // FIXME: Implement.
339 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000340
Ted Kremenek67f28532009-06-17 22:02:04 +0000341 // FIXME: Remove.
342 BasicValueFactory& getBasicVals() {
343 return StateMgr.getBasicVals();
344 }
345
346 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000347 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000348};
349
350} // end anonymous namespace
351
Ted Kremenek9af46f52009-06-16 22:36:44 +0000352//===----------------------------------------------------------------------===//
353// RegionStore creation.
354//===----------------------------------------------------------------------===//
355
356StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
357 RegionStoreFeatures F = maximal_features_tag();
358 return new RegionStoreManager(StMgr, F);
359}
360
361StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
362 RegionStoreFeatures F = minimal_features_tag();
363 F.enableFields(true);
364 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000365}
366
Ted Kremenek14453bf2009-03-03 19:02:42 +0000367SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000368 RegionBindingsTy B = GetRegionBindings(state->getStore());
369 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
370
371 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
372 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
373 M->add(R->getSuperRegion(), R);
374 }
375
Ted Kremenek14453bf2009-03-03 19:02:42 +0000376 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000377}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000378
Ted Kremenek9af46f52009-06-16 22:36:44 +0000379//===----------------------------------------------------------------------===//
380// getLValueXXX methods.
381//===----------------------------------------------------------------------===//
382
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000383/// getLValueString - Returns an SVal representing the lvalue of a
384/// StringLiteral. Within RegionStore a StringLiteral has an
385/// associated StringRegion, and the lvalue of a StringLiteral is the
386/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000387SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000388 const StringLiteral* S) {
389 return loc::MemRegionVal(MRMgr.getStringRegion(S));
390}
391
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000392/// getLValueVar - Returns an SVal that represents the lvalue of a
393/// variable. Within RegionStore a variable has an associated
394/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000395SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000396 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
397}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000398
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000399/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
400/// of a compound literal. Within RegionStore a compound literal
401/// has an associated region, and the lvalue of the compound literal
402/// is the lvalue of that region.
403SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000404RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000405 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000406 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
407}
408
Ted Kremenek67f28532009-06-17 22:02:04 +0000409SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000410 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000411 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000412}
413
Ted Kremenek67f28532009-06-17 22:02:04 +0000414SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000415 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000416 return getLValueFieldOrIvar(St, Base, D);
417}
418
Ted Kremenek67f28532009-06-17 22:02:04 +0000419SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000420 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000421 if (Base.isUnknownOrUndef())
422 return Base;
423
424 Loc BaseL = cast<Loc>(Base);
425 const MemRegion* BaseR = 0;
426
427 switch (BaseL.getSubKind()) {
428 case loc::MemRegionKind:
429 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
430 break;
431
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000432 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000433 // These are anormal cases. Flag an undefined value.
434 return UndefinedVal();
435
436 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000437 // While these seem funny, this can happen through casts.
438 // FIXME: What we should return is the field offset. For example,
439 // add the field offset to the integer value. That way funny things
440 // like this work properly: &(((struct foo *) 0xa)->f)
441 return Base;
442
443 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000444 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000445 return Base;
446 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000447
448 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
449 // of FieldDecl.
450 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
451 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000452
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000453 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000454}
455
Ted Kremenek67f28532009-06-17 22:02:04 +0000456SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000457 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000458 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000459
Ted Kremenekde7ec632009-03-09 22:44:49 +0000460 // If the base is an unknown or undefined value, just return it back.
461 // FIXME: For absolute pointer addresses, we just return that value back as
462 // well, although in reality we should return the offset added to that
463 // value.
464 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000465 return Base;
466
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000467 // Only handle integer offsets... for now.
468 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000469 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000470
Zhongxing Xuce760782009-05-09 13:20:07 +0000471 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000472
473 // Pointer of any type can be cast and used as array base.
474 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
475
476 if (!ElemR) {
477 //
478 // If the base region is not an ElementRegion, create one.
479 // This can happen in the following example:
480 //
481 // char *p = __builtin_alloc(10);
482 // p[1] = 8;
483 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000484 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000485 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000486
487 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
488 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
489 const llvm::APSInt& OffI = CI->getValue();
490 if (OffI.isUnsigned()) {
491 llvm::APSInt Tmp = OffI;
492 Tmp.setIsSigned(true);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000493 Offset = ValMgr.makeIntVal(Tmp);
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000494 }
495 }
Ted Kremenekf936f452009-05-04 06:18:28 +0000496 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000497 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000498 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000499
500 SVal BaseIdx = ElemR->getIndex();
501
502 if (!isa<nonloc::ConcreteInt>(BaseIdx))
503 return UnknownVal();
504
505 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
506 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
507 assert(BaseIdxI.isSigned());
508
509 // FIXME: This appears to be the assumption of this code. We should review
510 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
511 // can't we need to put a comment here. If it can, we should handle it.
512 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000513
Zhongxing Xuce760782009-05-09 13:20:07 +0000514 const MemRegion *ArrayR = ElemR->getSuperRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000515 SVal NewIdx;
516
517 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
518 // 'Offset' might be unsigned. We have to convert it to signed and
519 // possibly extend it.
520 llvm::APSInt Tmp = OffI;
521
522 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
523 Tmp.extend(BaseIdxI.getBitWidth());
524
525 Tmp.setIsSigned(true);
526 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000527 NewIdx = ValMgr.makeIntVal(Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000528 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000529 else
530 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000531
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000532 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
533 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000534}
535
Ted Kremenek9af46f52009-06-16 22:36:44 +0000536//===----------------------------------------------------------------------===//
537// Extents for regions.
538//===----------------------------------------------------------------------===//
539
Ted Kremenek67f28532009-06-17 22:02:04 +0000540SVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000541 const MemRegion* R) {
542 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
543 // Get the type of the variable.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000544 QualType T = VR->getDesugaredValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000545
Ted Kremenek14553ab2009-01-30 00:08:43 +0000546 // FIXME: Handle variable-length arrays.
547 if (isa<VariableArrayType>(T))
548 return UnknownVal();
549
550 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
551 // return the size as signed integer.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000552 return ValMgr.makeIntVal(CAT->getSize(), false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000553 }
Zhongxing Xu20794942009-05-06 08:33:50 +0000554
Ted Kremenek67f28532009-06-17 22:02:04 +0000555 const QualType* CastTy = state->get<RegionCasts>(VR);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000556
Zhongxing Xue6536af2009-05-08 02:00:55 +0000557 // If the VarRegion is cast to other type, compute the size with respect to
558 // that type.
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000559 if (CastTy) {
560 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000561 QualType VarTy = VR->getValueType(getContext());
Zhongxing Xue6536af2009-05-08 02:00:55 +0000562 uint64_t EleSize = getContext().getTypeSize(EleTy);
563 uint64_t VarSize = getContext().getTypeSize(VarTy);
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000564 assert(VarSize != 0);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000565 return ValMgr.makeIntVal(VarSize/EleSize, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000566 }
567
Ted Kremenek14553ab2009-01-30 00:08:43 +0000568 // Clients can use ordinary variables as if they were arrays. These
569 // essentially are arrays of size 1.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000570 return ValMgr.makeIntVal(1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000571 }
572
573 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000574 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000575 // We intentionally made the size value signed because it participates in
576 // operations with signed indices.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000577 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000578 }
579
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000580 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
581 // FIXME: Unsupported yet.
582 FR = 0;
583 return UnknownVal();
584 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000585
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000586 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000587 return UnknownVal();
588 }
589
Zhongxing Xuce760782009-05-09 13:20:07 +0000590 if (isa<AllocaRegion>(R)) {
591 return UnknownVal();
592 }
593
Zhongxing Xuccb16162009-05-06 08:08:27 +0000594 if (isa<ElementRegion>(R)) {
595 return UnknownVal();
596 }
597
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000598 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000599 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000600}
601
Ted Kremenek67f28532009-06-17 22:02:04 +0000602const GRState *RegionStoreManager::setExtent(const GRState *state,
603 const MemRegion *region,
604 SVal extent) {
605 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000606}
607
608//===----------------------------------------------------------------------===//
609// Location and region casting.
610//===----------------------------------------------------------------------===//
611
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000612/// ArrayToPointer - Emulates the "decay" of an array to a pointer
613/// type. 'Array' represents the lvalue of the array being decayed
614/// to a pointer, and the returned SVal represents the decayed
615/// version of that lvalue (i.e., a pointer to the first element of
616/// the array). This is called by GRExprEngine when evaluating casts
617/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000618SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000619 if (!isa<loc::MemRegionVal>(Array))
620 return UnknownVal();
621
622 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
623 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
624
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000625 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000626 return UnknownVal();
627
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000628 // Strip off typedefs from the ArrayRegion's ValueType.
629 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000630 ArrayType *AT = cast<ArrayType>(T);
631 T = AT->getElementType();
632
Zhongxing Xu63123d82008-11-23 04:30:35 +0000633 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000634 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000635
636 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000637}
638
Ted Kremenek9af46f52009-06-16 22:36:44 +0000639//===----------------------------------------------------------------------===//
640// Pointer arithmetic.
641//===----------------------------------------------------------------------===//
642
Zhongxing Xu262fd032009-05-20 09:00:16 +0000643SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000644 BinaryOperator::Opcode Op, Loc L, NonLoc R,
645 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000646 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000647 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000648 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000649
Zhongxing Xua1718c72009-04-03 07:33:13 +0000650 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000651 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000652
653 // If the operand is a symbolic or alloca region, create the first element
654 // region on it.
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000655 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR)) {
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000656 QualType T;
657 // If the SymbolicRegion was cast to another type, use that type.
658 if (const QualType *t = state->get<RegionCasts>(SR)) {
659 T = *t;
660 } else {
661 // Otherwise use the symbol's type.
662 SymbolRef Sym = SR->getSymbol();
663 T = Sym->getType(getContext());
664 }
Zhongxing Xu53454dc2009-06-12 03:59:12 +0000665 QualType EleTy = T->getAsPointerType()->getPointeeType();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000666
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000667 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000668 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Zhongxing Xu262fd032009-05-20 09:00:16 +0000669 }
670 else if (const AllocaRegion *AR = dyn_cast<AllocaRegion>(MR)) {
671 // Get the alloca region's current cast type.
Zhongxing Xu262fd032009-05-20 09:00:16 +0000672
Ted Kremenek67f28532009-06-17 22:02:04 +0000673
674 GRStateTrait<RegionCasts>::lookup_type T = state->get<RegionCasts>(AR);
Zhongxing Xu262fd032009-05-20 09:00:16 +0000675 assert(T && "alloca region has no type.");
676 QualType EleTy = cast<PointerType>(T->getTypePtr())->getPointeeType();
677 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000678 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Zhongxing Xu262fd032009-05-20 09:00:16 +0000679 }
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000680 else if (isa<FieldRegion>(MR)) {
681 // Not track pointer arithmetic on struct fields.
682 return UnknownVal();
683 }
684 else {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000685 ER = cast<ElementRegion>(MR);
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000686 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000687
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000688 SVal Idx = ER->getIndex();
689
690 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
691 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
692
693 // Only support concrete integer indexes for now.
694 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000695 // FIXME: For now, convert the signedness and bitwidth of offset in case
696 // they don't match. This can result from pointer arithmetic. In reality,
697 // we should figure out what are the proper semantics and implement them.
698 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000699 // This addresses the test case test/Analysis/ptr-arith.c
700 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000701 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
702 Offset->getValue()));
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000703 SVal NewIdx = Base->evalBinOp(ValMgr, Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000704 const MemRegion* NewER =
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000705 MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion(),
706 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000707 return ValMgr.makeLoc(NewER);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000708
Ted Kremenek5dc27462009-03-03 02:51:43 +0000709 }
710
711 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000712}
713
Ted Kremenek9af46f52009-06-16 22:36:44 +0000714//===----------------------------------------------------------------------===//
715// Loading values from regions.
716//===----------------------------------------------------------------------===//
717
Ted Kremenek67f28532009-06-17 22:02:04 +0000718SVal RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
719
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000720 assert(!isa<UnknownVal>(L) && "location unknown");
721 assert(!isa<UndefinedVal>(L) && "location undefined");
722
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000723 // FIXME: Is this even possible? Shouldn't this be treated as a null
724 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000725 if (isa<loc::ConcreteInt>(L))
726 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000727
Ted Kremenek67f28532009-06-17 22:02:04 +0000728 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000729
Zhongxing Xu91844122009-05-20 09:18:48 +0000730 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000731 // Example:
732 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000733 // char* p = alloca();
734 // read(p);
735 // c = *p;
736 if (isa<SymbolicRegion>(MR) || isa<AllocaRegion>(MR))
Zhongxing Xua1718c72009-04-03 07:33:13 +0000737 return UnknownVal();
738
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000739 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
740 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000741 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000742 assert(R && "bad region");
743
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000744 // FIXME: We should eventually handle funny addressing. e.g.:
745 //
746 // int x = ...;
747 // int *p = &x;
748 // char *q = (char*) p;
749 // char c = *q; // returns the first byte of 'x'.
750 //
751 // Such funny addressing will occur due to layering of regions.
752
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000753 QualType RTy = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000754
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000755 if (RTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000756 return RetrieveStruct(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000757
758 if (RTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000759 return RetrieveArray(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000760
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000761 // FIXME: handle Vector types.
762 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000763 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +0000764
765 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
766 return RetrieveField(state, FR);
767
768 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
769 return RetrieveElement(state, ER);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000770
Ted Kremenek67f28532009-06-17 22:02:04 +0000771 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000772 RegionBindingsTy::data_type* V = B.lookup(R);
773
774 // Check if the region has a binding.
775 if (V)
776 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000777
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000778 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
779 const MemRegion *SR = IVR->getSuperRegion();
780
781 // If the super region is 'self' then return the symbol representing
782 // the value of the ivar upon entry to the method.
783 if (SR == SelfRegion) {
784 // FIXME: Do we need to handle the case where the super region
785 // has a view? We want to canonicalize the bindings.
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000786 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000787 }
788
789 // Otherwise, we need a new symbol. For now return Unknown.
790 return UnknownVal();
791 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000792
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000793 // The location does not have a bound value. This means that it has
794 // the value it had upon its creation and/or entry to the analyzed
795 // function/method. These are either symbolic values or 'undefined'.
796
797 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000798 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
799 const VarDecl *VD = VR->getDecl();
800
Ted Kremenekcec35252009-03-04 00:23:05 +0000801 if (VD == SelfDecl)
802 return loc::MemRegionVal(getSelfRegion(0));
803
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000804 if (VR->hasGlobalsOrParametersStorage())
805 return ValMgr.getRegionValueSymbolValOrUnknown(VR, VD->getType());
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000806 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000807
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000808 if (R->hasHeapOrStackStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000809 // All stack variables are considered to have undefined values
810 // upon creation. All heap allocated blocks are considered to
811 // have undefined values as well unless they are explicitly bound
812 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000813 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000814 }
815
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000816 // If the region is already cast to another type, use that type to create the
817 // symbol value.
818 if (const QualType *p = state->get<RegionCasts>(R)) {
819 QualType T = *p;
820 RTy = T->getAsPointerType()->getPointeeType();
821 }
822
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000823 // All other values are symbolic.
824 return ValMgr.getRegionValueSymbolValOrUnknown(R, RTy);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000825}
826
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000827SVal RegionStoreManager::RetrieveElement(const GRState* state,
828 const ElementRegion* R) {
829 // Check if the region has a binding.
830 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +0000831 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000832 return *V;
833
Ted Kremenek921109a2009-07-01 23:19:52 +0000834 const MemRegion* superR = R->getSuperRegion();
835
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000836 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +0000837 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000838 const StringLiteral *Str = StrR->getStringLiteral();
839 SVal Idx = R->getIndex();
840 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
841 int64_t i = CI->getValue().getSExtValue();
842 char c;
843 if (i == Str->getByteLength())
844 c = '\0';
845 else
846 c = Str->getStrData()[i];
847 return ValMgr.makeIntVal(c, getContext().CharTy);
848 }
849 }
850
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000851 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +0000852 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000853 if (D->hasConjuredSymbol())
854 return ValMgr.getRegionValueSymbolVal(R);
855 else
856 return *D;
857 }
858
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000859 // Check if the super region has a binding.
Ted Kremenek921109a2009-07-01 23:19:52 +0000860 if (B.lookup(superR)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000861 // We do not extract the bit value from super region for now.
Zhongxing Xu8834af32009-07-03 06:11:41 +0000862 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000863 }
Ted Kremenek921109a2009-07-01 23:19:52 +0000864
865 if (R->hasHeapStorage()) {
866 // FIXME: If the region has heap storage and we know nothing special
867 // about its bindings, should we instead return UnknownVal? Seems like
868 // we should only return UndefinedVal in the cases where we know the value
869 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000870 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +0000871 }
872
Ted Kremenekdc147262009-07-02 22:02:15 +0000873 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +0000874 // Currently we don't reason specially about Clang-style vectors. Check
875 // if superR is a vector and if so return Unknown.
876 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
877 if (typedSuperR->getValueType(getContext())->isVectorType())
878 return UnknownVal();
879 }
880
881 return UndefinedVal();
882 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000883
884 QualType Ty = R->getValueType(getContext());
885
886 // If the region is already cast to another type, use that type to create the
887 // symbol value.
888 if (const QualType *p = state->get<RegionCasts>(R))
889 Ty = (*p)->getAsPointerType()->getPointeeType();
890
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000891 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000892}
893
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000894SVal RegionStoreManager::RetrieveField(const GRState* state,
895 const FieldRegion* R) {
896 QualType Ty = R->getValueType(getContext());
897
898 // Check if the region has a binding.
899 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +0000900 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000901 return *V;
902
Ted Kremenek8b2ba312009-07-01 23:30:34 +0000903 const MemRegion* superR = R->getSuperRegion();
904 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000905 if (D->hasConjuredSymbol())
906 return ValMgr.getRegionValueSymbolVal(R);
907
908 if (D->isZeroConstant())
909 return ValMgr.makeZeroVal(Ty);
910
911 if (D->isUnknown())
912 return *D;
913
914 assert(0 && "Unknown default value");
915 }
916
Ted Kremenekdc147262009-07-02 22:02:15 +0000917 // FIXME: Is this correct? Should it be UnknownVal?
918 if (R->hasHeapStorage())
919 return UndefinedVal();
920
921 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000922 return UndefinedVal();
923
924 // If the region is already cast to another type, use that type to create the
925 // symbol value.
926 if (const QualType *p = state->get<RegionCasts>(R)) {
927 QualType tmp = *p;
928 Ty = tmp->getAsPointerType()->getPointeeType();
929 }
930
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000931 // All other values are symbolic.
932 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000933}
934
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000935SVal RegionStoreManager::RetrieveStruct(const GRState *state,
936 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000937 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000938 assert(T->isStructureType());
939
Zhongxing Xub7507d12009-06-11 07:27:30 +0000940 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000941 RecordDecl* RD = RT->getDecl();
942 assert(RD->isDefinition());
943
944 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
945
Ted Kremenek67f28532009-06-17 22:02:04 +0000946 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
947 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000948 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000949
Douglas Gregore267ff32008-12-11 20:41:00 +0000950 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
951 FieldEnd = Fields.rend();
952 Field != FieldEnd; ++Field) {
953 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000954 QualType FTy = (*Field)->getType();
Ted Kremenek67f28532009-06-17 22:02:04 +0000955 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000956 StructVal = getBasicVals().consVals(FieldValue, StructVal);
957 }
958
Zhongxing Xud91ee272009-06-23 09:02:15 +0000959 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000960}
961
Ted Kremenek67f28532009-06-17 22:02:04 +0000962SVal RegionStoreManager::RetrieveArray(const GRState *state,
963 const TypedRegion * R) {
964
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000965 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000966 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
967
968 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000969 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu2ee52142009-05-11 12:48:56 +0000970 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000971
972 for (; i < Size; ++i) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000973 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000974 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
975 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +0000976 QualType ETy = ER->getElementType();
Ted Kremenek67f28532009-06-17 22:02:04 +0000977 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000978 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
979 }
980
Zhongxing Xud91ee272009-06-23 09:02:15 +0000981 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000982}
983
Ted Kremenek9af46f52009-06-16 22:36:44 +0000984//===----------------------------------------------------------------------===//
985// Binding values to regions.
986//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +0000987
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000988Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000989 const MemRegion* R = 0;
990
991 if (isa<loc::MemRegionVal>(L))
992 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +0000993
994 if (R) {
995 RegionBindingsTy B = GetRegionBindings(store);
996 return RBFactory.Remove(B, R).getRoot();
997 }
998
999 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001000}
1001
Ted Kremenek67f28532009-06-17 22:02:04 +00001002const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001003 if (isa<loc::ConcreteInt>(L))
1004 return state;
1005
Ted Kremenek9af46f52009-06-16 22:36:44 +00001006 // If we get here, the location should be a region.
1007 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001008
1009 // Check if the region is a struct region.
1010 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1011 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001012 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001013
Ted Kremenek67f28532009-06-17 22:02:04 +00001014 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001015
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001016 B = RBFactory.Add(B, R, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001017
Ted Kremenek67f28532009-06-17 22:02:04 +00001018 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001019}
1020
Ted Kremenek67f28532009-06-17 22:02:04 +00001021const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001022 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001023
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001024 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001025 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001026
Ted Kremenek0964a062009-01-21 06:57:53 +00001027 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001028 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001029 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001030 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001031
Zhongxing Xud91ee272009-06-23 09:02:15 +00001032 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001033}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001034
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001035// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001036const GRState *
1037RegionStoreManager::BindCompoundLiteral(const GRState *state,
1038 const CompoundLiteralExpr* CL,
1039 SVal V) {
1040
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001041 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001042 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001043}
1044
Ted Kremenek67f28532009-06-17 22:02:04 +00001045const GRState *RegionStoreManager::BindArray(const GRState *state,
1046 const TypedRegion* R,
1047 SVal Init) {
1048
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001049 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001050 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001051 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001052
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001053 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001054 llvm::APSInt i(llvm::APInt::getNullValue(Size.getBitWidth()), false);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001055
1056 // Check if the init expr is a StringLiteral.
1057 if (isa<loc::MemRegionVal>(Init)) {
1058 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1059 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1060 const char* str = S->getStrData();
1061 unsigned len = S->getByteLength();
1062 unsigned j = 0;
1063
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001064 // Copy bytes from the string literal into the target array. Trailing bytes
1065 // in the array that are not covered by the string literal are initialized
1066 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001067 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001068 if (j >= len)
1069 break;
1070
Zhongxing Xu3038c5a2009-06-23 06:13:19 +00001071 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001072 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx,R,getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001073
Zhongxing Xud91ee272009-06-23 09:02:15 +00001074 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001075 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001076 }
1077
Ted Kremenek67f28532009-06-17 22:02:04 +00001078 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001079 }
1080
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001081 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001082 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1083
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001084 for (; i < Size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001085 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001086 if (VI == VE)
1087 break;
1088
Zhongxing Xu3038c5a2009-06-23 06:13:19 +00001089 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001090 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001091
1092 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001093 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001094 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001095 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001096 }
1097
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001098 // If the init list is shorter than the array length, set the array default
1099 // value.
1100 if (i < Size) {
1101 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001102 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001103 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001104 }
1105 }
1106
Ted Kremenek67f28532009-06-17 22:02:04 +00001107 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001108}
1109
Ted Kremenek67f28532009-06-17 22:02:04 +00001110const GRState *
1111RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1112 SVal V) {
1113
1114 if (!Features.supportsFields())
1115 return state;
1116
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001117 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001118 assert(T->isStructureType());
1119
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001120 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001121 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001122
1123 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001124 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001125
Ted Kremenek67f28532009-06-17 22:02:04 +00001126 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1127 // Ignore them and kill the field values.
1128 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1129 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001130
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001131 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001132 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001133
1134 RecordDecl::field_iterator FI, FE;
1135
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001136 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001137
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001138 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001139 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001140
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001141 QualType FTy = (*FI)->getType();
1142 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1143
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001144 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001145 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001146 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001147 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001148 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001149 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001150 }
1151
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001152 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001153 if (FI != FE)
1154 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001155
Ted Kremenek67f28532009-06-17 22:02:04 +00001156 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001157}
1158
Ted Kremenek67f28532009-06-17 22:02:04 +00001159const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001160 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001161
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001162 // Set the default value of the struct region to "unknown".
1163 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001164
1165 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001166 Store store = state->getStore();
1167 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001168 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001169 const MemRegion* R = I.getKey();
1170 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1171 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001172 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001173 }
1174
Ted Kremenek67f28532009-06-17 22:02:04 +00001175 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001176}
1177
Ted Kremenek9af46f52009-06-16 22:36:44 +00001178//===----------------------------------------------------------------------===//
1179// Region views.
1180//===----------------------------------------------------------------------===//
1181
Ted Kremenek67f28532009-06-17 22:02:04 +00001182const GRState *RegionStoreManager::AddRegionView(const GRState *state,
1183 const MemRegion* View,
1184 const MemRegion* Base) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001185
1186 // First, retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001187 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001188 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001189
1190 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001191 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001192
1193 // Create a new state with the new region view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001194 return state->set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001195}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001196
Ted Kremenek67f28532009-06-17 22:02:04 +00001197const GRState *RegionStoreManager::RemoveRegionView(const GRState *state,
1198 const MemRegion* View,
1199 const MemRegion* Base) {
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001200 // Retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001201 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001202
1203 // If the base region has no view, return.
1204 if (!d)
Ted Kremenek67f28532009-06-17 22:02:04 +00001205 return state;
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001206
1207 // Remove the view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001208 return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001209}
Zhongxing Xu20794942009-05-06 08:33:50 +00001210
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001211const GRState *RegionStoreManager::setCastType(const GRState *state,
1212 const MemRegion* R, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001213 return state->set<RegionCasts>(R, T);
Zhongxing Xu20794942009-05-06 08:33:50 +00001214}
Zhongxing Xu264e9372009-05-12 10:10:00 +00001215
Ted Kremenek67f28532009-06-17 22:02:04 +00001216const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1217 const MemRegion* R, SVal V) {
1218 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001219}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001220
1221//===----------------------------------------------------------------------===//
1222// State pruning.
1223//===----------------------------------------------------------------------===//
1224
1225static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1226 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1227 const MemRegion *R = XR->getRegion();
1228
1229 while (R) {
1230 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1231 SymReaper.markLive(SR->getSymbol());
1232 return;
1233 }
1234
1235 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1236 R = SR->getSuperRegion();
1237 continue;
1238 }
1239
1240 break;
1241 }
1242
1243 return;
1244 }
1245
1246 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1247 SymReaper.markLive(*SI);
1248}
1249
Ted Kremenek67f28532009-06-17 22:02:04 +00001250Store RegionStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001251 SymbolReaper& SymReaper,
1252 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001253{
Ted Kremenek9af46f52009-06-16 22:36:44 +00001254 Store store = state->getStore();
1255 RegionBindingsTy B = GetRegionBindings(store);
1256
1257 // Lazily constructed backmap from MemRegions to SubRegions.
1258 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1259 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1260
1261 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
1262 // the ability to reuse memory. This way we can keep TmpAlloc around as
1263 // an instance variable of RegionStoreManager (avoiding repeated malloc
1264 // overhead).
1265 llvm::BumpPtrAllocator TmpAlloc;
1266
1267 // Factory objects.
1268 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1269 SubRegionsTy::Factory SubRegF(TmpAlloc);
1270
1271 // The backmap from regions to subregions.
1272 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1273
1274 // Do a pass over the regions in the store. For VarRegions we check if
1275 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001276 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001277 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1278
1279 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1280 IntermediateRoots.push_back(I.getKey());
1281 }
1282
1283 while (!IntermediateRoots.empty()) {
1284 const MemRegion* R = IntermediateRoots.back();
1285 IntermediateRoots.pop_back();
1286
1287 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001288 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001289 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001290 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001291 }
1292 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1293 if (SymReaper.isLive(SR->getSymbol()))
1294 RegionRoots.push_back(SR);
1295 }
1296 else {
1297 // Get the super region for R.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001298 const MemRegion* superR = cast<SubRegion>(R)->getSuperRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001299
1300 // Get the current set of subregions for SuperR.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001301 const SubRegionsTy* SRptr = SubRegMap.lookup(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001302 SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
1303
1304 // Add R to the subregions of SuperR.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001305 SubRegMap = SubRegMapF.Add(SubRegMap, superR, SubRegF.Add(SRs, R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001306
1307 // Super region may be VarRegion or subregion of another VarRegion. Add it
1308 // to the work list.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001309 if (isa<SubRegion>(superR))
1310 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001311 }
1312 }
1313
1314 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1315 // of the store. We want to find all live symbols and dead regions.
1316 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1317
1318 while (!RegionRoots.empty()) {
1319 // Dequeue the next region on the worklist.
1320 const MemRegion* R = RegionRoots.back();
1321 RegionRoots.pop_back();
1322
1323 // Check if we have already processed this region.
1324 if (Marked.count(R)) continue;
1325
1326 // Mark this region as processed. This is needed for termination in case
1327 // a region is referenced more than once.
1328 Marked.insert(R);
1329
1330 // Mark the symbol for any live SymbolicRegion as "live". This means we
1331 // should continue to track that symbol.
1332 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1333 SymReaper.markLive(SymR->getSymbol());
1334
1335 // Get the data binding for R (if any).
1336 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1337 if (Xptr) {
1338 SVal X = *Xptr;
1339 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1340
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001341 // If X is a region, then add it to the RegionRoots.
1342 if (const MemRegion *RX = X.getAsRegion()) {
1343 RegionRoots.push_back(RX);
1344
1345 // Mark the super region of the RX as live.
1346 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1347 // 'y' => element region. 'x' is its super region.
1348 // We only add one level super region for now.
Zhongxing Xu5ff8e8c2009-07-01 02:12:57 +00001349 // FIXME: maybe multiple level of super regions should be added.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001350 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1351 RegionRoots.push_back(SR->getSuperRegion());
1352 }
1353 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001354 }
1355
1356 // Get the subregions of R. These are RegionRoots as well since they
1357 // represent values that are also bound to R.
1358 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1359 if (!SRptr) continue;
1360 SubRegionsTy SR = *SRptr;
1361
1362 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1363 RegionRoots.push_back(*I);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001364
Ted Kremenek9af46f52009-06-16 22:36:44 +00001365 }
1366
1367 // We have now scanned the store, marking reachable regions and symbols
1368 // as live. We now remove all the regions that are dead from the store
1369 // as well as update DSymbols with the set symbols that are now dead.
1370 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1371 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001372 // If this region live? Is so, none of its symbols are dead.
1373 if (Marked.count(R))
1374 continue;
1375
1376 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001377 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001378
1379 // Mark all non-live symbols that this region references as dead.
1380 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1381 SymReaper.maybeDead(SymR->getSymbol());
1382
1383 SVal X = I.getData();
1384 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1385 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
1386 }
1387
1388 return store;
1389}
1390
1391//===----------------------------------------------------------------------===//
1392// Utility methods.
1393//===----------------------------------------------------------------------===//
1394
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001395void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001396 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001397 RegionBindingsTy B = GetRegionBindings(store);
1398 OS << "Store:" << nl;
1399
1400 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1401 OS << ' '; I.getKey()->print(OS); OS << " : ";
1402 I.getData().print(OS); OS << nl;
1403 }
1404}