blob: c8c392bfaf21bbe5a69bdbcb5acea75f8b28ce65 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000017#include "clang/AST/CharUnits.h"
Zhongxing Xuc5063572010-03-16 13:14:16 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/ExprCXX.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21#include "clang/Analysis/AnalysisContext.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Checker/PathSensitive/GRState.h"
24#include "clang/Checker/PathSensitive/GRStateTrait.h"
25#include "clang/Checker/PathSensitive/MemRegion.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000027#include "llvm/ADT/ImmutableMap.h"
28#include "llvm/ADT/Optional.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000029#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000030
31using namespace clang;
Ted Kremenek66d51422010-04-09 20:26:58 +000032using llvm::Optional;
Zhongxing Xu17892752008-10-08 02:50:44 +000033
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000035// Representation of binding keys.
36//===----------------------------------------------------------------------===//
37
38namespace {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000039class BindingKey {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000040public:
Ted Kremeneke393f4a2010-02-03 03:06:46 +000041 enum Kind { Direct = 0x0, Default = 0x1 };
42private:
43 llvm ::PointerIntPair<const MemRegion*, 1> P;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000044 uint64_t Offset;
45
Ted Kremeneke393f4a2010-02-03 03:06:46 +000046 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
Jordy Rosee7011172010-08-16 01:15:17 +000047 : P(r, (unsigned) k), Offset(offset) {}
Ted Kremeneke393f4a2010-02-03 03:06:46 +000048public:
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000049
Ted Kremeneke393f4a2010-02-03 03:06:46 +000050 bool isDirect() const { return P.getInt() == Direct; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000051
Ted Kremeneke393f4a2010-02-03 03:06:46 +000052 const MemRegion *getRegion() const { return P.getPointer(); }
53 uint64_t getOffset() const { return Offset; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000054
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000055 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000056 ID.AddPointer(P.getOpaqueValue());
57 ID.AddInteger(Offset);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000058 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000059
Ted Kremeneke393f4a2010-02-03 03:06:46 +000060 static BindingKey Make(const MemRegion *R, Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000061
Ted Kremeneke393f4a2010-02-03 03:06:46 +000062 bool operator<(const BindingKey &X) const {
63 if (P.getOpaqueValue() < X.P.getOpaqueValue())
64 return true;
65 if (P.getOpaqueValue() > X.P.getOpaqueValue())
66 return false;
67 return Offset < X.Offset;
68 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000069
Ted Kremeneke393f4a2010-02-03 03:06:46 +000070 bool operator==(const BindingKey &X) const {
71 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
72 Offset == X.Offset;
73 }
Jordy Rosee7011172010-08-16 01:15:17 +000074
Jordy Rosed5addb42010-08-16 20:53:01 +000075 bool isValid() const {
Jordy Rosee7011172010-08-16 01:15:17 +000076 return getRegion() != NULL;
77 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000078};
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000079} // end anonymous namespace
80
81namespace llvm {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000082 static inline
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000083 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000084 os << '(' << K.getRegion() << ',' << K.getOffset()
85 << ',' << (K.isDirect() ? "direct" : "default")
86 << ')';
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000087 return os;
88 }
89} // end llvm namespace
90
91//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +000092// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000093//===----------------------------------------------------------------------===//
94
Ted Kremeneke393f4a2010-02-03 03:06:46 +000095typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000096
Ted Kremenek50dc1b32008-12-24 01:05:03 +000097//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000098// Fine-grained control of RegionStoreManager.
99//===----------------------------------------------------------------------===//
100
101namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000102struct minimal_features_tag {};
103struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000105class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000106 bool SupportsFields;
Ted Kremenek9af46f52009-06-16 22:36:44 +0000107public:
108 RegionStoreFeatures(minimal_features_tag) :
Ted Kremenek0752d6d2010-08-20 06:06:41 +0000109 SupportsFields(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenek9af46f52009-06-16 22:36:44 +0000111 RegionStoreFeatures(maximal_features_tag) :
Ted Kremenek0752d6d2010-08-20 06:06:41 +0000112 SupportsFields(true) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenek9af46f52009-06-16 22:36:44 +0000114 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenek9af46f52009-06-16 22:36:44 +0000116 bool supportsFields() const { return SupportsFields; }
Ted Kremenek9af46f52009-06-16 22:36:44 +0000117};
118}
119
120//===----------------------------------------------------------------------===//
Ted 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> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000201 /// getDefaultBinding - Returns an SVal* representing an optional default
202 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000203 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000204
Ted Kremenek027e2662009-11-19 20:20:24 +0000205 /// setImplicitDefaultValue - Set the default binding for the provided
206 /// MemRegion to the value implicitly defined for compound literals when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000207 /// the value is not specified.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000208 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000210 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
211 /// type. 'Array' represents the lvalue of the array being decayed
212 /// to a pointer, and the returned SVal represents the decayed
213 /// version of that lvalue (i.e., a pointer to the first element of
214 /// the array). This is called by GRExprEngine when evaluating
215 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000216 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000217
Zhongxing Xu461147f2010-02-05 05:24:20 +0000218 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000219
Mike Stump1eb44332009-09-09 15:08:12 +0000220 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000221 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000222 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000223
Ted Kremenek67f28532009-06-17 22:02:04 +0000224 //===-------------------------------------------------------------------===//
225 // Binding values to regions.
226 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000227
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000228 Store InvalidateRegions(Store store,
229 const MemRegion * const *Begin,
230 const MemRegion * const *End,
231 const Expr *E, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000232 InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000233 bool invalidateGlobals,
234 InvalidatedRegions *Regions);
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 Kremeneke393f4a2010-02-03 03:06:46 +0000257public: // Part of public interface to class.
258
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000259 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000260
Zhongxing Xu54460092010-06-01 04:49:26 +0000261 // BindDefault is only used to initialize a region with a default value.
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000262 Store BindDefault(Store store, const MemRegion *R, SVal V) {
Zhongxing Xu54460092010-06-01 04:49:26 +0000263 RegionBindings B = GetRegionBindings(store);
264 assert(!Lookup(B, R, BindingKey::Default));
265 assert(!Lookup(B, R, BindingKey::Direct));
266 return Add(B, R, BindingKey::Default, V).getRoot();
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000267 }
268
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000269 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
270 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000272 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000273
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000274 Store BindDeclWithNoInit(Store store, const VarRegion *) {
275 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000276 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000277
Ted Kremenek67f28532009-06-17 22:02:04 +0000278 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000279 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000281 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000282
283 /// KillStruct - Set the entire struct to unknown.
Ted Kremenek281e9dc2010-07-29 00:28:47 +0000284 Store KillStruct(Store store, const TypedRegion* R, SVal DefaultVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000285
Ted Kremenek67f28532009-06-17 22:02:04 +0000286 Store Remove(Store store, Loc LV);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000287
Ted Kremenek67f28532009-06-17 22:02:04 +0000288
289 //===------------------------------------------------------------------===//
290 // Loading values from regions.
291 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenek67f28532009-06-17 22:02:04 +0000293 /// The high level logic for this method is this:
294 /// Retrieve (L)
295 /// if L has binding
296 /// return L's binding
297 /// else if L is in killset
298 /// return unknown
299 /// else
300 /// if L is on stack or heap
301 /// return undefined
302 /// else
303 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000304 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000305
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000306 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000307
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000308 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Zhongxing Xu576bb922010-02-05 03:01:53 +0000310 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Zhongxing Xu576bb922010-02-05 03:01:53 +0000312 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Zhongxing Xu576bb922010-02-05 03:01:53 +0000314 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000316 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000317 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremenek67f28532009-06-17 22:02:04 +0000319 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000320 /// struct copy:
321 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000322 /// x = y;
323 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000324 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Zhongxing Xu576bb922010-02-05 03:01:53 +0000326 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000328 /// Used to lazily generate derived symbols for bindings that are defined
329 /// implicitly by default bindings in a super region.
330 Optional<SVal> RetrieveDerivedDefaultValue(RegionBindings B,
331 const MemRegion *superR,
332 const TypedRegion *R, QualType Ty);
333
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000334 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000335 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000336 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000338 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
339 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000340
341 //===------------------------------------------------------------------===//
342 // State pruning.
343 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Ted Kremenek67f28532009-06-17 22:02:04 +0000345 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
346 /// It returns a new Store with these values removed.
Zhongxing Xu5e4cb342010-08-15 12:45:09 +0000347 Store RemoveDeadBindings(Store store, const StackFrameContext *LCtx,
348 SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000349 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
350
Jordy Roseff59efd2010-08-03 20:44:35 +0000351 Store EnterStackFrame(const GRState *state, const StackFrameContext *frame);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000352
Ted Kremenek67f28532009-06-17 22:02:04 +0000353 //===------------------------------------------------------------------===//
354 // Region "extents".
355 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Jordy Rose32f26562010-07-04 00:00:41 +0000357 // FIXME: This method will soon be eliminated; see the note in Store.h.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000358 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000359 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000360
361 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000362 // Utility methods.
363 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek451ac092009-08-06 04:50:20 +0000365 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000366 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000367 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000368
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000369 void print(Store store, llvm::raw_ostream& Out, const char* nl,
370 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000371
372 void iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek0e9910f2010-06-17 00:24:42 +0000373 RegionBindings B = GetRegionBindings(store);
374 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
375 const BindingKey &K = I.getKey();
376 if (!K.isDirect())
377 continue;
378 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) {
379 // FIXME: Possibly incorporate the offset?
380 if (!f.HandleBinding(*this, store, R, I.getData()))
381 return;
382 }
383 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000384 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000385};
386
387} // end anonymous namespace
388
Ted Kremenek9af46f52009-06-16 22:36:44 +0000389//===----------------------------------------------------------------------===//
390// RegionStore creation.
391//===----------------------------------------------------------------------===//
392
393StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
394 RegionStoreFeatures F = maximal_features_tag();
395 return new RegionStoreManager(StMgr, F);
396}
397
398StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
399 RegionStoreFeatures F = minimal_features_tag();
400 F.enableFields(true);
401 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000402}
403
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000404void
405RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000406 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000407 const MemRegion *superR = R->getSuperRegion();
408 if (add(superR, R))
409 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000410 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000411}
412
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000413RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000414RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
415 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000416 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000418 llvm::SmallVector<const SubRegion*, 10> WL;
419
Ted Kremenek451ac092009-08-06 04:50:20 +0000420 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000421 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000422 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Mike Stump1eb44332009-09-09 15:08:12 +0000424 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000425 // don't have direct bindings but are super regions of those that do.
426 while (!WL.empty()) {
427 const SubRegion *R = WL.back();
428 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000429 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000430 }
431
Ted Kremenek14453bf2009-03-03 19:02:42 +0000432 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000433}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000434
Ted Kremenek9af46f52009-06-16 22:36:44 +0000435//===----------------------------------------------------------------------===//
Ted Kremeneka4fab032010-03-10 07:19:59 +0000436// Region Cluster analysis.
437//===----------------------------------------------------------------------===//
438
439namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000440template <typename DERIVED>
Ted Kremeneka4fab032010-03-10 07:19:59 +0000441class ClusterAnalysis {
442protected:
443 typedef BumpVector<BindingKey> RegionCluster;
444 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5499b842010-03-10 16:32:56 +0000445 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
446 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
447 WorkList;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000448
449 BumpVectorContext BVC;
450 ClusterMap ClusterM;
Ted Kremenek5499b842010-03-10 16:32:56 +0000451 WorkList WL;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000452
453 RegionStoreManager &RM;
454 ASTContext &Ctx;
455 ValueManager &ValMgr;
456
Ted Kremenek5499b842010-03-10 16:32:56 +0000457 RegionBindings B;
458
Ted Kremeneka4fab032010-03-10 07:19:59 +0000459public:
Ted Kremenek5499b842010-03-10 16:32:56 +0000460 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
461 RegionBindings b)
462 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
463 B(b) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000464
Ted Kremenek5499b842010-03-10 16:32:56 +0000465 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000466
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000467 RegionCluster &AddToCluster(BindingKey K) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000468 const MemRegion *R = K.getRegion();
469 const MemRegion *baseR = R->getBaseRegion();
470 RegionCluster &C = getCluster(baseR);
471 C.push_back(K, BVC);
472 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000473 return C;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000474 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000475
Ted Kremenek5499b842010-03-10 16:32:56 +0000476 bool isVisited(const MemRegion *R) {
477 return (bool) Visited[&getCluster(R->getBaseRegion())];
478 }
479
480 RegionCluster& getCluster(const MemRegion *R) {
481 RegionCluster *&CRef = ClusterM[R];
482 if (!CRef) {
483 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
484 CRef = new (Mem) RegionCluster(BVC, 10);
485 }
486 return *CRef;
487 }
488
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000489 void GenerateClusters(bool includeGlobals = false) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000490 // Scan the entire set of bindings and make the region clusters.
491 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000492 RegionCluster &C = AddToCluster(RI.getKey());
Ted Kremenek5499b842010-03-10 16:32:56 +0000493 if (const MemRegion *R = RI.getData().getAsRegion()) {
494 // Generate a cluster, but don't add the region to the cluster
495 // if there aren't any bindings.
496 getCluster(R->getBaseRegion());
497 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000498 if (includeGlobals) {
499 const MemRegion *R = RI.getKey().getRegion();
500 if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()))
501 AddToWorkList(R, C);
502 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000503 }
504 }
Ted Kremenek5499b842010-03-10 16:32:56 +0000505
506 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
507 if (unsigned &visited = Visited[&C])
508 return false;
509 else
510 visited = 1;
511
512 WL.push_back(std::make_pair(R, &C));
513 return true;
514 }
515
516 bool AddToWorkList(BindingKey K) {
517 return AddToWorkList(K.getRegion());
518 }
519
520 bool AddToWorkList(const MemRegion *R) {
521 const MemRegion *baseR = R->getBaseRegion();
522 return AddToWorkList(baseR, getCluster(baseR));
523 }
524
525 void RunWorkList() {
526 while (!WL.empty()) {
527 const MemRegion *baseR;
528 RegionCluster *C;
529 llvm::tie(baseR, C) = WL.back();
530 WL.pop_back();
531
532 // First visit the cluster.
533 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
534
Ted Kremenek75a2d942010-04-01 00:15:55 +0000535 // Next, visit the base region.
536 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
Ted Kremenek5499b842010-03-10 16:32:56 +0000537 }
538 }
539
540public:
541 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
542 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
Ted Kremenek75a2d942010-04-01 00:15:55 +0000543 void VisitBaseRegion(const MemRegion *baseR) {}
Ted Kremenek5499b842010-03-10 16:32:56 +0000544};
Ted Kremeneka4fab032010-03-10 07:19:59 +0000545}
546
547//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000548// Binding invalidation.
549//===----------------------------------------------------------------------===//
550
Zhongxing Xu13d50172009-10-11 08:08:02 +0000551void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
552 const MemRegion *R,
553 RegionStoreSubRegionMap &M) {
Ted Kremeneka4fab032010-03-10 07:19:59 +0000554
Ted Kremenekdf165012010-02-02 22:38:47 +0000555 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
556 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
557 I != E; ++I)
558 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000559
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000560 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000561}
562
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000563namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000564class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
565{
566 const Expr *Ex;
567 unsigned Count;
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000568 StoreManager::InvalidatedSymbols *IS;
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000569 StoreManager::InvalidatedRegions *Regions;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000570public:
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000571 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenek5499b842010-03-10 16:32:56 +0000572 GRStateManager &stateMgr,
573 RegionBindings b,
574 const Expr *ex, unsigned count,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000575 StoreManager::InvalidatedSymbols *is,
576 StoreManager::InvalidatedRegions *r)
Ted Kremenek5499b842010-03-10 16:32:56 +0000577 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000578 Ex(ex), Count(count), IS(is), Regions(r) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000579
Ted Kremenek5499b842010-03-10 16:32:56 +0000580 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek75a2d942010-04-01 00:15:55 +0000581 void VisitBaseRegion(const MemRegion *baseR);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000582
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000583private:
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000584 void VisitBinding(SVal V);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000585};
Ted Kremenek5b290652010-02-03 04:16:00 +0000586}
587
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000588void InvalidateRegionsWorker::VisitBinding(SVal V) {
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000589 // A symbol? Mark it touched by the invalidation.
590 if (IS)
591 if (SymbolRef Sym = V.getAsSymbol())
592 IS->insert(Sym);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000593
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000594 if (const MemRegion *R = V.getAsRegion()) {
595 AddToWorkList(R);
596 return;
597 }
598
599 // Is it a LazyCompoundVal? All references get invalidated as well.
600 if (const nonloc::LazyCompoundVal *LCS =
601 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
602
603 const MemRegion *LazyR = LCS->getRegion();
604 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
605
606 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +0000607 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
608 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000609 VisitBinding(RI.getData());
610 }
611
612 return;
613 }
614}
615
Ted Kremenek5499b842010-03-10 16:32:56 +0000616void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
617 BindingKey *I, BindingKey *E) {
618 for ( ; I != E; ++I) {
619 // Get the old binding. Is it a region? If so, add it to the worklist.
620 const BindingKey &K = *I;
621 if (const SVal *V = RM.Lookup(B, K))
622 VisitBinding(*V);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000623
Ted Kremenek5499b842010-03-10 16:32:56 +0000624 B = RM.Remove(B, K);
625 }
626}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000627
Ted Kremenek75a2d942010-04-01 00:15:55 +0000628void InvalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000629 if (IS) {
630 // Symbolic region? Mark that symbol touched by the invalidation.
631 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
632 IS->insert(SR->getSymbol());
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000633 }
634
Ted Kremenek5499b842010-03-10 16:32:56 +0000635 // BlockDataRegion? If so, invalidate captured variables that are passed
636 // by reference.
637 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
638 for (BlockDataRegion::referenced_vars_iterator
639 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
640 BI != BE; ++BI) {
641 const VarRegion *VR = *BI;
642 const VarDecl *VD = VR->getDecl();
643 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
644 AddToWorkList(VR);
645 }
646 return;
647 }
648
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000649 // Otherwise, we have a normal data region. Record that we touched the region.
650 if (Regions)
651 Regions->push_back(baseR);
652
Ted Kremenek5499b842010-03-10 16:32:56 +0000653 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()) {
Zhongxing Xu0b46b1b2010-08-21 06:26:59 +0000670 // Invalidate the region by setting its default value to
671 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek5499b842010-03-10 16:32:56 +0000672 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
673 Count);
674 B = RM.Add(B, baseR, BindingKey::Default, V);
675 return;
676 }
677
678 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
679 // Set the default value of the array to conjured symbol.
680 DefinedOrUnknownSVal V =
681 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
682 B = RM.Add(B, baseR, BindingKey::Default, V);
683 return;
684 }
685
686 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
687 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
688 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000689}
690
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000691Store RegionStoreManager::InvalidateRegions(Store store,
692 const MemRegion * const *I,
693 const MemRegion * const *E,
694 const Expr *Ex, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000695 InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000696 bool invalidateGlobals,
697 InvalidatedRegions *Regions) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000698 InvalidateRegionsWorker W(*this, StateMgr,
699 RegionStoreManager::GetRegionBindings(store),
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000700 Ex, Count, IS, Regions);
Ted Kremenek5499b842010-03-10 16:32:56 +0000701
702 // Scan the bindings and generate the clusters.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000703 W.GenerateClusters(invalidateGlobals);
Ted Kremenek5499b842010-03-10 16:32:56 +0000704
705 // Add I .. E to the worklist.
706 for ( ; I != E; ++I)
707 W.AddToWorkList(*I);
708
709 W.RunWorkList();
710
711 // Return the new bindings.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000712 RegionBindings B = W.getRegionBindings();
713
714 if (invalidateGlobals) {
715 // Bind the non-static globals memory space to a new symbol that we will
716 // use to derive the bindings for all non-static globals.
717 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion();
718 SVal V =
719 ValMgr.getConjuredSymbolVal(/* SymbolTag = */ (void*) GS, Ex,
720 /* symbol type, doesn't matter */ Ctx.IntTy,
721 Count);
722 B = Add(B, BindingKey::Make(GS, BindingKey::Default), V);
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000723
724 // Even if there are no bindings in the global scope, we still need to
725 // record that we touched it.
726 if (Regions)
727 Regions->push_back(GS);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000728 }
729
730 return B.getRoot();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000731}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000732
Ted Kremenek9af46f52009-06-16 22:36:44 +0000733//===----------------------------------------------------------------------===//
734// Extents for regions.
735//===----------------------------------------------------------------------===//
736
Zhongxing Xue884ff82009-11-12 02:48:32 +0000737DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000738 const MemRegion *R,
739 QualType EleTy) {
Jordy Rose32f26562010-07-04 00:00:41 +0000740 SVal Size = cast<SubRegion>(R)->getExtent(ValMgr);
741 SValuator &SVator = ValMgr.getSValuator();
742 const llvm::APSInt *SizeInt = SVator.getKnownValue(state, Size);
743 if (!SizeInt)
744 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Jordy Rose32f26562010-07-04 00:00:41 +0000746 CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000747 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Jordy Rose32f26562010-07-04 00:00:41 +0000749 // If a variable is reinterpreted as a type that doesn't fit into a larger
750 // type evenly, round it down.
751 // This is a signed value, since it's used in arithmetic with signed indices.
752 return ValMgr.makeIntVal(RegionSize / EleSize, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000753}
754
Ted Kremenek9af46f52009-06-16 22:36:44 +0000755//===----------------------------------------------------------------------===//
756// Location and region casting.
757//===----------------------------------------------------------------------===//
758
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000759/// ArrayToPointer - Emulates the "decay" of an array to a pointer
760/// type. 'Array' represents the lvalue of the array being decayed
761/// to a pointer, and the returned SVal represents the decayed
762/// version of that lvalue (i.e., a pointer to the first element of
763/// the array). This is called by GRExprEngine when evaluating casts
764/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000765SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000766 if (!isa<loc::MemRegionVal>(Array))
767 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Ted Kremenekabb042f2008-12-13 19:24:37 +0000769 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
770 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000772 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000773 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000775 // Strip off typedefs from the ArrayRegion's ValueType.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000776 QualType T = ArrayR->getValueType().getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000777 ArrayType *AT = cast<ArrayType>(T);
778 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremenek75185b52009-07-16 00:00:11 +0000780 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000781 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000782}
783
Ted Kremenek9af46f52009-06-16 22:36:44 +0000784//===----------------------------------------------------------------------===//
785// Pointer arithmetic.
786//===----------------------------------------------------------------------===//
787
Zhongxing Xu461147f2010-02-05 05:24:20 +0000788SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000789 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000790 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000791 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000792 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000793
Jordy Roseeac4a002010-06-28 08:26:15 +0000794 // Special case for zero RHS.
795 if (R.isZeroConstant()) {
796 switch (Op) {
797 default:
798 // Handle it normally.
799 break;
800 case BinaryOperator::Add:
801 case BinaryOperator::Sub:
802 // FIXME: does this need to be casted to match resultTy?
803 return L;
804 }
805 }
806
Zhongxing Xua1718c72009-04-03 07:33:13 +0000807 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000808 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000809
Ted Kremenek3bccf082009-07-11 00:58:27 +0000810 switch (MR->getKind()) {
811 case MemRegion::SymbolicRegionKind: {
812 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000813 SymbolRef Sym = SR->getSymbol();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000814 QualType T = Sym->getType(Ctx);
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000815 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000817 if (const PointerType *PT = T->getAs<PointerType>())
818 EleTy = PT->getPointeeType();
819 else
John McCall183700f2009-09-21 23:43:11 +0000820 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Ted Kremenek3bccf082009-07-11 00:58:27 +0000822 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000823 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000824 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000825 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000826 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000827 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000828 QualType EleTy = Ctx.CharTy; // Create an ElementRegion of bytes.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000829 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000830 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000831 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000832 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000833
Ted Kremenek3bccf082009-07-11 00:58:27 +0000834 case MemRegion::ElementRegionKind: {
835 ER = cast<ElementRegion>(MR);
836 break;
837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek3bccf082009-07-11 00:58:27 +0000839 // Not yet handled.
840 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000841 case MemRegion::StringRegionKind: {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000842
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000843 }
844 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000845 case MemRegion::CompoundLiteralRegionKind:
846 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000847 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000848 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000849 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000851 case MemRegion::FunctionTextRegionKind:
852 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000853 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000854 // Technically this can happen if people do funny things with casts.
855 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenekde0d2632010-01-05 02:18:06 +0000857 case MemRegion::CXXThisRegionKind:
858 assert(0 &&
859 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000860 case MemRegion::GenericMemSpaceRegionKind:
861 case MemRegion::StackLocalsSpaceRegionKind:
862 case MemRegion::StackArgumentsSpaceRegionKind:
863 case MemRegion::HeapSpaceRegionKind:
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000864 case MemRegion::NonStaticGlobalSpaceRegionKind:
865 case MemRegion::StaticGlobalSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000866 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000867 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
868 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000869 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000870
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000871 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000872 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000873
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000874 // For now, only support:
875 // (a) concrete integer indices that can easily be resolved
876 // (b) 0 + symbolic index
877 if (Base) {
878 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
879 // FIXME: Should use SValuator here.
880 SVal NewIdx =
881 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000882 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000883 const MemRegion* NewER =
884 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000885 ER->getSuperRegion(), Ctx);
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000886 return ValMgr.makeLoc(NewER);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000887 }
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000888 if (0 == Base->getValue()) {
889 const MemRegion* NewER =
890 MRMgr.getElementRegion(ER->getElementType(), R,
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000891 ER->getSuperRegion(), Ctx);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000892 return ValMgr.makeLoc(NewER);
893 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Ted Kremenek5dc27462009-03-03 02:51:43 +0000896 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000897}
898
Ted Kremenek9af46f52009-06-16 22:36:44 +0000899//===----------------------------------------------------------------------===//
900// Loading values from regions.
901//===----------------------------------------------------------------------===//
902
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000903Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Zhongxing Xubdfa85f2010-05-29 06:23:24 +0000904 const MemRegion *R) {
Zhongxing Xu42c67bf2010-05-29 06:49:04 +0000905
906 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
907 return *V;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000908
Zhongxing Xu13d50172009-10-11 08:08:02 +0000909 return Optional<SVal>();
910}
911
912Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000913 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000914 if (R->isBoundable())
915 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +0000916 if (TR->getValueType()->isUnionType())
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000917 return UnknownVal();
918
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000919 if (const SVal *V = Lookup(B, R, BindingKey::Default))
920 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +0000921
922 return Optional<SVal>();
923}
924
Ted Kremeneka6275a52009-07-15 02:31:43 +0000925static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
926 RTy = Ctx.getCanonicalType(RTy);
927 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Ted Kremeneka6275a52009-07-15 02:31:43 +0000929 if (RTy == UsedTy)
930 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000931
932
Ted Kremenek25c54572009-07-20 22:58:02 +0000933 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000934 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000935 // represents a reinterpretation.
936 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000937 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000938 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000939
940 return PUsedTy && PRTy &&
941 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000942 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000943 }
944
945 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000946}
947
Zhongxing Xu576bb922010-02-05 03:01:53 +0000948SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000949 assert(!isa<UnknownVal>(L) && "location unknown");
950 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000951
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000952 // FIXME: Is this even possible? Shouldn't this be treated as a null
953 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000954 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000955 return UndefinedVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000956
Ted Kremenek67f28532009-06-17 22:02:04 +0000957 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000958
Tom Care7b050302010-06-25 18:22:31 +0000959 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR)) {
960 if (T.isNull()) {
961 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Zhongxing Xu57663fe2010-08-15 10:08:38 +0000962 T = SR->getSymbol()->getType(Ctx);
Tom Care7b050302010-06-25 18:22:31 +0000963 }
Zhongxing Xu81491852010-02-08 08:43:02 +0000964 MR = GetElementZeroRegion(MR, T);
Tom Care7b050302010-06-25 18:22:31 +0000965 }
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000967 if (isa<CodeTextRegion>(MR)) {
968 assert(0 && "Why load from a code text region?");
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000969 return UnknownVal();
Zhongxing Xu2db08ca2010-03-01 05:29:02 +0000970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000972 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
973 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000974 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000975 QualType RTy = R->getValueType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000976
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000977 // FIXME: We should eventually handle funny addressing. e.g.:
978 //
979 // int x = ...;
980 // int *p = &x;
981 // char *q = (char*) p;
982 // char c = *q; // returns the first byte of 'x'.
983 //
984 // Such funny addressing will occur due to layering of regions.
985
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000986#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000987 ASTContext &Ctx = getContext();
988 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000989 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
990 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000991 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000992 assert(Ctx.getCanonicalType(RTy) ==
993 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +0000994 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000995#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000996
Douglas Gregorfb87b892010-04-26 21:31:17 +0000997 if (RTy->isStructureOrClassType())
Zhongxing Xu576bb922010-02-05 03:01:53 +0000998 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001000 // FIXME: Handle unions.
1001 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001002 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001003
1004 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001005 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001006
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001007 // FIXME: handle Vector types.
1008 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001009 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001010
1011 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +00001012 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001013
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001014 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1015 // FIXME: Here we actually perform an implicit conversion from the loaded
1016 // value to the element type. Eventually we want to compose these values
1017 // more intelligently. For example, an 'element' can encompass multiple
1018 // bound regions (e.g., several bound bytes), or could be a subset of
1019 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001020 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001023 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1024 // FIXME: Here we actually perform an implicit conversion from the loaded
1025 // value to the ivar type. What we should model is stores to ivars
1026 // that blow past the extent of the ivar. If the address of the ivar is
1027 // reinterpretted, it is possible we stored a different value that could
1028 // fit within the ivar. Either we need to cast these when storing them
1029 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001030 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001031 }
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001033 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1034 // FIXME: Here we actually perform an implicit conversion from the loaded
1035 // value to the variable type. What we should model is stores to variables
1036 // that blow past the extent of the variable. If the address of the
1037 // variable is reinterpretted, it is possible we stored a different value
1038 // that could fit within the variable. Either we need to cast these when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001039 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001040 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001041 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001042
Zhongxing Xu576bb922010-02-05 03:01:53 +00001043 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001044 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001045
1046 // Check if the region has a binding.
1047 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001048 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001049
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001050 // The location does not have a bound value. This means that it has
1051 // the value it had upon its creation and/or entry to the analyzed
1052 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001053 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001054 // All stack variables are considered to have undefined values
1055 // upon creation. All heap allocated blocks are considered to
1056 // have undefined values as well unless they are explicitly bound
1057 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001058 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001059 }
1060
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001061 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001062 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001063}
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001065std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001066RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001067 if (Optional<SVal> OV = getDirectBinding(B, R))
1068 if (const nonloc::LazyCompoundVal *V =
1069 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001070 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001072 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001073 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001074 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001076 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001077 return std::make_pair(X.first,
1078 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001079 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001080 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001081 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001082 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001084 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001085 return std::make_pair(X.first,
1086 MRMgr.getFieldRegionWithSuper(FR, X.second));
1087 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001088 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xudcbcbdc2010-02-10 02:02:10 +00001089 // possible for a valid lazy binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001090 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001091}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001092
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001093SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001094 const ElementRegion* R) {
1095 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001096 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001097 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001098 return *V;
1099
Ted Kremenek921109a2009-07-01 23:19:52 +00001100 const MemRegion* superR = R->getSuperRegion();
1101
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001102 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001103 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001104 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001105 // an integer, e.g., *((unsigned int*)"hello")
Zhongxing Xu018220c2010-08-11 06:10:55 +00001106 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001107 if (T != Ctx.getCanonicalType(R->getElementType()))
1108 return UnknownVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001109
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001110 const StringLiteral *Str = StrR->getStringLiteral();
1111 SVal Idx = R->getIndex();
1112 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1113 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001114 int64_t byteLength = Str->getByteLength();
Jordy Rose167cc372010-07-29 06:40:33 +00001115 // Technically, only i == byteLength is guaranteed to be null.
1116 // However, such overflows should be caught before reaching this point;
1117 // the only time such an access would be made is if a string literal was
1118 // used to initialize a larger array.
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +00001119 char c = (i >= byteLength) ? '\0' : Str->getString()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001120 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001121 }
1122 }
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Ted Kremeneka709b872010-05-31 01:22:04 +00001124 // Handle the case where we are indexing into a larger scalar object.
1125 // For example, this handles:
1126 // int x = ...
1127 // char *y = &x;
1128 // return *y;
1129 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001130 const RegionRawOffset &O = R->getAsArrayOffset();
Ted Kremeneka709b872010-05-31 01:22:04 +00001131 if (const TypedRegion *baseR = dyn_cast_or_null<TypedRegion>(O.getRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001132 QualType baseT = baseR->getValueType();
Ted Kremeneka709b872010-05-31 01:22:04 +00001133 if (baseT->isScalarType()) {
1134 QualType elemT = R->getElementType();
1135 if (elemT->isScalarType()) {
1136 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1137 if (const Optional<SVal> &V = getDirectBinding(B, superR)) {
1138 if (SymbolRef parentSym = V->getAsSymbol())
1139 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001140
Ted Kremeneka709b872010-05-31 01:22:04 +00001141 if (V->isUnknownOrUndef())
1142 return *V;
1143 // Other cases: give up. We are indexing into a larger object
1144 // that has some value, but we don't know how to handle that yet.
1145 return UnknownVal();
1146 }
1147 }
1148 }
Zhongxing Xu42c67bf2010-05-29 06:49:04 +00001149 }
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001150 }
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001151 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001152}
1153
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001154SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001155 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001156
1157 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001158 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001159 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001160 return *V;
1161
Zhongxing Xu018220c2010-08-11 06:10:55 +00001162 QualType Ty = R->getValueType();
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001163 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001164}
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001166Optional<SVal>
1167RegionStoreManager::RetrieveDerivedDefaultValue(RegionBindings B,
1168 const MemRegion *superR,
1169 const TypedRegion *R,
1170 QualType Ty) {
1171
1172 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
1173 if (SymbolRef parentSym = D->getAsSymbol())
1174 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1175
1176 if (D->isZeroConstant())
1177 return ValMgr.makeZeroVal(Ty);
1178
1179 if (D->isUnknownOrUndef())
1180 return *D;
1181
1182 assert(0 && "Unknown default value");
1183 }
1184
1185 return Optional<SVal>();
1186}
1187
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001188SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001189 const TypedRegion *R,
1190 QualType Ty,
1191 const MemRegion *superR) {
1192
Mike Stump1eb44332009-09-09 15:08:12 +00001193 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001194 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001196 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001198 while (superR) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001199 if (const Optional<SVal> &D = RetrieveDerivedDefaultValue(B, superR, R, Ty))
1200 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001202 // If our super region is a field or element itself, walk up the region
1203 // hierarchy to see if there is a default value installed in an ancestor.
1204 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1205 superR = cast<SubRegion>(superR)->getSuperRegion();
1206 continue;
1207 }
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001209 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001212 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001213 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001214 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001215 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001217 if (lazyBindingRegion) {
1218 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1219 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001220 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001221 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001222 }
1223
Ted Kremenekde0d2632010-01-05 02:18:06 +00001224 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001225 if (isa<ElementRegion>(R)) {
1226 // Currently we don't reason specially about Clang-style vectors. Check
1227 // if superR is a vector and if so return Unknown.
1228 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001229 if (typedSuperR->getValueType()->isVectorType())
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001230 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001231 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001232 }
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001234 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001235 }
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001237 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001238 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001239}
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Zhongxing Xu576bb922010-02-05 03:01:53 +00001241SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001242
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001243 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001244 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001245
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001246 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001247 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001249 const MemRegion *superR = R->getSuperRegion();
1250
Ted Kremenekab22ee92009-10-20 01:20:57 +00001251 // Check if the super region has a default binding.
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001252 if (const Optional<SVal> &V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001253 if (SymbolRef parentSym = V->getAsSymbol())
1254 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001256 // Other cases: give up.
1257 return UnknownVal();
1258 }
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Zhongxing Xu576bb922010-02-05 03:01:53 +00001260 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001261}
1262
Zhongxing Xu576bb922010-02-05 03:01:53 +00001263SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Ted Kremenek9031dd72009-07-21 00:12:07 +00001265 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001266 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001268 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001269 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Ted Kremenek9031dd72009-07-21 00:12:07 +00001271 // Lazily derive a value for the VarRegion.
1272 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001273 QualType T = VD->getType();
1274 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001275
1276 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek4dc15662010-02-06 03:57:59 +00001277 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu14d23282010-03-01 06:56:52 +00001278 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Ted Kremenek4dc15662010-02-06 03:57:59 +00001280 if (isa<GlobalsSpaceRegion>(MS)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001281 if (isa<NonStaticGlobalSpaceRegion>(MS)) {
Ted Kremenek4552ff02010-03-30 20:31:04 +00001282 // Is 'VD' declared constant? If so, retrieve the constant value.
1283 QualType CT = Ctx.getCanonicalType(T);
1284 if (CT.isConstQualified()) {
1285 const Expr *Init = VD->getInit();
1286 // Do the null check first, as we want to call 'IgnoreParenCasts'.
1287 if (Init)
1288 if (const IntegerLiteral *IL =
1289 dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) {
1290 const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL);
1291 return ValMgr.getSValuator().EvalCast(V, Init->getType(),
1292 IL->getType());
1293 }
1294 }
1295
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001296 if (const Optional<SVal> &V = RetrieveDerivedDefaultValue(B, MS, R, CT))
1297 return V.getValue();
1298
Zhongxing Xu14d23282010-03-01 06:56:52 +00001299 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek4552ff02010-03-30 20:31:04 +00001300 }
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Ted Kremenek4dc15662010-02-06 03:57:59 +00001302 if (T->isIntegerType())
1303 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001304 if (T->isPointerType())
1305 return ValMgr.makeNull();
1306
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001307 return UnknownVal();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001308 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001309
Ted Kremenek9031dd72009-07-21 00:12:07 +00001310 return UndefinedVal();
1311}
1312
Zhongxing Xu576bb922010-02-05 03:01:53 +00001313SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001314 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001315 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001316}
1317
Zhongxing Xu576bb922010-02-05 03:01:53 +00001318SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001319 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001320 assert(T->isStructureOrClassType());
Zhongxing Xu576bb922010-02-05 03:01:53 +00001321 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001322}
1323
Zhongxing Xu576bb922010-02-05 03:01:53 +00001324SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001325 assert(isa<ConstantArrayType>(R->getValueType()));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001326 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001327}
1328
Ted Kremenek9af46f52009-06-16 22:36:44 +00001329//===----------------------------------------------------------------------===//
1330// Binding values to regions.
1331//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001332
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001333Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001334 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001335 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001336 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Ted Kremenek0964a062009-01-21 06:57:53 +00001338 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001339}
1340
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001341Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001342 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001343 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001344
Ted Kremenek9af46f52009-06-16 22:36:44 +00001345 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001346 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Ted Kremenek9af46f52009-06-16 22:36:44 +00001348 // Check if the region is a struct region.
1349 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu018220c2010-08-11 06:10:55 +00001350 if (TR->getValueType()->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001351 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001353 // Special case: the current region represents a cast and it and the super
1354 // region both have pointer types or intptr_t types. If so, perform the
1355 // bind to the super region.
1356 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001357 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001358 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1359 if (ER->getIndex().isZeroConstant()) {
1360 if (const TypedRegion *superR =
1361 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +00001362 QualType superTy = superR->getValueType();
1363 QualType erTy = ER->getValueType();
Mike Stump1eb44332009-09-09 15:08:12 +00001364
1365 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001366 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +00001367 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001368 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001369 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001370 // For now, just invalidate the fields of the struct/union/class.
1371 // FIXME: Precisely handle the fields of the record.
Jordy Rose58f8b202010-08-05 03:28:45 +00001372 if (superTy->isStructureOrClassType())
1373 return KillStruct(store, superR, UnknownVal());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001374 }
1375 }
1376 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001377 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1378 // Binding directly to a symbolic region should be treated as binding
1379 // to element 0.
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001380 QualType T = SR->getSymbol()->getType(Ctx);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001381
Ted Kremenek852274d2009-12-16 03:18:58 +00001382 // FIXME: Is this the right way to handle symbols that are references?
1383 if (const PointerType *PT = T->getAs<PointerType>())
1384 T = PT->getPointeeType();
1385 else
1386 T = T->getAs<ReferenceType>()->getPointeeType();
1387
Ted Kremenek0954cde2009-09-24 04:11:44 +00001388 R = GetElementZeroRegion(SR, T);
1389 }
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001391 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001392 RegionBindings B = GetRegionBindings(store);
1393 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001394}
1395
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001396Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001397 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001398
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001399 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001400
Ted Kremenek0964a062009-01-21 06:57:53 +00001401 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001402 return BindArray(store, VR, InitVal);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001403 if (T->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001404 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001405
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001406 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001407}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001408
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001409// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001410Store RegionStoreManager::BindCompoundLiteral(Store store,
1411 const CompoundLiteralExpr *CL,
1412 const LocationContext *LC,
1413 SVal V) {
1414 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001415 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001416}
1417
Zhongxing Xua5ce9662010-06-01 03:01:33 +00001418
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001419Store RegionStoreManager::setImplicitDefaultValue(Store store,
1420 const MemRegion *R,
1421 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001422 RegionBindings B = GetRegionBindings(store);
1423 SVal V;
1424
1425 if (Loc::IsLocType(T))
1426 V = ValMgr.makeNull();
1427 else if (T->isIntegerType())
1428 V = ValMgr.makeZeroVal(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001429 else if (T->isStructureOrClassType() || T->isArrayType()) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001430 // Set the default value to a zero constant when it is a structure
1431 // or array. The type doesn't really matter.
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001432 V = ValMgr.makeZeroVal(Ctx.IntTy);
Ted Kremenek027e2662009-11-19 20:20:24 +00001433 }
1434 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001435 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001436 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001437
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001438 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001439}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001440
1441Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001442 SVal Init) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001443
Zhongxing Xu018220c2010-08-11 06:10:55 +00001444 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001445 QualType ElementTy = AT->getElementType();
Ted Kremenekfee90812010-01-26 23:51:00 +00001446 Optional<uint64_t> Size;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001447
Ted Kremenekfee90812010-01-26 23:51:00 +00001448 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1449 Size = CAT->getSize().getZExtValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001450
Jordy Rose167cc372010-07-29 06:40:33 +00001451 // Check if the init expr is a string literal.
1452 if (loc::MemRegionVal *MRV = dyn_cast<loc::MemRegionVal>(&Init)) {
1453 const StringRegion *S = cast<StringRegion>(MRV->getRegion());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001454
Jordy Rose167cc372010-07-29 06:40:33 +00001455 // Treat the string as a lazy compound value.
1456 nonloc::LazyCompoundVal LCV =
1457 cast<nonloc::LazyCompoundVal>(ValMgr.makeLazyCompoundVal(store, S));
1458 return CopyLazyBindings(LCV, store, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001459 }
1460
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001461 // Handle lazy compound values.
1462 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001463 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001464
1465 // Remaining case: explicit compound values.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001466
Ted Kremenek027e2662009-11-19 20:20:24 +00001467 if (Init.isUnknown())
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001468 return setImplicitDefaultValue(store, R, ElementTy);
1469
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001470 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001471 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001472 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenekfee90812010-01-26 23:51:00 +00001474 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001475 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001476 if (VI == VE)
1477 break;
1478
Ted Kremenek46537392009-07-16 01:33:37 +00001479 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu57663fe2010-08-15 10:08:38 +00001480 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001481
Douglas Gregorfb87b892010-04-26 21:31:17 +00001482 if (ElementTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001483 store = BindStruct(store, ER, *VI);
Jordy Rose59b6dca2010-08-20 01:05:59 +00001484 else if (ElementTy->isArrayType())
1485 store = BindArray(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001486 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001487 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001488 }
1489
Ted Kremenek027e2662009-11-19 20:20:24 +00001490 // If the init list is shorter than the array length, set the
1491 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001492 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001493 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001494
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001495 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001496}
1497
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001498Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1499 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Ted Kremenek67f28532009-06-17 22:02:04 +00001501 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001502 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Zhongxing Xu018220c2010-08-11 06:10:55 +00001504 QualType T = R->getValueType();
Douglas Gregorfb87b892010-04-26 21:31:17 +00001505 assert(T->isStructureOrClassType());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001506
Ted Kremenek6217b802009-07-29 21:53:49 +00001507 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001508 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001509
1510 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001511 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001512
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001513 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001514 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001515 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001517 // We may get non-CompoundVal accidentally due to imprecise cast logic or
1518 // that we are binding symbolic struct value. Kill the field values, and if
1519 // the value is symbolic go and bind it as a "default" binding.
Ted Kremenek4af473c2010-08-17 23:29:06 +00001520 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V)) {
1521 SVal SV = isa<nonloc::SymbolVal>(V) ? V : UnknownVal();
1522 return KillStruct(store, R, SV);
1523 }
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001524
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001525 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001526 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001527
1528 RecordDecl::field_iterator FI, FE;
1529
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001530 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001531
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001532 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001533 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001534
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001535 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001536 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001537
Ted Kremenekcf549592009-09-22 21:19:14 +00001538 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001539 store = BindArray(store, FR, *VI);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001540 else if (FTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001541 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001542 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001543 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001544 }
1545
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001546 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001547 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001548 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001549 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001550 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001551 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001552
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001553 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001554}
1555
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001556Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R,
1557 SVal DefaultVal) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001558 RegionBindings B = GetRegionBindings(store);
1559 llvm::OwningPtr<RegionStoreSubRegionMap>
1560 SubRegions(getRegionStoreSubRegionMap(store));
1561 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001562
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001563 // Set the default value of the struct region to "unknown".
Ted Kremenek281e9dc2010-07-29 00:28:47 +00001564 return Add(B, R, BindingKey::Default, DefaultVal).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001565}
1566
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001567Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1568 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001569
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001570 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001571 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001572
Mike Stump1eb44332009-09-09 15:08:12 +00001573 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001574 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001575
Mike Stump1eb44332009-09-09 15:08:12 +00001576 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001577 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001579 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1580 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001581 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001582}
1583
1584//===----------------------------------------------------------------------===//
1585// "Raw" retrievals and bindings.
1586//===----------------------------------------------------------------------===//
1587
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001588BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001589 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001590 const RegionRawOffset &O = ER->getAsArrayOffset();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001591
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001592 // FIXME: There are some ElementRegions for which we cannot compute
Jordy Rosee7011172010-08-16 01:15:17 +00001593 // raw offsets yet, including regions with symbolic offsets. These will be
1594 // ignored by the store.
1595 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001596 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001597
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001598 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001599}
1600
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001601RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001602 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001603 return B;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001604 return RBFactory.Add(B, K, V);
1605}
1606
1607RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001608 BindingKey::Kind k, SVal V) {
1609 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001610}
1611
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001612const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001613 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001614 return NULL;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001615 return B.lookup(K);
1616}
1617
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001618const SVal *RegionStoreManager::Lookup(RegionBindings B,
1619 const MemRegion *R,
1620 BindingKey::Kind k) {
1621 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001622}
1623
1624RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
Jordy Rosed5addb42010-08-16 20:53:01 +00001625 if (!K.isValid())
Jordy Rosee7011172010-08-16 01:15:17 +00001626 return B;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001627 return RBFactory.Remove(B, K);
1628}
1629
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001630RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1631 BindingKey::Kind k){
1632 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001633}
1634
Ted Kremenek9af46f52009-06-16 22:36:44 +00001635//===----------------------------------------------------------------------===//
1636// State pruning.
1637//===----------------------------------------------------------------------===//
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001638
Ted Kremenek5499b842010-03-10 16:32:56 +00001639namespace {
1640class RemoveDeadBindingsWorker :
1641 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1642 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1643 SymbolReaper &SymReaper;
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001644 const StackFrameContext *CurrentLCtx;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001645
Ted Kremenek5499b842010-03-10 16:32:56 +00001646public:
1647 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1648 RegionBindings b, SymbolReaper &symReaper,
Jordy Rose7dadf792010-07-01 20:09:55 +00001649 const StackFrameContext *LCtx)
Ted Kremenek5499b842010-03-10 16:32:56 +00001650 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
Jordy Rose7dadf792010-07-01 20:09:55 +00001651 SymReaper(symReaper), CurrentLCtx(LCtx) {}
Ted Kremenek5499b842010-03-10 16:32:56 +00001652
1653 // Called by ClusterAnalysis.
1654 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1655 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek5499b842010-03-10 16:32:56 +00001656
Ted Kremenek75a2d942010-04-01 00:15:55 +00001657 void VisitBindingKey(BindingKey K);
Ted Kremenek5499b842010-03-10 16:32:56 +00001658 bool UpdatePostponed();
1659 void VisitBinding(SVal V);
1660};
1661}
1662
1663void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1664 RegionCluster &C) {
1665
1666 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
Jordy Rose7dadf792010-07-01 20:09:55 +00001667 if (SymReaper.isLive(VR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001668 AddToWorkList(baseR, C);
1669
1670 return;
1671 }
1672
1673 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1674 if (SymReaper.isLive(SR->getSymbol()))
1675 AddToWorkList(SR, C);
1676 else
1677 Postponed.push_back(SR);
1678
1679 return;
1680 }
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001681
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001682 if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
1683 AddToWorkList(baseR, C);
1684 return;
1685 }
1686
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001687 // CXXThisRegion in the current or parent location context is live.
1688 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001689 const StackArgumentsSpaceRegion *StackReg =
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001690 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
1691 const StackFrameContext *RegCtx = StackReg->getStackFrame();
1692 if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
1693 AddToWorkList(TR, C);
1694 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001695}
1696
1697void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1698 BindingKey *I, BindingKey *E) {
Ted Kremenek75a2d942010-04-01 00:15:55 +00001699 for ( ; I != E; ++I)
1700 VisitBindingKey(*I);
Ted Kremenek5499b842010-03-10 16:32:56 +00001701}
1702
1703void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1704 // Is it a LazyCompoundVal? All referenced regions are live as well.
1705 if (const nonloc::LazyCompoundVal *LCS =
1706 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1707
1708 const MemRegion *LazyR = LCS->getRegion();
1709 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1710 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenek0ea0e8b2010-07-06 23:53:29 +00001711 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
1712 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001713 VisitBinding(RI.getData());
1714 }
1715 return;
1716 }
1717
1718 // If V is a region, then add it to the worklist.
1719 if (const MemRegion *R = V.getAsRegion())
1720 AddToWorkList(R);
1721
1722 // Update the set of live symbols.
1723 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1724 SI!=SE;++SI)
1725 SymReaper.markLive(*SI);
1726}
1727
Ted Kremenek75a2d942010-04-01 00:15:55 +00001728void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) {
1729 const MemRegion *R = K.getRegion();
1730
Ted Kremenek5499b842010-03-10 16:32:56 +00001731 // Mark this region "live" by adding it to the worklist. This will cause
1732 // use to visit all regions in the cluster (if we haven't visited them
1733 // already).
Ted Kremenek75a2d942010-04-01 00:15:55 +00001734 if (AddToWorkList(R)) {
1735 // Mark the symbol for any live SymbolicRegion as "live". This means we
1736 // should continue to track that symbol.
1737 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1738 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek5499b842010-03-10 16:32:56 +00001739
Ted Kremenek75a2d942010-04-01 00:15:55 +00001740 // For BlockDataRegions, enqueue the VarRegions for variables marked
1741 // with __block (passed-by-reference).
1742 // via BlockDeclRefExprs.
1743 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1744 for (BlockDataRegion::referenced_vars_iterator
1745 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1746 RI != RE; ++RI) {
1747 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1748 AddToWorkList(*RI);
1749 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001750
Ted Kremenek75a2d942010-04-01 00:15:55 +00001751 // No possible data bindings on a BlockDataRegion.
1752 return;
Ted Kremenek5499b842010-03-10 16:32:56 +00001753 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001754 }
1755
Ted Kremenek75a2d942010-04-01 00:15:55 +00001756 // Visit the data binding for K.
1757 if (const SVal *V = RM.Lookup(B, K))
Ted Kremenek5499b842010-03-10 16:32:56 +00001758 VisitBinding(*V);
1759}
1760
1761bool RemoveDeadBindingsWorker::UpdatePostponed() {
1762 // See if any postponed SymbolicRegions are actually live now, after
1763 // having done a scan.
1764 bool changed = false;
1765
1766 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1767 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1768 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1769 if (SymReaper.isLive(SR->getSymbol())) {
1770 changed |= AddToWorkList(SR);
1771 *I = NULL;
1772 }
1773 }
1774 }
1775
1776 return changed;
1777}
1778
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001779Store RegionStoreManager::RemoveDeadBindings(Store store,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001780 const StackFrameContext *LCtx,
Zhongxing Xu72119c42010-02-05 05:34:29 +00001781 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001782 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001783{
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001784 RegionBindings B = GetRegionBindings(store);
Jordy Rose7dadf792010-07-01 20:09:55 +00001785 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
Ted Kremenek5499b842010-03-10 16:32:56 +00001786 W.GenerateClusters();
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Ted Kremenek5499b842010-03-10 16:32:56 +00001788 // Enqueue the region roots onto the worklist.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001789 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenek5499b842010-03-10 16:32:56 +00001790 E=RegionRoots.end(); I!=E; ++I)
1791 W.AddToWorkList(*I);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001792
Ted Kremenek5499b842010-03-10 16:32:56 +00001793 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001794
Ted Kremenek9af46f52009-06-16 22:36:44 +00001795 // We have now scanned the store, marking reachable regions and symbols
1796 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001797 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001798 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek5499b842010-03-10 16:32:56 +00001799 const BindingKey &K = I.getKey();
1800
Ted Kremenekb7118f72010-03-10 16:38:41 +00001801 // If the cluster has been visited, we know the region has been marked.
Ted Kremenek5499b842010-03-10 16:32:56 +00001802 if (W.isVisited(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001803 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Ted Kremenek5499b842010-03-10 16:32:56 +00001805 // Remove the dead entry.
1806 B = Remove(B, K);
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Ted Kremenek5499b842010-03-10 16:32:56 +00001808 // Mark all non-live symbols that this binding references as dead.
1809 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001810 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001812 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001813 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1814 for (; SI != SE; ++SI)
1815 SymReaper.maybeDead(*SI);
1816 }
Zhongxing Xu5e4cb342010-08-15 12:45:09 +00001817
1818 return B.getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001819}
1820
Ted Kremenek5499b842010-03-10 16:32:56 +00001821
Jordy Roseff59efd2010-08-03 20:44:35 +00001822Store RegionStoreManager::EnterStackFrame(const GRState *state,
1823 const StackFrameContext *frame) {
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001824 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001825 FunctionDecl::param_const_iterator PI = FD->param_begin();
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001826 Store store = state->getStore();
Zhongxing Xuc5063572010-03-16 13:14:16 +00001827
1828 if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) {
1829 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1830
1831 // Copy the arg expression value to the arg variables.
1832 for (; AI != AE; ++AI, ++PI) {
1833 SVal ArgVal = state->getSVal(*AI);
1834 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1835 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001836 } else if (const CXXConstructExpr *CE =
Zhongxing Xuc5063572010-03-16 13:14:16 +00001837 dyn_cast<CXXConstructExpr>(frame->getCallSite())) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001838 CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(),
Zhongxing Xuc5063572010-03-16 13:14:16 +00001839 AE = CE->arg_end();
1840
1841 // Copy the arg expression value to the arg variables.
1842 for (; AI != AE; ++AI, ++PI) {
1843 SVal ArgVal = state->getSVal(*AI);
1844 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1845 }
1846 } else
Jordy Roseff59efd2010-08-03 20:44:35 +00001847 llvm_unreachable("Unhandled call expression.");
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001848
Jordy Roseff59efd2010-08-03 20:44:35 +00001849 return store;
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001850}
1851
Ted Kremenek9af46f52009-06-16 22:36:44 +00001852//===----------------------------------------------------------------------===//
1853// Utility methods.
1854//===----------------------------------------------------------------------===//
1855
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001856void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001857 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001858 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001859 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Ted Kremenek451ac092009-08-06 04:50:20 +00001861 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001862 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001863}