blob: 36022d57376dbb0f79217185a626f2a2a09d5ea4 [file] [log] [blame]
Zhongxing Xud9959ae2008-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 Kremenekd6b87082010-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 Xud9959ae2008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek2f6eb142009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xuea8c48d2009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu228b0d42010-01-18 08:54:31 +000024#include "clang/AST/CharUnits.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000025
26#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xuceca8062008-11-16 04:07:26 +000027#include "llvm/ADT/ImmutableList.h"
Zhongxing Xu1359e002008-10-24 06:01:33 +000028#include "llvm/Support/raw_ostream.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000029
30using namespace clang;
31
Ted Kremenekfa417142009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek920ad712009-07-22 04:35:42 +000033
Ted Kremenek8e994a22010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
Ted Kremenek8e994a22010-01-11 00:07:44 +000035// Representation of binding keys.
36//===----------------------------------------------------------------------===//
37
38namespace {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000039class BindingKey {
Ted Kremenek8e994a22010-01-11 00:07:44 +000040public:
Ted Kremenek64efd0d2010-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 Kremenek8e994a22010-01-11 00:07:44 +000045
Ted Kremenek64efd0d2010-02-03 03:06:46 +000046 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
Zhongxing Xubd96bf12010-02-05 02:26:30 +000047 : P(r, (unsigned) k), Offset(offset) { assert(r); }
Ted Kremenek64efd0d2010-02-03 03:06:46 +000048public:
49
50 bool isDefault() const { return P.getInt() == Default; }
51 bool isDirect() const { return P.getInt() == Direct; }
52
53 const MemRegion *getRegion() const { return P.getPointer(); }
54 uint64_t getOffset() const { return Offset; }
Ted Kremenek8e994a22010-01-11 00:07:44 +000055
56 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000057 ID.AddPointer(P.getOpaqueValue());
58 ID.AddInteger(Offset);
Ted Kremenek8e994a22010-01-11 00:07:44 +000059 }
Ted Kremenek64efd0d2010-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 Kremenek8e994a22010-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 Kremenek64efd0d2010-02-03 03:06:46 +000081 os << '(' << K.getRegion() << ',' << K.getOffset()
82 << ',' << (K.isDirect() ? "direct" : "default")
83 << ')';
Ted Kremenek8e994a22010-01-11 00:07:44 +000084 return os;
85 }
86} // end llvm namespace
87
88//===----------------------------------------------------------------------===//
Zhongxing Xu9165ed62008-11-24 09:44:56 +000089// Actual Store type.
Ted Kremenek8e994a22010-01-11 00:07:44 +000090//===----------------------------------------------------------------------===//
91
Ted Kremenek64efd0d2010-02-03 03:06:46 +000092typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xu9165ed62008-11-24 09:44:56 +000093
Ted Kremenekae189ec2008-12-24 01:05:03 +000094//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +000095// Fine-grained control of RegionStoreManager.
96//===----------------------------------------------------------------------===//
97
98namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000099struct minimal_features_tag {};
100struct maximal_features_tag {};
Mike Stump11289f42009-09-09 15:08:12 +0000101
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000102class RegionStoreFeatures {
Ted Kremenek4533a552009-06-16 22:36:44 +0000103 bool SupportsFields;
104 bool SupportsRemaining;
Mike Stump11289f42009-09-09 15:08:12 +0000105
Ted Kremenek4533a552009-06-16 22:36:44 +0000106public:
107 RegionStoreFeatures(minimal_features_tag) :
108 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremenek4533a552009-06-16 22:36:44 +0000110 RegionStoreFeatures(maximal_features_tag) :
111 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenek4533a552009-06-16 22:36:44 +0000113 void enableFields(bool t) { SupportsFields = t; }
Mike Stump11289f42009-09-09 15:08:12 +0000114
Ted Kremenek4533a552009-06-16 22:36:44 +0000115 bool supportsFields() const { return SupportsFields; }
116 bool supportsRemaining() const { return SupportsRemaining; }
117};
118}
119
120//===----------------------------------------------------------------------===//
Ted Kremenekae189ec2008-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 Kremenek682c3a62009-01-07 22:18:50 +0000126//
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000127namespace { class RegionExtents {}; }
Ted Kremenekae189ec2008-12-24 01:05:03 +0000128static int RegionExtentsIndex = 0;
Zhongxing Xu9165ed62008-11-24 09:44:56 +0000129namespace clang {
Ted Kremenekae189ec2008-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 Xu9165ed62008-11-24 09:44:56 +0000134}
135
Ted Kremenekae189ec2008-12-24 01:05:03 +0000136//===----------------------------------------------------------------------===//
Ted Kremenek1f22aa72009-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 Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000144 return ty->isIntegerType() && ty->isScalarType() &&
145 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenekae189ec2008-12-24 01:05:03 +0000149// Main RegionStore logic.
150//===----------------------------------------------------------------------===//
Ted Kremenek677779a2008-12-04 02:08:27 +0000151
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000152namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000153
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000154class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekb251eb62010-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 Kremenek8dc671c2009-03-03 01:35:36 +0000160 Map M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000161public:
Ted Kremenek844a7292009-08-05 19:09:24 +0000162 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000163 Map::iterator I = M.find(Parent);
Ted Kremenek844a7292009-08-05 19:09:24 +0000164
165 if (I == M.end()) {
Ted Kremenek68c1f012009-08-05 05:31:02 +0000166 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenek844a7292009-08-05 19:09:24 +0000167 return true;
168 }
169
170 I->second = F.Add(I->second, SubRegion);
171 return false;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000172 }
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremenekfa417142009-08-06 01:20:57 +0000174 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000175
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000176 ~RegionStoreSubRegionMap() {}
Ted Kremenekb251eb62010-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 Stump11289f42009-09-09 15:08:12 +0000182
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000183 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +0000184 Map::const_iterator I = M.find(Parent);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000185
186 if (I == M.end())
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000187 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000188
Ted Kremenekb251eb62010-02-02 22:38:47 +0000189 Set S = I->second;
190 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000191 if (!V.Visit(Parent, *SI))
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000192 return false;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000193 }
Mike Stump11289f42009-09-09 15:08:12 +0000194
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000195 return true;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000196 }
Mike Stump11289f42009-09-09 15:08:12 +0000197};
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000198
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000199
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000200class RegionStoreManager : public StoreManager {
Ted Kremenek4533a552009-06-16 22:36:44 +0000201 const RegionStoreFeatures Features;
Ted Kremenek2c85f172009-08-06 04:50:20 +0000202 RegionBindings::Factory RBFactory;
Ted Kremenekcc224242009-09-29 06:35:00 +0000203
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000204 typedef llvm::DenseMap<Store, RegionStoreSubRegionMap*> SMCache;
Ted Kremenekcc224242009-09-29 06:35:00 +0000205 SMCache SC;
206
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000207public:
Mike Stump11289f42009-09-09 15:08:12 +0000208 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenek43015262009-07-29 21:43:22 +0000209 : StoreManager(mgr),
Ted Kremenek4533a552009-06-16 22:36:44 +0000210 Features(f),
Ted Kremenek33617e02010-02-04 04:14:49 +0000211 RBFactory(mgr.getAllocator()) {}
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000212
Ted Kremenekcc224242009-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 Xud9959ae2008-10-08 02:50:44 +0000217
Zhongxing Xuf6682042010-02-05 05:18:47 +0000218 SubRegionMap *getSubRegionMap(Store store) {
219 return getRegionStoreSubRegionMap(store);
220 }
Mike Stump11289f42009-09-09 15:08:12 +0000221
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000222 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump11289f42009-09-09 15:08:12 +0000223
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000224 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
225 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000226 /// getDefaultBinding - Returns an SVal* representing an optional default
227 /// binding associated with a region and its subregions.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000228 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek439a6d12009-11-19 20:20:24 +0000229
230 /// setImplicitDefaultValue - Set the default binding for the provided
231 /// MemRegion to the value implicitly defined for compound literals when
232 /// the value is not specified.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000233 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenek2907ab72008-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 Xu7d6387b2009-10-14 03:33:08 +0000239 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000240
Ted Kremenek2907ab72008-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 Xu7d6387b2009-10-14 03:33:08 +0000245 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000246
Ted Kremenek2907ab72008-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 Xu7d6387b2009-10-14 03:33:08 +0000250 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump11289f42009-09-09 15:08:12 +0000251
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000252 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000253
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000254 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump11289f42009-09-09 15:08:12 +0000255
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000256 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000257
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000258 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000259
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000260
Ted Kremenek2907ab72008-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 Xua865b792009-03-30 05:55:46 +0000267 SVal ArrayToPointer(Loc Array);
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000268
Zhongxing Xu0d081f32010-02-05 05:24:20 +0000269 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000270
Mike Stump11289f42009-09-09 15:08:12 +0000271 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek608677a2009-08-21 23:25:54 +0000272 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000273 }
Ted Kremenek608677a2009-08-21 23:25:54 +0000274
Ted Kremenek609df302009-06-17 22:02:04 +0000275 //===-------------------------------------------------------------------===//
276 // Binding values to regions.
277 //===-------------------------------------------------------------------===//
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000278
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000279 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
280 unsigned Count, InvalidatedSymbols *IS) {
281 return RegionStoreManager::InvalidateRegions(store, &R, &R+1, E, Count, IS);
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000282 }
283
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000284 Store InvalidateRegions(Store store,
285 const MemRegion * const *Begin,
286 const MemRegion * const *End,
287 const Expr *E, unsigned Count,
288 InvalidatedSymbols *IS);
Mike Stump11289f42009-09-09 15:08:12 +0000289
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000290public: // Made public for helper classes.
291
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000292 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremenekfa417142009-08-06 01:20:57 +0000293 RegionStoreSubRegionMap &M);
Mike Stump11289f42009-09-09 15:08:12 +0000294
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000295 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
296
297 RegionBindings Add(RegionBindings B, const MemRegion *R,
298 BindingKey::Kind k, SVal V);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000299
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000300 const SVal *Lookup(RegionBindings B, BindingKey K);
301 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000302
303 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000304 RegionBindings Remove(RegionBindings B, const MemRegion *R,
305 BindingKey::Kind k);
306
307 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
308 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
309 }
310
Ted Kremenek8e994a22010-01-11 00:07:44 +0000311 Store Remove(Store store, BindingKey K);
312
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000313public: // Part of public interface to class.
314
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000315 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek609df302009-06-17 22:02:04 +0000316
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000317 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
318 const LocationContext *LC, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000319
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000320 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek609df302009-06-17 22:02:04 +0000321
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000322 Store BindDeclWithNoInit(Store store, const VarRegion *) {
323 return store;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000324 }
Zhongxing Xu83aff702008-10-21 05:29:26 +0000325
Ted Kremenek609df302009-06-17 22:02:04 +0000326 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000327 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000328
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000329 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000330
331 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000332 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek609df302009-06-17 22:02:04 +0000333
Ted Kremenek609df302009-06-17 22:02:04 +0000334 Store Remove(Store store, Loc LV);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000335
Ted Kremenek609df302009-06-17 22:02:04 +0000336
337 //===------------------------------------------------------------------===//
338 // Loading values from regions.
339 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek609df302009-06-17 22:02:04 +0000341 /// The high level logic for this method is this:
342 /// Retrieve (L)
343 /// if L has binding
344 /// return L's binding
345 /// else if L is in killset
346 /// return unknown
347 /// else
348 /// if L is on stack or heap
349 /// return undefined
350 /// else
351 /// return symbolic
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000352 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xue67ea5c2009-06-25 04:50:44 +0000353
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000354 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xu2d160732009-06-25 05:29:39 +0000355
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000356 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000357
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000358 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000359
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000360 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000361
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000362 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000363
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000364 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek040e3b92009-08-06 22:33:36 +0000365 QualType Ty, const MemRegion *superR);
Mike Stump11289f42009-09-09 15:08:12 +0000366
Ted Kremenek609df302009-06-17 22:02:04 +0000367 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump11289f42009-09-09 15:08:12 +0000368 /// struct copy:
369 /// struct s x, y;
Ted Kremenek609df302009-06-17 22:02:04 +0000370 /// x = y;
371 /// y's value is retrieved by this method.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000372 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000373
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000374 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000375
Zhongxing Xufd62a332009-12-21 06:52:24 +0000376 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000377 std::pair<Store, const MemRegion*>
Ted Kremenek2c85f172009-08-06 04:50:20 +0000378 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000379
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000380 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
381 const TypedRegion *R);
Ted Kremenek609df302009-06-17 22:02:04 +0000382
Ted Kremenek267e45a2009-09-24 04:11:44 +0000383 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
384 QualType T);
385
Ted Kremenek609df302009-06-17 22:02:04 +0000386 //===------------------------------------------------------------------===//
387 // State pruning.
388 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000389
Ted Kremenek609df302009-06-17 22:02:04 +0000390 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
391 /// It returns a new Store with these values removed.
Zhongxing Xuad0ef842010-02-05 05:34:29 +0000392 Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek609df302009-06-17 22:02:04 +0000393 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
394
Zhongxing Xudaa41762009-10-13 02:24:55 +0000395 const GRState *EnterStackFrame(const GRState *state,
396 const StackFrameContext *frame);
397
Ted Kremenek609df302009-06-17 22:02:04 +0000398 //===------------------------------------------------------------------===//
399 // Region "extents".
400 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000401
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000402 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent);
Zhongxing Xu383c2732009-11-12 02:48:32 +0000403 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000404 const MemRegion* R, QualType EleTy);
Ted Kremenek609df302009-06-17 22:02:04 +0000405
406 //===------------------------------------------------------------------===//
Ted Kremenek609df302009-06-17 22:02:04 +0000407 // Utility methods.
408 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000409
Ted Kremenek2c85f172009-08-06 04:50:20 +0000410 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000411 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000412 }
Zhongxing Xucebb7412008-10-24 01:38:55 +0000413
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000414 void print(Store store, llvm::raw_ostream& Out, const char* nl,
415 const char *sep);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000416
417 void iterBindings(Store store, BindingsHandler& f) {
418 // FIXME: Implement.
419 }
Zhongxing Xu6c0d5882008-10-31 07:16:08 +0000420
Ted Kremenek609df302009-06-17 22:02:04 +0000421 // FIXME: Remove.
422 BasicValueFactory& getBasicVals() {
423 return StateMgr.getBasicVals();
424 }
Mike Stump11289f42009-09-09 15:08:12 +0000425
Ted Kremenek609df302009-06-17 22:02:04 +0000426 // FIXME: Remove.
Zhongxing Xu6c0d5882008-10-31 07:16:08 +0000427 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000428};
429
430} // end anonymous namespace
431
Ted Kremenek4533a552009-06-16 22:36:44 +0000432//===----------------------------------------------------------------------===//
433// RegionStore creation.
434//===----------------------------------------------------------------------===//
435
436StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
437 RegionStoreFeatures F = maximal_features_tag();
438 return new RegionStoreManager(StMgr, F);
439}
440
441StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
442 RegionStoreFeatures F = minimal_features_tag();
443 F.enableFields(true);
444 return new RegionStoreManager(StMgr, F);
Ted Kremenek6779f892008-10-24 01:04:59 +0000445}
446
Ted Kremenekfa417142009-08-06 01:20:57 +0000447void
448RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump11289f42009-09-09 15:08:12 +0000449 const SubRegion *R) {
Ted Kremenekfa417142009-08-06 01:20:57 +0000450 const MemRegion *superR = R->getSuperRegion();
451 if (add(superR, R))
452 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump11289f42009-09-09 15:08:12 +0000453 WL.push_back(sr);
Ted Kremenekfa417142009-08-06 01:20:57 +0000454}
455
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000456RegionStoreSubRegionMap*
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000457RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
458 RegionBindings B = GetRegionBindings(store);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000459 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump11289f42009-09-09 15:08:12 +0000460
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000461 llvm::SmallVector<const SubRegion*, 10> WL;
462
Ted Kremenek2c85f172009-08-06 04:50:20 +0000463 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek8e994a22010-01-11 00:07:44 +0000464 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremenekfa417142009-08-06 01:20:57 +0000465 M->process(WL, R);
Mike Stump11289f42009-09-09 15:08:12 +0000466
Mike Stump11289f42009-09-09 15:08:12 +0000467 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000468 // don't have direct bindings but are super regions of those that do.
469 while (!WL.empty()) {
470 const SubRegion *R = WL.back();
471 WL.pop_back();
Ted Kremenekfa417142009-08-06 01:20:57 +0000472 M->process(WL, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000473 }
474
Ted Kremenek9f276d62009-03-03 19:02:42 +0000475 return M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000476}
Ted Kremenek2907ab72008-12-24 07:46:32 +0000477
Ted Kremenek4533a552009-06-16 22:36:44 +0000478//===----------------------------------------------------------------------===//
Ted Kremenekbca70672009-07-29 18:16:25 +0000479// Binding invalidation.
480//===----------------------------------------------------------------------===//
481
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000482void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
483 const MemRegion *R,
484 RegionStoreSubRegionMap &M) {
Ted Kremenekb251eb62010-02-02 22:38:47 +0000485
486 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
487 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
488 I != E; ++I)
489 RemoveSubRegionBindings(B, *I, M);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000490
491 B = Remove(B, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000492}
493
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000494namespace {
495class InvalidateRegionsWorker {
Ted Kremenekd9605642010-02-03 04:16:00 +0000496 typedef BumpVector<BindingKey> RegionCluster;
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000497 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenekd9605642010-02-03 04:16:00 +0000498 typedef llvm::SmallVector<std::pair<const MemRegion *,RegionCluster*>, 10>
499 WorkList;
Mike Stump11289f42009-09-09 15:08:12 +0000500
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000501 BumpVectorContext BVC;
502 ClusterMap ClusterM;
503 WorkList WL;
504public:
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000505 Store InvalidateRegions(RegionStoreManager &RM, Store store,
506 const MemRegion * const *I,const MemRegion * const *E,
507 const Expr *Ex, unsigned Count,
508 StoreManager::InvalidatedSymbols *IS,
509 ASTContext &Ctx, ValueManager &ValMgr);
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000510
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000511private:
Ted Kremenekd9605642010-02-03 04:16:00 +0000512 void AddToWorkList(BindingKey K);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000513 void AddToWorkList(const MemRegion *R);
Ted Kremenekd9605642010-02-03 04:16:00 +0000514 void AddToCluster(BindingKey K);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000515 RegionCluster **getCluster(const MemRegion *R);
516};
517}
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000518
Ted Kremenekd9605642010-02-03 04:16:00 +0000519void InvalidateRegionsWorker::AddToCluster(BindingKey K) {
520 const MemRegion *R = K.getRegion();
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000521 const MemRegion *baseR = R->getBaseRegion();
522 RegionCluster **CPtr = getCluster(baseR);
Ted Kremenekd9605642010-02-03 04:16:00 +0000523 assert(*CPtr);
524 (*CPtr)->push_back(K, BVC);
525}
526
527void InvalidateRegionsWorker::AddToWorkList(BindingKey K) {
528 AddToWorkList(K.getRegion());
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000529}
530
531void InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) {
Ted Kremenekd9605642010-02-03 04:16:00 +0000532 const MemRegion *baseR = R->getBaseRegion();
533 RegionCluster **CPtr = getCluster(baseR);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000534 if (RegionCluster *C = *CPtr) {
Ted Kremenekd9605642010-02-03 04:16:00 +0000535 WL.push_back(std::make_pair(baseR, C));
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000536 *CPtr = NULL;
537 }
538}
539
540InvalidateRegionsWorker::RegionCluster **
541InvalidateRegionsWorker::getCluster(const MemRegion *R) {
542 RegionCluster *&CRef = ClusterM[R];
543 if (!CRef) {
544 void *Mem = BVC.getAllocator().Allocate<RegionCluster>();
545 CRef = new (Mem) RegionCluster(BVC, 10);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000546 }
547 return &CRef;
548}
549
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000550Store InvalidateRegionsWorker::InvalidateRegions(RegionStoreManager &RM,
551 Store store,
552 const MemRegion * const *I,
553 const MemRegion * const *E,
554 const Expr *Ex, unsigned Count,
555 StoreManager::InvalidatedSymbols *IS,
556 ASTContext &Ctx,
557 ValueManager &ValMgr) {
558 RegionBindings B = RegionStoreManager::GetRegionBindings(store);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000559
560 // Scan the entire store and make the region clusters.
561 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
Ted Kremenekd9605642010-02-03 04:16:00 +0000562 AddToCluster(RI.getKey());
563 if (const MemRegion *R = RI.getData().getAsRegion()) {
564 // Generate a cluster, but don't add the region to the cluster
565 // if there aren't any bindings.
566 getCluster(R->getBaseRegion());
567 }
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000568 }
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000569
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000570 // Add the cluster for I .. E to a worklist.
571 for ( ; I != E; ++I)
572 AddToWorkList(*I);
573
574 while (!WL.empty()) {
Ted Kremenekd9605642010-02-03 04:16:00 +0000575 const MemRegion *baseR;
576 RegionCluster *C;
577 llvm::tie(baseR, C) = WL.back();
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000578 WL.pop_back();
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000579
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000580 for (RegionCluster::iterator I = C->begin(), E = C->end(); I != E; ++I) {
Ted Kremenekd9605642010-02-03 04:16:00 +0000581 BindingKey K = *I;
Ted Kremenek1eb68092009-10-16 00:30:49 +0000582
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000583 // Get the old binding. Is it a region? If so, add it to the worklist.
Ted Kremenekd9605642010-02-03 04:16:00 +0000584 if (const SVal *V = RM.Lookup(B, K)) {
585 if (const MemRegion *R = V->getAsRegion())
586 AddToWorkList(R);
Ted Kremenek5bee5c42009-12-03 08:25:47 +0000587
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000588 // A symbol? Mark it touched by the invalidation.
589 if (IS)
590 if (SymbolRef Sym = V->getAsSymbol())
591 IS->insert(Sym);
Ted Kremenek5bee5c42009-12-03 08:25:47 +0000592 }
Ted Kremenek5daec8a2009-09-29 03:34:03 +0000593
Ted Kremenekd9605642010-02-03 04:16:00 +0000594 B = RM.Remove(B, K);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000595 }
Ted Kremenekd9605642010-02-03 04:16:00 +0000596
597 // Now inspect the base region.
598
599 if (IS) {
600 // Symbolic region? Mark that symbol touched by the invalidation.
601 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
602 IS->insert(SR->getSymbol());
603 }
604
605 // BlockDataRegion? If so, invalidate captured variables that are passed
606 // by reference.
607 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
608 for (BlockDataRegion::referenced_vars_iterator
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000609 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
610 BI != BE; ++BI) {
611 const VarRegion *VR = *BI;
612 const VarDecl *VD = VR->getDecl();
613 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
Ted Kremenekd9605642010-02-03 04:16:00 +0000614 AddToWorkList(VR);
615 }
616 continue;
617 }
618
619 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
620 // Invalidate the region by setting its default value to
621 // conjured symbol. The type of the symbol is irrelavant.
622 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
623 Count);
624 B = RM.Add(B, baseR, BindingKey::Default, V);
625 continue;
626 }
627
628 if (!baseR->isBoundable())
629 continue;
630
631 const TypedRegion *TR = cast<TypedRegion>(baseR);
632 QualType T = TR->getValueType(Ctx);
633
634 // Invalidate the binding.
635 if (const RecordType *RT = T->getAsStructureType()) {
636 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
637 // No record definition. There is nothing we can do.
638 if (!RD) {
639 B = RM.Remove(B, baseR);
640 continue;
641 }
642
643 // Invalidate the region by setting its default value to
644 // conjured symbol. The type of the symbol is irrelavant.
645 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
646 Count);
647 B = RM.Add(B, baseR, BindingKey::Default, V);
648 continue;
649 }
650
651 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
652 // Set the default value of the array to conjured symbol.
653 DefinedOrUnknownSVal V =
654 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
655 B = RM.Add(B, baseR, BindingKey::Default, V);
656 continue;
657 }
658
659 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
660 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
661 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenekfa417142009-08-06 01:20:57 +0000662 }
663
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000664 // Create a new state with the updated bindings.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000665 return B.getRoot();
Ted Kremenekbca70672009-07-29 18:16:25 +0000666}
667
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000668Store RegionStoreManager::InvalidateRegions(Store store,
669 const MemRegion * const *I,
670 const MemRegion * const *E,
671 const Expr *Ex, unsigned Count,
672 InvalidatedSymbols *IS) {
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000673 InvalidateRegionsWorker W;
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000674 return W.InvalidateRegions(*this, store, I, E, Ex, Count, IS, getContext(),
675 StateMgr.getValueManager());
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000676}
677
678
Ted Kremenekbca70672009-07-29 18:16:25 +0000679//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +0000680// getLValueXXX methods.
681//===----------------------------------------------------------------------===//
682
Ted Kremenek2907ab72008-12-24 07:46:32 +0000683/// getLValueString - Returns an SVal representing the lvalue of a
684/// StringLiteral. Within RegionStore a StringLiteral has an
685/// associated StringRegion, and the lvalue of a StringLiteral is the
686/// lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000687SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000688 return loc::MemRegionVal(MRMgr.getStringRegion(S));
689}
690
Ted Kremenek2907ab72008-12-24 07:46:32 +0000691/// getLValueVar - Returns an SVal that represents the lvalue of a
692/// variable. Within RegionStore a variable has an associated
693/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000694SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenek14536f62009-08-21 22:28:32 +0000695 const LocationContext *LC) {
696 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000697}
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000698
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000699SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
700 return getLValueFieldOrIvar(D, Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000701}
702
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000703SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
704 return getLValueFieldOrIvar(D, Base);
Ted Kremenekd3c82762009-03-05 04:50:08 +0000705}
706
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000707SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000708 if (Base.isUnknownOrUndef())
709 return Base;
710
711 Loc BaseL = cast<Loc>(Base);
712 const MemRegion* BaseR = 0;
713
714 switch (BaseL.getSubKind()) {
715 case loc::MemRegionKind:
716 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
717 break;
718
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000719 case loc::GotoLabelKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000720 // These are anormal cases. Flag an undefined value.
721 return UndefinedVal();
722
723 case loc::ConcreteIntKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000724 // While these seem funny, this can happen through casts.
725 // FIXME: What we should return is the field offset. For example,
726 // add the field offset to the integer value. That way funny things
727 // like this work properly: &(((struct foo *) 0xa)->f)
728 return Base;
729
730 default:
Zhongxing Xue79a4e62008-11-07 08:57:30 +0000731 assert(0 && "Unhandled Base.");
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000732 return Base;
733 }
Mike Stump11289f42009-09-09 15:08:12 +0000734
Ted Kremenekd3c82762009-03-05 04:50:08 +0000735 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
736 // of FieldDecl.
737 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
738 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000739
Ted Kremenekd3c82762009-03-05 04:50:08 +0000740 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000741}
742
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000743SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
744 SVal Base) {
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000745
Ted Kremenek06032222009-03-09 22:44:49 +0000746 // If the base is an unknown or undefined value, just return it back.
747 // FIXME: For absolute pointer addresses, we just return that value back as
748 // well, although in reality we should return the offset added to that
749 // value.
750 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu36d4ade2008-10-27 12:23:17 +0000751 return Base;
752
Ted Kremenek92d48a72009-01-22 20:27:48 +0000753 // Only handle integer offsets... for now.
754 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000755 return UnknownVal();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000756
Zhongxing Xu7c382642009-05-09 13:20:07 +0000757 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000758
759 // Pointer of any type can be cast and used as array base.
760 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump11289f42009-09-09 15:08:12 +0000761
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000762 // Convert the offset to the appropriate size and signedness.
763 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump11289f42009-09-09 15:08:12 +0000764
Ted Kremenek92d48a72009-01-22 20:27:48 +0000765 if (!ElemR) {
766 //
767 // If the base region is not an ElementRegion, create one.
768 // This can happen in the following example:
769 //
770 // char *p = __builtin_alloc(10);
771 // p[1] = 8;
772 //
Zhongxing Xu7c382642009-05-09 13:20:07 +0000773 // Observe that 'p' binds to an AllocaRegion.
Ted Kremenek92d48a72009-01-22 20:27:48 +0000774 //
Ted Kremenek02e50892009-05-04 06:18:28 +0000775 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000776 BaseRegion, getContext()));
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000777 }
Mike Stump11289f42009-09-09 15:08:12 +0000778
Ted Kremenek92d48a72009-01-22 20:27:48 +0000779 SVal BaseIdx = ElemR->getIndex();
Mike Stump11289f42009-09-09 15:08:12 +0000780
Ted Kremenek92d48a72009-01-22 20:27:48 +0000781 if (!isa<nonloc::ConcreteInt>(BaseIdx))
782 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000783
Ted Kremenek92d48a72009-01-22 20:27:48 +0000784 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
785 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
786 assert(BaseIdxI.isSigned());
Mike Stump11289f42009-09-09 15:08:12 +0000787
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000788 // Compute the new index.
789 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump11289f42009-09-09 15:08:12 +0000790
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000791 // Construct the new ElementRegion.
792 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000793 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump11289f42009-09-09 15:08:12 +0000794 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000795}
796
Ted Kremenek4533a552009-06-16 22:36:44 +0000797//===----------------------------------------------------------------------===//
798// Extents for regions.
799//===----------------------------------------------------------------------===//
800
Zhongxing Xu383c2732009-11-12 02:48:32 +0000801DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000802 const MemRegion *R,
803 QualType EleTy) {
Mike Stump11289f42009-09-09 15:08:12 +0000804
Ted Kremenek94575aa2009-07-10 22:30:06 +0000805 switch (R->getKind()) {
Ted Kremenekacd71a42010-01-05 02:18:06 +0000806 case MemRegion::CXXThisRegionKind:
807 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek04af9f22009-12-07 22:05:27 +0000808 case MemRegion::GenericMemSpaceRegionKind:
809 case MemRegion::StackLocalsSpaceRegionKind:
810 case MemRegion::StackArgumentsSpaceRegionKind:
811 case MemRegion::HeapSpaceRegionKind:
812 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +0000813 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000814 assert(0 && "Cannot index into a MemSpace");
Mike Stump11289f42009-09-09 15:08:12 +0000815 return UnknownVal();
816
Ted Kremenek10a50e72009-11-25 01:32:22 +0000817 case MemRegion::FunctionTextRegionKind:
818 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000819 case MemRegion::BlockDataRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000820 // Technically this can happen if people do funny things with casts.
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000821 return UnknownVal();
Ted Kremenek94575aa2009-07-10 22:30:06 +0000822
823 // Not yet handled.
824 case MemRegion::AllocaRegionKind:
825 case MemRegion::CompoundLiteralRegionKind:
826 case MemRegion::ElementRegionKind:
827 case MemRegion::FieldRegionKind:
828 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000829 case MemRegion::CXXObjectRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000830 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000831
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000832 case MemRegion::SymbolicRegionKind: {
833 const SVal *Size = state->get<RegionExtents>(R);
834 if (!Size)
835 return UnknownVal();
836 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
837 if (!CI)
838 return UnknownVal();
839
840 CharUnits RegionSize =
841 CharUnits::fromQuantity(CI->getValue().getSExtValue());
842 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
843 assert(RegionSize % EleSize == 0);
844
845 return ValMgr.makeIntVal(RegionSize / EleSize, false);
846 }
847
Ted Kremenek94575aa2009-07-10 22:30:06 +0000848 case MemRegion::StringRegionKind: {
849 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump11289f42009-09-09 15:08:12 +0000850 // We intentionally made the size value signed because it participates in
Ted Kremenek94575aa2009-07-10 22:30:06 +0000851 // operations with signed indices.
852 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000853 }
Mike Stump11289f42009-09-09 15:08:12 +0000854
Ted Kremenek94575aa2009-07-10 22:30:06 +0000855 case MemRegion::VarRegionKind: {
856 const VarRegion* VR = cast<VarRegion>(R);
857 // Get the type of the variable.
858 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000859
Ted Kremenek94575aa2009-07-10 22:30:06 +0000860 // FIXME: Handle variable-length arrays.
861 if (isa<VariableArrayType>(T))
862 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000863
Ted Kremenek94575aa2009-07-10 22:30:06 +0000864 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
865 // return the size as signed integer.
866 return ValMgr.makeIntVal(CAT->getSize(), false);
867 }
Ted Kremenekca7935d2009-08-02 05:15:23 +0000868
Ted Kremenek94575aa2009-07-10 22:30:06 +0000869 // Clients can use ordinary variables as if they were arrays. These
870 // essentially are arrays of size 1.
871 return ValMgr.makeIntVal(1, false);
Zhongxing Xuea8c48d2009-05-06 11:51:48 +0000872 }
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000873 }
Mike Stump11289f42009-09-09 15:08:12 +0000874
Ted Kremenek94575aa2009-07-10 22:30:06 +0000875 assert(0 && "Unreachable");
Ted Kremenek47ad37d2009-01-06 19:12:06 +0000876 return UnknownVal();
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000877}
878
Ted Kremenek609df302009-06-17 22:02:04 +0000879const GRState *RegionStoreManager::setExtent(const GRState *state,
880 const MemRegion *region,
881 SVal extent) {
882 return state->set<RegionExtents>(region, extent);
Ted Kremenek4533a552009-06-16 22:36:44 +0000883}
884
885//===----------------------------------------------------------------------===//
886// Location and region casting.
887//===----------------------------------------------------------------------===//
888
Ted Kremenek2907ab72008-12-24 07:46:32 +0000889/// ArrayToPointer - Emulates the "decay" of an array to a pointer
890/// type. 'Array' represents the lvalue of the array being decayed
891/// to a pointer, and the returned SVal represents the decayed
892/// version of that lvalue (i.e., a pointer to the first element of
893/// the array). This is called by GRExprEngine when evaluating casts
894/// from arrays to pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +0000895SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekf065b152008-12-13 19:24:37 +0000896 if (!isa<loc::MemRegionVal>(Array))
897 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000898
Ted Kremenekf065b152008-12-13 19:24:37 +0000899 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
900 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000901
Ted Kremenek167f2fa2009-01-13 01:03:27 +0000902 if (!ArrayR)
Ted Kremenekf065b152008-12-13 19:24:37 +0000903 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000904
Zhongxing Xu34d04b32009-05-09 03:57:34 +0000905 // Strip off typedefs from the ArrayRegion's ValueType.
John McCalla1925362009-09-29 23:03:30 +0000906 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenek02e50892009-05-04 06:18:28 +0000907 ArrayType *AT = cast<ArrayType>(T);
908 T = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +0000909
Ted Kremenekccc22922009-07-16 00:00:11 +0000910 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenek721fcc02009-12-04 00:26:31 +0000911 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
912 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000913}
914
Ted Kremenek4533a552009-06-16 22:36:44 +0000915//===----------------------------------------------------------------------===//
916// Pointer arithmetic.
917//===----------------------------------------------------------------------===//
918
Zhongxing Xu0d081f32010-02-05 05:24:20 +0000919SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenekaf1ac822009-06-26 00:41:43 +0000920 QualType resultTy) {
Zhongxing Xud6daef92009-05-09 15:18:12 +0000921 // Assume the base location is MemRegionVal.
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000922 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xue7d14932009-03-02 07:52:23 +0000923 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000924
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000925 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xud6daef92009-05-09 15:18:12 +0000926 const ElementRegion *ER = 0;
Zhongxing Xua7907602009-05-20 09:00:16 +0000927
Ted Kremenekf6f04612009-07-11 00:58:27 +0000928 switch (MR->getKind()) {
929 case MemRegion::SymbolicRegionKind: {
930 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000931 SymbolRef Sym = SR->getSymbol();
Ted Kremenekd1d60662009-08-25 22:55:09 +0000932 QualType T = Sym->getType(getContext());
933 QualType EleTy;
Mike Stump11289f42009-09-09 15:08:12 +0000934
Ted Kremenekd1d60662009-08-25 22:55:09 +0000935 if (const PointerType *PT = T->getAs<PointerType>())
936 EleTy = PT->getPointeeType();
937 else
John McCall9dd450b2009-09-21 23:43:11 +0000938 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000939
Ted Kremenekf6f04612009-07-11 00:58:27 +0000940 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
941 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000942 break;
Zhongxing Xucc457622009-06-19 04:51:14 +0000943 }
Ted Kremenekf6f04612009-07-11 00:58:27 +0000944 case MemRegion::AllocaRegionKind: {
Ted Kremenekf6f04612009-07-11 00:58:27 +0000945 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000946 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000947 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenekf6f04612009-07-11 00:58:27 +0000948 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
949 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000950 break;
Ted Kremenekf6f04612009-07-11 00:58:27 +0000951 }
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000952
Ted Kremenekf6f04612009-07-11 00:58:27 +0000953 case MemRegion::ElementRegionKind: {
954 ER = cast<ElementRegion>(MR);
955 break;
956 }
Mike Stump11289f42009-09-09 15:08:12 +0000957
Ted Kremenekf6f04612009-07-11 00:58:27 +0000958 // Not yet handled.
959 case MemRegion::VarRegionKind:
Ted Kremenek8ec57712009-10-06 01:39:48 +0000960 case MemRegion::StringRegionKind: {
961
962 }
963 // Fall-through.
Ted Kremenekf6f04612009-07-11 00:58:27 +0000964 case MemRegion::CompoundLiteralRegionKind:
965 case MemRegion::FieldRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000966 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000967 case MemRegion::CXXObjectRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000968 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000969
Ted Kremenek10a50e72009-11-25 01:32:22 +0000970 case MemRegion::FunctionTextRegionKind:
971 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000972 case MemRegion::BlockDataRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000973 // Technically this can happen if people do funny things with casts.
974 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000975
Ted Kremenekacd71a42010-01-05 02:18:06 +0000976 case MemRegion::CXXThisRegionKind:
977 assert(0 &&
978 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek04af9f22009-12-07 22:05:27 +0000979 case MemRegion::GenericMemSpaceRegionKind:
980 case MemRegion::StackLocalsSpaceRegionKind:
981 case MemRegion::StackArgumentsSpaceRegionKind:
982 case MemRegion::HeapSpaceRegionKind:
983 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +0000984 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000985 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
986 return UnknownVal();
Zhongxing Xu540c0092009-06-21 13:24:24 +0000987 }
Zhongxing Xu507202e2009-03-11 07:43:49 +0000988
Zhongxing Xue7d14932009-03-02 07:52:23 +0000989 SVal Idx = ER->getIndex();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000990 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xue7d14932009-03-02 07:52:23 +0000991
Ted Kremenek8ec57712009-10-06 01:39:48 +0000992 // For now, only support:
993 // (a) concrete integer indices that can easily be resolved
994 // (b) 0 + symbolic index
995 if (Base) {
996 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
997 // FIXME: Should use SValuator here.
998 SVal NewIdx =
999 Base->evalBinOp(ValMgr, Op,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001000 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenek8ec57712009-10-06 01:39:48 +00001001 const MemRegion* NewER =
1002 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
1003 ER->getSuperRegion(), getContext());
1004 return ValMgr.makeLoc(NewER);
1005 }
1006 if (0 == Base->getValue()) {
1007 const MemRegion* NewER =
1008 MRMgr.getElementRegion(ER->getElementType(), R,
1009 ER->getSuperRegion(), getContext());
1010 return ValMgr.makeLoc(NewER);
1011 }
Ted Kremenek4c8a5812009-03-03 02:51:43 +00001012 }
Mike Stump11289f42009-09-09 15:08:12 +00001013
Ted Kremenek4c8a5812009-03-03 02:51:43 +00001014 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +00001015}
1016
Ted Kremenek4533a552009-06-16 22:36:44 +00001017//===----------------------------------------------------------------------===//
1018// Loading values from regions.
1019//===----------------------------------------------------------------------===//
1020
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001021Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001022 const MemRegion *R) {
1023 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
1024 return *V;
1025
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001026 return Optional<SVal>();
1027}
1028
1029Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001030 const MemRegion *R) {
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001031 if (R->isBoundable())
1032 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
1033 if (TR->getValueType(getContext())->isUnionType())
1034 return UnknownVal();
1035
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001036 if (const SVal *V = Lookup(B, R, BindingKey::Default))
1037 return *V;
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001038
1039 return Optional<SVal>();
1040}
1041
1042Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
1043 const MemRegion *R) {
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001044
1045 if (Optional<SVal> V = getDirectBinding(B, R))
1046 return V;
1047
1048 return getDefaultBinding(B, R);
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001049}
1050
Ted Kremeneke6fea682009-07-15 02:31:43 +00001051static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
1052 RTy = Ctx.getCanonicalType(RTy);
1053 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump11289f42009-09-09 15:08:12 +00001054
Ted Kremeneke6fea682009-07-15 02:31:43 +00001055 if (RTy == UsedTy)
1056 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001057
1058
Ted Kremenek834e2f62009-07-20 22:58:02 +00001059 // Recursively check the types. We basically want to see if a pointer value
Mike Stump11289f42009-09-09 15:08:12 +00001060 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek834e2f62009-07-20 22:58:02 +00001061 // represents a reinterpretation.
1062 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump11289f42009-09-09 15:08:12 +00001063 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001064 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek834e2f62009-07-20 22:58:02 +00001065
1066 return PUsedTy && PRTy &&
1067 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump11289f42009-09-09 15:08:12 +00001068 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek834e2f62009-07-20 22:58:02 +00001069 }
1070
1071 return true;
Ted Kremeneke6fea682009-07-15 02:31:43 +00001072}
1073
Ted Kremenek267e45a2009-09-24 04:11:44 +00001074const ElementRegion *
1075RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
1076 ASTContext &Ctx = getContext();
1077 SVal idx = ValMgr.makeZeroArrayIndex();
1078 assert(!T.isNull());
1079 return MRMgr.getElementRegion(T, idx, SR, Ctx);
1080}
Ted Kremenek267e45a2009-09-24 04:11:44 +00001081
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001082SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu83aff702008-10-21 05:29:26 +00001083 assert(!isa<UnknownVal>(L) && "location unknown");
1084 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek9158fb72009-12-15 23:23:27 +00001085
Ted Kremenek2907ab72008-12-24 07:46:32 +00001086 // FIXME: Is this even possible? Shouldn't this be treated as a null
1087 // dereference at a higher level?
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001088 if (isa<loc::ConcreteInt>(L))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001089 return UndefinedVal();
Ted Kremenek9158fb72009-12-15 23:23:27 +00001090
Ted Kremenek609df302009-06-17 22:02:04 +00001091 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuec7e7df2009-04-03 07:33:13 +00001092
Zhongxing Xu1075cc02009-05-20 09:18:48 +00001093 // FIXME: return symbolic value for these cases.
Zhongxing Xuec7e7df2009-04-03 07:33:13 +00001094 // Example:
1095 // void f(int* p) { int x = *p; }
Zhongxing Xu1075cc02009-05-20 09:18:48 +00001096 // char* p = alloca();
1097 // read(p);
1098 // c = *p;
Ted Kremenek0c37d192009-07-14 20:48:22 +00001099 if (isa<AllocaRegion>(MR))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001100 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001101
Ted Kremenek267e45a2009-09-24 04:11:44 +00001102 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1103 MR = GetElementZeroRegion(SR, T);
Mike Stump11289f42009-09-09 15:08:12 +00001104
Ted Kremenek0bb32e32009-08-03 21:41:46 +00001105 if (isa<CodeTextRegion>(MR))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001106 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001107
Ted Kremenek2907ab72008-12-24 07:46:32 +00001108 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1109 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek609df302009-06-17 22:02:04 +00001110 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001111 QualType RTy = R->getValueType(getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001112
Ted Kremenek2907ab72008-12-24 07:46:32 +00001113 // FIXME: We should eventually handle funny addressing. e.g.:
1114 //
1115 // int x = ...;
1116 // int *p = &x;
1117 // char *q = (char*) p;
1118 // char c = *q; // returns the first byte of 'x'.
1119 //
1120 // Such funny addressing will occur due to layering of regions.
1121
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001122#if 0
Ted Kremeneke6fea682009-07-15 02:31:43 +00001123 ASTContext &Ctx = getContext();
1124 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001125 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1126 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001127 RTy = T;
Ted Kremenek57fa7e32009-07-15 04:23:32 +00001128 assert(Ctx.getCanonicalType(RTy) ==
1129 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump11289f42009-09-09 15:08:12 +00001130 }
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001131#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001132
Zhongxing Xuce270a62009-03-09 09:15:51 +00001133 if (RTy->isStructureType())
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001134 return RetrieveStruct(store, R);
Mike Stump11289f42009-09-09 15:08:12 +00001135
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001136 // FIXME: Handle unions.
1137 if (RTy->isUnionType())
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001138 return UnknownVal();
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001139
1140 if (RTy->isArrayType())
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001141 return RetrieveArray(store, R);
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001142
Zhongxing Xuce270a62009-03-09 09:15:51 +00001143 // FIXME: handle Vector types.
1144 if (RTy->isVectorType())
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001145 return UnknownVal();
Zhongxing Xu0628f532009-06-28 14:16:39 +00001146
1147 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001148 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu0628f532009-06-28 14:16:39 +00001149
Ted Kremenekbe909b52010-01-11 02:33:26 +00001150 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1151 // FIXME: Here we actually perform an implicit conversion from the loaded
1152 // value to the element type. Eventually we want to compose these values
1153 // more intelligently. For example, an 'element' can encompass multiple
1154 // bound regions (e.g., several bound bytes), or could be a subset of
1155 // a larger value.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001156 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001157 }
Mike Stump11289f42009-09-09 15:08:12 +00001158
Ted Kremenekbe909b52010-01-11 02:33:26 +00001159 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1160 // FIXME: Here we actually perform an implicit conversion from the loaded
1161 // value to the ivar type. What we should model is stores to ivars
1162 // that blow past the extent of the ivar. If the address of the ivar is
1163 // reinterpretted, it is possible we stored a different value that could
1164 // fit within the ivar. Either we need to cast these when storing them
1165 // or reinterpret them lazily (as we do here).
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001166 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001167 }
Mike Stump11289f42009-09-09 15:08:12 +00001168
Ted Kremenekbe909b52010-01-11 02:33:26 +00001169 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1170 // FIXME: Here we actually perform an implicit conversion from the loaded
1171 // value to the variable type. What we should model is stores to variables
1172 // that blow past the extent of the variable. If the address of the
1173 // variable is reinterpretted, it is possible we stored a different value
1174 // that could fit within the variable. Either we need to cast these when
1175 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001176 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001177 }
Ted Kremenek834e2f62009-07-20 22:58:02 +00001178
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001179 RegionBindings B = GetRegionBindings(store);
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001180 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001181
1182 // Check if the region has a binding.
1183 if (V)
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001184 return *V;
Ted Kremenek2907ab72008-12-24 07:46:32 +00001185
Ted Kremenek2907ab72008-12-24 07:46:32 +00001186 // The location does not have a bound value. This means that it has
1187 // the value it had upon its creation and/or entry to the analyzed
1188 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekacd71a42010-01-05 02:18:06 +00001189 if (R->hasStackNonParametersStorage()) {
Ted Kremenek2907ab72008-12-24 07:46:32 +00001190 // All stack variables are considered to have undefined values
1191 // upon creation. All heap allocated blocks are considered to
1192 // have undefined values as well unless they are explicitly bound
1193 // to specific values.
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001194 return UndefinedVal();
Ted Kremenek2907ab72008-12-24 07:46:32 +00001195 }
1196
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001197 // All other values are symbolic.
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001198 return ValMgr.getRegionValueSymbolVal(R, RTy);
Zhongxing Xu83aff702008-10-21 05:29:26 +00001199}
Mike Stump11289f42009-09-09 15:08:12 +00001200
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001201std::pair<Store, const MemRegion *>
Ted Kremenek2c85f172009-08-06 04:50:20 +00001202RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001203 if (Optional<SVal> OV = getDirectBinding(B, R))
1204 if (const nonloc::LazyCompoundVal *V =
1205 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001206 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001207
Ted Kremenekfa417142009-08-06 01:20:57 +00001208 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001209 const std::pair<Store, const MemRegion *> &X =
Ted Kremenekfa417142009-08-06 01:20:57 +00001210 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001211
Ted Kremenekfa417142009-08-06 01:20:57 +00001212 if (X.first)
1213 return std::make_pair(X.first,
1214 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump11289f42009-09-09 15:08:12 +00001215 }
Ted Kremenekfa417142009-08-06 01:20:57 +00001216 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001217 const std::pair<Store, const MemRegion *> &X =
Ted Kremenekfa417142009-08-06 01:20:57 +00001218 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001219
Ted Kremenekfa417142009-08-06 01:20:57 +00001220 if (X.first)
1221 return std::make_pair(X.first,
1222 MRMgr.getFieldRegionWithSuper(FR, X.second));
1223 }
1224
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001225 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremenekfa417142009-08-06 01:20:57 +00001226}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001227
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001228SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xu2d160732009-06-25 05:29:39 +00001229 const ElementRegion* R) {
1230 // Check if the region has a binding.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001231 RegionBindings B = GetRegionBindings(store);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001232 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu2d160732009-06-25 05:29:39 +00001233 return *V;
1234
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001235 const MemRegion* superR = R->getSuperRegion();
1236
Zhongxing Xu2d160732009-06-25 05:29:39 +00001237 // Check if the region is an element region of a string literal.
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001238 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek228539f2009-09-29 16:36:48 +00001239 // FIXME: Handle loads from strings where the literal is treated as
1240 // an integer, e.g., *((unsigned int*)"hello")
1241 ASTContext &Ctx = getContext();
Douglas Gregor4ef1d402009-11-09 22:08:55 +00001242 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek228539f2009-09-29 16:36:48 +00001243 if (T != Ctx.getCanonicalType(R->getElementType()))
1244 return UnknownVal();
1245
Zhongxing Xu2d160732009-06-25 05:29:39 +00001246 const StringLiteral *Str = StrR->getStringLiteral();
1247 SVal Idx = R->getIndex();
1248 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1249 int64_t i = CI->getValue().getSExtValue();
Mike Stump11289f42009-09-09 15:08:12 +00001250 int64_t byteLength = Str->getByteLength();
Ted Kremenekb5850f92009-09-05 17:59:01 +00001251 if (i > byteLength) {
1252 // Buffer overflow checking in GRExprEngine should handle this case,
1253 // but we shouldn't rely on it to not overflow here if that checking
1254 // is disabled.
1255 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001256 }
Ted Kremenekb5850f92009-09-05 17:59:01 +00001257 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek228539f2009-09-29 16:36:48 +00001258 return ValMgr.makeIntVal(c, T);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001259 }
1260 }
Mike Stump11289f42009-09-09 15:08:12 +00001261
Ted Kremenek040e3b92009-08-06 22:33:36 +00001262 // Check if the immediate super region has a direct binding.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001263 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneke6fea682009-07-15 02:31:43 +00001264 if (SymbolRef parentSym = V->getAsSymbol())
1265 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek920ad712009-07-22 04:35:42 +00001266
1267 if (V->isUnknownOrUndef())
1268 return *V;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001269
1270 // Handle LazyCompoundVals for the immediate super region. Other cases
1271 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump11289f42009-09-09 15:08:12 +00001272 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek040e3b92009-08-06 22:33:36 +00001273 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump11289f42009-09-09 15:08:12 +00001274
Ted Kremenek040e3b92009-08-06 22:33:36 +00001275 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001276 return RetrieveElement(LCV->getStore(), R);
Ted Kremenek040e3b92009-08-06 22:33:36 +00001277 }
Mike Stump11289f42009-09-09 15:08:12 +00001278
Ted Kremeneke6fea682009-07-15 02:31:43 +00001279 // Other cases: give up.
Zhongxing Xu61e66922009-07-03 06:11:41 +00001280 return UnknownVal();
Zhongxing Xue205d43c2009-06-30 12:32:59 +00001281 }
Ted Kremenekbe909b52010-01-11 02:33:26 +00001282
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001283 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001284}
1285
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001286SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001287 const FieldRegion* R) {
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001288
1289 // Check if the region has a binding.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001290 RegionBindings B = GetRegionBindings(store);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001291 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001292 return *V;
1293
Ted Kremenek040e3b92009-08-06 22:33:36 +00001294 QualType Ty = R->getValueType(getContext());
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001295 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek040e3b92009-08-06 22:33:36 +00001296}
Mike Stump11289f42009-09-09 15:08:12 +00001297
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001298SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001299 const TypedRegion *R,
1300 QualType Ty,
1301 const MemRegion *superR) {
1302
Mike Stump11289f42009-09-09 15:08:12 +00001303 // At this point we have already checked in either RetrieveElement or
Ted Kremenek040e3b92009-08-06 22:33:36 +00001304 // RetrieveField if 'R' has a direct binding.
Mike Stump11289f42009-09-09 15:08:12 +00001305
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001306 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001307
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001308 while (superR) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001309 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001310 if (SymbolRef parentSym = D->getAsSymbol())
1311 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001312
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001313 if (D->isZeroConstant())
1314 return ValMgr.makeZeroVal(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001315
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001316 if (D->isUnknown())
1317 return *D;
Mike Stump11289f42009-09-09 15:08:12 +00001318
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001319 assert(0 && "Unknown default value");
1320 }
Mike Stump11289f42009-09-09 15:08:12 +00001321
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001322 // If our super region is a field or element itself, walk up the region
1323 // hierarchy to see if there is a default value installed in an ancestor.
1324 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1325 superR = cast<SubRegion>(superR)->getSuperRegion();
1326 continue;
1327 }
Mike Stump11289f42009-09-09 15:08:12 +00001328
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001329 break;
Ted Kremenekfa417142009-08-06 01:20:57 +00001330 }
Mike Stump11289f42009-09-09 15:08:12 +00001331
Ted Kremenekfa417142009-08-06 01:20:57 +00001332 // Lazy binding?
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001333 Store lazyBindingStore = NULL;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001334 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001335 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump11289f42009-09-09 15:08:12 +00001336
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001337 if (lazyBindingStore) {
Ted Kremenek040e3b92009-08-06 22:33:36 +00001338 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump11289f42009-09-09 15:08:12 +00001339
Ted Kremenek040e3b92009-08-06 22:33:36 +00001340 if (isa<ElementRegion>(R))
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001341 return RetrieveElement(lazyBindingStore,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001342 cast<ElementRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001343
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001344 return RetrieveField(lazyBindingStore,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001345 cast<FieldRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001346 }
1347
Ted Kremenekacd71a42010-01-05 02:18:06 +00001348 if (R->hasStackNonParametersStorage()) {
Ted Kremenek040e3b92009-08-06 22:33:36 +00001349 if (isa<ElementRegion>(R)) {
1350 // Currently we don't reason specially about Clang-style vectors. Check
1351 // if superR is a vector and if so return Unknown.
1352 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1353 if (typedSuperR->getValueType(getContext())->isVectorType())
1354 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001355 }
Ted Kremenek040e3b92009-08-06 22:33:36 +00001356 }
Mike Stump11289f42009-09-09 15:08:12 +00001357
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001358 return UndefinedVal();
Ted Kremenek040e3b92009-08-06 22:33:36 +00001359 }
Mike Stump11289f42009-09-09 15:08:12 +00001360
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001361 // All other values are symbolic.
Ted Kremenek9158fb72009-12-15 23:23:27 +00001362 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001363}
Mike Stump11289f42009-09-09 15:08:12 +00001364
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001365SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek48029552009-07-15 06:09:28 +00001366
Ted Kremenek48029552009-07-15 06:09:28 +00001367 // Check if the region has a binding.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001368 RegionBindings B = GetRegionBindings(store);
Ted Kremenek48029552009-07-15 06:09:28 +00001369
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001370 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek48029552009-07-15 06:09:28 +00001371 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001372
Ted Kremenek48029552009-07-15 06:09:28 +00001373 const MemRegion *superR = R->getSuperRegion();
1374
Ted Kremenek481c1212009-10-20 01:20:57 +00001375 // Check if the super region has a default binding.
1376 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek48029552009-07-15 06:09:28 +00001377 if (SymbolRef parentSym = V->getAsSymbol())
1378 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001379
Ted Kremenek48029552009-07-15 06:09:28 +00001380 // Other cases: give up.
1381 return UnknownVal();
1382 }
Mike Stump11289f42009-09-09 15:08:12 +00001383
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001384 return RetrieveLazySymbol(R);
Ted Kremenek834e2f62009-07-20 22:58:02 +00001385}
1386
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001387SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001388
Ted Kremenekfe12f882009-07-21 00:12:07 +00001389 // Check if the region has a binding.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001390 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001391
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001392 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenekfe12f882009-07-21 00:12:07 +00001393 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001394
Ted Kremenekfe12f882009-07-21 00:12:07 +00001395 // Lazily derive a value for the VarRegion.
1396 const VarDecl *VD = R->getDecl();
Ted Kremenek30fe9ec2010-02-06 03:57:59 +00001397 QualType T = VD->getType();
1398 const MemSpaceRegion *MS = R->getMemorySpace();
1399
1400 if (isa<UnknownSpaceRegion>(MS) ||
1401 isa<StackArgumentsSpaceRegion>(MS))
1402 return ValMgr.getRegionValueSymbolVal(R, T);
Mike Stump11289f42009-09-09 15:08:12 +00001403
Ted Kremenek30fe9ec2010-02-06 03:57:59 +00001404 if (isa<GlobalsSpaceRegion>(MS)) {
1405 if (VD->isFileVarDecl())
1406 return ValMgr.getRegionValueSymbolVal(R, T);
Mike Stump11289f42009-09-09 15:08:12 +00001407
Ted Kremenek30fe9ec2010-02-06 03:57:59 +00001408 if (T->isIntegerType())
1409 return ValMgr.makeIntVal(0, T);
Ted Kremenekbdfcacb2010-02-06 04:04:46 +00001410 if (T->isPointerType())
1411 return ValMgr.makeNull();
1412
Ted Kremenek30fe9ec2010-02-06 03:57:59 +00001413 return UnknownVal();
1414 }
1415
Ted Kremenekfe12f882009-07-21 00:12:07 +00001416 return UndefinedVal();
1417}
1418
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001419SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001420
Ted Kremenek834e2f62009-07-20 22:58:02 +00001421 QualType valTy = R->getValueType(getContext());
Ted Kremenek920ad712009-07-22 04:35:42 +00001422
Ted Kremenek48029552009-07-15 06:09:28 +00001423 // All other values are symbolic.
Ted Kremenek9158fb72009-12-15 23:23:27 +00001424 return ValMgr.getRegionValueSymbolVal(R, valTy);
Ted Kremenek48029552009-07-15 06:09:28 +00001425}
1426
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001427SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001428 QualType T = R->getValueType(getContext());
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001429 assert(T->isStructureType());
1430
Zhongxing Xud85a9912009-06-11 07:27:30 +00001431 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001432 RecordDecl* RD = RT->getDecl();
1433 assert(RD->isDefinition());
Mike Stumpc700b362009-08-06 12:56:50 +00001434 (void)RD;
Ted Kremenekfa417142009-08-06 01:20:57 +00001435#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001436 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1437
Ted Kremenek609df302009-06-17 22:02:04 +00001438 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1439 // reverse iterator, we should implement one.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001440 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor91f84212008-12-11 16:49:14 +00001441
Douglas Gregor7a4fad12008-12-11 20:41:00 +00001442 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1443 FieldEnd = Fields.rend();
1444 Field != FieldEnd; ++Field) {
1445 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001446 QualType FTy = (*Field)->getType();
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001447 SVal FieldValue = Retrieve(store, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001448 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1449 }
1450
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001451 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001452#else
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001453 return ValMgr.makeLazyCompoundVal(store, R);
Ted Kremenekfa417142009-08-06 01:20:57 +00001454#endif
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001455}
1456
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001457SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Ted Kremenekfa417142009-08-06 01:20:57 +00001458#if USE_EXPLICIT_COMPOUND
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001459 QualType T = R->getValueType(getContext());
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001460 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1461
1462 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001463 uint64_t size = CAT->getSize().getZExtValue();
1464 for (uint64_t i = 0; i < size; ++i) {
1465 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu838a0db2009-06-16 09:55:50 +00001466 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump11289f42009-09-09 15:08:12 +00001467 getContext());
Ted Kremenek02e50892009-05-04 06:18:28 +00001468 QualType ETy = ER->getElementType();
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001469 SVal ElementVal = Retrieve(store, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001470 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1471 }
1472
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001473 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001474#else
1475 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001476 return ValMgr.makeLazyCompoundVal(store, R);
Ted Kremenekfa417142009-08-06 01:20:57 +00001477#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001478}
1479
Ted Kremenek4533a552009-06-16 22:36:44 +00001480//===----------------------------------------------------------------------===//
1481// Binding values to regions.
1482//===----------------------------------------------------------------------===//
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001483
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001484Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001485 if (isa<loc::MemRegionVal>(L))
Ted Kremenekbe909b52010-01-11 02:33:26 +00001486 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001487 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump11289f42009-09-09 15:08:12 +00001488
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001489 return store;
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001490}
1491
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001492Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +00001493 if (isa<loc::ConcreteInt>(L))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001494 return store;
Zhongxing Xud260db12009-06-28 10:16:11 +00001495
Ted Kremenek4533a552009-06-16 22:36:44 +00001496 // If we get here, the location should be a region.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001497 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +00001498
Ted Kremenek4533a552009-06-16 22:36:44 +00001499 // Check if the region is a struct region.
1500 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1501 if (TR->getValueType(getContext())->isStructureType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001502 return BindStruct(store, TR, V);
Mike Stump11289f42009-09-09 15:08:12 +00001503
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001504 // Special case: the current region represents a cast and it and the super
1505 // region both have pointer types or intptr_t types. If so, perform the
1506 // bind to the super region.
1507 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump11289f42009-09-09 15:08:12 +00001508 // loads that treat integers as pointers and vis versa.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001509 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1510 if (ER->getIndex().isZeroConstant()) {
1511 if (const TypedRegion *superR =
1512 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1513 ASTContext &Ctx = getContext();
1514 QualType superTy = superR->getValueType(Ctx);
1515 QualType erTy = ER->getValueType(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001516
1517 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001518 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu319deb82010-02-04 04:56:43 +00001519 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001520 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001521 }
Ted Kremenek25c9c142009-09-21 22:58:52 +00001522 // For now, just invalidate the fields of the struct/union/class.
1523 // FIXME: Precisely handle the fields of the record.
1524 if (superTy->isRecordType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001525 return InvalidateRegion(store, superR, NULL, 0, NULL);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001526 }
1527 }
1528 }
Ted Kremenek267e45a2009-09-24 04:11:44 +00001529 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1530 // Binding directly to a symbolic region should be treated as binding
1531 // to element 0.
1532 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001533
1534 // FIXME: Is this the right way to handle symbols that are references?
1535 if (const PointerType *PT = T->getAs<PointerType>())
1536 T = PT->getPointeeType();
1537 else
1538 T = T->getAs<ReferenceType>()->getPointeeType();
1539
Ted Kremenek267e45a2009-09-24 04:11:44 +00001540 R = GetElementZeroRegion(SR, T);
1541 }
Mike Stump11289f42009-09-09 15:08:12 +00001542
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001543 // Perform the binding.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001544 RegionBindings B = GetRegionBindings(store);
1545 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek4533a552009-06-16 22:36:44 +00001546}
1547
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001548Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
1549 SVal InitVal) {
Zhongxing Xu29188c22008-11-13 08:41:36 +00001550
Ted Kremenekb006b822009-11-04 00:09:15 +00001551 QualType T = VR->getDecl()->getType();
Zhongxing Xuce716382008-10-31 08:10:01 +00001552
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001553 if (T->isArrayType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001554 return BindArray(store, VR, InitVal);
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001555 if (T->isStructureType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001556 return BindStruct(store, VR, InitVal);
Zhongxing Xu2e8e6042008-11-02 12:13:30 +00001557
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001558 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001559}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001560
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001561// FIXME: this method should be merged into Bind().
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001562Store RegionStoreManager::BindCompoundLiteral(Store store,
1563 const CompoundLiteralExpr *CL,
1564 const LocationContext *LC,
1565 SVal V) {
1566 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek04af9f22009-12-07 22:05:27 +00001567 V);
Zhongxing Xu2c677c32008-11-07 10:38:33 +00001568}
1569
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001570Store RegionStoreManager::setImplicitDefaultValue(Store store,
1571 const MemRegion *R,
1572 QualType T) {
Ted Kremenek439a6d12009-11-19 20:20:24 +00001573 RegionBindings B = GetRegionBindings(store);
1574 SVal V;
1575
1576 if (Loc::IsLocType(T))
1577 V = ValMgr.makeNull();
1578 else if (T->isIntegerType())
1579 V = ValMgr.makeZeroVal(T);
1580 else if (T->isStructureType() || T->isArrayType()) {
1581 // Set the default value to a zero constant when it is a structure
1582 // or array. The type doesn't really matter.
1583 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1584 }
1585 else {
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001586 return store;
Ted Kremenek439a6d12009-11-19 20:20:24 +00001587 }
Ted Kremenek8e994a22010-01-11 00:07:44 +00001588
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001589 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek439a6d12009-11-19 20:20:24 +00001590}
1591
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001592Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
1593 SVal Init) {
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001594
1595 ASTContext &Ctx = getContext();
1596 const ArrayType *AT =
1597 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
1598 QualType ElementTy = AT->getElementType();
1599 Optional<uint64_t> Size;
1600
1601 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1602 Size = CAT->getSize().getZExtValue();
1603
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001604 // Check if the init expr is a StringLiteral.
1605 if (isa<loc::MemRegionVal>(Init)) {
1606 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1607 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1608 const char* str = S->getStrData();
1609 unsigned len = S->getByteLength();
1610 unsigned j = 0;
1611
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001612 // Copy bytes from the string literal into the target array. Trailing bytes
1613 // in the array that are not covered by the string literal are initialized
1614 // to zero.
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001615
1616 // We assume that string constants are bound to
1617 // constant arrays.
Ted Kremenek968999b2010-01-27 16:31:37 +00001618 uint64_t size = Size.getValue();
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001619
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001620 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001621 if (j >= len)
1622 break;
1623
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001624 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001625 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1626 getContext());
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001627
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001628 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001629 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001630 }
1631
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001632 return store;
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001633 }
1634
Ted Kremenekfa417142009-08-06 01:20:57 +00001635 // Handle lazy compound values.
1636 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001637 return CopyLazyBindings(*LCV, store, R);
Mike Stump11289f42009-09-09 15:08:12 +00001638
1639 // Remaining case: explicit compound values.
Ted Kremenek439a6d12009-11-19 20:20:24 +00001640
1641 if (Init.isUnknown())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001642 return setImplicitDefaultValue(store, R, ElementTy);
Ted Kremenek439a6d12009-11-19 20:20:24 +00001643
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001644 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001645 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001646 uint64_t i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001647
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001648 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001649 // The init list might be shorter than the array length.
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001650 if (VI == VE)
1651 break;
1652
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001653 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001654 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001655
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001656 if (ElementTy->isStructureType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001657 store = BindStruct(store, ER, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001658 else
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001659 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001660 }
1661
Ted Kremenek439a6d12009-11-19 20:20:24 +00001662 // If the init list is shorter than the array length, set the
1663 // array default value.
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001664 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001665 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001666
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001667 return store;
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001668}
1669
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001670Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1671 SVal V) {
Mike Stump11289f42009-09-09 15:08:12 +00001672
Ted Kremenek609df302009-06-17 22:02:04 +00001673 if (!Features.supportsFields())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001674 return store;
Mike Stump11289f42009-09-09 15:08:12 +00001675
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001676 QualType T = R->getValueType(getContext());
Zhongxing Xub393b502008-10-31 10:53:01 +00001677 assert(T->isStructureType());
1678
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001679 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xub393b502008-10-31 10:53:01 +00001680 RecordDecl* RD = RT->getDecl();
Zhongxing Xud2e89ae2009-03-11 09:07:35 +00001681
1682 if (!RD->isDefinition())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001683 return store;
Zhongxing Xub393b502008-10-31 10:53:01 +00001684
Ted Kremenekfa417142009-08-06 01:20:57 +00001685 // Handle lazy compound values.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001686 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001687 return CopyLazyBindings(*LCV, store, R);
Mike Stump11289f42009-09-09 15:08:12 +00001688
Ted Kremenek609df302009-06-17 22:02:04 +00001689 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1690 // Ignore them and kill the field values.
1691 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001692 return KillStruct(store, R);
Zhongxing Xu519a47d2009-06-11 09:11:27 +00001693
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001694 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xub393b502008-10-31 10:53:01 +00001695 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xu0442e962009-06-23 05:43:16 +00001696
1697 RecordDecl::field_iterator FI, FE;
1698
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001699 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001700
Zhongxing Xu0442e962009-06-23 05:43:16 +00001701 if (VI == VE)
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001702 break;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001703
Zhongxing Xub393b502008-10-31 10:53:01 +00001704 QualType FTy = (*FI)->getType();
Ted Kremenek30030012009-09-22 21:19:14 +00001705 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xub393b502008-10-31 10:53:01 +00001706
Ted Kremenek30030012009-09-22 21:19:14 +00001707 if (FTy->isArrayType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001708 store = BindArray(store, FR, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001709 else if (FTy->isStructureType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001710 store = BindStruct(store, FR, *VI);
Ted Kremenek30030012009-09-22 21:19:14 +00001711 else
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001712 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu729518b2008-10-24 08:42:28 +00001713 }
1714
Zhongxing Xu0442e962009-06-23 05:43:16 +00001715 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001716 if (FI != FE) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001717 RegionBindings B = GetRegionBindings(store);
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001718 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001719 store = B.getRoot();
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001720 }
Zhongxing Xu0442e962009-06-23 05:43:16 +00001721
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001722 return store;
Zhongxing Xue5816f22008-11-19 11:06:24 +00001723}
1724
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001725Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1726 RegionBindings B = GetRegionBindings(store);
1727 llvm::OwningPtr<RegionStoreSubRegionMap>
1728 SubRegions(getRegionStoreSubRegionMap(store));
1729 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xucff637a2009-01-13 01:49:57 +00001730
Zhongxing Xuc53b4442009-06-25 05:52:16 +00001731 // Set the default value of the struct region to "unknown".
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001732 return Add(B, R, BindingKey::Default, UnknownVal()).getRoot();
Zhongxing Xucff637a2009-01-13 01:49:57 +00001733}
1734
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001735Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1736 Store store, const TypedRegion *R) {
Ted Kremenek4533a552009-06-16 22:36:44 +00001737
Ted Kremenekfa417142009-08-06 01:20:57 +00001738 // Nuke the old bindings stemming from R.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001739 RegionBindings B = GetRegionBindings(store);
Ted Kremenekfa417142009-08-06 01:20:57 +00001740
Mike Stump11289f42009-09-09 15:08:12 +00001741 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001742 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenekfa417142009-08-06 01:20:57 +00001743
Mike Stump11289f42009-09-09 15:08:12 +00001744 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001745 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump11289f42009-09-09 15:08:12 +00001746
Ted Kremenekfa417142009-08-06 01:20:57 +00001747 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1748 // results in a zero-copy algorithm.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001749 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek8e994a22010-01-11 00:07:44 +00001750}
1751
1752//===----------------------------------------------------------------------===//
1753// "Raw" retrievals and bindings.
1754//===----------------------------------------------------------------------===//
1755
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001756BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekbe909b52010-01-11 02:33:26 +00001757 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1758 const RegionRawOffset &O = ER->getAsRawOffset();
1759
1760 if (O.getRegion())
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001761 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001762
1763 // FIXME: There are some ElementRegions for which we cannot compute
1764 // raw offsets yet, including regions with symbolic offsets.
1765 }
1766
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001767 return BindingKey(R, 0, k);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001768}
1769
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001770RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001771 return RBFactory.Add(B, K, V);
1772}
1773
1774RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001775 BindingKey::Kind k, SVal V) {
1776 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek8e994a22010-01-11 00:07:44 +00001777}
1778
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001779const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001780 return B.lookup(K);
1781}
1782
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001783const SVal *RegionStoreManager::Lookup(RegionBindings B,
1784 const MemRegion *R,
1785 BindingKey::Kind k) {
1786 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek8e994a22010-01-11 00:07:44 +00001787}
1788
1789RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1790 return RBFactory.Remove(B, K);
1791}
1792
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001793RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1794 BindingKey::Kind k){
1795 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek8e994a22010-01-11 00:07:44 +00001796}
1797
1798Store RegionStoreManager::Remove(Store store, BindingKey K) {
1799 RegionBindings B = GetRegionBindings(store);
1800 return Remove(B, K).getRoot();
Ted Kremenekfa417142009-08-06 01:20:57 +00001801}
Mike Stump11289f42009-09-09 15:08:12 +00001802
Ted Kremenek4533a552009-06-16 22:36:44 +00001803//===----------------------------------------------------------------------===//
1804// State pruning.
1805//===----------------------------------------------------------------------===//
Ted Kremenekcc224242009-09-29 06:35:00 +00001806
Zhongxing Xuad0ef842010-02-05 05:34:29 +00001807Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
1808 SymbolReaper& SymReaper,
Ted Kremenek4533a552009-06-16 22:36:44 +00001809 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +00001810{
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001811 typedef std::pair<Store, const MemRegion *> RBDNode;
Ted Kremenek9f3a6432009-10-17 17:45:11 +00001812
Ted Kremenek2c85f172009-08-06 04:50:20 +00001813 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001814
Ted Kremenek4533a552009-06-16 22:36:44 +00001815 // The backmap from regions to subregions.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001816 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001817 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenekcc224242009-09-29 06:35:00 +00001818
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001819 // Do a pass over the regions in the store. For VarRegions we check if
1820 // the variable is still live and if so add it to the list of live roots.
1821 // For other regions we populate our region backmap.
Ted Kremenek4533a552009-06-16 22:36:44 +00001822 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenekcc224242009-09-29 06:35:00 +00001823
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001824 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001825 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001826 const MemRegion *R = I.getKey().getRegion();
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001827 IntermediateRoots.push_back(R);
Ted Kremenek4533a552009-06-16 22:36:44 +00001828 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001829
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001830 // Process the "intermediate" roots to find if they are referenced by
Mike Stump11289f42009-09-09 15:08:12 +00001831 // real roots.
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001832 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001833 llvm::SmallVector<RBDNode, 10> Postponed;
1834
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001835 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001836
Ted Kremenek4533a552009-06-16 22:36:44 +00001837 while (!IntermediateRoots.empty()) {
1838 const MemRegion* R = IntermediateRoots.back();
1839 IntermediateRoots.pop_back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001840
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001841 if (IntermediateVisited.count(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001842 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001843 IntermediateVisited.insert(R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001844
Ted Kremenek4533a552009-06-16 22:36:44 +00001845 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekc32f2c22009-12-04 20:32:20 +00001846 if (SymReaper.isLive(Loc, VR))
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001847 WorkList.push_back(std::make_pair(store, VR));
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001848 continue;
1849 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001850
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001851 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001852 llvm::SmallVectorImpl<RBDNode> &Q =
1853 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1854
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001855 Q.push_back(std::make_pair(store, SR));
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001856
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001857 continue;
Ted Kremenek4533a552009-06-16 22:36:44 +00001858 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001859
1860 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001861 if (const SubRegion* superR =
Ted Kremenekcc224242009-09-29 06:35:00 +00001862 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001863 IntermediateRoots.push_back(superR);
Ted Kremenek4533a552009-06-16 22:36:44 +00001864 }
Mike Stump11289f42009-09-09 15:08:12 +00001865
Ted Kremenekcc224242009-09-29 06:35:00 +00001866 // Enqueue the RegionRoots onto WorkList.
1867 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1868 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001869 WorkList.push_back(std::make_pair(store, *I));
Mike Stump11289f42009-09-09 15:08:12 +00001870 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001871 RegionRoots.clear();
1872
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001873 llvm::DenseSet<RBDNode> Visited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001874
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001875tryAgain:
Ted Kremenekcc224242009-09-29 06:35:00 +00001876 while (!WorkList.empty()) {
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001877 RBDNode N = WorkList.back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001878 WorkList.pop_back();
1879
1880 // Have we visited this node before?
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001881 if (Visited.count(N))
Ted Kremenekcc224242009-09-29 06:35:00 +00001882 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001883 Visited.insert(N);
Mike Stump11289f42009-09-09 15:08:12 +00001884
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001885 const MemRegion *R = N.second;
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001886 Store store_N = N.first;
Ted Kremenekcc224242009-09-29 06:35:00 +00001887
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001888 // Enqueue subregions.
1889 RegionStoreSubRegionMap *M;
1890
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001891 if (store == store_N)
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001892 M = SubRegions.get();
1893 else {
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001894 RegionStoreSubRegionMap *& SM = SC[store_N];
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001895 if (!SM)
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001896 SM = getRegionStoreSubRegionMap(store_N);
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001897 M = SM;
1898 }
Ted Kremenekb251eb62010-02-02 22:38:47 +00001899
1900 if (const RegionStoreSubRegionMap::Set *S = M->getSubRegions(R))
1901 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
1902 I != E; ++I)
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001903 WorkList.push_back(std::make_pair(store_N, *I));
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001904
Ted Kremenekcc224242009-09-29 06:35:00 +00001905 // Enqueue the super region.
1906 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1907 const MemRegion *superR = SR->getSuperRegion();
1908 if (!isa<MemSpaceRegion>(superR)) {
1909 // If 'R' is a field or an element, we want to keep the bindings
1910 // for the other fields and elements around. The reason is that
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001911 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8b2f5d32009-10-17 07:32:08 +00001912 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1913 || isa<ObjCIvarRegion>(R));
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001914 WorkList.push_back(std::make_pair(store_N, superR));
Ted Kremenekcc224242009-09-29 06:35:00 +00001915 }
1916 }
1917
1918 // Mark the symbol for any live SymbolicRegion as "live". This means we
1919 // should continue to track that symbol.
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001920 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001921 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001922
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001923 // For BlockDataRegions, enqueue the VarRegions for variables marked
1924 // with __block (passed-by-reference).
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001925 // via BlockDeclRefExprs.
1926 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1927 for (BlockDataRegion::referenced_vars_iterator
1928 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001929 RI != RE; ++RI) {
1930 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001931 WorkList.push_back(std::make_pair(store_N, *RI));
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001932 }
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001933 // No possible data bindings on a BlockDataRegion. Continue to the
1934 // next region in the worklist.
1935 continue;
1936 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001937
Ted Kremenekcc224242009-09-29 06:35:00 +00001938 RegionBindings B_N = GetRegionBindings(store_N);
1939
1940 // Get the data binding for R (if any).
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001941 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001942
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001943 if (V) {
1944 // Check for lazy bindings.
1945 if (const nonloc::LazyCompoundVal *LCV =
1946 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenekcc224242009-09-29 06:35:00 +00001947
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001948 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001949 WorkList.push_back(std::make_pair(D->getStore(), D->getRegion()));
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001950 }
1951 else {
Ted Kremenekcc224242009-09-29 06:35:00 +00001952 // Update the set of live symbols.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001953 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenekcc224242009-09-29 06:35:00 +00001954 SI!=SE;++SI)
1955 SymReaper.markLive(*SI);
1956
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001957 // If V is a region, then add it to the worklist.
1958 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001959 WorkList.push_back(std::make_pair(store_N, RX));
Ted Kremenekcc224242009-09-29 06:35:00 +00001960 }
1961 }
1962 }
1963
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001964 // See if any postponed SymbolicRegions are actually live now, after
1965 // having done a scan.
1966 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1967 E = Postponed.end() ; I != E ; ++I) {
1968 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1969 if (SymReaper.isLive(SR->getSymbol())) {
1970 WorkList.push_back(*I);
1971 I->second = NULL;
1972 }
1973 }
1974 }
1975
1976 if (!WorkList.empty())
1977 goto tryAgain;
1978
Ted Kremenek4533a552009-06-16 22:36:44 +00001979 // We have now scanned the store, marking reachable regions and symbols
1980 // as live. We now remove all the regions that are dead from the store
Mike Stump11289f42009-09-09 15:08:12 +00001981 // as well as update DSymbols with the set symbols that are now dead.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001982 Store new_store = store;
Ted Kremenek2c85f172009-08-06 04:50:20 +00001983 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001984 const MemRegion* R = I.getKey().getRegion();
Ted Kremenek4533a552009-06-16 22:36:44 +00001985 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001986 if (Visited.count(std::make_pair(store, R)))
Ted Kremenek4533a552009-06-16 22:36:44 +00001987 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001988
Ted Kremenek4533a552009-06-16 22:36:44 +00001989 // Remove this dead region from the store.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001990 new_store = Remove(new_store, I.getKey());
Mike Stump11289f42009-09-09 15:08:12 +00001991
Ted Kremenek4533a552009-06-16 22:36:44 +00001992 // Mark all non-live symbols that this region references as dead.
1993 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1994 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00001995
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001996 SVal X = I.getData();
Ted Kremenekf106ab92009-08-02 05:00:15 +00001997 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1998 for (; SI != SE; ++SI)
1999 SymReaper.maybeDead(*SI);
2000 }
Mike Stump11289f42009-09-09 15:08:12 +00002001
Zhongxing Xuad0ef842010-02-05 05:34:29 +00002002 return new_store;
Ted Kremenek4533a552009-06-16 22:36:44 +00002003}
2004
Zhongxing Xudaa41762009-10-13 02:24:55 +00002005GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
2006 StackFrameContext const *frame) {
2007 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
2008 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
2009
2010 FunctionDecl::param_const_iterator PI = FD->param_begin();
2011
2012 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
2013
2014 // Copy the arg expression value to the arg variables.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00002015 Store store = state->getStore();
Zhongxing Xudaa41762009-10-13 02:24:55 +00002016 for (; AI != AE; ++AI, ++PI) {
2017 SVal ArgVal = state->getSVal(*AI);
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00002018 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xudaa41762009-10-13 02:24:55 +00002019 }
2020
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00002021 return state->makeWithStore(store);
Zhongxing Xudaa41762009-10-13 02:24:55 +00002022}
2023
Ted Kremenek4533a552009-06-16 22:36:44 +00002024//===----------------------------------------------------------------------===//
2025// Utility methods.
2026//===----------------------------------------------------------------------===//
2027
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002028void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek4533a552009-06-16 22:36:44 +00002029 const char* nl, const char *sep) {
Ted Kremenek2c85f172009-08-06 04:50:20 +00002030 RegionBindings B = GetRegionBindings(store);
Ted Kremenek481c1212009-10-20 01:20:57 +00002031 OS << "Store (direct and default bindings):" << nl;
Mike Stump11289f42009-09-09 15:08:12 +00002032
Ted Kremenek2c85f172009-08-06 04:50:20 +00002033 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002034 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek4533a552009-06-16 22:36:44 +00002035}