blob: 92830f0231a43c0881bc570a1bc61f65fd307371 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000021#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000022
23#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000024#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000025#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000026#include "llvm/Support/Compiler.h"
27
28using namespace clang;
29
Ted Kremenek356e9d62009-07-22 04:35:42 +000030#define HEAP_UNDEFINED 0
Ted Kremeneka5e81f12009-08-06 01:20:57 +000031#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000032
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 "Extents"
64//===----------------------------------------------------------------------===//
65//
66// MemRegions represent chunks of memory with a size (their "extent"). This
67// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000068//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000069namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
70static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000071namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000072 template<> struct GRStateTrait<RegionExtents>
73 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
74 static void* GDMIndex() { return &RegionExtentsIndex; }
75 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000076}
77
Ted Kremenek50dc1b32008-12-24 01:05:03 +000078//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000079// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000080//===----------------------------------------------------------------------===//
81//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000082// This GDM entry tracks what regions have a default value if they have no bound
83// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000084//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000085namespace {
86class VISIBILITY_HIDDEN RegionDefaultValue {
87public:
88 typedef llvm::ImmutableMap<const MemRegion*, SVal> MapTy;
89};
90}
Ted Kremenek50dc1b32008-12-24 01:05:03 +000091static int RegionDefaultValueIndex = 0;
92namespace clang {
93 template<> struct GRStateTrait<RegionDefaultValue>
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000094 : public GRStatePartialTrait<RegionDefaultValue::MapTy> {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000095 static void* GDMIndex() { return &RegionDefaultValueIndex; }
96 };
97}
98
99//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000100// Utility functions.
101//===----------------------------------------------------------------------===//
102
103static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
104 if (ty->isAnyPointerType())
105 return true;
106
107 return ty->isIntegerType() && ty->isScalarType() &&
108 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
109}
110
111//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000112// Main RegionStore logic.
113//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000114
Zhongxing Xu17892752008-10-08 02:50:44 +0000115namespace {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000116
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000117class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
118 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
119 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
120 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000121 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000122public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000123 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000124 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000125
126 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000127 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000128 return true;
129 }
130
131 I->second = F.Add(I->second, SubRegion);
132 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000133 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000134
135 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000136
137 ~RegionStoreSubRegionMap() {}
138
Ted Kremenek5dc27462009-03-03 02:51:43 +0000139 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000140 Map::iterator I = M.find(Parent);
141
142 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000143 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000144
145 llvm::ImmutableSet<const MemRegion*> S = I->second;
146 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
147 SI != SE; ++SI) {
148 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000149 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000150 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000151
152 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000153 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000154
155 typedef SetTy::iterator iterator;
156
157 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
158 Map::iterator I = M.find(R);
159 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
160 return std::make_pair(S.begin(), S.end());
161 }
Ted Kremenek59e8f112009-03-03 01:35:36 +0000162};
163
Zhongxing Xu17892752008-10-08 02:50:44 +0000164class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000165 const RegionStoreFeatures Features;
Zhongxing Xu17892752008-10-08 02:50:44 +0000166 RegionBindingsTy::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000167
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000168 const MemRegion* SelfRegion;
169 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000170
171public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000172 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000173 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000174 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000175 RBFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000176 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000177 if (const ObjCMethodDecl* MD =
178 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
179 SelfDecl = MD->getSelfDecl();
180 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000181
182 virtual ~RegionStoreManager() {}
183
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000184 SubRegionMap *getSubRegionMap(const GRState *state);
185
186 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000187
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000188 /// getLValueString - Returns an SVal representing the lvalue of a
189 /// StringLiteral. Within RegionStore a StringLiteral has an
190 /// associated StringRegion, and the lvalue of a StringLiteral is
191 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000192 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000193
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000194 /// getLValueCompoundLiteral - Returns an SVal representing the
195 /// lvalue of a compound literal. Within RegionStore a compound
196 /// literal has an associated region, and the lvalue of the
197 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000198 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000199
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000200 /// getLValueVar - Returns an SVal that represents the lvalue of a
201 /// variable. Within RegionStore a variable has an associated
202 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000203 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000204
Ted Kremenek67f28532009-06-17 22:02:04 +0000205 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000206
Ted Kremenek67f28532009-06-17 22:02:04 +0000207 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000208
Ted Kremenek67f28532009-06-17 22:02:04 +0000209 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000210
Ted Kremenek67f28532009-06-17 22:02:04 +0000211 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000212 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000213
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000214
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000215 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
216 /// type. 'Array' represents the lvalue of the array being decayed
217 /// to a pointer, and the returned SVal represents the decayed
218 /// version of that lvalue (i.e., a pointer to the first element of
219 /// the array). This is called by GRExprEngine when evaluating
220 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000221 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000222
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000223 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000224 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000225
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000226 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000227
228 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
229 /// 'this' object (C++). When used when analyzing a normal function this
230 /// method returns NULL.
231 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000232 if (!SelfDecl)
233 return 0;
234
235 if (!SelfRegion) {
236 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
237 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
238 MRMgr.getHeapRegion());
239 }
240
241 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000242 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000243
244 //===-------------------------------------------------------------------===//
245 // Binding values to regions.
246 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000247
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000248 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
249 const Expr *E, unsigned Count);
250
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000251private:
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000252 void RemoveSubRegionBindings(RegionBindingsTy &B,
253 RegionDefaultValue::MapTy &DVM,
254 RegionDefaultValue::MapTy::Factory &DVMFactory,
255 const MemRegion *R,
256 RegionStoreSubRegionMap &M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000257
258public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000259 const GRState *Bind(const GRState *state, Loc LV, SVal V);
260
261 const GRState *BindCompoundLiteral(const GRState *state,
262 const CompoundLiteralExpr* CL, SVal V);
263
264 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
265
266 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
267 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000268 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000269
Ted Kremenek67f28532009-06-17 22:02:04 +0000270 /// BindStruct - Bind a compound value to a structure.
271 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
272
273 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
274
275 /// KillStruct - Set the entire struct to unknown.
276 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
277
278 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
279
280 Store Remove(Store store, Loc LV);
281
282 //===------------------------------------------------------------------===//
283 // Loading values from regions.
284 //===------------------------------------------------------------------===//
285
286 /// The high level logic for this method is this:
287 /// Retrieve (L)
288 /// if L has binding
289 /// return L's binding
290 /// else if L is in killset
291 /// return unknown
292 /// else
293 /// if L is on stack or heap
294 /// return undefined
295 /// else
296 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000297 SValuator::CastResult Retrieve(const GRState *state, Loc L,
298 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000299
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000300 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000301
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000302 SVal RetrieveField(const GRState *state, const FieldRegion *R);
303
304 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000305
Ted Kremenek9031dd72009-07-21 00:12:07 +0000306 SVal RetrieveVar(const GRState *state, const VarRegion *R);
307
Ted Kremenek25c54572009-07-20 22:58:02 +0000308 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
309
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000310 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
311 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000312
Ted Kremenek67f28532009-06-17 22:02:04 +0000313 /// Retrieve the values in a struct and return a CompoundVal, used when doing
314 /// struct copy:
315 /// struct s x, y;
316 /// x = y;
317 /// y's value is retrieved by this method.
318 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
319
320 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000321
322 std::pair<const GRState*, const MemRegion*>
323 GetLazyBinding(RegionBindingsTy B, const MemRegion *R);
324
325 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
326 const GRState *state,
327 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000328
329 //===------------------------------------------------------------------===//
330 // State pruning.
331 //===------------------------------------------------------------------===//
332
333 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
334 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000335 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000336 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
337
338 //===------------------------------------------------------------------===//
339 // Region "extents".
340 //===------------------------------------------------------------------===//
341
342 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
343 SVal getSizeInElements(const GRState *state, const MemRegion* R);
344
345 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000346 // Utility methods.
347 //===------------------------------------------------------------------===//
348
Zhongxing Xu17892752008-10-08 02:50:44 +0000349 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000350 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000351 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000352
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000353 void print(Store store, llvm::raw_ostream& Out, const char* nl,
354 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000355
356 void iterBindings(Store store, BindingsHandler& f) {
357 // FIXME: Implement.
358 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000359
Ted Kremenek67f28532009-06-17 22:02:04 +0000360 // FIXME: Remove.
361 BasicValueFactory& getBasicVals() {
362 return StateMgr.getBasicVals();
363 }
364
365 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000366 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000367};
368
369} // end anonymous namespace
370
Ted Kremenek9af46f52009-06-16 22:36:44 +0000371//===----------------------------------------------------------------------===//
372// RegionStore creation.
373//===----------------------------------------------------------------------===//
374
375StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
376 RegionStoreFeatures F = maximal_features_tag();
377 return new RegionStoreManager(StMgr, F);
378}
379
380StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
381 RegionStoreFeatures F = minimal_features_tag();
382 F.enableFields(true);
383 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000384}
385
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000386void
387RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
388 const SubRegion *R) {
389 const MemRegion *superR = R->getSuperRegion();
390 if (add(superR, R))
391 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
392 WL.push_back(sr);
393}
394
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000395RegionStoreSubRegionMap*
396RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000397 RegionBindingsTy B = GetRegionBindings(state->getStore());
398 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
399
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000400 llvm::SmallVector<const SubRegion*, 10> WL;
401
402 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000403 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
404 M->process(WL, R);
405
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000406 RegionDefaultValue::MapTy DVM = state->get<RegionDefaultValue>();
407 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000408 I != E; ++I)
409 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
410 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000411
412 // We also need to record in the subregion map "intermediate" regions that
413 // don't have direct bindings but are super regions of those that do.
414 while (!WL.empty()) {
415 const SubRegion *R = WL.back();
416 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000417 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000418 }
419
Ted Kremenek14453bf2009-03-03 19:02:42 +0000420 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000421}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000422
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000423SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
424 return getRegionStoreSubRegionMap(state);
425}
426
Ted Kremenek9af46f52009-06-16 22:36:44 +0000427//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000428// Binding invalidation.
429//===----------------------------------------------------------------------===//
430
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000431void
432RegionStoreManager::RemoveSubRegionBindings(RegionBindingsTy &B,
433 RegionDefaultValue::MapTy &DVM,
434 RegionDefaultValue::MapTy::Factory &DVMFactory,
435 const MemRegion *R,
436 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000437
438 RegionStoreSubRegionMap::iterator I, E;
439
440 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000441 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000442
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000443 B = RBFactory.Remove(B, R);
444 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000445}
446
447
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000448const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
449 const MemRegion *R,
450 const Expr *E,
451 unsigned Count) {
452 ASTContext& Ctx = StateMgr.getContext();
453
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000454 // Strip away casts.
455 R = R->getBaseRegion();
456
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000457 // Remove the bindings to subregions.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000458 {
459 // Get the mapping of regions -> subregions.
460 llvm::OwningPtr<RegionStoreSubRegionMap>
461 SubRegions(getRegionStoreSubRegionMap(state));
462
463 RegionBindingsTy B = GetRegionBindings(state->getStore());
464 RegionDefaultValue::MapTy DVM = state->get<RegionDefaultValue>();
465 RegionDefaultValue::MapTy::Factory &DVMFactory =
466 state->get_context<RegionDefaultValue>();
467
468 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
469 state = state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
470 }
471
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000472 if (!R->isBoundable())
473 return state;
474
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000475 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
476 isa<ObjCObjectRegion>(R)) {
477 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000478 // conjured symbol. The type of the symbol is irrelavant.
479 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000480 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000481 }
482
483 const TypedRegion *TR = cast<TypedRegion>(R);
484 QualType T = TR->getValueType(Ctx);
485
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000486 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000487 // FIXME: handle structs with default region value.
488 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
489
490 // No record definition. There is nothing we can do.
491 if (!RD)
492 return state;
493
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000494 // Invalidate the region by setting its default value to
495 // conjured symbol. The type of the symbol is irrelavant.
496 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
497 return setDefaultValue(state, R, V);
498 }
499
500 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000501 // Set the default value of the array to conjured symbol.
502 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
503 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000504 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000505 }
506
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000507 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
508 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
509 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000510}
511
512//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000513// getLValueXXX methods.
514//===----------------------------------------------------------------------===//
515
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000516/// getLValueString - Returns an SVal representing the lvalue of a
517/// StringLiteral. Within RegionStore a StringLiteral has an
518/// associated StringRegion, and the lvalue of a StringLiteral is the
519/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000520SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000521 const StringLiteral* S) {
522 return loc::MemRegionVal(MRMgr.getStringRegion(S));
523}
524
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000525/// getLValueVar - Returns an SVal that represents the lvalue of a
526/// variable. Within RegionStore a variable has an associated
527/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000528SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000529 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
530}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000531
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000532/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
533/// of a compound literal. Within RegionStore a compound literal
534/// has an associated region, and the lvalue of the compound literal
535/// is the lvalue of that region.
536SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000537RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000538 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000539 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
540}
541
Ted Kremenek67f28532009-06-17 22:02:04 +0000542SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000543 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000544 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000545}
546
Ted Kremenek67f28532009-06-17 22:02:04 +0000547SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000548 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000549 return getLValueFieldOrIvar(St, Base, D);
550}
551
Ted Kremenek67f28532009-06-17 22:02:04 +0000552SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000553 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000554 if (Base.isUnknownOrUndef())
555 return Base;
556
557 Loc BaseL = cast<Loc>(Base);
558 const MemRegion* BaseR = 0;
559
560 switch (BaseL.getSubKind()) {
561 case loc::MemRegionKind:
562 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
563 break;
564
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000565 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000566 // These are anormal cases. Flag an undefined value.
567 return UndefinedVal();
568
569 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000570 // While these seem funny, this can happen through casts.
571 // FIXME: What we should return is the field offset. For example,
572 // add the field offset to the integer value. That way funny things
573 // like this work properly: &(((struct foo *) 0xa)->f)
574 return Base;
575
576 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000577 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000578 return Base;
579 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000580
581 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
582 // of FieldDecl.
583 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
584 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000585
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000586 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000587}
588
Ted Kremenek67f28532009-06-17 22:02:04 +0000589SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000590 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000591 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000592
Ted Kremenekde7ec632009-03-09 22:44:49 +0000593 // If the base is an unknown or undefined value, just return it back.
594 // FIXME: For absolute pointer addresses, we just return that value back as
595 // well, although in reality we should return the offset added to that
596 // value.
597 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000598 return Base;
599
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000600 // Only handle integer offsets... for now.
601 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000602 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000603
Zhongxing Xuce760782009-05-09 13:20:07 +0000604 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000605
606 // Pointer of any type can be cast and used as array base.
607 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
608
Ted Kremenek46537392009-07-16 01:33:37 +0000609 // Convert the offset to the appropriate size and signedness.
610 Offset = ValMgr.convertToArrayIndex(Offset);
611
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000612 if (!ElemR) {
613 //
614 // If the base region is not an ElementRegion, create one.
615 // This can happen in the following example:
616 //
617 // char *p = __builtin_alloc(10);
618 // p[1] = 8;
619 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000620 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000621 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000622 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000623 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000624 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000625
626 SVal BaseIdx = ElemR->getIndex();
627
628 if (!isa<nonloc::ConcreteInt>(BaseIdx))
629 return UnknownVal();
630
631 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
632 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
633 assert(BaseIdxI.isSigned());
634
Ted Kremenek46537392009-07-16 01:33:37 +0000635 // Compute the new index.
636 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000637
Ted Kremenek46537392009-07-16 01:33:37 +0000638 // Construct the new ElementRegion.
639 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000640 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
641 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000642}
643
Ted Kremenek9af46f52009-06-16 22:36:44 +0000644//===----------------------------------------------------------------------===//
645// Extents for regions.
646//===----------------------------------------------------------------------===//
647
Ted Kremenek67f28532009-06-17 22:02:04 +0000648SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000649 const MemRegion *R) {
650
651 switch (R->getKind()) {
652 case MemRegion::MemSpaceRegionKind:
653 assert(0 && "Cannot index into a MemSpace");
654 return UnknownVal();
655
656 case MemRegion::CodeTextRegionKind:
657 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000658 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000659
660 // Not yet handled.
661 case MemRegion::AllocaRegionKind:
662 case MemRegion::CompoundLiteralRegionKind:
663 case MemRegion::ElementRegionKind:
664 case MemRegion::FieldRegionKind:
665 case MemRegion::ObjCIvarRegionKind:
666 case MemRegion::ObjCObjectRegionKind:
667 case MemRegion::SymbolicRegionKind:
668 return UnknownVal();
669
670 case MemRegion::StringRegionKind: {
671 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
672 // We intentionally made the size value signed because it participates in
673 // operations with signed indices.
674 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000675 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000676
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000677 case MemRegion::VarRegionKind: {
678 const VarRegion* VR = cast<VarRegion>(R);
679 // Get the type of the variable.
680 QualType T = VR->getDesugaredValueType(getContext());
681
682 // FIXME: Handle variable-length arrays.
683 if (isa<VariableArrayType>(T))
684 return UnknownVal();
685
686 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
687 // return the size as signed integer.
688 return ValMgr.makeIntVal(CAT->getSize(), false);
689 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000690
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000691 // Clients can use ordinary variables as if they were arrays. These
692 // essentially are arrays of size 1.
693 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000694 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000695
696 case MemRegion::BEG_DECL_REGIONS:
697 case MemRegion::END_DECL_REGIONS:
698 case MemRegion::BEG_TYPED_REGIONS:
699 case MemRegion::END_TYPED_REGIONS:
700 assert(0 && "Infeasible region");
701 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000702 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000703
704 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000705 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000706}
707
Ted Kremenek67f28532009-06-17 22:02:04 +0000708const GRState *RegionStoreManager::setExtent(const GRState *state,
709 const MemRegion *region,
710 SVal extent) {
711 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000712}
713
714//===----------------------------------------------------------------------===//
715// Location and region casting.
716//===----------------------------------------------------------------------===//
717
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000718/// ArrayToPointer - Emulates the "decay" of an array to a pointer
719/// type. 'Array' represents the lvalue of the array being decayed
720/// to a pointer, and the returned SVal represents the decayed
721/// version of that lvalue (i.e., a pointer to the first element of
722/// the array). This is called by GRExprEngine when evaluating casts
723/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000724SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000725 if (!isa<loc::MemRegionVal>(Array))
726 return UnknownVal();
727
728 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
729 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
730
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000731 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000732 return UnknownVal();
733
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000734 // Strip off typedefs from the ArrayRegion's ValueType.
735 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000736 ArrayType *AT = cast<ArrayType>(T);
737 T = AT->getElementType();
738
Ted Kremenek75185b52009-07-16 00:00:11 +0000739 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
740 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000741
742 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000743}
744
Ted Kremenek9af46f52009-06-16 22:36:44 +0000745//===----------------------------------------------------------------------===//
746// Pointer arithmetic.
747//===----------------------------------------------------------------------===//
748
Zhongxing Xu262fd032009-05-20 09:00:16 +0000749SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000750 BinaryOperator::Opcode Op, Loc L, NonLoc R,
751 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000752 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000753 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000754 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000755
Zhongxing Xua1718c72009-04-03 07:33:13 +0000756 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000757 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000758
Ted Kremenek3bccf082009-07-11 00:58:27 +0000759 switch (MR->getKind()) {
760 case MemRegion::SymbolicRegionKind: {
761 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000762 SymbolRef Sym = SR->getSymbol();
763 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000764 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000765 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
766 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
767 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000768 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000769 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000770 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000771 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000772 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000773 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
774 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
775 break;
776 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000777
Ted Kremenek3bccf082009-07-11 00:58:27 +0000778 case MemRegion::ElementRegionKind: {
779 ER = cast<ElementRegion>(MR);
780 break;
781 }
782
783 // Not yet handled.
784 case MemRegion::VarRegionKind:
785 case MemRegion::StringRegionKind:
786 case MemRegion::CompoundLiteralRegionKind:
787 case MemRegion::FieldRegionKind:
788 case MemRegion::ObjCObjectRegionKind:
789 case MemRegion::ObjCIvarRegionKind:
790 return UnknownVal();
791
Ted Kremenek3bccf082009-07-11 00:58:27 +0000792 case MemRegion::CodeTextRegionKind:
793 // Technically this can happen if people do funny things with casts.
794 return UnknownVal();
795
796 case MemRegion::MemSpaceRegionKind:
797 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
798 return UnknownVal();
799
800 case MemRegion::BEG_DECL_REGIONS:
801 case MemRegion::END_DECL_REGIONS:
802 case MemRegion::BEG_TYPED_REGIONS:
803 case MemRegion::END_TYPED_REGIONS:
804 assert(0 && "Infeasible region");
805 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000806 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000807
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000808 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000809 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
810 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
811
812 // Only support concrete integer indexes for now.
813 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000814 // FIXME: Should use SValuator here.
815 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
816 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000817 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000818 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
819 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000820 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000821 }
822
823 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000824}
825
Ted Kremenek9af46f52009-06-16 22:36:44 +0000826//===----------------------------------------------------------------------===//
827// Loading values from regions.
828//===----------------------------------------------------------------------===//
829
Ted Kremeneka6275a52009-07-15 02:31:43 +0000830static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
831 RTy = Ctx.getCanonicalType(RTy);
832 UsedTy = Ctx.getCanonicalType(UsedTy);
833
834 if (RTy == UsedTy)
835 return false;
836
Ted Kremenek25c54572009-07-20 22:58:02 +0000837
838 // Recursively check the types. We basically want to see if a pointer value
839 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
840 // represents a reinterpretation.
841 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000842 const PointerType *PRTy = RTy->getAs<PointerType>();
843 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000844
845 return PUsedTy && PRTy &&
846 IsReinterpreted(PRTy->getPointeeType(),
847 PUsedTy->getPointeeType(), Ctx);
848 }
849
850 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000851}
852
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000853SValuator::CastResult
854RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000855
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000856 assert(!isa<UnknownVal>(L) && "location unknown");
857 assert(!isa<UndefinedVal>(L) && "location undefined");
858
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000859 // FIXME: Is this even possible? Shouldn't this be treated as a null
860 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000861 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000862 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000863
Ted Kremenek67f28532009-06-17 22:02:04 +0000864 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000865
Zhongxing Xu91844122009-05-20 09:18:48 +0000866 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000867 // Example:
868 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000869 // char* p = alloca();
870 // read(p);
871 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000872 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000873 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000874
875 if (isa<SymbolicRegion>(MR)) {
876 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000877 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000878 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000879 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
880 }
881
Ted Kremenek968f0a62009-08-03 21:41:46 +0000882 if (isa<CodeTextRegion>(MR))
883 return SValuator::CastResult(state, UnknownVal());
884
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000885 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
886 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000887 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000888 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000889
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000890 // FIXME: We should eventually handle funny addressing. e.g.:
891 //
892 // int x = ...;
893 // int *p = &x;
894 // char *q = (char*) p;
895 // char c = *q; // returns the first byte of 'x'.
896 //
897 // Such funny addressing will occur due to layering of regions.
898
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000899#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000900 ASTContext &Ctx = getContext();
901 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000902 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
903 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000904 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000905 assert(Ctx.getCanonicalType(RTy) ==
906 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000907 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000908#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000909
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000910 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000911 return SValuator::CastResult(state, RetrieveStruct(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000912
913 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000914 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000915
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000916 // FIXME: handle Vector types.
917 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000918 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000919
920 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000921 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000922
923 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000924 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000925
Ted Kremenek25c54572009-07-20 22:58:02 +0000926 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000927 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000928
929 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000930 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000931
Ted Kremenek67f28532009-06-17 22:02:04 +0000932 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000933 RegionBindingsTy::data_type* V = B.lookup(R);
934
935 // Check if the region has a binding.
936 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000937 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000938
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000939 // The location does not have a bound value. This means that it has
940 // the value it had upon its creation and/or entry to the analyzed
941 // function/method. These are either symbolic values or 'undefined'.
942
Ted Kremenek356e9d62009-07-22 04:35:42 +0000943#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000944 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000945#else
946 if (R->hasStackStorage()) {
947#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000948 // All stack variables are considered to have undefined values
949 // upon creation. All heap allocated blocks are considered to
950 // have undefined values as well unless they are explicitly bound
951 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000952 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000953 }
954
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000955 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000956 return SValuator::CastResult(state,
957 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000958}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000959
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000960std::pair<const GRState*, const MemRegion*>
961RegionStoreManager::GetLazyBinding(RegionBindingsTy B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000962
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000963 if (const nonloc::LazyCompoundVal *V =
964 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
965 return std::make_pair(V->getState(), V->getRegion());
966
967 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
968 const std::pair<const GRState *, const MemRegion *> &X =
969 GetLazyBinding(B, ER->getSuperRegion());
970
971 if (X.first)
972 return std::make_pair(X.first,
973 MRMgr.getElementRegionWithSuper(ER, X.second));
974 }
975 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
976 const std::pair<const GRState *, const MemRegion *> &X =
977 GetLazyBinding(B, FR->getSuperRegion());
978
979 if (X.first)
980 return std::make_pair(X.first,
981 MRMgr.getFieldRegionWithSuper(FR, X.second));
982 }
983
984 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
985}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000986
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000987SVal RegionStoreManager::RetrieveElement(const GRState* state,
988 const ElementRegion* R) {
989 // Check if the region has a binding.
990 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +0000991 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000992 return *V;
993
Ted Kremenek921109a2009-07-01 23:19:52 +0000994 const MemRegion* superR = R->getSuperRegion();
995
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000996 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +0000997 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000998 const StringLiteral *Str = StrR->getStringLiteral();
999 SVal Idx = R->getIndex();
1000 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1001 int64_t i = CI->getValue().getSExtValue();
1002 char c;
1003 if (i == Str->getByteLength())
1004 c = '\0';
1005 else
1006 c = Str->getStrData()[i];
1007 return ValMgr.makeIntVal(c, getContext().CharTy);
1008 }
1009 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001010
1011 // Special case: the current region represents a cast and it and the super
1012 // region both have pointer types or intptr_t types. If so, perform the
1013 // retrieve from the super region and appropriately "cast" the value.
1014 // This is needed to support OSAtomicCompareAndSwap and friends or other
1015 // loads that treat integers as pointers and vis versa.
1016 if (R->getIndex().isZeroConstant()) {
1017 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1018 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001019 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1020 QualType valTy = R->getValueType(Ctx);
1021 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1022 // Retrieve the value from the super region. This will be casted to
1023 // valTy when we return to 'Retrieve'.
1024 const SValuator::CastResult &cr = Retrieve(state,
1025 loc::MemRegionVal(superR),
1026 valTy);
1027 return cr.getSVal();
1028 }
1029 }
1030 }
1031 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001032
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001033 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +00001034 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001035 if (D->hasConjuredSymbol())
1036 return ValMgr.getRegionValueSymbolVal(R);
1037 else
1038 return *D;
1039 }
1040
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001041 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001042 if (const SVal *V = B.lookup(superR)) {
1043 if (SymbolRef parentSym = V->getAsSymbol())
1044 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001045
1046 if (V->isUnknownOrUndef())
1047 return *V;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001048
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001049 // Handle LazyCompoundVals below.
1050 if (const nonloc::LazyCompoundVal *LVC =
1051 dyn_cast<nonloc::LazyCompoundVal>(V)) {
1052 return RetrieveElement(LVC->getState(),
1053 MRMgr.getElementRegionWithSuper(R,
1054 LVC->getRegion()));
1055 }
1056
Ted Kremeneka6275a52009-07-15 02:31:43 +00001057 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001058 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001059 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001060
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001061 // Lazy binding?
1062 const GRState *lazyBindingState = NULL;
1063 const MemRegion *LazyBindingRegion = NULL;
1064 llvm::tie(lazyBindingState, LazyBindingRegion) = GetLazyBinding(B, R);
1065
1066 if (lazyBindingState) {
1067 assert(LazyBindingRegion && "Lazy-binding region not set");
1068 return RetrieveElement(lazyBindingState,
1069 cast<ElementRegion>(LazyBindingRegion));
1070 }
1071
1072 // Default value cases.
Ted Kremenek356e9d62009-07-22 04:35:42 +00001073#if 0
Ted Kremenek921109a2009-07-01 23:19:52 +00001074 if (R->hasHeapStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001075 // FIXME: If the region has heap storage and we know nothing special
1076 // about its bindings, should we instead return UnknownVal? Seems like
1077 // we should only return UndefinedVal in the cases where we know the value
1078 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001079 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +00001080 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001081#endif
1082
Ted Kremenekdc147262009-07-02 22:02:15 +00001083 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +00001084 // Currently we don't reason specially about Clang-style vectors. Check
1085 // if superR is a vector and if so return Unknown.
1086 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1087 if (typedSuperR->getValueType(getContext())->isVectorType())
1088 return UnknownVal();
1089 }
1090
1091 return UndefinedVal();
1092 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001093
1094 QualType Ty = R->getValueType(getContext());
1095
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001096 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001097}
1098
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001099SVal RegionStoreManager::RetrieveField(const GRState* state,
1100 const FieldRegion* R) {
1101 QualType Ty = R->getValueType(getContext());
1102
1103 // Check if the region has a binding.
1104 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001105 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001106 return *V;
1107
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001108 const MemRegion* superR = R->getSuperRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001109 while (superR) {
1110 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
1111 if (SymbolRef parentSym = D->getAsSymbol())
1112 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001113
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001114 if (D->isZeroConstant())
1115 return ValMgr.makeZeroVal(Ty);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001116
1117 if (const nonloc::LazyCompoundVal *LCV =
1118 dyn_cast<nonloc::LazyCompoundVal>(D)) {
1119 const FieldRegion *FR =
1120 MRMgr.getFieldRegionWithSuper(R, LCV->getRegion());
1121 return RetrieveField(LCV->getState(), FR);
1122 }
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001123
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001124 if (D->isUnknown())
1125 return *D;
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001126
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001127 assert(0 && "Unknown default value");
1128 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001129
1130 if (const SVal *V = B.lookup(superR)) {
1131 // Handle LazyCompoundVals below.
1132 if (isa<nonloc::CompoundVal>(*V))
1133 break;
1134 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001135
1136 // If our super region is a field or element itself, walk up the region
1137 // hierarchy to see if there is a default value installed in an ancestor.
1138 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1139 superR = cast<SubRegion>(superR)->getSuperRegion();
1140 continue;
1141 }
1142
1143 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001144 }
1145
1146 // Lazy binding?
1147 const GRState *lazyBindingState = NULL;
1148 const MemRegion *LazyBindingRegion = NULL;
1149 llvm::tie(lazyBindingState, LazyBindingRegion) = GetLazyBinding(B, R);
1150
1151 if (lazyBindingState) {
1152 assert(LazyBindingRegion && "Lazy-binding region not set");
1153 return RetrieveField(lazyBindingState,
1154 cast<FieldRegion>(LazyBindingRegion));
1155 }
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001156
Ted Kremenek356e9d62009-07-22 04:35:42 +00001157#if HEAP_UNDEFINED
Ted Kremenekdc147262009-07-02 22:02:15 +00001158 // FIXME: Is this correct? Should it be UnknownVal?
1159 if (R->hasHeapStorage())
1160 return UndefinedVal();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001161#endif
Ted Kremenekdc147262009-07-02 22:02:15 +00001162
1163 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001164 return UndefinedVal();
1165
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001166 // All other values are symbolic.
1167 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001168}
1169
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001170SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1171 const ObjCIvarRegion* R) {
1172
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001173 // Check if the region has a binding.
1174 RegionBindingsTy B = GetRegionBindings(state->getStore());
1175
1176 if (const SVal* V = B.lookup(R))
1177 return *V;
1178
1179 const MemRegion *superR = R->getSuperRegion();
1180
1181 // Check if the super region has a binding.
1182 if (const SVal *V = B.lookup(superR)) {
1183 if (SymbolRef parentSym = V->getAsSymbol())
1184 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1185
1186 // Other cases: give up.
1187 return UnknownVal();
1188 }
1189
Ted Kremenek25c54572009-07-20 22:58:02 +00001190 return RetrieveLazySymbol(state, R);
1191}
1192
Ted Kremenek9031dd72009-07-21 00:12:07 +00001193SVal RegionStoreManager::RetrieveVar(const GRState *state,
1194 const VarRegion *R) {
1195
1196 // Check if the region has a binding.
1197 RegionBindingsTy B = GetRegionBindings(state->getStore());
1198
1199 if (const SVal* V = B.lookup(R))
1200 return *V;
1201
1202 // Lazily derive a value for the VarRegion.
1203 const VarDecl *VD = R->getDecl();
1204
1205 if (VD == SelfDecl)
1206 return loc::MemRegionVal(getSelfRegion(0));
1207
1208 if (R->hasGlobalsOrParametersStorage())
1209 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1210
1211 return UndefinedVal();
1212}
1213
Ted Kremenek25c54572009-07-20 22:58:02 +00001214SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1215 const TypedRegion *R) {
1216
1217 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001218
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001219 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001220 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001221}
1222
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001223SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1224 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001225 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001226 assert(T->isStructureType());
1227
Zhongxing Xub7507d12009-06-11 07:27:30 +00001228 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001229 RecordDecl* RD = RT->getDecl();
1230 assert(RD->isDefinition());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001231#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001232 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1233
Ted Kremenek67f28532009-06-17 22:02:04 +00001234 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1235 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001236 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001237
Douglas Gregore267ff32008-12-11 20:41:00 +00001238 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1239 FieldEnd = Fields.rend();
1240 Field != FieldEnd; ++Field) {
1241 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001242 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001243 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001244 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1245 }
1246
Zhongxing Xud91ee272009-06-23 09:02:15 +00001247 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001248#else
1249 return ValMgr.makeLazyCompoundVal(state, R);
1250#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001251}
1252
Ted Kremenek67f28532009-06-17 22:02:04 +00001253SVal RegionStoreManager::RetrieveArray(const GRState *state,
1254 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001255#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001256 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001257 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1258
1259 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001260 uint64_t size = CAT->getSize().getZExtValue();
1261 for (uint64_t i = 0; i < size; ++i) {
1262 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001263 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1264 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001265 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001266 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001267 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1268 }
1269
Zhongxing Xud91ee272009-06-23 09:02:15 +00001270 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001271#else
1272 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1273 return ValMgr.makeLazyCompoundVal(state, R);
1274#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001275}
1276
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001277SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1278 const GRState *state,
1279 const TypedRegion *R,
1280 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001281 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001282 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001283
1284 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001285 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001286}
1287
Ted Kremenek9af46f52009-06-16 22:36:44 +00001288//===----------------------------------------------------------------------===//
1289// Binding values to regions.
1290//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001291
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001292Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001293 const MemRegion* R = 0;
1294
1295 if (isa<loc::MemRegionVal>(L))
1296 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001297
1298 if (R) {
1299 RegionBindingsTy B = GetRegionBindings(store);
1300 return RBFactory.Remove(B, R).getRoot();
1301 }
1302
1303 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001304}
1305
Ted Kremenek67f28532009-06-17 22:02:04 +00001306const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001307 if (isa<loc::ConcreteInt>(L))
1308 return state;
1309
Ted Kremenek9af46f52009-06-16 22:36:44 +00001310 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001311 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001312
1313 // Check if the region is a struct region.
1314 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1315 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001316 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001317
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001318 // Special case: the current region represents a cast and it and the super
1319 // region both have pointer types or intptr_t types. If so, perform the
1320 // bind to the super region.
1321 // This is needed to support OSAtomicCompareAndSwap and friends or other
1322 // loads that treat integers as pointers and vis versa.
1323 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1324 if (ER->getIndex().isZeroConstant()) {
1325 if (const TypedRegion *superR =
1326 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1327 ASTContext &Ctx = getContext();
1328 QualType superTy = superR->getValueType(Ctx);
1329 QualType erTy = ER->getValueType(Ctx);
1330
1331 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1332 IsAnyPointerOrIntptr(erTy, Ctx)) {
1333 SValuator::CastResult cr =
1334 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1335 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1336 }
1337 }
1338 }
1339 }
1340
1341 // Perform the binding.
Ted Kremenek67f28532009-06-17 22:02:04 +00001342 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001343 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001344}
1345
Ted Kremenek67f28532009-06-17 22:02:04 +00001346const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001347 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001348
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001349 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001350 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001351
Ted Kremenek0964a062009-01-21 06:57:53 +00001352 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001353 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001354 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001355 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001356
Zhongxing Xud91ee272009-06-23 09:02:15 +00001357 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001358}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001359
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001360// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001361const GRState *
1362RegionStoreManager::BindCompoundLiteral(const GRState *state,
1363 const CompoundLiteralExpr* CL,
1364 SVal V) {
1365
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001366 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001367 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001368}
1369
Ted Kremenek67f28532009-06-17 22:02:04 +00001370const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001371 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001372 SVal Init) {
1373
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001374 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001375 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001376 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001377
Ted Kremenek46537392009-07-16 01:33:37 +00001378 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001379
1380 // Check if the init expr is a StringLiteral.
1381 if (isa<loc::MemRegionVal>(Init)) {
1382 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1383 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1384 const char* str = S->getStrData();
1385 unsigned len = S->getByteLength();
1386 unsigned j = 0;
1387
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001388 // Copy bytes from the string literal into the target array. Trailing bytes
1389 // in the array that are not covered by the string literal are initialized
1390 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001391 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001392 if (j >= len)
1393 break;
1394
Ted Kremenek46537392009-07-16 01:33:37 +00001395 SVal Idx = ValMgr.makeArrayIndex(i);
1396 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1397 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001398
Zhongxing Xud91ee272009-06-23 09:02:15 +00001399 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001400 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001401 }
1402
Ted Kremenek67f28532009-06-17 22:02:04 +00001403 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001404 }
1405
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001406 // Handle lazy compound values.
1407 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1408 return CopyLazyBindings(*LCV, state, R);
1409
1410 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001411 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001412 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001413 uint64_t i = 0;
1414
1415 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001416 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001417 if (VI == VE)
1418 break;
1419
Ted Kremenek46537392009-07-16 01:33:37 +00001420 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001421 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001422
1423 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001424 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001425 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001426 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001427 }
1428
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001429 // If the init list is shorter than the array length, set the array default
1430 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001431 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001432 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001433 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001434 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001435 }
1436 }
1437
Ted Kremenek67f28532009-06-17 22:02:04 +00001438 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001439}
1440
Ted Kremenek67f28532009-06-17 22:02:04 +00001441const GRState *
1442RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1443 SVal V) {
1444
1445 if (!Features.supportsFields())
1446 return state;
1447
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001448 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001449 assert(T->isStructureType());
1450
Ted Kremenek6217b802009-07-29 21:53:49 +00001451 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001452 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001453
1454 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001455 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001456
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001457 // Handle lazy compound values.
1458 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1459 return CopyLazyBindings(*LCV, state, R);
1460
Ted Kremenek67f28532009-06-17 22:02:04 +00001461 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1462 // Ignore them and kill the field values.
1463 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1464 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001465
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001466 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001467 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001468
1469 RecordDecl::field_iterator FI, FE;
1470
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001471 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001472
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001473 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001474 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001475
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001476 QualType FTy = (*FI)->getType();
1477 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1478
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001479 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001480 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001481 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001482 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001483 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001484 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001485 }
1486
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001487 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001488 if (FI != FE)
1489 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001490
Ted Kremenek67f28532009-06-17 22:02:04 +00001491 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001492}
1493
Ted Kremenek67f28532009-06-17 22:02:04 +00001494const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001495 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001496
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001497 // Set the default value of the struct region to "unknown".
1498 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001499
1500 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001501 Store store = state->getStore();
1502 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001503 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001504 const MemRegion* R = I.getKey();
1505 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1506 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001507 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001508 }
1509
Ted Kremenek67f28532009-06-17 22:02:04 +00001510 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001511}
1512
Ted Kremenek67f28532009-06-17 22:02:04 +00001513const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1514 const MemRegion* R, SVal V) {
1515 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001516}
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001517
1518const GRState*
1519RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1520 const GRState *state,
1521 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001522
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001523 // Nuke the old bindings stemming from R.
1524 RegionBindingsTy B = GetRegionBindings(state->getStore());
1525 RegionDefaultValue::MapTy DVM = state->get<RegionDefaultValue>();
1526 RegionDefaultValue::MapTy::Factory &DVMFactory =
1527 state->get_context<RegionDefaultValue>();
1528
1529 llvm::OwningPtr<RegionStoreSubRegionMap>
1530 SubRegions(getRegionStoreSubRegionMap(state));
1531
1532 // B and DVM are updated after the call to RemoveSubRegionBindings.
1533 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
1534
1535 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1536 // results in a zero-copy algorithm.
1537 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1538}
1539
Ted Kremenek9af46f52009-06-16 22:36:44 +00001540//===----------------------------------------------------------------------===//
1541// State pruning.
1542//===----------------------------------------------------------------------===//
1543
1544static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1545 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1546 const MemRegion *R = XR->getRegion();
1547
1548 while (R) {
1549 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1550 SymReaper.markLive(SR->getSymbol());
1551 return;
1552 }
1553
1554 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1555 R = SR->getSuperRegion();
1556 continue;
1557 }
1558
1559 break;
1560 }
1561
1562 return;
1563 }
1564
1565 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1566 SymReaper.markLive(*SI);
1567}
1568
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001569void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1570 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001571 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001572{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001573 Store store = state.getStore();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001574 RegionBindingsTy B = GetRegionBindings(store);
1575
1576 // Lazily constructed backmap from MemRegions to SubRegions.
1577 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1578 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1579
Ted Kremenek9af46f52009-06-16 22:36:44 +00001580 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001581 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001582 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001583
1584 // Do a pass over the regions in the store. For VarRegions we check if
1585 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001586 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001587 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1588
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001589 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001590 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001591 const MemRegion *R = I.getKey();
1592 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001593 }
1594
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001595 // Scan the default bindings for "intermediate" roots.
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001596 RegionDefaultValue::MapTy DVM = state.get<RegionDefaultValue>();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001597 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1598 I != E; ++I) {
1599 const MemRegion *R = I.getKey();
1600 IntermediateRoots.push_back(R);
1601 }
1602
1603 // Process the "intermediate" roots to find if they are referenced by
1604 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001605 while (!IntermediateRoots.empty()) {
1606 const MemRegion* R = IntermediateRoots.back();
1607 IntermediateRoots.pop_back();
1608
1609 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001610 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001611 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001612 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001613 continue;
1614 }
1615
1616 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001617 if (SymReaper.isLive(SR->getSymbol()))
1618 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001619 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001620 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001621
1622 // Add the super region for R to the worklist if it is a subregion.
1623 if (const SubRegion* superR =
1624 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1625 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001626 }
1627
1628 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1629 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001630 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001631 while (!RegionRoots.empty()) {
1632 // Dequeue the next region on the worklist.
1633 const MemRegion* R = RegionRoots.back();
1634 RegionRoots.pop_back();
1635
1636 // Check if we have already processed this region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001637 if (Marked.count(R))
1638 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001639
1640 // Mark this region as processed. This is needed for termination in case
1641 // a region is referenced more than once.
1642 Marked.insert(R);
1643
1644 // Mark the symbol for any live SymbolicRegion as "live". This means we
1645 // should continue to track that symbol.
1646 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1647 SymReaper.markLive(SymR->getSymbol());
1648
1649 // Get the data binding for R (if any).
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001650 const SVal* Xptr = B.lookup(R);
1651 if (!Xptr) {
1652 // No direct binding? Get the default binding for R (if any).
1653 Xptr = DVM.lookup(R);
1654 }
1655
1656 // Direct or default binding?
Ted Kremenek9af46f52009-06-16 22:36:44 +00001657 if (Xptr) {
1658 SVal X = *Xptr;
1659 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1660
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001661 // If X is a region, then add it to the RegionRoots.
1662 if (const MemRegion *RX = X.getAsRegion()) {
1663 RegionRoots.push_back(RX);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001664 // Mark the super region of the RX as live.
1665 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1666 // 'y' => element region. 'x' is its super region.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001667 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1668 RegionRoots.push_back(SR->getSuperRegion());
1669 }
1670 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001671 }
1672
1673 // Get the subregions of R. These are RegionRoots as well since they
1674 // represent values that are also bound to R.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001675 RegionStoreSubRegionMap::iterator I, E;
1676 for (llvm::tie(I, E) = SubRegions->begin_end(R); I != E; ++I)
Ted Kremenek9af46f52009-06-16 22:36:44 +00001677 RegionRoots.push_back(*I);
1678 }
1679
1680 // We have now scanned the store, marking reachable regions and symbols
1681 // as live. We now remove all the regions that are dead from the store
1682 // as well as update DSymbols with the set symbols that are now dead.
1683 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1684 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001685 // If this region live? Is so, none of its symbols are dead.
1686 if (Marked.count(R))
1687 continue;
1688
1689 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001690 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001691
1692 // Mark all non-live symbols that this region references as dead.
1693 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1694 SymReaper.maybeDead(SymR->getSymbol());
1695
1696 SVal X = I.getData();
1697 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001698 for (; SI != SE; ++SI)
1699 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001700 }
1701
Ted Kremenek093569c2009-08-02 05:00:15 +00001702 // Remove dead 'default' bindings.
1703 RegionDefaultValue::MapTy NewDVM = DVM;
1704 RegionDefaultValue::MapTy::Factory &DVMFactory =
1705 state.get_context<RegionDefaultValue>();
1706
1707 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1708 I != E; ++I) {
1709 const MemRegion *R = I.getKey();
1710
1711 // If this region live? Is so, none of its symbols are dead.
1712 if (Marked.count(R))
1713 continue;
1714
1715 // Remove this dead region.
1716 NewDVM = DVMFactory.Remove(NewDVM, R);
1717
1718 // Mark all non-live symbols that this region references as dead.
1719 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1720 SymReaper.maybeDead(SymR->getSymbol());
1721
1722 SVal X = I.getData();
1723 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1724 for (; SI != SE; ++SI)
1725 SymReaper.maybeDead(*SI);
1726 }
1727
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001728 // FIXME: Do a pass over nonloc::LazyCompoundVals and the symbols
1729 // that they reference.
1730
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001731 // Write the store back.
1732 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001733
1734 // Write the updated default bindings back.
1735 // FIXME: Right now this involves a fetching of a persistent state.
1736 // We can do better.
1737 if (DVM != NewDVM)
1738 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001739}
1740
1741//===----------------------------------------------------------------------===//
1742// Utility methods.
1743//===----------------------------------------------------------------------===//
1744
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001745void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001746 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001747 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001748 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001749
Ted Kremenek6f9b3a42009-07-13 23:53:06 +00001750 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001751 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001752}