blob: 32a1dc01759f775511182e7d94c3011c7c3bf90f [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 USE_REGION_CASTS 0
31#define HEAP_UNDEFINED 0
32
Zhongxing Xubaf03a72008-11-24 09:44:56 +000033// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000034typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000035
Ted Kremenek50dc1b32008-12-24 01:05:03 +000036//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000037// Fine-grained control of RegionStoreManager.
38//===----------------------------------------------------------------------===//
39
40namespace {
41struct VISIBILITY_HIDDEN minimal_features_tag {};
42struct VISIBILITY_HIDDEN maximal_features_tag {};
43
44class VISIBILITY_HIDDEN RegionStoreFeatures {
45 bool SupportsFields;
46 bool SupportsRemaining;
47
48public:
49 RegionStoreFeatures(minimal_features_tag) :
50 SupportsFields(false), SupportsRemaining(false) {}
51
52 RegionStoreFeatures(maximal_features_tag) :
53 SupportsFields(true), SupportsRemaining(false) {}
54
55 void enableFields(bool t) { SupportsFields = t; }
56
57 bool supportsFields() const { return SupportsFields; }
58 bool supportsRemaining() const { return SupportsRemaining; }
59};
60}
61
62//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000063// Region "Views"
64//===----------------------------------------------------------------------===//
65//
66// MemRegions can be layered on top of each other. This GDM entry tracks
67// what are the MemRegions that layer a given MemRegion.
68//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000069typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000070namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
71static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000072namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000073 template<> struct GRStateTrait<RegionViewMap>
74 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
75 RegionViews> > {
76
77 static void* GDMIndex() { return &RegionViewMapIndex; }
78 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000079}
Zhongxing Xu17892752008-10-08 02:50:44 +000080
Zhongxing Xu20794942009-05-06 08:33:50 +000081// RegionCasts records the current cast type of a region.
82namespace { class VISIBILITY_HIDDEN RegionCasts {}; }
83static int RegionCastsIndex = 0;
84namespace clang {
85 template<> struct GRStateTrait<RegionCasts>
86 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
87 QualType> > {
88 static void* GDMIndex() { return &RegionCastsIndex; }
89 };
90}
91
Ted Kremenek50dc1b32008-12-24 01:05:03 +000092//===----------------------------------------------------------------------===//
93// Region "Extents"
94//===----------------------------------------------------------------------===//
95//
96// MemRegions represent chunks of memory with a size (their "extent"). This
97// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000098//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000099namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
100static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000101namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000102 template<> struct GRStateTrait<RegionExtents>
103 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
104 static void* GDMIndex() { return &RegionExtentsIndex; }
105 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000106}
107
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000108//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000109// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000110//===----------------------------------------------------------------------===//
111//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000112// This GDM entry tracks what regions have a default value if they have no bound
113// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000114//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000115namespace {
116class VISIBILITY_HIDDEN RegionDefaultValue {
117public:
118 typedef llvm::ImmutableMap<const MemRegion*, SVal> MapTy;
119};
120}
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000121static int RegionDefaultValueIndex = 0;
122namespace clang {
123 template<> struct GRStateTrait<RegionDefaultValue>
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000124 : public GRStatePartialTrait<RegionDefaultValue::MapTy> {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000125 static void* GDMIndex() { return &RegionDefaultValueIndex; }
126 };
127}
128
129//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000130// Utility functions.
131//===----------------------------------------------------------------------===//
132
133static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
134 if (ty->isAnyPointerType())
135 return true;
136
137 return ty->isIntegerType() && ty->isScalarType() &&
138 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
139}
140
141//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000142// Main RegionStore logic.
143//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000144
Zhongxing Xu17892752008-10-08 02:50:44 +0000145namespace {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000146
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000147class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
148 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
149 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
150 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000151 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000152public:
153 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
154 Map::iterator I = M.find(Parent);
155 M.insert(std::make_pair(Parent,
156 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
157 }
158
159 ~RegionStoreSubRegionMap() {}
160
Ted Kremenek5dc27462009-03-03 02:51:43 +0000161 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000162 Map::iterator I = M.find(Parent);
163
164 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000165 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000166
167 llvm::ImmutableSet<const MemRegion*> S = I->second;
168 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
169 SI != SE; ++SI) {
170 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000171 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000172 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000173
174 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000175 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000176
177 typedef SetTy::iterator iterator;
178
179 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
180 Map::iterator I = M.find(R);
181 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
182 return std::make_pair(S.begin(), S.end());
183 }
Ted Kremenek59e8f112009-03-03 01:35:36 +0000184};
185
Zhongxing Xu17892752008-10-08 02:50:44 +0000186class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000187 const RegionStoreFeatures Features;
Zhongxing Xu17892752008-10-08 02:50:44 +0000188 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000189 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000190
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000191 const MemRegion* SelfRegion;
192 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000193
194public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000195 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000196 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000197 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000198 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000199 RVFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000200 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000201 if (const ObjCMethodDecl* MD =
202 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
203 SelfDecl = MD->getSelfDecl();
204 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000205
206 virtual ~RegionStoreManager() {}
207
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000208 SubRegionMap *getSubRegionMap(const GRState *state);
209
210 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000211
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000212 /// getLValueString - Returns an SVal representing the lvalue of a
213 /// StringLiteral. Within RegionStore a StringLiteral has an
214 /// associated StringRegion, and the lvalue of a StringLiteral is
215 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000216 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000217
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000218 /// getLValueCompoundLiteral - Returns an SVal representing the
219 /// lvalue of a compound literal. Within RegionStore a compound
220 /// literal has an associated region, and the lvalue of the
221 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000222 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000223
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000224 /// getLValueVar - Returns an SVal that represents the lvalue of a
225 /// variable. Within RegionStore a variable has an associated
226 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000227 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000228
Ted Kremenek67f28532009-06-17 22:02:04 +0000229 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000230
Ted Kremenek67f28532009-06-17 22:02:04 +0000231 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000232
Ted Kremenek67f28532009-06-17 22:02:04 +0000233 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000234
Ted Kremenek67f28532009-06-17 22:02:04 +0000235 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000236 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000237
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000238
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000239 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
240 /// type. 'Array' represents the lvalue of the array being decayed
241 /// to a pointer, and the returned SVal represents the decayed
242 /// version of that lvalue (i.e., a pointer to the first element of
243 /// the array). This is called by GRExprEngine when evaluating
244 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000245 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000246
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000247 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000248 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000249
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000250 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000251
252 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
253 /// 'this' object (C++). When used when analyzing a normal function this
254 /// method returns NULL.
255 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000256 if (!SelfDecl)
257 return 0;
258
259 if (!SelfRegion) {
260 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
261 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
262 MRMgr.getHeapRegion());
263 }
264
265 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000266 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000267
268 //===-------------------------------------------------------------------===//
269 // Binding values to regions.
270 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000271
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000272 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
273 const Expr *E, unsigned Count);
274
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000275private:
276 RegionBindingsTy RemoveSubRegionBindings(RegionBindingsTy B,
277 const MemRegion *R,
278 RegionStoreSubRegionMap &M);
279
280public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000281 const GRState *Bind(const GRState *state, Loc LV, SVal V);
282
283 const GRState *BindCompoundLiteral(const GRState *state,
284 const CompoundLiteralExpr* CL, SVal V);
285
286 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
287
288 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
289 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000290 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000291
Ted Kremenek67f28532009-06-17 22:02:04 +0000292 /// BindStruct - Bind a compound value to a structure.
293 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
294
295 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
296
297 /// KillStruct - Set the entire struct to unknown.
298 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
299
300 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
301
302 Store Remove(Store store, Loc LV);
303
304 //===------------------------------------------------------------------===//
305 // Loading values from regions.
306 //===------------------------------------------------------------------===//
307
308 /// The high level logic for this method is this:
309 /// Retrieve (L)
310 /// if L has binding
311 /// return L's binding
312 /// else if L is in killset
313 /// return unknown
314 /// else
315 /// if L is on stack or heap
316 /// return undefined
317 /// else
318 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000319 SValuator::CastResult Retrieve(const GRState *state, Loc L,
320 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000321
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000322 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000323
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000324 SVal RetrieveField(const GRState *state, const FieldRegion *R);
325
326 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000327
Ted Kremenek9031dd72009-07-21 00:12:07 +0000328 SVal RetrieveVar(const GRState *state, const VarRegion *R);
329
Ted Kremenek25c54572009-07-20 22:58:02 +0000330 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
331
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000332 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
333 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000334
Ted Kremenek67f28532009-06-17 22:02:04 +0000335 /// Retrieve the values in a struct and return a CompoundVal, used when doing
336 /// struct copy:
337 /// struct s x, y;
338 /// x = y;
339 /// y's value is retrieved by this method.
340 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
341
342 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
343
344 //===------------------------------------------------------------------===//
345 // State pruning.
346 //===------------------------------------------------------------------===//
347
348 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
349 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000350 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000351 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
352
353 //===------------------------------------------------------------------===//
354 // Region "extents".
355 //===------------------------------------------------------------------===//
356
357 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
358 SVal getSizeInElements(const GRState *state, const MemRegion* R);
359
360 //===------------------------------------------------------------------===//
361 // Region "views".
362 //===------------------------------------------------------------------===//
363
364 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
365 const MemRegion* Base);
366
367 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
368 const MemRegion* Base);
369
370 //===------------------------------------------------------------------===//
371 // Utility methods.
372 //===------------------------------------------------------------------===//
373
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000374 const GRState *setCastType(const GRState *state, const MemRegion* R,
375 QualType T);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000376
Zhongxing Xu82037252009-07-14 01:12:46 +0000377 const QualType *getCastType(const GRState *state, const MemRegion *R) {
378 return state->get<RegionCasts>(R);
379 }
380
Zhongxing Xu17892752008-10-08 02:50:44 +0000381 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000382 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000383 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000384
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000385 void print(Store store, llvm::raw_ostream& Out, const char* nl,
386 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000387
388 void iterBindings(Store store, BindingsHandler& f) {
389 // FIXME: Implement.
390 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000391
Ted Kremenek67f28532009-06-17 22:02:04 +0000392 // FIXME: Remove.
393 BasicValueFactory& getBasicVals() {
394 return StateMgr.getBasicVals();
395 }
396
397 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000398 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000399};
400
401} // end anonymous namespace
402
Zhongxing Xu82037252009-07-14 01:12:46 +0000403static bool isGenericPtr(ASTContext &Ctx, QualType Ty) {
404 if (Ty->isObjCIdType() || Ty->isObjCQualifiedIdType())
405 return true;
406
407 while (true) {
408 Ty = Ctx.getCanonicalType(Ty);
409
410 if (Ty->isVoidType())
411 return true;
412
Ted Kremenek6217b802009-07-29 21:53:49 +0000413 if (const PointerType *PT = Ty->getAs<PointerType>()) {
Zhongxing Xu82037252009-07-14 01:12:46 +0000414 Ty = PT->getPointeeType();
415 continue;
416 }
417
418 break;
419 }
420
421 return false;
422}
423
Ted Kremenek9af46f52009-06-16 22:36:44 +0000424//===----------------------------------------------------------------------===//
425// RegionStore creation.
426//===----------------------------------------------------------------------===//
427
428StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
429 RegionStoreFeatures F = maximal_features_tag();
430 return new RegionStoreManager(StMgr, F);
431}
432
433StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
434 RegionStoreFeatures F = minimal_features_tag();
435 F.enableFields(true);
436 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000437}
438
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000439RegionStoreSubRegionMap*
440RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000441 RegionBindingsTy B = GetRegionBindings(state->getStore());
442 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
443
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000444 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
445 llvm::SmallVector<const SubRegion*, 10> WL;
446
447 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek59e8f112009-03-03 01:35:36 +0000448 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000449 WL.push_back(R);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000450
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000451 RegionDefaultValue::MapTy DVM = state->get<RegionDefaultValue>();
452 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
453 I != E; ++I)
454 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
455 WL.push_back(R);
456
457 // We also need to record in the subregion map "intermediate" regions that
458 // don't have direct bindings but are super regions of those that do.
459 while (!WL.empty()) {
460 const SubRegion *R = WL.back();
461 WL.pop_back();
462
463 if (Marked.count(R))
464 continue;
465
466 const MemRegion *superR = R->getSuperRegion();
467 M->add(superR, R);
468 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
469 WL.push_back(sr);
470 }
471
Ted Kremenek14453bf2009-03-03 19:02:42 +0000472 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000473}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000474
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000475SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
476 return getRegionStoreSubRegionMap(state);
477}
478
Ted Kremenek9af46f52009-06-16 22:36:44 +0000479//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000480// Binding invalidation.
481//===----------------------------------------------------------------------===//
482
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000483RegionBindingsTy
484RegionStoreManager::RemoveSubRegionBindings(RegionBindingsTy B,
485 const MemRegion *R,
486 RegionStoreSubRegionMap &M) {
487
488 RegionStoreSubRegionMap::iterator I, E;
489
490 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
491 B = RemoveSubRegionBindings(B, *I, M);
492
493 return RBFactory.Remove(B, R);
494}
495
496
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000497const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
498 const MemRegion *R,
499 const Expr *E,
500 unsigned Count) {
501 ASTContext& Ctx = StateMgr.getContext();
502
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000503 // Strip away casts.
504 R = R->getBaseRegion();
505
506 // Get the mapping of regions -> subregions.
507 llvm::OwningPtr<RegionStoreSubRegionMap>
508 SubRegions(getRegionStoreSubRegionMap(state));
509
510 // Remove the bindings to subregions.
511 RegionBindingsTy B = GetRegionBindings(state->getStore());
512 B = RemoveSubRegionBindings(B, R, *SubRegions.get());
513 state = state->makeWithStore(B.getRoot());
514
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000515 if (!R->isBoundable())
516 return state;
517
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000518 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
519 isa<ObjCObjectRegion>(R)) {
520 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000521 // conjured symbol. The type of the symbol is irrelavant.
522 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000523 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000524 }
525
526 const TypedRegion *TR = cast<TypedRegion>(R);
527 QualType T = TR->getValueType(Ctx);
528
529 // FIXME: The code causes a crash when using RegionStore on the test case
530 // 'test_invalidate_cast_int' (misc-ps.m). Consider removing it
531 // permanently. Region casts are probably not too strict to handle
532 // the transient interpretation of memory. Instead we can use the QualType
533 // passed to 'Retrieve' and friends to determine the most current
534 // interpretation of memory when it is actually used.
535#if 0
536 // If the region is cast to another type, use that type.
537 if (const QualType *CastTy = getCastType(state, R)) {
538 assert(!(*CastTy)->isObjCObjectPointerType());
Ted Kremenek6217b802009-07-29 21:53:49 +0000539 QualType NewT = (*CastTy)->getAs<PointerType>()->getPointeeType();
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000540
541 // The only exception is if the original region had a location type as its
542 // value type we always want to treat the region as binding to a location.
543 // This issue can arise when pointers are casted to integers and back.
544
545 if (!(Loc::IsLocType(T) && !Loc::IsLocType(NewT)))
546 T = NewT;
547 }
548#endif
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000549
550 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000551 // FIXME: handle structs with default region value.
552 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
553
554 // No record definition. There is nothing we can do.
555 if (!RD)
556 return state;
557
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000558 // Invalidate the region by setting its default value to
559 // conjured symbol. The type of the symbol is irrelavant.
560 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
561 return setDefaultValue(state, R, V);
562 }
563
564 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000565 // Set the default value of the array to conjured symbol.
566 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
567 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000568 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000569 }
570
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000571 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
572 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
573 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000574}
575
576//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000577// getLValueXXX methods.
578//===----------------------------------------------------------------------===//
579
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000580/// getLValueString - Returns an SVal representing the lvalue of a
581/// StringLiteral. Within RegionStore a StringLiteral has an
582/// associated StringRegion, and the lvalue of a StringLiteral is the
583/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000584SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000585 const StringLiteral* S) {
586 return loc::MemRegionVal(MRMgr.getStringRegion(S));
587}
588
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000589/// getLValueVar - Returns an SVal that represents the lvalue of a
590/// variable. Within RegionStore a variable has an associated
591/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000592SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000593 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
594}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000595
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000596/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
597/// of a compound literal. Within RegionStore a compound literal
598/// has an associated region, and the lvalue of the compound literal
599/// is the lvalue of that region.
600SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000601RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000602 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000603 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
604}
605
Ted Kremenek67f28532009-06-17 22:02:04 +0000606SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000607 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000608 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000609}
610
Ted Kremenek67f28532009-06-17 22:02:04 +0000611SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000612 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000613 return getLValueFieldOrIvar(St, Base, D);
614}
615
Ted Kremenek67f28532009-06-17 22:02:04 +0000616SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000617 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000618 if (Base.isUnknownOrUndef())
619 return Base;
620
621 Loc BaseL = cast<Loc>(Base);
622 const MemRegion* BaseR = 0;
623
624 switch (BaseL.getSubKind()) {
625 case loc::MemRegionKind:
626 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
627 break;
628
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000629 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000630 // These are anormal cases. Flag an undefined value.
631 return UndefinedVal();
632
633 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000634 // While these seem funny, this can happen through casts.
635 // FIXME: What we should return is the field offset. For example,
636 // add the field offset to the integer value. That way funny things
637 // like this work properly: &(((struct foo *) 0xa)->f)
638 return Base;
639
640 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000641 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000642 return Base;
643 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000644
645 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
646 // of FieldDecl.
647 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
648 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000649
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000650 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000651}
652
Ted Kremenek67f28532009-06-17 22:02:04 +0000653SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000654 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000655 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000656
Ted Kremenekde7ec632009-03-09 22:44:49 +0000657 // If the base is an unknown or undefined value, just return it back.
658 // FIXME: For absolute pointer addresses, we just return that value back as
659 // well, although in reality we should return the offset added to that
660 // value.
661 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000662 return Base;
663
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000664 // Only handle integer offsets... for now.
665 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000666 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000667
Zhongxing Xuce760782009-05-09 13:20:07 +0000668 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000669
670 // Pointer of any type can be cast and used as array base.
671 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
672
Ted Kremenek46537392009-07-16 01:33:37 +0000673 // Convert the offset to the appropriate size and signedness.
674 Offset = ValMgr.convertToArrayIndex(Offset);
675
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000676 if (!ElemR) {
677 //
678 // If the base region is not an ElementRegion, create one.
679 // This can happen in the following example:
680 //
681 // char *p = __builtin_alloc(10);
682 // p[1] = 8;
683 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000684 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000685 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000686 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000687 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000688 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000689
690 SVal BaseIdx = ElemR->getIndex();
691
692 if (!isa<nonloc::ConcreteInt>(BaseIdx))
693 return UnknownVal();
694
695 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
696 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
697 assert(BaseIdxI.isSigned());
698
Ted Kremenek46537392009-07-16 01:33:37 +0000699 // Compute the new index.
700 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000701
Ted Kremenek46537392009-07-16 01:33:37 +0000702 // Construct the new ElementRegion.
703 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000704 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
705 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000706}
707
Ted Kremenek9af46f52009-06-16 22:36:44 +0000708//===----------------------------------------------------------------------===//
709// Extents for regions.
710//===----------------------------------------------------------------------===//
711
Ted Kremenek67f28532009-06-17 22:02:04 +0000712SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000713 const MemRegion *R) {
714
715 switch (R->getKind()) {
716 case MemRegion::MemSpaceRegionKind:
717 assert(0 && "Cannot index into a MemSpace");
718 return UnknownVal();
719
720 case MemRegion::CodeTextRegionKind:
721 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000722 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000723
724 // Not yet handled.
725 case MemRegion::AllocaRegionKind:
726 case MemRegion::CompoundLiteralRegionKind:
727 case MemRegion::ElementRegionKind:
728 case MemRegion::FieldRegionKind:
729 case MemRegion::ObjCIvarRegionKind:
730 case MemRegion::ObjCObjectRegionKind:
731 case MemRegion::SymbolicRegionKind:
732 return UnknownVal();
733
734 case MemRegion::StringRegionKind: {
735 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
736 // We intentionally made the size value signed because it participates in
737 // operations with signed indices.
738 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000739 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000740
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000741 case MemRegion::VarRegionKind: {
742 const VarRegion* VR = cast<VarRegion>(R);
743 // Get the type of the variable.
744 QualType T = VR->getDesugaredValueType(getContext());
745
746 // FIXME: Handle variable-length arrays.
747 if (isa<VariableArrayType>(T))
748 return UnknownVal();
749
750 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
751 // return the size as signed integer.
752 return ValMgr.makeIntVal(CAT->getSize(), false);
753 }
754
755 const QualType* CastTy = state->get<RegionCasts>(VR);
756
757 // If the VarRegion is cast to other type, compute the size with respect to
758 // that type.
759 if (CastTy) {
760 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
761 QualType VarTy = VR->getValueType(getContext());
762 uint64_t EleSize = getContext().getTypeSize(EleTy);
763 uint64_t VarSize = getContext().getTypeSize(VarTy);
764 assert(VarSize != 0);
765 return ValMgr.makeIntVal(VarSize/EleSize, false);
766 }
767
768 // Clients can use ordinary variables as if they were arrays. These
769 // essentially are arrays of size 1.
770 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000771 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000772
773 case MemRegion::BEG_DECL_REGIONS:
774 case MemRegion::END_DECL_REGIONS:
775 case MemRegion::BEG_TYPED_REGIONS:
776 case MemRegion::END_TYPED_REGIONS:
777 assert(0 && "Infeasible region");
778 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000779 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000780
781 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000782 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000783}
784
Ted Kremenek67f28532009-06-17 22:02:04 +0000785const GRState *RegionStoreManager::setExtent(const GRState *state,
786 const MemRegion *region,
787 SVal extent) {
788 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000789}
790
791//===----------------------------------------------------------------------===//
792// Location and region casting.
793//===----------------------------------------------------------------------===//
794
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000795/// ArrayToPointer - Emulates the "decay" of an array to a pointer
796/// type. 'Array' represents the lvalue of the array being decayed
797/// to a pointer, and the returned SVal represents the decayed
798/// version of that lvalue (i.e., a pointer to the first element of
799/// the array). This is called by GRExprEngine when evaluating casts
800/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000801SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000802 if (!isa<loc::MemRegionVal>(Array))
803 return UnknownVal();
804
805 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
806 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
807
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000808 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000809 return UnknownVal();
810
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000811 // Strip off typedefs from the ArrayRegion's ValueType.
812 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000813 ArrayType *AT = cast<ArrayType>(T);
814 T = AT->getElementType();
815
Ted Kremenek75185b52009-07-16 00:00:11 +0000816 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
817 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000818
819 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000820}
821
Ted Kremenek9af46f52009-06-16 22:36:44 +0000822//===----------------------------------------------------------------------===//
823// Pointer arithmetic.
824//===----------------------------------------------------------------------===//
825
Zhongxing Xu262fd032009-05-20 09:00:16 +0000826SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000827 BinaryOperator::Opcode Op, Loc L, NonLoc R,
828 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000829 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000830 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000831 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000832
Zhongxing Xua1718c72009-04-03 07:33:13 +0000833 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000834 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000835
Ted Kremenek3bccf082009-07-11 00:58:27 +0000836 switch (MR->getKind()) {
837 case MemRegion::SymbolicRegionKind: {
838 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
839 QualType T;
Ted Kremenek356e9d62009-07-22 04:35:42 +0000840#if USE_REGION_CASTS
Ted Kremenek3bccf082009-07-11 00:58:27 +0000841 // If the SymbolicRegion was cast to another type, use that type.
842 if (const QualType *t = state->get<RegionCasts>(SR))
843 T = *t;
Ted Kremenek356e9d62009-07-22 04:35:42 +0000844 else
845#endif
846 {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000847 // Otherwise use the symbol's type.
848 SymbolRef Sym = SR->getSymbol();
849 T = Sym->getType(getContext());
850 }
851
Ted Kremenek6217b802009-07-29 21:53:49 +0000852 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000853 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
854 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
855 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000856 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000857 case MemRegion::AllocaRegionKind: {
858 // Get the alloca region's current cast type.
859 const AllocaRegion *AR = cast<AllocaRegion>(MR);
860 QualType T = *(state->get<RegionCasts>(AR));
Ted Kremenek6217b802009-07-29 21:53:49 +0000861 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000862 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
863 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
864 break;
865 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000866
Ted Kremenek3bccf082009-07-11 00:58:27 +0000867 case MemRegion::ElementRegionKind: {
868 ER = cast<ElementRegion>(MR);
869 break;
870 }
871
872 // Not yet handled.
873 case MemRegion::VarRegionKind:
874 case MemRegion::StringRegionKind:
875 case MemRegion::CompoundLiteralRegionKind:
876 case MemRegion::FieldRegionKind:
877 case MemRegion::ObjCObjectRegionKind:
878 case MemRegion::ObjCIvarRegionKind:
879 return UnknownVal();
880
Ted Kremenek3bccf082009-07-11 00:58:27 +0000881 case MemRegion::CodeTextRegionKind:
882 // Technically this can happen if people do funny things with casts.
883 return UnknownVal();
884
885 case MemRegion::MemSpaceRegionKind:
886 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
887 return UnknownVal();
888
889 case MemRegion::BEG_DECL_REGIONS:
890 case MemRegion::END_DECL_REGIONS:
891 case MemRegion::BEG_TYPED_REGIONS:
892 case MemRegion::END_TYPED_REGIONS:
893 assert(0 && "Infeasible region");
894 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000895 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000896
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000897 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000898 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
899 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
900
901 // Only support concrete integer indexes for now.
902 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000903 // FIXME: Should use SValuator here.
904 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
905 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000906 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000907 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
908 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000909 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000910 }
911
912 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000913}
914
Ted Kremenek9af46f52009-06-16 22:36:44 +0000915//===----------------------------------------------------------------------===//
916// Loading values from regions.
917//===----------------------------------------------------------------------===//
918
Ted Kremeneka6275a52009-07-15 02:31:43 +0000919static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
920 RTy = Ctx.getCanonicalType(RTy);
921 UsedTy = Ctx.getCanonicalType(UsedTy);
922
923 if (RTy == UsedTy)
924 return false;
925
Ted Kremenek25c54572009-07-20 22:58:02 +0000926
927 // Recursively check the types. We basically want to see if a pointer value
928 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
929 // represents a reinterpretation.
930 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000931 const PointerType *PRTy = RTy->getAs<PointerType>();
932 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000933
934 return PUsedTy && PRTy &&
935 IsReinterpreted(PRTy->getPointeeType(),
936 PUsedTy->getPointeeType(), Ctx);
937 }
938
939 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000940}
941
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000942SValuator::CastResult
943RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000944
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000945 assert(!isa<UnknownVal>(L) && "location unknown");
946 assert(!isa<UndefinedVal>(L) && "location undefined");
947
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000948 // FIXME: Is this even possible? Shouldn't this be treated as a null
949 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000950 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000951 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000952
Ted Kremenek67f28532009-06-17 22:02:04 +0000953 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000954
Zhongxing Xu91844122009-05-20 09:18:48 +0000955 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000956 // Example:
957 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000958 // char* p = alloca();
959 // read(p);
960 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000961 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000962 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000963
964 if (isa<SymbolicRegion>(MR)) {
965 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000966 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000967 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000968 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
969 }
970
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000971 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
972 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000973 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000974 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000975
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000976 // FIXME: We should eventually handle funny addressing. e.g.:
977 //
978 // int x = ...;
979 // int *p = &x;
980 // char *q = (char*) p;
981 // char c = *q; // returns the first byte of 'x'.
982 //
983 // Such funny addressing will occur due to layering of regions.
984
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000985#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000986 ASTContext &Ctx = getContext();
987 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000988 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
989 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000990 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000991 assert(Ctx.getCanonicalType(RTy) ==
992 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000993 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000994#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000995
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000996 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000997 return SValuator::CastResult(state, RetrieveStruct(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000998
999 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001000 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001001
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001002 // FIXME: handle Vector types.
1003 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001004 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001005
1006 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001007 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001008
1009 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001010 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001011
Ted Kremenek25c54572009-07-20 22:58:02 +00001012 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001013 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001014
1015 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001016 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +00001017
Ted Kremenek67f28532009-06-17 22:02:04 +00001018 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001019 RegionBindingsTy::data_type* V = B.lookup(R);
1020
1021 // Check if the region has a binding.
1022 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001023 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001024
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001025 // The location does not have a bound value. This means that it has
1026 // the value it had upon its creation and/or entry to the analyzed
1027 // function/method. These are either symbolic values or 'undefined'.
1028
Ted Kremenek356e9d62009-07-22 04:35:42 +00001029#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001030 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001031#else
1032 if (R->hasStackStorage()) {
1033#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001034 // All stack variables are considered to have undefined values
1035 // upon creation. All heap allocated blocks are considered to
1036 // have undefined values as well unless they are explicitly bound
1037 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001038 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001039 }
1040
Ted Kremenek356e9d62009-07-22 04:35:42 +00001041#if USE_REGION_CASTS
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001042 // If the region is already cast to another type, use that type to create the
1043 // symbol value.
1044 if (const QualType *p = state->get<RegionCasts>(R)) {
1045 QualType T = *p;
Ted Kremenek6217b802009-07-29 21:53:49 +00001046 RTy = T->getAs<PointerType>()->getPointeeType();
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001047 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001048#endif
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001049
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001050 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001051 return SValuator::CastResult(state,
1052 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001053}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001054
1055
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001056
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001057SVal RegionStoreManager::RetrieveElement(const GRState* state,
1058 const ElementRegion* R) {
1059 // Check if the region has a binding.
1060 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001061 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001062 return *V;
1063
Ted Kremenek921109a2009-07-01 23:19:52 +00001064 const MemRegion* superR = R->getSuperRegion();
1065
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001066 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001067 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001068 const StringLiteral *Str = StrR->getStringLiteral();
1069 SVal Idx = R->getIndex();
1070 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1071 int64_t i = CI->getValue().getSExtValue();
1072 char c;
1073 if (i == Str->getByteLength())
1074 c = '\0';
1075 else
1076 c = Str->getStrData()[i];
1077 return ValMgr.makeIntVal(c, getContext().CharTy);
1078 }
1079 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001080
1081 // Special case: the current region represents a cast and it and the super
1082 // region both have pointer types or intptr_t types. If so, perform the
1083 // retrieve from the super region and appropriately "cast" the value.
1084 // This is needed to support OSAtomicCompareAndSwap and friends or other
1085 // loads that treat integers as pointers and vis versa.
1086 if (R->getIndex().isZeroConstant()) {
1087 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1088 ASTContext &Ctx = getContext();
1089
1090 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1091 QualType valTy = R->getValueType(Ctx);
1092 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1093 // Retrieve the value from the super region. This will be casted to
1094 // valTy when we return to 'Retrieve'.
1095 const SValuator::CastResult &cr = Retrieve(state,
1096 loc::MemRegionVal(superR),
1097 valTy);
1098 return cr.getSVal();
1099 }
1100 }
1101 }
1102 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001103
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001104 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +00001105 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001106 if (D->hasConjuredSymbol())
1107 return ValMgr.getRegionValueSymbolVal(R);
1108 else
1109 return *D;
1110 }
1111
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001112 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001113 if (const SVal *V = B.lookup(superR)) {
1114 if (SymbolRef parentSym = V->getAsSymbol())
1115 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001116
1117 if (V->isUnknownOrUndef())
1118 return *V;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001119
1120 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001121 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001122 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001123
Ted Kremenek356e9d62009-07-22 04:35:42 +00001124#if 0
Ted Kremenek921109a2009-07-01 23:19:52 +00001125 if (R->hasHeapStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001126 // FIXME: If the region has heap storage and we know nothing special
1127 // about its bindings, should we instead return UnknownVal? Seems like
1128 // we should only return UndefinedVal in the cases where we know the value
1129 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001130 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +00001131 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001132#endif
1133
Ted Kremenekdc147262009-07-02 22:02:15 +00001134 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +00001135 // Currently we don't reason specially about Clang-style vectors. Check
1136 // if superR is a vector and if so return Unknown.
1137 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1138 if (typedSuperR->getValueType(getContext())->isVectorType())
1139 return UnknownVal();
1140 }
1141
1142 return UndefinedVal();
1143 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001144
1145 QualType Ty = R->getValueType(getContext());
1146
Ted Kremenek356e9d62009-07-22 04:35:42 +00001147#if USE_REGION_CASTS
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001148 // If the region is already cast to another type, use that type to create the
1149 // symbol value.
1150 if (const QualType *p = state->get<RegionCasts>(R))
Ted Kremenek6217b802009-07-29 21:53:49 +00001151 Ty = (*p)->getAs<PointerType>()->getPointeeType();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001152#endif
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001153
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001154 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001155}
1156
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001157SVal RegionStoreManager::RetrieveField(const GRState* state,
1158 const FieldRegion* R) {
1159 QualType Ty = R->getValueType(getContext());
1160
1161 // Check if the region has a binding.
1162 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001163 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001164 return *V;
1165
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001166 const MemRegion* superR = R->getSuperRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001167 while (superR) {
1168 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
1169 if (SymbolRef parentSym = D->getAsSymbol())
1170 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001171
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001172 if (D->isZeroConstant())
1173 return ValMgr.makeZeroVal(Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001174
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001175 if (D->isUnknown())
1176 return *D;
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001177
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001178 assert(0 && "Unknown default value");
1179 }
1180
1181 // If our super region is a field or element itself, walk up the region
1182 // hierarchy to see if there is a default value installed in an ancestor.
1183 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1184 superR = cast<SubRegion>(superR)->getSuperRegion();
1185 continue;
1186 }
1187
1188 break;
1189 }
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001190
Ted Kremenek356e9d62009-07-22 04:35:42 +00001191#if HEAP_UNDEFINED
Ted Kremenekdc147262009-07-02 22:02:15 +00001192 // FIXME: Is this correct? Should it be UnknownVal?
1193 if (R->hasHeapStorage())
1194 return UndefinedVal();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001195#endif
Ted Kremenekdc147262009-07-02 22:02:15 +00001196
1197 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001198 return UndefinedVal();
1199
Ted Kremenek356e9d62009-07-22 04:35:42 +00001200#if USE_REGION_CASTS
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001201 // If the region is already cast to another type, use that type to create the
1202 // symbol value.
1203 if (const QualType *p = state->get<RegionCasts>(R)) {
1204 QualType tmp = *p;
Ted Kremenek6217b802009-07-29 21:53:49 +00001205 Ty = tmp->getAs<PointerType>()->getPointeeType();
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001206 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001207#endif
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001208
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001209 // All other values are symbolic.
1210 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001211}
1212
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001213SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1214 const ObjCIvarRegion* R) {
1215
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001216 // Check if the region has a binding.
1217 RegionBindingsTy B = GetRegionBindings(state->getStore());
1218
1219 if (const SVal* V = B.lookup(R))
1220 return *V;
1221
1222 const MemRegion *superR = R->getSuperRegion();
1223
1224 // Check if the super region has a binding.
1225 if (const SVal *V = B.lookup(superR)) {
1226 if (SymbolRef parentSym = V->getAsSymbol())
1227 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1228
1229 // Other cases: give up.
1230 return UnknownVal();
1231 }
1232
Ted Kremenek25c54572009-07-20 22:58:02 +00001233 return RetrieveLazySymbol(state, R);
1234}
1235
Ted Kremenek9031dd72009-07-21 00:12:07 +00001236SVal RegionStoreManager::RetrieveVar(const GRState *state,
1237 const VarRegion *R) {
1238
1239 // Check if the region has a binding.
1240 RegionBindingsTy B = GetRegionBindings(state->getStore());
1241
1242 if (const SVal* V = B.lookup(R))
1243 return *V;
1244
1245 // Lazily derive a value for the VarRegion.
1246 const VarDecl *VD = R->getDecl();
1247
1248 if (VD == SelfDecl)
1249 return loc::MemRegionVal(getSelfRegion(0));
1250
1251 if (R->hasGlobalsOrParametersStorage())
1252 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1253
1254 return UndefinedVal();
1255}
1256
Ted Kremenek25c54572009-07-20 22:58:02 +00001257SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1258 const TypedRegion *R) {
1259
1260 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001261
1262#if USE_REGION_CASTS
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001263 // If the region is already cast to another type, use that type to create the
1264 // symbol value.
Ted Kremenek25c54572009-07-20 22:58:02 +00001265 if (const QualType *ty = state->get<RegionCasts>(R)) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001266 if (const PointerType *PT = (*ty)->getAs<PointerType>()) {
Ted Kremenek25c54572009-07-20 22:58:02 +00001267 QualType castTy = PT->getPointeeType();
1268
1269 if (!IsReinterpreted(valTy, castTy, getContext()))
1270 valTy = castTy;
1271 }
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001272 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001273#endif
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001274
1275 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001276 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001277}
1278
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001279SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1280 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001281 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001282 assert(T->isStructureType());
1283
Zhongxing Xub7507d12009-06-11 07:27:30 +00001284 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001285 RecordDecl* RD = RT->getDecl();
1286 assert(RD->isDefinition());
1287
1288 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1289
Ted Kremenek67f28532009-06-17 22:02:04 +00001290 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1291 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001292 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001293
Douglas Gregore267ff32008-12-11 20:41:00 +00001294 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1295 FieldEnd = Fields.rend();
1296 Field != FieldEnd; ++Field) {
1297 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001298 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001299 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001300 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1301 }
1302
Zhongxing Xud91ee272009-06-23 09:02:15 +00001303 return ValMgr.makeCompoundVal(T, StructVal);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001304}
1305
Ted Kremenek67f28532009-06-17 22:02:04 +00001306SVal RegionStoreManager::RetrieveArray(const GRState *state,
1307 const TypedRegion * R) {
1308
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001309 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001310 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1311
1312 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001313 uint64_t size = CAT->getSize().getZExtValue();
1314 for (uint64_t i = 0; i < size; ++i) {
1315 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001316 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1317 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001318 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001319 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001320 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1321 }
1322
Zhongxing Xud91ee272009-06-23 09:02:15 +00001323 return ValMgr.makeCompoundVal(T, ArrayVal);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001324}
1325
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001326SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1327 const GRState *state,
1328 const TypedRegion *R,
1329 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001330 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001331 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001332
1333 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001334 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001335}
1336
Ted Kremenek9af46f52009-06-16 22:36:44 +00001337//===----------------------------------------------------------------------===//
1338// Binding values to regions.
1339//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001340
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001341Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001342 const MemRegion* R = 0;
1343
1344 if (isa<loc::MemRegionVal>(L))
1345 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001346
1347 if (R) {
1348 RegionBindingsTy B = GetRegionBindings(store);
1349 return RBFactory.Remove(B, R).getRoot();
1350 }
1351
1352 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001353}
1354
Ted Kremenek67f28532009-06-17 22:02:04 +00001355const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001356 if (isa<loc::ConcreteInt>(L))
1357 return state;
1358
Ted Kremenek9af46f52009-06-16 22:36:44 +00001359 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001360 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001361
1362 // Check if the region is a struct region.
1363 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1364 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001365 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001366
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001367 // Special case: the current region represents a cast and it and the super
1368 // region both have pointer types or intptr_t types. If so, perform the
1369 // bind to the super region.
1370 // This is needed to support OSAtomicCompareAndSwap and friends or other
1371 // loads that treat integers as pointers and vis versa.
1372 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1373 if (ER->getIndex().isZeroConstant()) {
1374 if (const TypedRegion *superR =
1375 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1376 ASTContext &Ctx = getContext();
1377 QualType superTy = superR->getValueType(Ctx);
1378 QualType erTy = ER->getValueType(Ctx);
1379
1380 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1381 IsAnyPointerOrIntptr(erTy, Ctx)) {
1382 SValuator::CastResult cr =
1383 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1384 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1385 }
1386 }
1387 }
1388 }
1389
1390 // Perform the binding.
Ted Kremenek67f28532009-06-17 22:02:04 +00001391 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001392 B = RBFactory.Add(B, R, V);
Ted Kremenek67f28532009-06-17 22:02:04 +00001393 return state->makeWithStore(B.getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001394}
1395
Ted Kremenek67f28532009-06-17 22:02:04 +00001396const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001397 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001398
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001399 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001400 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001401
Ted Kremenek0964a062009-01-21 06:57:53 +00001402 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001403 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001404 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001405 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001406
Zhongxing Xud91ee272009-06-23 09:02:15 +00001407 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001408}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001409
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001410// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001411const GRState *
1412RegionStoreManager::BindCompoundLiteral(const GRState *state,
1413 const CompoundLiteralExpr* CL,
1414 SVal V) {
1415
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001416 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001417 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001418}
1419
Ted Kremenek67f28532009-06-17 22:02:04 +00001420const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001421 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001422 SVal Init) {
1423
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001424 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001425 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001426 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001427
Ted Kremenek46537392009-07-16 01:33:37 +00001428 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001429
1430 // Check if the init expr is a StringLiteral.
1431 if (isa<loc::MemRegionVal>(Init)) {
1432 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1433 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1434 const char* str = S->getStrData();
1435 unsigned len = S->getByteLength();
1436 unsigned j = 0;
1437
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001438 // Copy bytes from the string literal into the target array. Trailing bytes
1439 // in the array that are not covered by the string literal are initialized
1440 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001441 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001442 if (j >= len)
1443 break;
1444
Ted Kremenek46537392009-07-16 01:33:37 +00001445 SVal Idx = ValMgr.makeArrayIndex(i);
1446 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1447 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001448
Zhongxing Xud91ee272009-06-23 09:02:15 +00001449 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001450 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001451 }
1452
Ted Kremenek67f28532009-06-17 22:02:04 +00001453 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001454 }
1455
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001456 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001457 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001458 uint64_t i = 0;
1459
1460 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001461 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001462 if (VI == VE)
1463 break;
1464
Ted Kremenek46537392009-07-16 01:33:37 +00001465 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001466 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001467
1468 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001469 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001470 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001471 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001472 }
1473
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001474 // If the init list is shorter than the array length, set the array default
1475 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001476 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001477 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001478 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001479 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001480 }
1481 }
1482
Ted Kremenek67f28532009-06-17 22:02:04 +00001483 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001484}
1485
Ted Kremenek67f28532009-06-17 22:02:04 +00001486const GRState *
1487RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1488 SVal V) {
1489
1490 if (!Features.supportsFields())
1491 return state;
1492
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001493 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001494 assert(T->isStructureType());
1495
Ted Kremenek6217b802009-07-29 21:53:49 +00001496 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001497 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001498
1499 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001500 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001501
Ted Kremenek67f28532009-06-17 22:02:04 +00001502 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1503 // Ignore them and kill the field values.
1504 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1505 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001506
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001507 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001508 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001509
1510 RecordDecl::field_iterator FI, FE;
1511
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001512 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001513
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001514 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001515 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001516
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001517 QualType FTy = (*FI)->getType();
1518 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1519
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001520 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001521 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001522 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001523 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001524 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001525 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001526 }
1527
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001528 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001529 if (FI != FE)
1530 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001531
Ted Kremenek67f28532009-06-17 22:02:04 +00001532 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001533}
1534
Ted Kremenek67f28532009-06-17 22:02:04 +00001535const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001536 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001537
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001538 // Set the default value of the struct region to "unknown".
1539 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001540
1541 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001542 Store store = state->getStore();
1543 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001544 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001545 const MemRegion* R = I.getKey();
1546 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1547 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001548 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001549 }
1550
Ted Kremenek67f28532009-06-17 22:02:04 +00001551 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001552}
1553
Ted Kremenek9af46f52009-06-16 22:36:44 +00001554//===----------------------------------------------------------------------===//
1555// Region views.
1556//===----------------------------------------------------------------------===//
1557
Ted Kremenek67f28532009-06-17 22:02:04 +00001558const GRState *RegionStoreManager::AddRegionView(const GRState *state,
1559 const MemRegion* View,
1560 const MemRegion* Base) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001561
1562 // First, retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001563 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001564 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001565
1566 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001567 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001568
1569 // Create a new state with the new region view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001570 return state->set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001571}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001572
Ted Kremenek67f28532009-06-17 22:02:04 +00001573const GRState *RegionStoreManager::RemoveRegionView(const GRState *state,
1574 const MemRegion* View,
1575 const MemRegion* Base) {
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001576 // Retrieve the region view of the base region.
Ted Kremenek67f28532009-06-17 22:02:04 +00001577 const RegionViews* d = state->get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001578
1579 // If the base region has no view, return.
1580 if (!d)
Ted Kremenek67f28532009-06-17 22:02:04 +00001581 return state;
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001582
1583 // Remove the view.
Ted Kremenek67f28532009-06-17 22:02:04 +00001584 return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001585}
Zhongxing Xu20794942009-05-06 08:33:50 +00001586
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001587const GRState *RegionStoreManager::setCastType(const GRState *state,
1588 const MemRegion* R, QualType T) {
Zhongxing Xu82037252009-07-14 01:12:46 +00001589 // We do not record generic cast type, since we are using cast type to
1590 // invlidate regions, and generic type is meaningless for invalidating
1591 // regions.
1592 // If the region already has a cast type before, that type is preserved.
1593 // FIXME: is this the right thing to do?
1594 if (isGenericPtr(getContext(), T))
1595 return state;
Ted Kremenek67f28532009-06-17 22:02:04 +00001596 return state->set<RegionCasts>(R, T);
Zhongxing Xu20794942009-05-06 08:33:50 +00001597}
Zhongxing Xu264e9372009-05-12 10:10:00 +00001598
Ted Kremenek67f28532009-06-17 22:02:04 +00001599const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1600 const MemRegion* R, SVal V) {
1601 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001602}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001603
1604//===----------------------------------------------------------------------===//
1605// State pruning.
1606//===----------------------------------------------------------------------===//
1607
1608static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1609 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1610 const MemRegion *R = XR->getRegion();
1611
1612 while (R) {
1613 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1614 SymReaper.markLive(SR->getSymbol());
1615 return;
1616 }
1617
1618 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1619 R = SR->getSuperRegion();
1620 continue;
1621 }
1622
1623 break;
1624 }
1625
1626 return;
1627 }
1628
1629 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1630 SymReaper.markLive(*SI);
1631}
1632
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001633void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1634 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001635 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001636{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001637 Store store = state.getStore();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001638 RegionBindingsTy B = GetRegionBindings(store);
1639
1640 // Lazily constructed backmap from MemRegions to SubRegions.
1641 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1642 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1643
Ted Kremenek9af46f52009-06-16 22:36:44 +00001644 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001645 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001646 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001647
1648 // Do a pass over the regions in the store. For VarRegions we check if
1649 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001650 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001651 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1652
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001653 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001654 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001655 const MemRegion *R = I.getKey();
1656 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001657 }
1658
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001659 // Scan the default bindings for "intermediate" roots.
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001660 RegionDefaultValue::MapTy DVM = state.get<RegionDefaultValue>();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001661 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1662 I != E; ++I) {
1663 const MemRegion *R = I.getKey();
1664 IntermediateRoots.push_back(R);
1665 }
1666
1667 // Process the "intermediate" roots to find if they are referenced by
1668 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001669 while (!IntermediateRoots.empty()) {
1670 const MemRegion* R = IntermediateRoots.back();
1671 IntermediateRoots.pop_back();
1672
1673 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001674 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001675 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001676 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001677 continue;
1678 }
1679
1680 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001681 if (SymReaper.isLive(SR->getSymbol()))
1682 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001683 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001684 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001685
1686 // Add the super region for R to the worklist if it is a subregion.
1687 if (const SubRegion* superR =
1688 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1689 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001690 }
1691
1692 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1693 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001694 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001695 while (!RegionRoots.empty()) {
1696 // Dequeue the next region on the worklist.
1697 const MemRegion* R = RegionRoots.back();
1698 RegionRoots.pop_back();
1699
1700 // Check if we have already processed this region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001701 if (Marked.count(R))
1702 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001703
1704 // Mark this region as processed. This is needed for termination in case
1705 // a region is referenced more than once.
1706 Marked.insert(R);
1707
1708 // Mark the symbol for any live SymbolicRegion as "live". This means we
1709 // should continue to track that symbol.
1710 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1711 SymReaper.markLive(SymR->getSymbol());
1712
1713 // Get the data binding for R (if any).
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001714 const SVal* Xptr = B.lookup(R);
1715 if (!Xptr) {
1716 // No direct binding? Get the default binding for R (if any).
1717 Xptr = DVM.lookup(R);
1718 }
1719
1720 // Direct or default binding?
Ted Kremenek9af46f52009-06-16 22:36:44 +00001721 if (Xptr) {
1722 SVal X = *Xptr;
1723 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1724
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001725 // If X is a region, then add it to the RegionRoots.
1726 if (const MemRegion *RX = X.getAsRegion()) {
1727 RegionRoots.push_back(RX);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001728 // Mark the super region of the RX as live.
1729 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1730 // 'y' => element region. 'x' is its super region.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001731 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1732 RegionRoots.push_back(SR->getSuperRegion());
1733 }
1734 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001735 }
1736
1737 // Get the subregions of R. These are RegionRoots as well since they
1738 // represent values that are also bound to R.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001739 RegionStoreSubRegionMap::iterator I, E;
1740 for (llvm::tie(I, E) = SubRegions->begin_end(R); I != E; ++I)
Ted Kremenek9af46f52009-06-16 22:36:44 +00001741 RegionRoots.push_back(*I);
1742 }
1743
1744 // We have now scanned the store, marking reachable regions and symbols
1745 // as live. We now remove all the regions that are dead from the store
1746 // as well as update DSymbols with the set symbols that are now dead.
1747 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1748 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001749 // If this region live? Is so, none of its symbols are dead.
1750 if (Marked.count(R))
1751 continue;
1752
1753 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001754 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001755
1756 // Mark all non-live symbols that this region references as dead.
1757 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1758 SymReaper.maybeDead(SymR->getSymbol());
1759
1760 SVal X = I.getData();
1761 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001762 for (; SI != SE; ++SI)
1763 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001764 }
1765
Ted Kremenek093569c2009-08-02 05:00:15 +00001766 // Remove dead 'default' bindings.
1767 RegionDefaultValue::MapTy NewDVM = DVM;
1768 RegionDefaultValue::MapTy::Factory &DVMFactory =
1769 state.get_context<RegionDefaultValue>();
1770
1771 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1772 I != E; ++I) {
1773 const MemRegion *R = I.getKey();
1774
1775 // If this region live? Is so, none of its symbols are dead.
1776 if (Marked.count(R))
1777 continue;
1778
1779 // Remove this dead region.
1780 NewDVM = DVMFactory.Remove(NewDVM, R);
1781
1782 // Mark all non-live symbols that this region references as dead.
1783 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1784 SymReaper.maybeDead(SymR->getSymbol());
1785
1786 SVal X = I.getData();
1787 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1788 for (; SI != SE; ++SI)
1789 SymReaper.maybeDead(*SI);
1790 }
1791
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001792 // Write the store back.
1793 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001794
1795 // Write the updated default bindings back.
1796 // FIXME: Right now this involves a fetching of a persistent state.
1797 // We can do better.
1798 if (DVM != NewDVM)
1799 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001800}
1801
1802//===----------------------------------------------------------------------===//
1803// Utility methods.
1804//===----------------------------------------------------------------------===//
1805
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001806void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001807 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001808 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001809 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001810
Ted Kremenek6f9b3a42009-07-13 23:53:06 +00001811 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001812 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001813}