blob: c51a4c8c1d63c8762600016778381f0e92a764ab [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)
Jordy Rosee7011172010-08-16 01:15:17 +000047 : P(r, (unsigned) k), Offset(offset) {}
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 isDirect() const { return P.getInt() == Direct; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000051
Ted Kremeneke393f4a2010-02-03 03:06:46 +000052 const MemRegion *getRegion() const { return P.getPointer(); }
53 uint64_t getOffset() const { return Offset; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000054
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000055 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000056 ID.AddPointer(P.getOpaqueValue());
57 ID.AddInteger(Offset);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000058 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000059
Ted Kremeneke393f4a2010-02-03 03:06:46 +000060 static BindingKey Make(const MemRegion *R, Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000061
Ted Kremeneke393f4a2010-02-03 03:06:46 +000062 bool operator<(const BindingKey &X) const {
63 if (P.getOpaqueValue() < X.P.getOpaqueValue())
64 return true;
65 if (P.getOpaqueValue() > X.P.getOpaqueValue())
66 return false;
67 return Offset < X.Offset;
68 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000069
Ted Kremeneke393f4a2010-02-03 03:06:46 +000070 bool operator==(const BindingKey &X) const {
71 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
72 Offset == X.Offset;
73 }
Jordy Rosee7011172010-08-16 01:15:17 +000074
Jordy Rosed5addb42010-08-16 20:53:01 +000075 bool isValid() const {
Jordy Rosee7011172010-08-16 01:15:17 +000076 return getRegion() != NULL;
77 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000078};
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000079} // end anonymous namespace
80
81namespace llvm {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000082 static inline
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000083 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000084 os << '(' << K.getRegion() << ',' << K.getOffset()
85 << ',' << (K.isDirect() ? "direct" : "default")
86 << ')';
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000087 return os;
88 }
89} // end llvm namespace
90
91//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +000092// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000093//===----------------------------------------------------------------------===//
94
Ted Kremeneke393f4a2010-02-03 03:06:46 +000095typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000096
Ted Kremenek50dc1b32008-12-24 01:05:03 +000097//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000098// Fine-grained control of RegionStoreManager.
99//===----------------------------------------------------------------------===//
100
101namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000102struct minimal_features_tag {};
103struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000105class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000106 bool SupportsFields;
Ted Kremenek9af46f52009-06-16 22:36:44 +0000107public:
108 RegionStoreFeatures(minimal_features_tag) :
Ted Kremenek0752d6d2010-08-20 06:06:41 +0000109 SupportsFields(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenek9af46f52009-06-16 22:36:44 +0000111 RegionStoreFeatures(maximal_features_tag) :
Ted Kremenek0752d6d2010-08-20 06:06:41 +0000112 SupportsFields(true) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenek9af46f52009-06-16 22:36:44 +0000114 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenek9af46f52009-06-16 22:36:44 +0000116 bool supportsFields() const { return SupportsFields; }
Ted Kremenek9af46f52009-06-16 22:36:44 +0000117};
118}
119
120//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000121// Main RegionStore logic.
122//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000123
Zhongxing Xu17892752008-10-08 02:50:44 +0000124namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000126class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekdf165012010-02-02 22:38:47 +0000127public:
128 typedef llvm::ImmutableSet<const MemRegion*> Set;
129 typedef llvm::DenseMap<const MemRegion*, Set> Map;
130private:
131 Set::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000132 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000133public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000134 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000135 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000136
137 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000138 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000139 return true;
140 }
141
142 I->second = F.Add(I->second, SubRegion);
143 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000144 }
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000146 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148 ~RegionStoreSubRegionMap() {}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000149
Ted Kremenekdf165012010-02-02 22:38:47 +0000150 const Set *getSubRegions(const MemRegion *Parent) const {
151 Map::const_iterator I = M.find(Parent);
152 return I == M.end() ? NULL : &I->second;
153 }
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ted Kremenek5dc27462009-03-03 02:51:43 +0000155 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000156 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000157
158 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000159 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremenekdf165012010-02-02 22:38:47 +0000161 Set S = I->second;
162 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000163 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000164 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenek5dc27462009-03-03 02:51:43 +0000167 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000168 }
Mike Stump1eb44332009-09-09 15:08:12 +0000169};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000170
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000171
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000172class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000173 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000174 RegionBindings::Factory RBFactory;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000175
Zhongxing Xu17892752008-10-08 02:50:44 +0000176public:
Mike Stump1eb44332009-09-09 15:08:12 +0000177 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000178 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000179 Features(f),
Ted Kremenek8928d412010-02-04 04:14:49 +0000180 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000181
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000182 SubRegionMap *getSubRegionMap(Store store) {
183 return getRegionStoreSubRegionMap(store);
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Zhongxing Xu13d50172009-10-11 08:08:02 +0000186 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Zhongxing Xu13d50172009-10-11 08:08:02 +0000188 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000189 /// getDefaultBinding - Returns an SVal* representing an optional default
190 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000191 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000192
Ted Kremenek027e2662009-11-19 20:20:24 +0000193 /// setImplicitDefaultValue - Set the default binding for the provided
194 /// MemRegion to the value implicitly defined for compound literals when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000195 /// the value is not specified.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000196 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000198 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
199 /// type. 'Array' represents the lvalue of the array being decayed
200 /// to a pointer, and the returned SVal represents the decayed
201 /// version of that lvalue (i.e., a pointer to the first element of
202 /// the array). This is called by GRExprEngine when evaluating
203 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000204 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000205
Zhongxing Xu461147f2010-02-05 05:24:20 +0000206 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000207
Mike Stump1eb44332009-09-09 15:08:12 +0000208 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000209 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000210 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000211
Ted Kremenek67f28532009-06-17 22:02:04 +0000212 //===-------------------------------------------------------------------===//
213 // Binding values to regions.
214 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000215
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000216 Store InvalidateRegions(Store store,
217 const MemRegion * const *Begin,
218 const MemRegion * const *End,
219 const Expr *E, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000220 InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000221 bool invalidateGlobals,
222 InvalidatedRegions *Regions);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000224public: // Made public for helper classes.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000225
Zhongxing Xu13d50172009-10-11 08:08:02 +0000226 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000227 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000229 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
230
231 RegionBindings Add(RegionBindings B, const MemRegion *R,
232 BindingKey::Kind k, SVal V);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000233
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000234 const SVal *Lookup(RegionBindings B, BindingKey K);
235 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000236
237 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000238 RegionBindings Remove(RegionBindings B, const MemRegion *R,
239 BindingKey::Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000240
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000241 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
242 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000243 }
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000244
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000245public: // Part of public interface to class.
246
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000247 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000248
Zhongxing Xu54460092010-06-01 04:49:26 +0000249 // BindDefault is only used to initialize a region with a default value.
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000250 Store BindDefault(Store store, const MemRegion *R, SVal V) {
Zhongxing Xu54460092010-06-01 04:49:26 +0000251 RegionBindings B = GetRegionBindings(store);
252 assert(!Lookup(B, R, BindingKey::Default));
253 assert(!Lookup(B, R, BindingKey::Direct));
254 return Add(B, R, BindingKey::Default, V).getRoot();
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000255 }
256
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000257 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
258 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000260 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000261
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000262 Store BindDeclWithNoInit(Store store, const VarRegion *) {
263 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000264 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000265
Ted Kremenek67f28532009-06-17 22:02:04 +0000266 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000267 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000269 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
271 /// KillStruct - Set the entire struct to unknown.
Ted Kremenek281e9dc2010-07-29 00:28:47 +0000272 Store KillStruct(Store store, const TypedRegion* R, SVal DefaultVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000273
Ted Kremenek67f28532009-06-17 22:02:04 +0000274 Store Remove(Store store, Loc LV);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000275
Ted Kremenek67f28532009-06-17 22:02:04 +0000276
277 //===------------------------------------------------------------------===//
278 // Loading values from regions.
279 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Ted Kremenek67f28532009-06-17 22:02:04 +0000281 /// The high level logic for this method is this:
282 /// Retrieve (L)
283 /// if L has binding
284 /// return L's binding
285 /// else if L is in killset
286 /// return unknown
287 /// else
288 /// if L is on stack or heap
289 /// return undefined
290 /// else
291 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000292 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000293
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000294 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000295
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000296 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Zhongxing Xu576bb922010-02-05 03:01:53 +0000298 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Zhongxing Xu576bb922010-02-05 03:01:53 +0000300 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Zhongxing Xu576bb922010-02-05 03:01:53 +0000302 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000304 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000305 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Ted Kremenek67f28532009-06-17 22:02:04 +0000307 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000308 /// struct copy:
309 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000310 /// x = y;
311 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000312 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Zhongxing Xu576bb922010-02-05 03:01:53 +0000314 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000316 /// Used to lazily generate derived symbols for bindings that are defined
317 /// implicitly by default bindings in a super region.
318 Optional<SVal> RetrieveDerivedDefaultValue(RegionBindings B,
319 const MemRegion *superR,
320 const TypedRegion *R, QualType Ty);
321
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000322 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000323 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000324 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000326 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
327 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000328
329 //===------------------------------------------------------------------===//
330 // State pruning.
331 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek67f28532009-06-17 22:02:04 +0000333 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
334 /// It returns a new Store with these values removed.
Zhongxing Xu5e4cb342010-08-15 12:45:09 +0000335 Store RemoveDeadBindings(Store store, const StackFrameContext *LCtx,
336 SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000337 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
338
Jordy Roseff59efd2010-08-03 20:44:35 +0000339 Store EnterStackFrame(const GRState *state, const StackFrameContext *frame);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000340
Ted Kremenek67f28532009-06-17 22:02:04 +0000341 //===------------------------------------------------------------------===//
342 // Region "extents".
343 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Jordy Rose32f26562010-07-04 00:00:41 +0000345 // FIXME: This method will soon be eliminated; see the note in Store.h.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000346 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000347 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000348
349 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000350 // Utility methods.
351 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremenek451ac092009-08-06 04:50:20 +0000353 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000354 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000355 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000356
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000357 void print(Store store, llvm::raw_ostream& Out, const char* nl,
358 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000359
360 void iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek0e9910f2010-06-17 00:24:42 +0000361 RegionBindings B = GetRegionBindings(store);
362 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
363 const BindingKey &K = I.getKey();
364 if (!K.isDirect())
365 continue;
366 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) {
367 // FIXME: Possibly incorporate the offset?
368 if (!f.HandleBinding(*this, store, R, I.getData()))
369 return;
370 }
371 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000372 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000373};
374
375} // end anonymous namespace
376
Ted Kremenek9af46f52009-06-16 22:36:44 +0000377//===----------------------------------------------------------------------===//
378// RegionStore creation.
379//===----------------------------------------------------------------------===//
380
381StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
382 RegionStoreFeatures F = maximal_features_tag();
383 return new RegionStoreManager(StMgr, F);
384}
385
386StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
387 RegionStoreFeatures F = minimal_features_tag();
388 F.enableFields(true);
389 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000390}
391
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000392void
393RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000394 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000395 const MemRegion *superR = R->getSuperRegion();
396 if (add(superR, R))
397 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000398 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000399}
400
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000401RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000402RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
403 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000404 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000406 llvm::SmallVector<const SubRegion*, 10> WL;
407
Ted Kremenek451ac092009-08-06 04:50:20 +0000408 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000409 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000410 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Mike Stump1eb44332009-09-09 15:08:12 +0000412 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000413 // don't have direct bindings but are super regions of those that do.
414 while (!WL.empty()) {
415 const SubRegion *R = WL.back();
416 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000417 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000418 }
419
Ted Kremenek14453bf2009-03-03 19:02:42 +0000420 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000421}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000422
Ted Kremenek9af46f52009-06-16 22:36:44 +0000423//===----------------------------------------------------------------------===//
Ted Kremeneka4fab032010-03-10 07:19:59 +0000424// Region Cluster analysis.
425//===----------------------------------------------------------------------===//
426
427namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000428template <typename DERIVED>
Ted Kremeneka4fab032010-03-10 07:19:59 +0000429class ClusterAnalysis {
430protected:
431 typedef BumpVector<BindingKey> RegionCluster;
432 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5499b842010-03-10 16:32:56 +0000433 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
434 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
435 WorkList;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000436
437 BumpVectorContext BVC;
438 ClusterMap ClusterM;
Ted Kremenek5499b842010-03-10 16:32:56 +0000439 WorkList WL;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000440
441 RegionStoreManager &RM;
442 ASTContext &Ctx;
443 ValueManager &ValMgr;
444
Ted Kremenek5499b842010-03-10 16:32:56 +0000445 RegionBindings B;
446
Ted Kremeneka4fab032010-03-10 07:19:59 +0000447public:
Ted Kremenek5499b842010-03-10 16:32:56 +0000448 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
449 RegionBindings b)
450 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
451 B(b) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000452
Ted Kremenek5499b842010-03-10 16:32:56 +0000453 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000454
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000455 RegionCluster &AddToCluster(BindingKey K) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000456 const MemRegion *R = K.getRegion();
457 const MemRegion *baseR = R->getBaseRegion();
458 RegionCluster &C = getCluster(baseR);
459 C.push_back(K, BVC);
460 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000461 return C;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000462 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000463
Ted Kremenek5499b842010-03-10 16:32:56 +0000464 bool isVisited(const MemRegion *R) {
465 return (bool) Visited[&getCluster(R->getBaseRegion())];
466 }
467
468 RegionCluster& getCluster(const MemRegion *R) {
469 RegionCluster *&CRef = ClusterM[R];
470 if (!CRef) {
471 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
472 CRef = new (Mem) RegionCluster(BVC, 10);
473 }
474 return *CRef;
475 }
476
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000477 void GenerateClusters(bool includeGlobals = false) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000478 // Scan the entire set of bindings and make the region clusters.
479 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000480 RegionCluster &C = AddToCluster(RI.getKey());
Ted Kremenek5499b842010-03-10 16:32:56 +0000481 if (const MemRegion *R = RI.getData().getAsRegion()) {
482 // Generate a cluster, but don't add the region to the cluster
483 // if there aren't any bindings.
484 getCluster(R->getBaseRegion());
485 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000486 if (includeGlobals) {
487 const MemRegion *R = RI.getKey().getRegion();
488 if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()))
489 AddToWorkList(R, C);
490 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000491 }
492 }
Ted Kremenek5499b842010-03-10 16:32:56 +0000493
494 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
495 if (unsigned &visited = Visited[&C])
496 return false;
497 else
498 visited = 1;
499
500 WL.push_back(std::make_pair(R, &C));
501 return true;
502 }
503
504 bool AddToWorkList(BindingKey K) {
505 return AddToWorkList(K.getRegion());
506 }
507
508 bool AddToWorkList(const MemRegion *R) {
509 const MemRegion *baseR = R->getBaseRegion();
510 return AddToWorkList(baseR, getCluster(baseR));
511 }
512
513 void RunWorkList() {
514 while (!WL.empty()) {
515 const MemRegion *baseR;
516 RegionCluster *C;
517 llvm::tie(baseR, C) = WL.back();
518 WL.pop_back();
519
520 // First visit the cluster.
521 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
522
Ted Kremenek75a2d942010-04-01 00:15:55 +0000523 // Next, visit the base region.
524 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
Ted Kremenek5499b842010-03-10 16:32:56 +0000525 }
526 }
527
528public:
529 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
530 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
Ted Kremenek75a2d942010-04-01 00:15:55 +0000531 void VisitBaseRegion(const MemRegion *baseR) {}
Ted Kremenek5499b842010-03-10 16:32:56 +0000532};
Ted Kremeneka4fab032010-03-10 07:19:59 +0000533}
534
535//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000536// Binding invalidation.
537//===----------------------------------------------------------------------===//
538
Zhongxing Xu13d50172009-10-11 08:08:02 +0000539void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
540 const MemRegion *R,
541 RegionStoreSubRegionMap &M) {
Ted Kremeneka4fab032010-03-10 07:19:59 +0000542
Ted Kremenekdf165012010-02-02 22:38:47 +0000543 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
544 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
545 I != E; ++I)
546 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000547
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000548 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000549}
550
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000551namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000552class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
553{
554 const Expr *Ex;
555 unsigned Count;
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000556 StoreManager::InvalidatedSymbols *IS;
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000557 StoreManager::InvalidatedRegions *Regions;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000558public:
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000559 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenek5499b842010-03-10 16:32:56 +0000560 GRStateManager &stateMgr,
561 RegionBindings b,
562 const Expr *ex, unsigned count,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000563 StoreManager::InvalidatedSymbols *is,
564 StoreManager::InvalidatedRegions *r)
Ted Kremenek5499b842010-03-10 16:32:56 +0000565 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000566 Ex(ex), Count(count), IS(is), Regions(r) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000567
Ted Kremenek5499b842010-03-10 16:32:56 +0000568 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek75a2d942010-04-01 00:15:55 +0000569 void VisitBaseRegion(const MemRegion *baseR);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000570
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000571private:
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000572 void VisitBinding(SVal V);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000573};
Ted Kremenek5b290652010-02-03 04:16:00 +0000574}
575
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000576void InvalidateRegionsWorker::VisitBinding(SVal V) {
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000577 // A symbol? Mark it touched by the invalidation.
578 if (IS)
579 if (SymbolRef Sym = V.getAsSymbol())
580 IS->insert(Sym);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000581
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000582 if (const MemRegion *R = V.getAsRegion()) {
583 AddToWorkList(R);
584 return;
585 }
586
587 // Is it a LazyCompoundVal? All references get invalidated as well.
588 if (const nonloc::LazyCompoundVal *LCS =
589 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
590
591 const MemRegion *LazyR = LCS->getRegion();
592 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
593
594 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +0000595 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
596 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000597 VisitBinding(RI.getData());
598 }
599
600 return;
601 }
602}
603
Ted Kremenek5499b842010-03-10 16:32:56 +0000604void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
605 BindingKey *I, BindingKey *E) {
606 for ( ; I != E; ++I) {
607 // Get the old binding. Is it a region? If so, add it to the worklist.
608 const BindingKey &K = *I;
609 if (const SVal *V = RM.Lookup(B, K))
610 VisitBinding(*V);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000611
Ted Kremenek5499b842010-03-10 16:32:56 +0000612 B = RM.Remove(B, K);
613 }
614}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000615
Ted Kremenek75a2d942010-04-01 00:15:55 +0000616void InvalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000617 if (IS) {
618 // Symbolic region? Mark that symbol touched by the invalidation.
619 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
620 IS->insert(SR->getSymbol());
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000621 }
622
Ted Kremenek5499b842010-03-10 16:32:56 +0000623 // BlockDataRegion? If so, invalidate captured variables that are passed
624 // by reference.
625 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
626 for (BlockDataRegion::referenced_vars_iterator
627 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
628 BI != BE; ++BI) {
629 const VarRegion *VR = *BI;
630 const VarDecl *VD = VR->getDecl();
631 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
632 AddToWorkList(VR);
633 }
634 return;
635 }
636
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000637 // Otherwise, we have a normal data region. Record that we touched the region.
638 if (Regions)
639 Regions->push_back(baseR);
640
Ted Kremenek5499b842010-03-10 16:32:56 +0000641 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
642 // Invalidate the region by setting its default value to
643 // conjured symbol. The type of the symbol is irrelavant.
644 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
645 Count);
646 B = RM.Add(B, baseR, BindingKey::Default, V);
647 return;
648 }
649
650 if (!baseR->isBoundable())
651 return;
652
653 const TypedRegion *TR = cast<TypedRegion>(baseR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000654 QualType T = TR->getValueType();
Ted Kremenek5499b842010-03-10 16:32:56 +0000655
656 // Invalidate the binding.
Zhongxing Xu61453402010-08-21 06:51:45 +0000657 if (T->isStructureType()) {
Zhongxing Xu0b46b1b2010-08-21 06:26:59 +0000658 // Invalidate the region by setting its default value to
659 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek5499b842010-03-10 16:32:56 +0000660 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
661 Count);
662 B = RM.Add(B, baseR, BindingKey::Default, V);
663 return;
664 }
665
666 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
667 // Set the default value of the array to conjured symbol.
668 DefinedOrUnknownSVal V =
669 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
670 B = RM.Add(B, baseR, BindingKey::Default, V);
671 return;
672 }
673
674 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
675 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
676 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000677}
678
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000679Store RegionStoreManager::InvalidateRegions(Store store,
680 const MemRegion * const *I,
681 const MemRegion * const *E,
682 const Expr *Ex, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000683 InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000684 bool invalidateGlobals,
685 InvalidatedRegions *Regions) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000686 InvalidateRegionsWorker W(*this, StateMgr,
687 RegionStoreManager::GetRegionBindings(store),
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000688 Ex, Count, IS, Regions);
Ted Kremenek5499b842010-03-10 16:32:56 +0000689
690 // Scan the bindings and generate the clusters.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000691 W.GenerateClusters(invalidateGlobals);
Ted Kremenek5499b842010-03-10 16:32:56 +0000692
693 // Add I .. E to the worklist.
694 for ( ; I != E; ++I)
695 W.AddToWorkList(*I);
696
697 W.RunWorkList();
698
699 // Return the new bindings.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000700 RegionBindings B = W.getRegionBindings();
701
702 if (invalidateGlobals) {
703 // Bind the non-static globals memory space to a new symbol that we will
704 // use to derive the bindings for all non-static globals.
705 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion();
706 SVal V =
707 ValMgr.getConjuredSymbolVal(/* SymbolTag = */ (void*) GS, Ex,
708 /* symbol type, doesn't matter */ Ctx.IntTy,
709 Count);
710 B = Add(B, BindingKey::Make(GS, BindingKey::Default), V);
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000711
712 // Even if there are no bindings in the global scope, we still need to
713 // record that we touched it.
714 if (Regions)
715 Regions->push_back(GS);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000716 }
717
718 return B.getRoot();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000719}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000720
Ted Kremenek9af46f52009-06-16 22:36:44 +0000721//===----------------------------------------------------------------------===//
722// Extents for regions.
723//===----------------------------------------------------------------------===//
724
Zhongxing Xue884ff82009-11-12 02:48:32 +0000725DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000726 const MemRegion *R,
727 QualType EleTy) {
Jordy Rose32f26562010-07-04 00:00:41 +0000728 SVal Size = cast<SubRegion>(R)->getExtent(ValMgr);
729 SValuator &SVator = ValMgr.getSValuator();
730 const llvm::APSInt *SizeInt = SVator.getKnownValue(state, Size);
731 if (!SizeInt)
732 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Jordy Rose32f26562010-07-04 00:00:41 +0000734 CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000735 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Jordy Rose32f26562010-07-04 00:00:41 +0000737 // If a variable is reinterpreted as a type that doesn't fit into a larger
738 // type evenly, round it down.
739 // This is a signed value, since it's used in arithmetic with signed indices.
740 return ValMgr.makeIntVal(RegionSize / EleSize, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000741}
742
Ted Kremenek9af46f52009-06-16 22:36:44 +0000743//===----------------------------------------------------------------------===//
744// Location and region casting.
745//===----------------------------------------------------------------------===//
746
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000747/// ArrayToPointer - Emulates the "decay" of an array to a pointer
748/// type. 'Array' represents the lvalue of the array being decayed
749/// to a pointer, and the returned SVal represents the decayed
750/// version of that lvalue (i.e., a pointer to the first element of
751/// the array). This is called by GRExprEngine when evaluating casts
752/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000753SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000754 if (!isa<loc::MemRegionVal>(Array))
755 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenekabb042f2008-12-13 19:24:37 +0000757 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
758 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000760 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000761 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000763 // Strip off typedefs from the ArrayRegion's ValueType.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000764 QualType T = ArrayR->getValueType().getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000765 ArrayType *AT = cast<ArrayType>(T);
766 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenek75185b52009-07-16 00:00:11 +0000768 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000769 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000770}
771
Ted Kremenek9af46f52009-06-16 22:36:44 +0000772//===----------------------------------------------------------------------===//
773// Pointer arithmetic.
774//===----------------------------------------------------------------------===//
775
Zhongxing Xu461147f2010-02-05 05:24:20 +0000776SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000777 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000778 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000779 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000780 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000781
Jordy Roseeac4a002010-06-28 08:26:15 +0000782 // Special case for zero RHS.
783 if (R.isZeroConstant()) {
784 switch (Op) {
785 default:
786 // Handle it normally.
787 break;
788 case BinaryOperator::Add:
789 case BinaryOperator::Sub:
790 // FIXME: does this need to be casted to match resultTy?
791 return L;
792 }
793 }
794
Zhongxing Xua1718c72009-04-03 07:33:13 +0000795 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000796 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000797
Ted Kremenek3bccf082009-07-11 00:58:27 +0000798 switch (MR->getKind()) {
799 case MemRegion::SymbolicRegionKind: {
800 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000801 SymbolRef Sym = SR->getSymbol();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000802 QualType T = Sym->getType(Ctx);
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000803 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000805 if (const PointerType *PT = T->getAs<PointerType>())
806 EleTy = PT->getPointeeType();
807 else
John McCall183700f2009-09-21 23:43:11 +0000808 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Ted Kremenek3bccf082009-07-11 00:58:27 +0000810 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000811 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000812 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000813 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000814 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000815 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000816 QualType EleTy = Ctx.CharTy; // Create an ElementRegion of bytes.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000817 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000818 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000819 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000820 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000821
Ted Kremenek3bccf082009-07-11 00:58:27 +0000822 case MemRegion::ElementRegionKind: {
823 ER = cast<ElementRegion>(MR);
824 break;
825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek3bccf082009-07-11 00:58:27 +0000827 // Not yet handled.
828 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000829 case MemRegion::StringRegionKind: {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000830
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000831 }
832 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000833 case MemRegion::CompoundLiteralRegionKind:
834 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000835 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000836 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000837 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000839 case MemRegion::FunctionTextRegionKind:
840 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000841 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000842 // Technically this can happen if people do funny things with casts.
843 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenekde0d2632010-01-05 02:18:06 +0000845 case MemRegion::CXXThisRegionKind:
846 assert(0 &&
847 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000848 case MemRegion::GenericMemSpaceRegionKind:
849 case MemRegion::StackLocalsSpaceRegionKind:
850 case MemRegion::StackArgumentsSpaceRegionKind:
851 case MemRegion::HeapSpaceRegionKind:
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000852 case MemRegion::NonStaticGlobalSpaceRegionKind:
853 case MemRegion::StaticGlobalSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000854 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000855 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
856 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000857 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000858
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000859 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000860 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000861
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000862 // For now, only support:
863 // (a) concrete integer indices that can easily be resolved
864 // (b) 0 + symbolic index
865 if (Base) {
866 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
867 // FIXME: Should use SValuator here.
868 SVal NewIdx =
869 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000870 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000871 const MemRegion* NewER =
872 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000873 ER->getSuperRegion(), Ctx);
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000874 return ValMgr.makeLoc(NewER);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000875 }
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000876 if (0 == Base->getValue()) {
877 const MemRegion* NewER =
878 MRMgr.getElementRegion(ER->getElementType(), R,
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000879 ER->getSuperRegion(), Ctx);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000880 return ValMgr.makeLoc(NewER);
881 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek5dc27462009-03-03 02:51:43 +0000884 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000885}
886
Ted Kremenek9af46f52009-06-16 22:36:44 +0000887//===----------------------------------------------------------------------===//
888// Loading values from regions.
889//===----------------------------------------------------------------------===//
890
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000891Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Zhongxing Xubdfa85f2010-05-29 06:23:24 +0000892 const MemRegion *R) {
Zhongxing Xu42c67bf2010-05-29 06:49:04 +0000893
894 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
895 return *V;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000896
Zhongxing Xu13d50172009-10-11 08:08:02 +0000897 return Optional<SVal>();
898}
899
900Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000901 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000902 if (R->isBoundable())
903 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +0000904 if (TR->getValueType()->isUnionType())
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000905 return UnknownVal();
906
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000907 if (const SVal *V = Lookup(B, R, BindingKey::Default))
908 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +0000909
910 return Optional<SVal>();
911}
912
Zhongxing Xu576bb922010-02-05 03:01:53 +0000913SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000914 assert(!isa<UnknownVal>(L) && "location unknown");
915 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000916
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000917 // FIXME: Is this even possible? Shouldn't this be treated as a null
918 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000919 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000920 return UndefinedVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000921
Ted Kremenek67f28532009-06-17 22:02:04 +0000922 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000923
Tom Care7b050302010-06-25 18:22:31 +0000924 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR)) {
925 if (T.isNull()) {
926 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000927 T = SR->getSymbol()->getType(Ctx);
Tom Care7b050302010-06-25 18:22:31 +0000928 }
Zhongxing Xu81491852010-02-08 08:43:02 +0000929 MR = GetElementZeroRegion(MR, T);
Tom Care7b050302010-06-25 18:22:31 +0000930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000932 if (isa<CodeTextRegion>(MR)) {
933 assert(0 && "Why load from a code text region?");
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000934 return UnknownVal();
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000935 }
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000937 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
938 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000939 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000940 QualType RTy = R->getValueType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000941
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000942 // FIXME: We should eventually handle funny addressing. e.g.:
943 //
944 // int x = ...;
945 // int *p = &x;
946 // char *q = (char*) p;
947 // char c = *q; // returns the first byte of 'x'.
948 //
949 // Such funny addressing will occur due to layering of regions.
950
Douglas Gregorfb87b892010-04-26 21:31:17 +0000951 if (RTy->isStructureOrClassType())
Zhongxing Xu576bb922010-02-05 03:01:53 +0000952 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000954 // FIXME: Handle unions.
955 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000956 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000957
958 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +0000959 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000960
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000961 // FIXME: handle Vector types.
962 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000963 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +0000964
965 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +0000966 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000967
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000968 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
969 // FIXME: Here we actually perform an implicit conversion from the loaded
970 // value to the element type. Eventually we want to compose these values
971 // more intelligently. For example, an 'element' can encompass multiple
972 // bound regions (e.g., several bound bytes), or could be a subset of
973 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000974 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000977 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
978 // FIXME: Here we actually perform an implicit conversion from the loaded
979 // value to the ivar type. What we should model is stores to ivars
980 // that blow past the extent of the ivar. If the address of the ivar is
981 // reinterpretted, it is possible we stored a different value that could
982 // fit within the ivar. Either we need to cast these when storing them
983 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +0000984 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000987 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
988 // FIXME: Here we actually perform an implicit conversion from the loaded
989 // value to the variable type. What we should model is stores to variables
990 // that blow past the extent of the variable. If the address of the
991 // variable is reinterpretted, it is possible we stored a different value
992 // that could fit within the variable. Either we need to cast these when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000993 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +0000994 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000995 }
Ted Kremenek25c54572009-07-20 22:58:02 +0000996
Zhongxing Xu576bb922010-02-05 03:01:53 +0000997 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000998 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000999
1000 // Check if the region has a binding.
1001 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001002 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001003
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001004 // The location does not have a bound value. This means that it has
1005 // the value it had upon its creation and/or entry to the analyzed
1006 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001007 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001008 // All stack variables are considered to have undefined values
1009 // upon creation. All heap allocated blocks are considered to
1010 // have undefined values as well unless they are explicitly bound
1011 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001012 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001013 }
1014
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001015 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001016 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001017}
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001019std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001020RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001021 if (Optional<SVal> OV = getDirectBinding(B, R))
1022 if (const nonloc::LazyCompoundVal *V =
1023 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001024 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001026 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001027 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001028 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001030 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001031 return std::make_pair(X.first,
1032 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001033 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001034 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001035 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001036 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001038 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001039 return std::make_pair(X.first,
1040 MRMgr.getFieldRegionWithSuper(FR, X.second));
1041 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001042 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xudcbcbdc2010-02-10 02:02:10 +00001043 // possible for a valid lazy binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001044 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001045}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001046
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001047SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001048 const ElementRegion* R) {
1049 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001050 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001051 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001052 return *V;
1053
Ted Kremenek921109a2009-07-01 23:19:52 +00001054 const MemRegion* superR = R->getSuperRegion();
1055
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001056 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001057 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001058 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001059 // an integer, e.g., *((unsigned int*)"hello")
Zhongxing Xu018220c2010-08-11 06:10:55 +00001060 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001061 if (T != Ctx.getCanonicalType(R->getElementType()))
1062 return UnknownVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001063
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001064 const StringLiteral *Str = StrR->getStringLiteral();
1065 SVal Idx = R->getIndex();
1066 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1067 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001068 int64_t byteLength = Str->getByteLength();
Jordy Rose167cc372010-07-29 06:40:33 +00001069 // Technically, only i == byteLength is guaranteed to be null.
1070 // However, such overflows should be caught before reaching this point;
1071 // the only time such an access would be made is if a string literal was
1072 // used to initialize a larger array.
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +00001073 char c = (i >= byteLength) ? '\0' : Str->getString()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001074 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001075 }
1076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Ted Kremeneka709b872010-05-31 01:22:04 +00001078 // Handle the case where we are indexing into a larger scalar object.
1079 // For example, this handles:
1080 // int x = ...
1081 // char *y = &x;
1082 // return *y;
1083 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001084 const RegionRawOffset &O = R->getAsArrayOffset();
Ted Kremeneka709b872010-05-31 01:22:04 +00001085 if (const TypedRegion *baseR = dyn_cast_or_null<TypedRegion>(O.getRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001086 QualType baseT = baseR->getValueType();
Ted Kremeneka709b872010-05-31 01:22:04 +00001087 if (baseT->isScalarType()) {
1088 QualType elemT = R->getElementType();
1089 if (elemT->isScalarType()) {
1090 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1091 if (const Optional<SVal> &V = getDirectBinding(B, superR)) {
1092 if (SymbolRef parentSym = V->getAsSymbol())
1093 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001094
Ted Kremeneka709b872010-05-31 01:22:04 +00001095 if (V->isUnknownOrUndef())
1096 return *V;
1097 // Other cases: give up. We are indexing into a larger object
1098 // that has some value, but we don't know how to handle that yet.
1099 return UnknownVal();
1100 }
1101 }
1102 }
Zhongxing Xu42c67bf2010-05-29 06:49:04 +00001103 }
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001104 }
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001105 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001106}
1107
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001108SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001109 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001110
1111 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001112 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001113 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001114 return *V;
1115
Zhongxing Xu018220c2010-08-11 06:10:55 +00001116 QualType Ty = R->getValueType();
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001117 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001118}
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001120Optional<SVal>
1121RegionStoreManager::RetrieveDerivedDefaultValue(RegionBindings B,
1122 const MemRegion *superR,
1123 const TypedRegion *R,
1124 QualType Ty) {
1125
1126 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
1127 if (SymbolRef parentSym = D->getAsSymbol())
1128 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1129
1130 if (D->isZeroConstant())
1131 return ValMgr.makeZeroVal(Ty);
1132
1133 if (D->isUnknownOrUndef())
1134 return *D;
1135
1136 assert(0 && "Unknown default value");
1137 }
1138
1139 return Optional<SVal>();
1140}
1141
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001142SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001143 const TypedRegion *R,
1144 QualType Ty,
1145 const MemRegion *superR) {
1146
Mike Stump1eb44332009-09-09 15:08:12 +00001147 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001148 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001150 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001152 while (superR) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001153 if (const Optional<SVal> &D = RetrieveDerivedDefaultValue(B, superR, R, Ty))
1154 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001156 // If our super region is a field or element itself, walk up the region
1157 // hierarchy to see if there is a default value installed in an ancestor.
1158 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1159 superR = cast<SubRegion>(superR)->getSuperRegion();
1160 continue;
1161 }
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001163 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001164 }
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001166 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001167 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001168 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001169 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001171 if (lazyBindingRegion) {
1172 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1173 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001174 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001175 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001176 }
1177
Ted Kremenekde0d2632010-01-05 02:18:06 +00001178 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001179 if (isa<ElementRegion>(R)) {
1180 // Currently we don't reason specially about Clang-style vectors. Check
1181 // if superR is a vector and if so return Unknown.
1182 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001183 if (typedSuperR->getValueType()->isVectorType())
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001184 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001185 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001186 }
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001188 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001189 }
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001191 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001192 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001193}
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Zhongxing Xu576bb922010-02-05 03:01:53 +00001195SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001196
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001197 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001198 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001199
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001200 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001201 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001203 const MemRegion *superR = R->getSuperRegion();
1204
Ted Kremenekab22ee92009-10-20 01:20:57 +00001205 // Check if the super region has a default binding.
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001206 if (const Optional<SVal> &V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001207 if (SymbolRef parentSym = V->getAsSymbol())
1208 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001210 // Other cases: give up.
1211 return UnknownVal();
1212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Zhongxing Xu576bb922010-02-05 03:01:53 +00001214 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001215}
1216
Zhongxing Xu576bb922010-02-05 03:01:53 +00001217SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Ted Kremenek9031dd72009-07-21 00:12:07 +00001219 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001220 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001222 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001223 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Ted Kremenek9031dd72009-07-21 00:12:07 +00001225 // Lazily derive a value for the VarRegion.
1226 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001227 QualType T = VD->getType();
1228 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001229
1230 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek4dc15662010-02-06 03:57:59 +00001231 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu14d23282010-03-01 06:56:52 +00001232 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Ted Kremenek4dc15662010-02-06 03:57:59 +00001234 if (isa<GlobalsSpaceRegion>(MS)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001235 if (isa<NonStaticGlobalSpaceRegion>(MS)) {
Ted Kremenek4552ff02010-03-30 20:31:04 +00001236 // Is 'VD' declared constant? If so, retrieve the constant value.
1237 QualType CT = Ctx.getCanonicalType(T);
1238 if (CT.isConstQualified()) {
1239 const Expr *Init = VD->getInit();
1240 // Do the null check first, as we want to call 'IgnoreParenCasts'.
1241 if (Init)
1242 if (const IntegerLiteral *IL =
1243 dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) {
1244 const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL);
1245 return ValMgr.getSValuator().EvalCast(V, Init->getType(),
1246 IL->getType());
1247 }
1248 }
1249
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001250 if (const Optional<SVal> &V = RetrieveDerivedDefaultValue(B, MS, R, CT))
1251 return V.getValue();
1252
Zhongxing Xu14d23282010-03-01 06:56:52 +00001253 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek4552ff02010-03-30 20:31:04 +00001254 }
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Ted Kremenek4dc15662010-02-06 03:57:59 +00001256 if (T->isIntegerType())
1257 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001258 if (T->isPointerType())
1259 return ValMgr.makeNull();
1260
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001261 return UnknownVal();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001262 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001263
Ted Kremenek9031dd72009-07-21 00:12:07 +00001264 return UndefinedVal();
1265}
1266
Zhongxing Xu576bb922010-02-05 03:01:53 +00001267SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001268 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001269 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001270}
1271
Zhongxing Xu576bb922010-02-05 03:01:53 +00001272SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001273 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001274 assert(T->isStructureOrClassType());
Zhongxing Xu576bb922010-02-05 03:01:53 +00001275 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001276}
1277
Zhongxing Xu576bb922010-02-05 03:01:53 +00001278SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001279 assert(isa<ConstantArrayType>(R->getValueType()));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001280 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001281}
1282
Ted Kremenek9af46f52009-06-16 22:36:44 +00001283//===----------------------------------------------------------------------===//
1284// Binding values to regions.
1285//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001286
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001287Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001288 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001289 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001290 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Ted Kremenek0964a062009-01-21 06:57:53 +00001292 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001293}
1294
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001295Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001296 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001297 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001298
Ted Kremenek9af46f52009-06-16 22:36:44 +00001299 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001300 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Ted Kremenek9af46f52009-06-16 22:36:44 +00001302 // Check if the region is a struct region.
1303 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +00001304 if (TR->getValueType()->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001305 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001307 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1308 if (ER->getIndex().isZeroConstant()) {
1309 if (const TypedRegion *superR =
1310 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001311 QualType superTy = superR->getValueType();
Ted Kremenek69181a82009-09-21 22:58:52 +00001312 // For now, just invalidate the fields of the struct/union/class.
Zhongxing Xu81fa4ec2010-08-21 11:03:37 +00001313 // This is for test rdar_test_7185607 in misc-ps-region-store.m.
Ted Kremenek69181a82009-09-21 22:58:52 +00001314 // FIXME: Precisely handle the fields of the record.
Jordy Rose58f8b202010-08-05 03:28:45 +00001315 if (superTy->isStructureOrClassType())
1316 return KillStruct(store, superR, UnknownVal());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001317 }
1318 }
1319 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001320 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1321 // Binding directly to a symbolic region should be treated as binding
1322 // to element 0.
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001323 QualType T = SR->getSymbol()->getType(Ctx);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001324
Ted Kremenek852274d2009-12-16 03:18:58 +00001325 // FIXME: Is this the right way to handle symbols that are references?
1326 if (const PointerType *PT = T->getAs<PointerType>())
1327 T = PT->getPointeeType();
1328 else
1329 T = T->getAs<ReferenceType>()->getPointeeType();
1330
Ted Kremenek0954cde2009-09-24 04:11:44 +00001331 R = GetElementZeroRegion(SR, T);
1332 }
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001334 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001335 RegionBindings B = GetRegionBindings(store);
1336 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001337}
1338
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001339Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001340 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001341
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001342 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001343
Ted Kremenek0964a062009-01-21 06:57:53 +00001344 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001345 return BindArray(store, VR, InitVal);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001346 if (T->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001347 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001348
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001349 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001350}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001351
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001352// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001353Store RegionStoreManager::BindCompoundLiteral(Store store,
1354 const CompoundLiteralExpr *CL,
1355 const LocationContext *LC,
1356 SVal V) {
1357 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001358 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001359}
1360
Zhongxing Xua5ce9662010-06-01 03:01:33 +00001361
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001362Store RegionStoreManager::setImplicitDefaultValue(Store store,
1363 const MemRegion *R,
1364 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001365 RegionBindings B = GetRegionBindings(store);
1366 SVal V;
1367
1368 if (Loc::IsLocType(T))
1369 V = ValMgr.makeNull();
1370 else if (T->isIntegerType())
1371 V = ValMgr.makeZeroVal(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001372 else if (T->isStructureOrClassType() || T->isArrayType()) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001373 // Set the default value to a zero constant when it is a structure
1374 // or array. The type doesn't really matter.
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001375 V = ValMgr.makeZeroVal(Ctx.IntTy);
Ted Kremenek027e2662009-11-19 20:20:24 +00001376 }
1377 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001378 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001379 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001380
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001381 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001382}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001383
1384Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001385 SVal Init) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001386
Zhongxing Xu018220c2010-08-11 06:10:55 +00001387 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001388 QualType ElementTy = AT->getElementType();
Ted Kremenekfee90812010-01-26 23:51:00 +00001389 Optional<uint64_t> Size;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001390
Ted Kremenekfee90812010-01-26 23:51:00 +00001391 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1392 Size = CAT->getSize().getZExtValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001393
Jordy Rose167cc372010-07-29 06:40:33 +00001394 // Check if the init expr is a string literal.
1395 if (loc::MemRegionVal *MRV = dyn_cast<loc::MemRegionVal>(&Init)) {
1396 const StringRegion *S = cast<StringRegion>(MRV->getRegion());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001397
Jordy Rose167cc372010-07-29 06:40:33 +00001398 // Treat the string as a lazy compound value.
1399 nonloc::LazyCompoundVal LCV =
1400 cast<nonloc::LazyCompoundVal>(ValMgr.makeLazyCompoundVal(store, S));
1401 return CopyLazyBindings(LCV, store, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001402 }
1403
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001404 // Handle lazy compound values.
1405 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001406 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001407
1408 // Remaining case: explicit compound values.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001409
Ted Kremenek027e2662009-11-19 20:20:24 +00001410 if (Init.isUnknown())
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001411 return setImplicitDefaultValue(store, R, ElementTy);
1412
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001413 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001414 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001415 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Ted Kremenekfee90812010-01-26 23:51:00 +00001417 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001418 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001419 if (VI == VE)
1420 break;
1421
Ted Kremenek46537392009-07-16 01:33:37 +00001422 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001423 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001424
Douglas Gregorfb87b892010-04-26 21:31:17 +00001425 if (ElementTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001426 store = BindStruct(store, ER, *VI);
Jordy Rose59b6dca2010-08-20 01:05:59 +00001427 else if (ElementTy->isArrayType())
1428 store = BindArray(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001429 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001430 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001431 }
1432
Ted Kremenek027e2662009-11-19 20:20:24 +00001433 // If the init list is shorter than the array length, set the
1434 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001435 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001436 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001437
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001438 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001439}
1440
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001441Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1442 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Ted Kremenek67f28532009-06-17 22:02:04 +00001444 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001445 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Zhongxing Xu018220c2010-08-11 06:10:55 +00001447 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001448 assert(T->isStructureOrClassType());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001449
Ted Kremenek6217b802009-07-29 21:53:49 +00001450 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001451 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001452
1453 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001454 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001455
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001456 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001457 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001458 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001460 // We may get non-CompoundVal accidentally due to imprecise cast logic or
1461 // that we are binding symbolic struct value. Kill the field values, and if
1462 // the value is symbolic go and bind it as a "default" binding.
Ted Kremenek4af473c2010-08-17 23:29:06 +00001463 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V)) {
1464 SVal SV = isa<nonloc::SymbolVal>(V) ? V : UnknownVal();
1465 return KillStruct(store, R, SV);
1466 }
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001467
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001468 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001469 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001470
1471 RecordDecl::field_iterator FI, FE;
1472
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001473 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001474
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001475 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001476 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001477
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001478 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001479 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001480
Ted Kremenekcf549592009-09-22 21:19:14 +00001481 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001482 store = BindArray(store, FR, *VI);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001483 else if (FTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001484 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001485 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001486 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001487 }
1488
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001489 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001490 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001491 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001492 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001493 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001494 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001495
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001496 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001497}
1498
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001499Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R,
1500 SVal DefaultVal) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001501 RegionBindings B = GetRegionBindings(store);
1502 llvm::OwningPtr<RegionStoreSubRegionMap>
1503 SubRegions(getRegionStoreSubRegionMap(store));
1504 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001505
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001506 // Set the default value of the struct region to "unknown".
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001507 return Add(B, R, BindingKey::Default, DefaultVal).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001508}
1509
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001510Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1511 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001512
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001513 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001514 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001515
Mike Stump1eb44332009-09-09 15:08:12 +00001516 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001517 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001518
Mike Stump1eb44332009-09-09 15:08:12 +00001519 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001520 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001522 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1523 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001524 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001525}
1526
1527//===----------------------------------------------------------------------===//
1528// "Raw" retrievals and bindings.
1529//===----------------------------------------------------------------------===//
1530
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001531BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001532 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001533 const RegionRawOffset &O = ER->getAsArrayOffset();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001534
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001535 // FIXME: There are some ElementRegions for which we cannot compute
Jordy Rosee7011172010-08-16 01:15:17 +00001536 // raw offsets yet, including regions with symbolic offsets. These will be
1537 // ignored by the store.
1538 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001539 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001540
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001541 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001542}
1543
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001544RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001545 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001546 return B;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001547 return RBFactory.Add(B, K, V);
1548}
1549
1550RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001551 BindingKey::Kind k, SVal V) {
1552 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001553}
1554
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001555const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001556 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001557 return NULL;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001558 return B.lookup(K);
1559}
1560
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001561const SVal *RegionStoreManager::Lookup(RegionBindings B,
1562 const MemRegion *R,
1563 BindingKey::Kind k) {
1564 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001565}
1566
1567RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001568 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001569 return B;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001570 return RBFactory.Remove(B, K);
1571}
1572
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001573RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1574 BindingKey::Kind k){
1575 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001576}
1577
Ted Kremenek9af46f52009-06-16 22:36:44 +00001578//===----------------------------------------------------------------------===//
1579// State pruning.
1580//===----------------------------------------------------------------------===//
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001581
Ted Kremenek5499b842010-03-10 16:32:56 +00001582namespace {
1583class RemoveDeadBindingsWorker :
1584 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1585 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1586 SymbolReaper &SymReaper;
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001587 const StackFrameContext *CurrentLCtx;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001588
Ted Kremenek5499b842010-03-10 16:32:56 +00001589public:
1590 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1591 RegionBindings b, SymbolReaper &symReaper,
Jordy Rose7dadf792010-07-01 20:09:55 +00001592 const StackFrameContext *LCtx)
Ted Kremenek5499b842010-03-10 16:32:56 +00001593 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
Jordy Rose7dadf792010-07-01 20:09:55 +00001594 SymReaper(symReaper), CurrentLCtx(LCtx) {}
Ted Kremenek5499b842010-03-10 16:32:56 +00001595
1596 // Called by ClusterAnalysis.
1597 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1598 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek5499b842010-03-10 16:32:56 +00001599
Ted Kremenek75a2d942010-04-01 00:15:55 +00001600 void VisitBindingKey(BindingKey K);
Ted Kremenek5499b842010-03-10 16:32:56 +00001601 bool UpdatePostponed();
1602 void VisitBinding(SVal V);
1603};
1604}
1605
1606void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1607 RegionCluster &C) {
1608
1609 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
Jordy Rose7dadf792010-07-01 20:09:55 +00001610 if (SymReaper.isLive(VR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001611 AddToWorkList(baseR, C);
1612
1613 return;
1614 }
1615
1616 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1617 if (SymReaper.isLive(SR->getSymbol()))
1618 AddToWorkList(SR, C);
1619 else
1620 Postponed.push_back(SR);
1621
1622 return;
1623 }
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001624
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001625 if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
1626 AddToWorkList(baseR, C);
1627 return;
1628 }
1629
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001630 // CXXThisRegion in the current or parent location context is live.
1631 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001632 const StackArgumentsSpaceRegion *StackReg =
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001633 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
1634 const StackFrameContext *RegCtx = StackReg->getStackFrame();
1635 if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
1636 AddToWorkList(TR, C);
1637 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001638}
1639
1640void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1641 BindingKey *I, BindingKey *E) {
Ted Kremenek75a2d942010-04-01 00:15:55 +00001642 for ( ; I != E; ++I)
1643 VisitBindingKey(*I);
Ted Kremenek5499b842010-03-10 16:32:56 +00001644}
1645
1646void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1647 // Is it a LazyCompoundVal? All referenced regions are live as well.
1648 if (const nonloc::LazyCompoundVal *LCS =
1649 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1650
1651 const MemRegion *LazyR = LCS->getRegion();
1652 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1653 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +00001654 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
1655 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001656 VisitBinding(RI.getData());
1657 }
1658 return;
1659 }
1660
1661 // If V is a region, then add it to the worklist.
1662 if (const MemRegion *R = V.getAsRegion())
1663 AddToWorkList(R);
1664
1665 // Update the set of live symbols.
1666 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1667 SI!=SE;++SI)
1668 SymReaper.markLive(*SI);
1669}
1670
Ted Kremenek75a2d942010-04-01 00:15:55 +00001671void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) {
1672 const MemRegion *R = K.getRegion();
1673
Ted Kremenek5499b842010-03-10 16:32:56 +00001674 // Mark this region "live" by adding it to the worklist. This will cause
1675 // use to visit all regions in the cluster (if we haven't visited them
1676 // already).
Ted Kremenek75a2d942010-04-01 00:15:55 +00001677 if (AddToWorkList(R)) {
1678 // Mark the symbol for any live SymbolicRegion as "live". This means we
1679 // should continue to track that symbol.
1680 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1681 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek5499b842010-03-10 16:32:56 +00001682
Ted Kremenek75a2d942010-04-01 00:15:55 +00001683 // For BlockDataRegions, enqueue the VarRegions for variables marked
1684 // with __block (passed-by-reference).
1685 // via BlockDeclRefExprs.
1686 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1687 for (BlockDataRegion::referenced_vars_iterator
1688 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1689 RI != RE; ++RI) {
1690 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1691 AddToWorkList(*RI);
1692 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001693
Ted Kremenek75a2d942010-04-01 00:15:55 +00001694 // No possible data bindings on a BlockDataRegion.
1695 return;
Ted Kremenek5499b842010-03-10 16:32:56 +00001696 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001697 }
1698
Ted Kremenek75a2d942010-04-01 00:15:55 +00001699 // Visit the data binding for K.
1700 if (const SVal *V = RM.Lookup(B, K))
Ted Kremenek5499b842010-03-10 16:32:56 +00001701 VisitBinding(*V);
1702}
1703
1704bool RemoveDeadBindingsWorker::UpdatePostponed() {
1705 // See if any postponed SymbolicRegions are actually live now, after
1706 // having done a scan.
1707 bool changed = false;
1708
1709 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1710 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1711 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1712 if (SymReaper.isLive(SR->getSymbol())) {
1713 changed |= AddToWorkList(SR);
1714 *I = NULL;
1715 }
1716 }
1717 }
1718
1719 return changed;
1720}
1721
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001722Store RegionStoreManager::RemoveDeadBindings(Store store,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001723 const StackFrameContext *LCtx,
Zhongxing Xu72119c42010-02-05 05:34:29 +00001724 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001725 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001726{
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001727 RegionBindings B = GetRegionBindings(store);
Jordy Rose7dadf792010-07-01 20:09:55 +00001728 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
Ted Kremenek5499b842010-03-10 16:32:56 +00001729 W.GenerateClusters();
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Ted Kremenek5499b842010-03-10 16:32:56 +00001731 // Enqueue the region roots onto the worklist.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001732 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenek5499b842010-03-10 16:32:56 +00001733 E=RegionRoots.end(); I!=E; ++I)
1734 W.AddToWorkList(*I);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001735
Ted Kremenek5499b842010-03-10 16:32:56 +00001736 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001737
Ted Kremenek9af46f52009-06-16 22:36:44 +00001738 // We have now scanned the store, marking reachable regions and symbols
1739 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001740 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001741 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek5499b842010-03-10 16:32:56 +00001742 const BindingKey &K = I.getKey();
1743
Ted Kremenekb7118f72010-03-10 16:38:41 +00001744 // If the cluster has been visited, we know the region has been marked.
Ted Kremenek5499b842010-03-10 16:32:56 +00001745 if (W.isVisited(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001746 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Ted Kremenek5499b842010-03-10 16:32:56 +00001748 // Remove the dead entry.
1749 B = Remove(B, K);
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Ted Kremenek5499b842010-03-10 16:32:56 +00001751 // Mark all non-live symbols that this binding references as dead.
1752 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001753 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001755 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001756 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1757 for (; SI != SE; ++SI)
1758 SymReaper.maybeDead(*SI);
1759 }
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001760
1761 return B.getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001762}
1763
Ted Kremenek5499b842010-03-10 16:32:56 +00001764
Jordy Roseff59efd2010-08-03 20:44:35 +00001765Store RegionStoreManager::EnterStackFrame(const GRState *state,
1766 const StackFrameContext *frame) {
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001767 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001768 FunctionDecl::param_const_iterator PI = FD->param_begin();
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001769 Store store = state->getStore();
Zhongxing Xuc5063572010-03-16 13:14:16 +00001770
1771 if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) {
1772 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1773
1774 // Copy the arg expression value to the arg variables.
1775 for (; AI != AE; ++AI, ++PI) {
1776 SVal ArgVal = state->getSVal(*AI);
1777 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1778 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001779 } else if (const CXXConstructExpr *CE =
Zhongxing Xuc5063572010-03-16 13:14:16 +00001780 dyn_cast<CXXConstructExpr>(frame->getCallSite())) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001781 CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(),
Zhongxing Xuc5063572010-03-16 13:14:16 +00001782 AE = CE->arg_end();
1783
1784 // Copy the arg expression value to the arg variables.
1785 for (; AI != AE; ++AI, ++PI) {
1786 SVal ArgVal = state->getSVal(*AI);
1787 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1788 }
1789 } else
Jordy Roseff59efd2010-08-03 20:44:35 +00001790 llvm_unreachable("Unhandled call expression.");
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001791
Jordy Roseff59efd2010-08-03 20:44:35 +00001792 return store;
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001793}
1794
Ted Kremenek9af46f52009-06-16 22:36:44 +00001795//===----------------------------------------------------------------------===//
1796// Utility methods.
1797//===----------------------------------------------------------------------===//
1798
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001799void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001800 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001801 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001802 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Ted Kremenek451ac092009-08-06 04:50:20 +00001804 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001805 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001806}