blob: 70201759e100382c0567f12df8f967687854de9f [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);
Ted Kremenek4ed45982009-08-05 05:31:02 +0000124 if (I == M.end())
125 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
126 else
127 I->second = F.Add(I->second, SubRegion);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000128 }
129
130 ~RegionStoreSubRegionMap() {}
131
Ted Kremenek5dc27462009-03-03 02:51:43 +0000132 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000133 Map::iterator I = M.find(Parent);
134
135 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000136 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000137
138 llvm::ImmutableSet<const MemRegion*> S = I->second;
139 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
140 SI != SE; ++SI) {
141 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000142 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000143 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000144
145 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000146 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000147
148 typedef SetTy::iterator iterator;
149
150 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
151 Map::iterator I = M.find(R);
152 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
153 return std::make_pair(S.begin(), S.end());
154 }
Ted Kremenek59e8f112009-03-03 01:35:36 +0000155};
156
Zhongxing Xu17892752008-10-08 02:50:44 +0000157class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000158 const RegionStoreFeatures Features;
Zhongxing Xu17892752008-10-08 02:50:44 +0000159 RegionBindingsTy::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000160
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000161 const MemRegion* SelfRegion;
162 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000163
164public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000165 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000166 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000167 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000168 RBFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000169 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000170 if (const ObjCMethodDecl* MD =
171 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
172 SelfDecl = MD->getSelfDecl();
173 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000174
175 virtual ~RegionStoreManager() {}
176
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000177 SubRegionMap *getSubRegionMap(const GRState *state);
178
179 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000180
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000181 /// getLValueString - Returns an SVal representing the lvalue of a
182 /// StringLiteral. Within RegionStore a StringLiteral has an
183 /// associated StringRegion, and the lvalue of a StringLiteral is
184 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000185 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000186
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000187 /// getLValueCompoundLiteral - Returns an SVal representing the
188 /// lvalue of a compound literal. Within RegionStore a compound
189 /// literal has an associated region, and the lvalue of the
190 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000191 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000192
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000193 /// getLValueVar - Returns an SVal that represents the lvalue of a
194 /// variable. Within RegionStore a variable has an associated
195 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000196 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000197
Ted Kremenek67f28532009-06-17 22:02:04 +0000198 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000199
Ted Kremenek67f28532009-06-17 22:02:04 +0000200 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000201
Ted Kremenek67f28532009-06-17 22:02:04 +0000202 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000203
Ted Kremenek67f28532009-06-17 22:02:04 +0000204 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000205 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000206
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000207
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000208 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
209 /// type. 'Array' represents the lvalue of the array being decayed
210 /// to a pointer, and the returned SVal represents the decayed
211 /// version of that lvalue (i.e., a pointer to the first element of
212 /// the array). This is called by GRExprEngine when evaluating
213 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000214 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000215
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000216 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000217 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000218
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000219 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000220
221 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
222 /// 'this' object (C++). When used when analyzing a normal function this
223 /// method returns NULL.
224 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000225 if (!SelfDecl)
226 return 0;
227
228 if (!SelfRegion) {
229 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
230 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
231 MRMgr.getHeapRegion());
232 }
233
234 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000235 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000236
237 //===-------------------------------------------------------------------===//
238 // Binding values to regions.
239 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000240
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000241 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
242 const Expr *E, unsigned Count);
243
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000244private:
245 RegionBindingsTy RemoveSubRegionBindings(RegionBindingsTy B,
246 const MemRegion *R,
247 RegionStoreSubRegionMap &M);
248
249public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000250 const GRState *Bind(const GRState *state, Loc LV, SVal V);
251
252 const GRState *BindCompoundLiteral(const GRState *state,
253 const CompoundLiteralExpr* CL, SVal V);
254
255 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
256
257 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
258 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000259 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000260
Ted Kremenek67f28532009-06-17 22:02:04 +0000261 /// BindStruct - Bind a compound value to a structure.
262 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
263
264 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
265
266 /// KillStruct - Set the entire struct to unknown.
267 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
268
269 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
270
271 Store Remove(Store store, Loc LV);
272
273 //===------------------------------------------------------------------===//
274 // Loading values from regions.
275 //===------------------------------------------------------------------===//
276
277 /// The high level logic for this method is this:
278 /// Retrieve (L)
279 /// if L has binding
280 /// return L's binding
281 /// else if L is in killset
282 /// return unknown
283 /// else
284 /// if L is on stack or heap
285 /// return undefined
286 /// else
287 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000288 SValuator::CastResult Retrieve(const GRState *state, Loc L,
289 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000290
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000291 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000292
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000293 SVal RetrieveField(const GRState *state, const FieldRegion *R);
294
295 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000296
Ted Kremenek9031dd72009-07-21 00:12:07 +0000297 SVal RetrieveVar(const GRState *state, const VarRegion *R);
298
Ted Kremenek25c54572009-07-20 22:58:02 +0000299 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
300
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000301 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
302 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000303
Ted Kremenek67f28532009-06-17 22:02:04 +0000304 /// Retrieve the values in a struct and return a CompoundVal, used when doing
305 /// struct copy:
306 /// struct s x, y;
307 /// x = y;
308 /// y's value is retrieved by this method.
309 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
310
311 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
312
313 //===------------------------------------------------------------------===//
314 // State pruning.
315 //===------------------------------------------------------------------===//
316
317 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
318 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000319 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000320 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
321
322 //===------------------------------------------------------------------===//
323 // Region "extents".
324 //===------------------------------------------------------------------===//
325
326 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
327 SVal getSizeInElements(const GRState *state, const MemRegion* R);
328
329 //===------------------------------------------------------------------===//
330 // Region "views".
331 //===------------------------------------------------------------------===//
332
333 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
334 const MemRegion* Base);
335
336 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
337 const MemRegion* Base);
338
339 //===------------------------------------------------------------------===//
340 // Utility methods.
341 //===------------------------------------------------------------------===//
342
Zhongxing Xu17892752008-10-08 02:50:44 +0000343 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000344 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000345 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000346
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000347 void print(Store store, llvm::raw_ostream& Out, const char* nl,
348 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000349
350 void iterBindings(Store store, BindingsHandler& f) {
351 // FIXME: Implement.
352 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000353
Ted Kremenek67f28532009-06-17 22:02:04 +0000354 // FIXME: Remove.
355 BasicValueFactory& getBasicVals() {
356 return StateMgr.getBasicVals();
357 }
358
359 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000360 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000361};
362
363} // end anonymous namespace
364
Ted Kremenek9af46f52009-06-16 22:36:44 +0000365//===----------------------------------------------------------------------===//
366// RegionStore creation.
367//===----------------------------------------------------------------------===//
368
369StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
370 RegionStoreFeatures F = maximal_features_tag();
371 return new RegionStoreManager(StMgr, F);
372}
373
374StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
375 RegionStoreFeatures F = minimal_features_tag();
376 F.enableFields(true);
377 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000378}
379
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000380RegionStoreSubRegionMap*
381RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000382 RegionBindingsTy B = GetRegionBindings(state->getStore());
383 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
384
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000385 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
386 llvm::SmallVector<const SubRegion*, 10> WL;
387
388 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek59e8f112009-03-03 01:35:36 +0000389 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000390 WL.push_back(R);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000391
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000392 RegionDefaultValue::MapTy DVM = state->get<RegionDefaultValue>();
393 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
394 I != E; ++I)
395 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
396 WL.push_back(R);
397
398 // We also need to record in the subregion map "intermediate" regions that
399 // don't have direct bindings but are super regions of those that do.
400 while (!WL.empty()) {
401 const SubRegion *R = WL.back();
402 WL.pop_back();
403
404 if (Marked.count(R))
405 continue;
406
407 const MemRegion *superR = R->getSuperRegion();
408 M->add(superR, R);
409 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
410 WL.push_back(sr);
411 }
412
Ted Kremenek14453bf2009-03-03 19:02:42 +0000413 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000414}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000415
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000416SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
417 return getRegionStoreSubRegionMap(state);
418}
419
Ted Kremenek9af46f52009-06-16 22:36:44 +0000420//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000421// Binding invalidation.
422//===----------------------------------------------------------------------===//
423
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000424RegionBindingsTy
425RegionStoreManager::RemoveSubRegionBindings(RegionBindingsTy B,
426 const MemRegion *R,
427 RegionStoreSubRegionMap &M) {
428
429 RegionStoreSubRegionMap::iterator I, E;
430
431 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
432 B = RemoveSubRegionBindings(B, *I, M);
433
434 return RBFactory.Remove(B, R);
435}
436
437
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000438const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
439 const MemRegion *R,
440 const Expr *E,
441 unsigned Count) {
442 ASTContext& Ctx = StateMgr.getContext();
443
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000444 // Strip away casts.
445 R = R->getBaseRegion();
446
447 // Get the mapping of regions -> subregions.
448 llvm::OwningPtr<RegionStoreSubRegionMap>
449 SubRegions(getRegionStoreSubRegionMap(state));
450
451 // Remove the bindings to subregions.
452 RegionBindingsTy B = GetRegionBindings(state->getStore());
453 B = RemoveSubRegionBindings(B, R, *SubRegions.get());
454 state = state->makeWithStore(B.getRoot());
455
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000456 if (!R->isBoundable())
457 return state;
458
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000459 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
460 isa<ObjCObjectRegion>(R)) {
461 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000462 // conjured symbol. The type of the symbol is irrelavant.
463 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000464 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000465 }
466
467 const TypedRegion *TR = cast<TypedRegion>(R);
468 QualType T = TR->getValueType(Ctx);
469
470 // FIXME: The code causes a crash when using RegionStore on the test case
471 // 'test_invalidate_cast_int' (misc-ps.m). Consider removing it
472 // permanently. Region casts are probably not too strict to handle
473 // the transient interpretation of memory. Instead we can use the QualType
474 // passed to 'Retrieve' and friends to determine the most current
475 // interpretation of memory when it is actually used.
476#if 0
477 // If the region is cast to another type, use that type.
478 if (const QualType *CastTy = getCastType(state, R)) {
479 assert(!(*CastTy)->isObjCObjectPointerType());
Ted Kremenek6217b802009-07-29 21:53:49 +0000480 QualType NewT = (*CastTy)->getAs<PointerType>()->getPointeeType();
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000481
482 // The only exception is if the original region had a location type as its
483 // value type we always want to treat the region as binding to a location.
484 // This issue can arise when pointers are casted to integers and back.
485
486 if (!(Loc::IsLocType(T) && !Loc::IsLocType(NewT)))
487 T = NewT;
488 }
489#endif
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000490
491 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000492 // FIXME: handle structs with default region value.
493 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
494
495 // No record definition. There is nothing we can do.
496 if (!RD)
497 return state;
498
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000499 // Invalidate the region by setting its default value to
500 // conjured symbol. The type of the symbol is irrelavant.
501 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
502 return setDefaultValue(state, R, V);
503 }
504
505 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000506 // Set the default value of the array to conjured symbol.
507 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
508 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000509 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000510 }
511
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000512 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
513 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
514 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000515}
516
517//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000518// getLValueXXX methods.
519//===----------------------------------------------------------------------===//
520
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000521/// getLValueString - Returns an SVal representing the lvalue of a
522/// StringLiteral. Within RegionStore a StringLiteral has an
523/// associated StringRegion, and the lvalue of a StringLiteral is the
524/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000525SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000526 const StringLiteral* S) {
527 return loc::MemRegionVal(MRMgr.getStringRegion(S));
528}
529
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000530/// getLValueVar - Returns an SVal that represents the lvalue of a
531/// variable. Within RegionStore a variable has an associated
532/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000533SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000534 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
535}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000536
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000537/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
538/// of a compound literal. Within RegionStore a compound literal
539/// has an associated region, and the lvalue of the compound literal
540/// is the lvalue of that region.
541SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000542RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000543 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000544 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
545}
546
Ted Kremenek67f28532009-06-17 22:02:04 +0000547SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000548 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000549 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000550}
551
Ted Kremenek67f28532009-06-17 22:02:04 +0000552SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000553 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000554 return getLValueFieldOrIvar(St, Base, D);
555}
556
Ted Kremenek67f28532009-06-17 22:02:04 +0000557SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000558 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000559 if (Base.isUnknownOrUndef())
560 return Base;
561
562 Loc BaseL = cast<Loc>(Base);
563 const MemRegion* BaseR = 0;
564
565 switch (BaseL.getSubKind()) {
566 case loc::MemRegionKind:
567 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
568 break;
569
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000570 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000571 // These are anormal cases. Flag an undefined value.
572 return UndefinedVal();
573
574 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000575 // While these seem funny, this can happen through casts.
576 // FIXME: What we should return is the field offset. For example,
577 // add the field offset to the integer value. That way funny things
578 // like this work properly: &(((struct foo *) 0xa)->f)
579 return Base;
580
581 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000582 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000583 return Base;
584 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000585
586 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
587 // of FieldDecl.
588 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
589 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000590
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000591 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000592}
593
Ted Kremenek67f28532009-06-17 22:02:04 +0000594SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000595 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000596 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000597
Ted Kremenekde7ec632009-03-09 22:44:49 +0000598 // If the base is an unknown or undefined value, just return it back.
599 // FIXME: For absolute pointer addresses, we just return that value back as
600 // well, although in reality we should return the offset added to that
601 // value.
602 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000603 return Base;
604
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000605 // Only handle integer offsets... for now.
606 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000607 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000608
Zhongxing Xuce760782009-05-09 13:20:07 +0000609 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000610
611 // Pointer of any type can be cast and used as array base.
612 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
613
Ted Kremenek46537392009-07-16 01:33:37 +0000614 // Convert the offset to the appropriate size and signedness.
615 Offset = ValMgr.convertToArrayIndex(Offset);
616
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000617 if (!ElemR) {
618 //
619 // If the base region is not an ElementRegion, create one.
620 // This can happen in the following example:
621 //
622 // char *p = __builtin_alloc(10);
623 // p[1] = 8;
624 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000625 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000626 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000627 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000628 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000629 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000630
631 SVal BaseIdx = ElemR->getIndex();
632
633 if (!isa<nonloc::ConcreteInt>(BaseIdx))
634 return UnknownVal();
635
636 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
637 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
638 assert(BaseIdxI.isSigned());
639
Ted Kremenek46537392009-07-16 01:33:37 +0000640 // Compute the new index.
641 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000642
Ted Kremenek46537392009-07-16 01:33:37 +0000643 // Construct the new ElementRegion.
644 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000645 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
646 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000647}
648
Ted Kremenek9af46f52009-06-16 22:36:44 +0000649//===----------------------------------------------------------------------===//
650// Extents for regions.
651//===----------------------------------------------------------------------===//
652
Ted Kremenek67f28532009-06-17 22:02:04 +0000653SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000654 const MemRegion *R) {
655
656 switch (R->getKind()) {
657 case MemRegion::MemSpaceRegionKind:
658 assert(0 && "Cannot index into a MemSpace");
659 return UnknownVal();
660
661 case MemRegion::CodeTextRegionKind:
662 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000663 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000664
665 // Not yet handled.
666 case MemRegion::AllocaRegionKind:
667 case MemRegion::CompoundLiteralRegionKind:
668 case MemRegion::ElementRegionKind:
669 case MemRegion::FieldRegionKind:
670 case MemRegion::ObjCIvarRegionKind:
671 case MemRegion::ObjCObjectRegionKind:
672 case MemRegion::SymbolicRegionKind:
673 return UnknownVal();
674
675 case MemRegion::StringRegionKind: {
676 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
677 // We intentionally made the size value signed because it participates in
678 // operations with signed indices.
679 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000680 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000681
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000682 case MemRegion::VarRegionKind: {
683 const VarRegion* VR = cast<VarRegion>(R);
684 // Get the type of the variable.
685 QualType T = VR->getDesugaredValueType(getContext());
686
687 // FIXME: Handle variable-length arrays.
688 if (isa<VariableArrayType>(T))
689 return UnknownVal();
690
691 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
692 // return the size as signed integer.
693 return ValMgr.makeIntVal(CAT->getSize(), false);
694 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000695
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000696 // Clients can use ordinary variables as if they were arrays. These
697 // essentially are arrays of size 1.
698 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000699 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000700
701 case MemRegion::BEG_DECL_REGIONS:
702 case MemRegion::END_DECL_REGIONS:
703 case MemRegion::BEG_TYPED_REGIONS:
704 case MemRegion::END_TYPED_REGIONS:
705 assert(0 && "Infeasible region");
706 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000707 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000708
709 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000710 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000711}
712
Ted Kremenek67f28532009-06-17 22:02:04 +0000713const GRState *RegionStoreManager::setExtent(const GRState *state,
714 const MemRegion *region,
715 SVal extent) {
716 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000717}
718
719//===----------------------------------------------------------------------===//
720// Location and region casting.
721//===----------------------------------------------------------------------===//
722
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000723/// ArrayToPointer - Emulates the "decay" of an array to a pointer
724/// type. 'Array' represents the lvalue of the array being decayed
725/// to a pointer, and the returned SVal represents the decayed
726/// version of that lvalue (i.e., a pointer to the first element of
727/// the array). This is called by GRExprEngine when evaluating casts
728/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000729SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000730 if (!isa<loc::MemRegionVal>(Array))
731 return UnknownVal();
732
733 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
734 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
735
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000736 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000737 return UnknownVal();
738
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000739 // Strip off typedefs from the ArrayRegion's ValueType.
740 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000741 ArrayType *AT = cast<ArrayType>(T);
742 T = AT->getElementType();
743
Ted Kremenek75185b52009-07-16 00:00:11 +0000744 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
745 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000746
747 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000748}
749
Ted Kremenek9af46f52009-06-16 22:36:44 +0000750//===----------------------------------------------------------------------===//
751// Pointer arithmetic.
752//===----------------------------------------------------------------------===//
753
Zhongxing Xu262fd032009-05-20 09:00:16 +0000754SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000755 BinaryOperator::Opcode Op, Loc L, NonLoc R,
756 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000757 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000758 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000759 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000760
Zhongxing Xua1718c72009-04-03 07:33:13 +0000761 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000762 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000763
Ted Kremenek3bccf082009-07-11 00:58:27 +0000764 switch (MR->getKind()) {
765 case MemRegion::SymbolicRegionKind: {
766 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000767 SymbolRef Sym = SR->getSymbol();
768 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000769 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000770 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
771 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
772 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000773 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000774 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000775 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000776 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000777 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000778 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
779 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
780 break;
781 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000782
Ted Kremenek3bccf082009-07-11 00:58:27 +0000783 case MemRegion::ElementRegionKind: {
784 ER = cast<ElementRegion>(MR);
785 break;
786 }
787
788 // Not yet handled.
789 case MemRegion::VarRegionKind:
790 case MemRegion::StringRegionKind:
791 case MemRegion::CompoundLiteralRegionKind:
792 case MemRegion::FieldRegionKind:
793 case MemRegion::ObjCObjectRegionKind:
794 case MemRegion::ObjCIvarRegionKind:
795 return UnknownVal();
796
Ted Kremenek3bccf082009-07-11 00:58:27 +0000797 case MemRegion::CodeTextRegionKind:
798 // Technically this can happen if people do funny things with casts.
799 return UnknownVal();
800
801 case MemRegion::MemSpaceRegionKind:
802 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
803 return UnknownVal();
804
805 case MemRegion::BEG_DECL_REGIONS:
806 case MemRegion::END_DECL_REGIONS:
807 case MemRegion::BEG_TYPED_REGIONS:
808 case MemRegion::END_TYPED_REGIONS:
809 assert(0 && "Infeasible region");
810 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000811 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000812
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000813 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000814 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
815 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
816
817 // Only support concrete integer indexes for now.
818 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000819 // FIXME: Should use SValuator here.
820 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
821 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000822 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000823 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
824 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000825 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000826 }
827
828 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000829}
830
Ted Kremenek9af46f52009-06-16 22:36:44 +0000831//===----------------------------------------------------------------------===//
832// Loading values from regions.
833//===----------------------------------------------------------------------===//
834
Ted Kremeneka6275a52009-07-15 02:31:43 +0000835static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
836 RTy = Ctx.getCanonicalType(RTy);
837 UsedTy = Ctx.getCanonicalType(UsedTy);
838
839 if (RTy == UsedTy)
840 return false;
841
Ted Kremenek25c54572009-07-20 22:58:02 +0000842
843 // Recursively check the types. We basically want to see if a pointer value
844 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
845 // represents a reinterpretation.
846 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000847 const PointerType *PRTy = RTy->getAs<PointerType>();
848 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000849
850 return PUsedTy && PRTy &&
851 IsReinterpreted(PRTy->getPointeeType(),
852 PUsedTy->getPointeeType(), Ctx);
853 }
854
855 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000856}
857
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000858SValuator::CastResult
859RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000860
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000861 assert(!isa<UnknownVal>(L) && "location unknown");
862 assert(!isa<UndefinedVal>(L) && "location undefined");
863
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000864 // FIXME: Is this even possible? Shouldn't this be treated as a null
865 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000866 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000867 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000868
Ted Kremenek67f28532009-06-17 22:02:04 +0000869 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000870
Zhongxing Xu91844122009-05-20 09:18:48 +0000871 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000872 // Example:
873 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000874 // char* p = alloca();
875 // read(p);
876 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000877 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000878 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000879
880 if (isa<SymbolicRegion>(MR)) {
881 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000882 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000883 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000884 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
885 }
886
Ted Kremenek968f0a62009-08-03 21:41:46 +0000887 if (isa<CodeTextRegion>(MR))
888 return SValuator::CastResult(state, UnknownVal());
889
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000890 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
891 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000892 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000893 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000894
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000895 // FIXME: We should eventually handle funny addressing. e.g.:
896 //
897 // int x = ...;
898 // int *p = &x;
899 // char *q = (char*) p;
900 // char c = *q; // returns the first byte of 'x'.
901 //
902 // Such funny addressing will occur due to layering of regions.
903
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000904#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000905 ASTContext &Ctx = getContext();
906 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000907 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
908 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000909 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000910 assert(Ctx.getCanonicalType(RTy) ==
911 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000912 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000913#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000914
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000915 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000916 return SValuator::CastResult(state, RetrieveStruct(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000917
918 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000919 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000920
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000921 // FIXME: handle Vector types.
922 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000923 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000924
925 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000926 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000927
928 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000929 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000930
Ted Kremenek25c54572009-07-20 22:58:02 +0000931 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000932 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000933
934 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000935 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000936
Ted Kremenek67f28532009-06-17 22:02:04 +0000937 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000938 RegionBindingsTy::data_type* V = B.lookup(R);
939
940 // Check if the region has a binding.
941 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000942 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000943
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000944 // The location does not have a bound value. This means that it has
945 // the value it had upon its creation and/or entry to the analyzed
946 // function/method. These are either symbolic values or 'undefined'.
947
Ted Kremenek356e9d62009-07-22 04:35:42 +0000948#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000949 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000950#else
951 if (R->hasStackStorage()) {
952#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000953 // All stack variables are considered to have undefined values
954 // upon creation. All heap allocated blocks are considered to
955 // have undefined values as well unless they are explicitly bound
956 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000957 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000958 }
959
Ted Kremenek356e9d62009-07-22 04:35:42 +0000960#if USE_REGION_CASTS
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000961 // If the region is already cast to another type, use that type to create the
962 // symbol value.
963 if (const QualType *p = state->get<RegionCasts>(R)) {
964 QualType T = *p;
Ted Kremenek6217b802009-07-29 21:53:49 +0000965 RTy = T->getAs<PointerType>()->getPointeeType();
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000966 }
Ted Kremenek356e9d62009-07-22 04:35:42 +0000967#endif
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000968
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000969 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000970 return SValuator::CastResult(state,
971 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000972}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000973
974
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000975
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000976SVal RegionStoreManager::RetrieveElement(const GRState* state,
977 const ElementRegion* R) {
978 // Check if the region has a binding.
979 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +0000980 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000981 return *V;
982
Ted Kremenek921109a2009-07-01 23:19:52 +0000983 const MemRegion* superR = R->getSuperRegion();
984
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000985 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +0000986 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000987 const StringLiteral *Str = StrR->getStringLiteral();
988 SVal Idx = R->getIndex();
989 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
990 int64_t i = CI->getValue().getSExtValue();
991 char c;
992 if (i == Str->getByteLength())
993 c = '\0';
994 else
995 c = Str->getStrData()[i];
996 return ValMgr.makeIntVal(c, getContext().CharTy);
997 }
998 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000999
1000 // Special case: the current region represents a cast and it and the super
1001 // region both have pointer types or intptr_t types. If so, perform the
1002 // retrieve from the super region and appropriately "cast" the value.
1003 // This is needed to support OSAtomicCompareAndSwap and friends or other
1004 // loads that treat integers as pointers and vis versa.
1005 if (R->getIndex().isZeroConstant()) {
1006 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1007 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001008 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1009 QualType valTy = R->getValueType(Ctx);
1010 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1011 // Retrieve the value from the super region. This will be casted to
1012 // valTy when we return to 'Retrieve'.
1013 const SValuator::CastResult &cr = Retrieve(state,
1014 loc::MemRegionVal(superR),
1015 valTy);
1016 return cr.getSVal();
1017 }
1018 }
1019 }
1020 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001021
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001022 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +00001023 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001024 if (D->hasConjuredSymbol())
1025 return ValMgr.getRegionValueSymbolVal(R);
1026 else
1027 return *D;
1028 }
1029
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001030 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001031 if (const SVal *V = B.lookup(superR)) {
1032 if (SymbolRef parentSym = V->getAsSymbol())
1033 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001034
1035 if (V->isUnknownOrUndef())
1036 return *V;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001037
1038 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001039 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001040 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001041
Ted Kremenek356e9d62009-07-22 04:35:42 +00001042#if 0
Ted Kremenek921109a2009-07-01 23:19:52 +00001043 if (R->hasHeapStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001044 // FIXME: If the region has heap storage and we know nothing special
1045 // about its bindings, should we instead return UnknownVal? Seems like
1046 // we should only return UndefinedVal in the cases where we know the value
1047 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001048 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +00001049 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001050#endif
1051
Ted Kremenekdc147262009-07-02 22:02:15 +00001052 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +00001053 // Currently we don't reason specially about Clang-style vectors. Check
1054 // if superR is a vector and if so return Unknown.
1055 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1056 if (typedSuperR->getValueType(getContext())->isVectorType())
1057 return UnknownVal();
1058 }
1059
1060 return UndefinedVal();
1061 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001062
1063 QualType Ty = R->getValueType(getContext());
1064
Ted Kremenek356e9d62009-07-22 04:35:42 +00001065#if USE_REGION_CASTS
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001066 // If the region is already cast to another type, use that type to create the
1067 // symbol value.
1068 if (const QualType *p = state->get<RegionCasts>(R))
Ted Kremenek6217b802009-07-29 21:53:49 +00001069 Ty = (*p)->getAs<PointerType>()->getPointeeType();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001070#endif
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001071
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001072 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001073}
1074
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001075SVal RegionStoreManager::RetrieveField(const GRState* state,
1076 const FieldRegion* R) {
1077 QualType Ty = R->getValueType(getContext());
1078
1079 // Check if the region has a binding.
1080 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001081 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001082 return *V;
1083
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001084 const MemRegion* superR = R->getSuperRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001085 while (superR) {
1086 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
1087 if (SymbolRef parentSym = D->getAsSymbol())
1088 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001089
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001090 if (D->isZeroConstant())
1091 return ValMgr.makeZeroVal(Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001092
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001093 if (D->isUnknown())
1094 return *D;
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001095
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001096 assert(0 && "Unknown default value");
1097 }
1098
1099 // If our super region is a field or element itself, walk up the region
1100 // hierarchy to see if there is a default value installed in an ancestor.
1101 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1102 superR = cast<SubRegion>(superR)->getSuperRegion();
1103 continue;
1104 }
1105
1106 break;
1107 }
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001108
Ted Kremenek356e9d62009-07-22 04:35:42 +00001109#if HEAP_UNDEFINED
Ted Kremenekdc147262009-07-02 22:02:15 +00001110 // FIXME: Is this correct? Should it be UnknownVal?
1111 if (R->hasHeapStorage())
1112 return UndefinedVal();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001113#endif
Ted Kremenekdc147262009-07-02 22:02:15 +00001114
1115 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001116 return UndefinedVal();
1117
Ted Kremenek356e9d62009-07-22 04:35:42 +00001118#if USE_REGION_CASTS
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001119 // If the region is already cast to another type, use that type to create the
1120 // symbol value.
1121 if (const QualType *p = state->get<RegionCasts>(R)) {
1122 QualType tmp = *p;
Ted Kremenek6217b802009-07-29 21:53:49 +00001123 Ty = tmp->getAs<PointerType>()->getPointeeType();
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001124 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001125#endif
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001126
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001127 // All other values are symbolic.
1128 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001129}
1130
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001131SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1132 const ObjCIvarRegion* R) {
1133
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001134 // Check if the region has a binding.
1135 RegionBindingsTy B = GetRegionBindings(state->getStore());
1136
1137 if (const SVal* V = B.lookup(R))
1138 return *V;
1139
1140 const MemRegion *superR = R->getSuperRegion();
1141
1142 // Check if the super region has a binding.
1143 if (const SVal *V = B.lookup(superR)) {
1144 if (SymbolRef parentSym = V->getAsSymbol())
1145 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1146
1147 // Other cases: give up.
1148 return UnknownVal();
1149 }
1150
Ted Kremenek25c54572009-07-20 22:58:02 +00001151 return RetrieveLazySymbol(state, R);
1152}
1153
Ted Kremenek9031dd72009-07-21 00:12:07 +00001154SVal RegionStoreManager::RetrieveVar(const GRState *state,
1155 const VarRegion *R) {
1156
1157 // Check if the region has a binding.
1158 RegionBindingsTy B = GetRegionBindings(state->getStore());
1159
1160 if (const SVal* V = B.lookup(R))
1161 return *V;
1162
1163 // Lazily derive a value for the VarRegion.
1164 const VarDecl *VD = R->getDecl();
1165
1166 if (VD == SelfDecl)
1167 return loc::MemRegionVal(getSelfRegion(0));
1168
1169 if (R->hasGlobalsOrParametersStorage())
1170 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1171
1172 return UndefinedVal();
1173}
1174
Ted Kremenek25c54572009-07-20 22:58:02 +00001175SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1176 const TypedRegion *R) {
1177
1178 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001179
1180#if USE_REGION_CASTS
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001181 // If the region is already cast to another type, use that type to create the
1182 // symbol value.
Ted Kremenek25c54572009-07-20 22:58:02 +00001183 if (const QualType *ty = state->get<RegionCasts>(R)) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001184 if (const PointerType *PT = (*ty)->getAs<PointerType>()) {
Ted Kremenek25c54572009-07-20 22:58:02 +00001185 QualType castTy = PT->getPointeeType();
1186
1187 if (!IsReinterpreted(valTy, castTy, getContext()))
1188 valTy = castTy;
1189 }
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001190 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001191#endif
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001192
1193 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001194 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001195}
1196
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001197SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1198 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001199 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001200 assert(T->isStructureType());
1201
Zhongxing Xub7507d12009-06-11 07:27:30 +00001202 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001203 RecordDecl* RD = RT->getDecl();
1204 assert(RD->isDefinition());
1205
1206 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1207
Ted Kremenek67f28532009-06-17 22:02:04 +00001208 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1209 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001210 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001211
Douglas Gregore267ff32008-12-11 20:41:00 +00001212 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1213 FieldEnd = Fields.rend();
1214 Field != FieldEnd; ++Field) {
1215 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001216 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001217 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001218 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1219 }
1220
Zhongxing Xud91ee272009-06-23 09:02:15 +00001221 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001222}
1223
Ted Kremenek67f28532009-06-17 22:02:04 +00001224SVal RegionStoreManager::RetrieveArray(const GRState *state,
1225 const TypedRegion * R) {
1226
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001227 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001228 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1229
1230 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001231 uint64_t size = CAT->getSize().getZExtValue();
1232 for (uint64_t i = 0; i < size; ++i) {
1233 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001234 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1235 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001236 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001237 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001238 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1239 }
1240
Zhongxing Xud91ee272009-06-23 09:02:15 +00001241 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001242}
1243
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001244SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1245 const GRState *state,
1246 const TypedRegion *R,
1247 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001248 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001249 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001250
1251 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001252 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001253}
1254
Ted Kremenek9af46f52009-06-16 22:36:44 +00001255//===----------------------------------------------------------------------===//
1256// Binding values to regions.
1257//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001258
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001259Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001260 const MemRegion* R = 0;
1261
1262 if (isa<loc::MemRegionVal>(L))
1263 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001264
1265 if (R) {
1266 RegionBindingsTy B = GetRegionBindings(store);
1267 return RBFactory.Remove(B, R).getRoot();
1268 }
1269
1270 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001271}
1272
Ted Kremenek67f28532009-06-17 22:02:04 +00001273const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001274 if (isa<loc::ConcreteInt>(L))
1275 return state;
1276
Ted Kremenek9af46f52009-06-16 22:36:44 +00001277 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001278 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001279
1280 // Check if the region is a struct region.
1281 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1282 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001283 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001284
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001285 // Special case: the current region represents a cast and it and the super
1286 // region both have pointer types or intptr_t types. If so, perform the
1287 // bind to the super region.
1288 // This is needed to support OSAtomicCompareAndSwap and friends or other
1289 // loads that treat integers as pointers and vis versa.
1290 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1291 if (ER->getIndex().isZeroConstant()) {
1292 if (const TypedRegion *superR =
1293 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1294 ASTContext &Ctx = getContext();
1295 QualType superTy = superR->getValueType(Ctx);
1296 QualType erTy = ER->getValueType(Ctx);
1297
1298 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1299 IsAnyPointerOrIntptr(erTy, Ctx)) {
1300 SValuator::CastResult cr =
1301 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1302 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1303 }
1304 }
1305 }
1306 }
1307
1308 // Perform the binding.
Ted Kremenek67f28532009-06-17 22:02:04 +00001309 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001310 B = RBFactory.Add(B, R, V);
Ted Kremenek67f28532009-06-17 22:02:04 +00001311 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001312}
1313
Ted Kremenek67f28532009-06-17 22:02:04 +00001314const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001315 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001316
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001317 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001318 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001319
Ted Kremenek0964a062009-01-21 06:57:53 +00001320 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001321 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001322 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001323 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001324
Zhongxing Xud91ee272009-06-23 09:02:15 +00001325 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001326}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001327
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001328// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001329const GRState *
1330RegionStoreManager::BindCompoundLiteral(const GRState *state,
1331 const CompoundLiteralExpr* CL,
1332 SVal V) {
1333
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001334 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001335 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001336}
1337
Ted Kremenek67f28532009-06-17 22:02:04 +00001338const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001339 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001340 SVal Init) {
1341
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001342 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001343 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001344 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001345
Ted Kremenek46537392009-07-16 01:33:37 +00001346 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001347
1348 // Check if the init expr is a StringLiteral.
1349 if (isa<loc::MemRegionVal>(Init)) {
1350 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1351 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1352 const char* str = S->getStrData();
1353 unsigned len = S->getByteLength();
1354 unsigned j = 0;
1355
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001356 // Copy bytes from the string literal into the target array. Trailing bytes
1357 // in the array that are not covered by the string literal are initialized
1358 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001359 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001360 if (j >= len)
1361 break;
1362
Ted Kremenek46537392009-07-16 01:33:37 +00001363 SVal Idx = ValMgr.makeArrayIndex(i);
1364 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1365 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001366
Zhongxing Xud91ee272009-06-23 09:02:15 +00001367 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001368 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001369 }
1370
Ted Kremenek67f28532009-06-17 22:02:04 +00001371 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001372 }
1373
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001374 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001375 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001376 uint64_t i = 0;
1377
1378 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001379 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001380 if (VI == VE)
1381 break;
1382
Ted Kremenek46537392009-07-16 01:33:37 +00001383 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001384 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001385
1386 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001387 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001388 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001389 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001390 }
1391
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001392 // If the init list is shorter than the array length, set the array default
1393 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001394 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001395 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001396 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001397 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001398 }
1399 }
1400
Ted Kremenek67f28532009-06-17 22:02:04 +00001401 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001402}
1403
Ted Kremenek67f28532009-06-17 22:02:04 +00001404const GRState *
1405RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1406 SVal V) {
1407
1408 if (!Features.supportsFields())
1409 return state;
1410
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001411 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001412 assert(T->isStructureType());
1413
Ted Kremenek6217b802009-07-29 21:53:49 +00001414 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001415 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001416
1417 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001418 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001419
Ted Kremenek67f28532009-06-17 22:02:04 +00001420 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1421 // Ignore them and kill the field values.
1422 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1423 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001424
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001425 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001426 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001427
1428 RecordDecl::field_iterator FI, FE;
1429
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001430 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001431
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001432 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001433 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001434
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001435 QualType FTy = (*FI)->getType();
1436 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1437
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001438 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001439 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001440 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001441 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001442 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001443 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001444 }
1445
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001446 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001447 if (FI != FE)
1448 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001449
Ted Kremenek67f28532009-06-17 22:02:04 +00001450 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001451}
1452
Ted Kremenek67f28532009-06-17 22:02:04 +00001453const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001454 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001455
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001456 // Set the default value of the struct region to "unknown".
1457 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001458
1459 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001460 Store store = state->getStore();
1461 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001462 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001463 const MemRegion* R = I.getKey();
1464 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1465 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001466 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001467 }
1468
Ted Kremenek67f28532009-06-17 22:02:04 +00001469 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001470}
1471
Ted Kremenek67f28532009-06-17 22:02:04 +00001472const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1473 const MemRegion* R, SVal V) {
1474 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001475}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001476
1477//===----------------------------------------------------------------------===//
1478// State pruning.
1479//===----------------------------------------------------------------------===//
1480
1481static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1482 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1483 const MemRegion *R = XR->getRegion();
1484
1485 while (R) {
1486 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1487 SymReaper.markLive(SR->getSymbol());
1488 return;
1489 }
1490
1491 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1492 R = SR->getSuperRegion();
1493 continue;
1494 }
1495
1496 break;
1497 }
1498
1499 return;
1500 }
1501
1502 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1503 SymReaper.markLive(*SI);
1504}
1505
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001506void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1507 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001508 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001509{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001510 Store store = state.getStore();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001511 RegionBindingsTy B = GetRegionBindings(store);
1512
1513 // Lazily constructed backmap from MemRegions to SubRegions.
1514 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1515 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1516
Ted Kremenek9af46f52009-06-16 22:36:44 +00001517 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001518 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001519 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001520
1521 // Do a pass over the regions in the store. For VarRegions we check if
1522 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001523 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001524 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1525
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001526 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001527 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001528 const MemRegion *R = I.getKey();
1529 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001530 }
1531
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001532 // Scan the default bindings for "intermediate" roots.
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001533 RegionDefaultValue::MapTy DVM = state.get<RegionDefaultValue>();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001534 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1535 I != E; ++I) {
1536 const MemRegion *R = I.getKey();
1537 IntermediateRoots.push_back(R);
1538 }
1539
1540 // Process the "intermediate" roots to find if they are referenced by
1541 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001542 while (!IntermediateRoots.empty()) {
1543 const MemRegion* R = IntermediateRoots.back();
1544 IntermediateRoots.pop_back();
1545
1546 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001547 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001548 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001549 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001550 continue;
1551 }
1552
1553 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001554 if (SymReaper.isLive(SR->getSymbol()))
1555 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001556 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001557 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001558
1559 // Add the super region for R to the worklist if it is a subregion.
1560 if (const SubRegion* superR =
1561 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1562 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001563 }
1564
1565 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1566 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001567 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001568 while (!RegionRoots.empty()) {
1569 // Dequeue the next region on the worklist.
1570 const MemRegion* R = RegionRoots.back();
1571 RegionRoots.pop_back();
1572
1573 // Check if we have already processed this region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001574 if (Marked.count(R))
1575 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001576
1577 // Mark this region as processed. This is needed for termination in case
1578 // a region is referenced more than once.
1579 Marked.insert(R);
1580
1581 // Mark the symbol for any live SymbolicRegion as "live". This means we
1582 // should continue to track that symbol.
1583 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1584 SymReaper.markLive(SymR->getSymbol());
1585
1586 // Get the data binding for R (if any).
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001587 const SVal* Xptr = B.lookup(R);
1588 if (!Xptr) {
1589 // No direct binding? Get the default binding for R (if any).
1590 Xptr = DVM.lookup(R);
1591 }
1592
1593 // Direct or default binding?
Ted Kremenek9af46f52009-06-16 22:36:44 +00001594 if (Xptr) {
1595 SVal X = *Xptr;
1596 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1597
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001598 // If X is a region, then add it to the RegionRoots.
1599 if (const MemRegion *RX = X.getAsRegion()) {
1600 RegionRoots.push_back(RX);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001601 // Mark the super region of the RX as live.
1602 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1603 // 'y' => element region. 'x' is its super region.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001604 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1605 RegionRoots.push_back(SR->getSuperRegion());
1606 }
1607 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001608 }
1609
1610 // Get the subregions of R. These are RegionRoots as well since they
1611 // represent values that are also bound to R.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001612 RegionStoreSubRegionMap::iterator I, E;
1613 for (llvm::tie(I, E) = SubRegions->begin_end(R); I != E; ++I)
Ted Kremenek9af46f52009-06-16 22:36:44 +00001614 RegionRoots.push_back(*I);
1615 }
1616
1617 // We have now scanned the store, marking reachable regions and symbols
1618 // as live. We now remove all the regions that are dead from the store
1619 // as well as update DSymbols with the set symbols that are now dead.
1620 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1621 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001622 // If this region live? Is so, none of its symbols are dead.
1623 if (Marked.count(R))
1624 continue;
1625
1626 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001627 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001628
1629 // Mark all non-live symbols that this region references as dead.
1630 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1631 SymReaper.maybeDead(SymR->getSymbol());
1632
1633 SVal X = I.getData();
1634 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001635 for (; SI != SE; ++SI)
1636 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001637 }
1638
Ted Kremenek093569c2009-08-02 05:00:15 +00001639 // Remove dead 'default' bindings.
1640 RegionDefaultValue::MapTy NewDVM = DVM;
1641 RegionDefaultValue::MapTy::Factory &DVMFactory =
1642 state.get_context<RegionDefaultValue>();
1643
1644 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1645 I != E; ++I) {
1646 const MemRegion *R = I.getKey();
1647
1648 // If this region live? Is so, none of its symbols are dead.
1649 if (Marked.count(R))
1650 continue;
1651
1652 // Remove this dead region.
1653 NewDVM = DVMFactory.Remove(NewDVM, R);
1654
1655 // Mark all non-live symbols that this region references as dead.
1656 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1657 SymReaper.maybeDead(SymR->getSymbol());
1658
1659 SVal X = I.getData();
1660 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1661 for (; SI != SE; ++SI)
1662 SymReaper.maybeDead(*SI);
1663 }
1664
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001665 // Write the store back.
1666 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001667
1668 // Write the updated default bindings back.
1669 // FIXME: Right now this involves a fetching of a persistent state.
1670 // We can do better.
1671 if (DVM != NewDVM)
1672 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001673}
1674
1675//===----------------------------------------------------------------------===//
1676// Utility methods.
1677//===----------------------------------------------------------------------===//
1678
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001679void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001680 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001681 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001682 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001683
Ted Kremenek6f9b3a42009-07-13 23:53:06 +00001684 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001685 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001686}