blob: d9ad02a35c4d9683531ce3763c0e801981fbadc0 [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//===----------------------------------------------------------------------===//
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000017#include "clang/AST/CharUnits.h"
Zhongxing Xuc5063572010-03-16 13:14:16 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/ExprCXX.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21#include "clang/Analysis/AnalysisContext.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Checker/PathSensitive/GRState.h"
24#include "clang/Checker/PathSensitive/GRStateTrait.h"
25#include "clang/Checker/PathSensitive/MemRegion.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000027#include "llvm/ADT/ImmutableMap.h"
28#include "llvm/ADT/Optional.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000029#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000030
31using namespace clang;
Ted Kremenek66d51422010-04-09 20:26:58 +000032using llvm::Optional;
Zhongxing Xu17892752008-10-08 02:50:44 +000033
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000035// Representation of binding keys.
36//===----------------------------------------------------------------------===//
37
38namespace {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000039class BindingKey {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000040public:
Ted Kremeneke393f4a2010-02-03 03:06:46 +000041 enum Kind { Direct = 0x0, Default = 0x1 };
42private:
43 llvm ::PointerIntPair<const MemRegion*, 1> P;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000044 uint64_t Offset;
45
Ted Kremeneke393f4a2010-02-03 03:06:46 +000046 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
Zhongxing Xubfcaf802010-02-05 02:26:30 +000047 : P(r, (unsigned) k), Offset(offset) { assert(r); }
Ted Kremeneke393f4a2010-02-03 03:06:46 +000048public:
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000049
Ted Kremeneke393f4a2010-02-03 03:06:46 +000050 bool isDefault() const { return P.getInt() == Default; }
51 bool isDirect() const { return P.getInt() == Direct; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000052
Ted Kremeneke393f4a2010-02-03 03:06:46 +000053 const MemRegion *getRegion() const { return P.getPointer(); }
54 uint64_t getOffset() const { return Offset; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000055
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000056 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000057 ID.AddPointer(P.getOpaqueValue());
58 ID.AddInteger(Offset);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000059 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000060
Ted Kremeneke393f4a2010-02-03 03:06:46 +000061 static BindingKey Make(const MemRegion *R, Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000062
Ted Kremeneke393f4a2010-02-03 03:06:46 +000063 bool operator<(const BindingKey &X) const {
64 if (P.getOpaqueValue() < X.P.getOpaqueValue())
65 return true;
66 if (P.getOpaqueValue() > X.P.getOpaqueValue())
67 return false;
68 return Offset < X.Offset;
69 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000070
Ted Kremeneke393f4a2010-02-03 03:06:46 +000071 bool operator==(const BindingKey &X) const {
72 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
73 Offset == X.Offset;
74 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000075};
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000076} // end anonymous namespace
77
78namespace llvm {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000079 static inline
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000080 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000081 os << '(' << K.getRegion() << ',' << K.getOffset()
82 << ',' << (K.isDirect() ? "direct" : "default")
83 << ')';
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000084 return os;
85 }
86} // end llvm namespace
87
88//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +000089// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000090//===----------------------------------------------------------------------===//
91
Ted Kremeneke393f4a2010-02-03 03:06:46 +000092typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000093
Ted Kremenek50dc1b32008-12-24 01:05:03 +000094//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000095// Fine-grained control of RegionStoreManager.
96//===----------------------------------------------------------------------===//
97
98namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000099struct minimal_features_tag {};
100struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000102class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000103 bool SupportsFields;
104 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek9af46f52009-06-16 22:36:44 +0000106public:
107 RegionStoreFeatures(minimal_features_tag) :
108 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek9af46f52009-06-16 22:36:44 +0000110 RegionStoreFeatures(maximal_features_tag) :
111 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek9af46f52009-06-16 22:36:44 +0000113 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek9af46f52009-06-16 22:36:44 +0000115 bool supportsFields() const { return SupportsFields; }
116 bool supportsRemaining() const { return SupportsRemaining; }
117};
118}
119
120//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000121// Region "Extents"
122//===----------------------------------------------------------------------===//
123//
124// MemRegions represent chunks of memory with a size (their "extent"). This
125// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000126//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000127namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000128static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000129namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000130 template<> struct GRStateTrait<RegionExtents>
131 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
132 static void* GDMIndex() { return &RegionExtentsIndex; }
133 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000134}
135
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000136//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000137// Utility functions.
138//===----------------------------------------------------------------------===//
139
140static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
141 if (ty->isAnyPointerType())
142 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144 return ty->isIntegerType() && ty->isScalarType() &&
145 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000149// Main RegionStore logic.
150//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000151
Zhongxing Xu17892752008-10-08 02:50:44 +0000152namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000154class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekdf165012010-02-02 22:38:47 +0000155public:
156 typedef llvm::ImmutableSet<const MemRegion*> Set;
157 typedef llvm::DenseMap<const MemRegion*, Set> Map;
158private:
159 Set::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000160 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000161public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000162 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000163 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000164
165 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000166 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000167 return true;
168 }
169
170 I->second = F.Add(I->second, SubRegion);
171 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000174 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek59e8f112009-03-03 01:35:36 +0000176 ~RegionStoreSubRegionMap() {}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000177
Ted Kremenekdf165012010-02-02 22:38:47 +0000178 const Set *getSubRegions(const MemRegion *Parent) const {
179 Map::const_iterator I = M.find(Parent);
180 return I == M.end() ? NULL : &I->second;
181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek5dc27462009-03-03 02:51:43 +0000183 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000184 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000185
186 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000187 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenekdf165012010-02-02 22:38:47 +0000189 Set S = I->second;
190 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000191 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000192 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek5dc27462009-03-03 02:51:43 +0000195 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000198
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000199
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000200class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000201 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000202 RegionBindings::Factory RBFactory;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000203
Zhongxing Xu17892752008-10-08 02:50:44 +0000204public:
Mike Stump1eb44332009-09-09 15:08:12 +0000205 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000206 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000207 Features(f),
Ted Kremenek8928d412010-02-04 04:14:49 +0000208 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000209
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000210 SubRegionMap *getSubRegionMap(Store store) {
211 return getRegionStoreSubRegionMap(store);
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Zhongxing Xu13d50172009-10-11 08:08:02 +0000214 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Zhongxing Xu13d50172009-10-11 08:08:02 +0000216 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
217 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000218 /// getDefaultBinding - Returns an SVal* representing an optional default
219 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000220 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000221
Ted Kremenek027e2662009-11-19 20:20:24 +0000222 /// setImplicitDefaultValue - Set the default binding for the provided
223 /// MemRegion to the value implicitly defined for compound literals when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000224 /// the value is not specified.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000225 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000227 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
228 /// type. 'Array' represents the lvalue of the array being decayed
229 /// to a pointer, and the returned SVal represents the decayed
230 /// version of that lvalue (i.e., a pointer to the first element of
231 /// the array). This is called by GRExprEngine when evaluating
232 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000233 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000234
Zhongxing Xu461147f2010-02-05 05:24:20 +0000235 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000236
Mike Stump1eb44332009-09-09 15:08:12 +0000237 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000238 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000239 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000240
Ted Kremenek67f28532009-06-17 22:02:04 +0000241 //===-------------------------------------------------------------------===//
242 // Binding values to regions.
243 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000244
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000245 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000246 unsigned Count, InvalidatedSymbols *IS) {
247 return RegionStoreManager::InvalidateRegions(store, &R, &R+1, E, Count, IS);
Ted Kremenek81a95832009-12-03 03:27:11 +0000248 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000249
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000250 Store InvalidateRegions(Store store,
251 const MemRegion * const *Begin,
252 const MemRegion * const *End,
253 const Expr *E, unsigned Count,
254 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000256public: // Made public for helper classes.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000257
Zhongxing Xu13d50172009-10-11 08:08:02 +0000258 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000259 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000261 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
262
263 RegionBindings Add(RegionBindings B, const MemRegion *R,
264 BindingKey::Kind k, SVal V);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000265
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000266 const SVal *Lookup(RegionBindings B, BindingKey K);
267 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000268
269 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000270 RegionBindings Remove(RegionBindings B, const MemRegion *R,
271 BindingKey::Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000272
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000273 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
274 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000275 }
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000276
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000277 Store Remove(Store store, BindingKey K);
278
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000279public: // Part of public interface to class.
280
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000281 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000282
Zhongxing Xu54460092010-06-01 04:49:26 +0000283 // BindDefault is only used to initialize a region with a default value.
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000284 Store BindDefault(Store store, const MemRegion *R, SVal V) {
Zhongxing Xu54460092010-06-01 04:49:26 +0000285 RegionBindings B = GetRegionBindings(store);
286 assert(!Lookup(B, R, BindingKey::Default));
287 assert(!Lookup(B, R, BindingKey::Direct));
288 return Add(B, R, BindingKey::Default, V).getRoot();
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000289 }
290
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000291 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
292 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000294 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000295
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000296 Store BindDeclWithNoInit(Store store, const VarRegion *) {
297 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000298 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000299
Ted Kremenek67f28532009-06-17 22:02:04 +0000300 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000301 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000303 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
305 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000306 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000307
Ted Kremenek67f28532009-06-17 22:02:04 +0000308 Store Remove(Store store, Loc LV);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000309
Ted Kremenek67f28532009-06-17 22:02:04 +0000310
311 //===------------------------------------------------------------------===//
312 // Loading values from regions.
313 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremenek67f28532009-06-17 22:02:04 +0000315 /// The high level logic for this method is this:
316 /// Retrieve (L)
317 /// if L has binding
318 /// return L's binding
319 /// else if L is in killset
320 /// return unknown
321 /// else
322 /// if L is on stack or heap
323 /// return undefined
324 /// else
325 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000326 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000327
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000328 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000329
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000330 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Zhongxing Xu576bb922010-02-05 03:01:53 +0000332 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Zhongxing Xu576bb922010-02-05 03:01:53 +0000334 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Zhongxing Xu576bb922010-02-05 03:01:53 +0000336 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000338 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000339 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek67f28532009-06-17 22:02:04 +0000341 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000342 /// struct copy:
343 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000344 /// x = y;
345 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000346 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Zhongxing Xu576bb922010-02-05 03:01:53 +0000348 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000350 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000351 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000352 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000354 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
355 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000356
357 //===------------------------------------------------------------------===//
358 // State pruning.
359 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenek67f28532009-06-17 22:02:04 +0000361 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
362 /// It returns a new Store with these values removed.
Zhongxing Xu95798982010-05-26 03:27:35 +0000363 const GRState *RemoveDeadBindings(GRState &state, Stmt* Loc,
364 const StackFrameContext *LCtx,
365 SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000366 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
367
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000368 const GRState *EnterStackFrame(const GRState *state,
369 const StackFrameContext *frame);
370
Ted Kremenek67f28532009-06-17 22:02:04 +0000371 //===------------------------------------------------------------------===//
372 // Region "extents".
373 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Zhongxing Xuab280992010-05-25 04:59:19 +0000375 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent){
376 return state->set<RegionExtents>(R, Extent);
377 }
378
379 Optional<SVal> getExtent(const GRState *state, const MemRegion *R) {
380 const SVal *V = state->get<RegionExtents>(R);
381 if (V)
382 return *V;
383 else
384 return Optional<SVal>();
385 }
386
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000387 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000388 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000389
390 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000391 // Utility methods.
392 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek451ac092009-08-06 04:50:20 +0000394 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000395 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000396 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000397
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000398 void print(Store store, llvm::raw_ostream& Out, const char* nl,
399 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000400
401 void iterBindings(Store store, BindingsHandler& f) {
402 // FIXME: Implement.
403 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000404
Ted Kremenek67f28532009-06-17 22:02:04 +0000405 // FIXME: Remove.
406 BasicValueFactory& getBasicVals() {
407 return StateMgr.getBasicVals();
408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Ted Kremenek67f28532009-06-17 22:02:04 +0000410 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000411 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000412};
413
414} // end anonymous namespace
415
Ted Kremenek9af46f52009-06-16 22:36:44 +0000416//===----------------------------------------------------------------------===//
417// RegionStore creation.
418//===----------------------------------------------------------------------===//
419
420StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
421 RegionStoreFeatures F = maximal_features_tag();
422 return new RegionStoreManager(StMgr, F);
423}
424
425StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
426 RegionStoreFeatures F = minimal_features_tag();
427 F.enableFields(true);
428 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000429}
430
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000431void
432RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000433 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000434 const MemRegion *superR = R->getSuperRegion();
435 if (add(superR, R))
436 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000437 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000438}
439
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000440RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000441RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
442 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000443 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000445 llvm::SmallVector<const SubRegion*, 10> WL;
446
Ted Kremenek451ac092009-08-06 04:50:20 +0000447 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000448 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000449 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Mike Stump1eb44332009-09-09 15:08:12 +0000451 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000452 // don't have direct bindings but are super regions of those that do.
453 while (!WL.empty()) {
454 const SubRegion *R = WL.back();
455 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000456 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000457 }
458
Ted Kremenek14453bf2009-03-03 19:02:42 +0000459 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000460}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000461
Ted Kremenek9af46f52009-06-16 22:36:44 +0000462//===----------------------------------------------------------------------===//
Ted Kremeneka4fab032010-03-10 07:19:59 +0000463// Region Cluster analysis.
464//===----------------------------------------------------------------------===//
465
466namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000467template <typename DERIVED>
Ted Kremeneka4fab032010-03-10 07:19:59 +0000468class ClusterAnalysis {
469protected:
470 typedef BumpVector<BindingKey> RegionCluster;
471 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5499b842010-03-10 16:32:56 +0000472 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
473 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
474 WorkList;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000475
476 BumpVectorContext BVC;
477 ClusterMap ClusterM;
Ted Kremenek5499b842010-03-10 16:32:56 +0000478 WorkList WL;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000479
480 RegionStoreManager &RM;
481 ASTContext &Ctx;
482 ValueManager &ValMgr;
483
Ted Kremenek5499b842010-03-10 16:32:56 +0000484 RegionBindings B;
485
Ted Kremeneka4fab032010-03-10 07:19:59 +0000486public:
Ted Kremenek5499b842010-03-10 16:32:56 +0000487 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
488 RegionBindings b)
489 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
490 B(b) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000491
Ted Kremenek5499b842010-03-10 16:32:56 +0000492 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000493
Ted Kremenek5499b842010-03-10 16:32:56 +0000494 void AddToCluster(BindingKey K) {
495 const MemRegion *R = K.getRegion();
496 const MemRegion *baseR = R->getBaseRegion();
497 RegionCluster &C = getCluster(baseR);
498 C.push_back(K, BVC);
499 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000500 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000501
Ted Kremenek5499b842010-03-10 16:32:56 +0000502 bool isVisited(const MemRegion *R) {
503 return (bool) Visited[&getCluster(R->getBaseRegion())];
504 }
505
506 RegionCluster& getCluster(const MemRegion *R) {
507 RegionCluster *&CRef = ClusterM[R];
508 if (!CRef) {
509 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
510 CRef = new (Mem) RegionCluster(BVC, 10);
511 }
512 return *CRef;
513 }
514
515 void GenerateClusters() {
516 // Scan the entire set of bindings and make the region clusters.
517 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
518 AddToCluster(RI.getKey());
519 if (const MemRegion *R = RI.getData().getAsRegion()) {
520 // Generate a cluster, but don't add the region to the cluster
521 // if there aren't any bindings.
522 getCluster(R->getBaseRegion());
523 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000524 }
525 }
Ted Kremenek5499b842010-03-10 16:32:56 +0000526
527 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
528 if (unsigned &visited = Visited[&C])
529 return false;
530 else
531 visited = 1;
532
533 WL.push_back(std::make_pair(R, &C));
534 return true;
535 }
536
537 bool AddToWorkList(BindingKey K) {
538 return AddToWorkList(K.getRegion());
539 }
540
541 bool AddToWorkList(const MemRegion *R) {
542 const MemRegion *baseR = R->getBaseRegion();
543 return AddToWorkList(baseR, getCluster(baseR));
544 }
545
546 void RunWorkList() {
547 while (!WL.empty()) {
548 const MemRegion *baseR;
549 RegionCluster *C;
550 llvm::tie(baseR, C) = WL.back();
551 WL.pop_back();
552
553 // First visit the cluster.
554 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
555
Ted Kremenek75a2d942010-04-01 00:15:55 +0000556 // Next, visit the base region.
557 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
Ted Kremenek5499b842010-03-10 16:32:56 +0000558 }
559 }
560
561public:
562 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
563 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
Ted Kremenek75a2d942010-04-01 00:15:55 +0000564 void VisitBaseRegion(const MemRegion *baseR) {}
Ted Kremenek5499b842010-03-10 16:32:56 +0000565};
Ted Kremeneka4fab032010-03-10 07:19:59 +0000566}
567
568//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000569// Binding invalidation.
570//===----------------------------------------------------------------------===//
571
Zhongxing Xu13d50172009-10-11 08:08:02 +0000572void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
573 const MemRegion *R,
574 RegionStoreSubRegionMap &M) {
Ted Kremeneka4fab032010-03-10 07:19:59 +0000575
Ted Kremenekdf165012010-02-02 22:38:47 +0000576 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
577 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
578 I != E; ++I)
579 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000580
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000581 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000582}
583
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000584namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000585class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
586{
587 const Expr *Ex;
588 unsigned Count;
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000589 StoreManager::InvalidatedSymbols *IS;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000590public:
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000591 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenek5499b842010-03-10 16:32:56 +0000592 GRStateManager &stateMgr,
593 RegionBindings b,
594 const Expr *ex, unsigned count,
595 StoreManager::InvalidatedSymbols *is)
596 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
597 Ex(ex), Count(count), IS(is) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000598
Ted Kremenek5499b842010-03-10 16:32:56 +0000599 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek75a2d942010-04-01 00:15:55 +0000600 void VisitBaseRegion(const MemRegion *baseR);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000601
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000602private:
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000603 void VisitBinding(SVal V);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000604};
Ted Kremenek5b290652010-02-03 04:16:00 +0000605}
606
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000607void InvalidateRegionsWorker::VisitBinding(SVal V) {
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000608 // A symbol? Mark it touched by the invalidation.
609 if (IS)
610 if (SymbolRef Sym = V.getAsSymbol())
611 IS->insert(Sym);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000612
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000613 if (const MemRegion *R = V.getAsRegion()) {
614 AddToWorkList(R);
615 return;
616 }
617
618 // Is it a LazyCompoundVal? All references get invalidated as well.
619 if (const nonloc::LazyCompoundVal *LCS =
620 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
621
622 const MemRegion *LazyR = LCS->getRegion();
623 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
624
625 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
626 const MemRegion *baseR = RI.getKey().getRegion();
627 if (cast<SubRegion>(baseR)->isSubRegionOf(LazyR))
628 VisitBinding(RI.getData());
629 }
630
631 return;
632 }
633}
634
Ted Kremenek5499b842010-03-10 16:32:56 +0000635void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
636 BindingKey *I, BindingKey *E) {
637 for ( ; I != E; ++I) {
638 // Get the old binding. Is it a region? If so, add it to the worklist.
639 const BindingKey &K = *I;
640 if (const SVal *V = RM.Lookup(B, K))
641 VisitBinding(*V);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000642
Ted Kremenek5499b842010-03-10 16:32:56 +0000643 B = RM.Remove(B, K);
644 }
645}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000646
Ted Kremenek75a2d942010-04-01 00:15:55 +0000647void InvalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000648 if (IS) {
649 // Symbolic region? Mark that symbol touched by the invalidation.
650 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
651 IS->insert(SR->getSymbol());
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000652 }
653
Ted Kremenek5499b842010-03-10 16:32:56 +0000654 // BlockDataRegion? If so, invalidate captured variables that are passed
655 // by reference.
656 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
657 for (BlockDataRegion::referenced_vars_iterator
658 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
659 BI != BE; ++BI) {
660 const VarRegion *VR = *BI;
661 const VarDecl *VD = VR->getDecl();
662 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
663 AddToWorkList(VR);
664 }
665 return;
666 }
667
668 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
669 // Invalidate the region by setting its default value to
670 // conjured symbol. The type of the symbol is irrelavant.
671 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
672 Count);
673 B = RM.Add(B, baseR, BindingKey::Default, V);
674 return;
675 }
676
677 if (!baseR->isBoundable())
678 return;
679
680 const TypedRegion *TR = cast<TypedRegion>(baseR);
681 QualType T = TR->getValueType(Ctx);
682
683 // Invalidate the binding.
684 if (const RecordType *RT = T->getAsStructureType()) {
685 const RecordDecl *RD = RT->getDecl()->getDefinition();
686 // No record definition. There is nothing we can do.
687 if (!RD) {
688 B = RM.Remove(B, baseR);
689 return;
690 }
691
692 // Invalidate the region by setting its default value to
693 // conjured symbol. The type of the symbol is irrelavant.
694 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
695 Count);
696 B = RM.Add(B, baseR, BindingKey::Default, V);
697 return;
698 }
699
700 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
701 // Set the default value of the array to conjured symbol.
702 DefinedOrUnknownSVal V =
703 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
704 B = RM.Add(B, baseR, BindingKey::Default, V);
705 return;
706 }
707
708 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
709 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
710 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000711}
712
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000713Store RegionStoreManager::InvalidateRegions(Store store,
714 const MemRegion * const *I,
715 const MemRegion * const *E,
716 const Expr *Ex, unsigned Count,
717 InvalidatedSymbols *IS) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000718 InvalidateRegionsWorker W(*this, StateMgr,
719 RegionStoreManager::GetRegionBindings(store),
720 Ex, Count, IS);
721
722 // Scan the bindings and generate the clusters.
723 W.GenerateClusters();
724
725 // Add I .. E to the worklist.
726 for ( ; I != E; ++I)
727 W.AddToWorkList(*I);
728
729 W.RunWorkList();
730
731 // Return the new bindings.
732 return W.getRegionBindings().getRoot();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000733}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000734
Ted Kremenek9af46f52009-06-16 22:36:44 +0000735//===----------------------------------------------------------------------===//
736// Extents for regions.
737//===----------------------------------------------------------------------===//
738
Zhongxing Xue884ff82009-11-12 02:48:32 +0000739DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000740 const MemRegion *R,
741 QualType EleTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000743 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000744 case MemRegion::CXXThisRegionKind:
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000745 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000746 case MemRegion::GenericMemSpaceRegionKind:
747 case MemRegion::StackLocalsSpaceRegionKind:
748 case MemRegion::StackArgumentsSpaceRegionKind:
749 case MemRegion::HeapSpaceRegionKind:
750 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000751 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000752 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000753 return UnknownVal();
754
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000755 case MemRegion::FunctionTextRegionKind:
756 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000757 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000758 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000759 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000760
761 // Not yet handled.
762 case MemRegion::AllocaRegionKind:
763 case MemRegion::CompoundLiteralRegionKind:
764 case MemRegion::ElementRegionKind:
765 case MemRegion::FieldRegionKind:
766 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000767 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000768 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000770 case MemRegion::SymbolicRegionKind: {
771 const SVal *Size = state->get<RegionExtents>(R);
772 if (!Size)
773 return UnknownVal();
774 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
775 if (!CI)
776 return UnknownVal();
777
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000778 CharUnits RegionSize =
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000779 CharUnits::fromQuantity(CI->getValue().getSExtValue());
780 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
781 assert(RegionSize % EleSize == 0);
782
783 return ValMgr.makeIntVal(RegionSize / EleSize, false);
784 }
785
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000786 case MemRegion::StringRegionKind: {
787 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000788 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000789 // operations with signed indices.
790 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000793 case MemRegion::VarRegionKind: {
794 const VarRegion* VR = cast<VarRegion>(R);
795 // Get the type of the variable.
796 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000798 // FIXME: Handle variable-length arrays.
799 if (isa<VariableArrayType>(T))
800 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000802 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
803 // return the size as signed integer.
804 return ValMgr.makeIntVal(CAT->getSize(), false);
805 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000806
Zhongxing Xu9618b852010-04-01 08:20:27 +0000807 // Clients can reinterpret ordinary variables as arrays, possibly of
808 // another type. The width is rounded down to ensure that an access is
809 // entirely within bounds.
810 CharUnits VarSize = getContext().getTypeSizeInChars(T);
811 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
812 return ValMgr.makeIntVal(VarSize / EleSize, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000813 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000814 }
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000816 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000817 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000818}
819
Ted Kremenek9af46f52009-06-16 22:36:44 +0000820//===----------------------------------------------------------------------===//
821// Location and region casting.
822//===----------------------------------------------------------------------===//
823
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000824/// ArrayToPointer - Emulates the "decay" of an array to a pointer
825/// type. 'Array' represents the lvalue of the array being decayed
826/// to a pointer, and the returned SVal represents the decayed
827/// version of that lvalue (i.e., a pointer to the first element of
828/// the array). This is called by GRExprEngine when evaluating casts
829/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000830SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000831 if (!isa<loc::MemRegionVal>(Array))
832 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Ted Kremenekabb042f2008-12-13 19:24:37 +0000834 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
835 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000837 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000838 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000840 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000841 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000842 ArrayType *AT = cast<ArrayType>(T);
843 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek75185b52009-07-16 00:00:11 +0000845 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000846 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
847 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000848}
849
Ted Kremenek9af46f52009-06-16 22:36:44 +0000850//===----------------------------------------------------------------------===//
851// Pointer arithmetic.
852//===----------------------------------------------------------------------===//
853
Zhongxing Xu461147f2010-02-05 05:24:20 +0000854SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000855 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000856 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000857 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000858 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000859
Zhongxing Xua1718c72009-04-03 07:33:13 +0000860 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000861 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000862
Ted Kremenek3bccf082009-07-11 00:58:27 +0000863 switch (MR->getKind()) {
864 case MemRegion::SymbolicRegionKind: {
865 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000866 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000867 QualType T = Sym->getType(getContext());
868 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000870 if (const PointerType *PT = T->getAs<PointerType>())
871 EleTy = PT->getPointeeType();
872 else
John McCall183700f2009-09-21 23:43:11 +0000873 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek3bccf082009-07-11 00:58:27 +0000875 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
876 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000877 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000878 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000879 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000880 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000881 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000882 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000883 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
884 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000885 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000886 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000887
Ted Kremenek3bccf082009-07-11 00:58:27 +0000888 case MemRegion::ElementRegionKind: {
889 ER = cast<ElementRegion>(MR);
890 break;
891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Ted Kremenek3bccf082009-07-11 00:58:27 +0000893 // Not yet handled.
894 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000895 case MemRegion::StringRegionKind: {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000896
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000897 }
898 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000899 case MemRegion::CompoundLiteralRegionKind:
900 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000901 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000902 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000903 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000905 case MemRegion::FunctionTextRegionKind:
906 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000907 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000908 // Technically this can happen if people do funny things with casts.
909 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenekde0d2632010-01-05 02:18:06 +0000911 case MemRegion::CXXThisRegionKind:
912 assert(0 &&
913 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000914 case MemRegion::GenericMemSpaceRegionKind:
915 case MemRegion::StackLocalsSpaceRegionKind:
916 case MemRegion::StackArgumentsSpaceRegionKind:
917 case MemRegion::HeapSpaceRegionKind:
918 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000919 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000920 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
921 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000922 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000923
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000924 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000925 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000926
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000927 // For now, only support:
928 // (a) concrete integer indices that can easily be resolved
929 // (b) 0 + symbolic index
930 if (Base) {
931 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
932 // FIXME: Should use SValuator here.
933 SVal NewIdx =
934 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000935 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000936 const MemRegion* NewER =
937 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
938 ER->getSuperRegion(), getContext());
939 return ValMgr.makeLoc(NewER);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000940 }
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000941 if (0 == Base->getValue()) {
942 const MemRegion* NewER =
943 MRMgr.getElementRegion(ER->getElementType(), R,
944 ER->getSuperRegion(), getContext());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000945 return ValMgr.makeLoc(NewER);
946 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000947 }
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Ted Kremenek5dc27462009-03-03 02:51:43 +0000949 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000950}
951
Ted Kremenek9af46f52009-06-16 22:36:44 +0000952//===----------------------------------------------------------------------===//
953// Loading values from regions.
954//===----------------------------------------------------------------------===//
955
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000956Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Zhongxing Xubdfa85f2010-05-29 06:23:24 +0000957 const MemRegion *R) {
Zhongxing Xu42c67bf2010-05-29 06:49:04 +0000958
959 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
960 return *V;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000961
Zhongxing Xu13d50172009-10-11 08:08:02 +0000962 return Optional<SVal>();
963}
964
965Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000966 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000967 if (R->isBoundable())
968 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
969 if (TR->getValueType(getContext())->isUnionType())
970 return UnknownVal();
971
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000972 if (const SVal *V = Lookup(B, R, BindingKey::Default))
973 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +0000974
975 return Optional<SVal>();
976}
977
978Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
979 const MemRegion *R) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000980
Ted Kremenek2cf073b2010-03-30 20:30:52 +0000981 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000982 return V;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000983
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000984 return getDefaultBinding(B, R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000985}
986
Ted Kremeneka6275a52009-07-15 02:31:43 +0000987static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
988 RTy = Ctx.getCanonicalType(RTy);
989 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Ted Kremeneka6275a52009-07-15 02:31:43 +0000991 if (RTy == UsedTy)
992 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000993
994
Ted Kremenek25c54572009-07-20 22:58:02 +0000995 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000996 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000997 // represents a reinterpretation.
998 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000999 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +00001000 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +00001001
1002 return PUsedTy && PRTy &&
1003 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +00001004 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +00001005 }
1006
1007 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001008}
1009
Zhongxing Xu576bb922010-02-05 03:01:53 +00001010SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001011 assert(!isa<UnknownVal>(L) && "location unknown");
1012 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001013
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001014 // FIXME: Is this even possible? Shouldn't this be treated as a null
1015 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001016 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001017 return UndefinedVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001018
Ted Kremenek67f28532009-06-17 22:02:04 +00001019 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001020
Zhongxing Xu81491852010-02-08 08:43:02 +00001021 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR))
1022 MR = GetElementZeroRegion(MR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Zhongxing Xu2db08ca2010-03-01 05:29:02 +00001024 if (isa<CodeTextRegion>(MR)) {
1025 assert(0 && "Why load from a code text region?");
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001026 return UnknownVal();
Zhongxing Xu2db08ca2010-03-01 05:29:02 +00001027 }
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001029 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1030 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001031 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001032 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001033
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001034 // FIXME: We should eventually handle funny addressing. e.g.:
1035 //
1036 // int x = ...;
1037 // int *p = &x;
1038 // char *q = (char*) p;
1039 // char c = *q; // returns the first byte of 'x'.
1040 //
1041 // Such funny addressing will occur due to layering of regions.
1042
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001043#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001044 ASTContext &Ctx = getContext();
1045 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001046 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1047 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001048 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001049 assert(Ctx.getCanonicalType(RTy) ==
1050 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001051 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001052#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001053
Douglas Gregorfb87b892010-04-26 21:31:17 +00001054 if (RTy->isStructureOrClassType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001055 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001057 // FIXME: Handle unions.
1058 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001059 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001060
1061 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001062 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001063
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001064 // FIXME: handle Vector types.
1065 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001066 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001067
1068 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +00001069 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001070
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001071 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1072 // FIXME: Here we actually perform an implicit conversion from the loaded
1073 // value to the element type. Eventually we want to compose these values
1074 // more intelligently. For example, an 'element' can encompass multiple
1075 // bound regions (e.g., several bound bytes), or could be a subset of
1076 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001077 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001078 }
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001080 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1081 // FIXME: Here we actually perform an implicit conversion from the loaded
1082 // value to the ivar type. What we should model is stores to ivars
1083 // that blow past the extent of the ivar. If the address of the ivar is
1084 // reinterpretted, it is possible we stored a different value that could
1085 // fit within the ivar. Either we need to cast these when storing them
1086 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001087 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001088 }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001090 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1091 // FIXME: Here we actually perform an implicit conversion from the loaded
1092 // value to the variable type. What we should model is stores to variables
1093 // that blow past the extent of the variable. If the address of the
1094 // variable is reinterpretted, it is possible we stored a different value
1095 // that could fit within the variable. Either we need to cast these when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001096 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001097 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001098 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001099
Zhongxing Xu576bb922010-02-05 03:01:53 +00001100 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001101 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001102
1103 // Check if the region has a binding.
1104 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001105 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001106
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001107 // The location does not have a bound value. This means that it has
1108 // the value it had upon its creation and/or entry to the analyzed
1109 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001110 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001111 // All stack variables are considered to have undefined values
1112 // upon creation. All heap allocated blocks are considered to
1113 // have undefined values as well unless they are explicitly bound
1114 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001115 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001116 }
1117
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001118 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001119 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001120}
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001122std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001123RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001124 if (Optional<SVal> OV = getDirectBinding(B, R))
1125 if (const nonloc::LazyCompoundVal *V =
1126 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001127 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001129 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001130 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001131 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001133 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001134 return std::make_pair(X.first,
1135 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001136 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001137 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001138 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001139 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001141 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001142 return std::make_pair(X.first,
1143 MRMgr.getFieldRegionWithSuper(FR, X.second));
1144 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001145 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xudcbcbdc2010-02-10 02:02:10 +00001146 // possible for a valid lazy binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001147 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001148}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001149
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001150SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001151 const ElementRegion* R) {
1152 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001153 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001154 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001155 return *V;
1156
Ted Kremenek921109a2009-07-01 23:19:52 +00001157 const MemRegion* superR = R->getSuperRegion();
1158
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001159 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001160 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001161 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001162 // an integer, e.g., *((unsigned int*)"hello")
1163 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001164 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001165 if (T != Ctx.getCanonicalType(R->getElementType()))
1166 return UnknownVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001167
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001168 const StringLiteral *Str = StrR->getStringLiteral();
1169 SVal Idx = R->getIndex();
1170 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1171 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001172 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001173 if (i > byteLength) {
1174 // Buffer overflow checking in GRExprEngine should handle this case,
1175 // but we shouldn't rely on it to not overflow here if that checking
1176 // is disabled.
1177 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001178 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001179 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001180 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001181 }
1182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremeneka709b872010-05-31 01:22:04 +00001184 // Handle the case where we are indexing into a larger scalar object.
1185 // For example, this handles:
1186 // int x = ...
1187 // char *y = &x;
1188 // return *y;
1189 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
1190 const RegionRawOffset &O = R->getAsRawOffset();
1191 if (const TypedRegion *baseR = dyn_cast_or_null<TypedRegion>(O.getRegion())) {
1192 QualType baseT = baseR->getValueType(Ctx);
1193 if (baseT->isScalarType()) {
1194 QualType elemT = R->getElementType();
1195 if (elemT->isScalarType()) {
1196 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1197 if (const Optional<SVal> &V = getDirectBinding(B, superR)) {
1198 if (SymbolRef parentSym = V->getAsSymbol())
1199 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1200
1201 if (V->isUnknownOrUndef())
1202 return *V;
1203 // Other cases: give up. We are indexing into a larger object
1204 // that has some value, but we don't know how to handle that yet.
1205 return UnknownVal();
1206 }
1207 }
1208 }
Zhongxing Xu42c67bf2010-05-29 06:49:04 +00001209 }
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001210 }
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001211 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001212}
1213
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001214SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001215 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001216
1217 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001218 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001219 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001220 return *V;
1221
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001222 QualType Ty = R->getValueType(getContext());
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001223 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001224}
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001226SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001227 const TypedRegion *R,
1228 QualType Ty,
1229 const MemRegion *superR) {
1230
Mike Stump1eb44332009-09-09 15:08:12 +00001231 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001232 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001234 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001236 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001237 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001238 if (SymbolRef parentSym = D->getAsSymbol())
1239 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001241 if (D->isZeroConstant())
1242 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Zhongxing Xua5ce9662010-06-01 03:01:33 +00001244 if (D->isUnknownOrUndef())
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001245 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001247 assert(0 && "Unknown default value");
1248 }
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001250 // If our super region is a field or element itself, walk up the region
1251 // hierarchy to see if there is a default value installed in an ancestor.
1252 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1253 superR = cast<SubRegion>(superR)->getSuperRegion();
1254 continue;
1255 }
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001257 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001258 }
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001260 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001261 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001262 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001263 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001265 if (lazyBindingRegion) {
1266 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1267 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001268 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001269 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001270 }
1271
Ted Kremenekde0d2632010-01-05 02:18:06 +00001272 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001273 if (isa<ElementRegion>(R)) {
1274 // Currently we don't reason specially about Clang-style vectors. Check
1275 // if superR is a vector and if so return Unknown.
1276 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1277 if (typedSuperR->getValueType(getContext())->isVectorType())
1278 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001279 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001280 }
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001282 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001283 }
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001285 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001286 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001287}
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Zhongxing Xu576bb922010-02-05 03:01:53 +00001289SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001290
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001291 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001292 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001293
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001294 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001295 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001297 const MemRegion *superR = R->getSuperRegion();
1298
Ted Kremenekab22ee92009-10-20 01:20:57 +00001299 // Check if the super region has a default binding.
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001300 if (const Optional<SVal> &V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001301 if (SymbolRef parentSym = V->getAsSymbol())
1302 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001304 // Other cases: give up.
1305 return UnknownVal();
1306 }
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Zhongxing Xu576bb922010-02-05 03:01:53 +00001308 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001309}
1310
Zhongxing Xu576bb922010-02-05 03:01:53 +00001311SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Ted Kremenek9031dd72009-07-21 00:12:07 +00001313 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001314 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001316 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001317 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Ted Kremenek9031dd72009-07-21 00:12:07 +00001319 // Lazily derive a value for the VarRegion.
1320 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001321 QualType T = VD->getType();
1322 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001323
1324 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek4dc15662010-02-06 03:57:59 +00001325 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu14d23282010-03-01 06:56:52 +00001326 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Ted Kremenek4dc15662010-02-06 03:57:59 +00001328 if (isa<GlobalsSpaceRegion>(MS)) {
Ted Kremenek4552ff02010-03-30 20:31:04 +00001329 if (VD->isFileVarDecl()) {
1330 // Is 'VD' declared constant? If so, retrieve the constant value.
1331 QualType CT = Ctx.getCanonicalType(T);
1332 if (CT.isConstQualified()) {
1333 const Expr *Init = VD->getInit();
1334 // Do the null check first, as we want to call 'IgnoreParenCasts'.
1335 if (Init)
1336 if (const IntegerLiteral *IL =
1337 dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) {
1338 const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL);
1339 return ValMgr.getSValuator().EvalCast(V, Init->getType(),
1340 IL->getType());
1341 }
1342 }
1343
Zhongxing Xu14d23282010-03-01 06:56:52 +00001344 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek4552ff02010-03-30 20:31:04 +00001345 }
Mike Stump1eb44332009-09-09 15:08:12 +00001346
Ted Kremenek4dc15662010-02-06 03:57:59 +00001347 if (T->isIntegerType())
1348 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001349 if (T->isPointerType())
1350 return ValMgr.makeNull();
1351
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001352 return UnknownVal();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001353 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001354
Ted Kremenek9031dd72009-07-21 00:12:07 +00001355 return UndefinedVal();
1356}
1357
Zhongxing Xu576bb922010-02-05 03:01:53 +00001358SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Ted Kremenek25c54572009-07-20 22:58:02 +00001360 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001361
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001362 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001363 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001364}
1365
Zhongxing Xu576bb922010-02-05 03:01:53 +00001366SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001367 QualType T = R->getValueType(getContext());
Douglas Gregorfb87b892010-04-26 21:31:17 +00001368 assert(T->isStructureOrClassType());
Zhongxing Xu576bb922010-02-05 03:01:53 +00001369 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001370}
1371
Zhongxing Xu576bb922010-02-05 03:01:53 +00001372SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001373 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001374 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001375}
1376
Ted Kremenek9af46f52009-06-16 22:36:44 +00001377//===----------------------------------------------------------------------===//
1378// Binding values to regions.
1379//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001380
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001381Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001382 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001383 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001384 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Ted Kremenek0964a062009-01-21 06:57:53 +00001386 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001387}
1388
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001389Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001390 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001391 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001392
Ted Kremenek9af46f52009-06-16 22:36:44 +00001393 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001394 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Ted Kremenek9af46f52009-06-16 22:36:44 +00001396 // Check if the region is a struct region.
1397 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Douglas Gregorfb87b892010-04-26 21:31:17 +00001398 if (TR->getValueType(getContext())->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001399 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001401 // Special case: the current region represents a cast and it and the super
1402 // region both have pointer types or intptr_t types. If so, perform the
1403 // bind to the super region.
1404 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001405 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001406 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1407 if (ER->getIndex().isZeroConstant()) {
1408 if (const TypedRegion *superR =
1409 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1410 ASTContext &Ctx = getContext();
1411 QualType superTy = superR->getValueType(Ctx);
1412 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001413
1414 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001415 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +00001416 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001417 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001418 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001419 // For now, just invalidate the fields of the struct/union/class.
1420 // FIXME: Precisely handle the fields of the record.
1421 if (superTy->isRecordType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001422 return InvalidateRegion(store, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001423 }
1424 }
1425 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001426 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1427 // Binding directly to a symbolic region should be treated as binding
1428 // to element 0.
1429 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001430
Ted Kremenek852274d2009-12-16 03:18:58 +00001431 // FIXME: Is this the right way to handle symbols that are references?
1432 if (const PointerType *PT = T->getAs<PointerType>())
1433 T = PT->getPointeeType();
1434 else
1435 T = T->getAs<ReferenceType>()->getPointeeType();
1436
Ted Kremenek0954cde2009-09-24 04:11:44 +00001437 R = GetElementZeroRegion(SR, T);
1438 }
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001440 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001441 RegionBindings B = GetRegionBindings(store);
1442 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001443}
1444
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001445Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001446 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001447
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001448 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001449
Ted Kremenek0964a062009-01-21 06:57:53 +00001450 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001451 return BindArray(store, VR, InitVal);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001452 if (T->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001453 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001454
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001455 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001456}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001457
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001458// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001459Store RegionStoreManager::BindCompoundLiteral(Store store,
1460 const CompoundLiteralExpr *CL,
1461 const LocationContext *LC,
1462 SVal V) {
1463 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001464 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001465}
1466
Zhongxing Xua5ce9662010-06-01 03:01:33 +00001467
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001468Store RegionStoreManager::setImplicitDefaultValue(Store store,
1469 const MemRegion *R,
1470 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001471 RegionBindings B = GetRegionBindings(store);
1472 SVal V;
1473
1474 if (Loc::IsLocType(T))
1475 V = ValMgr.makeNull();
1476 else if (T->isIntegerType())
1477 V = ValMgr.makeZeroVal(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001478 else if (T->isStructureOrClassType() || T->isArrayType()) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001479 // Set the default value to a zero constant when it is a structure
1480 // or array. The type doesn't really matter.
1481 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1482 }
1483 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001484 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001485 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001486
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001487 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001488}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001489
1490Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001491 SVal Init) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001492
Ted Kremenekfee90812010-01-26 23:51:00 +00001493 ASTContext &Ctx = getContext();
1494 const ArrayType *AT =
1495 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001496 QualType ElementTy = AT->getElementType();
Ted Kremenekfee90812010-01-26 23:51:00 +00001497 Optional<uint64_t> Size;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001498
Ted Kremenekfee90812010-01-26 23:51:00 +00001499 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1500 Size = CAT->getSize().getZExtValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001501
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001502 // Check if the init expr is a StringLiteral.
1503 if (isa<loc::MemRegionVal>(Init)) {
1504 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1505 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1506 const char* str = S->getStrData();
1507 unsigned len = S->getByteLength();
1508 unsigned j = 0;
1509
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001510 // Copy bytes from the string literal into the target array. Trailing bytes
1511 // in the array that are not covered by the string literal are initialized
1512 // to zero.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001513
Ted Kremenekfee90812010-01-26 23:51:00 +00001514 // We assume that string constants are bound to
1515 // constant arrays.
Ted Kremenek39c2ea12010-01-27 16:31:37 +00001516 uint64_t size = Size.getValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001517
Ted Kremenek46537392009-07-16 01:33:37 +00001518 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001519 if (j >= len)
1520 break;
1521
Ted Kremenek46537392009-07-16 01:33:37 +00001522 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001523 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1524 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001525
Zhongxing Xud91ee272009-06-23 09:02:15 +00001526 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001527 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001528 }
1529
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001530 return store;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001531 }
1532
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001533 // Handle lazy compound values.
1534 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001535 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001536
1537 // Remaining case: explicit compound values.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001538
Ted Kremenek027e2662009-11-19 20:20:24 +00001539 if (Init.isUnknown())
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001540 return setImplicitDefaultValue(store, R, ElementTy);
1541
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001542 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001543 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001544 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Ted Kremenekfee90812010-01-26 23:51:00 +00001546 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001547 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001548 if (VI == VE)
1549 break;
1550
Ted Kremenek46537392009-07-16 01:33:37 +00001551 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001552 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001553
Douglas Gregorfb87b892010-04-26 21:31:17 +00001554 if (ElementTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001555 store = BindStruct(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001556 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001557 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001558 }
1559
Ted Kremenek027e2662009-11-19 20:20:24 +00001560 // If the init list is shorter than the array length, set the
1561 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001562 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001563 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001564
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001565 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001566}
1567
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001568Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1569 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Ted Kremenek67f28532009-06-17 22:02:04 +00001571 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001572 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001574 QualType T = R->getValueType(getContext());
Douglas Gregorfb87b892010-04-26 21:31:17 +00001575 assert(T->isStructureOrClassType());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001576
Ted Kremenek6217b802009-07-29 21:53:49 +00001577 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001578 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001579
1580 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001581 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001582
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001583 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001584 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001585 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Ted Kremenek67f28532009-06-17 22:02:04 +00001587 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1588 // Ignore them and kill the field values.
1589 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001590 return KillStruct(store, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001591
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001592 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001593 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001594
1595 RecordDecl::field_iterator FI, FE;
1596
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001597 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001598
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001599 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001600 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001601
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001602 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001603 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001604
Ted Kremenekcf549592009-09-22 21:19:14 +00001605 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001606 store = BindArray(store, FR, *VI);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001607 else if (FTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001608 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001609 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001610 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001611 }
1612
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001613 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001614 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001615 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001616 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001617 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001618 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001619
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001620 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001621}
1622
Zhongxing Xu13d50172009-10-11 08:08:02 +00001623Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1624 RegionBindings B = GetRegionBindings(store);
1625 llvm::OwningPtr<RegionStoreSubRegionMap>
1626 SubRegions(getRegionStoreSubRegionMap(store));
1627 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001628
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001629 // Set the default value of the struct region to "unknown".
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001630 return Add(B, R, BindingKey::Default, UnknownVal()).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001631}
1632
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001633Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1634 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001635
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001636 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001637 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001638
Mike Stump1eb44332009-09-09 15:08:12 +00001639 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001640 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001641
Mike Stump1eb44332009-09-09 15:08:12 +00001642 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001643 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001645 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1646 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001647 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001648}
1649
1650//===----------------------------------------------------------------------===//
1651// "Raw" retrievals and bindings.
1652//===----------------------------------------------------------------------===//
1653
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001654BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001655 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1656 const RegionRawOffset &O = ER->getAsRawOffset();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001657
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001658 if (O.getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001659 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001660
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001661 // FIXME: There are some ElementRegions for which we cannot compute
1662 // raw offsets yet, including regions with symbolic offsets.
1663 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001664
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001665 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001666}
1667
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001668RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001669 return RBFactory.Add(B, K, V);
1670}
1671
1672RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001673 BindingKey::Kind k, SVal V) {
1674 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001675}
1676
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001677const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001678 return B.lookup(K);
1679}
1680
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001681const SVal *RegionStoreManager::Lookup(RegionBindings B,
1682 const MemRegion *R,
1683 BindingKey::Kind k) {
1684 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001685}
1686
1687RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1688 return RBFactory.Remove(B, K);
1689}
1690
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001691RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1692 BindingKey::Kind k){
1693 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001694}
1695
1696Store RegionStoreManager::Remove(Store store, BindingKey K) {
1697 RegionBindings B = GetRegionBindings(store);
1698 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001699}
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Ted Kremenek9af46f52009-06-16 22:36:44 +00001701//===----------------------------------------------------------------------===//
1702// State pruning.
1703//===----------------------------------------------------------------------===//
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001704
Ted Kremenek5499b842010-03-10 16:32:56 +00001705namespace {
1706class RemoveDeadBindingsWorker :
1707 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1708 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1709 SymbolReaper &SymReaper;
1710 Stmt *Loc;
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001711 const StackFrameContext *CurrentLCtx;
1712
Ted Kremenek5499b842010-03-10 16:32:56 +00001713public:
1714 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1715 RegionBindings b, SymbolReaper &symReaper,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001716 Stmt *loc, const StackFrameContext *LCtx)
Ted Kremenek5499b842010-03-10 16:32:56 +00001717 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001718 SymReaper(symReaper), Loc(loc), CurrentLCtx(LCtx) {}
Ted Kremenek5499b842010-03-10 16:32:56 +00001719
1720 // Called by ClusterAnalysis.
1721 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1722 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek5499b842010-03-10 16:32:56 +00001723
Ted Kremenek75a2d942010-04-01 00:15:55 +00001724 void VisitBindingKey(BindingKey K);
Ted Kremenek5499b842010-03-10 16:32:56 +00001725 bool UpdatePostponed();
1726 void VisitBinding(SVal V);
1727};
1728}
1729
1730void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1731 RegionCluster &C) {
1732
1733 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
1734 if (SymReaper.isLive(Loc, VR))
1735 AddToWorkList(baseR, C);
1736
1737 return;
1738 }
1739
1740 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1741 if (SymReaper.isLive(SR->getSymbol()))
1742 AddToWorkList(SR, C);
1743 else
1744 Postponed.push_back(SR);
1745
1746 return;
1747 }
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001748
1749 // CXXThisRegion in the current or parent location context is live.
1750 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
1751 const StackArgumentsSpaceRegion *StackReg =
1752 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
1753 const StackFrameContext *RegCtx = StackReg->getStackFrame();
1754 if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
1755 AddToWorkList(TR, C);
1756 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001757}
1758
1759void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1760 BindingKey *I, BindingKey *E) {
Ted Kremenek75a2d942010-04-01 00:15:55 +00001761 for ( ; I != E; ++I)
1762 VisitBindingKey(*I);
Ted Kremenek5499b842010-03-10 16:32:56 +00001763}
1764
1765void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1766 // Is it a LazyCompoundVal? All referenced regions are live as well.
1767 if (const nonloc::LazyCompoundVal *LCS =
1768 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1769
1770 const MemRegion *LazyR = LCS->getRegion();
1771 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1772 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
1773 const MemRegion *baseR = RI.getKey().getRegion();
1774 if (cast<SubRegion>(baseR)->isSubRegionOf(LazyR))
1775 VisitBinding(RI.getData());
1776 }
1777 return;
1778 }
1779
1780 // If V is a region, then add it to the worklist.
1781 if (const MemRegion *R = V.getAsRegion())
1782 AddToWorkList(R);
1783
1784 // Update the set of live symbols.
1785 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1786 SI!=SE;++SI)
1787 SymReaper.markLive(*SI);
1788}
1789
Ted Kremenek75a2d942010-04-01 00:15:55 +00001790void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) {
1791 const MemRegion *R = K.getRegion();
1792
Ted Kremenek5499b842010-03-10 16:32:56 +00001793 // Mark this region "live" by adding it to the worklist. This will cause
1794 // use to visit all regions in the cluster (if we haven't visited them
1795 // already).
Ted Kremenek75a2d942010-04-01 00:15:55 +00001796 if (AddToWorkList(R)) {
1797 // Mark the symbol for any live SymbolicRegion as "live". This means we
1798 // should continue to track that symbol.
1799 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1800 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek5499b842010-03-10 16:32:56 +00001801
Ted Kremenek75a2d942010-04-01 00:15:55 +00001802 // For BlockDataRegions, enqueue the VarRegions for variables marked
1803 // with __block (passed-by-reference).
1804 // via BlockDeclRefExprs.
1805 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1806 for (BlockDataRegion::referenced_vars_iterator
1807 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1808 RI != RE; ++RI) {
1809 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1810 AddToWorkList(*RI);
1811 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001812
Ted Kremenek75a2d942010-04-01 00:15:55 +00001813 // No possible data bindings on a BlockDataRegion.
1814 return;
Ted Kremenek5499b842010-03-10 16:32:56 +00001815 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001816 }
1817
Ted Kremenek75a2d942010-04-01 00:15:55 +00001818 // Visit the data binding for K.
1819 if (const SVal *V = RM.Lookup(B, K))
Ted Kremenek5499b842010-03-10 16:32:56 +00001820 VisitBinding(*V);
1821}
1822
1823bool RemoveDeadBindingsWorker::UpdatePostponed() {
1824 // See if any postponed SymbolicRegions are actually live now, after
1825 // having done a scan.
1826 bool changed = false;
1827
1828 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1829 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1830 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1831 if (SymReaper.isLive(SR->getSymbol())) {
1832 changed |= AddToWorkList(SR);
1833 *I = NULL;
1834 }
1835 }
1836 }
1837
1838 return changed;
1839}
1840
Zhongxing Xu95798982010-05-26 03:27:35 +00001841const GRState *RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001842 const StackFrameContext *LCtx,
Zhongxing Xu72119c42010-02-05 05:34:29 +00001843 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001844 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001845{
Zhongxing Xu95798982010-05-26 03:27:35 +00001846 RegionBindings B = GetRegionBindings(state.getStore());
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001847 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, Loc, LCtx);
Ted Kremenek5499b842010-03-10 16:32:56 +00001848 W.GenerateClusters();
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Ted Kremenek5499b842010-03-10 16:32:56 +00001850 // Enqueue the region roots onto the worklist.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001851 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenek5499b842010-03-10 16:32:56 +00001852 E=RegionRoots.end(); I!=E; ++I)
1853 W.AddToWorkList(*I);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001854
Ted Kremenek5499b842010-03-10 16:32:56 +00001855 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001856
Ted Kremenek9af46f52009-06-16 22:36:44 +00001857 // We have now scanned the store, marking reachable regions and symbols
1858 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001859 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001860 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek5499b842010-03-10 16:32:56 +00001861 const BindingKey &K = I.getKey();
1862
Ted Kremenekb7118f72010-03-10 16:38:41 +00001863 // If the cluster has been visited, we know the region has been marked.
Ted Kremenek5499b842010-03-10 16:32:56 +00001864 if (W.isVisited(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001865 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Ted Kremenek5499b842010-03-10 16:32:56 +00001867 // Remove the dead entry.
1868 B = Remove(B, K);
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Ted Kremenek5499b842010-03-10 16:32:56 +00001870 // Mark all non-live symbols that this binding references as dead.
1871 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001872 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001874 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001875 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1876 for (; SI != SE; ++SI)
1877 SymReaper.maybeDead(*SI);
1878 }
Zhongxing Xu95798982010-05-26 03:27:35 +00001879 state.setStore(B.getRoot());
1880 const GRState *s = StateMgr.getPersistentState(state);
1881 // Remove the extents of dead symbolic regions.
Zhongxing Xued4214c2010-05-26 03:36:08 +00001882 llvm::ImmutableMap<const MemRegion*,SVal> Extents = s->get<RegionExtents>();
Zhongxing Xu95798982010-05-26 03:27:35 +00001883 for (llvm::ImmutableMap<const MemRegion *, SVal>::iterator I=Extents.begin(),
1884 E = Extents.end(); I != E; ++I) {
1885 if (!W.isVisited(I->first))
1886 s = s->remove<RegionExtents>(I->first);
1887 }
1888 return s;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001889}
1890
Ted Kremenek5499b842010-03-10 16:32:56 +00001891
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001892GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1893 StackFrameContext const *frame) {
1894 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001895 FunctionDecl::param_const_iterator PI = FD->param_begin();
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001896 Store store = state->getStore();
Zhongxing Xuc5063572010-03-16 13:14:16 +00001897
1898 if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) {
1899 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1900
1901 // Copy the arg expression value to the arg variables.
1902 for (; AI != AE; ++AI, ++PI) {
1903 SVal ArgVal = state->getSVal(*AI);
1904 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1905 }
1906 } else if (const CXXConstructExpr *CE =
1907 dyn_cast<CXXConstructExpr>(frame->getCallSite())) {
1908 CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(),
1909 AE = CE->arg_end();
1910
1911 // Copy the arg expression value to the arg variables.
1912 for (; AI != AE; ++AI, ++PI) {
1913 SVal ArgVal = state->getSVal(*AI);
1914 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1915 }
1916 } else
1917 assert(0 && "Unhandled call expression.");
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001918
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001919 return state->makeWithStore(store);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001920}
1921
Ted Kremenek9af46f52009-06-16 22:36:44 +00001922//===----------------------------------------------------------------------===//
1923// Utility methods.
1924//===----------------------------------------------------------------------===//
1925
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001926void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001927 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001928 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001929 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Ted Kremenek451ac092009-08-06 04:50:20 +00001931 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001932 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001933}