blob: 221406e1f303e787e71f87784239803a867aa5d9 [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,
234 bool invalidateGlobals);
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000236public: // Made public for helper classes.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000237
Zhongxing Xu13d50172009-10-11 08:08:02 +0000238 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000239 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000241 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
242
243 RegionBindings Add(RegionBindings B, const MemRegion *R,
244 BindingKey::Kind k, SVal V);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000245
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000246 const SVal *Lookup(RegionBindings B, BindingKey K);
247 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000248
249 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000250 RegionBindings Remove(RegionBindings B, const MemRegion *R,
251 BindingKey::Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000252
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000253 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
254 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000255 }
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000256
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000257 Store Remove(Store store, BindingKey K);
258
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000259public: // Part of public interface to class.
260
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000261 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000262
Zhongxing Xu54460092010-06-01 04:49:26 +0000263 // BindDefault is only used to initialize a region with a default value.
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000264 Store BindDefault(Store store, const MemRegion *R, SVal V) {
Zhongxing Xu54460092010-06-01 04:49:26 +0000265 RegionBindings B = GetRegionBindings(store);
266 assert(!Lookup(B, R, BindingKey::Default));
267 assert(!Lookup(B, R, BindingKey::Direct));
268 return Add(B, R, BindingKey::Default, V).getRoot();
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000269 }
270
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000271 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
272 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000274 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000275
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000276 Store BindDeclWithNoInit(Store store, const VarRegion *) {
277 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000278 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000279
Ted Kremenek67f28532009-06-17 22:02:04 +0000280 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000281 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000283 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000284
285 /// KillStruct - Set the entire struct to unknown.
Ted Kremenek281e9dc2010-07-29 00:28:47 +0000286 Store KillStruct(Store store, const TypedRegion* R, SVal DefaultVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000287
Ted Kremenek67f28532009-06-17 22:02:04 +0000288 Store Remove(Store store, Loc LV);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000289
Ted Kremenek67f28532009-06-17 22:02:04 +0000290
291 //===------------------------------------------------------------------===//
292 // Loading values from regions.
293 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenek67f28532009-06-17 22:02:04 +0000295 /// The high level logic for this method is this:
296 /// Retrieve (L)
297 /// if L has binding
298 /// return L's binding
299 /// else if L is in killset
300 /// return unknown
301 /// else
302 /// if L is on stack or heap
303 /// return undefined
304 /// else
305 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000306 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000307
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000308 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000309
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000310 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Zhongxing Xu576bb922010-02-05 03:01:53 +0000312 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Zhongxing Xu576bb922010-02-05 03:01:53 +0000314 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Zhongxing Xu576bb922010-02-05 03:01:53 +0000316 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000318 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000319 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenek67f28532009-06-17 22:02:04 +0000321 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000322 /// struct copy:
323 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000324 /// x = y;
325 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000326 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Zhongxing Xu576bb922010-02-05 03:01:53 +0000328 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000330 /// Used to lazily generate derived symbols for bindings that are defined
331 /// implicitly by default bindings in a super region.
332 Optional<SVal> RetrieveDerivedDefaultValue(RegionBindings B,
333 const MemRegion *superR,
334 const TypedRegion *R, QualType Ty);
335
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000336 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000337 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000338 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000340 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
341 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000342
343 //===------------------------------------------------------------------===//
344 // State pruning.
345 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenek67f28532009-06-17 22:02:04 +0000347 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
348 /// It returns a new Store with these values removed.
Jordy Rose7dadf792010-07-01 20:09:55 +0000349 const GRState *RemoveDeadBindings(GRState &state,
Zhongxing Xu95798982010-05-26 03:27:35 +0000350 const StackFrameContext *LCtx,
351 SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000352 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
353
Jordy Roseff59efd2010-08-03 20:44:35 +0000354 Store EnterStackFrame(const GRState *state, const StackFrameContext *frame);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000355
Ted Kremenek67f28532009-06-17 22:02:04 +0000356 //===------------------------------------------------------------------===//
357 // Region "extents".
358 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Jordy Rose32f26562010-07-04 00:00:41 +0000360 // FIXME: This method will soon be eliminated; see the note in Store.h.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000361 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000362 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000363
364 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000365 // Utility methods.
366 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek451ac092009-08-06 04:50:20 +0000368 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000369 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000370 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000371
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000372 void print(Store store, llvm::raw_ostream& Out, const char* nl,
373 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000374
375 void iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek0e9910f2010-06-17 00:24:42 +0000376 RegionBindings B = GetRegionBindings(store);
377 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
378 const BindingKey &K = I.getKey();
379 if (!K.isDirect())
380 continue;
381 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) {
382 // FIXME: Possibly incorporate the offset?
383 if (!f.HandleBinding(*this, store, R, I.getData()))
384 return;
385 }
386 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek67f28532009-06-17 22:02:04 +0000389 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000390 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000391};
392
393} // end anonymous namespace
394
Ted Kremenek9af46f52009-06-16 22:36:44 +0000395//===----------------------------------------------------------------------===//
396// RegionStore creation.
397//===----------------------------------------------------------------------===//
398
399StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
400 RegionStoreFeatures F = maximal_features_tag();
401 return new RegionStoreManager(StMgr, F);
402}
403
404StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
405 RegionStoreFeatures F = minimal_features_tag();
406 F.enableFields(true);
407 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000408}
409
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000410void
411RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000412 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000413 const MemRegion *superR = R->getSuperRegion();
414 if (add(superR, R))
415 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000416 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000417}
418
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000419RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000420RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
421 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000422 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000424 llvm::SmallVector<const SubRegion*, 10> WL;
425
Ted Kremenek451ac092009-08-06 04:50:20 +0000426 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000427 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000428 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Mike Stump1eb44332009-09-09 15:08:12 +0000430 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000431 // don't have direct bindings but are super regions of those that do.
432 while (!WL.empty()) {
433 const SubRegion *R = WL.back();
434 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000435 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000436 }
437
Ted Kremenek14453bf2009-03-03 19:02:42 +0000438 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000439}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000440
Ted Kremenek9af46f52009-06-16 22:36:44 +0000441//===----------------------------------------------------------------------===//
Ted Kremeneka4fab032010-03-10 07:19:59 +0000442// Region Cluster analysis.
443//===----------------------------------------------------------------------===//
444
445namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000446template <typename DERIVED>
Ted Kremeneka4fab032010-03-10 07:19:59 +0000447class ClusterAnalysis {
448protected:
449 typedef BumpVector<BindingKey> RegionCluster;
450 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5499b842010-03-10 16:32:56 +0000451 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
452 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
453 WorkList;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000454
455 BumpVectorContext BVC;
456 ClusterMap ClusterM;
Ted Kremenek5499b842010-03-10 16:32:56 +0000457 WorkList WL;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000458
459 RegionStoreManager &RM;
460 ASTContext &Ctx;
461 ValueManager &ValMgr;
462
Ted Kremenek5499b842010-03-10 16:32:56 +0000463 RegionBindings B;
464
Ted Kremeneka4fab032010-03-10 07:19:59 +0000465public:
Ted Kremenek5499b842010-03-10 16:32:56 +0000466 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
467 RegionBindings b)
468 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
469 B(b) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000470
Ted Kremenek5499b842010-03-10 16:32:56 +0000471 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000472
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000473 RegionCluster &AddToCluster(BindingKey K) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000474 const MemRegion *R = K.getRegion();
475 const MemRegion *baseR = R->getBaseRegion();
476 RegionCluster &C = getCluster(baseR);
477 C.push_back(K, BVC);
478 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000479 return C;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000480 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000481
Ted Kremenek5499b842010-03-10 16:32:56 +0000482 bool isVisited(const MemRegion *R) {
483 return (bool) Visited[&getCluster(R->getBaseRegion())];
484 }
485
486 RegionCluster& getCluster(const MemRegion *R) {
487 RegionCluster *&CRef = ClusterM[R];
488 if (!CRef) {
489 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
490 CRef = new (Mem) RegionCluster(BVC, 10);
491 }
492 return *CRef;
493 }
494
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000495 void GenerateClusters(bool includeGlobals = false) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000496 // Scan the entire set of bindings and make the region clusters.
497 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000498 RegionCluster &C = AddToCluster(RI.getKey());
Ted Kremenek5499b842010-03-10 16:32:56 +0000499 if (const MemRegion *R = RI.getData().getAsRegion()) {
500 // Generate a cluster, but don't add the region to the cluster
501 // if there aren't any bindings.
502 getCluster(R->getBaseRegion());
503 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000504 if (includeGlobals) {
505 const MemRegion *R = RI.getKey().getRegion();
506 if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()))
507 AddToWorkList(R, C);
508 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000509 }
510 }
Ted Kremenek5499b842010-03-10 16:32:56 +0000511
512 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
513 if (unsigned &visited = Visited[&C])
514 return false;
515 else
516 visited = 1;
517
518 WL.push_back(std::make_pair(R, &C));
519 return true;
520 }
521
522 bool AddToWorkList(BindingKey K) {
523 return AddToWorkList(K.getRegion());
524 }
525
526 bool AddToWorkList(const MemRegion *R) {
527 const MemRegion *baseR = R->getBaseRegion();
528 return AddToWorkList(baseR, getCluster(baseR));
529 }
530
531 void RunWorkList() {
532 while (!WL.empty()) {
533 const MemRegion *baseR;
534 RegionCluster *C;
535 llvm::tie(baseR, C) = WL.back();
536 WL.pop_back();
537
538 // First visit the cluster.
539 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
540
Ted Kremenek75a2d942010-04-01 00:15:55 +0000541 // Next, visit the base region.
542 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
Ted Kremenek5499b842010-03-10 16:32:56 +0000543 }
544 }
545
546public:
547 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
548 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
Ted Kremenek75a2d942010-04-01 00:15:55 +0000549 void VisitBaseRegion(const MemRegion *baseR) {}
Ted Kremenek5499b842010-03-10 16:32:56 +0000550};
Ted Kremeneka4fab032010-03-10 07:19:59 +0000551}
552
553//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000554// Binding invalidation.
555//===----------------------------------------------------------------------===//
556
Zhongxing Xu13d50172009-10-11 08:08:02 +0000557void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
558 const MemRegion *R,
559 RegionStoreSubRegionMap &M) {
Ted Kremeneka4fab032010-03-10 07:19:59 +0000560
Ted Kremenekdf165012010-02-02 22:38:47 +0000561 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
562 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
563 I != E; ++I)
564 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000565
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000566 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000567}
568
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000569namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000570class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
571{
572 const Expr *Ex;
573 unsigned Count;
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000574 StoreManager::InvalidatedSymbols *IS;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000575public:
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000576 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenek5499b842010-03-10 16:32:56 +0000577 GRStateManager &stateMgr,
578 RegionBindings b,
579 const Expr *ex, unsigned count,
580 StoreManager::InvalidatedSymbols *is)
581 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
582 Ex(ex), Count(count), IS(is) {}
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
653 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
654 // Invalidate the region by setting its default value to
655 // conjured symbol. The type of the symbol is irrelavant.
656 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
657 Count);
658 B = RM.Add(B, baseR, BindingKey::Default, V);
659 return;
660 }
661
662 if (!baseR->isBoundable())
663 return;
664
665 const TypedRegion *TR = cast<TypedRegion>(baseR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000666 QualType T = TR->getValueType();
Ted Kremenek5499b842010-03-10 16:32:56 +0000667
668 // Invalidate the binding.
669 if (const RecordType *RT = T->getAsStructureType()) {
670 const RecordDecl *RD = RT->getDecl()->getDefinition();
671 // No record definition. There is nothing we can do.
672 if (!RD) {
673 B = RM.Remove(B, baseR);
674 return;
675 }
676
677 // Invalidate the region by setting its default value to
678 // conjured symbol. The type of the symbol is irrelavant.
679 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
680 Count);
681 B = RM.Add(B, baseR, BindingKey::Default, V);
682 return;
683 }
684
685 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
686 // Set the default value of the array to conjured symbol.
687 DefinedOrUnknownSVal V =
688 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
689 B = RM.Add(B, baseR, BindingKey::Default, V);
690 return;
691 }
692
693 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
694 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
695 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000696}
697
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000698Store RegionStoreManager::InvalidateRegions(Store store,
699 const MemRegion * const *I,
700 const MemRegion * const *E,
701 const Expr *Ex, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000702 InvalidatedSymbols *IS,
703 bool invalidateGlobals) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000704 InvalidateRegionsWorker W(*this, StateMgr,
705 RegionStoreManager::GetRegionBindings(store),
706 Ex, Count, IS);
707
708 // Scan the bindings and generate the clusters.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000709 W.GenerateClusters(invalidateGlobals);
Ted Kremenek5499b842010-03-10 16:32:56 +0000710
711 // Add I .. E to the worklist.
712 for ( ; I != E; ++I)
713 W.AddToWorkList(*I);
714
715 W.RunWorkList();
716
717 // Return the new bindings.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000718 RegionBindings B = W.getRegionBindings();
719
720 if (invalidateGlobals) {
721 // Bind the non-static globals memory space to a new symbol that we will
722 // use to derive the bindings for all non-static globals.
723 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion();
724 SVal V =
725 ValMgr.getConjuredSymbolVal(/* SymbolTag = */ (void*) GS, Ex,
726 /* symbol type, doesn't matter */ Ctx.IntTy,
727 Count);
728 B = Add(B, BindingKey::Make(GS, BindingKey::Default), V);
729 }
730
731 return B.getRoot();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000732}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000733
Ted Kremenek9af46f52009-06-16 22:36:44 +0000734//===----------------------------------------------------------------------===//
735// Extents for regions.
736//===----------------------------------------------------------------------===//
737
Zhongxing Xue884ff82009-11-12 02:48:32 +0000738DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000739 const MemRegion *R,
740 QualType EleTy) {
Jordy Rose32f26562010-07-04 00:00:41 +0000741 SVal Size = cast<SubRegion>(R)->getExtent(ValMgr);
742 SValuator &SVator = ValMgr.getSValuator();
743 const llvm::APSInt *SizeInt = SVator.getKnownValue(state, Size);
744 if (!SizeInt)
745 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Jordy Rose32f26562010-07-04 00:00:41 +0000747 CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
748 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Jordy Rose32f26562010-07-04 00:00:41 +0000750 // If a variable is reinterpreted as a type that doesn't fit into a larger
751 // type evenly, round it down.
752 // This is a signed value, since it's used in arithmetic with signed indices.
753 return ValMgr.makeIntVal(RegionSize / EleSize, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000754}
755
Ted Kremenek9af46f52009-06-16 22:36:44 +0000756//===----------------------------------------------------------------------===//
757// Location and region casting.
758//===----------------------------------------------------------------------===//
759
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000760/// ArrayToPointer - Emulates the "decay" of an array to a pointer
761/// type. 'Array' represents the lvalue of the array being decayed
762/// to a pointer, and the returned SVal represents the decayed
763/// version of that lvalue (i.e., a pointer to the first element of
764/// the array). This is called by GRExprEngine when evaluating casts
765/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000766SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000767 if (!isa<loc::MemRegionVal>(Array))
768 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenekabb042f2008-12-13 19:24:37 +0000770 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
771 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000773 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000774 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000776 // Strip off typedefs from the ArrayRegion's ValueType.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000777 QualType T = ArrayR->getValueType().getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000778 ArrayType *AT = cast<ArrayType>(T);
779 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Ted Kremenek75185b52009-07-16 00:00:11 +0000781 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000782 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
783 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000784}
785
Ted Kremenek9af46f52009-06-16 22:36:44 +0000786//===----------------------------------------------------------------------===//
787// Pointer arithmetic.
788//===----------------------------------------------------------------------===//
789
Zhongxing Xu461147f2010-02-05 05:24:20 +0000790SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000791 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000792 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000793 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000794 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000795
Jordy Roseeac4a002010-06-28 08:26:15 +0000796 // Special case for zero RHS.
797 if (R.isZeroConstant()) {
798 switch (Op) {
799 default:
800 // Handle it normally.
801 break;
802 case BinaryOperator::Add:
803 case BinaryOperator::Sub:
804 // FIXME: does this need to be casted to match resultTy?
805 return L;
806 }
807 }
808
Zhongxing Xua1718c72009-04-03 07:33:13 +0000809 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000810 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000811
Ted Kremenek3bccf082009-07-11 00:58:27 +0000812 switch (MR->getKind()) {
813 case MemRegion::SymbolicRegionKind: {
814 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000815 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000816 QualType T = Sym->getType(getContext());
817 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000819 if (const PointerType *PT = T->getAs<PointerType>())
820 EleTy = PT->getPointeeType();
821 else
John McCall183700f2009-09-21 23:43:11 +0000822 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Ted Kremenek3bccf082009-07-11 00:58:27 +0000824 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
825 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000826 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000827 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000828 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000829 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenek3f8612b2010-06-22 23:58:31 +0000830 QualType EleTy = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000831 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
832 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000833 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000834 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000835
Ted Kremenek3bccf082009-07-11 00:58:27 +0000836 case MemRegion::ElementRegionKind: {
837 ER = cast<ElementRegion>(MR);
838 break;
839 }
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Ted Kremenek3bccf082009-07-11 00:58:27 +0000841 // Not yet handled.
842 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000843 case MemRegion::StringRegionKind: {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000844
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000845 }
846 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000847 case MemRegion::CompoundLiteralRegionKind:
848 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000849 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000850 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000851 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000853 case MemRegion::FunctionTextRegionKind:
854 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000855 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000856 // Technically this can happen if people do funny things with casts.
857 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Ted Kremenekde0d2632010-01-05 02:18:06 +0000859 case MemRegion::CXXThisRegionKind:
860 assert(0 &&
861 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000862 case MemRegion::GenericMemSpaceRegionKind:
863 case MemRegion::StackLocalsSpaceRegionKind:
864 case MemRegion::StackArgumentsSpaceRegionKind:
865 case MemRegion::HeapSpaceRegionKind:
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000866 case MemRegion::NonStaticGlobalSpaceRegionKind:
867 case MemRegion::StaticGlobalSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000868 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000869 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
870 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000871 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000872
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000873 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000874 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000875
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000876 // For now, only support:
877 // (a) concrete integer indices that can easily be resolved
878 // (b) 0 + symbolic index
879 if (Base) {
880 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
881 // FIXME: Should use SValuator here.
882 SVal NewIdx =
883 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000884 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000885 const MemRegion* NewER =
886 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
887 ER->getSuperRegion(), getContext());
888 return ValMgr.makeLoc(NewER);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000889 }
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000890 if (0 == Base->getValue()) {
891 const MemRegion* NewER =
892 MRMgr.getElementRegion(ER->getElementType(), R,
893 ER->getSuperRegion(), getContext());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000894 return ValMgr.makeLoc(NewER);
895 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremenek5dc27462009-03-03 02:51:43 +0000898 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000899}
900
Ted Kremenek9af46f52009-06-16 22:36:44 +0000901//===----------------------------------------------------------------------===//
902// Loading values from regions.
903//===----------------------------------------------------------------------===//
904
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000905Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Zhongxing Xubdfa85f2010-05-29 06:23:24 +0000906 const MemRegion *R) {
Zhongxing Xu42c67bf2010-05-29 06:49:04 +0000907
908 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
909 return *V;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000910
Zhongxing Xu13d50172009-10-11 08:08:02 +0000911 return Optional<SVal>();
912}
913
914Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000915 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000916 if (R->isBoundable())
917 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +0000918 if (TR->getValueType()->isUnionType())
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000919 return UnknownVal();
920
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000921 if (const SVal *V = Lookup(B, R, BindingKey::Default))
922 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +0000923
924 return Optional<SVal>();
925}
926
927Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
928 const MemRegion *R) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000929
Ted Kremenek2cf073b2010-03-30 20:30:52 +0000930 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000931 return V;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000932
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000933 return getDefaultBinding(B, R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000934}
935
Ted Kremeneka6275a52009-07-15 02:31:43 +0000936static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
937 RTy = Ctx.getCanonicalType(RTy);
938 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Ted Kremeneka6275a52009-07-15 02:31:43 +0000940 if (RTy == UsedTy)
941 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
943
Ted Kremenek25c54572009-07-20 22:58:02 +0000944 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000945 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000946 // represents a reinterpretation.
947 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000948 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000949 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000950
951 return PUsedTy && PRTy &&
952 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000953 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000954 }
955
956 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000957}
958
Zhongxing Xu576bb922010-02-05 03:01:53 +0000959SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000960 assert(!isa<UnknownVal>(L) && "location unknown");
961 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000962
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000963 // FIXME: Is this even possible? Shouldn't this be treated as a null
964 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000965 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000966 return UndefinedVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000967
Ted Kremenek67f28532009-06-17 22:02:04 +0000968 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000969
Tom Care7b050302010-06-25 18:22:31 +0000970 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR)) {
971 if (T.isNull()) {
972 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
973 T = SR->getSymbol()->getType(getContext());
974 }
Zhongxing Xu81491852010-02-08 08:43:02 +0000975 MR = GetElementZeroRegion(MR, T);
Tom Care7b050302010-06-25 18:22:31 +0000976 }
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000978 if (isa<CodeTextRegion>(MR)) {
979 assert(0 && "Why load from a code text region?");
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000980 return UnknownVal();
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000983 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
984 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000985 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000986 QualType RTy = R->getValueType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000987
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000988 // FIXME: We should eventually handle funny addressing. e.g.:
989 //
990 // int x = ...;
991 // int *p = &x;
992 // char *q = (char*) p;
993 // char c = *q; // returns the first byte of 'x'.
994 //
995 // Such funny addressing will occur due to layering of regions.
996
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000997#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000998 ASTContext &Ctx = getContext();
999 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001000 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1001 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001002 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001003 assert(Ctx.getCanonicalType(RTy) ==
1004 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001005 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001006#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001007
Douglas Gregorfb87b892010-04-26 21:31:17 +00001008 if (RTy->isStructureOrClassType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001009 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001011 // FIXME: Handle unions.
1012 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001013 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001014
1015 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001016 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001017
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001018 // FIXME: handle Vector types.
1019 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001020 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001021
1022 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +00001023 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001024
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001025 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1026 // FIXME: Here we actually perform an implicit conversion from the loaded
1027 // value to the element type. Eventually we want to compose these values
1028 // more intelligently. For example, an 'element' can encompass multiple
1029 // bound regions (e.g., several bound bytes), or could be a subset of
1030 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001031 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001034 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1035 // FIXME: Here we actually perform an implicit conversion from the loaded
1036 // value to the ivar type. What we should model is stores to ivars
1037 // that blow past the extent of the ivar. If the address of the ivar is
1038 // reinterpretted, it is possible we stored a different value that could
1039 // fit within the ivar. Either we need to cast these when storing them
1040 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001041 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001042 }
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001044 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1045 // FIXME: Here we actually perform an implicit conversion from the loaded
1046 // value to the variable type. What we should model is stores to variables
1047 // that blow past the extent of the variable. If the address of the
1048 // variable is reinterpretted, it is possible we stored a different value
1049 // that could fit within the variable. Either we need to cast these when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001050 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001051 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001052 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001053
Zhongxing Xu576bb922010-02-05 03:01:53 +00001054 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001055 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001056
1057 // Check if the region has a binding.
1058 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001059 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001060
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001061 // The location does not have a bound value. This means that it has
1062 // the value it had upon its creation and/or entry to the analyzed
1063 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001064 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001065 // All stack variables are considered to have undefined values
1066 // upon creation. All heap allocated blocks are considered to
1067 // have undefined values as well unless they are explicitly bound
1068 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001069 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001070 }
1071
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001072 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001073 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001074}
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001076std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001077RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001078 if (Optional<SVal> OV = getDirectBinding(B, R))
1079 if (const nonloc::LazyCompoundVal *V =
1080 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001081 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001083 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001084 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001085 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001087 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001088 return std::make_pair(X.first,
1089 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001090 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001091 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001092 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001093 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001095 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001096 return std::make_pair(X.first,
1097 MRMgr.getFieldRegionWithSuper(FR, X.second));
1098 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001099 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xudcbcbdc2010-02-10 02:02:10 +00001100 // possible for a valid lazy binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001101 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001102}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001103
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001104SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001105 const ElementRegion* R) {
1106 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001107 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001108 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001109 return *V;
1110
Ted Kremenek921109a2009-07-01 23:19:52 +00001111 const MemRegion* superR = R->getSuperRegion();
1112
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001113 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001114 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001115 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001116 // an integer, e.g., *((unsigned int*)"hello")
1117 ASTContext &Ctx = getContext();
Zhongxing Xu018220c2010-08-11 06:10:55 +00001118 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001119 if (T != Ctx.getCanonicalType(R->getElementType()))
1120 return UnknownVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001121
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001122 const StringLiteral *Str = StrR->getStringLiteral();
1123 SVal Idx = R->getIndex();
1124 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1125 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001126 int64_t byteLength = Str->getByteLength();
Jordy Rose167cc372010-07-29 06:40:33 +00001127 // Technically, only i == byteLength is guaranteed to be null.
1128 // However, such overflows should be caught before reaching this point;
1129 // the only time such an access would be made is if a string literal was
1130 // used to initialize a larger array.
1131 char c = (i >= byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001132 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001133 }
1134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Ted Kremeneka709b872010-05-31 01:22:04 +00001136 // Handle the case where we are indexing into a larger scalar object.
1137 // For example, this handles:
1138 // int x = ...
1139 // char *y = &x;
1140 // return *y;
1141 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001142 const RegionRawOffset &O = R->getAsArrayOffset();
Ted Kremeneka709b872010-05-31 01:22:04 +00001143 if (const TypedRegion *baseR = dyn_cast_or_null<TypedRegion>(O.getRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001144 QualType baseT = baseR->getValueType();
Ted Kremeneka709b872010-05-31 01:22:04 +00001145 if (baseT->isScalarType()) {
1146 QualType elemT = R->getElementType();
1147 if (elemT->isScalarType()) {
1148 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1149 if (const Optional<SVal> &V = getDirectBinding(B, superR)) {
1150 if (SymbolRef parentSym = V->getAsSymbol())
1151 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001152
Ted Kremeneka709b872010-05-31 01:22:04 +00001153 if (V->isUnknownOrUndef())
1154 return *V;
1155 // Other cases: give up. We are indexing into a larger object
1156 // that has some value, but we don't know how to handle that yet.
1157 return UnknownVal();
1158 }
1159 }
1160 }
Zhongxing Xu42c67bf2010-05-29 06:49:04 +00001161 }
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001162 }
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001163 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001164}
1165
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001166SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001167 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001168
1169 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001170 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001171 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001172 return *V;
1173
Zhongxing Xu018220c2010-08-11 06:10:55 +00001174 QualType Ty = R->getValueType();
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001175 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001176}
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001178Optional<SVal>
1179RegionStoreManager::RetrieveDerivedDefaultValue(RegionBindings B,
1180 const MemRegion *superR,
1181 const TypedRegion *R,
1182 QualType Ty) {
1183
1184 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
1185 if (SymbolRef parentSym = D->getAsSymbol())
1186 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1187
1188 if (D->isZeroConstant())
1189 return ValMgr.makeZeroVal(Ty);
1190
1191 if (D->isUnknownOrUndef())
1192 return *D;
1193
1194 assert(0 && "Unknown default value");
1195 }
1196
1197 return Optional<SVal>();
1198}
1199
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001200SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001201 const TypedRegion *R,
1202 QualType Ty,
1203 const MemRegion *superR) {
1204
Mike Stump1eb44332009-09-09 15:08:12 +00001205 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001206 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001208 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001210 while (superR) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001211 if (const Optional<SVal> &D = RetrieveDerivedDefaultValue(B, superR, R, Ty))
1212 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001214 // If our super region is a field or element itself, walk up the region
1215 // hierarchy to see if there is a default value installed in an ancestor.
1216 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1217 superR = cast<SubRegion>(superR)->getSuperRegion();
1218 continue;
1219 }
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001221 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001222 }
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001224 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001225 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001226 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001227 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001229 if (lazyBindingRegion) {
1230 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1231 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001232 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001233 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001234 }
1235
Ted Kremenekde0d2632010-01-05 02:18:06 +00001236 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001237 if (isa<ElementRegion>(R)) {
1238 // Currently we don't reason specially about Clang-style vectors. Check
1239 // if superR is a vector and if so return Unknown.
1240 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001241 if (typedSuperR->getValueType()->isVectorType())
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001242 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001243 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001246 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001247 }
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001249 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001250 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001251}
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Zhongxing Xu576bb922010-02-05 03:01:53 +00001253SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001254
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001255 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001256 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001257
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001258 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001259 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001261 const MemRegion *superR = R->getSuperRegion();
1262
Ted Kremenekab22ee92009-10-20 01:20:57 +00001263 // Check if the super region has a default binding.
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001264 if (const Optional<SVal> &V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001265 if (SymbolRef parentSym = V->getAsSymbol())
1266 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001268 // Other cases: give up.
1269 return UnknownVal();
1270 }
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Zhongxing Xu576bb922010-02-05 03:01:53 +00001272 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001273}
1274
Zhongxing Xu576bb922010-02-05 03:01:53 +00001275SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Ted Kremenek9031dd72009-07-21 00:12:07 +00001277 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001278 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001280 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001281 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Ted Kremenek9031dd72009-07-21 00:12:07 +00001283 // Lazily derive a value for the VarRegion.
1284 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001285 QualType T = VD->getType();
1286 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001287
1288 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek4dc15662010-02-06 03:57:59 +00001289 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu14d23282010-03-01 06:56:52 +00001290 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Ted Kremenek4dc15662010-02-06 03:57:59 +00001292 if (isa<GlobalsSpaceRegion>(MS)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001293 if (isa<NonStaticGlobalSpaceRegion>(MS)) {
Ted Kremenek4552ff02010-03-30 20:31:04 +00001294 // Is 'VD' declared constant? If so, retrieve the constant value.
1295 QualType CT = Ctx.getCanonicalType(T);
1296 if (CT.isConstQualified()) {
1297 const Expr *Init = VD->getInit();
1298 // Do the null check first, as we want to call 'IgnoreParenCasts'.
1299 if (Init)
1300 if (const IntegerLiteral *IL =
1301 dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) {
1302 const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL);
1303 return ValMgr.getSValuator().EvalCast(V, Init->getType(),
1304 IL->getType());
1305 }
1306 }
1307
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001308 if (const Optional<SVal> &V = RetrieveDerivedDefaultValue(B, MS, R, CT))
1309 return V.getValue();
1310
Zhongxing Xu14d23282010-03-01 06:56:52 +00001311 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek4552ff02010-03-30 20:31:04 +00001312 }
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Ted Kremenek4dc15662010-02-06 03:57:59 +00001314 if (T->isIntegerType())
1315 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001316 if (T->isPointerType())
1317 return ValMgr.makeNull();
1318
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001319 return UnknownVal();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001320 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001321
Ted Kremenek9031dd72009-07-21 00:12:07 +00001322 return UndefinedVal();
1323}
1324
Zhongxing Xu576bb922010-02-05 03:01:53 +00001325SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001326 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001327 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001328}
1329
Zhongxing Xu576bb922010-02-05 03:01:53 +00001330SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001331 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001332 assert(T->isStructureOrClassType());
Zhongxing Xu576bb922010-02-05 03:01:53 +00001333 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001334}
1335
Zhongxing Xu576bb922010-02-05 03:01:53 +00001336SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001337 assert(isa<ConstantArrayType>(R->getValueType()));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001338 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001339}
1340
Ted Kremenek9af46f52009-06-16 22:36:44 +00001341//===----------------------------------------------------------------------===//
1342// Binding values to regions.
1343//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001344
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001345Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001346 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001347 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001348 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Ted Kremenek0964a062009-01-21 06:57:53 +00001350 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001351}
1352
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001353Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001354 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001355 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001356
Ted Kremenek9af46f52009-06-16 22:36:44 +00001357 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001358 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Ted Kremenek9af46f52009-06-16 22:36:44 +00001360 // Check if the region is a struct region.
1361 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +00001362 if (TR->getValueType()->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001363 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001365 // Special case: the current region represents a cast and it and the super
1366 // region both have pointer types or intptr_t types. If so, perform the
1367 // bind to the super region.
1368 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001369 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001370 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1371 if (ER->getIndex().isZeroConstant()) {
1372 if (const TypedRegion *superR =
1373 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1374 ASTContext &Ctx = getContext();
Zhongxing Xu018220c2010-08-11 06:10:55 +00001375 QualType superTy = superR->getValueType();
1376 QualType erTy = ER->getValueType();
Mike Stump1eb44332009-09-09 15:08:12 +00001377
1378 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001379 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +00001380 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001381 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001382 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001383 // For now, just invalidate the fields of the struct/union/class.
1384 // FIXME: Precisely handle the fields of the record.
Jordy Rose58f8b202010-08-05 03:28:45 +00001385 if (superTy->isStructureOrClassType())
1386 return KillStruct(store, superR, UnknownVal());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001387 }
1388 }
1389 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001390 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1391 // Binding directly to a symbolic region should be treated as binding
1392 // to element 0.
1393 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001394
Ted Kremenek852274d2009-12-16 03:18:58 +00001395 // FIXME: Is this the right way to handle symbols that are references?
1396 if (const PointerType *PT = T->getAs<PointerType>())
1397 T = PT->getPointeeType();
1398 else
1399 T = T->getAs<ReferenceType>()->getPointeeType();
1400
Ted Kremenek0954cde2009-09-24 04:11:44 +00001401 R = GetElementZeroRegion(SR, T);
1402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001404 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001405 RegionBindings B = GetRegionBindings(store);
1406 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001407}
1408
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001409Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001410 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001411
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001412 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001413
Ted Kremenek0964a062009-01-21 06:57:53 +00001414 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001415 return BindArray(store, VR, InitVal);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001416 if (T->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001417 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001418
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001419 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001420}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001421
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001422// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001423Store RegionStoreManager::BindCompoundLiteral(Store store,
1424 const CompoundLiteralExpr *CL,
1425 const LocationContext *LC,
1426 SVal V) {
1427 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001428 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001429}
1430
Zhongxing Xua5ce9662010-06-01 03:01:33 +00001431
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001432Store RegionStoreManager::setImplicitDefaultValue(Store store,
1433 const MemRegion *R,
1434 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001435 RegionBindings B = GetRegionBindings(store);
1436 SVal V;
1437
1438 if (Loc::IsLocType(T))
1439 V = ValMgr.makeNull();
1440 else if (T->isIntegerType())
1441 V = ValMgr.makeZeroVal(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001442 else if (T->isStructureOrClassType() || T->isArrayType()) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001443 // Set the default value to a zero constant when it is a structure
1444 // or array. The type doesn't really matter.
1445 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1446 }
1447 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001448 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001449 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001450
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001451 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001452}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001453
1454Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001455 SVal Init) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001456
Ted Kremenekfee90812010-01-26 23:51:00 +00001457 ASTContext &Ctx = getContext();
Zhongxing Xu018220c2010-08-11 06:10:55 +00001458 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001459 QualType ElementTy = AT->getElementType();
Ted Kremenekfee90812010-01-26 23:51:00 +00001460 Optional<uint64_t> Size;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001461
Ted Kremenekfee90812010-01-26 23:51:00 +00001462 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1463 Size = CAT->getSize().getZExtValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001464
Jordy Rose167cc372010-07-29 06:40:33 +00001465 // Check if the init expr is a string literal.
1466 if (loc::MemRegionVal *MRV = dyn_cast<loc::MemRegionVal>(&Init)) {
1467 const StringRegion *S = cast<StringRegion>(MRV->getRegion());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001468
Jordy Rose167cc372010-07-29 06:40:33 +00001469 // Treat the string as a lazy compound value.
1470 nonloc::LazyCompoundVal LCV =
1471 cast<nonloc::LazyCompoundVal>(ValMgr.makeLazyCompoundVal(store, S));
1472 return CopyLazyBindings(LCV, store, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001473 }
1474
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001475 // Handle lazy compound values.
1476 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001477 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001478
1479 // Remaining case: explicit compound values.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001480
Ted Kremenek027e2662009-11-19 20:20:24 +00001481 if (Init.isUnknown())
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001482 return setImplicitDefaultValue(store, R, ElementTy);
1483
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001484 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001485 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001486 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Ted Kremenekfee90812010-01-26 23:51:00 +00001488 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001489 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001490 if (VI == VE)
1491 break;
1492
Ted Kremenek46537392009-07-16 01:33:37 +00001493 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001494 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001495
Douglas Gregorfb87b892010-04-26 21:31:17 +00001496 if (ElementTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001497 store = BindStruct(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001498 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001499 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001500 }
1501
Ted Kremenek027e2662009-11-19 20:20:24 +00001502 // If the init list is shorter than the array length, set the
1503 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001504 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001505 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001506
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001507 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001508}
1509
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001510Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1511 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Ted Kremenek67f28532009-06-17 22:02:04 +00001513 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001514 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Zhongxing Xu018220c2010-08-11 06:10:55 +00001516 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001517 assert(T->isStructureOrClassType());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001518
Ted Kremenek6217b802009-07-29 21:53:49 +00001519 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001520 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001521
1522 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001523 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001524
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001525 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001526 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001527 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001529 // We may get non-CompoundVal accidentally due to imprecise cast logic or
1530 // that we are binding symbolic struct value. Kill the field values, and if
1531 // the value is symbolic go and bind it as a "default" binding.
Ted Kremenek67f28532009-06-17 22:02:04 +00001532 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001533 return KillStruct(store, R, isa<nonloc::SymbolVal>(V) ? V : UnknownVal());
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001534
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001535 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001536 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001537
1538 RecordDecl::field_iterator FI, FE;
1539
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001540 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001541
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001542 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001543 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001544
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001545 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001546 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001547
Ted Kremenekcf549592009-09-22 21:19:14 +00001548 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001549 store = BindArray(store, FR, *VI);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001550 else if (FTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001551 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001552 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001553 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001554 }
1555
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001556 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001557 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001558 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001559 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001560 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001561 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001562
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001563 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001564}
1565
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001566Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R,
1567 SVal DefaultVal) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001568 RegionBindings B = GetRegionBindings(store);
1569 llvm::OwningPtr<RegionStoreSubRegionMap>
1570 SubRegions(getRegionStoreSubRegionMap(store));
1571 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001572
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001573 // Set the default value of the struct region to "unknown".
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001574 return Add(B, R, BindingKey::Default, DefaultVal).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001575}
1576
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001577Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1578 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001579
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001580 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001581 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001582
Mike Stump1eb44332009-09-09 15:08:12 +00001583 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001584 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001585
Mike Stump1eb44332009-09-09 15:08:12 +00001586 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001587 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001589 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1590 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001591 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001592}
1593
1594//===----------------------------------------------------------------------===//
1595// "Raw" retrievals and bindings.
1596//===----------------------------------------------------------------------===//
1597
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001598BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001599 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001600 const RegionRawOffset &O = ER->getAsArrayOffset();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001601
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001602 if (O.getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001603 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001604
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001605 // FIXME: There are some ElementRegions for which we cannot compute
1606 // raw offsets yet, including regions with symbolic offsets.
1607 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001608
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001609 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001610}
1611
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001612RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001613 return RBFactory.Add(B, K, V);
1614}
1615
1616RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001617 BindingKey::Kind k, SVal V) {
1618 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001619}
1620
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001621const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001622 return B.lookup(K);
1623}
1624
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001625const SVal *RegionStoreManager::Lookup(RegionBindings B,
1626 const MemRegion *R,
1627 BindingKey::Kind k) {
1628 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001629}
1630
1631RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1632 return RBFactory.Remove(B, K);
1633}
1634
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001635RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1636 BindingKey::Kind k){
1637 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001638}
1639
1640Store RegionStoreManager::Remove(Store store, BindingKey K) {
1641 RegionBindings B = GetRegionBindings(store);
1642 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001643}
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Ted Kremenek9af46f52009-06-16 22:36:44 +00001645//===----------------------------------------------------------------------===//
1646// State pruning.
1647//===----------------------------------------------------------------------===//
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001648
Ted Kremenek5499b842010-03-10 16:32:56 +00001649namespace {
1650class RemoveDeadBindingsWorker :
1651 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1652 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1653 SymbolReaper &SymReaper;
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001654 const StackFrameContext *CurrentLCtx;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001655
Ted Kremenek5499b842010-03-10 16:32:56 +00001656public:
1657 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1658 RegionBindings b, SymbolReaper &symReaper,
Jordy Rose7dadf792010-07-01 20:09:55 +00001659 const StackFrameContext *LCtx)
Ted Kremenek5499b842010-03-10 16:32:56 +00001660 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
Jordy Rose7dadf792010-07-01 20:09:55 +00001661 SymReaper(symReaper), CurrentLCtx(LCtx) {}
Ted Kremenek5499b842010-03-10 16:32:56 +00001662
1663 // Called by ClusterAnalysis.
1664 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1665 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek5499b842010-03-10 16:32:56 +00001666
Ted Kremenek75a2d942010-04-01 00:15:55 +00001667 void VisitBindingKey(BindingKey K);
Ted Kremenek5499b842010-03-10 16:32:56 +00001668 bool UpdatePostponed();
1669 void VisitBinding(SVal V);
1670};
1671}
1672
1673void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1674 RegionCluster &C) {
1675
1676 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
Jordy Rose7dadf792010-07-01 20:09:55 +00001677 if (SymReaper.isLive(VR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001678 AddToWorkList(baseR, C);
1679
1680 return;
1681 }
1682
1683 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1684 if (SymReaper.isLive(SR->getSymbol()))
1685 AddToWorkList(SR, C);
1686 else
1687 Postponed.push_back(SR);
1688
1689 return;
1690 }
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001691
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001692 if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
1693 AddToWorkList(baseR, C);
1694 return;
1695 }
1696
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001697 // CXXThisRegion in the current or parent location context is live.
1698 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001699 const StackArgumentsSpaceRegion *StackReg =
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001700 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
1701 const StackFrameContext *RegCtx = StackReg->getStackFrame();
1702 if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
1703 AddToWorkList(TR, C);
1704 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001705}
1706
1707void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1708 BindingKey *I, BindingKey *E) {
Ted Kremenek75a2d942010-04-01 00:15:55 +00001709 for ( ; I != E; ++I)
1710 VisitBindingKey(*I);
Ted Kremenek5499b842010-03-10 16:32:56 +00001711}
1712
1713void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1714 // Is it a LazyCompoundVal? All referenced regions are live as well.
1715 if (const nonloc::LazyCompoundVal *LCS =
1716 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1717
1718 const MemRegion *LazyR = LCS->getRegion();
1719 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1720 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +00001721 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
1722 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001723 VisitBinding(RI.getData());
1724 }
1725 return;
1726 }
1727
1728 // If V is a region, then add it to the worklist.
1729 if (const MemRegion *R = V.getAsRegion())
1730 AddToWorkList(R);
1731
1732 // Update the set of live symbols.
1733 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1734 SI!=SE;++SI)
1735 SymReaper.markLive(*SI);
1736}
1737
Ted Kremenek75a2d942010-04-01 00:15:55 +00001738void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) {
1739 const MemRegion *R = K.getRegion();
1740
Ted Kremenek5499b842010-03-10 16:32:56 +00001741 // Mark this region "live" by adding it to the worklist. This will cause
1742 // use to visit all regions in the cluster (if we haven't visited them
1743 // already).
Ted Kremenek75a2d942010-04-01 00:15:55 +00001744 if (AddToWorkList(R)) {
1745 // Mark the symbol for any live SymbolicRegion as "live". This means we
1746 // should continue to track that symbol.
1747 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1748 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek5499b842010-03-10 16:32:56 +00001749
Ted Kremenek75a2d942010-04-01 00:15:55 +00001750 // For BlockDataRegions, enqueue the VarRegions for variables marked
1751 // with __block (passed-by-reference).
1752 // via BlockDeclRefExprs.
1753 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1754 for (BlockDataRegion::referenced_vars_iterator
1755 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1756 RI != RE; ++RI) {
1757 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1758 AddToWorkList(*RI);
1759 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001760
Ted Kremenek75a2d942010-04-01 00:15:55 +00001761 // No possible data bindings on a BlockDataRegion.
1762 return;
Ted Kremenek5499b842010-03-10 16:32:56 +00001763 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001764 }
1765
Ted Kremenek75a2d942010-04-01 00:15:55 +00001766 // Visit the data binding for K.
1767 if (const SVal *V = RM.Lookup(B, K))
Ted Kremenek5499b842010-03-10 16:32:56 +00001768 VisitBinding(*V);
1769}
1770
1771bool RemoveDeadBindingsWorker::UpdatePostponed() {
1772 // See if any postponed SymbolicRegions are actually live now, after
1773 // having done a scan.
1774 bool changed = false;
1775
1776 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1777 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1778 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1779 if (SymReaper.isLive(SR->getSymbol())) {
1780 changed |= AddToWorkList(SR);
1781 *I = NULL;
1782 }
1783 }
1784 }
1785
1786 return changed;
1787}
1788
Jordy Rose7dadf792010-07-01 20:09:55 +00001789const GRState *RegionStoreManager::RemoveDeadBindings(GRState &state,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001790 const StackFrameContext *LCtx,
Zhongxing Xu72119c42010-02-05 05:34:29 +00001791 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001792 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001793{
Zhongxing Xu95798982010-05-26 03:27:35 +00001794 RegionBindings B = GetRegionBindings(state.getStore());
Jordy Rose7dadf792010-07-01 20:09:55 +00001795 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
Ted Kremenek5499b842010-03-10 16:32:56 +00001796 W.GenerateClusters();
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Ted Kremenek5499b842010-03-10 16:32:56 +00001798 // Enqueue the region roots onto the worklist.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001799 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenek5499b842010-03-10 16:32:56 +00001800 E=RegionRoots.end(); I!=E; ++I)
1801 W.AddToWorkList(*I);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001802
Ted Kremenek5499b842010-03-10 16:32:56 +00001803 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001804
Ted Kremenek9af46f52009-06-16 22:36:44 +00001805 // We have now scanned the store, marking reachable regions and symbols
1806 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001807 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001808 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek5499b842010-03-10 16:32:56 +00001809 const BindingKey &K = I.getKey();
1810
Ted Kremenekb7118f72010-03-10 16:38:41 +00001811 // If the cluster has been visited, we know the region has been marked.
Ted Kremenek5499b842010-03-10 16:32:56 +00001812 if (W.isVisited(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001813 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Ted Kremenek5499b842010-03-10 16:32:56 +00001815 // Remove the dead entry.
1816 B = Remove(B, K);
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Ted Kremenek5499b842010-03-10 16:32:56 +00001818 // Mark all non-live symbols that this binding references as dead.
1819 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001820 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001822 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001823 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1824 for (; SI != SE; ++SI)
1825 SymReaper.maybeDead(*SI);
1826 }
Zhongxing Xu95798982010-05-26 03:27:35 +00001827 state.setStore(B.getRoot());
1828 const GRState *s = StateMgr.getPersistentState(state);
Zhongxing Xu95798982010-05-26 03:27:35 +00001829 return s;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001830}
1831
Ted Kremenek5499b842010-03-10 16:32:56 +00001832
Jordy Roseff59efd2010-08-03 20:44:35 +00001833Store RegionStoreManager::EnterStackFrame(const GRState *state,
1834 const StackFrameContext *frame) {
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001835 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001836 FunctionDecl::param_const_iterator PI = FD->param_begin();
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001837 Store store = state->getStore();
Zhongxing Xuc5063572010-03-16 13:14:16 +00001838
1839 if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) {
1840 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1841
1842 // Copy the arg expression value to the arg variables.
1843 for (; AI != AE; ++AI, ++PI) {
1844 SVal ArgVal = state->getSVal(*AI);
1845 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1846 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001847 } else if (const CXXConstructExpr *CE =
Zhongxing Xuc5063572010-03-16 13:14:16 +00001848 dyn_cast<CXXConstructExpr>(frame->getCallSite())) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001849 CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(),
Zhongxing Xuc5063572010-03-16 13:14:16 +00001850 AE = CE->arg_end();
1851
1852 // Copy the arg expression value to the arg variables.
1853 for (; AI != AE; ++AI, ++PI) {
1854 SVal ArgVal = state->getSVal(*AI);
1855 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1856 }
1857 } else
Jordy Roseff59efd2010-08-03 20:44:35 +00001858 llvm_unreachable("Unhandled call expression.");
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001859
Jordy Roseff59efd2010-08-03 20:44:35 +00001860 return store;
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001861}
1862
Ted Kremenek9af46f52009-06-16 22:36:44 +00001863//===----------------------------------------------------------------------===//
1864// Utility methods.
1865//===----------------------------------------------------------------------===//
1866
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001867void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001868 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001869 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001870 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Ted Kremenek451ac092009-08-06 04:50:20 +00001872 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001873 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001874}