blob: 398babc9d7fdd78d82614753fae083be7d9541e1 [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
Ted Kremenek356e9d62009-07-22 04:35:42 +000030#define HEAP_UNDEFINED 0
31
Zhongxing Xubaf03a72008-11-24 09:44:56 +000032// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000033typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000034
Ted Kremenek50dc1b32008-12-24 01:05:03 +000035//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000036// Fine-grained control of RegionStoreManager.
37//===----------------------------------------------------------------------===//
38
39namespace {
40struct VISIBILITY_HIDDEN minimal_features_tag {};
41struct VISIBILITY_HIDDEN maximal_features_tag {};
42
43class VISIBILITY_HIDDEN RegionStoreFeatures {
44 bool SupportsFields;
45 bool SupportsRemaining;
46
47public:
48 RegionStoreFeatures(minimal_features_tag) :
49 SupportsFields(false), SupportsRemaining(false) {}
50
51 RegionStoreFeatures(maximal_features_tag) :
52 SupportsFields(true), SupportsRemaining(false) {}
53
54 void enableFields(bool t) { SupportsFields = t; }
55
56 bool supportsFields() const { return SupportsFields; }
57 bool supportsRemaining() const { return SupportsRemaining; }
58};
59}
60
61//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000062// Region "Extents"
63//===----------------------------------------------------------------------===//
64//
65// MemRegions represent chunks of memory with a size (their "extent"). This
66// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000067//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000068namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
69static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000070namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000071 template<> struct GRStateTrait<RegionExtents>
72 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
73 static void* GDMIndex() { return &RegionExtentsIndex; }
74 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000075}
76
Ted Kremenek50dc1b32008-12-24 01:05:03 +000077//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000078// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000079//===----------------------------------------------------------------------===//
80//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000081// This GDM entry tracks what regions have a default value if they have no bound
82// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000083//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000084namespace {
85class VISIBILITY_HIDDEN RegionDefaultValue {
86public:
87 typedef llvm::ImmutableMap<const MemRegion*, SVal> MapTy;
88};
89}
Ted Kremenek50dc1b32008-12-24 01:05:03 +000090static int RegionDefaultValueIndex = 0;
91namespace clang {
92 template<> struct GRStateTrait<RegionDefaultValue>
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000093 : public GRStatePartialTrait<RegionDefaultValue::MapTy> {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000094 static void* GDMIndex() { return &RegionDefaultValueIndex; }
95 };
96}
97
98//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000099// Utility functions.
100//===----------------------------------------------------------------------===//
101
102static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
103 if (ty->isAnyPointerType())
104 return true;
105
106 return ty->isIntegerType() && ty->isScalarType() &&
107 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
108}
109
110//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000111// Main RegionStore logic.
112//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000113
Zhongxing Xu17892752008-10-08 02:50:44 +0000114namespace {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000115
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000116class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
117 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
118 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
119 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000120 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000121public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000122 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000123 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000124
125 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000126 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000127 return true;
128 }
129
130 I->second = F.Add(I->second, SubRegion);
131 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000132 }
133
134 ~RegionStoreSubRegionMap() {}
135
Ted Kremenek5dc27462009-03-03 02:51:43 +0000136 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000137 Map::iterator I = M.find(Parent);
138
139 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000140 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000141
142 llvm::ImmutableSet<const MemRegion*> S = I->second;
143 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
144 SI != SE; ++SI) {
145 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000146 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000147 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000148
149 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000150 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000151
152 typedef SetTy::iterator iterator;
153
154 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
155 Map::iterator I = M.find(R);
156 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
157 return std::make_pair(S.begin(), S.end());
158 }
Ted Kremenek59e8f112009-03-03 01:35:36 +0000159};
160
Zhongxing Xu17892752008-10-08 02:50:44 +0000161class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000162 const RegionStoreFeatures Features;
Zhongxing Xu17892752008-10-08 02:50:44 +0000163 RegionBindingsTy::Factory RBFactory;
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 Kremenekf7a0cf42009-07-29 21:43:22 +0000170 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000171 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000172 RBFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000173 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000174 if (const ObjCMethodDecl* MD =
175 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
176 SelfDecl = MD->getSelfDecl();
177 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000178
179 virtual ~RegionStoreManager() {}
180
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000181 SubRegionMap *getSubRegionMap(const GRState *state);
182
183 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000184
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000185 /// getLValueString - Returns an SVal representing the lvalue of a
186 /// StringLiteral. Within RegionStore a StringLiteral has an
187 /// associated StringRegion, and the lvalue of a StringLiteral is
188 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000189 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000190
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000191 /// getLValueCompoundLiteral - Returns an SVal representing the
192 /// lvalue of a compound literal. Within RegionStore a compound
193 /// literal has an associated region, and the lvalue of the
194 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000195 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000196
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000197 /// getLValueVar - Returns an SVal that represents the lvalue of a
198 /// variable. Within RegionStore a variable has an associated
199 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000200 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000201
Ted Kremenek67f28532009-06-17 22:02:04 +0000202 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000203
Ted Kremenek67f28532009-06-17 22:02:04 +0000204 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000205
Ted Kremenek67f28532009-06-17 22:02:04 +0000206 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000207
Ted Kremenek67f28532009-06-17 22:02:04 +0000208 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000209 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000210
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000211
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000212 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
213 /// type. 'Array' represents the lvalue of the array being decayed
214 /// to a pointer, and the returned SVal represents the decayed
215 /// version of that lvalue (i.e., a pointer to the first element of
216 /// the array). This is called by GRExprEngine when evaluating
217 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000218 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000219
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000220 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000221 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000222
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000223 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000224
225 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
226 /// 'this' object (C++). When used when analyzing a normal function this
227 /// method returns NULL.
228 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000229 if (!SelfDecl)
230 return 0;
231
232 if (!SelfRegion) {
233 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
234 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
235 MRMgr.getHeapRegion());
236 }
237
238 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000239 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000240
241 //===-------------------------------------------------------------------===//
242 // Binding values to regions.
243 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000244
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000245 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
246 const Expr *E, unsigned Count);
247
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000248private:
249 RegionBindingsTy RemoveSubRegionBindings(RegionBindingsTy B,
250 const MemRegion *R,
251 RegionStoreSubRegionMap &M);
252
253public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000254 const GRState *Bind(const GRState *state, Loc LV, SVal V);
255
256 const GRState *BindCompoundLiteral(const GRState *state,
257 const CompoundLiteralExpr* CL, SVal V);
258
259 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
260
261 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
262 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000263 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000264
Ted Kremenek67f28532009-06-17 22:02:04 +0000265 /// BindStruct - Bind a compound value to a structure.
266 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
267
268 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
269
270 /// KillStruct - Set the entire struct to unknown.
271 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
272
273 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
274
275 Store Remove(Store store, Loc LV);
276
277 //===------------------------------------------------------------------===//
278 // Loading values from regions.
279 //===------------------------------------------------------------------===//
280
281 /// The high level logic for this method is this:
282 /// Retrieve (L)
283 /// if L has binding
284 /// return L's binding
285 /// else if L is in killset
286 /// return unknown
287 /// else
288 /// if L is on stack or heap
289 /// return undefined
290 /// else
291 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000292 SValuator::CastResult Retrieve(const GRState *state, Loc L,
293 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000294
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000295 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000296
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000297 SVal RetrieveField(const GRState *state, const FieldRegion *R);
298
299 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000300
Ted Kremenek9031dd72009-07-21 00:12:07 +0000301 SVal RetrieveVar(const GRState *state, const VarRegion *R);
302
Ted Kremenek25c54572009-07-20 22:58:02 +0000303 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
304
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000305 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
306 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000307
Ted Kremenek67f28532009-06-17 22:02:04 +0000308 /// Retrieve the values in a struct and return a CompoundVal, used when doing
309 /// struct copy:
310 /// struct s x, y;
311 /// x = y;
312 /// y's value is retrieved by this method.
313 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
314
315 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
316
317 //===------------------------------------------------------------------===//
318 // State pruning.
319 //===------------------------------------------------------------------===//
320
321 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
322 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000323 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000324 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
325
326 //===------------------------------------------------------------------===//
327 // Region "extents".
328 //===------------------------------------------------------------------===//
329
330 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
331 SVal getSizeInElements(const GRState *state, const MemRegion* R);
332
333 //===------------------------------------------------------------------===//
334 // Region "views".
335 //===------------------------------------------------------------------===//
336
337 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
338 const MemRegion* Base);
339
340 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
341 const MemRegion* Base);
342
343 //===------------------------------------------------------------------===//
344 // Utility methods.
345 //===------------------------------------------------------------------===//
346
Zhongxing Xu17892752008-10-08 02:50:44 +0000347 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000348 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000349 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000350
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000351 void print(Store store, llvm::raw_ostream& Out, const char* nl,
352 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000353
354 void iterBindings(Store store, BindingsHandler& f) {
355 // FIXME: Implement.
356 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000357
Ted Kremenek67f28532009-06-17 22:02:04 +0000358 // FIXME: Remove.
359 BasicValueFactory& getBasicVals() {
360 return StateMgr.getBasicVals();
361 }
362
363 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000364 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000365};
366
367} // end anonymous namespace
368
Ted Kremenek9af46f52009-06-16 22:36:44 +0000369//===----------------------------------------------------------------------===//
370// RegionStore creation.
371//===----------------------------------------------------------------------===//
372
373StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
374 RegionStoreFeatures F = maximal_features_tag();
375 return new RegionStoreManager(StMgr, F);
376}
377
378StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
379 RegionStoreFeatures F = minimal_features_tag();
380 F.enableFields(true);
381 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000382}
383
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000384RegionStoreSubRegionMap*
385RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000386 RegionBindingsTy B = GetRegionBindings(state->getStore());
387 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
388
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000389 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
390 llvm::SmallVector<const SubRegion*, 10> WL;
391
392 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek59e8f112009-03-03 01:35:36 +0000393 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000394 WL.push_back(R);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000395
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000396 RegionDefaultValue::MapTy DVM = state->get<RegionDefaultValue>();
397 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
398 I != E; ++I)
399 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
400 WL.push_back(R);
401
402 // We also need to record in the subregion map "intermediate" regions that
403 // don't have direct bindings but are super regions of those that do.
404 while (!WL.empty()) {
405 const SubRegion *R = WL.back();
406 WL.pop_back();
407
408 if (Marked.count(R))
409 continue;
410
411 const MemRegion *superR = R->getSuperRegion();
Ted Kremenekd8c01922009-08-05 19:09:24 +0000412 if (M->add(superR, R))
413 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
414 WL.push_back(sr);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000415 }
416
Ted Kremenek14453bf2009-03-03 19:02:42 +0000417 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000418}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000419
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000420SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
421 return getRegionStoreSubRegionMap(state);
422}
423
Ted Kremenek9af46f52009-06-16 22:36:44 +0000424//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000425// Binding invalidation.
426//===----------------------------------------------------------------------===//
427
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000428RegionBindingsTy
429RegionStoreManager::RemoveSubRegionBindings(RegionBindingsTy B,
430 const MemRegion *R,
431 RegionStoreSubRegionMap &M) {
432
433 RegionStoreSubRegionMap::iterator I, E;
434
435 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
436 B = RemoveSubRegionBindings(B, *I, M);
437
438 return RBFactory.Remove(B, R);
439}
440
441
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000442const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
443 const MemRegion *R,
444 const Expr *E,
445 unsigned Count) {
446 ASTContext& Ctx = StateMgr.getContext();
447
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000448 // Strip away casts.
449 R = R->getBaseRegion();
450
451 // Get the mapping of regions -> subregions.
452 llvm::OwningPtr<RegionStoreSubRegionMap>
453 SubRegions(getRegionStoreSubRegionMap(state));
454
455 // Remove the bindings to subregions.
456 RegionBindingsTy B = GetRegionBindings(state->getStore());
457 B = RemoveSubRegionBindings(B, R, *SubRegions.get());
458 state = state->makeWithStore(B.getRoot());
459
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000460 if (!R->isBoundable())
461 return state;
462
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000463 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
464 isa<ObjCObjectRegion>(R)) {
465 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000466 // conjured symbol. The type of the symbol is irrelavant.
467 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000468 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000469 }
470
471 const TypedRegion *TR = cast<TypedRegion>(R);
472 QualType T = TR->getValueType(Ctx);
473
474 // FIXME: The code causes a crash when using RegionStore on the test case
475 // 'test_invalidate_cast_int' (misc-ps.m). Consider removing it
476 // permanently. Region casts are probably not too strict to handle
477 // the transient interpretation of memory. Instead we can use the QualType
478 // passed to 'Retrieve' and friends to determine the most current
479 // interpretation of memory when it is actually used.
480#if 0
481 // If the region is cast to another type, use that type.
482 if (const QualType *CastTy = getCastType(state, R)) {
483 assert(!(*CastTy)->isObjCObjectPointerType());
Ted Kremenek6217b802009-07-29 21:53:49 +0000484 QualType NewT = (*CastTy)->getAs<PointerType>()->getPointeeType();
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000485
486 // The only exception is if the original region had a location type as its
487 // value type we always want to treat the region as binding to a location.
488 // This issue can arise when pointers are casted to integers and back.
489
490 if (!(Loc::IsLocType(T) && !Loc::IsLocType(NewT)))
491 T = NewT;
492 }
493#endif
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000494
495 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000496 // FIXME: handle structs with default region value.
497 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
498
499 // No record definition. There is nothing we can do.
500 if (!RD)
501 return state;
502
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000503 // Invalidate the region by setting its default value to
504 // conjured symbol. The type of the symbol is irrelavant.
505 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
506 return setDefaultValue(state, R, V);
507 }
508
509 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000510 // Set the default value of the array to conjured symbol.
511 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
512 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000513 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000514 }
515
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000516 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
517 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
518 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000519}
520
521//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000522// getLValueXXX methods.
523//===----------------------------------------------------------------------===//
524
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000525/// getLValueString - Returns an SVal representing the lvalue of a
526/// StringLiteral. Within RegionStore a StringLiteral has an
527/// associated StringRegion, and the lvalue of a StringLiteral is the
528/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000529SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000530 const StringLiteral* S) {
531 return loc::MemRegionVal(MRMgr.getStringRegion(S));
532}
533
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000534/// getLValueVar - Returns an SVal that represents the lvalue of a
535/// variable. Within RegionStore a variable has an associated
536/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000537SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000538 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
539}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000540
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000541/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
542/// of a compound literal. Within RegionStore a compound literal
543/// has an associated region, and the lvalue of the compound literal
544/// is the lvalue of that region.
545SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000546RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000547 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000548 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
549}
550
Ted Kremenek67f28532009-06-17 22:02:04 +0000551SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000552 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000553 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000554}
555
Ted Kremenek67f28532009-06-17 22:02:04 +0000556SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000557 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000558 return getLValueFieldOrIvar(St, Base, D);
559}
560
Ted Kremenek67f28532009-06-17 22:02:04 +0000561SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000562 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000563 if (Base.isUnknownOrUndef())
564 return Base;
565
566 Loc BaseL = cast<Loc>(Base);
567 const MemRegion* BaseR = 0;
568
569 switch (BaseL.getSubKind()) {
570 case loc::MemRegionKind:
571 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
572 break;
573
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000574 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000575 // These are anormal cases. Flag an undefined value.
576 return UndefinedVal();
577
578 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000579 // While these seem funny, this can happen through casts.
580 // FIXME: What we should return is the field offset. For example,
581 // add the field offset to the integer value. That way funny things
582 // like this work properly: &(((struct foo *) 0xa)->f)
583 return Base;
584
585 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000586 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000587 return Base;
588 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000589
590 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
591 // of FieldDecl.
592 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
593 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000594
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000595 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000596}
597
Ted Kremenek67f28532009-06-17 22:02:04 +0000598SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000599 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000600 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000601
Ted Kremenekde7ec632009-03-09 22:44:49 +0000602 // If the base is an unknown or undefined value, just return it back.
603 // FIXME: For absolute pointer addresses, we just return that value back as
604 // well, although in reality we should return the offset added to that
605 // value.
606 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000607 return Base;
608
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000609 // Only handle integer offsets... for now.
610 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000611 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000612
Zhongxing Xuce760782009-05-09 13:20:07 +0000613 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000614
615 // Pointer of any type can be cast and used as array base.
616 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
617
Ted Kremenek46537392009-07-16 01:33:37 +0000618 // Convert the offset to the appropriate size and signedness.
619 Offset = ValMgr.convertToArrayIndex(Offset);
620
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000621 if (!ElemR) {
622 //
623 // If the base region is not an ElementRegion, create one.
624 // This can happen in the following example:
625 //
626 // char *p = __builtin_alloc(10);
627 // p[1] = 8;
628 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000629 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000630 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000631 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000632 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000633 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000634
635 SVal BaseIdx = ElemR->getIndex();
636
637 if (!isa<nonloc::ConcreteInt>(BaseIdx))
638 return UnknownVal();
639
640 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
641 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
642 assert(BaseIdxI.isSigned());
643
Ted Kremenek46537392009-07-16 01:33:37 +0000644 // Compute the new index.
645 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000646
Ted Kremenek46537392009-07-16 01:33:37 +0000647 // Construct the new ElementRegion.
648 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000649 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
650 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000651}
652
Ted Kremenek9af46f52009-06-16 22:36:44 +0000653//===----------------------------------------------------------------------===//
654// Extents for regions.
655//===----------------------------------------------------------------------===//
656
Ted Kremenek67f28532009-06-17 22:02:04 +0000657SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000658 const MemRegion *R) {
659
660 switch (R->getKind()) {
661 case MemRegion::MemSpaceRegionKind:
662 assert(0 && "Cannot index into a MemSpace");
663 return UnknownVal();
664
665 case MemRegion::CodeTextRegionKind:
666 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000667 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000668
669 // Not yet handled.
670 case MemRegion::AllocaRegionKind:
671 case MemRegion::CompoundLiteralRegionKind:
672 case MemRegion::ElementRegionKind:
673 case MemRegion::FieldRegionKind:
674 case MemRegion::ObjCIvarRegionKind:
675 case MemRegion::ObjCObjectRegionKind:
676 case MemRegion::SymbolicRegionKind:
677 return UnknownVal();
678
679 case MemRegion::StringRegionKind: {
680 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
681 // We intentionally made the size value signed because it participates in
682 // operations with signed indices.
683 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000684 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000685
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000686 case MemRegion::VarRegionKind: {
687 const VarRegion* VR = cast<VarRegion>(R);
688 // Get the type of the variable.
689 QualType T = VR->getDesugaredValueType(getContext());
690
691 // FIXME: Handle variable-length arrays.
692 if (isa<VariableArrayType>(T))
693 return UnknownVal();
694
695 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
696 // return the size as signed integer.
697 return ValMgr.makeIntVal(CAT->getSize(), false);
698 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000699
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000700 // Clients can use ordinary variables as if they were arrays. These
701 // essentially are arrays of size 1.
702 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000703 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000704
705 case MemRegion::BEG_DECL_REGIONS:
706 case MemRegion::END_DECL_REGIONS:
707 case MemRegion::BEG_TYPED_REGIONS:
708 case MemRegion::END_TYPED_REGIONS:
709 assert(0 && "Infeasible region");
710 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000711 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000712
713 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000714 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000715}
716
Ted Kremenek67f28532009-06-17 22:02:04 +0000717const GRState *RegionStoreManager::setExtent(const GRState *state,
718 const MemRegion *region,
719 SVal extent) {
720 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000721}
722
723//===----------------------------------------------------------------------===//
724// Location and region casting.
725//===----------------------------------------------------------------------===//
726
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000727/// ArrayToPointer - Emulates the "decay" of an array to a pointer
728/// type. 'Array' represents the lvalue of the array being decayed
729/// to a pointer, and the returned SVal represents the decayed
730/// version of that lvalue (i.e., a pointer to the first element of
731/// the array). This is called by GRExprEngine when evaluating casts
732/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000733SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000734 if (!isa<loc::MemRegionVal>(Array))
735 return UnknownVal();
736
737 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
738 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
739
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000740 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000741 return UnknownVal();
742
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000743 // Strip off typedefs from the ArrayRegion's ValueType.
744 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000745 ArrayType *AT = cast<ArrayType>(T);
746 T = AT->getElementType();
747
Ted Kremenek75185b52009-07-16 00:00:11 +0000748 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
749 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000750
751 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000752}
753
Ted Kremenek9af46f52009-06-16 22:36:44 +0000754//===----------------------------------------------------------------------===//
755// Pointer arithmetic.
756//===----------------------------------------------------------------------===//
757
Zhongxing Xu262fd032009-05-20 09:00:16 +0000758SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000759 BinaryOperator::Opcode Op, Loc L, NonLoc R,
760 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000761 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000762 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000763 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000764
Zhongxing Xua1718c72009-04-03 07:33:13 +0000765 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000766 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000767
Ted Kremenek3bccf082009-07-11 00:58:27 +0000768 switch (MR->getKind()) {
769 case MemRegion::SymbolicRegionKind: {
770 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000771 SymbolRef Sym = SR->getSymbol();
772 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000773 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000774 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
775 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
776 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000777 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000778 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000779 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000780 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000781 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000782 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
783 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
784 break;
785 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000786
Ted Kremenek3bccf082009-07-11 00:58:27 +0000787 case MemRegion::ElementRegionKind: {
788 ER = cast<ElementRegion>(MR);
789 break;
790 }
791
792 // Not yet handled.
793 case MemRegion::VarRegionKind:
794 case MemRegion::StringRegionKind:
795 case MemRegion::CompoundLiteralRegionKind:
796 case MemRegion::FieldRegionKind:
797 case MemRegion::ObjCObjectRegionKind:
798 case MemRegion::ObjCIvarRegionKind:
799 return UnknownVal();
800
Ted Kremenek3bccf082009-07-11 00:58:27 +0000801 case MemRegion::CodeTextRegionKind:
802 // Technically this can happen if people do funny things with casts.
803 return UnknownVal();
804
805 case MemRegion::MemSpaceRegionKind:
806 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
807 return UnknownVal();
808
809 case MemRegion::BEG_DECL_REGIONS:
810 case MemRegion::END_DECL_REGIONS:
811 case MemRegion::BEG_TYPED_REGIONS:
812 case MemRegion::END_TYPED_REGIONS:
813 assert(0 && "Infeasible region");
814 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000815 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000816
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000817 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000818 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
819 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
820
821 // Only support concrete integer indexes for now.
822 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000823 // FIXME: Should use SValuator here.
824 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
825 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000826 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000827 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
828 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000829 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000830 }
831
832 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000833}
834
Ted Kremenek9af46f52009-06-16 22:36:44 +0000835//===----------------------------------------------------------------------===//
836// Loading values from regions.
837//===----------------------------------------------------------------------===//
838
Ted Kremeneka6275a52009-07-15 02:31:43 +0000839static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
840 RTy = Ctx.getCanonicalType(RTy);
841 UsedTy = Ctx.getCanonicalType(UsedTy);
842
843 if (RTy == UsedTy)
844 return false;
845
Ted Kremenek25c54572009-07-20 22:58:02 +0000846
847 // Recursively check the types. We basically want to see if a pointer value
848 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
849 // represents a reinterpretation.
850 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000851 const PointerType *PRTy = RTy->getAs<PointerType>();
852 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000853
854 return PUsedTy && PRTy &&
855 IsReinterpreted(PRTy->getPointeeType(),
856 PUsedTy->getPointeeType(), Ctx);
857 }
858
859 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000860}
861
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000862SValuator::CastResult
863RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000864
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000865 assert(!isa<UnknownVal>(L) && "location unknown");
866 assert(!isa<UndefinedVal>(L) && "location undefined");
867
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000868 // FIXME: Is this even possible? Shouldn't this be treated as a null
869 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000870 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000871 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000872
Ted Kremenek67f28532009-06-17 22:02:04 +0000873 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000874
Zhongxing Xu91844122009-05-20 09:18:48 +0000875 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000876 // Example:
877 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000878 // char* p = alloca();
879 // read(p);
880 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000881 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000882 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000883
884 if (isa<SymbolicRegion>(MR)) {
885 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000886 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000887 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000888 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
889 }
890
Ted Kremenek968f0a62009-08-03 21:41:46 +0000891 if (isa<CodeTextRegion>(MR))
892 return SValuator::CastResult(state, UnknownVal());
893
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000894 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
895 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000896 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000897 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000898
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000899 // FIXME: We should eventually handle funny addressing. e.g.:
900 //
901 // int x = ...;
902 // int *p = &x;
903 // char *q = (char*) p;
904 // char c = *q; // returns the first byte of 'x'.
905 //
906 // Such funny addressing will occur due to layering of regions.
907
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000908#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000909 ASTContext &Ctx = getContext();
910 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000911 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
912 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000913 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000914 assert(Ctx.getCanonicalType(RTy) ==
915 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000916 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000917#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000918
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000919 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000920 return SValuator::CastResult(state, RetrieveStruct(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000921
922 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000923 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000924
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000925 // FIXME: handle Vector types.
926 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000927 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000928
929 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000930 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000931
932 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000933 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000934
Ted Kremenek25c54572009-07-20 22:58:02 +0000935 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000936 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000937
938 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000939 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000940
Ted Kremenek67f28532009-06-17 22:02:04 +0000941 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000942 RegionBindingsTy::data_type* V = B.lookup(R);
943
944 // Check if the region has a binding.
945 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000946 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000947
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000948 // The location does not have a bound value. This means that it has
949 // the value it had upon its creation and/or entry to the analyzed
950 // function/method. These are either symbolic values or 'undefined'.
951
Ted Kremenek356e9d62009-07-22 04:35:42 +0000952#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000953 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000954#else
955 if (R->hasStackStorage()) {
956#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000957 // All stack variables are considered to have undefined values
958 // upon creation. All heap allocated blocks are considered to
959 // have undefined values as well unless they are explicitly bound
960 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000961 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000962 }
963
Ted Kremenek356e9d62009-07-22 04:35:42 +0000964#if USE_REGION_CASTS
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000965 // If the region is already cast to another type, use that type to create the
966 // symbol value.
967 if (const QualType *p = state->get<RegionCasts>(R)) {
968 QualType T = *p;
Ted Kremenek6217b802009-07-29 21:53:49 +0000969 RTy = T->getAs<PointerType>()->getPointeeType();
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000970 }
Ted Kremenek356e9d62009-07-22 04:35:42 +0000971#endif
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000972
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000973 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000974 return SValuator::CastResult(state,
975 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000976}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000977
978
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000979
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000980SVal RegionStoreManager::RetrieveElement(const GRState* state,
981 const ElementRegion* R) {
982 // Check if the region has a binding.
983 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +0000984 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000985 return *V;
986
Ted Kremenek921109a2009-07-01 23:19:52 +0000987 const MemRegion* superR = R->getSuperRegion();
988
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000989 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +0000990 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000991 const StringLiteral *Str = StrR->getStringLiteral();
992 SVal Idx = R->getIndex();
993 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
994 int64_t i = CI->getValue().getSExtValue();
995 char c;
996 if (i == Str->getByteLength())
997 c = '\0';
998 else
999 c = Str->getStrData()[i];
1000 return ValMgr.makeIntVal(c, getContext().CharTy);
1001 }
1002 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001003
1004 // Special case: the current region represents a cast and it and the super
1005 // region both have pointer types or intptr_t types. If so, perform the
1006 // retrieve from the super region and appropriately "cast" the value.
1007 // This is needed to support OSAtomicCompareAndSwap and friends or other
1008 // loads that treat integers as pointers and vis versa.
1009 if (R->getIndex().isZeroConstant()) {
1010 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1011 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001012 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1013 QualType valTy = R->getValueType(Ctx);
1014 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1015 // Retrieve the value from the super region. This will be casted to
1016 // valTy when we return to 'Retrieve'.
1017 const SValuator::CastResult &cr = Retrieve(state,
1018 loc::MemRegionVal(superR),
1019 valTy);
1020 return cr.getSVal();
1021 }
1022 }
1023 }
1024 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001025
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001026 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +00001027 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001028 if (D->hasConjuredSymbol())
1029 return ValMgr.getRegionValueSymbolVal(R);
1030 else
1031 return *D;
1032 }
1033
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001034 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001035 if (const SVal *V = B.lookup(superR)) {
1036 if (SymbolRef parentSym = V->getAsSymbol())
1037 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001038
1039 if (V->isUnknownOrUndef())
1040 return *V;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001041
1042 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001043 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001044 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001045
Ted Kremenek356e9d62009-07-22 04:35:42 +00001046#if 0
Ted Kremenek921109a2009-07-01 23:19:52 +00001047 if (R->hasHeapStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001048 // FIXME: If the region has heap storage and we know nothing special
1049 // about its bindings, should we instead return UnknownVal? Seems like
1050 // we should only return UndefinedVal in the cases where we know the value
1051 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001052 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +00001053 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001054#endif
1055
Ted Kremenekdc147262009-07-02 22:02:15 +00001056 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +00001057 // Currently we don't reason specially about Clang-style vectors. Check
1058 // if superR is a vector and if so return Unknown.
1059 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1060 if (typedSuperR->getValueType(getContext())->isVectorType())
1061 return UnknownVal();
1062 }
1063
1064 return UndefinedVal();
1065 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001066
1067 QualType Ty = R->getValueType(getContext());
1068
Ted Kremenek356e9d62009-07-22 04:35:42 +00001069#if USE_REGION_CASTS
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001070 // If the region is already cast to another type, use that type to create the
1071 // symbol value.
1072 if (const QualType *p = state->get<RegionCasts>(R))
Ted Kremenek6217b802009-07-29 21:53:49 +00001073 Ty = (*p)->getAs<PointerType>()->getPointeeType();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001074#endif
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001075
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001076 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001077}
1078
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001079SVal RegionStoreManager::RetrieveField(const GRState* state,
1080 const FieldRegion* R) {
1081 QualType Ty = R->getValueType(getContext());
1082
1083 // Check if the region has a binding.
1084 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001085 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001086 return *V;
1087
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001088 const MemRegion* superR = R->getSuperRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001089 while (superR) {
1090 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
1091 if (SymbolRef parentSym = D->getAsSymbol())
1092 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001093
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001094 if (D->isZeroConstant())
1095 return ValMgr.makeZeroVal(Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001096
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001097 if (D->isUnknown())
1098 return *D;
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001099
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001100 assert(0 && "Unknown default value");
1101 }
1102
1103 // If our super region is a field or element itself, walk up the region
1104 // hierarchy to see if there is a default value installed in an ancestor.
1105 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1106 superR = cast<SubRegion>(superR)->getSuperRegion();
1107 continue;
1108 }
1109
1110 break;
1111 }
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001112
Ted Kremenek356e9d62009-07-22 04:35:42 +00001113#if HEAP_UNDEFINED
Ted Kremenekdc147262009-07-02 22:02:15 +00001114 // FIXME: Is this correct? Should it be UnknownVal?
1115 if (R->hasHeapStorage())
1116 return UndefinedVal();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001117#endif
Ted Kremenekdc147262009-07-02 22:02:15 +00001118
1119 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001120 return UndefinedVal();
1121
Ted Kremenek356e9d62009-07-22 04:35:42 +00001122#if USE_REGION_CASTS
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001123 // If the region is already cast to another type, use that type to create the
1124 // symbol value.
1125 if (const QualType *p = state->get<RegionCasts>(R)) {
1126 QualType tmp = *p;
Ted Kremenek6217b802009-07-29 21:53:49 +00001127 Ty = tmp->getAs<PointerType>()->getPointeeType();
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001128 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001129#endif
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001130
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001131 // All other values are symbolic.
1132 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001133}
1134
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001135SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1136 const ObjCIvarRegion* R) {
1137
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001138 // Check if the region has a binding.
1139 RegionBindingsTy B = GetRegionBindings(state->getStore());
1140
1141 if (const SVal* V = B.lookup(R))
1142 return *V;
1143
1144 const MemRegion *superR = R->getSuperRegion();
1145
1146 // Check if the super region has a binding.
1147 if (const SVal *V = B.lookup(superR)) {
1148 if (SymbolRef parentSym = V->getAsSymbol())
1149 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1150
1151 // Other cases: give up.
1152 return UnknownVal();
1153 }
1154
Ted Kremenek25c54572009-07-20 22:58:02 +00001155 return RetrieveLazySymbol(state, R);
1156}
1157
Ted Kremenek9031dd72009-07-21 00:12:07 +00001158SVal RegionStoreManager::RetrieveVar(const GRState *state,
1159 const VarRegion *R) {
1160
1161 // Check if the region has a binding.
1162 RegionBindingsTy B = GetRegionBindings(state->getStore());
1163
1164 if (const SVal* V = B.lookup(R))
1165 return *V;
1166
1167 // Lazily derive a value for the VarRegion.
1168 const VarDecl *VD = R->getDecl();
1169
1170 if (VD == SelfDecl)
1171 return loc::MemRegionVal(getSelfRegion(0));
1172
1173 if (R->hasGlobalsOrParametersStorage())
1174 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1175
1176 return UndefinedVal();
1177}
1178
Ted Kremenek25c54572009-07-20 22:58:02 +00001179SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1180 const TypedRegion *R) {
1181
1182 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001183
1184#if USE_REGION_CASTS
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001185 // If the region is already cast to another type, use that type to create the
1186 // symbol value.
Ted Kremenek25c54572009-07-20 22:58:02 +00001187 if (const QualType *ty = state->get<RegionCasts>(R)) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001188 if (const PointerType *PT = (*ty)->getAs<PointerType>()) {
Ted Kremenek25c54572009-07-20 22:58:02 +00001189 QualType castTy = PT->getPointeeType();
1190
1191 if (!IsReinterpreted(valTy, castTy, getContext()))
1192 valTy = castTy;
1193 }
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001194 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001195#endif
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001196
1197 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001198 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001199}
1200
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001201SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1202 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001203 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001204 assert(T->isStructureType());
1205
Zhongxing Xub7507d12009-06-11 07:27:30 +00001206 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001207 RecordDecl* RD = RT->getDecl();
1208 assert(RD->isDefinition());
1209
1210 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1211
Ted Kremenek67f28532009-06-17 22:02:04 +00001212 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1213 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001214 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001215
Douglas Gregore267ff32008-12-11 20:41:00 +00001216 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1217 FieldEnd = Fields.rend();
1218 Field != FieldEnd; ++Field) {
1219 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001220 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001221 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001222 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1223 }
1224
Zhongxing Xud91ee272009-06-23 09:02:15 +00001225 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001226}
1227
Ted Kremenek67f28532009-06-17 22:02:04 +00001228SVal RegionStoreManager::RetrieveArray(const GRState *state,
1229 const TypedRegion * R) {
1230
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001231 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001232 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1233
1234 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001235 uint64_t size = CAT->getSize().getZExtValue();
1236 for (uint64_t i = 0; i < size; ++i) {
1237 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001238 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1239 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001240 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001241 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001242 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1243 }
1244
Zhongxing Xud91ee272009-06-23 09:02:15 +00001245 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001246}
1247
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001248SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1249 const GRState *state,
1250 const TypedRegion *R,
1251 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001252 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001253 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001254
1255 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001256 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001257}
1258
Ted Kremenek9af46f52009-06-16 22:36:44 +00001259//===----------------------------------------------------------------------===//
1260// Binding values to regions.
1261//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001262
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001263Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001264 const MemRegion* R = 0;
1265
1266 if (isa<loc::MemRegionVal>(L))
1267 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001268
1269 if (R) {
1270 RegionBindingsTy B = GetRegionBindings(store);
1271 return RBFactory.Remove(B, R).getRoot();
1272 }
1273
1274 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001275}
1276
Ted Kremenek67f28532009-06-17 22:02:04 +00001277const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001278 if (isa<loc::ConcreteInt>(L))
1279 return state;
1280
Ted Kremenek9af46f52009-06-16 22:36:44 +00001281 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001282 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001283
1284 // Check if the region is a struct region.
1285 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1286 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001287 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001288
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001289 // Special case: the current region represents a cast and it and the super
1290 // region both have pointer types or intptr_t types. If so, perform the
1291 // bind to the super region.
1292 // This is needed to support OSAtomicCompareAndSwap and friends or other
1293 // loads that treat integers as pointers and vis versa.
1294 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1295 if (ER->getIndex().isZeroConstant()) {
1296 if (const TypedRegion *superR =
1297 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1298 ASTContext &Ctx = getContext();
1299 QualType superTy = superR->getValueType(Ctx);
1300 QualType erTy = ER->getValueType(Ctx);
1301
1302 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1303 IsAnyPointerOrIntptr(erTy, Ctx)) {
1304 SValuator::CastResult cr =
1305 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1306 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1307 }
1308 }
1309 }
1310 }
1311
1312 // Perform the binding.
Ted Kremenek67f28532009-06-17 22:02:04 +00001313 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001314 B = RBFactory.Add(B, R, V);
Ted Kremenek67f28532009-06-17 22:02:04 +00001315 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001316}
1317
Ted Kremenek67f28532009-06-17 22:02:04 +00001318const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001319 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001320
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001321 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001322 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001323
Ted Kremenek0964a062009-01-21 06:57:53 +00001324 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001325 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001326 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001327 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001328
Zhongxing Xud91ee272009-06-23 09:02:15 +00001329 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001330}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001331
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001332// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001333const GRState *
1334RegionStoreManager::BindCompoundLiteral(const GRState *state,
1335 const CompoundLiteralExpr* CL,
1336 SVal V) {
1337
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001338 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001339 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001340}
1341
Ted Kremenek67f28532009-06-17 22:02:04 +00001342const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001343 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001344 SVal Init) {
1345
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001346 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001347 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001348 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001349
Ted Kremenek46537392009-07-16 01:33:37 +00001350 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001351
1352 // Check if the init expr is a StringLiteral.
1353 if (isa<loc::MemRegionVal>(Init)) {
1354 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1355 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1356 const char* str = S->getStrData();
1357 unsigned len = S->getByteLength();
1358 unsigned j = 0;
1359
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001360 // Copy bytes from the string literal into the target array. Trailing bytes
1361 // in the array that are not covered by the string literal are initialized
1362 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001363 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001364 if (j >= len)
1365 break;
1366
Ted Kremenek46537392009-07-16 01:33:37 +00001367 SVal Idx = ValMgr.makeArrayIndex(i);
1368 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1369 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001370
Zhongxing Xud91ee272009-06-23 09:02:15 +00001371 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001372 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001373 }
1374
Ted Kremenek67f28532009-06-17 22:02:04 +00001375 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001376 }
1377
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001378 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001379 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001380 uint64_t i = 0;
1381
1382 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001383 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001384 if (VI == VE)
1385 break;
1386
Ted Kremenek46537392009-07-16 01:33:37 +00001387 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001388 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001389
1390 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001391 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001392 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001393 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001394 }
1395
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001396 // If the init list is shorter than the array length, set the array default
1397 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001398 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001399 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001400 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001401 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001402 }
1403 }
1404
Ted Kremenek67f28532009-06-17 22:02:04 +00001405 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001406}
1407
Ted Kremenek67f28532009-06-17 22:02:04 +00001408const GRState *
1409RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1410 SVal V) {
1411
1412 if (!Features.supportsFields())
1413 return state;
1414
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001415 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001416 assert(T->isStructureType());
1417
Ted Kremenek6217b802009-07-29 21:53:49 +00001418 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001419 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001420
1421 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001422 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001423
Ted Kremenek67f28532009-06-17 22:02:04 +00001424 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1425 // Ignore them and kill the field values.
1426 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1427 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001428
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001429 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001430 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001431
1432 RecordDecl::field_iterator FI, FE;
1433
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001434 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001435
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001436 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001437 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001438
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001439 QualType FTy = (*FI)->getType();
1440 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1441
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001442 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001443 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001444 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001445 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001446 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001447 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001448 }
1449
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001450 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001451 if (FI != FE)
1452 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001453
Ted Kremenek67f28532009-06-17 22:02:04 +00001454 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001455}
1456
Ted Kremenek67f28532009-06-17 22:02:04 +00001457const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001458 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001459
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001460 // Set the default value of the struct region to "unknown".
1461 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001462
1463 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001464 Store store = state->getStore();
1465 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001466 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001467 const MemRegion* R = I.getKey();
1468 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1469 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001470 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001471 }
1472
Ted Kremenek67f28532009-06-17 22:02:04 +00001473 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001474}
1475
Ted Kremenek67f28532009-06-17 22:02:04 +00001476const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1477 const MemRegion* R, SVal V) {
1478 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001479}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001480
1481//===----------------------------------------------------------------------===//
1482// State pruning.
1483//===----------------------------------------------------------------------===//
1484
1485static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1486 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1487 const MemRegion *R = XR->getRegion();
1488
1489 while (R) {
1490 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1491 SymReaper.markLive(SR->getSymbol());
1492 return;
1493 }
1494
1495 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1496 R = SR->getSuperRegion();
1497 continue;
1498 }
1499
1500 break;
1501 }
1502
1503 return;
1504 }
1505
1506 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1507 SymReaper.markLive(*SI);
1508}
1509
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001510void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1511 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001512 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001513{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001514 Store store = state.getStore();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001515 RegionBindingsTy B = GetRegionBindings(store);
1516
1517 // Lazily constructed backmap from MemRegions to SubRegions.
1518 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1519 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1520
Ted Kremenek9af46f52009-06-16 22:36:44 +00001521 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001522 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001523 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001524
1525 // Do a pass over the regions in the store. For VarRegions we check if
1526 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001527 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001528 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1529
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001530 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001531 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001532 const MemRegion *R = I.getKey();
1533 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001534 }
1535
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001536 // Scan the default bindings for "intermediate" roots.
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001537 RegionDefaultValue::MapTy DVM = state.get<RegionDefaultValue>();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001538 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1539 I != E; ++I) {
1540 const MemRegion *R = I.getKey();
1541 IntermediateRoots.push_back(R);
1542 }
1543
1544 // Process the "intermediate" roots to find if they are referenced by
1545 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001546 while (!IntermediateRoots.empty()) {
1547 const MemRegion* R = IntermediateRoots.back();
1548 IntermediateRoots.pop_back();
1549
1550 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001551 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001552 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001553 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001554 continue;
1555 }
1556
1557 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001558 if (SymReaper.isLive(SR->getSymbol()))
1559 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001560 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001561 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001562
1563 // Add the super region for R to the worklist if it is a subregion.
1564 if (const SubRegion* superR =
1565 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1566 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001567 }
1568
1569 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1570 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001571 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001572 while (!RegionRoots.empty()) {
1573 // Dequeue the next region on the worklist.
1574 const MemRegion* R = RegionRoots.back();
1575 RegionRoots.pop_back();
1576
1577 // Check if we have already processed this region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001578 if (Marked.count(R))
1579 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001580
1581 // Mark this region as processed. This is needed for termination in case
1582 // a region is referenced more than once.
1583 Marked.insert(R);
1584
1585 // Mark the symbol for any live SymbolicRegion as "live". This means we
1586 // should continue to track that symbol.
1587 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1588 SymReaper.markLive(SymR->getSymbol());
1589
1590 // Get the data binding for R (if any).
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001591 const SVal* Xptr = B.lookup(R);
1592 if (!Xptr) {
1593 // No direct binding? Get the default binding for R (if any).
1594 Xptr = DVM.lookup(R);
1595 }
1596
1597 // Direct or default binding?
Ted Kremenek9af46f52009-06-16 22:36:44 +00001598 if (Xptr) {
1599 SVal X = *Xptr;
1600 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1601
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001602 // If X is a region, then add it to the RegionRoots.
1603 if (const MemRegion *RX = X.getAsRegion()) {
1604 RegionRoots.push_back(RX);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001605 // Mark the super region of the RX as live.
1606 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1607 // 'y' => element region. 'x' is its super region.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001608 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1609 RegionRoots.push_back(SR->getSuperRegion());
1610 }
1611 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001612 }
1613
1614 // Get the subregions of R. These are RegionRoots as well since they
1615 // represent values that are also bound to R.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001616 RegionStoreSubRegionMap::iterator I, E;
1617 for (llvm::tie(I, E) = SubRegions->begin_end(R); I != E; ++I)
Ted Kremenek9af46f52009-06-16 22:36:44 +00001618 RegionRoots.push_back(*I);
1619 }
1620
1621 // We have now scanned the store, marking reachable regions and symbols
1622 // as live. We now remove all the regions that are dead from the store
1623 // as well as update DSymbols with the set symbols that are now dead.
1624 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1625 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001626 // If this region live? Is so, none of its symbols are dead.
1627 if (Marked.count(R))
1628 continue;
1629
1630 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001631 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001632
1633 // Mark all non-live symbols that this region references as dead.
1634 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1635 SymReaper.maybeDead(SymR->getSymbol());
1636
1637 SVal X = I.getData();
1638 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001639 for (; SI != SE; ++SI)
1640 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001641 }
1642
Ted Kremenek093569c2009-08-02 05:00:15 +00001643 // Remove dead 'default' bindings.
1644 RegionDefaultValue::MapTy NewDVM = DVM;
1645 RegionDefaultValue::MapTy::Factory &DVMFactory =
1646 state.get_context<RegionDefaultValue>();
1647
1648 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1649 I != E; ++I) {
1650 const MemRegion *R = I.getKey();
1651
1652 // If this region live? Is so, none of its symbols are dead.
1653 if (Marked.count(R))
1654 continue;
1655
1656 // Remove this dead region.
1657 NewDVM = DVMFactory.Remove(NewDVM, R);
1658
1659 // Mark all non-live symbols that this region references as dead.
1660 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1661 SymReaper.maybeDead(SymR->getSymbol());
1662
1663 SVal X = I.getData();
1664 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1665 for (; SI != SE; ++SI)
1666 SymReaper.maybeDead(*SI);
1667 }
1668
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001669 // Write the store back.
1670 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001671
1672 // Write the updated default bindings back.
1673 // FIXME: Right now this involves a fetching of a persistent state.
1674 // We can do better.
1675 if (DVM != NewDVM)
1676 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001677}
1678
1679//===----------------------------------------------------------------------===//
1680// Utility methods.
1681//===----------------------------------------------------------------------===//
1682
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001683void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001684 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001685 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001686 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001687
Ted Kremenek6f9b3a42009-07-13 23:53:06 +00001688 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001689 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001690}