blob: e5c75b9371f9e9325e97a601bd43da26dc4aaf96 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000017#include "clang/AST/CharUnits.h"
Zhongxing Xuc5063572010-03-16 13:14:16 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/ExprCXX.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21#include "clang/Analysis/AnalysisContext.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Checker/PathSensitive/GRState.h"
24#include "clang/Checker/PathSensitive/GRStateTrait.h"
25#include "clang/Checker/PathSensitive/MemRegion.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000027#include "llvm/ADT/ImmutableMap.h"
28#include "llvm/ADT/Optional.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000029#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000030
31using namespace clang;
Ted Kremenek66d51422010-04-09 20:26:58 +000032using llvm::Optional;
Zhongxing Xu17892752008-10-08 02:50:44 +000033
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000035// Representation of binding keys.
36//===----------------------------------------------------------------------===//
37
38namespace {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000039class BindingKey {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000040public:
Ted Kremeneke393f4a2010-02-03 03:06:46 +000041 enum Kind { Direct = 0x0, Default = 0x1 };
42private:
43 llvm ::PointerIntPair<const MemRegion*, 1> P;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000044 uint64_t Offset;
45
Ted Kremeneke393f4a2010-02-03 03:06:46 +000046 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
Zhongxing Xubfcaf802010-02-05 02:26:30 +000047 : P(r, (unsigned) k), Offset(offset) { assert(r); }
Ted Kremeneke393f4a2010-02-03 03:06:46 +000048public:
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000049
Ted Kremeneke393f4a2010-02-03 03:06:46 +000050 bool isDefault() const { return P.getInt() == Default; }
51 bool isDirect() const { return P.getInt() == Direct; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000052
Ted Kremeneke393f4a2010-02-03 03:06:46 +000053 const MemRegion *getRegion() const { return P.getPointer(); }
54 uint64_t getOffset() const { return Offset; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000055
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000056 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000057 ID.AddPointer(P.getOpaqueValue());
58 ID.AddInteger(Offset);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000059 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000060
Ted Kremeneke393f4a2010-02-03 03:06:46 +000061 static BindingKey Make(const MemRegion *R, Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000062
Ted Kremeneke393f4a2010-02-03 03:06:46 +000063 bool operator<(const BindingKey &X) const {
64 if (P.getOpaqueValue() < X.P.getOpaqueValue())
65 return true;
66 if (P.getOpaqueValue() > X.P.getOpaqueValue())
67 return false;
68 return Offset < X.Offset;
69 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000070
Ted Kremeneke393f4a2010-02-03 03:06:46 +000071 bool operator==(const BindingKey &X) const {
72 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
73 Offset == X.Offset;
74 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000075};
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000076} // end anonymous namespace
77
78namespace llvm {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000079 static inline
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000080 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000081 os << '(' << K.getRegion() << ',' << K.getOffset()
82 << ',' << (K.isDirect() ? "direct" : "default")
83 << ')';
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000084 return os;
85 }
86} // end llvm namespace
87
88//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +000089// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000090//===----------------------------------------------------------------------===//
91
Ted Kremeneke393f4a2010-02-03 03:06:46 +000092typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000093
Ted Kremenek50dc1b32008-12-24 01:05:03 +000094//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000095// Fine-grained control of RegionStoreManager.
96//===----------------------------------------------------------------------===//
97
98namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000099struct minimal_features_tag {};
100struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000102class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000103 bool SupportsFields;
104 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek9af46f52009-06-16 22:36:44 +0000106public:
107 RegionStoreFeatures(minimal_features_tag) :
108 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek9af46f52009-06-16 22:36:44 +0000110 RegionStoreFeatures(maximal_features_tag) :
111 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek9af46f52009-06-16 22:36:44 +0000113 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek9af46f52009-06-16 22:36:44 +0000115 bool supportsFields() const { return SupportsFields; }
116 bool supportsRemaining() const { return SupportsRemaining; }
117};
118}
119
120//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000121// Utility functions.
122//===----------------------------------------------------------------------===//
123
124static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
125 if (ty->isAnyPointerType())
126 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000128 return ty->isIntegerType() && ty->isScalarType() &&
129 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
130}
131
132//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000133// Main RegionStore logic.
134//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000135
Zhongxing Xu17892752008-10-08 02:50:44 +0000136namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000138class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekdf165012010-02-02 22:38:47 +0000139public:
140 typedef llvm::ImmutableSet<const MemRegion*> Set;
141 typedef llvm::DenseMap<const MemRegion*, Set> Map;
142private:
143 Set::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000144 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000145public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000146 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000147 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000148
149 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000150 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000151 return true;
152 }
153
154 I->second = F.Add(I->second, SubRegion);
155 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000158 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Ted Kremenek59e8f112009-03-03 01:35:36 +0000160 ~RegionStoreSubRegionMap() {}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000161
Ted Kremenekdf165012010-02-02 22:38:47 +0000162 const Set *getSubRegions(const MemRegion *Parent) const {
163 Map::const_iterator I = M.find(Parent);
164 return I == M.end() ? NULL : &I->second;
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenek5dc27462009-03-03 02:51:43 +0000167 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000168 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000169
170 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000171 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Ted Kremenekdf165012010-02-02 22:38:47 +0000173 Set S = I->second;
174 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000175 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000176 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek5dc27462009-03-03 02:51:43 +0000179 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000182
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000183
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000184class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000185 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000186 RegionBindings::Factory RBFactory;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000187
Zhongxing Xu17892752008-10-08 02:50:44 +0000188public:
Mike Stump1eb44332009-09-09 15:08:12 +0000189 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000190 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000191 Features(f),
Ted Kremenek8928d412010-02-04 04:14:49 +0000192 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000193
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000194 SubRegionMap *getSubRegionMap(Store store) {
195 return getRegionStoreSubRegionMap(store);
196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Zhongxing Xu13d50172009-10-11 08:08:02 +0000198 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Zhongxing Xu13d50172009-10-11 08:08:02 +0000200 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
201 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000202 /// getDefaultBinding - Returns an SVal* representing an optional default
203 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000204 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000205
Ted Kremenek027e2662009-11-19 20:20:24 +0000206 /// setImplicitDefaultValue - Set the default binding for the provided
207 /// MemRegion to the value implicitly defined for compound literals when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000208 /// the value is not specified.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000209 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000211 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
212 /// type. 'Array' represents the lvalue of the array being decayed
213 /// to a pointer, and the returned SVal represents the decayed
214 /// version of that lvalue (i.e., a pointer to the first element of
215 /// the array). This is called by GRExprEngine when evaluating
216 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000217 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000218
Zhongxing Xu461147f2010-02-05 05:24:20 +0000219 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000220
Mike Stump1eb44332009-09-09 15:08:12 +0000221 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000222 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000223 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000224
Ted Kremenek67f28532009-06-17 22:02:04 +0000225 //===-------------------------------------------------------------------===//
226 // Binding values to regions.
227 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000228
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000229 Store InvalidateRegions(Store store,
230 const MemRegion * const *Begin,
231 const MemRegion * const *End,
232 const Expr *E, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000233 InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000234 bool invalidateGlobals,
235 InvalidatedRegions *Regions);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000237public: // Made public for helper classes.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000238
Zhongxing Xu13d50172009-10-11 08:08:02 +0000239 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000240 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000242 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
243
244 RegionBindings Add(RegionBindings B, const MemRegion *R,
245 BindingKey::Kind k, SVal V);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000246
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000247 const SVal *Lookup(RegionBindings B, BindingKey K);
248 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000249
250 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000251 RegionBindings Remove(RegionBindings B, const MemRegion *R,
252 BindingKey::Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000253
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000254 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
255 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000256 }
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000257
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000258 Store Remove(Store store, BindingKey K);
259
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000260public: // Part of public interface to class.
261
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000262 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000263
Zhongxing Xu54460092010-06-01 04:49:26 +0000264 // BindDefault is only used to initialize a region with a default value.
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000265 Store BindDefault(Store store, const MemRegion *R, SVal V) {
Zhongxing Xu54460092010-06-01 04:49:26 +0000266 RegionBindings B = GetRegionBindings(store);
267 assert(!Lookup(B, R, BindingKey::Default));
268 assert(!Lookup(B, R, BindingKey::Direct));
269 return Add(B, R, BindingKey::Default, V).getRoot();
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000270 }
271
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000272 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
273 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000275 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000276
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000277 Store BindDeclWithNoInit(Store store, const VarRegion *) {
278 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000279 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000280
Ted Kremenek67f28532009-06-17 22:02:04 +0000281 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000282 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000284 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
286 /// KillStruct - Set the entire struct to unknown.
Ted Kremenek281e9dc2010-07-29 00:28:47 +0000287 Store KillStruct(Store store, const TypedRegion* R, SVal DefaultVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000288
Ted Kremenek67f28532009-06-17 22:02:04 +0000289 Store Remove(Store store, Loc LV);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000290
Ted Kremenek67f28532009-06-17 22:02:04 +0000291
292 //===------------------------------------------------------------------===//
293 // Loading values from regions.
294 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Ted Kremenek67f28532009-06-17 22:02:04 +0000296 /// The high level logic for this method is this:
297 /// Retrieve (L)
298 /// if L has binding
299 /// return L's binding
300 /// else if L is in killset
301 /// return unknown
302 /// else
303 /// if L is on stack or heap
304 /// return undefined
305 /// else
306 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000307 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000308
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000309 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000310
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000311 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Zhongxing Xu576bb922010-02-05 03:01:53 +0000313 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Zhongxing Xu576bb922010-02-05 03:01:53 +0000315 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Zhongxing Xu576bb922010-02-05 03:01:53 +0000317 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000319 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000320 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Ted Kremenek67f28532009-06-17 22:02:04 +0000322 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000323 /// struct copy:
324 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000325 /// x = y;
326 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000327 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Zhongxing Xu576bb922010-02-05 03:01:53 +0000329 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000331 /// Used to lazily generate derived symbols for bindings that are defined
332 /// implicitly by default bindings in a super region.
333 Optional<SVal> RetrieveDerivedDefaultValue(RegionBindings B,
334 const MemRegion *superR,
335 const TypedRegion *R, QualType Ty);
336
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000337 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000338 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000339 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000341 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
342 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000343
344 //===------------------------------------------------------------------===//
345 // State pruning.
346 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek67f28532009-06-17 22:02:04 +0000348 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
349 /// It returns a new Store with these values removed.
Jordy Rose7dadf792010-07-01 20:09:55 +0000350 const GRState *RemoveDeadBindings(GRState &state,
Zhongxing Xu95798982010-05-26 03:27:35 +0000351 const StackFrameContext *LCtx,
352 SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000353 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
354
Jordy Roseff59efd2010-08-03 20:44:35 +0000355 Store EnterStackFrame(const GRState *state, const StackFrameContext *frame);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000356
Ted Kremenek67f28532009-06-17 22:02:04 +0000357 //===------------------------------------------------------------------===//
358 // Region "extents".
359 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Jordy Rose32f26562010-07-04 00:00:41 +0000361 // FIXME: This method will soon be eliminated; see the note in Store.h.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000362 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000363 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000364
365 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000366 // Utility methods.
367 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek451ac092009-08-06 04:50:20 +0000369 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000370 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000371 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000372
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000373 void print(Store store, llvm::raw_ostream& Out, const char* nl,
374 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000375
376 void iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek0e9910f2010-06-17 00:24:42 +0000377 RegionBindings B = GetRegionBindings(store);
378 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
379 const BindingKey &K = I.getKey();
380 if (!K.isDirect())
381 continue;
382 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) {
383 // FIXME: Possibly incorporate the offset?
384 if (!f.HandleBinding(*this, store, R, I.getData()))
385 return;
386 }
387 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000388 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000389};
390
391} // end anonymous namespace
392
Ted Kremenek9af46f52009-06-16 22:36:44 +0000393//===----------------------------------------------------------------------===//
394// RegionStore creation.
395//===----------------------------------------------------------------------===//
396
397StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
398 RegionStoreFeatures F = maximal_features_tag();
399 return new RegionStoreManager(StMgr, F);
400}
401
402StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
403 RegionStoreFeatures F = minimal_features_tag();
404 F.enableFields(true);
405 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000406}
407
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000408void
409RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000410 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000411 const MemRegion *superR = R->getSuperRegion();
412 if (add(superR, R))
413 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000414 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000415}
416
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000417RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000418RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
419 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000420 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000422 llvm::SmallVector<const SubRegion*, 10> WL;
423
Ted Kremenek451ac092009-08-06 04:50:20 +0000424 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000425 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000426 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Mike Stump1eb44332009-09-09 15:08:12 +0000428 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000429 // don't have direct bindings but are super regions of those that do.
430 while (!WL.empty()) {
431 const SubRegion *R = WL.back();
432 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000433 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000434 }
435
Ted Kremenek14453bf2009-03-03 19:02:42 +0000436 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000437}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000438
Ted Kremenek9af46f52009-06-16 22:36:44 +0000439//===----------------------------------------------------------------------===//
Ted Kremeneka4fab032010-03-10 07:19:59 +0000440// Region Cluster analysis.
441//===----------------------------------------------------------------------===//
442
443namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000444template <typename DERIVED>
Ted Kremeneka4fab032010-03-10 07:19:59 +0000445class ClusterAnalysis {
446protected:
447 typedef BumpVector<BindingKey> RegionCluster;
448 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5499b842010-03-10 16:32:56 +0000449 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
450 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
451 WorkList;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000452
453 BumpVectorContext BVC;
454 ClusterMap ClusterM;
Ted Kremenek5499b842010-03-10 16:32:56 +0000455 WorkList WL;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000456
457 RegionStoreManager &RM;
458 ASTContext &Ctx;
459 ValueManager &ValMgr;
460
Ted Kremenek5499b842010-03-10 16:32:56 +0000461 RegionBindings B;
462
Ted Kremeneka4fab032010-03-10 07:19:59 +0000463public:
Ted Kremenek5499b842010-03-10 16:32:56 +0000464 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
465 RegionBindings b)
466 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
467 B(b) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000468
Ted Kremenek5499b842010-03-10 16:32:56 +0000469 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000470
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000471 RegionCluster &AddToCluster(BindingKey K) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000472 const MemRegion *R = K.getRegion();
473 const MemRegion *baseR = R->getBaseRegion();
474 RegionCluster &C = getCluster(baseR);
475 C.push_back(K, BVC);
476 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000477 return C;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000478 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000479
Ted Kremenek5499b842010-03-10 16:32:56 +0000480 bool isVisited(const MemRegion *R) {
481 return (bool) Visited[&getCluster(R->getBaseRegion())];
482 }
483
484 RegionCluster& getCluster(const MemRegion *R) {
485 RegionCluster *&CRef = ClusterM[R];
486 if (!CRef) {
487 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
488 CRef = new (Mem) RegionCluster(BVC, 10);
489 }
490 return *CRef;
491 }
492
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000493 void GenerateClusters(bool includeGlobals = false) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000494 // Scan the entire set of bindings and make the region clusters.
495 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000496 RegionCluster &C = AddToCluster(RI.getKey());
Ted Kremenek5499b842010-03-10 16:32:56 +0000497 if (const MemRegion *R = RI.getData().getAsRegion()) {
498 // Generate a cluster, but don't add the region to the cluster
499 // if there aren't any bindings.
500 getCluster(R->getBaseRegion());
501 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000502 if (includeGlobals) {
503 const MemRegion *R = RI.getKey().getRegion();
504 if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()))
505 AddToWorkList(R, C);
506 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000507 }
508 }
Ted Kremenek5499b842010-03-10 16:32:56 +0000509
510 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
511 if (unsigned &visited = Visited[&C])
512 return false;
513 else
514 visited = 1;
515
516 WL.push_back(std::make_pair(R, &C));
517 return true;
518 }
519
520 bool AddToWorkList(BindingKey K) {
521 return AddToWorkList(K.getRegion());
522 }
523
524 bool AddToWorkList(const MemRegion *R) {
525 const MemRegion *baseR = R->getBaseRegion();
526 return AddToWorkList(baseR, getCluster(baseR));
527 }
528
529 void RunWorkList() {
530 while (!WL.empty()) {
531 const MemRegion *baseR;
532 RegionCluster *C;
533 llvm::tie(baseR, C) = WL.back();
534 WL.pop_back();
535
536 // First visit the cluster.
537 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
538
Ted Kremenek75a2d942010-04-01 00:15:55 +0000539 // Next, visit the base region.
540 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
Ted Kremenek5499b842010-03-10 16:32:56 +0000541 }
542 }
543
544public:
545 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
546 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
Ted Kremenek75a2d942010-04-01 00:15:55 +0000547 void VisitBaseRegion(const MemRegion *baseR) {}
Ted Kremenek5499b842010-03-10 16:32:56 +0000548};
Ted Kremeneka4fab032010-03-10 07:19:59 +0000549}
550
551//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000552// Binding invalidation.
553//===----------------------------------------------------------------------===//
554
Zhongxing Xu13d50172009-10-11 08:08:02 +0000555void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
556 const MemRegion *R,
557 RegionStoreSubRegionMap &M) {
Ted Kremeneka4fab032010-03-10 07:19:59 +0000558
Ted Kremenekdf165012010-02-02 22:38:47 +0000559 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
560 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
561 I != E; ++I)
562 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000563
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000564 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000565}
566
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000567namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000568class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
569{
570 const Expr *Ex;
571 unsigned Count;
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000572 StoreManager::InvalidatedSymbols *IS;
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000573 StoreManager::InvalidatedRegions *Regions;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000574public:
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000575 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenek5499b842010-03-10 16:32:56 +0000576 GRStateManager &stateMgr,
577 RegionBindings b,
578 const Expr *ex, unsigned count,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000579 StoreManager::InvalidatedSymbols *is,
580 StoreManager::InvalidatedRegions *r)
Ted Kremenek5499b842010-03-10 16:32:56 +0000581 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000582 Ex(ex), Count(count), IS(is), Regions(r) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000583
Ted Kremenek5499b842010-03-10 16:32:56 +0000584 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek75a2d942010-04-01 00:15:55 +0000585 void VisitBaseRegion(const MemRegion *baseR);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000586
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000587private:
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000588 void VisitBinding(SVal V);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000589};
Ted Kremenek5b290652010-02-03 04:16:00 +0000590}
591
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000592void InvalidateRegionsWorker::VisitBinding(SVal V) {
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000593 // A symbol? Mark it touched by the invalidation.
594 if (IS)
595 if (SymbolRef Sym = V.getAsSymbol())
596 IS->insert(Sym);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000597
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000598 if (const MemRegion *R = V.getAsRegion()) {
599 AddToWorkList(R);
600 return;
601 }
602
603 // Is it a LazyCompoundVal? All references get invalidated as well.
604 if (const nonloc::LazyCompoundVal *LCS =
605 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
606
607 const MemRegion *LazyR = LCS->getRegion();
608 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
609
610 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +0000611 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
612 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000613 VisitBinding(RI.getData());
614 }
615
616 return;
617 }
618}
619
Ted Kremenek5499b842010-03-10 16:32:56 +0000620void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
621 BindingKey *I, BindingKey *E) {
622 for ( ; I != E; ++I) {
623 // Get the old binding. Is it a region? If so, add it to the worklist.
624 const BindingKey &K = *I;
625 if (const SVal *V = RM.Lookup(B, K))
626 VisitBinding(*V);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000627
Ted Kremenek5499b842010-03-10 16:32:56 +0000628 B = RM.Remove(B, K);
629 }
630}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000631
Ted Kremenek75a2d942010-04-01 00:15:55 +0000632void InvalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000633 if (IS) {
634 // Symbolic region? Mark that symbol touched by the invalidation.
635 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
636 IS->insert(SR->getSymbol());
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000637 }
638
Ted Kremenek5499b842010-03-10 16:32:56 +0000639 // BlockDataRegion? If so, invalidate captured variables that are passed
640 // by reference.
641 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
642 for (BlockDataRegion::referenced_vars_iterator
643 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
644 BI != BE; ++BI) {
645 const VarRegion *VR = *BI;
646 const VarDecl *VD = VR->getDecl();
647 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
648 AddToWorkList(VR);
649 }
650 return;
651 }
652
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000653 // Otherwise, we have a normal data region. Record that we touched the region.
654 if (Regions)
655 Regions->push_back(baseR);
656
Ted Kremenek5499b842010-03-10 16:32:56 +0000657 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
658 // Invalidate the region by setting its default value to
659 // conjured symbol. The type of the symbol is irrelavant.
660 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
661 Count);
662 B = RM.Add(B, baseR, BindingKey::Default, V);
663 return;
664 }
665
666 if (!baseR->isBoundable())
667 return;
668
669 const TypedRegion *TR = cast<TypedRegion>(baseR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000670 QualType T = TR->getValueType();
Ted Kremenek5499b842010-03-10 16:32:56 +0000671
672 // Invalidate the binding.
673 if (const RecordType *RT = T->getAsStructureType()) {
674 const RecordDecl *RD = RT->getDecl()->getDefinition();
675 // No record definition. There is nothing we can do.
676 if (!RD) {
677 B = RM.Remove(B, baseR);
678 return;
679 }
680
681 // Invalidate the region by setting its default value to
682 // conjured symbol. The type of the symbol is irrelavant.
683 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
684 Count);
685 B = RM.Add(B, baseR, BindingKey::Default, V);
686 return;
687 }
688
689 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
690 // Set the default value of the array to conjured symbol.
691 DefinedOrUnknownSVal V =
692 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
693 B = RM.Add(B, baseR, BindingKey::Default, V);
694 return;
695 }
696
697 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
698 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
699 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000700}
701
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000702Store RegionStoreManager::InvalidateRegions(Store store,
703 const MemRegion * const *I,
704 const MemRegion * const *E,
705 const Expr *Ex, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000706 InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000707 bool invalidateGlobals,
708 InvalidatedRegions *Regions) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000709 InvalidateRegionsWorker W(*this, StateMgr,
710 RegionStoreManager::GetRegionBindings(store),
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000711 Ex, Count, IS, Regions);
Ted Kremenek5499b842010-03-10 16:32:56 +0000712
713 // Scan the bindings and generate the clusters.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000714 W.GenerateClusters(invalidateGlobals);
Ted Kremenek5499b842010-03-10 16:32:56 +0000715
716 // Add I .. E to the worklist.
717 for ( ; I != E; ++I)
718 W.AddToWorkList(*I);
719
720 W.RunWorkList();
721
722 // Return the new bindings.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000723 RegionBindings B = W.getRegionBindings();
724
725 if (invalidateGlobals) {
726 // Bind the non-static globals memory space to a new symbol that we will
727 // use to derive the bindings for all non-static globals.
728 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion();
729 SVal V =
730 ValMgr.getConjuredSymbolVal(/* SymbolTag = */ (void*) GS, Ex,
731 /* symbol type, doesn't matter */ Ctx.IntTy,
732 Count);
733 B = Add(B, BindingKey::Make(GS, BindingKey::Default), V);
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000734
735 // Even if there are no bindings in the global scope, we still need to
736 // record that we touched it.
737 if (Regions)
738 Regions->push_back(GS);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000739 }
740
741 return B.getRoot();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000742}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000743
Ted Kremenek9af46f52009-06-16 22:36:44 +0000744//===----------------------------------------------------------------------===//
745// Extents for regions.
746//===----------------------------------------------------------------------===//
747
Zhongxing Xue884ff82009-11-12 02:48:32 +0000748DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000749 const MemRegion *R,
750 QualType EleTy) {
Jordy Rose32f26562010-07-04 00:00:41 +0000751 SVal Size = cast<SubRegion>(R)->getExtent(ValMgr);
752 SValuator &SVator = ValMgr.getSValuator();
753 const llvm::APSInt *SizeInt = SVator.getKnownValue(state, Size);
754 if (!SizeInt)
755 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Jordy Rose32f26562010-07-04 00:00:41 +0000757 CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000758 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Jordy Rose32f26562010-07-04 00:00:41 +0000760 // If a variable is reinterpreted as a type that doesn't fit into a larger
761 // type evenly, round it down.
762 // This is a signed value, since it's used in arithmetic with signed indices.
763 return ValMgr.makeIntVal(RegionSize / EleSize, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000764}
765
Ted Kremenek9af46f52009-06-16 22:36:44 +0000766//===----------------------------------------------------------------------===//
767// Location and region casting.
768//===----------------------------------------------------------------------===//
769
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000770/// ArrayToPointer - Emulates the "decay" of an array to a pointer
771/// type. 'Array' represents the lvalue of the array being decayed
772/// to a pointer, and the returned SVal represents the decayed
773/// version of that lvalue (i.e., a pointer to the first element of
774/// the array). This is called by GRExprEngine when evaluating casts
775/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000776SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000777 if (!isa<loc::MemRegionVal>(Array))
778 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremenekabb042f2008-12-13 19:24:37 +0000780 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
781 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000783 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000784 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000786 // Strip off typedefs from the ArrayRegion's ValueType.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000787 QualType T = ArrayR->getValueType().getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000788 ArrayType *AT = cast<ArrayType>(T);
789 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenek75185b52009-07-16 00:00:11 +0000791 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000792 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000793}
794
Ted Kremenek9af46f52009-06-16 22:36:44 +0000795//===----------------------------------------------------------------------===//
796// Pointer arithmetic.
797//===----------------------------------------------------------------------===//
798
Zhongxing Xu461147f2010-02-05 05:24:20 +0000799SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000800 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000801 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000802 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000803 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000804
Jordy Roseeac4a002010-06-28 08:26:15 +0000805 // Special case for zero RHS.
806 if (R.isZeroConstant()) {
807 switch (Op) {
808 default:
809 // Handle it normally.
810 break;
811 case BinaryOperator::Add:
812 case BinaryOperator::Sub:
813 // FIXME: does this need to be casted to match resultTy?
814 return L;
815 }
816 }
817
Zhongxing Xua1718c72009-04-03 07:33:13 +0000818 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000819 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000820
Ted Kremenek3bccf082009-07-11 00:58:27 +0000821 switch (MR->getKind()) {
822 case MemRegion::SymbolicRegionKind: {
823 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000824 SymbolRef Sym = SR->getSymbol();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000825 QualType T = Sym->getType(Ctx);
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000826 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000828 if (const PointerType *PT = T->getAs<PointerType>())
829 EleTy = PT->getPointeeType();
830 else
John McCall183700f2009-09-21 23:43:11 +0000831 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Ted Kremenek3bccf082009-07-11 00:58:27 +0000833 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000834 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000835 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000836 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000837 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000838 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000839 QualType EleTy = Ctx.CharTy; // Create an ElementRegion of bytes.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000840 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000841 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000842 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000843 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000844
Ted Kremenek3bccf082009-07-11 00:58:27 +0000845 case MemRegion::ElementRegionKind: {
846 ER = cast<ElementRegion>(MR);
847 break;
848 }
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Ted Kremenek3bccf082009-07-11 00:58:27 +0000850 // Not yet handled.
851 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000852 case MemRegion::StringRegionKind: {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000853
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000854 }
855 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000856 case MemRegion::CompoundLiteralRegionKind:
857 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000858 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000859 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000860 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000862 case MemRegion::FunctionTextRegionKind:
863 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000864 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000865 // Technically this can happen if people do funny things with casts.
866 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Ted Kremenekde0d2632010-01-05 02:18:06 +0000868 case MemRegion::CXXThisRegionKind:
869 assert(0 &&
870 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000871 case MemRegion::GenericMemSpaceRegionKind:
872 case MemRegion::StackLocalsSpaceRegionKind:
873 case MemRegion::StackArgumentsSpaceRegionKind:
874 case MemRegion::HeapSpaceRegionKind:
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000875 case MemRegion::NonStaticGlobalSpaceRegionKind:
876 case MemRegion::StaticGlobalSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000877 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000878 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
879 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000880 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000881
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000882 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000883 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000884
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000885 // For now, only support:
886 // (a) concrete integer indices that can easily be resolved
887 // (b) 0 + symbolic index
888 if (Base) {
889 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
890 // FIXME: Should use SValuator here.
891 SVal NewIdx =
892 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000893 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000894 const MemRegion* NewER =
895 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000896 ER->getSuperRegion(), Ctx);
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000897 return ValMgr.makeLoc(NewER);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000898 }
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000899 if (0 == Base->getValue()) {
900 const MemRegion* NewER =
901 MRMgr.getElementRegion(ER->getElementType(), R,
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000902 ER->getSuperRegion(), Ctx);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000903 return ValMgr.makeLoc(NewER);
904 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000905 }
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Ted Kremenek5dc27462009-03-03 02:51:43 +0000907 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000908}
909
Ted Kremenek9af46f52009-06-16 22:36:44 +0000910//===----------------------------------------------------------------------===//
911// Loading values from regions.
912//===----------------------------------------------------------------------===//
913
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000914Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Zhongxing Xubdfa85f2010-05-29 06:23:24 +0000915 const MemRegion *R) {
Zhongxing Xu42c67bf2010-05-29 06:49:04 +0000916
917 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
918 return *V;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000919
Zhongxing Xu13d50172009-10-11 08:08:02 +0000920 return Optional<SVal>();
921}
922
923Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000924 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000925 if (R->isBoundable())
926 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +0000927 if (TR->getValueType()->isUnionType())
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000928 return UnknownVal();
929
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000930 if (const SVal *V = Lookup(B, R, BindingKey::Default))
931 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +0000932
933 return Optional<SVal>();
934}
935
936Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
937 const MemRegion *R) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000938
Ted Kremenek2cf073b2010-03-30 20:30:52 +0000939 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000940 return V;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000941
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000942 return getDefaultBinding(B, R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000943}
944
Ted Kremeneka6275a52009-07-15 02:31:43 +0000945static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
946 RTy = Ctx.getCanonicalType(RTy);
947 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Ted Kremeneka6275a52009-07-15 02:31:43 +0000949 if (RTy == UsedTy)
950 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000951
952
Ted Kremenek25c54572009-07-20 22:58:02 +0000953 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000954 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000955 // represents a reinterpretation.
956 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000957 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000958 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000959
960 return PUsedTy && PRTy &&
961 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000962 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000963 }
964
965 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000966}
967
Zhongxing Xu576bb922010-02-05 03:01:53 +0000968SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000969 assert(!isa<UnknownVal>(L) && "location unknown");
970 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000971
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000972 // FIXME: Is this even possible? Shouldn't this be treated as a null
973 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000974 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000975 return UndefinedVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000976
Ted Kremenek67f28532009-06-17 22:02:04 +0000977 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000978
Tom Care7b050302010-06-25 18:22:31 +0000979 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR)) {
980 if (T.isNull()) {
981 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000982 T = SR->getSymbol()->getType(Ctx);
Tom Care7b050302010-06-25 18:22:31 +0000983 }
Zhongxing Xu81491852010-02-08 08:43:02 +0000984 MR = GetElementZeroRegion(MR, T);
Tom Care7b050302010-06-25 18:22:31 +0000985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000987 if (isa<CodeTextRegion>(MR)) {
988 assert(0 && "Why load from a code text region?");
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000989 return UnknownVal();
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000992 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
993 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000994 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000995 QualType RTy = R->getValueType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000996
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000997 // FIXME: We should eventually handle funny addressing. e.g.:
998 //
999 // int x = ...;
1000 // int *p = &x;
1001 // char *q = (char*) p;
1002 // char c = *q; // returns the first byte of 'x'.
1003 //
1004 // Such funny addressing will occur due to layering of regions.
1005
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001006#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001007 ASTContext &Ctx = getContext();
1008 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001009 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1010 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001011 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001012 assert(Ctx.getCanonicalType(RTy) ==
1013 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001014 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001015#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001016
Douglas Gregorfb87b892010-04-26 21:31:17 +00001017 if (RTy->isStructureOrClassType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001018 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001020 // FIXME: Handle unions.
1021 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001022 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001023
1024 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001025 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001026
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001027 // FIXME: handle Vector types.
1028 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001029 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001030
1031 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +00001032 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001033
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001034 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1035 // FIXME: Here we actually perform an implicit conversion from the loaded
1036 // value to the element type. Eventually we want to compose these values
1037 // more intelligently. For example, an 'element' can encompass multiple
1038 // bound regions (e.g., several bound bytes), or could be a subset of
1039 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001040 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001043 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1044 // FIXME: Here we actually perform an implicit conversion from the loaded
1045 // value to the ivar type. What we should model is stores to ivars
1046 // that blow past the extent of the ivar. If the address of the ivar is
1047 // reinterpretted, it is possible we stored a different value that could
1048 // fit within the ivar. Either we need to cast these when storing them
1049 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001050 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001053 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1054 // FIXME: Here we actually perform an implicit conversion from the loaded
1055 // value to the variable type. What we should model is stores to variables
1056 // that blow past the extent of the variable. If the address of the
1057 // variable is reinterpretted, it is possible we stored a different value
1058 // that could fit within the variable. Either we need to cast these when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001059 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001060 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001061 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001062
Zhongxing Xu576bb922010-02-05 03:01:53 +00001063 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001064 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001065
1066 // Check if the region has a binding.
1067 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001068 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001069
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001070 // The location does not have a bound value. This means that it has
1071 // the value it had upon its creation and/or entry to the analyzed
1072 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001073 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001074 // All stack variables are considered to have undefined values
1075 // upon creation. All heap allocated blocks are considered to
1076 // have undefined values as well unless they are explicitly bound
1077 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001078 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001079 }
1080
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001081 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001082 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001083}
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001085std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001086RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001087 if (Optional<SVal> OV = getDirectBinding(B, R))
1088 if (const nonloc::LazyCompoundVal *V =
1089 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001090 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001092 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001093 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001094 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001096 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001097 return std::make_pair(X.first,
1098 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001099 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001100 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001101 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001102 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001104 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001105 return std::make_pair(X.first,
1106 MRMgr.getFieldRegionWithSuper(FR, X.second));
1107 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001108 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xudcbcbdc2010-02-10 02:02:10 +00001109 // possible for a valid lazy binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001110 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001111}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001112
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001113SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001114 const ElementRegion* R) {
1115 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001116 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001117 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001118 return *V;
1119
Ted Kremenek921109a2009-07-01 23:19:52 +00001120 const MemRegion* superR = R->getSuperRegion();
1121
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001122 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001123 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001124 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001125 // an integer, e.g., *((unsigned int*)"hello")
Zhongxing Xu018220c2010-08-11 06:10:55 +00001126 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001127 if (T != Ctx.getCanonicalType(R->getElementType()))
1128 return UnknownVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001129
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001130 const StringLiteral *Str = StrR->getStringLiteral();
1131 SVal Idx = R->getIndex();
1132 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1133 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001134 int64_t byteLength = Str->getByteLength();
Jordy Rose167cc372010-07-29 06:40:33 +00001135 // Technically, only i == byteLength is guaranteed to be null.
1136 // However, such overflows should be caught before reaching this point;
1137 // the only time such an access would be made is if a string literal was
1138 // used to initialize a larger array.
1139 char c = (i >= byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001140 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001141 }
1142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Ted Kremeneka709b872010-05-31 01:22:04 +00001144 // Handle the case where we are indexing into a larger scalar object.
1145 // For example, this handles:
1146 // int x = ...
1147 // char *y = &x;
1148 // return *y;
1149 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001150 const RegionRawOffset &O = R->getAsArrayOffset();
Ted Kremeneka709b872010-05-31 01:22:04 +00001151 if (const TypedRegion *baseR = dyn_cast_or_null<TypedRegion>(O.getRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001152 QualType baseT = baseR->getValueType();
Ted Kremeneka709b872010-05-31 01:22:04 +00001153 if (baseT->isScalarType()) {
1154 QualType elemT = R->getElementType();
1155 if (elemT->isScalarType()) {
1156 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1157 if (const Optional<SVal> &V = getDirectBinding(B, superR)) {
1158 if (SymbolRef parentSym = V->getAsSymbol())
1159 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001160
Ted Kremeneka709b872010-05-31 01:22:04 +00001161 if (V->isUnknownOrUndef())
1162 return *V;
1163 // Other cases: give up. We are indexing into a larger object
1164 // that has some value, but we don't know how to handle that yet.
1165 return UnknownVal();
1166 }
1167 }
1168 }
Zhongxing Xu42c67bf2010-05-29 06:49:04 +00001169 }
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001170 }
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001171 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001172}
1173
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001174SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001175 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001176
1177 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001178 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001179 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001180 return *V;
1181
Zhongxing Xu018220c2010-08-11 06:10:55 +00001182 QualType Ty = R->getValueType();
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001183 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001184}
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001186Optional<SVal>
1187RegionStoreManager::RetrieveDerivedDefaultValue(RegionBindings B,
1188 const MemRegion *superR,
1189 const TypedRegion *R,
1190 QualType Ty) {
1191
1192 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
1193 if (SymbolRef parentSym = D->getAsSymbol())
1194 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1195
1196 if (D->isZeroConstant())
1197 return ValMgr.makeZeroVal(Ty);
1198
1199 if (D->isUnknownOrUndef())
1200 return *D;
1201
1202 assert(0 && "Unknown default value");
1203 }
1204
1205 return Optional<SVal>();
1206}
1207
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001208SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001209 const TypedRegion *R,
1210 QualType Ty,
1211 const MemRegion *superR) {
1212
Mike Stump1eb44332009-09-09 15:08:12 +00001213 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001214 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001216 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001218 while (superR) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001219 if (const Optional<SVal> &D = RetrieveDerivedDefaultValue(B, superR, R, Ty))
1220 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001222 // If our super region is a field or element itself, walk up the region
1223 // hierarchy to see if there is a default value installed in an ancestor.
1224 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1225 superR = cast<SubRegion>(superR)->getSuperRegion();
1226 continue;
1227 }
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001229 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001230 }
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001232 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001233 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001234 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001235 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001237 if (lazyBindingRegion) {
1238 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1239 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001240 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001241 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001242 }
1243
Ted Kremenekde0d2632010-01-05 02:18:06 +00001244 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001245 if (isa<ElementRegion>(R)) {
1246 // Currently we don't reason specially about Clang-style vectors. Check
1247 // if superR is a vector and if so return Unknown.
1248 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001249 if (typedSuperR->getValueType()->isVectorType())
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001250 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001251 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001252 }
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001254 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001255 }
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001257 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001258 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001259}
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Zhongxing Xu576bb922010-02-05 03:01:53 +00001261SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001262
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001263 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001264 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001265
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001266 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001267 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001269 const MemRegion *superR = R->getSuperRegion();
1270
Ted Kremenekab22ee92009-10-20 01:20:57 +00001271 // Check if the super region has a default binding.
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001272 if (const Optional<SVal> &V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001273 if (SymbolRef parentSym = V->getAsSymbol())
1274 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001276 // Other cases: give up.
1277 return UnknownVal();
1278 }
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Zhongxing Xu576bb922010-02-05 03:01:53 +00001280 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001281}
1282
Zhongxing Xu576bb922010-02-05 03:01:53 +00001283SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Ted Kremenek9031dd72009-07-21 00:12:07 +00001285 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001286 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001288 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001289 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Ted Kremenek9031dd72009-07-21 00:12:07 +00001291 // Lazily derive a value for the VarRegion.
1292 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001293 QualType T = VD->getType();
1294 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001295
1296 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek4dc15662010-02-06 03:57:59 +00001297 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu14d23282010-03-01 06:56:52 +00001298 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Ted Kremenek4dc15662010-02-06 03:57:59 +00001300 if (isa<GlobalsSpaceRegion>(MS)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001301 if (isa<NonStaticGlobalSpaceRegion>(MS)) {
Ted Kremenek4552ff02010-03-30 20:31:04 +00001302 // Is 'VD' declared constant? If so, retrieve the constant value.
1303 QualType CT = Ctx.getCanonicalType(T);
1304 if (CT.isConstQualified()) {
1305 const Expr *Init = VD->getInit();
1306 // Do the null check first, as we want to call 'IgnoreParenCasts'.
1307 if (Init)
1308 if (const IntegerLiteral *IL =
1309 dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) {
1310 const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL);
1311 return ValMgr.getSValuator().EvalCast(V, Init->getType(),
1312 IL->getType());
1313 }
1314 }
1315
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001316 if (const Optional<SVal> &V = RetrieveDerivedDefaultValue(B, MS, R, CT))
1317 return V.getValue();
1318
Zhongxing Xu14d23282010-03-01 06:56:52 +00001319 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek4552ff02010-03-30 20:31:04 +00001320 }
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Ted Kremenek4dc15662010-02-06 03:57:59 +00001322 if (T->isIntegerType())
1323 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001324 if (T->isPointerType())
1325 return ValMgr.makeNull();
1326
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001327 return UnknownVal();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001328 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001329
Ted Kremenek9031dd72009-07-21 00:12:07 +00001330 return UndefinedVal();
1331}
1332
Zhongxing Xu576bb922010-02-05 03:01:53 +00001333SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001334 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001335 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001336}
1337
Zhongxing Xu576bb922010-02-05 03:01:53 +00001338SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001339 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001340 assert(T->isStructureOrClassType());
Zhongxing Xu576bb922010-02-05 03:01:53 +00001341 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001342}
1343
Zhongxing Xu576bb922010-02-05 03:01:53 +00001344SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001345 assert(isa<ConstantArrayType>(R->getValueType()));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001346 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001347}
1348
Ted Kremenek9af46f52009-06-16 22:36:44 +00001349//===----------------------------------------------------------------------===//
1350// Binding values to regions.
1351//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001352
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001353Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001354 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001355 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001356 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Ted Kremenek0964a062009-01-21 06:57:53 +00001358 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001359}
1360
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001361Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001362 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001363 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001364
Ted Kremenek9af46f52009-06-16 22:36:44 +00001365 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001366 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Ted Kremenek9af46f52009-06-16 22:36:44 +00001368 // Check if the region is a struct region.
1369 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +00001370 if (TR->getValueType()->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001371 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001373 // Special case: the current region represents a cast and it and the super
1374 // region both have pointer types or intptr_t types. If so, perform the
1375 // bind to the super region.
1376 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001377 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001378 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1379 if (ER->getIndex().isZeroConstant()) {
1380 if (const TypedRegion *superR =
1381 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001382 QualType superTy = superR->getValueType();
1383 QualType erTy = ER->getValueType();
Mike Stump1eb44332009-09-09 15:08:12 +00001384
1385 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001386 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +00001387 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001388 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001389 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001390 // For now, just invalidate the fields of the struct/union/class.
1391 // FIXME: Precisely handle the fields of the record.
Jordy Rose58f8b202010-08-05 03:28:45 +00001392 if (superTy->isStructureOrClassType())
1393 return KillStruct(store, superR, UnknownVal());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001394 }
1395 }
1396 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001397 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1398 // Binding directly to a symbolic region should be treated as binding
1399 // to element 0.
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001400 QualType T = SR->getSymbol()->getType(Ctx);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001401
Ted Kremenek852274d2009-12-16 03:18:58 +00001402 // FIXME: Is this the right way to handle symbols that are references?
1403 if (const PointerType *PT = T->getAs<PointerType>())
1404 T = PT->getPointeeType();
1405 else
1406 T = T->getAs<ReferenceType>()->getPointeeType();
1407
Ted Kremenek0954cde2009-09-24 04:11:44 +00001408 R = GetElementZeroRegion(SR, T);
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001411 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001412 RegionBindings B = GetRegionBindings(store);
1413 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001414}
1415
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001416Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001417 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001418
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001419 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001420
Ted Kremenek0964a062009-01-21 06:57:53 +00001421 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001422 return BindArray(store, VR, InitVal);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001423 if (T->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001424 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001425
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001426 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001427}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001428
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001429// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001430Store RegionStoreManager::BindCompoundLiteral(Store store,
1431 const CompoundLiteralExpr *CL,
1432 const LocationContext *LC,
1433 SVal V) {
1434 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001435 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001436}
1437
Zhongxing Xua5ce9662010-06-01 03:01:33 +00001438
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001439Store RegionStoreManager::setImplicitDefaultValue(Store store,
1440 const MemRegion *R,
1441 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001442 RegionBindings B = GetRegionBindings(store);
1443 SVal V;
1444
1445 if (Loc::IsLocType(T))
1446 V = ValMgr.makeNull();
1447 else if (T->isIntegerType())
1448 V = ValMgr.makeZeroVal(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001449 else if (T->isStructureOrClassType() || T->isArrayType()) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001450 // Set the default value to a zero constant when it is a structure
1451 // or array. The type doesn't really matter.
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001452 V = ValMgr.makeZeroVal(Ctx.IntTy);
Ted Kremenek027e2662009-11-19 20:20:24 +00001453 }
1454 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001455 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001456 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001457
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001458 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001459}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001460
1461Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001462 SVal Init) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001463
Zhongxing Xu018220c2010-08-11 06:10:55 +00001464 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001465 QualType ElementTy = AT->getElementType();
Ted Kremenekfee90812010-01-26 23:51:00 +00001466 Optional<uint64_t> Size;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001467
Ted Kremenekfee90812010-01-26 23:51:00 +00001468 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1469 Size = CAT->getSize().getZExtValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001470
Jordy Rose167cc372010-07-29 06:40:33 +00001471 // Check if the init expr is a string literal.
1472 if (loc::MemRegionVal *MRV = dyn_cast<loc::MemRegionVal>(&Init)) {
1473 const StringRegion *S = cast<StringRegion>(MRV->getRegion());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001474
Jordy Rose167cc372010-07-29 06:40:33 +00001475 // Treat the string as a lazy compound value.
1476 nonloc::LazyCompoundVal LCV =
1477 cast<nonloc::LazyCompoundVal>(ValMgr.makeLazyCompoundVal(store, S));
1478 return CopyLazyBindings(LCV, store, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001479 }
1480
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001481 // Handle lazy compound values.
1482 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001483 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001484
1485 // Remaining case: explicit compound values.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001486
Ted Kremenek027e2662009-11-19 20:20:24 +00001487 if (Init.isUnknown())
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001488 return setImplicitDefaultValue(store, R, ElementTy);
1489
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001490 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001491 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001492 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Ted Kremenekfee90812010-01-26 23:51:00 +00001494 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001495 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001496 if (VI == VE)
1497 break;
1498
Ted Kremenek46537392009-07-16 01:33:37 +00001499 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001500 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001501
Douglas Gregorfb87b892010-04-26 21:31:17 +00001502 if (ElementTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001503 store = BindStruct(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001504 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001505 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001506 }
1507
Ted Kremenek027e2662009-11-19 20:20:24 +00001508 // If the init list is shorter than the array length, set the
1509 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001510 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001511 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001512
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001513 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001514}
1515
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001516Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1517 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Ted Kremenek67f28532009-06-17 22:02:04 +00001519 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001520 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Zhongxing Xu018220c2010-08-11 06:10:55 +00001522 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001523 assert(T->isStructureOrClassType());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001524
Ted Kremenek6217b802009-07-29 21:53:49 +00001525 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001526 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001527
1528 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001529 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001530
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001531 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001532 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001533 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001535 // We may get non-CompoundVal accidentally due to imprecise cast logic or
1536 // that we are binding symbolic struct value. Kill the field values, and if
1537 // the value is symbolic go and bind it as a "default" binding.
Ted Kremenek67f28532009-06-17 22:02:04 +00001538 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001539 return KillStruct(store, R, isa<nonloc::SymbolVal>(V) ? V : UnknownVal());
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001540
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001541 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001542 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001543
1544 RecordDecl::field_iterator FI, FE;
1545
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001546 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001547
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001548 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001549 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001550
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001551 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001552 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001553
Ted Kremenekcf549592009-09-22 21:19:14 +00001554 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001555 store = BindArray(store, FR, *VI);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001556 else if (FTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001557 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001558 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001559 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001560 }
1561
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001562 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001563 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001564 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001565 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001566 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001567 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001568
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001569 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001570}
1571
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001572Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R,
1573 SVal DefaultVal) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001574 RegionBindings B = GetRegionBindings(store);
1575 llvm::OwningPtr<RegionStoreSubRegionMap>
1576 SubRegions(getRegionStoreSubRegionMap(store));
1577 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001578
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001579 // Set the default value of the struct region to "unknown".
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001580 return Add(B, R, BindingKey::Default, DefaultVal).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001581}
1582
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001583Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1584 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001585
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001586 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001587 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001588
Mike Stump1eb44332009-09-09 15:08:12 +00001589 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001590 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001591
Mike Stump1eb44332009-09-09 15:08:12 +00001592 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001593 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001595 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1596 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001597 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001598}
1599
1600//===----------------------------------------------------------------------===//
1601// "Raw" retrievals and bindings.
1602//===----------------------------------------------------------------------===//
1603
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001604BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001605 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001606 const RegionRawOffset &O = ER->getAsArrayOffset();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001607
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001608 if (O.getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001609 return BindingKey(O.getRegion(), O.getByteOffset(), k);
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
1612 // raw offsets yet, including regions with symbolic offsets.
1613 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001614
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001615 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001616}
1617
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001618RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001619 return RBFactory.Add(B, K, V);
1620}
1621
1622RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001623 BindingKey::Kind k, SVal V) {
1624 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001625}
1626
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001627const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001628 return B.lookup(K);
1629}
1630
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001631const SVal *RegionStoreManager::Lookup(RegionBindings B,
1632 const MemRegion *R,
1633 BindingKey::Kind k) {
1634 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001635}
1636
1637RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1638 return RBFactory.Remove(B, K);
1639}
1640
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001641RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1642 BindingKey::Kind k){
1643 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001644}
1645
1646Store RegionStoreManager::Remove(Store store, BindingKey K) {
1647 RegionBindings B = GetRegionBindings(store);
1648 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001649}
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Ted Kremenek9af46f52009-06-16 22:36:44 +00001651//===----------------------------------------------------------------------===//
1652// State pruning.
1653//===----------------------------------------------------------------------===//
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001654
Ted Kremenek5499b842010-03-10 16:32:56 +00001655namespace {
1656class RemoveDeadBindingsWorker :
1657 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1658 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1659 SymbolReaper &SymReaper;
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001660 const StackFrameContext *CurrentLCtx;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001661
Ted Kremenek5499b842010-03-10 16:32:56 +00001662public:
1663 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1664 RegionBindings b, SymbolReaper &symReaper,
Jordy Rose7dadf792010-07-01 20:09:55 +00001665 const StackFrameContext *LCtx)
Ted Kremenek5499b842010-03-10 16:32:56 +00001666 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
Jordy Rose7dadf792010-07-01 20:09:55 +00001667 SymReaper(symReaper), CurrentLCtx(LCtx) {}
Ted Kremenek5499b842010-03-10 16:32:56 +00001668
1669 // Called by ClusterAnalysis.
1670 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1671 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek5499b842010-03-10 16:32:56 +00001672
Ted Kremenek75a2d942010-04-01 00:15:55 +00001673 void VisitBindingKey(BindingKey K);
Ted Kremenek5499b842010-03-10 16:32:56 +00001674 bool UpdatePostponed();
1675 void VisitBinding(SVal V);
1676};
1677}
1678
1679void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1680 RegionCluster &C) {
1681
1682 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
Jordy Rose7dadf792010-07-01 20:09:55 +00001683 if (SymReaper.isLive(VR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001684 AddToWorkList(baseR, C);
1685
1686 return;
1687 }
1688
1689 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1690 if (SymReaper.isLive(SR->getSymbol()))
1691 AddToWorkList(SR, C);
1692 else
1693 Postponed.push_back(SR);
1694
1695 return;
1696 }
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001697
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001698 if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
1699 AddToWorkList(baseR, C);
1700 return;
1701 }
1702
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001703 // CXXThisRegion in the current or parent location context is live.
1704 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001705 const StackArgumentsSpaceRegion *StackReg =
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001706 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
1707 const StackFrameContext *RegCtx = StackReg->getStackFrame();
1708 if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
1709 AddToWorkList(TR, C);
1710 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001711}
1712
1713void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1714 BindingKey *I, BindingKey *E) {
Ted Kremenek75a2d942010-04-01 00:15:55 +00001715 for ( ; I != E; ++I)
1716 VisitBindingKey(*I);
Ted Kremenek5499b842010-03-10 16:32:56 +00001717}
1718
1719void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1720 // Is it a LazyCompoundVal? All referenced regions are live as well.
1721 if (const nonloc::LazyCompoundVal *LCS =
1722 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1723
1724 const MemRegion *LazyR = LCS->getRegion();
1725 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1726 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +00001727 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
1728 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001729 VisitBinding(RI.getData());
1730 }
1731 return;
1732 }
1733
1734 // If V is a region, then add it to the worklist.
1735 if (const MemRegion *R = V.getAsRegion())
1736 AddToWorkList(R);
1737
1738 // Update the set of live symbols.
1739 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1740 SI!=SE;++SI)
1741 SymReaper.markLive(*SI);
1742}
1743
Ted Kremenek75a2d942010-04-01 00:15:55 +00001744void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) {
1745 const MemRegion *R = K.getRegion();
1746
Ted Kremenek5499b842010-03-10 16:32:56 +00001747 // Mark this region "live" by adding it to the worklist. This will cause
1748 // use to visit all regions in the cluster (if we haven't visited them
1749 // already).
Ted Kremenek75a2d942010-04-01 00:15:55 +00001750 if (AddToWorkList(R)) {
1751 // Mark the symbol for any live SymbolicRegion as "live". This means we
1752 // should continue to track that symbol.
1753 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1754 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek5499b842010-03-10 16:32:56 +00001755
Ted Kremenek75a2d942010-04-01 00:15:55 +00001756 // For BlockDataRegions, enqueue the VarRegions for variables marked
1757 // with __block (passed-by-reference).
1758 // via BlockDeclRefExprs.
1759 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1760 for (BlockDataRegion::referenced_vars_iterator
1761 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1762 RI != RE; ++RI) {
1763 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1764 AddToWorkList(*RI);
1765 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001766
Ted Kremenek75a2d942010-04-01 00:15:55 +00001767 // No possible data bindings on a BlockDataRegion.
1768 return;
Ted Kremenek5499b842010-03-10 16:32:56 +00001769 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001770 }
1771
Ted Kremenek75a2d942010-04-01 00:15:55 +00001772 // Visit the data binding for K.
1773 if (const SVal *V = RM.Lookup(B, K))
Ted Kremenek5499b842010-03-10 16:32:56 +00001774 VisitBinding(*V);
1775}
1776
1777bool RemoveDeadBindingsWorker::UpdatePostponed() {
1778 // See if any postponed SymbolicRegions are actually live now, after
1779 // having done a scan.
1780 bool changed = false;
1781
1782 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1783 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1784 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1785 if (SymReaper.isLive(SR->getSymbol())) {
1786 changed |= AddToWorkList(SR);
1787 *I = NULL;
1788 }
1789 }
1790 }
1791
1792 return changed;
1793}
1794
Jordy Rose7dadf792010-07-01 20:09:55 +00001795const GRState *RegionStoreManager::RemoveDeadBindings(GRState &state,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001796 const StackFrameContext *LCtx,
Zhongxing Xu72119c42010-02-05 05:34:29 +00001797 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001798 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001799{
Zhongxing Xu95798982010-05-26 03:27:35 +00001800 RegionBindings B = GetRegionBindings(state.getStore());
Jordy Rose7dadf792010-07-01 20:09:55 +00001801 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
Ted Kremenek5499b842010-03-10 16:32:56 +00001802 W.GenerateClusters();
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Ted Kremenek5499b842010-03-10 16:32:56 +00001804 // Enqueue the region roots onto the worklist.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001805 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenek5499b842010-03-10 16:32:56 +00001806 E=RegionRoots.end(); I!=E; ++I)
1807 W.AddToWorkList(*I);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001808
Ted Kremenek5499b842010-03-10 16:32:56 +00001809 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001810
Ted Kremenek9af46f52009-06-16 22:36:44 +00001811 // We have now scanned the store, marking reachable regions and symbols
1812 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001813 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001814 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek5499b842010-03-10 16:32:56 +00001815 const BindingKey &K = I.getKey();
1816
Ted Kremenekb7118f72010-03-10 16:38:41 +00001817 // If the cluster has been visited, we know the region has been marked.
Ted Kremenek5499b842010-03-10 16:32:56 +00001818 if (W.isVisited(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001819 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Ted Kremenek5499b842010-03-10 16:32:56 +00001821 // Remove the dead entry.
1822 B = Remove(B, K);
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Ted Kremenek5499b842010-03-10 16:32:56 +00001824 // Mark all non-live symbols that this binding references as dead.
1825 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001826 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001828 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001829 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1830 for (; SI != SE; ++SI)
1831 SymReaper.maybeDead(*SI);
1832 }
Zhongxing Xu95798982010-05-26 03:27:35 +00001833 state.setStore(B.getRoot());
1834 const GRState *s = StateMgr.getPersistentState(state);
Zhongxing Xu95798982010-05-26 03:27:35 +00001835 return s;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001836}
1837
Ted Kremenek5499b842010-03-10 16:32:56 +00001838
Jordy Roseff59efd2010-08-03 20:44:35 +00001839Store RegionStoreManager::EnterStackFrame(const GRState *state,
1840 const StackFrameContext *frame) {
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001841 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001842 FunctionDecl::param_const_iterator PI = FD->param_begin();
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001843 Store store = state->getStore();
Zhongxing Xuc5063572010-03-16 13:14:16 +00001844
1845 if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) {
1846 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1847
1848 // Copy the arg expression value to the arg variables.
1849 for (; AI != AE; ++AI, ++PI) {
1850 SVal ArgVal = state->getSVal(*AI);
1851 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1852 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001853 } else if (const CXXConstructExpr *CE =
Zhongxing Xuc5063572010-03-16 13:14:16 +00001854 dyn_cast<CXXConstructExpr>(frame->getCallSite())) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001855 CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(),
Zhongxing Xuc5063572010-03-16 13:14:16 +00001856 AE = CE->arg_end();
1857
1858 // Copy the arg expression value to the arg variables.
1859 for (; AI != AE; ++AI, ++PI) {
1860 SVal ArgVal = state->getSVal(*AI);
1861 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1862 }
1863 } else
Jordy Roseff59efd2010-08-03 20:44:35 +00001864 llvm_unreachable("Unhandled call expression.");
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001865
Jordy Roseff59efd2010-08-03 20:44:35 +00001866 return store;
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001867}
1868
Ted Kremenek9af46f52009-06-16 22:36:44 +00001869//===----------------------------------------------------------------------===//
1870// Utility methods.
1871//===----------------------------------------------------------------------===//
1872
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001873void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001874 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001875 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001876 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Ted Kremenek451ac092009-08-06 04:50:20 +00001878 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001879 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001880}