blob: 8cb4d980214fad21ea950715b778f2f50e2697f5 [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 Kremenek32c3fa42009-07-21 21:03:30 +0000318 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
319 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000320
Ted Kremenek67f28532009-06-17 22:02:04 +0000321 /// Retrieve the values in a struct and return a CompoundVal, used when doing
322 /// struct copy:
323 /// struct s x, y;
324 /// x = y;
325 /// y's value is retrieved by this method.
326 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
327
328 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000329
330 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000331 GetLazyBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000332
333 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
334 const GRState *state,
335 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000336
337 //===------------------------------------------------------------------===//
338 // State pruning.
339 //===------------------------------------------------------------------===//
340
341 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
342 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000343 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000344 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
345
346 //===------------------------------------------------------------------===//
347 // Region "extents".
348 //===------------------------------------------------------------------===//
349
350 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
351 SVal getSizeInElements(const GRState *state, const MemRegion* R);
352
353 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000354 // Utility methods.
355 //===------------------------------------------------------------------===//
356
Ted Kremenek451ac092009-08-06 04:50:20 +0000357 static inline RegionBindings GetRegionBindings(Store store) {
358 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000359 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000360
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000361 void print(Store store, llvm::raw_ostream& Out, const char* nl,
362 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000363
364 void iterBindings(Store store, BindingsHandler& f) {
365 // FIXME: Implement.
366 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000367
Ted Kremenek67f28532009-06-17 22:02:04 +0000368 // FIXME: Remove.
369 BasicValueFactory& getBasicVals() {
370 return StateMgr.getBasicVals();
371 }
372
373 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000374 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000375};
376
377} // end anonymous namespace
378
Ted Kremenek9af46f52009-06-16 22:36:44 +0000379//===----------------------------------------------------------------------===//
380// RegionStore creation.
381//===----------------------------------------------------------------------===//
382
383StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
384 RegionStoreFeatures F = maximal_features_tag();
385 return new RegionStoreManager(StMgr, F);
386}
387
388StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
389 RegionStoreFeatures F = minimal_features_tag();
390 F.enableFields(true);
391 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000392}
393
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000394void
395RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
396 const SubRegion *R) {
397 const MemRegion *superR = R->getSuperRegion();
398 if (add(superR, R))
399 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
400 WL.push_back(sr);
401}
402
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000403RegionStoreSubRegionMap*
404RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek451ac092009-08-06 04:50:20 +0000405 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek59e8f112009-03-03 01:35:36 +0000406 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
407
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000408 llvm::SmallVector<const SubRegion*, 10> WL;
409
Ted Kremenek451ac092009-08-06 04:50:20 +0000410 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000411 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
412 M->process(WL, R);
413
Ted Kremenek451ac092009-08-06 04:50:20 +0000414 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
415 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000416 I != E; ++I)
417 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
418 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000419
420 // We also need to record in the subregion map "intermediate" regions that
421 // don't have direct bindings but are super regions of those that do.
422 while (!WL.empty()) {
423 const SubRegion *R = WL.back();
424 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000425 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000426 }
427
Ted Kremenek14453bf2009-03-03 19:02:42 +0000428 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000429}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000430
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000431SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
432 return getRegionStoreSubRegionMap(state);
433}
434
Ted Kremenek9af46f52009-06-16 22:36:44 +0000435//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000436// Binding invalidation.
437//===----------------------------------------------------------------------===//
438
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000439void
Ted Kremenek451ac092009-08-06 04:50:20 +0000440RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
441 RegionDefaultBindings &DVM,
442 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000443 const MemRegion *R,
444 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000445
446 RegionStoreSubRegionMap::iterator I, E;
447
448 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000449 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000450
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000451 B = RBFactory.Remove(B, R);
452 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000453}
454
455
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000456const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
457 const MemRegion *R,
458 const Expr *E,
459 unsigned Count) {
460 ASTContext& Ctx = StateMgr.getContext();
461
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000462 // Strip away casts.
463 R = R->getBaseRegion();
464
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000465 // Remove the bindings to subregions.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000466 {
467 // Get the mapping of regions -> subregions.
468 llvm::OwningPtr<RegionStoreSubRegionMap>
469 SubRegions(getRegionStoreSubRegionMap(state));
470
Ted Kremenek451ac092009-08-06 04:50:20 +0000471 RegionBindings B = GetRegionBindings(state->getStore());
472 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
473 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000474 state->get_context<RegionDefaultValue>();
475
476 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
477 state = state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
478 }
479
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000480 if (!R->isBoundable())
481 return state;
482
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000483 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
484 isa<ObjCObjectRegion>(R)) {
485 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000486 // conjured symbol. The type of the symbol is irrelavant.
487 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000488 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000489 }
490
491 const TypedRegion *TR = cast<TypedRegion>(R);
492 QualType T = TR->getValueType(Ctx);
493
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000494 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000495 // FIXME: handle structs with default region value.
496 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
497
498 // No record definition. There is nothing we can do.
499 if (!RD)
500 return state;
501
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000502 // Invalidate the region by setting its default value to
503 // conjured symbol. The type of the symbol is irrelavant.
504 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
505 return setDefaultValue(state, R, V);
506 }
507
508 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000509 // Set the default value of the array to conjured symbol.
510 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
511 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000512 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000513 }
514
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000515 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
516 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
517 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000518}
519
520//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000521// getLValueXXX methods.
522//===----------------------------------------------------------------------===//
523
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000524/// getLValueString - Returns an SVal representing the lvalue of a
525/// StringLiteral. Within RegionStore a StringLiteral has an
526/// associated StringRegion, and the lvalue of a StringLiteral is the
527/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000528SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000529 const StringLiteral* S) {
530 return loc::MemRegionVal(MRMgr.getStringRegion(S));
531}
532
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000533/// getLValueVar - Returns an SVal that represents the lvalue of a
534/// variable. Within RegionStore a variable has an associated
535/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000536SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000537 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
538}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000539
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000540/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
541/// of a compound literal. Within RegionStore a compound literal
542/// has an associated region, and the lvalue of the compound literal
543/// is the lvalue of that region.
544SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000545RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000546 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000547 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
548}
549
Ted Kremenek67f28532009-06-17 22:02:04 +0000550SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000551 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000552 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000553}
554
Ted Kremenek67f28532009-06-17 22:02:04 +0000555SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000556 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000557 return getLValueFieldOrIvar(St, Base, D);
558}
559
Ted Kremenek67f28532009-06-17 22:02:04 +0000560SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000561 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000562 if (Base.isUnknownOrUndef())
563 return Base;
564
565 Loc BaseL = cast<Loc>(Base);
566 const MemRegion* BaseR = 0;
567
568 switch (BaseL.getSubKind()) {
569 case loc::MemRegionKind:
570 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
571 break;
572
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000573 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000574 // These are anormal cases. Flag an undefined value.
575 return UndefinedVal();
576
577 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000578 // While these seem funny, this can happen through casts.
579 // FIXME: What we should return is the field offset. For example,
580 // add the field offset to the integer value. That way funny things
581 // like this work properly: &(((struct foo *) 0xa)->f)
582 return Base;
583
584 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000585 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000586 return Base;
587 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000588
589 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
590 // of FieldDecl.
591 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
592 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000593
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000594 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000595}
596
Ted Kremenek67f28532009-06-17 22:02:04 +0000597SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000598 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000599 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000600
Ted Kremenekde7ec632009-03-09 22:44:49 +0000601 // If the base is an unknown or undefined value, just return it back.
602 // FIXME: For absolute pointer addresses, we just return that value back as
603 // well, although in reality we should return the offset added to that
604 // value.
605 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000606 return Base;
607
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000608 // Only handle integer offsets... for now.
609 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000610 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000611
Zhongxing Xuce760782009-05-09 13:20:07 +0000612 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000613
614 // Pointer of any type can be cast and used as array base.
615 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
616
Ted Kremenek46537392009-07-16 01:33:37 +0000617 // Convert the offset to the appropriate size and signedness.
618 Offset = ValMgr.convertToArrayIndex(Offset);
619
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000620 if (!ElemR) {
621 //
622 // If the base region is not an ElementRegion, create one.
623 // This can happen in the following example:
624 //
625 // char *p = __builtin_alloc(10);
626 // p[1] = 8;
627 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000628 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000629 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000630 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000631 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000632 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000633
634 SVal BaseIdx = ElemR->getIndex();
635
636 if (!isa<nonloc::ConcreteInt>(BaseIdx))
637 return UnknownVal();
638
639 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
640 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
641 assert(BaseIdxI.isSigned());
642
Ted Kremenek46537392009-07-16 01:33:37 +0000643 // Compute the new index.
644 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000645
Ted Kremenek46537392009-07-16 01:33:37 +0000646 // Construct the new ElementRegion.
647 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000648 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
649 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000650}
651
Ted Kremenek9af46f52009-06-16 22:36:44 +0000652//===----------------------------------------------------------------------===//
653// Extents for regions.
654//===----------------------------------------------------------------------===//
655
Ted Kremenek67f28532009-06-17 22:02:04 +0000656SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000657 const MemRegion *R) {
658
659 switch (R->getKind()) {
660 case MemRegion::MemSpaceRegionKind:
661 assert(0 && "Cannot index into a MemSpace");
662 return UnknownVal();
663
664 case MemRegion::CodeTextRegionKind:
665 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000666 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000667
668 // Not yet handled.
669 case MemRegion::AllocaRegionKind:
670 case MemRegion::CompoundLiteralRegionKind:
671 case MemRegion::ElementRegionKind:
672 case MemRegion::FieldRegionKind:
673 case MemRegion::ObjCIvarRegionKind:
674 case MemRegion::ObjCObjectRegionKind:
675 case MemRegion::SymbolicRegionKind:
676 return UnknownVal();
677
678 case MemRegion::StringRegionKind: {
679 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
680 // We intentionally made the size value signed because it participates in
681 // operations with signed indices.
682 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000683 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000684
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000685 case MemRegion::VarRegionKind: {
686 const VarRegion* VR = cast<VarRegion>(R);
687 // Get the type of the variable.
688 QualType T = VR->getDesugaredValueType(getContext());
689
690 // FIXME: Handle variable-length arrays.
691 if (isa<VariableArrayType>(T))
692 return UnknownVal();
693
694 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
695 // return the size as signed integer.
696 return ValMgr.makeIntVal(CAT->getSize(), false);
697 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000698
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000699 // Clients can use ordinary variables as if they were arrays. These
700 // essentially are arrays of size 1.
701 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000702 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000703
704 case MemRegion::BEG_DECL_REGIONS:
705 case MemRegion::END_DECL_REGIONS:
706 case MemRegion::BEG_TYPED_REGIONS:
707 case MemRegion::END_TYPED_REGIONS:
708 assert(0 && "Infeasible region");
709 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000710 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000711
712 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000713 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000714}
715
Ted Kremenek67f28532009-06-17 22:02:04 +0000716const GRState *RegionStoreManager::setExtent(const GRState *state,
717 const MemRegion *region,
718 SVal extent) {
719 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000720}
721
722//===----------------------------------------------------------------------===//
723// Location and region casting.
724//===----------------------------------------------------------------------===//
725
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000726/// ArrayToPointer - Emulates the "decay" of an array to a pointer
727/// type. 'Array' represents the lvalue of the array being decayed
728/// to a pointer, and the returned SVal represents the decayed
729/// version of that lvalue (i.e., a pointer to the first element of
730/// the array). This is called by GRExprEngine when evaluating casts
731/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000732SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000733 if (!isa<loc::MemRegionVal>(Array))
734 return UnknownVal();
735
736 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
737 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
738
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000739 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000740 return UnknownVal();
741
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000742 // Strip off typedefs from the ArrayRegion's ValueType.
743 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000744 ArrayType *AT = cast<ArrayType>(T);
745 T = AT->getElementType();
746
Ted Kremenek75185b52009-07-16 00:00:11 +0000747 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
748 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000749
750 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000751}
752
Ted Kremenek9af46f52009-06-16 22:36:44 +0000753//===----------------------------------------------------------------------===//
754// Pointer arithmetic.
755//===----------------------------------------------------------------------===//
756
Zhongxing Xu262fd032009-05-20 09:00:16 +0000757SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000758 BinaryOperator::Opcode Op, Loc L, NonLoc R,
759 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000760 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000761 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000762 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000763
Zhongxing Xua1718c72009-04-03 07:33:13 +0000764 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000765 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000766
Ted Kremenek3bccf082009-07-11 00:58:27 +0000767 switch (MR->getKind()) {
768 case MemRegion::SymbolicRegionKind: {
769 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000770 SymbolRef Sym = SR->getSymbol();
771 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000772 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000773 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
774 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
775 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000776 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000777 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000778 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000779 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000780 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000781 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
782 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
783 break;
784 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000785
Ted Kremenek3bccf082009-07-11 00:58:27 +0000786 case MemRegion::ElementRegionKind: {
787 ER = cast<ElementRegion>(MR);
788 break;
789 }
790
791 // Not yet handled.
792 case MemRegion::VarRegionKind:
793 case MemRegion::StringRegionKind:
794 case MemRegion::CompoundLiteralRegionKind:
795 case MemRegion::FieldRegionKind:
796 case MemRegion::ObjCObjectRegionKind:
797 case MemRegion::ObjCIvarRegionKind:
798 return UnknownVal();
799
Ted Kremenek3bccf082009-07-11 00:58:27 +0000800 case MemRegion::CodeTextRegionKind:
801 // Technically this can happen if people do funny things with casts.
802 return UnknownVal();
803
804 case MemRegion::MemSpaceRegionKind:
805 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
806 return UnknownVal();
807
808 case MemRegion::BEG_DECL_REGIONS:
809 case MemRegion::END_DECL_REGIONS:
810 case MemRegion::BEG_TYPED_REGIONS:
811 case MemRegion::END_TYPED_REGIONS:
812 assert(0 && "Infeasible region");
813 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000814 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000815
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000816 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000817 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
818 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
819
820 // Only support concrete integer indexes for now.
821 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000822 // FIXME: Should use SValuator here.
823 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
824 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000825 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000826 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
827 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000828 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000829 }
830
831 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000832}
833
Ted Kremenek9af46f52009-06-16 22:36:44 +0000834//===----------------------------------------------------------------------===//
835// Loading values from regions.
836//===----------------------------------------------------------------------===//
837
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000838Optional<SVal> RegionStoreManager::getDefaultBinding(const GRState *state,
839 const MemRegion *R) {
840
841 if (R->isBoundable())
842 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
843 if (TR->getValueType(getContext())->isUnionType())
844 return UnknownVal();
845
846 return Optional<SVal>::create(state->get<RegionDefaultValue>(R));
847}
848
Ted Kremeneka6275a52009-07-15 02:31:43 +0000849static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
850 RTy = Ctx.getCanonicalType(RTy);
851 UsedTy = Ctx.getCanonicalType(UsedTy);
852
853 if (RTy == UsedTy)
854 return false;
855
Ted Kremenek25c54572009-07-20 22:58:02 +0000856
857 // Recursively check the types. We basically want to see if a pointer value
858 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
859 // represents a reinterpretation.
860 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000861 const PointerType *PRTy = RTy->getAs<PointerType>();
862 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000863
864 return PUsedTy && PRTy &&
865 IsReinterpreted(PRTy->getPointeeType(),
866 PUsedTy->getPointeeType(), Ctx);
867 }
868
869 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000870}
871
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000872SValuator::CastResult
873RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000874
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000875 assert(!isa<UnknownVal>(L) && "location unknown");
876 assert(!isa<UndefinedVal>(L) && "location undefined");
877
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000878 // FIXME: Is this even possible? Shouldn't this be treated as a null
879 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000880 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000881 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000882
Ted Kremenek67f28532009-06-17 22:02:04 +0000883 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000884
Zhongxing Xu91844122009-05-20 09:18:48 +0000885 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000886 // Example:
887 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000888 // char* p = alloca();
889 // read(p);
890 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000891 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000892 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000893
894 if (isa<SymbolicRegion>(MR)) {
895 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000896 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000897 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000898 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
899 }
900
Ted Kremenek968f0a62009-08-03 21:41:46 +0000901 if (isa<CodeTextRegion>(MR))
902 return SValuator::CastResult(state, UnknownVal());
903
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000904 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
905 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000906 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000907 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000908
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000909 // FIXME: We should eventually handle funny addressing. e.g.:
910 //
911 // int x = ...;
912 // int *p = &x;
913 // char *q = (char*) p;
914 // char c = *q; // returns the first byte of 'x'.
915 //
916 // Such funny addressing will occur due to layering of regions.
917
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000918#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000919 ASTContext &Ctx = getContext();
920 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000921 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
922 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000923 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000924 assert(Ctx.getCanonicalType(RTy) ==
925 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000926 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000927#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000928
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000929 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000930 return SValuator::CastResult(state, RetrieveStruct(state, R));
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000931
932 // FIXME: Handle unions.
933 if (RTy->isUnionType())
934 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000935
936 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000937 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000938
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000939 // FIXME: handle Vector types.
940 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000941 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000942
943 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000944 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000945
946 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000947 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000948
Ted Kremenek25c54572009-07-20 22:58:02 +0000949 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000950 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000951
952 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000953 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000954
Ted Kremenek451ac092009-08-06 04:50:20 +0000955 RegionBindings B = GetRegionBindings(state->getStore());
956 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000957
958 // Check if the region has a binding.
959 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000960 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000961
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000962 // The location does not have a bound value. This means that it has
963 // the value it had upon its creation and/or entry to the analyzed
964 // function/method. These are either symbolic values or 'undefined'.
965
Ted Kremenek356e9d62009-07-22 04:35:42 +0000966#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000967 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000968#else
969 if (R->hasStackStorage()) {
970#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000971 // All stack variables are considered to have undefined values
972 // upon creation. All heap allocated blocks are considered to
973 // have undefined values as well unless they are explicitly bound
974 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000975 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000976 }
977
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000978 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000979 return SValuator::CastResult(state,
980 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000981}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000982
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000983std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000984RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000985
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000986 if (const nonloc::LazyCompoundVal *V =
987 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
988 return std::make_pair(V->getState(), V->getRegion());
989
990 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
991 const std::pair<const GRState *, const MemRegion *> &X =
992 GetLazyBinding(B, ER->getSuperRegion());
993
994 if (X.first)
995 return std::make_pair(X.first,
996 MRMgr.getElementRegionWithSuper(ER, X.second));
997 }
998 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
999 const std::pair<const GRState *, const MemRegion *> &X =
1000 GetLazyBinding(B, FR->getSuperRegion());
1001
1002 if (X.first)
1003 return std::make_pair(X.first,
1004 MRMgr.getFieldRegionWithSuper(FR, X.second));
1005 }
1006
1007 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1008}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001009
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001010SVal RegionStoreManager::RetrieveElement(const GRState* state,
1011 const ElementRegion* R) {
1012 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001013 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001014 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001015 return *V;
1016
Ted Kremenek921109a2009-07-01 23:19:52 +00001017 const MemRegion* superR = R->getSuperRegion();
1018
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001019 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001020 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001021 const StringLiteral *Str = StrR->getStringLiteral();
1022 SVal Idx = R->getIndex();
1023 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1024 int64_t i = CI->getValue().getSExtValue();
1025 char c;
1026 if (i == Str->getByteLength())
1027 c = '\0';
1028 else
1029 c = Str->getStrData()[i];
1030 return ValMgr.makeIntVal(c, getContext().CharTy);
1031 }
1032 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001033
1034 // Special case: the current region represents a cast and it and the super
1035 // region both have pointer types or intptr_t types. If so, perform the
1036 // retrieve from the super region and appropriately "cast" the value.
1037 // This is needed to support OSAtomicCompareAndSwap and friends or other
1038 // loads that treat integers as pointers and vis versa.
1039 if (R->getIndex().isZeroConstant()) {
1040 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1041 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001042 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1043 QualType valTy = R->getValueType(Ctx);
1044 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1045 // Retrieve the value from the super region. This will be casted to
1046 // valTy when we return to 'Retrieve'.
1047 const SValuator::CastResult &cr = Retrieve(state,
1048 loc::MemRegionVal(superR),
1049 valTy);
1050 return cr.getSVal();
1051 }
1052 }
1053 }
1054 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001055
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001056 // Check if the super region has a default value.
Ted Kremenek921109a2009-07-01 23:19:52 +00001057 if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001058 if (D->hasConjuredSymbol())
1059 return ValMgr.getRegionValueSymbolVal(R);
1060 else
1061 return *D;
1062 }
1063
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001064 // Check if the super region has a binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001065 if (const SVal *V = B.lookup(superR)) {
1066 if (SymbolRef parentSym = V->getAsSymbol())
1067 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001068
1069 if (V->isUnknownOrUndef())
1070 return *V;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001071
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001072 // Handle LazyCompoundVals below.
1073 if (const nonloc::LazyCompoundVal *LVC =
1074 dyn_cast<nonloc::LazyCompoundVal>(V)) {
1075 return RetrieveElement(LVC->getState(),
1076 MRMgr.getElementRegionWithSuper(R,
1077 LVC->getRegion()));
1078 }
1079
Ted Kremeneka6275a52009-07-15 02:31:43 +00001080 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001081 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001082 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001083
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001084 // Lazy binding?
1085 const GRState *lazyBindingState = NULL;
1086 const MemRegion *LazyBindingRegion = NULL;
1087 llvm::tie(lazyBindingState, LazyBindingRegion) = GetLazyBinding(B, R);
1088
1089 if (lazyBindingState) {
1090 assert(LazyBindingRegion && "Lazy-binding region not set");
1091 return RetrieveElement(lazyBindingState,
1092 cast<ElementRegion>(LazyBindingRegion));
1093 }
1094
1095 // Default value cases.
Ted Kremenek356e9d62009-07-22 04:35:42 +00001096#if 0
Ted Kremenek921109a2009-07-01 23:19:52 +00001097 if (R->hasHeapStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001098 // FIXME: If the region has heap storage and we know nothing special
1099 // about its bindings, should we instead return UnknownVal? Seems like
1100 // we should only return UndefinedVal in the cases where we know the value
1101 // will be undefined.
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001102 return UndefinedVal();
Ted Kremenek921109a2009-07-01 23:19:52 +00001103 }
Ted Kremenek356e9d62009-07-22 04:35:42 +00001104#endif
1105
Ted Kremenekdc147262009-07-02 22:02:15 +00001106 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Ted Kremenek921109a2009-07-01 23:19:52 +00001107 // Currently we don't reason specially about Clang-style vectors. Check
1108 // if superR is a vector and if so return Unknown.
1109 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1110 if (typedSuperR->getValueType(getContext())->isVectorType())
1111 return UnknownVal();
1112 }
1113
1114 return UndefinedVal();
1115 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001116
1117 QualType Ty = R->getValueType(getContext());
1118
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001119 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001120}
1121
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001122SVal RegionStoreManager::RetrieveField(const GRState* state,
1123 const FieldRegion* R) {
1124 QualType Ty = R->getValueType(getContext());
1125
1126 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001127 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001128 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001129 return *V;
1130
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001131 const MemRegion* superR = R->getSuperRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001132 while (superR) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001133 if (const Optional<SVal> &D = getDefaultBinding(state, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001134 if (SymbolRef parentSym = D->getAsSymbol())
1135 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001136
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001137 if (D->isZeroConstant())
1138 return ValMgr.makeZeroVal(Ty);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001139
1140 if (const nonloc::LazyCompoundVal *LCV =
1141 dyn_cast<nonloc::LazyCompoundVal>(D)) {
1142 const FieldRegion *FR =
1143 MRMgr.getFieldRegionWithSuper(R, LCV->getRegion());
1144 return RetrieveField(LCV->getState(), FR);
1145 }
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001146
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001147 if (D->isUnknown())
1148 return *D;
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001149
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001150 assert(0 && "Unknown default value");
1151 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001152
1153 if (const SVal *V = B.lookup(superR)) {
1154 // Handle LazyCompoundVals below.
1155 if (isa<nonloc::CompoundVal>(*V))
1156 break;
1157 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001158
1159 // If our super region is a field or element itself, walk up the region
1160 // hierarchy to see if there is a default value installed in an ancestor.
1161 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1162 superR = cast<SubRegion>(superR)->getSuperRegion();
1163 continue;
1164 }
1165
1166 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001167 }
1168
1169 // Lazy binding?
1170 const GRState *lazyBindingState = NULL;
1171 const MemRegion *LazyBindingRegion = NULL;
1172 llvm::tie(lazyBindingState, LazyBindingRegion) = GetLazyBinding(B, R);
1173
1174 if (lazyBindingState) {
1175 assert(LazyBindingRegion && "Lazy-binding region not set");
1176 return RetrieveField(lazyBindingState,
1177 cast<FieldRegion>(LazyBindingRegion));
1178 }
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001179
Ted Kremenek356e9d62009-07-22 04:35:42 +00001180#if HEAP_UNDEFINED
Ted Kremenekdc147262009-07-02 22:02:15 +00001181 // FIXME: Is this correct? Should it be UnknownVal?
1182 if (R->hasHeapStorage())
1183 return UndefinedVal();
Ted Kremenek356e9d62009-07-22 04:35:42 +00001184#endif
Ted Kremenekdc147262009-07-02 22:02:15 +00001185
1186 if (R->hasStackStorage() && !R->hasParametersStorage())
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001187 return UndefinedVal();
1188
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001189 // All other values are symbolic.
1190 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001191}
1192
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001193SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1194 const ObjCIvarRegion* R) {
1195
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001196 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001197 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001198
1199 if (const SVal* V = B.lookup(R))
1200 return *V;
1201
1202 const MemRegion *superR = R->getSuperRegion();
1203
1204 // Check if the super region has a binding.
1205 if (const SVal *V = B.lookup(superR)) {
1206 if (SymbolRef parentSym = V->getAsSymbol())
1207 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1208
1209 // Other cases: give up.
1210 return UnknownVal();
1211 }
1212
Ted Kremenek25c54572009-07-20 22:58:02 +00001213 return RetrieveLazySymbol(state, R);
1214}
1215
Ted Kremenek9031dd72009-07-21 00:12:07 +00001216SVal RegionStoreManager::RetrieveVar(const GRState *state,
1217 const VarRegion *R) {
1218
1219 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001220 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek9031dd72009-07-21 00:12:07 +00001221
1222 if (const SVal* V = B.lookup(R))
1223 return *V;
1224
1225 // Lazily derive a value for the VarRegion.
1226 const VarDecl *VD = R->getDecl();
1227
1228 if (VD == SelfDecl)
1229 return loc::MemRegionVal(getSelfRegion(0));
1230
1231 if (R->hasGlobalsOrParametersStorage())
1232 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1233
1234 return UndefinedVal();
1235}
1236
Ted Kremenek25c54572009-07-20 22:58:02 +00001237SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1238 const TypedRegion *R) {
1239
1240 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001241
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001242 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001243 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001244}
1245
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001246SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1247 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001248 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001249 assert(T->isStructureType());
1250
Zhongxing Xub7507d12009-06-11 07:27:30 +00001251 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001252 RecordDecl* RD = RT->getDecl();
1253 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001254 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001255#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001256 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1257
Ted Kremenek67f28532009-06-17 22:02:04 +00001258 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1259 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001260 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001261
Douglas Gregore267ff32008-12-11 20:41:00 +00001262 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1263 FieldEnd = Fields.rend();
1264 Field != FieldEnd; ++Field) {
1265 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001266 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001267 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001268 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1269 }
1270
Zhongxing Xud91ee272009-06-23 09:02:15 +00001271 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001272#else
1273 return ValMgr.makeLazyCompoundVal(state, R);
1274#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001275}
1276
Ted Kremenek67f28532009-06-17 22:02:04 +00001277SVal RegionStoreManager::RetrieveArray(const GRState *state,
1278 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001279#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001280 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001281 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1282
1283 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001284 uint64_t size = CAT->getSize().getZExtValue();
1285 for (uint64_t i = 0; i < size; ++i) {
1286 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001287 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1288 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001289 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001290 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001291 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1292 }
1293
Zhongxing Xud91ee272009-06-23 09:02:15 +00001294 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001295#else
1296 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1297 return ValMgr.makeLazyCompoundVal(state, R);
1298#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001299}
1300
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001301SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1302 const GRState *state,
1303 const TypedRegion *R,
1304 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001305 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001306 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001307
1308 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001309 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001310}
1311
Ted Kremenek9af46f52009-06-16 22:36:44 +00001312//===----------------------------------------------------------------------===//
1313// Binding values to regions.
1314//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001315
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001316Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001317 const MemRegion* R = 0;
1318
1319 if (isa<loc::MemRegionVal>(L))
1320 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001321
1322 if (R) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001323 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001324 return RBFactory.Remove(B, R).getRoot();
1325 }
1326
1327 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001328}
1329
Ted Kremenek67f28532009-06-17 22:02:04 +00001330const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001331 if (isa<loc::ConcreteInt>(L))
1332 return state;
1333
Ted Kremenek9af46f52009-06-16 22:36:44 +00001334 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001335 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001336
1337 // Check if the region is a struct region.
1338 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1339 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001340 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001341
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001342 // Special case: the current region represents a cast and it and the super
1343 // region both have pointer types or intptr_t types. If so, perform the
1344 // bind to the super region.
1345 // This is needed to support OSAtomicCompareAndSwap and friends or other
1346 // loads that treat integers as pointers and vis versa.
1347 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1348 if (ER->getIndex().isZeroConstant()) {
1349 if (const TypedRegion *superR =
1350 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1351 ASTContext &Ctx = getContext();
1352 QualType superTy = superR->getValueType(Ctx);
1353 QualType erTy = ER->getValueType(Ctx);
1354
1355 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1356 IsAnyPointerOrIntptr(erTy, Ctx)) {
1357 SValuator::CastResult cr =
1358 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1359 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1360 }
1361 }
1362 }
1363 }
1364
1365 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001366 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001367 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001368}
1369
Ted Kremenek67f28532009-06-17 22:02:04 +00001370const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001371 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001372
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001373 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001374 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001375
Ted Kremenek0964a062009-01-21 06:57:53 +00001376 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001377 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001378 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001379 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001380
Zhongxing Xud91ee272009-06-23 09:02:15 +00001381 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001382}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001383
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001384// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001385const GRState *
1386RegionStoreManager::BindCompoundLiteral(const GRState *state,
1387 const CompoundLiteralExpr* CL,
1388 SVal V) {
1389
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001390 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001391 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001392}
1393
Ted Kremenek67f28532009-06-17 22:02:04 +00001394const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001395 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001396 SVal Init) {
1397
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001398 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001399 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001400 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001401
Ted Kremenek46537392009-07-16 01:33:37 +00001402 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001403
1404 // Check if the init expr is a StringLiteral.
1405 if (isa<loc::MemRegionVal>(Init)) {
1406 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1407 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1408 const char* str = S->getStrData();
1409 unsigned len = S->getByteLength();
1410 unsigned j = 0;
1411
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001412 // Copy bytes from the string literal into the target array. Trailing bytes
1413 // in the array that are not covered by the string literal are initialized
1414 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001415 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001416 if (j >= len)
1417 break;
1418
Ted Kremenek46537392009-07-16 01:33:37 +00001419 SVal Idx = ValMgr.makeArrayIndex(i);
1420 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1421 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001422
Zhongxing Xud91ee272009-06-23 09:02:15 +00001423 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001424 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001425 }
1426
Ted Kremenek67f28532009-06-17 22:02:04 +00001427 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001428 }
1429
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001430 // Handle lazy compound values.
1431 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1432 return CopyLazyBindings(*LCV, state, R);
1433
1434 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001435 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001436 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001437 uint64_t i = 0;
1438
1439 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001440 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001441 if (VI == VE)
1442 break;
1443
Ted Kremenek46537392009-07-16 01:33:37 +00001444 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001445 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001446
1447 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001448 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001449 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001450 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001451 }
1452
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001453 // If the init list is shorter than the array length, set the array default
1454 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001455 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001456 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001457 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001458 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001459 }
1460 }
1461
Ted Kremenek67f28532009-06-17 22:02:04 +00001462 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001463}
1464
Ted Kremenek67f28532009-06-17 22:02:04 +00001465const GRState *
1466RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1467 SVal V) {
1468
1469 if (!Features.supportsFields())
1470 return state;
1471
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001472 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001473 assert(T->isStructureType());
1474
Ted Kremenek6217b802009-07-29 21:53:49 +00001475 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001476 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001477
1478 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001479 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001480
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001481 // Handle lazy compound values.
1482 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1483 return CopyLazyBindings(*LCV, state, R);
1484
Ted Kremenek67f28532009-06-17 22:02:04 +00001485 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1486 // Ignore them and kill the field values.
1487 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1488 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001489
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001490 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001491 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001492
1493 RecordDecl::field_iterator FI, FE;
1494
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001495 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001496
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001497 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001498 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001499
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001500 QualType FTy = (*FI)->getType();
1501 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1502
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001503 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001504 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001505 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001506 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001507 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001508 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001509 }
1510
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001511 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001512 if (FI != FE)
1513 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001514
Ted Kremenek67f28532009-06-17 22:02:04 +00001515 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001516}
1517
Ted Kremenek67f28532009-06-17 22:02:04 +00001518const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001519 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001520
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001521 // Set the default value of the struct region to "unknown".
1522 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001523
1524 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001525 Store store = state->getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001526 RegionBindings B = GetRegionBindings(store);
1527 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001528 const MemRegion* R = I.getKey();
1529 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1530 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001531 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001532 }
1533
Ted Kremenek67f28532009-06-17 22:02:04 +00001534 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001535}
1536
Ted Kremenek67f28532009-06-17 22:02:04 +00001537const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1538 const MemRegion* R, SVal V) {
1539 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001540}
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001541
1542const GRState*
1543RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1544 const GRState *state,
1545 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001546
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001547 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001548 RegionBindings B = GetRegionBindings(state->getStore());
1549 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
1550 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001551 state->get_context<RegionDefaultValue>();
1552
1553 llvm::OwningPtr<RegionStoreSubRegionMap>
1554 SubRegions(getRegionStoreSubRegionMap(state));
1555
1556 // B and DVM are updated after the call to RemoveSubRegionBindings.
1557 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
1558
1559 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1560 // results in a zero-copy algorithm.
1561 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1562}
1563
Ted Kremenek9af46f52009-06-16 22:36:44 +00001564//===----------------------------------------------------------------------===//
1565// State pruning.
1566//===----------------------------------------------------------------------===//
Ted Kremenek451ac092009-08-06 04:50:20 +00001567
Ted Kremenek9af46f52009-06-16 22:36:44 +00001568static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1569 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1570 const MemRegion *R = XR->getRegion();
1571
1572 while (R) {
1573 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1574 SymReaper.markLive(SR->getSymbol());
1575 return;
1576 }
1577
1578 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1579 R = SR->getSuperRegion();
1580 continue;
1581 }
1582
1583 break;
1584 }
1585
1586 return;
1587 }
1588
1589 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1590 SymReaper.markLive(*SI);
1591}
Ted Kremenek451ac092009-08-06 04:50:20 +00001592
1593namespace {
1594class VISIBILITY_HIDDEN TreeScanner {
1595 RegionBindings B;
1596 RegionDefaultBindings DB;
1597 SymbolReaper &SymReaper;
1598 llvm::DenseSet<const MemRegion*> &Marked;
1599 llvm::DenseSet<const LazyCompoundValData*> &ScannedLazyVals;
1600 RegionStoreSubRegionMap &M;
1601 RegionStoreManager &RS;
1602 llvm::SmallVectorImpl<const MemRegion*> &RegionRoots;
1603 const bool MarkKeys;
1604public:
1605 TreeScanner(RegionBindings b, RegionDefaultBindings db,
1606 SymbolReaper &symReaper,
1607 llvm::DenseSet<const MemRegion*> &marked,
1608 llvm::DenseSet<const LazyCompoundValData*> &scannedLazyVals,
1609 RegionStoreSubRegionMap &m, RegionStoreManager &rs,
1610 llvm::SmallVectorImpl<const MemRegion*> &regionRoots,
1611 bool markKeys = true)
1612 : B(b), DB(db), SymReaper(symReaper), Marked(marked),
1613 ScannedLazyVals(scannedLazyVals), M(m),
1614 RS(rs), RegionRoots(regionRoots), MarkKeys(markKeys) {}
1615
1616 void scanTree(const MemRegion *R);
1617};
1618} // end anonymous namespace
1619
1620
1621void TreeScanner::scanTree(const MemRegion *R) {
1622 if (MarkKeys) {
1623 if (Marked.count(R))
1624 return;
1625
1626 Marked.insert(R);
1627 }
1628
1629 // Mark the symbol for any live SymbolicRegion as "live". This means we
1630 // should continue to track that symbol.
1631 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1632 SymReaper.markLive(SymR->getSymbol());
1633
1634 // Get the data binding for R (if any).
1635 const SVal* Xptr = B.lookup(R);
1636
1637 // Check for lazy bindings.
1638 if (const nonloc::LazyCompoundVal *V =
1639 dyn_cast_or_null<nonloc::LazyCompoundVal>(Xptr)) {
1640
1641 const LazyCompoundValData *D = V->getCVData();
1642
1643 if (!ScannedLazyVals.count(D)) {
1644 // Scan the bindings in the LazyCompoundVal.
1645 ScannedLazyVals.insert(D);
1646
1647 // FIXME: Cache subregion maps.
1648 const GRState *lazyState = D->getState();
1649
1650 llvm::OwningPtr<RegionStoreSubRegionMap>
1651 lazySM(RS.getRegionStoreSubRegionMap(lazyState));
1652
1653 Store lazyStore = lazyState->getStore();
1654 RegionBindings lazyB = RS.GetRegionBindings(lazyStore);
1655
1656 RegionDefaultBindings lazyDB = lazyState->get<RegionDefaultValue>();
1657
1658 // Scan the bindings.
1659 TreeScanner scan(lazyB, lazyDB, SymReaper, Marked, ScannedLazyVals,
1660 *lazySM.get(), RS, RegionRoots, false);
1661
1662 scan.scanTree(D->getRegion());
1663 }
1664 }
1665 else {
1666 // No direct binding? Get the default binding for R (if any).
1667 if (!Xptr)
1668 Xptr = DB.lookup(R);
1669
1670 // Direct or default binding?
1671 if (Xptr) {
1672 SVal X = *Xptr;
1673 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1674
1675 // If X is a region, then add it to the RegionRoots.
1676 if (const MemRegion *RX = X.getAsRegion()) {
1677 RegionRoots.push_back(RX);
1678 // Mark the super region of the RX as live.
1679 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1680 // 'y' => element region. 'x' is its super region.
1681 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1682 RegionRoots.push_back(SR->getSuperRegion());
1683 }
1684 }
1685 }
1686 }
1687
1688 RegionStoreSubRegionMap::iterator I, E;
1689
1690 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
1691 scanTree(*I);
1692}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001693
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001694void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1695 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001696 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001697{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001698 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001699 RegionBindings B = GetRegionBindings(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001700
1701 // Lazily constructed backmap from MemRegions to SubRegions.
1702 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1703 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1704
Ted Kremenek9af46f52009-06-16 22:36:44 +00001705 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001706 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001707 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001708
1709 // Do a pass over the regions in the store. For VarRegions we check if
1710 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001711 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001712 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1713
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001714 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001715 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001716 const MemRegion *R = I.getKey();
1717 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001718 }
1719
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001720 // Scan the default bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001721 RegionDefaultBindings DVM = state.get<RegionDefaultValue>();
1722 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001723 I != E; ++I) {
1724 const MemRegion *R = I.getKey();
1725 IntermediateRoots.push_back(R);
1726 }
1727
1728 // Process the "intermediate" roots to find if they are referenced by
1729 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001730 while (!IntermediateRoots.empty()) {
1731 const MemRegion* R = IntermediateRoots.back();
1732 IntermediateRoots.pop_back();
1733
1734 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001735 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001736 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001737 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001738 continue;
1739 }
1740
1741 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001742 if (SymReaper.isLive(SR->getSymbol()))
1743 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001744 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001745 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001746
1747 // Add the super region for R to the worklist if it is a subregion.
1748 if (const SubRegion* superR =
1749 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1750 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001751 }
1752
1753 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1754 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek451ac092009-08-06 04:50:20 +00001755 llvm::DenseSet<const MemRegion*> Marked;
1756 llvm::DenseSet<const LazyCompoundValData*> LazyVals;
1757 TreeScanner TS(B, DVM, SymReaper, Marked, LazyVals, *SubRegions.get(),
1758 *this, RegionRoots);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001759
Ted Kremenek451ac092009-08-06 04:50:20 +00001760 while (!RegionRoots.empty()) {
1761 const MemRegion *R = RegionRoots.back();
1762 RegionRoots.pop_back();
1763 TS.scanTree(R);
1764 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001765
Ted Kremenek9af46f52009-06-16 22:36:44 +00001766 // We have now scanned the store, marking reachable regions and symbols
1767 // as live. We now remove all the regions that are dead from the store
1768 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001769 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001770 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001771 // If this region live? Is so, none of its symbols are dead.
1772 if (Marked.count(R))
1773 continue;
1774
1775 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001776 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001777
1778 // Mark all non-live symbols that this region references as dead.
1779 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1780 SymReaper.maybeDead(SymR->getSymbol());
1781
1782 SVal X = I.getData();
1783 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001784 for (; SI != SE; ++SI)
1785 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001786 }
1787
Ted Kremenek093569c2009-08-02 05:00:15 +00001788 // Remove dead 'default' bindings.
Ted Kremenek451ac092009-08-06 04:50:20 +00001789 RegionDefaultBindings NewDVM = DVM;
1790 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremenek093569c2009-08-02 05:00:15 +00001791 state.get_context<RegionDefaultValue>();
1792
Ted Kremenek451ac092009-08-06 04:50:20 +00001793 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek093569c2009-08-02 05:00:15 +00001794 I != E; ++I) {
1795 const MemRegion *R = I.getKey();
1796
1797 // If this region live? Is so, none of its symbols are dead.
1798 if (Marked.count(R))
1799 continue;
1800
1801 // Remove this dead region.
1802 NewDVM = DVMFactory.Remove(NewDVM, R);
1803
1804 // Mark all non-live symbols that this region references as dead.
1805 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1806 SymReaper.maybeDead(SymR->getSymbol());
1807
1808 SVal X = I.getData();
1809 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1810 for (; SI != SE; ++SI)
1811 SymReaper.maybeDead(*SI);
1812 }
1813
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001814 // Write the store back.
1815 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001816
1817 // Write the updated default bindings back.
1818 // FIXME: Right now this involves a fetching of a persistent state.
1819 // We can do better.
1820 if (DVM != NewDVM)
1821 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001822}
1823
1824//===----------------------------------------------------------------------===//
1825// Utility methods.
1826//===----------------------------------------------------------------------===//
1827
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001828void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001829 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001830 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001831 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001832
Ted Kremenek451ac092009-08-06 04:50:20 +00001833 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001834 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001835}