blob: 543783d924f5fd6725c273fb36d3252d60fd82ac [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:
122 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
123 Map::iterator I = M.find(Parent);
124 M.insert(std::make_pair(Parent,
125 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
126 }
127
128 ~RegionStoreSubRegionMap() {}
129
Ted Kremenek5dc27462009-03-03 02:51:43 +0000130 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000131 Map::iterator I = M.find(Parent);
132
133 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000134 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000135
136 llvm::ImmutableSet<const MemRegion*> S = I->second;
137 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
138 SI != SE; ++SI) {
139 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000140 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000141 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000142
143 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000144 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000145
146 typedef SetTy::iterator iterator;
147
148 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
149 Map::iterator I = M.find(R);
150 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
151 return std::make_pair(S.begin(), S.end());
152 }
Ted Kremenek59e8f112009-03-03 01:35:36 +0000153};
154
Zhongxing Xu17892752008-10-08 02:50:44 +0000155class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000156 const RegionStoreFeatures Features;
Zhongxing Xu17892752008-10-08 02:50:44 +0000157 RegionBindingsTy::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000158
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000159 const MemRegion* SelfRegion;
160 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000161
162public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000163 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000164 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000165 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000166 RBFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000167 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000168 if (const ObjCMethodDecl* MD =
169 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
170 SelfDecl = MD->getSelfDecl();
171 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000172
173 virtual ~RegionStoreManager() {}
174
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000175 SubRegionMap *getSubRegionMap(const GRState *state);
176
177 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000178
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000179 /// getLValueString - Returns an SVal representing the lvalue of a
180 /// StringLiteral. Within RegionStore a StringLiteral has an
181 /// associated StringRegion, and the lvalue of a StringLiteral is
182 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000183 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000184
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000185 /// getLValueCompoundLiteral - Returns an SVal representing the
186 /// lvalue of a compound literal. Within RegionStore a compound
187 /// literal has an associated region, and the lvalue of the
188 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000189 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000190
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000191 /// getLValueVar - Returns an SVal that represents the lvalue of a
192 /// variable. Within RegionStore a variable has an associated
193 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000194 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000195
Ted Kremenek67f28532009-06-17 22:02:04 +0000196 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000197
Ted Kremenek67f28532009-06-17 22:02:04 +0000198 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000199
Ted Kremenek67f28532009-06-17 22:02:04 +0000200 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000201
Ted Kremenek67f28532009-06-17 22:02:04 +0000202 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000203 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000204
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000205
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000206 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
207 /// type. 'Array' represents the lvalue of the array being decayed
208 /// to a pointer, and the returned SVal represents the decayed
209 /// version of that lvalue (i.e., a pointer to the first element of
210 /// the array). This is called by GRExprEngine when evaluating
211 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000212 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000213
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000214 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000215 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000216
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000217 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000218
219 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
220 /// 'this' object (C++). When used when analyzing a normal function this
221 /// method returns NULL.
222 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000223 if (!SelfDecl)
224 return 0;
225
226 if (!SelfRegion) {
227 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
228 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
229 MRMgr.getHeapRegion());
230 }
231
232 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000233 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000234
235 //===-------------------------------------------------------------------===//
236 // Binding values to regions.
237 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000238
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000239 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
240 const Expr *E, unsigned Count);
241
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000242private:
243 RegionBindingsTy RemoveSubRegionBindings(RegionBindingsTy B,
244 const MemRegion *R,
245 RegionStoreSubRegionMap &M);
246
247public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000248 const GRState *Bind(const GRState *state, Loc LV, SVal V);
249
250 const GRState *BindCompoundLiteral(const GRState *state,
251 const CompoundLiteralExpr* CL, SVal V);
252
253 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
254
255 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
256 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000257 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000258
Ted Kremenek67f28532009-06-17 22:02:04 +0000259 /// BindStruct - Bind a compound value to a structure.
260 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
261
262 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
263
264 /// KillStruct - Set the entire struct to unknown.
265 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
266
267 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
268
269 Store Remove(Store store, Loc LV);
270
271 //===------------------------------------------------------------------===//
272 // Loading values from regions.
273 //===------------------------------------------------------------------===//
274
275 /// The high level logic for this method is this:
276 /// Retrieve (L)
277 /// if L has binding
278 /// return L's binding
279 /// else if L is in killset
280 /// return unknown
281 /// else
282 /// if L is on stack or heap
283 /// return undefined
284 /// else
285 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000286 SValuator::CastResult Retrieve(const GRState *state, Loc L,
287 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000288
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000289 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000290
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000291 SVal RetrieveField(const GRState *state, const FieldRegion *R);
292
293 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000294
Ted Kremenek9031dd72009-07-21 00:12:07 +0000295 SVal RetrieveVar(const GRState *state, const VarRegion *R);
296
Ted Kremenek25c54572009-07-20 22:58:02 +0000297 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
298
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000299 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
300 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000301
Ted Kremenek67f28532009-06-17 22:02:04 +0000302 /// Retrieve the values in a struct and return a CompoundVal, used when doing
303 /// struct copy:
304 /// struct s x, y;
305 /// x = y;
306 /// y's value is retrieved by this method.
307 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
308
309 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
310
311 //===------------------------------------------------------------------===//
312 // State pruning.
313 //===------------------------------------------------------------------===//
314
315 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
316 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000317 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000318 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
319
320 //===------------------------------------------------------------------===//
321 // Region "extents".
322 //===------------------------------------------------------------------===//
323
324 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
325 SVal getSizeInElements(const GRState *state, const MemRegion* R);
326
327 //===------------------------------------------------------------------===//
328 // Region "views".
329 //===------------------------------------------------------------------===//
330
331 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
332 const MemRegion* Base);
333
334 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
335 const MemRegion* Base);
336
337 //===------------------------------------------------------------------===//
338 // Utility methods.
339 //===------------------------------------------------------------------===//
340
Zhongxing Xu17892752008-10-08 02:50:44 +0000341 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000342 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000343 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000344
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000345 void print(Store store, llvm::raw_ostream& Out, const char* nl,
346 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000347
348 void iterBindings(Store store, BindingsHandler& f) {
349 // FIXME: Implement.
350 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000351
Ted Kremenek67f28532009-06-17 22:02:04 +0000352 // FIXME: Remove.
353 BasicValueFactory& getBasicVals() {
354 return StateMgr.getBasicVals();
355 }
356
357 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000358 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000359};
360
361} // end anonymous namespace
362
Ted Kremenek9af46f52009-06-16 22:36:44 +0000363//===----------------------------------------------------------------------===//
364// RegionStore creation.
365//===----------------------------------------------------------------------===//
366
367StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
368 RegionStoreFeatures F = maximal_features_tag();
369 return new RegionStoreManager(StMgr, F);
370}
371
372StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
373 RegionStoreFeatures F = minimal_features_tag();
374 F.enableFields(true);
375 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000376}
377
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000378RegionStoreSubRegionMap*
379RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000380 RegionBindingsTy B = GetRegionBindings(state->getStore());
381 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
382
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000383 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
384 llvm::SmallVector<const SubRegion*, 10> WL;
385
386 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek59e8f112009-03-03 01:35:36 +0000387 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000388 WL.push_back(R);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000389
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000390 RegionDefaultValue::MapTy DVM = state->get<RegionDefaultValue>();
391 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
392 I != E; ++I)
393 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
394 WL.push_back(R);
395
396 // We also need to record in the subregion map "intermediate" regions that
397 // don't have direct bindings but are super regions of those that do.
398 while (!WL.empty()) {
399 const SubRegion *R = WL.back();
400 WL.pop_back();
401
402 if (Marked.count(R))
403 continue;
404
405 const MemRegion *superR = R->getSuperRegion();
406 M->add(superR, R);
407 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
408 WL.push_back(sr);
409 }
410
Ted Kremenek14453bf2009-03-03 19:02:42 +0000411 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000412}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000413
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000414SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
415 return getRegionStoreSubRegionMap(state);
416}
417
Ted Kremenek9af46f52009-06-16 22:36:44 +0000418//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000419// Binding invalidation.
420//===----------------------------------------------------------------------===//
421
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000422RegionBindingsTy
423RegionStoreManager::RemoveSubRegionBindings(RegionBindingsTy B,
424 const MemRegion *R,
425 RegionStoreSubRegionMap &M) {
426
427 RegionStoreSubRegionMap::iterator I, E;
428
429 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
430 B = RemoveSubRegionBindings(B, *I, M);
431
432 return RBFactory.Remove(B, R);
433}
434
435
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000436const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
437 const MemRegion *R,
438 const Expr *E,
439 unsigned Count) {
440 ASTContext& Ctx = StateMgr.getContext();
441
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000442 // Strip away casts.
443 R = R->getBaseRegion();
444
445 // Get the mapping of regions -> subregions.
446 llvm::OwningPtr<RegionStoreSubRegionMap>
447 SubRegions(getRegionStoreSubRegionMap(state));
448
449 // Remove the bindings to subregions.
450 RegionBindingsTy B = GetRegionBindings(state->getStore());
451 B = RemoveSubRegionBindings(B, R, *SubRegions.get());
452 state = state->makeWithStore(B.getRoot());
453
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000454 if (!R->isBoundable())
455 return state;
456
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000457 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
458 isa<ObjCObjectRegion>(R)) {
459 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000460 // conjured symbol. The type of the symbol is irrelavant.
461 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000462 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000463 }
464
465 const TypedRegion *TR = cast<TypedRegion>(R);
466 QualType T = TR->getValueType(Ctx);
467
468 // FIXME: The code causes a crash when using RegionStore on the test case
469 // 'test_invalidate_cast_int' (misc-ps.m). Consider removing it
470 // permanently. Region casts are probably not too strict to handle
471 // the transient interpretation of memory. Instead we can use the QualType
472 // passed to 'Retrieve' and friends to determine the most current
473 // interpretation of memory when it is actually used.
474#if 0
475 // If the region is cast to another type, use that type.
476 if (const QualType *CastTy = getCastType(state, R)) {
477 assert(!(*CastTy)->isObjCObjectPointerType());
Ted Kremenek6217b802009-07-29 21:53:49 +0000478 QualType NewT = (*CastTy)->getAs<PointerType>()->getPointeeType();
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000479
480 // The only exception is if the original region had a location type as its
481 // value type we always want to treat the region as binding to a location.
482 // This issue can arise when pointers are casted to integers and back.
483
484 if (!(Loc::IsLocType(T) && !Loc::IsLocType(NewT)))
485 T = NewT;
486 }
487#endif
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000488
489 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000490 // FIXME: handle structs with default region value.
491 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
492
493 // No record definition. There is nothing we can do.
494 if (!RD)
495 return state;
496
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000497 // Invalidate the region by setting its default value to
498 // conjured symbol. The type of the symbol is irrelavant.
499 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
500 return setDefaultValue(state, R, V);
501 }
502
503 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000504 // Set the default value of the array to conjured symbol.
505 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
506 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000507 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000508 }
509
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000510 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
511 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
512 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000513}
514
515//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000516// getLValueXXX methods.
517//===----------------------------------------------------------------------===//
518
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000519/// getLValueString - Returns an SVal representing the lvalue of a
520/// StringLiteral. Within RegionStore a StringLiteral has an
521/// associated StringRegion, and the lvalue of a StringLiteral is the
522/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000523SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000524 const StringLiteral* S) {
525 return loc::MemRegionVal(MRMgr.getStringRegion(S));
526}
527
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000528/// getLValueVar - Returns an SVal that represents the lvalue of a
529/// variable. Within RegionStore a variable has an associated
530/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000531SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000532 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
533}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000534
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000535/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
536/// of a compound literal. Within RegionStore a compound literal
537/// has an associated region, and the lvalue of the compound literal
538/// is the lvalue of that region.
539SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000540RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000541 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000542 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
543}
544
Ted Kremenek67f28532009-06-17 22:02:04 +0000545SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000546 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000547 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000548}
549
Ted Kremenek67f28532009-06-17 22:02:04 +0000550SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000551 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000552 return getLValueFieldOrIvar(St, Base, D);
553}
554
Ted Kremenek67f28532009-06-17 22:02:04 +0000555SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000556 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000557 if (Base.isUnknownOrUndef())
558 return Base;
559
560 Loc BaseL = cast<Loc>(Base);
561 const MemRegion* BaseR = 0;
562
563 switch (BaseL.getSubKind()) {
564 case loc::MemRegionKind:
565 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
566 break;
567
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000568 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000569 // These are anormal cases. Flag an undefined value.
570 return UndefinedVal();
571
572 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000573 // While these seem funny, this can happen through casts.
574 // FIXME: What we should return is the field offset. For example,
575 // add the field offset to the integer value. That way funny things
576 // like this work properly: &(((struct foo *) 0xa)->f)
577 return Base;
578
579 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000580 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000581 return Base;
582 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000583
584 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
585 // of FieldDecl.
586 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
587 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000588
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000589 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000590}
591
Ted Kremenek67f28532009-06-17 22:02:04 +0000592SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000593 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000594 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000595
Ted Kremenekde7ec632009-03-09 22:44:49 +0000596 // If the base is an unknown or undefined value, just return it back.
597 // FIXME: For absolute pointer addresses, we just return that value back as
598 // well, although in reality we should return the offset added to that
599 // value.
600 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000601 return Base;
602
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000603 // Only handle integer offsets... for now.
604 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000605 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000606
Zhongxing Xuce760782009-05-09 13:20:07 +0000607 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000608
609 // Pointer of any type can be cast and used as array base.
610 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
611
Ted Kremenek46537392009-07-16 01:33:37 +0000612 // Convert the offset to the appropriate size and signedness.
613 Offset = ValMgr.convertToArrayIndex(Offset);
614
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000615 if (!ElemR) {
616 //
617 // If the base region is not an ElementRegion, create one.
618 // This can happen in the following example:
619 //
620 // char *p = __builtin_alloc(10);
621 // p[1] = 8;
622 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000623 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000624 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000625 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000626 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000627 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000628
629 SVal BaseIdx = ElemR->getIndex();
630
631 if (!isa<nonloc::ConcreteInt>(BaseIdx))
632 return UnknownVal();
633
634 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
635 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
636 assert(BaseIdxI.isSigned());
637
Ted Kremenek46537392009-07-16 01:33:37 +0000638 // Compute the new index.
639 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000640
Ted Kremenek46537392009-07-16 01:33:37 +0000641 // Construct the new ElementRegion.
642 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000643 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
644 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000645}
646
Ted Kremenek9af46f52009-06-16 22:36:44 +0000647//===----------------------------------------------------------------------===//
648// Extents for regions.
649//===----------------------------------------------------------------------===//
650
Ted Kremenek67f28532009-06-17 22:02:04 +0000651SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000652 const MemRegion *R) {
653
654 switch (R->getKind()) {
655 case MemRegion::MemSpaceRegionKind:
656 assert(0 && "Cannot index into a MemSpace");
657 return UnknownVal();
658
659 case MemRegion::CodeTextRegionKind:
660 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000661 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000662
663 // Not yet handled.
664 case MemRegion::AllocaRegionKind:
665 case MemRegion::CompoundLiteralRegionKind:
666 case MemRegion::ElementRegionKind:
667 case MemRegion::FieldRegionKind:
668 case MemRegion::ObjCIvarRegionKind:
669 case MemRegion::ObjCObjectRegionKind:
670 case MemRegion::SymbolicRegionKind:
671 return UnknownVal();
672
673 case MemRegion::StringRegionKind: {
674 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
675 // We intentionally made the size value signed because it participates in
676 // operations with signed indices.
677 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000678 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000679
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000680 case MemRegion::VarRegionKind: {
681 const VarRegion* VR = cast<VarRegion>(R);
682 // Get the type of the variable.
683 QualType T = VR->getDesugaredValueType(getContext());
684
685 // FIXME: Handle variable-length arrays.
686 if (isa<VariableArrayType>(T))
687 return UnknownVal();
688
689 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
690 // return the size as signed integer.
691 return ValMgr.makeIntVal(CAT->getSize(), false);
692 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000693
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000694 // Clients can use ordinary variables as if they were arrays. These
695 // essentially are arrays of size 1.
696 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000697 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000698
699 case MemRegion::BEG_DECL_REGIONS:
700 case MemRegion::END_DECL_REGIONS:
701 case MemRegion::BEG_TYPED_REGIONS:
702 case MemRegion::END_TYPED_REGIONS:
703 assert(0 && "Infeasible region");
704 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000705 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000706
707 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000708 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000709}
710
Ted Kremenek67f28532009-06-17 22:02:04 +0000711const GRState *RegionStoreManager::setExtent(const GRState *state,
712 const MemRegion *region,
713 SVal extent) {
714 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000715}
716
717//===----------------------------------------------------------------------===//
718// Location and region casting.
719//===----------------------------------------------------------------------===//
720
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000721/// ArrayToPointer - Emulates the "decay" of an array to a pointer
722/// type. 'Array' represents the lvalue of the array being decayed
723/// to a pointer, and the returned SVal represents the decayed
724/// version of that lvalue (i.e., a pointer to the first element of
725/// the array). This is called by GRExprEngine when evaluating casts
726/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000727SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000728 if (!isa<loc::MemRegionVal>(Array))
729 return UnknownVal();
730
731 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
732 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
733
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000734 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000735 return UnknownVal();
736
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000737 // Strip off typedefs from the ArrayRegion's ValueType.
738 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000739 ArrayType *AT = cast<ArrayType>(T);
740 T = AT->getElementType();
741
Ted Kremenek75185b52009-07-16 00:00:11 +0000742 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
743 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000744
745 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000746}
747
Ted Kremenek9af46f52009-06-16 22:36:44 +0000748//===----------------------------------------------------------------------===//
749// Pointer arithmetic.
750//===----------------------------------------------------------------------===//
751
Zhongxing Xu262fd032009-05-20 09:00:16 +0000752SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000753 BinaryOperator::Opcode Op, Loc L, NonLoc R,
754 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000755 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000756 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000757 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000758
Zhongxing Xua1718c72009-04-03 07:33:13 +0000759 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000760 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000761
Ted Kremenek3bccf082009-07-11 00:58:27 +0000762 switch (MR->getKind()) {
763 case MemRegion::SymbolicRegionKind: {
764 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000765 SymbolRef Sym = SR->getSymbol();
766 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000767 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000768 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
769 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
770 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000771 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000772 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000773 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000774 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000775 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000776 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
777 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
778 break;
779 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000780
Ted Kremenek3bccf082009-07-11 00:58:27 +0000781 case MemRegion::ElementRegionKind: {
782 ER = cast<ElementRegion>(MR);
783 break;
784 }
785
786 // Not yet handled.
787 case MemRegion::VarRegionKind:
788 case MemRegion::StringRegionKind:
789 case MemRegion::CompoundLiteralRegionKind:
790 case MemRegion::FieldRegionKind:
791 case MemRegion::ObjCObjectRegionKind:
792 case MemRegion::ObjCIvarRegionKind:
793 return UnknownVal();
794
Ted Kremenek3bccf082009-07-11 00:58:27 +0000795 case MemRegion::CodeTextRegionKind:
796 // Technically this can happen if people do funny things with casts.
797 return UnknownVal();
798
799 case MemRegion::MemSpaceRegionKind:
800 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
801 return UnknownVal();
802
803 case MemRegion::BEG_DECL_REGIONS:
804 case MemRegion::END_DECL_REGIONS:
805 case MemRegion::BEG_TYPED_REGIONS:
806 case MemRegion::END_TYPED_REGIONS:
807 assert(0 && "Infeasible region");
808 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000809 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000810
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000811 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000812 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
813 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
814
815 // Only support concrete integer indexes for now.
816 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000817 // FIXME: Should use SValuator here.
818 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
819 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000820 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000821 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
822 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000823 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000824 }
825
826 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000827}
828
Ted Kremenek9af46f52009-06-16 22:36:44 +0000829//===----------------------------------------------------------------------===//
830// Loading values from regions.
831//===----------------------------------------------------------------------===//
832
Ted Kremeneka6275a52009-07-15 02:31:43 +0000833static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
834 RTy = Ctx.getCanonicalType(RTy);
835 UsedTy = Ctx.getCanonicalType(UsedTy);
836
837 if (RTy == UsedTy)
838 return false;
839
Ted Kremenek25c54572009-07-20 22:58:02 +0000840
841 // Recursively check the types. We basically want to see if a pointer value
842 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
843 // represents a reinterpretation.
844 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000845 const PointerType *PRTy = RTy->getAs<PointerType>();
846 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000847
848 return PUsedTy && PRTy &&
849 IsReinterpreted(PRTy->getPointeeType(),
850 PUsedTy->getPointeeType(), Ctx);
851 }
852
853 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000854}
855
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000856SValuator::CastResult
857RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000858
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000859 assert(!isa<UnknownVal>(L) && "location unknown");
860 assert(!isa<UndefinedVal>(L) && "location undefined");
861
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000862 // FIXME: Is this even possible? Shouldn't this be treated as a null
863 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000864 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000865 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000866
Ted Kremenek67f28532009-06-17 22:02:04 +0000867 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000868
Zhongxing Xu91844122009-05-20 09:18:48 +0000869 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000870 // Example:
871 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000872 // char* p = alloca();
873 // read(p);
874 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000875 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000876 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000877
878 if (isa<SymbolicRegion>(MR)) {
879 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000880 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000881 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000882 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
883 }
884
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000885 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
886 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000887 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000888 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000889
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000890 // FIXME: We should eventually handle funny addressing. e.g.:
891 //
892 // int x = ...;
893 // int *p = &x;
894 // char *q = (char*) p;
895 // char c = *q; // returns the first byte of 'x'.
896 //
897 // Such funny addressing will occur due to layering of regions.
898
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000899#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000900 ASTContext &Ctx = getContext();
901 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000902 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
903 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000904 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000905 assert(Ctx.getCanonicalType(RTy) ==
906 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000907 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000908#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000909
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000910 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000911 return SValuator::CastResult(state, RetrieveStruct(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000912
913 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000914 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000915
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000916 // FIXME: handle Vector types.
917 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000918 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000919
920 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000921 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000922
923 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000924 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000925
Ted Kremenek25c54572009-07-20 22:58:02 +0000926 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000927 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000928
929 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000930 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000931
Ted Kremenek67f28532009-06-17 22:02:04 +0000932 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000933 RegionBindingsTy::data_type* V = B.lookup(R);
934
935 // Check if the region has a binding.
936 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000937 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000938
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000939 // The location does not have a bound value. This means that it has
940 // the value it had upon its creation and/or entry to the analyzed
941 // function/method. These are either symbolic values or 'undefined'.
942
Ted Kremenek356e9d62009-07-22 04:35:42 +0000943#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000944 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000945#else
946 if (R->hasStackStorage()) {
947#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000948 // All stack variables are considered to have undefined values
949 // upon creation. All heap allocated blocks are considered to
950 // have undefined values as well unless they are explicitly bound
951 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000952 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000953 }
954
Ted Kremenek356e9d62009-07-22 04:35:42 +0000955#if USE_REGION_CASTS
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000956 // If the region is already cast to another type, use that type to create the
957 // symbol value.
958 if (const QualType *p = state->get<RegionCasts>(R)) {
959 QualType T = *p;
Ted Kremenek6217b802009-07-29 21:53:49 +0000960 RTy = T->getAs<PointerType>()->getPointeeType();
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000961 }
Ted Kremenek356e9d62009-07-22 04:35:42 +0000962#endif
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000963
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000964 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000965 return SValuator::CastResult(state,
966 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000967}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000968
969
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000970
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000971SVal RegionStoreManager::RetrieveElement(const GRState* state,
972 const ElementRegion* R) {
973 // Check if the region has a binding.
974 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +0000975 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000976 return *V;
977
Ted Kremenek921109a2009-07-01 23:19:52 +0000978 const MemRegion* superR = R->getSuperRegion();
979
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000980 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +0000981 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000982 const StringLiteral *Str = StrR->getStringLiteral();
983 SVal Idx = R->getIndex();
984 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
985 int64_t i = CI->getValue().getSExtValue();
986 char c;
987 if (i == Str->getByteLength())
988 c = '\0';
989 else
990 c = Str->getStrData()[i];
991 return ValMgr.makeIntVal(c, getContext().CharTy);
992 }
993 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000994
995 // Special case: the current region represents a cast and it and the super
996 // region both have pointer types or intptr_t types. If so, perform the
997 // retrieve from the super region and appropriately "cast" the value.
998 // This is needed to support OSAtomicCompareAndSwap and friends or other
999 // loads that treat integers as pointers and vis versa.
1000 if (R->getIndex().isZeroConstant()) {
1001 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1002 ASTContext &Ctx = getContext();
1003
1004 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1005 QualType valTy = R->getValueType(Ctx);
1006 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1007 // Retrieve the value from the super region. This will be casted to
1008 // valTy when we return to 'Retrieve'.
1009 const SValuator::CastResult &cr = Retrieve(state,
1010 loc::MemRegionVal(superR),
1011 valTy);
1012 return cr.getSVal();
1013 }
1014 }
1015 }
1016 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001017
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001018 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +00001019 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001020 if (D->hasConjuredSymbol())
1021 return ValMgr.getRegionValueSymbolVal(R);
1022 else
1023 return *D;
1024 }
1025
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001026 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001027 if (const SVal *V = B.lookup(superR)) {
1028 if (SymbolRef parentSym = V->getAsSymbol())
1029 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001030
1031 if (V->isUnknownOrUndef())
1032 return *V;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001033
1034 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001035 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001036 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001037
Ted Kremenek356e9d62009-07-22 04:35:42 +00001038#if 0
Ted Kremenek921109a2009-07-01 23:19:52 +00001039 if (R->hasHeapStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001040 // FIXME: If the region has heap storage and we know nothing special
1041 // about its bindings, should we instead return UnknownVal? Seems like
1042 // we should only return UndefinedVal in the cases where we know the value
1043 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001044 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +00001045 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001046#endif
1047
Ted Kremenekdc147262009-07-02 22:02:15 +00001048 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +00001049 // Currently we don't reason specially about Clang-style vectors. Check
1050 // if superR is a vector and if so return Unknown.
1051 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1052 if (typedSuperR->getValueType(getContext())->isVectorType())
1053 return UnknownVal();
1054 }
1055
1056 return UndefinedVal();
1057 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001058
1059 QualType Ty = R->getValueType(getContext());
1060
Ted Kremenek356e9d62009-07-22 04:35:42 +00001061#if USE_REGION_CASTS
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001062 // If the region is already cast to another type, use that type to create the
1063 // symbol value.
1064 if (const QualType *p = state->get<RegionCasts>(R))
Ted Kremenek6217b802009-07-29 21:53:49 +00001065 Ty = (*p)->getAs<PointerType>()->getPointeeType();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001066#endif
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001067
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001068 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001069}
1070
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001071SVal RegionStoreManager::RetrieveField(const GRState* state,
1072 const FieldRegion* R) {
1073 QualType Ty = R->getValueType(getContext());
1074
1075 // Check if the region has a binding.
1076 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001077 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001078 return *V;
1079
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001080 const MemRegion* superR = R->getSuperRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001081 while (superR) {
1082 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
1083 if (SymbolRef parentSym = D->getAsSymbol())
1084 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001085
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001086 if (D->isZeroConstant())
1087 return ValMgr.makeZeroVal(Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001088
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001089 if (D->isUnknown())
1090 return *D;
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001091
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001092 assert(0 && "Unknown default value");
1093 }
1094
1095 // If our super region is a field or element itself, walk up the region
1096 // hierarchy to see if there is a default value installed in an ancestor.
1097 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1098 superR = cast<SubRegion>(superR)->getSuperRegion();
1099 continue;
1100 }
1101
1102 break;
1103 }
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001104
Ted Kremenek356e9d62009-07-22 04:35:42 +00001105#if HEAP_UNDEFINED
Ted Kremenekdc147262009-07-02 22:02:15 +00001106 // FIXME: Is this correct? Should it be UnknownVal?
1107 if (R->hasHeapStorage())
1108 return UndefinedVal();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001109#endif
Ted Kremenekdc147262009-07-02 22:02:15 +00001110
1111 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001112 return UndefinedVal();
1113
Ted Kremenek356e9d62009-07-22 04:35:42 +00001114#if USE_REGION_CASTS
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001115 // If the region is already cast to another type, use that type to create the
1116 // symbol value.
1117 if (const QualType *p = state->get<RegionCasts>(R)) {
1118 QualType tmp = *p;
Ted Kremenek6217b802009-07-29 21:53:49 +00001119 Ty = tmp->getAs<PointerType>()->getPointeeType();
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001120 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001121#endif
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001122
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001123 // All other values are symbolic.
1124 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001125}
1126
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001127SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1128 const ObjCIvarRegion* R) {
1129
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001130 // Check if the region has a binding.
1131 RegionBindingsTy B = GetRegionBindings(state->getStore());
1132
1133 if (const SVal* V = B.lookup(R))
1134 return *V;
1135
1136 const MemRegion *superR = R->getSuperRegion();
1137
1138 // Check if the super region has a binding.
1139 if (const SVal *V = B.lookup(superR)) {
1140 if (SymbolRef parentSym = V->getAsSymbol())
1141 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1142
1143 // Other cases: give up.
1144 return UnknownVal();
1145 }
1146
Ted Kremenek25c54572009-07-20 22:58:02 +00001147 return RetrieveLazySymbol(state, R);
1148}
1149
Ted Kremenek9031dd72009-07-21 00:12:07 +00001150SVal RegionStoreManager::RetrieveVar(const GRState *state,
1151 const VarRegion *R) {
1152
1153 // Check if the region has a binding.
1154 RegionBindingsTy B = GetRegionBindings(state->getStore());
1155
1156 if (const SVal* V = B.lookup(R))
1157 return *V;
1158
1159 // Lazily derive a value for the VarRegion.
1160 const VarDecl *VD = R->getDecl();
1161
1162 if (VD == SelfDecl)
1163 return loc::MemRegionVal(getSelfRegion(0));
1164
1165 if (R->hasGlobalsOrParametersStorage())
1166 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1167
1168 return UndefinedVal();
1169}
1170
Ted Kremenek25c54572009-07-20 22:58:02 +00001171SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1172 const TypedRegion *R) {
1173
1174 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001175
1176#if USE_REGION_CASTS
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001177 // If the region is already cast to another type, use that type to create the
1178 // symbol value.
Ted Kremenek25c54572009-07-20 22:58:02 +00001179 if (const QualType *ty = state->get<RegionCasts>(R)) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001180 if (const PointerType *PT = (*ty)->getAs<PointerType>()) {
Ted Kremenek25c54572009-07-20 22:58:02 +00001181 QualType castTy = PT->getPointeeType();
1182
1183 if (!IsReinterpreted(valTy, castTy, getContext()))
1184 valTy = castTy;
1185 }
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001186 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001187#endif
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001188
1189 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001190 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001191}
1192
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001193SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1194 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001195 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001196 assert(T->isStructureType());
1197
Zhongxing Xub7507d12009-06-11 07:27:30 +00001198 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001199 RecordDecl* RD = RT->getDecl();
1200 assert(RD->isDefinition());
1201
1202 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1203
Ted Kremenek67f28532009-06-17 22:02:04 +00001204 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1205 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001206 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001207
Douglas Gregore267ff32008-12-11 20:41:00 +00001208 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1209 FieldEnd = Fields.rend();
1210 Field != FieldEnd; ++Field) {
1211 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001212 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001213 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001214 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1215 }
1216
Zhongxing Xud91ee272009-06-23 09:02:15 +00001217 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001218}
1219
Ted Kremenek67f28532009-06-17 22:02:04 +00001220SVal RegionStoreManager::RetrieveArray(const GRState *state,
1221 const TypedRegion * R) {
1222
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001223 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001224 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1225
1226 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001227 uint64_t size = CAT->getSize().getZExtValue();
1228 for (uint64_t i = 0; i < size; ++i) {
1229 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001230 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1231 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001232 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001233 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001234 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1235 }
1236
Zhongxing Xud91ee272009-06-23 09:02:15 +00001237 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001238}
1239
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001240SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1241 const GRState *state,
1242 const TypedRegion *R,
1243 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001244 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001245 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001246
1247 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001248 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001249}
1250
Ted Kremenek9af46f52009-06-16 22:36:44 +00001251//===----------------------------------------------------------------------===//
1252// Binding values to regions.
1253//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001254
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001255Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001256 const MemRegion* R = 0;
1257
1258 if (isa<loc::MemRegionVal>(L))
1259 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001260
1261 if (R) {
1262 RegionBindingsTy B = GetRegionBindings(store);
1263 return RBFactory.Remove(B, R).getRoot();
1264 }
1265
1266 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001267}
1268
Ted Kremenek67f28532009-06-17 22:02:04 +00001269const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001270 if (isa<loc::ConcreteInt>(L))
1271 return state;
1272
Ted Kremenek9af46f52009-06-16 22:36:44 +00001273 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001274 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001275
1276 // Check if the region is a struct region.
1277 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1278 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001279 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001280
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001281 // Special case: the current region represents a cast and it and the super
1282 // region both have pointer types or intptr_t types. If so, perform the
1283 // bind to the super region.
1284 // This is needed to support OSAtomicCompareAndSwap and friends or other
1285 // loads that treat integers as pointers and vis versa.
1286 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1287 if (ER->getIndex().isZeroConstant()) {
1288 if (const TypedRegion *superR =
1289 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1290 ASTContext &Ctx = getContext();
1291 QualType superTy = superR->getValueType(Ctx);
1292 QualType erTy = ER->getValueType(Ctx);
1293
1294 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1295 IsAnyPointerOrIntptr(erTy, Ctx)) {
1296 SValuator::CastResult cr =
1297 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1298 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1299 }
1300 }
1301 }
1302 }
1303
1304 // Perform the binding.
Ted Kremenek67f28532009-06-17 22:02:04 +00001305 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001306 B = RBFactory.Add(B, R, V);
Ted Kremenek67f28532009-06-17 22:02:04 +00001307 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001308}
1309
Ted Kremenek67f28532009-06-17 22:02:04 +00001310const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001311 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001312
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001313 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001314 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001315
Ted Kremenek0964a062009-01-21 06:57:53 +00001316 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001317 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001318 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001319 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001320
Zhongxing Xud91ee272009-06-23 09:02:15 +00001321 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001322}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001323
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001324// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001325const GRState *
1326RegionStoreManager::BindCompoundLiteral(const GRState *state,
1327 const CompoundLiteralExpr* CL,
1328 SVal V) {
1329
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001330 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001331 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001332}
1333
Ted Kremenek67f28532009-06-17 22:02:04 +00001334const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001335 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001336 SVal Init) {
1337
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001338 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001339 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001340 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001341
Ted Kremenek46537392009-07-16 01:33:37 +00001342 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001343
1344 // Check if the init expr is a StringLiteral.
1345 if (isa<loc::MemRegionVal>(Init)) {
1346 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1347 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1348 const char* str = S->getStrData();
1349 unsigned len = S->getByteLength();
1350 unsigned j = 0;
1351
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001352 // Copy bytes from the string literal into the target array. Trailing bytes
1353 // in the array that are not covered by the string literal are initialized
1354 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001355 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001356 if (j >= len)
1357 break;
1358
Ted Kremenek46537392009-07-16 01:33:37 +00001359 SVal Idx = ValMgr.makeArrayIndex(i);
1360 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1361 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001362
Zhongxing Xud91ee272009-06-23 09:02:15 +00001363 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001364 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001365 }
1366
Ted Kremenek67f28532009-06-17 22:02:04 +00001367 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001368 }
1369
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001370 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001371 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001372 uint64_t i = 0;
1373
1374 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001375 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001376 if (VI == VE)
1377 break;
1378
Ted Kremenek46537392009-07-16 01:33:37 +00001379 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001380 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001381
1382 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001383 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001384 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001385 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001386 }
1387
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001388 // If the init list is shorter than the array length, set the array default
1389 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001390 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001391 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001392 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001393 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001394 }
1395 }
1396
Ted Kremenek67f28532009-06-17 22:02:04 +00001397 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001398}
1399
Ted Kremenek67f28532009-06-17 22:02:04 +00001400const GRState *
1401RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1402 SVal V) {
1403
1404 if (!Features.supportsFields())
1405 return state;
1406
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001407 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001408 assert(T->isStructureType());
1409
Ted Kremenek6217b802009-07-29 21:53:49 +00001410 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001411 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001412
1413 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001414 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001415
Ted Kremenek67f28532009-06-17 22:02:04 +00001416 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1417 // Ignore them and kill the field values.
1418 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1419 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001420
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001421 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001422 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001423
1424 RecordDecl::field_iterator FI, FE;
1425
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001426 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001427
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001428 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001429 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001430
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001431 QualType FTy = (*FI)->getType();
1432 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1433
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001434 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001435 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001436 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001437 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001438 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001439 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001440 }
1441
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001442 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001443 if (FI != FE)
1444 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001445
Ted Kremenek67f28532009-06-17 22:02:04 +00001446 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001447}
1448
Ted Kremenek67f28532009-06-17 22:02:04 +00001449const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001450 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001451
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001452 // Set the default value of the struct region to "unknown".
1453 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001454
1455 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001456 Store store = state->getStore();
1457 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001458 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001459 const MemRegion* R = I.getKey();
1460 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1461 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001462 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001463 }
1464
Ted Kremenek67f28532009-06-17 22:02:04 +00001465 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001466}
1467
Ted Kremenek67f28532009-06-17 22:02:04 +00001468const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1469 const MemRegion* R, SVal V) {
1470 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001471}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001472
1473//===----------------------------------------------------------------------===//
1474// State pruning.
1475//===----------------------------------------------------------------------===//
1476
1477static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1478 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1479 const MemRegion *R = XR->getRegion();
1480
1481 while (R) {
1482 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1483 SymReaper.markLive(SR->getSymbol());
1484 return;
1485 }
1486
1487 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1488 R = SR->getSuperRegion();
1489 continue;
1490 }
1491
1492 break;
1493 }
1494
1495 return;
1496 }
1497
1498 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1499 SymReaper.markLive(*SI);
1500}
1501
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001502void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1503 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001504 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001505{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001506 Store store = state.getStore();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001507 RegionBindingsTy B = GetRegionBindings(store);
1508
1509 // Lazily constructed backmap from MemRegions to SubRegions.
1510 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1511 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1512
Ted Kremenek9af46f52009-06-16 22:36:44 +00001513 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001514 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001515 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001516
1517 // Do a pass over the regions in the store. For VarRegions we check if
1518 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001519 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001520 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1521
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001522 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001523 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001524 const MemRegion *R = I.getKey();
1525 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001526 }
1527
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001528 // Scan the default bindings for "intermediate" roots.
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001529 RegionDefaultValue::MapTy DVM = state.get<RegionDefaultValue>();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001530 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1531 I != E; ++I) {
1532 const MemRegion *R = I.getKey();
1533 IntermediateRoots.push_back(R);
1534 }
1535
1536 // Process the "intermediate" roots to find if they are referenced by
1537 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001538 while (!IntermediateRoots.empty()) {
1539 const MemRegion* R = IntermediateRoots.back();
1540 IntermediateRoots.pop_back();
1541
1542 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001543 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001544 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001545 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001546 continue;
1547 }
1548
1549 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001550 if (SymReaper.isLive(SR->getSymbol()))
1551 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001552 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001553 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001554
1555 // Add the super region for R to the worklist if it is a subregion.
1556 if (const SubRegion* superR =
1557 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1558 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001559 }
1560
1561 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1562 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001563 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001564 while (!RegionRoots.empty()) {
1565 // Dequeue the next region on the worklist.
1566 const MemRegion* R = RegionRoots.back();
1567 RegionRoots.pop_back();
1568
1569 // Check if we have already processed this region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001570 if (Marked.count(R))
1571 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001572
1573 // Mark this region as processed. This is needed for termination in case
1574 // a region is referenced more than once.
1575 Marked.insert(R);
1576
1577 // Mark the symbol for any live SymbolicRegion as "live". This means we
1578 // should continue to track that symbol.
1579 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1580 SymReaper.markLive(SymR->getSymbol());
1581
1582 // Get the data binding for R (if any).
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001583 const SVal* Xptr = B.lookup(R);
1584 if (!Xptr) {
1585 // No direct binding? Get the default binding for R (if any).
1586 Xptr = DVM.lookup(R);
1587 }
1588
1589 // Direct or default binding?
Ted Kremenek9af46f52009-06-16 22:36:44 +00001590 if (Xptr) {
1591 SVal X = *Xptr;
1592 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1593
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001594 // If X is a region, then add it to the RegionRoots.
1595 if (const MemRegion *RX = X.getAsRegion()) {
1596 RegionRoots.push_back(RX);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001597 // Mark the super region of the RX as live.
1598 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1599 // 'y' => element region. 'x' is its super region.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001600 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1601 RegionRoots.push_back(SR->getSuperRegion());
1602 }
1603 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001604 }
1605
1606 // Get the subregions of R. These are RegionRoots as well since they
1607 // represent values that are also bound to R.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001608 RegionStoreSubRegionMap::iterator I, E;
1609 for (llvm::tie(I, E) = SubRegions->begin_end(R); I != E; ++I)
Ted Kremenek9af46f52009-06-16 22:36:44 +00001610 RegionRoots.push_back(*I);
1611 }
1612
1613 // We have now scanned the store, marking reachable regions and symbols
1614 // as live. We now remove all the regions that are dead from the store
1615 // as well as update DSymbols with the set symbols that are now dead.
1616 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1617 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001618 // If this region live? Is so, none of its symbols are dead.
1619 if (Marked.count(R))
1620 continue;
1621
1622 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001623 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001624
1625 // Mark all non-live symbols that this region references as dead.
1626 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1627 SymReaper.maybeDead(SymR->getSymbol());
1628
1629 SVal X = I.getData();
1630 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001631 for (; SI != SE; ++SI)
1632 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001633 }
1634
Ted Kremenek093569c2009-08-02 05:00:15 +00001635 // Remove dead 'default' bindings.
1636 RegionDefaultValue::MapTy NewDVM = DVM;
1637 RegionDefaultValue::MapTy::Factory &DVMFactory =
1638 state.get_context<RegionDefaultValue>();
1639
1640 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1641 I != E; ++I) {
1642 const MemRegion *R = I.getKey();
1643
1644 // If this region live? Is so, none of its symbols are dead.
1645 if (Marked.count(R))
1646 continue;
1647
1648 // Remove this dead region.
1649 NewDVM = DVMFactory.Remove(NewDVM, R);
1650
1651 // Mark all non-live symbols that this region references as dead.
1652 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1653 SymReaper.maybeDead(SymR->getSymbol());
1654
1655 SVal X = I.getData();
1656 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1657 for (; SI != SE; ++SI)
1658 SymReaper.maybeDead(*SI);
1659 }
1660
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001661 // Write the store back.
1662 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001663
1664 // Write the updated default bindings back.
1665 // FIXME: Right now this involves a fetching of a persistent state.
1666 // We can do better.
1667 if (DVM != NewDVM)
1668 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001669}
1670
1671//===----------------------------------------------------------------------===//
1672// Utility methods.
1673//===----------------------------------------------------------------------===//
1674
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001675void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001676 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001677 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001678 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001679
Ted Kremenek6f9b3a42009-07-13 23:53:06 +00001680 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001681 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001682}