blob: d556aed73af9c18ffe8a9bcc5a0f846c7ff26980 [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 //===------------------------------------------------------------------===//
346 // Region "views".
347 //===------------------------------------------------------------------===//
348
349 const GRState *AddRegionView(const GRState *state, const MemRegion* View,
350 const MemRegion* Base);
351
352 const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
353 const MemRegion* Base);
354
355 //===------------------------------------------------------------------===//
356 // Utility methods.
357 //===------------------------------------------------------------------===//
358
Zhongxing Xu17892752008-10-08 02:50:44 +0000359 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000360 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000361 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000362
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000363 void print(Store store, llvm::raw_ostream& Out, const char* nl,
364 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000365
366 void iterBindings(Store store, BindingsHandler& f) {
367 // FIXME: Implement.
368 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000369
Ted Kremenek67f28532009-06-17 22:02:04 +0000370 // FIXME: Remove.
371 BasicValueFactory& getBasicVals() {
372 return StateMgr.getBasicVals();
373 }
374
375 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000376 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000377};
378
379} // end anonymous namespace
380
Ted Kremenek9af46f52009-06-16 22:36:44 +0000381//===----------------------------------------------------------------------===//
382// RegionStore creation.
383//===----------------------------------------------------------------------===//
384
385StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
386 RegionStoreFeatures F = maximal_features_tag();
387 return new RegionStoreManager(StMgr, F);
388}
389
390StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
391 RegionStoreFeatures F = minimal_features_tag();
392 F.enableFields(true);
393 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000394}
395
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000396void
397RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
398 const SubRegion *R) {
399 const MemRegion *superR = R->getSuperRegion();
400 if (add(superR, R))
401 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
402 WL.push_back(sr);
403}
404
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000405RegionStoreSubRegionMap*
406RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000407 RegionBindingsTy B = GetRegionBindings(state->getStore());
408 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
409
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000410 llvm::SmallVector<const SubRegion*, 10> WL;
411
412 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000413 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
414 M->process(WL, R);
415
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000416 RegionDefaultValue::MapTy DVM = state->get<RegionDefaultValue>();
417 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000418 I != E; ++I)
419 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
420 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000421
422 // We also need to record in the subregion map "intermediate" regions that
423 // don't have direct bindings but are super regions of those that do.
424 while (!WL.empty()) {
425 const SubRegion *R = WL.back();
426 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000427 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000428 }
429
Ted Kremenek14453bf2009-03-03 19:02:42 +0000430 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000431}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000432
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000433SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
434 return getRegionStoreSubRegionMap(state);
435}
436
Ted Kremenek9af46f52009-06-16 22:36:44 +0000437//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000438// Binding invalidation.
439//===----------------------------------------------------------------------===//
440
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000441void
442RegionStoreManager::RemoveSubRegionBindings(RegionBindingsTy &B,
443 RegionDefaultValue::MapTy &DVM,
444 RegionDefaultValue::MapTy::Factory &DVMFactory,
445 const MemRegion *R,
446 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000447
448 RegionStoreSubRegionMap::iterator I, E;
449
450 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000451 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000452
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000453 B = RBFactory.Remove(B, R);
454 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000455}
456
457
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000458const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
459 const MemRegion *R,
460 const Expr *E,
461 unsigned Count) {
462 ASTContext& Ctx = StateMgr.getContext();
463
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000464 // Strip away casts.
465 R = R->getBaseRegion();
466
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000467 // Remove the bindings to subregions.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000468 {
469 // Get the mapping of regions -> subregions.
470 llvm::OwningPtr<RegionStoreSubRegionMap>
471 SubRegions(getRegionStoreSubRegionMap(state));
472
473 RegionBindingsTy B = GetRegionBindings(state->getStore());
474 RegionDefaultValue::MapTy DVM = state->get<RegionDefaultValue>();
475 RegionDefaultValue::MapTy::Factory &DVMFactory =
476 state->get_context<RegionDefaultValue>();
477
478 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
479 state = state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
480 }
481
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000482 if (!R->isBoundable())
483 return state;
484
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000485 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
486 isa<ObjCObjectRegion>(R)) {
487 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000488 // conjured symbol. The type of the symbol is irrelavant.
489 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000490 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000491 }
492
493 const TypedRegion *TR = cast<TypedRegion>(R);
494 QualType T = TR->getValueType(Ctx);
495
496 // FIXME: The code causes a crash when using RegionStore on the test case
497 // 'test_invalidate_cast_int' (misc-ps.m). Consider removing it
498 // permanently. Region casts are probably not too strict to handle
499 // the transient interpretation of memory. Instead we can use the QualType
500 // passed to 'Retrieve' and friends to determine the most current
501 // interpretation of memory when it is actually used.
502#if 0
503 // If the region is cast to another type, use that type.
504 if (const QualType *CastTy = getCastType(state, R)) {
505 assert(!(*CastTy)->isObjCObjectPointerType());
Ted Kremenek6217b802009-07-29 21:53:49 +0000506 QualType NewT = (*CastTy)->getAs<PointerType>()->getPointeeType();
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000507
508 // The only exception is if the original region had a location type as its
509 // value type we always want to treat the region as binding to a location.
510 // This issue can arise when pointers are casted to integers and back.
511
512 if (!(Loc::IsLocType(T) && !Loc::IsLocType(NewT)))
513 T = NewT;
514 }
515#endif
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000516
517 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000518 // FIXME: handle structs with default region value.
519 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
520
521 // No record definition. There is nothing we can do.
522 if (!RD)
523 return state;
524
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000525 // Invalidate the region by setting its default value to
526 // conjured symbol. The type of the symbol is irrelavant.
527 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
528 return setDefaultValue(state, R, V);
529 }
530
531 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000532 // Set the default value of the array to conjured symbol.
533 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
534 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000535 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000536 }
537
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000538 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
539 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
540 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000541}
542
543//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000544// getLValueXXX methods.
545//===----------------------------------------------------------------------===//
546
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000547/// getLValueString - Returns an SVal representing the lvalue of a
548/// StringLiteral. Within RegionStore a StringLiteral has an
549/// associated StringRegion, and the lvalue of a StringLiteral is the
550/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000551SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000552 const StringLiteral* S) {
553 return loc::MemRegionVal(MRMgr.getStringRegion(S));
554}
555
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000556/// getLValueVar - Returns an SVal that represents the lvalue of a
557/// variable. Within RegionStore a variable has an associated
558/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000559SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000560 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
561}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000562
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000563/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
564/// of a compound literal. Within RegionStore a compound literal
565/// has an associated region, and the lvalue of the compound literal
566/// is the lvalue of that region.
567SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000568RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000569 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000570 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
571}
572
Ted Kremenek67f28532009-06-17 22:02:04 +0000573SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000574 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000575 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000576}
577
Ted Kremenek67f28532009-06-17 22:02:04 +0000578SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000579 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000580 return getLValueFieldOrIvar(St, Base, D);
581}
582
Ted Kremenek67f28532009-06-17 22:02:04 +0000583SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000584 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000585 if (Base.isUnknownOrUndef())
586 return Base;
587
588 Loc BaseL = cast<Loc>(Base);
589 const MemRegion* BaseR = 0;
590
591 switch (BaseL.getSubKind()) {
592 case loc::MemRegionKind:
593 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
594 break;
595
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000596 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000597 // These are anormal cases. Flag an undefined value.
598 return UndefinedVal();
599
600 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000601 // While these seem funny, this can happen through casts.
602 // FIXME: What we should return is the field offset. For example,
603 // add the field offset to the integer value. That way funny things
604 // like this work properly: &(((struct foo *) 0xa)->f)
605 return Base;
606
607 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000608 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000609 return Base;
610 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000611
612 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
613 // of FieldDecl.
614 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
615 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000616
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000617 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000618}
619
Ted Kremenek67f28532009-06-17 22:02:04 +0000620SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000621 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000622 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000623
Ted Kremenekde7ec632009-03-09 22:44:49 +0000624 // If the base is an unknown or undefined value, just return it back.
625 // FIXME: For absolute pointer addresses, we just return that value back as
626 // well, although in reality we should return the offset added to that
627 // value.
628 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000629 return Base;
630
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000631 // Only handle integer offsets... for now.
632 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000633 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000634
Zhongxing Xuce760782009-05-09 13:20:07 +0000635 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000636
637 // Pointer of any type can be cast and used as array base.
638 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
639
Ted Kremenek46537392009-07-16 01:33:37 +0000640 // Convert the offset to the appropriate size and signedness.
641 Offset = ValMgr.convertToArrayIndex(Offset);
642
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000643 if (!ElemR) {
644 //
645 // If the base region is not an ElementRegion, create one.
646 // This can happen in the following example:
647 //
648 // char *p = __builtin_alloc(10);
649 // p[1] = 8;
650 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000651 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000652 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000653 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000654 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000655 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000656
657 SVal BaseIdx = ElemR->getIndex();
658
659 if (!isa<nonloc::ConcreteInt>(BaseIdx))
660 return UnknownVal();
661
662 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
663 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
664 assert(BaseIdxI.isSigned());
665
Ted Kremenek46537392009-07-16 01:33:37 +0000666 // Compute the new index.
667 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000668
Ted Kremenek46537392009-07-16 01:33:37 +0000669 // Construct the new ElementRegion.
670 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000671 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
672 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000673}
674
Ted Kremenek9af46f52009-06-16 22:36:44 +0000675//===----------------------------------------------------------------------===//
676// Extents for regions.
677//===----------------------------------------------------------------------===//
678
Ted Kremenek67f28532009-06-17 22:02:04 +0000679SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000680 const MemRegion *R) {
681
682 switch (R->getKind()) {
683 case MemRegion::MemSpaceRegionKind:
684 assert(0 && "Cannot index into a MemSpace");
685 return UnknownVal();
686
687 case MemRegion::CodeTextRegionKind:
688 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000689 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000690
691 // Not yet handled.
692 case MemRegion::AllocaRegionKind:
693 case MemRegion::CompoundLiteralRegionKind:
694 case MemRegion::ElementRegionKind:
695 case MemRegion::FieldRegionKind:
696 case MemRegion::ObjCIvarRegionKind:
697 case MemRegion::ObjCObjectRegionKind:
698 case MemRegion::SymbolicRegionKind:
699 return UnknownVal();
700
701 case MemRegion::StringRegionKind: {
702 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
703 // We intentionally made the size value signed because it participates in
704 // operations with signed indices.
705 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000706 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000707
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000708 case MemRegion::VarRegionKind: {
709 const VarRegion* VR = cast<VarRegion>(R);
710 // Get the type of the variable.
711 QualType T = VR->getDesugaredValueType(getContext());
712
713 // FIXME: Handle variable-length arrays.
714 if (isa<VariableArrayType>(T))
715 return UnknownVal();
716
717 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
718 // return the size as signed integer.
719 return ValMgr.makeIntVal(CAT->getSize(), false);
720 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000721
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000722 // Clients can use ordinary variables as if they were arrays. These
723 // essentially are arrays of size 1.
724 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000725 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000726
727 case MemRegion::BEG_DECL_REGIONS:
728 case MemRegion::END_DECL_REGIONS:
729 case MemRegion::BEG_TYPED_REGIONS:
730 case MemRegion::END_TYPED_REGIONS:
731 assert(0 && "Infeasible region");
732 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000733 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000734
735 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000736 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000737}
738
Ted Kremenek67f28532009-06-17 22:02:04 +0000739const GRState *RegionStoreManager::setExtent(const GRState *state,
740 const MemRegion *region,
741 SVal extent) {
742 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000743}
744
745//===----------------------------------------------------------------------===//
746// Location and region casting.
747//===----------------------------------------------------------------------===//
748
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000749/// ArrayToPointer - Emulates the "decay" of an array to a pointer
750/// type. 'Array' represents the lvalue of the array being decayed
751/// to a pointer, and the returned SVal represents the decayed
752/// version of that lvalue (i.e., a pointer to the first element of
753/// the array). This is called by GRExprEngine when evaluating casts
754/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000755SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000756 if (!isa<loc::MemRegionVal>(Array))
757 return UnknownVal();
758
759 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
760 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
761
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000762 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000763 return UnknownVal();
764
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000765 // Strip off typedefs from the ArrayRegion's ValueType.
766 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000767 ArrayType *AT = cast<ArrayType>(T);
768 T = AT->getElementType();
769
Ted Kremenek75185b52009-07-16 00:00:11 +0000770 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
771 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000772
773 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000774}
775
Ted Kremenek9af46f52009-06-16 22:36:44 +0000776//===----------------------------------------------------------------------===//
777// Pointer arithmetic.
778//===----------------------------------------------------------------------===//
779
Zhongxing Xu262fd032009-05-20 09:00:16 +0000780SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000781 BinaryOperator::Opcode Op, Loc L, NonLoc R,
782 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000783 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000784 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000785 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000786
Zhongxing Xua1718c72009-04-03 07:33:13 +0000787 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000788 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000789
Ted Kremenek3bccf082009-07-11 00:58:27 +0000790 switch (MR->getKind()) {
791 case MemRegion::SymbolicRegionKind: {
792 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000793 SymbolRef Sym = SR->getSymbol();
794 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000795 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000796 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
797 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
798 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000799 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000800 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000801 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000802 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000803 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000804 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
805 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
806 break;
807 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000808
Ted Kremenek3bccf082009-07-11 00:58:27 +0000809 case MemRegion::ElementRegionKind: {
810 ER = cast<ElementRegion>(MR);
811 break;
812 }
813
814 // Not yet handled.
815 case MemRegion::VarRegionKind:
816 case MemRegion::StringRegionKind:
817 case MemRegion::CompoundLiteralRegionKind:
818 case MemRegion::FieldRegionKind:
819 case MemRegion::ObjCObjectRegionKind:
820 case MemRegion::ObjCIvarRegionKind:
821 return UnknownVal();
822
Ted Kremenek3bccf082009-07-11 00:58:27 +0000823 case MemRegion::CodeTextRegionKind:
824 // Technically this can happen if people do funny things with casts.
825 return UnknownVal();
826
827 case MemRegion::MemSpaceRegionKind:
828 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
829 return UnknownVal();
830
831 case MemRegion::BEG_DECL_REGIONS:
832 case MemRegion::END_DECL_REGIONS:
833 case MemRegion::BEG_TYPED_REGIONS:
834 case MemRegion::END_TYPED_REGIONS:
835 assert(0 && "Infeasible region");
836 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000837 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000838
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000839 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000840 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
841 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
842
843 // Only support concrete integer indexes for now.
844 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000845 // FIXME: Should use SValuator here.
846 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
847 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000848 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000849 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
850 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000851 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000852 }
853
854 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000855}
856
Ted Kremenek9af46f52009-06-16 22:36:44 +0000857//===----------------------------------------------------------------------===//
858// Loading values from regions.
859//===----------------------------------------------------------------------===//
860
Ted Kremeneka6275a52009-07-15 02:31:43 +0000861static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
862 RTy = Ctx.getCanonicalType(RTy);
863 UsedTy = Ctx.getCanonicalType(UsedTy);
864
865 if (RTy == UsedTy)
866 return false;
867
Ted Kremenek25c54572009-07-20 22:58:02 +0000868
869 // Recursively check the types. We basically want to see if a pointer value
870 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
871 // represents a reinterpretation.
872 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000873 const PointerType *PRTy = RTy->getAs<PointerType>();
874 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000875
876 return PUsedTy && PRTy &&
877 IsReinterpreted(PRTy->getPointeeType(),
878 PUsedTy->getPointeeType(), Ctx);
879 }
880
881 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000882}
883
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000884SValuator::CastResult
885RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000886
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000887 assert(!isa<UnknownVal>(L) && "location unknown");
888 assert(!isa<UndefinedVal>(L) && "location undefined");
889
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000890 // FIXME: Is this even possible? Shouldn't this be treated as a null
891 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000892 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000893 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000894
Ted Kremenek67f28532009-06-17 22:02:04 +0000895 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000896
Zhongxing Xu91844122009-05-20 09:18:48 +0000897 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000898 // Example:
899 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000900 // char* p = alloca();
901 // read(p);
902 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000903 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000904 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000905
906 if (isa<SymbolicRegion>(MR)) {
907 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000908 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000909 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000910 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
911 }
912
Ted Kremenek968f0a62009-08-03 21:41:46 +0000913 if (isa<CodeTextRegion>(MR))
914 return SValuator::CastResult(state, UnknownVal());
915
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000916 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
917 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000918 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000919 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000920
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000921 // FIXME: We should eventually handle funny addressing. e.g.:
922 //
923 // int x = ...;
924 // int *p = &x;
925 // char *q = (char*) p;
926 // char c = *q; // returns the first byte of 'x'.
927 //
928 // Such funny addressing will occur due to layering of regions.
929
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000930#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000931 ASTContext &Ctx = getContext();
932 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000933 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
934 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000935 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000936 assert(Ctx.getCanonicalType(RTy) ==
937 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000938 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000939#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000940
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000941 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000942 return SValuator::CastResult(state, RetrieveStruct(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000943
944 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000945 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000946
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000947 // FIXME: handle Vector types.
948 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000949 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000950
951 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000952 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000953
954 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000955 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000956
Ted Kremenek25c54572009-07-20 22:58:02 +0000957 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000958 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000959
960 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000961 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000962
Ted Kremenek67f28532009-06-17 22:02:04 +0000963 RegionBindingsTy B = GetRegionBindings(state->getStore());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000964 RegionBindingsTy::data_type* V = B.lookup(R);
965
966 // Check if the region has a binding.
967 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000968 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000969
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000970 // The location does not have a bound value. This means that it has
971 // the value it had upon its creation and/or entry to the analyzed
972 // function/method. These are either symbolic values or 'undefined'.
973
Ted Kremenek356e9d62009-07-22 04:35:42 +0000974#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000975 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000976#else
977 if (R->hasStackStorage()) {
978#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000979 // All stack variables are considered to have undefined values
980 // upon creation. All heap allocated blocks are considered to
981 // have undefined values as well unless they are explicitly bound
982 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000983 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000984 }
985
Ted Kremenek356e9d62009-07-22 04:35:42 +0000986#if USE_REGION_CASTS
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000987 // If the region is already cast to another type, use that type to create the
988 // symbol value.
989 if (const QualType *p = state->get<RegionCasts>(R)) {
990 QualType T = *p;
Ted Kremenek6217b802009-07-29 21:53:49 +0000991 RTy = T->getAs<PointerType>()->getPointeeType();
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000992 }
Ted Kremenek356e9d62009-07-22 04:35:42 +0000993#endif
Zhongxing Xu88c675f2009-06-18 06:29:10 +0000994
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000995 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000996 return SValuator::CastResult(state,
997 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000998}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000999
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001000std::pair<const GRState*, const MemRegion*>
1001RegionStoreManager::GetLazyBinding(RegionBindingsTy B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001002
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001003 if (const nonloc::LazyCompoundVal *V =
1004 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
1005 return std::make_pair(V->getState(), V->getRegion());
1006
1007 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1008 const std::pair<const GRState *, const MemRegion *> &X =
1009 GetLazyBinding(B, ER->getSuperRegion());
1010
1011 if (X.first)
1012 return std::make_pair(X.first,
1013 MRMgr.getElementRegionWithSuper(ER, X.second));
1014 }
1015 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1016 const std::pair<const GRState *, const MemRegion *> &X =
1017 GetLazyBinding(B, FR->getSuperRegion());
1018
1019 if (X.first)
1020 return std::make_pair(X.first,
1021 MRMgr.getFieldRegionWithSuper(FR, X.second));
1022 }
1023
1024 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1025}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001026
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001027SVal RegionStoreManager::RetrieveElement(const GRState* state,
1028 const ElementRegion* R) {
1029 // Check if the region has a binding.
1030 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001031 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001032 return *V;
1033
Ted Kremenek921109a2009-07-01 23:19:52 +00001034 const MemRegion* superR = R->getSuperRegion();
1035
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001036 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001037 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001038 const StringLiteral *Str = StrR->getStringLiteral();
1039 SVal Idx = R->getIndex();
1040 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1041 int64_t i = CI->getValue().getSExtValue();
1042 char c;
1043 if (i == Str->getByteLength())
1044 c = '\0';
1045 else
1046 c = Str->getStrData()[i];
1047 return ValMgr.makeIntVal(c, getContext().CharTy);
1048 }
1049 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001050
1051 // Special case: the current region represents a cast and it and the super
1052 // region both have pointer types or intptr_t types. If so, perform the
1053 // retrieve from the super region and appropriately "cast" the value.
1054 // This is needed to support OSAtomicCompareAndSwap and friends or other
1055 // loads that treat integers as pointers and vis versa.
1056 if (R->getIndex().isZeroConstant()) {
1057 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1058 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001059 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1060 QualType valTy = R->getValueType(Ctx);
1061 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1062 // Retrieve the value from the super region. This will be casted to
1063 // valTy when we return to 'Retrieve'.
1064 const SValuator::CastResult &cr = Retrieve(state,
1065 loc::MemRegionVal(superR),
1066 valTy);
1067 return cr.getSVal();
1068 }
1069 }
1070 }
1071 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001072
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001073 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +00001074 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001075 if (D->hasConjuredSymbol())
1076 return ValMgr.getRegionValueSymbolVal(R);
1077 else
1078 return *D;
1079 }
1080
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001081 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001082 if (const SVal *V = B.lookup(superR)) {
1083 if (SymbolRef parentSym = V->getAsSymbol())
1084 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001085
1086 if (V->isUnknownOrUndef())
1087 return *V;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001088
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001089 // Handle LazyCompoundVals below.
1090 if (const nonloc::LazyCompoundVal *LVC =
1091 dyn_cast<nonloc::LazyCompoundVal>(V)) {
1092 return RetrieveElement(LVC->getState(),
1093 MRMgr.getElementRegionWithSuper(R,
1094 LVC->getRegion()));
1095 }
1096
Ted Kremeneka6275a52009-07-15 02:31:43 +00001097 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001098 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001099 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001100
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001101 // Lazy binding?
1102 const GRState *lazyBindingState = NULL;
1103 const MemRegion *LazyBindingRegion = NULL;
1104 llvm::tie(lazyBindingState, LazyBindingRegion) = GetLazyBinding(B, R);
1105
1106 if (lazyBindingState) {
1107 assert(LazyBindingRegion && "Lazy-binding region not set");
1108 return RetrieveElement(lazyBindingState,
1109 cast<ElementRegion>(LazyBindingRegion));
1110 }
1111
1112 // Default value cases.
Ted Kremenek356e9d62009-07-22 04:35:42 +00001113#if 0
Ted Kremenek921109a2009-07-01 23:19:52 +00001114 if (R->hasHeapStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001115 // FIXME: If the region has heap storage and we know nothing special
1116 // about its bindings, should we instead return UnknownVal? Seems like
1117 // we should only return UndefinedVal in the cases where we know the value
1118 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001119 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +00001120 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001121#endif
1122
Ted Kremenekdc147262009-07-02 22:02:15 +00001123 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +00001124 // Currently we don't reason specially about Clang-style vectors. Check
1125 // if superR is a vector and if so return Unknown.
1126 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1127 if (typedSuperR->getValueType(getContext())->isVectorType())
1128 return UnknownVal();
1129 }
1130
1131 return UndefinedVal();
1132 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001133
1134 QualType Ty = R->getValueType(getContext());
1135
Ted Kremenek356e9d62009-07-22 04:35:42 +00001136#if USE_REGION_CASTS
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001137 // If the region is already cast to another type, use that type to create the
1138 // symbol value.
1139 if (const QualType *p = state->get<RegionCasts>(R))
Ted Kremenek6217b802009-07-29 21:53:49 +00001140 Ty = (*p)->getAs<PointerType>()->getPointeeType();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001141#endif
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001142
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001143 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001144}
1145
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001146SVal RegionStoreManager::RetrieveField(const GRState* state,
1147 const FieldRegion* R) {
1148 QualType Ty = R->getValueType(getContext());
1149
1150 // Check if the region has a binding.
1151 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001152 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001153 return *V;
1154
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001155 const MemRegion* superR = R->getSuperRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001156 while (superR) {
1157 if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
1158 if (SymbolRef parentSym = D->getAsSymbol())
1159 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001160
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001161 if (D->isZeroConstant())
1162 return ValMgr.makeZeroVal(Ty);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001163
1164 if (const nonloc::LazyCompoundVal *LCV =
1165 dyn_cast<nonloc::LazyCompoundVal>(D)) {
1166 const FieldRegion *FR =
1167 MRMgr.getFieldRegionWithSuper(R, LCV->getRegion());
1168 return RetrieveField(LCV->getState(), FR);
1169 }
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001170
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001171 if (D->isUnknown())
1172 return *D;
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001173
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001174 assert(0 && "Unknown default value");
1175 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001176
1177 if (const SVal *V = B.lookup(superR)) {
1178 // Handle LazyCompoundVals below.
1179 if (isa<nonloc::CompoundVal>(*V))
1180 break;
1181 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001182
1183 // If our super region is a field or element itself, walk up the region
1184 // hierarchy to see if there is a default value installed in an ancestor.
1185 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1186 superR = cast<SubRegion>(superR)->getSuperRegion();
1187 continue;
1188 }
1189
1190 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001191 }
1192
1193 // Lazy binding?
1194 const GRState *lazyBindingState = NULL;
1195 const MemRegion *LazyBindingRegion = NULL;
1196 llvm::tie(lazyBindingState, LazyBindingRegion) = GetLazyBinding(B, R);
1197
1198 if (lazyBindingState) {
1199 assert(LazyBindingRegion && "Lazy-binding region not set");
1200 return RetrieveField(lazyBindingState,
1201 cast<FieldRegion>(LazyBindingRegion));
1202 }
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001203
Ted Kremenek356e9d62009-07-22 04:35:42 +00001204#if HEAP_UNDEFINED
Ted Kremenekdc147262009-07-02 22:02:15 +00001205 // FIXME: Is this correct? Should it be UnknownVal?
1206 if (R->hasHeapStorage())
1207 return UndefinedVal();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001208#endif
Ted Kremenekdc147262009-07-02 22:02:15 +00001209
1210 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001211 return UndefinedVal();
1212
Ted Kremenek356e9d62009-07-22 04:35:42 +00001213#if USE_REGION_CASTS
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001214 // If the region is already cast to another type, use that type to create the
1215 // symbol value.
1216 if (const QualType *p = state->get<RegionCasts>(R)) {
1217 QualType tmp = *p;
Ted Kremenek6217b802009-07-29 21:53:49 +00001218 Ty = tmp->getAs<PointerType>()->getPointeeType();
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001219 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001220#endif
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001221
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001222 // All other values are symbolic.
1223 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001224}
1225
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001226SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1227 const ObjCIvarRegion* R) {
1228
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001229 // Check if the region has a binding.
1230 RegionBindingsTy B = GetRegionBindings(state->getStore());
1231
1232 if (const SVal* V = B.lookup(R))
1233 return *V;
1234
1235 const MemRegion *superR = R->getSuperRegion();
1236
1237 // Check if the super region has a binding.
1238 if (const SVal *V = B.lookup(superR)) {
1239 if (SymbolRef parentSym = V->getAsSymbol())
1240 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1241
1242 // Other cases: give up.
1243 return UnknownVal();
1244 }
1245
Ted Kremenek25c54572009-07-20 22:58:02 +00001246 return RetrieveLazySymbol(state, R);
1247}
1248
Ted Kremenek9031dd72009-07-21 00:12:07 +00001249SVal RegionStoreManager::RetrieveVar(const GRState *state,
1250 const VarRegion *R) {
1251
1252 // Check if the region has a binding.
1253 RegionBindingsTy B = GetRegionBindings(state->getStore());
1254
1255 if (const SVal* V = B.lookup(R))
1256 return *V;
1257
1258 // Lazily derive a value for the VarRegion.
1259 const VarDecl *VD = R->getDecl();
1260
1261 if (VD == SelfDecl)
1262 return loc::MemRegionVal(getSelfRegion(0));
1263
1264 if (R->hasGlobalsOrParametersStorage())
1265 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1266
1267 return UndefinedVal();
1268}
1269
Ted Kremenek25c54572009-07-20 22:58:02 +00001270SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1271 const TypedRegion *R) {
1272
1273 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001274
1275#if USE_REGION_CASTS
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001276 // If the region is already cast to another type, use that type to create the
1277 // symbol value.
Ted Kremenek25c54572009-07-20 22:58:02 +00001278 if (const QualType *ty = state->get<RegionCasts>(R)) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001279 if (const PointerType *PT = (*ty)->getAs<PointerType>()) {
Ted Kremenek25c54572009-07-20 22:58:02 +00001280 QualType castTy = PT->getPointeeType();
1281
1282 if (!IsReinterpreted(valTy, castTy, getContext()))
1283 valTy = castTy;
1284 }
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001285 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001286#endif
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001287
1288 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001289 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001290}
1291
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001292SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1293 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001294 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001295 assert(T->isStructureType());
1296
Zhongxing Xub7507d12009-06-11 07:27:30 +00001297 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001298 RecordDecl* RD = RT->getDecl();
1299 assert(RD->isDefinition());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001300#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001301 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1302
Ted Kremenek67f28532009-06-17 22:02:04 +00001303 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1304 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001305 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001306
Douglas Gregore267ff32008-12-11 20:41:00 +00001307 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1308 FieldEnd = Fields.rend();
1309 Field != FieldEnd; ++Field) {
1310 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001311 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001312 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001313 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1314 }
1315
Zhongxing Xud91ee272009-06-23 09:02:15 +00001316 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001317#else
1318 return ValMgr.makeLazyCompoundVal(state, R);
1319#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001320}
1321
Ted Kremenek67f28532009-06-17 22:02:04 +00001322SVal RegionStoreManager::RetrieveArray(const GRState *state,
1323 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001324#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001325 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001326 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1327
1328 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001329 uint64_t size = CAT->getSize().getZExtValue();
1330 for (uint64_t i = 0; i < size; ++i) {
1331 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001332 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1333 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001334 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001335 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001336 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1337 }
1338
Zhongxing Xud91ee272009-06-23 09:02:15 +00001339 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001340#else
1341 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1342 return ValMgr.makeLazyCompoundVal(state, R);
1343#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001344}
1345
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001346SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1347 const GRState *state,
1348 const TypedRegion *R,
1349 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001350 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001351 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001352
1353 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001354 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001355}
1356
Ted Kremenek9af46f52009-06-16 22:36:44 +00001357//===----------------------------------------------------------------------===//
1358// Binding values to regions.
1359//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001360
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001361Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001362 const MemRegion* R = 0;
1363
1364 if (isa<loc::MemRegionVal>(L))
1365 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001366
1367 if (R) {
1368 RegionBindingsTy B = GetRegionBindings(store);
1369 return RBFactory.Remove(B, R).getRoot();
1370 }
1371
1372 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001373}
1374
Ted Kremenek67f28532009-06-17 22:02:04 +00001375const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001376 if (isa<loc::ConcreteInt>(L))
1377 return state;
1378
Ted Kremenek9af46f52009-06-16 22:36:44 +00001379 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001380 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001381
1382 // Check if the region is a struct region.
1383 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1384 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001385 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001386
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001387 // Special case: the current region represents a cast and it and the super
1388 // region both have pointer types or intptr_t types. If so, perform the
1389 // bind to the super region.
1390 // This is needed to support OSAtomicCompareAndSwap and friends or other
1391 // loads that treat integers as pointers and vis versa.
1392 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1393 if (ER->getIndex().isZeroConstant()) {
1394 if (const TypedRegion *superR =
1395 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1396 ASTContext &Ctx = getContext();
1397 QualType superTy = superR->getValueType(Ctx);
1398 QualType erTy = ER->getValueType(Ctx);
1399
1400 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1401 IsAnyPointerOrIntptr(erTy, Ctx)) {
1402 SValuator::CastResult cr =
1403 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1404 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1405 }
1406 }
1407 }
1408 }
1409
1410 // Perform the binding.
Ted Kremenek67f28532009-06-17 22:02:04 +00001411 RegionBindingsTy B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001412 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001413}
1414
Ted Kremenek67f28532009-06-17 22:02:04 +00001415const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001416 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001417
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001418 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001419 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001420
Ted Kremenek0964a062009-01-21 06:57:53 +00001421 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001422 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001423 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001424 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001425
Zhongxing Xud91ee272009-06-23 09:02:15 +00001426 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001427}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001428
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001429// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001430const GRState *
1431RegionStoreManager::BindCompoundLiteral(const GRState *state,
1432 const CompoundLiteralExpr* CL,
1433 SVal V) {
1434
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001435 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001436 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001437}
1438
Ted Kremenek67f28532009-06-17 22:02:04 +00001439const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001440 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001441 SVal Init) {
1442
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001443 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001444 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001445 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001446
Ted Kremenek46537392009-07-16 01:33:37 +00001447 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001448
1449 // Check if the init expr is a StringLiteral.
1450 if (isa<loc::MemRegionVal>(Init)) {
1451 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1452 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1453 const char* str = S->getStrData();
1454 unsigned len = S->getByteLength();
1455 unsigned j = 0;
1456
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001457 // Copy bytes from the string literal into the target array. Trailing bytes
1458 // in the array that are not covered by the string literal are initialized
1459 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001460 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001461 if (j >= len)
1462 break;
1463
Ted Kremenek46537392009-07-16 01:33:37 +00001464 SVal Idx = ValMgr.makeArrayIndex(i);
1465 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1466 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001467
Zhongxing Xud91ee272009-06-23 09:02:15 +00001468 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001469 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001470 }
1471
Ted Kremenek67f28532009-06-17 22:02:04 +00001472 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001473 }
1474
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001475 // Handle lazy compound values.
1476 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1477 return CopyLazyBindings(*LCV, state, R);
1478
1479 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001480 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001481 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001482 uint64_t i = 0;
1483
1484 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001485 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001486 if (VI == VE)
1487 break;
1488
Ted Kremenek46537392009-07-16 01:33:37 +00001489 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001490 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001491
1492 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001493 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001494 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001495 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001496 }
1497
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001498 // If the init list is shorter than the array length, set the array default
1499 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001500 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001501 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001502 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001503 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001504 }
1505 }
1506
Ted Kremenek67f28532009-06-17 22:02:04 +00001507 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001508}
1509
Ted Kremenek67f28532009-06-17 22:02:04 +00001510const GRState *
1511RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1512 SVal V) {
1513
1514 if (!Features.supportsFields())
1515 return state;
1516
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001517 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001518 assert(T->isStructureType());
1519
Ted Kremenek6217b802009-07-29 21:53:49 +00001520 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001521 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001522
1523 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001524 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001525
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001526 // Handle lazy compound values.
1527 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1528 return CopyLazyBindings(*LCV, state, R);
1529
Ted Kremenek67f28532009-06-17 22:02:04 +00001530 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1531 // Ignore them and kill the field values.
1532 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1533 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001534
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001535 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001536 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001537
1538 RecordDecl::field_iterator FI, FE;
1539
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001540 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001541
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001542 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001543 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001544
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001545 QualType FTy = (*FI)->getType();
1546 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1547
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001548 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001549 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001550 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001551 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001552 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001553 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001554 }
1555
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001556 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001557 if (FI != FE)
1558 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001559
Ted Kremenek67f28532009-06-17 22:02:04 +00001560 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001561}
1562
Ted Kremenek67f28532009-06-17 22:02:04 +00001563const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001564 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001565
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001566 // Set the default value of the struct region to "unknown".
1567 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001568
1569 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001570 Store store = state->getStore();
1571 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001572 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001573 const MemRegion* R = I.getKey();
1574 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1575 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001576 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001577 }
1578
Ted Kremenek67f28532009-06-17 22:02:04 +00001579 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001580}
1581
Ted Kremenek67f28532009-06-17 22:02:04 +00001582const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1583 const MemRegion* R, SVal V) {
1584 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001585}
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001586
1587const GRState*
1588RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1589 const GRState *state,
1590 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001591
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001592 // Nuke the old bindings stemming from R.
1593 RegionBindingsTy B = GetRegionBindings(state->getStore());
1594 RegionDefaultValue::MapTy DVM = state->get<RegionDefaultValue>();
1595 RegionDefaultValue::MapTy::Factory &DVMFactory =
1596 state->get_context<RegionDefaultValue>();
1597
1598 llvm::OwningPtr<RegionStoreSubRegionMap>
1599 SubRegions(getRegionStoreSubRegionMap(state));
1600
1601 // B and DVM are updated after the call to RemoveSubRegionBindings.
1602 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
1603
1604 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1605 // results in a zero-copy algorithm.
1606 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1607}
1608
Ted Kremenek9af46f52009-06-16 22:36:44 +00001609//===----------------------------------------------------------------------===//
1610// State pruning.
1611//===----------------------------------------------------------------------===//
1612
1613static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1614 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1615 const MemRegion *R = XR->getRegion();
1616
1617 while (R) {
1618 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1619 SymReaper.markLive(SR->getSymbol());
1620 return;
1621 }
1622
1623 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1624 R = SR->getSuperRegion();
1625 continue;
1626 }
1627
1628 break;
1629 }
1630
1631 return;
1632 }
1633
1634 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1635 SymReaper.markLive(*SI);
1636}
1637
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001638void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1639 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001640 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001641{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001642 Store store = state.getStore();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001643 RegionBindingsTy B = GetRegionBindings(store);
1644
1645 // Lazily constructed backmap from MemRegions to SubRegions.
1646 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1647 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1648
Ted Kremenek9af46f52009-06-16 22:36:44 +00001649 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001650 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001651 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001652
1653 // Do a pass over the regions in the store. For VarRegions we check if
1654 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001655 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001656 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1657
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001658 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001659 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001660 const MemRegion *R = I.getKey();
1661 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001662 }
1663
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001664 // Scan the default bindings for "intermediate" roots.
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001665 RegionDefaultValue::MapTy DVM = state.get<RegionDefaultValue>();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001666 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1667 I != E; ++I) {
1668 const MemRegion *R = I.getKey();
1669 IntermediateRoots.push_back(R);
1670 }
1671
1672 // Process the "intermediate" roots to find if they are referenced by
1673 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001674 while (!IntermediateRoots.empty()) {
1675 const MemRegion* R = IntermediateRoots.back();
1676 IntermediateRoots.pop_back();
1677
1678 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001679 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001680 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001681 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001682 continue;
1683 }
1684
1685 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001686 if (SymReaper.isLive(SR->getSymbol()))
1687 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001688 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001689 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001690
1691 // Add the super region for R to the worklist if it is a subregion.
1692 if (const SubRegion* superR =
1693 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1694 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001695 }
1696
1697 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1698 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001699 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001700 while (!RegionRoots.empty()) {
1701 // Dequeue the next region on the worklist.
1702 const MemRegion* R = RegionRoots.back();
1703 RegionRoots.pop_back();
1704
1705 // Check if we have already processed this region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001706 if (Marked.count(R))
1707 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001708
1709 // Mark this region as processed. This is needed for termination in case
1710 // a region is referenced more than once.
1711 Marked.insert(R);
1712
1713 // Mark the symbol for any live SymbolicRegion as "live". This means we
1714 // should continue to track that symbol.
1715 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1716 SymReaper.markLive(SymR->getSymbol());
1717
1718 // Get the data binding for R (if any).
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001719 const SVal* Xptr = B.lookup(R);
1720 if (!Xptr) {
1721 // No direct binding? Get the default binding for R (if any).
1722 Xptr = DVM.lookup(R);
1723 }
1724
1725 // Direct or default binding?
Ted Kremenek9af46f52009-06-16 22:36:44 +00001726 if (Xptr) {
1727 SVal X = *Xptr;
1728 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1729
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001730 // If X is a region, then add it to the RegionRoots.
1731 if (const MemRegion *RX = X.getAsRegion()) {
1732 RegionRoots.push_back(RX);
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001733 // Mark the super region of the RX as live.
1734 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1735 // 'y' => element region. 'x' is its super region.
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001736 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1737 RegionRoots.push_back(SR->getSuperRegion());
1738 }
1739 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001740 }
1741
1742 // Get the subregions of R. These are RegionRoots as well since they
1743 // represent values that are also bound to R.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001744 RegionStoreSubRegionMap::iterator I, E;
1745 for (llvm::tie(I, E) = SubRegions->begin_end(R); I != E; ++I)
Ted Kremenek9af46f52009-06-16 22:36:44 +00001746 RegionRoots.push_back(*I);
1747 }
1748
1749 // We have now scanned the store, marking reachable regions and symbols
1750 // as live. We now remove all the regions that are dead from the store
1751 // as well as update DSymbols with the set symbols that are now dead.
1752 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1753 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001754 // If this region live? Is so, none of its symbols are dead.
1755 if (Marked.count(R))
1756 continue;
1757
1758 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001759 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001760
1761 // Mark all non-live symbols that this region references as dead.
1762 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1763 SymReaper.maybeDead(SymR->getSymbol());
1764
1765 SVal X = I.getData();
1766 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001767 for (; SI != SE; ++SI)
1768 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001769 }
1770
Ted Kremenek093569c2009-08-02 05:00:15 +00001771 // Remove dead 'default' bindings.
1772 RegionDefaultValue::MapTy NewDVM = DVM;
1773 RegionDefaultValue::MapTy::Factory &DVMFactory =
1774 state.get_context<RegionDefaultValue>();
1775
1776 for (RegionDefaultValue::MapTy::iterator I = DVM.begin(), E = DVM.end();
1777 I != E; ++I) {
1778 const MemRegion *R = I.getKey();
1779
1780 // If this region live? Is so, none of its symbols are dead.
1781 if (Marked.count(R))
1782 continue;
1783
1784 // Remove this dead region.
1785 NewDVM = DVMFactory.Remove(NewDVM, R);
1786
1787 // Mark all non-live symbols that this region references as dead.
1788 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1789 SymReaper.maybeDead(SymR->getSymbol());
1790
1791 SVal X = I.getData();
1792 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1793 for (; SI != SE; ++SI)
1794 SymReaper.maybeDead(*SI);
1795 }
1796
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001797 // FIXME: Do a pass over nonloc::LazyCompoundVals and the symbols
1798 // that they reference.
1799
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001800 // Write the store back.
1801 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001802
1803 // Write the updated default bindings back.
1804 // FIXME: Right now this involves a fetching of a persistent state.
1805 // We can do better.
1806 if (DVM != NewDVM)
1807 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001808}
1809
1810//===----------------------------------------------------------------------===//
1811// Utility methods.
1812//===----------------------------------------------------------------------===//
1813
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001814void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001815 const char* nl, const char *sep) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001816 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001817 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001818
Ted Kremenek6f9b3a42009-07-13 23:53:06 +00001819 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001820 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001821}