blob: 7dd503c0f5694ecd5a7c99e0e2761330e00c4cc6 [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 isDefault() const { return P.getInt() == Default; }
51 bool isDirect() const { return P.getInt() == Direct; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000052
Ted Kremeneke393f4a2010-02-03 03:06:46 +000053 const MemRegion *getRegion() const { return P.getPointer(); }
54 uint64_t getOffset() const { return Offset; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000055
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000056 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000057 ID.AddPointer(P.getOpaqueValue());
58 ID.AddInteger(Offset);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000059 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000060
Ted Kremeneke393f4a2010-02-03 03:06:46 +000061 static BindingKey Make(const MemRegion *R, Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000062
Ted Kremeneke393f4a2010-02-03 03:06:46 +000063 bool operator<(const BindingKey &X) const {
64 if (P.getOpaqueValue() < X.P.getOpaqueValue())
65 return true;
66 if (P.getOpaqueValue() > X.P.getOpaqueValue())
67 return false;
68 return Offset < X.Offset;
69 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000070
Ted Kremeneke393f4a2010-02-03 03:06:46 +000071 bool operator==(const BindingKey &X) const {
72 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
73 Offset == X.Offset;
74 }
Jordy Rosee7011172010-08-16 01:15:17 +000075
Jordy Rosed5addb42010-08-16 20:53:01 +000076 bool isValid() const {
Jordy Rosee7011172010-08-16 01:15:17 +000077 return getRegion() != NULL;
78 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000079};
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000080} // end anonymous namespace
81
82namespace llvm {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000083 static inline
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000084 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000085 os << '(' << K.getRegion() << ',' << K.getOffset()
86 << ',' << (K.isDirect() ? "direct" : "default")
87 << ')';
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000088 return os;
89 }
90} // end llvm namespace
91
92//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +000093// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000094//===----------------------------------------------------------------------===//
95
Ted Kremeneke393f4a2010-02-03 03:06:46 +000096typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000097
Ted Kremenek50dc1b32008-12-24 01:05:03 +000098//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000099// Fine-grained control of RegionStoreManager.
100//===----------------------------------------------------------------------===//
101
102namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000103struct minimal_features_tag {};
104struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000106class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000107 bool SupportsFields;
108 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek9af46f52009-06-16 22:36:44 +0000110public:
111 RegionStoreFeatures(minimal_features_tag) :
112 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenek9af46f52009-06-16 22:36:44 +0000114 RegionStoreFeatures(maximal_features_tag) :
115 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Ted Kremenek9af46f52009-06-16 22:36:44 +0000117 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenek9af46f52009-06-16 22:36:44 +0000119 bool supportsFields() const { return SupportsFields; }
120 bool supportsRemaining() const { return SupportsRemaining; }
121};
122}
123
124//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000125// Utility functions.
126//===----------------------------------------------------------------------===//
127
128static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
129 if (ty->isAnyPointerType())
130 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000132 return ty->isIntegerType() && ty->isScalarType() &&
133 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
134}
135
136//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000137// Main RegionStore logic.
138//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000139
Zhongxing Xu17892752008-10-08 02:50:44 +0000140namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000142class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekdf165012010-02-02 22:38:47 +0000143public:
144 typedef llvm::ImmutableSet<const MemRegion*> Set;
145 typedef llvm::DenseMap<const MemRegion*, Set> Map;
146private:
147 Set::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000149public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000150 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000151 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000152
153 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000154 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000155 return true;
156 }
157
158 I->second = F.Add(I->second, SubRegion);
159 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000162 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek59e8f112009-03-03 01:35:36 +0000164 ~RegionStoreSubRegionMap() {}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000165
Ted Kremenekdf165012010-02-02 22:38:47 +0000166 const Set *getSubRegions(const MemRegion *Parent) const {
167 Map::const_iterator I = M.find(Parent);
168 return I == M.end() ? NULL : &I->second;
169 }
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremenek5dc27462009-03-03 02:51:43 +0000171 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000172 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000173
174 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000175 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenekdf165012010-02-02 22:38:47 +0000177 Set S = I->second;
178 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000179 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000180 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek5dc27462009-03-03 02:51:43 +0000183 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000186
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000187
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000188class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000189 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000190 RegionBindings::Factory RBFactory;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000191
Zhongxing Xu17892752008-10-08 02:50:44 +0000192public:
Mike Stump1eb44332009-09-09 15:08:12 +0000193 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000194 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000195 Features(f),
Ted Kremenek8928d412010-02-04 04:14:49 +0000196 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000197
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000198 SubRegionMap *getSubRegionMap(Store store) {
199 return getRegionStoreSubRegionMap(store);
200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Zhongxing Xu13d50172009-10-11 08:08:02 +0000202 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Zhongxing Xu13d50172009-10-11 08:08:02 +0000204 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
205 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000206 /// getDefaultBinding - Returns an SVal* representing an optional default
207 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000208 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000209
Ted Kremenek027e2662009-11-19 20:20:24 +0000210 /// setImplicitDefaultValue - Set the default binding for the provided
211 /// MemRegion to the value implicitly defined for compound literals when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000212 /// the value is not specified.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000213 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000215 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
216 /// type. 'Array' represents the lvalue of the array being decayed
217 /// to a pointer, and the returned SVal represents the decayed
218 /// version of that lvalue (i.e., a pointer to the first element of
219 /// the array). This is called by GRExprEngine when evaluating
220 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000221 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000222
Zhongxing Xu461147f2010-02-05 05:24:20 +0000223 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000224
Mike Stump1eb44332009-09-09 15:08:12 +0000225 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000226 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000227 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000228
Ted Kremenek67f28532009-06-17 22:02:04 +0000229 //===-------------------------------------------------------------------===//
230 // Binding values to regions.
231 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000232
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000233 Store InvalidateRegions(Store store,
234 const MemRegion * const *Begin,
235 const MemRegion * const *End,
236 const Expr *E, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000237 InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000238 bool invalidateGlobals,
239 InvalidatedRegions *Regions);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000241public: // Made public for helper classes.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000242
Zhongxing Xu13d50172009-10-11 08:08:02 +0000243 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000244 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000246 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
247
248 RegionBindings Add(RegionBindings B, const MemRegion *R,
249 BindingKey::Kind k, SVal V);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000250
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000251 const SVal *Lookup(RegionBindings B, BindingKey K);
252 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000253
254 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000255 RegionBindings Remove(RegionBindings B, const MemRegion *R,
256 BindingKey::Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000257
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000258 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
259 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000260 }
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000261
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000262 Store Remove(Store store, BindingKey K);
263
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000264public: // Part of public interface to class.
265
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000266 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000267
Zhongxing Xu54460092010-06-01 04:49:26 +0000268 // BindDefault is only used to initialize a region with a default value.
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000269 Store BindDefault(Store store, const MemRegion *R, SVal V) {
Zhongxing Xu54460092010-06-01 04:49:26 +0000270 RegionBindings B = GetRegionBindings(store);
271 assert(!Lookup(B, R, BindingKey::Default));
272 assert(!Lookup(B, R, BindingKey::Direct));
273 return Add(B, R, BindingKey::Default, V).getRoot();
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000274 }
275
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000276 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
277 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000279 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000280
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000281 Store BindDeclWithNoInit(Store store, const VarRegion *) {
282 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000283 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000284
Ted Kremenek67f28532009-06-17 22:02:04 +0000285 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000286 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000288 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
290 /// KillStruct - Set the entire struct to unknown.
Ted Kremenek281e9dc2010-07-29 00:28:47 +0000291 Store KillStruct(Store store, const TypedRegion* R, SVal DefaultVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000292
Ted Kremenek67f28532009-06-17 22:02:04 +0000293 Store Remove(Store store, Loc LV);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000294
Ted Kremenek67f28532009-06-17 22:02:04 +0000295
296 //===------------------------------------------------------------------===//
297 // Loading values from regions.
298 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenek67f28532009-06-17 22:02:04 +0000300 /// The high level logic for this method is this:
301 /// Retrieve (L)
302 /// if L has binding
303 /// return L's binding
304 /// else if L is in killset
305 /// return unknown
306 /// else
307 /// if L is on stack or heap
308 /// return undefined
309 /// else
310 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000311 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000312
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000313 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000314
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000315 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Zhongxing Xu576bb922010-02-05 03:01:53 +0000317 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Zhongxing Xu576bb922010-02-05 03:01:53 +0000319 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Zhongxing Xu576bb922010-02-05 03:01:53 +0000321 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000323 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000324 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Ted Kremenek67f28532009-06-17 22:02:04 +0000326 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000327 /// struct copy:
328 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000329 /// x = y;
330 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000331 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Zhongxing Xu576bb922010-02-05 03:01:53 +0000333 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000335 /// Used to lazily generate derived symbols for bindings that are defined
336 /// implicitly by default bindings in a super region.
337 Optional<SVal> RetrieveDerivedDefaultValue(RegionBindings B,
338 const MemRegion *superR,
339 const TypedRegion *R, QualType Ty);
340
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000341 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000342 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000343 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000345 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
346 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000347
348 //===------------------------------------------------------------------===//
349 // State pruning.
350 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenek67f28532009-06-17 22:02:04 +0000352 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
353 /// It returns a new Store with these values removed.
Zhongxing Xu5e4cb342010-08-15 12:45:09 +0000354 Store RemoveDeadBindings(Store store, const StackFrameContext *LCtx,
355 SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000356 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
357
Jordy Roseff59efd2010-08-03 20:44:35 +0000358 Store EnterStackFrame(const GRState *state, const StackFrameContext *frame);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000359
Ted Kremenek67f28532009-06-17 22:02:04 +0000360 //===------------------------------------------------------------------===//
361 // Region "extents".
362 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Jordy Rose32f26562010-07-04 00:00:41 +0000364 // FIXME: This method will soon be eliminated; see the note in Store.h.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000365 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000366 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000367
368 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000369 // Utility methods.
370 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek451ac092009-08-06 04:50:20 +0000372 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000373 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000374 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000375
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000376 void print(Store store, llvm::raw_ostream& Out, const char* nl,
377 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000378
379 void iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek0e9910f2010-06-17 00:24:42 +0000380 RegionBindings B = GetRegionBindings(store);
381 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
382 const BindingKey &K = I.getKey();
383 if (!K.isDirect())
384 continue;
385 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) {
386 // FIXME: Possibly incorporate the offset?
387 if (!f.HandleBinding(*this, store, R, I.getData()))
388 return;
389 }
390 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000391 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000392};
393
394} // end anonymous namespace
395
Ted Kremenek9af46f52009-06-16 22:36:44 +0000396//===----------------------------------------------------------------------===//
397// RegionStore creation.
398//===----------------------------------------------------------------------===//
399
400StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
401 RegionStoreFeatures F = maximal_features_tag();
402 return new RegionStoreManager(StMgr, F);
403}
404
405StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
406 RegionStoreFeatures F = minimal_features_tag();
407 F.enableFields(true);
408 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000409}
410
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000411void
412RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000413 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000414 const MemRegion *superR = R->getSuperRegion();
415 if (add(superR, R))
416 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000417 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000418}
419
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000420RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000421RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
422 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000423 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000425 llvm::SmallVector<const SubRegion*, 10> WL;
426
Ted Kremenek451ac092009-08-06 04:50:20 +0000427 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000428 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000429 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Mike Stump1eb44332009-09-09 15:08:12 +0000431 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000432 // don't have direct bindings but are super regions of those that do.
433 while (!WL.empty()) {
434 const SubRegion *R = WL.back();
435 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000436 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000437 }
438
Ted Kremenek14453bf2009-03-03 19:02:42 +0000439 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000440}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000441
Ted Kremenek9af46f52009-06-16 22:36:44 +0000442//===----------------------------------------------------------------------===//
Ted Kremeneka4fab032010-03-10 07:19:59 +0000443// Region Cluster analysis.
444//===----------------------------------------------------------------------===//
445
446namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000447template <typename DERIVED>
Ted Kremeneka4fab032010-03-10 07:19:59 +0000448class ClusterAnalysis {
449protected:
450 typedef BumpVector<BindingKey> RegionCluster;
451 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5499b842010-03-10 16:32:56 +0000452 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
453 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
454 WorkList;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000455
456 BumpVectorContext BVC;
457 ClusterMap ClusterM;
Ted Kremenek5499b842010-03-10 16:32:56 +0000458 WorkList WL;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000459
460 RegionStoreManager &RM;
461 ASTContext &Ctx;
462 ValueManager &ValMgr;
463
Ted Kremenek5499b842010-03-10 16:32:56 +0000464 RegionBindings B;
465
Ted Kremeneka4fab032010-03-10 07:19:59 +0000466public:
Ted Kremenek5499b842010-03-10 16:32:56 +0000467 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
468 RegionBindings b)
469 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
470 B(b) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000471
Ted Kremenek5499b842010-03-10 16:32:56 +0000472 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000473
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000474 RegionCluster &AddToCluster(BindingKey K) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000475 const MemRegion *R = K.getRegion();
476 const MemRegion *baseR = R->getBaseRegion();
477 RegionCluster &C = getCluster(baseR);
478 C.push_back(K, BVC);
479 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000480 return C;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000481 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000482
Ted Kremenek5499b842010-03-10 16:32:56 +0000483 bool isVisited(const MemRegion *R) {
484 return (bool) Visited[&getCluster(R->getBaseRegion())];
485 }
486
487 RegionCluster& getCluster(const MemRegion *R) {
488 RegionCluster *&CRef = ClusterM[R];
489 if (!CRef) {
490 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
491 CRef = new (Mem) RegionCluster(BVC, 10);
492 }
493 return *CRef;
494 }
495
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000496 void GenerateClusters(bool includeGlobals = false) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000497 // Scan the entire set of bindings and make the region clusters.
498 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000499 RegionCluster &C = AddToCluster(RI.getKey());
Ted Kremenek5499b842010-03-10 16:32:56 +0000500 if (const MemRegion *R = RI.getData().getAsRegion()) {
501 // Generate a cluster, but don't add the region to the cluster
502 // if there aren't any bindings.
503 getCluster(R->getBaseRegion());
504 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000505 if (includeGlobals) {
506 const MemRegion *R = RI.getKey().getRegion();
507 if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()))
508 AddToWorkList(R, C);
509 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000510 }
511 }
Ted Kremenek5499b842010-03-10 16:32:56 +0000512
513 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
514 if (unsigned &visited = Visited[&C])
515 return false;
516 else
517 visited = 1;
518
519 WL.push_back(std::make_pair(R, &C));
520 return true;
521 }
522
523 bool AddToWorkList(BindingKey K) {
524 return AddToWorkList(K.getRegion());
525 }
526
527 bool AddToWorkList(const MemRegion *R) {
528 const MemRegion *baseR = R->getBaseRegion();
529 return AddToWorkList(baseR, getCluster(baseR));
530 }
531
532 void RunWorkList() {
533 while (!WL.empty()) {
534 const MemRegion *baseR;
535 RegionCluster *C;
536 llvm::tie(baseR, C) = WL.back();
537 WL.pop_back();
538
539 // First visit the cluster.
540 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
541
Ted Kremenek75a2d942010-04-01 00:15:55 +0000542 // Next, visit the base region.
543 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
Ted Kremenek5499b842010-03-10 16:32:56 +0000544 }
545 }
546
547public:
548 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
549 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
Ted Kremenek75a2d942010-04-01 00:15:55 +0000550 void VisitBaseRegion(const MemRegion *baseR) {}
Ted Kremenek5499b842010-03-10 16:32:56 +0000551};
Ted Kremeneka4fab032010-03-10 07:19:59 +0000552}
553
554//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000555// Binding invalidation.
556//===----------------------------------------------------------------------===//
557
Zhongxing Xu13d50172009-10-11 08:08:02 +0000558void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
559 const MemRegion *R,
560 RegionStoreSubRegionMap &M) {
Ted Kremeneka4fab032010-03-10 07:19:59 +0000561
Ted Kremenekdf165012010-02-02 22:38:47 +0000562 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
563 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
564 I != E; ++I)
565 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000566
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000567 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000568}
569
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000570namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000571class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
572{
573 const Expr *Ex;
574 unsigned Count;
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000575 StoreManager::InvalidatedSymbols *IS;
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000576 StoreManager::InvalidatedRegions *Regions;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000577public:
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000578 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenek5499b842010-03-10 16:32:56 +0000579 GRStateManager &stateMgr,
580 RegionBindings b,
581 const Expr *ex, unsigned count,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000582 StoreManager::InvalidatedSymbols *is,
583 StoreManager::InvalidatedRegions *r)
Ted Kremenek5499b842010-03-10 16:32:56 +0000584 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000585 Ex(ex), Count(count), IS(is), Regions(r) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000586
Ted Kremenek5499b842010-03-10 16:32:56 +0000587 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek75a2d942010-04-01 00:15:55 +0000588 void VisitBaseRegion(const MemRegion *baseR);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000589
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000590private:
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000591 void VisitBinding(SVal V);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000592};
Ted Kremenek5b290652010-02-03 04:16:00 +0000593}
594
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000595void InvalidateRegionsWorker::VisitBinding(SVal V) {
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000596 // A symbol? Mark it touched by the invalidation.
597 if (IS)
598 if (SymbolRef Sym = V.getAsSymbol())
599 IS->insert(Sym);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000600
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000601 if (const MemRegion *R = V.getAsRegion()) {
602 AddToWorkList(R);
603 return;
604 }
605
606 // Is it a LazyCompoundVal? All references get invalidated as well.
607 if (const nonloc::LazyCompoundVal *LCS =
608 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
609
610 const MemRegion *LazyR = LCS->getRegion();
611 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
612
613 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +0000614 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
615 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000616 VisitBinding(RI.getData());
617 }
618
619 return;
620 }
621}
622
Ted Kremenek5499b842010-03-10 16:32:56 +0000623void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
624 BindingKey *I, BindingKey *E) {
625 for ( ; I != E; ++I) {
626 // Get the old binding. Is it a region? If so, add it to the worklist.
627 const BindingKey &K = *I;
628 if (const SVal *V = RM.Lookup(B, K))
629 VisitBinding(*V);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000630
Ted Kremenek5499b842010-03-10 16:32:56 +0000631 B = RM.Remove(B, K);
632 }
633}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000634
Ted Kremenek75a2d942010-04-01 00:15:55 +0000635void InvalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000636 if (IS) {
637 // Symbolic region? Mark that symbol touched by the invalidation.
638 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
639 IS->insert(SR->getSymbol());
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000640 }
641
Ted Kremenek5499b842010-03-10 16:32:56 +0000642 // BlockDataRegion? If so, invalidate captured variables that are passed
643 // by reference.
644 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
645 for (BlockDataRegion::referenced_vars_iterator
646 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
647 BI != BE; ++BI) {
648 const VarRegion *VR = *BI;
649 const VarDecl *VD = VR->getDecl();
650 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
651 AddToWorkList(VR);
652 }
653 return;
654 }
655
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000656 // Otherwise, we have a normal data region. Record that we touched the region.
657 if (Regions)
658 Regions->push_back(baseR);
659
Ted Kremenek5499b842010-03-10 16:32:56 +0000660 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
661 // Invalidate the region by setting its default value to
662 // conjured symbol. The type of the symbol is irrelavant.
663 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
664 Count);
665 B = RM.Add(B, baseR, BindingKey::Default, V);
666 return;
667 }
668
669 if (!baseR->isBoundable())
670 return;
671
672 const TypedRegion *TR = cast<TypedRegion>(baseR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000673 QualType T = TR->getValueType();
Ted Kremenek5499b842010-03-10 16:32:56 +0000674
675 // Invalidate the binding.
676 if (const RecordType *RT = T->getAsStructureType()) {
677 const RecordDecl *RD = RT->getDecl()->getDefinition();
678 // No record definition. There is nothing we can do.
679 if (!RD) {
680 B = RM.Remove(B, baseR);
681 return;
682 }
683
684 // Invalidate the region by setting its default value to
685 // conjured symbol. The type of the symbol is irrelavant.
686 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
687 Count);
688 B = RM.Add(B, baseR, BindingKey::Default, V);
689 return;
690 }
691
692 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
693 // Set the default value of the array to conjured symbol.
694 DefinedOrUnknownSVal V =
695 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
696 B = RM.Add(B, baseR, BindingKey::Default, V);
697 return;
698 }
699
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),
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000714 Ex, Count, IS, Regions);
Ted Kremenek5499b842010-03-10 16:32:56 +0000715
716 // Scan the bindings and generate the clusters.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000717 W.GenerateClusters(invalidateGlobals);
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());
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000761 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Jordy Rose32f26562010-07-04 00:00:41 +0000763 // If a variable is reinterpreted as a type that doesn't fit into a larger
764 // type evenly, round it down.
765 // This is a signed value, since it's used in arithmetic with signed indices.
766 return ValMgr.makeIntVal(RegionSize / EleSize, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000767}
768
Ted Kremenek9af46f52009-06-16 22:36:44 +0000769//===----------------------------------------------------------------------===//
770// Location and region casting.
771//===----------------------------------------------------------------------===//
772
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000773/// ArrayToPointer - Emulates the "decay" of an array to a pointer
774/// type. 'Array' represents the lvalue of the array being decayed
775/// to a pointer, and the returned SVal represents the decayed
776/// version of that lvalue (i.e., a pointer to the first element of
777/// the array). This is called by GRExprEngine when evaluating casts
778/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000779SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000780 if (!isa<loc::MemRegionVal>(Array))
781 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenekabb042f2008-12-13 19:24:37 +0000783 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
784 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000786 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000787 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000789 // Strip off typedefs from the ArrayRegion's ValueType.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000790 QualType T = ArrayR->getValueType().getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000791 ArrayType *AT = cast<ArrayType>(T);
792 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremenek75185b52009-07-16 00:00:11 +0000794 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000795 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000796}
797
Ted Kremenek9af46f52009-06-16 22:36:44 +0000798//===----------------------------------------------------------------------===//
799// Pointer arithmetic.
800//===----------------------------------------------------------------------===//
801
Zhongxing Xu461147f2010-02-05 05:24:20 +0000802SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000803 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000804 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000805 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000806 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000807
Jordy Roseeac4a002010-06-28 08:26:15 +0000808 // Special case for zero RHS.
809 if (R.isZeroConstant()) {
810 switch (Op) {
811 default:
812 // Handle it normally.
813 break;
814 case BinaryOperator::Add:
815 case BinaryOperator::Sub:
816 // FIXME: does this need to be casted to match resultTy?
817 return L;
818 }
819 }
820
Zhongxing Xua1718c72009-04-03 07:33:13 +0000821 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000822 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000823
Ted Kremenek3bccf082009-07-11 00:58:27 +0000824 switch (MR->getKind()) {
825 case MemRegion::SymbolicRegionKind: {
826 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000827 SymbolRef Sym = SR->getSymbol();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000828 QualType T = Sym->getType(Ctx);
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000829 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000831 if (const PointerType *PT = T->getAs<PointerType>())
832 EleTy = PT->getPointeeType();
833 else
John McCall183700f2009-09-21 23:43:11 +0000834 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Ted Kremenek3bccf082009-07-11 00:58:27 +0000836 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000837 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000838 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000839 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000840 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000841 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000842 QualType EleTy = Ctx.CharTy; // Create an ElementRegion of bytes.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000843 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000844 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000845 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000846 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000847
Ted Kremenek3bccf082009-07-11 00:58:27 +0000848 case MemRegion::ElementRegionKind: {
849 ER = cast<ElementRegion>(MR);
850 break;
851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Ted Kremenek3bccf082009-07-11 00:58:27 +0000853 // Not yet handled.
854 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000855 case MemRegion::StringRegionKind: {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000856
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000857 }
858 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000859 case MemRegion::CompoundLiteralRegionKind:
860 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000861 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000862 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000863 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000865 case MemRegion::FunctionTextRegionKind:
866 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000867 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000868 // Technically this can happen if people do funny things with casts.
869 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Ted Kremenekde0d2632010-01-05 02:18:06 +0000871 case MemRegion::CXXThisRegionKind:
872 assert(0 &&
873 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000874 case MemRegion::GenericMemSpaceRegionKind:
875 case MemRegion::StackLocalsSpaceRegionKind:
876 case MemRegion::StackArgumentsSpaceRegionKind:
877 case MemRegion::HeapSpaceRegionKind:
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000878 case MemRegion::NonStaticGlobalSpaceRegionKind:
879 case MemRegion::StaticGlobalSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000880 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000881 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
882 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000883 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000884
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000885 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000886 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000887
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000888 // For now, only support:
889 // (a) concrete integer indices that can easily be resolved
890 // (b) 0 + symbolic index
891 if (Base) {
892 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
893 // FIXME: Should use SValuator here.
894 SVal NewIdx =
895 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000896 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000897 const MemRegion* NewER =
898 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000899 ER->getSuperRegion(), Ctx);
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000900 return ValMgr.makeLoc(NewER);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000901 }
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000902 if (0 == Base->getValue()) {
903 const MemRegion* NewER =
904 MRMgr.getElementRegion(ER->getElementType(), R,
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000905 ER->getSuperRegion(), Ctx);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000906 return ValMgr.makeLoc(NewER);
907 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000908 }
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Ted Kremenek5dc27462009-03-03 02:51:43 +0000910 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000911}
912
Ted Kremenek9af46f52009-06-16 22:36:44 +0000913//===----------------------------------------------------------------------===//
914// Loading values from regions.
915//===----------------------------------------------------------------------===//
916
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000917Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Zhongxing Xubdfa85f2010-05-29 06:23:24 +0000918 const MemRegion *R) {
Zhongxing Xu42c67bf2010-05-29 06:49:04 +0000919
920 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
921 return *V;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000922
Zhongxing Xu13d50172009-10-11 08:08:02 +0000923 return Optional<SVal>();
924}
925
926Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000927 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000928 if (R->isBoundable())
929 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +0000930 if (TR->getValueType()->isUnionType())
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000931 return UnknownVal();
932
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000933 if (const SVal *V = Lookup(B, R, BindingKey::Default))
934 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +0000935
936 return Optional<SVal>();
937}
938
939Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
940 const MemRegion *R) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000941
Ted Kremenek2cf073b2010-03-30 20:30:52 +0000942 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000943 return V;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000944
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000945 return getDefaultBinding(B, R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000946}
947
Ted Kremeneka6275a52009-07-15 02:31:43 +0000948static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
949 RTy = Ctx.getCanonicalType(RTy);
950 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Ted Kremeneka6275a52009-07-15 02:31:43 +0000952 if (RTy == UsedTy)
953 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
955
Ted Kremenek25c54572009-07-20 22:58:02 +0000956 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000957 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000958 // represents a reinterpretation.
959 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000960 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000961 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000962
963 return PUsedTy && PRTy &&
964 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000965 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000966 }
967
968 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000969}
970
Zhongxing Xu576bb922010-02-05 03:01:53 +0000971SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000972 assert(!isa<UnknownVal>(L) && "location unknown");
973 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000974
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000975 // FIXME: Is this even possible? Shouldn't this be treated as a null
976 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000977 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000978 return UndefinedVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000979
Ted Kremenek67f28532009-06-17 22:02:04 +0000980 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000981
Tom Care7b050302010-06-25 18:22:31 +0000982 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR)) {
983 if (T.isNull()) {
984 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000985 T = SR->getSymbol()->getType(Ctx);
Tom Care7b050302010-06-25 18:22:31 +0000986 }
Zhongxing Xu81491852010-02-08 08:43:02 +0000987 MR = GetElementZeroRegion(MR, T);
Tom Care7b050302010-06-25 18:22:31 +0000988 }
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000990 if (isa<CodeTextRegion>(MR)) {
991 assert(0 && "Why load from a code text region?");
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000992 return UnknownVal();
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000995 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
996 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000997 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000998 QualType RTy = R->getValueType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000999
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001000 // FIXME: We should eventually handle funny addressing. e.g.:
1001 //
1002 // int x = ...;
1003 // int *p = &x;
1004 // char *q = (char*) p;
1005 // char c = *q; // returns the first byte of 'x'.
1006 //
1007 // Such funny addressing will occur due to layering of regions.
1008
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001009#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001010 ASTContext &Ctx = getContext();
1011 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001012 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1013 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001014 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001015 assert(Ctx.getCanonicalType(RTy) ==
1016 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001017 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001018#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001019
Douglas Gregorfb87b892010-04-26 21:31:17 +00001020 if (RTy->isStructureOrClassType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001021 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001023 // FIXME: Handle unions.
1024 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001025 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001026
1027 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001028 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001029
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001030 // FIXME: handle Vector types.
1031 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001032 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001033
1034 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +00001035 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001036
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001037 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1038 // FIXME: Here we actually perform an implicit conversion from the loaded
1039 // value to the element type. Eventually we want to compose these values
1040 // more intelligently. For example, an 'element' can encompass multiple
1041 // bound regions (e.g., several bound bytes), or could be a subset of
1042 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001043 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001044 }
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001046 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1047 // FIXME: Here we actually perform an implicit conversion from the loaded
1048 // value to the ivar type. What we should model is stores to ivars
1049 // that blow past the extent of the ivar. If the address of the ivar is
1050 // reinterpretted, it is possible we stored a different value that could
1051 // fit within the ivar. Either we need to cast these when storing them
1052 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001053 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001056 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1057 // FIXME: Here we actually perform an implicit conversion from the loaded
1058 // value to the variable type. What we should model is stores to variables
1059 // that blow past the extent of the variable. If the address of the
1060 // variable is reinterpretted, it is possible we stored a different value
1061 // that could fit within the variable. Either we need to cast these when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001062 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001063 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001064 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001065
Zhongxing Xu576bb922010-02-05 03:01:53 +00001066 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001067 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001068
1069 // Check if the region has a binding.
1070 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001071 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001072
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001073 // The location does not have a bound value. This means that it has
1074 // the value it had upon its creation and/or entry to the analyzed
1075 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001076 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001077 // All stack variables are considered to have undefined values
1078 // upon creation. All heap allocated blocks are considered to
1079 // have undefined values as well unless they are explicitly bound
1080 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001081 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001082 }
1083
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001084 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001085 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001086}
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001088std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001089RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001090 if (Optional<SVal> OV = getDirectBinding(B, R))
1091 if (const nonloc::LazyCompoundVal *V =
1092 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001093 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001095 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001096 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001097 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001099 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001100 return std::make_pair(X.first,
1101 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001102 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001103 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001104 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001105 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001107 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001108 return std::make_pair(X.first,
1109 MRMgr.getFieldRegionWithSuper(FR, X.second));
1110 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001111 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xudcbcbdc2010-02-10 02:02:10 +00001112 // possible for a valid lazy binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001113 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001114}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001115
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001116SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001117 const ElementRegion* R) {
1118 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001119 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001120 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001121 return *V;
1122
Ted Kremenek921109a2009-07-01 23:19:52 +00001123 const MemRegion* superR = R->getSuperRegion();
1124
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001125 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001126 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001127 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001128 // an integer, e.g., *((unsigned int*)"hello")
Zhongxing Xu018220c2010-08-11 06:10:55 +00001129 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001130 if (T != Ctx.getCanonicalType(R->getElementType()))
1131 return UnknownVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001132
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001133 const StringLiteral *Str = StrR->getStringLiteral();
1134 SVal Idx = R->getIndex();
1135 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1136 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001137 int64_t byteLength = Str->getByteLength();
Jordy Rose167cc372010-07-29 06:40:33 +00001138 // Technically, only i == byteLength is guaranteed to be null.
1139 // However, such overflows should be caught before reaching this point;
1140 // the only time such an access would be made is if a string literal was
1141 // used to initialize a larger array.
1142 char c = (i >= byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001143 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001144 }
1145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Ted Kremeneka709b872010-05-31 01:22:04 +00001147 // Handle the case where we are indexing into a larger scalar object.
1148 // For example, this handles:
1149 // int x = ...
1150 // char *y = &x;
1151 // return *y;
1152 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001153 const RegionRawOffset &O = R->getAsArrayOffset();
Ted Kremeneka709b872010-05-31 01:22:04 +00001154 if (const TypedRegion *baseR = dyn_cast_or_null<TypedRegion>(O.getRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001155 QualType baseT = baseR->getValueType();
Ted Kremeneka709b872010-05-31 01:22:04 +00001156 if (baseT->isScalarType()) {
1157 QualType elemT = R->getElementType();
1158 if (elemT->isScalarType()) {
1159 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1160 if (const Optional<SVal> &V = getDirectBinding(B, superR)) {
1161 if (SymbolRef parentSym = V->getAsSymbol())
1162 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001163
Ted Kremeneka709b872010-05-31 01:22:04 +00001164 if (V->isUnknownOrUndef())
1165 return *V;
1166 // Other cases: give up. We are indexing into a larger object
1167 // that has some value, but we don't know how to handle that yet.
1168 return UnknownVal();
1169 }
1170 }
1171 }
Zhongxing Xu42c67bf2010-05-29 06:49:04 +00001172 }
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001173 }
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001174 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001175}
1176
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001177SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001178 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001179
1180 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001181 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001182 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001183 return *V;
1184
Zhongxing Xu018220c2010-08-11 06:10:55 +00001185 QualType Ty = R->getValueType();
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001186 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001187}
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001189Optional<SVal>
1190RegionStoreManager::RetrieveDerivedDefaultValue(RegionBindings B,
1191 const MemRegion *superR,
1192 const TypedRegion *R,
1193 QualType Ty) {
1194
1195 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
1196 if (SymbolRef parentSym = D->getAsSymbol())
1197 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1198
1199 if (D->isZeroConstant())
1200 return ValMgr.makeZeroVal(Ty);
1201
1202 if (D->isUnknownOrUndef())
1203 return *D;
1204
1205 assert(0 && "Unknown default value");
1206 }
1207
1208 return Optional<SVal>();
1209}
1210
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001211SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001212 const TypedRegion *R,
1213 QualType Ty,
1214 const MemRegion *superR) {
1215
Mike Stump1eb44332009-09-09 15:08:12 +00001216 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001217 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001219 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001221 while (superR) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001222 if (const Optional<SVal> &D = RetrieveDerivedDefaultValue(B, superR, R, Ty))
1223 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001225 // If our super region is a field or element itself, walk up the region
1226 // hierarchy to see if there is a default value installed in an ancestor.
1227 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1228 superR = cast<SubRegion>(superR)->getSuperRegion();
1229 continue;
1230 }
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001232 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001233 }
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001235 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001236 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001237 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001238 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001240 if (lazyBindingRegion) {
1241 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1242 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001243 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001244 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001245 }
1246
Ted Kremenekde0d2632010-01-05 02:18:06 +00001247 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001248 if (isa<ElementRegion>(R)) {
1249 // Currently we don't reason specially about Clang-style vectors. Check
1250 // if superR is a vector and if so return Unknown.
1251 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001252 if (typedSuperR->getValueType()->isVectorType())
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001253 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001254 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001255 }
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001257 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001258 }
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001260 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001261 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001262}
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Zhongxing Xu576bb922010-02-05 03:01:53 +00001264SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001265
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001266 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001267 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001268
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001269 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001270 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001272 const MemRegion *superR = R->getSuperRegion();
1273
Ted Kremenekab22ee92009-10-20 01:20:57 +00001274 // Check if the super region has a default binding.
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001275 if (const Optional<SVal> &V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001276 if (SymbolRef parentSym = V->getAsSymbol())
1277 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001279 // Other cases: give up.
1280 return UnknownVal();
1281 }
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Zhongxing Xu576bb922010-02-05 03:01:53 +00001283 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001284}
1285
Zhongxing Xu576bb922010-02-05 03:01:53 +00001286SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Ted Kremenek9031dd72009-07-21 00:12:07 +00001288 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001289 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001291 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001292 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Ted Kremenek9031dd72009-07-21 00:12:07 +00001294 // Lazily derive a value for the VarRegion.
1295 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001296 QualType T = VD->getType();
1297 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001298
1299 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek4dc15662010-02-06 03:57:59 +00001300 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu14d23282010-03-01 06:56:52 +00001301 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Ted Kremenek4dc15662010-02-06 03:57:59 +00001303 if (isa<GlobalsSpaceRegion>(MS)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001304 if (isa<NonStaticGlobalSpaceRegion>(MS)) {
Ted Kremenek4552ff02010-03-30 20:31:04 +00001305 // Is 'VD' declared constant? If so, retrieve the constant value.
1306 QualType CT = Ctx.getCanonicalType(T);
1307 if (CT.isConstQualified()) {
1308 const Expr *Init = VD->getInit();
1309 // Do the null check first, as we want to call 'IgnoreParenCasts'.
1310 if (Init)
1311 if (const IntegerLiteral *IL =
1312 dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) {
1313 const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL);
1314 return ValMgr.getSValuator().EvalCast(V, Init->getType(),
1315 IL->getType());
1316 }
1317 }
1318
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001319 if (const Optional<SVal> &V = RetrieveDerivedDefaultValue(B, MS, R, CT))
1320 return V.getValue();
1321
Zhongxing Xu14d23282010-03-01 06:56:52 +00001322 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek4552ff02010-03-30 20:31:04 +00001323 }
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Ted Kremenek4dc15662010-02-06 03:57:59 +00001325 if (T->isIntegerType())
1326 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001327 if (T->isPointerType())
1328 return ValMgr.makeNull();
1329
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001330 return UnknownVal();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001331 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001332
Ted Kremenek9031dd72009-07-21 00:12:07 +00001333 return UndefinedVal();
1334}
1335
Zhongxing Xu576bb922010-02-05 03:01:53 +00001336SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001337 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001338 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001339}
1340
Zhongxing Xu576bb922010-02-05 03:01:53 +00001341SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001342 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001343 assert(T->isStructureOrClassType());
Zhongxing Xu576bb922010-02-05 03:01:53 +00001344 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001345}
1346
Zhongxing Xu576bb922010-02-05 03:01:53 +00001347SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001348 assert(isa<ConstantArrayType>(R->getValueType()));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001349 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001350}
1351
Ted Kremenek9af46f52009-06-16 22:36:44 +00001352//===----------------------------------------------------------------------===//
1353// Binding values to regions.
1354//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001355
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001356Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001357 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001358 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001359 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Ted Kremenek0964a062009-01-21 06:57:53 +00001361 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001362}
1363
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001364Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001365 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001366 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001367
Ted Kremenek9af46f52009-06-16 22:36:44 +00001368 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001369 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Ted Kremenek9af46f52009-06-16 22:36:44 +00001371 // Check if the region is a struct region.
1372 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +00001373 if (TR->getValueType()->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001374 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001376 // Special case: the current region represents a cast and it and the super
1377 // region both have pointer types or intptr_t types. If so, perform the
1378 // bind to the super region.
1379 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001380 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001381 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1382 if (ER->getIndex().isZeroConstant()) {
1383 if (const TypedRegion *superR =
1384 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001385 QualType superTy = superR->getValueType();
1386 QualType erTy = ER->getValueType();
Mike Stump1eb44332009-09-09 15:08:12 +00001387
1388 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001389 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +00001390 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001391 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001392 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001393 // For now, just invalidate the fields of the struct/union/class.
1394 // FIXME: Precisely handle the fields of the record.
Jordy Rose58f8b202010-08-05 03:28:45 +00001395 if (superTy->isStructureOrClassType())
1396 return KillStruct(store, superR, UnknownVal());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001397 }
1398 }
1399 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001400 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1401 // Binding directly to a symbolic region should be treated as binding
1402 // to element 0.
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001403 QualType T = SR->getSymbol()->getType(Ctx);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001404
Ted Kremenek852274d2009-12-16 03:18:58 +00001405 // FIXME: Is this the right way to handle symbols that are references?
1406 if (const PointerType *PT = T->getAs<PointerType>())
1407 T = PT->getPointeeType();
1408 else
1409 T = T->getAs<ReferenceType>()->getPointeeType();
1410
Ted Kremenek0954cde2009-09-24 04:11:44 +00001411 R = GetElementZeroRegion(SR, T);
1412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001414 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001415 RegionBindings B = GetRegionBindings(store);
1416 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001417}
1418
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001419Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001420 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001421
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001422 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001423
Ted Kremenek0964a062009-01-21 06:57:53 +00001424 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001425 return BindArray(store, VR, InitVal);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001426 if (T->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001427 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001428
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001429 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001430}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001431
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001432// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001433Store RegionStoreManager::BindCompoundLiteral(Store store,
1434 const CompoundLiteralExpr *CL,
1435 const LocationContext *LC,
1436 SVal V) {
1437 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001438 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001439}
1440
Zhongxing Xua5ce9662010-06-01 03:01:33 +00001441
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001442Store RegionStoreManager::setImplicitDefaultValue(Store store,
1443 const MemRegion *R,
1444 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001445 RegionBindings B = GetRegionBindings(store);
1446 SVal V;
1447
1448 if (Loc::IsLocType(T))
1449 V = ValMgr.makeNull();
1450 else if (T->isIntegerType())
1451 V = ValMgr.makeZeroVal(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001452 else if (T->isStructureOrClassType() || T->isArrayType()) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001453 // Set the default value to a zero constant when it is a structure
1454 // or array. The type doesn't really matter.
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001455 V = ValMgr.makeZeroVal(Ctx.IntTy);
Ted Kremenek027e2662009-11-19 20:20:24 +00001456 }
1457 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001458 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001459 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001460
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001461 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001462}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001463
1464Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001465 SVal Init) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001466
Zhongxing Xu018220c2010-08-11 06:10:55 +00001467 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001468 QualType ElementTy = AT->getElementType();
Ted Kremenekfee90812010-01-26 23:51:00 +00001469 Optional<uint64_t> Size;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001470
Ted Kremenekfee90812010-01-26 23:51:00 +00001471 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1472 Size = CAT->getSize().getZExtValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001473
Jordy Rose167cc372010-07-29 06:40:33 +00001474 // Check if the init expr is a string literal.
1475 if (loc::MemRegionVal *MRV = dyn_cast<loc::MemRegionVal>(&Init)) {
1476 const StringRegion *S = cast<StringRegion>(MRV->getRegion());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001477
Jordy Rose167cc372010-07-29 06:40:33 +00001478 // Treat the string as a lazy compound value.
1479 nonloc::LazyCompoundVal LCV =
1480 cast<nonloc::LazyCompoundVal>(ValMgr.makeLazyCompoundVal(store, S));
1481 return CopyLazyBindings(LCV, store, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001482 }
1483
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001484 // Handle lazy compound values.
1485 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001486 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001487
1488 // Remaining case: explicit compound values.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001489
Ted Kremenek027e2662009-11-19 20:20:24 +00001490 if (Init.isUnknown())
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001491 return setImplicitDefaultValue(store, R, ElementTy);
1492
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001493 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001494 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001495 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Ted Kremenekfee90812010-01-26 23:51:00 +00001497 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001498 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001499 if (VI == VE)
1500 break;
1501
Ted Kremenek46537392009-07-16 01:33:37 +00001502 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001503 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001504
Douglas Gregorfb87b892010-04-26 21:31:17 +00001505 if (ElementTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001506 store = BindStruct(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001507 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001508 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001509 }
1510
Ted Kremenek027e2662009-11-19 20:20:24 +00001511 // If the init list is shorter than the array length, set the
1512 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001513 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001514 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001515
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001516 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001517}
1518
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001519Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1520 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Ted Kremenek67f28532009-06-17 22:02:04 +00001522 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001523 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Zhongxing Xu018220c2010-08-11 06:10:55 +00001525 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001526 assert(T->isStructureOrClassType());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001527
Ted Kremenek6217b802009-07-29 21:53:49 +00001528 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001529 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001530
1531 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001532 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001533
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001534 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001535 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001536 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001538 // We may get non-CompoundVal accidentally due to imprecise cast logic or
1539 // that we are binding symbolic struct value. Kill the field values, and if
1540 // the value is symbolic go and bind it as a "default" binding.
Ted Kremenek67f28532009-06-17 22:02:04 +00001541 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001542 return KillStruct(store, R, isa<nonloc::SymbolVal>(V) ? V : UnknownVal());
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001543
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001544 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001545 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001546
1547 RecordDecl::field_iterator FI, FE;
1548
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001549 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001550
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001551 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001552 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001553
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001554 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001555 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001556
Ted Kremenekcf549592009-09-22 21:19:14 +00001557 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001558 store = BindArray(store, FR, *VI);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001559 else if (FTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001560 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001561 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001562 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001563 }
1564
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001565 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001566 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001567 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001568 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001569 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001570 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001571
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001572 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001573}
1574
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001575Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R,
1576 SVal DefaultVal) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001577 RegionBindings B = GetRegionBindings(store);
1578 llvm::OwningPtr<RegionStoreSubRegionMap>
1579 SubRegions(getRegionStoreSubRegionMap(store));
1580 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001581
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001582 // Set the default value of the struct region to "unknown".
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001583 return Add(B, R, BindingKey::Default, DefaultVal).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001584}
1585
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001586Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1587 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001588
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001589 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001590 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001591
Mike Stump1eb44332009-09-09 15:08:12 +00001592 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001593 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001594
Mike Stump1eb44332009-09-09 15:08:12 +00001595 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001596 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001598 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1599 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001600 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001601}
1602
1603//===----------------------------------------------------------------------===//
1604// "Raw" retrievals and bindings.
1605//===----------------------------------------------------------------------===//
1606
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001607BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001608 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001609 const RegionRawOffset &O = ER->getAsArrayOffset();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001610
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001611 // FIXME: There are some ElementRegions for which we cannot compute
Jordy Rosee7011172010-08-16 01:15:17 +00001612 // raw offsets yet, including regions with symbolic offsets. These will be
1613 // ignored by the store.
1614 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001615 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001616
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001617 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001618}
1619
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001620RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001621 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001622 return B;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001623 return RBFactory.Add(B, K, V);
1624}
1625
1626RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001627 BindingKey::Kind k, SVal V) {
1628 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001629}
1630
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001631const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001632 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001633 return NULL;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001634 return B.lookup(K);
1635}
1636
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001637const SVal *RegionStoreManager::Lookup(RegionBindings B,
1638 const MemRegion *R,
1639 BindingKey::Kind k) {
1640 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001641}
1642
1643RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001644 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001645 return B;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001646 return RBFactory.Remove(B, K);
1647}
1648
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001649RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1650 BindingKey::Kind k){
1651 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001652}
1653
1654Store RegionStoreManager::Remove(Store store, BindingKey K) {
1655 RegionBindings B = GetRegionBindings(store);
1656 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001657}
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Ted Kremenek9af46f52009-06-16 22:36:44 +00001659//===----------------------------------------------------------------------===//
1660// State pruning.
1661//===----------------------------------------------------------------------===//
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001662
Ted Kremenek5499b842010-03-10 16:32:56 +00001663namespace {
1664class RemoveDeadBindingsWorker :
1665 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1666 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1667 SymbolReaper &SymReaper;
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001668 const StackFrameContext *CurrentLCtx;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001669
Ted Kremenek5499b842010-03-10 16:32:56 +00001670public:
1671 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1672 RegionBindings b, SymbolReaper &symReaper,
Jordy Rose7dadf792010-07-01 20:09:55 +00001673 const StackFrameContext *LCtx)
Ted Kremenek5499b842010-03-10 16:32:56 +00001674 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
Jordy Rose7dadf792010-07-01 20:09:55 +00001675 SymReaper(symReaper), CurrentLCtx(LCtx) {}
Ted Kremenek5499b842010-03-10 16:32:56 +00001676
1677 // Called by ClusterAnalysis.
1678 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1679 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek5499b842010-03-10 16:32:56 +00001680
Ted Kremenek75a2d942010-04-01 00:15:55 +00001681 void VisitBindingKey(BindingKey K);
Ted Kremenek5499b842010-03-10 16:32:56 +00001682 bool UpdatePostponed();
1683 void VisitBinding(SVal V);
1684};
1685}
1686
1687void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1688 RegionCluster &C) {
1689
1690 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
Jordy Rose7dadf792010-07-01 20:09:55 +00001691 if (SymReaper.isLive(VR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001692 AddToWorkList(baseR, C);
1693
1694 return;
1695 }
1696
1697 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1698 if (SymReaper.isLive(SR->getSymbol()))
1699 AddToWorkList(SR, C);
1700 else
1701 Postponed.push_back(SR);
1702
1703 return;
1704 }
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001705
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001706 if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
1707 AddToWorkList(baseR, C);
1708 return;
1709 }
1710
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001711 // CXXThisRegion in the current or parent location context is live.
1712 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001713 const StackArgumentsSpaceRegion *StackReg =
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001714 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
1715 const StackFrameContext *RegCtx = StackReg->getStackFrame();
1716 if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
1717 AddToWorkList(TR, C);
1718 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001719}
1720
1721void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1722 BindingKey *I, BindingKey *E) {
Ted Kremenek75a2d942010-04-01 00:15:55 +00001723 for ( ; I != E; ++I)
1724 VisitBindingKey(*I);
Ted Kremenek5499b842010-03-10 16:32:56 +00001725}
1726
1727void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1728 // Is it a LazyCompoundVal? All referenced regions are live as well.
1729 if (const nonloc::LazyCompoundVal *LCS =
1730 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1731
1732 const MemRegion *LazyR = LCS->getRegion();
1733 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1734 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +00001735 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
1736 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001737 VisitBinding(RI.getData());
1738 }
1739 return;
1740 }
1741
1742 // If V is a region, then add it to the worklist.
1743 if (const MemRegion *R = V.getAsRegion())
1744 AddToWorkList(R);
1745
1746 // Update the set of live symbols.
1747 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1748 SI!=SE;++SI)
1749 SymReaper.markLive(*SI);
1750}
1751
Ted Kremenek75a2d942010-04-01 00:15:55 +00001752void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) {
1753 const MemRegion *R = K.getRegion();
1754
Ted Kremenek5499b842010-03-10 16:32:56 +00001755 // Mark this region "live" by adding it to the worklist. This will cause
1756 // use to visit all regions in the cluster (if we haven't visited them
1757 // already).
Ted Kremenek75a2d942010-04-01 00:15:55 +00001758 if (AddToWorkList(R)) {
1759 // Mark the symbol for any live SymbolicRegion as "live". This means we
1760 // should continue to track that symbol.
1761 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1762 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek5499b842010-03-10 16:32:56 +00001763
Ted Kremenek75a2d942010-04-01 00:15:55 +00001764 // For BlockDataRegions, enqueue the VarRegions for variables marked
1765 // with __block (passed-by-reference).
1766 // via BlockDeclRefExprs.
1767 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1768 for (BlockDataRegion::referenced_vars_iterator
1769 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1770 RI != RE; ++RI) {
1771 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1772 AddToWorkList(*RI);
1773 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001774
Ted Kremenek75a2d942010-04-01 00:15:55 +00001775 // No possible data bindings on a BlockDataRegion.
1776 return;
Ted Kremenek5499b842010-03-10 16:32:56 +00001777 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001778 }
1779
Ted Kremenek75a2d942010-04-01 00:15:55 +00001780 // Visit the data binding for K.
1781 if (const SVal *V = RM.Lookup(B, K))
Ted Kremenek5499b842010-03-10 16:32:56 +00001782 VisitBinding(*V);
1783}
1784
1785bool RemoveDeadBindingsWorker::UpdatePostponed() {
1786 // See if any postponed SymbolicRegions are actually live now, after
1787 // having done a scan.
1788 bool changed = false;
1789
1790 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1791 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1792 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1793 if (SymReaper.isLive(SR->getSymbol())) {
1794 changed |= AddToWorkList(SR);
1795 *I = NULL;
1796 }
1797 }
1798 }
1799
1800 return changed;
1801}
1802
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001803Store RegionStoreManager::RemoveDeadBindings(Store store,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001804 const StackFrameContext *LCtx,
Zhongxing Xu72119c42010-02-05 05:34:29 +00001805 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001806 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001807{
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001808 RegionBindings B = GetRegionBindings(store);
Jordy Rose7dadf792010-07-01 20:09:55 +00001809 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
Ted Kremenek5499b842010-03-10 16:32:56 +00001810 W.GenerateClusters();
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Ted Kremenek5499b842010-03-10 16:32:56 +00001812 // Enqueue the region roots onto the worklist.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001813 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenek5499b842010-03-10 16:32:56 +00001814 E=RegionRoots.end(); I!=E; ++I)
1815 W.AddToWorkList(*I);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001816
Ted Kremenek5499b842010-03-10 16:32:56 +00001817 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001818
Ted Kremenek9af46f52009-06-16 22:36:44 +00001819 // We have now scanned the store, marking reachable regions and symbols
1820 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001821 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001822 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek5499b842010-03-10 16:32:56 +00001823 const BindingKey &K = I.getKey();
1824
Ted Kremenekb7118f72010-03-10 16:38:41 +00001825 // If the cluster has been visited, we know the region has been marked.
Ted Kremenek5499b842010-03-10 16:32:56 +00001826 if (W.isVisited(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001827 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Ted Kremenek5499b842010-03-10 16:32:56 +00001829 // Remove the dead entry.
1830 B = Remove(B, K);
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Ted Kremenek5499b842010-03-10 16:32:56 +00001832 // Mark all non-live symbols that this binding references as dead.
1833 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001834 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001836 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001837 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1838 for (; SI != SE; ++SI)
1839 SymReaper.maybeDead(*SI);
1840 }
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001841
1842 return B.getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001843}
1844
Ted Kremenek5499b842010-03-10 16:32:56 +00001845
Jordy Roseff59efd2010-08-03 20:44:35 +00001846Store RegionStoreManager::EnterStackFrame(const GRState *state,
1847 const StackFrameContext *frame) {
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001848 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001849 FunctionDecl::param_const_iterator PI = FD->param_begin();
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001850 Store store = state->getStore();
Zhongxing Xuc5063572010-03-16 13:14:16 +00001851
1852 if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) {
1853 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1854
1855 // Copy the arg expression value to the arg variables.
1856 for (; AI != AE; ++AI, ++PI) {
1857 SVal ArgVal = state->getSVal(*AI);
1858 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1859 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001860 } else if (const CXXConstructExpr *CE =
Zhongxing Xuc5063572010-03-16 13:14:16 +00001861 dyn_cast<CXXConstructExpr>(frame->getCallSite())) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001862 CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(),
Zhongxing Xuc5063572010-03-16 13:14:16 +00001863 AE = CE->arg_end();
1864
1865 // Copy the arg expression value to the arg variables.
1866 for (; AI != AE; ++AI, ++PI) {
1867 SVal ArgVal = state->getSVal(*AI);
1868 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1869 }
1870 } else
Jordy Roseff59efd2010-08-03 20:44:35 +00001871 llvm_unreachable("Unhandled call expression.");
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001872
Jordy Roseff59efd2010-08-03 20:44:35 +00001873 return store;
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001874}
1875
Ted Kremenek9af46f52009-06-16 22:36:44 +00001876//===----------------------------------------------------------------------===//
1877// Utility methods.
1878//===----------------------------------------------------------------------===//
1879
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001880void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001881 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001882 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001883 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Ted Kremenek451ac092009-08-06 04:50:20 +00001885 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001886 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001887}