blob: 48e6de7cd8d57eb6be3ec53cc8797bf42cfc28ce [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"
Ted Kremenekd4e5a602009-08-06 21:43:54 +000021#include "clang/Analysis/Support/Optional.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000022#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000023
24#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000025#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000026#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000027#include "llvm/Support/Compiler.h"
28
29using namespace clang;
30
Ted Kremenek356e9d62009-07-22 04:35:42 +000031#define HEAP_UNDEFINED 0
Ted Kremeneka5e81f12009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000033
Zhongxing Xubaf03a72008-11-24 09:44:56 +000034// Actual Store type.
Ted Kremenek451ac092009-08-06 04:50:20 +000035typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000036
Ted Kremenek50dc1b32008-12-24 01:05:03 +000037//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000038// Fine-grained control of RegionStoreManager.
39//===----------------------------------------------------------------------===//
40
41namespace {
42struct VISIBILITY_HIDDEN minimal_features_tag {};
43struct VISIBILITY_HIDDEN maximal_features_tag {};
44
45class VISIBILITY_HIDDEN RegionStoreFeatures {
46 bool SupportsFields;
47 bool SupportsRemaining;
48
49public:
50 RegionStoreFeatures(minimal_features_tag) :
51 SupportsFields(false), SupportsRemaining(false) {}
52
53 RegionStoreFeatures(maximal_features_tag) :
54 SupportsFields(true), SupportsRemaining(false) {}
55
56 void enableFields(bool t) { SupportsFields = t; }
57
58 bool supportsFields() const { return SupportsFields; }
59 bool supportsRemaining() const { return SupportsRemaining; }
60};
61}
62
63//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000064// Region "Extents"
65//===----------------------------------------------------------------------===//
66//
67// MemRegions represent chunks of memory with a size (their "extent"). This
68// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000069//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000070namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
71static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000072namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000073 template<> struct GRStateTrait<RegionExtents>
74 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
75 static void* GDMIndex() { return &RegionExtentsIndex; }
76 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000077}
78
Ted Kremenek50dc1b32008-12-24 01:05:03 +000079//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000080// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000081//===----------------------------------------------------------------------===//
82//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000083// This GDM entry tracks what regions have a default value if they have no bound
84// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000085//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000086namespace {
87class VISIBILITY_HIDDEN RegionDefaultValue {
88public:
89 typedef llvm::ImmutableMap<const MemRegion*, SVal> MapTy;
90};
91}
Ted Kremenek50dc1b32008-12-24 01:05:03 +000092static int RegionDefaultValueIndex = 0;
93namespace clang {
94 template<> struct GRStateTrait<RegionDefaultValue>
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000095 : public GRStatePartialTrait<RegionDefaultValue::MapTy> {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000096 static void* GDMIndex() { return &RegionDefaultValueIndex; }
97 };
98}
99
Ted Kremenek451ac092009-08-06 04:50:20 +0000100typedef RegionDefaultValue::MapTy RegionDefaultBindings;
101
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000102//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000103// Utility functions.
104//===----------------------------------------------------------------------===//
105
106static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
107 if (ty->isAnyPointerType())
108 return true;
109
110 return ty->isIntegerType() && ty->isScalarType() &&
111 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
112}
113
114//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000115// Main RegionStore logic.
116//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000117
Zhongxing Xu17892752008-10-08 02:50:44 +0000118namespace {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000119
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000120class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
121 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
122 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
123 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000124 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000125public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000126 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000127 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000128
129 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000130 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000131 return true;
132 }
133
134 I->second = F.Add(I->second, SubRegion);
135 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000136 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000137
138 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000139
140 ~RegionStoreSubRegionMap() {}
141
Ted Kremenek5dc27462009-03-03 02:51:43 +0000142 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000143 Map::iterator I = M.find(Parent);
144
145 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000146 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000147
148 llvm::ImmutableSet<const MemRegion*> S = I->second;
149 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
150 SI != SE; ++SI) {
151 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000152 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000153 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000154
155 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000156 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000157
158 typedef SetTy::iterator iterator;
159
160 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
161 Map::iterator I = M.find(R);
162 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
163 return std::make_pair(S.begin(), S.end());
164 }
Ted Kremenek59e8f112009-03-03 01:35:36 +0000165};
166
Zhongxing Xu17892752008-10-08 02:50:44 +0000167class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000168 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000169 RegionBindings::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000170
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000171 const MemRegion* SelfRegion;
172 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000173
174public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000175 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000176 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000177 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000178 RBFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000179 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000180 if (const ObjCMethodDecl* MD =
181 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
182 SelfDecl = MD->getSelfDecl();
183 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000184
185 virtual ~RegionStoreManager() {}
186
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000187 SubRegionMap *getSubRegionMap(const GRState *state);
188
189 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000190
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000191
192 /// getDefaultBinding - Returns an SVal* representing an optional default
193 /// binding associated with a region and its subregions.
194 Optional<SVal> getDefaultBinding(const GRState *state, const MemRegion *R);
195
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000196 /// getLValueString - Returns an SVal representing the lvalue of a
197 /// StringLiteral. Within RegionStore a StringLiteral has an
198 /// associated StringRegion, and the lvalue of a StringLiteral is
199 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000200 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000201
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000202 /// getLValueCompoundLiteral - Returns an SVal representing the
203 /// lvalue of a compound literal. Within RegionStore a compound
204 /// literal has an associated region, and the lvalue of the
205 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000206 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000207
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000208 /// getLValueVar - Returns an SVal that represents the lvalue of a
209 /// variable. Within RegionStore a variable has an associated
210 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000211 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000212
Ted Kremenek67f28532009-06-17 22:02:04 +0000213 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000214
Ted Kremenek67f28532009-06-17 22:02:04 +0000215 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000216
Ted Kremenek67f28532009-06-17 22:02:04 +0000217 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000218
Ted Kremenek67f28532009-06-17 22:02:04 +0000219 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000220 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000221
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000222
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000223 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
224 /// type. 'Array' represents the lvalue of the array being decayed
225 /// to a pointer, and the returned SVal represents the decayed
226 /// version of that lvalue (i.e., a pointer to the first element of
227 /// the array). This is called by GRExprEngine when evaluating
228 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000229 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000230
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000231 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000232 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000233
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000234 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000235
236 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
237 /// 'this' object (C++). When used when analyzing a normal function this
238 /// method returns NULL.
239 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000240 if (!SelfDecl)
241 return 0;
242
243 if (!SelfRegion) {
244 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
245 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
246 MRMgr.getHeapRegion());
247 }
248
249 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000250 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000251
252 //===-------------------------------------------------------------------===//
253 // Binding values to regions.
254 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000255
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000256 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
257 const Expr *E, unsigned Count);
258
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000259private:
Ted Kremenek451ac092009-08-06 04:50:20 +0000260 void RemoveSubRegionBindings(RegionBindings &B,
261 RegionDefaultBindings &DVM,
262 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000263 const MemRegion *R,
264 RegionStoreSubRegionMap &M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000265
266public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000267 const GRState *Bind(const GRState *state, Loc LV, SVal V);
268
269 const GRState *BindCompoundLiteral(const GRState *state,
270 const CompoundLiteralExpr* CL, SVal V);
271
272 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
273
274 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
275 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000276 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000277
Ted Kremenek67f28532009-06-17 22:02:04 +0000278 /// BindStruct - Bind a compound value to a structure.
279 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
280
281 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
282
283 /// KillStruct - Set the entire struct to unknown.
284 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
285
286 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
287
288 Store Remove(Store store, Loc LV);
289
290 //===------------------------------------------------------------------===//
291 // Loading values from regions.
292 //===------------------------------------------------------------------===//
293
294 /// The high level logic for this method is this:
295 /// Retrieve (L)
296 /// if L has binding
297 /// return L's binding
298 /// else if L is in killset
299 /// return unknown
300 /// else
301 /// if L is on stack or heap
302 /// return undefined
303 /// else
304 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000305 SValuator::CastResult Retrieve(const GRState *state, Loc L,
306 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000307
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000308 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000309
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000310 SVal RetrieveField(const GRState *state, const FieldRegion *R);
311
312 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000313
Ted Kremenek9031dd72009-07-21 00:12:07 +0000314 SVal RetrieveVar(const GRState *state, const VarRegion *R);
315
Ted Kremenek25c54572009-07-20 22:58:02 +0000316 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
317
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000318 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
319 QualType Ty, const MemRegion *superR);
320
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000321 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
322 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000323
Ted Kremenek67f28532009-06-17 22:02:04 +0000324 /// Retrieve the values in a struct and return a CompoundVal, used when doing
325 /// struct copy:
326 /// struct s x, y;
327 /// x = y;
328 /// y's value is retrieved by this method.
329 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
330
331 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000332
333 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000334 GetLazyBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000335
336 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
337 const GRState *state,
338 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000339
340 //===------------------------------------------------------------------===//
341 // State pruning.
342 //===------------------------------------------------------------------===//
343
344 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
345 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000346 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000347 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
348
349 //===------------------------------------------------------------------===//
350 // Region "extents".
351 //===------------------------------------------------------------------===//
352
353 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
354 SVal getSizeInElements(const GRState *state, const MemRegion* R);
355
356 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000357 // Utility methods.
358 //===------------------------------------------------------------------===//
359
Ted Kremenek451ac092009-08-06 04:50:20 +0000360 static inline RegionBindings GetRegionBindings(Store store) {
361 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000362 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000363
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000364 void print(Store store, llvm::raw_ostream& Out, const char* nl,
365 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000366
367 void iterBindings(Store store, BindingsHandler& f) {
368 // FIXME: Implement.
369 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000370
Ted Kremenek67f28532009-06-17 22:02:04 +0000371 // FIXME: Remove.
372 BasicValueFactory& getBasicVals() {
373 return StateMgr.getBasicVals();
374 }
375
376 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000377 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000378};
379
380} // end anonymous namespace
381
Ted Kremenek9af46f52009-06-16 22:36:44 +0000382//===----------------------------------------------------------------------===//
383// RegionStore creation.
384//===----------------------------------------------------------------------===//
385
386StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
387 RegionStoreFeatures F = maximal_features_tag();
388 return new RegionStoreManager(StMgr, F);
389}
390
391StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
392 RegionStoreFeatures F = minimal_features_tag();
393 F.enableFields(true);
394 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000395}
396
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000397void
398RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
399 const SubRegion *R) {
400 const MemRegion *superR = R->getSuperRegion();
401 if (add(superR, R))
402 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
403 WL.push_back(sr);
404}
405
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000406RegionStoreSubRegionMap*
407RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek451ac092009-08-06 04:50:20 +0000408 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek59e8f112009-03-03 01:35:36 +0000409 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
410
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000411 llvm::SmallVector<const SubRegion*, 10> WL;
412
Ted Kremenek451ac092009-08-06 04:50:20 +0000413 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000414 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
415 M->process(WL, R);
416
Ted Kremenek451ac092009-08-06 04:50:20 +0000417 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
418 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000419 I != E; ++I)
420 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
421 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000422
423 // We also need to record in the subregion map "intermediate" regions that
424 // don't have direct bindings but are super regions of those that do.
425 while (!WL.empty()) {
426 const SubRegion *R = WL.back();
427 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000428 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000429 }
430
Ted Kremenek14453bf2009-03-03 19:02:42 +0000431 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000432}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000433
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000434SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
435 return getRegionStoreSubRegionMap(state);
436}
437
Ted Kremenek9af46f52009-06-16 22:36:44 +0000438//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000439// Binding invalidation.
440//===----------------------------------------------------------------------===//
441
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000442void
Ted Kremenek451ac092009-08-06 04:50:20 +0000443RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
444 RegionDefaultBindings &DVM,
445 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000446 const MemRegion *R,
447 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000448
449 RegionStoreSubRegionMap::iterator I, E;
450
451 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000452 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000453
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000454 B = RBFactory.Remove(B, R);
455 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000456}
457
458
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000459const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
460 const MemRegion *R,
461 const Expr *E,
462 unsigned Count) {
463 ASTContext& Ctx = StateMgr.getContext();
464
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000465 // Strip away casts.
466 R = R->getBaseRegion();
467
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000468 // Remove the bindings to subregions.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000469 {
470 // Get the mapping of regions -> subregions.
471 llvm::OwningPtr<RegionStoreSubRegionMap>
472 SubRegions(getRegionStoreSubRegionMap(state));
473
Ted Kremenek451ac092009-08-06 04:50:20 +0000474 RegionBindings B = GetRegionBindings(state->getStore());
475 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
476 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000477 state->get_context<RegionDefaultValue>();
478
479 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
480 state = state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
481 }
482
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000483 if (!R->isBoundable())
484 return state;
485
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000486 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
487 isa<ObjCObjectRegion>(R)) {
488 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000489 // conjured symbol. The type of the symbol is irrelavant.
490 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000491 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000492 }
493
494 const TypedRegion *TR = cast<TypedRegion>(R);
495 QualType T = TR->getValueType(Ctx);
496
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000497 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000498 // FIXME: handle structs with default region value.
499 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
500
501 // No record definition. There is nothing we can do.
502 if (!RD)
503 return state;
504
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000505 // Invalidate the region by setting its default value to
506 // conjured symbol. The type of the symbol is irrelavant.
507 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
508 return setDefaultValue(state, R, V);
509 }
510
511 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000512 // Set the default value of the array to conjured symbol.
513 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
514 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000515 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000516 }
517
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000518 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
519 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
520 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000521}
522
523//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000524// getLValueXXX methods.
525//===----------------------------------------------------------------------===//
526
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000527/// getLValueString - Returns an SVal representing the lvalue of a
528/// StringLiteral. Within RegionStore a StringLiteral has an
529/// associated StringRegion, and the lvalue of a StringLiteral is the
530/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000531SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000532 const StringLiteral* S) {
533 return loc::MemRegionVal(MRMgr.getStringRegion(S));
534}
535
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000536/// getLValueVar - Returns an SVal that represents the lvalue of a
537/// variable. Within RegionStore a variable has an associated
538/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000539SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000540 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
541}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000542
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000543/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
544/// of a compound literal. Within RegionStore a compound literal
545/// has an associated region, and the lvalue of the compound literal
546/// is the lvalue of that region.
547SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000548RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000549 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000550 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
551}
552
Ted Kremenek67f28532009-06-17 22:02:04 +0000553SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000554 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000555 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000556}
557
Ted Kremenek67f28532009-06-17 22:02:04 +0000558SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000559 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000560 return getLValueFieldOrIvar(St, Base, D);
561}
562
Ted Kremenek67f28532009-06-17 22:02:04 +0000563SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000564 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000565 if (Base.isUnknownOrUndef())
566 return Base;
567
568 Loc BaseL = cast<Loc>(Base);
569 const MemRegion* BaseR = 0;
570
571 switch (BaseL.getSubKind()) {
572 case loc::MemRegionKind:
573 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
574 break;
575
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000576 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000577 // These are anormal cases. Flag an undefined value.
578 return UndefinedVal();
579
580 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000581 // While these seem funny, this can happen through casts.
582 // FIXME: What we should return is the field offset. For example,
583 // add the field offset to the integer value. That way funny things
584 // like this work properly: &(((struct foo *) 0xa)->f)
585 return Base;
586
587 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000588 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000589 return Base;
590 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000591
592 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
593 // of FieldDecl.
594 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
595 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000596
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000597 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000598}
599
Ted Kremenek67f28532009-06-17 22:02:04 +0000600SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000601 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000602 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000603
Ted Kremenekde7ec632009-03-09 22:44:49 +0000604 // If the base is an unknown or undefined value, just return it back.
605 // FIXME: For absolute pointer addresses, we just return that value back as
606 // well, although in reality we should return the offset added to that
607 // value.
608 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000609 return Base;
610
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000611 // Only handle integer offsets... for now.
612 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000613 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000614
Zhongxing Xuce760782009-05-09 13:20:07 +0000615 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000616
617 // Pointer of any type can be cast and used as array base.
618 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
619
Ted Kremenek46537392009-07-16 01:33:37 +0000620 // Convert the offset to the appropriate size and signedness.
621 Offset = ValMgr.convertToArrayIndex(Offset);
622
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000623 if (!ElemR) {
624 //
625 // If the base region is not an ElementRegion, create one.
626 // This can happen in the following example:
627 //
628 // char *p = __builtin_alloc(10);
629 // p[1] = 8;
630 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000631 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000632 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000633 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000634 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000635 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000636
637 SVal BaseIdx = ElemR->getIndex();
638
639 if (!isa<nonloc::ConcreteInt>(BaseIdx))
640 return UnknownVal();
641
642 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
643 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
644 assert(BaseIdxI.isSigned());
645
Ted Kremenek46537392009-07-16 01:33:37 +0000646 // Compute the new index.
647 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000648
Ted Kremenek46537392009-07-16 01:33:37 +0000649 // Construct the new ElementRegion.
650 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000651 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
652 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000653}
654
Ted Kremenek9af46f52009-06-16 22:36:44 +0000655//===----------------------------------------------------------------------===//
656// Extents for regions.
657//===----------------------------------------------------------------------===//
658
Ted Kremenek67f28532009-06-17 22:02:04 +0000659SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000660 const MemRegion *R) {
661
662 switch (R->getKind()) {
663 case MemRegion::MemSpaceRegionKind:
664 assert(0 && "Cannot index into a MemSpace");
665 return UnknownVal();
666
667 case MemRegion::CodeTextRegionKind:
668 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000669 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000670
671 // Not yet handled.
672 case MemRegion::AllocaRegionKind:
673 case MemRegion::CompoundLiteralRegionKind:
674 case MemRegion::ElementRegionKind:
675 case MemRegion::FieldRegionKind:
676 case MemRegion::ObjCIvarRegionKind:
677 case MemRegion::ObjCObjectRegionKind:
678 case MemRegion::SymbolicRegionKind:
679 return UnknownVal();
680
681 case MemRegion::StringRegionKind: {
682 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
683 // We intentionally made the size value signed because it participates in
684 // operations with signed indices.
685 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000686 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000687
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000688 case MemRegion::VarRegionKind: {
689 const VarRegion* VR = cast<VarRegion>(R);
690 // Get the type of the variable.
691 QualType T = VR->getDesugaredValueType(getContext());
692
693 // FIXME: Handle variable-length arrays.
694 if (isa<VariableArrayType>(T))
695 return UnknownVal();
696
697 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
698 // return the size as signed integer.
699 return ValMgr.makeIntVal(CAT->getSize(), false);
700 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000701
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000702 // Clients can use ordinary variables as if they were arrays. These
703 // essentially are arrays of size 1.
704 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000705 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000706
707 case MemRegion::BEG_DECL_REGIONS:
708 case MemRegion::END_DECL_REGIONS:
709 case MemRegion::BEG_TYPED_REGIONS:
710 case MemRegion::END_TYPED_REGIONS:
711 assert(0 && "Infeasible region");
712 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000713 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000714
715 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000716 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000717}
718
Ted Kremenek67f28532009-06-17 22:02:04 +0000719const GRState *RegionStoreManager::setExtent(const GRState *state,
720 const MemRegion *region,
721 SVal extent) {
722 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000723}
724
725//===----------------------------------------------------------------------===//
726// Location and region casting.
727//===----------------------------------------------------------------------===//
728
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000729/// ArrayToPointer - Emulates the "decay" of an array to a pointer
730/// type. 'Array' represents the lvalue of the array being decayed
731/// to a pointer, and the returned SVal represents the decayed
732/// version of that lvalue (i.e., a pointer to the first element of
733/// the array). This is called by GRExprEngine when evaluating casts
734/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000735SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000736 if (!isa<loc::MemRegionVal>(Array))
737 return UnknownVal();
738
739 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
740 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
741
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000742 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000743 return UnknownVal();
744
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000745 // Strip off typedefs from the ArrayRegion's ValueType.
746 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000747 ArrayType *AT = cast<ArrayType>(T);
748 T = AT->getElementType();
749
Ted Kremenek75185b52009-07-16 00:00:11 +0000750 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
751 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000752
753 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000754}
755
Ted Kremenek9af46f52009-06-16 22:36:44 +0000756//===----------------------------------------------------------------------===//
757// Pointer arithmetic.
758//===----------------------------------------------------------------------===//
759
Zhongxing Xu262fd032009-05-20 09:00:16 +0000760SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000761 BinaryOperator::Opcode Op, Loc L, NonLoc R,
762 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000763 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000764 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000765 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000766
Zhongxing Xua1718c72009-04-03 07:33:13 +0000767 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000768 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000769
Ted Kremenek3bccf082009-07-11 00:58:27 +0000770 switch (MR->getKind()) {
771 case MemRegion::SymbolicRegionKind: {
772 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000773 SymbolRef Sym = SR->getSymbol();
774 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000775 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000776 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
777 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
778 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000779 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000780 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000781 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000782 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000783 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000784 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
785 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
786 break;
787 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000788
Ted Kremenek3bccf082009-07-11 00:58:27 +0000789 case MemRegion::ElementRegionKind: {
790 ER = cast<ElementRegion>(MR);
791 break;
792 }
793
794 // Not yet handled.
795 case MemRegion::VarRegionKind:
796 case MemRegion::StringRegionKind:
797 case MemRegion::CompoundLiteralRegionKind:
798 case MemRegion::FieldRegionKind:
799 case MemRegion::ObjCObjectRegionKind:
800 case MemRegion::ObjCIvarRegionKind:
801 return UnknownVal();
802
Ted Kremenek3bccf082009-07-11 00:58:27 +0000803 case MemRegion::CodeTextRegionKind:
804 // Technically this can happen if people do funny things with casts.
805 return UnknownVal();
806
807 case MemRegion::MemSpaceRegionKind:
808 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
809 return UnknownVal();
810
811 case MemRegion::BEG_DECL_REGIONS:
812 case MemRegion::END_DECL_REGIONS:
813 case MemRegion::BEG_TYPED_REGIONS:
814 case MemRegion::END_TYPED_REGIONS:
815 assert(0 && "Infeasible region");
816 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000817 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000818
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000819 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000820 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
821 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
822
823 // Only support concrete integer indexes for now.
824 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000825 // FIXME: Should use SValuator here.
826 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
827 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000828 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000829 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
830 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000831 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000832 }
833
834 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000835}
836
Ted Kremenek9af46f52009-06-16 22:36:44 +0000837//===----------------------------------------------------------------------===//
838// Loading values from regions.
839//===----------------------------------------------------------------------===//
840
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000841Optional<SVal> RegionStoreManager::getDefaultBinding(const GRState *state,
842 const MemRegion *R) {
843
844 if (R->isBoundable())
845 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
846 if (TR->getValueType(getContext())->isUnionType())
847 return UnknownVal();
848
849 return Optional<SVal>::create(state->get<RegionDefaultValue>(R));
850}
851
Ted Kremeneka6275a52009-07-15 02:31:43 +0000852static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
853 RTy = Ctx.getCanonicalType(RTy);
854 UsedTy = Ctx.getCanonicalType(UsedTy);
855
856 if (RTy == UsedTy)
857 return false;
858
Ted Kremenek25c54572009-07-20 22:58:02 +0000859
860 // Recursively check the types. We basically want to see if a pointer value
861 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
862 // represents a reinterpretation.
863 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000864 const PointerType *PRTy = RTy->getAs<PointerType>();
865 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000866
867 return PUsedTy && PRTy &&
868 IsReinterpreted(PRTy->getPointeeType(),
869 PUsedTy->getPointeeType(), Ctx);
870 }
871
872 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000873}
874
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000875SValuator::CastResult
876RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000877
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000878 assert(!isa<UnknownVal>(L) && "location unknown");
879 assert(!isa<UndefinedVal>(L) && "location undefined");
880
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000881 // FIXME: Is this even possible? Shouldn't this be treated as a null
882 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000883 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000884 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000885
Ted Kremenek67f28532009-06-17 22:02:04 +0000886 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000887
Zhongxing Xu91844122009-05-20 09:18:48 +0000888 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000889 // Example:
890 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000891 // char* p = alloca();
892 // read(p);
893 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000894 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000895 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000896
897 if (isa<SymbolicRegion>(MR)) {
898 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000899 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000900 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000901 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
902 }
903
Ted Kremenek968f0a62009-08-03 21:41:46 +0000904 if (isa<CodeTextRegion>(MR))
905 return SValuator::CastResult(state, UnknownVal());
906
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000907 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
908 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000909 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000910 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000911
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000912 // FIXME: We should eventually handle funny addressing. e.g.:
913 //
914 // int x = ...;
915 // int *p = &x;
916 // char *q = (char*) p;
917 // char c = *q; // returns the first byte of 'x'.
918 //
919 // Such funny addressing will occur due to layering of regions.
920
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000921#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000922 ASTContext &Ctx = getContext();
923 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000924 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
925 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000926 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000927 assert(Ctx.getCanonicalType(RTy) ==
928 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000929 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000930#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000931
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000932 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000933 return SValuator::CastResult(state, RetrieveStruct(state, R));
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000934
935 // FIXME: Handle unions.
936 if (RTy->isUnionType())
937 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000938
939 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000940 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000941
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000942 // FIXME: handle Vector types.
943 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000944 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000945
946 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000947 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000948
949 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000950 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000951
Ted Kremenek25c54572009-07-20 22:58:02 +0000952 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000953 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000954
955 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000956 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000957
Ted Kremenek451ac092009-08-06 04:50:20 +0000958 RegionBindings B = GetRegionBindings(state->getStore());
959 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000960
961 // Check if the region has a binding.
962 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000963 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000964
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000965 // The location does not have a bound value. This means that it has
966 // the value it had upon its creation and/or entry to the analyzed
967 // function/method. These are either symbolic values or 'undefined'.
968
Ted Kremenek356e9d62009-07-22 04:35:42 +0000969#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000970 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000971#else
972 if (R->hasStackStorage()) {
973#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000974 // All stack variables are considered to have undefined values
975 // upon creation. All heap allocated blocks are considered to
976 // have undefined values as well unless they are explicitly bound
977 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000978 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000979 }
980
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000981 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000982 return SValuator::CastResult(state,
983 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000984}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000985
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000986std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000987RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000988
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000989 if (const nonloc::LazyCompoundVal *V =
990 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
991 return std::make_pair(V->getState(), V->getRegion());
992
993 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
994 const std::pair<const GRState *, const MemRegion *> &X =
995 GetLazyBinding(B, ER->getSuperRegion());
996
997 if (X.first)
998 return std::make_pair(X.first,
999 MRMgr.getElementRegionWithSuper(ER, X.second));
1000 }
1001 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1002 const std::pair<const GRState *, const MemRegion *> &X =
1003 GetLazyBinding(B, FR->getSuperRegion());
1004
1005 if (X.first)
1006 return std::make_pair(X.first,
1007 MRMgr.getFieldRegionWithSuper(FR, X.second));
1008 }
1009
1010 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1011}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001012
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001013SVal RegionStoreManager::RetrieveElement(const GRState* state,
1014 const ElementRegion* R) {
1015 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001016 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001017 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001018 return *V;
1019
Ted Kremenek921109a2009-07-01 23:19:52 +00001020 const MemRegion* superR = R->getSuperRegion();
1021
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001022 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001023 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001024 const StringLiteral *Str = StrR->getStringLiteral();
1025 SVal Idx = R->getIndex();
1026 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1027 int64_t i = CI->getValue().getSExtValue();
1028 char c;
1029 if (i == Str->getByteLength())
1030 c = '\0';
1031 else
1032 c = Str->getStrData()[i];
1033 return ValMgr.makeIntVal(c, getContext().CharTy);
1034 }
1035 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001036
1037 // Special case: the current region represents a cast and it and the super
1038 // region both have pointer types or intptr_t types. If so, perform the
1039 // retrieve from the super region and appropriately "cast" the value.
1040 // This is needed to support OSAtomicCompareAndSwap and friends or other
1041 // loads that treat integers as pointers and vis versa.
1042 if (R->getIndex().isZeroConstant()) {
1043 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1044 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001045 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1046 QualType valTy = R->getValueType(Ctx);
1047 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1048 // Retrieve the value from the super region. This will be casted to
1049 // valTy when we return to 'Retrieve'.
1050 const SValuator::CastResult &cr = Retrieve(state,
1051 loc::MemRegionVal(superR),
1052 valTy);
1053 return cr.getSVal();
1054 }
1055 }
1056 }
1057 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001058
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001059 // Check if the immediate super region has a direct binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001060 if (const SVal *V = B.lookup(superR)) {
1061 if (SymbolRef parentSym = V->getAsSymbol())
1062 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001063
1064 if (V->isUnknownOrUndef())
1065 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001066
1067 // Handle LazyCompoundVals for the immediate super region. Other cases
1068 // are handled in 'RetrieveFieldOrElementCommon'.
1069 if (const nonloc::LazyCompoundVal *LCV =
1070 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001071
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001072 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1073 return RetrieveElement(LCV->getState(), R);
1074 }
1075
Ted Kremeneka6275a52009-07-15 02:31:43 +00001076 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001077 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001078 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001079
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001080 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001081}
1082
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001083SVal RegionStoreManager::RetrieveField(const GRState* state,
1084 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001085
1086 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001087 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001088 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001089 return *V;
1090
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001091 QualType Ty = R->getValueType(getContext());
1092 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1093}
1094
1095SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1096 const TypedRegion *R,
1097 QualType Ty,
1098 const MemRegion *superR) {
1099
1100 // At this point we have already checked in either RetrieveElement or
1101 // RetrieveField if 'R' has a direct binding.
1102
1103 RegionBindings B = GetRegionBindings(state->getStore());
1104
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001105 while (superR) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001106 if (const Optional<SVal> &D = getDefaultBinding(state, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001107 if (SymbolRef parentSym = D->getAsSymbol())
1108 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001109
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001110 if (D->isZeroConstant())
1111 return ValMgr.makeZeroVal(Ty);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001112
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001113 if (D->isUnknown())
1114 return *D;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001115
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001116 assert(0 && "Unknown default value");
1117 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001118
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001119 // If our super region is a field or element itself, walk up the region
1120 // hierarchy to see if there is a default value installed in an ancestor.
1121 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1122 superR = cast<SubRegion>(superR)->getSuperRegion();
1123 continue;
1124 }
1125
1126 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001127 }
1128
1129 // Lazy binding?
1130 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001131 const MemRegion *lazyBindingRegion = NULL;
1132 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001133
1134 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001135 assert(lazyBindingRegion && "Lazy-binding region not set");
1136
1137 if (isa<ElementRegion>(R))
1138 return RetrieveElement(lazyBindingState,
1139 cast<ElementRegion>(lazyBindingRegion));
1140
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001141 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001142 cast<FieldRegion>(lazyBindingRegion));
1143 }
Ted Kremenekdc147262009-07-02 22:02:15 +00001144
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001145 if (R->hasStackStorage() && !R->hasParametersStorage()) {
1146
1147 if (isa<ElementRegion>(R)) {
1148 // Currently we don't reason specially about Clang-style vectors. Check
1149 // if superR is a vector and if so return Unknown.
1150 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1151 if (typedSuperR->getValueType(getContext())->isVectorType())
1152 return UnknownVal();
1153 }
1154 }
1155
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001156 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001157 }
1158
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001159 // All other values are symbolic.
1160 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001161}
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001162
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001163SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1164 const ObjCIvarRegion* R) {
1165
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001166 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001167 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001168
1169 if (const SVal* V = B.lookup(R))
1170 return *V;
1171
1172 const MemRegion *superR = R->getSuperRegion();
1173
1174 // Check if the super region has a binding.
1175 if (const SVal *V = B.lookup(superR)) {
1176 if (SymbolRef parentSym = V->getAsSymbol())
1177 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1178
1179 // Other cases: give up.
1180 return UnknownVal();
1181 }
1182
Ted Kremenek25c54572009-07-20 22:58:02 +00001183 return RetrieveLazySymbol(state, R);
1184}
1185
Ted Kremenek9031dd72009-07-21 00:12:07 +00001186SVal RegionStoreManager::RetrieveVar(const GRState *state,
1187 const VarRegion *R) {
1188
1189 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001190 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek9031dd72009-07-21 00:12:07 +00001191
1192 if (const SVal* V = B.lookup(R))
1193 return *V;
1194
1195 // Lazily derive a value for the VarRegion.
1196 const VarDecl *VD = R->getDecl();
1197
1198 if (VD == SelfDecl)
1199 return loc::MemRegionVal(getSelfRegion(0));
1200
1201 if (R->hasGlobalsOrParametersStorage())
1202 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1203
1204 return UndefinedVal();
1205}
1206
Ted Kremenek25c54572009-07-20 22:58:02 +00001207SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1208 const TypedRegion *R) {
1209
1210 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001211
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001212 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001213 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001214}
1215
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001216SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1217 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001218 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001219 assert(T->isStructureType());
1220
Zhongxing Xub7507d12009-06-11 07:27:30 +00001221 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001222 RecordDecl* RD = RT->getDecl();
1223 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001224 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001225#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001226 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1227
Ted Kremenek67f28532009-06-17 22:02:04 +00001228 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1229 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001230 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001231
Douglas Gregore267ff32008-12-11 20:41:00 +00001232 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1233 FieldEnd = Fields.rend();
1234 Field != FieldEnd; ++Field) {
1235 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001236 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001237 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001238 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1239 }
1240
Zhongxing Xud91ee272009-06-23 09:02:15 +00001241 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001242#else
1243 return ValMgr.makeLazyCompoundVal(state, R);
1244#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001245}
1246
Ted Kremenek67f28532009-06-17 22:02:04 +00001247SVal RegionStoreManager::RetrieveArray(const GRState *state,
1248 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001249#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001250 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001251 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1252
1253 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001254 uint64_t size = CAT->getSize().getZExtValue();
1255 for (uint64_t i = 0; i < size; ++i) {
1256 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001257 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1258 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001259 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001260 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001261 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1262 }
1263
Zhongxing Xud91ee272009-06-23 09:02:15 +00001264 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001265#else
1266 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1267 return ValMgr.makeLazyCompoundVal(state, R);
1268#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001269}
1270
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001271SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1272 const GRState *state,
1273 const TypedRegion *R,
1274 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001275 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001276 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001277
1278 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001279 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001280}
1281
Ted Kremenek9af46f52009-06-16 22:36:44 +00001282//===----------------------------------------------------------------------===//
1283// Binding values to regions.
1284//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001285
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001286Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001287 const MemRegion* R = 0;
1288
1289 if (isa<loc::MemRegionVal>(L))
1290 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001291
1292 if (R) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001293 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001294 return RBFactory.Remove(B, R).getRoot();
1295 }
1296
1297 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001298}
1299
Ted Kremenek67f28532009-06-17 22:02:04 +00001300const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001301 if (isa<loc::ConcreteInt>(L))
1302 return state;
1303
Ted Kremenek9af46f52009-06-16 22:36:44 +00001304 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001305 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001306
1307 // Check if the region is a struct region.
1308 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1309 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001310 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001311
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001312 // Special case: the current region represents a cast and it and the super
1313 // region both have pointer types or intptr_t types. If so, perform the
1314 // bind to the super region.
1315 // This is needed to support OSAtomicCompareAndSwap and friends or other
1316 // loads that treat integers as pointers and vis versa.
1317 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1318 if (ER->getIndex().isZeroConstant()) {
1319 if (const TypedRegion *superR =
1320 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1321 ASTContext &Ctx = getContext();
1322 QualType superTy = superR->getValueType(Ctx);
1323 QualType erTy = ER->getValueType(Ctx);
1324
1325 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1326 IsAnyPointerOrIntptr(erTy, Ctx)) {
1327 SValuator::CastResult cr =
1328 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1329 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1330 }
1331 }
1332 }
1333 }
1334
1335 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001336 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001337 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001338}
1339
Ted Kremenek67f28532009-06-17 22:02:04 +00001340const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001341 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001342
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001343 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001344 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001345
Ted Kremenek0964a062009-01-21 06:57:53 +00001346 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001347 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001348 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001349 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001350
Zhongxing Xud91ee272009-06-23 09:02:15 +00001351 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001352}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001353
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001354// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001355const GRState *
1356RegionStoreManager::BindCompoundLiteral(const GRState *state,
1357 const CompoundLiteralExpr* CL,
1358 SVal V) {
1359
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001360 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001361 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001362}
1363
Ted Kremenek67f28532009-06-17 22:02:04 +00001364const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001365 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001366 SVal Init) {
1367
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001368 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001369 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001370 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001371
Ted Kremenek46537392009-07-16 01:33:37 +00001372 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001373
1374 // Check if the init expr is a StringLiteral.
1375 if (isa<loc::MemRegionVal>(Init)) {
1376 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1377 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1378 const char* str = S->getStrData();
1379 unsigned len = S->getByteLength();
1380 unsigned j = 0;
1381
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001382 // Copy bytes from the string literal into the target array. Trailing bytes
1383 // in the array that are not covered by the string literal are initialized
1384 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001385 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001386 if (j >= len)
1387 break;
1388
Ted Kremenek46537392009-07-16 01:33:37 +00001389 SVal Idx = ValMgr.makeArrayIndex(i);
1390 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1391 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001392
Zhongxing Xud91ee272009-06-23 09:02:15 +00001393 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001394 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001395 }
1396
Ted Kremenek67f28532009-06-17 22:02:04 +00001397 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001398 }
1399
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001400 // Handle lazy compound values.
1401 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1402 return CopyLazyBindings(*LCV, state, R);
1403
1404 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001405 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001406 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001407 uint64_t i = 0;
1408
1409 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001410 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001411 if (VI == VE)
1412 break;
1413
Ted Kremenek46537392009-07-16 01:33:37 +00001414 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001415 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001416
1417 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001418 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001419 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001420 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001421 }
1422
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001423 // If the init list is shorter than the array length, set the array default
1424 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001425 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001426 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001427 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001428 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001429 }
1430 }
1431
Ted Kremenek67f28532009-06-17 22:02:04 +00001432 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001433}
1434
Ted Kremenek67f28532009-06-17 22:02:04 +00001435const GRState *
1436RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1437 SVal V) {
1438
1439 if (!Features.supportsFields())
1440 return state;
1441
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001442 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001443 assert(T->isStructureType());
1444
Ted Kremenek6217b802009-07-29 21:53:49 +00001445 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001446 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001447
1448 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001449 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001450
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001451 // Handle lazy compound values.
1452 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1453 return CopyLazyBindings(*LCV, state, R);
1454
Ted Kremenek67f28532009-06-17 22:02:04 +00001455 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1456 // Ignore them and kill the field values.
1457 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1458 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001459
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001460 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001461 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001462
1463 RecordDecl::field_iterator FI, FE;
1464
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001465 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001466
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001467 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001468 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001469
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001470 QualType FTy = (*FI)->getType();
1471 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1472
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001473 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001474 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001475 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001476 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001477 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001478 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001479 }
1480
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001481 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001482 if (FI != FE)
1483 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001484
Ted Kremenek67f28532009-06-17 22:02:04 +00001485 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001486}
1487
Ted Kremenek67f28532009-06-17 22:02:04 +00001488const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001489 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001490
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001491 // Set the default value of the struct region to "unknown".
1492 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001493
1494 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001495 Store store = state->getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001496 RegionBindings B = GetRegionBindings(store);
1497 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001498 const MemRegion* R = I.getKey();
1499 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1500 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001501 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001502 }
1503
Ted Kremenek67f28532009-06-17 22:02:04 +00001504 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001505}
1506
Ted Kremenek67f28532009-06-17 22:02:04 +00001507const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1508 const MemRegion* R, SVal V) {
1509 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001510}
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001511
1512const GRState*
1513RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1514 const GRState *state,
1515 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001516
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001517 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001518 RegionBindings B = GetRegionBindings(state->getStore());
1519 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
1520 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001521 state->get_context<RegionDefaultValue>();
1522
1523 llvm::OwningPtr<RegionStoreSubRegionMap>
1524 SubRegions(getRegionStoreSubRegionMap(state));
1525
1526 // B and DVM are updated after the call to RemoveSubRegionBindings.
1527 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
1528
1529 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1530 // results in a zero-copy algorithm.
1531 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1532}
1533
Ted Kremenek9af46f52009-06-16 22:36:44 +00001534//===----------------------------------------------------------------------===//
1535// State pruning.
1536//===----------------------------------------------------------------------===//
Ted Kremenek451ac092009-08-06 04:50:20 +00001537
Ted Kremenek9af46f52009-06-16 22:36:44 +00001538static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1539 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1540 const MemRegion *R = XR->getRegion();
1541
1542 while (R) {
1543 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1544 SymReaper.markLive(SR->getSymbol());
1545 return;
1546 }
1547
1548 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1549 R = SR->getSuperRegion();
1550 continue;
1551 }
1552
1553 break;
1554 }
1555
1556 return;
1557 }
1558
1559 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1560 SymReaper.markLive(*SI);
1561}
Ted Kremenek451ac092009-08-06 04:50:20 +00001562
1563namespace {
1564class VISIBILITY_HIDDEN TreeScanner {
1565 RegionBindings B;
1566 RegionDefaultBindings DB;
1567 SymbolReaper &SymReaper;
1568 llvm::DenseSet<const MemRegion*> &Marked;
1569 llvm::DenseSet<const LazyCompoundValData*> &ScannedLazyVals;
1570 RegionStoreSubRegionMap &M;
1571 RegionStoreManager &RS;
1572 llvm::SmallVectorImpl<const MemRegion*> &RegionRoots;
1573 const bool MarkKeys;
1574public:
1575 TreeScanner(RegionBindings b, RegionDefaultBindings db,
1576 SymbolReaper &symReaper,
1577 llvm::DenseSet<const MemRegion*> &marked,
1578 llvm::DenseSet<const LazyCompoundValData*> &scannedLazyVals,
1579 RegionStoreSubRegionMap &m, RegionStoreManager &rs,
1580 llvm::SmallVectorImpl<const MemRegion*> &regionRoots,
1581 bool markKeys = true)
1582 : B(b), DB(db), SymReaper(symReaper), Marked(marked),
1583 ScannedLazyVals(scannedLazyVals), M(m),
1584 RS(rs), RegionRoots(regionRoots), MarkKeys(markKeys) {}
1585
1586 void scanTree(const MemRegion *R);
1587};
1588} // end anonymous namespace
1589
1590
1591void TreeScanner::scanTree(const MemRegion *R) {
1592 if (MarkKeys) {
1593 if (Marked.count(R))
1594 return;
1595
1596 Marked.insert(R);
1597 }
1598
1599 // Mark the symbol for any live SymbolicRegion as "live". This means we
1600 // should continue to track that symbol.
1601 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1602 SymReaper.markLive(SymR->getSymbol());
1603
1604 // Get the data binding for R (if any).
1605 const SVal* Xptr = B.lookup(R);
1606
1607 // Check for lazy bindings.
1608 if (const nonloc::LazyCompoundVal *V =
1609 dyn_cast_or_null<nonloc::LazyCompoundVal>(Xptr)) {
1610
1611 const LazyCompoundValData *D = V->getCVData();
1612
1613 if (!ScannedLazyVals.count(D)) {
1614 // Scan the bindings in the LazyCompoundVal.
1615 ScannedLazyVals.insert(D);
1616
1617 // FIXME: Cache subregion maps.
1618 const GRState *lazyState = D->getState();
1619
1620 llvm::OwningPtr<RegionStoreSubRegionMap>
1621 lazySM(RS.getRegionStoreSubRegionMap(lazyState));
1622
1623 Store lazyStore = lazyState->getStore();
1624 RegionBindings lazyB = RS.GetRegionBindings(lazyStore);
1625
1626 RegionDefaultBindings lazyDB = lazyState->get<RegionDefaultValue>();
1627
1628 // Scan the bindings.
1629 TreeScanner scan(lazyB, lazyDB, SymReaper, Marked, ScannedLazyVals,
1630 *lazySM.get(), RS, RegionRoots, false);
1631
1632 scan.scanTree(D->getRegion());
1633 }
1634 }
1635 else {
1636 // No direct binding? Get the default binding for R (if any).
1637 if (!Xptr)
1638 Xptr = DB.lookup(R);
1639
1640 // Direct or default binding?
1641 if (Xptr) {
1642 SVal X = *Xptr;
1643 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1644
1645 // If X is a region, then add it to the RegionRoots.
1646 if (const MemRegion *RX = X.getAsRegion()) {
1647 RegionRoots.push_back(RX);
1648 // Mark the super region of the RX as live.
1649 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1650 // 'y' => element region. 'x' is its super region.
1651 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1652 RegionRoots.push_back(SR->getSuperRegion());
1653 }
1654 }
1655 }
1656 }
1657
1658 RegionStoreSubRegionMap::iterator I, E;
1659
1660 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
1661 scanTree(*I);
1662}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001663
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001664void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1665 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001666 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001667{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001668 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001669 RegionBindings B = GetRegionBindings(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001670
1671 // Lazily constructed backmap from MemRegions to SubRegions.
1672 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1673 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1674
Ted Kremenek9af46f52009-06-16 22:36:44 +00001675 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001676 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001677 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001678
1679 // Do a pass over the regions in the store. For VarRegions we check if
1680 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001681 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001682 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1683
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001684 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001685 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001686 const MemRegion *R = I.getKey();
1687 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001688 }
1689
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001690 // Scan the default bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001691 RegionDefaultBindings DVM = state.get<RegionDefaultValue>();
1692 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001693 I != E; ++I) {
1694 const MemRegion *R = I.getKey();
1695 IntermediateRoots.push_back(R);
1696 }
1697
1698 // Process the "intermediate" roots to find if they are referenced by
1699 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001700 while (!IntermediateRoots.empty()) {
1701 const MemRegion* R = IntermediateRoots.back();
1702 IntermediateRoots.pop_back();
1703
1704 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001705 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001706 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001707 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001708 continue;
1709 }
1710
1711 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001712 if (SymReaper.isLive(SR->getSymbol()))
1713 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001714 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001715 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001716
1717 // Add the super region for R to the worklist if it is a subregion.
1718 if (const SubRegion* superR =
1719 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1720 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001721 }
1722
1723 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1724 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek451ac092009-08-06 04:50:20 +00001725 llvm::DenseSet<const MemRegion*> Marked;
1726 llvm::DenseSet<const LazyCompoundValData*> LazyVals;
1727 TreeScanner TS(B, DVM, SymReaper, Marked, LazyVals, *SubRegions.get(),
1728 *this, RegionRoots);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001729
Ted Kremenek451ac092009-08-06 04:50:20 +00001730 while (!RegionRoots.empty()) {
1731 const MemRegion *R = RegionRoots.back();
1732 RegionRoots.pop_back();
1733 TS.scanTree(R);
1734 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001735
Ted Kremenek9af46f52009-06-16 22:36:44 +00001736 // We have now scanned the store, marking reachable regions and symbols
1737 // as live. We now remove all the regions that are dead from the store
1738 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001739 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001740 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001741 // If this region live? Is so, none of its symbols are dead.
1742 if (Marked.count(R))
1743 continue;
1744
1745 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001746 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001747
1748 // Mark all non-live symbols that this region references as dead.
1749 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1750 SymReaper.maybeDead(SymR->getSymbol());
1751
1752 SVal X = I.getData();
1753 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001754 for (; SI != SE; ++SI)
1755 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001756 }
1757
Ted Kremenek093569c2009-08-02 05:00:15 +00001758 // Remove dead 'default' bindings.
Ted Kremenek451ac092009-08-06 04:50:20 +00001759 RegionDefaultBindings NewDVM = DVM;
1760 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremenek093569c2009-08-02 05:00:15 +00001761 state.get_context<RegionDefaultValue>();
1762
Ted Kremenek451ac092009-08-06 04:50:20 +00001763 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek093569c2009-08-02 05:00:15 +00001764 I != E; ++I) {
1765 const MemRegion *R = I.getKey();
1766
1767 // If this region live? Is so, none of its symbols are dead.
1768 if (Marked.count(R))
1769 continue;
1770
1771 // Remove this dead region.
1772 NewDVM = DVMFactory.Remove(NewDVM, R);
1773
1774 // Mark all non-live symbols that this region references as dead.
1775 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1776 SymReaper.maybeDead(SymR->getSymbol());
1777
1778 SVal X = I.getData();
1779 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1780 for (; SI != SE; ++SI)
1781 SymReaper.maybeDead(*SI);
1782 }
1783
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001784 // Write the store back.
1785 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001786
1787 // Write the updated default bindings back.
1788 // FIXME: Right now this involves a fetching of a persistent state.
1789 // We can do better.
1790 if (DVM != NewDVM)
1791 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001792}
1793
1794//===----------------------------------------------------------------------===//
1795// Utility methods.
1796//===----------------------------------------------------------------------===//
1797
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001798void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001799 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001800 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001801 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001802
Ted Kremenek451ac092009-08-06 04:50:20 +00001803 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001804 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001805}