blob: 247c6040c817b81e526d29e5f8d03d34c871225e [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000021#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000022
23#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000024#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000025#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000026#include "llvm/Support/Compiler.h"
27
28using namespace clang;
29
Zhongxing Xubaf03a72008-11-24 09:44:56 +000030// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000031typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000032
Ted Kremenek50dc1b32008-12-24 01:05:03 +000033//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000034// Fine-grained control of RegionStoreManager.
35//===----------------------------------------------------------------------===//
36
37namespace {
38struct VISIBILITY_HIDDEN minimal_features_tag {};
39struct VISIBILITY_HIDDEN maximal_features_tag {};
40
41class VISIBILITY_HIDDEN RegionStoreFeatures {
42 bool SupportsFields;
43 bool SupportsRemaining;
44
45public:
46 RegionStoreFeatures(minimal_features_tag) :
47 SupportsFields(false), SupportsRemaining(false) {}
48
49 RegionStoreFeatures(maximal_features_tag) :
50 SupportsFields(true), SupportsRemaining(false) {}
51
52 void enableFields(bool t) { SupportsFields = t; }
53
54 bool supportsFields() const { return SupportsFields; }
55 bool supportsRemaining() const { return SupportsRemaining; }
56};
57}
58
59//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000060// Region "Views"
61//===----------------------------------------------------------------------===//
62//
63// MemRegions can be layered on top of each other. This GDM entry tracks
64// what are the MemRegions that layer a given MemRegion.
65//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000066typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000067namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
68static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000069namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000070 template<> struct GRStateTrait<RegionViewMap>
71 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
72 RegionViews> > {
73
74 static void* GDMIndex() { return &RegionViewMapIndex; }
75 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000076}
Zhongxing Xu17892752008-10-08 02:50:44 +000077
Zhongxing Xu20794942009-05-06 08:33:50 +000078// RegionCasts records the current cast type of a region.
79namespace { class VISIBILITY_HIDDEN RegionCasts {}; }
80static int RegionCastsIndex = 0;
81namespace clang {
82 template<> struct GRStateTrait<RegionCasts>
83 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
84 QualType> > {
85 static void* GDMIndex() { return &RegionCastsIndex; }
86 };
87}
88
Ted Kremenek50dc1b32008-12-24 01:05:03 +000089//===----------------------------------------------------------------------===//
90// Region "Extents"
91//===----------------------------------------------------------------------===//
92//
93// MemRegions represent chunks of memory with a size (their "extent"). This
94// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000095//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000096namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
97static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000098namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000099 template<> struct GRStateTrait<RegionExtents>
100 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
101 static void* GDMIndex() { return &RegionExtentsIndex; }
102 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000103}
104
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000105//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000106// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000107//===----------------------------------------------------------------------===//
108//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000109// This GDM entry tracks what regions have a default value if they have no bound
110// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000111//
112namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
113static int RegionDefaultValueIndex = 0;
114namespace clang {
115 template<> struct GRStateTrait<RegionDefaultValue>
116 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
117 static void* GDMIndex() { return &RegionDefaultValueIndex; }
118 };
119}
120
121//===----------------------------------------------------------------------===//
122// Main RegionStore logic.
123//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000124
Zhongxing Xu17892752008-10-08 02:50:44 +0000125namespace {
126
Ted Kremenek59e8f112009-03-03 01:35:36 +0000127class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
128 typedef llvm::DenseMap<const MemRegion*,
129 llvm::ImmutableSet<const MemRegion*> > Map;
130
131 llvm::ImmutableSet<const MemRegion*>::Factory F;
132 Map M;
133
134public:
135 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
136 Map::iterator I = M.find(Parent);
137 M.insert(std::make_pair(Parent,
138 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
139 }
140
141 ~RegionStoreSubRegionMap() {}
142
Ted Kremenek5dc27462009-03-03 02:51:43 +0000143 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000144 Map::iterator I = M.find(Parent);
145
146 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000147 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148
149 llvm::ImmutableSet<const MemRegion*> S = I->second;
150 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
151 SI != SE; ++SI) {
152 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000153 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000154 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000155
156 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000157 }
158};
159
Zhongxing Xu17892752008-10-08 02:50:44 +0000160class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000161 const RegionStoreFeatures Features;
Zhongxing Xu17892752008-10-08 02:50:44 +0000162 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000163 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000164
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000165 const MemRegion* SelfRegion;
166 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000167
168public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000169 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000170 : StoreManager(mgr, true),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000171 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000172 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000173 RVFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000174 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000175 if (const ObjCMethodDecl* MD =
176 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
177 SelfDecl = MD->getSelfDecl();
178 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000179
180 virtual ~RegionStoreManager() {}
181
Ted Kremenek14453bf2009-03-03 19:02:42 +0000182 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000183
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000184 /// getLValueString - Returns an SVal representing the lvalue of a
185 /// StringLiteral. Within RegionStore a StringLiteral has an
186 /// associated StringRegion, and the lvalue of a StringLiteral is
187 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000188 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000189
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000190 /// getLValueCompoundLiteral - Returns an SVal representing the
191 /// lvalue of a compound literal. Within RegionStore a compound
192 /// literal has an associated region, and the lvalue of the
193 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000194 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000195
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000196 /// getLValueVar - Returns an SVal that represents the lvalue of a
197 /// variable. Within RegionStore a variable has an associated
198 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000199 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000200
Ted Kremenek67f28532009-06-17 22:02:04 +0000201 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000202
Ted Kremenek67f28532009-06-17 22:02:04 +0000203 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000204
Ted Kremenek67f28532009-06-17 22:02:04 +0000205 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000206
Ted Kremenek67f28532009-06-17 22:02:04 +0000207 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000208 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000209
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000210
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000211 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
212 /// type. 'Array' represents the lvalue of the array being decayed
213 /// to a pointer, and the returned SVal represents the decayed
214 /// version of that lvalue (i.e., a pointer to the first element of
215 /// the array). This is called by GRExprEngine when evaluating
216 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000217 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000218
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000219 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000220 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000221
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000222 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000223
224 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
225 /// 'this' object (C++). When used when analyzing a normal function this
226 /// method returns NULL.
227 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000228 if (!SelfDecl)
229 return 0;
230
231 if (!SelfRegion) {
232 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
233 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
234 MRMgr.getHeapRegion());
235 }
236
237 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000238 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000239
240 //===-------------------------------------------------------------------===//
241 // Binding values to regions.
242 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000243
Ted Kremenek67f28532009-06-17 22:02:04 +0000244 const GRState *Bind(const GRState *state, Loc LV, SVal V);
245
246 const GRState *BindCompoundLiteral(const GRState *state,
247 const CompoundLiteralExpr* CL, SVal V);
248
249 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
250
251 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
252 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000253 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000254
Ted Kremenek67f28532009-06-17 22:02:04 +0000255 /// BindStruct - Bind a compound value to a structure.
256 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
257
258 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
259
260 /// KillStruct - Set the entire struct to unknown.
261 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
262
263 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
264
265 Store Remove(Store store, Loc LV);
266
267 //===------------------------------------------------------------------===//
268 // Loading values from regions.
269 //===------------------------------------------------------------------===//
270
271 /// The high level logic for this method is this:
272 /// Retrieve (L)
273 /// if L has binding
274 /// return L's binding
275 /// else if L is in killset
276 /// return unknown
277 /// else
278 /// if L is on stack or heap
279 /// return undefined
280 /// else
281 /// return symbolic
282 SVal Retrieve(const GRState *state, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000283
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000284 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000285
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000286 SVal RetrieveField(const GRState *state, const FieldRegion *R);
287
288 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000289
290 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
291
292 SVal CastRetrievedVal(SVal val, const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000293
Ted Kremenek67f28532009-06-17 22:02:04 +0000294 /// Retrieve the values in a struct and return a CompoundVal, used when doing
295 /// struct copy:
296 /// struct s x, y;
297 /// x = y;
298 /// y's value is retrieved by this method.
299 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
300
301 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
302
303 //===------------------------------------------------------------------===//
304 // State pruning.
305 //===------------------------------------------------------------------===//
306
307 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
308 /// It returns a new Store with these values removed.
309 Store RemoveDeadBindings(const GRState *state, Stmt* Loc, SymbolReaper& SymReaper,
310 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
311
312 //===------------------------------------------------------------------===//
313 // Region "extents".
314 //===------------------------------------------------------------------===//
315
316 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
317 SVal getSizeInElements(const GRState *state, const MemRegion* R);
318
319 //===------------------------------------------------------------------===//
320 // Region "views".
321 //===------------------------------------------------------------------===//
322
323 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
324 const MemRegion* Base);
325
326 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
327 const MemRegion* Base);
328
329 //===------------------------------------------------------------------===//
330 // Utility methods.
331 //===------------------------------------------------------------------===//
332
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000333 const GRState *setCastType(const GRState *state, const MemRegion* R,
334 QualType T);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000335
Zhongxing Xu82037252009-07-14 01:12:46 +0000336 const QualType *getCastType(const GRState *state, const MemRegion *R) {
337 return state->get<RegionCasts>(R);
338 }
339
Zhongxing Xu17892752008-10-08 02:50:44 +0000340 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000341 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000342 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000343
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000344 void print(Store store, llvm::raw_ostream& Out, const char* nl,
345 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000346
347 void iterBindings(Store store, BindingsHandler& f) {
348 // FIXME: Implement.
349 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000350
Ted Kremenek67f28532009-06-17 22:02:04 +0000351 // FIXME: Remove.
352 BasicValueFactory& getBasicVals() {
353 return StateMgr.getBasicVals();
354 }
355
356 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000357 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000358};
359
360} // end anonymous namespace
361
Zhongxing Xu82037252009-07-14 01:12:46 +0000362static bool isGenericPtr(ASTContext &Ctx, QualType Ty) {
363 if (Ty->isObjCIdType() || Ty->isObjCQualifiedIdType())
364 return true;
365
366 while (true) {
367 Ty = Ctx.getCanonicalType(Ty);
368
369 if (Ty->isVoidType())
370 return true;
371
Ted Kremenek35366a62009-07-17 17:50:17 +0000372 if (const PointerType *PT = Ty->getAsPointerType()) {
Zhongxing Xu82037252009-07-14 01:12:46 +0000373 Ty = PT->getPointeeType();
374 continue;
375 }
376
377 break;
378 }
379
380 return false;
381}
382
Ted Kremenek9af46f52009-06-16 22:36:44 +0000383//===----------------------------------------------------------------------===//
384// RegionStore creation.
385//===----------------------------------------------------------------------===//
386
387StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
388 RegionStoreFeatures F = maximal_features_tag();
389 return new RegionStoreManager(StMgr, F);
390}
391
392StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
393 RegionStoreFeatures F = minimal_features_tag();
394 F.enableFields(true);
395 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000396}
397
Ted Kremenek14453bf2009-03-03 19:02:42 +0000398SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000399 RegionBindingsTy B = GetRegionBindings(state->getStore());
400 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
401
402 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
403 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
404 M->add(R->getSuperRegion(), R);
405 }
406
Ted Kremenek14453bf2009-03-03 19:02:42 +0000407 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000408}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000409
Ted Kremenek9af46f52009-06-16 22:36:44 +0000410//===----------------------------------------------------------------------===//
411// getLValueXXX methods.
412//===----------------------------------------------------------------------===//
413
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000414/// getLValueString - Returns an SVal representing the lvalue of a
415/// StringLiteral. Within RegionStore a StringLiteral has an
416/// associated StringRegion, and the lvalue of a StringLiteral is the
417/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000418SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000419 const StringLiteral* S) {
420 return loc::MemRegionVal(MRMgr.getStringRegion(S));
421}
422
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000423/// getLValueVar - Returns an SVal that represents the lvalue of a
424/// variable. Within RegionStore a variable has an associated
425/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000426SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000427 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
428}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000429
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000430/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
431/// of a compound literal. Within RegionStore a compound literal
432/// has an associated region, and the lvalue of the compound literal
433/// is the lvalue of that region.
434SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000435RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000436 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000437 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
438}
439
Ted Kremenek67f28532009-06-17 22:02:04 +0000440SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000441 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000442 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000443}
444
Ted Kremenek67f28532009-06-17 22:02:04 +0000445SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000446 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000447 return getLValueFieldOrIvar(St, Base, D);
448}
449
Ted Kremenek67f28532009-06-17 22:02:04 +0000450SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000451 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000452 if (Base.isUnknownOrUndef())
453 return Base;
454
455 Loc BaseL = cast<Loc>(Base);
456 const MemRegion* BaseR = 0;
457
458 switch (BaseL.getSubKind()) {
459 case loc::MemRegionKind:
460 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
461 break;
462
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000463 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000464 // These are anormal cases. Flag an undefined value.
465 return UndefinedVal();
466
467 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000468 // While these seem funny, this can happen through casts.
469 // FIXME: What we should return is the field offset. For example,
470 // add the field offset to the integer value. That way funny things
471 // like this work properly: &(((struct foo *) 0xa)->f)
472 return Base;
473
474 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000475 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000476 return Base;
477 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000478
479 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
480 // of FieldDecl.
481 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
482 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000483
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000484 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000485}
486
Ted Kremenek67f28532009-06-17 22:02:04 +0000487SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000488 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000489 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000490
Ted Kremenekde7ec632009-03-09 22:44:49 +0000491 // If the base is an unknown or undefined value, just return it back.
492 // FIXME: For absolute pointer addresses, we just return that value back as
493 // well, although in reality we should return the offset added to that
494 // value.
495 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000496 return Base;
497
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000498 // Only handle integer offsets... for now.
499 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000500 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000501
Zhongxing Xuce760782009-05-09 13:20:07 +0000502 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000503
504 // Pointer of any type can be cast and used as array base.
505 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
506
Ted Kremenek46537392009-07-16 01:33:37 +0000507 // Convert the offset to the appropriate size and signedness.
508 Offset = ValMgr.convertToArrayIndex(Offset);
509
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000510 if (!ElemR) {
511 //
512 // If the base region is not an ElementRegion, create one.
513 // This can happen in the following example:
514 //
515 // char *p = __builtin_alloc(10);
516 // p[1] = 8;
517 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000518 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000519 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000520 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000521 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000522 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000523
524 SVal BaseIdx = ElemR->getIndex();
525
526 if (!isa<nonloc::ConcreteInt>(BaseIdx))
527 return UnknownVal();
528
529 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
530 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
531 assert(BaseIdxI.isSigned());
532
Ted Kremenek46537392009-07-16 01:33:37 +0000533 // Compute the new index.
534 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000535
Ted Kremenek46537392009-07-16 01:33:37 +0000536 // Construct the new ElementRegion.
537 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000538 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
539 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000540}
541
Ted Kremenek9af46f52009-06-16 22:36:44 +0000542//===----------------------------------------------------------------------===//
543// Extents for regions.
544//===----------------------------------------------------------------------===//
545
Ted Kremenek67f28532009-06-17 22:02:04 +0000546SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000547 const MemRegion *R) {
548
549 switch (R->getKind()) {
550 case MemRegion::MemSpaceRegionKind:
551 assert(0 && "Cannot index into a MemSpace");
552 return UnknownVal();
553
554 case MemRegion::CodeTextRegionKind:
555 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000556 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000557
558 // Not yet handled.
559 case MemRegion::AllocaRegionKind:
560 case MemRegion::CompoundLiteralRegionKind:
561 case MemRegion::ElementRegionKind:
562 case MemRegion::FieldRegionKind:
563 case MemRegion::ObjCIvarRegionKind:
564 case MemRegion::ObjCObjectRegionKind:
565 case MemRegion::SymbolicRegionKind:
566 return UnknownVal();
567
568 case MemRegion::StringRegionKind: {
569 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
570 // We intentionally made the size value signed because it participates in
571 // operations with signed indices.
572 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000573 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000574
575 // TypedViewRegion will soon be removed.
576 case MemRegion::TypedViewRegionKind:
577 return UnknownVal();
Zhongxing Xu20794942009-05-06 08:33:50 +0000578
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000579 case MemRegion::VarRegionKind: {
580 const VarRegion* VR = cast<VarRegion>(R);
581 // Get the type of the variable.
582 QualType T = VR->getDesugaredValueType(getContext());
583
584 // FIXME: Handle variable-length arrays.
585 if (isa<VariableArrayType>(T))
586 return UnknownVal();
587
588 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
589 // return the size as signed integer.
590 return ValMgr.makeIntVal(CAT->getSize(), false);
591 }
592
593 const QualType* CastTy = state->get<RegionCasts>(VR);
594
595 // If the VarRegion is cast to other type, compute the size with respect to
596 // that type.
597 if (CastTy) {
598 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
599 QualType VarTy = VR->getValueType(getContext());
600 uint64_t EleSize = getContext().getTypeSize(EleTy);
601 uint64_t VarSize = getContext().getTypeSize(VarTy);
602 assert(VarSize != 0);
603 return ValMgr.makeIntVal(VarSize/EleSize, false);
604 }
605
606 // Clients can use ordinary variables as if they were arrays. These
607 // essentially are arrays of size 1.
608 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000609 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000610
611 case MemRegion::BEG_DECL_REGIONS:
612 case MemRegion::END_DECL_REGIONS:
613 case MemRegion::BEG_TYPED_REGIONS:
614 case MemRegion::END_TYPED_REGIONS:
615 assert(0 && "Infeasible region");
616 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000617 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000618
619 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000620 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000621}
622
Ted Kremenek67f28532009-06-17 22:02:04 +0000623const GRState *RegionStoreManager::setExtent(const GRState *state,
624 const MemRegion *region,
625 SVal extent) {
626 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000627}
628
629//===----------------------------------------------------------------------===//
630// Location and region casting.
631//===----------------------------------------------------------------------===//
632
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000633/// ArrayToPointer - Emulates the "decay" of an array to a pointer
634/// type. 'Array' represents the lvalue of the array being decayed
635/// to a pointer, and the returned SVal represents the decayed
636/// version of that lvalue (i.e., a pointer to the first element of
637/// the array). This is called by GRExprEngine when evaluating casts
638/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000639SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000640 if (!isa<loc::MemRegionVal>(Array))
641 return UnknownVal();
642
643 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
644 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
645
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000646 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000647 return UnknownVal();
648
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000649 // Strip off typedefs from the ArrayRegion's ValueType.
650 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000651 ArrayType *AT = cast<ArrayType>(T);
652 T = AT->getElementType();
653
Ted Kremenek75185b52009-07-16 00:00:11 +0000654 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
655 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000656
657 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000658}
659
Ted Kremenek9af46f52009-06-16 22:36:44 +0000660//===----------------------------------------------------------------------===//
661// Pointer arithmetic.
662//===----------------------------------------------------------------------===//
663
Zhongxing Xu262fd032009-05-20 09:00:16 +0000664SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000665 BinaryOperator::Opcode Op, Loc L, NonLoc R,
666 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000667 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000668 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000669 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000670
Zhongxing Xua1718c72009-04-03 07:33:13 +0000671 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000672 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000673
Ted Kremenek3bccf082009-07-11 00:58:27 +0000674 switch (MR->getKind()) {
675 case MemRegion::SymbolicRegionKind: {
676 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
677 QualType T;
678 // If the SymbolicRegion was cast to another type, use that type.
679 if (const QualType *t = state->get<RegionCasts>(SR))
680 T = *t;
681 else {
682 // Otherwise use the symbol's type.
683 SymbolRef Sym = SR->getSymbol();
684 T = Sym->getType(getContext());
685 }
686
Ted Kremenek35366a62009-07-17 17:50:17 +0000687 QualType EleTy = T->getAsPointerType()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000688 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
689 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
690 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000691 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000692 case MemRegion::AllocaRegionKind: {
693 // Get the alloca region's current cast type.
694 const AllocaRegion *AR = cast<AllocaRegion>(MR);
695 QualType T = *(state->get<RegionCasts>(AR));
Ted Kremenek35366a62009-07-17 17:50:17 +0000696 QualType EleTy = T->getAsPointerType()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000697 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
698 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
699 break;
700 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000701
Ted Kremenek3bccf082009-07-11 00:58:27 +0000702 case MemRegion::ElementRegionKind: {
703 ER = cast<ElementRegion>(MR);
704 break;
705 }
706
707 // Not yet handled.
708 case MemRegion::VarRegionKind:
709 case MemRegion::StringRegionKind:
710 case MemRegion::CompoundLiteralRegionKind:
711 case MemRegion::FieldRegionKind:
712 case MemRegion::ObjCObjectRegionKind:
713 case MemRegion::ObjCIvarRegionKind:
714 return UnknownVal();
715
716 // TypedViewRegion will soon be removed.
717 case MemRegion::TypedViewRegionKind:
718 return UnknownVal();
719
720 case MemRegion::CodeTextRegionKind:
721 // Technically this can happen if people do funny things with casts.
722 return UnknownVal();
723
724 case MemRegion::MemSpaceRegionKind:
725 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
726 return UnknownVal();
727
728 case MemRegion::BEG_DECL_REGIONS:
729 case MemRegion::END_DECL_REGIONS:
730 case MemRegion::BEG_TYPED_REGIONS:
731 case MemRegion::END_TYPED_REGIONS:
732 assert(0 && "Infeasible region");
733 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000734 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000735
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000736 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000737 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
738 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
739
740 // Only support concrete integer indexes for now.
741 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000742 // FIXME: Should use SValuator here.
743 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
744 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000745 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000746 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
747 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000748 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000749 }
750
751 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000752}
753
Ted Kremenek9af46f52009-06-16 22:36:44 +0000754//===----------------------------------------------------------------------===//
755// Loading values from regions.
756//===----------------------------------------------------------------------===//
757
Ted Kremeneka6275a52009-07-15 02:31:43 +0000758static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
759 RTy = Ctx.getCanonicalType(RTy);
760 UsedTy = Ctx.getCanonicalType(UsedTy);
761
762 if (RTy == UsedTy)
763 return false;
764
Ted Kremenek25c54572009-07-20 22:58:02 +0000765
766 // Recursively check the types. We basically want to see if a pointer value
767 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
768 // represents a reinterpretation.
769 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
770 const PointerType *PRTy = RTy->getAsPointerType();
771 const PointerType *PUsedTy = UsedTy->getAsPointerType();
772
773 return PUsedTy && PRTy &&
774 IsReinterpreted(PRTy->getPointeeType(),
775 PUsedTy->getPointeeType(), Ctx);
776 }
777
778 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000779}
780
Ted Kremenek67f28532009-06-17 22:02:04 +0000781SVal RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
782
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000783 assert(!isa<UnknownVal>(L) && "location unknown");
784 assert(!isa<UndefinedVal>(L) && "location undefined");
785
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000786 // FIXME: Is this even possible? Shouldn't this be treated as a null
787 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000788 if (isa<loc::ConcreteInt>(L))
789 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000790
Ted Kremenek67f28532009-06-17 22:02:04 +0000791 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000792
Zhongxing Xu91844122009-05-20 09:18:48 +0000793 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000794 // Example:
795 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000796 // char* p = alloca();
797 // read(p);
798 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000799 if (isa<AllocaRegion>(MR))
Zhongxing Xua1718c72009-04-03 07:33:13 +0000800 return UnknownVal();
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000801
802 if (isa<SymbolicRegion>(MR)) {
803 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000804 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000805 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000806 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
807 }
808
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000809 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
810 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000811 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000812 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000813
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000814 // FIXME: We should eventually handle funny addressing. e.g.:
815 //
816 // int x = ...;
817 // int *p = &x;
818 // char *q = (char*) p;
819 // char c = *q; // returns the first byte of 'x'.
820 //
821 // Such funny addressing will occur due to layering of regions.
822
Ted Kremeneka6275a52009-07-15 02:31:43 +0000823 ASTContext &Ctx = getContext();
824 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000825 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
826 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000827 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000828 assert(Ctx.getCanonicalType(RTy) ==
829 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000830 }
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000831
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000832 if (RTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000833 return RetrieveStruct(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000834
835 if (RTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +0000836 return RetrieveArray(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000837
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000838 // FIXME: handle Vector types.
839 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000840 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +0000841
842 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek25c54572009-07-20 22:58:02 +0000843 return CastRetrievedVal(RetrieveField(state, FR), FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000844
845 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek25c54572009-07-20 22:58:02 +0000846 return CastRetrievedVal(RetrieveElement(state, ER), ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000847
Ted Kremenek25c54572009-07-20 22:58:02 +0000848 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
849 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T);
850
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 Kremenek869fb4a2008-12-24 07:46:32 +0000858 // The location does not have a bound value. This means that it has
859 // the value it had upon its creation and/or entry to the analyzed
860 // function/method. These are either symbolic values or 'undefined'.
861
862 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000863 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
864 const VarDecl *VD = VR->getDecl();
865
Ted Kremenekcec35252009-03-04 00:23:05 +0000866 if (VD == SelfDecl)
867 return loc::MemRegionVal(getSelfRegion(0));
868
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000869 if (VR->hasGlobalsOrParametersStorage())
870 return ValMgr.getRegionValueSymbolValOrUnknown(VR, VD->getType());
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000871 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000872
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000873 if (R->hasHeapOrStackStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000874 // All stack variables are considered to have undefined values
875 // upon creation. All heap allocated blocks are considered to
876 // have undefined values as well unless they are explicitly bound
877 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000878 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000879 }
880
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000881 // If the region is already cast to another type, use that type to create the
882 // symbol value.
883 if (const QualType *p = state->get<RegionCasts>(R)) {
884 QualType T = *p;
Ted Kremenek35366a62009-07-17 17:50:17 +0000885 RTy = T->getAsPointerType()->getPointeeType();
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000886 }
887
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000888 // All other values are symbolic.
889 return ValMgr.getRegionValueSymbolValOrUnknown(R, RTy);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000890}
891
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000892SVal RegionStoreManager::RetrieveElement(const GRState* state,
893 const ElementRegion* R) {
894 // Check if the region has a binding.
895 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +0000896 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000897 return *V;
898
Ted Kremenek921109a2009-07-01 23:19:52 +0000899 const MemRegion* superR = R->getSuperRegion();
900
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000901 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +0000902 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000903 const StringLiteral *Str = StrR->getStringLiteral();
904 SVal Idx = R->getIndex();
905 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
906 int64_t i = CI->getValue().getSExtValue();
907 char c;
908 if (i == Str->getByteLength())
909 c = '\0';
910 else
911 c = Str->getStrData()[i];
912 return ValMgr.makeIntVal(c, getContext().CharTy);
913 }
914 }
915
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000916 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +0000917 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000918 if (D->hasConjuredSymbol())
919 return ValMgr.getRegionValueSymbolVal(R);
920 else
921 return *D;
922 }
923
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000924 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +0000925 if (const SVal *V = B.lookup(superR)) {
926 if (SymbolRef parentSym = V->getAsSymbol())
927 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
928
929 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +0000930 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +0000931 }
Ted Kremenek921109a2009-07-01 23:19:52 +0000932
933 if (R->hasHeapStorage()) {
934 // FIXME: If the region has heap storage and we know nothing special
935 // about its bindings, should we instead return UnknownVal? Seems like
936 // we should only return UndefinedVal in the cases where we know the value
937 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000938 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +0000939 }
940
Ted Kremenekdc147262009-07-02 22:02:15 +0000941 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +0000942 // Currently we don't reason specially about Clang-style vectors. Check
943 // if superR is a vector and if so return Unknown.
944 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
945 if (typedSuperR->getValueType(getContext())->isVectorType())
946 return UnknownVal();
947 }
948
949 return UndefinedVal();
950 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000951
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))
Ted Kremenek35366a62009-07-17 17:50:17 +0000957 Ty = (*p)->getAsPointerType()->getPointeeType();
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000958
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000959 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000960}
961
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000962SVal RegionStoreManager::RetrieveField(const GRState* state,
963 const FieldRegion* R) {
964 QualType Ty = R->getValueType(getContext());
965
966 // Check if the region has a binding.
967 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +0000968 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000969 return *V;
970
Ted Kremenek8b2ba312009-07-01 23:30:34 +0000971 const MemRegion* superR = R->getSuperRegion();
972 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000973 if (D->hasConjuredSymbol())
974 return ValMgr.getRegionValueSymbolVal(R);
975
976 if (D->isZeroConstant())
977 return ValMgr.makeZeroVal(Ty);
978
979 if (D->isUnknown())
980 return *D;
981
982 assert(0 && "Unknown default value");
983 }
984
Ted Kremenekdc147262009-07-02 22:02:15 +0000985 // FIXME: Is this correct? Should it be UnknownVal?
986 if (R->hasHeapStorage())
987 return UndefinedVal();
988
989 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000990 return UndefinedVal();
991
992 // If the region is already cast to another type, use that type to create the
993 // symbol value.
994 if (const QualType *p = state->get<RegionCasts>(R)) {
995 QualType tmp = *p;
Ted Kremenek35366a62009-07-17 17:50:17 +0000996 Ty = tmp->getAsPointerType()->getPointeeType();
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000997 }
998
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000999 // All other values are symbolic.
1000 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001001}
1002
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001003SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1004 const ObjCIvarRegion* R) {
1005
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001006 // Check if the region has a binding.
1007 RegionBindingsTy B = GetRegionBindings(state->getStore());
1008
1009 if (const SVal* V = B.lookup(R))
1010 return *V;
1011
1012 const MemRegion *superR = R->getSuperRegion();
1013
1014 // Check if the super region has a binding.
1015 if (const SVal *V = B.lookup(superR)) {
1016 if (SymbolRef parentSym = V->getAsSymbol())
1017 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1018
1019 // Other cases: give up.
1020 return UnknownVal();
1021 }
1022
Ted Kremenek25c54572009-07-20 22:58:02 +00001023 return RetrieveLazySymbol(state, R);
1024}
1025
1026SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1027 const TypedRegion *R) {
1028
1029 QualType valTy = R->getValueType(getContext());
1030
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001031 // If the region is already cast to another type, use that type to create the
1032 // symbol value.
Ted Kremenek25c54572009-07-20 22:58:02 +00001033 if (const QualType *ty = state->get<RegionCasts>(R)) {
1034 if (const PointerType *PT = (*ty)->getAsPointerType()) {
1035 QualType castTy = PT->getPointeeType();
1036
1037 if (!IsReinterpreted(valTy, castTy, getContext()))
1038 valTy = castTy;
1039 }
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001040 }
1041
1042 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001043 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001044}
1045
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001046SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1047 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001048 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001049 assert(T->isStructureType());
1050
Zhongxing Xub7507d12009-06-11 07:27:30 +00001051 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001052 RecordDecl* RD = RT->getDecl();
1053 assert(RD->isDefinition());
1054
1055 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1056
Ted Kremenek67f28532009-06-17 22:02:04 +00001057 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1058 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001059 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001060
Douglas Gregore267ff32008-12-11 20:41:00 +00001061 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1062 FieldEnd = Fields.rend();
1063 Field != FieldEnd; ++Field) {
1064 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001065 QualType FTy = (*Field)->getType();
Ted Kremenek67f28532009-06-17 22:02:04 +00001066 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001067 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1068 }
1069
Zhongxing Xud91ee272009-06-23 09:02:15 +00001070 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001071}
1072
Ted Kremenek67f28532009-06-17 22:02:04 +00001073SVal RegionStoreManager::RetrieveArray(const GRState *state,
1074 const TypedRegion * R) {
1075
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001076 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001077 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1078
1079 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001080 uint64_t size = CAT->getSize().getZExtValue();
1081 for (uint64_t i = 0; i < size; ++i) {
1082 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001083 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1084 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001085 QualType ETy = ER->getElementType();
Ted Kremenek67f28532009-06-17 22:02:04 +00001086 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001087 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1088 }
1089
Zhongxing Xud91ee272009-06-23 09:02:15 +00001090 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001091}
1092
Ted Kremenek25c54572009-07-20 22:58:02 +00001093SVal RegionStoreManager::CastRetrievedVal(SVal V, const TypedRegion *R,
1094 QualType castTy) {
1095
1096 if (castTy.isNull() || R->getValueType(getContext()) == castTy)
1097 return V;
1098
1099 return ValMgr.getSValuator().EvalCast(V, castTy);
1100}
1101
Ted Kremenek9af46f52009-06-16 22:36:44 +00001102//===----------------------------------------------------------------------===//
1103// Binding values to regions.
1104//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001105
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001106Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001107 const MemRegion* R = 0;
1108
1109 if (isa<loc::MemRegionVal>(L))
1110 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001111
1112 if (R) {
1113 RegionBindingsTy B = GetRegionBindings(store);
1114 return RBFactory.Remove(B, R).getRoot();
1115 }
1116
1117 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001118}
1119
Ted Kremenek67f28532009-06-17 22:02:04 +00001120const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001121 if (isa<loc::ConcreteInt>(L))
1122 return state;
1123
Ted Kremenek9af46f52009-06-16 22:36:44 +00001124 // If we get here, the location should be a region.
1125 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001126
1127 // Check if the region is a struct region.
1128 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1129 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001130 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001131
Ted Kremenek67f28532009-06-17 22:02:04 +00001132 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001133
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001134 B = RBFactory.Add(B, R, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001135
Ted Kremenek67f28532009-06-17 22:02:04 +00001136 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001137}
1138
Ted Kremenek67f28532009-06-17 22:02:04 +00001139const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001140 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001141
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001142 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001143 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001144
Ted Kremenek0964a062009-01-21 06:57:53 +00001145 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001146 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001147 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001148 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001149
Zhongxing Xud91ee272009-06-23 09:02:15 +00001150 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001151}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001152
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001153// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001154const GRState *
1155RegionStoreManager::BindCompoundLiteral(const GRState *state,
1156 const CompoundLiteralExpr* CL,
1157 SVal V) {
1158
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001159 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001160 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001161}
1162
Ted Kremenek67f28532009-06-17 22:02:04 +00001163const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001164 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001165 SVal Init) {
1166
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001167 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001168 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001169 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001170
Ted Kremenek46537392009-07-16 01:33:37 +00001171 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001172
1173 // Check if the init expr is a StringLiteral.
1174 if (isa<loc::MemRegionVal>(Init)) {
1175 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1176 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1177 const char* str = S->getStrData();
1178 unsigned len = S->getByteLength();
1179 unsigned j = 0;
1180
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001181 // Copy bytes from the string literal into the target array. Trailing bytes
1182 // in the array that are not covered by the string literal are initialized
1183 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001184 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001185 if (j >= len)
1186 break;
1187
Ted Kremenek46537392009-07-16 01:33:37 +00001188 SVal Idx = ValMgr.makeArrayIndex(i);
1189 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1190 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001191
Zhongxing Xud91ee272009-06-23 09:02:15 +00001192 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001193 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001194 }
1195
Ted Kremenek67f28532009-06-17 22:02:04 +00001196 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001197 }
1198
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001199 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001200 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001201 uint64_t i = 0;
1202
1203 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001204 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001205 if (VI == VE)
1206 break;
1207
Ted Kremenek46537392009-07-16 01:33:37 +00001208 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001209 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001210
1211 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001212 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001213 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001214 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001215 }
1216
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001217 // If the init list is shorter than the array length, set the array default
1218 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001219 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001220 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001221 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001222 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001223 }
1224 }
1225
Ted Kremenek67f28532009-06-17 22:02:04 +00001226 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001227}
1228
Ted Kremenek67f28532009-06-17 22:02:04 +00001229const GRState *
1230RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1231 SVal V) {
1232
1233 if (!Features.supportsFields())
1234 return state;
1235
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001236 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001237 assert(T->isStructureType());
1238
Ted Kremenek35366a62009-07-17 17:50:17 +00001239 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001240 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001241
1242 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001243 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001244
Ted Kremenek67f28532009-06-17 22:02:04 +00001245 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1246 // Ignore them and kill the field values.
1247 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1248 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001249
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001250 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001251 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001252
1253 RecordDecl::field_iterator FI, FE;
1254
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001255 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001256
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001257 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001258 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001259
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001260 QualType FTy = (*FI)->getType();
1261 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1262
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001263 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001264 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001265 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001266 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001267 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001268 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001269 }
1270
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001271 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001272 if (FI != FE)
1273 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001274
Ted Kremenek67f28532009-06-17 22:02:04 +00001275 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001276}
1277
Ted Kremenek67f28532009-06-17 22:02:04 +00001278const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001279 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001280
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001281 // Set the default value of the struct region to "unknown".
1282 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001283
1284 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001285 Store store = state->getStore();
1286 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001287 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001288 const MemRegion* R = I.getKey();
1289 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1290 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001291 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001292 }
1293
Ted Kremenek67f28532009-06-17 22:02:04 +00001294 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001295}
1296
Ted Kremenek9af46f52009-06-16 22:36:44 +00001297//===----------------------------------------------------------------------===//
1298// Region views.
1299//===----------------------------------------------------------------------===//
1300
Ted Kremenek67f28532009-06-17 22:02:04 +00001301const GRState *RegionStoreManager::AddRegionView(const GRState *state,
1302 const MemRegion* View,
1303 const MemRegion* Base) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001304
1305 // First, retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001306 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001307 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001308
1309 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001310 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001311
1312 // Create a new state with the new region view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001313 return state->set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001314}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001315
Ted Kremenek67f28532009-06-17 22:02:04 +00001316const GRState *RegionStoreManager::RemoveRegionView(const GRState *state,
1317 const MemRegion* View,
1318 const MemRegion* Base) {
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001319 // Retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001320 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001321
1322 // If the base region has no view, return.
1323 if (!d)
Ted Kremenek67f28532009-06-17 22:02:04 +00001324 return state;
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001325
1326 // Remove the view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001327 return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001328}
Zhongxing Xu20794942009-05-06 08:33:50 +00001329
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001330const GRState *RegionStoreManager::setCastType(const GRState *state,
1331 const MemRegion* R, QualType T) {
Zhongxing Xu82037252009-07-14 01:12:46 +00001332 // We do not record generic cast type, since we are using cast type to
1333 // invlidate regions, and generic type is meaningless for invalidating
1334 // regions.
1335 // If the region already has a cast type before, that type is preserved.
1336 // FIXME: is this the right thing to do?
1337 if (isGenericPtr(getContext(), T))
1338 return state;
Ted Kremenek67f28532009-06-17 22:02:04 +00001339 return state->set<RegionCasts>(R, T);
Zhongxing Xu20794942009-05-06 08:33:50 +00001340}
Zhongxing Xu264e9372009-05-12 10:10:00 +00001341
Ted Kremenek67f28532009-06-17 22:02:04 +00001342const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1343 const MemRegion* R, SVal V) {
1344 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001345}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001346
1347//===----------------------------------------------------------------------===//
1348// State pruning.
1349//===----------------------------------------------------------------------===//
1350
1351static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1352 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1353 const MemRegion *R = XR->getRegion();
1354
1355 while (R) {
1356 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1357 SymReaper.markLive(SR->getSymbol());
1358 return;
1359 }
1360
1361 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1362 R = SR->getSuperRegion();
1363 continue;
1364 }
1365
1366 break;
1367 }
1368
1369 return;
1370 }
1371
1372 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1373 SymReaper.markLive(*SI);
1374}
1375
Ted Kremenek67f28532009-06-17 22:02:04 +00001376Store RegionStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001377 SymbolReaper& SymReaper,
1378 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001379{
Ted Kremenek9af46f52009-06-16 22:36:44 +00001380 Store store = state->getStore();
1381 RegionBindingsTy B = GetRegionBindings(store);
1382
1383 // Lazily constructed backmap from MemRegions to SubRegions.
1384 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1385 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1386
1387 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
1388 // the ability to reuse memory. This way we can keep TmpAlloc around as
1389 // an instance variable of RegionStoreManager (avoiding repeated malloc
1390 // overhead).
1391 llvm::BumpPtrAllocator TmpAlloc;
1392
1393 // Factory objects.
1394 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1395 SubRegionsTy::Factory SubRegF(TmpAlloc);
1396
1397 // The backmap from regions to subregions.
1398 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1399
1400 // Do a pass over the regions in the store. For VarRegions we check if
1401 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001402 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001403 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1404
1405 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1406 IntermediateRoots.push_back(I.getKey());
1407 }
1408
1409 while (!IntermediateRoots.empty()) {
1410 const MemRegion* R = IntermediateRoots.back();
1411 IntermediateRoots.pop_back();
1412
1413 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001414 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001415 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001416 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001417 }
1418 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1419 if (SymReaper.isLive(SR->getSymbol()))
1420 RegionRoots.push_back(SR);
1421 }
1422 else {
1423 // Get the super region for R.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001424 const MemRegion* superR = cast<SubRegion>(R)->getSuperRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001425
1426 // Get the current set of subregions for SuperR.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001427 const SubRegionsTy* SRptr = SubRegMap.lookup(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001428 SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
1429
1430 // Add R to the subregions of SuperR.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001431 SubRegMap = SubRegMapF.Add(SubRegMap, superR, SubRegF.Add(SRs, R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001432
1433 // Super region may be VarRegion or subregion of another VarRegion. Add it
1434 // to the work list.
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001435 if (isa<SubRegion>(superR))
1436 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001437 }
1438 }
1439
1440 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1441 // of the store. We want to find all live symbols and dead regions.
1442 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1443
1444 while (!RegionRoots.empty()) {
1445 // Dequeue the next region on the worklist.
1446 const MemRegion* R = RegionRoots.back();
1447 RegionRoots.pop_back();
1448
1449 // Check if we have already processed this region.
1450 if (Marked.count(R)) continue;
1451
1452 // Mark this region as processed. This is needed for termination in case
1453 // a region is referenced more than once.
1454 Marked.insert(R);
1455
1456 // Mark the symbol for any live SymbolicRegion as "live". This means we
1457 // should continue to track that symbol.
1458 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1459 SymReaper.markLive(SymR->getSymbol());
1460
1461 // Get the data binding for R (if any).
1462 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1463 if (Xptr) {
1464 SVal X = *Xptr;
1465 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1466
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001467 // If X is a region, then add it to the RegionRoots.
1468 if (const MemRegion *RX = X.getAsRegion()) {
1469 RegionRoots.push_back(RX);
1470
1471 // Mark the super region of the RX as live.
1472 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1473 // 'y' => element region. 'x' is its super region.
1474 // We only add one level super region for now.
Zhongxing Xu5ff8e8c2009-07-01 02:12:57 +00001475 // FIXME: maybe multiple level of super regions should be added.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001476 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1477 RegionRoots.push_back(SR->getSuperRegion());
1478 }
1479 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001480 }
1481
1482 // Get the subregions of R. These are RegionRoots as well since they
1483 // represent values that are also bound to R.
1484 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1485 if (!SRptr) continue;
1486 SubRegionsTy SR = *SRptr;
1487
1488 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1489 RegionRoots.push_back(*I);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001490
Ted Kremenek9af46f52009-06-16 22:36:44 +00001491 }
1492
1493 // We have now scanned the store, marking reachable regions and symbols
1494 // as live. We now remove all the regions that are dead from the store
1495 // as well as update DSymbols with the set symbols that are now dead.
1496 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1497 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001498 // If this region live? Is so, none of its symbols are dead.
1499 if (Marked.count(R))
1500 continue;
1501
1502 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001503 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001504
1505 // Mark all non-live symbols that this region references as dead.
1506 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1507 SymReaper.maybeDead(SymR->getSymbol());
1508
1509 SVal X = I.getData();
1510 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1511 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
1512 }
1513
1514 return store;
1515}
1516
1517//===----------------------------------------------------------------------===//
1518// Utility methods.
1519//===----------------------------------------------------------------------===//
1520
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001521void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001522 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001523 RegionBindingsTy B = GetRegionBindings(store);
1524 OS << "Store:" << nl;
1525
Ted Kremenek6f9b3a42009-07-13 23:53:06 +00001526 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
1527 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001528}