blob: 231be0af18d77986f4e7e271328b7af3546560a1 [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
Zhongxing Xudc015e52010-08-21 12:24:38 +000081BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
82 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
83 const RegionRawOffset &O = ER->getAsArrayOffset();
84
85 // FIXME: There are some ElementRegions for which we cannot compute
86 // raw offsets yet, including regions with symbolic offsets. These will be
87 // ignored by the store.
88 return BindingKey(O.getRegion(), O.getByteOffset(), k);
89 }
90
91 return BindingKey(R, 0, k);
92}
93
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000094namespace llvm {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000095 static inline
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000096 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000097 os << '(' << K.getRegion() << ',' << K.getOffset()
98 << ',' << (K.isDirect() ? "direct" : "default")
99 << ')';
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000100 return os;
101 }
102} // end llvm namespace
103
104//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000105// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000106//===----------------------------------------------------------------------===//
107
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000108typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000109
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000110//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000111// Fine-grained control of RegionStoreManager.
112//===----------------------------------------------------------------------===//
113
114namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000115struct minimal_features_tag {};
116struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000118class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000119 bool SupportsFields;
Ted Kremenek9af46f52009-06-16 22:36:44 +0000120public:
121 RegionStoreFeatures(minimal_features_tag) :
Ted Kremenek0752d6d2010-08-20 06:06:41 +0000122 SupportsFields(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Ted Kremenek9af46f52009-06-16 22:36:44 +0000124 RegionStoreFeatures(maximal_features_tag) :
Ted Kremenek0752d6d2010-08-20 06:06:41 +0000125 SupportsFields(true) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Ted Kremenek9af46f52009-06-16 22:36:44 +0000127 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenek9af46f52009-06-16 22:36:44 +0000129 bool supportsFields() const { return SupportsFields; }
Ted Kremenek9af46f52009-06-16 22:36:44 +0000130};
131}
132
133//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000134// Main RegionStore logic.
135//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000136
Zhongxing Xu17892752008-10-08 02:50:44 +0000137namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000139class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekdf165012010-02-02 22:38:47 +0000140public:
141 typedef llvm::ImmutableSet<const MemRegion*> Set;
142 typedef llvm::DenseMap<const MemRegion*, Set> Map;
143private:
144 Set::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000145 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000146public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000147 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000149
150 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000151 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000152 return true;
153 }
154
155 I->second = F.Add(I->second, SubRegion);
156 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000159 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremenek59e8f112009-03-03 01:35:36 +0000161 ~RegionStoreSubRegionMap() {}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000162
Ted Kremenekdf165012010-02-02 22:38:47 +0000163 const Set *getSubRegions(const MemRegion *Parent) const {
164 Map::const_iterator I = M.find(Parent);
165 return I == M.end() ? NULL : &I->second;
166 }
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenek5dc27462009-03-03 02:51:43 +0000168 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000169 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000170
171 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000172 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremenekdf165012010-02-02 22:38:47 +0000174 Set S = I->second;
175 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000176 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000177 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek5dc27462009-03-03 02:51:43 +0000180 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000183
Zhongxing Xu796788f2010-08-23 01:37:32 +0000184void
185RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
186 const SubRegion *R) {
187 const MemRegion *superR = R->getSuperRegion();
188 if (add(superR, R))
189 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
190 WL.push_back(sr);
191}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000192
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000193class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000194 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000195 RegionBindings::Factory RBFactory;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000196
Zhongxing Xu17892752008-10-08 02:50:44 +0000197public:
Mike Stump1eb44332009-09-09 15:08:12 +0000198 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000199 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000200 Features(f),
Ted Kremenek8928d412010-02-04 04:14:49 +0000201 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000202
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000203 SubRegionMap *getSubRegionMap(Store store) {
204 return getRegionStoreSubRegionMap(store);
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Zhongxing Xu13d50172009-10-11 08:08:02 +0000207 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Zhongxing Xu13d50172009-10-11 08:08:02 +0000209 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000210 /// getDefaultBinding - Returns an SVal* representing an optional default
211 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000212 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000213
Ted Kremenek027e2662009-11-19 20:20:24 +0000214 /// setImplicitDefaultValue - Set the default binding for the provided
215 /// MemRegion to the value implicitly defined for compound literals when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000216 /// the value is not specified.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000217 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000219 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
220 /// type. 'Array' represents the lvalue of the array being decayed
221 /// to a pointer, and the returned SVal represents the decayed
222 /// version of that lvalue (i.e., a pointer to the first element of
223 /// the array). This is called by GRExprEngine when evaluating
224 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000225 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000226
Zhongxing Xu461147f2010-02-05 05:24:20 +0000227 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000228
Mike Stump1eb44332009-09-09 15:08:12 +0000229 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000230 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000231 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000232
Ted Kremenek67f28532009-06-17 22:02:04 +0000233 //===-------------------------------------------------------------------===//
234 // Binding values to regions.
235 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000236
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000237 Store InvalidateRegions(Store store,
238 const MemRegion * const *Begin,
239 const MemRegion * const *End,
240 const Expr *E, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000241 InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000242 bool invalidateGlobals,
243 InvalidatedRegions *Regions);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000245public: // Made public for helper classes.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000246
Zhongxing Xu13d50172009-10-11 08:08:02 +0000247 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000248 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000250 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
251
252 RegionBindings Add(RegionBindings B, const MemRegion *R,
253 BindingKey::Kind k, SVal V);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000254
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000255 const SVal *Lookup(RegionBindings B, BindingKey K);
256 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000257
258 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000259 RegionBindings Remove(RegionBindings B, const MemRegion *R,
260 BindingKey::Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000261
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000262 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
263 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000264 }
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000265
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000266public: // Part of public interface to class.
267
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000268 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000269
Zhongxing Xu54460092010-06-01 04:49:26 +0000270 // BindDefault is only used to initialize a region with a default value.
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000271 Store BindDefault(Store store, const MemRegion *R, SVal V) {
Zhongxing Xu54460092010-06-01 04:49:26 +0000272 RegionBindings B = GetRegionBindings(store);
273 assert(!Lookup(B, R, BindingKey::Default));
274 assert(!Lookup(B, R, BindingKey::Direct));
275 return Add(B, R, BindingKey::Default, V).getRoot();
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000276 }
277
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000278 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
279 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000281 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000282
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000283 Store BindDeclWithNoInit(Store store, const VarRegion *) {
284 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000285 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000286
Ted Kremenek67f28532009-06-17 22:02:04 +0000287 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000288 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000290 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
292 /// KillStruct - Set the entire struct to unknown.
Ted Kremenek281e9dc2010-07-29 00:28:47 +0000293 Store KillStruct(Store store, const TypedRegion* R, SVal DefaultVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000294
Ted Kremenek67f28532009-06-17 22:02:04 +0000295 Store Remove(Store store, Loc LV);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000296
Ted Kremenek67f28532009-06-17 22:02:04 +0000297
298 //===------------------------------------------------------------------===//
299 // Loading values from regions.
300 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenek67f28532009-06-17 22:02:04 +0000302 /// The high level logic for this method is this:
303 /// Retrieve (L)
304 /// if L has binding
305 /// return L's binding
306 /// else if L is in killset
307 /// return unknown
308 /// else
309 /// if L is on stack or heap
310 /// return undefined
311 /// else
312 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000313 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000314
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000315 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000316
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000317 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Zhongxing Xu576bb922010-02-05 03:01:53 +0000319 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Zhongxing Xu576bb922010-02-05 03:01:53 +0000321 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Zhongxing Xu576bb922010-02-05 03:01:53 +0000323 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000325 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000326 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenek67f28532009-06-17 22:02:04 +0000328 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000329 /// struct copy:
330 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000331 /// x = y;
332 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000333 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Zhongxing Xu576bb922010-02-05 03:01:53 +0000335 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000337 /// Used to lazily generate derived symbols for bindings that are defined
338 /// implicitly by default bindings in a super region.
339 Optional<SVal> RetrieveDerivedDefaultValue(RegionBindings B,
340 const MemRegion *superR,
341 const TypedRegion *R, QualType Ty);
342
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000343 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000344 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000345 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000347 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
348 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000349
350 //===------------------------------------------------------------------===//
351 // State pruning.
352 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek67f28532009-06-17 22:02:04 +0000354 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
355 /// It returns a new Store with these values removed.
Zhongxing Xu5e4cb342010-08-15 12:45:09 +0000356 Store RemoveDeadBindings(Store store, const StackFrameContext *LCtx,
357 SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000358 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
359
Jordy Roseff59efd2010-08-03 20:44:35 +0000360 Store EnterStackFrame(const GRState *state, const StackFrameContext *frame);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000361
Ted Kremenek67f28532009-06-17 22:02:04 +0000362 //===------------------------------------------------------------------===//
363 // Region "extents".
364 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Jordy Rose32f26562010-07-04 00:00:41 +0000366 // FIXME: This method will soon be eliminated; see the note in Store.h.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000367 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000368 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000369
370 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000371 // Utility methods.
372 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek451ac092009-08-06 04:50:20 +0000374 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000375 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000376 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000377
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000378 void print(Store store, llvm::raw_ostream& Out, const char* nl,
379 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000380
381 void iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek0e9910f2010-06-17 00:24:42 +0000382 RegionBindings B = GetRegionBindings(store);
383 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
384 const BindingKey &K = I.getKey();
385 if (!K.isDirect())
386 continue;
387 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) {
388 // FIXME: Possibly incorporate the offset?
389 if (!f.HandleBinding(*this, store, R, I.getData()))
390 return;
391 }
392 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000393 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000394};
395
396} // end anonymous namespace
397
Ted Kremenek9af46f52009-06-16 22:36:44 +0000398//===----------------------------------------------------------------------===//
399// RegionStore creation.
400//===----------------------------------------------------------------------===//
401
402StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
403 RegionStoreFeatures F = maximal_features_tag();
404 return new RegionStoreManager(StMgr, F);
405}
406
407StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
408 RegionStoreFeatures F = minimal_features_tag();
409 F.enableFields(true);
410 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000411}
412
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000413
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000414RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000415RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
416 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000417 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000419 llvm::SmallVector<const SubRegion*, 10> WL;
420
Ted Kremenek451ac092009-08-06 04:50:20 +0000421 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000422 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000423 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Mike Stump1eb44332009-09-09 15:08:12 +0000425 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000426 // don't have direct bindings but are super regions of those that do.
427 while (!WL.empty()) {
428 const SubRegion *R = WL.back();
429 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000430 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000431 }
432
Ted Kremenek14453bf2009-03-03 19:02:42 +0000433 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000434}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000435
Ted Kremenek9af46f52009-06-16 22:36:44 +0000436//===----------------------------------------------------------------------===//
Ted Kremeneka4fab032010-03-10 07:19:59 +0000437// Region Cluster analysis.
438//===----------------------------------------------------------------------===//
439
440namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000441template <typename DERIVED>
Ted Kremeneka4fab032010-03-10 07:19:59 +0000442class ClusterAnalysis {
443protected:
444 typedef BumpVector<BindingKey> RegionCluster;
445 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5499b842010-03-10 16:32:56 +0000446 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
447 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
448 WorkList;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000449
450 BumpVectorContext BVC;
451 ClusterMap ClusterM;
Ted Kremenek5499b842010-03-10 16:32:56 +0000452 WorkList WL;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000453
454 RegionStoreManager &RM;
455 ASTContext &Ctx;
456 ValueManager &ValMgr;
457
Ted Kremenek5499b842010-03-10 16:32:56 +0000458 RegionBindings B;
Ted Kremenekd45d59f2010-10-26 00:06:15 +0000459
460 const bool includeGlobals;
Ted Kremenek5499b842010-03-10 16:32:56 +0000461
Ted Kremeneka4fab032010-03-10 07:19:59 +0000462public:
Ted Kremenek5499b842010-03-10 16:32:56 +0000463 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
Ted Kremenekd45d59f2010-10-26 00:06:15 +0000464 RegionBindings b, const bool includeGlobals)
Ted Kremenek5499b842010-03-10 16:32:56 +0000465 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
Ted Kremenekd45d59f2010-10-26 00:06:15 +0000466 B(b), includeGlobals(includeGlobals) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000467
Ted Kremenek5499b842010-03-10 16:32:56 +0000468 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000469
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000470 RegionCluster &AddToCluster(BindingKey K) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000471 const MemRegion *R = K.getRegion();
472 const MemRegion *baseR = R->getBaseRegion();
473 RegionCluster &C = getCluster(baseR);
474 C.push_back(K, BVC);
475 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000476 return C;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000477 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000478
Ted Kremenek5499b842010-03-10 16:32:56 +0000479 bool isVisited(const MemRegion *R) {
480 return (bool) Visited[&getCluster(R->getBaseRegion())];
481 }
482
483 RegionCluster& getCluster(const MemRegion *R) {
484 RegionCluster *&CRef = ClusterM[R];
485 if (!CRef) {
486 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
487 CRef = new (Mem) RegionCluster(BVC, 10);
488 }
489 return *CRef;
490 }
491
Ted Kremenekd45d59f2010-10-26 00:06:15 +0000492 void GenerateClusters() {
Ted Kremenek5499b842010-03-10 16:32:56 +0000493 // Scan the entire set of bindings and make the region clusters.
494 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000495 RegionCluster &C = AddToCluster(RI.getKey());
Ted Kremenek5499b842010-03-10 16:32:56 +0000496 if (const MemRegion *R = RI.getData().getAsRegion()) {
497 // Generate a cluster, but don't add the region to the cluster
498 // if there aren't any bindings.
499 getCluster(R->getBaseRegion());
500 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000501 if (includeGlobals) {
502 const MemRegion *R = RI.getKey().getRegion();
503 if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()))
504 AddToWorkList(R, C);
505 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000506 }
507 }
Ted Kremenek5499b842010-03-10 16:32:56 +0000508
509 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
510 if (unsigned &visited = Visited[&C])
511 return false;
512 else
513 visited = 1;
514
515 WL.push_back(std::make_pair(R, &C));
516 return true;
517 }
518
519 bool AddToWorkList(BindingKey K) {
520 return AddToWorkList(K.getRegion());
521 }
522
523 bool AddToWorkList(const MemRegion *R) {
524 const MemRegion *baseR = R->getBaseRegion();
525 return AddToWorkList(baseR, getCluster(baseR));
526 }
527
528 void RunWorkList() {
529 while (!WL.empty()) {
530 const MemRegion *baseR;
531 RegionCluster *C;
532 llvm::tie(baseR, C) = WL.back();
533 WL.pop_back();
534
535 // First visit the cluster.
536 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
537
Ted Kremenek75a2d942010-04-01 00:15:55 +0000538 // Next, visit the base region.
539 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
Ted Kremenek5499b842010-03-10 16:32:56 +0000540 }
541 }
542
543public:
544 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
545 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
Ted Kremenek75a2d942010-04-01 00:15:55 +0000546 void VisitBaseRegion(const MemRegion *baseR) {}
Ted Kremenek5499b842010-03-10 16:32:56 +0000547};
Ted Kremeneka4fab032010-03-10 07:19:59 +0000548}
549
550//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000551// Binding invalidation.
552//===----------------------------------------------------------------------===//
553
Zhongxing Xu13d50172009-10-11 08:08:02 +0000554void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
555 const MemRegion *R,
556 RegionStoreSubRegionMap &M) {
Ted Kremeneka4fab032010-03-10 07:19:59 +0000557
Ted Kremenekdf165012010-02-02 22:38:47 +0000558 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
559 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
560 I != E; ++I)
561 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000562
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000563 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000564}
565
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000566namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000567class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
568{
569 const Expr *Ex;
570 unsigned Count;
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000571 StoreManager::InvalidatedSymbols *IS;
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000572 StoreManager::InvalidatedRegions *Regions;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000573public:
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000574 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenek5499b842010-03-10 16:32:56 +0000575 GRStateManager &stateMgr,
576 RegionBindings b,
577 const Expr *ex, unsigned count,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000578 StoreManager::InvalidatedSymbols *is,
Ted Kremenekd45d59f2010-10-26 00:06:15 +0000579 StoreManager::InvalidatedRegions *r,
580 bool includeGlobals)
581 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b, includeGlobals),
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000582 Ex(ex), Count(count), IS(is), Regions(r) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000583
Ted Kremenek5499b842010-03-10 16:32:56 +0000584 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek75a2d942010-04-01 00:15:55 +0000585 void VisitBaseRegion(const MemRegion *baseR);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000586
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000587private:
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000588 void VisitBinding(SVal V);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000589};
Ted Kremenek5b290652010-02-03 04:16:00 +0000590}
591
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000592void InvalidateRegionsWorker::VisitBinding(SVal V) {
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000593 // A symbol? Mark it touched by the invalidation.
594 if (IS)
595 if (SymbolRef Sym = V.getAsSymbol())
596 IS->insert(Sym);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000597
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000598 if (const MemRegion *R = V.getAsRegion()) {
599 AddToWorkList(R);
600 return;
601 }
602
603 // Is it a LazyCompoundVal? All references get invalidated as well.
604 if (const nonloc::LazyCompoundVal *LCS =
605 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
606
607 const MemRegion *LazyR = LCS->getRegion();
608 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
609
610 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +0000611 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
612 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000613 VisitBinding(RI.getData());
614 }
615
616 return;
617 }
618}
619
Ted Kremenek5499b842010-03-10 16:32:56 +0000620void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
621 BindingKey *I, BindingKey *E) {
622 for ( ; I != E; ++I) {
623 // Get the old binding. Is it a region? If so, add it to the worklist.
624 const BindingKey &K = *I;
625 if (const SVal *V = RM.Lookup(B, K))
626 VisitBinding(*V);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000627
Ted Kremenek5499b842010-03-10 16:32:56 +0000628 B = RM.Remove(B, K);
629 }
630}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000631
Ted Kremenek75a2d942010-04-01 00:15:55 +0000632void InvalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000633 if (IS) {
634 // Symbolic region? Mark that symbol touched by the invalidation.
635 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
636 IS->insert(SR->getSymbol());
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000637 }
638
Ted Kremenek5499b842010-03-10 16:32:56 +0000639 // BlockDataRegion? If so, invalidate captured variables that are passed
640 // by reference.
641 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
642 for (BlockDataRegion::referenced_vars_iterator
643 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
644 BI != BE; ++BI) {
645 const VarRegion *VR = *BI;
646 const VarDecl *VD = VR->getDecl();
647 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
648 AddToWorkList(VR);
649 }
650 return;
651 }
652
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000653 // Otherwise, we have a normal data region. Record that we touched the region.
654 if (Regions)
655 Regions->push_back(baseR);
656
Ted Kremenek5499b842010-03-10 16:32:56 +0000657 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
658 // Invalidate the region by setting its default value to
659 // conjured symbol. The type of the symbol is irrelavant.
660 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 (!baseR->isBoundable())
667 return;
668
669 const TypedRegion *TR = cast<TypedRegion>(baseR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000670 QualType T = TR->getValueType();
Ted Kremenek5499b842010-03-10 16:32:56 +0000671
672 // Invalidate the binding.
Zhongxing Xu61453402010-08-21 06:51:45 +0000673 if (T->isStructureType()) {
Zhongxing Xu0b46b1b2010-08-21 06:26:59 +0000674 // Invalidate the region by setting its default value to
675 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek5499b842010-03-10 16:32:56 +0000676 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
677 Count);
678 B = RM.Add(B, baseR, BindingKey::Default, V);
679 return;
680 }
681
682 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
683 // Set the default value of the array to conjured symbol.
684 DefinedOrUnknownSVal V =
685 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
686 B = RM.Add(B, baseR, BindingKey::Default, V);
687 return;
688 }
Ted Kremenekc1143e52010-10-26 00:06:17 +0000689
690 if (includeGlobals &&
691 isa<NonStaticGlobalSpaceRegion>(baseR->getMemorySpace())) {
692 // If the region is a global and we are invalidating all globals,
693 // just erase the entry. This causes all globals to be lazily
694 // symbolicated from the same base symbol.
695 B = RM.Remove(B, baseR);
696 return;
697 }
698
Ted Kremenek5499b842010-03-10 16:32:56 +0000699
700 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
701 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
702 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000703}
704
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000705Store RegionStoreManager::InvalidateRegions(Store store,
706 const MemRegion * const *I,
707 const MemRegion * const *E,
708 const Expr *Ex, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000709 InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000710 bool invalidateGlobals,
711 InvalidatedRegions *Regions) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000712 InvalidateRegionsWorker W(*this, StateMgr,
713 RegionStoreManager::GetRegionBindings(store),
Ted Kremenekd45d59f2010-10-26 00:06:15 +0000714 Ex, Count, IS, Regions, invalidateGlobals);
Ted Kremenek5499b842010-03-10 16:32:56 +0000715
716 // Scan the bindings and generate the clusters.
Ted Kremenekd45d59f2010-10-26 00:06:15 +0000717 W.GenerateClusters();
Ted Kremenek5499b842010-03-10 16:32:56 +0000718
719 // Add I .. E to the worklist.
720 for ( ; I != E; ++I)
721 W.AddToWorkList(*I);
722
723 W.RunWorkList();
724
725 // Return the new bindings.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000726 RegionBindings B = W.getRegionBindings();
727
728 if (invalidateGlobals) {
729 // Bind the non-static globals memory space to a new symbol that we will
730 // use to derive the bindings for all non-static globals.
731 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion();
732 SVal V =
733 ValMgr.getConjuredSymbolVal(/* SymbolTag = */ (void*) GS, Ex,
734 /* symbol type, doesn't matter */ Ctx.IntTy,
735 Count);
736 B = Add(B, BindingKey::Make(GS, BindingKey::Default), V);
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000737
738 // Even if there are no bindings in the global scope, we still need to
739 // record that we touched it.
740 if (Regions)
741 Regions->push_back(GS);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000742 }
743
744 return B.getRoot();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000745}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000746
Ted Kremenek9af46f52009-06-16 22:36:44 +0000747//===----------------------------------------------------------------------===//
748// Extents for regions.
749//===----------------------------------------------------------------------===//
750
Zhongxing Xue884ff82009-11-12 02:48:32 +0000751DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000752 const MemRegion *R,
753 QualType EleTy) {
Jordy Rose32f26562010-07-04 00:00:41 +0000754 SVal Size = cast<SubRegion>(R)->getExtent(ValMgr);
755 SValuator &SVator = ValMgr.getSValuator();
756 const llvm::APSInt *SizeInt = SVator.getKnownValue(state, Size);
757 if (!SizeInt)
758 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Jordy Rose32f26562010-07-04 00:00:41 +0000760 CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
Ted Kremenek555c77a2010-09-14 23:08:34 +0000761
762 if (Ctx.getAsVariableArrayType(EleTy)) {
763 // FIXME: We need to track extra state to properly record the size
764 // of VLAs. Returning UnknownVal here, however, is a stop-gap so that
765 // we don't have a divide-by-zero below.
766 return UnknownVal();
767 }
768
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000769 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Jordy Rose32f26562010-07-04 00:00:41 +0000771 // If a variable is reinterpreted as a type that doesn't fit into a larger
772 // type evenly, round it down.
773 // This is a signed value, since it's used in arithmetic with signed indices.
774 return ValMgr.makeIntVal(RegionSize / EleSize, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000775}
776
Ted Kremenek9af46f52009-06-16 22:36:44 +0000777//===----------------------------------------------------------------------===//
778// Location and region casting.
779//===----------------------------------------------------------------------===//
780
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000781/// ArrayToPointer - Emulates the "decay" of an array to a pointer
782/// type. 'Array' represents the lvalue of the array being decayed
783/// to a pointer, and the returned SVal represents the decayed
784/// version of that lvalue (i.e., a pointer to the first element of
785/// the array). This is called by GRExprEngine when evaluating casts
786/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000787SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000788 if (!isa<loc::MemRegionVal>(Array))
789 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenekabb042f2008-12-13 19:24:37 +0000791 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
792 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000794 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000795 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000797 // Strip off typedefs from the ArrayRegion's ValueType.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000798 QualType T = ArrayR->getValueType().getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000799 ArrayType *AT = cast<ArrayType>(T);
800 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremenek02282ac2010-09-15 03:13:30 +0000802 NonLoc ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000803 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000804}
805
Ted Kremenek9af46f52009-06-16 22:36:44 +0000806//===----------------------------------------------------------------------===//
807// Pointer arithmetic.
808//===----------------------------------------------------------------------===//
809
Zhongxing Xu461147f2010-02-05 05:24:20 +0000810SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000811 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000812 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000813 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000814 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000815
Jordy Roseeac4a002010-06-28 08:26:15 +0000816 // Special case for zero RHS.
817 if (R.isZeroConstant()) {
818 switch (Op) {
819 default:
820 // Handle it normally.
821 break;
John McCall2de56d12010-08-25 11:45:40 +0000822 case BO_Add:
823 case BO_Sub:
Jordy Roseeac4a002010-06-28 08:26:15 +0000824 // FIXME: does this need to be casted to match resultTy?
825 return L;
826 }
827 }
828
Zhongxing Xua1718c72009-04-03 07:33:13 +0000829 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000830 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000831
Ted Kremenek3bccf082009-07-11 00:58:27 +0000832 switch (MR->getKind()) {
833 case MemRegion::SymbolicRegionKind: {
834 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000835 SymbolRef Sym = SR->getSymbol();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000836 QualType T = Sym->getType(Ctx);
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000837 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000839 if (const PointerType *PT = T->getAs<PointerType>())
840 EleTy = PT->getPointeeType();
841 else
John McCall183700f2009-09-21 23:43:11 +0000842 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Ted Kremenek02282ac2010-09-15 03:13:30 +0000844 const NonLoc &ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000845 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000846 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000847 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000848 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000849 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000850 QualType EleTy = Ctx.CharTy; // Create an ElementRegion of bytes.
Ted Kremenek02282ac2010-09-15 03:13:30 +0000851 NonLoc ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000852 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000853 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000854 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000855
Ted Kremenek3bccf082009-07-11 00:58:27 +0000856 case MemRegion::ElementRegionKind: {
857 ER = cast<ElementRegion>(MR);
858 break;
859 }
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Ted Kremenek3bccf082009-07-11 00:58:27 +0000861 // Not yet handled.
862 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000863 case MemRegion::StringRegionKind: {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000864
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000865 }
866 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000867 case MemRegion::CompoundLiteralRegionKind:
868 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000869 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000870 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000871 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000873 case MemRegion::FunctionTextRegionKind:
874 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000875 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000876 // Technically this can happen if people do funny things with casts.
877 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenekde0d2632010-01-05 02:18:06 +0000879 case MemRegion::CXXThisRegionKind:
880 assert(0 &&
881 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000882 case MemRegion::GenericMemSpaceRegionKind:
883 case MemRegion::StackLocalsSpaceRegionKind:
884 case MemRegion::StackArgumentsSpaceRegionKind:
885 case MemRegion::HeapSpaceRegionKind:
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000886 case MemRegion::NonStaticGlobalSpaceRegionKind:
887 case MemRegion::StaticGlobalSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000888 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000889 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
890 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000891 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000892
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000893 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000894 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000895
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000896 // For now, only support:
897 // (a) concrete integer indices that can easily be resolved
898 // (b) 0 + symbolic index
899 if (Base) {
900 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
901 // FIXME: Should use SValuator here.
902 SVal NewIdx =
903 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000904 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenek02282ac2010-09-15 03:13:30 +0000905
906 if (!isa<NonLoc>(NewIdx))
907 return UnknownVal();
908
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000909 const MemRegion* NewER =
Ted Kremenek02282ac2010-09-15 03:13:30 +0000910 MRMgr.getElementRegion(ER->getElementType(), cast<NonLoc>(NewIdx),
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000911 ER->getSuperRegion(), Ctx);
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000912 return ValMgr.makeLoc(NewER);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000913 }
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000914 if (0 == Base->getValue()) {
915 const MemRegion* NewER =
916 MRMgr.getElementRegion(ER->getElementType(), R,
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000917 ER->getSuperRegion(), Ctx);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000918 return ValMgr.makeLoc(NewER);
919 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Ted Kremenek5dc27462009-03-03 02:51:43 +0000922 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000923}
924
Ted Kremenek9af46f52009-06-16 22:36:44 +0000925//===----------------------------------------------------------------------===//
926// Loading values from regions.
927//===----------------------------------------------------------------------===//
928
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000929Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Zhongxing Xubdfa85f2010-05-29 06:23:24 +0000930 const MemRegion *R) {
Zhongxing Xu42c67bf2010-05-29 06:49:04 +0000931
932 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
933 return *V;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000934
Zhongxing Xu13d50172009-10-11 08:08:02 +0000935 return Optional<SVal>();
936}
937
938Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000939 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000940 if (R->isBoundable())
941 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +0000942 if (TR->getValueType()->isUnionType())
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000943 return UnknownVal();
944
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000945 if (const SVal *V = Lookup(B, R, BindingKey::Default))
946 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +0000947
948 return Optional<SVal>();
949}
950
Zhongxing Xu576bb922010-02-05 03:01:53 +0000951SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000952 assert(!isa<UnknownVal>(L) && "location unknown");
953 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000954
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000955 // FIXME: Is this even possible? Shouldn't this be treated as a null
956 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000957 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000958 return UndefinedVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000959
Ted Kremenek67f28532009-06-17 22:02:04 +0000960 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000961
Tom Care7b050302010-06-25 18:22:31 +0000962 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR)) {
963 if (T.isNull()) {
964 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000965 T = SR->getSymbol()->getType(Ctx);
Tom Care7b050302010-06-25 18:22:31 +0000966 }
Zhongxing Xu81491852010-02-08 08:43:02 +0000967 MR = GetElementZeroRegion(MR, T);
Tom Care7b050302010-06-25 18:22:31 +0000968 }
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000970 if (isa<CodeTextRegion>(MR)) {
971 assert(0 && "Why load from a code text region?");
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000972 return UnknownVal();
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000973 }
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000975 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
976 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000977 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000978 QualType RTy = R->getValueType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000979
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000980 // FIXME: We should eventually handle funny addressing. e.g.:
981 //
982 // int x = ...;
983 // int *p = &x;
984 // char *q = (char*) p;
985 // char c = *q; // returns the first byte of 'x'.
986 //
987 // Such funny addressing will occur due to layering of regions.
988
Douglas Gregorfb87b892010-04-26 21:31:17 +0000989 if (RTy->isStructureOrClassType())
Zhongxing Xu576bb922010-02-05 03:01:53 +0000990 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000992 // FIXME: Handle unions.
993 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000994 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000995
996 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +0000997 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000998
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000999 // FIXME: handle Vector types.
1000 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001001 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001002
1003 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +00001004 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001005
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001006 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1007 // FIXME: Here we actually perform an implicit conversion from the loaded
1008 // value to the element type. Eventually we want to compose these values
1009 // more intelligently. For example, an 'element' can encompass multiple
1010 // bound regions (e.g., several bound bytes), or could be a subset of
1011 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001012 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001013 }
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001015 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1016 // FIXME: Here we actually perform an implicit conversion from the loaded
1017 // value to the ivar type. What we should model is stores to ivars
1018 // that blow past the extent of the ivar. If the address of the ivar is
1019 // reinterpretted, it is possible we stored a different value that could
1020 // fit within the ivar. Either we need to cast these when storing them
1021 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001022 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001025 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1026 // FIXME: Here we actually perform an implicit conversion from the loaded
1027 // value to the variable type. What we should model is stores to variables
1028 // that blow past the extent of the variable. If the address of the
1029 // variable is reinterpretted, it is possible we stored a different value
1030 // that could fit within the variable. Either we need to cast these when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001031 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001032 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001033 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001034
Zhongxing Xu576bb922010-02-05 03:01:53 +00001035 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001036 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001037
1038 // Check if the region has a binding.
1039 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001040 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001041
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001042 // The location does not have a bound value. This means that it has
1043 // the value it had upon its creation and/or entry to the analyzed
1044 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001045 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001046 // All stack variables are considered to have undefined values
1047 // upon creation. All heap allocated blocks are considered to
1048 // have undefined values as well unless they are explicitly bound
1049 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001050 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001051 }
1052
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001053 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001054 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001055}
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001057std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001058RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001059 if (Optional<SVal> OV = getDirectBinding(B, R))
1060 if (const nonloc::LazyCompoundVal *V =
1061 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001062 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001064 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001065 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001066 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001068 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001069 return std::make_pair(X.first,
1070 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001071 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001072 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001073 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001074 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001076 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001077 return std::make_pair(X.first,
1078 MRMgr.getFieldRegionWithSuper(FR, X.second));
1079 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001080 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xudcbcbdc2010-02-10 02:02:10 +00001081 // possible for a valid lazy binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001082 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001083}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001084
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001085SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001086 const ElementRegion* R) {
1087 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001088 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001089 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001090 return *V;
1091
Ted Kremenek921109a2009-07-01 23:19:52 +00001092 const MemRegion* superR = R->getSuperRegion();
1093
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001094 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001095 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001096 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001097 // an integer, e.g., *((unsigned int*)"hello")
Zhongxing Xu018220c2010-08-11 06:10:55 +00001098 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001099 if (T != Ctx.getCanonicalType(R->getElementType()))
1100 return UnknownVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001101
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001102 const StringLiteral *Str = StrR->getStringLiteral();
1103 SVal Idx = R->getIndex();
1104 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1105 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001106 int64_t byteLength = Str->getByteLength();
Jordy Rose167cc372010-07-29 06:40:33 +00001107 // Technically, only i == byteLength is guaranteed to be null.
1108 // However, such overflows should be caught before reaching this point;
1109 // the only time such an access would be made is if a string literal was
1110 // used to initialize a larger array.
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +00001111 char c = (i >= byteLength) ? '\0' : Str->getString()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001112 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001113 }
1114 }
Ted Kremenek1e4a32a2010-09-01 23:00:46 +00001115
1116 // Check for loads from a code text region. For such loads, just give up.
Ted Kremenek08140f92010-09-02 00:34:30 +00001117 if (isa<CodeTextRegion>(superR))
Ted Kremenek1e4a32a2010-09-01 23:00:46 +00001118 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Ted Kremeneka709b872010-05-31 01:22:04 +00001120 // Handle the case where we are indexing into a larger scalar object.
1121 // For example, this handles:
1122 // int x = ...
1123 // char *y = &x;
1124 // return *y;
1125 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001126 const RegionRawOffset &O = R->getAsArrayOffset();
Ted Kremeneka709b872010-05-31 01:22:04 +00001127 if (const TypedRegion *baseR = dyn_cast_or_null<TypedRegion>(O.getRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001128 QualType baseT = baseR->getValueType();
Ted Kremeneka709b872010-05-31 01:22:04 +00001129 if (baseT->isScalarType()) {
1130 QualType elemT = R->getElementType();
1131 if (elemT->isScalarType()) {
1132 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1133 if (const Optional<SVal> &V = getDirectBinding(B, superR)) {
1134 if (SymbolRef parentSym = V->getAsSymbol())
1135 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001136
Ted Kremeneka709b872010-05-31 01:22:04 +00001137 if (V->isUnknownOrUndef())
1138 return *V;
1139 // Other cases: give up. We are indexing into a larger object
1140 // that has some value, but we don't know how to handle that yet.
1141 return UnknownVal();
1142 }
1143 }
1144 }
Zhongxing Xu42c67bf2010-05-29 06:49:04 +00001145 }
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001146 }
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001147 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001148}
1149
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001150SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001151 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001152
1153 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001154 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001155 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001156 return *V;
1157
Zhongxing Xu018220c2010-08-11 06:10:55 +00001158 QualType Ty = R->getValueType();
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001159 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001160}
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001162Optional<SVal>
1163RegionStoreManager::RetrieveDerivedDefaultValue(RegionBindings B,
1164 const MemRegion *superR,
1165 const TypedRegion *R,
1166 QualType Ty) {
1167
1168 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
1169 if (SymbolRef parentSym = D->getAsSymbol())
1170 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1171
1172 if (D->isZeroConstant())
1173 return ValMgr.makeZeroVal(Ty);
1174
1175 if (D->isUnknownOrUndef())
1176 return *D;
1177
1178 assert(0 && "Unknown default value");
1179 }
1180
1181 return Optional<SVal>();
1182}
1183
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001184SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001185 const TypedRegion *R,
1186 QualType Ty,
1187 const MemRegion *superR) {
1188
Mike Stump1eb44332009-09-09 15:08:12 +00001189 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001190 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001192 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001194 while (superR) {
Ted Kremenekc1143e52010-10-26 00:06:17 +00001195 if (const Optional<SVal> &D =
1196 RetrieveDerivedDefaultValue(B, superR, R, Ty))
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001197 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001199 // If our super region is a field or element itself, walk up the region
1200 // hierarchy to see if there is a default value installed in an ancestor.
Ted Kremenekc1143e52010-10-26 00:06:17 +00001201 if (const SubRegion *SR = dyn_cast<SubRegion>(superR)) {
1202 superR = SR->getSuperRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001203 continue;
1204 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001205 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001206 }
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001208 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001209 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001210 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001211 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001213 if (lazyBindingRegion) {
1214 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1215 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001216 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001217 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001218 }
1219
Ted Kremenekde0d2632010-01-05 02:18:06 +00001220 if (R->hasStackNonParametersStorage()) {
Ted Kremenek41be9672010-09-01 23:27:26 +00001221 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001222 // Currently we don't reason specially about Clang-style vectors. Check
1223 // if superR is a vector and if so return Unknown.
1224 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001225 if (typedSuperR->getValueType()->isVectorType())
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001226 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001227 }
Ted Kremenek41be9672010-09-01 23:27:26 +00001228
1229 // FIXME: We also need to take ElementRegions with symbolic indexes into
1230 // account.
1231 if (!ER->getIndex().isConstant())
1232 return UnknownVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001233 }
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001235 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001236 }
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001238 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001239 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001240}
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Zhongxing Xu576bb922010-02-05 03:01:53 +00001242SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001243
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001244 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001245 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001246
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001247 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001248 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001250 const MemRegion *superR = R->getSuperRegion();
1251
Ted Kremenekab22ee92009-10-20 01:20:57 +00001252 // Check if the super region has a default binding.
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001253 if (const Optional<SVal> &V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001254 if (SymbolRef parentSym = V->getAsSymbol())
1255 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001257 // Other cases: give up.
1258 return UnknownVal();
1259 }
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Zhongxing Xu576bb922010-02-05 03:01:53 +00001261 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001262}
1263
Zhongxing Xu576bb922010-02-05 03:01:53 +00001264SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Ted Kremenek9031dd72009-07-21 00:12:07 +00001266 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001267 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001269 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001270 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Ted Kremenek9031dd72009-07-21 00:12:07 +00001272 // Lazily derive a value for the VarRegion.
1273 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001274 QualType T = VD->getType();
1275 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001276
1277 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek4dc15662010-02-06 03:57:59 +00001278 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu14d23282010-03-01 06:56:52 +00001279 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Ted Kremenek4dc15662010-02-06 03:57:59 +00001281 if (isa<GlobalsSpaceRegion>(MS)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001282 if (isa<NonStaticGlobalSpaceRegion>(MS)) {
Ted Kremenek4552ff02010-03-30 20:31:04 +00001283 // Is 'VD' declared constant? If so, retrieve the constant value.
1284 QualType CT = Ctx.getCanonicalType(T);
1285 if (CT.isConstQualified()) {
1286 const Expr *Init = VD->getInit();
1287 // Do the null check first, as we want to call 'IgnoreParenCasts'.
1288 if (Init)
1289 if (const IntegerLiteral *IL =
1290 dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) {
1291 const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL);
1292 return ValMgr.getSValuator().EvalCast(V, Init->getType(),
1293 IL->getType());
1294 }
1295 }
1296
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001297 if (const Optional<SVal> &V = RetrieveDerivedDefaultValue(B, MS, R, CT))
1298 return V.getValue();
1299
Zhongxing Xu14d23282010-03-01 06:56:52 +00001300 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek4552ff02010-03-30 20:31:04 +00001301 }
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Ted Kremenek4dc15662010-02-06 03:57:59 +00001303 if (T->isIntegerType())
1304 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001305 if (T->isPointerType())
1306 return ValMgr.makeNull();
1307
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001308 return UnknownVal();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001309 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001310
Ted Kremenek9031dd72009-07-21 00:12:07 +00001311 return UndefinedVal();
1312}
1313
Zhongxing Xu576bb922010-02-05 03:01:53 +00001314SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001315 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001316 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001317}
1318
Zhongxing Xu576bb922010-02-05 03:01:53 +00001319SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001320 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001321 assert(T->isStructureOrClassType());
Zhongxing Xu576bb922010-02-05 03:01:53 +00001322 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001323}
1324
Zhongxing Xu576bb922010-02-05 03:01:53 +00001325SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Ted Kremenek091cbbd2010-09-14 23:29:38 +00001326 assert(Ctx.getAsConstantArrayType(R->getValueType()));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001327 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001328}
1329
Ted Kremenek9af46f52009-06-16 22:36:44 +00001330//===----------------------------------------------------------------------===//
1331// Binding values to regions.
1332//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001333
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001334Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001335 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001336 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001337 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Ted Kremenek0964a062009-01-21 06:57:53 +00001339 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001340}
1341
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001342Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001343 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001344 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001345
Ted Kremenek9af46f52009-06-16 22:36:44 +00001346 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001347 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Ted Kremenek9af46f52009-06-16 22:36:44 +00001349 // Check if the region is a struct region.
1350 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +00001351 if (TR->getValueType()->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001352 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001354 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1355 if (ER->getIndex().isZeroConstant()) {
1356 if (const TypedRegion *superR =
1357 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001358 QualType superTy = superR->getValueType();
Ted Kremenek69181a82009-09-21 22:58:52 +00001359 // For now, just invalidate the fields of the struct/union/class.
Zhongxing Xu81fa4ec2010-08-21 11:03:37 +00001360 // This is for test rdar_test_7185607 in misc-ps-region-store.m.
Ted Kremenek69181a82009-09-21 22:58:52 +00001361 // FIXME: Precisely handle the fields of the record.
Jordy Rose58f8b202010-08-05 03:28:45 +00001362 if (superTy->isStructureOrClassType())
1363 return KillStruct(store, superR, UnknownVal());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001364 }
1365 }
1366 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001367 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1368 // Binding directly to a symbolic region should be treated as binding
1369 // to element 0.
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001370 QualType T = SR->getSymbol()->getType(Ctx);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001371
Ted Kremenek852274d2009-12-16 03:18:58 +00001372 // FIXME: Is this the right way to handle symbols that are references?
1373 if (const PointerType *PT = T->getAs<PointerType>())
1374 T = PT->getPointeeType();
1375 else
1376 T = T->getAs<ReferenceType>()->getPointeeType();
1377
Ted Kremenek0954cde2009-09-24 04:11:44 +00001378 R = GetElementZeroRegion(SR, T);
1379 }
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001381 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001382 RegionBindings B = GetRegionBindings(store);
1383 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001384}
1385
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001386Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001387 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001388
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001389 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001390
Ted Kremenek0964a062009-01-21 06:57:53 +00001391 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001392 return BindArray(store, VR, InitVal);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001393 if (T->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001394 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001395
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001396 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001397}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001398
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001399// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001400Store RegionStoreManager::BindCompoundLiteral(Store store,
1401 const CompoundLiteralExpr *CL,
1402 const LocationContext *LC,
1403 SVal V) {
1404 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001405 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001406}
1407
Zhongxing Xua5ce9662010-06-01 03:01:33 +00001408
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001409Store RegionStoreManager::setImplicitDefaultValue(Store store,
1410 const MemRegion *R,
1411 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001412 RegionBindings B = GetRegionBindings(store);
1413 SVal V;
1414
1415 if (Loc::IsLocType(T))
1416 V = ValMgr.makeNull();
1417 else if (T->isIntegerType())
1418 V = ValMgr.makeZeroVal(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001419 else if (T->isStructureOrClassType() || T->isArrayType()) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001420 // Set the default value to a zero constant when it is a structure
1421 // or array. The type doesn't really matter.
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001422 V = ValMgr.makeZeroVal(Ctx.IntTy);
Ted Kremenek027e2662009-11-19 20:20:24 +00001423 }
1424 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001425 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001426 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001427
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001428 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001429}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001430
1431Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001432 SVal Init) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001433
Zhongxing Xu018220c2010-08-11 06:10:55 +00001434 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001435 QualType ElementTy = AT->getElementType();
Ted Kremenekfee90812010-01-26 23:51:00 +00001436 Optional<uint64_t> Size;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001437
Ted Kremenekfee90812010-01-26 23:51:00 +00001438 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1439 Size = CAT->getSize().getZExtValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001440
Jordy Rose167cc372010-07-29 06:40:33 +00001441 // Check if the init expr is a string literal.
1442 if (loc::MemRegionVal *MRV = dyn_cast<loc::MemRegionVal>(&Init)) {
1443 const StringRegion *S = cast<StringRegion>(MRV->getRegion());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001444
Jordy Rose167cc372010-07-29 06:40:33 +00001445 // Treat the string as a lazy compound value.
1446 nonloc::LazyCompoundVal LCV =
1447 cast<nonloc::LazyCompoundVal>(ValMgr.makeLazyCompoundVal(store, S));
1448 return CopyLazyBindings(LCV, store, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001449 }
1450
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001451 // Handle lazy compound values.
1452 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001453 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001454
1455 // Remaining case: explicit compound values.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001456
Ted Kremenek027e2662009-11-19 20:20:24 +00001457 if (Init.isUnknown())
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001458 return setImplicitDefaultValue(store, R, ElementTy);
1459
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001460 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001461 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001462 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Ted Kremenekfee90812010-01-26 23:51:00 +00001464 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001465 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001466 if (VI == VE)
1467 break;
1468
Ted Kremenek02282ac2010-09-15 03:13:30 +00001469 const NonLoc &Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001470 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001471
Douglas Gregorfb87b892010-04-26 21:31:17 +00001472 if (ElementTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001473 store = BindStruct(store, ER, *VI);
Jordy Rose59b6dca2010-08-20 01:05:59 +00001474 else if (ElementTy->isArrayType())
1475 store = BindArray(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001476 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001477 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001478 }
1479
Ted Kremenek027e2662009-11-19 20:20:24 +00001480 // If the init list is shorter than the array length, set the
1481 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001482 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001483 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001484
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001485 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001486}
1487
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001488Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1489 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Ted Kremenek67f28532009-06-17 22:02:04 +00001491 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001492 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Zhongxing Xu018220c2010-08-11 06:10:55 +00001494 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001495 assert(T->isStructureOrClassType());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001496
Ted Kremenek6217b802009-07-29 21:53:49 +00001497 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001498 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001499
1500 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001501 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001502
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001503 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001504 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001505 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001507 // We may get non-CompoundVal accidentally due to imprecise cast logic or
1508 // that we are binding symbolic struct value. Kill the field values, and if
1509 // the value is symbolic go and bind it as a "default" binding.
Ted Kremenek4af473c2010-08-17 23:29:06 +00001510 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V)) {
1511 SVal SV = isa<nonloc::SymbolVal>(V) ? V : UnknownVal();
1512 return KillStruct(store, R, SV);
1513 }
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001514
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001515 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001516 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001517
1518 RecordDecl::field_iterator FI, FE;
1519
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001520 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001521
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001522 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001523 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001524
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001525 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001526 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001527
Ted Kremenekcf549592009-09-22 21:19:14 +00001528 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001529 store = BindArray(store, FR, *VI);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001530 else if (FTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001531 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001532 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001533 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001534 }
1535
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001536 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001537 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001538 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001539 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001540 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001541 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001542
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001543 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001544}
1545
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001546Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R,
1547 SVal DefaultVal) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001548 RegionBindings B = GetRegionBindings(store);
1549 llvm::OwningPtr<RegionStoreSubRegionMap>
1550 SubRegions(getRegionStoreSubRegionMap(store));
1551 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001552
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001553 // Set the default value of the struct region to "unknown".
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001554 return Add(B, R, BindingKey::Default, DefaultVal).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001555}
1556
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001557Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1558 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001559
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001560 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001561 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001562
Mike Stump1eb44332009-09-09 15:08:12 +00001563 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001564 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001565
Mike Stump1eb44332009-09-09 15:08:12 +00001566 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001567 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001569 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1570 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001571 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001572}
1573
1574//===----------------------------------------------------------------------===//
1575// "Raw" retrievals and bindings.
1576//===----------------------------------------------------------------------===//
1577
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001578
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001579RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001580 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001581 return B;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001582 return RBFactory.Add(B, K, V);
1583}
1584
1585RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001586 BindingKey::Kind k, SVal V) {
1587 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001588}
1589
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001590const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001591 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001592 return NULL;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001593 return B.lookup(K);
1594}
1595
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001596const SVal *RegionStoreManager::Lookup(RegionBindings B,
1597 const MemRegion *R,
1598 BindingKey::Kind k) {
1599 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001600}
1601
1602RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001603 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001604 return B;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001605 return RBFactory.Remove(B, K);
1606}
1607
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001608RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1609 BindingKey::Kind k){
1610 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001611}
1612
Ted Kremenek9af46f52009-06-16 22:36:44 +00001613//===----------------------------------------------------------------------===//
1614// State pruning.
1615//===----------------------------------------------------------------------===//
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001616
Ted Kremenek5499b842010-03-10 16:32:56 +00001617namespace {
1618class RemoveDeadBindingsWorker :
1619 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1620 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1621 SymbolReaper &SymReaper;
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001622 const StackFrameContext *CurrentLCtx;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001623
Ted Kremenek5499b842010-03-10 16:32:56 +00001624public:
1625 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1626 RegionBindings b, SymbolReaper &symReaper,
Jordy Rose7dadf792010-07-01 20:09:55 +00001627 const StackFrameContext *LCtx)
Ted Kremenekd45d59f2010-10-26 00:06:15 +00001628 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b,
1629 /* includeGlobals = */ false),
Jordy Rose7dadf792010-07-01 20:09:55 +00001630 SymReaper(symReaper), CurrentLCtx(LCtx) {}
Ted Kremenek5499b842010-03-10 16:32:56 +00001631
1632 // Called by ClusterAnalysis.
1633 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1634 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek5499b842010-03-10 16:32:56 +00001635
Ted Kremenek75a2d942010-04-01 00:15:55 +00001636 void VisitBindingKey(BindingKey K);
Ted Kremenek5499b842010-03-10 16:32:56 +00001637 bool UpdatePostponed();
1638 void VisitBinding(SVal V);
1639};
1640}
1641
1642void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1643 RegionCluster &C) {
1644
1645 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
Jordy Rose7dadf792010-07-01 20:09:55 +00001646 if (SymReaper.isLive(VR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001647 AddToWorkList(baseR, C);
1648
1649 return;
1650 }
1651
1652 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1653 if (SymReaper.isLive(SR->getSymbol()))
1654 AddToWorkList(SR, C);
1655 else
1656 Postponed.push_back(SR);
1657
1658 return;
1659 }
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001660
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001661 if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
1662 AddToWorkList(baseR, C);
1663 return;
1664 }
1665
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001666 // CXXThisRegion in the current or parent location context is live.
1667 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001668 const StackArgumentsSpaceRegion *StackReg =
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001669 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
1670 const StackFrameContext *RegCtx = StackReg->getStackFrame();
1671 if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
1672 AddToWorkList(TR, C);
1673 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001674}
1675
1676void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1677 BindingKey *I, BindingKey *E) {
Ted Kremenek75a2d942010-04-01 00:15:55 +00001678 for ( ; I != E; ++I)
1679 VisitBindingKey(*I);
Ted Kremenek5499b842010-03-10 16:32:56 +00001680}
1681
1682void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1683 // Is it a LazyCompoundVal? All referenced regions are live as well.
1684 if (const nonloc::LazyCompoundVal *LCS =
1685 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1686
1687 const MemRegion *LazyR = LCS->getRegion();
1688 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1689 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +00001690 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
1691 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001692 VisitBinding(RI.getData());
1693 }
1694 return;
1695 }
1696
1697 // If V is a region, then add it to the worklist.
1698 if (const MemRegion *R = V.getAsRegion())
1699 AddToWorkList(R);
1700
1701 // Update the set of live symbols.
1702 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1703 SI!=SE;++SI)
1704 SymReaper.markLive(*SI);
1705}
1706
Ted Kremenek75a2d942010-04-01 00:15:55 +00001707void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) {
1708 const MemRegion *R = K.getRegion();
1709
Ted Kremenek5499b842010-03-10 16:32:56 +00001710 // Mark this region "live" by adding it to the worklist. This will cause
1711 // use to visit all regions in the cluster (if we haven't visited them
1712 // already).
Ted Kremenek75a2d942010-04-01 00:15:55 +00001713 if (AddToWorkList(R)) {
1714 // Mark the symbol for any live SymbolicRegion as "live". This means we
1715 // should continue to track that symbol.
1716 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1717 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek5499b842010-03-10 16:32:56 +00001718
Ted Kremenek75a2d942010-04-01 00:15:55 +00001719 // For BlockDataRegions, enqueue the VarRegions for variables marked
1720 // with __block (passed-by-reference).
1721 // via BlockDeclRefExprs.
1722 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1723 for (BlockDataRegion::referenced_vars_iterator
1724 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1725 RI != RE; ++RI) {
1726 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1727 AddToWorkList(*RI);
1728 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001729
Ted Kremenek75a2d942010-04-01 00:15:55 +00001730 // No possible data bindings on a BlockDataRegion.
1731 return;
Ted Kremenek5499b842010-03-10 16:32:56 +00001732 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001733 }
1734
Ted Kremenek75a2d942010-04-01 00:15:55 +00001735 // Visit the data binding for K.
1736 if (const SVal *V = RM.Lookup(B, K))
Ted Kremenek5499b842010-03-10 16:32:56 +00001737 VisitBinding(*V);
1738}
1739
1740bool RemoveDeadBindingsWorker::UpdatePostponed() {
1741 // See if any postponed SymbolicRegions are actually live now, after
1742 // having done a scan.
1743 bool changed = false;
1744
1745 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1746 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1747 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1748 if (SymReaper.isLive(SR->getSymbol())) {
1749 changed |= AddToWorkList(SR);
1750 *I = NULL;
1751 }
1752 }
1753 }
1754
1755 return changed;
1756}
1757
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001758Store RegionStoreManager::RemoveDeadBindings(Store store,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001759 const StackFrameContext *LCtx,
Zhongxing Xu72119c42010-02-05 05:34:29 +00001760 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001761 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001762{
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001763 RegionBindings B = GetRegionBindings(store);
Jordy Rose7dadf792010-07-01 20:09:55 +00001764 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
Ted Kremenek5499b842010-03-10 16:32:56 +00001765 W.GenerateClusters();
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Ted Kremenek5499b842010-03-10 16:32:56 +00001767 // Enqueue the region roots onto the worklist.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001768 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenek5499b842010-03-10 16:32:56 +00001769 E=RegionRoots.end(); I!=E; ++I)
1770 W.AddToWorkList(*I);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001771
Ted Kremenek5499b842010-03-10 16:32:56 +00001772 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001773
Ted Kremenek9af46f52009-06-16 22:36:44 +00001774 // We have now scanned the store, marking reachable regions and symbols
1775 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001776 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001777 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek5499b842010-03-10 16:32:56 +00001778 const BindingKey &K = I.getKey();
1779
Ted Kremenekb7118f72010-03-10 16:38:41 +00001780 // If the cluster has been visited, we know the region has been marked.
Ted Kremenek5499b842010-03-10 16:32:56 +00001781 if (W.isVisited(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001782 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Ted Kremenek5499b842010-03-10 16:32:56 +00001784 // Remove the dead entry.
1785 B = Remove(B, K);
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Ted Kremenek5499b842010-03-10 16:32:56 +00001787 // Mark all non-live symbols that this binding references as dead.
1788 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001789 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001791 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001792 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1793 for (; SI != SE; ++SI)
1794 SymReaper.maybeDead(*SI);
1795 }
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001796
1797 return B.getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001798}
1799
Ted Kremenek5499b842010-03-10 16:32:56 +00001800
Jordy Roseff59efd2010-08-03 20:44:35 +00001801Store RegionStoreManager::EnterStackFrame(const GRState *state,
1802 const StackFrameContext *frame) {
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001803 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001804 FunctionDecl::param_const_iterator PI = FD->param_begin();
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001805 Store store = state->getStore();
Zhongxing Xuc5063572010-03-16 13:14:16 +00001806
1807 if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) {
1808 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1809
1810 // Copy the arg expression value to the arg variables.
1811 for (; AI != AE; ++AI, ++PI) {
1812 SVal ArgVal = state->getSVal(*AI);
1813 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1814 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001815 } else if (const CXXConstructExpr *CE =
Zhongxing Xuc5063572010-03-16 13:14:16 +00001816 dyn_cast<CXXConstructExpr>(frame->getCallSite())) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001817 CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(),
Zhongxing Xuc5063572010-03-16 13:14:16 +00001818 AE = CE->arg_end();
1819
1820 // Copy the arg expression value to the arg variables.
1821 for (; AI != AE; ++AI, ++PI) {
1822 SVal ArgVal = state->getSVal(*AI);
1823 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1824 }
1825 } else
Jordy Roseff59efd2010-08-03 20:44:35 +00001826 llvm_unreachable("Unhandled call expression.");
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001827
Jordy Roseff59efd2010-08-03 20:44:35 +00001828 return store;
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001829}
1830
Ted Kremenek9af46f52009-06-16 22:36:44 +00001831//===----------------------------------------------------------------------===//
1832// Utility methods.
1833//===----------------------------------------------------------------------===//
1834
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001835void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001836 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001837 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001838 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Ted Kremenek451ac092009-08-06 04:50:20 +00001840 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001841 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001842}