blob: e527f356b0379d97955df844b675a3a92cf2342d [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//===----------------------------------------------------------------------===//
Ted Kremenek1309f9a2010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/MemRegion.h"
18#include "clang/Analysis/AnalysisContext.h"
19#include "clang/Checker/PathSensitive/GRState.h"
20#include "clang/Checker/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd4e5a602009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000024#include "clang/AST/CharUnits.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025
26#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000027#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000028#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000029
30using namespace clang;
31
Ted Kremeneka5e81f12009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000033
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000035// Representation of binding keys.
36//===----------------------------------------------------------------------===//
37
38namespace {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000039class BindingKey {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000040public:
Ted Kremeneke393f4a2010-02-03 03:06:46 +000041 enum Kind { Direct = 0x0, Default = 0x1 };
42private:
43 llvm ::PointerIntPair<const MemRegion*, 1> P;
44 uint64_t Offset;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000045
Ted Kremeneke393f4a2010-02-03 03:06:46 +000046 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
Zhongxing Xubfcaf802010-02-05 02:26:30 +000047 : P(r, (unsigned) k), Offset(offset) { assert(r); }
Ted Kremeneke393f4a2010-02-03 03:06:46 +000048public:
49
50 bool isDefault() const { return P.getInt() == Default; }
51 bool isDirect() const { return P.getInt() == Direct; }
52
53 const MemRegion *getRegion() const { return P.getPointer(); }
54 uint64_t getOffset() const { return Offset; }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000055
56 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000057 ID.AddPointer(P.getOpaqueValue());
58 ID.AddInteger(Offset);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000059 }
Ted Kremeneke393f4a2010-02-03 03:06:46 +000060
61 static BindingKey Make(const MemRegion *R, Kind k);
62
63 bool operator<(const BindingKey &X) const {
64 if (P.getOpaqueValue() < X.P.getOpaqueValue())
65 return true;
66 if (P.getOpaqueValue() > X.P.getOpaqueValue())
67 return false;
68 return Offset < X.Offset;
69 }
70
71 bool operator==(const BindingKey &X) const {
72 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
73 Offset == X.Offset;
74 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000075};
76} // end anonymous namespace
77
78namespace llvm {
79 static inline
80 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000081 os << '(' << K.getRegion() << ',' << K.getOffset()
82 << ',' << (K.isDirect() ? "direct" : "default")
83 << ')';
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000084 return os;
85 }
86} // end llvm namespace
87
88//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +000089// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000090//===----------------------------------------------------------------------===//
91
Ted Kremeneke393f4a2010-02-03 03:06:46 +000092typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000093
Ted Kremenek50dc1b32008-12-24 01:05:03 +000094//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000095// Fine-grained control of RegionStoreManager.
96//===----------------------------------------------------------------------===//
97
98namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000099struct minimal_features_tag {};
100struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000102class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000103 bool SupportsFields;
104 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek9af46f52009-06-16 22:36:44 +0000106public:
107 RegionStoreFeatures(minimal_features_tag) :
108 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek9af46f52009-06-16 22:36:44 +0000110 RegionStoreFeatures(maximal_features_tag) :
111 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek9af46f52009-06-16 22:36:44 +0000113 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek9af46f52009-06-16 22:36:44 +0000115 bool supportsFields() const { return SupportsFields; }
116 bool supportsRemaining() const { return SupportsRemaining; }
117};
118}
119
120//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000121// Region "Extents"
122//===----------------------------------------------------------------------===//
123//
124// MemRegions represent chunks of memory with a size (their "extent"). This
125// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000126//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000127namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000128static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000129namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000130 template<> struct GRStateTrait<RegionExtents>
131 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
132 static void* GDMIndex() { return &RegionExtentsIndex; }
133 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000134}
135
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000136//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000137// Utility functions.
138//===----------------------------------------------------------------------===//
139
140static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
141 if (ty->isAnyPointerType())
142 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144 return ty->isIntegerType() && ty->isScalarType() &&
145 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000149// Main RegionStore logic.
150//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000151
Zhongxing Xu17892752008-10-08 02:50:44 +0000152namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000154class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekdf165012010-02-02 22:38:47 +0000155public:
156 typedef llvm::ImmutableSet<const MemRegion*> Set;
157 typedef llvm::DenseMap<const MemRegion*, Set> Map;
158private:
159 Set::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000160 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000161public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000162 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000163 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000164
165 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000166 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000167 return true;
168 }
169
170 I->second = F.Add(I->second, SubRegion);
171 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000174 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek59e8f112009-03-03 01:35:36 +0000176 ~RegionStoreSubRegionMap() {}
Ted Kremenekdf165012010-02-02 22:38:47 +0000177
178 const Set *getSubRegions(const MemRegion *Parent) const {
179 Map::const_iterator I = M.find(Parent);
180 return I == M.end() ? NULL : &I->second;
181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek5dc27462009-03-03 02:51:43 +0000183 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000184 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000185
186 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000187 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenekdf165012010-02-02 22:38:47 +0000189 Set S = I->second;
190 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000191 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000192 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek5dc27462009-03-03 02:51:43 +0000195 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000198
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000199
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000200class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000201 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000202 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000203
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000204 typedef llvm::DenseMap<Store, RegionStoreSubRegionMap*> SMCache;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000205 SMCache SC;
206
Zhongxing Xu17892752008-10-08 02:50:44 +0000207public:
Mike Stump1eb44332009-09-09 15:08:12 +0000208 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000209 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000210 Features(f),
Ted Kremenek8928d412010-02-04 04:14:49 +0000211 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000212
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000213 virtual ~RegionStoreManager() {
214 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
215 delete (*I).second;
216 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000217
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000218 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Zhongxing Xu13d50172009-10-11 08:08:02 +0000220 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Zhongxing Xu13d50172009-10-11 08:08:02 +0000222 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
223 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000224 /// getDefaultBinding - Returns an SVal* representing an optional default
225 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000226 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000227
228 /// setImplicitDefaultValue - Set the default binding for the provided
229 /// MemRegion to the value implicitly defined for compound literals when
230 /// the value is not specified.
231 const GRState *setImplicitDefaultValue(const GRState *state,
232 const MemRegion *R,
233 QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000235 /// getLValueString - Returns an SVal representing the lvalue of a
236 /// StringLiteral. Within RegionStore a StringLiteral has an
237 /// associated StringRegion, and the lvalue of a StringLiteral is
238 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000239 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000240
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000241 /// getLValueCompoundLiteral - Returns an SVal representing the
242 /// lvalue of a compound literal. Within RegionStore a compound
243 /// literal has an associated region, and the lvalue of the
244 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000245 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000246
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000247 /// getLValueVar - Returns an SVal that represents the lvalue of a
248 /// variable. Within RegionStore a variable has an associated
249 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000250 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000252 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000253
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000254 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000256 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000257
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000258 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000259
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000260
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000261 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
262 /// type. 'Array' represents the lvalue of the array being decayed
263 /// to a pointer, and the returned SVal represents the decayed
264 /// version of that lvalue (i.e., a pointer to the first element of
265 /// the array). This is called by GRExprEngine when evaluating
266 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000267 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000268
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000269 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000270 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000271
Mike Stump1eb44332009-09-09 15:08:12 +0000272 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000273 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000274 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000275
Ted Kremenek67f28532009-06-17 22:02:04 +0000276 //===-------------------------------------------------------------------===//
277 // Binding values to regions.
278 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000279
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000280 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000281 const Expr *E, unsigned Count,
Ted Kremenek81a95832009-12-03 03:27:11 +0000282 InvalidatedSymbols *IS) {
283 return RegionStoreManager::InvalidateRegions(state, &R, &R+1, E, Count, IS);
284 }
285
286 const GRState *InvalidateRegions(const GRState *state,
287 const MemRegion * const *Begin,
288 const MemRegion * const *End,
289 const Expr *E, unsigned Count,
290 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000292public: // Made public for helper classes.
293
Zhongxing Xu13d50172009-10-11 08:08:02 +0000294 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000295 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000297 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
298
299 RegionBindings Add(RegionBindings B, const MemRegion *R,
300 BindingKey::Kind k, SVal V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000301
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000302 const SVal *Lookup(RegionBindings B, BindingKey K);
303 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000304
305 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000306 RegionBindings Remove(RegionBindings B, const MemRegion *R,
307 BindingKey::Kind k);
308
309 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
310 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
311 }
312
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000313 Store Remove(Store store, BindingKey K);
314
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000315public: // Part of public interface to class.
316
Ted Kremenek67f28532009-06-17 22:02:04 +0000317 const GRState *Bind(const GRState *state, Loc LV, SVal V);
318
319 const GRState *BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +0000320 const CompoundLiteralExpr* CL,
321 const LocationContext *LC,
322 SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000324 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
325 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000326
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000327 const GRState *BindDeclWithNoInit(const GRState *state,
328 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000329 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000330 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000331
Ted Kremenek67f28532009-06-17 22:02:04 +0000332 /// BindStruct - Bind a compound value to a structure.
333 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek67f28532009-06-17 22:02:04 +0000335 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
337 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000338 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000339
Ted Kremenek67f28532009-06-17 22:02:04 +0000340 Store Remove(Store store, Loc LV);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000341
Ted Kremenek67f28532009-06-17 22:02:04 +0000342
343 //===------------------------------------------------------------------===//
344 // Loading values from regions.
345 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenek67f28532009-06-17 22:02:04 +0000347 /// The high level logic for this method is this:
348 /// Retrieve (L)
349 /// if L has binding
350 /// return L's binding
351 /// else if L is in killset
352 /// return unknown
353 /// else
354 /// if L is on stack or heap
355 /// return undefined
356 /// else
357 /// return symbolic
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000358 SVal Retrieve(const GRState *state, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000359
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000360 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000361
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000362 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000364 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek9031dd72009-07-21 00:12:07 +0000366 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek25c54572009-07-20 22:58:02 +0000368 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000370 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000371 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek67f28532009-06-17 22:02:04 +0000373 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000374 /// struct copy:
375 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000376 /// x = y;
377 /// y's value is retrieved by this method.
378 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Ted Kremenek67f28532009-06-17 22:02:04 +0000380 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000382 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000383 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000384 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000386 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
387 const GRState *state,
388 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000389
Ted Kremenek0954cde2009-09-24 04:11:44 +0000390 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
391 QualType T);
392
Ted Kremenek67f28532009-06-17 22:02:04 +0000393 //===------------------------------------------------------------------===//
394 // State pruning.
395 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek67f28532009-06-17 22:02:04 +0000397 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
398 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000399 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000400 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
401
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000402 const GRState *EnterStackFrame(const GRState *state,
403 const StackFrameContext *frame);
404
Ted Kremenek67f28532009-06-17 22:02:04 +0000405 //===------------------------------------------------------------------===//
406 // Region "extents".
407 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000409 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000410 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000411 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000412
413 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000414 // Utility methods.
415 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek451ac092009-08-06 04:50:20 +0000417 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000418 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000419 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000420
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000421 void print(Store store, llvm::raw_ostream& Out, const char* nl,
422 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000423
424 void iterBindings(Store store, BindingsHandler& f) {
425 // FIXME: Implement.
426 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000427
Ted Kremenek67f28532009-06-17 22:02:04 +0000428 // FIXME: Remove.
429 BasicValueFactory& getBasicVals() {
430 return StateMgr.getBasicVals();
431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek67f28532009-06-17 22:02:04 +0000433 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000434 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000435};
436
437} // end anonymous namespace
438
Ted Kremenek9af46f52009-06-16 22:36:44 +0000439//===----------------------------------------------------------------------===//
440// RegionStore creation.
441//===----------------------------------------------------------------------===//
442
443StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
444 RegionStoreFeatures F = maximal_features_tag();
445 return new RegionStoreManager(StMgr, F);
446}
447
448StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
449 RegionStoreFeatures F = minimal_features_tag();
450 F.enableFields(true);
451 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000452}
453
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000454void
455RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000456 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000457 const MemRegion *superR = R->getSuperRegion();
458 if (add(superR, R))
459 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000460 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000461}
462
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000463RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000464RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
465 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000466 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000468 llvm::SmallVector<const SubRegion*, 10> WL;
469
Ted Kremenek451ac092009-08-06 04:50:20 +0000470 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000471 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000472 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Mike Stump1eb44332009-09-09 15:08:12 +0000474 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000475 // don't have direct bindings but are super regions of those that do.
476 while (!WL.empty()) {
477 const SubRegion *R = WL.back();
478 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000479 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000480 }
481
Ted Kremenek14453bf2009-03-03 19:02:42 +0000482 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000483}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000484
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000485SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000486 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000487}
488
Ted Kremenek9af46f52009-06-16 22:36:44 +0000489//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000490// Binding invalidation.
491//===----------------------------------------------------------------------===//
492
Ted Kremenekdf165012010-02-02 22:38:47 +0000493
Zhongxing Xu13d50172009-10-11 08:08:02 +0000494void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
495 const MemRegion *R,
496 RegionStoreSubRegionMap &M) {
Ted Kremenekdf165012010-02-02 22:38:47 +0000497
498 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
499 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
500 I != E; ++I)
501 RemoveSubRegionBindings(B, *I, M);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000502
503 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000504}
505
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000506namespace {
507class InvalidateRegionsWorker {
Ted Kremenek5b290652010-02-03 04:16:00 +0000508 typedef BumpVector<BindingKey> RegionCluster;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000509 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5b290652010-02-03 04:16:00 +0000510 typedef llvm::SmallVector<std::pair<const MemRegion *,RegionCluster*>, 10>
511 WorkList;
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000513 BumpVectorContext BVC;
514 ClusterMap ClusterM;
515 WorkList WL;
516public:
517 const GRState *InvalidateRegions(RegionStoreManager &RM,
518 const GRState *state,
519 const MemRegion * const *I,
520 const MemRegion * const *E,
521 const Expr *Ex,
522 unsigned Count,
523 StoreManager::InvalidatedSymbols *IS);
Ted Kremenek87806792009-09-27 20:45:21 +0000524
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000525private:
Ted Kremenek5b290652010-02-03 04:16:00 +0000526 void AddToWorkList(BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000527 void AddToWorkList(const MemRegion *R);
Ted Kremenek5b290652010-02-03 04:16:00 +0000528 void AddToCluster(BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000529 RegionCluster **getCluster(const MemRegion *R);
530};
531}
Zhongxing Xu13d50172009-10-11 08:08:02 +0000532
Ted Kremenek5b290652010-02-03 04:16:00 +0000533void InvalidateRegionsWorker::AddToCluster(BindingKey K) {
534 const MemRegion *R = K.getRegion();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000535 const MemRegion *baseR = R->getBaseRegion();
536 RegionCluster **CPtr = getCluster(baseR);
Ted Kremenek5b290652010-02-03 04:16:00 +0000537 assert(*CPtr);
538 (*CPtr)->push_back(K, BVC);
539}
540
541void InvalidateRegionsWorker::AddToWorkList(BindingKey K) {
542 AddToWorkList(K.getRegion());
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000543}
544
545void InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000546 const MemRegion *baseR = R->getBaseRegion();
547 RegionCluster **CPtr = getCluster(baseR);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000548 if (RegionCluster *C = *CPtr) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000549 WL.push_back(std::make_pair(baseR, C));
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000550 *CPtr = NULL;
551 }
552}
553
554InvalidateRegionsWorker::RegionCluster **
555InvalidateRegionsWorker::getCluster(const MemRegion *R) {
556 RegionCluster *&CRef = ClusterM[R];
557 if (!CRef) {
558 void *Mem = BVC.getAllocator().Allocate<RegionCluster>();
559 CRef = new (Mem) RegionCluster(BVC, 10);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000560 }
561 return &CRef;
562}
563
564const GRState *
565InvalidateRegionsWorker::InvalidateRegions(RegionStoreManager &RM,
566 const GRState *state,
567 const MemRegion * const *I,
568 const MemRegion * const *E,
569 const Expr *Ex, unsigned Count,
570 StoreManager::InvalidatedSymbols *IS)
571{
572 ASTContext &Ctx = state->getStateManager().getContext();
573 ValueManager &ValMgr = state->getStateManager().getValueManager();
574 RegionBindings B = RegionStoreManager::GetRegionBindings(state->getStore());
575
576 // Scan the entire store and make the region clusters.
577 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000578 AddToCluster(RI.getKey());
579 if (const MemRegion *R = RI.getData().getAsRegion()) {
580 // Generate a cluster, but don't add the region to the cluster
581 // if there aren't any bindings.
582 getCluster(R->getBaseRegion());
583 }
Ted Kremenek81a95832009-12-03 03:27:11 +0000584 }
Ted Kremenek87806792009-09-27 20:45:21 +0000585
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000586 // Add the cluster for I .. E to a worklist.
587 for ( ; I != E; ++I)
588 AddToWorkList(*I);
589
590 while (!WL.empty()) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000591 const MemRegion *baseR;
592 RegionCluster *C;
593 llvm::tie(baseR, C) = WL.back();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000594 WL.pop_back();
Ted Kremenek87806792009-09-27 20:45:21 +0000595
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000596 for (RegionCluster::iterator I = C->begin(), E = C->end(); I != E; ++I) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000597 BindingKey K = *I;
Ted Kremenek473e1672009-10-16 00:30:49 +0000598
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000599 // Get the old binding. Is it a region? If so, add it to the worklist.
Ted Kremenek5b290652010-02-03 04:16:00 +0000600 if (const SVal *V = RM.Lookup(B, K)) {
601 if (const MemRegion *R = V->getAsRegion())
602 AddToWorkList(R);
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000603
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000604 // A symbol? Mark it touched by the invalidation.
605 if (IS)
606 if (SymbolRef Sym = V->getAsSymbol())
607 IS->insert(Sym);
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000608 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000609
Ted Kremenek5b290652010-02-03 04:16:00 +0000610 B = RM.Remove(B, K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000611 }
Ted Kremenek5b290652010-02-03 04:16:00 +0000612
613 // Now inspect the base region.
614
615 if (IS) {
616 // Symbolic region? Mark that symbol touched by the invalidation.
617 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
618 IS->insert(SR->getSymbol());
619 }
620
621 // BlockDataRegion? If so, invalidate captured variables that are passed
622 // by reference.
623 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
624 for (BlockDataRegion::referenced_vars_iterator
625 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
626 I != E; ++I) {
627 const VarRegion *VR = *I;
628 if (VR->getDecl()->getAttr<BlocksAttr>())
629 AddToWorkList(VR);
630 }
631 continue;
632 }
633
634 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
635 // Invalidate the region by setting its default value to
636 // conjured symbol. The type of the symbol is irrelavant.
637 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
638 Count);
639 B = RM.Add(B, baseR, BindingKey::Default, V);
640 continue;
641 }
642
643 if (!baseR->isBoundable())
644 continue;
645
646 const TypedRegion *TR = cast<TypedRegion>(baseR);
647 QualType T = TR->getValueType(Ctx);
648
649 // Invalidate the binding.
650 if (const RecordType *RT = T->getAsStructureType()) {
651 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
652 // No record definition. There is nothing we can do.
653 if (!RD) {
654 B = RM.Remove(B, baseR);
655 continue;
656 }
657
658 // Invalidate the region by setting its default value to
659 // conjured symbol. The type of the symbol is irrelavant.
660 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
661 Count);
662 B = RM.Add(B, baseR, BindingKey::Default, V);
663 continue;
664 }
665
666 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
667 // Set the default value of the array to conjured symbol.
668 DefinedOrUnknownSVal V =
669 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
670 B = RM.Add(B, baseR, BindingKey::Default, V);
671 continue;
672 }
673
674 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
675 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
676 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000677 }
678
Ted Kremenek87806792009-09-27 20:45:21 +0000679 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000680 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000681}
682
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000683const GRState *RegionStoreManager::InvalidateRegions(const GRState *state,
684 const MemRegion * const *I,
685 const MemRegion * const *E,
686 const Expr *Ex,
687 unsigned Count,
688 InvalidatedSymbols *IS) {
689 InvalidateRegionsWorker W;
690 return W.InvalidateRegions(*this, state, I, E, Ex, Count, IS);
691}
692
693
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000694//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000695// getLValueXXX methods.
696//===----------------------------------------------------------------------===//
697
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000698/// getLValueString - Returns an SVal representing the lvalue of a
699/// StringLiteral. Within RegionStore a StringLiteral has an
700/// associated StringRegion, and the lvalue of a StringLiteral is the
701/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000702SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000703 return loc::MemRegionVal(MRMgr.getStringRegion(S));
704}
705
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000706/// getLValueVar - Returns an SVal that represents the lvalue of a
707/// variable. Within RegionStore a variable has an associated
708/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000709SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000710 const LocationContext *LC) {
711 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000712}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000713
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000714SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
715 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000716}
717
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000718SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
719 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000720}
721
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000722SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000723 if (Base.isUnknownOrUndef())
724 return Base;
725
726 Loc BaseL = cast<Loc>(Base);
727 const MemRegion* BaseR = 0;
728
729 switch (BaseL.getSubKind()) {
730 case loc::MemRegionKind:
731 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
732 break;
733
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000734 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000735 // These are anormal cases. Flag an undefined value.
736 return UndefinedVal();
737
738 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000739 // While these seem funny, this can happen through casts.
740 // FIXME: What we should return is the field offset. For example,
741 // add the field offset to the integer value. That way funny things
742 // like this work properly: &(((struct foo *) 0xa)->f)
743 return Base;
744
745 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000746 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000747 return Base;
748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000750 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
751 // of FieldDecl.
752 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
753 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000754
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000755 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000756}
757
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000758SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
759 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000760
Ted Kremenekde7ec632009-03-09 22:44:49 +0000761 // If the base is an unknown or undefined value, just return it back.
762 // FIXME: For absolute pointer addresses, we just return that value back as
763 // well, although in reality we should return the offset added to that
764 // value.
765 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000766 return Base;
767
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000768 // Only handle integer offsets... for now.
769 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000770 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000771
Zhongxing Xuce760782009-05-09 13:20:07 +0000772 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000773
774 // Pointer of any type can be cast and used as array base.
775 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek46537392009-07-16 01:33:37 +0000777 // Convert the offset to the appropriate size and signedness.
778 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000780 if (!ElemR) {
781 //
782 // If the base region is not an ElementRegion, create one.
783 // This can happen in the following example:
784 //
785 // char *p = __builtin_alloc(10);
786 // p[1] = 8;
787 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000788 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000789 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000790 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000791 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000792 }
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000794 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000796 if (!isa<nonloc::ConcreteInt>(BaseIdx))
797 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000799 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
800 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
801 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek46537392009-07-16 01:33:37 +0000803 // Compute the new index.
804 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek46537392009-07-16 01:33:37 +0000806 // Construct the new ElementRegion.
807 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000808 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000809 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000810}
811
Ted Kremenek9af46f52009-06-16 22:36:44 +0000812//===----------------------------------------------------------------------===//
813// Extents for regions.
814//===----------------------------------------------------------------------===//
815
Zhongxing Xue884ff82009-11-12 02:48:32 +0000816DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000817 const MemRegion *R,
818 QualType EleTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000820 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000821 case MemRegion::CXXThisRegionKind:
822 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000823 case MemRegion::GenericMemSpaceRegionKind:
824 case MemRegion::StackLocalsSpaceRegionKind:
825 case MemRegion::StackArgumentsSpaceRegionKind:
826 case MemRegion::HeapSpaceRegionKind:
827 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000828 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000829 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000830 return UnknownVal();
831
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000832 case MemRegion::FunctionTextRegionKind:
833 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000834 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000835 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000836 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000837
838 // Not yet handled.
839 case MemRegion::AllocaRegionKind:
840 case MemRegion::CompoundLiteralRegionKind:
841 case MemRegion::ElementRegionKind:
842 case MemRegion::FieldRegionKind:
843 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000844 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000845 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000847 case MemRegion::SymbolicRegionKind: {
848 const SVal *Size = state->get<RegionExtents>(R);
849 if (!Size)
850 return UnknownVal();
851 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
852 if (!CI)
853 return UnknownVal();
854
855 CharUnits RegionSize =
856 CharUnits::fromQuantity(CI->getValue().getSExtValue());
857 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
858 assert(RegionSize % EleSize == 0);
859
860 return ValMgr.makeIntVal(RegionSize / EleSize, false);
861 }
862
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000863 case MemRegion::StringRegionKind: {
864 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000865 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000866 // operations with signed indices.
867 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000868 }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000870 case MemRegion::VarRegionKind: {
871 const VarRegion* VR = cast<VarRegion>(R);
872 // Get the type of the variable.
873 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000875 // FIXME: Handle variable-length arrays.
876 if (isa<VariableArrayType>(T))
877 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000879 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
880 // return the size as signed integer.
881 return ValMgr.makeIntVal(CAT->getSize(), false);
882 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000883
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000884 // Clients can use ordinary variables as if they were arrays. These
885 // essentially are arrays of size 1.
886 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000887 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000888 }
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000890 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000891 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000892}
893
Ted Kremenek67f28532009-06-17 22:02:04 +0000894const GRState *RegionStoreManager::setExtent(const GRState *state,
895 const MemRegion *region,
896 SVal extent) {
897 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000898}
899
900//===----------------------------------------------------------------------===//
901// Location and region casting.
902//===----------------------------------------------------------------------===//
903
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000904/// ArrayToPointer - Emulates the "decay" of an array to a pointer
905/// type. 'Array' represents the lvalue of the array being decayed
906/// to a pointer, and the returned SVal represents the decayed
907/// version of that lvalue (i.e., a pointer to the first element of
908/// the array). This is called by GRExprEngine when evaluating casts
909/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000910SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000911 if (!isa<loc::MemRegionVal>(Array))
912 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Ted Kremenekabb042f2008-12-13 19:24:37 +0000914 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
915 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000917 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000918 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000920 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000921 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000922 ArrayType *AT = cast<ArrayType>(T);
923 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Ted Kremenek75185b52009-07-16 00:00:11 +0000925 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000926 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
927 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000928}
929
Ted Kremenek9af46f52009-06-16 22:36:44 +0000930//===----------------------------------------------------------------------===//
931// Pointer arithmetic.
932//===----------------------------------------------------------------------===//
933
Mike Stump1eb44332009-09-09 15:08:12 +0000934SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000935 BinaryOperator::Opcode Op, Loc L, NonLoc R,
936 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000937 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000938 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000939 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000940
Zhongxing Xua1718c72009-04-03 07:33:13 +0000941 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000942 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000943
Ted Kremenek3bccf082009-07-11 00:58:27 +0000944 switch (MR->getKind()) {
945 case MemRegion::SymbolicRegionKind: {
946 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000947 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000948 QualType T = Sym->getType(getContext());
949 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000951 if (const PointerType *PT = T->getAs<PointerType>())
952 EleTy = PT->getPointeeType();
953 else
John McCall183700f2009-09-21 23:43:11 +0000954 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek3bccf082009-07-11 00:58:27 +0000956 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
957 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000958 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000959 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000960 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000961 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000962 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000963 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000964 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
965 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000966 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000967 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000968
Ted Kremenek3bccf082009-07-11 00:58:27 +0000969 case MemRegion::ElementRegionKind: {
970 ER = cast<ElementRegion>(MR);
971 break;
972 }
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Ted Kremenek3bccf082009-07-11 00:58:27 +0000974 // Not yet handled.
975 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000976 case MemRegion::StringRegionKind: {
977
978 }
979 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000980 case MemRegion::CompoundLiteralRegionKind:
981 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000982 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000983 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000984 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000986 case MemRegion::FunctionTextRegionKind:
987 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000988 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000989 // Technically this can happen if people do funny things with casts.
990 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenekde0d2632010-01-05 02:18:06 +0000992 case MemRegion::CXXThisRegionKind:
993 assert(0 &&
994 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000995 case MemRegion::GenericMemSpaceRegionKind:
996 case MemRegion::StackLocalsSpaceRegionKind:
997 case MemRegion::StackArgumentsSpaceRegionKind:
998 case MemRegion::HeapSpaceRegionKind:
999 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001000 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +00001001 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
1002 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +00001003 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +00001004
Zhongxing Xu94aa6c12009-03-02 07:52:23 +00001005 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +00001006 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +00001007
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +00001008 // For now, only support:
1009 // (a) concrete integer indices that can easily be resolved
1010 // (b) 0 + symbolic index
1011 if (Base) {
1012 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
1013 // FIXME: Should use SValuator here.
1014 SVal NewIdx =
1015 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +00001016 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +00001017 const MemRegion* NewER =
1018 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
1019 ER->getSuperRegion(), getContext());
1020 return ValMgr.makeLoc(NewER);
1021 }
1022 if (0 == Base->getValue()) {
1023 const MemRegion* NewER =
1024 MRMgr.getElementRegion(ER->getElementType(), R,
1025 ER->getSuperRegion(), getContext());
1026 return ValMgr.makeLoc(NewER);
1027 }
Ted Kremenek5dc27462009-03-03 02:51:43 +00001028 }
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Ted Kremenek5dc27462009-03-03 02:51:43 +00001030 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +00001031}
1032
Ted Kremenek9af46f52009-06-16 22:36:44 +00001033//===----------------------------------------------------------------------===//
1034// Loading values from regions.
1035//===----------------------------------------------------------------------===//
1036
Zhongxing Xu13d50172009-10-11 08:08:02 +00001037Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001038 const MemRegion *R) {
1039 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
1040 return *V;
1041
Zhongxing Xu13d50172009-10-11 08:08:02 +00001042 return Optional<SVal>();
1043}
1044
1045Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001046 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001047 if (R->isBoundable())
1048 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
1049 if (TR->getValueType(getContext())->isUnionType())
1050 return UnknownVal();
1051
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001052 if (const SVal *V = Lookup(B, R, BindingKey::Default))
1053 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +00001054
1055 return Optional<SVal>();
1056}
1057
1058Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
1059 const MemRegion *R) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001060
1061 if (Optional<SVal> V = getDirectBinding(B, R))
1062 return V;
1063
1064 return getDefaultBinding(B, R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001065}
1066
Ted Kremeneka6275a52009-07-15 02:31:43 +00001067static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
1068 RTy = Ctx.getCanonicalType(RTy);
1069 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Ted Kremeneka6275a52009-07-15 02:31:43 +00001071 if (RTy == UsedTy)
1072 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001073
1074
Ted Kremenek25c54572009-07-20 22:58:02 +00001075 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +00001076 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +00001077 // represents a reinterpretation.
1078 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001079 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +00001080 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +00001081
1082 return PUsedTy && PRTy &&
1083 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +00001084 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +00001085 }
1086
1087 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001088}
1089
Ted Kremenek0954cde2009-09-24 04:11:44 +00001090const ElementRegion *
1091RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
1092 ASTContext &Ctx = getContext();
1093 SVal idx = ValMgr.makeZeroArrayIndex();
1094 assert(!T.isNull());
1095 return MRMgr.getElementRegion(T, idx, SR, Ctx);
1096}
Ted Kremenek0954cde2009-09-24 04:11:44 +00001097
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001098SVal RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001099 assert(!isa<UnknownVal>(L) && "location unknown");
1100 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek28172a22009-12-15 23:23:27 +00001101
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001102 // FIXME: Is this even possible? Shouldn't this be treated as a null
1103 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001104 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001105 return UndefinedVal();
Ted Kremenek28172a22009-12-15 23:23:27 +00001106
Ted Kremenek67f28532009-06-17 22:02:04 +00001107 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001108
Zhongxing Xu91844122009-05-20 09:18:48 +00001109 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +00001110 // Example:
1111 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +00001112 // char* p = alloca();
1113 // read(p);
1114 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +00001115 if (isa<AllocaRegion>(MR))
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001116 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Ted Kremenek0954cde2009-09-24 04:11:44 +00001118 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1119 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Ted Kremenek968f0a62009-08-03 21:41:46 +00001121 if (isa<CodeTextRegion>(MR))
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001122 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001124 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1125 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001126 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001127 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001128
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001129 // FIXME: We should eventually handle funny addressing. e.g.:
1130 //
1131 // int x = ...;
1132 // int *p = &x;
1133 // char *q = (char*) p;
1134 // char c = *q; // returns the first byte of 'x'.
1135 //
1136 // Such funny addressing will occur due to layering of regions.
1137
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001138#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001139 ASTContext &Ctx = getContext();
1140 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001141 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1142 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001143 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001144 assert(Ctx.getCanonicalType(RTy) ==
1145 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001146 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001147#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001148
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001149 if (RTy->isStructureType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001150 return RetrieveStruct(state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001152 // FIXME: Handle unions.
1153 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001154 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001155
1156 if (RTy->isArrayType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001157 return RetrieveArray(state, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001158
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001159 // FIXME: handle Vector types.
1160 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001161 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001162
1163 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001164 return CastRetrievedVal(RetrieveField(state->getStore(), FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001165
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001166 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1167 // FIXME: Here we actually perform an implicit conversion from the loaded
1168 // value to the element type. Eventually we want to compose these values
1169 // more intelligently. For example, an 'element' can encompass multiple
1170 // bound regions (e.g., several bound bytes), or could be a subset of
1171 // a larger value.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001172 return CastRetrievedVal(RetrieveElement(state->getStore(), ER), ER, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001173 }
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001175 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1176 // FIXME: Here we actually perform an implicit conversion from the loaded
1177 // value to the ivar type. What we should model is stores to ivars
1178 // that blow past the extent of the ivar. If the address of the ivar is
1179 // reinterpretted, it is possible we stored a different value that could
1180 // fit within the ivar. Either we need to cast these when storing them
1181 // or reinterpret them lazily (as we do here).
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001182 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001183 }
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001185 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1186 // FIXME: Here we actually perform an implicit conversion from the loaded
1187 // value to the variable type. What we should model is stores to variables
1188 // that blow past the extent of the variable. If the address of the
1189 // variable is reinterpretted, it is possible we stored a different value
1190 // that could fit within the variable. Either we need to cast these when
1191 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001192 return CastRetrievedVal(RetrieveVar(state, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001193 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001194
Ted Kremenek451ac092009-08-06 04:50:20 +00001195 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001196 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001197
1198 // Check if the region has a binding.
1199 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001200 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001201
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001202 // The location does not have a bound value. This means that it has
1203 // the value it had upon its creation and/or entry to the analyzed
1204 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001205 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001206 // All stack variables are considered to have undefined values
1207 // upon creation. All heap allocated blocks are considered to
1208 // have undefined values as well unless they are explicitly bound
1209 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001210 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001211 }
1212
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001213 // All other values are symbolic.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001214 return ValMgr.getRegionValueSymbolVal(R, RTy);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001215}
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001217std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001218RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001219 if (Optional<SVal> OV = getDirectBinding(B, R))
1220 if (const nonloc::LazyCompoundVal *V =
1221 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001222 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001224 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001225 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001226 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001228 if (X.first)
1229 return std::make_pair(X.first,
1230 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001231 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001232 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001233 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001234 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001236 if (X.first)
1237 return std::make_pair(X.first,
1238 MRMgr.getFieldRegionWithSuper(FR, X.second));
1239 }
1240
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001241 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001242}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001243
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001244SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001245 const ElementRegion* R) {
1246 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001247 RegionBindings B = GetRegionBindings(store);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001248 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001249 return *V;
1250
Ted Kremenek921109a2009-07-01 23:19:52 +00001251 const MemRegion* superR = R->getSuperRegion();
1252
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001253 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001254 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001255 // FIXME: Handle loads from strings where the literal is treated as
1256 // an integer, e.g., *((unsigned int*)"hello")
1257 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001258 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001259 if (T != Ctx.getCanonicalType(R->getElementType()))
1260 return UnknownVal();
1261
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001262 const StringLiteral *Str = StrR->getStringLiteral();
1263 SVal Idx = R->getIndex();
1264 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1265 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001266 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001267 if (i > byteLength) {
1268 // Buffer overflow checking in GRExprEngine should handle this case,
1269 // but we shouldn't rely on it to not overflow here if that checking
1270 // is disabled.
1271 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001272 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001273 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001274 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001275 }
1276 }
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001278 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001279 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001280 if (SymbolRef parentSym = V->getAsSymbol())
1281 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001282
1283 if (V->isUnknownOrUndef())
1284 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001285
1286 // Handle LazyCompoundVals for the immediate super region. Other cases
1287 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001288 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001289 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001291 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001292 return RetrieveElement(LCV->getStore(), R);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001293 }
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Ted Kremeneka6275a52009-07-15 02:31:43 +00001295 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001296 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001297 }
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001298
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001299 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001300}
1301
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001302SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001303 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001304
1305 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001306 RegionBindings B = GetRegionBindings(store);
Zhongxing Xu13d50172009-10-11 08:08:02 +00001307 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001308 return *V;
1309
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001310 QualType Ty = R->getValueType(getContext());
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001311 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001312}
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001314SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001315 const TypedRegion *R,
1316 QualType Ty,
1317 const MemRegion *superR) {
1318
Mike Stump1eb44332009-09-09 15:08:12 +00001319 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001320 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001322 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001324 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001325 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001326 if (SymbolRef parentSym = D->getAsSymbol())
1327 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001329 if (D->isZeroConstant())
1330 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001332 if (D->isUnknown())
1333 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001335 assert(0 && "Unknown default value");
1336 }
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001338 // If our super region is a field or element itself, walk up the region
1339 // hierarchy to see if there is a default value installed in an ancestor.
1340 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1341 superR = cast<SubRegion>(superR)->getSuperRegion();
1342 continue;
1343 }
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001345 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001346 }
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001348 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001349 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001350 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001351 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001353 if (lazyBindingStore) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001354 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001356 if (isa<ElementRegion>(R))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001357 return RetrieveElement(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001358 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001360 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001361 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001362 }
1363
Ted Kremenekde0d2632010-01-05 02:18:06 +00001364 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001365 if (isa<ElementRegion>(R)) {
1366 // Currently we don't reason specially about Clang-style vectors. Check
1367 // if superR is a vector and if so return Unknown.
1368 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1369 if (typedSuperR->getValueType(getContext())->isVectorType())
1370 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001371 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001372 }
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001374 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001375 }
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001377 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001378 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001379}
Mike Stump1eb44332009-09-09 15:08:12 +00001380
1381SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001382 const ObjCIvarRegion* R) {
1383
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001384 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001385 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001386
Zhongxing Xu13d50172009-10-11 08:08:02 +00001387 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001388 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001390 const MemRegion *superR = R->getSuperRegion();
1391
Ted Kremenekab22ee92009-10-20 01:20:57 +00001392 // Check if the super region has a default binding.
1393 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001394 if (SymbolRef parentSym = V->getAsSymbol())
1395 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001397 // Other cases: give up.
1398 return UnknownVal();
1399 }
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Ted Kremenek25c54572009-07-20 22:58:02 +00001401 return RetrieveLazySymbol(state, R);
1402}
1403
Ted Kremenek9031dd72009-07-21 00:12:07 +00001404SVal RegionStoreManager::RetrieveVar(const GRState *state,
1405 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Ted Kremenek9031dd72009-07-21 00:12:07 +00001407 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001408 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Zhongxing Xu13d50172009-10-11 08:08:02 +00001410 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001411 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Ted Kremenek9031dd72009-07-21 00:12:07 +00001413 // Lazily derive a value for the VarRegion.
1414 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001416 if (R->hasGlobalsOrParametersStorage() ||
1417 isa<UnknownSpaceRegion>(R->getMemorySpace()))
Ted Kremenek28172a22009-12-15 23:23:27 +00001418 return ValMgr.getRegionValueSymbolVal(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Ted Kremenek9031dd72009-07-21 00:12:07 +00001420 return UndefinedVal();
1421}
1422
Mike Stump1eb44332009-09-09 15:08:12 +00001423SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001424 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Ted Kremenek25c54572009-07-20 22:58:02 +00001426 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001427
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001428 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001429 return ValMgr.getRegionValueSymbolVal(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001430}
1431
Mike Stump1eb44332009-09-09 15:08:12 +00001432SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1433 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001434 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001435 assert(T->isStructureType());
1436
Zhongxing Xub7507d12009-06-11 07:27:30 +00001437 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001438 RecordDecl* RD = RT->getDecl();
1439 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001440 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001441#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001442 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1443
Ted Kremenek67f28532009-06-17 22:02:04 +00001444 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1445 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001446 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001447
Douglas Gregore267ff32008-12-11 20:41:00 +00001448 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1449 FieldEnd = Fields.rend();
1450 Field != FieldEnd; ++Field) {
1451 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001452 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001453 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001454 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1455 }
1456
Zhongxing Xud91ee272009-06-23 09:02:15 +00001457 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001458#else
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001459 return ValMgr.makeLazyCompoundVal(state->getStore(), R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001460#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001461}
1462
Ted Kremenek67f28532009-06-17 22:02:04 +00001463SVal RegionStoreManager::RetrieveArray(const GRState *state,
1464 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001465#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001466 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001467 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1468
1469 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001470 uint64_t size = CAT->getSize().getZExtValue();
1471 for (uint64_t i = 0; i < size; ++i) {
1472 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001473 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001474 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001475 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001476 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001477 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1478 }
1479
Zhongxing Xud91ee272009-06-23 09:02:15 +00001480 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001481#else
1482 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001483 return ValMgr.makeLazyCompoundVal(state->getStore(), R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001484#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001485}
1486
Ted Kremenek9af46f52009-06-16 22:36:44 +00001487//===----------------------------------------------------------------------===//
1488// Binding values to regions.
1489//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001490
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001491Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001492 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001493 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001494 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Ted Kremenek0964a062009-01-21 06:57:53 +00001496 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001497}
1498
Ted Kremenek67f28532009-06-17 22:02:04 +00001499const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001500 if (isa<loc::ConcreteInt>(L))
1501 return state;
1502
Ted Kremenek9af46f52009-06-16 22:36:44 +00001503 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001504 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek9af46f52009-06-16 22:36:44 +00001506 // Check if the region is a struct region.
1507 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1508 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001509 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001511 // Special case: the current region represents a cast and it and the super
1512 // region both have pointer types or intptr_t types. If so, perform the
1513 // bind to the super region.
1514 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001515 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001516 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1517 if (ER->getIndex().isZeroConstant()) {
1518 if (const TypedRegion *superR =
1519 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1520 ASTContext &Ctx = getContext();
1521 QualType superTy = superR->getValueType(Ctx);
1522 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001523
1524 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001525 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +00001526 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
1527 return Bind(state, loc::MemRegionVal(superR), V);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001528 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001529 // For now, just invalidate the fields of the struct/union/class.
1530 // FIXME: Precisely handle the fields of the record.
1531 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001532 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001533 }
1534 }
1535 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001536 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1537 // Binding directly to a symbolic region should be treated as binding
1538 // to element 0.
1539 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek852274d2009-12-16 03:18:58 +00001540
1541 // FIXME: Is this the right way to handle symbols that are references?
1542 if (const PointerType *PT = T->getAs<PointerType>())
1543 T = PT->getPointeeType();
1544 else
1545 T = T->getAs<ReferenceType>()->getPointeeType();
1546
Ted Kremenek0954cde2009-09-24 04:11:44 +00001547 R = GetElementZeroRegion(SR, T);
1548 }
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001550 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001551 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001552 return state->makeWithStore(Add(B, R, BindingKey::Direct, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001553}
1554
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001555const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001556 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001557 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001558
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001559 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001560
Ted Kremenek0964a062009-01-21 06:57:53 +00001561 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001562 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001563 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001564 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001565
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001566 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001567}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001568
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001569// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001570const GRState *
1571RegionStoreManager::BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +00001572 const CompoundLiteralExpr *CL,
1573 const LocationContext *LC,
Ted Kremenek67f28532009-06-17 22:02:04 +00001574 SVal V) {
Ted Kremenek67d12872009-12-07 22:05:27 +00001575 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
1576 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001577}
1578
Ted Kremenek027e2662009-11-19 20:20:24 +00001579const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1580 const MemRegion *R,
1581 QualType T) {
1582 Store store = state->getStore();
1583 RegionBindings B = GetRegionBindings(store);
1584 SVal V;
1585
1586 if (Loc::IsLocType(T))
1587 V = ValMgr.makeNull();
1588 else if (T->isIntegerType())
1589 V = ValMgr.makeZeroVal(T);
1590 else if (T->isStructureType() || T->isArrayType()) {
1591 // Set the default value to a zero constant when it is a structure
1592 // or array. The type doesn't really matter.
1593 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1594 }
1595 else {
1596 return state;
1597 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001598
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001599 return state->makeWithStore(Add(B, R, BindingKey::Default, V).getRoot());
Ted Kremenek027e2662009-11-19 20:20:24 +00001600}
1601
Ted Kremenek67f28532009-06-17 22:02:04 +00001602const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001603 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001604 SVal Init) {
Ted Kremenekfee90812010-01-26 23:51:00 +00001605
1606 ASTContext &Ctx = getContext();
1607 const ArrayType *AT =
1608 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
1609 QualType ElementTy = AT->getElementType();
1610 Optional<uint64_t> Size;
1611
1612 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1613 Size = CAT->getSize().getZExtValue();
1614
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001615 // Check if the init expr is a StringLiteral.
1616 if (isa<loc::MemRegionVal>(Init)) {
1617 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1618 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1619 const char* str = S->getStrData();
1620 unsigned len = S->getByteLength();
1621 unsigned j = 0;
1622
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001623 // Copy bytes from the string literal into the target array. Trailing bytes
1624 // in the array that are not covered by the string literal are initialized
1625 // to zero.
Ted Kremenekfee90812010-01-26 23:51:00 +00001626
1627 // We assume that string constants are bound to
1628 // constant arrays.
Ted Kremenek39c2ea12010-01-27 16:31:37 +00001629 uint64_t size = Size.getValue();
Ted Kremenekfee90812010-01-26 23:51:00 +00001630
Ted Kremenek46537392009-07-16 01:33:37 +00001631 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001632 if (j >= len)
1633 break;
1634
Ted Kremenek46537392009-07-16 01:33:37 +00001635 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001636 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1637 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001638
Zhongxing Xud91ee272009-06-23 09:02:15 +00001639 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001640 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001641 }
1642
Ted Kremenek67f28532009-06-17 22:02:04 +00001643 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001644 }
1645
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001646 // Handle lazy compound values.
1647 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1648 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001649
1650 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001651
1652 if (Init.isUnknown())
1653 return setImplicitDefaultValue(state, R, ElementTy);
1654
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001655 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001656 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001657 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Ted Kremenekfee90812010-01-26 23:51:00 +00001659 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001660 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001661 if (VI == VE)
1662 break;
1663
Ted Kremenek46537392009-07-16 01:33:37 +00001664 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001665 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001666
Ted Kremenekfee90812010-01-26 23:51:00 +00001667 if (ElementTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001668 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001669 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001670 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001671 }
1672
Ted Kremenek027e2662009-11-19 20:20:24 +00001673 // If the init list is shorter than the array length, set the
1674 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001675 if (Size.hasValue() && i < Size.getValue())
Ted Kremenek027e2662009-11-19 20:20:24 +00001676 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001677
Ted Kremenek67f28532009-06-17 22:02:04 +00001678 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001679}
1680
Ted Kremenek67f28532009-06-17 22:02:04 +00001681const GRState *
1682RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1683 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Ted Kremenek67f28532009-06-17 22:02:04 +00001685 if (!Features.supportsFields())
1686 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001688 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001689 assert(T->isStructureType());
1690
Ted Kremenek6217b802009-07-29 21:53:49 +00001691 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001692 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001693
1694 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001695 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001696
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001697 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001698 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001699 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Ted Kremenek67f28532009-06-17 22:02:04 +00001701 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1702 // Ignore them and kill the field values.
1703 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001704 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001705
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001706 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001707 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001708
1709 RecordDecl::field_iterator FI, FE;
1710
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001711 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001712
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001713 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001714 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001715
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001716 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001717 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001718
Ted Kremenekcf549592009-09-22 21:19:14 +00001719 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001720 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001721 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001722 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001723 else
1724 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001725 }
1726
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001727 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001728 if (FI != FE) {
1729 Store store = state->getStore();
1730 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001731 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001732 state = state->makeWithStore(B.getRoot());
1733 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001734
Ted Kremenek67f28532009-06-17 22:02:04 +00001735 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001736}
1737
Zhongxing Xu13d50172009-10-11 08:08:02 +00001738Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1739 RegionBindings B = GetRegionBindings(store);
1740 llvm::OwningPtr<RegionStoreSubRegionMap>
1741 SubRegions(getRegionStoreSubRegionMap(store));
1742 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001743
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001744 // Set the default value of the struct region to "unknown".
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001745 return Add(B, R, BindingKey::Default, UnknownVal()).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001746}
1747
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001748const GRState*
1749RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1750 const GRState *state,
1751 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001752
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001753 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001754 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001755
Mike Stump1eb44332009-09-09 15:08:12 +00001756 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001757 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001758
Mike Stump1eb44332009-09-09 15:08:12 +00001759 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001760 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001762 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1763 // results in a zero-copy algorithm.
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001764 return state->makeWithStore(Add(B, R, BindingKey::Direct, V).getRoot());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001765}
1766
1767//===----------------------------------------------------------------------===//
1768// "Raw" retrievals and bindings.
1769//===----------------------------------------------------------------------===//
1770
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001771BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001772 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1773 const RegionRawOffset &O = ER->getAsRawOffset();
1774
1775 if (O.getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001776 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001777
1778 // FIXME: There are some ElementRegions for which we cannot compute
1779 // raw offsets yet, including regions with symbolic offsets.
1780 }
1781
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001782 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001783}
1784
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001785RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001786 return RBFactory.Add(B, K, V);
1787}
1788
1789RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001790 BindingKey::Kind k, SVal V) {
1791 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001792}
1793
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001794const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001795 return B.lookup(K);
1796}
1797
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001798const SVal *RegionStoreManager::Lookup(RegionBindings B,
1799 const MemRegion *R,
1800 BindingKey::Kind k) {
1801 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001802}
1803
1804RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1805 return RBFactory.Remove(B, K);
1806}
1807
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001808RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1809 BindingKey::Kind k){
1810 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001811}
1812
1813Store RegionStoreManager::Remove(Store store, BindingKey K) {
1814 RegionBindings B = GetRegionBindings(store);
1815 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001816}
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Ted Kremenek9af46f52009-06-16 22:36:44 +00001818//===----------------------------------------------------------------------===//
1819// State pruning.
1820//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001821
Mike Stump1eb44332009-09-09 15:08:12 +00001822void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001823 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001824 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001825{
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001826 typedef std::pair<Store, const MemRegion *> RBDNode;
Ted Kremenek781115c2009-10-17 17:45:11 +00001827
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001828 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001829 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Ted Kremenek9af46f52009-06-16 22:36:44 +00001831 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001832 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001833 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001834
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001835 // Do a pass over the regions in the store. For VarRegions we check if
1836 // the variable is still live and if so add it to the list of live roots.
1837 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001838 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001839
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001840 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001841 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001842 const MemRegion *R = I.getKey().getRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001843 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001844 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001845
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001846 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001847 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001848 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001849 llvm::SmallVector<RBDNode, 10> Postponed;
1850
Zhongxing Xu6800b332009-10-18 04:15:47 +00001851 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001852
Ted Kremenek9af46f52009-06-16 22:36:44 +00001853 while (!IntermediateRoots.empty()) {
1854 const MemRegion* R = IntermediateRoots.back();
1855 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001856
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001857 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001858 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001859 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001860
Ted Kremenek9af46f52009-06-16 22:36:44 +00001861 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekedeb5b62009-12-04 20:32:20 +00001862 if (SymReaper.isLive(Loc, VR))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001863 WorkList.push_back(std::make_pair(store, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001864 continue;
1865 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001866
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001867 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001868 llvm::SmallVectorImpl<RBDNode> &Q =
1869 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1870
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001871 Q.push_back(std::make_pair(store, SR));
Ted Kremenek01756192009-10-29 05:14:17 +00001872
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001873 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001874 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001875
1876 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001877 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001878 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001879 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001880 }
Mike Stump1eb44332009-09-09 15:08:12 +00001881
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001882 // Enqueue the RegionRoots onto WorkList.
1883 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1884 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001885 WorkList.push_back(std::make_pair(store, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001886 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001887 RegionRoots.clear();
1888
Zhongxing Xu6800b332009-10-18 04:15:47 +00001889 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001890
Ted Kremenek01756192009-10-29 05:14:17 +00001891tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001892 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001893 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001894 WorkList.pop_back();
1895
1896 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001897 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001898 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001899 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001901 const MemRegion *R = N.second;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001902 Store store_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001903
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001904 // Enqueue subregions.
1905 RegionStoreSubRegionMap *M;
1906
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001907 if (store == store_N)
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001908 M = SubRegions.get();
1909 else {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001910 RegionStoreSubRegionMap *& SM = SC[store_N];
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001911 if (!SM)
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001912 SM = getRegionStoreSubRegionMap(store_N);
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001913 M = SM;
1914 }
Ted Kremenekdf165012010-02-02 22:38:47 +00001915
1916 if (const RegionStoreSubRegionMap::Set *S = M->getSubRegions(R))
1917 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
1918 I != E; ++I)
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001919 WorkList.push_back(std::make_pair(store_N, *I));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001920
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001921 // Enqueue the super region.
1922 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1923 const MemRegion *superR = SR->getSuperRegion();
1924 if (!isa<MemSpaceRegion>(superR)) {
1925 // If 'R' is a field or an element, we want to keep the bindings
1926 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001927 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001928 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1929 || isa<ObjCIvarRegion>(R));
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001930 WorkList.push_back(std::make_pair(store_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001931 }
1932 }
1933
1934 // Mark the symbol for any live SymbolicRegion as "live". This means we
1935 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001936 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001937 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001938
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001939 // For BlockDataRegions, enqueue the VarRegions for variables marked
1940 // with __block (passed-by-reference).
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001941 // via BlockDeclRefExprs.
1942 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1943 for (BlockDataRegion::referenced_vars_iterator
1944 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001945 RI != RE; ++RI) {
1946 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001947 WorkList.push_back(std::make_pair(store_N, *RI));
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001948 }
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001949 // No possible data bindings on a BlockDataRegion. Continue to the
1950 // next region in the worklist.
1951 continue;
1952 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001953
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001954 RegionBindings B_N = GetRegionBindings(store_N);
1955
1956 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001957 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001958
Zhongxing Xu13d50172009-10-11 08:08:02 +00001959 if (V) {
1960 // Check for lazy bindings.
1961 if (const nonloc::LazyCompoundVal *LCV =
1962 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001963
Zhongxing Xu13d50172009-10-11 08:08:02 +00001964 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001965 WorkList.push_back(std::make_pair(D->getStore(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001966 }
1967 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001968 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001969 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001970 SI!=SE;++SI)
1971 SymReaper.markLive(*SI);
1972
Zhongxing Xu13d50172009-10-11 08:08:02 +00001973 // If V is a region, then add it to the worklist.
1974 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001975 WorkList.push_back(std::make_pair(store_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001976 }
1977 }
1978 }
1979
Ted Kremenek01756192009-10-29 05:14:17 +00001980 // See if any postponed SymbolicRegions are actually live now, after
1981 // having done a scan.
1982 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1983 E = Postponed.end() ; I != E ; ++I) {
1984 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1985 if (SymReaper.isLive(SR->getSymbol())) {
1986 WorkList.push_back(*I);
1987 I->second = NULL;
1988 }
1989 }
1990 }
1991
1992 if (!WorkList.empty())
1993 goto tryAgain;
1994
Ted Kremenek9af46f52009-06-16 22:36:44 +00001995 // We have now scanned the store, marking reachable regions and symbols
1996 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001997 // as well as update DSymbols with the set symbols that are now dead.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001998 Store new_store = store;
Ted Kremenek451ac092009-08-06 04:50:20 +00001999 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00002000 const MemRegion* R = I.getKey().getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00002001 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00002002 if (Visited.count(std::make_pair(store, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00002003 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Ted Kremenek9af46f52009-06-16 22:36:44 +00002005 // Remove this dead region from the store.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00002006 new_store = Remove(new_store, I.getKey());
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Ted Kremenek9af46f52009-06-16 22:36:44 +00002008 // Mark all non-live symbols that this region references as dead.
2009 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
2010 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Ted Kremeneke393f4a2010-02-03 03:06:46 +00002012 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00002013 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
2014 for (; SI != SE; ++SI)
2015 SymReaper.maybeDead(*SI);
2016 }
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Ted Kremenek2f26bc32009-08-02 04:45:08 +00002018 // Write the store back.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00002019 state.setStore(new_store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00002020}
2021
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00002022GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
2023 StackFrameContext const *frame) {
2024 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
2025 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
2026
2027 FunctionDecl::param_const_iterator PI = FD->param_begin();
2028
2029 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
2030
2031 // Copy the arg expression value to the arg variables.
2032 for (; AI != AE; ++AI, ++PI) {
2033 SVal ArgVal = state->getSVal(*AI);
Ted Kremenekb48ad642009-12-04 00:26:31 +00002034 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00002035 }
2036
2037 return state;
2038}
2039
Ted Kremenek9af46f52009-06-16 22:36:44 +00002040//===----------------------------------------------------------------------===//
2041// Utility methods.
2042//===----------------------------------------------------------------------===//
2043
Ted Kremenek53ba0b62009-06-24 23:06:47 +00002044void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00002045 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00002046 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00002047 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00002048
Ted Kremenek451ac092009-08-06 04:50:20 +00002049 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002050 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00002051}