blob: d829199d3523ad376913c3a9772776a14c53b8f9 [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)
47 : P(r, (unsigned) k), Offset(offset) {}
48public:
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
204 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
205 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 Kremenekdf165012010-02-02 22:38:47 +0000211 RBFactory(mgr.getAllocator(), 3) {}
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
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000358 SValuator::CastResult Retrieve(const GRState *state, Loc L,
359 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000360
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000361 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000362
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000363 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000365 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek9031dd72009-07-21 00:12:07 +0000367 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek25c54572009-07-20 22:58:02 +0000369 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000371 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
372 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek67f28532009-06-17 22:02:04 +0000374 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000375 /// struct copy:
376 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000377 /// x = y;
378 /// y's value is retrieved by this method.
379 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek67f28532009-06-17 22:02:04 +0000381 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000383 /// Get the state and region whose binding this region R corresponds to.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000384 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000385 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000387 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
388 const GRState *state,
389 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000390
Ted Kremenek0954cde2009-09-24 04:11:44 +0000391 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
392 QualType T);
393
Ted Kremenek67f28532009-06-17 22:02:04 +0000394 //===------------------------------------------------------------------===//
395 // State pruning.
396 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek67f28532009-06-17 22:02:04 +0000398 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
399 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000400 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000401 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
402
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000403 const GRState *EnterStackFrame(const GRState *state,
404 const StackFrameContext *frame);
405
Ted Kremenek67f28532009-06-17 22:02:04 +0000406 //===------------------------------------------------------------------===//
407 // Region "extents".
408 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000410 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000411 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000412 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000413
414 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000415 // Utility methods.
416 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek451ac092009-08-06 04:50:20 +0000418 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000419 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000420 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000421
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000422 void print(Store store, llvm::raw_ostream& Out, const char* nl,
423 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000424
425 void iterBindings(Store store, BindingsHandler& f) {
426 // FIXME: Implement.
427 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000428
Ted Kremenek67f28532009-06-17 22:02:04 +0000429 // FIXME: Remove.
430 BasicValueFactory& getBasicVals() {
431 return StateMgr.getBasicVals();
432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek67f28532009-06-17 22:02:04 +0000434 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000435 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000436};
437
438} // end anonymous namespace
439
Ted Kremenek9af46f52009-06-16 22:36:44 +0000440//===----------------------------------------------------------------------===//
441// RegionStore creation.
442//===----------------------------------------------------------------------===//
443
444StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
445 RegionStoreFeatures F = maximal_features_tag();
446 return new RegionStoreManager(StMgr, F);
447}
448
449StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
450 RegionStoreFeatures F = minimal_features_tag();
451 F.enableFields(true);
452 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000453}
454
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000455void
456RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000457 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000458 const MemRegion *superR = R->getSuperRegion();
459 if (add(superR, R))
460 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000461 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000462}
463
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000464RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000465RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
466 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000467 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000469 llvm::SmallVector<const SubRegion*, 10> WL;
470
Ted Kremenek451ac092009-08-06 04:50:20 +0000471 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000472 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000473 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Mike Stump1eb44332009-09-09 15:08:12 +0000475 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000476 // don't have direct bindings but are super regions of those that do.
477 while (!WL.empty()) {
478 const SubRegion *R = WL.back();
479 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000480 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000481 }
482
Ted Kremenek14453bf2009-03-03 19:02:42 +0000483 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000484}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000485
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000486SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000487 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000488}
489
Ted Kremenek9af46f52009-06-16 22:36:44 +0000490//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000491// Binding invalidation.
492//===----------------------------------------------------------------------===//
493
Ted Kremenekdf165012010-02-02 22:38:47 +0000494
Zhongxing Xu13d50172009-10-11 08:08:02 +0000495void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
496 const MemRegion *R,
497 RegionStoreSubRegionMap &M) {
Ted Kremenekdf165012010-02-02 22:38:47 +0000498
499 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
500 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
501 I != E; ++I)
502 RemoveSubRegionBindings(B, *I, M);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000503
504 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000505}
506
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000507namespace {
508class InvalidateRegionsWorker {
Ted Kremenek5b290652010-02-03 04:16:00 +0000509 typedef BumpVector<BindingKey> RegionCluster;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000510 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5b290652010-02-03 04:16:00 +0000511 typedef llvm::SmallVector<std::pair<const MemRegion *,RegionCluster*>, 10>
512 WorkList;
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000514 BumpVectorContext BVC;
515 ClusterMap ClusterM;
516 WorkList WL;
517public:
518 const GRState *InvalidateRegions(RegionStoreManager &RM,
519 const GRState *state,
520 const MemRegion * const *I,
521 const MemRegion * const *E,
522 const Expr *Ex,
523 unsigned Count,
524 StoreManager::InvalidatedSymbols *IS);
Ted Kremenek87806792009-09-27 20:45:21 +0000525
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000526private:
Ted Kremenek5b290652010-02-03 04:16:00 +0000527 void AddToWorkList(BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000528 void AddToWorkList(const MemRegion *R);
Ted Kremenek5b290652010-02-03 04:16:00 +0000529 void AddToCluster(BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000530 RegionCluster **getCluster(const MemRegion *R);
531};
532}
Zhongxing Xu13d50172009-10-11 08:08:02 +0000533
Ted Kremenek5b290652010-02-03 04:16:00 +0000534void InvalidateRegionsWorker::AddToCluster(BindingKey K) {
535 const MemRegion *R = K.getRegion();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000536 const MemRegion *baseR = R->getBaseRegion();
537 RegionCluster **CPtr = getCluster(baseR);
Ted Kremenek5b290652010-02-03 04:16:00 +0000538 assert(*CPtr);
539 (*CPtr)->push_back(K, BVC);
540}
541
542void InvalidateRegionsWorker::AddToWorkList(BindingKey K) {
543 AddToWorkList(K.getRegion());
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000544}
545
546void InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000547 const MemRegion *baseR = R->getBaseRegion();
548 RegionCluster **CPtr = getCluster(baseR);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000549 if (RegionCluster *C = *CPtr) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000550 WL.push_back(std::make_pair(baseR, C));
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000551 *CPtr = NULL;
552 }
553}
554
555InvalidateRegionsWorker::RegionCluster **
556InvalidateRegionsWorker::getCluster(const MemRegion *R) {
557 RegionCluster *&CRef = ClusterM[R];
558 if (!CRef) {
559 void *Mem = BVC.getAllocator().Allocate<RegionCluster>();
560 CRef = new (Mem) RegionCluster(BVC, 10);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000561 }
562 return &CRef;
563}
564
565const GRState *
566InvalidateRegionsWorker::InvalidateRegions(RegionStoreManager &RM,
567 const GRState *state,
568 const MemRegion * const *I,
569 const MemRegion * const *E,
570 const Expr *Ex, unsigned Count,
571 StoreManager::InvalidatedSymbols *IS)
572{
573 ASTContext &Ctx = state->getStateManager().getContext();
574 ValueManager &ValMgr = state->getStateManager().getValueManager();
575 RegionBindings B = RegionStoreManager::GetRegionBindings(state->getStore());
576
577 // Scan the entire store and make the region clusters.
578 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000579 AddToCluster(RI.getKey());
580 if (const MemRegion *R = RI.getData().getAsRegion()) {
581 // Generate a cluster, but don't add the region to the cluster
582 // if there aren't any bindings.
583 getCluster(R->getBaseRegion());
584 }
Ted Kremenek81a95832009-12-03 03:27:11 +0000585 }
Ted Kremenek87806792009-09-27 20:45:21 +0000586
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000587 // Add the cluster for I .. E to a worklist.
588 for ( ; I != E; ++I)
589 AddToWorkList(*I);
590
591 while (!WL.empty()) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000592 const MemRegion *baseR;
593 RegionCluster *C;
594 llvm::tie(baseR, C) = WL.back();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000595 WL.pop_back();
Ted Kremenek87806792009-09-27 20:45:21 +0000596
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000597 for (RegionCluster::iterator I = C->begin(), E = C->end(); I != E; ++I) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000598 BindingKey K = *I;
Ted Kremenek473e1672009-10-16 00:30:49 +0000599
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000600 // Get the old binding. Is it a region? If so, add it to the worklist.
Ted Kremenek5b290652010-02-03 04:16:00 +0000601 if (const SVal *V = RM.Lookup(B, K)) {
602 if (const MemRegion *R = V->getAsRegion())
603 AddToWorkList(R);
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000604
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000605 // A symbol? Mark it touched by the invalidation.
606 if (IS)
607 if (SymbolRef Sym = V->getAsSymbol())
608 IS->insert(Sym);
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000609 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000610
Ted Kremenek5b290652010-02-03 04:16:00 +0000611 B = RM.Remove(B, K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000612 }
Ted Kremenek5b290652010-02-03 04:16:00 +0000613
614 // Now inspect the base region.
615
616 if (IS) {
617 // Symbolic region? Mark that symbol touched by the invalidation.
618 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
619 IS->insert(SR->getSymbol());
620 }
621
622 // BlockDataRegion? If so, invalidate captured variables that are passed
623 // by reference.
624 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
625 for (BlockDataRegion::referenced_vars_iterator
626 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
627 I != E; ++I) {
628 const VarRegion *VR = *I;
629 if (VR->getDecl()->getAttr<BlocksAttr>())
630 AddToWorkList(VR);
631 }
632 continue;
633 }
634
635 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
636 // Invalidate the region by setting its default value to
637 // conjured symbol. The type of the symbol is irrelavant.
638 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
639 Count);
640 B = RM.Add(B, baseR, BindingKey::Default, V);
641 continue;
642 }
643
644 if (!baseR->isBoundable())
645 continue;
646
647 const TypedRegion *TR = cast<TypedRegion>(baseR);
648 QualType T = TR->getValueType(Ctx);
649
650 // Invalidate the binding.
651 if (const RecordType *RT = T->getAsStructureType()) {
652 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
653 // No record definition. There is nothing we can do.
654 if (!RD) {
655 B = RM.Remove(B, baseR);
656 continue;
657 }
658
659 // Invalidate the region by setting its default value to
660 // conjured symbol. The type of the symbol is irrelavant.
661 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
662 Count);
663 B = RM.Add(B, baseR, BindingKey::Default, V);
664 continue;
665 }
666
667 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
668 // Set the default value of the array to conjured symbol.
669 DefinedOrUnknownSVal V =
670 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
671 B = RM.Add(B, baseR, BindingKey::Default, V);
672 continue;
673 }
674
675 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
676 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
677 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000678 }
679
Ted Kremenek87806792009-09-27 20:45:21 +0000680 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000681 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000682}
683
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000684const GRState *RegionStoreManager::InvalidateRegions(const GRState *state,
685 const MemRegion * const *I,
686 const MemRegion * const *E,
687 const Expr *Ex,
688 unsigned Count,
689 InvalidatedSymbols *IS) {
690 InvalidateRegionsWorker W;
691 return W.InvalidateRegions(*this, state, I, E, Ex, Count, IS);
692}
693
694
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000695//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000696// getLValueXXX methods.
697//===----------------------------------------------------------------------===//
698
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000699/// getLValueString - Returns an SVal representing the lvalue of a
700/// StringLiteral. Within RegionStore a StringLiteral has an
701/// associated StringRegion, and the lvalue of a StringLiteral is the
702/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000703SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000704 return loc::MemRegionVal(MRMgr.getStringRegion(S));
705}
706
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000707/// getLValueVar - Returns an SVal that represents the lvalue of a
708/// variable. Within RegionStore a variable has an associated
709/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000710SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000711 const LocationContext *LC) {
712 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000713}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000714
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000715SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
716 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000717}
718
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000719SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
720 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000721}
722
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000723SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000724 if (Base.isUnknownOrUndef())
725 return Base;
726
727 Loc BaseL = cast<Loc>(Base);
728 const MemRegion* BaseR = 0;
729
730 switch (BaseL.getSubKind()) {
731 case loc::MemRegionKind:
732 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
733 break;
734
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000735 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000736 // These are anormal cases. Flag an undefined value.
737 return UndefinedVal();
738
739 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000740 // While these seem funny, this can happen through casts.
741 // FIXME: What we should return is the field offset. For example,
742 // add the field offset to the integer value. That way funny things
743 // like this work properly: &(((struct foo *) 0xa)->f)
744 return Base;
745
746 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000747 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000748 return Base;
749 }
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000751 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
752 // of FieldDecl.
753 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
754 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000755
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000756 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000757}
758
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000759SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
760 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000761
Ted Kremenekde7ec632009-03-09 22:44:49 +0000762 // If the base is an unknown or undefined value, just return it back.
763 // FIXME: For absolute pointer addresses, we just return that value back as
764 // well, although in reality we should return the offset added to that
765 // value.
766 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000767 return Base;
768
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000769 // Only handle integer offsets... for now.
770 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000771 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000772
Zhongxing Xuce760782009-05-09 13:20:07 +0000773 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000774
775 // Pointer of any type can be cast and used as array base.
776 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Ted Kremenek46537392009-07-16 01:33:37 +0000778 // Convert the offset to the appropriate size and signedness.
779 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000781 if (!ElemR) {
782 //
783 // If the base region is not an ElementRegion, create one.
784 // This can happen in the following example:
785 //
786 // char *p = __builtin_alloc(10);
787 // p[1] = 8;
788 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000789 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000790 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000791 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000792 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000795 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000797 if (!isa<nonloc::ConcreteInt>(BaseIdx))
798 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000800 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
801 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
802 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek46537392009-07-16 01:33:37 +0000804 // Compute the new index.
805 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremenek46537392009-07-16 01:33:37 +0000807 // Construct the new ElementRegion.
808 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000809 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000810 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000811}
812
Ted Kremenek9af46f52009-06-16 22:36:44 +0000813//===----------------------------------------------------------------------===//
814// Extents for regions.
815//===----------------------------------------------------------------------===//
816
Zhongxing Xue884ff82009-11-12 02:48:32 +0000817DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000818 const MemRegion *R,
819 QualType EleTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000821 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000822 case MemRegion::CXXThisRegionKind:
823 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000824 case MemRegion::GenericMemSpaceRegionKind:
825 case MemRegion::StackLocalsSpaceRegionKind:
826 case MemRegion::StackArgumentsSpaceRegionKind:
827 case MemRegion::HeapSpaceRegionKind:
828 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000829 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000830 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000831 return UnknownVal();
832
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000833 case MemRegion::FunctionTextRegionKind:
834 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000835 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000836 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000837 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000838
839 // Not yet handled.
840 case MemRegion::AllocaRegionKind:
841 case MemRegion::CompoundLiteralRegionKind:
842 case MemRegion::ElementRegionKind:
843 case MemRegion::FieldRegionKind:
844 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000845 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000846 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000848 case MemRegion::SymbolicRegionKind: {
849 const SVal *Size = state->get<RegionExtents>(R);
850 if (!Size)
851 return UnknownVal();
852 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
853 if (!CI)
854 return UnknownVal();
855
856 CharUnits RegionSize =
857 CharUnits::fromQuantity(CI->getValue().getSExtValue());
858 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
859 assert(RegionSize % EleSize == 0);
860
861 return ValMgr.makeIntVal(RegionSize / EleSize, false);
862 }
863
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000864 case MemRegion::StringRegionKind: {
865 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000866 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000867 // operations with signed indices.
868 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000871 case MemRegion::VarRegionKind: {
872 const VarRegion* VR = cast<VarRegion>(R);
873 // Get the type of the variable.
874 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000876 // FIXME: Handle variable-length arrays.
877 if (isa<VariableArrayType>(T))
878 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000880 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
881 // return the size as signed integer.
882 return ValMgr.makeIntVal(CAT->getSize(), false);
883 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000884
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000885 // Clients can use ordinary variables as if they were arrays. These
886 // essentially are arrays of size 1.
887 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000888 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000891 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000892 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000893}
894
Ted Kremenek67f28532009-06-17 22:02:04 +0000895const GRState *RegionStoreManager::setExtent(const GRState *state,
896 const MemRegion *region,
897 SVal extent) {
898 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000899}
900
901//===----------------------------------------------------------------------===//
902// Location and region casting.
903//===----------------------------------------------------------------------===//
904
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000905/// ArrayToPointer - Emulates the "decay" of an array to a pointer
906/// type. 'Array' represents the lvalue of the array being decayed
907/// to a pointer, and the returned SVal represents the decayed
908/// version of that lvalue (i.e., a pointer to the first element of
909/// the array). This is called by GRExprEngine when evaluating casts
910/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000911SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000912 if (!isa<loc::MemRegionVal>(Array))
913 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Ted Kremenekabb042f2008-12-13 19:24:37 +0000915 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
916 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000918 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000919 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000921 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000922 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000923 ArrayType *AT = cast<ArrayType>(T);
924 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Ted Kremenek75185b52009-07-16 00:00:11 +0000926 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000927 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
928 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000929}
930
Ted Kremenek9af46f52009-06-16 22:36:44 +0000931//===----------------------------------------------------------------------===//
932// Pointer arithmetic.
933//===----------------------------------------------------------------------===//
934
Mike Stump1eb44332009-09-09 15:08:12 +0000935SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000936 BinaryOperator::Opcode Op, Loc L, NonLoc R,
937 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000938 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000939 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000940 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000941
Zhongxing Xua1718c72009-04-03 07:33:13 +0000942 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000943 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000944
Ted Kremenek3bccf082009-07-11 00:58:27 +0000945 switch (MR->getKind()) {
946 case MemRegion::SymbolicRegionKind: {
947 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000948 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000949 QualType T = Sym->getType(getContext());
950 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000952 if (const PointerType *PT = T->getAs<PointerType>())
953 EleTy = PT->getPointeeType();
954 else
John McCall183700f2009-09-21 23:43:11 +0000955 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Ted Kremenek3bccf082009-07-11 00:58:27 +0000957 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
958 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000959 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000960 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000961 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000962 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000963 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000964 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000965 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
966 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000967 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000968 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000969
Ted Kremenek3bccf082009-07-11 00:58:27 +0000970 case MemRegion::ElementRegionKind: {
971 ER = cast<ElementRegion>(MR);
972 break;
973 }
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Ted Kremenek3bccf082009-07-11 00:58:27 +0000975 // Not yet handled.
976 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000977 case MemRegion::StringRegionKind: {
978
979 }
980 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000981 case MemRegion::CompoundLiteralRegionKind:
982 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000983 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000984 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000985 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000987 case MemRegion::FunctionTextRegionKind:
988 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000989 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000990 // Technically this can happen if people do funny things with casts.
991 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Ted Kremenekde0d2632010-01-05 02:18:06 +0000993 case MemRegion::CXXThisRegionKind:
994 assert(0 &&
995 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000996 case MemRegion::GenericMemSpaceRegionKind:
997 case MemRegion::StackLocalsSpaceRegionKind:
998 case MemRegion::StackArgumentsSpaceRegionKind:
999 case MemRegion::HeapSpaceRegionKind:
1000 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001001 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +00001002 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
1003 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +00001004 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +00001005
Zhongxing Xu94aa6c12009-03-02 07:52:23 +00001006 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +00001007 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +00001008
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +00001009 // For now, only support:
1010 // (a) concrete integer indices that can easily be resolved
1011 // (b) 0 + symbolic index
1012 if (Base) {
1013 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
1014 // FIXME: Should use SValuator here.
1015 SVal NewIdx =
1016 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +00001017 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +00001018 const MemRegion* NewER =
1019 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
1020 ER->getSuperRegion(), getContext());
1021 return ValMgr.makeLoc(NewER);
1022 }
1023 if (0 == Base->getValue()) {
1024 const MemRegion* NewER =
1025 MRMgr.getElementRegion(ER->getElementType(), R,
1026 ER->getSuperRegion(), getContext());
1027 return ValMgr.makeLoc(NewER);
1028 }
Ted Kremenek5dc27462009-03-03 02:51:43 +00001029 }
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Ted Kremenek5dc27462009-03-03 02:51:43 +00001031 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +00001032}
1033
Ted Kremenek9af46f52009-06-16 22:36:44 +00001034//===----------------------------------------------------------------------===//
1035// Loading values from regions.
1036//===----------------------------------------------------------------------===//
1037
Zhongxing Xu13d50172009-10-11 08:08:02 +00001038Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001039 const MemRegion *R) {
1040 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
1041 return *V;
1042
Zhongxing Xu13d50172009-10-11 08:08:02 +00001043 return Optional<SVal>();
1044}
1045
1046Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001047 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001048 if (R->isBoundable())
1049 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
1050 if (TR->getValueType(getContext())->isUnionType())
1051 return UnknownVal();
1052
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001053 if (const SVal *V = Lookup(B, R, BindingKey::Default))
1054 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +00001055
1056 return Optional<SVal>();
1057}
1058
1059Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
1060 const MemRegion *R) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001061
1062 if (Optional<SVal> V = getDirectBinding(B, R))
1063 return V;
1064
1065 return getDefaultBinding(B, R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001066}
1067
Ted Kremeneka6275a52009-07-15 02:31:43 +00001068static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
1069 RTy = Ctx.getCanonicalType(RTy);
1070 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Ted Kremeneka6275a52009-07-15 02:31:43 +00001072 if (RTy == UsedTy)
1073 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001074
1075
Ted Kremenek25c54572009-07-20 22:58:02 +00001076 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +00001077 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +00001078 // represents a reinterpretation.
1079 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001080 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +00001081 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +00001082
1083 return PUsedTy && PRTy &&
1084 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +00001085 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +00001086 }
1087
1088 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001089}
1090
Ted Kremenek0954cde2009-09-24 04:11:44 +00001091const ElementRegion *
1092RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
1093 ASTContext &Ctx = getContext();
1094 SVal idx = ValMgr.makeZeroArrayIndex();
1095 assert(!T.isNull());
1096 return MRMgr.getElementRegion(T, idx, SR, Ctx);
1097}
1098
1099
1100
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001101SValuator::CastResult
1102RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001103
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001104 assert(!isa<UnknownVal>(L) && "location unknown");
1105 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek28172a22009-12-15 23:23:27 +00001106
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001107 // FIXME: Is this even possible? Shouldn't this be treated as a null
1108 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001109 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001110 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek28172a22009-12-15 23:23:27 +00001111
Ted Kremenek67f28532009-06-17 22:02:04 +00001112 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001113
Zhongxing Xu91844122009-05-20 09:18:48 +00001114 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +00001115 // Example:
1116 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +00001117 // char* p = alloca();
1118 // read(p);
1119 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +00001120 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001121 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Ted Kremenek0954cde2009-09-24 04:11:44 +00001123 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1124 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Ted Kremenek968f0a62009-08-03 21:41:46 +00001126 if (isa<CodeTextRegion>(MR))
1127 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001129 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1130 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001131 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001132 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001133
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001134 // FIXME: We should eventually handle funny addressing. e.g.:
1135 //
1136 // int x = ...;
1137 // int *p = &x;
1138 // char *q = (char*) p;
1139 // char c = *q; // returns the first byte of 'x'.
1140 //
1141 // Such funny addressing will occur due to layering of regions.
1142
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001143#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001144 ASTContext &Ctx = getContext();
1145 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001146 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1147 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001148 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001149 assert(Ctx.getCanonicalType(RTy) ==
1150 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001151 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001152#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001153
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001154 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001155 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001157 // FIXME: Handle unions.
1158 if (RTy->isUnionType())
1159 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001160
1161 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001162 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001163
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001164 // FIXME: handle Vector types.
1165 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001166 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001167
1168 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001169 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001170 CastRetrievedVal(RetrieveField(state, FR), FR,
1171 T, false));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001172
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001173 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1174 // FIXME: Here we actually perform an implicit conversion from the loaded
1175 // value to the element type. Eventually we want to compose these values
1176 // more intelligently. For example, an 'element' can encompass multiple
1177 // bound regions (e.g., several bound bytes), or could be a subset of
1178 // a larger value.
Zhongxing Xu652be342009-11-16 04:49:44 +00001179 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001180 CastRetrievedVal(RetrieveElement(state, ER),
1181 ER, T, false));
1182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001184 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1185 // FIXME: Here we actually perform an implicit conversion from the loaded
1186 // value to the ivar type. What we should model is stores to ivars
1187 // that blow past the extent of the ivar. If the address of the ivar is
1188 // reinterpretted, it is possible we stored a different value that could
1189 // fit within the ivar. Either we need to cast these when storing them
1190 // or reinterpret them lazily (as we do here).
Zhongxing Xu652be342009-11-16 04:49:44 +00001191 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001192 CastRetrievedVal(RetrieveObjCIvar(state, IVR),
1193 IVR, T, false));
1194 }
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001196 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1197 // FIXME: Here we actually perform an implicit conversion from the loaded
1198 // value to the variable type. What we should model is stores to variables
1199 // that blow past the extent of the variable. If the address of the
1200 // variable is reinterpretted, it is possible we stored a different value
1201 // that could fit within the variable. Either we need to cast these when
1202 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu652be342009-11-16 04:49:44 +00001203 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001204 CastRetrievedVal(RetrieveVar(state, VR), VR, T,
1205 false));
1206 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001207
Ted Kremenek451ac092009-08-06 04:50:20 +00001208 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001209 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001210
1211 // Check if the region has a binding.
1212 if (V)
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001213 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001214
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001215 // The location does not have a bound value. This means that it has
1216 // the value it had upon its creation and/or entry to the analyzed
1217 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001218 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001219 // All stack variables are considered to have undefined values
1220 // upon creation. All heap allocated blocks are considered to
1221 // have undefined values as well unless they are explicitly bound
1222 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001223 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001224 }
1225
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001226 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001227 return SValuator::CastResult(state, ValMgr.getRegionValueSymbolVal(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001228}
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001230std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001231RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001232 if (Optional<SVal> OV = getDirectBinding(B, R))
1233 if (const nonloc::LazyCompoundVal *V =
1234 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1235 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001237 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1238 const std::pair<const GRState *, const MemRegion *> &X =
1239 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001241 if (X.first)
1242 return std::make_pair(X.first,
1243 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001244 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001245 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1246 const std::pair<const GRState *, const MemRegion *> &X =
1247 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001249 if (X.first)
1250 return std::make_pair(X.first,
1251 MRMgr.getFieldRegionWithSuper(FR, X.second));
1252 }
1253
1254 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1255}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001256
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001257SVal RegionStoreManager::RetrieveElement(const GRState* state,
1258 const ElementRegion* R) {
1259 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001260 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001261 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001262 return *V;
1263
Ted Kremenek921109a2009-07-01 23:19:52 +00001264 const MemRegion* superR = R->getSuperRegion();
1265
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001266 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001267 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001268 // FIXME: Handle loads from strings where the literal is treated as
1269 // an integer, e.g., *((unsigned int*)"hello")
1270 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001271 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001272 if (T != Ctx.getCanonicalType(R->getElementType()))
1273 return UnknownVal();
1274
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001275 const StringLiteral *Str = StrR->getStringLiteral();
1276 SVal Idx = R->getIndex();
1277 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1278 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001279 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001280 if (i > byteLength) {
1281 // Buffer overflow checking in GRExprEngine should handle this case,
1282 // but we shouldn't rely on it to not overflow here if that checking
1283 // is disabled.
1284 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001285 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001286 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001287 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001288 }
1289 }
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001291 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001292 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001293 if (SymbolRef parentSym = V->getAsSymbol())
1294 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001295
1296 if (V->isUnknownOrUndef())
1297 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001298
1299 // Handle LazyCompoundVals for the immediate super region. Other cases
1300 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001301 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001302 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001304 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1305 return RetrieveElement(LCV->getState(), R);
1306 }
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Ted Kremeneka6275a52009-07-15 02:31:43 +00001308 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001309 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001310 }
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001311
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001312 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001313}
1314
Mike Stump1eb44332009-09-09 15:08:12 +00001315SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001316 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001317
1318 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001319 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001320 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001321 return *V;
1322
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001323 QualType Ty = R->getValueType(getContext());
1324 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1325}
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001327SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1328 const TypedRegion *R,
1329 QualType Ty,
1330 const MemRegion *superR) {
1331
Mike Stump1eb44332009-09-09 15:08:12 +00001332 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001333 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001335 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001337 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001338 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001339 if (SymbolRef parentSym = D->getAsSymbol())
1340 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001342 if (D->isZeroConstant())
1343 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001345 if (D->isUnknown())
1346 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001348 assert(0 && "Unknown default value");
1349 }
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001351 // If our super region is a field or element itself, walk up the region
1352 // hierarchy to see if there is a default value installed in an ancestor.
1353 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1354 superR = cast<SubRegion>(superR)->getSuperRegion();
1355 continue;
1356 }
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001358 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001359 }
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001361 // Lazy binding?
1362 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001363 const MemRegion *lazyBindingRegion = NULL;
1364 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001366 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001367 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001369 if (isa<ElementRegion>(R))
1370 return RetrieveElement(lazyBindingState,
1371 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001373 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001374 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001375 }
1376
Ted Kremenekde0d2632010-01-05 02:18:06 +00001377 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001378 if (isa<ElementRegion>(R)) {
1379 // Currently we don't reason specially about Clang-style vectors. Check
1380 // if superR is a vector and if so return Unknown.
1381 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1382 if (typedSuperR->getValueType(getContext())->isVectorType())
1383 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001384 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001385 }
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001387 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001388 }
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001390 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001391 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001392}
Mike Stump1eb44332009-09-09 15:08:12 +00001393
1394SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001395 const ObjCIvarRegion* R) {
1396
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001397 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001398 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001399
Zhongxing Xu13d50172009-10-11 08:08:02 +00001400 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001401 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001403 const MemRegion *superR = R->getSuperRegion();
1404
Ted Kremenekab22ee92009-10-20 01:20:57 +00001405 // Check if the super region has a default binding.
1406 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001407 if (SymbolRef parentSym = V->getAsSymbol())
1408 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001410 // Other cases: give up.
1411 return UnknownVal();
1412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Ted Kremenek25c54572009-07-20 22:58:02 +00001414 return RetrieveLazySymbol(state, R);
1415}
1416
Ted Kremenek9031dd72009-07-21 00:12:07 +00001417SVal RegionStoreManager::RetrieveVar(const GRState *state,
1418 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Ted Kremenek9031dd72009-07-21 00:12:07 +00001420 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001421 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Zhongxing Xu13d50172009-10-11 08:08:02 +00001423 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001424 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Ted Kremenek9031dd72009-07-21 00:12:07 +00001426 // Lazily derive a value for the VarRegion.
1427 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001429 if (R->hasGlobalsOrParametersStorage() ||
1430 isa<UnknownSpaceRegion>(R->getMemorySpace()))
Ted Kremenek28172a22009-12-15 23:23:27 +00001431 return ValMgr.getRegionValueSymbolVal(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Ted Kremenek9031dd72009-07-21 00:12:07 +00001433 return UndefinedVal();
1434}
1435
Mike Stump1eb44332009-09-09 15:08:12 +00001436SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001437 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Ted Kremenek25c54572009-07-20 22:58:02 +00001439 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001440
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001441 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001442 return ValMgr.getRegionValueSymbolVal(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001443}
1444
Mike Stump1eb44332009-09-09 15:08:12 +00001445SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1446 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001447 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001448 assert(T->isStructureType());
1449
Zhongxing Xub7507d12009-06-11 07:27:30 +00001450 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001451 RecordDecl* RD = RT->getDecl();
1452 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001453 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001454#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001455 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1456
Ted Kremenek67f28532009-06-17 22:02:04 +00001457 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1458 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001459 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001460
Douglas Gregore267ff32008-12-11 20:41:00 +00001461 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1462 FieldEnd = Fields.rend();
1463 Field != FieldEnd; ++Field) {
1464 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001465 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001466 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001467 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1468 }
1469
Zhongxing Xud91ee272009-06-23 09:02:15 +00001470 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001471#else
1472 return ValMgr.makeLazyCompoundVal(state, R);
1473#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001474}
1475
Ted Kremenek67f28532009-06-17 22:02:04 +00001476SVal RegionStoreManager::RetrieveArray(const GRState *state,
1477 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001478#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001479 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001480 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1481
1482 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001483 uint64_t size = CAT->getSize().getZExtValue();
1484 for (uint64_t i = 0; i < size; ++i) {
1485 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001486 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001487 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001488 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001489 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001490 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1491 }
1492
Zhongxing Xud91ee272009-06-23 09:02:15 +00001493 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001494#else
1495 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1496 return ValMgr.makeLazyCompoundVal(state, R);
1497#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001498}
1499
Ted Kremenek9af46f52009-06-16 22:36:44 +00001500//===----------------------------------------------------------------------===//
1501// Binding values to regions.
1502//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001503
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001504Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001505 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001506 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001507 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Ted Kremenek0964a062009-01-21 06:57:53 +00001509 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001510}
1511
Ted Kremenek67f28532009-06-17 22:02:04 +00001512const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001513 if (isa<loc::ConcreteInt>(L))
1514 return state;
1515
Ted Kremenek9af46f52009-06-16 22:36:44 +00001516 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001517 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Ted Kremenek9af46f52009-06-16 22:36:44 +00001519 // Check if the region is a struct region.
1520 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1521 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001522 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001524 // Special case: the current region represents a cast and it and the super
1525 // region both have pointer types or intptr_t types. If so, perform the
1526 // bind to the super region.
1527 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001528 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001529 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1530 if (ER->getIndex().isZeroConstant()) {
1531 if (const TypedRegion *superR =
1532 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1533 ASTContext &Ctx = getContext();
1534 QualType superTy = superR->getValueType(Ctx);
1535 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001536
1537 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001538 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001539 SValuator::CastResult cr =
1540 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001541 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1542 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001543 // For now, just invalidate the fields of the struct/union/class.
1544 // FIXME: Precisely handle the fields of the record.
1545 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001546 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001547 }
1548 }
1549 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001550 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1551 // Binding directly to a symbolic region should be treated as binding
1552 // to element 0.
1553 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek852274d2009-12-16 03:18:58 +00001554
1555 // FIXME: Is this the right way to handle symbols that are references?
1556 if (const PointerType *PT = T->getAs<PointerType>())
1557 T = PT->getPointeeType();
1558 else
1559 T = T->getAs<ReferenceType>()->getPointeeType();
1560
Ted Kremenek0954cde2009-09-24 04:11:44 +00001561 R = GetElementZeroRegion(SR, T);
1562 }
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001564 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001565 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001566 return state->makeWithStore(Add(B, R, BindingKey::Direct, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001567}
1568
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001569const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001570 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001571 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001572
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001573 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001574
Ted Kremenek0964a062009-01-21 06:57:53 +00001575 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001576 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001577 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001578 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001579
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001580 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001581}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001582
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001583// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001584const GRState *
1585RegionStoreManager::BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +00001586 const CompoundLiteralExpr *CL,
1587 const LocationContext *LC,
Ted Kremenek67f28532009-06-17 22:02:04 +00001588 SVal V) {
Ted Kremenek67d12872009-12-07 22:05:27 +00001589 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
1590 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001591}
1592
Ted Kremenek027e2662009-11-19 20:20:24 +00001593const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1594 const MemRegion *R,
1595 QualType T) {
1596 Store store = state->getStore();
1597 RegionBindings B = GetRegionBindings(store);
1598 SVal V;
1599
1600 if (Loc::IsLocType(T))
1601 V = ValMgr.makeNull();
1602 else if (T->isIntegerType())
1603 V = ValMgr.makeZeroVal(T);
1604 else if (T->isStructureType() || T->isArrayType()) {
1605 // Set the default value to a zero constant when it is a structure
1606 // or array. The type doesn't really matter.
1607 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1608 }
1609 else {
1610 return state;
1611 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001612
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001613 return state->makeWithStore(Add(B, R, BindingKey::Default, V).getRoot());
Ted Kremenek027e2662009-11-19 20:20:24 +00001614}
1615
Ted Kremenek67f28532009-06-17 22:02:04 +00001616const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001617 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001618 SVal Init) {
Ted Kremenekfee90812010-01-26 23:51:00 +00001619
1620 ASTContext &Ctx = getContext();
1621 const ArrayType *AT =
1622 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
1623 QualType ElementTy = AT->getElementType();
1624 Optional<uint64_t> Size;
1625
1626 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1627 Size = CAT->getSize().getZExtValue();
1628
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001629 // Check if the init expr is a StringLiteral.
1630 if (isa<loc::MemRegionVal>(Init)) {
1631 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1632 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1633 const char* str = S->getStrData();
1634 unsigned len = S->getByteLength();
1635 unsigned j = 0;
1636
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001637 // Copy bytes from the string literal into the target array. Trailing bytes
1638 // in the array that are not covered by the string literal are initialized
1639 // to zero.
Ted Kremenekfee90812010-01-26 23:51:00 +00001640
1641 // We assume that string constants are bound to
1642 // constant arrays.
Ted Kremenek39c2ea12010-01-27 16:31:37 +00001643 uint64_t size = Size.getValue();
Ted Kremenekfee90812010-01-26 23:51:00 +00001644
Ted Kremenek46537392009-07-16 01:33:37 +00001645 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001646 if (j >= len)
1647 break;
1648
Ted Kremenek46537392009-07-16 01:33:37 +00001649 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001650 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1651 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001652
Zhongxing Xud91ee272009-06-23 09:02:15 +00001653 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001654 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001655 }
1656
Ted Kremenek67f28532009-06-17 22:02:04 +00001657 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001658 }
1659
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001660 // Handle lazy compound values.
1661 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1662 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001663
1664 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001665
1666 if (Init.isUnknown())
1667 return setImplicitDefaultValue(state, R, ElementTy);
1668
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001669 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001670 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001671 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Ted Kremenekfee90812010-01-26 23:51:00 +00001673 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001674 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001675 if (VI == VE)
1676 break;
1677
Ted Kremenek46537392009-07-16 01:33:37 +00001678 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001679 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001680
Ted Kremenekfee90812010-01-26 23:51:00 +00001681 if (ElementTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001682 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001683 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001684 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001685 }
1686
Ted Kremenek027e2662009-11-19 20:20:24 +00001687 // If the init list is shorter than the array length, set the
1688 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001689 if (Size.hasValue() && i < Size.getValue())
Ted Kremenek027e2662009-11-19 20:20:24 +00001690 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001691
Ted Kremenek67f28532009-06-17 22:02:04 +00001692 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001693}
1694
Ted Kremenek67f28532009-06-17 22:02:04 +00001695const GRState *
1696RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1697 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Ted Kremenek67f28532009-06-17 22:02:04 +00001699 if (!Features.supportsFields())
1700 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001702 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001703 assert(T->isStructureType());
1704
Ted Kremenek6217b802009-07-29 21:53:49 +00001705 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001706 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001707
1708 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001709 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001710
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001711 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001712 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001713 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Ted Kremenek67f28532009-06-17 22:02:04 +00001715 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1716 // Ignore them and kill the field values.
1717 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001718 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001719
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001720 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001721 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001722
1723 RecordDecl::field_iterator FI, FE;
1724
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001725 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001726
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001727 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001728 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001729
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001730 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001731 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001732
Ted Kremenekcf549592009-09-22 21:19:14 +00001733 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001734 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001735 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001736 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001737 else
1738 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001739 }
1740
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001741 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001742 if (FI != FE) {
1743 Store store = state->getStore();
1744 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001745 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001746 state = state->makeWithStore(B.getRoot());
1747 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001748
Ted Kremenek67f28532009-06-17 22:02:04 +00001749 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001750}
1751
Zhongxing Xu13d50172009-10-11 08:08:02 +00001752Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1753 RegionBindings B = GetRegionBindings(store);
1754 llvm::OwningPtr<RegionStoreSubRegionMap>
1755 SubRegions(getRegionStoreSubRegionMap(store));
1756 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001757
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001758 // Set the default value of the struct region to "unknown".
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001759 return Add(B, R, BindingKey::Default, UnknownVal()).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001760}
1761
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001762const GRState*
1763RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1764 const GRState *state,
1765 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001766
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001767 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001768 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001769
Mike Stump1eb44332009-09-09 15:08:12 +00001770 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001771 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001772
Mike Stump1eb44332009-09-09 15:08:12 +00001773 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001774 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001776 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1777 // results in a zero-copy algorithm.
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001778 return state->makeWithStore(Add(B, R, BindingKey::Direct, V).getRoot());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001779}
1780
1781//===----------------------------------------------------------------------===//
1782// "Raw" retrievals and bindings.
1783//===----------------------------------------------------------------------===//
1784
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001785BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001786 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1787 const RegionRawOffset &O = ER->getAsRawOffset();
1788
1789 if (O.getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001790 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001791
1792 // FIXME: There are some ElementRegions for which we cannot compute
1793 // raw offsets yet, including regions with symbolic offsets.
1794 }
1795
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001796 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001797}
1798
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001799RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001800 return RBFactory.Add(B, K, V);
1801}
1802
1803RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001804 BindingKey::Kind k, SVal V) {
1805 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001806}
1807
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001808const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001809 return B.lookup(K);
1810}
1811
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001812const SVal *RegionStoreManager::Lookup(RegionBindings B,
1813 const MemRegion *R,
1814 BindingKey::Kind k) {
1815 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001816}
1817
1818RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1819 return RBFactory.Remove(B, K);
1820}
1821
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001822RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1823 BindingKey::Kind k){
1824 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001825}
1826
1827Store RegionStoreManager::Remove(Store store, BindingKey K) {
1828 RegionBindings B = GetRegionBindings(store);
1829 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001830}
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Ted Kremenek9af46f52009-06-16 22:36:44 +00001832//===----------------------------------------------------------------------===//
1833// State pruning.
1834//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001835
Mike Stump1eb44332009-09-09 15:08:12 +00001836void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001837 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001838 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001839{
Ted Kremenek781115c2009-10-17 17:45:11 +00001840 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1841
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001842 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001843 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Ted Kremenek9af46f52009-06-16 22:36:44 +00001845 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001846 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001847 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001848
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001849 // Do a pass over the regions in the store. For VarRegions we check if
1850 // the variable is still live and if so add it to the list of live roots.
1851 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001852 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001853
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001854 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001855 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001856 const MemRegion *R = I.getKey().getRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001857 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001858 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001859
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001860 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001861 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001862 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001863 llvm::SmallVector<RBDNode, 10> Postponed;
1864
Zhongxing Xu6800b332009-10-18 04:15:47 +00001865 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001866
Ted Kremenek9af46f52009-06-16 22:36:44 +00001867 while (!IntermediateRoots.empty()) {
1868 const MemRegion* R = IntermediateRoots.back();
1869 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001870
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001871 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001872 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001873 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001874
Ted Kremenek9af46f52009-06-16 22:36:44 +00001875 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekedeb5b62009-12-04 20:32:20 +00001876 if (SymReaper.isLive(Loc, VR))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001877 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001878 continue;
1879 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001880
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001881 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001882 llvm::SmallVectorImpl<RBDNode> &Q =
1883 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1884
1885 Q.push_back(std::make_pair(&state, SR));
1886
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001887 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001888 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001889
1890 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001891 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001892 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001893 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001894 }
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001896 // Enqueue the RegionRoots onto WorkList.
1897 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1898 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001899 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001900 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001901 RegionRoots.clear();
1902
Zhongxing Xu6800b332009-10-18 04:15:47 +00001903 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001904
Ted Kremenek01756192009-10-29 05:14:17 +00001905tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001906 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001907 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001908 WorkList.pop_back();
1909
1910 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001911 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001912 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001913 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001915 const MemRegion *R = N.second;
1916 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001917
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001918 // Enqueue subregions.
1919 RegionStoreSubRegionMap *M;
1920
1921 if (&state == state_N)
1922 M = SubRegions.get();
1923 else {
1924 RegionStoreSubRegionMap *& SM = SC[state_N];
1925 if (!SM)
1926 SM = getRegionStoreSubRegionMap(state_N->getStore());
1927 M = SM;
1928 }
Ted Kremenekdf165012010-02-02 22:38:47 +00001929
1930 if (const RegionStoreSubRegionMap::Set *S = M->getSubRegions(R))
1931 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
1932 I != E; ++I)
1933 WorkList.push_back(std::make_pair(state_N, *I));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001934
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001935 // Enqueue the super region.
1936 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1937 const MemRegion *superR = SR->getSuperRegion();
1938 if (!isa<MemSpaceRegion>(superR)) {
1939 // If 'R' is a field or an element, we want to keep the bindings
1940 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001941 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001942 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1943 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001944 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001945 }
1946 }
1947
1948 // Mark the symbol for any live SymbolicRegion as "live". This means we
1949 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001950 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001951 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001952
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001953 // For BlockDataRegions, enqueue the VarRegions for variables marked
1954 // with __block (passed-by-reference).
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001955 // via BlockDeclRefExprs.
1956 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1957 for (BlockDataRegion::referenced_vars_iterator
1958 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001959 RI != RE; ++RI) {
1960 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1961 WorkList.push_back(std::make_pair(state_N, *RI));
1962 }
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001963 // No possible data bindings on a BlockDataRegion. Continue to the
1964 // next region in the worklist.
1965 continue;
1966 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001967
1968 Store store_N = state_N->getStore();
1969 RegionBindings B_N = GetRegionBindings(store_N);
1970
1971 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001972 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001973
Zhongxing Xu13d50172009-10-11 08:08:02 +00001974 if (V) {
1975 // Check for lazy bindings.
1976 if (const nonloc::LazyCompoundVal *LCV =
1977 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001978
Zhongxing Xu13d50172009-10-11 08:08:02 +00001979 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001980 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001981 }
1982 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001983 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001984 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001985 SI!=SE;++SI)
1986 SymReaper.markLive(*SI);
1987
Zhongxing Xu13d50172009-10-11 08:08:02 +00001988 // If V is a region, then add it to the worklist.
1989 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001990 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001991 }
1992 }
1993 }
1994
Ted Kremenek01756192009-10-29 05:14:17 +00001995 // See if any postponed SymbolicRegions are actually live now, after
1996 // having done a scan.
1997 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1998 E = Postponed.end() ; I != E ; ++I) {
1999 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
2000 if (SymReaper.isLive(SR->getSymbol())) {
2001 WorkList.push_back(*I);
2002 I->second = NULL;
2003 }
2004 }
2005 }
2006
2007 if (!WorkList.empty())
2008 goto tryAgain;
2009
Ted Kremenek9af46f52009-06-16 22:36:44 +00002010 // We have now scanned the store, marking reachable regions and symbols
2011 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00002012 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00002013 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00002014 const MemRegion* R = I.getKey().getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00002015 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00002016 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00002017 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Ted Kremenek9af46f52009-06-16 22:36:44 +00002019 // Remove this dead region from the store.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00002020 store = Remove(store, I.getKey());
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Ted Kremenek9af46f52009-06-16 22:36:44 +00002022 // Mark all non-live symbols that this region references as dead.
2023 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
2024 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00002025
Ted Kremeneke393f4a2010-02-03 03:06:46 +00002026 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00002027 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
2028 for (; SI != SE; ++SI)
2029 SymReaper.maybeDead(*SI);
2030 }
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Ted Kremenek2f26bc32009-08-02 04:45:08 +00002032 // Write the store back.
2033 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00002034}
2035
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00002036GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
2037 StackFrameContext const *frame) {
2038 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
2039 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
2040
2041 FunctionDecl::param_const_iterator PI = FD->param_begin();
2042
2043 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
2044
2045 // Copy the arg expression value to the arg variables.
2046 for (; AI != AE; ++AI, ++PI) {
2047 SVal ArgVal = state->getSVal(*AI);
Ted Kremenekb48ad642009-12-04 00:26:31 +00002048 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00002049 }
2050
2051 return state;
2052}
2053
Ted Kremenek9af46f52009-06-16 22:36:44 +00002054//===----------------------------------------------------------------------===//
2055// Utility methods.
2056//===----------------------------------------------------------------------===//
2057
Ted Kremenek53ba0b62009-06-24 23:06:47 +00002058void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00002059 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00002060 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00002061 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Ted Kremenek451ac092009-08-06 04:50:20 +00002063 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002064 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00002065}