blob: 8efa0cc0010a5d48f181cba84253218b37991597 [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 Xu17fd8632009-08-17 06:19:58 +0000234 Store getInitialStore(const LocationContext *InitLoc) {
235 return RBFactory.GetEmptyMap().getRoot();
236 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000237
238 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
239 /// 'this' object (C++). When used when analyzing a normal function this
240 /// method returns NULL.
241 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000242 if (!SelfDecl)
243 return 0;
244
245 if (!SelfRegion) {
246 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
247 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
248 MRMgr.getHeapRegion());
249 }
250
251 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000252 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000253
254 //===-------------------------------------------------------------------===//
255 // Binding values to regions.
256 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000257
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000258 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
259 const Expr *E, unsigned Count);
260
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000261private:
Ted Kremenek451ac092009-08-06 04:50:20 +0000262 void RemoveSubRegionBindings(RegionBindings &B,
263 RegionDefaultBindings &DVM,
264 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000265 const MemRegion *R,
266 RegionStoreSubRegionMap &M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000267
268public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000269 const GRState *Bind(const GRState *state, Loc LV, SVal V);
270
271 const GRState *BindCompoundLiteral(const GRState *state,
272 const CompoundLiteralExpr* CL, SVal V);
273
274 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
275
276 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
277 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000278 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000279
Ted Kremenek67f28532009-06-17 22:02:04 +0000280 /// BindStruct - Bind a compound value to a structure.
281 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
282
283 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
284
285 /// KillStruct - Set the entire struct to unknown.
286 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
287
288 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
289
290 Store Remove(Store store, Loc LV);
291
292 //===------------------------------------------------------------------===//
293 // Loading values from regions.
294 //===------------------------------------------------------------------===//
295
296 /// The high level logic for this method is this:
297 /// Retrieve (L)
298 /// if L has binding
299 /// return L's binding
300 /// else if L is in killset
301 /// return unknown
302 /// else
303 /// if L is on stack or heap
304 /// return undefined
305 /// else
306 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000307 SValuator::CastResult Retrieve(const GRState *state, Loc L,
308 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000309
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000310 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000311
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000312 SVal RetrieveField(const GRState *state, const FieldRegion *R);
313
314 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000315
Ted Kremenek9031dd72009-07-21 00:12:07 +0000316 SVal RetrieveVar(const GRState *state, const VarRegion *R);
317
Ted Kremenek25c54572009-07-20 22:58:02 +0000318 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
319
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000320 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
321 QualType Ty, const MemRegion *superR);
322
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000323 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
324 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000325
Ted Kremenek67f28532009-06-17 22:02:04 +0000326 /// Retrieve the values in a struct and return a CompoundVal, used when doing
327 /// struct copy:
328 /// struct s x, y;
329 /// x = y;
330 /// y's value is retrieved by this method.
331 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
332
333 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000334
335 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000336 GetLazyBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000337
338 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
339 const GRState *state,
340 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000341
342 //===------------------------------------------------------------------===//
343 // State pruning.
344 //===------------------------------------------------------------------===//
345
346 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
347 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000348 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000349 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
350
351 //===------------------------------------------------------------------===//
352 // Region "extents".
353 //===------------------------------------------------------------------===//
354
355 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
356 SVal getSizeInElements(const GRState *state, const MemRegion* R);
357
358 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000359 // Utility methods.
360 //===------------------------------------------------------------------===//
361
Ted Kremenek451ac092009-08-06 04:50:20 +0000362 static inline RegionBindings GetRegionBindings(Store store) {
363 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000364 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000365
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000366 void print(Store store, llvm::raw_ostream& Out, const char* nl,
367 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000368
369 void iterBindings(Store store, BindingsHandler& f) {
370 // FIXME: Implement.
371 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000372
Ted Kremenek67f28532009-06-17 22:02:04 +0000373 // FIXME: Remove.
374 BasicValueFactory& getBasicVals() {
375 return StateMgr.getBasicVals();
376 }
377
378 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000379 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000380};
381
382} // end anonymous namespace
383
Ted Kremenek9af46f52009-06-16 22:36:44 +0000384//===----------------------------------------------------------------------===//
385// RegionStore creation.
386//===----------------------------------------------------------------------===//
387
388StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
389 RegionStoreFeatures F = maximal_features_tag();
390 return new RegionStoreManager(StMgr, F);
391}
392
393StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
394 RegionStoreFeatures F = minimal_features_tag();
395 F.enableFields(true);
396 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000397}
398
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000399void
400RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
401 const SubRegion *R) {
402 const MemRegion *superR = R->getSuperRegion();
403 if (add(superR, R))
404 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
405 WL.push_back(sr);
406}
407
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000408RegionStoreSubRegionMap*
409RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek451ac092009-08-06 04:50:20 +0000410 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek59e8f112009-03-03 01:35:36 +0000411 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
412
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000413 llvm::SmallVector<const SubRegion*, 10> WL;
414
Ted Kremenek451ac092009-08-06 04:50:20 +0000415 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000416 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
417 M->process(WL, R);
418
Ted Kremenek451ac092009-08-06 04:50:20 +0000419 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
420 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000421 I != E; ++I)
422 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
423 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000424
425 // We also need to record in the subregion map "intermediate" regions that
426 // don't have direct bindings but are super regions of those that do.
427 while (!WL.empty()) {
428 const SubRegion *R = WL.back();
429 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000430 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000431 }
432
Ted Kremenek14453bf2009-03-03 19:02:42 +0000433 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000434}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000435
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000436SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
437 return getRegionStoreSubRegionMap(state);
438}
439
Ted Kremenek9af46f52009-06-16 22:36:44 +0000440//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000441// Binding invalidation.
442//===----------------------------------------------------------------------===//
443
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000444void
Ted Kremenek451ac092009-08-06 04:50:20 +0000445RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
446 RegionDefaultBindings &DVM,
447 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000448 const MemRegion *R,
449 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000450
451 RegionStoreSubRegionMap::iterator I, E;
452
453 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000454 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000455
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000456 B = RBFactory.Remove(B, R);
457 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000458}
459
460
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000461const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
462 const MemRegion *R,
463 const Expr *E,
464 unsigned Count) {
465 ASTContext& Ctx = StateMgr.getContext();
466
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000467 // Strip away casts.
468 R = R->getBaseRegion();
469
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000470 // Remove the bindings to subregions.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000471 {
472 // Get the mapping of regions -> subregions.
473 llvm::OwningPtr<RegionStoreSubRegionMap>
474 SubRegions(getRegionStoreSubRegionMap(state));
475
Ted Kremenek451ac092009-08-06 04:50:20 +0000476 RegionBindings B = GetRegionBindings(state->getStore());
477 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
478 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000479 state->get_context<RegionDefaultValue>();
480
481 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
482 state = state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
483 }
484
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000485 if (!R->isBoundable())
486 return state;
487
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000488 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
489 isa<ObjCObjectRegion>(R)) {
490 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000491 // conjured symbol. The type of the symbol is irrelavant.
492 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000493 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000494 }
495
496 const TypedRegion *TR = cast<TypedRegion>(R);
497 QualType T = TR->getValueType(Ctx);
498
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000499 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000500 // FIXME: handle structs with default region value.
501 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
502
503 // No record definition. There is nothing we can do.
504 if (!RD)
505 return state;
506
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000507 // Invalidate the region by setting its default value to
508 // conjured symbol. The type of the symbol is irrelavant.
509 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
510 return setDefaultValue(state, R, V);
511 }
512
513 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000514 // Set the default value of the array to conjured symbol.
515 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
516 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000517 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000518 }
519
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000520 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
521 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
522 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000523}
524
525//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000526// getLValueXXX methods.
527//===----------------------------------------------------------------------===//
528
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000529/// getLValueString - Returns an SVal representing the lvalue of a
530/// StringLiteral. Within RegionStore a StringLiteral has an
531/// associated StringRegion, and the lvalue of a StringLiteral is the
532/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000533SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000534 const StringLiteral* S) {
535 return loc::MemRegionVal(MRMgr.getStringRegion(S));
536}
537
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000538/// getLValueVar - Returns an SVal that represents the lvalue of a
539/// variable. Within RegionStore a variable has an associated
540/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000541SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000542 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
543}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000544
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000545/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
546/// of a compound literal. Within RegionStore a compound literal
547/// has an associated region, and the lvalue of the compound literal
548/// is the lvalue of that region.
549SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000550RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000551 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000552 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
553}
554
Ted Kremenek67f28532009-06-17 22:02:04 +0000555SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000556 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000557 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000558}
559
Ted Kremenek67f28532009-06-17 22:02:04 +0000560SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000561 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000562 return getLValueFieldOrIvar(St, Base, D);
563}
564
Ted Kremenek67f28532009-06-17 22:02:04 +0000565SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000566 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000567 if (Base.isUnknownOrUndef())
568 return Base;
569
570 Loc BaseL = cast<Loc>(Base);
571 const MemRegion* BaseR = 0;
572
573 switch (BaseL.getSubKind()) {
574 case loc::MemRegionKind:
575 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
576 break;
577
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000578 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000579 // These are anormal cases. Flag an undefined value.
580 return UndefinedVal();
581
582 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000583 // While these seem funny, this can happen through casts.
584 // FIXME: What we should return is the field offset. For example,
585 // add the field offset to the integer value. That way funny things
586 // like this work properly: &(((struct foo *) 0xa)->f)
587 return Base;
588
589 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000590 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000591 return Base;
592 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000593
594 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
595 // of FieldDecl.
596 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
597 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000598
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000599 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000600}
601
Ted Kremenek67f28532009-06-17 22:02:04 +0000602SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000603 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000604 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000605
Ted Kremenekde7ec632009-03-09 22:44:49 +0000606 // If the base is an unknown or undefined value, just return it back.
607 // FIXME: For absolute pointer addresses, we just return that value back as
608 // well, although in reality we should return the offset added to that
609 // value.
610 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000611 return Base;
612
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000613 // Only handle integer offsets... for now.
614 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000615 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000616
Zhongxing Xuce760782009-05-09 13:20:07 +0000617 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000618
619 // Pointer of any type can be cast and used as array base.
620 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
621
Ted Kremenek46537392009-07-16 01:33:37 +0000622 // Convert the offset to the appropriate size and signedness.
623 Offset = ValMgr.convertToArrayIndex(Offset);
624
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000625 if (!ElemR) {
626 //
627 // If the base region is not an ElementRegion, create one.
628 // This can happen in the following example:
629 //
630 // char *p = __builtin_alloc(10);
631 // p[1] = 8;
632 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000633 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000634 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000635 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000636 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000637 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000638
639 SVal BaseIdx = ElemR->getIndex();
640
641 if (!isa<nonloc::ConcreteInt>(BaseIdx))
642 return UnknownVal();
643
644 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
645 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
646 assert(BaseIdxI.isSigned());
647
Ted Kremenek46537392009-07-16 01:33:37 +0000648 // Compute the new index.
649 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000650
Ted Kremenek46537392009-07-16 01:33:37 +0000651 // Construct the new ElementRegion.
652 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000653 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
654 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000655}
656
Ted Kremenek9af46f52009-06-16 22:36:44 +0000657//===----------------------------------------------------------------------===//
658// Extents for regions.
659//===----------------------------------------------------------------------===//
660
Ted Kremenek67f28532009-06-17 22:02:04 +0000661SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000662 const MemRegion *R) {
663
664 switch (R->getKind()) {
665 case MemRegion::MemSpaceRegionKind:
666 assert(0 && "Cannot index into a MemSpace");
667 return UnknownVal();
668
669 case MemRegion::CodeTextRegionKind:
670 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000671 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000672
673 // Not yet handled.
674 case MemRegion::AllocaRegionKind:
675 case MemRegion::CompoundLiteralRegionKind:
676 case MemRegion::ElementRegionKind:
677 case MemRegion::FieldRegionKind:
678 case MemRegion::ObjCIvarRegionKind:
679 case MemRegion::ObjCObjectRegionKind:
680 case MemRegion::SymbolicRegionKind:
681 return UnknownVal();
682
683 case MemRegion::StringRegionKind: {
684 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
685 // We intentionally made the size value signed because it participates in
686 // operations with signed indices.
687 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000688 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000689
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000690 case MemRegion::VarRegionKind: {
691 const VarRegion* VR = cast<VarRegion>(R);
692 // Get the type of the variable.
693 QualType T = VR->getDesugaredValueType(getContext());
694
695 // FIXME: Handle variable-length arrays.
696 if (isa<VariableArrayType>(T))
697 return UnknownVal();
698
699 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
700 // return the size as signed integer.
701 return ValMgr.makeIntVal(CAT->getSize(), false);
702 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000703
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000704 // Clients can use ordinary variables as if they were arrays. These
705 // essentially are arrays of size 1.
706 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000707 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000708
709 case MemRegion::BEG_DECL_REGIONS:
710 case MemRegion::END_DECL_REGIONS:
711 case MemRegion::BEG_TYPED_REGIONS:
712 case MemRegion::END_TYPED_REGIONS:
713 assert(0 && "Infeasible region");
714 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000715 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000716
717 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000718 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000719}
720
Ted Kremenek67f28532009-06-17 22:02:04 +0000721const GRState *RegionStoreManager::setExtent(const GRState *state,
722 const MemRegion *region,
723 SVal extent) {
724 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000725}
726
727//===----------------------------------------------------------------------===//
728// Location and region casting.
729//===----------------------------------------------------------------------===//
730
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000731/// ArrayToPointer - Emulates the "decay" of an array to a pointer
732/// type. 'Array' represents the lvalue of the array being decayed
733/// to a pointer, and the returned SVal represents the decayed
734/// version of that lvalue (i.e., a pointer to the first element of
735/// the array). This is called by GRExprEngine when evaluating casts
736/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000737SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000738 if (!isa<loc::MemRegionVal>(Array))
739 return UnknownVal();
740
741 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
742 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
743
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000744 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000745 return UnknownVal();
746
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000747 // Strip off typedefs from the ArrayRegion's ValueType.
748 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000749 ArrayType *AT = cast<ArrayType>(T);
750 T = AT->getElementType();
751
Ted Kremenek75185b52009-07-16 00:00:11 +0000752 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
753 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000754
755 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000756}
757
Ted Kremenek9af46f52009-06-16 22:36:44 +0000758//===----------------------------------------------------------------------===//
759// Pointer arithmetic.
760//===----------------------------------------------------------------------===//
761
Zhongxing Xu262fd032009-05-20 09:00:16 +0000762SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000763 BinaryOperator::Opcode Op, Loc L, NonLoc R,
764 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000765 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000766 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000767 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000768
Zhongxing Xua1718c72009-04-03 07:33:13 +0000769 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000770 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000771
Ted Kremenek3bccf082009-07-11 00:58:27 +0000772 switch (MR->getKind()) {
773 case MemRegion::SymbolicRegionKind: {
774 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000775 SymbolRef Sym = SR->getSymbol();
776 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000777 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000778 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
779 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
780 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000781 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000782 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000783 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000784 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000785 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000786 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
787 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
788 break;
789 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000790
Ted Kremenek3bccf082009-07-11 00:58:27 +0000791 case MemRegion::ElementRegionKind: {
792 ER = cast<ElementRegion>(MR);
793 break;
794 }
795
796 // Not yet handled.
797 case MemRegion::VarRegionKind:
798 case MemRegion::StringRegionKind:
799 case MemRegion::CompoundLiteralRegionKind:
800 case MemRegion::FieldRegionKind:
801 case MemRegion::ObjCObjectRegionKind:
802 case MemRegion::ObjCIvarRegionKind:
803 return UnknownVal();
804
Ted Kremenek3bccf082009-07-11 00:58:27 +0000805 case MemRegion::CodeTextRegionKind:
806 // Technically this can happen if people do funny things with casts.
807 return UnknownVal();
808
809 case MemRegion::MemSpaceRegionKind:
810 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
811 return UnknownVal();
812
813 case MemRegion::BEG_DECL_REGIONS:
814 case MemRegion::END_DECL_REGIONS:
815 case MemRegion::BEG_TYPED_REGIONS:
816 case MemRegion::END_TYPED_REGIONS:
817 assert(0 && "Infeasible region");
818 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000819 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000820
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000821 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000822 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
823 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
824
825 // Only support concrete integer indexes for now.
826 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000827 // FIXME: Should use SValuator here.
828 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
829 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000830 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000831 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
832 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000833 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000834 }
835
836 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000837}
838
Ted Kremenek9af46f52009-06-16 22:36:44 +0000839//===----------------------------------------------------------------------===//
840// Loading values from regions.
841//===----------------------------------------------------------------------===//
842
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000843Optional<SVal> RegionStoreManager::getDefaultBinding(const GRState *state,
844 const MemRegion *R) {
845
846 if (R->isBoundable())
847 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
848 if (TR->getValueType(getContext())->isUnionType())
849 return UnknownVal();
850
851 return Optional<SVal>::create(state->get<RegionDefaultValue>(R));
852}
853
Ted Kremeneka6275a52009-07-15 02:31:43 +0000854static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
855 RTy = Ctx.getCanonicalType(RTy);
856 UsedTy = Ctx.getCanonicalType(UsedTy);
857
858 if (RTy == UsedTy)
859 return false;
860
Ted Kremenek25c54572009-07-20 22:58:02 +0000861
862 // Recursively check the types. We basically want to see if a pointer value
863 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
864 // represents a reinterpretation.
865 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000866 const PointerType *PRTy = RTy->getAs<PointerType>();
867 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000868
869 return PUsedTy && PRTy &&
870 IsReinterpreted(PRTy->getPointeeType(),
871 PUsedTy->getPointeeType(), Ctx);
872 }
873
874 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000875}
876
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000877SValuator::CastResult
878RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000879
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000880 assert(!isa<UnknownVal>(L) && "location unknown");
881 assert(!isa<UndefinedVal>(L) && "location undefined");
882
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000883 // FIXME: Is this even possible? Shouldn't this be treated as a null
884 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000885 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000886 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000887
Ted Kremenek67f28532009-06-17 22:02:04 +0000888 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000889
Zhongxing Xu91844122009-05-20 09:18:48 +0000890 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000891 // Example:
892 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000893 // char* p = alloca();
894 // read(p);
895 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000896 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000897 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000898
899 if (isa<SymbolicRegion>(MR)) {
900 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000901 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000902 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000903 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
904 }
905
Ted Kremenek968f0a62009-08-03 21:41:46 +0000906 if (isa<CodeTextRegion>(MR))
907 return SValuator::CastResult(state, UnknownVal());
908
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000909 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
910 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000911 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000912 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000913
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000914 // FIXME: We should eventually handle funny addressing. e.g.:
915 //
916 // int x = ...;
917 // int *p = &x;
918 // char *q = (char*) p;
919 // char c = *q; // returns the first byte of 'x'.
920 //
921 // Such funny addressing will occur due to layering of regions.
922
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000923#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000924 ASTContext &Ctx = getContext();
925 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000926 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
927 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000928 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000929 assert(Ctx.getCanonicalType(RTy) ==
930 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000931 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000932#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000933
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000934 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000935 return SValuator::CastResult(state, RetrieveStruct(state, R));
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000936
937 // FIXME: Handle unions.
938 if (RTy->isUnionType())
939 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000940
941 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000942 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000943
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000944 // FIXME: handle Vector types.
945 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000946 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000947
948 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000949 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000950
951 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000952 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000953
Ted Kremenek25c54572009-07-20 22:58:02 +0000954 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000955 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000956
957 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000958 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000959
Ted Kremenek451ac092009-08-06 04:50:20 +0000960 RegionBindings B = GetRegionBindings(state->getStore());
961 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000962
963 // Check if the region has a binding.
964 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000965 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000966
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000967 // The location does not have a bound value. This means that it has
968 // the value it had upon its creation and/or entry to the analyzed
969 // function/method. These are either symbolic values or 'undefined'.
970
Ted Kremenek356e9d62009-07-22 04:35:42 +0000971#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000972 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000973#else
974 if (R->hasStackStorage()) {
975#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000976 // All stack variables are considered to have undefined values
977 // upon creation. All heap allocated blocks are considered to
978 // have undefined values as well unless they are explicitly bound
979 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000980 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000981 }
982
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000983 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000984 return SValuator::CastResult(state,
985 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000986}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000987
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000988std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000989RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000990
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000991 if (const nonloc::LazyCompoundVal *V =
992 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
993 return std::make_pair(V->getState(), V->getRegion());
994
995 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
996 const std::pair<const GRState *, const MemRegion *> &X =
997 GetLazyBinding(B, ER->getSuperRegion());
998
999 if (X.first)
1000 return std::make_pair(X.first,
1001 MRMgr.getElementRegionWithSuper(ER, X.second));
1002 }
1003 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1004 const std::pair<const GRState *, const MemRegion *> &X =
1005 GetLazyBinding(B, FR->getSuperRegion());
1006
1007 if (X.first)
1008 return std::make_pair(X.first,
1009 MRMgr.getFieldRegionWithSuper(FR, X.second));
1010 }
1011
1012 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1013}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001014
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001015SVal RegionStoreManager::RetrieveElement(const GRState* state,
1016 const ElementRegion* R) {
1017 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001018 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001019 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001020 return *V;
1021
Ted Kremenek921109a2009-07-01 23:19:52 +00001022 const MemRegion* superR = R->getSuperRegion();
1023
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001024 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001025 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001026 const StringLiteral *Str = StrR->getStringLiteral();
1027 SVal Idx = R->getIndex();
1028 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1029 int64_t i = CI->getValue().getSExtValue();
1030 char c;
1031 if (i == Str->getByteLength())
1032 c = '\0';
1033 else
1034 c = Str->getStrData()[i];
1035 return ValMgr.makeIntVal(c, getContext().CharTy);
1036 }
1037 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001038
1039 // Special case: the current region represents a cast and it and the super
1040 // region both have pointer types or intptr_t types. If so, perform the
1041 // retrieve from the super region and appropriately "cast" the value.
1042 // This is needed to support OSAtomicCompareAndSwap and friends or other
1043 // loads that treat integers as pointers and vis versa.
1044 if (R->getIndex().isZeroConstant()) {
1045 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1046 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001047 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1048 QualType valTy = R->getValueType(Ctx);
1049 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1050 // Retrieve the value from the super region. This will be casted to
1051 // valTy when we return to 'Retrieve'.
1052 const SValuator::CastResult &cr = Retrieve(state,
1053 loc::MemRegionVal(superR),
1054 valTy);
1055 return cr.getSVal();
1056 }
1057 }
1058 }
1059 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001060
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001061 // Check if the immediate super region has a direct binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001062 if (const SVal *V = B.lookup(superR)) {
1063 if (SymbolRef parentSym = V->getAsSymbol())
1064 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001065
1066 if (V->isUnknownOrUndef())
1067 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001068
1069 // Handle LazyCompoundVals for the immediate super region. Other cases
1070 // are handled in 'RetrieveFieldOrElementCommon'.
1071 if (const nonloc::LazyCompoundVal *LCV =
1072 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001073
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001074 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1075 return RetrieveElement(LCV->getState(), R);
1076 }
1077
Ted Kremeneka6275a52009-07-15 02:31:43 +00001078 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001079 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001080 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001081
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001082 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001083}
1084
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001085SVal RegionStoreManager::RetrieveField(const GRState* state,
1086 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001087
1088 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001089 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001090 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001091 return *V;
1092
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001093 QualType Ty = R->getValueType(getContext());
1094 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1095}
1096
1097SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1098 const TypedRegion *R,
1099 QualType Ty,
1100 const MemRegion *superR) {
1101
1102 // At this point we have already checked in either RetrieveElement or
1103 // RetrieveField if 'R' has a direct binding.
1104
1105 RegionBindings B = GetRegionBindings(state->getStore());
1106
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001107 while (superR) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001108 if (const Optional<SVal> &D = getDefaultBinding(state, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001109 if (SymbolRef parentSym = D->getAsSymbol())
1110 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001111
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001112 if (D->isZeroConstant())
1113 return ValMgr.makeZeroVal(Ty);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001114
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001115 if (D->isUnknown())
1116 return *D;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001117
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001118 assert(0 && "Unknown default value");
1119 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001120
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001121 // If our super region is a field or element itself, walk up the region
1122 // hierarchy to see if there is a default value installed in an ancestor.
1123 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1124 superR = cast<SubRegion>(superR)->getSuperRegion();
1125 continue;
1126 }
1127
1128 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001129 }
1130
1131 // Lazy binding?
1132 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001133 const MemRegion *lazyBindingRegion = NULL;
1134 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001135
1136 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001137 assert(lazyBindingRegion && "Lazy-binding region not set");
1138
1139 if (isa<ElementRegion>(R))
1140 return RetrieveElement(lazyBindingState,
1141 cast<ElementRegion>(lazyBindingRegion));
1142
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001143 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001144 cast<FieldRegion>(lazyBindingRegion));
1145 }
Ted Kremenekdc147262009-07-02 22:02:15 +00001146
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001147 if (R->hasStackStorage() && !R->hasParametersStorage()) {
1148
1149 if (isa<ElementRegion>(R)) {
1150 // Currently we don't reason specially about Clang-style vectors. Check
1151 // if superR is a vector and if so return Unknown.
1152 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1153 if (typedSuperR->getValueType(getContext())->isVectorType())
1154 return UnknownVal();
1155 }
1156 }
1157
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001158 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001159 }
1160
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001161 // All other values are symbolic.
1162 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001163}
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001164
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001165SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1166 const ObjCIvarRegion* R) {
1167
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001168 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001169 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001170
1171 if (const SVal* V = B.lookup(R))
1172 return *V;
1173
1174 const MemRegion *superR = R->getSuperRegion();
1175
1176 // Check if the super region has a binding.
1177 if (const SVal *V = B.lookup(superR)) {
1178 if (SymbolRef parentSym = V->getAsSymbol())
1179 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1180
1181 // Other cases: give up.
1182 return UnknownVal();
1183 }
1184
Ted Kremenek25c54572009-07-20 22:58:02 +00001185 return RetrieveLazySymbol(state, R);
1186}
1187
Ted Kremenek9031dd72009-07-21 00:12:07 +00001188SVal RegionStoreManager::RetrieveVar(const GRState *state,
1189 const VarRegion *R) {
1190
1191 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001192 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek9031dd72009-07-21 00:12:07 +00001193
1194 if (const SVal* V = B.lookup(R))
1195 return *V;
1196
1197 // Lazily derive a value for the VarRegion.
1198 const VarDecl *VD = R->getDecl();
1199
1200 if (VD == SelfDecl)
1201 return loc::MemRegionVal(getSelfRegion(0));
1202
1203 if (R->hasGlobalsOrParametersStorage())
1204 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1205
1206 return UndefinedVal();
1207}
1208
Ted Kremenek25c54572009-07-20 22:58:02 +00001209SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1210 const TypedRegion *R) {
1211
1212 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001213
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001214 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001215 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001216}
1217
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001218SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1219 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001220 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001221 assert(T->isStructureType());
1222
Zhongxing Xub7507d12009-06-11 07:27:30 +00001223 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001224 RecordDecl* RD = RT->getDecl();
1225 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001226 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001227#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001228 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1229
Ted Kremenek67f28532009-06-17 22:02:04 +00001230 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1231 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001232 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001233
Douglas Gregore267ff32008-12-11 20:41:00 +00001234 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1235 FieldEnd = Fields.rend();
1236 Field != FieldEnd; ++Field) {
1237 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001238 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001239 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001240 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1241 }
1242
Zhongxing Xud91ee272009-06-23 09:02:15 +00001243 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001244#else
1245 return ValMgr.makeLazyCompoundVal(state, R);
1246#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001247}
1248
Ted Kremenek67f28532009-06-17 22:02:04 +00001249SVal RegionStoreManager::RetrieveArray(const GRState *state,
1250 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001251#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001252 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001253 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1254
1255 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001256 uint64_t size = CAT->getSize().getZExtValue();
1257 for (uint64_t i = 0; i < size; ++i) {
1258 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001259 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1260 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001261 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001262 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001263 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1264 }
1265
Zhongxing Xud91ee272009-06-23 09:02:15 +00001266 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001267#else
1268 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1269 return ValMgr.makeLazyCompoundVal(state, R);
1270#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001271}
1272
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001273SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1274 const GRState *state,
1275 const TypedRegion *R,
1276 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001277 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001278 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001279
1280 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001281 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001282}
1283
Ted Kremenek9af46f52009-06-16 22:36:44 +00001284//===----------------------------------------------------------------------===//
1285// Binding values to regions.
1286//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001287
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001288Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001289 const MemRegion* R = 0;
1290
1291 if (isa<loc::MemRegionVal>(L))
1292 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001293
1294 if (R) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001295 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001296 return RBFactory.Remove(B, R).getRoot();
1297 }
1298
1299 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001300}
1301
Ted Kremenek67f28532009-06-17 22:02:04 +00001302const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001303 if (isa<loc::ConcreteInt>(L))
1304 return state;
1305
Ted Kremenek9af46f52009-06-16 22:36:44 +00001306 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001307 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001308
1309 // Check if the region is a struct region.
1310 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1311 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001312 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001313
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001314 // Special case: the current region represents a cast and it and the super
1315 // region both have pointer types or intptr_t types. If so, perform the
1316 // bind to the super region.
1317 // This is needed to support OSAtomicCompareAndSwap and friends or other
1318 // loads that treat integers as pointers and vis versa.
1319 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1320 if (ER->getIndex().isZeroConstant()) {
1321 if (const TypedRegion *superR =
1322 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1323 ASTContext &Ctx = getContext();
1324 QualType superTy = superR->getValueType(Ctx);
1325 QualType erTy = ER->getValueType(Ctx);
1326
1327 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1328 IsAnyPointerOrIntptr(erTy, Ctx)) {
1329 SValuator::CastResult cr =
1330 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1331 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1332 }
1333 }
1334 }
1335 }
1336
1337 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001338 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001339 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001340}
1341
Ted Kremenek67f28532009-06-17 22:02:04 +00001342const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001343 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001344
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001345 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001346 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001347
Ted Kremenek0964a062009-01-21 06:57:53 +00001348 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001349 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001350 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001351 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001352
Zhongxing Xud91ee272009-06-23 09:02:15 +00001353 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001354}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001355
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001356// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001357const GRState *
1358RegionStoreManager::BindCompoundLiteral(const GRState *state,
1359 const CompoundLiteralExpr* CL,
1360 SVal V) {
1361
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001362 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001363 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001364}
1365
Ted Kremenek67f28532009-06-17 22:02:04 +00001366const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001367 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001368 SVal Init) {
1369
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001370 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001371 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001372 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001373
Ted Kremenek46537392009-07-16 01:33:37 +00001374 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001375
1376 // Check if the init expr is a StringLiteral.
1377 if (isa<loc::MemRegionVal>(Init)) {
1378 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1379 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1380 const char* str = S->getStrData();
1381 unsigned len = S->getByteLength();
1382 unsigned j = 0;
1383
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001384 // Copy bytes from the string literal into the target array. Trailing bytes
1385 // in the array that are not covered by the string literal are initialized
1386 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001387 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001388 if (j >= len)
1389 break;
1390
Ted Kremenek46537392009-07-16 01:33:37 +00001391 SVal Idx = ValMgr.makeArrayIndex(i);
1392 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1393 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001394
Zhongxing Xud91ee272009-06-23 09:02:15 +00001395 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001396 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001397 }
1398
Ted Kremenek67f28532009-06-17 22:02:04 +00001399 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001400 }
1401
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001402 // Handle lazy compound values.
1403 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1404 return CopyLazyBindings(*LCV, state, R);
1405
1406 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001407 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001408 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001409 uint64_t i = 0;
1410
1411 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001412 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001413 if (VI == VE)
1414 break;
1415
Ted Kremenek46537392009-07-16 01:33:37 +00001416 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001417 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001418
1419 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001420 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001421 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001422 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001423 }
1424
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001425 // If the init list is shorter than the array length, set the array default
1426 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001427 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001428 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001429 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001430 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001431 }
1432 }
1433
Ted Kremenek67f28532009-06-17 22:02:04 +00001434 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001435}
1436
Ted Kremenek67f28532009-06-17 22:02:04 +00001437const GRState *
1438RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1439 SVal V) {
1440
1441 if (!Features.supportsFields())
1442 return state;
1443
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001444 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001445 assert(T->isStructureType());
1446
Ted Kremenek6217b802009-07-29 21:53:49 +00001447 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001448 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001449
1450 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001451 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001452
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001453 // Handle lazy compound values.
1454 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1455 return CopyLazyBindings(*LCV, state, R);
1456
Ted Kremenek67f28532009-06-17 22:02:04 +00001457 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1458 // Ignore them and kill the field values.
1459 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1460 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001461
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001462 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001463 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001464
1465 RecordDecl::field_iterator FI, FE;
1466
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001467 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001468
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001469 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001470 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001471
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001472 QualType FTy = (*FI)->getType();
1473 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1474
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001475 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001476 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001477 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001478 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001479 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001480 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001481 }
1482
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001483 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001484 if (FI != FE)
1485 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001486
Ted Kremenek67f28532009-06-17 22:02:04 +00001487 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001488}
1489
Ted Kremenek67f28532009-06-17 22:02:04 +00001490const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001491 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001492
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001493 // Set the default value of the struct region to "unknown".
1494 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001495
1496 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001497 Store store = state->getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001498 RegionBindings B = GetRegionBindings(store);
1499 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001500 const MemRegion* R = I.getKey();
1501 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1502 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001503 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001504 }
1505
Ted Kremenek67f28532009-06-17 22:02:04 +00001506 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001507}
1508
Ted Kremenek67f28532009-06-17 22:02:04 +00001509const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1510 const MemRegion* R, SVal V) {
1511 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001512}
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001513
1514const GRState*
1515RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1516 const GRState *state,
1517 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001518
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001519 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001520 RegionBindings B = GetRegionBindings(state->getStore());
1521 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
1522 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001523 state->get_context<RegionDefaultValue>();
1524
1525 llvm::OwningPtr<RegionStoreSubRegionMap>
1526 SubRegions(getRegionStoreSubRegionMap(state));
1527
1528 // B and DVM are updated after the call to RemoveSubRegionBindings.
1529 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
1530
1531 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1532 // results in a zero-copy algorithm.
1533 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1534}
1535
Ted Kremenek9af46f52009-06-16 22:36:44 +00001536//===----------------------------------------------------------------------===//
1537// State pruning.
1538//===----------------------------------------------------------------------===//
Ted Kremenek451ac092009-08-06 04:50:20 +00001539
Ted Kremenek9af46f52009-06-16 22:36:44 +00001540static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1541 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1542 const MemRegion *R = XR->getRegion();
1543
1544 while (R) {
1545 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1546 SymReaper.markLive(SR->getSymbol());
1547 return;
1548 }
1549
1550 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1551 R = SR->getSuperRegion();
1552 continue;
1553 }
1554
1555 break;
1556 }
1557
1558 return;
1559 }
1560
1561 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1562 SymReaper.markLive(*SI);
1563}
Ted Kremenek451ac092009-08-06 04:50:20 +00001564
1565namespace {
1566class VISIBILITY_HIDDEN TreeScanner {
1567 RegionBindings B;
1568 RegionDefaultBindings DB;
1569 SymbolReaper &SymReaper;
1570 llvm::DenseSet<const MemRegion*> &Marked;
1571 llvm::DenseSet<const LazyCompoundValData*> &ScannedLazyVals;
1572 RegionStoreSubRegionMap &M;
1573 RegionStoreManager &RS;
1574 llvm::SmallVectorImpl<const MemRegion*> &RegionRoots;
1575 const bool MarkKeys;
1576public:
1577 TreeScanner(RegionBindings b, RegionDefaultBindings db,
1578 SymbolReaper &symReaper,
1579 llvm::DenseSet<const MemRegion*> &marked,
1580 llvm::DenseSet<const LazyCompoundValData*> &scannedLazyVals,
1581 RegionStoreSubRegionMap &m, RegionStoreManager &rs,
1582 llvm::SmallVectorImpl<const MemRegion*> &regionRoots,
1583 bool markKeys = true)
1584 : B(b), DB(db), SymReaper(symReaper), Marked(marked),
1585 ScannedLazyVals(scannedLazyVals), M(m),
1586 RS(rs), RegionRoots(regionRoots), MarkKeys(markKeys) {}
1587
1588 void scanTree(const MemRegion *R);
1589};
1590} // end anonymous namespace
1591
1592
1593void TreeScanner::scanTree(const MemRegion *R) {
1594 if (MarkKeys) {
1595 if (Marked.count(R))
1596 return;
1597
1598 Marked.insert(R);
1599 }
1600
1601 // Mark the symbol for any live SymbolicRegion as "live". This means we
1602 // should continue to track that symbol.
1603 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1604 SymReaper.markLive(SymR->getSymbol());
1605
1606 // Get the data binding for R (if any).
1607 const SVal* Xptr = B.lookup(R);
1608
1609 // Check for lazy bindings.
1610 if (const nonloc::LazyCompoundVal *V =
1611 dyn_cast_or_null<nonloc::LazyCompoundVal>(Xptr)) {
1612
1613 const LazyCompoundValData *D = V->getCVData();
1614
1615 if (!ScannedLazyVals.count(D)) {
1616 // Scan the bindings in the LazyCompoundVal.
1617 ScannedLazyVals.insert(D);
1618
1619 // FIXME: Cache subregion maps.
1620 const GRState *lazyState = D->getState();
1621
1622 llvm::OwningPtr<RegionStoreSubRegionMap>
1623 lazySM(RS.getRegionStoreSubRegionMap(lazyState));
1624
1625 Store lazyStore = lazyState->getStore();
1626 RegionBindings lazyB = RS.GetRegionBindings(lazyStore);
1627
1628 RegionDefaultBindings lazyDB = lazyState->get<RegionDefaultValue>();
1629
1630 // Scan the bindings.
1631 TreeScanner scan(lazyB, lazyDB, SymReaper, Marked, ScannedLazyVals,
1632 *lazySM.get(), RS, RegionRoots, false);
1633
1634 scan.scanTree(D->getRegion());
1635 }
1636 }
1637 else {
1638 // No direct binding? Get the default binding for R (if any).
1639 if (!Xptr)
1640 Xptr = DB.lookup(R);
1641
1642 // Direct or default binding?
1643 if (Xptr) {
1644 SVal X = *Xptr;
1645 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1646
1647 // If X is a region, then add it to the RegionRoots.
1648 if (const MemRegion *RX = X.getAsRegion()) {
1649 RegionRoots.push_back(RX);
1650 // Mark the super region of the RX as live.
1651 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1652 // 'y' => element region. 'x' is its super region.
1653 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1654 RegionRoots.push_back(SR->getSuperRegion());
1655 }
1656 }
1657 }
1658 }
1659
1660 RegionStoreSubRegionMap::iterator I, E;
1661
1662 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
1663 scanTree(*I);
1664}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001665
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001666void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1667 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001668 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001669{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001670 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001671 RegionBindings B = GetRegionBindings(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001672
1673 // Lazily constructed backmap from MemRegions to SubRegions.
1674 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1675 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1676
Ted Kremenek9af46f52009-06-16 22:36:44 +00001677 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001678 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001679 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001680
1681 // Do a pass over the regions in the store. For VarRegions we check if
1682 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001683 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001684 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1685
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001686 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001687 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001688 const MemRegion *R = I.getKey();
1689 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001690 }
1691
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001692 // Scan the default bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001693 RegionDefaultBindings DVM = state.get<RegionDefaultValue>();
1694 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001695 I != E; ++I) {
1696 const MemRegion *R = I.getKey();
1697 IntermediateRoots.push_back(R);
1698 }
1699
1700 // Process the "intermediate" roots to find if they are referenced by
1701 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001702 while (!IntermediateRoots.empty()) {
1703 const MemRegion* R = IntermediateRoots.back();
1704 IntermediateRoots.pop_back();
1705
1706 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001707 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001708 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001709 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001710 continue;
1711 }
1712
1713 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001714 if (SymReaper.isLive(SR->getSymbol()))
1715 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001716 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001717 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001718
1719 // Add the super region for R to the worklist if it is a subregion.
1720 if (const SubRegion* superR =
1721 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1722 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001723 }
1724
1725 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1726 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek451ac092009-08-06 04:50:20 +00001727 llvm::DenseSet<const MemRegion*> Marked;
1728 llvm::DenseSet<const LazyCompoundValData*> LazyVals;
1729 TreeScanner TS(B, DVM, SymReaper, Marked, LazyVals, *SubRegions.get(),
1730 *this, RegionRoots);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001731
Ted Kremenek451ac092009-08-06 04:50:20 +00001732 while (!RegionRoots.empty()) {
1733 const MemRegion *R = RegionRoots.back();
1734 RegionRoots.pop_back();
1735 TS.scanTree(R);
1736 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001737
Ted Kremenek9af46f52009-06-16 22:36:44 +00001738 // We have now scanned the store, marking reachable regions and symbols
1739 // as live. We now remove all the regions that are dead from the store
1740 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001741 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001742 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001743 // If this region live? Is so, none of its symbols are dead.
1744 if (Marked.count(R))
1745 continue;
1746
1747 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001748 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001749
1750 // Mark all non-live symbols that this region references as dead.
1751 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1752 SymReaper.maybeDead(SymR->getSymbol());
1753
1754 SVal X = I.getData();
1755 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001756 for (; SI != SE; ++SI)
1757 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001758 }
1759
Ted Kremenek093569c2009-08-02 05:00:15 +00001760 // Remove dead 'default' bindings.
Ted Kremenek451ac092009-08-06 04:50:20 +00001761 RegionDefaultBindings NewDVM = DVM;
1762 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremenek093569c2009-08-02 05:00:15 +00001763 state.get_context<RegionDefaultValue>();
1764
Ted Kremenek451ac092009-08-06 04:50:20 +00001765 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek093569c2009-08-02 05:00:15 +00001766 I != E; ++I) {
1767 const MemRegion *R = I.getKey();
1768
1769 // If this region live? Is so, none of its symbols are dead.
1770 if (Marked.count(R))
1771 continue;
1772
1773 // Remove this dead region.
1774 NewDVM = DVMFactory.Remove(NewDVM, R);
1775
1776 // Mark all non-live symbols that this region references as dead.
1777 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1778 SymReaper.maybeDead(SymR->getSymbol());
1779
1780 SVal X = I.getData();
1781 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1782 for (; SI != SE; ++SI)
1783 SymReaper.maybeDead(*SI);
1784 }
1785
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001786 // Write the store back.
1787 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001788
1789 // Write the updated default bindings back.
1790 // FIXME: Right now this involves a fetching of a persistent state.
1791 // We can do better.
1792 if (DVM != NewDVM)
1793 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001794}
1795
1796//===----------------------------------------------------------------------===//
1797// Utility methods.
1798//===----------------------------------------------------------------------===//
1799
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001800void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001801 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001802 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001803 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001804
Ted Kremenek451ac092009-08-06 04:50:20 +00001805 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001806 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001807}