blob: b53fdee138c3b3313d295ce040fec38fbbc5ad1d [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
Ted Kremenek1309f9a2010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/MemRegion.h"
18#include "clang/Analysis/AnalysisContext.h"
19#include "clang/Checker/PathSensitive/GRState.h"
20#include "clang/Checker/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd4e5a602009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000024#include "clang/AST/CharUnits.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025
26#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000027#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000028#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000029
30using namespace clang;
31
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000032//===----------------------------------------------------------------------===//
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000033// Representation of binding keys.
34//===----------------------------------------------------------------------===//
35
36namespace {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000037class BindingKey {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000038public:
Ted Kremeneke393f4a2010-02-03 03:06:46 +000039 enum Kind { Direct = 0x0, Default = 0x1 };
40private:
41 llvm ::PointerIntPair<const MemRegion*, 1> P;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000042 uint64_t Offset;
43
Ted Kremeneke393f4a2010-02-03 03:06:46 +000044 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
Zhongxing Xubfcaf802010-02-05 02:26:30 +000045 : P(r, (unsigned) k), Offset(offset) { assert(r); }
Ted Kremeneke393f4a2010-02-03 03:06:46 +000046public:
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000047
Ted Kremeneke393f4a2010-02-03 03:06:46 +000048 bool isDefault() const { return P.getInt() == Default; }
49 bool isDirect() const { return P.getInt() == Direct; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000050
Ted Kremeneke393f4a2010-02-03 03:06:46 +000051 const MemRegion *getRegion() const { return P.getPointer(); }
52 uint64_t getOffset() const { return Offset; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000053
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000054 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000055 ID.AddPointer(P.getOpaqueValue());
56 ID.AddInteger(Offset);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000057 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000058
Ted Kremeneke393f4a2010-02-03 03:06:46 +000059 static BindingKey Make(const MemRegion *R, Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000060
Ted Kremeneke393f4a2010-02-03 03:06:46 +000061 bool operator<(const BindingKey &X) const {
62 if (P.getOpaqueValue() < X.P.getOpaqueValue())
63 return true;
64 if (P.getOpaqueValue() > X.P.getOpaqueValue())
65 return false;
66 return Offset < X.Offset;
67 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000068
Ted Kremeneke393f4a2010-02-03 03:06:46 +000069 bool operator==(const BindingKey &X) const {
70 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
71 Offset == X.Offset;
72 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000073};
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000074} // end anonymous namespace
75
76namespace llvm {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000077 static inline
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000078 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000079 os << '(' << K.getRegion() << ',' << K.getOffset()
80 << ',' << (K.isDirect() ? "direct" : "default")
81 << ')';
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000082 return os;
83 }
84} // end llvm namespace
85
86//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +000087// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000088//===----------------------------------------------------------------------===//
89
Ted Kremeneke393f4a2010-02-03 03:06:46 +000090typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000091
Ted Kremenek50dc1b32008-12-24 01:05:03 +000092//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000093// Fine-grained control of RegionStoreManager.
94//===----------------------------------------------------------------------===//
95
96namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000097struct minimal_features_tag {};
98struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +000099
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000100class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000101 bool SupportsFields;
102 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Ted Kremenek9af46f52009-06-16 22:36:44 +0000104public:
105 RegionStoreFeatures(minimal_features_tag) :
106 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenek9af46f52009-06-16 22:36:44 +0000108 RegionStoreFeatures(maximal_features_tag) :
109 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenek9af46f52009-06-16 22:36:44 +0000111 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek9af46f52009-06-16 22:36:44 +0000113 bool supportsFields() const { return SupportsFields; }
114 bool supportsRemaining() const { return SupportsRemaining; }
115};
116}
117
118//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000119// Region "Extents"
120//===----------------------------------------------------------------------===//
121//
122// MemRegions represent chunks of memory with a size (their "extent"). This
123// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000124//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000125namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000126static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000127namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000128 template<> struct GRStateTrait<RegionExtents>
129 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
130 static void* GDMIndex() { return &RegionExtentsIndex; }
131 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000132}
133
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000134//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000135// Utility functions.
136//===----------------------------------------------------------------------===//
137
138static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
139 if (ty->isAnyPointerType())
140 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000142 return ty->isIntegerType() && ty->isScalarType() &&
143 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
144}
145
146//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000147// Main RegionStore logic.
148//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000149
Zhongxing Xu17892752008-10-08 02:50:44 +0000150namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000152class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekdf165012010-02-02 22:38:47 +0000153public:
154 typedef llvm::ImmutableSet<const MemRegion*> Set;
155 typedef llvm::DenseMap<const MemRegion*, Set> Map;
156private:
157 Set::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000158 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000159public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000160 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000161 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000162
163 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000164 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000165 return true;
166 }
167
168 I->second = F.Add(I->second, SubRegion);
169 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000170 }
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000172 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremenek59e8f112009-03-03 01:35:36 +0000174 ~RegionStoreSubRegionMap() {}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000175
Ted Kremenekdf165012010-02-02 22:38:47 +0000176 const Set *getSubRegions(const MemRegion *Parent) const {
177 Map::const_iterator I = M.find(Parent);
178 return I == M.end() ? NULL : &I->second;
179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek5dc27462009-03-03 02:51:43 +0000181 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000182 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000183
184 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000185 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Ted Kremenekdf165012010-02-02 22:38:47 +0000187 Set S = I->second;
188 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000189 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000190 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenek5dc27462009-03-03 02:51:43 +0000193 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000196
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000197
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000198class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000199 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000200 RegionBindings::Factory RBFactory;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000201
Zhongxing Xu17892752008-10-08 02:50:44 +0000202public:
Mike Stump1eb44332009-09-09 15:08:12 +0000203 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000204 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000205 Features(f),
Ted Kremenek8928d412010-02-04 04:14:49 +0000206 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000207
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000208 SubRegionMap *getSubRegionMap(Store store) {
209 return getRegionStoreSubRegionMap(store);
210 }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Zhongxing Xu13d50172009-10-11 08:08:02 +0000212 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Zhongxing Xu13d50172009-10-11 08:08:02 +0000214 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
215 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000216 /// getDefaultBinding - Returns an SVal* representing an optional default
217 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000218 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000219
Ted Kremenek027e2662009-11-19 20:20:24 +0000220 /// setImplicitDefaultValue - Set the default binding for the provided
221 /// MemRegion to the value implicitly defined for compound literals when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000222 /// the value is not specified.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000223 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000225 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
226 /// type. 'Array' represents the lvalue of the array being decayed
227 /// to a pointer, and the returned SVal represents the decayed
228 /// version of that lvalue (i.e., a pointer to the first element of
229 /// the array). This is called by GRExprEngine when evaluating
230 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000231 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000232
Zhongxing Xu461147f2010-02-05 05:24:20 +0000233 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000234
Mike Stump1eb44332009-09-09 15:08:12 +0000235 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000236 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000237 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000238
Ted Kremenek67f28532009-06-17 22:02:04 +0000239 //===-------------------------------------------------------------------===//
240 // Binding values to regions.
241 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000242
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000243 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000244 unsigned Count, InvalidatedSymbols *IS) {
245 return RegionStoreManager::InvalidateRegions(store, &R, &R+1, E, Count, IS);
Ted Kremenek81a95832009-12-03 03:27:11 +0000246 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000247
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000248 Store InvalidateRegions(Store store,
249 const MemRegion * const *Begin,
250 const MemRegion * const *End,
251 const Expr *E, unsigned Count,
252 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000254public: // Made public for helper classes.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000255
Zhongxing Xu13d50172009-10-11 08:08:02 +0000256 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000257 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000259 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
260
261 RegionBindings Add(RegionBindings B, const MemRegion *R,
262 BindingKey::Kind k, SVal V);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000263
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000264 const SVal *Lookup(RegionBindings B, BindingKey K);
265 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000266
267 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000268 RegionBindings Remove(RegionBindings B, const MemRegion *R,
269 BindingKey::Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000270
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000271 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
272 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000273 }
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000274
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000275 Store Remove(Store store, BindingKey K);
276
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000277public: // Part of public interface to class.
278
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000279 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000280
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000281 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
282 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000284 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000285
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000286 Store BindDeclWithNoInit(Store store, const VarRegion *) {
287 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000288 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000289
Ted Kremenek67f28532009-06-17 22:02:04 +0000290 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000291 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000293 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000294
295 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000296 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000297
Ted Kremenek67f28532009-06-17 22:02:04 +0000298 Store Remove(Store store, Loc LV);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000299
Ted Kremenek67f28532009-06-17 22:02:04 +0000300
301 //===------------------------------------------------------------------===//
302 // Loading values from regions.
303 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremenek67f28532009-06-17 22:02:04 +0000305 /// The high level logic for this method is this:
306 /// Retrieve (L)
307 /// if L has binding
308 /// return L's binding
309 /// else if L is in killset
310 /// return unknown
311 /// else
312 /// if L is on stack or heap
313 /// return undefined
314 /// else
315 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000316 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000317
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000318 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000319
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000320 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Zhongxing Xu576bb922010-02-05 03:01:53 +0000322 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Zhongxing Xu576bb922010-02-05 03:01:53 +0000324 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Zhongxing Xu576bb922010-02-05 03:01:53 +0000326 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000328 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000329 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Ted Kremenek67f28532009-06-17 22:02:04 +0000331 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000332 /// struct copy:
333 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000334 /// x = y;
335 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000336 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Zhongxing Xu576bb922010-02-05 03:01:53 +0000338 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000340 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000341 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000342 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000344 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
345 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000346
Zhongxing Xu81491852010-02-08 08:43:02 +0000347 const ElementRegion *GetElementZeroRegion(const MemRegion *R, QualType T);
Ted Kremenek0954cde2009-09-24 04:11:44 +0000348
Ted Kremenek67f28532009-06-17 22:02:04 +0000349 //===------------------------------------------------------------------===//
350 // State pruning.
351 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremenek67f28532009-06-17 22:02:04 +0000353 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
354 /// It returns a new Store with these values removed.
Zhongxing Xu72119c42010-02-05 05:34:29 +0000355 Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000356 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
357
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000358 const GRState *EnterStackFrame(const GRState *state,
359 const StackFrameContext *frame);
360
Ted Kremenek67f28532009-06-17 22:02:04 +0000361 //===------------------------------------------------------------------===//
362 // Region "extents".
363 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000365 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000366 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000367 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000368
369 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000370 // Utility methods.
371 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek451ac092009-08-06 04:50:20 +0000373 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000374 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000375 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000376
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000377 void print(Store store, llvm::raw_ostream& Out, const char* nl,
378 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000379
380 void iterBindings(Store store, BindingsHandler& f) {
381 // FIXME: Implement.
382 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000383
Ted Kremenek67f28532009-06-17 22:02:04 +0000384 // FIXME: Remove.
385 BasicValueFactory& getBasicVals() {
386 return StateMgr.getBasicVals();
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek67f28532009-06-17 22:02:04 +0000389 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000390 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000391};
392
393} // end anonymous namespace
394
Ted Kremenek9af46f52009-06-16 22:36:44 +0000395//===----------------------------------------------------------------------===//
396// RegionStore creation.
397//===----------------------------------------------------------------------===//
398
399StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
400 RegionStoreFeatures F = maximal_features_tag();
401 return new RegionStoreManager(StMgr, F);
402}
403
404StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
405 RegionStoreFeatures F = minimal_features_tag();
406 F.enableFields(true);
407 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000408}
409
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000410void
411RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000412 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000413 const MemRegion *superR = R->getSuperRegion();
414 if (add(superR, R))
415 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000416 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000417}
418
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000419RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000420RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
421 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000422 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000424 llvm::SmallVector<const SubRegion*, 10> WL;
425
Ted Kremenek451ac092009-08-06 04:50:20 +0000426 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000427 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000428 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Mike Stump1eb44332009-09-09 15:08:12 +0000430 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000431 // don't have direct bindings but are super regions of those that do.
432 while (!WL.empty()) {
433 const SubRegion *R = WL.back();
434 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000435 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000436 }
437
Ted Kremenek14453bf2009-03-03 19:02:42 +0000438 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000439}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000440
Ted Kremenek9af46f52009-06-16 22:36:44 +0000441//===----------------------------------------------------------------------===//
Ted Kremeneka4fab032010-03-10 07:19:59 +0000442// Region Cluster analysis.
443//===----------------------------------------------------------------------===//
444
445namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000446template <typename DERIVED>
Ted Kremeneka4fab032010-03-10 07:19:59 +0000447class ClusterAnalysis {
448protected:
449 typedef BumpVector<BindingKey> RegionCluster;
450 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5499b842010-03-10 16:32:56 +0000451 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
452 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
453 WorkList;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000454
455 BumpVectorContext BVC;
456 ClusterMap ClusterM;
Ted Kremenek5499b842010-03-10 16:32:56 +0000457 WorkList WL;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000458
459 RegionStoreManager &RM;
460 ASTContext &Ctx;
461 ValueManager &ValMgr;
462
Ted Kremenek5499b842010-03-10 16:32:56 +0000463 RegionBindings B;
464
Ted Kremeneka4fab032010-03-10 07:19:59 +0000465public:
Ted Kremenek5499b842010-03-10 16:32:56 +0000466 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
467 RegionBindings b)
468 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
469 B(b) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000470
Ted Kremenek5499b842010-03-10 16:32:56 +0000471 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000472
Ted Kremenek5499b842010-03-10 16:32:56 +0000473 void AddToCluster(BindingKey K) {
474 const MemRegion *R = K.getRegion();
475 const MemRegion *baseR = R->getBaseRegion();
476 RegionCluster &C = getCluster(baseR);
477 C.push_back(K, BVC);
478 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000479 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000480
Ted Kremenek5499b842010-03-10 16:32:56 +0000481 bool isVisited(const MemRegion *R) {
482 return (bool) Visited[&getCluster(R->getBaseRegion())];
483 }
484
485 RegionCluster& getCluster(const MemRegion *R) {
486 RegionCluster *&CRef = ClusterM[R];
487 if (!CRef) {
488 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
489 CRef = new (Mem) RegionCluster(BVC, 10);
490 }
491 return *CRef;
492 }
493
494 void GenerateClusters() {
495 // Scan the entire set of bindings and make the region clusters.
496 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
497 AddToCluster(RI.getKey());
498 if (const MemRegion *R = RI.getData().getAsRegion()) {
499 // Generate a cluster, but don't add the region to the cluster
500 // if there aren't any bindings.
501 getCluster(R->getBaseRegion());
502 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000503 }
504 }
Ted Kremenek5499b842010-03-10 16:32:56 +0000505
506 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
507 if (unsigned &visited = Visited[&C])
508 return false;
509 else
510 visited = 1;
511
512 WL.push_back(std::make_pair(R, &C));
513 return true;
514 }
515
516 bool AddToWorkList(BindingKey K) {
517 return AddToWorkList(K.getRegion());
518 }
519
520 bool AddToWorkList(const MemRegion *R) {
521 const MemRegion *baseR = R->getBaseRegion();
522 return AddToWorkList(baseR, getCluster(baseR));
523 }
524
525 void RunWorkList() {
526 while (!WL.empty()) {
527 const MemRegion *baseR;
528 RegionCluster *C;
529 llvm::tie(baseR, C) = WL.back();
530 WL.pop_back();
531
532 // First visit the cluster.
533 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
534
535 // Next, visit the region.
536 static_cast<DERIVED*>(this)->VisitRegion(baseR);
537 }
538 }
539
540public:
541 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
542 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
543 void VisitRegion(const MemRegion *baseR) {}
544};
Ted Kremeneka4fab032010-03-10 07:19:59 +0000545}
546
547//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000548// Binding invalidation.
549//===----------------------------------------------------------------------===//
550
Zhongxing Xu13d50172009-10-11 08:08:02 +0000551void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
552 const MemRegion *R,
553 RegionStoreSubRegionMap &M) {
Ted Kremeneka4fab032010-03-10 07:19:59 +0000554
Ted Kremenekdf165012010-02-02 22:38:47 +0000555 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
556 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
557 I != E; ++I)
558 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000559
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000560 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000561}
562
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000563namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000564class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
565{
566 const Expr *Ex;
567 unsigned Count;
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000568 StoreManager::InvalidatedSymbols *IS;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000569public:
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000570 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenek5499b842010-03-10 16:32:56 +0000571 GRStateManager &stateMgr,
572 RegionBindings b,
573 const Expr *ex, unsigned count,
574 StoreManager::InvalidatedSymbols *is)
575 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
576 Ex(ex), Count(count), IS(is) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000577
Ted Kremenek5499b842010-03-10 16:32:56 +0000578 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
579 void VisitRegion(const MemRegion *baseR);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000580
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000581private:
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000582 void VisitBinding(SVal V);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000583};
Ted Kremenek5b290652010-02-03 04:16:00 +0000584}
585
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000586void InvalidateRegionsWorker::VisitBinding(SVal V) {
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000587 // A symbol? Mark it touched by the invalidation.
588 if (IS)
589 if (SymbolRef Sym = V.getAsSymbol())
590 IS->insert(Sym);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000591
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000592 if (const MemRegion *R = V.getAsRegion()) {
593 AddToWorkList(R);
594 return;
595 }
596
597 // Is it a LazyCompoundVal? All references get invalidated as well.
598 if (const nonloc::LazyCompoundVal *LCS =
599 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
600
601 const MemRegion *LazyR = LCS->getRegion();
602 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
603
604 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
605 const MemRegion *baseR = RI.getKey().getRegion();
606 if (cast<SubRegion>(baseR)->isSubRegionOf(LazyR))
607 VisitBinding(RI.getData());
608 }
609
610 return;
611 }
612}
613
Ted Kremenek5499b842010-03-10 16:32:56 +0000614void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
615 BindingKey *I, BindingKey *E) {
616 for ( ; I != E; ++I) {
617 // Get the old binding. Is it a region? If so, add it to the worklist.
618 const BindingKey &K = *I;
619 if (const SVal *V = RM.Lookup(B, K))
620 VisitBinding(*V);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000621
Ted Kremenek5499b842010-03-10 16:32:56 +0000622 B = RM.Remove(B, K);
623 }
624}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000625
Ted Kremenek5499b842010-03-10 16:32:56 +0000626void InvalidateRegionsWorker::VisitRegion(const MemRegion *baseR) {
627 if (IS) {
628 // Symbolic region? Mark that symbol touched by the invalidation.
629 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
630 IS->insert(SR->getSymbol());
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000631 }
632
Ted Kremenek5499b842010-03-10 16:32:56 +0000633 // BlockDataRegion? If so, invalidate captured variables that are passed
634 // by reference.
635 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
636 for (BlockDataRegion::referenced_vars_iterator
637 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
638 BI != BE; ++BI) {
639 const VarRegion *VR = *BI;
640 const VarDecl *VD = VR->getDecl();
641 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
642 AddToWorkList(VR);
643 }
644 return;
645 }
646
647 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
648 // Invalidate the region by setting its default value to
649 // conjured symbol. The type of the symbol is irrelavant.
650 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
651 Count);
652 B = RM.Add(B, baseR, BindingKey::Default, V);
653 return;
654 }
655
656 if (!baseR->isBoundable())
657 return;
658
659 const TypedRegion *TR = cast<TypedRegion>(baseR);
660 QualType T = TR->getValueType(Ctx);
661
662 // Invalidate the binding.
663 if (const RecordType *RT = T->getAsStructureType()) {
664 const RecordDecl *RD = RT->getDecl()->getDefinition();
665 // No record definition. There is nothing we can do.
666 if (!RD) {
667 B = RM.Remove(B, baseR);
668 return;
669 }
670
671 // Invalidate the region by setting its default value to
672 // conjured symbol. The type of the symbol is irrelavant.
673 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
674 Count);
675 B = RM.Add(B, baseR, BindingKey::Default, V);
676 return;
677 }
678
679 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
680 // Set the default value of the array to conjured symbol.
681 DefinedOrUnknownSVal V =
682 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
683 B = RM.Add(B, baseR, BindingKey::Default, V);
684 return;
685 }
686
687 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
688 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
689 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000690}
691
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000692Store RegionStoreManager::InvalidateRegions(Store store,
693 const MemRegion * const *I,
694 const MemRegion * const *E,
695 const Expr *Ex, unsigned Count,
696 InvalidatedSymbols *IS) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000697 InvalidateRegionsWorker W(*this, StateMgr,
698 RegionStoreManager::GetRegionBindings(store),
699 Ex, Count, IS);
700
701 // Scan the bindings and generate the clusters.
702 W.GenerateClusters();
703
704 // Add I .. E to the worklist.
705 for ( ; I != E; ++I)
706 W.AddToWorkList(*I);
707
708 W.RunWorkList();
709
710 // Return the new bindings.
711 return W.getRegionBindings().getRoot();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000712}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000713
Ted Kremenek9af46f52009-06-16 22:36:44 +0000714//===----------------------------------------------------------------------===//
715// Extents for regions.
716//===----------------------------------------------------------------------===//
717
Zhongxing Xue884ff82009-11-12 02:48:32 +0000718DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000719 const MemRegion *R,
720 QualType EleTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000722 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000723 case MemRegion::CXXThisRegionKind:
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000724 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000725 case MemRegion::GenericMemSpaceRegionKind:
726 case MemRegion::StackLocalsSpaceRegionKind:
727 case MemRegion::StackArgumentsSpaceRegionKind:
728 case MemRegion::HeapSpaceRegionKind:
729 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000730 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000731 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000732 return UnknownVal();
733
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000734 case MemRegion::FunctionTextRegionKind:
735 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000736 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000737 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000738 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000739
740 // Not yet handled.
741 case MemRegion::AllocaRegionKind:
742 case MemRegion::CompoundLiteralRegionKind:
743 case MemRegion::ElementRegionKind:
744 case MemRegion::FieldRegionKind:
745 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000746 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000747 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000749 case MemRegion::SymbolicRegionKind: {
750 const SVal *Size = state->get<RegionExtents>(R);
751 if (!Size)
752 return UnknownVal();
753 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
754 if (!CI)
755 return UnknownVal();
756
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000757 CharUnits RegionSize =
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000758 CharUnits::fromQuantity(CI->getValue().getSExtValue());
759 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
760 assert(RegionSize % EleSize == 0);
761
762 return ValMgr.makeIntVal(RegionSize / EleSize, false);
763 }
764
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000765 case MemRegion::StringRegionKind: {
766 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000767 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000768 // operations with signed indices.
769 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000772 case MemRegion::VarRegionKind: {
773 const VarRegion* VR = cast<VarRegion>(R);
774 // Get the type of the variable.
775 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000777 // FIXME: Handle variable-length arrays.
778 if (isa<VariableArrayType>(T))
779 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000781 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
782 // return the size as signed integer.
783 return ValMgr.makeIntVal(CAT->getSize(), false);
784 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000785
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000786 // Clients can use ordinary variables as if they were arrays. These
787 // essentially are arrays of size 1.
788 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000789 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000792 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000793 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000794}
795
Ted Kremenek67f28532009-06-17 22:02:04 +0000796const GRState *RegionStoreManager::setExtent(const GRState *state,
797 const MemRegion *region,
798 SVal extent) {
799 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000800}
801
802//===----------------------------------------------------------------------===//
803// Location and region casting.
804//===----------------------------------------------------------------------===//
805
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000806/// ArrayToPointer - Emulates the "decay" of an array to a pointer
807/// type. 'Array' represents the lvalue of the array being decayed
808/// to a pointer, and the returned SVal represents the decayed
809/// version of that lvalue (i.e., a pointer to the first element of
810/// the array). This is called by GRExprEngine when evaluating casts
811/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000812SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000813 if (!isa<loc::MemRegionVal>(Array))
814 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenekabb042f2008-12-13 19:24:37 +0000816 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
817 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000819 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000820 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000822 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000823 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000824 ArrayType *AT = cast<ArrayType>(T);
825 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek75185b52009-07-16 00:00:11 +0000827 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000828 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
829 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000830}
831
Ted Kremenek9af46f52009-06-16 22:36:44 +0000832//===----------------------------------------------------------------------===//
833// Pointer arithmetic.
834//===----------------------------------------------------------------------===//
835
Zhongxing Xu461147f2010-02-05 05:24:20 +0000836SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000837 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000838 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000839 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000840 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000841
Zhongxing Xua1718c72009-04-03 07:33:13 +0000842 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000843 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000844
Ted Kremenek3bccf082009-07-11 00:58:27 +0000845 switch (MR->getKind()) {
846 case MemRegion::SymbolicRegionKind: {
847 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000848 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000849 QualType T = Sym->getType(getContext());
850 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000852 if (const PointerType *PT = T->getAs<PointerType>())
853 EleTy = PT->getPointeeType();
854 else
John McCall183700f2009-09-21 23:43:11 +0000855 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenek3bccf082009-07-11 00:58:27 +0000857 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
858 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000859 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000860 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000861 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000862 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000863 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000864 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000865 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
866 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000867 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000868 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000869
Ted Kremenek3bccf082009-07-11 00:58:27 +0000870 case MemRegion::ElementRegionKind: {
871 ER = cast<ElementRegion>(MR);
872 break;
873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek3bccf082009-07-11 00:58:27 +0000875 // Not yet handled.
876 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000877 case MemRegion::StringRegionKind: {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000878
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000879 }
880 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000881 case MemRegion::CompoundLiteralRegionKind:
882 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000883 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000884 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000885 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000887 case MemRegion::FunctionTextRegionKind:
888 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000889 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000890 // Technically this can happen if people do funny things with casts.
891 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Ted Kremenekde0d2632010-01-05 02:18:06 +0000893 case MemRegion::CXXThisRegionKind:
894 assert(0 &&
895 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000896 case MemRegion::GenericMemSpaceRegionKind:
897 case MemRegion::StackLocalsSpaceRegionKind:
898 case MemRegion::StackArgumentsSpaceRegionKind:
899 case MemRegion::HeapSpaceRegionKind:
900 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000901 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000902 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
903 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000904 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000905
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000906 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000907 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000908
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000909 // For now, only support:
910 // (a) concrete integer indices that can easily be resolved
911 // (b) 0 + symbolic index
912 if (Base) {
913 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
914 // FIXME: Should use SValuator here.
915 SVal NewIdx =
916 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000917 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000918 const MemRegion* NewER =
919 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
920 ER->getSuperRegion(), getContext());
921 return ValMgr.makeLoc(NewER);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000922 }
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000923 if (0 == Base->getValue()) {
924 const MemRegion* NewER =
925 MRMgr.getElementRegion(ER->getElementType(), R,
926 ER->getSuperRegion(), getContext());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000927 return ValMgr.makeLoc(NewER);
928 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000929 }
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Ted Kremenek5dc27462009-03-03 02:51:43 +0000931 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000932}
933
Ted Kremenek9af46f52009-06-16 22:36:44 +0000934//===----------------------------------------------------------------------===//
935// Loading values from regions.
936//===----------------------------------------------------------------------===//
937
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000938Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000939 const MemRegion *R) {
940 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000941 return *V;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000942
Zhongxing Xu13d50172009-10-11 08:08:02 +0000943 return Optional<SVal>();
944}
945
946Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000947 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000948 if (R->isBoundable())
949 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
950 if (TR->getValueType(getContext())->isUnionType())
951 return UnknownVal();
952
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000953 if (const SVal *V = Lookup(B, R, BindingKey::Default))
954 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +0000955
956 return Optional<SVal>();
957}
958
959Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
960 const MemRegion *R) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000961
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000962 if (Optional<SVal> V = getDirectBinding(B, R))
963 return V;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000964
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000965 return getDefaultBinding(B, R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000966}
967
Ted Kremeneka6275a52009-07-15 02:31:43 +0000968static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
969 RTy = Ctx.getCanonicalType(RTy);
970 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Ted Kremeneka6275a52009-07-15 02:31:43 +0000972 if (RTy == UsedTy)
973 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
975
Ted Kremenek25c54572009-07-20 22:58:02 +0000976 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000977 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000978 // represents a reinterpretation.
979 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000980 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000981 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000982
983 return PUsedTy && PRTy &&
984 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000985 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000986 }
987
988 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000989}
990
Ted Kremenek0954cde2009-09-24 04:11:44 +0000991const ElementRegion *
Zhongxing Xu81491852010-02-08 08:43:02 +0000992RegionStoreManager::GetElementZeroRegion(const MemRegion *R, QualType T) {
Ted Kremenek0954cde2009-09-24 04:11:44 +0000993 ASTContext &Ctx = getContext();
994 SVal idx = ValMgr.makeZeroArrayIndex();
995 assert(!T.isNull());
Zhongxing Xu81491852010-02-08 08:43:02 +0000996 return MRMgr.getElementRegion(T, idx, R, Ctx);
Ted Kremenek0954cde2009-09-24 04:11:44 +0000997}
Ted Kremenek0954cde2009-09-24 04:11:44 +0000998
Zhongxing Xu576bb922010-02-05 03:01:53 +0000999SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001000 assert(!isa<UnknownVal>(L) && "location unknown");
1001 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001002
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001003 // FIXME: Is this even possible? Shouldn't this be treated as a null
1004 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001005 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001006 return UndefinedVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001007
Ted Kremenek67f28532009-06-17 22:02:04 +00001008 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001009
Zhongxing Xu81491852010-02-08 08:43:02 +00001010 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR))
1011 MR = GetElementZeroRegion(MR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Zhongxing Xu2db08ca2010-03-01 05:29:02 +00001013 if (isa<CodeTextRegion>(MR)) {
1014 assert(0 && "Why load from a code text region?");
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001015 return UnknownVal();
Zhongxing Xu2db08ca2010-03-01 05:29:02 +00001016 }
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001018 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1019 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001020 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001021 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001022
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001023 // FIXME: We should eventually handle funny addressing. e.g.:
1024 //
1025 // int x = ...;
1026 // int *p = &x;
1027 // char *q = (char*) p;
1028 // char c = *q; // returns the first byte of 'x'.
1029 //
1030 // Such funny addressing will occur due to layering of regions.
1031
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001032#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001033 ASTContext &Ctx = getContext();
1034 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001035 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1036 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001037 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001038 assert(Ctx.getCanonicalType(RTy) ==
1039 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001040 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001041#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001042
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001043 if (RTy->isStructureType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001044 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001046 // FIXME: Handle unions.
1047 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001048 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001049
1050 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001051 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001052
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001053 // FIXME: handle Vector types.
1054 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001055 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001056
1057 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +00001058 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001059
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001060 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1061 // FIXME: Here we actually perform an implicit conversion from the loaded
1062 // value to the element type. Eventually we want to compose these values
1063 // more intelligently. For example, an 'element' can encompass multiple
1064 // bound regions (e.g., several bound bytes), or could be a subset of
1065 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001066 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001067 }
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001069 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1070 // FIXME: Here we actually perform an implicit conversion from the loaded
1071 // value to the ivar type. What we should model is stores to ivars
1072 // that blow past the extent of the ivar. If the address of the ivar is
1073 // reinterpretted, it is possible we stored a different value that could
1074 // fit within the ivar. Either we need to cast these when storing them
1075 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001076 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001079 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1080 // FIXME: Here we actually perform an implicit conversion from the loaded
1081 // value to the variable type. What we should model is stores to variables
1082 // that blow past the extent of the variable. If the address of the
1083 // variable is reinterpretted, it is possible we stored a different value
1084 // that could fit within the variable. Either we need to cast these when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001085 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001086 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001087 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001088
Zhongxing Xu576bb922010-02-05 03:01:53 +00001089 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001090 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001091
1092 // Check if the region has a binding.
1093 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001094 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001095
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001096 // The location does not have a bound value. This means that it has
1097 // the value it had upon its creation and/or entry to the analyzed
1098 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001099 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001100 // All stack variables are considered to have undefined values
1101 // upon creation. All heap allocated blocks are considered to
1102 // have undefined values as well unless they are explicitly bound
1103 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001104 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001105 }
1106
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001107 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001108 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001109}
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001111std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001112RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001113 if (Optional<SVal> OV = getDirectBinding(B, R))
1114 if (const nonloc::LazyCompoundVal *V =
1115 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001116 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001118 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001119 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001120 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001122 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001123 return std::make_pair(X.first,
1124 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001125 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001126 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001127 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001128 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001130 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001131 return std::make_pair(X.first,
1132 MRMgr.getFieldRegionWithSuper(FR, X.second));
1133 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001134 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xudcbcbdc2010-02-10 02:02:10 +00001135 // possible for a valid lazy binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001136 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001137}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001138
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001139SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001140 const ElementRegion* R) {
1141 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001142 RegionBindings B = GetRegionBindings(store);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001143 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001144 return *V;
1145
Ted Kremenek921109a2009-07-01 23:19:52 +00001146 const MemRegion* superR = R->getSuperRegion();
1147
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001148 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001149 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001150 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001151 // an integer, e.g., *((unsigned int*)"hello")
1152 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001153 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001154 if (T != Ctx.getCanonicalType(R->getElementType()))
1155 return UnknownVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001156
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001157 const StringLiteral *Str = StrR->getStringLiteral();
1158 SVal Idx = R->getIndex();
1159 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1160 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001161 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001162 if (i > byteLength) {
1163 // Buffer overflow checking in GRExprEngine should handle this case,
1164 // but we shouldn't rely on it to not overflow here if that checking
1165 // is disabled.
1166 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001167 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001168 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001169 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001170 }
1171 }
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001173 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001174 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001175 if (SymbolRef parentSym = V->getAsSymbol())
1176 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001177
1178 if (V->isUnknownOrUndef())
1179 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001180
1181 // Handle LazyCompoundVals for the immediate super region. Other cases
1182 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001183 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001184 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001186 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001187 return RetrieveElement(LCV->getStore(), R);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001188 }
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Ted Kremeneka6275a52009-07-15 02:31:43 +00001190 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001191 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001192 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001193
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001194 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001195}
1196
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001197SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001198 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001199
1200 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001201 RegionBindings B = GetRegionBindings(store);
Zhongxing Xu13d50172009-10-11 08:08:02 +00001202 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001203 return *V;
1204
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001205 QualType Ty = R->getValueType(getContext());
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001206 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001207}
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001209SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001210 const TypedRegion *R,
1211 QualType Ty,
1212 const MemRegion *superR) {
1213
Mike Stump1eb44332009-09-09 15:08:12 +00001214 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001215 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001217 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001219 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001220 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001221 if (SymbolRef parentSym = D->getAsSymbol())
1222 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001224 if (D->isZeroConstant())
1225 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001227 if (D->isUnknown())
1228 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001230 assert(0 && "Unknown default value");
1231 }
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001233 // If our super region is a field or element itself, walk up the region
1234 // hierarchy to see if there is a default value installed in an ancestor.
1235 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1236 superR = cast<SubRegion>(superR)->getSuperRegion();
1237 continue;
1238 }
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001240 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001243 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001244 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001245 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001246 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001248 if (lazyBindingRegion) {
1249 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1250 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001251 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001252 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001253 }
1254
Ted Kremenekde0d2632010-01-05 02:18:06 +00001255 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001256 if (isa<ElementRegion>(R)) {
1257 // Currently we don't reason specially about Clang-style vectors. Check
1258 // if superR is a vector and if so return Unknown.
1259 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1260 if (typedSuperR->getValueType(getContext())->isVectorType())
1261 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001262 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001263 }
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001265 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001266 }
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001268 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001269 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001270}
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Zhongxing Xu576bb922010-02-05 03:01:53 +00001272SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001273
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001274 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001275 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001276
Zhongxing Xu13d50172009-10-11 08:08:02 +00001277 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001278 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001280 const MemRegion *superR = R->getSuperRegion();
1281
Ted Kremenekab22ee92009-10-20 01:20:57 +00001282 // Check if the super region has a default binding.
1283 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001284 if (SymbolRef parentSym = V->getAsSymbol())
1285 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001287 // Other cases: give up.
1288 return UnknownVal();
1289 }
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Zhongxing Xu576bb922010-02-05 03:01:53 +00001291 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001292}
1293
Zhongxing Xu576bb922010-02-05 03:01:53 +00001294SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Ted Kremenek9031dd72009-07-21 00:12:07 +00001296 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001297 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Zhongxing Xu13d50172009-10-11 08:08:02 +00001299 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001300 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Ted Kremenek9031dd72009-07-21 00:12:07 +00001302 // Lazily derive a value for the VarRegion.
1303 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001304 QualType T = VD->getType();
1305 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001306
1307 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek4dc15662010-02-06 03:57:59 +00001308 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu14d23282010-03-01 06:56:52 +00001309 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Ted Kremenek4dc15662010-02-06 03:57:59 +00001311 if (isa<GlobalsSpaceRegion>(MS)) {
1312 if (VD->isFileVarDecl())
Zhongxing Xu14d23282010-03-01 06:56:52 +00001313 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Ted Kremenek4dc15662010-02-06 03:57:59 +00001315 if (T->isIntegerType())
1316 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001317 if (T->isPointerType())
1318 return ValMgr.makeNull();
1319
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001320 return UnknownVal();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001321 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001322
Ted Kremenek9031dd72009-07-21 00:12:07 +00001323 return UndefinedVal();
1324}
1325
Zhongxing Xu576bb922010-02-05 03:01:53 +00001326SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Ted Kremenek25c54572009-07-20 22:58:02 +00001328 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001329
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001330 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001331 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001332}
1333
Zhongxing Xu576bb922010-02-05 03:01:53 +00001334SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001335 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001336 assert(T->isStructureType());
Ted Kremenek957a2b72010-03-10 19:09:37 +00001337 assert(T->getAsStructureType()->getDecl()->isDefinition());
Zhongxing Xu576bb922010-02-05 03:01:53 +00001338 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001339}
1340
Zhongxing Xu576bb922010-02-05 03:01:53 +00001341SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001342 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001343 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001344}
1345
Ted Kremenek9af46f52009-06-16 22:36:44 +00001346//===----------------------------------------------------------------------===//
1347// Binding values to regions.
1348//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001349
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001350Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001351 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001352 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001353 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Ted Kremenek0964a062009-01-21 06:57:53 +00001355 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001356}
1357
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001358Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001359 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001360 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001361
Ted Kremenek9af46f52009-06-16 22:36:44 +00001362 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001363 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Ted Kremenek9af46f52009-06-16 22:36:44 +00001365 // Check if the region is a struct region.
1366 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1367 if (TR->getValueType(getContext())->isStructureType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001368 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001370 // Special case: the current region represents a cast and it and the super
1371 // region both have pointer types or intptr_t types. If so, perform the
1372 // bind to the super region.
1373 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001374 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001375 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1376 if (ER->getIndex().isZeroConstant()) {
1377 if (const TypedRegion *superR =
1378 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1379 ASTContext &Ctx = getContext();
1380 QualType superTy = superR->getValueType(Ctx);
1381 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001382
1383 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001384 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +00001385 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001386 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001387 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001388 // For now, just invalidate the fields of the struct/union/class.
1389 // FIXME: Precisely handle the fields of the record.
1390 if (superTy->isRecordType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001391 return InvalidateRegion(store, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001392 }
1393 }
1394 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001395 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1396 // Binding directly to a symbolic region should be treated as binding
1397 // to element 0.
1398 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001399
Ted Kremenek852274d2009-12-16 03:18:58 +00001400 // FIXME: Is this the right way to handle symbols that are references?
1401 if (const PointerType *PT = T->getAs<PointerType>())
1402 T = PT->getPointeeType();
1403 else
1404 T = T->getAs<ReferenceType>()->getPointeeType();
1405
Ted Kremenek0954cde2009-09-24 04:11:44 +00001406 R = GetElementZeroRegion(SR, T);
1407 }
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001409 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001410 RegionBindings B = GetRegionBindings(store);
1411 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001412}
1413
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001414Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001415 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001416
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001417 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001418
Ted Kremenek0964a062009-01-21 06:57:53 +00001419 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001420 return BindArray(store, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001421 if (T->isStructureType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001422 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001423
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001424 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001425}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001426
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001427// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001428Store RegionStoreManager::BindCompoundLiteral(Store store,
1429 const CompoundLiteralExpr *CL,
1430 const LocationContext *LC,
1431 SVal V) {
1432 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001433 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001434}
1435
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001436Store RegionStoreManager::setImplicitDefaultValue(Store store,
1437 const MemRegion *R,
1438 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001439 RegionBindings B = GetRegionBindings(store);
1440 SVal V;
1441
1442 if (Loc::IsLocType(T))
1443 V = ValMgr.makeNull();
1444 else if (T->isIntegerType())
1445 V = ValMgr.makeZeroVal(T);
1446 else if (T->isStructureType() || T->isArrayType()) {
1447 // Set the default value to a zero constant when it is a structure
1448 // or array. The type doesn't really matter.
1449 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1450 }
1451 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001452 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001453 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001454
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001455 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001456}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001457
1458Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001459 SVal Init) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001460
Ted Kremenekfee90812010-01-26 23:51:00 +00001461 ASTContext &Ctx = getContext();
1462 const ArrayType *AT =
1463 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001464 QualType ElementTy = AT->getElementType();
Ted Kremenekfee90812010-01-26 23:51:00 +00001465 Optional<uint64_t> Size;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001466
Ted Kremenekfee90812010-01-26 23:51:00 +00001467 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1468 Size = CAT->getSize().getZExtValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001469
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001470 // Check if the init expr is a StringLiteral.
1471 if (isa<loc::MemRegionVal>(Init)) {
1472 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1473 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1474 const char* str = S->getStrData();
1475 unsigned len = S->getByteLength();
1476 unsigned j = 0;
1477
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001478 // Copy bytes from the string literal into the target array. Trailing bytes
1479 // in the array that are not covered by the string literal are initialized
1480 // to zero.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001481
Ted Kremenekfee90812010-01-26 23:51:00 +00001482 // We assume that string constants are bound to
1483 // constant arrays.
Ted Kremenek39c2ea12010-01-27 16:31:37 +00001484 uint64_t size = Size.getValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001485
Ted Kremenek46537392009-07-16 01:33:37 +00001486 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001487 if (j >= len)
1488 break;
1489
Ted Kremenek46537392009-07-16 01:33:37 +00001490 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001491 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1492 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001493
Zhongxing Xud91ee272009-06-23 09:02:15 +00001494 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001495 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001496 }
1497
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001498 return store;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001499 }
1500
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001501 // Handle lazy compound values.
1502 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001503 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001504
1505 // Remaining case: explicit compound values.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001506
Ted Kremenek027e2662009-11-19 20:20:24 +00001507 if (Init.isUnknown())
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001508 return setImplicitDefaultValue(store, R, ElementTy);
1509
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001510 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001511 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001512 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Ted Kremenekfee90812010-01-26 23:51:00 +00001514 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001515 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001516 if (VI == VE)
1517 break;
1518
Ted Kremenek46537392009-07-16 01:33:37 +00001519 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001520 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001521
Ted Kremenekfee90812010-01-26 23:51:00 +00001522 if (ElementTy->isStructureType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001523 store = BindStruct(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001524 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001525 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001526 }
1527
Ted Kremenek027e2662009-11-19 20:20:24 +00001528 // If the init list is shorter than the array length, set the
1529 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001530 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001531 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001532
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001533 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001534}
1535
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001536Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1537 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Ted Kremenek67f28532009-06-17 22:02:04 +00001539 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001540 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001542 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001543 assert(T->isStructureType());
1544
Ted Kremenek6217b802009-07-29 21:53:49 +00001545 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001546 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001547
1548 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001549 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001550
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001551 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001552 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001553 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Ted Kremenek67f28532009-06-17 22:02:04 +00001555 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1556 // Ignore them and kill the field values.
1557 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001558 return KillStruct(store, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001559
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001560 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001561 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001562
1563 RecordDecl::field_iterator FI, FE;
1564
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001565 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001566
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001567 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001568 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001569
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001570 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001571 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001572
Ted Kremenekcf549592009-09-22 21:19:14 +00001573 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001574 store = BindArray(store, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001575 else if (FTy->isStructureType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001576 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001577 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001578 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001579 }
1580
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001581 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001582 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001583 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001584 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001585 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001586 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001587
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001588 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001589}
1590
Zhongxing Xu13d50172009-10-11 08:08:02 +00001591Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1592 RegionBindings B = GetRegionBindings(store);
1593 llvm::OwningPtr<RegionStoreSubRegionMap>
1594 SubRegions(getRegionStoreSubRegionMap(store));
1595 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001596
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001597 // Set the default value of the struct region to "unknown".
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001598 return Add(B, R, BindingKey::Default, UnknownVal()).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001599}
1600
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001601Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1602 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001603
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001604 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001605 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001606
Mike Stump1eb44332009-09-09 15:08:12 +00001607 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001608 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001609
Mike Stump1eb44332009-09-09 15:08:12 +00001610 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001611 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001613 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1614 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001615 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001616}
1617
1618//===----------------------------------------------------------------------===//
1619// "Raw" retrievals and bindings.
1620//===----------------------------------------------------------------------===//
1621
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001622BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001623 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1624 const RegionRawOffset &O = ER->getAsRawOffset();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001625
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001626 if (O.getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001627 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001628
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001629 // FIXME: There are some ElementRegions for which we cannot compute
1630 // raw offsets yet, including regions with symbolic offsets.
1631 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001632
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001633 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001634}
1635
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001636RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001637 return RBFactory.Add(B, K, V);
1638}
1639
1640RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001641 BindingKey::Kind k, SVal V) {
1642 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001643}
1644
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001645const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001646 return B.lookup(K);
1647}
1648
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001649const SVal *RegionStoreManager::Lookup(RegionBindings B,
1650 const MemRegion *R,
1651 BindingKey::Kind k) {
1652 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001653}
1654
1655RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1656 return RBFactory.Remove(B, K);
1657}
1658
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001659RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1660 BindingKey::Kind k){
1661 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001662}
1663
1664Store RegionStoreManager::Remove(Store store, BindingKey K) {
1665 RegionBindings B = GetRegionBindings(store);
1666 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001667}
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Ted Kremenek9af46f52009-06-16 22:36:44 +00001669//===----------------------------------------------------------------------===//
1670// State pruning.
1671//===----------------------------------------------------------------------===//
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001672
Ted Kremenek5499b842010-03-10 16:32:56 +00001673namespace {
1674class RemoveDeadBindingsWorker :
1675 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1676 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1677 SymbolReaper &SymReaper;
1678 Stmt *Loc;
1679public:
1680 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1681 RegionBindings b, SymbolReaper &symReaper,
1682 Stmt *loc)
1683 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
1684 SymReaper(symReaper), Loc(loc) {}
1685
1686 // Called by ClusterAnalysis.
1687 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1688 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
1689 void VisitRegion(const MemRegion *baseR);
1690
1691 bool UpdatePostponed();
1692 void VisitBinding(SVal V);
1693};
1694}
1695
1696void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1697 RegionCluster &C) {
1698
1699 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
1700 if (SymReaper.isLive(Loc, VR))
1701 AddToWorkList(baseR, C);
1702
1703 return;
1704 }
1705
1706 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1707 if (SymReaper.isLive(SR->getSymbol()))
1708 AddToWorkList(SR, C);
1709 else
1710 Postponed.push_back(SR);
1711
1712 return;
1713 }
1714}
1715
1716void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1717 BindingKey *I, BindingKey *E) {
1718 for ( ; I != E; ++I) {
1719 const MemRegion *R = I->getRegion();
1720 if (R != baseR)
1721 VisitRegion(R);
1722 }
1723}
1724
1725void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1726 // Is it a LazyCompoundVal? All referenced regions are live as well.
1727 if (const nonloc::LazyCompoundVal *LCS =
1728 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1729
1730 const MemRegion *LazyR = LCS->getRegion();
1731 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1732 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
1733 const MemRegion *baseR = RI.getKey().getRegion();
1734 if (cast<SubRegion>(baseR)->isSubRegionOf(LazyR))
1735 VisitBinding(RI.getData());
1736 }
1737 return;
1738 }
1739
1740 // If V is a region, then add it to the worklist.
1741 if (const MemRegion *R = V.getAsRegion())
1742 AddToWorkList(R);
1743
1744 // Update the set of live symbols.
1745 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1746 SI!=SE;++SI)
1747 SymReaper.markLive(*SI);
1748}
1749
1750void RemoveDeadBindingsWorker::VisitRegion(const MemRegion *R) {
1751 // Mark this region "live" by adding it to the worklist. This will cause
1752 // use to visit all regions in the cluster (if we haven't visited them
1753 // already).
1754 AddToWorkList(R);
1755
1756 // Mark the symbol for any live SymbolicRegion as "live". This means we
1757 // should continue to track that symbol.
1758 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1759 SymReaper.markLive(SymR->getSymbol());
1760
1761 // For BlockDataRegions, enqueue the VarRegions for variables marked
1762 // with __block (passed-by-reference).
1763 // via BlockDeclRefExprs.
1764 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1765 for (BlockDataRegion::referenced_vars_iterator
1766 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1767 RI != RE; ++RI) {
1768 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1769 AddToWorkList(*RI);
1770 }
1771
1772 // No possible data bindings on a BlockDataRegion.
1773 return;
1774 }
1775
1776 // Get the data binding for R (if any).
1777 if (Optional<SVal> V = RM.getBinding(B, R))
1778 VisitBinding(*V);
1779}
1780
1781bool RemoveDeadBindingsWorker::UpdatePostponed() {
1782 // See if any postponed SymbolicRegions are actually live now, after
1783 // having done a scan.
1784 bool changed = false;
1785
1786 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1787 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1788 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1789 if (SymReaper.isLive(SR->getSymbol())) {
1790 changed |= AddToWorkList(SR);
1791 *I = NULL;
1792 }
1793 }
1794 }
1795
1796 return changed;
1797}
1798
Zhongxing Xu72119c42010-02-05 05:34:29 +00001799Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
1800 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001801 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001802{
Ted Kremenek451ac092009-08-06 04:50:20 +00001803 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5499b842010-03-10 16:32:56 +00001804 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, Loc);
1805 W.GenerateClusters();
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Ted Kremenek5499b842010-03-10 16:32:56 +00001807 // Enqueue the region roots onto the worklist.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001808 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenek5499b842010-03-10 16:32:56 +00001809 E=RegionRoots.end(); I!=E; ++I)
1810 W.AddToWorkList(*I);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001811
Ted Kremenek5499b842010-03-10 16:32:56 +00001812 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001813
Ted Kremenek9af46f52009-06-16 22:36:44 +00001814 // We have now scanned the store, marking reachable regions and symbols
1815 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001816 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001817 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek5499b842010-03-10 16:32:56 +00001818 const BindingKey &K = I.getKey();
1819
Ted Kremenekb7118f72010-03-10 16:38:41 +00001820 // If the cluster has been visited, we know the region has been marked.
Ted Kremenek5499b842010-03-10 16:32:56 +00001821 if (W.isVisited(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001822 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Ted Kremenek5499b842010-03-10 16:32:56 +00001824 // Remove the dead entry.
1825 B = Remove(B, K);
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Ted Kremenek5499b842010-03-10 16:32:56 +00001827 // Mark all non-live symbols that this binding references as dead.
1828 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001829 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001831 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001832 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1833 for (; SI != SE; ++SI)
1834 SymReaper.maybeDead(*SI);
1835 }
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Ted Kremenek5499b842010-03-10 16:32:56 +00001837 return B.getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001838}
1839
Ted Kremenek5499b842010-03-10 16:32:56 +00001840
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001841GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1842 StackFrameContext const *frame) {
1843 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1844 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1845
1846 FunctionDecl::param_const_iterator PI = FD->param_begin();
1847
1848 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1849
1850 // Copy the arg expression value to the arg variables.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001851 Store store = state->getStore();
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001852 for (; AI != AE; ++AI, ++PI) {
Ted Kremenek13976632010-02-08 16:18:51 +00001853 SVal ArgVal = state->getSVal(*AI);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001854 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001855 }
1856
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001857 return state->makeWithStore(store);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001858}
1859
Ted Kremenek9af46f52009-06-16 22:36:44 +00001860//===----------------------------------------------------------------------===//
1861// Utility methods.
1862//===----------------------------------------------------------------------===//
1863
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001864void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001865 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001866 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001867 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Ted Kremenek451ac092009-08-06 04:50:20 +00001869 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001870 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001871}