blob: 9f55a63f382b6f623acb0412e9a2e6e9d827b0c0 [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 Kremenek53ba0b62009-06-24 23:06:47 +0000241 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
242 NonLoc R);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000243
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000244 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000245
246 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
247 /// 'this' object (C++). When used when analyzing a normal function this
248 /// method returns NULL.
249 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000250 if (!SelfDecl)
251 return 0;
252
253 if (!SelfRegion) {
254 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
255 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
256 MRMgr.getHeapRegion());
257 }
258
259 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000260 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000261
262 //===-------------------------------------------------------------------===//
263 // Binding values to regions.
264 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000265
Ted Kremenek67f28532009-06-17 22:02:04 +0000266 const GRState *Bind(const GRState *state, Loc LV, SVal V);
267
268 const GRState *BindCompoundLiteral(const GRState *state,
269 const CompoundLiteralExpr* CL, SVal V);
270
271 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
272
273 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
274 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000275 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000276
Ted Kremenek67f28532009-06-17 22:02:04 +0000277 /// BindStruct - Bind a compound value to a structure.
278 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
279
280 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
281
282 /// KillStruct - Set the entire struct to unknown.
283 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
284
285 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
286
287 Store Remove(Store store, Loc LV);
288
289 //===------------------------------------------------------------------===//
290 // Loading values from regions.
291 //===------------------------------------------------------------------===//
292
293 /// The high level logic for this method is this:
294 /// Retrieve (L)
295 /// if L has binding
296 /// return L's binding
297 /// else if L is in killset
298 /// return unknown
299 /// else
300 /// if L is on stack or heap
301 /// return undefined
302 /// else
303 /// return symbolic
304 SVal Retrieve(const GRState *state, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000305
306 SVal RetrieveField(const GRState* state, const FieldRegion* R);
307
Ted Kremenek67f28532009-06-17 22:02:04 +0000308 /// Retrieve the values in a struct and return a CompoundVal, used when doing
309 /// struct copy:
310 /// struct s x, y;
311 /// x = y;
312 /// y's value is retrieved by this method.
313 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
314
315 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
316
317 //===------------------------------------------------------------------===//
318 // State pruning.
319 //===------------------------------------------------------------------===//
320
321 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
322 /// It returns a new Store with these values removed.
323 Store RemoveDeadBindings(const GRState *state, Stmt* Loc, SymbolReaper& SymReaper,
324 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
325
326 //===------------------------------------------------------------------===//
327 // Region "extents".
328 //===------------------------------------------------------------------===//
329
330 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
331 SVal getSizeInElements(const GRState *state, const MemRegion* R);
332
333 //===------------------------------------------------------------------===//
334 // Region "views".
335 //===------------------------------------------------------------------===//
336
337 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
338 const MemRegion* Base);
339
340 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
341 const MemRegion* Base);
342
343 //===------------------------------------------------------------------===//
344 // Utility methods.
345 //===------------------------------------------------------------------===//
346
347 const GRState *setCastType(const GRState *state, const MemRegion* R, QualType T);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000348
Zhongxing Xu17892752008-10-08 02:50:44 +0000349 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000350 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000351 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000352
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000353 void print(Store store, llvm::raw_ostream& Out, const char* nl,
354 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000355
356 void iterBindings(Store store, BindingsHandler& f) {
357 // FIXME: Implement.
358 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000359
Ted Kremenek67f28532009-06-17 22:02:04 +0000360 // FIXME: Remove.
361 BasicValueFactory& getBasicVals() {
362 return StateMgr.getBasicVals();
363 }
364
365 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000366 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xua15f7ac2009-05-08 01:33:18 +0000367
Ted Kremenek67f28532009-06-17 22:02:04 +0000368 // FIXME: Use ValueManager?
369 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000370};
371
372} // end anonymous namespace
373
Ted Kremenek9af46f52009-06-16 22:36:44 +0000374//===----------------------------------------------------------------------===//
375// RegionStore creation.
376//===----------------------------------------------------------------------===//
377
378StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
379 RegionStoreFeatures F = maximal_features_tag();
380 return new RegionStoreManager(StMgr, F);
381}
382
383StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
384 RegionStoreFeatures F = minimal_features_tag();
385 F.enableFields(true);
386 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000387}
388
Ted Kremenek14453bf2009-03-03 19:02:42 +0000389SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000390 RegionBindingsTy B = GetRegionBindings(state->getStore());
391 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
392
393 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
394 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
395 M->add(R->getSuperRegion(), R);
396 }
397
Ted Kremenek14453bf2009-03-03 19:02:42 +0000398 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000399}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000400
Ted Kremenek9af46f52009-06-16 22:36:44 +0000401//===----------------------------------------------------------------------===//
402// getLValueXXX methods.
403//===----------------------------------------------------------------------===//
404
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000405/// getLValueString - Returns an SVal representing the lvalue of a
406/// StringLiteral. Within RegionStore a StringLiteral has an
407/// associated StringRegion, and the lvalue of a StringLiteral is the
408/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000409SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000410 const StringLiteral* S) {
411 return loc::MemRegionVal(MRMgr.getStringRegion(S));
412}
413
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000414/// getLValueVar - Returns an SVal that represents the lvalue of a
415/// variable. Within RegionStore a variable has an associated
416/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000417SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000418 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
419}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000420
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000421/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
422/// of a compound literal. Within RegionStore a compound literal
423/// has an associated region, and the lvalue of the compound literal
424/// is the lvalue of that region.
425SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000426RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000427 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000428 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
429}
430
Ted Kremenek67f28532009-06-17 22:02:04 +0000431SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000432 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000433 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000434}
435
Ted Kremenek67f28532009-06-17 22:02:04 +0000436SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000437 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000438 return getLValueFieldOrIvar(St, Base, D);
439}
440
Ted Kremenek67f28532009-06-17 22:02:04 +0000441SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000442 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000443 if (Base.isUnknownOrUndef())
444 return Base;
445
446 Loc BaseL = cast<Loc>(Base);
447 const MemRegion* BaseR = 0;
448
449 switch (BaseL.getSubKind()) {
450 case loc::MemRegionKind:
451 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
452 break;
453
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000454 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000455 // These are anormal cases. Flag an undefined value.
456 return UndefinedVal();
457
458 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000459 // While these seem funny, this can happen through casts.
460 // FIXME: What we should return is the field offset. For example,
461 // add the field offset to the integer value. That way funny things
462 // like this work properly: &(((struct foo *) 0xa)->f)
463 return Base;
464
465 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000466 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000467 return Base;
468 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000469
470 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
471 // of FieldDecl.
472 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
473 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000474
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000475 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000476}
477
Ted Kremenek67f28532009-06-17 22:02:04 +0000478SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000479 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000480 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000481
Ted Kremenekde7ec632009-03-09 22:44:49 +0000482 // If the base is an unknown or undefined value, just return it back.
483 // FIXME: For absolute pointer addresses, we just return that value back as
484 // well, although in reality we should return the offset added to that
485 // value.
486 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000487 return Base;
488
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000489 // Only handle integer offsets... for now.
490 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000491 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000492
Zhongxing Xuce760782009-05-09 13:20:07 +0000493 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000494
495 // Pointer of any type can be cast and used as array base.
496 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
497
498 if (!ElemR) {
499 //
500 // If the base region is not an ElementRegion, create one.
501 // This can happen in the following example:
502 //
503 // char *p = __builtin_alloc(10);
504 // p[1] = 8;
505 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000506 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000507 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000508
509 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
510 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
511 const llvm::APSInt& OffI = CI->getValue();
512 if (OffI.isUnsigned()) {
513 llvm::APSInt Tmp = OffI;
514 Tmp.setIsSigned(true);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000515 Offset = ValMgr.makeIntVal(Tmp);
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000516 }
517 }
Ted Kremenekf936f452009-05-04 06:18:28 +0000518 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000519 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000520 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000521
522 SVal BaseIdx = ElemR->getIndex();
523
524 if (!isa<nonloc::ConcreteInt>(BaseIdx))
525 return UnknownVal();
526
527 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
528 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
529 assert(BaseIdxI.isSigned());
530
531 // FIXME: This appears to be the assumption of this code. We should review
532 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
533 // can't we need to put a comment here. If it can, we should handle it.
534 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000535
Zhongxing Xuce760782009-05-09 13:20:07 +0000536 const MemRegion *ArrayR = ElemR->getSuperRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000537 SVal NewIdx;
538
539 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
540 // 'Offset' might be unsigned. We have to convert it to signed and
541 // possibly extend it.
542 llvm::APSInt Tmp = OffI;
543
544 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
545 Tmp.extend(BaseIdxI.getBitWidth());
546
547 Tmp.setIsSigned(true);
548 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000549 NewIdx = ValMgr.makeIntVal(Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000550 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000551 else
552 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000553
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000554 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
555 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000556}
557
Ted Kremenek9af46f52009-06-16 22:36:44 +0000558//===----------------------------------------------------------------------===//
559// Extents for regions.
560//===----------------------------------------------------------------------===//
561
Ted Kremenek67f28532009-06-17 22:02:04 +0000562SVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000563 const MemRegion* R) {
564 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
565 // Get the type of the variable.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000566 QualType T = VR->getDesugaredValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000567
Ted Kremenek14553ab2009-01-30 00:08:43 +0000568 // FIXME: Handle variable-length arrays.
569 if (isa<VariableArrayType>(T))
570 return UnknownVal();
571
572 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
573 // return the size as signed integer.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000574 return ValMgr.makeIntVal(CAT->getSize(), false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000575 }
Zhongxing Xu20794942009-05-06 08:33:50 +0000576
Ted Kremenek67f28532009-06-17 22:02:04 +0000577 const QualType* CastTy = state->get<RegionCasts>(VR);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000578
Zhongxing Xue6536af2009-05-08 02:00:55 +0000579 // If the VarRegion is cast to other type, compute the size with respect to
580 // that type.
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000581 if (CastTy) {
582 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000583 QualType VarTy = VR->getValueType(getContext());
Zhongxing Xue6536af2009-05-08 02:00:55 +0000584 uint64_t EleSize = getContext().getTypeSize(EleTy);
585 uint64_t VarSize = getContext().getTypeSize(VarTy);
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000586 assert(VarSize != 0);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000587 return ValMgr.makeIntVal(VarSize/EleSize, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000588 }
589
Ted Kremenek14553ab2009-01-30 00:08:43 +0000590 // Clients can use ordinary variables as if they were arrays. These
591 // essentially are arrays of size 1.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000592 return ValMgr.makeIntVal(1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000593 }
594
595 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000596 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000597 // We intentionally made the size value signed because it participates in
598 // operations with signed indices.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000599 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000600 }
601
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000602 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
603 // FIXME: Unsupported yet.
604 FR = 0;
605 return UnknownVal();
606 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000607
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000608 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000609 return UnknownVal();
610 }
611
Zhongxing Xuce760782009-05-09 13:20:07 +0000612 if (isa<AllocaRegion>(R)) {
613 return UnknownVal();
614 }
615
Zhongxing Xuccb16162009-05-06 08:08:27 +0000616 if (isa<ElementRegion>(R)) {
617 return UnknownVal();
618 }
619
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000620 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000621 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000622}
623
Ted Kremenek67f28532009-06-17 22:02:04 +0000624const GRState *RegionStoreManager::setExtent(const GRState *state,
625 const MemRegion *region,
626 SVal extent) {
627 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000628}
629
630//===----------------------------------------------------------------------===//
631// Location and region casting.
632//===----------------------------------------------------------------------===//
633
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000634/// ArrayToPointer - Emulates the "decay" of an array to a pointer
635/// type. 'Array' represents the lvalue of the array being decayed
636/// to a pointer, and the returned SVal represents the decayed
637/// version of that lvalue (i.e., a pointer to the first element of
638/// the array). This is called by GRExprEngine when evaluating casts
639/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000640SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000641 if (!isa<loc::MemRegionVal>(Array))
642 return UnknownVal();
643
644 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
645 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
646
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000647 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000648 return UnknownVal();
649
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000650 // Strip off typedefs from the ArrayRegion's ValueType.
651 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000652 ArrayType *AT = cast<ArrayType>(T);
653 T = AT->getElementType();
654
Zhongxing Xu63123d82008-11-23 04:30:35 +0000655 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000656 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000657
658 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000659}
660
Zhongxing Xu88980592009-05-06 02:42:32 +0000661RegionStoreManager::CastResult
Ted Kremenek67f28532009-06-17 22:02:04 +0000662RegionStoreManager::CastRegion(const GRState *state, const MemRegion* R,
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000663 QualType CastToTy) {
Zhongxing Xu88980592009-05-06 02:42:32 +0000664
665 ASTContext& Ctx = StateMgr.getContext();
666
667 // We need to know the real type of CastToTy.
668 QualType ToTy = Ctx.getCanonicalType(CastToTy);
669
670 // Check cast to ObjCQualifiedID type.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000671 if (ToTy->isObjCQualifiedIdType()) {
Zhongxing Xu88980592009-05-06 02:42:32 +0000672 // FIXME: Record the type information aside.
673 return CastResult(state, R);
674 }
675
676 // CodeTextRegion should be cast to only function pointer type.
677 if (isa<CodeTextRegion>(R)) {
Zhongxing Xu99823a72009-06-22 08:36:10 +0000678 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType()
679 || (CastToTy->isPointerType()
680 && CastToTy->getAsPointerType()->getPointeeType()->isVoidType()));
Zhongxing Xu88980592009-05-06 02:42:32 +0000681 return CastResult(state, R);
682 }
683
Zhongxing Xudb3a0982009-05-09 10:03:08 +0000684 // Now assume we are casting from pointer to pointer. Other cases should
685 // already be handled.
Zhongxing Xu88980592009-05-06 02:42:32 +0000686 QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType();
687
Zhongxing Xu88980592009-05-06 02:42:32 +0000688 // Process region cast according to the kind of the region being cast.
689
Zhongxing Xu88980592009-05-06 02:42:32 +0000690 // FIXME: Need to handle arbitrary downcasts.
Zhongxing Xu88980592009-05-06 02:42:32 +0000691 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
Zhongxing Xuce760782009-05-09 13:20:07 +0000692 state = setCastType(state, R, ToTy);
693 return CastResult(state, R);
Zhongxing Xu88980592009-05-06 02:42:32 +0000694 }
695
696 // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally
697 // they should not be cast. We only layer an ElementRegion when the cast-to
698 // pointee type is of smaller size. In other cases, we return the original
699 // VarRegion.
700 if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R)
701 || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) {
Zhongxing Xu2572eda2009-05-08 07:28:25 +0000702 // If the pointee type is incomplete, do not compute its size, and return
703 // the original region.
704 if (const RecordType *RT = dyn_cast<RecordType>(PointeeTy.getTypePtr())) {
705 const RecordDecl *D = RT->getDecl();
706 if (!D->getDefinition(getContext()))
707 return CastResult(state, R);
708 }
709
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000710 QualType ObjTy = cast<TypedRegion>(R)->getValueType(getContext());
Zhongxing Xufb1e3312009-05-08 02:12:59 +0000711 uint64_t PointeeTySize = getContext().getTypeSize(PointeeTy);
712 uint64_t ObjTySize = getContext().getTypeSize(ObjTy);
713
Zhongxing Xu5bf32872009-05-09 15:34:29 +0000714 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000715 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
716 ObjTySize == 0 /* R has 'void*' type. */) {
Zhongxing Xu20794942009-05-06 08:33:50 +0000717 // Record the cast type of the region.
718 state = setCastType(state, R, ToTy);
719
Zhongxing Xuccb16162009-05-06 08:08:27 +0000720 SVal Idx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000721 ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx,R,getContext());
Zhongxing Xuccb16162009-05-06 08:08:27 +0000722 return CastResult(state, ER);
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000723 } else {
724 state = setCastType(state, R, ToTy);
Zhongxing Xuccb16162009-05-06 08:08:27 +0000725 return CastResult(state, R);
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000726 }
Zhongxing Xu88980592009-05-06 02:42:32 +0000727 }
728
Zhongxing Xu88980592009-05-06 02:42:32 +0000729 if (isa<ObjCObjectRegion>(R)) {
730 return CastResult(state, R);
731 }
732
733 assert(0 && "Unprocessed region.");
Daniel Dunbar4e609002009-05-18 16:48:48 +0000734 return 0;
Zhongxing Xu88980592009-05-06 02:42:32 +0000735}
736
Ted Kremenek9af46f52009-06-16 22:36:44 +0000737//===----------------------------------------------------------------------===//
738// Pointer arithmetic.
739//===----------------------------------------------------------------------===//
740
Zhongxing Xu262fd032009-05-20 09:00:16 +0000741SVal RegionStoreManager::EvalBinOp(const GRState *state,
742 BinaryOperator::Opcode Op, Loc L, NonLoc R) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000743 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000744 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000745 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000746
Zhongxing Xua1718c72009-04-03 07:33:13 +0000747 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000748 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000749
750 // If the operand is a symbolic or alloca region, create the first element
751 // region on it.
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000752 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR)) {
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000753 QualType T;
754 // If the SymbolicRegion was cast to another type, use that type.
755 if (const QualType *t = state->get<RegionCasts>(SR)) {
756 T = *t;
757 } else {
758 // Otherwise use the symbol's type.
759 SymbolRef Sym = SR->getSymbol();
760 T = Sym->getType(getContext());
761 }
Zhongxing Xu53454dc2009-06-12 03:59:12 +0000762 QualType EleTy = T->getAsPointerType()->getPointeeType();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000763
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000764 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000765 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Zhongxing Xu262fd032009-05-20 09:00:16 +0000766 }
767 else if (const AllocaRegion *AR = dyn_cast<AllocaRegion>(MR)) {
768 // Get the alloca region's current cast type.
Zhongxing Xu262fd032009-05-20 09:00:16 +0000769
Ted Kremenek67f28532009-06-17 22:02:04 +0000770
771 GRStateTrait<RegionCasts>::lookup_type T = state->get<RegionCasts>(AR);
Zhongxing Xu262fd032009-05-20 09:00:16 +0000772 assert(T && "alloca region has no type.");
773 QualType EleTy = cast<PointerType>(T->getTypePtr())->getPointeeType();
774 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000775 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Zhongxing Xu262fd032009-05-20 09:00:16 +0000776 }
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000777 else if (isa<FieldRegion>(MR)) {
778 // Not track pointer arithmetic on struct fields.
779 return UnknownVal();
780 }
781 else {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000782 ER = cast<ElementRegion>(MR);
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000783 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000784
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000785 SVal Idx = ER->getIndex();
786
787 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
788 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
789
790 // Only support concrete integer indexes for now.
791 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000792 // FIXME: For now, convert the signedness and bitwidth of offset in case
793 // they don't match. This can result from pointer arithmetic. In reality,
794 // we should figure out what are the proper semantics and implement them.
795 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000796 // This addresses the test case test/Analysis/ptr-arith.c
797 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000798 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
799 Offset->getValue()));
800 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000801 const MemRegion* NewER =
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000802 MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion(),
803 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000804 return ValMgr.makeLoc(NewER);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000805
Ted Kremenek5dc27462009-03-03 02:51:43 +0000806 }
807
808 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000809}
810
Ted Kremenek9af46f52009-06-16 22:36:44 +0000811//===----------------------------------------------------------------------===//
812// Loading values from regions.
813//===----------------------------------------------------------------------===//
814
Ted Kremenek67f28532009-06-17 22:02:04 +0000815SVal RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
816
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000817 assert(!isa<UnknownVal>(L) && "location unknown");
818 assert(!isa<UndefinedVal>(L) && "location undefined");
819
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000820 // FIXME: Is this even possible? Shouldn't this be treated as a null
821 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000822 if (isa<loc::ConcreteInt>(L))
823 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000824
Ted Kremenek67f28532009-06-17 22:02:04 +0000825 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000826
Zhongxing Xu91844122009-05-20 09:18:48 +0000827 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000828 // Example:
829 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000830 // char* p = alloca();
831 // read(p);
832 // c = *p;
833 if (isa<SymbolicRegion>(MR) || isa<AllocaRegion>(MR))
Zhongxing Xua1718c72009-04-03 07:33:13 +0000834 return UnknownVal();
835
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000836 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
837 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000838 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000839 assert(R && "bad region");
840
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000841 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
842 return RetrieveField(state, FR);
843
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000844 // FIXME: We should eventually handle funny addressing. e.g.:
845 //
846 // int x = ...;
847 // int *p = &x;
848 // char *q = (char*) p;
849 // char c = *q; // returns the first byte of 'x'.
850 //
851 // Such funny addressing will occur due to layering of regions.
852
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000853 QualType RTy = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000854
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000855 if (RTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000856 return RetrieveStruct(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000857
858 if (RTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000859 return RetrieveArray(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000860
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000861 // FIXME: handle Vector types.
862 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000863 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000864
Ted Kremenek67f28532009-06-17 22:02:04 +0000865 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000866 RegionBindingsTy::data_type* V = B.lookup(R);
867
868 // Check if the region has a binding.
869 if (V)
870 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000871
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000872 // Check if the region is in killset.
Ted Kremenek67f28532009-06-17 22:02:04 +0000873 if (state->contains<RegionKills>(R))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000874 return UnknownVal();
875
Zhongxing Xuc87d5fb2009-05-11 14:23:36 +0000876 // Check if the region is an element region of a string literal.
877 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
878 if (const StringRegion *StrR=dyn_cast<StringRegion>(ER->getSuperRegion())) {
879 const StringLiteral *Str = StrR->getStringLiteral();
880 SVal Idx = ER->getIndex();
881 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
882 int64_t i = CI->getValue().getSExtValue();
883 char c;
884 if (i == Str->getByteLength())
885 c = '\0';
886 else
887 c = Str->getStrData()[i];
888 const llvm::APSInt &V = getBasicVals().getValue(c, getContext().CharTy);
889 return nonloc::ConcreteInt(V);
890 }
891 }
892 }
893
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000894 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000895 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
896 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xue8ab9ea2009-06-24 01:12:41 +0000897 const SVal* D = state->get<RegionDefaultValue>(SuperR);
Zhongxing Xu264e9372009-05-12 10:10:00 +0000898 if (D) {
899 // If the default value is symbolic, we need to create a new symbol.
900 if (D->hasConjuredSymbol())
901 return ValMgr.getRegionValueSymbolVal(R);
902 else
903 return *D;
904 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000905 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000906
907 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
908 const MemRegion *SR = IVR->getSuperRegion();
909
910 // If the super region is 'self' then return the symbol representing
911 // the value of the ivar upon entry to the method.
912 if (SR == SelfRegion) {
913 // FIXME: Do we need to handle the case where the super region
914 // has a view? We want to canonicalize the bindings.
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000915 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000916 }
917
918 // Otherwise, we need a new symbol. For now return Unknown.
919 return UnknownVal();
920 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000921
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000922 // The location does not have a bound value. This means that it has
923 // the value it had upon its creation and/or entry to the analyzed
924 // function/method. These are either symbolic values or 'undefined'.
925
926 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000927 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
928 const VarDecl *VD = VR->getDecl();
929
Ted Kremenekcec35252009-03-04 00:23:05 +0000930 if (VD == SelfDecl)
931 return loc::MemRegionVal(getSelfRegion(0));
932
933 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
934 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000935 QualType VTy = VD->getType();
936 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000937 return ValMgr.getRegionValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000938 else
939 return UnknownVal();
940 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000941 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000942
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000943 if (R->hasHeapOrStackStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000944 // All stack variables are considered to have undefined values
945 // upon creation. All heap allocated blocks are considered to
946 // have undefined values as well unless they are explicitly bound
947 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000948 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000949 }
950
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000951 // If the region is already cast to another type, use that type to create the
952 // symbol value.
953 if (const QualType *p = state->get<RegionCasts>(R)) {
954 QualType T = *p;
955 RTy = T->getAsPointerType()->getPointeeType();
956 }
957
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000958 // All other integer values are symbolic.
959 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000960 return ValMgr.getRegionValueSymbolVal(R, RTy);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000961 else
962 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000963}
964
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000965SVal RegionStoreManager::RetrieveField(const GRState* state,
966 const FieldRegion* R) {
967 QualType Ty = R->getValueType(getContext());
968
969 // Check if the region has a binding.
970 RegionBindingsTy B = GetRegionBindings(state->getStore());
971 const SVal* V = B.lookup(R);
972 if (V)
973 return *V;
974
975 // Check if the region is killed.
976 if (state->contains<RegionKills>(R))
977 return UnknownVal();
978
979 const MemRegion* SuperR = R->getSuperRegion();
980 const SVal* D = state->get<RegionDefaultValue>(SuperR);
981 if (D) {
982 if (D->hasConjuredSymbol())
983 return ValMgr.getRegionValueSymbolVal(R);
984
985 if (D->isZeroConstant())
986 return ValMgr.makeZeroVal(Ty);
987
988 if (D->isUnknown())
989 return *D;
990
991 assert(0 && "Unknown default value");
992 }
993
994 if (R->hasHeapOrStackStorage())
995 return UndefinedVal();
996
997 // If the region is already cast to another type, use that type to create the
998 // symbol value.
999 if (const QualType *p = state->get<RegionCasts>(R)) {
1000 QualType tmp = *p;
1001 Ty = tmp->getAsPointerType()->getPointeeType();
1002 }
1003
1004 // All other integer values are symbolic.
1005 if (Loc::IsLocType(Ty) || Ty->isIntegerType())
1006 return ValMgr.getRegionValueSymbolVal(R, Ty);
1007 else
1008 return UnknownVal();
1009}
1010
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001011SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1012 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001013 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001014 assert(T->isStructureType());
1015
Zhongxing Xub7507d12009-06-11 07:27:30 +00001016 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001017 RecordDecl* RD = RT->getDecl();
1018 assert(RD->isDefinition());
1019
1020 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1021
Ted Kremenek67f28532009-06-17 22:02:04 +00001022 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1023 // reverse iterator, we should implement one.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001024 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
1025 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +00001026
Douglas Gregore267ff32008-12-11 20:41:00 +00001027 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1028 FieldEnd = Fields.rend();
1029 Field != FieldEnd; ++Field) {
1030 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001031 QualType FTy = (*Field)->getType();
Ted Kremenek67f28532009-06-17 22:02:04 +00001032 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001033 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1034 }
1035
Zhongxing Xud91ee272009-06-23 09:02:15 +00001036 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001037}
1038
Ted Kremenek67f28532009-06-17 22:02:04 +00001039SVal RegionStoreManager::RetrieveArray(const GRState *state,
1040 const TypedRegion * R) {
1041
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001042 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001043 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1044
1045 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001046 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu2ee52142009-05-11 12:48:56 +00001047 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001048
1049 for (; i < Size; ++i) {
Zhongxing Xud91ee272009-06-23 09:02:15 +00001050 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001051 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1052 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001053 QualType ETy = ER->getElementType();
Ted Kremenek67f28532009-06-17 22:02:04 +00001054 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001055 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1056 }
1057
Zhongxing Xud91ee272009-06-23 09:02:15 +00001058 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001059}
1060
Ted Kremenek9af46f52009-06-16 22:36:44 +00001061//===----------------------------------------------------------------------===//
1062// Binding values to regions.
1063//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001064
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001065Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001066 const MemRegion* R = 0;
1067
1068 if (isa<loc::MemRegionVal>(L))
1069 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001070
1071 if (R) {
1072 RegionBindingsTy B = GetRegionBindings(store);
1073 return RBFactory.Remove(B, R).getRoot();
1074 }
1075
1076 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001077}
1078
Ted Kremenek67f28532009-06-17 22:02:04 +00001079const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001080 // If we get here, the location should be a region.
1081 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001082
1083 // Check if the region is a struct region.
1084 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1085 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001086 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001087
Ted Kremenek67f28532009-06-17 22:02:04 +00001088 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001089
1090 if (V.isUnknown()) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001091 B = RBFactory.Remove(B, R); // Remove the binding.
1092 state = state->add<RegionKills>(R); // Add the region to the killset.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001093 }
1094 else
Ted Kremenek67f28532009-06-17 22:02:04 +00001095 B = RBFactory.Add(B, R, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001096
Ted Kremenek67f28532009-06-17 22:02:04 +00001097 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001098}
1099
Ted Kremenek67f28532009-06-17 22:02:04 +00001100const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001101 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001102
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001103 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001104 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001105
Ted Kremenek0964a062009-01-21 06:57:53 +00001106 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001107 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001108 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001109 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001110
Zhongxing Xud91ee272009-06-23 09:02:15 +00001111 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001112}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001113
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001114// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001115const GRState *
1116RegionStoreManager::BindCompoundLiteral(const GRState *state,
1117 const CompoundLiteralExpr* CL,
1118 SVal V) {
1119
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001120 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001121 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001122}
1123
Ted Kremenek67f28532009-06-17 22:02:04 +00001124const GRState *RegionStoreManager::BindArray(const GRState *state,
1125 const TypedRegion* R,
1126 SVal Init) {
1127
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001128 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001129 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001130 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001131
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001132 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001133 llvm::APSInt i(llvm::APInt::getNullValue(Size.getBitWidth()), false);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001134
1135 // Check if the init expr is a StringLiteral.
1136 if (isa<loc::MemRegionVal>(Init)) {
1137 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1138 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1139 const char* str = S->getStrData();
1140 unsigned len = S->getByteLength();
1141 unsigned j = 0;
1142
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001143 // Copy bytes from the string literal into the target array. Trailing bytes
1144 // in the array that are not covered by the string literal are initialized
1145 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001146 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001147 if (j >= len)
1148 break;
1149
Zhongxing Xu3038c5a2009-06-23 06:13:19 +00001150 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001151 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx,R,getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001152
Zhongxing Xud91ee272009-06-23 09:02:15 +00001153 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001154 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001155 }
1156
Ted Kremenek67f28532009-06-17 22:02:04 +00001157 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001158 }
1159
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001160 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001161 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1162
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001163 for (; i < Size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001164 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001165 if (VI == VE)
1166 break;
1167
Zhongxing Xu3038c5a2009-06-23 06:13:19 +00001168 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001169 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001170
1171 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001172 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001173 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001174 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001175 }
1176
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001177 // If the init list is shorter than the array length, set the array default
1178 // value.
1179 if (i < Size) {
1180 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001181 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001182 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001183 }
1184 }
1185
Ted Kremenek67f28532009-06-17 22:02:04 +00001186 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001187}
1188
Ted Kremenek67f28532009-06-17 22:02:04 +00001189const GRState *
1190RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1191 SVal V) {
1192
1193 if (!Features.supportsFields())
1194 return state;
1195
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001196 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001197 assert(T->isStructureType());
1198
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001199 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001200 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001201
1202 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001203 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001204
Ted Kremenek67f28532009-06-17 22:02:04 +00001205 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1206 // Ignore them and kill the field values.
1207 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1208 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001209
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001210 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001211 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001212
1213 RecordDecl::field_iterator FI, FE;
1214
1215 for (FI = RD->field_begin(getContext()), FE = RD->field_end(getContext());
Ted Kremenek67f28532009-06-17 22:02:04 +00001216 FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001217
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001218 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001219 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001220
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001221 QualType FTy = (*FI)->getType();
1222 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1223
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001224 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001225 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001226 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001227 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001228 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001229 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001230 }
1231
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001232 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001233 if (FI != FE)
1234 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001235
Ted Kremenek67f28532009-06-17 22:02:04 +00001236 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001237}
1238
Ted Kremenek67f28532009-06-17 22:02:04 +00001239const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001240 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001241
Ted Kremenek67f28532009-06-17 22:02:04 +00001242 // (1) Kill the struct region because it is assigned "unknown".
1243 // (2) Set the default value of the struct region to "unknown".
1244 state = state->add<RegionKills>(R)->set<RegionDefaultValue>(R, UnknownVal());
1245 Store store = state->getStore();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001246 RegionBindingsTy B = GetRegionBindings(store);
1247
1248 // Remove all bindings for the subregions of the struct.
1249 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001250 const MemRegion* R = I.getKey();
1251 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1252 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001253 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001254 // FIXME: Maybe we should also remove the bindings for the "views" of the
1255 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001256 }
1257
Ted Kremenek67f28532009-06-17 22:02:04 +00001258 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001259}
1260
Ted Kremenek9af46f52009-06-16 22:36:44 +00001261//===----------------------------------------------------------------------===//
1262// Region views.
1263//===----------------------------------------------------------------------===//
1264
Ted Kremenek67f28532009-06-17 22:02:04 +00001265const GRState *RegionStoreManager::AddRegionView(const GRState *state,
1266 const MemRegion* View,
1267 const MemRegion* Base) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001268
1269 // First, retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001270 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001271 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001272
1273 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001274 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001275
1276 // Create a new state with the new region view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001277 return state->set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001278}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001279
Ted Kremenek67f28532009-06-17 22:02:04 +00001280const GRState *RegionStoreManager::RemoveRegionView(const GRState *state,
1281 const MemRegion* View,
1282 const MemRegion* Base) {
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001283 // Retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001284 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001285
1286 // If the base region has no view, return.
1287 if (!d)
Ted Kremenek67f28532009-06-17 22:02:04 +00001288 return state;
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001289
1290 // Remove the view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001291 return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001292}
Zhongxing Xu20794942009-05-06 08:33:50 +00001293
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001294const GRState *RegionStoreManager::setCastType(const GRState *state,
1295 const MemRegion* R, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001296 return state->set<RegionCasts>(R, T);
Zhongxing Xu20794942009-05-06 08:33:50 +00001297}
Zhongxing Xu264e9372009-05-12 10:10:00 +00001298
Ted Kremenek67f28532009-06-17 22:02:04 +00001299const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1300 const MemRegion* R, SVal V) {
1301 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001302}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001303
1304//===----------------------------------------------------------------------===//
1305// State pruning.
1306//===----------------------------------------------------------------------===//
1307
1308static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1309 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1310 const MemRegion *R = XR->getRegion();
1311
1312 while (R) {
1313 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1314 SymReaper.markLive(SR->getSymbol());
1315 return;
1316 }
1317
1318 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1319 R = SR->getSuperRegion();
1320 continue;
1321 }
1322
1323 break;
1324 }
1325
1326 return;
1327 }
1328
1329 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1330 SymReaper.markLive(*SI);
1331}
1332
Ted Kremenek67f28532009-06-17 22:02:04 +00001333Store RegionStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001334 SymbolReaper& SymReaper,
1335 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001336{
Ted Kremenek9af46f52009-06-16 22:36:44 +00001337 Store store = state->getStore();
1338 RegionBindingsTy B = GetRegionBindings(store);
1339
1340 // Lazily constructed backmap from MemRegions to SubRegions.
1341 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1342 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1343
1344 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
1345 // the ability to reuse memory. This way we can keep TmpAlloc around as
1346 // an instance variable of RegionStoreManager (avoiding repeated malloc
1347 // overhead).
1348 llvm::BumpPtrAllocator TmpAlloc;
1349
1350 // Factory objects.
1351 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1352 SubRegionsTy::Factory SubRegF(TmpAlloc);
1353
1354 // The backmap from regions to subregions.
1355 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1356
1357 // Do a pass over the regions in the store. For VarRegions we check if
1358 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001359 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001360 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1361
1362 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1363 IntermediateRoots.push_back(I.getKey());
1364 }
1365
1366 while (!IntermediateRoots.empty()) {
1367 const MemRegion* R = IntermediateRoots.back();
1368 IntermediateRoots.pop_back();
1369
1370 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
1371 if (SymReaper.isLive(Loc, VR->getDecl()))
1372 RegionRoots.push_back(VR); // This is a live "root".
1373 }
1374 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1375 if (SymReaper.isLive(SR->getSymbol()))
1376 RegionRoots.push_back(SR);
1377 }
1378 else {
1379 // Get the super region for R.
1380 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
1381
1382 // Get the current set of subregions for SuperR.
1383 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
1384 SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
1385
1386 // Add R to the subregions of SuperR.
1387 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SRs, R));
1388
1389 // Super region may be VarRegion or subregion of another VarRegion. Add it
1390 // to the work list.
1391 if (isa<SubRegion>(SuperR))
1392 IntermediateRoots.push_back(SuperR);
1393 }
1394 }
1395
1396 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1397 // of the store. We want to find all live symbols and dead regions.
1398 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1399
1400 while (!RegionRoots.empty()) {
1401 // Dequeue the next region on the worklist.
1402 const MemRegion* R = RegionRoots.back();
1403 RegionRoots.pop_back();
1404
1405 // Check if we have already processed this region.
1406 if (Marked.count(R)) continue;
1407
1408 // Mark this region as processed. This is needed for termination in case
1409 // a region is referenced more than once.
1410 Marked.insert(R);
1411
1412 // Mark the symbol for any live SymbolicRegion as "live". This means we
1413 // should continue to track that symbol.
1414 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1415 SymReaper.markLive(SymR->getSymbol());
1416
1417 // Get the data binding for R (if any).
1418 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1419 if (Xptr) {
1420 SVal X = *Xptr;
1421 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1422
1423 // If X is a region, then add it the RegionRoots.
1424 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1425 RegionRoots.push_back(RegionX->getRegion());
1426 }
1427
1428 // Get the subregions of R. These are RegionRoots as well since they
1429 // represent values that are also bound to R.
1430 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1431 if (!SRptr) continue;
1432 SubRegionsTy SR = *SRptr;
1433
1434 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1435 RegionRoots.push_back(*I);
1436 }
1437
1438 // We have now scanned the store, marking reachable regions and symbols
1439 // as live. We now remove all the regions that are dead from the store
1440 // as well as update DSymbols with the set symbols that are now dead.
1441 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1442 const MemRegion* R = I.getKey();
1443
1444 // If this region live? Is so, none of its symbols are dead.
1445 if (Marked.count(R))
1446 continue;
1447
1448 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001449 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001450
1451 // Mark all non-live symbols that this region references as dead.
1452 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1453 SymReaper.maybeDead(SymR->getSymbol());
1454
1455 SVal X = I.getData();
1456 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1457 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
1458 }
1459
1460 return store;
1461}
1462
1463//===----------------------------------------------------------------------===//
1464// Utility methods.
1465//===----------------------------------------------------------------------===//
1466
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001467void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001468 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001469 RegionBindingsTy B = GetRegionBindings(store);
1470 OS << "Store:" << nl;
1471
1472 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1473 OS << ' '; I.getKey()->print(OS); OS << " : ";
1474 I.getData().print(OS); OS << nl;
1475 }
1476}