blob: e87245f9c2ebeb3d4be8c5c6634473abad230518 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000021#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000022
23#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000024#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000025#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000026#include "llvm/Support/Compiler.h"
27
28using namespace clang;
29
Zhongxing Xubaf03a72008-11-24 09:44:56 +000030// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000031typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000032
Ted Kremenek50dc1b32008-12-24 01:05:03 +000033//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000034// Fine-grained control of RegionStoreManager.
35//===----------------------------------------------------------------------===//
36
37namespace {
38struct VISIBILITY_HIDDEN minimal_features_tag {};
39struct VISIBILITY_HIDDEN maximal_features_tag {};
40
41class VISIBILITY_HIDDEN RegionStoreFeatures {
42 bool SupportsFields;
43 bool SupportsRemaining;
44
45public:
46 RegionStoreFeatures(minimal_features_tag) :
47 SupportsFields(false), SupportsRemaining(false) {}
48
49 RegionStoreFeatures(maximal_features_tag) :
50 SupportsFields(true), SupportsRemaining(false) {}
51
52 void enableFields(bool t) { SupportsFields = t; }
53
54 bool supportsFields() const { return SupportsFields; }
55 bool supportsRemaining() const { return SupportsRemaining; }
56};
57}
58
59//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000060// Region "Views"
61//===----------------------------------------------------------------------===//
62//
63// MemRegions can be layered on top of each other. This GDM entry tracks
64// what are the MemRegions that layer a given MemRegion.
65//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000066typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000067namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
68static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000069namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000070 template<> struct GRStateTrait<RegionViewMap>
71 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
72 RegionViews> > {
73
74 static void* GDMIndex() { return &RegionViewMapIndex; }
75 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000076}
Zhongxing Xu17892752008-10-08 02:50:44 +000077
Zhongxing Xu20794942009-05-06 08:33:50 +000078// RegionCasts records the current cast type of a region.
79namespace { class VISIBILITY_HIDDEN RegionCasts {}; }
80static int RegionCastsIndex = 0;
81namespace clang {
82 template<> struct GRStateTrait<RegionCasts>
83 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
84 QualType> > {
85 static void* GDMIndex() { return &RegionCastsIndex; }
86 };
87}
88
Ted Kremenek50dc1b32008-12-24 01:05:03 +000089//===----------------------------------------------------------------------===//
90// Region "Extents"
91//===----------------------------------------------------------------------===//
92//
93// MemRegions represent chunks of memory with a size (their "extent"). This
94// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000095//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000096namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
97static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000098namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000099 template<> struct GRStateTrait<RegionExtents>
100 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
101 static void* GDMIndex() { return &RegionExtentsIndex; }
102 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000103}
104
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000105//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000106// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000107//===----------------------------------------------------------------------===//
108//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000109// This GDM entry tracks what regions have a default value if they have no bound
110// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000111//
112namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
113static int RegionDefaultValueIndex = 0;
114namespace clang {
115 template<> struct GRStateTrait<RegionDefaultValue>
116 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
117 static void* GDMIndex() { return &RegionDefaultValueIndex; }
118 };
119}
120
121//===----------------------------------------------------------------------===//
122// Main RegionStore logic.
123//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000124
Zhongxing Xu17892752008-10-08 02:50:44 +0000125namespace {
126
Ted Kremenek59e8f112009-03-03 01:35:36 +0000127class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
128 typedef llvm::DenseMap<const MemRegion*,
129 llvm::ImmutableSet<const MemRegion*> > Map;
130
131 llvm::ImmutableSet<const MemRegion*>::Factory F;
132 Map M;
133
134public:
135 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
136 Map::iterator I = M.find(Parent);
137 M.insert(std::make_pair(Parent,
138 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
139 }
140
141 ~RegionStoreSubRegionMap() {}
142
Ted Kremenek5dc27462009-03-03 02:51:43 +0000143 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000144 Map::iterator I = M.find(Parent);
145
146 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000147 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148
149 llvm::ImmutableSet<const MemRegion*> S = I->second;
150 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
151 SI != SE; ++SI) {
152 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000153 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000154 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000155
156 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000157 }
158};
159
Zhongxing Xu17892752008-10-08 02:50:44 +0000160class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000161 const RegionStoreFeatures Features;
Zhongxing Xu17892752008-10-08 02:50:44 +0000162 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000163 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000164
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000165 const MemRegion* SelfRegion;
166 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000167
168public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000169 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000170 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000171 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000172 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000173 RVFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000174 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000175 if (const ObjCMethodDecl* MD =
176 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
177 SelfDecl = MD->getSelfDecl();
178 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000179
180 virtual ~RegionStoreManager() {}
181
Ted Kremenek14453bf2009-03-03 19:02:42 +0000182 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000183
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000184 /// getLValueString - Returns an SVal representing the lvalue of a
185 /// StringLiteral. Within RegionStore a StringLiteral has an
186 /// associated StringRegion, and the lvalue of a StringLiteral is
187 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000188 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000189
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000190 /// getLValueCompoundLiteral - Returns an SVal representing the
191 /// lvalue of a compound literal. Within RegionStore a compound
192 /// literal has an associated region, and the lvalue of the
193 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000194 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000195
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000196 /// getLValueVar - Returns an SVal that represents the lvalue of a
197 /// variable. Within RegionStore a variable has an associated
198 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000199 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000200
Ted Kremenek67f28532009-06-17 22:02:04 +0000201 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000202
Ted Kremenek67f28532009-06-17 22:02:04 +0000203 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000204
Ted Kremenek67f28532009-06-17 22:02:04 +0000205 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000206
Ted Kremenek67f28532009-06-17 22:02:04 +0000207 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000208 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000209
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000210
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000211 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
212 /// type. 'Array' represents the lvalue of the array being decayed
213 /// to a pointer, and the returned SVal represents the decayed
214 /// version of that lvalue (i.e., a pointer to the first element of
215 /// the array). This is called by GRExprEngine when evaluating
216 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000217 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000218
Ted Kremenek67f28532009-06-17 22:02:04 +0000219 CastResult CastRegion(const GRState *state, const MemRegion* R,
Zhongxing Xu88980592009-05-06 02:42:32 +0000220 QualType CastToTy);
221
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000222 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
223 NonLoc R);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000224
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000225 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000226
227 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
228 /// 'this' object (C++). When used when analyzing a normal function this
229 /// method returns NULL.
230 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000231 if (!SelfDecl)
232 return 0;
233
234 if (!SelfRegion) {
235 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
236 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
237 MRMgr.getHeapRegion());
238 }
239
240 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000241 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000242
243 //===-------------------------------------------------------------------===//
244 // Binding values to regions.
245 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000246
Ted Kremenek67f28532009-06-17 22:02:04 +0000247 const GRState *Bind(const GRState *state, Loc LV, SVal V);
248
249 const GRState *BindCompoundLiteral(const GRState *state,
250 const CompoundLiteralExpr* CL, SVal V);
251
252 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
253
254 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
255 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000256 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000257
Ted Kremenek67f28532009-06-17 22:02:04 +0000258 /// BindStruct - Bind a compound value to a structure.
259 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
260
261 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
262
263 /// KillStruct - Set the entire struct to unknown.
264 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
265
266 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
267
268 Store Remove(Store store, Loc LV);
269
270 //===------------------------------------------------------------------===//
271 // Loading values from regions.
272 //===------------------------------------------------------------------===//
273
274 /// The high level logic for this method is this:
275 /// Retrieve (L)
276 /// if L has binding
277 /// return L's binding
278 /// else if L is in killset
279 /// return unknown
280 /// else
281 /// if L is on stack or heap
282 /// return undefined
283 /// else
284 /// return symbolic
285 SVal Retrieve(const GRState *state, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000286
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000287 SVal RetrieveElement(const GRState* state, const ElementRegion* R);
288
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000289 SVal RetrieveField(const GRState* state, const FieldRegion* R);
290
Ted Kremenek67f28532009-06-17 22:02:04 +0000291 /// Retrieve the values in a struct and return a CompoundVal, used when doing
292 /// struct copy:
293 /// struct s x, y;
294 /// x = y;
295 /// y's value is retrieved by this method.
296 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
297
298 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
299
300 //===------------------------------------------------------------------===//
301 // State pruning.
302 //===------------------------------------------------------------------===//
303
304 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
305 /// It returns a new Store with these values removed.
306 Store RemoveDeadBindings(const GRState *state, Stmt* Loc, SymbolReaper& SymReaper,
307 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
308
309 //===------------------------------------------------------------------===//
310 // Region "extents".
311 //===------------------------------------------------------------------===//
312
313 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
314 SVal getSizeInElements(const GRState *state, const MemRegion* R);
315
316 //===------------------------------------------------------------------===//
317 // Region "views".
318 //===------------------------------------------------------------------===//
319
320 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
321 const MemRegion* Base);
322
323 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
324 const MemRegion* Base);
325
326 //===------------------------------------------------------------------===//
327 // Utility methods.
328 //===------------------------------------------------------------------===//
329
330 const GRState *setCastType(const GRState *state, const MemRegion* R, QualType T);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000331
Zhongxing Xu17892752008-10-08 02:50:44 +0000332 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000333 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000334 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000335
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000336 void print(Store store, llvm::raw_ostream& Out, const char* nl,
337 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000338
339 void iterBindings(Store store, BindingsHandler& f) {
340 // FIXME: Implement.
341 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000342
Ted Kremenek67f28532009-06-17 22:02:04 +0000343 // FIXME: Remove.
344 BasicValueFactory& getBasicVals() {
345 return StateMgr.getBasicVals();
346 }
347
348 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000349 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xua15f7ac2009-05-08 01:33:18 +0000350
Ted Kremenek67f28532009-06-17 22:02:04 +0000351 // FIXME: Use ValueManager?
352 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000353};
354
355} // end anonymous namespace
356
Ted Kremenek9af46f52009-06-16 22:36:44 +0000357//===----------------------------------------------------------------------===//
358// RegionStore creation.
359//===----------------------------------------------------------------------===//
360
361StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
362 RegionStoreFeatures F = maximal_features_tag();
363 return new RegionStoreManager(StMgr, F);
364}
365
366StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
367 RegionStoreFeatures F = minimal_features_tag();
368 F.enableFields(true);
369 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000370}
371
Ted Kremenek14453bf2009-03-03 19:02:42 +0000372SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000373 RegionBindingsTy B = GetRegionBindings(state->getStore());
374 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
375
376 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
377 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
378 M->add(R->getSuperRegion(), R);
379 }
380
Ted Kremenek14453bf2009-03-03 19:02:42 +0000381 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000382}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000383
Ted Kremenek9af46f52009-06-16 22:36:44 +0000384//===----------------------------------------------------------------------===//
385// getLValueXXX methods.
386//===----------------------------------------------------------------------===//
387
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000388/// getLValueString - Returns an SVal representing the lvalue of a
389/// StringLiteral. Within RegionStore a StringLiteral has an
390/// associated StringRegion, and the lvalue of a StringLiteral is the
391/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000392SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000393 const StringLiteral* S) {
394 return loc::MemRegionVal(MRMgr.getStringRegion(S));
395}
396
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000397/// getLValueVar - Returns an SVal that represents the lvalue of a
398/// variable. Within RegionStore a variable has an associated
399/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000400SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000401 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
402}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000403
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000404/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
405/// of a compound literal. Within RegionStore a compound literal
406/// has an associated region, and the lvalue of the compound literal
407/// is the lvalue of that region.
408SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000409RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000410 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000411 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
412}
413
Ted Kremenek67f28532009-06-17 22:02:04 +0000414SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000415 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000416 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000417}
418
Ted Kremenek67f28532009-06-17 22:02:04 +0000419SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000420 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000421 return getLValueFieldOrIvar(St, Base, D);
422}
423
Ted Kremenek67f28532009-06-17 22:02:04 +0000424SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000425 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000426 if (Base.isUnknownOrUndef())
427 return Base;
428
429 Loc BaseL = cast<Loc>(Base);
430 const MemRegion* BaseR = 0;
431
432 switch (BaseL.getSubKind()) {
433 case loc::MemRegionKind:
434 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
435 break;
436
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000437 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000438 // These are anormal cases. Flag an undefined value.
439 return UndefinedVal();
440
441 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000442 // While these seem funny, this can happen through casts.
443 // FIXME: What we should return is the field offset. For example,
444 // add the field offset to the integer value. That way funny things
445 // like this work properly: &(((struct foo *) 0xa)->f)
446 return Base;
447
448 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000449 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000450 return Base;
451 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000452
453 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
454 // of FieldDecl.
455 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
456 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000457
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000458 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000459}
460
Ted Kremenek67f28532009-06-17 22:02:04 +0000461SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000462 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000463 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000464
Ted Kremenekde7ec632009-03-09 22:44:49 +0000465 // If the base is an unknown or undefined value, just return it back.
466 // FIXME: For absolute pointer addresses, we just return that value back as
467 // well, although in reality we should return the offset added to that
468 // value.
469 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000470 return Base;
471
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000472 // Only handle integer offsets... for now.
473 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000474 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000475
Zhongxing Xuce760782009-05-09 13:20:07 +0000476 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000477
478 // Pointer of any type can be cast and used as array base.
479 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
480
481 if (!ElemR) {
482 //
483 // If the base region is not an ElementRegion, create one.
484 // This can happen in the following example:
485 //
486 // char *p = __builtin_alloc(10);
487 // p[1] = 8;
488 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000489 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000490 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000491
492 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
493 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
494 const llvm::APSInt& OffI = CI->getValue();
495 if (OffI.isUnsigned()) {
496 llvm::APSInt Tmp = OffI;
497 Tmp.setIsSigned(true);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000498 Offset = ValMgr.makeIntVal(Tmp);
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000499 }
500 }
Ted Kremenekf936f452009-05-04 06:18:28 +0000501 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000502 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000503 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000504
505 SVal BaseIdx = ElemR->getIndex();
506
507 if (!isa<nonloc::ConcreteInt>(BaseIdx))
508 return UnknownVal();
509
510 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
511 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
512 assert(BaseIdxI.isSigned());
513
514 // FIXME: This appears to be the assumption of this code. We should review
515 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
516 // can't we need to put a comment here. If it can, we should handle it.
517 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000518
Zhongxing Xuce760782009-05-09 13:20:07 +0000519 const MemRegion *ArrayR = ElemR->getSuperRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000520 SVal NewIdx;
521
522 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
523 // 'Offset' might be unsigned. We have to convert it to signed and
524 // possibly extend it.
525 llvm::APSInt Tmp = OffI;
526
527 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
528 Tmp.extend(BaseIdxI.getBitWidth());
529
530 Tmp.setIsSigned(true);
531 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000532 NewIdx = ValMgr.makeIntVal(Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000533 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000534 else
535 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000536
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000537 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
538 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000539}
540
Ted Kremenek9af46f52009-06-16 22:36:44 +0000541//===----------------------------------------------------------------------===//
542// Extents for regions.
543//===----------------------------------------------------------------------===//
544
Ted Kremenek67f28532009-06-17 22:02:04 +0000545SVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000546 const MemRegion* R) {
547 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
548 // Get the type of the variable.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000549 QualType T = VR->getDesugaredValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000550
Ted Kremenek14553ab2009-01-30 00:08:43 +0000551 // FIXME: Handle variable-length arrays.
552 if (isa<VariableArrayType>(T))
553 return UnknownVal();
554
555 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
556 // return the size as signed integer.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000557 return ValMgr.makeIntVal(CAT->getSize(), false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000558 }
Zhongxing Xu20794942009-05-06 08:33:50 +0000559
Ted Kremenek67f28532009-06-17 22:02:04 +0000560 const QualType* CastTy = state->get<RegionCasts>(VR);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000561
Zhongxing Xue6536af2009-05-08 02:00:55 +0000562 // If the VarRegion is cast to other type, compute the size with respect to
563 // that type.
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000564 if (CastTy) {
565 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000566 QualType VarTy = VR->getValueType(getContext());
Zhongxing Xue6536af2009-05-08 02:00:55 +0000567 uint64_t EleSize = getContext().getTypeSize(EleTy);
568 uint64_t VarSize = getContext().getTypeSize(VarTy);
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000569 assert(VarSize != 0);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000570 return ValMgr.makeIntVal(VarSize/EleSize, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000571 }
572
Ted Kremenek14553ab2009-01-30 00:08:43 +0000573 // Clients can use ordinary variables as if they were arrays. These
574 // essentially are arrays of size 1.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000575 return ValMgr.makeIntVal(1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000576 }
577
578 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000579 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000580 // We intentionally made the size value signed because it participates in
581 // operations with signed indices.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000582 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000583 }
584
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000585 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
586 // FIXME: Unsupported yet.
587 FR = 0;
588 return UnknownVal();
589 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000590
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000591 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000592 return UnknownVal();
593 }
594
Zhongxing Xuce760782009-05-09 13:20:07 +0000595 if (isa<AllocaRegion>(R)) {
596 return UnknownVal();
597 }
598
Zhongxing Xuccb16162009-05-06 08:08:27 +0000599 if (isa<ElementRegion>(R)) {
600 return UnknownVal();
601 }
602
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000603 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000604 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000605}
606
Ted Kremenek67f28532009-06-17 22:02:04 +0000607const GRState *RegionStoreManager::setExtent(const GRState *state,
608 const MemRegion *region,
609 SVal extent) {
610 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000611}
612
613//===----------------------------------------------------------------------===//
614// Location and region casting.
615//===----------------------------------------------------------------------===//
616
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000617/// ArrayToPointer - Emulates the "decay" of an array to a pointer
618/// type. 'Array' represents the lvalue of the array being decayed
619/// to a pointer, and the returned SVal represents the decayed
620/// version of that lvalue (i.e., a pointer to the first element of
621/// the array). This is called by GRExprEngine when evaluating casts
622/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000623SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000624 if (!isa<loc::MemRegionVal>(Array))
625 return UnknownVal();
626
627 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
628 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
629
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000630 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000631 return UnknownVal();
632
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000633 // Strip off typedefs from the ArrayRegion's ValueType.
634 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000635 ArrayType *AT = cast<ArrayType>(T);
636 T = AT->getElementType();
637
Zhongxing Xu63123d82008-11-23 04:30:35 +0000638 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000639 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000640
641 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000642}
643
Zhongxing Xu88980592009-05-06 02:42:32 +0000644RegionStoreManager::CastResult
Ted Kremenek67f28532009-06-17 22:02:04 +0000645RegionStoreManager::CastRegion(const GRState *state, const MemRegion* R,
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000646 QualType CastToTy) {
Zhongxing Xu88980592009-05-06 02:42:32 +0000647
648 ASTContext& Ctx = StateMgr.getContext();
649
650 // We need to know the real type of CastToTy.
651 QualType ToTy = Ctx.getCanonicalType(CastToTy);
652
653 // Check cast to ObjCQualifiedID type.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000654 if (ToTy->isObjCQualifiedIdType()) {
Zhongxing Xu88980592009-05-06 02:42:32 +0000655 // FIXME: Record the type information aside.
656 return CastResult(state, R);
657 }
658
659 // CodeTextRegion should be cast to only function pointer type.
660 if (isa<CodeTextRegion>(R)) {
Zhongxing Xu99823a72009-06-22 08:36:10 +0000661 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType()
662 || (CastToTy->isPointerType()
663 && CastToTy->getAsPointerType()->getPointeeType()->isVoidType()));
Zhongxing Xu88980592009-05-06 02:42:32 +0000664 return CastResult(state, R);
665 }
666
Zhongxing Xudb3a0982009-05-09 10:03:08 +0000667 // Now assume we are casting from pointer to pointer. Other cases should
668 // already be handled.
Zhongxing Xu88980592009-05-06 02:42:32 +0000669 QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType();
670
Zhongxing Xu88980592009-05-06 02:42:32 +0000671 // Process region cast according to the kind of the region being cast.
672
Zhongxing Xu88980592009-05-06 02:42:32 +0000673 // FIXME: Need to handle arbitrary downcasts.
Zhongxing Xu88980592009-05-06 02:42:32 +0000674 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
Zhongxing Xuce760782009-05-09 13:20:07 +0000675 state = setCastType(state, R, ToTy);
676 return CastResult(state, R);
Zhongxing Xu88980592009-05-06 02:42:32 +0000677 }
678
679 // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally
680 // they should not be cast. We only layer an ElementRegion when the cast-to
681 // pointee type is of smaller size. In other cases, we return the original
682 // VarRegion.
683 if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R)
684 || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) {
Zhongxing Xu2572eda2009-05-08 07:28:25 +0000685 // If the pointee type is incomplete, do not compute its size, and return
686 // the original region.
687 if (const RecordType *RT = dyn_cast<RecordType>(PointeeTy.getTypePtr())) {
688 const RecordDecl *D = RT->getDecl();
689 if (!D->getDefinition(getContext()))
690 return CastResult(state, R);
691 }
692
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000693 QualType ObjTy = cast<TypedRegion>(R)->getValueType(getContext());
Zhongxing Xufb1e3312009-05-08 02:12:59 +0000694 uint64_t PointeeTySize = getContext().getTypeSize(PointeeTy);
695 uint64_t ObjTySize = getContext().getTypeSize(ObjTy);
696
Zhongxing Xu5bf32872009-05-09 15:34:29 +0000697 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000698 (ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
699 ObjTySize == 0 /* R has 'void*' type. */) {
Zhongxing Xu20794942009-05-06 08:33:50 +0000700 // Record the cast type of the region.
701 state = setCastType(state, R, ToTy);
702
Zhongxing Xuccb16162009-05-06 08:08:27 +0000703 SVal Idx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000704 ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx,R,getContext());
Zhongxing Xuccb16162009-05-06 08:08:27 +0000705 return CastResult(state, ER);
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000706 } else {
707 state = setCastType(state, R, ToTy);
Zhongxing Xuccb16162009-05-06 08:08:27 +0000708 return CastResult(state, R);
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000709 }
Zhongxing Xu88980592009-05-06 02:42:32 +0000710 }
711
Zhongxing Xu88980592009-05-06 02:42:32 +0000712 if (isa<ObjCObjectRegion>(R)) {
713 return CastResult(state, R);
714 }
715
716 assert(0 && "Unprocessed region.");
Daniel Dunbar4e609002009-05-18 16:48:48 +0000717 return 0;
Zhongxing Xu88980592009-05-06 02:42:32 +0000718}
719
Ted Kremenek9af46f52009-06-16 22:36:44 +0000720//===----------------------------------------------------------------------===//
721// Pointer arithmetic.
722//===----------------------------------------------------------------------===//
723
Zhongxing Xu262fd032009-05-20 09:00:16 +0000724SVal RegionStoreManager::EvalBinOp(const GRState *state,
725 BinaryOperator::Opcode Op, Loc L, NonLoc R) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000726 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000727 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000728 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000729
Zhongxing Xua1718c72009-04-03 07:33:13 +0000730 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000731 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000732
733 // If the operand is a symbolic or alloca region, create the first element
734 // region on it.
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000735 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR)) {
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000736 QualType T;
737 // If the SymbolicRegion was cast to another type, use that type.
738 if (const QualType *t = state->get<RegionCasts>(SR)) {
739 T = *t;
740 } else {
741 // Otherwise use the symbol's type.
742 SymbolRef Sym = SR->getSymbol();
743 T = Sym->getType(getContext());
744 }
Zhongxing Xu53454dc2009-06-12 03:59:12 +0000745 QualType EleTy = T->getAsPointerType()->getPointeeType();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000746
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000747 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000748 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Zhongxing Xu262fd032009-05-20 09:00:16 +0000749 }
750 else if (const AllocaRegion *AR = dyn_cast<AllocaRegion>(MR)) {
751 // Get the alloca region's current cast type.
Zhongxing Xu262fd032009-05-20 09:00:16 +0000752
Ted Kremenek67f28532009-06-17 22:02:04 +0000753
754 GRStateTrait<RegionCasts>::lookup_type T = state->get<RegionCasts>(AR);
Zhongxing Xu262fd032009-05-20 09:00:16 +0000755 assert(T && "alloca region has no type.");
756 QualType EleTy = cast<PointerType>(T->getTypePtr())->getPointeeType();
757 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000758 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Zhongxing Xu262fd032009-05-20 09:00:16 +0000759 }
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000760 else if (isa<FieldRegion>(MR)) {
761 // Not track pointer arithmetic on struct fields.
762 return UnknownVal();
763 }
764 else {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000765 ER = cast<ElementRegion>(MR);
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000766 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000767
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000768 SVal Idx = ER->getIndex();
769
770 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
771 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
772
773 // Only support concrete integer indexes for now.
774 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000775 // FIXME: For now, convert the signedness and bitwidth of offset in case
776 // they don't match. This can result from pointer arithmetic. In reality,
777 // we should figure out what are the proper semantics and implement them.
778 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000779 // This addresses the test case test/Analysis/ptr-arith.c
780 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000781 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
782 Offset->getValue()));
783 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000784 const MemRegion* NewER =
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000785 MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion(),
786 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000787 return ValMgr.makeLoc(NewER);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000788
Ted Kremenek5dc27462009-03-03 02:51:43 +0000789 }
790
791 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000792}
793
Ted Kremenek9af46f52009-06-16 22:36:44 +0000794//===----------------------------------------------------------------------===//
795// Loading values from regions.
796//===----------------------------------------------------------------------===//
797
Ted Kremenek67f28532009-06-17 22:02:04 +0000798SVal RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
799
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000800 assert(!isa<UnknownVal>(L) && "location unknown");
801 assert(!isa<UndefinedVal>(L) && "location undefined");
802
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000803 // FIXME: Is this even possible? Shouldn't this be treated as a null
804 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000805 if (isa<loc::ConcreteInt>(L))
806 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000807
Ted Kremenek67f28532009-06-17 22:02:04 +0000808 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000809
Zhongxing Xu91844122009-05-20 09:18:48 +0000810 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000811 // Example:
812 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000813 // char* p = alloca();
814 // read(p);
815 // c = *p;
816 if (isa<SymbolicRegion>(MR) || isa<AllocaRegion>(MR))
Zhongxing Xua1718c72009-04-03 07:33:13 +0000817 return UnknownVal();
818
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000819 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
820 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000821 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000822 assert(R && "bad region");
823
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000824 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
825 return RetrieveField(state, FR);
826
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000827 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
828 return RetrieveElement(state, ER);
829
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000830 // FIXME: We should eventually handle funny addressing. e.g.:
831 //
832 // int x = ...;
833 // int *p = &x;
834 // char *q = (char*) p;
835 // char c = *q; // returns the first byte of 'x'.
836 //
837 // Such funny addressing will occur due to layering of regions.
838
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000839 QualType RTy = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000840
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000841 if (RTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000842 return RetrieveStruct(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000843
844 if (RTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000845 return RetrieveArray(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000846
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000847 // FIXME: handle Vector types.
848 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000849 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000850
Ted Kremenek67f28532009-06-17 22:02:04 +0000851 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000852 RegionBindingsTy::data_type* V = B.lookup(R);
853
854 // Check if the region has a binding.
855 if (V)
856 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000857
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000858 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
859 const MemRegion *SR = IVR->getSuperRegion();
860
861 // If the super region is 'self' then return the symbol representing
862 // the value of the ivar upon entry to the method.
863 if (SR == SelfRegion) {
864 // FIXME: Do we need to handle the case where the super region
865 // has a view? We want to canonicalize the bindings.
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000866 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000867 }
868
869 // Otherwise, we need a new symbol. For now return Unknown.
870 return UnknownVal();
871 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000872
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000873 // The location does not have a bound value. This means that it has
874 // the value it had upon its creation and/or entry to the analyzed
875 // function/method. These are either symbolic values or 'undefined'.
876
877 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000878 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
879 const VarDecl *VD = VR->getDecl();
880
Ted Kremenekcec35252009-03-04 00:23:05 +0000881 if (VD == SelfDecl)
882 return loc::MemRegionVal(getSelfRegion(0));
883
884 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
885 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000886 QualType VTy = VD->getType();
887 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000888 return ValMgr.getRegionValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000889 else
890 return UnknownVal();
891 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000892 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000893
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000894 if (R->hasHeapOrStackStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000895 // All stack variables are considered to have undefined values
896 // upon creation. All heap allocated blocks are considered to
897 // have undefined values as well unless they are explicitly bound
898 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000899 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000900 }
901
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000902 // If the region is already cast to another type, use that type to create the
903 // symbol value.
904 if (const QualType *p = state->get<RegionCasts>(R)) {
905 QualType T = *p;
906 RTy = T->getAsPointerType()->getPointeeType();
907 }
908
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000909 // All other integer values are symbolic.
910 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000911 return ValMgr.getRegionValueSymbolVal(R, RTy);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000912 else
913 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000914}
915
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000916SVal RegionStoreManager::RetrieveElement(const GRState* state,
917 const ElementRegion* R) {
918 // Check if the region has a binding.
919 RegionBindingsTy B = GetRegionBindings(state->getStore());
920 const SVal* V = B.lookup(R);
921 if (V)
922 return *V;
923
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000924 // Check if the region is an element region of a string literal.
925 if (const StringRegion *StrR=dyn_cast<StringRegion>(R->getSuperRegion())) {
926 const StringLiteral *Str = StrR->getStringLiteral();
927 SVal Idx = R->getIndex();
928 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
929 int64_t i = CI->getValue().getSExtValue();
930 char c;
931 if (i == Str->getByteLength())
932 c = '\0';
933 else
934 c = Str->getStrData()[i];
935 return ValMgr.makeIntVal(c, getContext().CharTy);
936 }
937 }
938
939 const MemRegion* SuperR = R->getSuperRegion();
940 const SVal* D = state->get<RegionDefaultValue>(SuperR);
941
942 if (D) {
943 if (D->hasConjuredSymbol())
944 return ValMgr.getRegionValueSymbolVal(R);
945 else
946 return *D;
947 }
948
949 if (R->hasHeapOrStackStorage())
950 return UndefinedVal();
951
952 QualType Ty = R->getValueType(getContext());
953
954 // If the region is already cast to another type, use that type to create the
955 // symbol value.
956 if (const QualType *p = state->get<RegionCasts>(R))
957 Ty = (*p)->getAsPointerType()->getPointeeType();
958
959 if (Loc::IsLocType(Ty) || Ty->isIntegerType())
960 return ValMgr.getRegionValueSymbolVal(R, Ty);
961 else
962 return UnknownVal();
963}
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
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000975 const MemRegion* SuperR = R->getSuperRegion();
976 const SVal* D = state->get<RegionDefaultValue>(SuperR);
977 if (D) {
978 if (D->hasConjuredSymbol())
979 return ValMgr.getRegionValueSymbolVal(R);
980
981 if (D->isZeroConstant())
982 return ValMgr.makeZeroVal(Ty);
983
984 if (D->isUnknown())
985 return *D;
986
987 assert(0 && "Unknown default value");
988 }
989
990 if (R->hasHeapOrStackStorage())
991 return UndefinedVal();
992
993 // If the region is already cast to another type, use that type to create the
994 // symbol value.
995 if (const QualType *p = state->get<RegionCasts>(R)) {
996 QualType tmp = *p;
997 Ty = tmp->getAsPointerType()->getPointeeType();
998 }
999
1000 // All other integer values are symbolic.
1001 if (Loc::IsLocType(Ty) || Ty->isIntegerType())
1002 return ValMgr.getRegionValueSymbolVal(R, Ty);
1003 else
1004 return UnknownVal();
1005}
1006
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001007SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1008 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001009 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001010 assert(T->isStructureType());
1011
Zhongxing Xub7507d12009-06-11 07:27:30 +00001012 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001013 RecordDecl* RD = RT->getDecl();
1014 assert(RD->isDefinition());
1015
1016 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1017
Ted Kremenek67f28532009-06-17 22:02:04 +00001018 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1019 // reverse iterator, we should implement one.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001020 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
1021 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +00001022
Douglas Gregore267ff32008-12-11 20:41:00 +00001023 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1024 FieldEnd = Fields.rend();
1025 Field != FieldEnd; ++Field) {
1026 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001027 QualType FTy = (*Field)->getType();
Ted Kremenek67f28532009-06-17 22:02:04 +00001028 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001029 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1030 }
1031
Zhongxing Xud91ee272009-06-23 09:02:15 +00001032 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001033}
1034
Ted Kremenek67f28532009-06-17 22:02:04 +00001035SVal RegionStoreManager::RetrieveArray(const GRState *state,
1036 const TypedRegion * R) {
1037
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001038 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001039 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1040
1041 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001042 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu2ee52142009-05-11 12:48:56 +00001043 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001044
1045 for (; i < Size; ++i) {
Zhongxing Xud91ee272009-06-23 09:02:15 +00001046 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001047 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1048 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001049 QualType ETy = ER->getElementType();
Ted Kremenek67f28532009-06-17 22:02:04 +00001050 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001051 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1052 }
1053
Zhongxing Xud91ee272009-06-23 09:02:15 +00001054 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001055}
1056
Ted Kremenek9af46f52009-06-16 22:36:44 +00001057//===----------------------------------------------------------------------===//
1058// Binding values to regions.
1059//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001060
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001061Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001062 const MemRegion* R = 0;
1063
1064 if (isa<loc::MemRegionVal>(L))
1065 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001066
1067 if (R) {
1068 RegionBindingsTy B = GetRegionBindings(store);
1069 return RBFactory.Remove(B, R).getRoot();
1070 }
1071
1072 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001073}
1074
Ted Kremenek67f28532009-06-17 22:02:04 +00001075const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001076 // If we get here, the location should be a region.
1077 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001078
1079 // Check if the region is a struct region.
1080 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1081 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001082 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001083
Ted Kremenek67f28532009-06-17 22:02:04 +00001084 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001085
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001086 B = RBFactory.Add(B, R, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001087
Ted Kremenek67f28532009-06-17 22:02:04 +00001088 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001089}
1090
Ted Kremenek67f28532009-06-17 22:02:04 +00001091const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001092 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001093
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001094 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001095 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001096
Ted Kremenek0964a062009-01-21 06:57:53 +00001097 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001098 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001099 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001100 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001101
Zhongxing Xud91ee272009-06-23 09:02:15 +00001102 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001103}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001104
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001105// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001106const GRState *
1107RegionStoreManager::BindCompoundLiteral(const GRState *state,
1108 const CompoundLiteralExpr* CL,
1109 SVal V) {
1110
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001111 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001112 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001113}
1114
Ted Kremenek67f28532009-06-17 22:02:04 +00001115const GRState *RegionStoreManager::BindArray(const GRState *state,
1116 const TypedRegion* R,
1117 SVal Init) {
1118
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001119 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001120 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001121 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001122
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001123 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001124 llvm::APSInt i(llvm::APInt::getNullValue(Size.getBitWidth()), false);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001125
1126 // Check if the init expr is a StringLiteral.
1127 if (isa<loc::MemRegionVal>(Init)) {
1128 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1129 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1130 const char* str = S->getStrData();
1131 unsigned len = S->getByteLength();
1132 unsigned j = 0;
1133
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001134 // Copy bytes from the string literal into the target array. Trailing bytes
1135 // in the array that are not covered by the string literal are initialized
1136 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001137 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001138 if (j >= len)
1139 break;
1140
Zhongxing Xu3038c5a2009-06-23 06:13:19 +00001141 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001142 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx,R,getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001143
Zhongxing Xud91ee272009-06-23 09:02:15 +00001144 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001145 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001146 }
1147
Ted Kremenek67f28532009-06-17 22:02:04 +00001148 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001149 }
1150
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001151 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001152 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1153
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001154 for (; i < Size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001155 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001156 if (VI == VE)
1157 break;
1158
Zhongxing Xu3038c5a2009-06-23 06:13:19 +00001159 SVal Idx = ValMgr.makeIntVal(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001160 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001161
1162 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001163 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001164 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001165 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001166 }
1167
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001168 // If the init list is shorter than the array length, set the array default
1169 // value.
1170 if (i < Size) {
1171 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001172 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001173 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001174 }
1175 }
1176
Ted Kremenek67f28532009-06-17 22:02:04 +00001177 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001178}
1179
Ted Kremenek67f28532009-06-17 22:02:04 +00001180const GRState *
1181RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1182 SVal V) {
1183
1184 if (!Features.supportsFields())
1185 return state;
1186
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001187 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001188 assert(T->isStructureType());
1189
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001190 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001191 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001192
1193 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001194 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001195
Ted Kremenek67f28532009-06-17 22:02:04 +00001196 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1197 // Ignore them and kill the field values.
1198 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1199 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001200
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001201 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001202 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001203
1204 RecordDecl::field_iterator FI, FE;
1205
1206 for (FI = RD->field_begin(getContext()), FE = RD->field_end(getContext());
Ted Kremenek67f28532009-06-17 22:02:04 +00001207 FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001208
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001209 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001210 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001211
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001212 QualType FTy = (*FI)->getType();
1213 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1214
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001215 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001216 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001217 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001218 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001219 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001220 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001221 }
1222
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001223 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001224 if (FI != FE)
1225 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001226
Ted Kremenek67f28532009-06-17 22:02:04 +00001227 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001228}
1229
Ted Kremenek67f28532009-06-17 22:02:04 +00001230const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001231 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001232
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001233 // Set the default value of the struct region to "unknown".
1234 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001235
1236 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001237 Store store = state->getStore();
1238 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001239 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001240 const MemRegion* R = I.getKey();
1241 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1242 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001243 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001244 }
1245
Ted Kremenek67f28532009-06-17 22:02:04 +00001246 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001247}
1248
Ted Kremenek9af46f52009-06-16 22:36:44 +00001249//===----------------------------------------------------------------------===//
1250// Region views.
1251//===----------------------------------------------------------------------===//
1252
Ted Kremenek67f28532009-06-17 22:02:04 +00001253const GRState *RegionStoreManager::AddRegionView(const GRState *state,
1254 const MemRegion* View,
1255 const MemRegion* Base) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001256
1257 // First, retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001258 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001259 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001260
1261 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001262 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001263
1264 // Create a new state with the new region view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001265 return state->set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001266}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001267
Ted Kremenek67f28532009-06-17 22:02:04 +00001268const GRState *RegionStoreManager::RemoveRegionView(const GRState *state,
1269 const MemRegion* View,
1270 const MemRegion* Base) {
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001271 // Retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001272 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001273
1274 // If the base region has no view, return.
1275 if (!d)
Ted Kremenek67f28532009-06-17 22:02:04 +00001276 return state;
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001277
1278 // Remove the view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001279 return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001280}
Zhongxing Xu20794942009-05-06 08:33:50 +00001281
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001282const GRState *RegionStoreManager::setCastType(const GRState *state,
1283 const MemRegion* R, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001284 return state->set<RegionCasts>(R, T);
Zhongxing Xu20794942009-05-06 08:33:50 +00001285}
Zhongxing Xu264e9372009-05-12 10:10:00 +00001286
Ted Kremenek67f28532009-06-17 22:02:04 +00001287const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1288 const MemRegion* R, SVal V) {
1289 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001290}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001291
1292//===----------------------------------------------------------------------===//
1293// State pruning.
1294//===----------------------------------------------------------------------===//
1295
1296static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1297 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1298 const MemRegion *R = XR->getRegion();
1299
1300 while (R) {
1301 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1302 SymReaper.markLive(SR->getSymbol());
1303 return;
1304 }
1305
1306 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1307 R = SR->getSuperRegion();
1308 continue;
1309 }
1310
1311 break;
1312 }
1313
1314 return;
1315 }
1316
1317 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1318 SymReaper.markLive(*SI);
1319}
1320
Ted Kremenek67f28532009-06-17 22:02:04 +00001321Store RegionStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001322 SymbolReaper& SymReaper,
1323 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001324{
Ted Kremenek9af46f52009-06-16 22:36:44 +00001325 Store store = state->getStore();
1326 RegionBindingsTy B = GetRegionBindings(store);
1327
1328 // Lazily constructed backmap from MemRegions to SubRegions.
1329 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1330 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1331
1332 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
1333 // the ability to reuse memory. This way we can keep TmpAlloc around as
1334 // an instance variable of RegionStoreManager (avoiding repeated malloc
1335 // overhead).
1336 llvm::BumpPtrAllocator TmpAlloc;
1337
1338 // Factory objects.
1339 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1340 SubRegionsTy::Factory SubRegF(TmpAlloc);
1341
1342 // The backmap from regions to subregions.
1343 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1344
1345 // Do a pass over the regions in the store. For VarRegions we check if
1346 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001347 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001348 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1349
1350 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1351 IntermediateRoots.push_back(I.getKey());
1352 }
1353
1354 while (!IntermediateRoots.empty()) {
1355 const MemRegion* R = IntermediateRoots.back();
1356 IntermediateRoots.pop_back();
1357
1358 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
1359 if (SymReaper.isLive(Loc, VR->getDecl()))
1360 RegionRoots.push_back(VR); // This is a live "root".
1361 }
1362 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1363 if (SymReaper.isLive(SR->getSymbol()))
1364 RegionRoots.push_back(SR);
1365 }
1366 else {
1367 // Get the super region for R.
1368 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
1369
1370 // Get the current set of subregions for SuperR.
1371 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
1372 SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
1373
1374 // Add R to the subregions of SuperR.
1375 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SRs, R));
1376
1377 // Super region may be VarRegion or subregion of another VarRegion. Add it
1378 // to the work list.
1379 if (isa<SubRegion>(SuperR))
1380 IntermediateRoots.push_back(SuperR);
1381 }
1382 }
1383
1384 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1385 // of the store. We want to find all live symbols and dead regions.
1386 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1387
1388 while (!RegionRoots.empty()) {
1389 // Dequeue the next region on the worklist.
1390 const MemRegion* R = RegionRoots.back();
1391 RegionRoots.pop_back();
1392
1393 // Check if we have already processed this region.
1394 if (Marked.count(R)) continue;
1395
1396 // Mark this region as processed. This is needed for termination in case
1397 // a region is referenced more than once.
1398 Marked.insert(R);
1399
1400 // Mark the symbol for any live SymbolicRegion as "live". This means we
1401 // should continue to track that symbol.
1402 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1403 SymReaper.markLive(SymR->getSymbol());
1404
1405 // Get the data binding for R (if any).
1406 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1407 if (Xptr) {
1408 SVal X = *Xptr;
1409 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1410
1411 // If X is a region, then add it the RegionRoots.
1412 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1413 RegionRoots.push_back(RegionX->getRegion());
1414 }
1415
1416 // Get the subregions of R. These are RegionRoots as well since they
1417 // represent values that are also bound to R.
1418 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1419 if (!SRptr) continue;
1420 SubRegionsTy SR = *SRptr;
1421
1422 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1423 RegionRoots.push_back(*I);
1424 }
1425
1426 // We have now scanned the store, marking reachable regions and symbols
1427 // as live. We now remove all the regions that are dead from the store
1428 // as well as update DSymbols with the set symbols that are now dead.
1429 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1430 const MemRegion* R = I.getKey();
1431
1432 // If this region live? Is so, none of its symbols are dead.
1433 if (Marked.count(R))
1434 continue;
1435
1436 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001437 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001438
1439 // Mark all non-live symbols that this region references as dead.
1440 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1441 SymReaper.maybeDead(SymR->getSymbol());
1442
1443 SVal X = I.getData();
1444 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1445 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
1446 }
1447
1448 return store;
1449}
1450
1451//===----------------------------------------------------------------------===//
1452// Utility methods.
1453//===----------------------------------------------------------------------===//
1454
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001455void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001456 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001457 RegionBindingsTy B = GetRegionBindings(store);
1458 OS << "Store:" << nl;
1459
1460 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1461 OS << ' '; I.getKey()->print(OS); OS << " : ";
1462 I.getData().print(OS); OS << nl;
1463 }
1464}