blob: 6f316c9c5921a114bfa7893558263638a3e4d685 [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//===----------------------------------------------------------------------===//
106// Region "killsets".
107//===----------------------------------------------------------------------===//
108//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000109// RegionStore lazily adds value bindings to regions when the analyzer handles
110// assignment statements. Killsets track which default values have been
111// killed, thus distinguishing between "unknown" values and default
112// values. Regions are added to killset only when they are assigned "unknown"
113// directly, otherwise we should have their value in the region bindings.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000114//
115namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000116static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000117namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000118 template<> struct GRStateTrait<RegionKills>
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000119 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000120 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000121 };
122}
123
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000124//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000125// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000126//===----------------------------------------------------------------------===//
127//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000128// This GDM entry tracks what regions have a default value if they have no bound
129// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000130//
131namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
132static int RegionDefaultValueIndex = 0;
133namespace clang {
134 template<> struct GRStateTrait<RegionDefaultValue>
135 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
136 static void* GDMIndex() { return &RegionDefaultValueIndex; }
137 };
138}
139
140//===----------------------------------------------------------------------===//
141// Main RegionStore logic.
142//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000143
Zhongxing Xu17892752008-10-08 02:50:44 +0000144namespace {
145
Ted Kremenek59e8f112009-03-03 01:35:36 +0000146class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
147 typedef llvm::DenseMap<const MemRegion*,
148 llvm::ImmutableSet<const MemRegion*> > Map;
149
150 llvm::ImmutableSet<const MemRegion*>::Factory F;
151 Map M;
152
153public:
154 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
155 Map::iterator I = M.find(Parent);
156 M.insert(std::make_pair(Parent,
157 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
158 }
159
160 ~RegionStoreSubRegionMap() {}
161
Ted Kremenek5dc27462009-03-03 02:51:43 +0000162 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000163 Map::iterator I = M.find(Parent);
164
165 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000166 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000167
168 llvm::ImmutableSet<const MemRegion*> S = I->second;
169 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
170 SI != SE; ++SI) {
171 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000172 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000173 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000174
175 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000176 }
177};
178
Zhongxing Xu17892752008-10-08 02:50:44 +0000179class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000180 const RegionStoreFeatures Features;
Zhongxing Xu17892752008-10-08 02:50:44 +0000181 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000182 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000183
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000184 const MemRegion* SelfRegion;
185 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000186
187public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000188 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000189 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000190 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000191 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000192 RVFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000193 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000194 if (const ObjCMethodDecl* MD =
195 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
196 SelfDecl = MD->getSelfDecl();
197 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000198
199 virtual ~RegionStoreManager() {}
200
Ted Kremenek14453bf2009-03-03 19:02:42 +0000201 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000202
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000203 /// getLValueString - Returns an SVal representing the lvalue of a
204 /// StringLiteral. Within RegionStore a StringLiteral has an
205 /// associated StringRegion, and the lvalue of a StringLiteral is
206 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000207 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000208
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000209 /// getLValueCompoundLiteral - Returns an SVal representing the
210 /// lvalue of a compound literal. Within RegionStore a compound
211 /// literal has an associated region, and the lvalue of the
212 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000213 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000214
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000215 /// getLValueVar - Returns an SVal that represents the lvalue of a
216 /// variable. Within RegionStore a variable has an associated
217 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000218 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000219
Ted Kremenek67f28532009-06-17 22:02:04 +0000220 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000221
Ted Kremenek67f28532009-06-17 22:02:04 +0000222 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000223
Ted Kremenek67f28532009-06-17 22:02:04 +0000224 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000225
Ted Kremenek67f28532009-06-17 22:02:04 +0000226 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000227 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000228
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000229
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000230 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
231 /// type. 'Array' represents the lvalue of the array being decayed
232 /// to a pointer, and the returned SVal represents the decayed
233 /// version of that lvalue (i.e., a pointer to the first element of
234 /// the array). This is called by GRExprEngine when evaluating
235 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000236 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000237
Ted Kremenek67f28532009-06-17 22:02:04 +0000238 CastResult CastRegion(const GRState *state, const MemRegion* R,
Zhongxing Xu88980592009-05-06 02:42:32 +0000239 QualType CastToTy);
240
Ted Kremenek67f28532009-06-17 22:02:04 +0000241 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,NonLoc R);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000242
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000243
Zhongxing Xu17892752008-10-08 02:50:44 +0000244
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000245
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000246 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000247
248 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
249 /// 'this' object (C++). When used when analyzing a normal function this
250 /// method returns NULL.
251 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000252 if (!SelfDecl)
253 return 0;
254
255 if (!SelfRegion) {
256 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
257 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
258 MRMgr.getHeapRegion());
259 }
260
261 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000262 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000263
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000264
Ted Kremenek67f28532009-06-17 22:02:04 +0000265
266 //===-------------------------------------------------------------------===//
267 // Binding values to regions.
268 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000269
Ted Kremenek67f28532009-06-17 22:02:04 +0000270 const GRState *Bind(const GRState *state, Loc LV, SVal V);
271
272 const GRState *BindCompoundLiteral(const GRState *state,
273 const CompoundLiteralExpr* CL, SVal V);
274
275 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
276
277 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
278 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000279 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000280
Ted Kremenek67f28532009-06-17 22:02:04 +0000281 /// BindStruct - Bind a compound value to a structure.
282 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
283
284 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
285
286 /// KillStruct - Set the entire struct to unknown.
287 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
288
289 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
290
291 Store Remove(Store store, Loc LV);
292
293 //===------------------------------------------------------------------===//
294 // Loading values from regions.
295 //===------------------------------------------------------------------===//
296
297 /// The high level logic for this method is this:
298 /// Retrieve (L)
299 /// if L has binding
300 /// return L's binding
301 /// else if L is in killset
302 /// return unknown
303 /// else
304 /// if L is on stack or heap
305 /// return undefined
306 /// else
307 /// return symbolic
308 SVal Retrieve(const GRState *state, Loc L, QualType T = QualType());
309
310 /// Retrieve the values in a struct and return a CompoundVal, used when doing
311 /// struct copy:
312 /// struct s x, y;
313 /// x = y;
314 /// y's value is retrieved by this method.
315 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
316
317 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
318
319 //===------------------------------------------------------------------===//
320 // State pruning.
321 //===------------------------------------------------------------------===//
322
323 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
324 /// It returns a new Store with these values removed.
325 Store RemoveDeadBindings(const GRState *state, Stmt* Loc, SymbolReaper& SymReaper,
326 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
327
328 //===------------------------------------------------------------------===//
329 // Region "extents".
330 //===------------------------------------------------------------------===//
331
332 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
333 SVal getSizeInElements(const GRState *state, const MemRegion* R);
334
335 //===------------------------------------------------------------------===//
336 // Region "views".
337 //===------------------------------------------------------------------===//
338
339 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
340 const MemRegion* Base);
341
342 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
343 const MemRegion* Base);
344
345 //===------------------------------------------------------------------===//
346 // Utility methods.
347 //===------------------------------------------------------------------===//
348
349 const GRState *setCastType(const GRState *state, const MemRegion* R, QualType T);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000350
Zhongxing Xu17892752008-10-08 02:50:44 +0000351 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000352 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000353 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000354
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000355 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000356
357 void iterBindings(Store store, BindingsHandler& f) {
358 // FIXME: Implement.
359 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000360
Ted Kremenek67f28532009-06-17 22:02:04 +0000361 // FIXME: Remove.
362 BasicValueFactory& getBasicVals() {
363 return StateMgr.getBasicVals();
364 }
365
366 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000367 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xua15f7ac2009-05-08 01:33:18 +0000368
Ted Kremenek67f28532009-06-17 22:02:04 +0000369 // FIXME: Use ValueManager?
370 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000371};
372
373} // end anonymous namespace
374
Ted Kremenek9af46f52009-06-16 22:36:44 +0000375//===----------------------------------------------------------------------===//
376// RegionStore creation.
377//===----------------------------------------------------------------------===//
378
379StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
380 RegionStoreFeatures F = maximal_features_tag();
381 return new RegionStoreManager(StMgr, F);
382}
383
384StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
385 RegionStoreFeatures F = minimal_features_tag();
386 F.enableFields(true);
387 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000388}
389
Ted Kremenek14453bf2009-03-03 19:02:42 +0000390SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000391 RegionBindingsTy B = GetRegionBindings(state->getStore());
392 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
393
394 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
395 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
396 M->add(R->getSuperRegion(), R);
397 }
398
Ted Kremenek14453bf2009-03-03 19:02:42 +0000399 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000400}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000401
Ted Kremenek9af46f52009-06-16 22:36:44 +0000402//===----------------------------------------------------------------------===//
403// getLValueXXX methods.
404//===----------------------------------------------------------------------===//
405
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000406/// getLValueString - Returns an SVal representing the lvalue of a
407/// StringLiteral. Within RegionStore a StringLiteral has an
408/// associated StringRegion, and the lvalue of a StringLiteral is the
409/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000410SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000411 const StringLiteral* S) {
412 return loc::MemRegionVal(MRMgr.getStringRegion(S));
413}
414
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000415/// getLValueVar - Returns an SVal that represents the lvalue of a
416/// variable. Within RegionStore a variable has an associated
417/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000418SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000419 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
420}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000421
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000422/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
423/// of a compound literal. Within RegionStore a compound literal
424/// has an associated region, and the lvalue of the compound literal
425/// is the lvalue of that region.
426SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000427RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000428 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000429 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
430}
431
Ted Kremenek67f28532009-06-17 22:02:04 +0000432SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000433 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000434 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000435}
436
Ted Kremenek67f28532009-06-17 22:02:04 +0000437SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000438 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000439 return getLValueFieldOrIvar(St, Base, D);
440}
441
Ted Kremenek67f28532009-06-17 22:02:04 +0000442SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000443 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000444 if (Base.isUnknownOrUndef())
445 return Base;
446
447 Loc BaseL = cast<Loc>(Base);
448 const MemRegion* BaseR = 0;
449
450 switch (BaseL.getSubKind()) {
451 case loc::MemRegionKind:
452 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
453 break;
454
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000455 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000456 // These are anormal cases. Flag an undefined value.
457 return UndefinedVal();
458
459 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000460 // While these seem funny, this can happen through casts.
461 // FIXME: What we should return is the field offset. For example,
462 // add the field offset to the integer value. That way funny things
463 // like this work properly: &(((struct foo *) 0xa)->f)
464 return Base;
465
466 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000467 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000468 return Base;
469 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000470
471 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
472 // of FieldDecl.
473 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
474 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000475
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000476 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000477}
478
Ted Kremenek67f28532009-06-17 22:02:04 +0000479SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000480 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000481 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000482
Ted Kremenekde7ec632009-03-09 22:44:49 +0000483 // If the base is an unknown or undefined value, just return it back.
484 // FIXME: For absolute pointer addresses, we just return that value back as
485 // well, although in reality we should return the offset added to that
486 // value.
487 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000488 return Base;
489
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000490 // Only handle integer offsets... for now.
491 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000492 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000493
Zhongxing Xuce760782009-05-09 13:20:07 +0000494 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000495
496 // Pointer of any type can be cast and used as array base.
497 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
498
499 if (!ElemR) {
500 //
501 // If the base region is not an ElementRegion, create one.
502 // This can happen in the following example:
503 //
504 // char *p = __builtin_alloc(10);
505 // p[1] = 8;
506 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000507 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000508 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000509
510 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
511 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
512 const llvm::APSInt& OffI = CI->getValue();
513 if (OffI.isUnsigned()) {
514 llvm::APSInt Tmp = OffI;
515 Tmp.setIsSigned(true);
516 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
517 }
518 }
Ted Kremenekf936f452009-05-04 06:18:28 +0000519 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000520 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000521 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000522
523 SVal BaseIdx = ElemR->getIndex();
524
525 if (!isa<nonloc::ConcreteInt>(BaseIdx))
526 return UnknownVal();
527
528 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
529 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
530 assert(BaseIdxI.isSigned());
531
532 // FIXME: This appears to be the assumption of this code. We should review
533 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
534 // can't we need to put a comment here. If it can, we should handle it.
535 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000536
Zhongxing Xuce760782009-05-09 13:20:07 +0000537 const MemRegion *ArrayR = ElemR->getSuperRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000538 SVal NewIdx;
539
540 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
541 // 'Offset' might be unsigned. We have to convert it to signed and
542 // possibly extend it.
543 llvm::APSInt Tmp = OffI;
544
545 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
546 Tmp.extend(BaseIdxI.getBitWidth());
547
548 Tmp.setIsSigned(true);
549 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000550 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000551 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000552 else
553 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000554
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000555 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
556 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000557}
558
Ted Kremenek9af46f52009-06-16 22:36:44 +0000559//===----------------------------------------------------------------------===//
560// Extents for regions.
561//===----------------------------------------------------------------------===//
562
Ted Kremenek67f28532009-06-17 22:02:04 +0000563SVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000564 const MemRegion* R) {
565 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
566 // Get the type of the variable.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000567 QualType T = VR->getDesugaredValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000568
Ted Kremenek14553ab2009-01-30 00:08:43 +0000569 // FIXME: Handle variable-length arrays.
570 if (isa<VariableArrayType>(T))
571 return UnknownVal();
572
573 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
574 // return the size as signed integer.
575 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
576 }
Zhongxing Xu20794942009-05-06 08:33:50 +0000577
Ted Kremenek67f28532009-06-17 22:02:04 +0000578 const QualType* CastTy = state->get<RegionCasts>(VR);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000579
Zhongxing Xue6536af2009-05-08 02:00:55 +0000580 // If the VarRegion is cast to other type, compute the size with respect to
581 // that type.
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000582 if (CastTy) {
583 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000584 QualType VarTy = VR->getValueType(getContext());
Zhongxing Xue6536af2009-05-08 02:00:55 +0000585 uint64_t EleSize = getContext().getTypeSize(EleTy);
586 uint64_t VarSize = getContext().getTypeSize(VarTy);
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000587 assert(VarSize != 0);
Zhongxing Xue6536af2009-05-08 02:00:55 +0000588 return NonLoc::MakeIntVal(getBasicVals(), VarSize / EleSize, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000589 }
590
Ted Kremenek14553ab2009-01-30 00:08:43 +0000591 // Clients can use ordinary variables as if they were arrays. These
592 // essentially are arrays of size 1.
593 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000594 }
595
596 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000597 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000598 // We intentionally made the size value signed because it participates in
599 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000600 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000601 }
602
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000603 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
604 // FIXME: Unsupported yet.
605 FR = 0;
606 return UnknownVal();
607 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000608
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000609 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000610 return UnknownVal();
611 }
612
Zhongxing Xuce760782009-05-09 13:20:07 +0000613 if (isa<AllocaRegion>(R)) {
614 return UnknownVal();
615 }
616
Zhongxing Xuccb16162009-05-06 08:08:27 +0000617 if (isa<ElementRegion>(R)) {
618 return UnknownVal();
619 }
620
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000621 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000622 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000623}
624
Ted Kremenek67f28532009-06-17 22:02:04 +0000625const GRState *RegionStoreManager::setExtent(const GRState *state,
626 const MemRegion *region,
627 SVal extent) {
628 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000629}
630
631//===----------------------------------------------------------------------===//
632// Location and region casting.
633//===----------------------------------------------------------------------===//
634
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000635/// ArrayToPointer - Emulates the "decay" of an array to a pointer
636/// type. 'Array' represents the lvalue of the array being decayed
637/// to a pointer, and the returned SVal represents the decayed
638/// version of that lvalue (i.e., a pointer to the first element of
639/// the array). This is called by GRExprEngine when evaluating casts
640/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000641SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000642 if (!isa<loc::MemRegionVal>(Array))
643 return UnknownVal();
644
645 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
646 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
647
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000648 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000649 return UnknownVal();
650
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000651 // Strip off typedefs from the ArrayRegion's ValueType.
652 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000653 ArrayType *AT = cast<ArrayType>(T);
654 T = AT->getElementType();
655
Zhongxing Xu63123d82008-11-23 04:30:35 +0000656 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000657 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000658
659 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000660}
661
Zhongxing Xu88980592009-05-06 02:42:32 +0000662RegionStoreManager::CastResult
Ted Kremenek67f28532009-06-17 22:02:04 +0000663RegionStoreManager::CastRegion(const GRState *state, const MemRegion* R,
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000664 QualType CastToTy) {
Zhongxing Xu88980592009-05-06 02:42:32 +0000665
666 ASTContext& Ctx = StateMgr.getContext();
667
668 // We need to know the real type of CastToTy.
669 QualType ToTy = Ctx.getCanonicalType(CastToTy);
670
671 // Check cast to ObjCQualifiedID type.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000672 if (ToTy->isObjCQualifiedIdType()) {
Zhongxing Xu88980592009-05-06 02:42:32 +0000673 // FIXME: Record the type information aside.
674 return CastResult(state, R);
675 }
676
677 // CodeTextRegion should be cast to only function pointer type.
678 if (isa<CodeTextRegion>(R)) {
679 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType());
680 return CastResult(state, R);
681 }
682
Zhongxing Xudb3a0982009-05-09 10:03:08 +0000683 // Now assume we are casting from pointer to pointer. Other cases should
684 // already be handled.
Zhongxing Xu88980592009-05-06 02:42:32 +0000685 QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType();
686
Zhongxing Xu88980592009-05-06 02:42:32 +0000687 // Process region cast according to the kind of the region being cast.
688
Zhongxing Xu88980592009-05-06 02:42:32 +0000689 // FIXME: Need to handle arbitrary downcasts.
Zhongxing Xu88980592009-05-06 02:42:32 +0000690 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
Zhongxing Xuce760782009-05-09 13:20:07 +0000691 state = setCastType(state, R, ToTy);
692 return CastResult(state, R);
Zhongxing Xu88980592009-05-06 02:42:32 +0000693 }
694
695 // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally
696 // they should not be cast. We only layer an ElementRegion when the cast-to
697 // pointee type is of smaller size. In other cases, we return the original
698 // VarRegion.
699 if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R)
700 || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) {
Zhongxing Xu2572eda2009-05-08 07:28:25 +0000701 // If the pointee type is incomplete, do not compute its size, and return
702 // the original region.
703 if (const RecordType *RT = dyn_cast<RecordType>(PointeeTy.getTypePtr())) {
704 const RecordDecl *D = RT->getDecl();
705 if (!D->getDefinition(getContext()))
706 return CastResult(state, R);
707 }
708
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000709 QualType ObjTy = cast<TypedRegion>(R)->getValueType(getContext());
Zhongxing Xufb1e3312009-05-08 02:12:59 +0000710 uint64_t PointeeTySize = getContext().getTypeSize(PointeeTy);
711 uint64_t ObjTySize = getContext().getTypeSize(ObjTy);
712
Zhongxing Xu5bf32872009-05-09 15:34:29 +0000713 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000714 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
715 ObjTySize == 0 /* R has 'void*' type. */) {
Zhongxing Xu20794942009-05-06 08:33:50 +0000716 // Record the cast type of the region.
717 state = setCastType(state, R, ToTy);
718
Zhongxing Xuccb16162009-05-06 08:08:27 +0000719 SVal Idx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000720 ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx,R,getContext());
Zhongxing Xuccb16162009-05-06 08:08:27 +0000721 return CastResult(state, ER);
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000722 } else {
723 state = setCastType(state, R, ToTy);
Zhongxing Xuccb16162009-05-06 08:08:27 +0000724 return CastResult(state, R);
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000725 }
Zhongxing Xu88980592009-05-06 02:42:32 +0000726 }
727
Zhongxing Xu88980592009-05-06 02:42:32 +0000728 if (isa<ObjCObjectRegion>(R)) {
729 return CastResult(state, R);
730 }
731
732 assert(0 && "Unprocessed region.");
Daniel Dunbar4e609002009-05-18 16:48:48 +0000733 return 0;
Zhongxing Xu88980592009-05-06 02:42:32 +0000734}
735
Ted Kremenek9af46f52009-06-16 22:36:44 +0000736//===----------------------------------------------------------------------===//
737// Pointer arithmetic.
738//===----------------------------------------------------------------------===//
739
Zhongxing Xu262fd032009-05-20 09:00:16 +0000740SVal RegionStoreManager::EvalBinOp(const GRState *state,
741 BinaryOperator::Opcode Op, Loc L, NonLoc R) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000742 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000743 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000744 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000745
Zhongxing Xua1718c72009-04-03 07:33:13 +0000746 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000747 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000748
749 // If the operand is a symbolic or alloca region, create the first element
750 // region on it.
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000751 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR)) {
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000752 QualType T;
753 // If the SymbolicRegion was cast to another type, use that type.
754 if (const QualType *t = state->get<RegionCasts>(SR)) {
755 T = *t;
756 } else {
757 // Otherwise use the symbol's type.
758 SymbolRef Sym = SR->getSymbol();
759 T = Sym->getType(getContext());
760 }
Zhongxing Xu53454dc2009-06-12 03:59:12 +0000761 QualType EleTy = T->getAsPointerType()->getPointeeType();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000762
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000763 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000764 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Zhongxing Xu262fd032009-05-20 09:00:16 +0000765 }
766 else if (const AllocaRegion *AR = dyn_cast<AllocaRegion>(MR)) {
767 // Get the alloca region's current cast type.
Zhongxing Xu262fd032009-05-20 09:00:16 +0000768
Ted Kremenek67f28532009-06-17 22:02:04 +0000769
770 GRStateTrait<RegionCasts>::lookup_type T = state->get<RegionCasts>(AR);
Zhongxing Xu262fd032009-05-20 09:00:16 +0000771 assert(T && "alloca region has no type.");
772 QualType EleTy = cast<PointerType>(T->getTypePtr())->getPointeeType();
773 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000774 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Zhongxing Xu262fd032009-05-20 09:00:16 +0000775 }
776 else
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000777 ER = cast<ElementRegion>(MR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000778
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000779 SVal Idx = ER->getIndex();
780
781 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
782 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
783
784 // Only support concrete integer indexes for now.
785 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000786 // FIXME: For now, convert the signedness and bitwidth of offset in case
787 // they don't match. This can result from pointer arithmetic. In reality,
788 // we should figure out what are the proper semantics and implement them.
789 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000790 // This addresses the test case test/Analysis/ptr-arith.c
791 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000792 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
793 Offset->getValue()));
794 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000795 const MemRegion* NewER =
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000796 MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion(),
797 getContext());
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000798 return Loc::MakeVal(NewER);
799
Ted Kremenek5dc27462009-03-03 02:51:43 +0000800 }
801
802 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000803}
804
Ted Kremenek9af46f52009-06-16 22:36:44 +0000805//===----------------------------------------------------------------------===//
806// Loading values from regions.
807//===----------------------------------------------------------------------===//
808
Ted Kremenek67f28532009-06-17 22:02:04 +0000809SVal RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
810
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000811 assert(!isa<UnknownVal>(L) && "location unknown");
812 assert(!isa<UndefinedVal>(L) && "location undefined");
813
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000814 // FIXME: Is this even possible? Shouldn't this be treated as a null
815 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000816 if (isa<loc::ConcreteInt>(L))
817 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000818
Ted Kremenek67f28532009-06-17 22:02:04 +0000819 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000820
Zhongxing Xu91844122009-05-20 09:18:48 +0000821 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000822 // Example:
823 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000824 // char* p = alloca();
825 // read(p);
826 // c = *p;
827 if (isa<SymbolicRegion>(MR) || isa<AllocaRegion>(MR))
Zhongxing Xua1718c72009-04-03 07:33:13 +0000828 return UnknownVal();
829
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000830 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
831 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000832 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000833 assert(R && "bad region");
834
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000835 // FIXME: We should eventually handle funny addressing. e.g.:
836 //
837 // int x = ...;
838 // int *p = &x;
839 // char *q = (char*) p;
840 // char c = *q; // returns the first byte of 'x'.
841 //
842 // Such funny addressing will occur due to layering of regions.
843
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000844 QualType RTy = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000845
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000846 if (RTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000847 return RetrieveStruct(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000848
849 if (RTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000850 return RetrieveArray(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000851
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000852 // FIXME: handle Vector types.
853 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000854 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000855
Ted Kremenek67f28532009-06-17 22:02:04 +0000856 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000857 RegionBindingsTy::data_type* V = B.lookup(R);
858
859 // Check if the region has a binding.
860 if (V)
861 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000862
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000863 // Check if the region is in killset.
Ted Kremenek67f28532009-06-17 22:02:04 +0000864 if (state->contains<RegionKills>(R))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000865 return UnknownVal();
866
Zhongxing Xuc87d5fb2009-05-11 14:23:36 +0000867 // Check if the region is an element region of a string literal.
868 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
869 if (const StringRegion *StrR=dyn_cast<StringRegion>(ER->getSuperRegion())) {
870 const StringLiteral *Str = StrR->getStringLiteral();
871 SVal Idx = ER->getIndex();
872 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
873 int64_t i = CI->getValue().getSExtValue();
874 char c;
875 if (i == Str->getByteLength())
876 c = '\0';
877 else
878 c = Str->getStrData()[i];
879 const llvm::APSInt &V = getBasicVals().getValue(c, getContext().CharTy);
880 return nonloc::ConcreteInt(V);
881 }
882 }
883 }
884
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000885 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000886 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
887 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
888 GRStateTrait<RegionDefaultValue>::lookup_type D =
Ted Kremenek67f28532009-06-17 22:02:04 +0000889 state->get<RegionDefaultValue>(SuperR);
Zhongxing Xu264e9372009-05-12 10:10:00 +0000890 if (D) {
891 // If the default value is symbolic, we need to create a new symbol.
892 if (D->hasConjuredSymbol())
893 return ValMgr.getRegionValueSymbolVal(R);
894 else
895 return *D;
896 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000897 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000898
899 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
900 const MemRegion *SR = IVR->getSuperRegion();
901
902 // If the super region is 'self' then return the symbol representing
903 // the value of the ivar upon entry to the method.
904 if (SR == SelfRegion) {
905 // FIXME: Do we need to handle the case where the super region
906 // has a view? We want to canonicalize the bindings.
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000907 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000908 }
909
910 // Otherwise, we need a new symbol. For now return Unknown.
911 return UnknownVal();
912 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000913
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000914 // The location does not have a bound value. This means that it has
915 // the value it had upon its creation and/or entry to the analyzed
916 // function/method. These are either symbolic values or 'undefined'.
917
918 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000919 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
920 const VarDecl *VD = VR->getDecl();
921
Ted Kremenekcec35252009-03-04 00:23:05 +0000922 if (VD == SelfDecl)
923 return loc::MemRegionVal(getSelfRegion(0));
924
925 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
926 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000927 QualType VTy = VD->getType();
928 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000929 return ValMgr.getRegionValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000930 else
931 return UnknownVal();
932 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000933 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000934
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000935 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
936 // All stack variables are considered to have undefined values
937 // upon creation. All heap allocated blocks are considered to
938 // have undefined values as well unless they are explicitly bound
939 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000940 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000941 }
942
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000943 // If the region is already cast to another type, use that type to create the
944 // symbol value.
945 if (const QualType *p = state->get<RegionCasts>(R)) {
946 QualType T = *p;
947 RTy = T->getAsPointerType()->getPointeeType();
948 }
949
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000950 // All other integer values are symbolic.
951 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000952 return ValMgr.getRegionValueSymbolVal(R, RTy);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000953 else
954 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000955}
956
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000957SVal RegionStoreManager::RetrieveStruct(const GRState *state,
958 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000959 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000960 assert(T->isStructureType());
961
Zhongxing Xub7507d12009-06-11 07:27:30 +0000962 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000963 RecordDecl* RD = RT->getDecl();
964 assert(RD->isDefinition());
965
966 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
967
Ted Kremenek67f28532009-06-17 22:02:04 +0000968 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
969 // reverse iterator, we should implement one.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000970 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
971 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +0000972
Douglas Gregore267ff32008-12-11 20:41:00 +0000973 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
974 FieldEnd = Fields.rend();
975 Field != FieldEnd; ++Field) {
976 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000977 QualType FTy = (*Field)->getType();
Ted Kremenek67f28532009-06-17 22:02:04 +0000978 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000979 StructVal = getBasicVals().consVals(FieldValue, StructVal);
980 }
981
982 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
983}
984
Ted Kremenek67f28532009-06-17 22:02:04 +0000985SVal RegionStoreManager::RetrieveArray(const GRState *state,
986 const TypedRegion * R) {
987
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000988 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000989 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
990
991 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000992 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu2ee52142009-05-11 12:48:56 +0000993 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000994
995 for (; i < Size; ++i) {
996 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000997 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
998 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +0000999 QualType ETy = ER->getElementType();
Ted Kremenek67f28532009-06-17 22:02:04 +00001000 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001001 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1002 }
1003
1004 return NonLoc::MakeCompoundVal(T, ArrayVal, getBasicVals());
1005}
1006
Ted Kremenek9af46f52009-06-16 22:36:44 +00001007//===----------------------------------------------------------------------===//
1008// Binding values to regions.
1009//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001010
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001011Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001012 const MemRegion* R = 0;
1013
1014 if (isa<loc::MemRegionVal>(L))
1015 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001016
1017 if (R) {
1018 RegionBindingsTy B = GetRegionBindings(store);
1019 return RBFactory.Remove(B, R).getRoot();
1020 }
1021
1022 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001023}
1024
Ted Kremenek67f28532009-06-17 22:02:04 +00001025const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001026 // If we get here, the location should be a region.
1027 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001028
1029 // Check if the region is a struct region.
1030 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1031 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001032 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001033
Ted Kremenek67f28532009-06-17 22:02:04 +00001034 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001035
1036 if (V.isUnknown()) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001037 B = RBFactory.Remove(B, R); // Remove the binding.
1038 state = state->add<RegionKills>(R); // Add the region to the killset.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001039 }
1040 else
Ted Kremenek67f28532009-06-17 22:02:04 +00001041 B = RBFactory.Add(B, R, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001042
Ted Kremenek67f28532009-06-17 22:02:04 +00001043 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001044}
1045
Ted Kremenek67f28532009-06-17 22:02:04 +00001046const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001047 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001048
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001049 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001050 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001051
Ted Kremenek0964a062009-01-21 06:57:53 +00001052 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001053 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001054 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001055 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001056
Ted Kremenek67f28532009-06-17 22:02:04 +00001057 return Bind(state, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001058}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001059
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001060// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001061const GRState *
1062RegionStoreManager::BindCompoundLiteral(const GRState *state,
1063 const CompoundLiteralExpr* CL,
1064 SVal V) {
1065
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001066 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001067 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001068}
1069
Ted Kremenek67f28532009-06-17 22:02:04 +00001070const GRState *RegionStoreManager::BindArray(const GRState *state,
1071 const TypedRegion* R,
1072 SVal Init) {
1073
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001074 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001075 assert(T->isArrayType());
1076
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001077 // When we are binding the whole array, it always has default value 0.
Ted Kremenek67f28532009-06-17 22:02:04 +00001078 state = state->set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(),
1079 0, false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001080
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001081 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1082
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001083 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001084 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1085 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001086
1087 // Check if the init expr is a StringLiteral.
1088 if (isa<loc::MemRegionVal>(Init)) {
1089 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1090 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1091 const char* str = S->getStrData();
1092 unsigned len = S->getByteLength();
1093 unsigned j = 0;
1094
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001095 // Copy bytes from the string literal into the target array. Trailing bytes
1096 // in the array that are not covered by the string literal are initialized
1097 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001098 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001099 if (j >= len)
1100 break;
1101
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001102 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001103 ElementRegion* ER =
1104 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001105 Idx, R, getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001106
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001107 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001108 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001109 }
1110
Ted Kremenek67f28532009-06-17 22:02:04 +00001111 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001112 }
1113
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001114 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001115 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1116
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001117 for (; i < Size; ++i, ++VI) {
1118 // The init list might be shorter than the array decl.
1119 if (VI == VE)
1120 break;
1121
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001122 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001123 ElementRegion* ER =
1124 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001125 Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001126
1127 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001128 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001129 else
Ted Kremenek67f28532009-06-17 22:02:04 +00001130 state = Bind(state, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001131 }
1132
Ted Kremenek67f28532009-06-17 22:02:04 +00001133 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001134}
1135
Ted Kremenek67f28532009-06-17 22:02:04 +00001136const GRState *
1137RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1138 SVal V) {
1139
1140 if (!Features.supportsFields())
1141 return state;
1142
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001143 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001144 assert(T->isStructureType());
1145
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001146 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001147 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001148
1149 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001150 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001151
Ted Kremenek67f28532009-06-17 22:02:04 +00001152 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1153 // Ignore them and kill the field values.
1154 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1155 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001156
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001157 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001158 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek67f28532009-06-17 22:02:04 +00001159
1160 for (RecordDecl::field_iterator FI = RD->field_begin(getContext()),
1161 FE = RD->field_end(getContext());
1162 FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001163
1164 // There may be fewer values than fields only when we are initializing a
1165 // struct decl. In this case, mark the region as having default value.
1166 if (VI == VE) {
Ted Kremenek14553ab2009-01-30 00:08:43 +00001167 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
Ted Kremenek67f28532009-06-17 22:02:04 +00001168 state = state->set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001169 break;
1170 }
1171
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001172 QualType FTy = (*FI)->getType();
1173 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1174
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001175 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001176 state = Bind(state, Loc::MakeVal(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001177 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001178 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001179 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001180 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001181 }
1182
Ted Kremenek67f28532009-06-17 22:02:04 +00001183 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001184}
1185
Ted Kremenek67f28532009-06-17 22:02:04 +00001186const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001187 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001188
Ted Kremenek67f28532009-06-17 22:02:04 +00001189 // (1) Kill the struct region because it is assigned "unknown".
1190 // (2) Set the default value of the struct region to "unknown".
1191 state = state->add<RegionKills>(R)->set<RegionDefaultValue>(R, UnknownVal());
1192 Store store = state->getStore();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001193 RegionBindingsTy B = GetRegionBindings(store);
1194
1195 // Remove all bindings for the subregions of the struct.
1196 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001197 const MemRegion* R = I.getKey();
1198 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1199 if (subRegion->isSubRegionOf(R))
1200 store = Remove(store, Loc::MakeVal(subRegion));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001201 // FIXME: Maybe we should also remove the bindings for the "views" of the
1202 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001203 }
1204
Ted Kremenek67f28532009-06-17 22:02:04 +00001205 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001206}
1207
Ted Kremenek9af46f52009-06-16 22:36:44 +00001208//===----------------------------------------------------------------------===//
1209// Region views.
1210//===----------------------------------------------------------------------===//
1211
Ted Kremenek67f28532009-06-17 22:02:04 +00001212const GRState *RegionStoreManager::AddRegionView(const GRState *state,
1213 const MemRegion* View,
1214 const MemRegion* Base) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001215
1216 // First, retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001217 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001218 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001219
1220 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001221 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001222
1223 // Create a new state with the new region view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001224 return state->set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001225}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001226
Ted Kremenek67f28532009-06-17 22:02:04 +00001227const GRState *RegionStoreManager::RemoveRegionView(const GRState *state,
1228 const MemRegion* View,
1229 const MemRegion* Base) {
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001230 // Retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001231 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001232
1233 // If the base region has no view, return.
1234 if (!d)
Ted Kremenek67f28532009-06-17 22:02:04 +00001235 return state;
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001236
1237 // Remove the view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001238 return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001239}
Zhongxing Xu20794942009-05-06 08:33:50 +00001240
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001241const GRState *RegionStoreManager::setCastType(const GRState *state,
1242 const MemRegion* R, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001243 return state->set<RegionCasts>(R, T);
Zhongxing Xu20794942009-05-06 08:33:50 +00001244}
Zhongxing Xu264e9372009-05-12 10:10:00 +00001245
Ted Kremenek67f28532009-06-17 22:02:04 +00001246const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1247 const MemRegion* R, SVal V) {
1248 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001249}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001250
1251//===----------------------------------------------------------------------===//
1252// State pruning.
1253//===----------------------------------------------------------------------===//
1254
1255static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1256 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1257 const MemRegion *R = XR->getRegion();
1258
1259 while (R) {
1260 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1261 SymReaper.markLive(SR->getSymbol());
1262 return;
1263 }
1264
1265 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1266 R = SR->getSuperRegion();
1267 continue;
1268 }
1269
1270 break;
1271 }
1272
1273 return;
1274 }
1275
1276 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1277 SymReaper.markLive(*SI);
1278}
1279
Ted Kremenek67f28532009-06-17 22:02:04 +00001280Store RegionStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001281 SymbolReaper& SymReaper,
1282 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001283{
Ted Kremenek9af46f52009-06-16 22:36:44 +00001284 Store store = state->getStore();
1285 RegionBindingsTy B = GetRegionBindings(store);
1286
1287 // Lazily constructed backmap from MemRegions to SubRegions.
1288 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1289 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1290
1291 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
1292 // the ability to reuse memory. This way we can keep TmpAlloc around as
1293 // an instance variable of RegionStoreManager (avoiding repeated malloc
1294 // overhead).
1295 llvm::BumpPtrAllocator TmpAlloc;
1296
1297 // Factory objects.
1298 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1299 SubRegionsTy::Factory SubRegF(TmpAlloc);
1300
1301 // The backmap from regions to subregions.
1302 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1303
1304 // Do a pass over the regions in the store. For VarRegions we check if
1305 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001306 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001307 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1308
1309 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1310 IntermediateRoots.push_back(I.getKey());
1311 }
1312
1313 while (!IntermediateRoots.empty()) {
1314 const MemRegion* R = IntermediateRoots.back();
1315 IntermediateRoots.pop_back();
1316
1317 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
1318 if (SymReaper.isLive(Loc, VR->getDecl()))
1319 RegionRoots.push_back(VR); // This is a live "root".
1320 }
1321 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1322 if (SymReaper.isLive(SR->getSymbol()))
1323 RegionRoots.push_back(SR);
1324 }
1325 else {
1326 // Get the super region for R.
1327 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
1328
1329 // Get the current set of subregions for SuperR.
1330 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
1331 SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
1332
1333 // Add R to the subregions of SuperR.
1334 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SRs, R));
1335
1336 // Super region may be VarRegion or subregion of another VarRegion. Add it
1337 // to the work list.
1338 if (isa<SubRegion>(SuperR))
1339 IntermediateRoots.push_back(SuperR);
1340 }
1341 }
1342
1343 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1344 // of the store. We want to find all live symbols and dead regions.
1345 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1346
1347 while (!RegionRoots.empty()) {
1348 // Dequeue the next region on the worklist.
1349 const MemRegion* R = RegionRoots.back();
1350 RegionRoots.pop_back();
1351
1352 // Check if we have already processed this region.
1353 if (Marked.count(R)) continue;
1354
1355 // Mark this region as processed. This is needed for termination in case
1356 // a region is referenced more than once.
1357 Marked.insert(R);
1358
1359 // Mark the symbol for any live SymbolicRegion as "live". This means we
1360 // should continue to track that symbol.
1361 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1362 SymReaper.markLive(SymR->getSymbol());
1363
1364 // Get the data binding for R (if any).
1365 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1366 if (Xptr) {
1367 SVal X = *Xptr;
1368 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1369
1370 // If X is a region, then add it the RegionRoots.
1371 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1372 RegionRoots.push_back(RegionX->getRegion());
1373 }
1374
1375 // Get the subregions of R. These are RegionRoots as well since they
1376 // represent values that are also bound to R.
1377 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1378 if (!SRptr) continue;
1379 SubRegionsTy SR = *SRptr;
1380
1381 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1382 RegionRoots.push_back(*I);
1383 }
1384
1385 // We have now scanned the store, marking reachable regions and symbols
1386 // as live. We now remove all the regions that are dead from the store
1387 // as well as update DSymbols with the set symbols that are now dead.
1388 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1389 const MemRegion* R = I.getKey();
1390
1391 // If this region live? Is so, none of its symbols are dead.
1392 if (Marked.count(R))
1393 continue;
1394
1395 // Remove this dead region from the store.
1396 store = Remove(store, Loc::MakeVal(R));
1397
1398 // Mark all non-live symbols that this region references as dead.
1399 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1400 SymReaper.maybeDead(SymR->getSymbol());
1401
1402 SVal X = I.getData();
1403 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1404 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
1405 }
1406
1407 return store;
1408}
1409
1410//===----------------------------------------------------------------------===//
1411// Utility methods.
1412//===----------------------------------------------------------------------===//
1413
1414void RegionStoreManager::print(Store store, std::ostream& Out,
1415 const char* nl, const char *sep) {
1416 llvm::raw_os_ostream OS(Out);
1417 RegionBindingsTy B = GetRegionBindings(store);
1418 OS << "Store:" << nl;
1419
1420 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1421 OS << ' '; I.getKey()->print(OS); OS << " : ";
1422 I.getData().print(OS); OS << nl;
1423 }
1424}